1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
45 #include "complaints.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
55 #include "typeprint.h"
58 #include "completer.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
69 #include "filestuff.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "common/hash_enum.h"
78 #include "filename-seen-cache.h"
81 #include <sys/types.h>
83 #include <unordered_set>
84 #include <unordered_map>
88 #include <forward_list>
90 typedef struct symbol *symbolp;
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
98 /* When non-zero, dump DIEs after they are read in. */
99 static unsigned int dwarf_die_debug = 0;
101 /* When non-zero, dump line number entries as they are read in. */
102 static unsigned int dwarf_line_debug = 0;
104 /* When non-zero, cross-check physname against demangler. */
105 static int check_physname = 0;
107 /* When non-zero, do not reject deprecated .gdb_index sections. */
108 static int use_deprecated_index_sections = 0;
110 static const struct objfile_data *dwarf2_objfile_data_key;
112 /* The "aclass" indices for various kinds of computed DWARF symbols. */
114 static int dwarf2_locexpr_index;
115 static int dwarf2_loclist_index;
116 static int dwarf2_locexpr_block_index;
117 static int dwarf2_loclist_block_index;
119 /* A descriptor for dwarf sections.
121 S.ASECTION, SIZE are typically initialized when the objfile is first
122 scanned. BUFFER, READIN are filled in later when the section is read.
123 If the section contained compressed data then SIZE is updated to record
124 the uncompressed size of the section.
126 DWP file format V2 introduces a wrinkle that is easiest to handle by
127 creating the concept of virtual sections contained within a real section.
128 In DWP V2 the sections of the input DWO files are concatenated together
129 into one section, but section offsets are kept relative to the original
131 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
132 the real section this "virtual" section is contained in, and BUFFER,SIZE
133 describe the virtual section. */
135 struct dwarf2_section_info
139 /* If this is a real section, the bfd section. */
141 /* If this is a virtual section, pointer to the containing ("real")
143 struct dwarf2_section_info *containing_section;
145 /* Pointer to section data, only valid if readin. */
146 const gdb_byte *buffer;
147 /* The size of the section, real or virtual. */
149 /* If this is a virtual section, the offset in the real section.
150 Only valid if is_virtual. */
151 bfd_size_type virtual_offset;
152 /* True if we have tried to read this section. */
154 /* True if this is a virtual section, False otherwise.
155 This specifies which of s.section and s.containing_section to use. */
159 typedef struct dwarf2_section_info dwarf2_section_info_def;
160 DEF_VEC_O (dwarf2_section_info_def);
162 /* All offsets in the index are of this type. It must be
163 architecture-independent. */
164 typedef uint32_t offset_type;
166 DEF_VEC_I (offset_type);
168 /* Ensure only legit values are used. */
169 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
171 gdb_assert ((unsigned int) (value) <= 1); \
172 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
175 /* Ensure only legit values are used. */
176 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
178 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
179 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
180 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
183 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
184 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
186 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
187 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
192 /* Convert VALUE between big- and little-endian. */
195 byte_swap (offset_type value)
199 result = (value & 0xff) << 24;
200 result |= (value & 0xff00) << 8;
201 result |= (value & 0xff0000) >> 8;
202 result |= (value & 0xff000000) >> 24;
206 #define MAYBE_SWAP(V) byte_swap (V)
209 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
210 #endif /* WORDS_BIGENDIAN */
212 /* An index into a (C++) symbol name component in a symbol name as
213 recorded in the mapped_index's symbol table. For each C++ symbol
214 in the symbol table, we record one entry for the start of each
215 component in the symbol in a table of name components, and then
216 sort the table, in order to be able to binary search symbol names,
217 ignoring leading namespaces, both completion and regular look up.
218 For example, for symbol "A::B::C", we'll have an entry that points
219 to "A::B::C", another that points to "B::C", and another for "C".
220 Note that function symbols in GDB index have no parameter
221 information, just the function/method names. You can convert a
222 name_component to a "const char *" using the
223 'mapped_index::symbol_name_at(offset_type)' method. */
225 struct name_component
227 /* Offset in the symbol name where the component starts. Stored as
228 a (32-bit) offset instead of a pointer to save memory and improve
229 locality on 64-bit architectures. */
230 offset_type name_offset;
232 /* The symbol's index in the symbol and constant pool tables of a
237 /* Base class containing bits shared by both .gdb_index and
238 .debug_name indexes. */
240 struct mapped_index_base
242 /* The name_component table (a sorted vector). See name_component's
243 description above. */
244 std::vector<name_component> name_components;
246 /* How NAME_COMPONENTS is sorted. */
247 enum case_sensitivity name_components_casing;
249 /* Return the number of names in the symbol table. */
250 virtual size_t symbol_name_count () const = 0;
252 /* Get the name of the symbol at IDX in the symbol table. */
253 virtual const char *symbol_name_at (offset_type idx) const = 0;
255 /* Return whether the name at IDX in the symbol table should be
257 virtual bool symbol_name_slot_invalid (offset_type idx) const
262 /* Build the symbol name component sorted vector, if we haven't
264 void build_name_components ();
266 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
267 possible matches for LN_NO_PARAMS in the name component
269 std::pair<std::vector<name_component>::const_iterator,
270 std::vector<name_component>::const_iterator>
271 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
273 /* Prevent deleting/destroying via a base class pointer. */
275 ~mapped_index_base() = default;
278 /* A description of the mapped index. The file format is described in
279 a comment by the code that writes the index. */
280 struct mapped_index final : public mapped_index_base
282 /* A slot/bucket in the symbol table hash. */
283 struct symbol_table_slot
285 const offset_type name;
286 const offset_type vec;
289 /* Index data format version. */
292 /* The total length of the buffer. */
295 /* The address table data. */
296 gdb::array_view<const gdb_byte> address_table;
298 /* The symbol table, implemented as a hash table. */
299 gdb::array_view<symbol_table_slot> symbol_table;
301 /* A pointer to the constant pool. */
302 const char *constant_pool;
304 bool symbol_name_slot_invalid (offset_type idx) const override
306 const auto &bucket = this->symbol_table[idx];
307 return bucket.name == 0 && bucket.vec;
310 /* Convenience method to get at the name of the symbol at IDX in the
312 const char *symbol_name_at (offset_type idx) const override
313 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
315 size_t symbol_name_count () const override
316 { return this->symbol_table.size (); }
319 /* A description of the mapped .debug_names.
320 Uninitialized map has CU_COUNT 0. */
321 struct mapped_debug_names final : public mapped_index_base
323 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
324 : dwarf2_per_objfile (dwarf2_per_objfile_)
327 struct dwarf2_per_objfile *dwarf2_per_objfile;
328 bfd_endian dwarf5_byte_order;
329 bool dwarf5_is_dwarf64;
330 bool augmentation_is_gdb;
332 uint32_t cu_count = 0;
333 uint32_t tu_count, bucket_count, name_count;
334 const gdb_byte *cu_table_reordered, *tu_table_reordered;
335 const uint32_t *bucket_table_reordered, *hash_table_reordered;
336 const gdb_byte *name_table_string_offs_reordered;
337 const gdb_byte *name_table_entry_offs_reordered;
338 const gdb_byte *entry_pool;
345 /* Attribute name DW_IDX_*. */
348 /* Attribute form DW_FORM_*. */
351 /* Value if FORM is DW_FORM_implicit_const. */
352 LONGEST implicit_const;
354 std::vector<attr> attr_vec;
357 std::unordered_map<ULONGEST, index_val> abbrev_map;
359 const char *namei_to_name (uint32_t namei) const;
361 /* Implementation of the mapped_index_base virtual interface, for
362 the name_components cache. */
364 const char *symbol_name_at (offset_type idx) const override
365 { return namei_to_name (idx); }
367 size_t symbol_name_count () const override
368 { return this->name_count; }
371 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
372 DEF_VEC_P (dwarf2_per_cu_ptr);
376 int nr_uniq_abbrev_tables;
378 int nr_symtab_sharers;
379 int nr_stmt_less_type_units;
380 int nr_all_type_units_reallocs;
383 /* Collection of data recorded per objfile.
384 This hangs off of dwarf2_objfile_data_key. */
386 struct dwarf2_per_objfile
388 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
389 dwarf2 section names, or is NULL if the standard ELF names are
391 dwarf2_per_objfile (struct objfile *objfile,
392 const dwarf2_debug_sections *names);
394 ~dwarf2_per_objfile ();
396 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
398 /* Free all cached compilation units. */
399 void free_cached_comp_units ();
401 /* This function is mapped across the sections and remembers the
402 offset and size of each of the debugging sections we are
404 void locate_sections (bfd *abfd, asection *sectp,
405 const dwarf2_debug_sections &names);
408 dwarf2_section_info info {};
409 dwarf2_section_info abbrev {};
410 dwarf2_section_info line {};
411 dwarf2_section_info loc {};
412 dwarf2_section_info loclists {};
413 dwarf2_section_info macinfo {};
414 dwarf2_section_info macro {};
415 dwarf2_section_info str {};
416 dwarf2_section_info line_str {};
417 dwarf2_section_info ranges {};
418 dwarf2_section_info rnglists {};
419 dwarf2_section_info addr {};
420 dwarf2_section_info frame {};
421 dwarf2_section_info eh_frame {};
422 dwarf2_section_info gdb_index {};
423 dwarf2_section_info debug_names {};
424 dwarf2_section_info debug_aranges {};
426 VEC (dwarf2_section_info_def) *types = NULL;
429 struct objfile *objfile = NULL;
431 /* Table of all the compilation units. This is used to locate
432 the target compilation unit of a particular reference. */
433 struct dwarf2_per_cu_data **all_comp_units = NULL;
435 /* The number of compilation units in ALL_COMP_UNITS. */
436 int n_comp_units = 0;
438 /* The number of .debug_types-related CUs. */
439 int n_type_units = 0;
441 /* The number of elements allocated in all_type_units.
442 If there are skeleton-less TUs, we add them to all_type_units lazily. */
443 int n_allocated_type_units = 0;
445 /* The .debug_types-related CUs (TUs).
446 This is stored in malloc space because we may realloc it. */
447 struct signatured_type **all_type_units = NULL;
449 /* Table of struct type_unit_group objects.
450 The hash key is the DW_AT_stmt_list value. */
451 htab_t type_unit_groups {};
453 /* A table mapping .debug_types signatures to its signatured_type entry.
454 This is NULL if the .debug_types section hasn't been read in yet. */
455 htab_t signatured_types {};
457 /* Type unit statistics, to see how well the scaling improvements
459 struct tu_stats tu_stats {};
461 /* A chain of compilation units that are currently read in, so that
462 they can be freed later. */
463 dwarf2_per_cu_data *read_in_chain = NULL;
465 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
466 This is NULL if the table hasn't been allocated yet. */
469 /* True if we've checked for whether there is a DWP file. */
470 bool dwp_checked = false;
472 /* The DWP file if there is one, or NULL. */
473 struct dwp_file *dwp_file = NULL;
475 /* The shared '.dwz' file, if one exists. This is used when the
476 original data was compressed using 'dwz -m'. */
477 struct dwz_file *dwz_file = NULL;
479 /* A flag indicating whether this objfile has a section loaded at a
481 bool has_section_at_zero = false;
483 /* True if we are using the mapped index,
484 or we are faking it for OBJF_READNOW's sake. */
485 bool using_index = false;
487 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
488 mapped_index *index_table = NULL;
490 /* The mapped index, or NULL if .debug_names is missing or not being used. */
491 std::unique_ptr<mapped_debug_names> debug_names_table;
493 /* When using index_table, this keeps track of all quick_file_names entries.
494 TUs typically share line table entries with a CU, so we maintain a
495 separate table of all line table entries to support the sharing.
496 Note that while there can be way more TUs than CUs, we've already
497 sorted all the TUs into "type unit groups", grouped by their
498 DW_AT_stmt_list value. Therefore the only sharing done here is with a
499 CU and its associated TU group if there is one. */
500 htab_t quick_file_names_table {};
502 /* Set during partial symbol reading, to prevent queueing of full
504 bool reading_partial_symbols = false;
506 /* Table mapping type DIEs to their struct type *.
507 This is NULL if not allocated yet.
508 The mapping is done via (CU/TU + DIE offset) -> type. */
509 htab_t die_type_hash {};
511 /* The CUs we recently read. */
512 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
514 /* Table containing line_header indexed by offset and offset_in_dwz. */
515 htab_t line_header_hash {};
517 /* Table containing all filenames. This is an optional because the
518 table is lazily constructed on first access. */
519 gdb::optional<filename_seen_cache> filenames_cache;
522 /* Get the dwarf2_per_objfile associated to OBJFILE. */
524 struct dwarf2_per_objfile *
525 get_dwarf2_per_objfile (struct objfile *objfile)
527 return ((struct dwarf2_per_objfile *)
528 objfile_data (objfile, dwarf2_objfile_data_key));
531 /* Set the dwarf2_per_objfile associated to OBJFILE. */
534 set_dwarf2_per_objfile (struct objfile *objfile,
535 struct dwarf2_per_objfile *dwarf2_per_objfile)
537 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
538 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
541 /* Default names of the debugging sections. */
543 /* Note that if the debugging section has been compressed, it might
544 have a name like .zdebug_info. */
546 static const struct dwarf2_debug_sections dwarf2_elf_names =
548 { ".debug_info", ".zdebug_info" },
549 { ".debug_abbrev", ".zdebug_abbrev" },
550 { ".debug_line", ".zdebug_line" },
551 { ".debug_loc", ".zdebug_loc" },
552 { ".debug_loclists", ".zdebug_loclists" },
553 { ".debug_macinfo", ".zdebug_macinfo" },
554 { ".debug_macro", ".zdebug_macro" },
555 { ".debug_str", ".zdebug_str" },
556 { ".debug_line_str", ".zdebug_line_str" },
557 { ".debug_ranges", ".zdebug_ranges" },
558 { ".debug_rnglists", ".zdebug_rnglists" },
559 { ".debug_types", ".zdebug_types" },
560 { ".debug_addr", ".zdebug_addr" },
561 { ".debug_frame", ".zdebug_frame" },
562 { ".eh_frame", NULL },
563 { ".gdb_index", ".zgdb_index" },
564 { ".debug_names", ".zdebug_names" },
565 { ".debug_aranges", ".zdebug_aranges" },
569 /* List of DWO/DWP sections. */
571 static const struct dwop_section_names
573 struct dwarf2_section_names abbrev_dwo;
574 struct dwarf2_section_names info_dwo;
575 struct dwarf2_section_names line_dwo;
576 struct dwarf2_section_names loc_dwo;
577 struct dwarf2_section_names loclists_dwo;
578 struct dwarf2_section_names macinfo_dwo;
579 struct dwarf2_section_names macro_dwo;
580 struct dwarf2_section_names str_dwo;
581 struct dwarf2_section_names str_offsets_dwo;
582 struct dwarf2_section_names types_dwo;
583 struct dwarf2_section_names cu_index;
584 struct dwarf2_section_names tu_index;
588 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
589 { ".debug_info.dwo", ".zdebug_info.dwo" },
590 { ".debug_line.dwo", ".zdebug_line.dwo" },
591 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
592 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
593 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
594 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
595 { ".debug_str.dwo", ".zdebug_str.dwo" },
596 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
597 { ".debug_types.dwo", ".zdebug_types.dwo" },
598 { ".debug_cu_index", ".zdebug_cu_index" },
599 { ".debug_tu_index", ".zdebug_tu_index" },
602 /* local data types */
604 /* The data in a compilation unit header, after target2host
605 translation, looks like this. */
606 struct comp_unit_head
610 unsigned char addr_size;
611 unsigned char signed_addr_p;
612 sect_offset abbrev_sect_off;
614 /* Size of file offsets; either 4 or 8. */
615 unsigned int offset_size;
617 /* Size of the length field; either 4 or 12. */
618 unsigned int initial_length_size;
620 enum dwarf_unit_type unit_type;
622 /* Offset to the first byte of this compilation unit header in the
623 .debug_info section, for resolving relative reference dies. */
624 sect_offset sect_off;
626 /* Offset to first die in this cu from the start of the cu.
627 This will be the first byte following the compilation unit header. */
628 cu_offset first_die_cu_offset;
630 /* 64-bit signature of this type unit - it is valid only for
631 UNIT_TYPE DW_UT_type. */
634 /* For types, offset in the type's DIE of the type defined by this TU. */
635 cu_offset type_cu_offset_in_tu;
638 /* Type used for delaying computation of method physnames.
639 See comments for compute_delayed_physnames. */
640 struct delayed_method_info
642 /* The type to which the method is attached, i.e., its parent class. */
645 /* The index of the method in the type's function fieldlists. */
648 /* The index of the method in the fieldlist. */
651 /* The name of the DIE. */
654 /* The DIE associated with this method. */
655 struct die_info *die;
658 typedef struct delayed_method_info delayed_method_info;
659 DEF_VEC_O (delayed_method_info);
661 /* Internal state when decoding a particular compilation unit. */
664 /* The header of the compilation unit. */
665 struct comp_unit_head header;
667 /* Base address of this compilation unit. */
668 CORE_ADDR base_address;
670 /* Non-zero if base_address has been set. */
673 /* The language we are debugging. */
674 enum language language;
675 const struct language_defn *language_defn;
677 const char *producer;
679 /* The generic symbol table building routines have separate lists for
680 file scope symbols and all all other scopes (local scopes). So
681 we need to select the right one to pass to add_symbol_to_list().
682 We do it by keeping a pointer to the correct list in list_in_scope.
684 FIXME: The original dwarf code just treated the file scope as the
685 first local scope, and all other local scopes as nested local
686 scopes, and worked fine. Check to see if we really need to
687 distinguish these in buildsym.c. */
688 struct pending **list_in_scope;
690 /* The abbrev table for this CU.
691 Normally this points to the abbrev table in the objfile.
692 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
693 struct abbrev_table *abbrev_table;
695 /* Hash table holding all the loaded partial DIEs
696 with partial_die->offset.SECT_OFF as hash. */
699 /* Storage for things with the same lifetime as this read-in compilation
700 unit, including partial DIEs. */
701 struct obstack comp_unit_obstack;
703 /* When multiple dwarf2_cu structures are living in memory, this field
704 chains them all together, so that they can be released efficiently.
705 We will probably also want a generation counter so that most-recently-used
706 compilation units are cached... */
707 struct dwarf2_per_cu_data *read_in_chain;
709 /* Backlink to our per_cu entry. */
710 struct dwarf2_per_cu_data *per_cu;
712 /* How many compilation units ago was this CU last referenced? */
715 /* A hash table of DIE cu_offset for following references with
716 die_info->offset.sect_off as hash. */
719 /* Full DIEs if read in. */
720 struct die_info *dies;
722 /* A set of pointers to dwarf2_per_cu_data objects for compilation
723 units referenced by this one. Only set during full symbol processing;
724 partial symbol tables do not have dependencies. */
727 /* Header data from the line table, during full symbol processing. */
728 struct line_header *line_header;
729 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
730 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
731 this is the DW_TAG_compile_unit die for this CU. We'll hold on
732 to the line header as long as this DIE is being processed. See
733 process_die_scope. */
734 die_info *line_header_die_owner;
736 /* A list of methods which need to have physnames computed
737 after all type information has been read. */
738 VEC (delayed_method_info) *method_list;
740 /* To be copied to symtab->call_site_htab. */
741 htab_t call_site_htab;
743 /* Non-NULL if this CU came from a DWO file.
744 There is an invariant here that is important to remember:
745 Except for attributes copied from the top level DIE in the "main"
746 (or "stub") file in preparation for reading the DWO file
747 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
748 Either there isn't a DWO file (in which case this is NULL and the point
749 is moot), or there is and either we're not going to read it (in which
750 case this is NULL) or there is and we are reading it (in which case this
752 struct dwo_unit *dwo_unit;
754 /* The DW_AT_addr_base attribute if present, zero otherwise
755 (zero is a valid value though).
756 Note this value comes from the Fission stub CU/TU's DIE. */
759 /* The DW_AT_ranges_base attribute if present, zero otherwise
760 (zero is a valid value though).
761 Note this value comes from the Fission stub CU/TU's DIE.
762 Also note that the value is zero in the non-DWO case so this value can
763 be used without needing to know whether DWO files are in use or not.
764 N.B. This does not apply to DW_AT_ranges appearing in
765 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
766 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
767 DW_AT_ranges_base *would* have to be applied, and we'd have to care
768 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
769 ULONGEST ranges_base;
771 /* Mark used when releasing cached dies. */
772 unsigned int mark : 1;
774 /* This CU references .debug_loc. See the symtab->locations_valid field.
775 This test is imperfect as there may exist optimized debug code not using
776 any location list and still facing inlining issues if handled as
777 unoptimized code. For a future better test see GCC PR other/32998. */
778 unsigned int has_loclist : 1;
780 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
781 if all the producer_is_* fields are valid. This information is cached
782 because profiling CU expansion showed excessive time spent in
783 producer_is_gxx_lt_4_6. */
784 unsigned int checked_producer : 1;
785 unsigned int producer_is_gxx_lt_4_6 : 1;
786 unsigned int producer_is_gcc_lt_4_3 : 1;
787 unsigned int producer_is_icc_lt_14 : 1;
789 /* When set, the file that we're processing is known to have
790 debugging info for C++ namespaces. GCC 3.3.x did not produce
791 this information, but later versions do. */
793 unsigned int processing_has_namespace_info : 1;
796 /* Persistent data held for a compilation unit, even when not
797 processing it. We put a pointer to this structure in the
798 read_symtab_private field of the psymtab. */
800 struct dwarf2_per_cu_data
802 /* The start offset and length of this compilation unit.
803 NOTE: Unlike comp_unit_head.length, this length includes
805 If the DIE refers to a DWO file, this is always of the original die,
807 sect_offset sect_off;
810 /* DWARF standard version this data has been read from (such as 4 or 5). */
813 /* Flag indicating this compilation unit will be read in before
814 any of the current compilation units are processed. */
815 unsigned int queued : 1;
817 /* This flag will be set when reading partial DIEs if we need to load
818 absolutely all DIEs for this compilation unit, instead of just the ones
819 we think are interesting. It gets set if we look for a DIE in the
820 hash table and don't find it. */
821 unsigned int load_all_dies : 1;
823 /* Non-zero if this CU is from .debug_types.
824 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
826 unsigned int is_debug_types : 1;
828 /* Non-zero if this CU is from the .dwz file. */
829 unsigned int is_dwz : 1;
831 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
832 This flag is only valid if is_debug_types is true.
833 We can't read a CU directly from a DWO file: There are required
834 attributes in the stub. */
835 unsigned int reading_dwo_directly : 1;
837 /* Non-zero if the TU has been read.
838 This is used to assist the "Stay in DWO Optimization" for Fission:
839 When reading a DWO, it's faster to read TUs from the DWO instead of
840 fetching them from random other DWOs (due to comdat folding).
841 If the TU has already been read, the optimization is unnecessary
842 (and unwise - we don't want to change where gdb thinks the TU lives
844 This flag is only valid if is_debug_types is true. */
845 unsigned int tu_read : 1;
847 /* The section this CU/TU lives in.
848 If the DIE refers to a DWO file, this is always the original die,
850 struct dwarf2_section_info *section;
852 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
853 of the CU cache it gets reset to NULL again. This is left as NULL for
854 dummy CUs (a CU header, but nothing else). */
855 struct dwarf2_cu *cu;
857 /* The corresponding dwarf2_per_objfile. */
858 struct dwarf2_per_objfile *dwarf2_per_objfile;
860 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
861 is active. Otherwise, the 'psymtab' field is active. */
864 /* The partial symbol table associated with this compilation unit,
865 or NULL for unread partial units. */
866 struct partial_symtab *psymtab;
868 /* Data needed by the "quick" functions. */
869 struct dwarf2_per_cu_quick_data *quick;
872 /* The CUs we import using DW_TAG_imported_unit. This is filled in
873 while reading psymtabs, used to compute the psymtab dependencies,
874 and then cleared. Then it is filled in again while reading full
875 symbols, and only deleted when the objfile is destroyed.
877 This is also used to work around a difference between the way gold
878 generates .gdb_index version <=7 and the way gdb does. Arguably this
879 is a gold bug. For symbols coming from TUs, gold records in the index
880 the CU that includes the TU instead of the TU itself. This breaks
881 dw2_lookup_symbol: It assumes that if the index says symbol X lives
882 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
883 will find X. Alas TUs live in their own symtab, so after expanding CU Y
884 we need to look in TU Z to find X. Fortunately, this is akin to
885 DW_TAG_imported_unit, so we just use the same mechanism: For
886 .gdb_index version <=7 this also records the TUs that the CU referred
887 to. Concurrently with this change gdb was modified to emit version 8
888 indices so we only pay a price for gold generated indices.
889 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
890 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
893 /* Entry in the signatured_types hash table. */
895 struct signatured_type
897 /* The "per_cu" object of this type.
898 This struct is used iff per_cu.is_debug_types.
899 N.B.: This is the first member so that it's easy to convert pointers
901 struct dwarf2_per_cu_data per_cu;
903 /* The type's signature. */
906 /* Offset in the TU of the type's DIE, as read from the TU header.
907 If this TU is a DWO stub and the definition lives in a DWO file
908 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
909 cu_offset type_offset_in_tu;
911 /* Offset in the section of the type's DIE.
912 If the definition lives in a DWO file, this is the offset in the
913 .debug_types.dwo section.
914 The value is zero until the actual value is known.
915 Zero is otherwise not a valid section offset. */
916 sect_offset type_offset_in_section;
918 /* Type units are grouped by their DW_AT_stmt_list entry so that they
919 can share them. This points to the containing symtab. */
920 struct type_unit_group *type_unit_group;
923 The first time we encounter this type we fully read it in and install it
924 in the symbol tables. Subsequent times we only need the type. */
927 /* Containing DWO unit.
928 This field is valid iff per_cu.reading_dwo_directly. */
929 struct dwo_unit *dwo_unit;
932 typedef struct signatured_type *sig_type_ptr;
933 DEF_VEC_P (sig_type_ptr);
935 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
936 This includes type_unit_group and quick_file_names. */
938 struct stmt_list_hash
940 /* The DWO unit this table is from or NULL if there is none. */
941 struct dwo_unit *dwo_unit;
943 /* Offset in .debug_line or .debug_line.dwo. */
944 sect_offset line_sect_off;
947 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
948 an object of this type. */
950 struct type_unit_group
952 /* dwarf2read.c's main "handle" on a TU symtab.
953 To simplify things we create an artificial CU that "includes" all the
954 type units using this stmt_list so that the rest of the code still has
955 a "per_cu" handle on the symtab.
956 This PER_CU is recognized by having no section. */
957 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
958 struct dwarf2_per_cu_data per_cu;
960 /* The TUs that share this DW_AT_stmt_list entry.
961 This is added to while parsing type units to build partial symtabs,
962 and is deleted afterwards and not used again. */
963 VEC (sig_type_ptr) *tus;
965 /* The compunit symtab.
966 Type units in a group needn't all be defined in the same source file,
967 so we create an essentially anonymous symtab as the compunit symtab. */
968 struct compunit_symtab *compunit_symtab;
970 /* The data used to construct the hash key. */
971 struct stmt_list_hash hash;
973 /* The number of symtabs from the line header.
974 The value here must match line_header.num_file_names. */
975 unsigned int num_symtabs;
977 /* The symbol tables for this TU (obtained from the files listed in
979 WARNING: The order of entries here must match the order of entries
980 in the line header. After the first TU using this type_unit_group, the
981 line header for the subsequent TUs is recreated from this. This is done
982 because we need to use the same symtabs for each TU using the same
983 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
984 there's no guarantee the line header doesn't have duplicate entries. */
985 struct symtab **symtabs;
988 /* These sections are what may appear in a (real or virtual) DWO file. */
992 struct dwarf2_section_info abbrev;
993 struct dwarf2_section_info line;
994 struct dwarf2_section_info loc;
995 struct dwarf2_section_info loclists;
996 struct dwarf2_section_info macinfo;
997 struct dwarf2_section_info macro;
998 struct dwarf2_section_info str;
999 struct dwarf2_section_info str_offsets;
1000 /* In the case of a virtual DWO file, these two are unused. */
1001 struct dwarf2_section_info info;
1002 VEC (dwarf2_section_info_def) *types;
1005 /* CUs/TUs in DWP/DWO files. */
1009 /* Backlink to the containing struct dwo_file. */
1010 struct dwo_file *dwo_file;
1012 /* The "id" that distinguishes this CU/TU.
1013 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1014 Since signatures came first, we stick with it for consistency. */
1017 /* The section this CU/TU lives in, in the DWO file. */
1018 struct dwarf2_section_info *section;
1020 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1021 sect_offset sect_off;
1022 unsigned int length;
1024 /* For types, offset in the type's DIE of the type defined by this TU. */
1025 cu_offset type_offset_in_tu;
1028 /* include/dwarf2.h defines the DWP section codes.
1029 It defines a max value but it doesn't define a min value, which we
1030 use for error checking, so provide one. */
1032 enum dwp_v2_section_ids
1037 /* Data for one DWO file.
1039 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1040 appears in a DWP file). DWP files don't really have DWO files per se -
1041 comdat folding of types "loses" the DWO file they came from, and from
1042 a high level view DWP files appear to contain a mass of random types.
1043 However, to maintain consistency with the non-DWP case we pretend DWP
1044 files contain virtual DWO files, and we assign each TU with one virtual
1045 DWO file (generally based on the line and abbrev section offsets -
1046 a heuristic that seems to work in practice). */
1050 /* The DW_AT_GNU_dwo_name attribute.
1051 For virtual DWO files the name is constructed from the section offsets
1052 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1053 from related CU+TUs. */
1054 const char *dwo_name;
1056 /* The DW_AT_comp_dir attribute. */
1057 const char *comp_dir;
1059 /* The bfd, when the file is open. Otherwise this is NULL.
1060 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1063 /* The sections that make up this DWO file.
1064 Remember that for virtual DWO files in DWP V2, these are virtual
1065 sections (for lack of a better name). */
1066 struct dwo_sections sections;
1068 /* The CUs in the file.
1069 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1070 an extension to handle LLVM's Link Time Optimization output (where
1071 multiple source files may be compiled into a single object/dwo pair). */
1074 /* Table of TUs in the file.
1075 Each element is a struct dwo_unit. */
1079 /* These sections are what may appear in a DWP file. */
1083 /* These are used by both DWP version 1 and 2. */
1084 struct dwarf2_section_info str;
1085 struct dwarf2_section_info cu_index;
1086 struct dwarf2_section_info tu_index;
1088 /* These are only used by DWP version 2 files.
1089 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1090 sections are referenced by section number, and are not recorded here.
1091 In DWP version 2 there is at most one copy of all these sections, each
1092 section being (effectively) comprised of the concatenation of all of the
1093 individual sections that exist in the version 1 format.
1094 To keep the code simple we treat each of these concatenated pieces as a
1095 section itself (a virtual section?). */
1096 struct dwarf2_section_info abbrev;
1097 struct dwarf2_section_info info;
1098 struct dwarf2_section_info line;
1099 struct dwarf2_section_info loc;
1100 struct dwarf2_section_info macinfo;
1101 struct dwarf2_section_info macro;
1102 struct dwarf2_section_info str_offsets;
1103 struct dwarf2_section_info types;
1106 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1107 A virtual DWO file is a DWO file as it appears in a DWP file. */
1109 struct virtual_v1_dwo_sections
1111 struct dwarf2_section_info abbrev;
1112 struct dwarf2_section_info line;
1113 struct dwarf2_section_info loc;
1114 struct dwarf2_section_info macinfo;
1115 struct dwarf2_section_info macro;
1116 struct dwarf2_section_info str_offsets;
1117 /* Each DWP hash table entry records one CU or one TU.
1118 That is recorded here, and copied to dwo_unit.section. */
1119 struct dwarf2_section_info info_or_types;
1122 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1123 In version 2, the sections of the DWO files are concatenated together
1124 and stored in one section of that name. Thus each ELF section contains
1125 several "virtual" sections. */
1127 struct virtual_v2_dwo_sections
1129 bfd_size_type abbrev_offset;
1130 bfd_size_type abbrev_size;
1132 bfd_size_type line_offset;
1133 bfd_size_type line_size;
1135 bfd_size_type loc_offset;
1136 bfd_size_type loc_size;
1138 bfd_size_type macinfo_offset;
1139 bfd_size_type macinfo_size;
1141 bfd_size_type macro_offset;
1142 bfd_size_type macro_size;
1144 bfd_size_type str_offsets_offset;
1145 bfd_size_type str_offsets_size;
1147 /* Each DWP hash table entry records one CU or one TU.
1148 That is recorded here, and copied to dwo_unit.section. */
1149 bfd_size_type info_or_types_offset;
1150 bfd_size_type info_or_types_size;
1153 /* Contents of DWP hash tables. */
1155 struct dwp_hash_table
1157 uint32_t version, nr_columns;
1158 uint32_t nr_units, nr_slots;
1159 const gdb_byte *hash_table, *unit_table;
1164 const gdb_byte *indices;
1168 /* This is indexed by column number and gives the id of the section
1170 #define MAX_NR_V2_DWO_SECTIONS \
1171 (1 /* .debug_info or .debug_types */ \
1172 + 1 /* .debug_abbrev */ \
1173 + 1 /* .debug_line */ \
1174 + 1 /* .debug_loc */ \
1175 + 1 /* .debug_str_offsets */ \
1176 + 1 /* .debug_macro or .debug_macinfo */)
1177 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1178 const gdb_byte *offsets;
1179 const gdb_byte *sizes;
1184 /* Data for one DWP file. */
1188 /* Name of the file. */
1191 /* File format version. */
1197 /* Section info for this file. */
1198 struct dwp_sections sections;
1200 /* Table of CUs in the file. */
1201 const struct dwp_hash_table *cus;
1203 /* Table of TUs in the file. */
1204 const struct dwp_hash_table *tus;
1206 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1210 /* Table to map ELF section numbers to their sections.
1211 This is only needed for the DWP V1 file format. */
1212 unsigned int num_sections;
1213 asection **elf_sections;
1216 /* This represents a '.dwz' file. */
1220 /* A dwz file can only contain a few sections. */
1221 struct dwarf2_section_info abbrev;
1222 struct dwarf2_section_info info;
1223 struct dwarf2_section_info str;
1224 struct dwarf2_section_info line;
1225 struct dwarf2_section_info macro;
1226 struct dwarf2_section_info gdb_index;
1227 struct dwarf2_section_info debug_names;
1229 /* The dwz's BFD. */
1233 /* Struct used to pass misc. parameters to read_die_and_children, et
1234 al. which are used for both .debug_info and .debug_types dies.
1235 All parameters here are unchanging for the life of the call. This
1236 struct exists to abstract away the constant parameters of die reading. */
1238 struct die_reader_specs
1240 /* The bfd of die_section. */
1243 /* The CU of the DIE we are parsing. */
1244 struct dwarf2_cu *cu;
1246 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1247 struct dwo_file *dwo_file;
1249 /* The section the die comes from.
1250 This is either .debug_info or .debug_types, or the .dwo variants. */
1251 struct dwarf2_section_info *die_section;
1253 /* die_section->buffer. */
1254 const gdb_byte *buffer;
1256 /* The end of the buffer. */
1257 const gdb_byte *buffer_end;
1259 /* The value of the DW_AT_comp_dir attribute. */
1260 const char *comp_dir;
1263 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1264 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1265 const gdb_byte *info_ptr,
1266 struct die_info *comp_unit_die,
1270 /* A 1-based directory index. This is a strong typedef to prevent
1271 accidentally using a directory index as a 0-based index into an
1273 enum class dir_index : unsigned int {};
1275 /* Likewise, a 1-based file name index. */
1276 enum class file_name_index : unsigned int {};
1280 file_entry () = default;
1282 file_entry (const char *name_, dir_index d_index_,
1283 unsigned int mod_time_, unsigned int length_)
1286 mod_time (mod_time_),
1290 /* Return the include directory at D_INDEX stored in LH. Returns
1291 NULL if D_INDEX is out of bounds. */
1292 const char *include_dir (const line_header *lh) const;
1294 /* The file name. Note this is an observing pointer. The memory is
1295 owned by debug_line_buffer. */
1296 const char *name {};
1298 /* The directory index (1-based). */
1299 dir_index d_index {};
1301 unsigned int mod_time {};
1303 unsigned int length {};
1305 /* True if referenced by the Line Number Program. */
1308 /* The associated symbol table, if any. */
1309 struct symtab *symtab {};
1312 /* The line number information for a compilation unit (found in the
1313 .debug_line section) begins with a "statement program header",
1314 which contains the following information. */
1321 /* Add an entry to the include directory table. */
1322 void add_include_dir (const char *include_dir);
1324 /* Add an entry to the file name table. */
1325 void add_file_name (const char *name, dir_index d_index,
1326 unsigned int mod_time, unsigned int length);
1328 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1329 is out of bounds. */
1330 const char *include_dir_at (dir_index index) const
1332 /* Convert directory index number (1-based) to vector index
1334 size_t vec_index = to_underlying (index) - 1;
1336 if (vec_index >= include_dirs.size ())
1338 return include_dirs[vec_index];
1341 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1342 is out of bounds. */
1343 file_entry *file_name_at (file_name_index index)
1345 /* Convert file name index number (1-based) to vector index
1347 size_t vec_index = to_underlying (index) - 1;
1349 if (vec_index >= file_names.size ())
1351 return &file_names[vec_index];
1354 /* Const version of the above. */
1355 const file_entry *file_name_at (unsigned int index) const
1357 if (index >= file_names.size ())
1359 return &file_names[index];
1362 /* Offset of line number information in .debug_line section. */
1363 sect_offset sect_off {};
1365 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1366 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1368 unsigned int total_length {};
1369 unsigned short version {};
1370 unsigned int header_length {};
1371 unsigned char minimum_instruction_length {};
1372 unsigned char maximum_ops_per_instruction {};
1373 unsigned char default_is_stmt {};
1375 unsigned char line_range {};
1376 unsigned char opcode_base {};
1378 /* standard_opcode_lengths[i] is the number of operands for the
1379 standard opcode whose value is i. This means that
1380 standard_opcode_lengths[0] is unused, and the last meaningful
1381 element is standard_opcode_lengths[opcode_base - 1]. */
1382 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1384 /* The include_directories table. Note these are observing
1385 pointers. The memory is owned by debug_line_buffer. */
1386 std::vector<const char *> include_dirs;
1388 /* The file_names table. */
1389 std::vector<file_entry> file_names;
1391 /* The start and end of the statement program following this
1392 header. These point into dwarf2_per_objfile->line_buffer. */
1393 const gdb_byte *statement_program_start {}, *statement_program_end {};
1396 typedef std::unique_ptr<line_header> line_header_up;
1399 file_entry::include_dir (const line_header *lh) const
1401 return lh->include_dir_at (d_index);
1404 /* When we construct a partial symbol table entry we only
1405 need this much information. */
1406 struct partial_die_info
1408 /* Offset of this DIE. */
1409 sect_offset sect_off;
1411 /* DWARF-2 tag for this DIE. */
1412 ENUM_BITFIELD(dwarf_tag) tag : 16;
1414 /* Assorted flags describing the data found in this DIE. */
1415 unsigned int has_children : 1;
1416 unsigned int is_external : 1;
1417 unsigned int is_declaration : 1;
1418 unsigned int has_type : 1;
1419 unsigned int has_specification : 1;
1420 unsigned int has_pc_info : 1;
1421 unsigned int may_be_inlined : 1;
1423 /* This DIE has been marked DW_AT_main_subprogram. */
1424 unsigned int main_subprogram : 1;
1426 /* Flag set if the SCOPE field of this structure has been
1428 unsigned int scope_set : 1;
1430 /* Flag set if the DIE has a byte_size attribute. */
1431 unsigned int has_byte_size : 1;
1433 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1434 unsigned int has_const_value : 1;
1436 /* Flag set if any of the DIE's children are template arguments. */
1437 unsigned int has_template_arguments : 1;
1439 /* Flag set if fixup_partial_die has been called on this die. */
1440 unsigned int fixup_called : 1;
1442 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1443 unsigned int is_dwz : 1;
1445 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1446 unsigned int spec_is_dwz : 1;
1448 /* The name of this DIE. Normally the value of DW_AT_name, but
1449 sometimes a default name for unnamed DIEs. */
1452 /* The linkage name, if present. */
1453 const char *linkage_name;
1455 /* The scope to prepend to our children. This is generally
1456 allocated on the comp_unit_obstack, so will disappear
1457 when this compilation unit leaves the cache. */
1460 /* Some data associated with the partial DIE. The tag determines
1461 which field is live. */
1464 /* The location description associated with this DIE, if any. */
1465 struct dwarf_block *locdesc;
1466 /* The offset of an import, for DW_TAG_imported_unit. */
1467 sect_offset sect_off;
1470 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1474 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1475 DW_AT_sibling, if any. */
1476 /* NOTE: This member isn't strictly necessary, read_partial_die could
1477 return DW_AT_sibling values to its caller load_partial_dies. */
1478 const gdb_byte *sibling;
1480 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1481 DW_AT_specification (or DW_AT_abstract_origin or
1482 DW_AT_extension). */
1483 sect_offset spec_offset;
1485 /* Pointers to this DIE's parent, first child, and next sibling,
1487 struct partial_die_info *die_parent, *die_child, *die_sibling;
1490 /* This data structure holds the information of an abbrev. */
1493 unsigned int number; /* number identifying abbrev */
1494 enum dwarf_tag tag; /* dwarf tag */
1495 unsigned short has_children; /* boolean */
1496 unsigned short num_attrs; /* number of attributes */
1497 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1498 struct abbrev_info *next; /* next in chain */
1503 ENUM_BITFIELD(dwarf_attribute) name : 16;
1504 ENUM_BITFIELD(dwarf_form) form : 16;
1506 /* It is valid only if FORM is DW_FORM_implicit_const. */
1507 LONGEST implicit_const;
1510 /* Size of abbrev_table.abbrev_hash_table. */
1511 #define ABBREV_HASH_SIZE 121
1513 /* Top level data structure to contain an abbreviation table. */
1517 /* Where the abbrev table came from.
1518 This is used as a sanity check when the table is used. */
1519 sect_offset sect_off;
1521 /* Storage for the abbrev table. */
1522 struct obstack abbrev_obstack;
1524 /* Hash table of abbrevs.
1525 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1526 It could be statically allocated, but the previous code didn't so we
1528 struct abbrev_info **abbrevs;
1531 /* Attributes have a name and a value. */
1534 ENUM_BITFIELD(dwarf_attribute) name : 16;
1535 ENUM_BITFIELD(dwarf_form) form : 15;
1537 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1538 field should be in u.str (existing only for DW_STRING) but it is kept
1539 here for better struct attribute alignment. */
1540 unsigned int string_is_canonical : 1;
1545 struct dwarf_block *blk;
1554 /* This data structure holds a complete die structure. */
1557 /* DWARF-2 tag for this DIE. */
1558 ENUM_BITFIELD(dwarf_tag) tag : 16;
1560 /* Number of attributes */
1561 unsigned char num_attrs;
1563 /* True if we're presently building the full type name for the
1564 type derived from this DIE. */
1565 unsigned char building_fullname : 1;
1567 /* True if this die is in process. PR 16581. */
1568 unsigned char in_process : 1;
1571 unsigned int abbrev;
1573 /* Offset in .debug_info or .debug_types section. */
1574 sect_offset sect_off;
1576 /* The dies in a compilation unit form an n-ary tree. PARENT
1577 points to this die's parent; CHILD points to the first child of
1578 this node; and all the children of a given node are chained
1579 together via their SIBLING fields. */
1580 struct die_info *child; /* Its first child, if any. */
1581 struct die_info *sibling; /* Its next sibling, if any. */
1582 struct die_info *parent; /* Its parent, if any. */
1584 /* An array of attributes, with NUM_ATTRS elements. There may be
1585 zero, but it's not common and zero-sized arrays are not
1586 sufficiently portable C. */
1587 struct attribute attrs[1];
1590 /* Get at parts of an attribute structure. */
1592 #define DW_STRING(attr) ((attr)->u.str)
1593 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1594 #define DW_UNSND(attr) ((attr)->u.unsnd)
1595 #define DW_BLOCK(attr) ((attr)->u.blk)
1596 #define DW_SND(attr) ((attr)->u.snd)
1597 #define DW_ADDR(attr) ((attr)->u.addr)
1598 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1600 /* Blocks are a bunch of untyped bytes. */
1605 /* Valid only if SIZE is not zero. */
1606 const gdb_byte *data;
1609 #ifndef ATTR_ALLOC_CHUNK
1610 #define ATTR_ALLOC_CHUNK 4
1613 /* Allocate fields for structs, unions and enums in this size. */
1614 #ifndef DW_FIELD_ALLOC_CHUNK
1615 #define DW_FIELD_ALLOC_CHUNK 4
1618 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1619 but this would require a corresponding change in unpack_field_as_long
1621 static int bits_per_byte = 8;
1625 struct nextfield *next;
1633 struct nextfnfield *next;
1634 struct fn_field fnfield;
1641 struct nextfnfield *head;
1644 struct decl_field_list
1646 struct decl_field field;
1647 struct decl_field_list *next;
1650 /* The routines that read and process dies for a C struct or C++ class
1651 pass lists of data member fields and lists of member function fields
1652 in an instance of a field_info structure, as defined below. */
1655 /* List of data member and baseclasses fields. */
1656 struct nextfield *fields, *baseclasses;
1658 /* Number of fields (including baseclasses). */
1661 /* Number of baseclasses. */
1664 /* Set if the accesibility of one of the fields is not public. */
1665 int non_public_fields;
1667 /* Member function fieldlist array, contains name of possibly overloaded
1668 member function, number of overloaded member functions and a pointer
1669 to the head of the member function field chain. */
1670 struct fnfieldlist *fnfieldlists;
1672 /* Number of entries in the fnfieldlists array. */
1675 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1676 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1677 struct decl_field_list *typedef_field_list;
1678 unsigned typedef_field_list_count;
1680 /* Nested types defined by this class and the number of elements in this
1682 struct decl_field_list *nested_types_list;
1683 unsigned nested_types_list_count;
1686 /* One item on the queue of compilation units to read in full symbols
1688 struct dwarf2_queue_item
1690 struct dwarf2_per_cu_data *per_cu;
1691 enum language pretend_language;
1692 struct dwarf2_queue_item *next;
1695 /* The current queue. */
1696 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1698 /* Loaded secondary compilation units are kept in memory until they
1699 have not been referenced for the processing of this many
1700 compilation units. Set this to zero to disable caching. Cache
1701 sizes of up to at least twenty will improve startup time for
1702 typical inter-CU-reference binaries, at an obvious memory cost. */
1703 static int dwarf_max_cache_age = 5;
1705 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1706 struct cmd_list_element *c, const char *value)
1708 fprintf_filtered (file, _("The upper bound on the age of cached "
1709 "DWARF compilation units is %s.\n"),
1713 /* local function prototypes */
1715 static const char *get_section_name (const struct dwarf2_section_info *);
1717 static const char *get_section_file_name (const struct dwarf2_section_info *);
1719 static void dwarf2_find_base_address (struct die_info *die,
1720 struct dwarf2_cu *cu);
1722 static struct partial_symtab *create_partial_symtab
1723 (struct dwarf2_per_cu_data *per_cu, const char *name);
1725 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1726 const gdb_byte *info_ptr,
1727 struct die_info *type_unit_die,
1728 int has_children, void *data);
1730 static void dwarf2_build_psymtabs_hard
1731 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1733 static void scan_partial_symbols (struct partial_die_info *,
1734 CORE_ADDR *, CORE_ADDR *,
1735 int, struct dwarf2_cu *);
1737 static void add_partial_symbol (struct partial_die_info *,
1738 struct dwarf2_cu *);
1740 static void add_partial_namespace (struct partial_die_info *pdi,
1741 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1742 int set_addrmap, struct dwarf2_cu *cu);
1744 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1745 CORE_ADDR *highpc, int set_addrmap,
1746 struct dwarf2_cu *cu);
1748 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1749 struct dwarf2_cu *cu);
1751 static void add_partial_subprogram (struct partial_die_info *pdi,
1752 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1753 int need_pc, struct dwarf2_cu *cu);
1755 static void dwarf2_read_symtab (struct partial_symtab *,
1758 static void psymtab_to_symtab_1 (struct partial_symtab *);
1760 static struct abbrev_info *abbrev_table_lookup_abbrev
1761 (const struct abbrev_table *, unsigned int);
1763 static struct abbrev_table *abbrev_table_read_table
1764 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1767 static void abbrev_table_free (struct abbrev_table *);
1769 static void abbrev_table_free_cleanup (void *);
1771 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1772 struct dwarf2_section_info *);
1774 static void dwarf2_free_abbrev_table (void *);
1776 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1778 static struct partial_die_info *load_partial_dies
1779 (const struct die_reader_specs *, const gdb_byte *, int);
1781 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1782 struct partial_die_info *,
1783 struct abbrev_info *,
1787 static struct partial_die_info *find_partial_die (sect_offset, int,
1788 struct dwarf2_cu *);
1790 static void fixup_partial_die (struct partial_die_info *,
1791 struct dwarf2_cu *);
1793 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1794 struct attribute *, struct attr_abbrev *,
1797 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1799 static int read_1_signed_byte (bfd *, const gdb_byte *);
1801 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1803 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1805 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1807 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1810 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1812 static LONGEST read_checked_initial_length_and_offset
1813 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1814 unsigned int *, unsigned int *);
1816 static LONGEST read_offset (bfd *, const gdb_byte *,
1817 const struct comp_unit_head *,
1820 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1822 static sect_offset read_abbrev_offset
1823 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1824 struct dwarf2_section_info *, sect_offset);
1826 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1828 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1830 static const char *read_indirect_string
1831 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1832 const struct comp_unit_head *, unsigned int *);
1834 static const char *read_indirect_line_string
1835 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1836 const struct comp_unit_head *, unsigned int *);
1838 static const char *read_indirect_string_at_offset
1839 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1840 LONGEST str_offset);
1842 static const char *read_indirect_string_from_dwz
1843 (struct objfile *objfile, struct dwz_file *, LONGEST);
1845 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1847 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1851 static const char *read_str_index (const struct die_reader_specs *reader,
1852 ULONGEST str_index);
1854 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1856 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1857 struct dwarf2_cu *);
1859 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1862 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1863 struct dwarf2_cu *cu);
1865 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1866 struct dwarf2_cu *cu);
1868 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1870 static struct die_info *die_specification (struct die_info *die,
1871 struct dwarf2_cu **);
1873 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1874 struct dwarf2_cu *cu);
1876 static void dwarf_decode_lines (struct line_header *, const char *,
1877 struct dwarf2_cu *, struct partial_symtab *,
1878 CORE_ADDR, int decode_mapping);
1880 static void dwarf2_start_subfile (const char *, const char *);
1882 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1883 const char *, const char *,
1886 static struct symbol *new_symbol (struct die_info *, struct type *,
1887 struct dwarf2_cu *);
1889 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1890 struct dwarf2_cu *, struct symbol *);
1892 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1893 struct dwarf2_cu *);
1895 static void dwarf2_const_value_attr (const struct attribute *attr,
1898 struct obstack *obstack,
1899 struct dwarf2_cu *cu, LONGEST *value,
1900 const gdb_byte **bytes,
1901 struct dwarf2_locexpr_baton **baton);
1903 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1905 static int need_gnat_info (struct dwarf2_cu *);
1907 static struct type *die_descriptive_type (struct die_info *,
1908 struct dwarf2_cu *);
1910 static void set_descriptive_type (struct type *, struct die_info *,
1911 struct dwarf2_cu *);
1913 static struct type *die_containing_type (struct die_info *,
1914 struct dwarf2_cu *);
1916 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1917 struct dwarf2_cu *);
1919 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1921 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1923 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1925 static char *typename_concat (struct obstack *obs, const char *prefix,
1926 const char *suffix, int physname,
1927 struct dwarf2_cu *cu);
1929 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1931 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1933 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1935 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1937 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1939 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1941 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1942 struct dwarf2_cu *, struct partial_symtab *);
1944 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1945 values. Keep the items ordered with increasing constraints compliance. */
1948 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1949 PC_BOUNDS_NOT_PRESENT,
1951 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1952 were present but they do not form a valid range of PC addresses. */
1955 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1958 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1962 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1963 CORE_ADDR *, CORE_ADDR *,
1965 struct partial_symtab *);
1967 static void get_scope_pc_bounds (struct die_info *,
1968 CORE_ADDR *, CORE_ADDR *,
1969 struct dwarf2_cu *);
1971 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1972 CORE_ADDR, struct dwarf2_cu *);
1974 static void dwarf2_add_field (struct field_info *, struct die_info *,
1975 struct dwarf2_cu *);
1977 static void dwarf2_attach_fields_to_type (struct field_info *,
1978 struct type *, struct dwarf2_cu *);
1980 static void dwarf2_add_member_fn (struct field_info *,
1981 struct die_info *, struct type *,
1982 struct dwarf2_cu *);
1984 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1986 struct dwarf2_cu *);
1988 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1990 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1992 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1994 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1996 static struct using_direct **using_directives (enum language);
1998 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2000 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2002 static struct type *read_module_type (struct die_info *die,
2003 struct dwarf2_cu *cu);
2005 static const char *namespace_name (struct die_info *die,
2006 int *is_anonymous, struct dwarf2_cu *);
2008 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2010 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2012 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2013 struct dwarf2_cu *);
2015 static struct die_info *read_die_and_siblings_1
2016 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2019 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2020 const gdb_byte *info_ptr,
2021 const gdb_byte **new_info_ptr,
2022 struct die_info *parent);
2024 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2025 struct die_info **, const gdb_byte *,
2028 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2029 struct die_info **, const gdb_byte *,
2032 static void process_die (struct die_info *, struct dwarf2_cu *);
2034 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2037 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2039 static const char *dwarf2_full_name (const char *name,
2040 struct die_info *die,
2041 struct dwarf2_cu *cu);
2043 static const char *dwarf2_physname (const char *name, struct die_info *die,
2044 struct dwarf2_cu *cu);
2046 static struct die_info *dwarf2_extension (struct die_info *die,
2047 struct dwarf2_cu **);
2049 static const char *dwarf_tag_name (unsigned int);
2051 static const char *dwarf_attr_name (unsigned int);
2053 static const char *dwarf_form_name (unsigned int);
2055 static const char *dwarf_bool_name (unsigned int);
2057 static const char *dwarf_type_encoding_name (unsigned int);
2059 static struct die_info *sibling_die (struct die_info *);
2061 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2063 static void dump_die_for_error (struct die_info *);
2065 static void dump_die_1 (struct ui_file *, int level, int max_level,
2068 /*static*/ void dump_die (struct die_info *, int max_level);
2070 static void store_in_ref_table (struct die_info *,
2071 struct dwarf2_cu *);
2073 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2075 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2077 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2078 const struct attribute *,
2079 struct dwarf2_cu **);
2081 static struct die_info *follow_die_ref (struct die_info *,
2082 const struct attribute *,
2083 struct dwarf2_cu **);
2085 static struct die_info *follow_die_sig (struct die_info *,
2086 const struct attribute *,
2087 struct dwarf2_cu **);
2089 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2090 struct dwarf2_cu *);
2092 static struct type *get_DW_AT_signature_type (struct die_info *,
2093 const struct attribute *,
2094 struct dwarf2_cu *);
2096 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2098 static void read_signatured_type (struct signatured_type *);
2100 static int attr_to_dynamic_prop (const struct attribute *attr,
2101 struct die_info *die, struct dwarf2_cu *cu,
2102 struct dynamic_prop *prop);
2104 /* memory allocation interface */
2106 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2108 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2110 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2112 static int attr_form_is_block (const struct attribute *);
2114 static int attr_form_is_section_offset (const struct attribute *);
2116 static int attr_form_is_constant (const struct attribute *);
2118 static int attr_form_is_ref (const struct attribute *);
2120 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2121 struct dwarf2_loclist_baton *baton,
2122 const struct attribute *attr);
2124 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2126 struct dwarf2_cu *cu,
2129 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2130 const gdb_byte *info_ptr,
2131 struct abbrev_info *abbrev);
2133 static void free_stack_comp_unit (void *);
2135 static hashval_t partial_die_hash (const void *item);
2137 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2139 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2140 (sect_offset sect_off, unsigned int offset_in_dwz,
2141 struct dwarf2_per_objfile *dwarf2_per_objfile);
2143 static void init_one_comp_unit (struct dwarf2_cu *cu,
2144 struct dwarf2_per_cu_data *per_cu);
2146 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2147 struct die_info *comp_unit_die,
2148 enum language pretend_language);
2150 static void free_heap_comp_unit (void *);
2152 static void free_cached_comp_units (void *);
2154 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2156 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2158 static struct type *set_die_type (struct die_info *, struct type *,
2159 struct dwarf2_cu *);
2161 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2163 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2165 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2168 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2171 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2174 static void dwarf2_add_dependence (struct dwarf2_cu *,
2175 struct dwarf2_per_cu_data *);
2177 static void dwarf2_mark (struct dwarf2_cu *);
2179 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2181 static struct type *get_die_type_at_offset (sect_offset,
2182 struct dwarf2_per_cu_data *);
2184 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2186 static void dwarf2_release_queue (void *dummy);
2188 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2189 enum language pretend_language);
2191 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2193 /* The return type of find_file_and_directory. Note, the enclosed
2194 string pointers are only valid while this object is valid. */
2196 struct file_and_directory
2198 /* The filename. This is never NULL. */
2201 /* The compilation directory. NULL if not known. If we needed to
2202 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2203 points directly to the DW_AT_comp_dir string attribute owned by
2204 the obstack that owns the DIE. */
2205 const char *comp_dir;
2207 /* If we needed to build a new string for comp_dir, this is what
2208 owns the storage. */
2209 std::string comp_dir_storage;
2212 static file_and_directory find_file_and_directory (struct die_info *die,
2213 struct dwarf2_cu *cu);
2215 static char *file_full_name (int file, struct line_header *lh,
2216 const char *comp_dir);
2218 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2219 enum class rcuh_kind { COMPILE, TYPE };
2221 static const gdb_byte *read_and_check_comp_unit_head
2222 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2223 struct comp_unit_head *header,
2224 struct dwarf2_section_info *section,
2225 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2226 rcuh_kind section_kind);
2228 static void init_cutu_and_read_dies
2229 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2230 int use_existing_cu, int keep,
2231 die_reader_func_ftype *die_reader_func, void *data);
2233 static void init_cutu_and_read_dies_simple
2234 (struct dwarf2_per_cu_data *this_cu,
2235 die_reader_func_ftype *die_reader_func, void *data);
2237 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2239 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2241 static struct dwo_unit *lookup_dwo_unit_in_dwp
2242 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2243 struct dwp_file *dwp_file, const char *comp_dir,
2244 ULONGEST signature, int is_debug_types);
2246 static struct dwp_file *get_dwp_file
2247 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2249 static struct dwo_unit *lookup_dwo_comp_unit
2250 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2252 static struct dwo_unit *lookup_dwo_type_unit
2253 (struct signatured_type *, const char *, const char *);
2255 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2257 static void free_dwo_file_cleanup (void *);
2259 struct free_dwo_file_cleanup_data
2261 struct dwo_file *dwo_file;
2262 struct dwarf2_per_objfile *dwarf2_per_objfile;
2265 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2267 static void check_producer (struct dwarf2_cu *cu);
2269 static void free_line_header_voidp (void *arg);
2271 /* Various complaints about symbol reading that don't abort the process. */
2274 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2276 complaint (&symfile_complaints,
2277 _("statement list doesn't fit in .debug_line section"));
2281 dwarf2_debug_line_missing_file_complaint (void)
2283 complaint (&symfile_complaints,
2284 _(".debug_line section has line data without a file"));
2288 dwarf2_debug_line_missing_end_sequence_complaint (void)
2290 complaint (&symfile_complaints,
2291 _(".debug_line section has line "
2292 "program sequence without an end"));
2296 dwarf2_complex_location_expr_complaint (void)
2298 complaint (&symfile_complaints, _("location expression too complex"));
2302 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2305 complaint (&symfile_complaints,
2306 _("const value length mismatch for '%s', got %d, expected %d"),
2311 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2313 complaint (&symfile_complaints,
2314 _("debug info runs off end of %s section"
2316 get_section_name (section),
2317 get_section_file_name (section));
2321 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2323 complaint (&symfile_complaints,
2324 _("macro debug info contains a "
2325 "malformed macro definition:\n`%s'"),
2330 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2332 complaint (&symfile_complaints,
2333 _("invalid attribute class or form for '%s' in '%s'"),
2337 /* Hash function for line_header_hash. */
2340 line_header_hash (const struct line_header *ofs)
2342 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2345 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2348 line_header_hash_voidp (const void *item)
2350 const struct line_header *ofs = (const struct line_header *) item;
2352 return line_header_hash (ofs);
2355 /* Equality function for line_header_hash. */
2358 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2360 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2361 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2363 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2364 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2369 /* Read the given attribute value as an address, taking the attribute's
2370 form into account. */
2373 attr_value_as_address (struct attribute *attr)
2377 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2379 /* Aside from a few clearly defined exceptions, attributes that
2380 contain an address must always be in DW_FORM_addr form.
2381 Unfortunately, some compilers happen to be violating this
2382 requirement by encoding addresses using other forms, such
2383 as DW_FORM_data4 for example. For those broken compilers,
2384 we try to do our best, without any guarantee of success,
2385 to interpret the address correctly. It would also be nice
2386 to generate a complaint, but that would require us to maintain
2387 a list of legitimate cases where a non-address form is allowed,
2388 as well as update callers to pass in at least the CU's DWARF
2389 version. This is more overhead than what we're willing to
2390 expand for a pretty rare case. */
2391 addr = DW_UNSND (attr);
2394 addr = DW_ADDR (attr);
2399 /* The suffix for an index file. */
2400 #define INDEX4_SUFFIX ".gdb-index"
2401 #define INDEX5_SUFFIX ".debug_names"
2402 #define DEBUG_STR_SUFFIX ".debug_str"
2404 /* See declaration. */
2406 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2407 const dwarf2_debug_sections *names)
2408 : objfile (objfile_)
2411 names = &dwarf2_elf_names;
2413 bfd *obfd = objfile->obfd;
2415 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2416 locate_sections (obfd, sec, *names);
2419 dwarf2_per_objfile::~dwarf2_per_objfile ()
2421 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2422 free_cached_comp_units ();
2424 if (quick_file_names_table)
2425 htab_delete (quick_file_names_table);
2427 if (line_header_hash)
2428 htab_delete (line_header_hash);
2430 /* Everything else should be on the objfile obstack. */
2433 /* See declaration. */
2436 dwarf2_per_objfile::free_cached_comp_units ()
2438 dwarf2_per_cu_data *per_cu = read_in_chain;
2439 dwarf2_per_cu_data **last_chain = &read_in_chain;
2440 while (per_cu != NULL)
2442 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2444 free_heap_comp_unit (per_cu->cu);
2445 *last_chain = next_cu;
2450 /* Try to locate the sections we need for DWARF 2 debugging
2451 information and return true if we have enough to do something.
2452 NAMES points to the dwarf2 section names, or is NULL if the standard
2453 ELF names are used. */
2456 dwarf2_has_info (struct objfile *objfile,
2457 const struct dwarf2_debug_sections *names)
2459 if (objfile->flags & OBJF_READNEVER)
2462 struct dwarf2_per_objfile *dwarf2_per_objfile
2463 = get_dwarf2_per_objfile (objfile);
2465 if (dwarf2_per_objfile == NULL)
2467 /* Initialize per-objfile state. */
2468 struct dwarf2_per_objfile *data
2469 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2471 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2472 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2474 return (!dwarf2_per_objfile->info.is_virtual
2475 && dwarf2_per_objfile->info.s.section != NULL
2476 && !dwarf2_per_objfile->abbrev.is_virtual
2477 && dwarf2_per_objfile->abbrev.s.section != NULL);
2480 /* Return the containing section of virtual section SECTION. */
2482 static struct dwarf2_section_info *
2483 get_containing_section (const struct dwarf2_section_info *section)
2485 gdb_assert (section->is_virtual);
2486 return section->s.containing_section;
2489 /* Return the bfd owner of SECTION. */
2492 get_section_bfd_owner (const struct dwarf2_section_info *section)
2494 if (section->is_virtual)
2496 section = get_containing_section (section);
2497 gdb_assert (!section->is_virtual);
2499 return section->s.section->owner;
2502 /* Return the bfd section of SECTION.
2503 Returns NULL if the section is not present. */
2506 get_section_bfd_section (const struct dwarf2_section_info *section)
2508 if (section->is_virtual)
2510 section = get_containing_section (section);
2511 gdb_assert (!section->is_virtual);
2513 return section->s.section;
2516 /* Return the name of SECTION. */
2519 get_section_name (const struct dwarf2_section_info *section)
2521 asection *sectp = get_section_bfd_section (section);
2523 gdb_assert (sectp != NULL);
2524 return bfd_section_name (get_section_bfd_owner (section), sectp);
2527 /* Return the name of the file SECTION is in. */
2530 get_section_file_name (const struct dwarf2_section_info *section)
2532 bfd *abfd = get_section_bfd_owner (section);
2534 return bfd_get_filename (abfd);
2537 /* Return the id of SECTION.
2538 Returns 0 if SECTION doesn't exist. */
2541 get_section_id (const struct dwarf2_section_info *section)
2543 asection *sectp = get_section_bfd_section (section);
2550 /* Return the flags of SECTION.
2551 SECTION (or containing section if this is a virtual section) must exist. */
2554 get_section_flags (const struct dwarf2_section_info *section)
2556 asection *sectp = get_section_bfd_section (section);
2558 gdb_assert (sectp != NULL);
2559 return bfd_get_section_flags (sectp->owner, sectp);
2562 /* When loading sections, we look either for uncompressed section or for
2563 compressed section names. */
2566 section_is_p (const char *section_name,
2567 const struct dwarf2_section_names *names)
2569 if (names->normal != NULL
2570 && strcmp (section_name, names->normal) == 0)
2572 if (names->compressed != NULL
2573 && strcmp (section_name, names->compressed) == 0)
2578 /* See declaration. */
2581 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2582 const dwarf2_debug_sections &names)
2584 flagword aflag = bfd_get_section_flags (abfd, sectp);
2586 if ((aflag & SEC_HAS_CONTENTS) == 0)
2589 else if (section_is_p (sectp->name, &names.info))
2591 this->info.s.section = sectp;
2592 this->info.size = bfd_get_section_size (sectp);
2594 else if (section_is_p (sectp->name, &names.abbrev))
2596 this->abbrev.s.section = sectp;
2597 this->abbrev.size = bfd_get_section_size (sectp);
2599 else if (section_is_p (sectp->name, &names.line))
2601 this->line.s.section = sectp;
2602 this->line.size = bfd_get_section_size (sectp);
2604 else if (section_is_p (sectp->name, &names.loc))
2606 this->loc.s.section = sectp;
2607 this->loc.size = bfd_get_section_size (sectp);
2609 else if (section_is_p (sectp->name, &names.loclists))
2611 this->loclists.s.section = sectp;
2612 this->loclists.size = bfd_get_section_size (sectp);
2614 else if (section_is_p (sectp->name, &names.macinfo))
2616 this->macinfo.s.section = sectp;
2617 this->macinfo.size = bfd_get_section_size (sectp);
2619 else if (section_is_p (sectp->name, &names.macro))
2621 this->macro.s.section = sectp;
2622 this->macro.size = bfd_get_section_size (sectp);
2624 else if (section_is_p (sectp->name, &names.str))
2626 this->str.s.section = sectp;
2627 this->str.size = bfd_get_section_size (sectp);
2629 else if (section_is_p (sectp->name, &names.line_str))
2631 this->line_str.s.section = sectp;
2632 this->line_str.size = bfd_get_section_size (sectp);
2634 else if (section_is_p (sectp->name, &names.addr))
2636 this->addr.s.section = sectp;
2637 this->addr.size = bfd_get_section_size (sectp);
2639 else if (section_is_p (sectp->name, &names.frame))
2641 this->frame.s.section = sectp;
2642 this->frame.size = bfd_get_section_size (sectp);
2644 else if (section_is_p (sectp->name, &names.eh_frame))
2646 this->eh_frame.s.section = sectp;
2647 this->eh_frame.size = bfd_get_section_size (sectp);
2649 else if (section_is_p (sectp->name, &names.ranges))
2651 this->ranges.s.section = sectp;
2652 this->ranges.size = bfd_get_section_size (sectp);
2654 else if (section_is_p (sectp->name, &names.rnglists))
2656 this->rnglists.s.section = sectp;
2657 this->rnglists.size = bfd_get_section_size (sectp);
2659 else if (section_is_p (sectp->name, &names.types))
2661 struct dwarf2_section_info type_section;
2663 memset (&type_section, 0, sizeof (type_section));
2664 type_section.s.section = sectp;
2665 type_section.size = bfd_get_section_size (sectp);
2667 VEC_safe_push (dwarf2_section_info_def, this->types,
2670 else if (section_is_p (sectp->name, &names.gdb_index))
2672 this->gdb_index.s.section = sectp;
2673 this->gdb_index.size = bfd_get_section_size (sectp);
2675 else if (section_is_p (sectp->name, &names.debug_names))
2677 this->debug_names.s.section = sectp;
2678 this->debug_names.size = bfd_get_section_size (sectp);
2680 else if (section_is_p (sectp->name, &names.debug_aranges))
2682 this->debug_aranges.s.section = sectp;
2683 this->debug_aranges.size = bfd_get_section_size (sectp);
2686 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2687 && bfd_section_vma (abfd, sectp) == 0)
2688 this->has_section_at_zero = true;
2691 /* A helper function that decides whether a section is empty,
2695 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2697 if (section->is_virtual)
2698 return section->size == 0;
2699 return section->s.section == NULL || section->size == 0;
2702 /* Read the contents of the section INFO.
2703 OBJFILE is the main object file, but not necessarily the file where
2704 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2706 If the section is compressed, uncompress it before returning. */
2709 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2713 gdb_byte *buf, *retbuf;
2717 info->buffer = NULL;
2720 if (dwarf2_section_empty_p (info))
2723 sectp = get_section_bfd_section (info);
2725 /* If this is a virtual section we need to read in the real one first. */
2726 if (info->is_virtual)
2728 struct dwarf2_section_info *containing_section =
2729 get_containing_section (info);
2731 gdb_assert (sectp != NULL);
2732 if ((sectp->flags & SEC_RELOC) != 0)
2734 error (_("Dwarf Error: DWP format V2 with relocations is not"
2735 " supported in section %s [in module %s]"),
2736 get_section_name (info), get_section_file_name (info));
2738 dwarf2_read_section (objfile, containing_section);
2739 /* Other code should have already caught virtual sections that don't
2741 gdb_assert (info->virtual_offset + info->size
2742 <= containing_section->size);
2743 /* If the real section is empty or there was a problem reading the
2744 section we shouldn't get here. */
2745 gdb_assert (containing_section->buffer != NULL);
2746 info->buffer = containing_section->buffer + info->virtual_offset;
2750 /* If the section has relocations, we must read it ourselves.
2751 Otherwise we attach it to the BFD. */
2752 if ((sectp->flags & SEC_RELOC) == 0)
2754 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2758 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2761 /* When debugging .o files, we may need to apply relocations; see
2762 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2763 We never compress sections in .o files, so we only need to
2764 try this when the section is not compressed. */
2765 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2768 info->buffer = retbuf;
2772 abfd = get_section_bfd_owner (info);
2773 gdb_assert (abfd != NULL);
2775 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2776 || bfd_bread (buf, info->size, abfd) != info->size)
2778 error (_("Dwarf Error: Can't read DWARF data"
2779 " in section %s [in module %s]"),
2780 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2784 /* A helper function that returns the size of a section in a safe way.
2785 If you are positive that the section has been read before using the
2786 size, then it is safe to refer to the dwarf2_section_info object's
2787 "size" field directly. In other cases, you must call this
2788 function, because for compressed sections the size field is not set
2789 correctly until the section has been read. */
2791 static bfd_size_type
2792 dwarf2_section_size (struct objfile *objfile,
2793 struct dwarf2_section_info *info)
2796 dwarf2_read_section (objfile, info);
2800 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2804 dwarf2_get_section_info (struct objfile *objfile,
2805 enum dwarf2_section_enum sect,
2806 asection **sectp, const gdb_byte **bufp,
2807 bfd_size_type *sizep)
2809 struct dwarf2_per_objfile *data
2810 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2811 dwarf2_objfile_data_key);
2812 struct dwarf2_section_info *info;
2814 /* We may see an objfile without any DWARF, in which case we just
2825 case DWARF2_DEBUG_FRAME:
2826 info = &data->frame;
2828 case DWARF2_EH_FRAME:
2829 info = &data->eh_frame;
2832 gdb_assert_not_reached ("unexpected section");
2835 dwarf2_read_section (objfile, info);
2837 *sectp = get_section_bfd_section (info);
2838 *bufp = info->buffer;
2839 *sizep = info->size;
2842 /* A helper function to find the sections for a .dwz file. */
2845 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2847 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2849 /* Note that we only support the standard ELF names, because .dwz
2850 is ELF-only (at the time of writing). */
2851 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2853 dwz_file->abbrev.s.section = sectp;
2854 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2856 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2858 dwz_file->info.s.section = sectp;
2859 dwz_file->info.size = bfd_get_section_size (sectp);
2861 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2863 dwz_file->str.s.section = sectp;
2864 dwz_file->str.size = bfd_get_section_size (sectp);
2866 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2868 dwz_file->line.s.section = sectp;
2869 dwz_file->line.size = bfd_get_section_size (sectp);
2871 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2873 dwz_file->macro.s.section = sectp;
2874 dwz_file->macro.size = bfd_get_section_size (sectp);
2876 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2878 dwz_file->gdb_index.s.section = sectp;
2879 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2881 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2883 dwz_file->debug_names.s.section = sectp;
2884 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2888 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2889 there is no .gnu_debugaltlink section in the file. Error if there
2890 is such a section but the file cannot be found. */
2892 static struct dwz_file *
2893 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2895 const char *filename;
2896 struct dwz_file *result;
2897 bfd_size_type buildid_len_arg;
2901 if (dwarf2_per_objfile->dwz_file != NULL)
2902 return dwarf2_per_objfile->dwz_file;
2904 bfd_set_error (bfd_error_no_error);
2905 gdb::unique_xmalloc_ptr<char> data
2906 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2907 &buildid_len_arg, &buildid));
2910 if (bfd_get_error () == bfd_error_no_error)
2912 error (_("could not read '.gnu_debugaltlink' section: %s"),
2913 bfd_errmsg (bfd_get_error ()));
2916 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2918 buildid_len = (size_t) buildid_len_arg;
2920 filename = data.get ();
2922 std::string abs_storage;
2923 if (!IS_ABSOLUTE_PATH (filename))
2925 gdb::unique_xmalloc_ptr<char> abs
2926 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2928 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2929 filename = abs_storage.c_str ();
2932 /* First try the file name given in the section. If that doesn't
2933 work, try to use the build-id instead. */
2934 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2935 if (dwz_bfd != NULL)
2937 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2941 if (dwz_bfd == NULL)
2942 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2944 if (dwz_bfd == NULL)
2945 error (_("could not find '.gnu_debugaltlink' file for %s"),
2946 objfile_name (dwarf2_per_objfile->objfile));
2948 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2950 result->dwz_bfd = dwz_bfd.release ();
2952 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2954 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2955 dwarf2_per_objfile->dwz_file = result;
2959 /* DWARF quick_symbols_functions support. */
2961 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2962 unique line tables, so we maintain a separate table of all .debug_line
2963 derived entries to support the sharing.
2964 All the quick functions need is the list of file names. We discard the
2965 line_header when we're done and don't need to record it here. */
2966 struct quick_file_names
2968 /* The data used to construct the hash key. */
2969 struct stmt_list_hash hash;
2971 /* The number of entries in file_names, real_names. */
2972 unsigned int num_file_names;
2974 /* The file names from the line table, after being run through
2976 const char **file_names;
2978 /* The file names from the line table after being run through
2979 gdb_realpath. These are computed lazily. */
2980 const char **real_names;
2983 /* When using the index (and thus not using psymtabs), each CU has an
2984 object of this type. This is used to hold information needed by
2985 the various "quick" methods. */
2986 struct dwarf2_per_cu_quick_data
2988 /* The file table. This can be NULL if there was no file table
2989 or it's currently not read in.
2990 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2991 struct quick_file_names *file_names;
2993 /* The corresponding symbol table. This is NULL if symbols for this
2994 CU have not yet been read. */
2995 struct compunit_symtab *compunit_symtab;
2997 /* A temporary mark bit used when iterating over all CUs in
2998 expand_symtabs_matching. */
2999 unsigned int mark : 1;
3001 /* True if we've tried to read the file table and found there isn't one.
3002 There will be no point in trying to read it again next time. */
3003 unsigned int no_file_data : 1;
3006 /* Utility hash function for a stmt_list_hash. */
3009 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3013 if (stmt_list_hash->dwo_unit != NULL)
3014 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3015 v += to_underlying (stmt_list_hash->line_sect_off);
3019 /* Utility equality function for a stmt_list_hash. */
3022 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3023 const struct stmt_list_hash *rhs)
3025 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3027 if (lhs->dwo_unit != NULL
3028 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3031 return lhs->line_sect_off == rhs->line_sect_off;
3034 /* Hash function for a quick_file_names. */
3037 hash_file_name_entry (const void *e)
3039 const struct quick_file_names *file_data
3040 = (const struct quick_file_names *) e;
3042 return hash_stmt_list_entry (&file_data->hash);
3045 /* Equality function for a quick_file_names. */
3048 eq_file_name_entry (const void *a, const void *b)
3050 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3051 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3053 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3056 /* Delete function for a quick_file_names. */
3059 delete_file_name_entry (void *e)
3061 struct quick_file_names *file_data = (struct quick_file_names *) e;
3064 for (i = 0; i < file_data->num_file_names; ++i)
3066 xfree ((void*) file_data->file_names[i]);
3067 if (file_data->real_names)
3068 xfree ((void*) file_data->real_names[i]);
3071 /* The space for the struct itself lives on objfile_obstack,
3072 so we don't free it here. */
3075 /* Create a quick_file_names hash table. */
3078 create_quick_file_names_table (unsigned int nr_initial_entries)
3080 return htab_create_alloc (nr_initial_entries,
3081 hash_file_name_entry, eq_file_name_entry,
3082 delete_file_name_entry, xcalloc, xfree);
3085 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3086 have to be created afterwards. You should call age_cached_comp_units after
3087 processing PER_CU->CU. dw2_setup must have been already called. */
3090 load_cu (struct dwarf2_per_cu_data *per_cu)
3092 if (per_cu->is_debug_types)
3093 load_full_type_unit (per_cu);
3095 load_full_comp_unit (per_cu, language_minimal);
3097 if (per_cu->cu == NULL)
3098 return; /* Dummy CU. */
3100 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3103 /* Read in the symbols for PER_CU. */
3106 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3108 struct cleanup *back_to;
3109 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3111 /* Skip type_unit_groups, reading the type units they contain
3112 is handled elsewhere. */
3113 if (IS_TYPE_UNIT_GROUP (per_cu))
3116 back_to = make_cleanup (dwarf2_release_queue, NULL);
3118 if (dwarf2_per_objfile->using_index
3119 ? per_cu->v.quick->compunit_symtab == NULL
3120 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3122 queue_comp_unit (per_cu, language_minimal);
3125 /* If we just loaded a CU from a DWO, and we're working with an index
3126 that may badly handle TUs, load all the TUs in that DWO as well.
3127 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3128 if (!per_cu->is_debug_types
3129 && per_cu->cu != NULL
3130 && per_cu->cu->dwo_unit != NULL
3131 && dwarf2_per_objfile->index_table != NULL
3132 && dwarf2_per_objfile->index_table->version <= 7
3133 /* DWP files aren't supported yet. */
3134 && get_dwp_file (dwarf2_per_objfile) == NULL)
3135 queue_and_load_all_dwo_tus (per_cu);
3138 process_queue (dwarf2_per_objfile);
3140 /* Age the cache, releasing compilation units that have not
3141 been used recently. */
3142 age_cached_comp_units (dwarf2_per_objfile);
3144 do_cleanups (back_to);
3147 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3148 the objfile from which this CU came. Returns the resulting symbol
3151 static struct compunit_symtab *
3152 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3154 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3156 gdb_assert (dwarf2_per_objfile->using_index);
3157 if (!per_cu->v.quick->compunit_symtab)
3159 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3160 dwarf2_per_objfile);
3161 scoped_restore decrementer = increment_reading_symtab ();
3162 dw2_do_instantiate_symtab (per_cu);
3163 process_cu_includes (dwarf2_per_objfile);
3164 do_cleanups (back_to);
3167 return per_cu->v.quick->compunit_symtab;
3170 /* Return the CU/TU given its index.
3172 This is intended for loops like:
3174 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3175 + dwarf2_per_objfile->n_type_units); ++i)
3177 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3183 static struct dwarf2_per_cu_data *
3184 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3187 if (index >= dwarf2_per_objfile->n_comp_units)
3189 index -= dwarf2_per_objfile->n_comp_units;
3190 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3191 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3194 return dwarf2_per_objfile->all_comp_units[index];
3197 /* Return the CU given its index.
3198 This differs from dw2_get_cutu in that it's for when you know INDEX
3201 static struct dwarf2_per_cu_data *
3202 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3204 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3206 return dwarf2_per_objfile->all_comp_units[index];
3209 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3210 objfile_obstack, and constructed with the specified field
3213 static dwarf2_per_cu_data *
3214 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3215 struct dwarf2_section_info *section,
3217 sect_offset sect_off, ULONGEST length)
3219 struct objfile *objfile = dwarf2_per_objfile->objfile;
3220 dwarf2_per_cu_data *the_cu
3221 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3222 struct dwarf2_per_cu_data);
3223 the_cu->sect_off = sect_off;
3224 the_cu->length = length;
3225 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3226 the_cu->section = section;
3227 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3228 struct dwarf2_per_cu_quick_data);
3229 the_cu->is_dwz = is_dwz;
3233 /* A helper for create_cus_from_index that handles a given list of
3237 create_cus_from_index_list (struct objfile *objfile,
3238 const gdb_byte *cu_list, offset_type n_elements,
3239 struct dwarf2_section_info *section,
3244 struct dwarf2_per_objfile *dwarf2_per_objfile
3245 = get_dwarf2_per_objfile (objfile);
3247 for (i = 0; i < n_elements; i += 2)
3249 gdb_static_assert (sizeof (ULONGEST) >= 8);
3251 sect_offset sect_off
3252 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3253 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3256 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3257 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3262 /* Read the CU list from the mapped index, and use it to create all
3263 the CU objects for this objfile. */
3266 create_cus_from_index (struct objfile *objfile,
3267 const gdb_byte *cu_list, offset_type cu_list_elements,
3268 const gdb_byte *dwz_list, offset_type dwz_elements)
3270 struct dwz_file *dwz;
3271 struct dwarf2_per_objfile *dwarf2_per_objfile
3272 = get_dwarf2_per_objfile (objfile);
3274 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3275 dwarf2_per_objfile->all_comp_units =
3276 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3277 dwarf2_per_objfile->n_comp_units);
3279 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3280 &dwarf2_per_objfile->info, 0, 0);
3282 if (dwz_elements == 0)
3285 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3286 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3287 cu_list_elements / 2);
3290 /* Create the signatured type hash table from the index. */
3293 create_signatured_type_table_from_index (struct objfile *objfile,
3294 struct dwarf2_section_info *section,
3295 const gdb_byte *bytes,
3296 offset_type elements)
3299 htab_t sig_types_hash;
3300 struct dwarf2_per_objfile *dwarf2_per_objfile
3301 = get_dwarf2_per_objfile (objfile);
3303 dwarf2_per_objfile->n_type_units
3304 = dwarf2_per_objfile->n_allocated_type_units
3306 dwarf2_per_objfile->all_type_units =
3307 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3309 sig_types_hash = allocate_signatured_type_table (objfile);
3311 for (i = 0; i < elements; i += 3)
3313 struct signatured_type *sig_type;
3316 cu_offset type_offset_in_tu;
3318 gdb_static_assert (sizeof (ULONGEST) >= 8);
3319 sect_offset sect_off
3320 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3322 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3324 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3327 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3328 struct signatured_type);
3329 sig_type->signature = signature;
3330 sig_type->type_offset_in_tu = type_offset_in_tu;
3331 sig_type->per_cu.is_debug_types = 1;
3332 sig_type->per_cu.section = section;
3333 sig_type->per_cu.sect_off = sect_off;
3334 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3335 sig_type->per_cu.v.quick
3336 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3337 struct dwarf2_per_cu_quick_data);
3339 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3342 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3345 dwarf2_per_objfile->signatured_types = sig_types_hash;
3348 /* Create the signatured type hash table from .debug_names. */
3351 create_signatured_type_table_from_debug_names
3352 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3353 const mapped_debug_names &map,
3354 struct dwarf2_section_info *section,
3355 struct dwarf2_section_info *abbrev_section)
3357 struct objfile *objfile = dwarf2_per_objfile->objfile;
3359 dwarf2_read_section (objfile, section);
3360 dwarf2_read_section (objfile, abbrev_section);
3362 dwarf2_per_objfile->n_type_units
3363 = dwarf2_per_objfile->n_allocated_type_units
3365 dwarf2_per_objfile->all_type_units
3366 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3368 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3370 for (uint32_t i = 0; i < map.tu_count; ++i)
3372 struct signatured_type *sig_type;
3375 cu_offset type_offset_in_tu;
3377 sect_offset sect_off
3378 = (sect_offset) (extract_unsigned_integer
3379 (map.tu_table_reordered + i * map.offset_size,
3381 map.dwarf5_byte_order));
3383 comp_unit_head cu_header;
3384 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3386 section->buffer + to_underlying (sect_off),
3389 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3390 struct signatured_type);
3391 sig_type->signature = cu_header.signature;
3392 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3393 sig_type->per_cu.is_debug_types = 1;
3394 sig_type->per_cu.section = section;
3395 sig_type->per_cu.sect_off = sect_off;
3396 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3397 sig_type->per_cu.v.quick
3398 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3399 struct dwarf2_per_cu_quick_data);
3401 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3404 dwarf2_per_objfile->all_type_units[i] = sig_type;
3407 dwarf2_per_objfile->signatured_types = sig_types_hash;
3410 /* Read the address map data from the mapped index, and use it to
3411 populate the objfile's psymtabs_addrmap. */
3414 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3415 struct mapped_index *index)
3417 struct objfile *objfile = dwarf2_per_objfile->objfile;
3418 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3419 const gdb_byte *iter, *end;
3420 struct addrmap *mutable_map;
3423 auto_obstack temp_obstack;
3425 mutable_map = addrmap_create_mutable (&temp_obstack);
3427 iter = index->address_table.data ();
3428 end = iter + index->address_table.size ();
3430 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3434 ULONGEST hi, lo, cu_index;
3435 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3437 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3439 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3444 complaint (&symfile_complaints,
3445 _(".gdb_index address table has invalid range (%s - %s)"),
3446 hex_string (lo), hex_string (hi));
3450 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3452 complaint (&symfile_complaints,
3453 _(".gdb_index address table has invalid CU number %u"),
3454 (unsigned) cu_index);
3458 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3459 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3460 addrmap_set_empty (mutable_map, lo, hi - 1,
3461 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3464 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3465 &objfile->objfile_obstack);
3468 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3469 populate the objfile's psymtabs_addrmap. */
3472 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3473 struct dwarf2_section_info *section)
3475 struct objfile *objfile = dwarf2_per_objfile->objfile;
3476 bfd *abfd = objfile->obfd;
3477 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3478 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3479 SECT_OFF_TEXT (objfile));
3481 auto_obstack temp_obstack;
3482 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3484 std::unordered_map<sect_offset,
3485 dwarf2_per_cu_data *,
3486 gdb::hash_enum<sect_offset>>
3487 debug_info_offset_to_per_cu;
3488 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3490 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3491 const auto insertpair
3492 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3493 if (!insertpair.second)
3495 warning (_("Section .debug_aranges in %s has duplicate "
3496 "debug_info_offset %u, ignoring .debug_aranges."),
3497 objfile_name (objfile), to_underlying (per_cu->sect_off));
3502 dwarf2_read_section (objfile, section);
3504 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3506 const gdb_byte *addr = section->buffer;
3508 while (addr < section->buffer + section->size)
3510 const gdb_byte *const entry_addr = addr;
3511 unsigned int bytes_read;
3513 const LONGEST entry_length = read_initial_length (abfd, addr,
3517 const gdb_byte *const entry_end = addr + entry_length;
3518 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3519 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3520 if (addr + entry_length > section->buffer + section->size)
3522 warning (_("Section .debug_aranges in %s entry at offset %zu "
3523 "length %s exceeds section length %s, "
3524 "ignoring .debug_aranges."),
3525 objfile_name (objfile), entry_addr - section->buffer,
3526 plongest (bytes_read + entry_length),
3527 pulongest (section->size));
3531 /* The version number. */
3532 const uint16_t version = read_2_bytes (abfd, addr);
3536 warning (_("Section .debug_aranges in %s entry at offset %zu "
3537 "has unsupported version %d, ignoring .debug_aranges."),
3538 objfile_name (objfile), entry_addr - section->buffer,
3543 const uint64_t debug_info_offset
3544 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3545 addr += offset_size;
3546 const auto per_cu_it
3547 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3548 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3550 warning (_("Section .debug_aranges in %s entry at offset %zu "
3551 "debug_info_offset %s does not exists, "
3552 "ignoring .debug_aranges."),
3553 objfile_name (objfile), entry_addr - section->buffer,
3554 pulongest (debug_info_offset));
3557 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3559 const uint8_t address_size = *addr++;
3560 if (address_size < 1 || address_size > 8)
3562 warning (_("Section .debug_aranges in %s entry at offset %zu "
3563 "address_size %u is invalid, ignoring .debug_aranges."),
3564 objfile_name (objfile), entry_addr - section->buffer,
3569 const uint8_t segment_selector_size = *addr++;
3570 if (segment_selector_size != 0)
3572 warning (_("Section .debug_aranges in %s entry at offset %zu "
3573 "segment_selector_size %u is not supported, "
3574 "ignoring .debug_aranges."),
3575 objfile_name (objfile), entry_addr - section->buffer,
3576 segment_selector_size);
3580 /* Must pad to an alignment boundary that is twice the address
3581 size. It is undocumented by the DWARF standard but GCC does
3583 for (size_t padding = ((-(addr - section->buffer))
3584 & (2 * address_size - 1));
3585 padding > 0; padding--)
3588 warning (_("Section .debug_aranges in %s entry at offset %zu "
3589 "padding is not zero, ignoring .debug_aranges."),
3590 objfile_name (objfile), entry_addr - section->buffer);
3596 if (addr + 2 * address_size > entry_end)
3598 warning (_("Section .debug_aranges in %s entry at offset %zu "
3599 "address list is not properly terminated, "
3600 "ignoring .debug_aranges."),
3601 objfile_name (objfile), entry_addr - section->buffer);
3604 ULONGEST start = extract_unsigned_integer (addr, address_size,
3606 addr += address_size;
3607 ULONGEST length = extract_unsigned_integer (addr, address_size,
3609 addr += address_size;
3610 if (start == 0 && length == 0)
3612 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3614 /* Symbol was eliminated due to a COMDAT group. */
3617 ULONGEST end = start + length;
3618 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3619 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3620 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3624 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3625 &objfile->objfile_obstack);
3628 /* The hash function for strings in the mapped index. This is the same as
3629 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3630 implementation. This is necessary because the hash function is tied to the
3631 format of the mapped index file. The hash values do not have to match with
3634 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3637 mapped_index_string_hash (int index_version, const void *p)
3639 const unsigned char *str = (const unsigned char *) p;
3643 while ((c = *str++) != 0)
3645 if (index_version >= 5)
3647 r = r * 67 + c - 113;
3653 /* Find a slot in the mapped index INDEX for the object named NAME.
3654 If NAME is found, set *VEC_OUT to point to the CU vector in the
3655 constant pool and return true. If NAME cannot be found, return
3659 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3660 offset_type **vec_out)
3663 offset_type slot, step;
3664 int (*cmp) (const char *, const char *);
3666 gdb::unique_xmalloc_ptr<char> without_params;
3667 if (current_language->la_language == language_cplus
3668 || current_language->la_language == language_fortran
3669 || current_language->la_language == language_d)
3671 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3674 if (strchr (name, '(') != NULL)
3676 without_params = cp_remove_params (name);
3678 if (without_params != NULL)
3679 name = without_params.get ();
3683 /* Index version 4 did not support case insensitive searches. But the
3684 indices for case insensitive languages are built in lowercase, therefore
3685 simulate our NAME being searched is also lowercased. */
3686 hash = mapped_index_string_hash ((index->version == 4
3687 && case_sensitivity == case_sensitive_off
3688 ? 5 : index->version),
3691 slot = hash & (index->symbol_table.size () - 1);
3692 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3693 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3699 const auto &bucket = index->symbol_table[slot];
3700 if (bucket.name == 0 && bucket.vec == 0)
3703 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3704 if (!cmp (name, str))
3706 *vec_out = (offset_type *) (index->constant_pool
3707 + MAYBE_SWAP (bucket.vec));
3711 slot = (slot + step) & (index->symbol_table.size () - 1);
3715 /* A helper function that reads the .gdb_index from SECTION and fills
3716 in MAP. FILENAME is the name of the file containing the section;
3717 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3718 ok to use deprecated sections.
3720 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3721 out parameters that are filled in with information about the CU and
3722 TU lists in the section.
3724 Returns 1 if all went well, 0 otherwise. */
3727 read_index_from_section (struct objfile *objfile,
3728 const char *filename,
3730 struct dwarf2_section_info *section,
3731 struct mapped_index *map,
3732 const gdb_byte **cu_list,
3733 offset_type *cu_list_elements,
3734 const gdb_byte **types_list,
3735 offset_type *types_list_elements)
3737 const gdb_byte *addr;
3738 offset_type version;
3739 offset_type *metadata;
3742 if (dwarf2_section_empty_p (section))
3745 /* Older elfutils strip versions could keep the section in the main
3746 executable while splitting it for the separate debug info file. */
3747 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3750 dwarf2_read_section (objfile, section);
3752 addr = section->buffer;
3753 /* Version check. */
3754 version = MAYBE_SWAP (*(offset_type *) addr);
3755 /* Versions earlier than 3 emitted every copy of a psymbol. This
3756 causes the index to behave very poorly for certain requests. Version 3
3757 contained incomplete addrmap. So, it seems better to just ignore such
3761 static int warning_printed = 0;
3762 if (!warning_printed)
3764 warning (_("Skipping obsolete .gdb_index section in %s."),
3766 warning_printed = 1;
3770 /* Index version 4 uses a different hash function than index version
3773 Versions earlier than 6 did not emit psymbols for inlined
3774 functions. Using these files will cause GDB not to be able to
3775 set breakpoints on inlined functions by name, so we ignore these
3776 indices unless the user has done
3777 "set use-deprecated-index-sections on". */
3778 if (version < 6 && !deprecated_ok)
3780 static int warning_printed = 0;
3781 if (!warning_printed)
3784 Skipping deprecated .gdb_index section in %s.\n\
3785 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3786 to use the section anyway."),
3788 warning_printed = 1;
3792 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3793 of the TU (for symbols coming from TUs),
3794 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3795 Plus gold-generated indices can have duplicate entries for global symbols,
3796 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3797 These are just performance bugs, and we can't distinguish gdb-generated
3798 indices from gold-generated ones, so issue no warning here. */
3800 /* Indexes with higher version than the one supported by GDB may be no
3801 longer backward compatible. */
3805 map->version = version;
3806 map->total_size = section->size;
3808 metadata = (offset_type *) (addr + sizeof (offset_type));
3811 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3812 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3816 *types_list = addr + MAYBE_SWAP (metadata[i]);
3817 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3818 - MAYBE_SWAP (metadata[i]))
3822 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3823 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3825 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3828 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3829 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3831 = gdb::array_view<mapped_index::symbol_table_slot>
3832 ((mapped_index::symbol_table_slot *) symbol_table,
3833 (mapped_index::symbol_table_slot *) symbol_table_end);
3836 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3841 /* Read .gdb_index. If everything went ok, initialize the "quick"
3842 elements of all the CUs and return 1. Otherwise, return 0. */
3845 dwarf2_read_index (struct objfile *objfile)
3847 struct mapped_index local_map, *map;
3848 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3849 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3850 struct dwz_file *dwz;
3851 struct dwarf2_per_objfile *dwarf2_per_objfile
3852 = get_dwarf2_per_objfile (objfile);
3854 if (!read_index_from_section (objfile, objfile_name (objfile),
3855 use_deprecated_index_sections,
3856 &dwarf2_per_objfile->gdb_index, &local_map,
3857 &cu_list, &cu_list_elements,
3858 &types_list, &types_list_elements))
3861 /* Don't use the index if it's empty. */
3862 if (local_map.symbol_table.empty ())
3865 /* If there is a .dwz file, read it so we can get its CU list as
3867 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3870 struct mapped_index dwz_map;
3871 const gdb_byte *dwz_types_ignore;
3872 offset_type dwz_types_elements_ignore;
3874 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3876 &dwz->gdb_index, &dwz_map,
3877 &dwz_list, &dwz_list_elements,
3879 &dwz_types_elements_ignore))
3881 warning (_("could not read '.gdb_index' section from %s; skipping"),
3882 bfd_get_filename (dwz->dwz_bfd));
3887 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3890 if (types_list_elements)
3892 struct dwarf2_section_info *section;
3894 /* We can only handle a single .debug_types when we have an
3896 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3899 section = VEC_index (dwarf2_section_info_def,
3900 dwarf2_per_objfile->types, 0);
3902 create_signatured_type_table_from_index (objfile, section, types_list,
3903 types_list_elements);
3906 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3908 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3909 map = new (map) mapped_index ();
3912 dwarf2_per_objfile->index_table = map;
3913 dwarf2_per_objfile->using_index = 1;
3914 dwarf2_per_objfile->quick_file_names_table =
3915 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3920 /* die_reader_func for dw2_get_file_names. */
3923 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3924 const gdb_byte *info_ptr,
3925 struct die_info *comp_unit_die,
3929 struct dwarf2_cu *cu = reader->cu;
3930 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3931 struct dwarf2_per_objfile *dwarf2_per_objfile
3932 = cu->per_cu->dwarf2_per_objfile;
3933 struct objfile *objfile = dwarf2_per_objfile->objfile;
3934 struct dwarf2_per_cu_data *lh_cu;
3935 struct attribute *attr;
3938 struct quick_file_names *qfn;
3940 gdb_assert (! this_cu->is_debug_types);
3942 /* Our callers never want to match partial units -- instead they
3943 will match the enclosing full CU. */
3944 if (comp_unit_die->tag == DW_TAG_partial_unit)
3946 this_cu->v.quick->no_file_data = 1;
3954 sect_offset line_offset {};
3956 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3959 struct quick_file_names find_entry;
3961 line_offset = (sect_offset) DW_UNSND (attr);
3963 /* We may have already read in this line header (TU line header sharing).
3964 If we have we're done. */
3965 find_entry.hash.dwo_unit = cu->dwo_unit;
3966 find_entry.hash.line_sect_off = line_offset;
3967 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3968 &find_entry, INSERT);
3971 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3975 lh = dwarf_decode_line_header (line_offset, cu);
3979 lh_cu->v.quick->no_file_data = 1;
3983 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3984 qfn->hash.dwo_unit = cu->dwo_unit;
3985 qfn->hash.line_sect_off = line_offset;
3986 gdb_assert (slot != NULL);
3989 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3991 qfn->num_file_names = lh->file_names.size ();
3993 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3994 for (i = 0; i < lh->file_names.size (); ++i)
3995 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3996 qfn->real_names = NULL;
3998 lh_cu->v.quick->file_names = qfn;
4001 /* A helper for the "quick" functions which attempts to read the line
4002 table for THIS_CU. */
4004 static struct quick_file_names *
4005 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4007 /* This should never be called for TUs. */
4008 gdb_assert (! this_cu->is_debug_types);
4009 /* Nor type unit groups. */
4010 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4012 if (this_cu->v.quick->file_names != NULL)
4013 return this_cu->v.quick->file_names;
4014 /* If we know there is no line data, no point in looking again. */
4015 if (this_cu->v.quick->no_file_data)
4018 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4020 if (this_cu->v.quick->no_file_data)
4022 return this_cu->v.quick->file_names;
4025 /* A helper for the "quick" functions which computes and caches the
4026 real path for a given file name from the line table. */
4029 dw2_get_real_path (struct objfile *objfile,
4030 struct quick_file_names *qfn, int index)
4032 if (qfn->real_names == NULL)
4033 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4034 qfn->num_file_names, const char *);
4036 if (qfn->real_names[index] == NULL)
4037 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4039 return qfn->real_names[index];
4042 static struct symtab *
4043 dw2_find_last_source_symtab (struct objfile *objfile)
4045 struct dwarf2_per_objfile *dwarf2_per_objfile
4046 = get_dwarf2_per_objfile (objfile);
4047 int index = dwarf2_per_objfile->n_comp_units - 1;
4048 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4049 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4054 return compunit_primary_filetab (cust);
4057 /* Traversal function for dw2_forget_cached_source_info. */
4060 dw2_free_cached_file_names (void **slot, void *info)
4062 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4064 if (file_data->real_names)
4068 for (i = 0; i < file_data->num_file_names; ++i)
4070 xfree ((void*) file_data->real_names[i]);
4071 file_data->real_names[i] = NULL;
4079 dw2_forget_cached_source_info (struct objfile *objfile)
4081 struct dwarf2_per_objfile *dwarf2_per_objfile
4082 = get_dwarf2_per_objfile (objfile);
4084 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4085 dw2_free_cached_file_names, NULL);
4088 /* Helper function for dw2_map_symtabs_matching_filename that expands
4089 the symtabs and calls the iterator. */
4092 dw2_map_expand_apply (struct objfile *objfile,
4093 struct dwarf2_per_cu_data *per_cu,
4094 const char *name, const char *real_path,
4095 gdb::function_view<bool (symtab *)> callback)
4097 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4099 /* Don't visit already-expanded CUs. */
4100 if (per_cu->v.quick->compunit_symtab)
4103 /* This may expand more than one symtab, and we want to iterate over
4105 dw2_instantiate_symtab (per_cu);
4107 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4108 last_made, callback);
4111 /* Implementation of the map_symtabs_matching_filename method. */
4114 dw2_map_symtabs_matching_filename
4115 (struct objfile *objfile, const char *name, const char *real_path,
4116 gdb::function_view<bool (symtab *)> callback)
4119 const char *name_basename = lbasename (name);
4120 struct dwarf2_per_objfile *dwarf2_per_objfile
4121 = get_dwarf2_per_objfile (objfile);
4123 /* The rule is CUs specify all the files, including those used by
4124 any TU, so there's no need to scan TUs here. */
4126 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4129 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4130 struct quick_file_names *file_data;
4132 /* We only need to look at symtabs not already expanded. */
4133 if (per_cu->v.quick->compunit_symtab)
4136 file_data = dw2_get_file_names (per_cu);
4137 if (file_data == NULL)
4140 for (j = 0; j < file_data->num_file_names; ++j)
4142 const char *this_name = file_data->file_names[j];
4143 const char *this_real_name;
4145 if (compare_filenames_for_search (this_name, name))
4147 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4153 /* Before we invoke realpath, which can get expensive when many
4154 files are involved, do a quick comparison of the basenames. */
4155 if (! basenames_may_differ
4156 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4159 this_real_name = dw2_get_real_path (objfile, file_data, j);
4160 if (compare_filenames_for_search (this_real_name, name))
4162 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4168 if (real_path != NULL)
4170 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4171 gdb_assert (IS_ABSOLUTE_PATH (name));
4172 if (this_real_name != NULL
4173 && FILENAME_CMP (real_path, this_real_name) == 0)
4175 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4187 /* Struct used to manage iterating over all CUs looking for a symbol. */
4189 struct dw2_symtab_iterator
4191 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4192 struct dwarf2_per_objfile *dwarf2_per_objfile;
4193 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4194 int want_specific_block;
4195 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4196 Unused if !WANT_SPECIFIC_BLOCK. */
4198 /* The kind of symbol we're looking for. */
4200 /* The list of CUs from the index entry of the symbol,
4201 or NULL if not found. */
4203 /* The next element in VEC to look at. */
4205 /* The number of elements in VEC, or zero if there is no match. */
4207 /* Have we seen a global version of the symbol?
4208 If so we can ignore all further global instances.
4209 This is to work around gold/15646, inefficient gold-generated
4214 /* Initialize the index symtab iterator ITER.
4215 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4216 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4219 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4220 struct dwarf2_per_objfile *dwarf2_per_objfile,
4221 int want_specific_block,
4226 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4227 iter->want_specific_block = want_specific_block;
4228 iter->block_index = block_index;
4229 iter->domain = domain;
4231 iter->global_seen = 0;
4233 mapped_index *index = dwarf2_per_objfile->index_table;
4235 /* index is NULL if OBJF_READNOW. */
4236 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4237 iter->length = MAYBE_SWAP (*iter->vec);
4245 /* Return the next matching CU or NULL if there are no more. */
4247 static struct dwarf2_per_cu_data *
4248 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4250 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4252 for ( ; iter->next < iter->length; ++iter->next)
4254 offset_type cu_index_and_attrs =
4255 MAYBE_SWAP (iter->vec[iter->next + 1]);
4256 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4257 struct dwarf2_per_cu_data *per_cu;
4258 int want_static = iter->block_index != GLOBAL_BLOCK;
4259 /* This value is only valid for index versions >= 7. */
4260 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4261 gdb_index_symbol_kind symbol_kind =
4262 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4263 /* Only check the symbol attributes if they're present.
4264 Indices prior to version 7 don't record them,
4265 and indices >= 7 may elide them for certain symbols
4266 (gold does this). */
4268 (dwarf2_per_objfile->index_table->version >= 7
4269 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4271 /* Don't crash on bad data. */
4272 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4273 + dwarf2_per_objfile->n_type_units))
4275 complaint (&symfile_complaints,
4276 _(".gdb_index entry has bad CU index"
4278 objfile_name (dwarf2_per_objfile->objfile));
4282 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4284 /* Skip if already read in. */
4285 if (per_cu->v.quick->compunit_symtab)
4288 /* Check static vs global. */
4291 if (iter->want_specific_block
4292 && want_static != is_static)
4294 /* Work around gold/15646. */
4295 if (!is_static && iter->global_seen)
4298 iter->global_seen = 1;
4301 /* Only check the symbol's kind if it has one. */
4304 switch (iter->domain)
4307 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4308 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4309 /* Some types are also in VAR_DOMAIN. */
4310 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4314 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4318 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4333 static struct compunit_symtab *
4334 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4335 const char *name, domain_enum domain)
4337 struct compunit_symtab *stab_best = NULL;
4338 struct dwarf2_per_objfile *dwarf2_per_objfile
4339 = get_dwarf2_per_objfile (objfile);
4341 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4343 struct dw2_symtab_iterator iter;
4344 struct dwarf2_per_cu_data *per_cu;
4346 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4348 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4350 struct symbol *sym, *with_opaque = NULL;
4351 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4352 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4353 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4355 sym = block_find_symbol (block, name, domain,
4356 block_find_non_opaque_type_preferred,
4359 /* Some caution must be observed with overloaded functions
4360 and methods, since the index will not contain any overload
4361 information (but NAME might contain it). */
4364 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4366 if (with_opaque != NULL
4367 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4370 /* Keep looking through other CUs. */
4377 dw2_print_stats (struct objfile *objfile)
4379 struct dwarf2_per_objfile *dwarf2_per_objfile
4380 = get_dwarf2_per_objfile (objfile);
4381 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4384 for (int i = 0; i < total; ++i)
4386 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4388 if (!per_cu->v.quick->compunit_symtab)
4391 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4392 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4395 /* This dumps minimal information about the index.
4396 It is called via "mt print objfiles".
4397 One use is to verify .gdb_index has been loaded by the
4398 gdb.dwarf2/gdb-index.exp testcase. */
4401 dw2_dump (struct objfile *objfile)
4403 struct dwarf2_per_objfile *dwarf2_per_objfile
4404 = get_dwarf2_per_objfile (objfile);
4406 gdb_assert (dwarf2_per_objfile->using_index);
4407 printf_filtered (".gdb_index:");
4408 if (dwarf2_per_objfile->index_table != NULL)
4410 printf_filtered (" version %d\n",
4411 dwarf2_per_objfile->index_table->version);
4414 printf_filtered (" faked for \"readnow\"\n");
4415 printf_filtered ("\n");
4419 dw2_relocate (struct objfile *objfile,
4420 const struct section_offsets *new_offsets,
4421 const struct section_offsets *delta)
4423 /* There's nothing to relocate here. */
4427 dw2_expand_symtabs_for_function (struct objfile *objfile,
4428 const char *func_name)
4430 struct dwarf2_per_objfile *dwarf2_per_objfile
4431 = get_dwarf2_per_objfile (objfile);
4433 struct dw2_symtab_iterator iter;
4434 struct dwarf2_per_cu_data *per_cu;
4436 /* Note: It doesn't matter what we pass for block_index here. */
4437 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4440 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4441 dw2_instantiate_symtab (per_cu);
4446 dw2_expand_all_symtabs (struct objfile *objfile)
4448 struct dwarf2_per_objfile *dwarf2_per_objfile
4449 = get_dwarf2_per_objfile (objfile);
4450 int total_units = (dwarf2_per_objfile->n_comp_units
4451 + dwarf2_per_objfile->n_type_units);
4453 for (int i = 0; i < total_units; ++i)
4455 struct dwarf2_per_cu_data *per_cu
4456 = dw2_get_cutu (dwarf2_per_objfile, i);
4458 dw2_instantiate_symtab (per_cu);
4463 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4464 const char *fullname)
4466 struct dwarf2_per_objfile *dwarf2_per_objfile
4467 = get_dwarf2_per_objfile (objfile);
4469 /* We don't need to consider type units here.
4470 This is only called for examining code, e.g. expand_line_sal.
4471 There can be an order of magnitude (or more) more type units
4472 than comp units, and we avoid them if we can. */
4474 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4477 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4478 struct quick_file_names *file_data;
4480 /* We only need to look at symtabs not already expanded. */
4481 if (per_cu->v.quick->compunit_symtab)
4484 file_data = dw2_get_file_names (per_cu);
4485 if (file_data == NULL)
4488 for (j = 0; j < file_data->num_file_names; ++j)
4490 const char *this_fullname = file_data->file_names[j];
4492 if (filename_cmp (this_fullname, fullname) == 0)
4494 dw2_instantiate_symtab (per_cu);
4502 dw2_map_matching_symbols (struct objfile *objfile,
4503 const char * name, domain_enum domain,
4505 int (*callback) (struct block *,
4506 struct symbol *, void *),
4507 void *data, symbol_name_match_type match,
4508 symbol_compare_ftype *ordered_compare)
4510 /* Currently unimplemented; used for Ada. The function can be called if the
4511 current language is Ada for a non-Ada objfile using GNU index. As Ada
4512 does not look for non-Ada symbols this function should just return. */
4515 /* Symbol name matcher for .gdb_index names.
4517 Symbol names in .gdb_index have a few particularities:
4519 - There's no indication of which is the language of each symbol.
4521 Since each language has its own symbol name matching algorithm,
4522 and we don't know which language is the right one, we must match
4523 each symbol against all languages. This would be a potential
4524 performance problem if it were not mitigated by the
4525 mapped_index::name_components lookup table, which significantly
4526 reduces the number of times we need to call into this matcher,
4527 making it a non-issue.
4529 - Symbol names in the index have no overload (parameter)
4530 information. I.e., in C++, "foo(int)" and "foo(long)" both
4531 appear as "foo" in the index, for example.
4533 This means that the lookup names passed to the symbol name
4534 matcher functions must have no parameter information either
4535 because (e.g.) symbol search name "foo" does not match
4536 lookup-name "foo(int)" [while swapping search name for lookup
4539 class gdb_index_symbol_name_matcher
4542 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4543 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4545 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4546 Returns true if any matcher matches. */
4547 bool matches (const char *symbol_name);
4550 /* A reference to the lookup name we're matching against. */
4551 const lookup_name_info &m_lookup_name;
4553 /* A vector holding all the different symbol name matchers, for all
4555 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4558 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4559 (const lookup_name_info &lookup_name)
4560 : m_lookup_name (lookup_name)
4562 /* Prepare the vector of comparison functions upfront, to avoid
4563 doing the same work for each symbol. Care is taken to avoid
4564 matching with the same matcher more than once if/when multiple
4565 languages use the same matcher function. */
4566 auto &matchers = m_symbol_name_matcher_funcs;
4567 matchers.reserve (nr_languages);
4569 matchers.push_back (default_symbol_name_matcher);
4571 for (int i = 0; i < nr_languages; i++)
4573 const language_defn *lang = language_def ((enum language) i);
4574 if (lang->la_get_symbol_name_matcher != NULL)
4576 symbol_name_matcher_ftype *name_matcher
4577 = lang->la_get_symbol_name_matcher (m_lookup_name);
4579 /* Don't insert the same comparison routine more than once.
4580 Note that we do this linear walk instead of a cheaper
4581 sorted insert, or use a std::set or something like that,
4582 because relative order of function addresses is not
4583 stable. This is not a problem in practice because the
4584 number of supported languages is low, and the cost here
4585 is tiny compared to the number of searches we'll do
4586 afterwards using this object. */
4587 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4589 matchers.push_back (name_matcher);
4595 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4597 for (auto matches_name : m_symbol_name_matcher_funcs)
4598 if (matches_name (symbol_name, m_lookup_name, NULL))
4604 /* Starting from a search name, return the string that finds the upper
4605 bound of all strings that start with SEARCH_NAME in a sorted name
4606 list. Returns the empty string to indicate that the upper bound is
4607 the end of the list. */
4610 make_sort_after_prefix_name (const char *search_name)
4612 /* When looking to complete "func", we find the upper bound of all
4613 symbols that start with "func" by looking for where we'd insert
4614 the closest string that would follow "func" in lexicographical
4615 order. Usually, that's "func"-with-last-character-incremented,
4616 i.e. "fund". Mind non-ASCII characters, though. Usually those
4617 will be UTF-8 multi-byte sequences, but we can't be certain.
4618 Especially mind the 0xff character, which is a valid character in
4619 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4620 rule out compilers allowing it in identifiers. Note that
4621 conveniently, strcmp/strcasecmp are specified to compare
4622 characters interpreted as unsigned char. So what we do is treat
4623 the whole string as a base 256 number composed of a sequence of
4624 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4625 to 0, and carries 1 to the following more-significant position.
4626 If the very first character in SEARCH_NAME ends up incremented
4627 and carries/overflows, then the upper bound is the end of the
4628 list. The string after the empty string is also the empty
4631 Some examples of this operation:
4633 SEARCH_NAME => "+1" RESULT
4637 "\xff" "a" "\xff" => "\xff" "b"
4642 Then, with these symbols for example:
4648 completing "func" looks for symbols between "func" and
4649 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4650 which finds "func" and "func1", but not "fund".
4654 funcÿ (Latin1 'ÿ' [0xff])
4658 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4659 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4663 ÿÿ (Latin1 'ÿ' [0xff])
4666 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4667 the end of the list.
4669 std::string after = search_name;
4670 while (!after.empty () && (unsigned char) after.back () == 0xff)
4672 if (!after.empty ())
4673 after.back () = (unsigned char) after.back () + 1;
4677 /* See declaration. */
4679 std::pair<std::vector<name_component>::const_iterator,
4680 std::vector<name_component>::const_iterator>
4681 mapped_index_base::find_name_components_bounds
4682 (const lookup_name_info &lookup_name_without_params) const
4685 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4688 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4690 /* Comparison function object for lower_bound that matches against a
4691 given symbol name. */
4692 auto lookup_compare_lower = [&] (const name_component &elem,
4695 const char *elem_qualified = this->symbol_name_at (elem.idx);
4696 const char *elem_name = elem_qualified + elem.name_offset;
4697 return name_cmp (elem_name, name) < 0;
4700 /* Comparison function object for upper_bound that matches against a
4701 given symbol name. */
4702 auto lookup_compare_upper = [&] (const char *name,
4703 const name_component &elem)
4705 const char *elem_qualified = this->symbol_name_at (elem.idx);
4706 const char *elem_name = elem_qualified + elem.name_offset;
4707 return name_cmp (name, elem_name) < 0;
4710 auto begin = this->name_components.begin ();
4711 auto end = this->name_components.end ();
4713 /* Find the lower bound. */
4716 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4719 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4722 /* Find the upper bound. */
4725 if (lookup_name_without_params.completion_mode ())
4727 /* In completion mode, we want UPPER to point past all
4728 symbols names that have the same prefix. I.e., with
4729 these symbols, and completing "func":
4731 function << lower bound
4733 other_function << upper bound
4735 We find the upper bound by looking for the insertion
4736 point of "func"-with-last-character-incremented,
4738 std::string after = make_sort_after_prefix_name (cplus);
4741 return std::lower_bound (lower, end, after.c_str (),
4742 lookup_compare_lower);
4745 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4748 return {lower, upper};
4751 /* See declaration. */
4754 mapped_index_base::build_name_components ()
4756 if (!this->name_components.empty ())
4759 this->name_components_casing = case_sensitivity;
4761 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4763 /* The code below only knows how to break apart components of C++
4764 symbol names (and other languages that use '::' as
4765 namespace/module separator). If we add support for wild matching
4766 to some language that uses some other operator (E.g., Ada, Go and
4767 D use '.'), then we'll need to try splitting the symbol name
4768 according to that language too. Note that Ada does support wild
4769 matching, but doesn't currently support .gdb_index. */
4770 auto count = this->symbol_name_count ();
4771 for (offset_type idx = 0; idx < count; idx++)
4773 if (this->symbol_name_slot_invalid (idx))
4776 const char *name = this->symbol_name_at (idx);
4778 /* Add each name component to the name component table. */
4779 unsigned int previous_len = 0;
4780 for (unsigned int current_len = cp_find_first_component (name);
4781 name[current_len] != '\0';
4782 current_len += cp_find_first_component (name + current_len))
4784 gdb_assert (name[current_len] == ':');
4785 this->name_components.push_back ({previous_len, idx});
4786 /* Skip the '::'. */
4788 previous_len = current_len;
4790 this->name_components.push_back ({previous_len, idx});
4793 /* Sort name_components elements by name. */
4794 auto name_comp_compare = [&] (const name_component &left,
4795 const name_component &right)
4797 const char *left_qualified = this->symbol_name_at (left.idx);
4798 const char *right_qualified = this->symbol_name_at (right.idx);
4800 const char *left_name = left_qualified + left.name_offset;
4801 const char *right_name = right_qualified + right.name_offset;
4803 return name_cmp (left_name, right_name) < 0;
4806 std::sort (this->name_components.begin (),
4807 this->name_components.end (),
4811 /* Helper for dw2_expand_symtabs_matching that works with a
4812 mapped_index_base instead of the containing objfile. This is split
4813 to a separate function in order to be able to unit test the
4814 name_components matching using a mock mapped_index_base. For each
4815 symbol name that matches, calls MATCH_CALLBACK, passing it the
4816 symbol's index in the mapped_index_base symbol table. */
4819 dw2_expand_symtabs_matching_symbol
4820 (mapped_index_base &index,
4821 const lookup_name_info &lookup_name_in,
4822 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4823 enum search_domain kind,
4824 gdb::function_view<void (offset_type)> match_callback)
4826 lookup_name_info lookup_name_without_params
4827 = lookup_name_in.make_ignore_params ();
4828 gdb_index_symbol_name_matcher lookup_name_matcher
4829 (lookup_name_without_params);
4831 /* Build the symbol name component sorted vector, if we haven't
4833 index.build_name_components ();
4835 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4837 /* Now for each symbol name in range, check to see if we have a name
4838 match, and if so, call the MATCH_CALLBACK callback. */
4840 /* The same symbol may appear more than once in the range though.
4841 E.g., if we're looking for symbols that complete "w", and we have
4842 a symbol named "w1::w2", we'll find the two name components for
4843 that same symbol in the range. To be sure we only call the
4844 callback once per symbol, we first collect the symbol name
4845 indexes that matched in a temporary vector and ignore
4847 std::vector<offset_type> matches;
4848 matches.reserve (std::distance (bounds.first, bounds.second));
4850 for (; bounds.first != bounds.second; ++bounds.first)
4852 const char *qualified = index.symbol_name_at (bounds.first->idx);
4854 if (!lookup_name_matcher.matches (qualified)
4855 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4858 matches.push_back (bounds.first->idx);
4861 std::sort (matches.begin (), matches.end ());
4863 /* Finally call the callback, once per match. */
4865 for (offset_type idx : matches)
4869 match_callback (idx);
4874 /* Above we use a type wider than idx's for 'prev', since 0 and
4875 (offset_type)-1 are both possible values. */
4876 static_assert (sizeof (prev) > sizeof (offset_type), "");
4881 namespace selftests { namespace dw2_expand_symtabs_matching {
4883 /* A mock .gdb_index/.debug_names-like name index table, enough to
4884 exercise dw2_expand_symtabs_matching_symbol, which works with the
4885 mapped_index_base interface. Builds an index from the symbol list
4886 passed as parameter to the constructor. */
4887 class mock_mapped_index : public mapped_index_base
4890 mock_mapped_index (gdb::array_view<const char *> symbols)
4891 : m_symbol_table (symbols)
4894 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4896 /* Return the number of names in the symbol table. */
4897 virtual size_t symbol_name_count () const
4899 return m_symbol_table.size ();
4902 /* Get the name of the symbol at IDX in the symbol table. */
4903 virtual const char *symbol_name_at (offset_type idx) const
4905 return m_symbol_table[idx];
4909 gdb::array_view<const char *> m_symbol_table;
4912 /* Convenience function that converts a NULL pointer to a "<null>"
4913 string, to pass to print routines. */
4916 string_or_null (const char *str)
4918 return str != NULL ? str : "<null>";
4921 /* Check if a lookup_name_info built from
4922 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4923 index. EXPECTED_LIST is the list of expected matches, in expected
4924 matching order. If no match expected, then an empty list is
4925 specified. Returns true on success. On failure prints a warning
4926 indicating the file:line that failed, and returns false. */
4929 check_match (const char *file, int line,
4930 mock_mapped_index &mock_index,
4931 const char *name, symbol_name_match_type match_type,
4932 bool completion_mode,
4933 std::initializer_list<const char *> expected_list)
4935 lookup_name_info lookup_name (name, match_type, completion_mode);
4937 bool matched = true;
4939 auto mismatch = [&] (const char *expected_str,
4942 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4943 "expected=\"%s\", got=\"%s\"\n"),
4945 (match_type == symbol_name_match_type::FULL
4947 name, string_or_null (expected_str), string_or_null (got));
4951 auto expected_it = expected_list.begin ();
4952 auto expected_end = expected_list.end ();
4954 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4956 [&] (offset_type idx)
4958 const char *matched_name = mock_index.symbol_name_at (idx);
4959 const char *expected_str
4960 = expected_it == expected_end ? NULL : *expected_it++;
4962 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4963 mismatch (expected_str, matched_name);
4966 const char *expected_str
4967 = expected_it == expected_end ? NULL : *expected_it++;
4968 if (expected_str != NULL)
4969 mismatch (expected_str, NULL);
4974 /* The symbols added to the mock mapped_index for testing (in
4976 static const char *test_symbols[] = {
4985 "ns2::tmpl<int>::foo2",
4986 "(anonymous namespace)::A::B::C",
4988 /* These are used to check that the increment-last-char in the
4989 matching algorithm for completion doesn't match "t1_fund" when
4990 completing "t1_func". */
4996 /* A UTF-8 name with multi-byte sequences to make sure that
4997 cp-name-parser understands this as a single identifier ("função"
4998 is "function" in PT). */
5001 /* \377 (0xff) is Latin1 'ÿ'. */
5004 /* \377 (0xff) is Latin1 'ÿ'. */
5008 /* A name with all sorts of complications. Starts with "z" to make
5009 it easier for the completion tests below. */
5010 #define Z_SYM_NAME \
5011 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5012 "::tuple<(anonymous namespace)::ui*, " \
5013 "std::default_delete<(anonymous namespace)::ui>, void>"
5018 /* Returns true if the mapped_index_base::find_name_component_bounds
5019 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5020 in completion mode. */
5023 check_find_bounds_finds (mapped_index_base &index,
5024 const char *search_name,
5025 gdb::array_view<const char *> expected_syms)
5027 lookup_name_info lookup_name (search_name,
5028 symbol_name_match_type::FULL, true);
5030 auto bounds = index.find_name_components_bounds (lookup_name);
5032 size_t distance = std::distance (bounds.first, bounds.second);
5033 if (distance != expected_syms.size ())
5036 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5038 auto nc_elem = bounds.first + exp_elem;
5039 const char *qualified = index.symbol_name_at (nc_elem->idx);
5040 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5047 /* Test the lower-level mapped_index::find_name_component_bounds
5051 test_mapped_index_find_name_component_bounds ()
5053 mock_mapped_index mock_index (test_symbols);
5055 mock_index.build_name_components ();
5057 /* Test the lower-level mapped_index::find_name_component_bounds
5058 method in completion mode. */
5060 static const char *expected_syms[] = {
5065 SELF_CHECK (check_find_bounds_finds (mock_index,
5066 "t1_func", expected_syms));
5069 /* Check that the increment-last-char in the name matching algorithm
5070 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5072 static const char *expected_syms1[] = {
5076 SELF_CHECK (check_find_bounds_finds (mock_index,
5077 "\377", expected_syms1));
5079 static const char *expected_syms2[] = {
5082 SELF_CHECK (check_find_bounds_finds (mock_index,
5083 "\377\377", expected_syms2));
5087 /* Test dw2_expand_symtabs_matching_symbol. */
5090 test_dw2_expand_symtabs_matching_symbol ()
5092 mock_mapped_index mock_index (test_symbols);
5094 /* We let all tests run until the end even if some fails, for debug
5096 bool any_mismatch = false;
5098 /* Create the expected symbols list (an initializer_list). Needed
5099 because lists have commas, and we need to pass them to CHECK,
5100 which is a macro. */
5101 #define EXPECT(...) { __VA_ARGS__ }
5103 /* Wrapper for check_match that passes down the current
5104 __FILE__/__LINE__. */
5105 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5106 any_mismatch |= !check_match (__FILE__, __LINE__, \
5108 NAME, MATCH_TYPE, COMPLETION_MODE, \
5111 /* Identity checks. */
5112 for (const char *sym : test_symbols)
5114 /* Should be able to match all existing symbols. */
5115 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5118 /* Should be able to match all existing symbols with
5120 std::string with_params = std::string (sym) + "(int)";
5121 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5124 /* Should be able to match all existing symbols with
5125 parameters and qualifiers. */
5126 with_params = std::string (sym) + " ( int ) const";
5127 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5130 /* This should really find sym, but cp-name-parser.y doesn't
5131 know about lvalue/rvalue qualifiers yet. */
5132 with_params = std::string (sym) + " ( int ) &&";
5133 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5137 /* Check that the name matching algorithm for completion doesn't get
5138 confused with Latin1 'ÿ' / 0xff. */
5140 static const char str[] = "\377";
5141 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5142 EXPECT ("\377", "\377\377123"));
5145 /* Check that the increment-last-char in the matching algorithm for
5146 completion doesn't match "t1_fund" when completing "t1_func". */
5148 static const char str[] = "t1_func";
5149 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5150 EXPECT ("t1_func", "t1_func1"));
5153 /* Check that completion mode works at each prefix of the expected
5156 static const char str[] = "function(int)";
5157 size_t len = strlen (str);
5160 for (size_t i = 1; i < len; i++)
5162 lookup.assign (str, i);
5163 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5164 EXPECT ("function"));
5168 /* While "w" is a prefix of both components, the match function
5169 should still only be called once. */
5171 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5173 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5177 /* Same, with a "complicated" symbol. */
5179 static const char str[] = Z_SYM_NAME;
5180 size_t len = strlen (str);
5183 for (size_t i = 1; i < len; i++)
5185 lookup.assign (str, i);
5186 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5187 EXPECT (Z_SYM_NAME));
5191 /* In FULL mode, an incomplete symbol doesn't match. */
5193 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5197 /* A complete symbol with parameters matches any overload, since the
5198 index has no overload info. */
5200 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5201 EXPECT ("std::zfunction", "std::zfunction2"));
5202 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5203 EXPECT ("std::zfunction", "std::zfunction2"));
5204 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5205 EXPECT ("std::zfunction", "std::zfunction2"));
5208 /* Check that whitespace is ignored appropriately. A symbol with a
5209 template argument list. */
5211 static const char expected[] = "ns::foo<int>";
5212 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5214 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5218 /* Check that whitespace is ignored appropriately. A symbol with a
5219 template argument list that includes a pointer. */
5221 static const char expected[] = "ns::foo<char*>";
5222 /* Try both completion and non-completion modes. */
5223 static const bool completion_mode[2] = {false, true};
5224 for (size_t i = 0; i < 2; i++)
5226 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5227 completion_mode[i], EXPECT (expected));
5228 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5229 completion_mode[i], EXPECT (expected));
5231 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5232 completion_mode[i], EXPECT (expected));
5233 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5234 completion_mode[i], EXPECT (expected));
5239 /* Check method qualifiers are ignored. */
5240 static const char expected[] = "ns::foo<char*>";
5241 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5242 symbol_name_match_type::FULL, true, EXPECT (expected));
5243 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5244 symbol_name_match_type::FULL, true, EXPECT (expected));
5245 CHECK_MATCH ("foo < char * > ( int ) const",
5246 symbol_name_match_type::WILD, true, EXPECT (expected));
5247 CHECK_MATCH ("foo < char * > ( int ) &&",
5248 symbol_name_match_type::WILD, true, EXPECT (expected));
5251 /* Test lookup names that don't match anything. */
5253 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5256 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5260 /* Some wild matching tests, exercising "(anonymous namespace)",
5261 which should not be confused with a parameter list. */
5263 static const char *syms[] = {
5267 "A :: B :: C ( int )",
5272 for (const char *s : syms)
5274 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5275 EXPECT ("(anonymous namespace)::A::B::C"));
5280 static const char expected[] = "ns2::tmpl<int>::foo2";
5281 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5283 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5287 SELF_CHECK (!any_mismatch);
5296 test_mapped_index_find_name_component_bounds ();
5297 test_dw2_expand_symtabs_matching_symbol ();
5300 }} // namespace selftests::dw2_expand_symtabs_matching
5302 #endif /* GDB_SELF_TEST */
5304 /* If FILE_MATCHER is NULL or if PER_CU has
5305 dwarf2_per_cu_quick_data::MARK set (see
5306 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5307 EXPANSION_NOTIFY on it. */
5310 dw2_expand_symtabs_matching_one
5311 (struct dwarf2_per_cu_data *per_cu,
5312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5313 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5315 if (file_matcher == NULL || per_cu->v.quick->mark)
5317 bool symtab_was_null
5318 = (per_cu->v.quick->compunit_symtab == NULL);
5320 dw2_instantiate_symtab (per_cu);
5322 if (expansion_notify != NULL
5324 && per_cu->v.quick->compunit_symtab != NULL)
5325 expansion_notify (per_cu->v.quick->compunit_symtab);
5329 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5330 matched, to expand corresponding CUs that were marked. IDX is the
5331 index of the symbol name that matched. */
5334 dw2_expand_marked_cus
5335 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5336 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5337 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5340 offset_type *vec, vec_len, vec_idx;
5341 bool global_seen = false;
5342 mapped_index &index = *dwarf2_per_objfile->index_table;
5344 vec = (offset_type *) (index.constant_pool
5345 + MAYBE_SWAP (index.symbol_table[idx].vec));
5346 vec_len = MAYBE_SWAP (vec[0]);
5347 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5349 struct dwarf2_per_cu_data *per_cu;
5350 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5351 /* This value is only valid for index versions >= 7. */
5352 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5353 gdb_index_symbol_kind symbol_kind =
5354 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5355 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5356 /* Only check the symbol attributes if they're present.
5357 Indices prior to version 7 don't record them,
5358 and indices >= 7 may elide them for certain symbols
5359 (gold does this). */
5362 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5364 /* Work around gold/15646. */
5367 if (!is_static && global_seen)
5373 /* Only check the symbol's kind if it has one. */
5378 case VARIABLES_DOMAIN:
5379 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5382 case FUNCTIONS_DOMAIN:
5383 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5387 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5395 /* Don't crash on bad data. */
5396 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5397 + dwarf2_per_objfile->n_type_units))
5399 complaint (&symfile_complaints,
5400 _(".gdb_index entry has bad CU index"
5402 objfile_name (dwarf2_per_objfile->objfile));
5406 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5407 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5412 /* If FILE_MATCHER is non-NULL, set all the
5413 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5414 that match FILE_MATCHER. */
5417 dw_expand_symtabs_matching_file_matcher
5418 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5419 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5421 if (file_matcher == NULL)
5424 objfile *const objfile = dwarf2_per_objfile->objfile;
5426 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5428 NULL, xcalloc, xfree));
5429 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5431 NULL, xcalloc, xfree));
5433 /* The rule is CUs specify all the files, including those used by
5434 any TU, so there's no need to scan TUs here. */
5436 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5439 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5440 struct quick_file_names *file_data;
5445 per_cu->v.quick->mark = 0;
5447 /* We only need to look at symtabs not already expanded. */
5448 if (per_cu->v.quick->compunit_symtab)
5451 file_data = dw2_get_file_names (per_cu);
5452 if (file_data == NULL)
5455 if (htab_find (visited_not_found.get (), file_data) != NULL)
5457 else if (htab_find (visited_found.get (), file_data) != NULL)
5459 per_cu->v.quick->mark = 1;
5463 for (j = 0; j < file_data->num_file_names; ++j)
5465 const char *this_real_name;
5467 if (file_matcher (file_data->file_names[j], false))
5469 per_cu->v.quick->mark = 1;
5473 /* Before we invoke realpath, which can get expensive when many
5474 files are involved, do a quick comparison of the basenames. */
5475 if (!basenames_may_differ
5476 && !file_matcher (lbasename (file_data->file_names[j]),
5480 this_real_name = dw2_get_real_path (objfile, file_data, j);
5481 if (file_matcher (this_real_name, false))
5483 per_cu->v.quick->mark = 1;
5488 slot = htab_find_slot (per_cu->v.quick->mark
5489 ? visited_found.get ()
5490 : visited_not_found.get (),
5497 dw2_expand_symtabs_matching
5498 (struct objfile *objfile,
5499 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5500 const lookup_name_info &lookup_name,
5501 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5502 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5503 enum search_domain kind)
5505 struct dwarf2_per_objfile *dwarf2_per_objfile
5506 = get_dwarf2_per_objfile (objfile);
5508 /* index_table is NULL if OBJF_READNOW. */
5509 if (!dwarf2_per_objfile->index_table)
5512 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5514 mapped_index &index = *dwarf2_per_objfile->index_table;
5516 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5518 kind, [&] (offset_type idx)
5520 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5521 expansion_notify, kind);
5525 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5528 static struct compunit_symtab *
5529 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5534 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5535 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5538 if (cust->includes == NULL)
5541 for (i = 0; cust->includes[i]; ++i)
5543 struct compunit_symtab *s = cust->includes[i];
5545 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5553 static struct compunit_symtab *
5554 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5555 struct bound_minimal_symbol msymbol,
5557 struct obj_section *section,
5560 struct dwarf2_per_cu_data *data;
5561 struct compunit_symtab *result;
5563 if (!objfile->psymtabs_addrmap)
5566 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5571 if (warn_if_readin && data->v.quick->compunit_symtab)
5572 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5573 paddress (get_objfile_arch (objfile), pc));
5576 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5578 gdb_assert (result != NULL);
5583 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5584 void *data, int need_fullname)
5586 struct dwarf2_per_objfile *dwarf2_per_objfile
5587 = get_dwarf2_per_objfile (objfile);
5589 if (!dwarf2_per_objfile->filenames_cache)
5591 dwarf2_per_objfile->filenames_cache.emplace ();
5593 htab_up visited (htab_create_alloc (10,
5594 htab_hash_pointer, htab_eq_pointer,
5595 NULL, xcalloc, xfree));
5597 /* The rule is CUs specify all the files, including those used
5598 by any TU, so there's no need to scan TUs here. We can
5599 ignore file names coming from already-expanded CUs. */
5601 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5603 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5605 if (per_cu->v.quick->compunit_symtab)
5607 void **slot = htab_find_slot (visited.get (),
5608 per_cu->v.quick->file_names,
5611 *slot = per_cu->v.quick->file_names;
5615 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5617 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5618 struct quick_file_names *file_data;
5621 /* We only need to look at symtabs not already expanded. */
5622 if (per_cu->v.quick->compunit_symtab)
5625 file_data = dw2_get_file_names (per_cu);
5626 if (file_data == NULL)
5629 slot = htab_find_slot (visited.get (), file_data, INSERT);
5632 /* Already visited. */
5637 for (int j = 0; j < file_data->num_file_names; ++j)
5639 const char *filename = file_data->file_names[j];
5640 dwarf2_per_objfile->filenames_cache->seen (filename);
5645 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5647 gdb::unique_xmalloc_ptr<char> this_real_name;
5650 this_real_name = gdb_realpath (filename);
5651 (*fun) (filename, this_real_name.get (), data);
5656 dw2_has_symbols (struct objfile *objfile)
5661 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5664 dw2_find_last_source_symtab,
5665 dw2_forget_cached_source_info,
5666 dw2_map_symtabs_matching_filename,
5671 dw2_expand_symtabs_for_function,
5672 dw2_expand_all_symtabs,
5673 dw2_expand_symtabs_with_fullname,
5674 dw2_map_matching_symbols,
5675 dw2_expand_symtabs_matching,
5676 dw2_find_pc_sect_compunit_symtab,
5678 dw2_map_symbol_filenames
5681 /* DWARF-5 debug_names reader. */
5683 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5684 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5686 /* A helper function that reads the .debug_names section in SECTION
5687 and fills in MAP. FILENAME is the name of the file containing the
5688 section; it is used for error reporting.
5690 Returns true if all went well, false otherwise. */
5693 read_debug_names_from_section (struct objfile *objfile,
5694 const char *filename,
5695 struct dwarf2_section_info *section,
5696 mapped_debug_names &map)
5698 if (dwarf2_section_empty_p (section))
5701 /* Older elfutils strip versions could keep the section in the main
5702 executable while splitting it for the separate debug info file. */
5703 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5706 dwarf2_read_section (objfile, section);
5708 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5710 const gdb_byte *addr = section->buffer;
5712 bfd *const abfd = get_section_bfd_owner (section);
5714 unsigned int bytes_read;
5715 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5718 map.dwarf5_is_dwarf64 = bytes_read != 4;
5719 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5720 if (bytes_read + length != section->size)
5722 /* There may be multiple per-CU indices. */
5723 warning (_("Section .debug_names in %s length %s does not match "
5724 "section length %s, ignoring .debug_names."),
5725 filename, plongest (bytes_read + length),
5726 pulongest (section->size));
5730 /* The version number. */
5731 uint16_t version = read_2_bytes (abfd, addr);
5735 warning (_("Section .debug_names in %s has unsupported version %d, "
5736 "ignoring .debug_names."),
5742 uint16_t padding = read_2_bytes (abfd, addr);
5746 warning (_("Section .debug_names in %s has unsupported padding %d, "
5747 "ignoring .debug_names."),
5752 /* comp_unit_count - The number of CUs in the CU list. */
5753 map.cu_count = read_4_bytes (abfd, addr);
5756 /* local_type_unit_count - The number of TUs in the local TU
5758 map.tu_count = read_4_bytes (abfd, addr);
5761 /* foreign_type_unit_count - The number of TUs in the foreign TU
5763 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5765 if (foreign_tu_count != 0)
5767 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5768 "ignoring .debug_names."),
5769 filename, static_cast<unsigned long> (foreign_tu_count));
5773 /* bucket_count - The number of hash buckets in the hash lookup
5775 map.bucket_count = read_4_bytes (abfd, addr);
5778 /* name_count - The number of unique names in the index. */
5779 map.name_count = read_4_bytes (abfd, addr);
5782 /* abbrev_table_size - The size in bytes of the abbreviations
5784 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5787 /* augmentation_string_size - The size in bytes of the augmentation
5788 string. This value is rounded up to a multiple of 4. */
5789 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5791 map.augmentation_is_gdb = ((augmentation_string_size
5792 == sizeof (dwarf5_augmentation))
5793 && memcmp (addr, dwarf5_augmentation,
5794 sizeof (dwarf5_augmentation)) == 0);
5795 augmentation_string_size += (-augmentation_string_size) & 3;
5796 addr += augmentation_string_size;
5799 map.cu_table_reordered = addr;
5800 addr += map.cu_count * map.offset_size;
5802 /* List of Local TUs */
5803 map.tu_table_reordered = addr;
5804 addr += map.tu_count * map.offset_size;
5806 /* Hash Lookup Table */
5807 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5808 addr += map.bucket_count * 4;
5809 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5810 addr += map.name_count * 4;
5813 map.name_table_string_offs_reordered = addr;
5814 addr += map.name_count * map.offset_size;
5815 map.name_table_entry_offs_reordered = addr;
5816 addr += map.name_count * map.offset_size;
5818 const gdb_byte *abbrev_table_start = addr;
5821 unsigned int bytes_read;
5822 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5827 const auto insertpair
5828 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5829 if (!insertpair.second)
5831 warning (_("Section .debug_names in %s has duplicate index %s, "
5832 "ignoring .debug_names."),
5833 filename, pulongest (index_num));
5836 mapped_debug_names::index_val &indexval = insertpair.first->second;
5837 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5842 mapped_debug_names::index_val::attr attr;
5843 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5845 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5847 if (attr.form == DW_FORM_implicit_const)
5849 attr.implicit_const = read_signed_leb128 (abfd, addr,
5853 if (attr.dw_idx == 0 && attr.form == 0)
5855 indexval.attr_vec.push_back (std::move (attr));
5858 if (addr != abbrev_table_start + abbrev_table_size)
5860 warning (_("Section .debug_names in %s has abbreviation_table "
5861 "of size %zu vs. written as %u, ignoring .debug_names."),
5862 filename, addr - abbrev_table_start, abbrev_table_size);
5865 map.entry_pool = addr;
5870 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5874 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5875 const mapped_debug_names &map,
5876 dwarf2_section_info §ion,
5877 bool is_dwz, int base_offset)
5879 sect_offset sect_off_prev;
5880 for (uint32_t i = 0; i <= map.cu_count; ++i)
5882 sect_offset sect_off_next;
5883 if (i < map.cu_count)
5886 = (sect_offset) (extract_unsigned_integer
5887 (map.cu_table_reordered + i * map.offset_size,
5889 map.dwarf5_byte_order));
5892 sect_off_next = (sect_offset) section.size;
5895 const ULONGEST length = sect_off_next - sect_off_prev;
5896 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5897 = create_cu_from_index_list (dwarf2_per_objfile, §ion, is_dwz,
5898 sect_off_prev, length);
5900 sect_off_prev = sect_off_next;
5904 /* Read the CU list from the mapped index, and use it to create all
5905 the CU objects for this dwarf2_per_objfile. */
5908 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5909 const mapped_debug_names &map,
5910 const mapped_debug_names &dwz_map)
5912 struct objfile *objfile = dwarf2_per_objfile->objfile;
5914 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5915 dwarf2_per_objfile->all_comp_units
5916 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5917 dwarf2_per_objfile->n_comp_units);
5919 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5920 dwarf2_per_objfile->info,
5922 0 /* base_offset */);
5924 if (dwz_map.cu_count == 0)
5927 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5928 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5930 map.cu_count /* base_offset */);
5933 /* Read .debug_names. If everything went ok, initialize the "quick"
5934 elements of all the CUs and return true. Otherwise, return false. */
5937 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5939 mapped_debug_names local_map (dwarf2_per_objfile);
5940 mapped_debug_names dwz_map (dwarf2_per_objfile);
5941 struct objfile *objfile = dwarf2_per_objfile->objfile;
5943 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5944 &dwarf2_per_objfile->debug_names,
5948 /* Don't use the index if it's empty. */
5949 if (local_map.name_count == 0)
5952 /* If there is a .dwz file, read it so we can get its CU list as
5954 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5957 if (!read_debug_names_from_section (objfile,
5958 bfd_get_filename (dwz->dwz_bfd),
5959 &dwz->debug_names, dwz_map))
5961 warning (_("could not read '.debug_names' section from %s; skipping"),
5962 bfd_get_filename (dwz->dwz_bfd));
5967 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5969 if (local_map.tu_count != 0)
5971 /* We can only handle a single .debug_types when we have an
5973 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5976 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5977 dwarf2_per_objfile->types, 0);
5979 create_signatured_type_table_from_debug_names
5980 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5983 create_addrmap_from_aranges (dwarf2_per_objfile,
5984 &dwarf2_per_objfile->debug_aranges);
5986 dwarf2_per_objfile->debug_names_table.reset
5987 (new mapped_debug_names (dwarf2_per_objfile));
5988 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5989 dwarf2_per_objfile->using_index = 1;
5990 dwarf2_per_objfile->quick_file_names_table =
5991 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5996 /* Symbol name hashing function as specified by DWARF-5. */
5999 dwarf5_djb_hash (const char *str_)
6001 const unsigned char *str = (const unsigned char *) str_;
6003 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6004 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6006 uint32_t hash = 5381;
6007 while (int c = *str++)
6008 hash = hash * 33 + tolower (c);
6012 /* Type used to manage iterating over all CUs looking for a symbol for
6015 class dw2_debug_names_iterator
6018 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6019 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6020 dw2_debug_names_iterator (const mapped_debug_names &map,
6021 bool want_specific_block,
6022 block_enum block_index, domain_enum domain,
6024 : m_map (map), m_want_specific_block (want_specific_block),
6025 m_block_index (block_index), m_domain (domain),
6026 m_addr (find_vec_in_debug_names (map, name))
6029 dw2_debug_names_iterator (const mapped_debug_names &map,
6030 search_domain search, uint32_t namei)
6033 m_addr (find_vec_in_debug_names (map, namei))
6036 /* Return the next matching CU or NULL if there are no more. */
6037 dwarf2_per_cu_data *next ();
6040 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6042 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6045 /* The internalized form of .debug_names. */
6046 const mapped_debug_names &m_map;
6048 /* If true, only look for symbols that match BLOCK_INDEX. */
6049 const bool m_want_specific_block = false;
6051 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6052 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6054 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6056 /* The kind of symbol we're looking for. */
6057 const domain_enum m_domain = UNDEF_DOMAIN;
6058 const search_domain m_search = ALL_DOMAIN;
6060 /* The list of CUs from the index entry of the symbol, or NULL if
6062 const gdb_byte *m_addr;
6066 mapped_debug_names::namei_to_name (uint32_t namei) const
6068 const ULONGEST namei_string_offs
6069 = extract_unsigned_integer ((name_table_string_offs_reordered
6070 + namei * offset_size),
6073 return read_indirect_string_at_offset
6074 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6077 /* Find a slot in .debug_names for the object named NAME. If NAME is
6078 found, return pointer to its pool data. If NAME cannot be found,
6082 dw2_debug_names_iterator::find_vec_in_debug_names
6083 (const mapped_debug_names &map, const char *name)
6085 int (*cmp) (const char *, const char *);
6087 if (current_language->la_language == language_cplus
6088 || current_language->la_language == language_fortran
6089 || current_language->la_language == language_d)
6091 /* NAME is already canonical. Drop any qualifiers as
6092 .debug_names does not contain any. */
6094 if (strchr (name, '(') != NULL)
6096 gdb::unique_xmalloc_ptr<char> without_params
6097 = cp_remove_params (name);
6099 if (without_params != NULL)
6101 name = without_params.get();
6106 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6108 const uint32_t full_hash = dwarf5_djb_hash (name);
6110 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6111 (map.bucket_table_reordered
6112 + (full_hash % map.bucket_count)), 4,
6113 map.dwarf5_byte_order);
6117 if (namei >= map.name_count)
6119 complaint (&symfile_complaints,
6120 _("Wrong .debug_names with name index %u but name_count=%u "
6122 namei, map.name_count,
6123 objfile_name (map.dwarf2_per_objfile->objfile));
6129 const uint32_t namei_full_hash
6130 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6131 (map.hash_table_reordered + namei), 4,
6132 map.dwarf5_byte_order);
6133 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6136 if (full_hash == namei_full_hash)
6138 const char *const namei_string = map.namei_to_name (namei);
6140 #if 0 /* An expensive sanity check. */
6141 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6143 complaint (&symfile_complaints,
6144 _("Wrong .debug_names hash for string at index %u "
6146 namei, objfile_name (dwarf2_per_objfile->objfile));
6151 if (cmp (namei_string, name) == 0)
6153 const ULONGEST namei_entry_offs
6154 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6155 + namei * map.offset_size),
6156 map.offset_size, map.dwarf5_byte_order);
6157 return map.entry_pool + namei_entry_offs;
6162 if (namei >= map.name_count)
6168 dw2_debug_names_iterator::find_vec_in_debug_names
6169 (const mapped_debug_names &map, uint32_t namei)
6171 if (namei >= map.name_count)
6173 complaint (&symfile_complaints,
6174 _("Wrong .debug_names with name index %u but name_count=%u "
6176 namei, map.name_count,
6177 objfile_name (map.dwarf2_per_objfile->objfile));
6181 const ULONGEST namei_entry_offs
6182 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6183 + namei * map.offset_size),
6184 map.offset_size, map.dwarf5_byte_order);
6185 return map.entry_pool + namei_entry_offs;
6188 /* See dw2_debug_names_iterator. */
6190 dwarf2_per_cu_data *
6191 dw2_debug_names_iterator::next ()
6196 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6197 struct objfile *objfile = dwarf2_per_objfile->objfile;
6198 bfd *const abfd = objfile->obfd;
6202 unsigned int bytes_read;
6203 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6204 m_addr += bytes_read;
6208 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6209 if (indexval_it == m_map.abbrev_map.cend ())
6211 complaint (&symfile_complaints,
6212 _("Wrong .debug_names undefined abbrev code %s "
6214 pulongest (abbrev), objfile_name (objfile));
6217 const mapped_debug_names::index_val &indexval = indexval_it->second;
6218 bool have_is_static = false;
6220 dwarf2_per_cu_data *per_cu = NULL;
6221 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6226 case DW_FORM_implicit_const:
6227 ull = attr.implicit_const;
6229 case DW_FORM_flag_present:
6233 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6234 m_addr += bytes_read;
6237 complaint (&symfile_complaints,
6238 _("Unsupported .debug_names form %s [in module %s]"),
6239 dwarf_form_name (attr.form),
6240 objfile_name (objfile));
6243 switch (attr.dw_idx)
6245 case DW_IDX_compile_unit:
6246 /* Don't crash on bad data. */
6247 if (ull >= dwarf2_per_objfile->n_comp_units)
6249 complaint (&symfile_complaints,
6250 _(".debug_names entry has bad CU index %s"
6253 objfile_name (dwarf2_per_objfile->objfile));
6256 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6258 case DW_IDX_type_unit:
6259 /* Don't crash on bad data. */
6260 if (ull >= dwarf2_per_objfile->n_type_units)
6262 complaint (&symfile_complaints,
6263 _(".debug_names entry has bad TU index %s"
6266 objfile_name (dwarf2_per_objfile->objfile));
6269 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6270 dwarf2_per_objfile->n_comp_units + ull);
6272 case DW_IDX_GNU_internal:
6273 if (!m_map.augmentation_is_gdb)
6275 have_is_static = true;
6278 case DW_IDX_GNU_external:
6279 if (!m_map.augmentation_is_gdb)
6281 have_is_static = true;
6287 /* Skip if already read in. */
6288 if (per_cu->v.quick->compunit_symtab)
6291 /* Check static vs global. */
6294 const bool want_static = m_block_index != GLOBAL_BLOCK;
6295 if (m_want_specific_block && want_static != is_static)
6299 /* Match dw2_symtab_iter_next, symbol_kind
6300 and debug_names::psymbol_tag. */
6304 switch (indexval.dwarf_tag)
6306 case DW_TAG_variable:
6307 case DW_TAG_subprogram:
6308 /* Some types are also in VAR_DOMAIN. */
6309 case DW_TAG_typedef:
6310 case DW_TAG_structure_type:
6317 switch (indexval.dwarf_tag)
6319 case DW_TAG_typedef:
6320 case DW_TAG_structure_type:
6327 switch (indexval.dwarf_tag)
6330 case DW_TAG_variable:
6340 /* Match dw2_expand_symtabs_matching, symbol_kind and
6341 debug_names::psymbol_tag. */
6344 case VARIABLES_DOMAIN:
6345 switch (indexval.dwarf_tag)
6347 case DW_TAG_variable:
6353 case FUNCTIONS_DOMAIN:
6354 switch (indexval.dwarf_tag)
6356 case DW_TAG_subprogram:
6363 switch (indexval.dwarf_tag)
6365 case DW_TAG_typedef:
6366 case DW_TAG_structure_type:
6379 static struct compunit_symtab *
6380 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6381 const char *name, domain_enum domain)
6383 const block_enum block_index = static_cast<block_enum> (block_index_int);
6384 struct dwarf2_per_objfile *dwarf2_per_objfile
6385 = get_dwarf2_per_objfile (objfile);
6387 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6390 /* index is NULL if OBJF_READNOW. */
6393 const auto &map = *mapp;
6395 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6396 block_index, domain, name);
6398 struct compunit_symtab *stab_best = NULL;
6399 struct dwarf2_per_cu_data *per_cu;
6400 while ((per_cu = iter.next ()) != NULL)
6402 struct symbol *sym, *with_opaque = NULL;
6403 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6404 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6405 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6407 sym = block_find_symbol (block, name, domain,
6408 block_find_non_opaque_type_preferred,
6411 /* Some caution must be observed with overloaded functions and
6412 methods, since the index will not contain any overload
6413 information (but NAME might contain it). */
6416 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6418 if (with_opaque != NULL
6419 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6422 /* Keep looking through other CUs. */
6428 /* This dumps minimal information about .debug_names. It is called
6429 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6430 uses this to verify that .debug_names has been loaded. */
6433 dw2_debug_names_dump (struct objfile *objfile)
6435 struct dwarf2_per_objfile *dwarf2_per_objfile
6436 = get_dwarf2_per_objfile (objfile);
6438 gdb_assert (dwarf2_per_objfile->using_index);
6439 printf_filtered (".debug_names:");
6440 if (dwarf2_per_objfile->debug_names_table)
6441 printf_filtered (" exists\n");
6443 printf_filtered (" faked for \"readnow\"\n");
6444 printf_filtered ("\n");
6448 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6449 const char *func_name)
6451 struct dwarf2_per_objfile *dwarf2_per_objfile
6452 = get_dwarf2_per_objfile (objfile);
6454 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6455 if (dwarf2_per_objfile->debug_names_table)
6457 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6459 /* Note: It doesn't matter what we pass for block_index here. */
6460 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6461 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6463 struct dwarf2_per_cu_data *per_cu;
6464 while ((per_cu = iter.next ()) != NULL)
6465 dw2_instantiate_symtab (per_cu);
6470 dw2_debug_names_expand_symtabs_matching
6471 (struct objfile *objfile,
6472 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6473 const lookup_name_info &lookup_name,
6474 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6475 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6476 enum search_domain kind)
6478 struct dwarf2_per_objfile *dwarf2_per_objfile
6479 = get_dwarf2_per_objfile (objfile);
6481 /* debug_names_table is NULL if OBJF_READNOW. */
6482 if (!dwarf2_per_objfile->debug_names_table)
6485 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6487 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6489 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6491 kind, [&] (offset_type namei)
6493 /* The name was matched, now expand corresponding CUs that were
6495 dw2_debug_names_iterator iter (map, kind, namei);
6497 struct dwarf2_per_cu_data *per_cu;
6498 while ((per_cu = iter.next ()) != NULL)
6499 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6504 const struct quick_symbol_functions dwarf2_debug_names_functions =
6507 dw2_find_last_source_symtab,
6508 dw2_forget_cached_source_info,
6509 dw2_map_symtabs_matching_filename,
6510 dw2_debug_names_lookup_symbol,
6512 dw2_debug_names_dump,
6514 dw2_debug_names_expand_symtabs_for_function,
6515 dw2_expand_all_symtabs,
6516 dw2_expand_symtabs_with_fullname,
6517 dw2_map_matching_symbols,
6518 dw2_debug_names_expand_symtabs_matching,
6519 dw2_find_pc_sect_compunit_symtab,
6521 dw2_map_symbol_filenames
6524 /* See symfile.h. */
6527 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6529 struct dwarf2_per_objfile *dwarf2_per_objfile
6530 = get_dwarf2_per_objfile (objfile);
6532 /* If we're about to read full symbols, don't bother with the
6533 indices. In this case we also don't care if some other debug
6534 format is making psymtabs, because they are all about to be
6536 if ((objfile->flags & OBJF_READNOW))
6540 dwarf2_per_objfile->using_index = 1;
6541 create_all_comp_units (dwarf2_per_objfile);
6542 create_all_type_units (dwarf2_per_objfile);
6543 dwarf2_per_objfile->quick_file_names_table =
6544 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6546 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6547 + dwarf2_per_objfile->n_type_units); ++i)
6549 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6551 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6552 struct dwarf2_per_cu_quick_data);
6555 /* Return 1 so that gdb sees the "quick" functions. However,
6556 these functions will be no-ops because we will have expanded
6558 *index_kind = dw_index_kind::GDB_INDEX;
6562 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6564 *index_kind = dw_index_kind::DEBUG_NAMES;
6568 if (dwarf2_read_index (objfile))
6570 *index_kind = dw_index_kind::GDB_INDEX;
6579 /* Build a partial symbol table. */
6582 dwarf2_build_psymtabs (struct objfile *objfile)
6584 struct dwarf2_per_objfile *dwarf2_per_objfile
6585 = get_dwarf2_per_objfile (objfile);
6587 if (objfile->global_psymbols.capacity () == 0
6588 && objfile->static_psymbols.capacity () == 0)
6589 init_psymbol_list (objfile, 1024);
6593 /* This isn't really ideal: all the data we allocate on the
6594 objfile's obstack is still uselessly kept around. However,
6595 freeing it seems unsafe. */
6596 psymtab_discarder psymtabs (objfile);
6597 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6600 CATCH (except, RETURN_MASK_ERROR)
6602 exception_print (gdb_stderr, except);
6607 /* Return the total length of the CU described by HEADER. */
6610 get_cu_length (const struct comp_unit_head *header)
6612 return header->initial_length_size + header->length;
6615 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6618 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6620 sect_offset bottom = cu_header->sect_off;
6621 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6623 return sect_off >= bottom && sect_off < top;
6626 /* Find the base address of the compilation unit for range lists and
6627 location lists. It will normally be specified by DW_AT_low_pc.
6628 In DWARF-3 draft 4, the base address could be overridden by
6629 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6630 compilation units with discontinuous ranges. */
6633 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6635 struct attribute *attr;
6638 cu->base_address = 0;
6640 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6643 cu->base_address = attr_value_as_address (attr);
6648 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6651 cu->base_address = attr_value_as_address (attr);
6657 /* Read in the comp unit header information from the debug_info at info_ptr.
6658 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6659 NOTE: This leaves members offset, first_die_offset to be filled in
6662 static const gdb_byte *
6663 read_comp_unit_head (struct comp_unit_head *cu_header,
6664 const gdb_byte *info_ptr,
6665 struct dwarf2_section_info *section,
6666 rcuh_kind section_kind)
6669 unsigned int bytes_read;
6670 const char *filename = get_section_file_name (section);
6671 bfd *abfd = get_section_bfd_owner (section);
6673 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6674 cu_header->initial_length_size = bytes_read;
6675 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6676 info_ptr += bytes_read;
6677 cu_header->version = read_2_bytes (abfd, info_ptr);
6679 if (cu_header->version < 5)
6680 switch (section_kind)
6682 case rcuh_kind::COMPILE:
6683 cu_header->unit_type = DW_UT_compile;
6685 case rcuh_kind::TYPE:
6686 cu_header->unit_type = DW_UT_type;
6689 internal_error (__FILE__, __LINE__,
6690 _("read_comp_unit_head: invalid section_kind"));
6694 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6695 (read_1_byte (abfd, info_ptr));
6697 switch (cu_header->unit_type)
6700 if (section_kind != rcuh_kind::COMPILE)
6701 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6702 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6706 section_kind = rcuh_kind::TYPE;
6709 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6710 "(is %d, should be %d or %d) [in module %s]"),
6711 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6714 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6717 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6720 info_ptr += bytes_read;
6721 if (cu_header->version < 5)
6723 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6726 signed_addr = bfd_get_sign_extend_vma (abfd);
6727 if (signed_addr < 0)
6728 internal_error (__FILE__, __LINE__,
6729 _("read_comp_unit_head: dwarf from non elf file"));
6730 cu_header->signed_addr_p = signed_addr;
6732 if (section_kind == rcuh_kind::TYPE)
6734 LONGEST type_offset;
6736 cu_header->signature = read_8_bytes (abfd, info_ptr);
6739 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6740 info_ptr += bytes_read;
6741 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6742 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6743 error (_("Dwarf Error: Too big type_offset in compilation unit "
6744 "header (is %s) [in module %s]"), plongest (type_offset),
6751 /* Helper function that returns the proper abbrev section for
6754 static struct dwarf2_section_info *
6755 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6757 struct dwarf2_section_info *abbrev;
6758 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6760 if (this_cu->is_dwz)
6761 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6763 abbrev = &dwarf2_per_objfile->abbrev;
6768 /* Subroutine of read_and_check_comp_unit_head and
6769 read_and_check_type_unit_head to simplify them.
6770 Perform various error checking on the header. */
6773 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6774 struct comp_unit_head *header,
6775 struct dwarf2_section_info *section,
6776 struct dwarf2_section_info *abbrev_section)
6778 const char *filename = get_section_file_name (section);
6780 if (header->version < 2 || header->version > 5)
6781 error (_("Dwarf Error: wrong version in compilation unit header "
6782 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6785 if (to_underlying (header->abbrev_sect_off)
6786 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6787 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6788 "(offset 0x%x + 6) [in module %s]"),
6789 to_underlying (header->abbrev_sect_off),
6790 to_underlying (header->sect_off),
6793 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6794 avoid potential 32-bit overflow. */
6795 if (((ULONGEST) header->sect_off + get_cu_length (header))
6797 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6798 "(offset 0x%x + 0) [in module %s]"),
6799 header->length, to_underlying (header->sect_off),
6803 /* Read in a CU/TU header and perform some basic error checking.
6804 The contents of the header are stored in HEADER.
6805 The result is a pointer to the start of the first DIE. */
6807 static const gdb_byte *
6808 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6809 struct comp_unit_head *header,
6810 struct dwarf2_section_info *section,
6811 struct dwarf2_section_info *abbrev_section,
6812 const gdb_byte *info_ptr,
6813 rcuh_kind section_kind)
6815 const gdb_byte *beg_of_comp_unit = info_ptr;
6817 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6819 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6821 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6823 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6829 /* Fetch the abbreviation table offset from a comp or type unit header. */
6832 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6833 struct dwarf2_section_info *section,
6834 sect_offset sect_off)
6836 bfd *abfd = get_section_bfd_owner (section);
6837 const gdb_byte *info_ptr;
6838 unsigned int initial_length_size, offset_size;
6841 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6842 info_ptr = section->buffer + to_underlying (sect_off);
6843 read_initial_length (abfd, info_ptr, &initial_length_size);
6844 offset_size = initial_length_size == 4 ? 4 : 8;
6845 info_ptr += initial_length_size;
6847 version = read_2_bytes (abfd, info_ptr);
6851 /* Skip unit type and address size. */
6855 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6858 /* Allocate a new partial symtab for file named NAME and mark this new
6859 partial symtab as being an include of PST. */
6862 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6863 struct objfile *objfile)
6865 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6867 if (!IS_ABSOLUTE_PATH (subpst->filename))
6869 /* It shares objfile->objfile_obstack. */
6870 subpst->dirname = pst->dirname;
6873 subpst->textlow = 0;
6874 subpst->texthigh = 0;
6876 subpst->dependencies
6877 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6878 subpst->dependencies[0] = pst;
6879 subpst->number_of_dependencies = 1;
6881 subpst->globals_offset = 0;
6882 subpst->n_global_syms = 0;
6883 subpst->statics_offset = 0;
6884 subpst->n_static_syms = 0;
6885 subpst->compunit_symtab = NULL;
6886 subpst->read_symtab = pst->read_symtab;
6889 /* No private part is necessary for include psymtabs. This property
6890 can be used to differentiate between such include psymtabs and
6891 the regular ones. */
6892 subpst->read_symtab_private = NULL;
6895 /* Read the Line Number Program data and extract the list of files
6896 included by the source file represented by PST. Build an include
6897 partial symtab for each of these included files. */
6900 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6901 struct die_info *die,
6902 struct partial_symtab *pst)
6905 struct attribute *attr;
6907 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6909 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6911 return; /* No linetable, so no includes. */
6913 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6914 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6918 hash_signatured_type (const void *item)
6920 const struct signatured_type *sig_type
6921 = (const struct signatured_type *) item;
6923 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6924 return sig_type->signature;
6928 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6930 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6931 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6933 return lhs->signature == rhs->signature;
6936 /* Allocate a hash table for signatured types. */
6939 allocate_signatured_type_table (struct objfile *objfile)
6941 return htab_create_alloc_ex (41,
6942 hash_signatured_type,
6945 &objfile->objfile_obstack,
6946 hashtab_obstack_allocate,
6947 dummy_obstack_deallocate);
6950 /* A helper function to add a signatured type CU to a table. */
6953 add_signatured_type_cu_to_table (void **slot, void *datum)
6955 struct signatured_type *sigt = (struct signatured_type *) *slot;
6956 struct signatured_type ***datap = (struct signatured_type ***) datum;
6964 /* A helper for create_debug_types_hash_table. Read types from SECTION
6965 and fill them into TYPES_HTAB. It will process only type units,
6966 therefore DW_UT_type. */
6969 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6970 struct dwo_file *dwo_file,
6971 dwarf2_section_info *section, htab_t &types_htab,
6972 rcuh_kind section_kind)
6974 struct objfile *objfile = dwarf2_per_objfile->objfile;
6975 struct dwarf2_section_info *abbrev_section;
6977 const gdb_byte *info_ptr, *end_ptr;
6979 abbrev_section = (dwo_file != NULL
6980 ? &dwo_file->sections.abbrev
6981 : &dwarf2_per_objfile->abbrev);
6983 if (dwarf_read_debug)
6984 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6985 get_section_name (section),
6986 get_section_file_name (abbrev_section));
6988 dwarf2_read_section (objfile, section);
6989 info_ptr = section->buffer;
6991 if (info_ptr == NULL)
6994 /* We can't set abfd until now because the section may be empty or
6995 not present, in which case the bfd is unknown. */
6996 abfd = get_section_bfd_owner (section);
6998 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6999 because we don't need to read any dies: the signature is in the
7002 end_ptr = info_ptr + section->size;
7003 while (info_ptr < end_ptr)
7005 struct signatured_type *sig_type;
7006 struct dwo_unit *dwo_tu;
7008 const gdb_byte *ptr = info_ptr;
7009 struct comp_unit_head header;
7010 unsigned int length;
7012 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7014 /* Initialize it due to a false compiler warning. */
7015 header.signature = -1;
7016 header.type_cu_offset_in_tu = (cu_offset) -1;
7018 /* We need to read the type's signature in order to build the hash
7019 table, but we don't need anything else just yet. */
7021 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7022 abbrev_section, ptr, section_kind);
7024 length = get_cu_length (&header);
7026 /* Skip dummy type units. */
7027 if (ptr >= info_ptr + length
7028 || peek_abbrev_code (abfd, ptr) == 0
7029 || header.unit_type != DW_UT_type)
7035 if (types_htab == NULL)
7038 types_htab = allocate_dwo_unit_table (objfile);
7040 types_htab = allocate_signatured_type_table (objfile);
7046 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7048 dwo_tu->dwo_file = dwo_file;
7049 dwo_tu->signature = header.signature;
7050 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7051 dwo_tu->section = section;
7052 dwo_tu->sect_off = sect_off;
7053 dwo_tu->length = length;
7057 /* N.B.: type_offset is not usable if this type uses a DWO file.
7058 The real type_offset is in the DWO file. */
7060 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7061 struct signatured_type);
7062 sig_type->signature = header.signature;
7063 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7064 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7065 sig_type->per_cu.is_debug_types = 1;
7066 sig_type->per_cu.section = section;
7067 sig_type->per_cu.sect_off = sect_off;
7068 sig_type->per_cu.length = length;
7071 slot = htab_find_slot (types_htab,
7072 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7074 gdb_assert (slot != NULL);
7077 sect_offset dup_sect_off;
7081 const struct dwo_unit *dup_tu
7082 = (const struct dwo_unit *) *slot;
7084 dup_sect_off = dup_tu->sect_off;
7088 const struct signatured_type *dup_tu
7089 = (const struct signatured_type *) *slot;
7091 dup_sect_off = dup_tu->per_cu.sect_off;
7094 complaint (&symfile_complaints,
7095 _("debug type entry at offset 0x%x is duplicate to"
7096 " the entry at offset 0x%x, signature %s"),
7097 to_underlying (sect_off), to_underlying (dup_sect_off),
7098 hex_string (header.signature));
7100 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7102 if (dwarf_read_debug > 1)
7103 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7104 to_underlying (sect_off),
7105 hex_string (header.signature));
7111 /* Create the hash table of all entries in the .debug_types
7112 (or .debug_types.dwo) section(s).
7113 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7114 otherwise it is NULL.
7116 The result is a pointer to the hash table or NULL if there are no types.
7118 Note: This function processes DWO files only, not DWP files. */
7121 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7122 struct dwo_file *dwo_file,
7123 VEC (dwarf2_section_info_def) *types,
7127 struct dwarf2_section_info *section;
7129 if (VEC_empty (dwarf2_section_info_def, types))
7133 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7135 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7136 types_htab, rcuh_kind::TYPE);
7139 /* Create the hash table of all entries in the .debug_types section,
7140 and initialize all_type_units.
7141 The result is zero if there is an error (e.g. missing .debug_types section),
7142 otherwise non-zero. */
7145 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7147 htab_t types_htab = NULL;
7148 struct signatured_type **iter;
7150 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7151 &dwarf2_per_objfile->info, types_htab,
7152 rcuh_kind::COMPILE);
7153 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7154 dwarf2_per_objfile->types, types_htab);
7155 if (types_htab == NULL)
7157 dwarf2_per_objfile->signatured_types = NULL;
7161 dwarf2_per_objfile->signatured_types = types_htab;
7163 dwarf2_per_objfile->n_type_units
7164 = dwarf2_per_objfile->n_allocated_type_units
7165 = htab_elements (types_htab);
7166 dwarf2_per_objfile->all_type_units =
7167 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7168 iter = &dwarf2_per_objfile->all_type_units[0];
7169 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7170 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7171 == dwarf2_per_objfile->n_type_units);
7176 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7177 If SLOT is non-NULL, it is the entry to use in the hash table.
7178 Otherwise we find one. */
7180 static struct signatured_type *
7181 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7184 struct objfile *objfile = dwarf2_per_objfile->objfile;
7185 int n_type_units = dwarf2_per_objfile->n_type_units;
7186 struct signatured_type *sig_type;
7188 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7190 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7192 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7193 dwarf2_per_objfile->n_allocated_type_units = 1;
7194 dwarf2_per_objfile->n_allocated_type_units *= 2;
7195 dwarf2_per_objfile->all_type_units
7196 = XRESIZEVEC (struct signatured_type *,
7197 dwarf2_per_objfile->all_type_units,
7198 dwarf2_per_objfile->n_allocated_type_units);
7199 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7201 dwarf2_per_objfile->n_type_units = n_type_units;
7203 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7204 struct signatured_type);
7205 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7206 sig_type->signature = sig;
7207 sig_type->per_cu.is_debug_types = 1;
7208 if (dwarf2_per_objfile->using_index)
7210 sig_type->per_cu.v.quick =
7211 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7212 struct dwarf2_per_cu_quick_data);
7217 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7220 gdb_assert (*slot == NULL);
7222 /* The rest of sig_type must be filled in by the caller. */
7226 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7227 Fill in SIG_ENTRY with DWO_ENTRY. */
7230 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7231 struct signatured_type *sig_entry,
7232 struct dwo_unit *dwo_entry)
7234 /* Make sure we're not clobbering something we don't expect to. */
7235 gdb_assert (! sig_entry->per_cu.queued);
7236 gdb_assert (sig_entry->per_cu.cu == NULL);
7237 if (dwarf2_per_objfile->using_index)
7239 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7240 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7243 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7244 gdb_assert (sig_entry->signature == dwo_entry->signature);
7245 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7246 gdb_assert (sig_entry->type_unit_group == NULL);
7247 gdb_assert (sig_entry->dwo_unit == NULL);
7249 sig_entry->per_cu.section = dwo_entry->section;
7250 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7251 sig_entry->per_cu.length = dwo_entry->length;
7252 sig_entry->per_cu.reading_dwo_directly = 1;
7253 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7254 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7255 sig_entry->dwo_unit = dwo_entry;
7258 /* Subroutine of lookup_signatured_type.
7259 If we haven't read the TU yet, create the signatured_type data structure
7260 for a TU to be read in directly from a DWO file, bypassing the stub.
7261 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7262 using .gdb_index, then when reading a CU we want to stay in the DWO file
7263 containing that CU. Otherwise we could end up reading several other DWO
7264 files (due to comdat folding) to process the transitive closure of all the
7265 mentioned TUs, and that can be slow. The current DWO file will have every
7266 type signature that it needs.
7267 We only do this for .gdb_index because in the psymtab case we already have
7268 to read all the DWOs to build the type unit groups. */
7270 static struct signatured_type *
7271 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7273 struct dwarf2_per_objfile *dwarf2_per_objfile
7274 = cu->per_cu->dwarf2_per_objfile;
7275 struct objfile *objfile = dwarf2_per_objfile->objfile;
7276 struct dwo_file *dwo_file;
7277 struct dwo_unit find_dwo_entry, *dwo_entry;
7278 struct signatured_type find_sig_entry, *sig_entry;
7281 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7283 /* If TU skeletons have been removed then we may not have read in any
7285 if (dwarf2_per_objfile->signatured_types == NULL)
7287 dwarf2_per_objfile->signatured_types
7288 = allocate_signatured_type_table (objfile);
7291 /* We only ever need to read in one copy of a signatured type.
7292 Use the global signatured_types array to do our own comdat-folding
7293 of types. If this is the first time we're reading this TU, and
7294 the TU has an entry in .gdb_index, replace the recorded data from
7295 .gdb_index with this TU. */
7297 find_sig_entry.signature = sig;
7298 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7299 &find_sig_entry, INSERT);
7300 sig_entry = (struct signatured_type *) *slot;
7302 /* We can get here with the TU already read, *or* in the process of being
7303 read. Don't reassign the global entry to point to this DWO if that's
7304 the case. Also note that if the TU is already being read, it may not
7305 have come from a DWO, the program may be a mix of Fission-compiled
7306 code and non-Fission-compiled code. */
7308 /* Have we already tried to read this TU?
7309 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7310 needn't exist in the global table yet). */
7311 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7314 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7315 dwo_unit of the TU itself. */
7316 dwo_file = cu->dwo_unit->dwo_file;
7318 /* Ok, this is the first time we're reading this TU. */
7319 if (dwo_file->tus == NULL)
7321 find_dwo_entry.signature = sig;
7322 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7323 if (dwo_entry == NULL)
7326 /* If the global table doesn't have an entry for this TU, add one. */
7327 if (sig_entry == NULL)
7328 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7330 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7331 sig_entry->per_cu.tu_read = 1;
7335 /* Subroutine of lookup_signatured_type.
7336 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7337 then try the DWP file. If the TU stub (skeleton) has been removed then
7338 it won't be in .gdb_index. */
7340 static struct signatured_type *
7341 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7343 struct dwarf2_per_objfile *dwarf2_per_objfile
7344 = cu->per_cu->dwarf2_per_objfile;
7345 struct objfile *objfile = dwarf2_per_objfile->objfile;
7346 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7347 struct dwo_unit *dwo_entry;
7348 struct signatured_type find_sig_entry, *sig_entry;
7351 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7352 gdb_assert (dwp_file != NULL);
7354 /* If TU skeletons have been removed then we may not have read in any
7356 if (dwarf2_per_objfile->signatured_types == NULL)
7358 dwarf2_per_objfile->signatured_types
7359 = allocate_signatured_type_table (objfile);
7362 find_sig_entry.signature = sig;
7363 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7364 &find_sig_entry, INSERT);
7365 sig_entry = (struct signatured_type *) *slot;
7367 /* Have we already tried to read this TU?
7368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7369 needn't exist in the global table yet). */
7370 if (sig_entry != NULL)
7373 if (dwp_file->tus == NULL)
7375 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7376 sig, 1 /* is_debug_types */);
7377 if (dwo_entry == NULL)
7380 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7381 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7386 /* Lookup a signature based type for DW_FORM_ref_sig8.
7387 Returns NULL if signature SIG is not present in the table.
7388 It is up to the caller to complain about this. */
7390 static struct signatured_type *
7391 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7393 struct dwarf2_per_objfile *dwarf2_per_objfile
7394 = cu->per_cu->dwarf2_per_objfile;
7397 && dwarf2_per_objfile->using_index)
7399 /* We're in a DWO/DWP file, and we're using .gdb_index.
7400 These cases require special processing. */
7401 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7402 return lookup_dwo_signatured_type (cu, sig);
7404 return lookup_dwp_signatured_type (cu, sig);
7408 struct signatured_type find_entry, *entry;
7410 if (dwarf2_per_objfile->signatured_types == NULL)
7412 find_entry.signature = sig;
7413 entry = ((struct signatured_type *)
7414 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7419 /* Low level DIE reading support. */
7421 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7424 init_cu_die_reader (struct die_reader_specs *reader,
7425 struct dwarf2_cu *cu,
7426 struct dwarf2_section_info *section,
7427 struct dwo_file *dwo_file)
7429 gdb_assert (section->readin && section->buffer != NULL);
7430 reader->abfd = get_section_bfd_owner (section);
7432 reader->dwo_file = dwo_file;
7433 reader->die_section = section;
7434 reader->buffer = section->buffer;
7435 reader->buffer_end = section->buffer + section->size;
7436 reader->comp_dir = NULL;
7439 /* Subroutine of init_cutu_and_read_dies to simplify it.
7440 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7441 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7444 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7445 from it to the DIE in the DWO. If NULL we are skipping the stub.
7446 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7447 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7448 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7449 STUB_COMP_DIR may be non-NULL.
7450 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7451 are filled in with the info of the DIE from the DWO file.
7452 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7453 provided an abbrev table to use.
7454 The result is non-zero if a valid (non-dummy) DIE was found. */
7457 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7458 struct dwo_unit *dwo_unit,
7459 int abbrev_table_provided,
7460 struct die_info *stub_comp_unit_die,
7461 const char *stub_comp_dir,
7462 struct die_reader_specs *result_reader,
7463 const gdb_byte **result_info_ptr,
7464 struct die_info **result_comp_unit_die,
7465 int *result_has_children)
7467 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7468 struct objfile *objfile = dwarf2_per_objfile->objfile;
7469 struct dwarf2_cu *cu = this_cu->cu;
7470 struct dwarf2_section_info *section;
7472 const gdb_byte *begin_info_ptr, *info_ptr;
7473 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7474 int i,num_extra_attrs;
7475 struct dwarf2_section_info *dwo_abbrev_section;
7476 struct attribute *attr;
7477 struct die_info *comp_unit_die;
7479 /* At most one of these may be provided. */
7480 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7482 /* These attributes aren't processed until later:
7483 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7484 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7485 referenced later. However, these attributes are found in the stub
7486 which we won't have later. In order to not impose this complication
7487 on the rest of the code, we read them here and copy them to the
7496 if (stub_comp_unit_die != NULL)
7498 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7500 if (! this_cu->is_debug_types)
7501 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7502 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7503 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7504 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7505 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7507 /* There should be a DW_AT_addr_base attribute here (if needed).
7508 We need the value before we can process DW_FORM_GNU_addr_index. */
7510 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7512 cu->addr_base = DW_UNSND (attr);
7514 /* There should be a DW_AT_ranges_base attribute here (if needed).
7515 We need the value before we can process DW_AT_ranges. */
7516 cu->ranges_base = 0;
7517 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7519 cu->ranges_base = DW_UNSND (attr);
7521 else if (stub_comp_dir != NULL)
7523 /* Reconstruct the comp_dir attribute to simplify the code below. */
7524 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7525 comp_dir->name = DW_AT_comp_dir;
7526 comp_dir->form = DW_FORM_string;
7527 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7528 DW_STRING (comp_dir) = stub_comp_dir;
7531 /* Set up for reading the DWO CU/TU. */
7532 cu->dwo_unit = dwo_unit;
7533 section = dwo_unit->section;
7534 dwarf2_read_section (objfile, section);
7535 abfd = get_section_bfd_owner (section);
7536 begin_info_ptr = info_ptr = (section->buffer
7537 + to_underlying (dwo_unit->sect_off));
7538 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7539 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7541 if (this_cu->is_debug_types)
7543 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7545 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7546 &cu->header, section,
7548 info_ptr, rcuh_kind::TYPE);
7549 /* This is not an assert because it can be caused by bad debug info. */
7550 if (sig_type->signature != cu->header.signature)
7552 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7553 " TU at offset 0x%x [in module %s]"),
7554 hex_string (sig_type->signature),
7555 hex_string (cu->header.signature),
7556 to_underlying (dwo_unit->sect_off),
7557 bfd_get_filename (abfd));
7559 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7560 /* For DWOs coming from DWP files, we don't know the CU length
7561 nor the type's offset in the TU until now. */
7562 dwo_unit->length = get_cu_length (&cu->header);
7563 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7565 /* Establish the type offset that can be used to lookup the type.
7566 For DWO files, we don't know it until now. */
7567 sig_type->type_offset_in_section
7568 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7572 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7573 &cu->header, section,
7575 info_ptr, rcuh_kind::COMPILE);
7576 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7577 /* For DWOs coming from DWP files, we don't know the CU length
7579 dwo_unit->length = get_cu_length (&cu->header);
7582 /* Replace the CU's original abbrev table with the DWO's.
7583 Reminder: We can't read the abbrev table until we've read the header. */
7584 if (abbrev_table_provided)
7586 /* Don't free the provided abbrev table, the caller of
7587 init_cutu_and_read_dies owns it. */
7588 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7589 /* Ensure the DWO abbrev table gets freed. */
7590 make_cleanup (dwarf2_free_abbrev_table, cu);
7594 dwarf2_free_abbrev_table (cu);
7595 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7596 /* Leave any existing abbrev table cleanup as is. */
7599 /* Read in the die, but leave space to copy over the attributes
7600 from the stub. This has the benefit of simplifying the rest of
7601 the code - all the work to maintain the illusion of a single
7602 DW_TAG_{compile,type}_unit DIE is done here. */
7603 num_extra_attrs = ((stmt_list != NULL)
7607 + (comp_dir != NULL));
7608 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7609 result_has_children, num_extra_attrs);
7611 /* Copy over the attributes from the stub to the DIE we just read in. */
7612 comp_unit_die = *result_comp_unit_die;
7613 i = comp_unit_die->num_attrs;
7614 if (stmt_list != NULL)
7615 comp_unit_die->attrs[i++] = *stmt_list;
7617 comp_unit_die->attrs[i++] = *low_pc;
7618 if (high_pc != NULL)
7619 comp_unit_die->attrs[i++] = *high_pc;
7621 comp_unit_die->attrs[i++] = *ranges;
7622 if (comp_dir != NULL)
7623 comp_unit_die->attrs[i++] = *comp_dir;
7624 comp_unit_die->num_attrs += num_extra_attrs;
7626 if (dwarf_die_debug)
7628 fprintf_unfiltered (gdb_stdlog,
7629 "Read die from %s@0x%x of %s:\n",
7630 get_section_name (section),
7631 (unsigned) (begin_info_ptr - section->buffer),
7632 bfd_get_filename (abfd));
7633 dump_die (comp_unit_die, dwarf_die_debug);
7636 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7637 TUs by skipping the stub and going directly to the entry in the DWO file.
7638 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7639 to get it via circuitous means. Blech. */
7640 if (comp_dir != NULL)
7641 result_reader->comp_dir = DW_STRING (comp_dir);
7643 /* Skip dummy compilation units. */
7644 if (info_ptr >= begin_info_ptr + dwo_unit->length
7645 || peek_abbrev_code (abfd, info_ptr) == 0)
7648 *result_info_ptr = info_ptr;
7652 /* Subroutine of init_cutu_and_read_dies to simplify it.
7653 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7654 Returns NULL if the specified DWO unit cannot be found. */
7656 static struct dwo_unit *
7657 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7658 struct die_info *comp_unit_die)
7660 struct dwarf2_cu *cu = this_cu->cu;
7662 struct dwo_unit *dwo_unit;
7663 const char *comp_dir, *dwo_name;
7665 gdb_assert (cu != NULL);
7667 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7668 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7669 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7671 if (this_cu->is_debug_types)
7673 struct signatured_type *sig_type;
7675 /* Since this_cu is the first member of struct signatured_type,
7676 we can go from a pointer to one to a pointer to the other. */
7677 sig_type = (struct signatured_type *) this_cu;
7678 signature = sig_type->signature;
7679 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7683 struct attribute *attr;
7685 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7687 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7689 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7690 signature = DW_UNSND (attr);
7691 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7698 /* Subroutine of init_cutu_and_read_dies to simplify it.
7699 See it for a description of the parameters.
7700 Read a TU directly from a DWO file, bypassing the stub.
7702 Note: This function could be a little bit simpler if we shared cleanups
7703 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7704 to do, so we keep this function self-contained. Or we could move this
7705 into our caller, but it's complex enough already. */
7708 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7709 int use_existing_cu, int keep,
7710 die_reader_func_ftype *die_reader_func,
7713 struct dwarf2_cu *cu;
7714 struct signatured_type *sig_type;
7715 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7716 struct die_reader_specs reader;
7717 const gdb_byte *info_ptr;
7718 struct die_info *comp_unit_die;
7720 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7722 /* Verify we can do the following downcast, and that we have the
7724 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7725 sig_type = (struct signatured_type *) this_cu;
7726 gdb_assert (sig_type->dwo_unit != NULL);
7728 cleanups = make_cleanup (null_cleanup, NULL);
7730 if (use_existing_cu && this_cu->cu != NULL)
7732 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7734 /* There's no need to do the rereading_dwo_cu handling that
7735 init_cutu_and_read_dies does since we don't read the stub. */
7739 /* If !use_existing_cu, this_cu->cu must be NULL. */
7740 gdb_assert (this_cu->cu == NULL);
7741 cu = XNEW (struct dwarf2_cu);
7742 init_one_comp_unit (cu, this_cu);
7743 /* If an error occurs while loading, release our storage. */
7744 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7747 /* A future optimization, if needed, would be to use an existing
7748 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7749 could share abbrev tables. */
7751 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7752 0 /* abbrev_table_provided */,
7753 NULL /* stub_comp_unit_die */,
7754 sig_type->dwo_unit->dwo_file->comp_dir,
7756 &comp_unit_die, &has_children) == 0)
7759 do_cleanups (cleanups);
7763 /* All the "real" work is done here. */
7764 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7766 /* This duplicates the code in init_cutu_and_read_dies,
7767 but the alternative is making the latter more complex.
7768 This function is only for the special case of using DWO files directly:
7769 no point in overly complicating the general case just to handle this. */
7770 if (free_cu_cleanup != NULL)
7774 /* We've successfully allocated this compilation unit. Let our
7775 caller clean it up when finished with it. */
7776 discard_cleanups (free_cu_cleanup);
7778 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7779 So we have to manually free the abbrev table. */
7780 dwarf2_free_abbrev_table (cu);
7782 /* Link this CU into read_in_chain. */
7783 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7784 dwarf2_per_objfile->read_in_chain = this_cu;
7787 do_cleanups (free_cu_cleanup);
7790 do_cleanups (cleanups);
7793 /* Initialize a CU (or TU) and read its DIEs.
7794 If the CU defers to a DWO file, read the DWO file as well.
7796 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7797 Otherwise the table specified in the comp unit header is read in and used.
7798 This is an optimization for when we already have the abbrev table.
7800 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7801 Otherwise, a new CU is allocated with xmalloc.
7803 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7804 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7806 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7807 linker) then DIE_READER_FUNC will not get called. */
7810 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7811 struct abbrev_table *abbrev_table,
7812 int use_existing_cu, int keep,
7813 die_reader_func_ftype *die_reader_func,
7816 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7817 struct objfile *objfile = dwarf2_per_objfile->objfile;
7818 struct dwarf2_section_info *section = this_cu->section;
7819 bfd *abfd = get_section_bfd_owner (section);
7820 struct dwarf2_cu *cu;
7821 const gdb_byte *begin_info_ptr, *info_ptr;
7822 struct die_reader_specs reader;
7823 struct die_info *comp_unit_die;
7825 struct attribute *attr;
7826 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7827 struct signatured_type *sig_type = NULL;
7828 struct dwarf2_section_info *abbrev_section;
7829 /* Non-zero if CU currently points to a DWO file and we need to
7830 reread it. When this happens we need to reread the skeleton die
7831 before we can reread the DWO file (this only applies to CUs, not TUs). */
7832 int rereading_dwo_cu = 0;
7834 if (dwarf_die_debug)
7835 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7836 this_cu->is_debug_types ? "type" : "comp",
7837 to_underlying (this_cu->sect_off));
7839 if (use_existing_cu)
7842 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7843 file (instead of going through the stub), short-circuit all of this. */
7844 if (this_cu->reading_dwo_directly)
7846 /* Narrow down the scope of possibilities to have to understand. */
7847 gdb_assert (this_cu->is_debug_types);
7848 gdb_assert (abbrev_table == NULL);
7849 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7850 die_reader_func, data);
7854 cleanups = make_cleanup (null_cleanup, NULL);
7856 /* This is cheap if the section is already read in. */
7857 dwarf2_read_section (objfile, section);
7859 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7861 abbrev_section = get_abbrev_section_for_cu (this_cu);
7863 if (use_existing_cu && this_cu->cu != NULL)
7866 /* If this CU is from a DWO file we need to start over, we need to
7867 refetch the attributes from the skeleton CU.
7868 This could be optimized by retrieving those attributes from when we
7869 were here the first time: the previous comp_unit_die was stored in
7870 comp_unit_obstack. But there's no data yet that we need this
7872 if (cu->dwo_unit != NULL)
7873 rereading_dwo_cu = 1;
7877 /* If !use_existing_cu, this_cu->cu must be NULL. */
7878 gdb_assert (this_cu->cu == NULL);
7879 cu = XNEW (struct dwarf2_cu);
7880 init_one_comp_unit (cu, this_cu);
7881 /* If an error occurs while loading, release our storage. */
7882 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7885 /* Get the header. */
7886 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7888 /* We already have the header, there's no need to read it in again. */
7889 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7893 if (this_cu->is_debug_types)
7895 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7896 &cu->header, section,
7897 abbrev_section, info_ptr,
7900 /* Since per_cu is the first member of struct signatured_type,
7901 we can go from a pointer to one to a pointer to the other. */
7902 sig_type = (struct signatured_type *) this_cu;
7903 gdb_assert (sig_type->signature == cu->header.signature);
7904 gdb_assert (sig_type->type_offset_in_tu
7905 == cu->header.type_cu_offset_in_tu);
7906 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7908 /* LENGTH has not been set yet for type units if we're
7909 using .gdb_index. */
7910 this_cu->length = get_cu_length (&cu->header);
7912 /* Establish the type offset that can be used to lookup the type. */
7913 sig_type->type_offset_in_section =
7914 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7916 this_cu->dwarf_version = cu->header.version;
7920 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7921 &cu->header, section,
7924 rcuh_kind::COMPILE);
7926 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7927 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7928 this_cu->dwarf_version = cu->header.version;
7932 /* Skip dummy compilation units. */
7933 if (info_ptr >= begin_info_ptr + this_cu->length
7934 || peek_abbrev_code (abfd, info_ptr) == 0)
7936 do_cleanups (cleanups);
7940 /* If we don't have them yet, read the abbrevs for this compilation unit.
7941 And if we need to read them now, make sure they're freed when we're
7942 done. Note that it's important that if the CU had an abbrev table
7943 on entry we don't free it when we're done: Somewhere up the call stack
7944 it may be in use. */
7945 if (abbrev_table != NULL)
7947 gdb_assert (cu->abbrev_table == NULL);
7948 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7949 cu->abbrev_table = abbrev_table;
7951 else if (cu->abbrev_table == NULL)
7953 dwarf2_read_abbrevs (cu, abbrev_section);
7954 make_cleanup (dwarf2_free_abbrev_table, cu);
7956 else if (rereading_dwo_cu)
7958 dwarf2_free_abbrev_table (cu);
7959 dwarf2_read_abbrevs (cu, abbrev_section);
7962 /* Read the top level CU/TU die. */
7963 init_cu_die_reader (&reader, cu, section, NULL);
7964 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7966 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7968 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7969 DWO CU, that this test will fail (the attribute will not be present). */
7970 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7973 struct dwo_unit *dwo_unit;
7974 struct die_info *dwo_comp_unit_die;
7978 complaint (&symfile_complaints,
7979 _("compilation unit with DW_AT_GNU_dwo_name"
7980 " has children (offset 0x%x) [in module %s]"),
7981 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7983 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7984 if (dwo_unit != NULL)
7986 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7987 abbrev_table != NULL,
7988 comp_unit_die, NULL,
7990 &dwo_comp_unit_die, &has_children) == 0)
7993 do_cleanups (cleanups);
7996 comp_unit_die = dwo_comp_unit_die;
8000 /* Yikes, we couldn't find the rest of the DIE, we only have
8001 the stub. A complaint has already been logged. There's
8002 not much more we can do except pass on the stub DIE to
8003 die_reader_func. We don't want to throw an error on bad
8008 /* All of the above is setup for this call. Yikes. */
8009 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8011 /* Done, clean up. */
8012 if (free_cu_cleanup != NULL)
8016 /* We've successfully allocated this compilation unit. Let our
8017 caller clean it up when finished with it. */
8018 discard_cleanups (free_cu_cleanup);
8020 /* We can only discard free_cu_cleanup and all subsequent cleanups.
8021 So we have to manually free the abbrev table. */
8022 dwarf2_free_abbrev_table (cu);
8024 /* Link this CU into read_in_chain. */
8025 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8026 dwarf2_per_objfile->read_in_chain = this_cu;
8029 do_cleanups (free_cu_cleanup);
8032 do_cleanups (cleanups);
8035 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8036 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8037 to have already done the lookup to find the DWO file).
8039 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8040 THIS_CU->is_debug_types, but nothing else.
8042 We fill in THIS_CU->length.
8044 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8045 linker) then DIE_READER_FUNC will not get called.
8047 THIS_CU->cu is always freed when done.
8048 This is done in order to not leave THIS_CU->cu in a state where we have
8049 to care whether it refers to the "main" CU or the DWO CU. */
8052 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8053 struct dwo_file *dwo_file,
8054 die_reader_func_ftype *die_reader_func,
8057 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8058 struct objfile *objfile = dwarf2_per_objfile->objfile;
8059 struct dwarf2_section_info *section = this_cu->section;
8060 bfd *abfd = get_section_bfd_owner (section);
8061 struct dwarf2_section_info *abbrev_section;
8062 struct dwarf2_cu cu;
8063 const gdb_byte *begin_info_ptr, *info_ptr;
8064 struct die_reader_specs reader;
8065 struct cleanup *cleanups;
8066 struct die_info *comp_unit_die;
8069 if (dwarf_die_debug)
8070 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8071 this_cu->is_debug_types ? "type" : "comp",
8072 to_underlying (this_cu->sect_off));
8074 gdb_assert (this_cu->cu == NULL);
8076 abbrev_section = (dwo_file != NULL
8077 ? &dwo_file->sections.abbrev
8078 : get_abbrev_section_for_cu (this_cu));
8080 /* This is cheap if the section is already read in. */
8081 dwarf2_read_section (objfile, section);
8083 init_one_comp_unit (&cu, this_cu);
8085 cleanups = make_cleanup (free_stack_comp_unit, &cu);
8087 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8088 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8089 &cu.header, section,
8090 abbrev_section, info_ptr,
8091 (this_cu->is_debug_types
8093 : rcuh_kind::COMPILE));
8095 this_cu->length = get_cu_length (&cu.header);
8097 /* Skip dummy compilation units. */
8098 if (info_ptr >= begin_info_ptr + this_cu->length
8099 || peek_abbrev_code (abfd, info_ptr) == 0)
8101 do_cleanups (cleanups);
8105 dwarf2_read_abbrevs (&cu, abbrev_section);
8106 make_cleanup (dwarf2_free_abbrev_table, &cu);
8108 init_cu_die_reader (&reader, &cu, section, dwo_file);
8109 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8111 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8113 do_cleanups (cleanups);
8116 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8117 does not lookup the specified DWO file.
8118 This cannot be used to read DWO files.
8120 THIS_CU->cu is always freed when done.
8121 This is done in order to not leave THIS_CU->cu in a state where we have
8122 to care whether it refers to the "main" CU or the DWO CU.
8123 We can revisit this if the data shows there's a performance issue. */
8126 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8127 die_reader_func_ftype *die_reader_func,
8130 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8133 /* Type Unit Groups.
8135 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8136 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8137 so that all types coming from the same compilation (.o file) are grouped
8138 together. A future step could be to put the types in the same symtab as
8139 the CU the types ultimately came from. */
8142 hash_type_unit_group (const void *item)
8144 const struct type_unit_group *tu_group
8145 = (const struct type_unit_group *) item;
8147 return hash_stmt_list_entry (&tu_group->hash);
8151 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8153 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8154 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8156 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8159 /* Allocate a hash table for type unit groups. */
8162 allocate_type_unit_groups_table (struct objfile *objfile)
8164 return htab_create_alloc_ex (3,
8165 hash_type_unit_group,
8168 &objfile->objfile_obstack,
8169 hashtab_obstack_allocate,
8170 dummy_obstack_deallocate);
8173 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8174 partial symtabs. We combine several TUs per psymtab to not let the size
8175 of any one psymtab grow too big. */
8176 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8177 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8179 /* Helper routine for get_type_unit_group.
8180 Create the type_unit_group object used to hold one or more TUs. */
8182 static struct type_unit_group *
8183 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8185 struct dwarf2_per_objfile *dwarf2_per_objfile
8186 = cu->per_cu->dwarf2_per_objfile;
8187 struct objfile *objfile = dwarf2_per_objfile->objfile;
8188 struct dwarf2_per_cu_data *per_cu;
8189 struct type_unit_group *tu_group;
8191 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8192 struct type_unit_group);
8193 per_cu = &tu_group->per_cu;
8194 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8196 if (dwarf2_per_objfile->using_index)
8198 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8199 struct dwarf2_per_cu_quick_data);
8203 unsigned int line_offset = to_underlying (line_offset_struct);
8204 struct partial_symtab *pst;
8207 /* Give the symtab a useful name for debug purposes. */
8208 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8209 name = xstrprintf ("<type_units_%d>",
8210 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8212 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8214 pst = create_partial_symtab (per_cu, name);
8220 tu_group->hash.dwo_unit = cu->dwo_unit;
8221 tu_group->hash.line_sect_off = line_offset_struct;
8226 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8227 STMT_LIST is a DW_AT_stmt_list attribute. */
8229 static struct type_unit_group *
8230 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8232 struct dwarf2_per_objfile *dwarf2_per_objfile
8233 = cu->per_cu->dwarf2_per_objfile;
8234 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8235 struct type_unit_group *tu_group;
8237 unsigned int line_offset;
8238 struct type_unit_group type_unit_group_for_lookup;
8240 if (dwarf2_per_objfile->type_unit_groups == NULL)
8242 dwarf2_per_objfile->type_unit_groups =
8243 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8246 /* Do we need to create a new group, or can we use an existing one? */
8250 line_offset = DW_UNSND (stmt_list);
8251 ++tu_stats->nr_symtab_sharers;
8255 /* Ugh, no stmt_list. Rare, but we have to handle it.
8256 We can do various things here like create one group per TU or
8257 spread them over multiple groups to split up the expansion work.
8258 To avoid worst case scenarios (too many groups or too large groups)
8259 we, umm, group them in bunches. */
8260 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8261 | (tu_stats->nr_stmt_less_type_units
8262 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8263 ++tu_stats->nr_stmt_less_type_units;
8266 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8267 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8268 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8269 &type_unit_group_for_lookup, INSERT);
8272 tu_group = (struct type_unit_group *) *slot;
8273 gdb_assert (tu_group != NULL);
8277 sect_offset line_offset_struct = (sect_offset) line_offset;
8278 tu_group = create_type_unit_group (cu, line_offset_struct);
8280 ++tu_stats->nr_symtabs;
8286 /* Partial symbol tables. */
8288 /* Create a psymtab named NAME and assign it to PER_CU.
8290 The caller must fill in the following details:
8291 dirname, textlow, texthigh. */
8293 static struct partial_symtab *
8294 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8296 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8297 struct partial_symtab *pst;
8299 pst = start_psymtab_common (objfile, name, 0,
8300 objfile->global_psymbols,
8301 objfile->static_psymbols);
8303 pst->psymtabs_addrmap_supported = 1;
8305 /* This is the glue that links PST into GDB's symbol API. */
8306 pst->read_symtab_private = per_cu;
8307 pst->read_symtab = dwarf2_read_symtab;
8308 per_cu->v.psymtab = pst;
8313 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8316 struct process_psymtab_comp_unit_data
8318 /* True if we are reading a DW_TAG_partial_unit. */
8320 int want_partial_unit;
8322 /* The "pretend" language that is used if the CU doesn't declare a
8325 enum language pretend_language;
8328 /* die_reader_func for process_psymtab_comp_unit. */
8331 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8332 const gdb_byte *info_ptr,
8333 struct die_info *comp_unit_die,
8337 struct dwarf2_cu *cu = reader->cu;
8338 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8339 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8340 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8342 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8343 struct partial_symtab *pst;
8344 enum pc_bounds_kind cu_bounds_kind;
8345 const char *filename;
8346 struct process_psymtab_comp_unit_data *info
8347 = (struct process_psymtab_comp_unit_data *) data;
8349 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8352 gdb_assert (! per_cu->is_debug_types);
8354 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8356 cu->list_in_scope = &file_symbols;
8358 /* Allocate a new partial symbol table structure. */
8359 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8360 if (filename == NULL)
8363 pst = create_partial_symtab (per_cu, filename);
8365 /* This must be done before calling dwarf2_build_include_psymtabs. */
8366 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8368 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8370 dwarf2_find_base_address (comp_unit_die, cu);
8372 /* Possibly set the default values of LOWPC and HIGHPC from
8374 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8375 &best_highpc, cu, pst);
8376 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8377 /* Store the contiguous range if it is not empty; it can be empty for
8378 CUs with no code. */
8379 addrmap_set_empty (objfile->psymtabs_addrmap,
8380 gdbarch_adjust_dwarf2_addr (gdbarch,
8381 best_lowpc + baseaddr),
8382 gdbarch_adjust_dwarf2_addr (gdbarch,
8383 best_highpc + baseaddr) - 1,
8386 /* Check if comp unit has_children.
8387 If so, read the rest of the partial symbols from this comp unit.
8388 If not, there's no more debug_info for this comp unit. */
8391 struct partial_die_info *first_die;
8392 CORE_ADDR lowpc, highpc;
8394 lowpc = ((CORE_ADDR) -1);
8395 highpc = ((CORE_ADDR) 0);
8397 first_die = load_partial_dies (reader, info_ptr, 1);
8399 scan_partial_symbols (first_die, &lowpc, &highpc,
8400 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8402 /* If we didn't find a lowpc, set it to highpc to avoid
8403 complaints from `maint check'. */
8404 if (lowpc == ((CORE_ADDR) -1))
8407 /* If the compilation unit didn't have an explicit address range,
8408 then use the information extracted from its child dies. */
8409 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8412 best_highpc = highpc;
8415 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8416 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8418 end_psymtab_common (objfile, pst);
8420 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8423 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8424 struct dwarf2_per_cu_data *iter;
8426 /* Fill in 'dependencies' here; we fill in 'users' in a
8428 pst->number_of_dependencies = len;
8430 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8432 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8435 pst->dependencies[i] = iter->v.psymtab;
8437 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8440 /* Get the list of files included in the current compilation unit,
8441 and build a psymtab for each of them. */
8442 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8444 if (dwarf_read_debug)
8446 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8448 fprintf_unfiltered (gdb_stdlog,
8449 "Psymtab for %s unit @0x%x: %s - %s"
8450 ", %d global, %d static syms\n",
8451 per_cu->is_debug_types ? "type" : "comp",
8452 to_underlying (per_cu->sect_off),
8453 paddress (gdbarch, pst->textlow),
8454 paddress (gdbarch, pst->texthigh),
8455 pst->n_global_syms, pst->n_static_syms);
8459 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8460 Process compilation unit THIS_CU for a psymtab. */
8463 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8464 int want_partial_unit,
8465 enum language pretend_language)
8467 /* If this compilation unit was already read in, free the
8468 cached copy in order to read it in again. This is
8469 necessary because we skipped some symbols when we first
8470 read in the compilation unit (see load_partial_dies).
8471 This problem could be avoided, but the benefit is unclear. */
8472 if (this_cu->cu != NULL)
8473 free_one_cached_comp_unit (this_cu);
8475 if (this_cu->is_debug_types)
8476 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8480 process_psymtab_comp_unit_data info;
8481 info.want_partial_unit = want_partial_unit;
8482 info.pretend_language = pretend_language;
8483 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8484 process_psymtab_comp_unit_reader, &info);
8487 /* Age out any secondary CUs. */
8488 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8491 /* Reader function for build_type_psymtabs. */
8494 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8495 const gdb_byte *info_ptr,
8496 struct die_info *type_unit_die,
8500 struct dwarf2_per_objfile *dwarf2_per_objfile
8501 = reader->cu->per_cu->dwarf2_per_objfile;
8502 struct objfile *objfile = dwarf2_per_objfile->objfile;
8503 struct dwarf2_cu *cu = reader->cu;
8504 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8505 struct signatured_type *sig_type;
8506 struct type_unit_group *tu_group;
8507 struct attribute *attr;
8508 struct partial_die_info *first_die;
8509 CORE_ADDR lowpc, highpc;
8510 struct partial_symtab *pst;
8512 gdb_assert (data == NULL);
8513 gdb_assert (per_cu->is_debug_types);
8514 sig_type = (struct signatured_type *) per_cu;
8519 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8520 tu_group = get_type_unit_group (cu, attr);
8522 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8524 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8525 cu->list_in_scope = &file_symbols;
8526 pst = create_partial_symtab (per_cu, "");
8529 first_die = load_partial_dies (reader, info_ptr, 1);
8531 lowpc = (CORE_ADDR) -1;
8532 highpc = (CORE_ADDR) 0;
8533 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8535 end_psymtab_common (objfile, pst);
8538 /* Struct used to sort TUs by their abbreviation table offset. */
8540 struct tu_abbrev_offset
8542 struct signatured_type *sig_type;
8543 sect_offset abbrev_offset;
8546 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8549 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8551 const struct tu_abbrev_offset * const *a
8552 = (const struct tu_abbrev_offset * const*) ap;
8553 const struct tu_abbrev_offset * const *b
8554 = (const struct tu_abbrev_offset * const*) bp;
8555 sect_offset aoff = (*a)->abbrev_offset;
8556 sect_offset boff = (*b)->abbrev_offset;
8558 return (aoff > boff) - (aoff < boff);
8561 /* Efficiently read all the type units.
8562 This does the bulk of the work for build_type_psymtabs.
8564 The efficiency is because we sort TUs by the abbrev table they use and
8565 only read each abbrev table once. In one program there are 200K TUs
8566 sharing 8K abbrev tables.
8568 The main purpose of this function is to support building the
8569 dwarf2_per_objfile->type_unit_groups table.
8570 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8571 can collapse the search space by grouping them by stmt_list.
8572 The savings can be significant, in the same program from above the 200K TUs
8573 share 8K stmt_list tables.
8575 FUNC is expected to call get_type_unit_group, which will create the
8576 struct type_unit_group if necessary and add it to
8577 dwarf2_per_objfile->type_unit_groups. */
8580 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8582 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8583 struct cleanup *cleanups;
8584 struct abbrev_table *abbrev_table;
8585 sect_offset abbrev_offset;
8586 struct tu_abbrev_offset *sorted_by_abbrev;
8589 /* It's up to the caller to not call us multiple times. */
8590 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8592 if (dwarf2_per_objfile->n_type_units == 0)
8595 /* TUs typically share abbrev tables, and there can be way more TUs than
8596 abbrev tables. Sort by abbrev table to reduce the number of times we
8597 read each abbrev table in.
8598 Alternatives are to punt or to maintain a cache of abbrev tables.
8599 This is simpler and efficient enough for now.
8601 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8602 symtab to use). Typically TUs with the same abbrev offset have the same
8603 stmt_list value too so in practice this should work well.
8605 The basic algorithm here is:
8607 sort TUs by abbrev table
8608 for each TU with same abbrev table:
8609 read abbrev table if first user
8610 read TU top level DIE
8611 [IWBN if DWO skeletons had DW_AT_stmt_list]
8614 if (dwarf_read_debug)
8615 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8617 /* Sort in a separate table to maintain the order of all_type_units
8618 for .gdb_index: TU indices directly index all_type_units. */
8619 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8620 dwarf2_per_objfile->n_type_units);
8621 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8623 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8625 sorted_by_abbrev[i].sig_type = sig_type;
8626 sorted_by_abbrev[i].abbrev_offset =
8627 read_abbrev_offset (dwarf2_per_objfile,
8628 sig_type->per_cu.section,
8629 sig_type->per_cu.sect_off);
8631 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8632 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8633 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8635 abbrev_offset = (sect_offset) ~(unsigned) 0;
8636 abbrev_table = NULL;
8637 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8639 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8641 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8643 /* Switch to the next abbrev table if necessary. */
8644 if (abbrev_table == NULL
8645 || tu->abbrev_offset != abbrev_offset)
8647 if (abbrev_table != NULL)
8649 abbrev_table_free (abbrev_table);
8650 /* Reset to NULL in case abbrev_table_read_table throws
8651 an error: abbrev_table_free_cleanup will get called. */
8652 abbrev_table = NULL;
8654 abbrev_offset = tu->abbrev_offset;
8656 abbrev_table_read_table (dwarf2_per_objfile,
8657 &dwarf2_per_objfile->abbrev,
8659 ++tu_stats->nr_uniq_abbrev_tables;
8662 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8663 build_type_psymtabs_reader, NULL);
8666 do_cleanups (cleanups);
8669 /* Print collected type unit statistics. */
8672 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8674 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8676 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8677 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8678 dwarf2_per_objfile->n_type_units);
8679 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8680 tu_stats->nr_uniq_abbrev_tables);
8681 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8682 tu_stats->nr_symtabs);
8683 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8684 tu_stats->nr_symtab_sharers);
8685 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8686 tu_stats->nr_stmt_less_type_units);
8687 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8688 tu_stats->nr_all_type_units_reallocs);
8691 /* Traversal function for build_type_psymtabs. */
8694 build_type_psymtab_dependencies (void **slot, void *info)
8696 struct dwarf2_per_objfile *dwarf2_per_objfile
8697 = (struct dwarf2_per_objfile *) info;
8698 struct objfile *objfile = dwarf2_per_objfile->objfile;
8699 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8700 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8701 struct partial_symtab *pst = per_cu->v.psymtab;
8702 int len = VEC_length (sig_type_ptr, tu_group->tus);
8703 struct signatured_type *iter;
8706 gdb_assert (len > 0);
8707 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8709 pst->number_of_dependencies = len;
8711 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8713 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8716 gdb_assert (iter->per_cu.is_debug_types);
8717 pst->dependencies[i] = iter->per_cu.v.psymtab;
8718 iter->type_unit_group = tu_group;
8721 VEC_free (sig_type_ptr, tu_group->tus);
8726 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8727 Build partial symbol tables for the .debug_types comp-units. */
8730 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8732 if (! create_all_type_units (dwarf2_per_objfile))
8735 build_type_psymtabs_1 (dwarf2_per_objfile);
8738 /* Traversal function for process_skeletonless_type_unit.
8739 Read a TU in a DWO file and build partial symbols for it. */
8742 process_skeletonless_type_unit (void **slot, void *info)
8744 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8745 struct dwarf2_per_objfile *dwarf2_per_objfile
8746 = (struct dwarf2_per_objfile *) info;
8747 struct signatured_type find_entry, *entry;
8749 /* If this TU doesn't exist in the global table, add it and read it in. */
8751 if (dwarf2_per_objfile->signatured_types == NULL)
8753 dwarf2_per_objfile->signatured_types
8754 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8757 find_entry.signature = dwo_unit->signature;
8758 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8760 /* If we've already seen this type there's nothing to do. What's happening
8761 is we're doing our own version of comdat-folding here. */
8765 /* This does the job that create_all_type_units would have done for
8767 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8768 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8771 /* This does the job that build_type_psymtabs_1 would have done. */
8772 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8773 build_type_psymtabs_reader, NULL);
8778 /* Traversal function for process_skeletonless_type_units. */
8781 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8783 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8785 if (dwo_file->tus != NULL)
8787 htab_traverse_noresize (dwo_file->tus,
8788 process_skeletonless_type_unit, info);
8794 /* Scan all TUs of DWO files, verifying we've processed them.
8795 This is needed in case a TU was emitted without its skeleton.
8796 Note: This can't be done until we know what all the DWO files are. */
8799 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8801 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8802 if (get_dwp_file (dwarf2_per_objfile) == NULL
8803 && dwarf2_per_objfile->dwo_files != NULL)
8805 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8806 process_dwo_file_for_skeletonless_type_units,
8807 dwarf2_per_objfile);
8811 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8814 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8818 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8820 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8821 struct partial_symtab *pst = per_cu->v.psymtab;
8827 for (j = 0; j < pst->number_of_dependencies; ++j)
8829 /* Set the 'user' field only if it is not already set. */
8830 if (pst->dependencies[j]->user == NULL)
8831 pst->dependencies[j]->user = pst;
8836 /* Build the partial symbol table by doing a quick pass through the
8837 .debug_info and .debug_abbrev sections. */
8840 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8842 struct cleanup *back_to;
8844 struct objfile *objfile = dwarf2_per_objfile->objfile;
8846 if (dwarf_read_debug)
8848 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8849 objfile_name (objfile));
8852 dwarf2_per_objfile->reading_partial_symbols = 1;
8854 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8856 /* Any cached compilation units will be linked by the per-objfile
8857 read_in_chain. Make sure to free them when we're done. */
8858 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8860 build_type_psymtabs (dwarf2_per_objfile);
8862 create_all_comp_units (dwarf2_per_objfile);
8864 /* Create a temporary address map on a temporary obstack. We later
8865 copy this to the final obstack. */
8866 auto_obstack temp_obstack;
8868 scoped_restore save_psymtabs_addrmap
8869 = make_scoped_restore (&objfile->psymtabs_addrmap,
8870 addrmap_create_mutable (&temp_obstack));
8872 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8874 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8876 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8879 /* This has to wait until we read the CUs, we need the list of DWOs. */
8880 process_skeletonless_type_units (dwarf2_per_objfile);
8882 /* Now that all TUs have been processed we can fill in the dependencies. */
8883 if (dwarf2_per_objfile->type_unit_groups != NULL)
8885 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8886 build_type_psymtab_dependencies, dwarf2_per_objfile);
8889 if (dwarf_read_debug)
8890 print_tu_stats (dwarf2_per_objfile);
8892 set_partial_user (dwarf2_per_objfile);
8894 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8895 &objfile->objfile_obstack);
8896 /* At this point we want to keep the address map. */
8897 save_psymtabs_addrmap.release ();
8899 do_cleanups (back_to);
8901 if (dwarf_read_debug)
8902 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8903 objfile_name (objfile));
8906 /* die_reader_func for load_partial_comp_unit. */
8909 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8910 const gdb_byte *info_ptr,
8911 struct die_info *comp_unit_die,
8915 struct dwarf2_cu *cu = reader->cu;
8917 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8919 /* Check if comp unit has_children.
8920 If so, read the rest of the partial symbols from this comp unit.
8921 If not, there's no more debug_info for this comp unit. */
8923 load_partial_dies (reader, info_ptr, 0);
8926 /* Load the partial DIEs for a secondary CU into memory.
8927 This is also used when rereading a primary CU with load_all_dies. */
8930 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8932 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8933 load_partial_comp_unit_reader, NULL);
8937 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8938 struct dwarf2_section_info *section,
8939 struct dwarf2_section_info *abbrev_section,
8940 unsigned int is_dwz,
8943 struct dwarf2_per_cu_data ***all_comp_units)
8945 const gdb_byte *info_ptr;
8946 struct objfile *objfile = dwarf2_per_objfile->objfile;
8948 if (dwarf_read_debug)
8949 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8950 get_section_name (section),
8951 get_section_file_name (section));
8953 dwarf2_read_section (objfile, section);
8955 info_ptr = section->buffer;
8957 while (info_ptr < section->buffer + section->size)
8959 struct dwarf2_per_cu_data *this_cu;
8961 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8963 comp_unit_head cu_header;
8964 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8965 abbrev_section, info_ptr,
8966 rcuh_kind::COMPILE);
8968 /* Save the compilation unit for later lookup. */
8969 if (cu_header.unit_type != DW_UT_type)
8971 this_cu = XOBNEW (&objfile->objfile_obstack,
8972 struct dwarf2_per_cu_data);
8973 memset (this_cu, 0, sizeof (*this_cu));
8977 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8978 struct signatured_type);
8979 memset (sig_type, 0, sizeof (*sig_type));
8980 sig_type->signature = cu_header.signature;
8981 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8982 this_cu = &sig_type->per_cu;
8984 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8985 this_cu->sect_off = sect_off;
8986 this_cu->length = cu_header.length + cu_header.initial_length_size;
8987 this_cu->is_dwz = is_dwz;
8988 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8989 this_cu->section = section;
8991 if (*n_comp_units == *n_allocated)
8994 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8995 *all_comp_units, *n_allocated);
8997 (*all_comp_units)[*n_comp_units] = this_cu;
9000 info_ptr = info_ptr + this_cu->length;
9004 /* Create a list of all compilation units in OBJFILE.
9005 This is only done for -readnow and building partial symtabs. */
9008 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9012 struct dwarf2_per_cu_data **all_comp_units;
9013 struct dwz_file *dwz;
9014 struct objfile *objfile = dwarf2_per_objfile->objfile;
9018 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9020 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9021 &dwarf2_per_objfile->abbrev, 0,
9022 &n_allocated, &n_comp_units, &all_comp_units);
9024 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9026 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9027 1, &n_allocated, &n_comp_units,
9030 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9031 struct dwarf2_per_cu_data *,
9033 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9034 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9035 xfree (all_comp_units);
9036 dwarf2_per_objfile->n_comp_units = n_comp_units;
9039 /* Process all loaded DIEs for compilation unit CU, starting at
9040 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9041 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9042 DW_AT_ranges). See the comments of add_partial_subprogram on how
9043 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9046 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9047 CORE_ADDR *highpc, int set_addrmap,
9048 struct dwarf2_cu *cu)
9050 struct partial_die_info *pdi;
9052 /* Now, march along the PDI's, descending into ones which have
9053 interesting children but skipping the children of the other ones,
9054 until we reach the end of the compilation unit. */
9060 fixup_partial_die (pdi, cu);
9062 /* Anonymous namespaces or modules have no name but have interesting
9063 children, so we need to look at them. Ditto for anonymous
9066 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9067 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9068 || pdi->tag == DW_TAG_imported_unit
9069 || pdi->tag == DW_TAG_inlined_subroutine)
9073 case DW_TAG_subprogram:
9074 case DW_TAG_inlined_subroutine:
9075 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9077 case DW_TAG_constant:
9078 case DW_TAG_variable:
9079 case DW_TAG_typedef:
9080 case DW_TAG_union_type:
9081 if (!pdi->is_declaration)
9083 add_partial_symbol (pdi, cu);
9086 case DW_TAG_class_type:
9087 case DW_TAG_interface_type:
9088 case DW_TAG_structure_type:
9089 if (!pdi->is_declaration)
9091 add_partial_symbol (pdi, cu);
9093 if (cu->language == language_rust && pdi->has_children)
9094 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9097 case DW_TAG_enumeration_type:
9098 if (!pdi->is_declaration)
9099 add_partial_enumeration (pdi, cu);
9101 case DW_TAG_base_type:
9102 case DW_TAG_subrange_type:
9103 /* File scope base type definitions are added to the partial
9105 add_partial_symbol (pdi, cu);
9107 case DW_TAG_namespace:
9108 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9111 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9113 case DW_TAG_imported_unit:
9115 struct dwarf2_per_cu_data *per_cu;
9117 /* For now we don't handle imported units in type units. */
9118 if (cu->per_cu->is_debug_types)
9120 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9121 " supported in type units [in module %s]"),
9122 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9125 per_cu = dwarf2_find_containing_comp_unit
9126 (pdi->d.sect_off, pdi->is_dwz,
9127 cu->per_cu->dwarf2_per_objfile);
9129 /* Go read the partial unit, if needed. */
9130 if (per_cu->v.psymtab == NULL)
9131 process_psymtab_comp_unit (per_cu, 1, cu->language);
9133 VEC_safe_push (dwarf2_per_cu_ptr,
9134 cu->per_cu->imported_symtabs, per_cu);
9137 case DW_TAG_imported_declaration:
9138 add_partial_symbol (pdi, cu);
9145 /* If the die has a sibling, skip to the sibling. */
9147 pdi = pdi->die_sibling;
9151 /* Functions used to compute the fully scoped name of a partial DIE.
9153 Normally, this is simple. For C++, the parent DIE's fully scoped
9154 name is concatenated with "::" and the partial DIE's name.
9155 Enumerators are an exception; they use the scope of their parent
9156 enumeration type, i.e. the name of the enumeration type is not
9157 prepended to the enumerator.
9159 There are two complexities. One is DW_AT_specification; in this
9160 case "parent" means the parent of the target of the specification,
9161 instead of the direct parent of the DIE. The other is compilers
9162 which do not emit DW_TAG_namespace; in this case we try to guess
9163 the fully qualified name of structure types from their members'
9164 linkage names. This must be done using the DIE's children rather
9165 than the children of any DW_AT_specification target. We only need
9166 to do this for structures at the top level, i.e. if the target of
9167 any DW_AT_specification (if any; otherwise the DIE itself) does not
9170 /* Compute the scope prefix associated with PDI's parent, in
9171 compilation unit CU. The result will be allocated on CU's
9172 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9173 field. NULL is returned if no prefix is necessary. */
9175 partial_die_parent_scope (struct partial_die_info *pdi,
9176 struct dwarf2_cu *cu)
9178 const char *grandparent_scope;
9179 struct partial_die_info *parent, *real_pdi;
9181 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9182 then this means the parent of the specification DIE. */
9185 while (real_pdi->has_specification)
9186 real_pdi = find_partial_die (real_pdi->spec_offset,
9187 real_pdi->spec_is_dwz, cu);
9189 parent = real_pdi->die_parent;
9193 if (parent->scope_set)
9194 return parent->scope;
9196 fixup_partial_die (parent, cu);
9198 grandparent_scope = partial_die_parent_scope (parent, cu);
9200 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9201 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9202 Work around this problem here. */
9203 if (cu->language == language_cplus
9204 && parent->tag == DW_TAG_namespace
9205 && strcmp (parent->name, "::") == 0
9206 && grandparent_scope == NULL)
9208 parent->scope = NULL;
9209 parent->scope_set = 1;
9213 if (pdi->tag == DW_TAG_enumerator)
9214 /* Enumerators should not get the name of the enumeration as a prefix. */
9215 parent->scope = grandparent_scope;
9216 else if (parent->tag == DW_TAG_namespace
9217 || parent->tag == DW_TAG_module
9218 || parent->tag == DW_TAG_structure_type
9219 || parent->tag == DW_TAG_class_type
9220 || parent->tag == DW_TAG_interface_type
9221 || parent->tag == DW_TAG_union_type
9222 || parent->tag == DW_TAG_enumeration_type)
9224 if (grandparent_scope == NULL)
9225 parent->scope = parent->name;
9227 parent->scope = typename_concat (&cu->comp_unit_obstack,
9229 parent->name, 0, cu);
9233 /* FIXME drow/2004-04-01: What should we be doing with
9234 function-local names? For partial symbols, we should probably be
9236 complaint (&symfile_complaints,
9237 _("unhandled containing DIE tag %d for DIE at %d"),
9238 parent->tag, to_underlying (pdi->sect_off));
9239 parent->scope = grandparent_scope;
9242 parent->scope_set = 1;
9243 return parent->scope;
9246 /* Return the fully scoped name associated with PDI, from compilation unit
9247 CU. The result will be allocated with malloc. */
9250 partial_die_full_name (struct partial_die_info *pdi,
9251 struct dwarf2_cu *cu)
9253 const char *parent_scope;
9255 /* If this is a template instantiation, we can not work out the
9256 template arguments from partial DIEs. So, unfortunately, we have
9257 to go through the full DIEs. At least any work we do building
9258 types here will be reused if full symbols are loaded later. */
9259 if (pdi->has_template_arguments)
9261 fixup_partial_die (pdi, cu);
9263 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9265 struct die_info *die;
9266 struct attribute attr;
9267 struct dwarf2_cu *ref_cu = cu;
9269 /* DW_FORM_ref_addr is using section offset. */
9270 attr.name = (enum dwarf_attribute) 0;
9271 attr.form = DW_FORM_ref_addr;
9272 attr.u.unsnd = to_underlying (pdi->sect_off);
9273 die = follow_die_ref (NULL, &attr, &ref_cu);
9275 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9279 parent_scope = partial_die_parent_scope (pdi, cu);
9280 if (parent_scope == NULL)
9283 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9287 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9289 struct dwarf2_per_objfile *dwarf2_per_objfile
9290 = cu->per_cu->dwarf2_per_objfile;
9291 struct objfile *objfile = dwarf2_per_objfile->objfile;
9292 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9294 const char *actual_name = NULL;
9296 char *built_actual_name;
9298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9300 built_actual_name = partial_die_full_name (pdi, cu);
9301 if (built_actual_name != NULL)
9302 actual_name = built_actual_name;
9304 if (actual_name == NULL)
9305 actual_name = pdi->name;
9309 case DW_TAG_inlined_subroutine:
9310 case DW_TAG_subprogram:
9311 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9312 if (pdi->is_external || cu->language == language_ada)
9314 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9315 of the global scope. But in Ada, we want to be able to access
9316 nested procedures globally. So all Ada subprograms are stored
9317 in the global scope. */
9318 add_psymbol_to_list (actual_name, strlen (actual_name),
9319 built_actual_name != NULL,
9320 VAR_DOMAIN, LOC_BLOCK,
9321 &objfile->global_psymbols,
9322 addr, cu->language, objfile);
9326 add_psymbol_to_list (actual_name, strlen (actual_name),
9327 built_actual_name != NULL,
9328 VAR_DOMAIN, LOC_BLOCK,
9329 &objfile->static_psymbols,
9330 addr, cu->language, objfile);
9333 if (pdi->main_subprogram && actual_name != NULL)
9334 set_objfile_main_name (objfile, actual_name, cu->language);
9336 case DW_TAG_constant:
9338 std::vector<partial_symbol *> *list;
9340 if (pdi->is_external)
9341 list = &objfile->global_psymbols;
9343 list = &objfile->static_psymbols;
9344 add_psymbol_to_list (actual_name, strlen (actual_name),
9345 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9346 list, 0, cu->language, objfile);
9349 case DW_TAG_variable:
9351 addr = decode_locdesc (pdi->d.locdesc, cu);
9355 && !dwarf2_per_objfile->has_section_at_zero)
9357 /* A global or static variable may also have been stripped
9358 out by the linker if unused, in which case its address
9359 will be nullified; do not add such variables into partial
9360 symbol table then. */
9362 else if (pdi->is_external)
9365 Don't enter into the minimal symbol tables as there is
9366 a minimal symbol table entry from the ELF symbols already.
9367 Enter into partial symbol table if it has a location
9368 descriptor or a type.
9369 If the location descriptor is missing, new_symbol will create
9370 a LOC_UNRESOLVED symbol, the address of the variable will then
9371 be determined from the minimal symbol table whenever the variable
9373 The address for the partial symbol table entry is not
9374 used by GDB, but it comes in handy for debugging partial symbol
9377 if (pdi->d.locdesc || pdi->has_type)
9378 add_psymbol_to_list (actual_name, strlen (actual_name),
9379 built_actual_name != NULL,
9380 VAR_DOMAIN, LOC_STATIC,
9381 &objfile->global_psymbols,
9383 cu->language, objfile);
9387 int has_loc = pdi->d.locdesc != NULL;
9389 /* Static Variable. Skip symbols whose value we cannot know (those
9390 without location descriptors or constant values). */
9391 if (!has_loc && !pdi->has_const_value)
9393 xfree (built_actual_name);
9397 add_psymbol_to_list (actual_name, strlen (actual_name),
9398 built_actual_name != NULL,
9399 VAR_DOMAIN, LOC_STATIC,
9400 &objfile->static_psymbols,
9401 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9402 cu->language, objfile);
9405 case DW_TAG_typedef:
9406 case DW_TAG_base_type:
9407 case DW_TAG_subrange_type:
9408 add_psymbol_to_list (actual_name, strlen (actual_name),
9409 built_actual_name != NULL,
9410 VAR_DOMAIN, LOC_TYPEDEF,
9411 &objfile->static_psymbols,
9412 0, cu->language, objfile);
9414 case DW_TAG_imported_declaration:
9415 case DW_TAG_namespace:
9416 add_psymbol_to_list (actual_name, strlen (actual_name),
9417 built_actual_name != NULL,
9418 VAR_DOMAIN, LOC_TYPEDEF,
9419 &objfile->global_psymbols,
9420 0, cu->language, objfile);
9423 add_psymbol_to_list (actual_name, strlen (actual_name),
9424 built_actual_name != NULL,
9425 MODULE_DOMAIN, LOC_TYPEDEF,
9426 &objfile->global_psymbols,
9427 0, cu->language, objfile);
9429 case DW_TAG_class_type:
9430 case DW_TAG_interface_type:
9431 case DW_TAG_structure_type:
9432 case DW_TAG_union_type:
9433 case DW_TAG_enumeration_type:
9434 /* Skip external references. The DWARF standard says in the section
9435 about "Structure, Union, and Class Type Entries": "An incomplete
9436 structure, union or class type is represented by a structure,
9437 union or class entry that does not have a byte size attribute
9438 and that has a DW_AT_declaration attribute." */
9439 if (!pdi->has_byte_size && pdi->is_declaration)
9441 xfree (built_actual_name);
9445 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9446 static vs. global. */
9447 add_psymbol_to_list (actual_name, strlen (actual_name),
9448 built_actual_name != NULL,
9449 STRUCT_DOMAIN, LOC_TYPEDEF,
9450 cu->language == language_cplus
9451 ? &objfile->global_psymbols
9452 : &objfile->static_psymbols,
9453 0, cu->language, objfile);
9456 case DW_TAG_enumerator:
9457 add_psymbol_to_list (actual_name, strlen (actual_name),
9458 built_actual_name != NULL,
9459 VAR_DOMAIN, LOC_CONST,
9460 cu->language == language_cplus
9461 ? &objfile->global_psymbols
9462 : &objfile->static_psymbols,
9463 0, cu->language, objfile);
9469 xfree (built_actual_name);
9472 /* Read a partial die corresponding to a namespace; also, add a symbol
9473 corresponding to that namespace to the symbol table. NAMESPACE is
9474 the name of the enclosing namespace. */
9477 add_partial_namespace (struct partial_die_info *pdi,
9478 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9479 int set_addrmap, struct dwarf2_cu *cu)
9481 /* Add a symbol for the namespace. */
9483 add_partial_symbol (pdi, cu);
9485 /* Now scan partial symbols in that namespace. */
9487 if (pdi->has_children)
9488 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9491 /* Read a partial die corresponding to a Fortran module. */
9494 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9495 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9497 /* Add a symbol for the namespace. */
9499 add_partial_symbol (pdi, cu);
9501 /* Now scan partial symbols in that module. */
9503 if (pdi->has_children)
9504 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9507 /* Read a partial die corresponding to a subprogram or an inlined
9508 subprogram and create a partial symbol for that subprogram.
9509 When the CU language allows it, this routine also defines a partial
9510 symbol for each nested subprogram that this subprogram contains.
9511 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9512 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9514 PDI may also be a lexical block, in which case we simply search
9515 recursively for subprograms defined inside that lexical block.
9516 Again, this is only performed when the CU language allows this
9517 type of definitions. */
9520 add_partial_subprogram (struct partial_die_info *pdi,
9521 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9522 int set_addrmap, struct dwarf2_cu *cu)
9524 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9526 if (pdi->has_pc_info)
9528 if (pdi->lowpc < *lowpc)
9529 *lowpc = pdi->lowpc;
9530 if (pdi->highpc > *highpc)
9531 *highpc = pdi->highpc;
9534 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9535 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9540 baseaddr = ANOFFSET (objfile->section_offsets,
9541 SECT_OFF_TEXT (objfile));
9542 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9543 pdi->lowpc + baseaddr);
9544 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9545 pdi->highpc + baseaddr);
9546 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9547 cu->per_cu->v.psymtab);
9551 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9553 if (!pdi->is_declaration)
9554 /* Ignore subprogram DIEs that do not have a name, they are
9555 illegal. Do not emit a complaint at this point, we will
9556 do so when we convert this psymtab into a symtab. */
9558 add_partial_symbol (pdi, cu);
9562 if (! pdi->has_children)
9565 if (cu->language == language_ada)
9567 pdi = pdi->die_child;
9570 fixup_partial_die (pdi, cu);
9571 if (pdi->tag == DW_TAG_subprogram
9572 || pdi->tag == DW_TAG_inlined_subroutine
9573 || pdi->tag == DW_TAG_lexical_block)
9574 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9575 pdi = pdi->die_sibling;
9580 /* Read a partial die corresponding to an enumeration type. */
9583 add_partial_enumeration (struct partial_die_info *enum_pdi,
9584 struct dwarf2_cu *cu)
9586 struct partial_die_info *pdi;
9588 if (enum_pdi->name != NULL)
9589 add_partial_symbol (enum_pdi, cu);
9591 pdi = enum_pdi->die_child;
9594 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9595 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9597 add_partial_symbol (pdi, cu);
9598 pdi = pdi->die_sibling;
9602 /* Return the initial uleb128 in the die at INFO_PTR. */
9605 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9607 unsigned int bytes_read;
9609 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9612 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9613 Return the corresponding abbrev, or NULL if the number is zero (indicating
9614 an empty DIE). In either case *BYTES_READ will be set to the length of
9615 the initial number. */
9617 static struct abbrev_info *
9618 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
9619 struct dwarf2_cu *cu)
9621 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9622 unsigned int abbrev_number;
9623 struct abbrev_info *abbrev;
9625 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9627 if (abbrev_number == 0)
9630 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
9633 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9634 " at offset 0x%x [in module %s]"),
9635 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9636 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9642 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9643 Returns a pointer to the end of a series of DIEs, terminated by an empty
9644 DIE. Any children of the skipped DIEs will also be skipped. */
9646 static const gdb_byte *
9647 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9649 struct dwarf2_cu *cu = reader->cu;
9650 struct abbrev_info *abbrev;
9651 unsigned int bytes_read;
9655 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9657 return info_ptr + bytes_read;
9659 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9663 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9664 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9665 abbrev corresponding to that skipped uleb128 should be passed in
9666 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9669 static const gdb_byte *
9670 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9671 struct abbrev_info *abbrev)
9673 unsigned int bytes_read;
9674 struct attribute attr;
9675 bfd *abfd = reader->abfd;
9676 struct dwarf2_cu *cu = reader->cu;
9677 const gdb_byte *buffer = reader->buffer;
9678 const gdb_byte *buffer_end = reader->buffer_end;
9679 unsigned int form, i;
9681 for (i = 0; i < abbrev->num_attrs; i++)
9683 /* The only abbrev we care about is DW_AT_sibling. */
9684 if (abbrev->attrs[i].name == DW_AT_sibling)
9686 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9687 if (attr.form == DW_FORM_ref_addr)
9688 complaint (&symfile_complaints,
9689 _("ignoring absolute DW_AT_sibling"));
9692 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9693 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9695 if (sibling_ptr < info_ptr)
9696 complaint (&symfile_complaints,
9697 _("DW_AT_sibling points backwards"));
9698 else if (sibling_ptr > reader->buffer_end)
9699 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9705 /* If it isn't DW_AT_sibling, skip this attribute. */
9706 form = abbrev->attrs[i].form;
9710 case DW_FORM_ref_addr:
9711 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9712 and later it is offset sized. */
9713 if (cu->header.version == 2)
9714 info_ptr += cu->header.addr_size;
9716 info_ptr += cu->header.offset_size;
9718 case DW_FORM_GNU_ref_alt:
9719 info_ptr += cu->header.offset_size;
9722 info_ptr += cu->header.addr_size;
9729 case DW_FORM_flag_present:
9730 case DW_FORM_implicit_const:
9742 case DW_FORM_ref_sig8:
9745 case DW_FORM_data16:
9748 case DW_FORM_string:
9749 read_direct_string (abfd, info_ptr, &bytes_read);
9750 info_ptr += bytes_read;
9752 case DW_FORM_sec_offset:
9754 case DW_FORM_GNU_strp_alt:
9755 info_ptr += cu->header.offset_size;
9757 case DW_FORM_exprloc:
9759 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9760 info_ptr += bytes_read;
9762 case DW_FORM_block1:
9763 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9765 case DW_FORM_block2:
9766 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9768 case DW_FORM_block4:
9769 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9773 case DW_FORM_ref_udata:
9774 case DW_FORM_GNU_addr_index:
9775 case DW_FORM_GNU_str_index:
9776 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9778 case DW_FORM_indirect:
9779 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9780 info_ptr += bytes_read;
9781 /* We need to continue parsing from here, so just go back to
9783 goto skip_attribute;
9786 error (_("Dwarf Error: Cannot handle %s "
9787 "in DWARF reader [in module %s]"),
9788 dwarf_form_name (form),
9789 bfd_get_filename (abfd));
9793 if (abbrev->has_children)
9794 return skip_children (reader, info_ptr);
9799 /* Locate ORIG_PDI's sibling.
9800 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9802 static const gdb_byte *
9803 locate_pdi_sibling (const struct die_reader_specs *reader,
9804 struct partial_die_info *orig_pdi,
9805 const gdb_byte *info_ptr)
9807 /* Do we know the sibling already? */
9809 if (orig_pdi->sibling)
9810 return orig_pdi->sibling;
9812 /* Are there any children to deal with? */
9814 if (!orig_pdi->has_children)
9817 /* Skip the children the long way. */
9819 return skip_children (reader, info_ptr);
9822 /* Expand this partial symbol table into a full symbol table. SELF is
9826 dwarf2_read_symtab (struct partial_symtab *self,
9827 struct objfile *objfile)
9829 struct dwarf2_per_objfile *dwarf2_per_objfile
9830 = get_dwarf2_per_objfile (objfile);
9834 warning (_("bug: psymtab for %s is already read in."),
9841 printf_filtered (_("Reading in symbols for %s..."),
9843 gdb_flush (gdb_stdout);
9846 /* If this psymtab is constructed from a debug-only objfile, the
9847 has_section_at_zero flag will not necessarily be correct. We
9848 can get the correct value for this flag by looking at the data
9849 associated with the (presumably stripped) associated objfile. */
9850 if (objfile->separate_debug_objfile_backlink)
9852 struct dwarf2_per_objfile *dpo_backlink
9853 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9855 dwarf2_per_objfile->has_section_at_zero
9856 = dpo_backlink->has_section_at_zero;
9859 dwarf2_per_objfile->reading_partial_symbols = 0;
9861 psymtab_to_symtab_1 (self);
9863 /* Finish up the debug error message. */
9865 printf_filtered (_("done.\n"));
9868 process_cu_includes (dwarf2_per_objfile);
9871 /* Reading in full CUs. */
9873 /* Add PER_CU to the queue. */
9876 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9877 enum language pretend_language)
9879 struct dwarf2_queue_item *item;
9882 item = XNEW (struct dwarf2_queue_item);
9883 item->per_cu = per_cu;
9884 item->pretend_language = pretend_language;
9887 if (dwarf2_queue == NULL)
9888 dwarf2_queue = item;
9890 dwarf2_queue_tail->next = item;
9892 dwarf2_queue_tail = item;
9895 /* If PER_CU is not yet queued, add it to the queue.
9896 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9898 The result is non-zero if PER_CU was queued, otherwise the result is zero
9899 meaning either PER_CU is already queued or it is already loaded.
9901 N.B. There is an invariant here that if a CU is queued then it is loaded.
9902 The caller is required to load PER_CU if we return non-zero. */
9905 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9906 struct dwarf2_per_cu_data *per_cu,
9907 enum language pretend_language)
9909 /* We may arrive here during partial symbol reading, if we need full
9910 DIEs to process an unusual case (e.g. template arguments). Do
9911 not queue PER_CU, just tell our caller to load its DIEs. */
9912 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9914 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9919 /* Mark the dependence relation so that we don't flush PER_CU
9921 if (dependent_cu != NULL)
9922 dwarf2_add_dependence (dependent_cu, per_cu);
9924 /* If it's already on the queue, we have nothing to do. */
9928 /* If the compilation unit is already loaded, just mark it as
9930 if (per_cu->cu != NULL)
9932 per_cu->cu->last_used = 0;
9936 /* Add it to the queue. */
9937 queue_comp_unit (per_cu, pretend_language);
9942 /* Process the queue. */
9945 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9947 struct dwarf2_queue_item *item, *next_item;
9949 if (dwarf_read_debug)
9951 fprintf_unfiltered (gdb_stdlog,
9952 "Expanding one or more symtabs of objfile %s ...\n",
9953 objfile_name (dwarf2_per_objfile->objfile));
9956 /* The queue starts out with one item, but following a DIE reference
9957 may load a new CU, adding it to the end of the queue. */
9958 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9960 if ((dwarf2_per_objfile->using_index
9961 ? !item->per_cu->v.quick->compunit_symtab
9962 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9963 /* Skip dummy CUs. */
9964 && item->per_cu->cu != NULL)
9966 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9967 unsigned int debug_print_threshold;
9970 if (per_cu->is_debug_types)
9972 struct signatured_type *sig_type =
9973 (struct signatured_type *) per_cu;
9975 sprintf (buf, "TU %s at offset 0x%x",
9976 hex_string (sig_type->signature),
9977 to_underlying (per_cu->sect_off));
9978 /* There can be 100s of TUs.
9979 Only print them in verbose mode. */
9980 debug_print_threshold = 2;
9984 sprintf (buf, "CU at offset 0x%x",
9985 to_underlying (per_cu->sect_off));
9986 debug_print_threshold = 1;
9989 if (dwarf_read_debug >= debug_print_threshold)
9990 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9992 if (per_cu->is_debug_types)
9993 process_full_type_unit (per_cu, item->pretend_language);
9995 process_full_comp_unit (per_cu, item->pretend_language);
9997 if (dwarf_read_debug >= debug_print_threshold)
9998 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10001 item->per_cu->queued = 0;
10002 next_item = item->next;
10006 dwarf2_queue_tail = NULL;
10008 if (dwarf_read_debug)
10010 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10011 objfile_name (dwarf2_per_objfile->objfile));
10015 /* Free all allocated queue entries. This function only releases anything if
10016 an error was thrown; if the queue was processed then it would have been
10017 freed as we went along. */
10020 dwarf2_release_queue (void *dummy)
10022 struct dwarf2_queue_item *item, *last;
10024 item = dwarf2_queue;
10027 /* Anything still marked queued is likely to be in an
10028 inconsistent state, so discard it. */
10029 if (item->per_cu->queued)
10031 if (item->per_cu->cu != NULL)
10032 free_one_cached_comp_unit (item->per_cu);
10033 item->per_cu->queued = 0;
10041 dwarf2_queue = dwarf2_queue_tail = NULL;
10044 /* Read in full symbols for PST, and anything it depends on. */
10047 psymtab_to_symtab_1 (struct partial_symtab *pst)
10049 struct dwarf2_per_cu_data *per_cu;
10055 for (i = 0; i < pst->number_of_dependencies; i++)
10056 if (!pst->dependencies[i]->readin
10057 && pst->dependencies[i]->user == NULL)
10059 /* Inform about additional files that need to be read in. */
10062 /* FIXME: i18n: Need to make this a single string. */
10063 fputs_filtered (" ", gdb_stdout);
10065 fputs_filtered ("and ", gdb_stdout);
10067 printf_filtered ("%s...", pst->dependencies[i]->filename);
10068 wrap_here (""); /* Flush output. */
10069 gdb_flush (gdb_stdout);
10071 psymtab_to_symtab_1 (pst->dependencies[i]);
10074 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10076 if (per_cu == NULL)
10078 /* It's an include file, no symbols to read for it.
10079 Everything is in the parent symtab. */
10084 dw2_do_instantiate_symtab (per_cu);
10087 /* Trivial hash function for die_info: the hash value of a DIE
10088 is its offset in .debug_info for this objfile. */
10091 die_hash (const void *item)
10093 const struct die_info *die = (const struct die_info *) item;
10095 return to_underlying (die->sect_off);
10098 /* Trivial comparison function for die_info structures: two DIEs
10099 are equal if they have the same offset. */
10102 die_eq (const void *item_lhs, const void *item_rhs)
10104 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10105 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10107 return die_lhs->sect_off == die_rhs->sect_off;
10110 /* die_reader_func for load_full_comp_unit.
10111 This is identical to read_signatured_type_reader,
10112 but is kept separate for now. */
10115 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10116 const gdb_byte *info_ptr,
10117 struct die_info *comp_unit_die,
10121 struct dwarf2_cu *cu = reader->cu;
10122 enum language *language_ptr = (enum language *) data;
10124 gdb_assert (cu->die_hash == NULL);
10126 htab_create_alloc_ex (cu->header.length / 12,
10130 &cu->comp_unit_obstack,
10131 hashtab_obstack_allocate,
10132 dummy_obstack_deallocate);
10135 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10136 &info_ptr, comp_unit_die);
10137 cu->dies = comp_unit_die;
10138 /* comp_unit_die is not stored in die_hash, no need. */
10140 /* We try not to read any attributes in this function, because not
10141 all CUs needed for references have been loaded yet, and symbol
10142 table processing isn't initialized. But we have to set the CU language,
10143 or we won't be able to build types correctly.
10144 Similarly, if we do not read the producer, we can not apply
10145 producer-specific interpretation. */
10146 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10149 /* Load the DIEs associated with PER_CU into memory. */
10152 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10153 enum language pretend_language)
10155 gdb_assert (! this_cu->is_debug_types);
10157 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10158 load_full_comp_unit_reader, &pretend_language);
10161 /* Add a DIE to the delayed physname list. */
10164 add_to_method_list (struct type *type, int fnfield_index, int index,
10165 const char *name, struct die_info *die,
10166 struct dwarf2_cu *cu)
10168 struct delayed_method_info mi;
10170 mi.fnfield_index = fnfield_index;
10174 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10177 /* A cleanup for freeing the delayed method list. */
10180 free_delayed_list (void *ptr)
10182 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10183 if (cu->method_list != NULL)
10185 VEC_free (delayed_method_info, cu->method_list);
10186 cu->method_list = NULL;
10190 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10191 "const" / "volatile". If so, decrements LEN by the length of the
10192 modifier and return true. Otherwise return false. */
10196 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10198 size_t mod_len = sizeof (mod) - 1;
10199 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10207 /* Compute the physnames of any methods on the CU's method list.
10209 The computation of method physnames is delayed in order to avoid the
10210 (bad) condition that one of the method's formal parameters is of an as yet
10211 incomplete type. */
10214 compute_delayed_physnames (struct dwarf2_cu *cu)
10217 struct delayed_method_info *mi;
10219 /* Only C++ delays computing physnames. */
10220 if (VEC_empty (delayed_method_info, cu->method_list))
10222 gdb_assert (cu->language == language_cplus);
10224 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10226 const char *physname;
10227 struct fn_fieldlist *fn_flp
10228 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
10229 physname = dwarf2_physname (mi->name, mi->die, cu);
10230 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10231 = physname ? physname : "";
10233 /* Since there's no tag to indicate whether a method is a
10234 const/volatile overload, extract that information out of the
10236 if (physname != NULL)
10238 size_t len = strlen (physname);
10242 if (physname[len] == ')') /* shortcut */
10244 else if (check_modifier (physname, len, " const"))
10245 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10246 else if (check_modifier (physname, len, " volatile"))
10247 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10255 /* Go objects should be embedded in a DW_TAG_module DIE,
10256 and it's not clear if/how imported objects will appear.
10257 To keep Go support simple until that's worked out,
10258 go back through what we've read and create something usable.
10259 We could do this while processing each DIE, and feels kinda cleaner,
10260 but that way is more invasive.
10261 This is to, for example, allow the user to type "p var" or "b main"
10262 without having to specify the package name, and allow lookups
10263 of module.object to work in contexts that use the expression
10267 fixup_go_packaging (struct dwarf2_cu *cu)
10269 char *package_name = NULL;
10270 struct pending *list;
10273 for (list = global_symbols; list != NULL; list = list->next)
10275 for (i = 0; i < list->nsyms; ++i)
10277 struct symbol *sym = list->symbol[i];
10279 if (SYMBOL_LANGUAGE (sym) == language_go
10280 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10282 char *this_package_name = go_symbol_package_name (sym);
10284 if (this_package_name == NULL)
10286 if (package_name == NULL)
10287 package_name = this_package_name;
10290 struct objfile *objfile
10291 = cu->per_cu->dwarf2_per_objfile->objfile;
10292 if (strcmp (package_name, this_package_name) != 0)
10293 complaint (&symfile_complaints,
10294 _("Symtab %s has objects from two different Go packages: %s and %s"),
10295 (symbol_symtab (sym) != NULL
10296 ? symtab_to_filename_for_display
10297 (symbol_symtab (sym))
10298 : objfile_name (objfile)),
10299 this_package_name, package_name);
10300 xfree (this_package_name);
10306 if (package_name != NULL)
10308 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10309 const char *saved_package_name
10310 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10312 strlen (package_name));
10313 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10314 saved_package_name);
10315 struct symbol *sym;
10317 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10319 sym = allocate_symbol (objfile);
10320 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10321 SYMBOL_SET_NAMES (sym, saved_package_name,
10322 strlen (saved_package_name), 0, objfile);
10323 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10324 e.g., "main" finds the "main" module and not C's main(). */
10325 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10326 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10327 SYMBOL_TYPE (sym) = type;
10329 add_symbol_to_list (sym, &global_symbols);
10331 xfree (package_name);
10335 /* Return the symtab for PER_CU. This works properly regardless of
10336 whether we're using the index or psymtabs. */
10338 static struct compunit_symtab *
10339 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10341 return (per_cu->dwarf2_per_objfile->using_index
10342 ? per_cu->v.quick->compunit_symtab
10343 : per_cu->v.psymtab->compunit_symtab);
10346 /* A helper function for computing the list of all symbol tables
10347 included by PER_CU. */
10350 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10351 htab_t all_children, htab_t all_type_symtabs,
10352 struct dwarf2_per_cu_data *per_cu,
10353 struct compunit_symtab *immediate_parent)
10357 struct compunit_symtab *cust;
10358 struct dwarf2_per_cu_data *iter;
10360 slot = htab_find_slot (all_children, per_cu, INSERT);
10363 /* This inclusion and its children have been processed. */
10368 /* Only add a CU if it has a symbol table. */
10369 cust = get_compunit_symtab (per_cu);
10372 /* If this is a type unit only add its symbol table if we haven't
10373 seen it yet (type unit per_cu's can share symtabs). */
10374 if (per_cu->is_debug_types)
10376 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10380 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10381 if (cust->user == NULL)
10382 cust->user = immediate_parent;
10387 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10388 if (cust->user == NULL)
10389 cust->user = immediate_parent;
10394 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10397 recursively_compute_inclusions (result, all_children,
10398 all_type_symtabs, iter, cust);
10402 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10406 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10408 gdb_assert (! per_cu->is_debug_types);
10410 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10413 struct dwarf2_per_cu_data *per_cu_iter;
10414 struct compunit_symtab *compunit_symtab_iter;
10415 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10416 htab_t all_children, all_type_symtabs;
10417 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10419 /* If we don't have a symtab, we can just skip this case. */
10423 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10424 NULL, xcalloc, xfree);
10425 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10426 NULL, xcalloc, xfree);
10429 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10433 recursively_compute_inclusions (&result_symtabs, all_children,
10434 all_type_symtabs, per_cu_iter,
10438 /* Now we have a transitive closure of all the included symtabs. */
10439 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10441 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10442 struct compunit_symtab *, len + 1);
10444 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10445 compunit_symtab_iter);
10447 cust->includes[ix] = compunit_symtab_iter;
10448 cust->includes[len] = NULL;
10450 VEC_free (compunit_symtab_ptr, result_symtabs);
10451 htab_delete (all_children);
10452 htab_delete (all_type_symtabs);
10456 /* Compute the 'includes' field for the symtabs of all the CUs we just
10460 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10463 struct dwarf2_per_cu_data *iter;
10466 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10470 if (! iter->is_debug_types)
10471 compute_compunit_symtab_includes (iter);
10474 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10477 /* Generate full symbol information for PER_CU, whose DIEs have
10478 already been loaded into memory. */
10481 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10482 enum language pretend_language)
10484 struct dwarf2_cu *cu = per_cu->cu;
10485 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10486 struct objfile *objfile = dwarf2_per_objfile->objfile;
10487 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10488 CORE_ADDR lowpc, highpc;
10489 struct compunit_symtab *cust;
10490 struct cleanup *delayed_list_cleanup;
10491 CORE_ADDR baseaddr;
10492 struct block *static_block;
10495 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10498 scoped_free_pendings free_pending;
10499 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10501 cu->list_in_scope = &file_symbols;
10503 cu->language = pretend_language;
10504 cu->language_defn = language_def (cu->language);
10506 /* Do line number decoding in read_file_scope () */
10507 process_die (cu->dies, cu);
10509 /* For now fudge the Go package. */
10510 if (cu->language == language_go)
10511 fixup_go_packaging (cu);
10513 /* Now that we have processed all the DIEs in the CU, all the types
10514 should be complete, and it should now be safe to compute all of the
10516 compute_delayed_physnames (cu);
10517 do_cleanups (delayed_list_cleanup);
10519 /* Some compilers don't define a DW_AT_high_pc attribute for the
10520 compilation unit. If the DW_AT_high_pc is missing, synthesize
10521 it, by scanning the DIE's below the compilation unit. */
10522 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10524 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10525 static_block = end_symtab_get_static_block (addr, 0, 1);
10527 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10528 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10529 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10530 addrmap to help ensure it has an accurate map of pc values belonging to
10532 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10534 cust = end_symtab_from_static_block (static_block,
10535 SECT_OFF_TEXT (objfile), 0);
10539 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10541 /* Set symtab language to language from DW_AT_language. If the
10542 compilation is from a C file generated by language preprocessors, do
10543 not set the language if it was already deduced by start_subfile. */
10544 if (!(cu->language == language_c
10545 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10546 COMPUNIT_FILETABS (cust)->language = cu->language;
10548 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10549 produce DW_AT_location with location lists but it can be possibly
10550 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10551 there were bugs in prologue debug info, fixed later in GCC-4.5
10552 by "unwind info for epilogues" patch (which is not directly related).
10554 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10555 needed, it would be wrong due to missing DW_AT_producer there.
10557 Still one can confuse GDB by using non-standard GCC compilation
10558 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10560 if (cu->has_loclist && gcc_4_minor >= 5)
10561 cust->locations_valid = 1;
10563 if (gcc_4_minor >= 5)
10564 cust->epilogue_unwind_valid = 1;
10566 cust->call_site_htab = cu->call_site_htab;
10569 if (dwarf2_per_objfile->using_index)
10570 per_cu->v.quick->compunit_symtab = cust;
10573 struct partial_symtab *pst = per_cu->v.psymtab;
10574 pst->compunit_symtab = cust;
10578 /* Push it for inclusion processing later. */
10579 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10582 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10583 already been loaded into memory. */
10586 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10587 enum language pretend_language)
10589 struct dwarf2_cu *cu = per_cu->cu;
10590 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10591 struct objfile *objfile = dwarf2_per_objfile->objfile;
10592 struct compunit_symtab *cust;
10593 struct cleanup *delayed_list_cleanup;
10594 struct signatured_type *sig_type;
10596 gdb_assert (per_cu->is_debug_types);
10597 sig_type = (struct signatured_type *) per_cu;
10600 scoped_free_pendings free_pending;
10601 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10603 cu->list_in_scope = &file_symbols;
10605 cu->language = pretend_language;
10606 cu->language_defn = language_def (cu->language);
10608 /* The symbol tables are set up in read_type_unit_scope. */
10609 process_die (cu->dies, cu);
10611 /* For now fudge the Go package. */
10612 if (cu->language == language_go)
10613 fixup_go_packaging (cu);
10615 /* Now that we have processed all the DIEs in the CU, all the types
10616 should be complete, and it should now be safe to compute all of the
10618 compute_delayed_physnames (cu);
10619 do_cleanups (delayed_list_cleanup);
10621 /* TUs share symbol tables.
10622 If this is the first TU to use this symtab, complete the construction
10623 of it with end_expandable_symtab. Otherwise, complete the addition of
10624 this TU's symbols to the existing symtab. */
10625 if (sig_type->type_unit_group->compunit_symtab == NULL)
10627 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10628 sig_type->type_unit_group->compunit_symtab = cust;
10632 /* Set symtab language to language from DW_AT_language. If the
10633 compilation is from a C file generated by language preprocessors,
10634 do not set the language if it was already deduced by
10636 if (!(cu->language == language_c
10637 && COMPUNIT_FILETABS (cust)->language != language_c))
10638 COMPUNIT_FILETABS (cust)->language = cu->language;
10643 augment_type_symtab ();
10644 cust = sig_type->type_unit_group->compunit_symtab;
10647 if (dwarf2_per_objfile->using_index)
10648 per_cu->v.quick->compunit_symtab = cust;
10651 struct partial_symtab *pst = per_cu->v.psymtab;
10652 pst->compunit_symtab = cust;
10657 /* Process an imported unit DIE. */
10660 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10662 struct attribute *attr;
10664 /* For now we don't handle imported units in type units. */
10665 if (cu->per_cu->is_debug_types)
10667 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10668 " supported in type units [in module %s]"),
10669 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10672 attr = dwarf2_attr (die, DW_AT_import, cu);
10675 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10676 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10677 dwarf2_per_cu_data *per_cu
10678 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10679 cu->per_cu->dwarf2_per_objfile);
10681 /* If necessary, add it to the queue and load its DIEs. */
10682 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10683 load_full_comp_unit (per_cu, cu->language);
10685 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10690 /* RAII object that represents a process_die scope: i.e.,
10691 starts/finishes processing a DIE. */
10692 class process_die_scope
10695 process_die_scope (die_info *die, dwarf2_cu *cu)
10696 : m_die (die), m_cu (cu)
10698 /* We should only be processing DIEs not already in process. */
10699 gdb_assert (!m_die->in_process);
10700 m_die->in_process = true;
10703 ~process_die_scope ()
10705 m_die->in_process = false;
10707 /* If we're done processing the DIE for the CU that owns the line
10708 header, we don't need the line header anymore. */
10709 if (m_cu->line_header_die_owner == m_die)
10711 delete m_cu->line_header;
10712 m_cu->line_header = NULL;
10713 m_cu->line_header_die_owner = NULL;
10722 /* Process a die and its children. */
10725 process_die (struct die_info *die, struct dwarf2_cu *cu)
10727 process_die_scope scope (die, cu);
10731 case DW_TAG_padding:
10733 case DW_TAG_compile_unit:
10734 case DW_TAG_partial_unit:
10735 read_file_scope (die, cu);
10737 case DW_TAG_type_unit:
10738 read_type_unit_scope (die, cu);
10740 case DW_TAG_subprogram:
10741 case DW_TAG_inlined_subroutine:
10742 read_func_scope (die, cu);
10744 case DW_TAG_lexical_block:
10745 case DW_TAG_try_block:
10746 case DW_TAG_catch_block:
10747 read_lexical_block_scope (die, cu);
10749 case DW_TAG_call_site:
10750 case DW_TAG_GNU_call_site:
10751 read_call_site_scope (die, cu);
10753 case DW_TAG_class_type:
10754 case DW_TAG_interface_type:
10755 case DW_TAG_structure_type:
10756 case DW_TAG_union_type:
10757 process_structure_scope (die, cu);
10759 case DW_TAG_enumeration_type:
10760 process_enumeration_scope (die, cu);
10763 /* These dies have a type, but processing them does not create
10764 a symbol or recurse to process the children. Therefore we can
10765 read them on-demand through read_type_die. */
10766 case DW_TAG_subroutine_type:
10767 case DW_TAG_set_type:
10768 case DW_TAG_array_type:
10769 case DW_TAG_pointer_type:
10770 case DW_TAG_ptr_to_member_type:
10771 case DW_TAG_reference_type:
10772 case DW_TAG_rvalue_reference_type:
10773 case DW_TAG_string_type:
10776 case DW_TAG_base_type:
10777 case DW_TAG_subrange_type:
10778 case DW_TAG_typedef:
10779 /* Add a typedef symbol for the type definition, if it has a
10781 new_symbol (die, read_type_die (die, cu), cu);
10783 case DW_TAG_common_block:
10784 read_common_block (die, cu);
10786 case DW_TAG_common_inclusion:
10788 case DW_TAG_namespace:
10789 cu->processing_has_namespace_info = 1;
10790 read_namespace (die, cu);
10792 case DW_TAG_module:
10793 cu->processing_has_namespace_info = 1;
10794 read_module (die, cu);
10796 case DW_TAG_imported_declaration:
10797 cu->processing_has_namespace_info = 1;
10798 if (read_namespace_alias (die, cu))
10800 /* The declaration is not a global namespace alias: fall through. */
10801 case DW_TAG_imported_module:
10802 cu->processing_has_namespace_info = 1;
10803 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10804 || cu->language != language_fortran))
10805 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10806 dwarf_tag_name (die->tag));
10807 read_import_statement (die, cu);
10810 case DW_TAG_imported_unit:
10811 process_imported_unit_die (die, cu);
10814 case DW_TAG_variable:
10815 read_variable (die, cu);
10819 new_symbol (die, NULL, cu);
10824 /* DWARF name computation. */
10826 /* A helper function for dwarf2_compute_name which determines whether DIE
10827 needs to have the name of the scope prepended to the name listed in the
10831 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10833 struct attribute *attr;
10837 case DW_TAG_namespace:
10838 case DW_TAG_typedef:
10839 case DW_TAG_class_type:
10840 case DW_TAG_interface_type:
10841 case DW_TAG_structure_type:
10842 case DW_TAG_union_type:
10843 case DW_TAG_enumeration_type:
10844 case DW_TAG_enumerator:
10845 case DW_TAG_subprogram:
10846 case DW_TAG_inlined_subroutine:
10847 case DW_TAG_member:
10848 case DW_TAG_imported_declaration:
10851 case DW_TAG_variable:
10852 case DW_TAG_constant:
10853 /* We only need to prefix "globally" visible variables. These include
10854 any variable marked with DW_AT_external or any variable that
10855 lives in a namespace. [Variables in anonymous namespaces
10856 require prefixing, but they are not DW_AT_external.] */
10858 if (dwarf2_attr (die, DW_AT_specification, cu))
10860 struct dwarf2_cu *spec_cu = cu;
10862 return die_needs_namespace (die_specification (die, &spec_cu),
10866 attr = dwarf2_attr (die, DW_AT_external, cu);
10867 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10868 && die->parent->tag != DW_TAG_module)
10870 /* A variable in a lexical block of some kind does not need a
10871 namespace, even though in C++ such variables may be external
10872 and have a mangled name. */
10873 if (die->parent->tag == DW_TAG_lexical_block
10874 || die->parent->tag == DW_TAG_try_block
10875 || die->parent->tag == DW_TAG_catch_block
10876 || die->parent->tag == DW_TAG_subprogram)
10885 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10886 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10887 defined for the given DIE. */
10889 static struct attribute *
10890 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10892 struct attribute *attr;
10894 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10896 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10901 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10902 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10903 defined for the given DIE. */
10905 static const char *
10906 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10908 const char *linkage_name;
10910 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10911 if (linkage_name == NULL)
10912 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10914 return linkage_name;
10917 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10918 compute the physname for the object, which include a method's:
10919 - formal parameters (C++),
10920 - receiver type (Go),
10922 The term "physname" is a bit confusing.
10923 For C++, for example, it is the demangled name.
10924 For Go, for example, it's the mangled name.
10926 For Ada, return the DIE's linkage name rather than the fully qualified
10927 name. PHYSNAME is ignored..
10929 The result is allocated on the objfile_obstack and canonicalized. */
10931 static const char *
10932 dwarf2_compute_name (const char *name,
10933 struct die_info *die, struct dwarf2_cu *cu,
10936 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10939 name = dwarf2_name (die, cu);
10941 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10942 but otherwise compute it by typename_concat inside GDB.
10943 FIXME: Actually this is not really true, or at least not always true.
10944 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10945 Fortran names because there is no mangling standard. So new_symbol_full
10946 will set the demangled name to the result of dwarf2_full_name, and it is
10947 the demangled name that GDB uses if it exists. */
10948 if (cu->language == language_ada
10949 || (cu->language == language_fortran && physname))
10951 /* For Ada unit, we prefer the linkage name over the name, as
10952 the former contains the exported name, which the user expects
10953 to be able to reference. Ideally, we want the user to be able
10954 to reference this entity using either natural or linkage name,
10955 but we haven't started looking at this enhancement yet. */
10956 const char *linkage_name = dw2_linkage_name (die, cu);
10958 if (linkage_name != NULL)
10959 return linkage_name;
10962 /* These are the only languages we know how to qualify names in. */
10964 && (cu->language == language_cplus
10965 || cu->language == language_fortran || cu->language == language_d
10966 || cu->language == language_rust))
10968 if (die_needs_namespace (die, cu))
10970 const char *prefix;
10971 const char *canonical_name = NULL;
10975 prefix = determine_prefix (die, cu);
10976 if (*prefix != '\0')
10978 char *prefixed_name = typename_concat (NULL, prefix, name,
10981 buf.puts (prefixed_name);
10982 xfree (prefixed_name);
10987 /* Template parameters may be specified in the DIE's DW_AT_name, or
10988 as children with DW_TAG_template_type_param or
10989 DW_TAG_value_type_param. If the latter, add them to the name
10990 here. If the name already has template parameters, then
10991 skip this step; some versions of GCC emit both, and
10992 it is more efficient to use the pre-computed name.
10994 Something to keep in mind about this process: it is very
10995 unlikely, or in some cases downright impossible, to produce
10996 something that will match the mangled name of a function.
10997 If the definition of the function has the same debug info,
10998 we should be able to match up with it anyway. But fallbacks
10999 using the minimal symbol, for instance to find a method
11000 implemented in a stripped copy of libstdc++, will not work.
11001 If we do not have debug info for the definition, we will have to
11002 match them up some other way.
11004 When we do name matching there is a related problem with function
11005 templates; two instantiated function templates are allowed to
11006 differ only by their return types, which we do not add here. */
11008 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11010 struct attribute *attr;
11011 struct die_info *child;
11014 die->building_fullname = 1;
11016 for (child = die->child; child != NULL; child = child->sibling)
11020 const gdb_byte *bytes;
11021 struct dwarf2_locexpr_baton *baton;
11024 if (child->tag != DW_TAG_template_type_param
11025 && child->tag != DW_TAG_template_value_param)
11036 attr = dwarf2_attr (child, DW_AT_type, cu);
11039 complaint (&symfile_complaints,
11040 _("template parameter missing DW_AT_type"));
11041 buf.puts ("UNKNOWN_TYPE");
11044 type = die_type (child, cu);
11046 if (child->tag == DW_TAG_template_type_param)
11048 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11052 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11055 complaint (&symfile_complaints,
11056 _("template parameter missing "
11057 "DW_AT_const_value"));
11058 buf.puts ("UNKNOWN_VALUE");
11062 dwarf2_const_value_attr (attr, type, name,
11063 &cu->comp_unit_obstack, cu,
11064 &value, &bytes, &baton);
11066 if (TYPE_NOSIGN (type))
11067 /* GDB prints characters as NUMBER 'CHAR'. If that's
11068 changed, this can use value_print instead. */
11069 c_printchar (value, type, &buf);
11072 struct value_print_options opts;
11075 v = dwarf2_evaluate_loc_desc (type, NULL,
11079 else if (bytes != NULL)
11081 v = allocate_value (type);
11082 memcpy (value_contents_writeable (v), bytes,
11083 TYPE_LENGTH (type));
11086 v = value_from_longest (type, value);
11088 /* Specify decimal so that we do not depend on
11090 get_formatted_print_options (&opts, 'd');
11092 value_print (v, &buf, &opts);
11098 die->building_fullname = 0;
11102 /* Close the argument list, with a space if necessary
11103 (nested templates). */
11104 if (!buf.empty () && buf.string ().back () == '>')
11111 /* For C++ methods, append formal parameter type
11112 information, if PHYSNAME. */
11114 if (physname && die->tag == DW_TAG_subprogram
11115 && cu->language == language_cplus)
11117 struct type *type = read_type_die (die, cu);
11119 c_type_print_args (type, &buf, 1, cu->language,
11120 &type_print_raw_options);
11122 if (cu->language == language_cplus)
11124 /* Assume that an artificial first parameter is
11125 "this", but do not crash if it is not. RealView
11126 marks unnamed (and thus unused) parameters as
11127 artificial; there is no way to differentiate
11129 if (TYPE_NFIELDS (type) > 0
11130 && TYPE_FIELD_ARTIFICIAL (type, 0)
11131 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11132 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11134 buf.puts (" const");
11138 const std::string &intermediate_name = buf.string ();
11140 if (cu->language == language_cplus)
11142 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11143 &objfile->per_bfd->storage_obstack);
11145 /* If we only computed INTERMEDIATE_NAME, or if
11146 INTERMEDIATE_NAME is already canonical, then we need to
11147 copy it to the appropriate obstack. */
11148 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11149 name = ((const char *)
11150 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11151 intermediate_name.c_str (),
11152 intermediate_name.length ()));
11154 name = canonical_name;
11161 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11162 If scope qualifiers are appropriate they will be added. The result
11163 will be allocated on the storage_obstack, or NULL if the DIE does
11164 not have a name. NAME may either be from a previous call to
11165 dwarf2_name or NULL.
11167 The output string will be canonicalized (if C++). */
11169 static const char *
11170 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11172 return dwarf2_compute_name (name, die, cu, 0);
11175 /* Construct a physname for the given DIE in CU. NAME may either be
11176 from a previous call to dwarf2_name or NULL. The result will be
11177 allocated on the objfile_objstack or NULL if the DIE does not have a
11180 The output string will be canonicalized (if C++). */
11182 static const char *
11183 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11185 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11186 const char *retval, *mangled = NULL, *canon = NULL;
11189 /* In this case dwarf2_compute_name is just a shortcut not building anything
11191 if (!die_needs_namespace (die, cu))
11192 return dwarf2_compute_name (name, die, cu, 1);
11194 mangled = dw2_linkage_name (die, cu);
11196 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11197 See https://github.com/rust-lang/rust/issues/32925. */
11198 if (cu->language == language_rust && mangled != NULL
11199 && strchr (mangled, '{') != NULL)
11202 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11204 gdb::unique_xmalloc_ptr<char> demangled;
11205 if (mangled != NULL)
11207 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11208 type. It is easier for GDB users to search for such functions as
11209 `name(params)' than `long name(params)'. In such case the minimal
11210 symbol names do not match the full symbol names but for template
11211 functions there is never a need to look up their definition from their
11212 declaration so the only disadvantage remains the minimal symbol
11213 variant `long name(params)' does not have the proper inferior type.
11216 if (cu->language == language_go)
11218 /* This is a lie, but we already lie to the caller new_symbol_full.
11219 new_symbol_full assumes we return the mangled name.
11220 This just undoes that lie until things are cleaned up. */
11224 demangled.reset (gdb_demangle (mangled,
11225 (DMGL_PARAMS | DMGL_ANSI
11226 | DMGL_RET_DROP)));
11229 canon = demangled.get ();
11237 if (canon == NULL || check_physname)
11239 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11241 if (canon != NULL && strcmp (physname, canon) != 0)
11243 /* It may not mean a bug in GDB. The compiler could also
11244 compute DW_AT_linkage_name incorrectly. But in such case
11245 GDB would need to be bug-to-bug compatible. */
11247 complaint (&symfile_complaints,
11248 _("Computed physname <%s> does not match demangled <%s> "
11249 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11250 physname, canon, mangled, to_underlying (die->sect_off),
11251 objfile_name (objfile));
11253 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11254 is available here - over computed PHYSNAME. It is safer
11255 against both buggy GDB and buggy compilers. */
11269 retval = ((const char *)
11270 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11271 retval, strlen (retval)));
11276 /* Inspect DIE in CU for a namespace alias. If one exists, record
11277 a new symbol for it.
11279 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11282 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11284 struct attribute *attr;
11286 /* If the die does not have a name, this is not a namespace
11288 attr = dwarf2_attr (die, DW_AT_name, cu);
11292 struct die_info *d = die;
11293 struct dwarf2_cu *imported_cu = cu;
11295 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11296 keep inspecting DIEs until we hit the underlying import. */
11297 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11298 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11300 attr = dwarf2_attr (d, DW_AT_import, cu);
11304 d = follow_die_ref (d, attr, &imported_cu);
11305 if (d->tag != DW_TAG_imported_declaration)
11309 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11311 complaint (&symfile_complaints,
11312 _("DIE at 0x%x has too many recursively imported "
11313 "declarations"), to_underlying (d->sect_off));
11320 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11322 type = get_die_type_at_offset (sect_off, cu->per_cu);
11323 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11325 /* This declaration is a global namespace alias. Add
11326 a symbol for it whose type is the aliased namespace. */
11327 new_symbol (die, type, cu);
11336 /* Return the using directives repository (global or local?) to use in the
11337 current context for LANGUAGE.
11339 For Ada, imported declarations can materialize renamings, which *may* be
11340 global. However it is impossible (for now?) in DWARF to distinguish
11341 "external" imported declarations and "static" ones. As all imported
11342 declarations seem to be static in all other languages, make them all CU-wide
11343 global only in Ada. */
11345 static struct using_direct **
11346 using_directives (enum language language)
11348 if (language == language_ada && context_stack_depth == 0)
11349 return &global_using_directives;
11351 return &local_using_directives;
11354 /* Read the import statement specified by the given die and record it. */
11357 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11360 struct attribute *import_attr;
11361 struct die_info *imported_die, *child_die;
11362 struct dwarf2_cu *imported_cu;
11363 const char *imported_name;
11364 const char *imported_name_prefix;
11365 const char *canonical_name;
11366 const char *import_alias;
11367 const char *imported_declaration = NULL;
11368 const char *import_prefix;
11369 std::vector<const char *> excludes;
11371 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11372 if (import_attr == NULL)
11374 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11375 dwarf_tag_name (die->tag));
11380 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11381 imported_name = dwarf2_name (imported_die, imported_cu);
11382 if (imported_name == NULL)
11384 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11386 The import in the following code:
11400 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11401 <52> DW_AT_decl_file : 1
11402 <53> DW_AT_decl_line : 6
11403 <54> DW_AT_import : <0x75>
11404 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11405 <59> DW_AT_name : B
11406 <5b> DW_AT_decl_file : 1
11407 <5c> DW_AT_decl_line : 2
11408 <5d> DW_AT_type : <0x6e>
11410 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11411 <76> DW_AT_byte_size : 4
11412 <77> DW_AT_encoding : 5 (signed)
11414 imports the wrong die ( 0x75 instead of 0x58 ).
11415 This case will be ignored until the gcc bug is fixed. */
11419 /* Figure out the local name after import. */
11420 import_alias = dwarf2_name (die, cu);
11422 /* Figure out where the statement is being imported to. */
11423 import_prefix = determine_prefix (die, cu);
11425 /* Figure out what the scope of the imported die is and prepend it
11426 to the name of the imported die. */
11427 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11429 if (imported_die->tag != DW_TAG_namespace
11430 && imported_die->tag != DW_TAG_module)
11432 imported_declaration = imported_name;
11433 canonical_name = imported_name_prefix;
11435 else if (strlen (imported_name_prefix) > 0)
11436 canonical_name = obconcat (&objfile->objfile_obstack,
11437 imported_name_prefix,
11438 (cu->language == language_d ? "." : "::"),
11439 imported_name, (char *) NULL);
11441 canonical_name = imported_name;
11443 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11444 for (child_die = die->child; child_die && child_die->tag;
11445 child_die = sibling_die (child_die))
11447 /* DWARF-4: A Fortran use statement with a “rename list” may be
11448 represented by an imported module entry with an import attribute
11449 referring to the module and owned entries corresponding to those
11450 entities that are renamed as part of being imported. */
11452 if (child_die->tag != DW_TAG_imported_declaration)
11454 complaint (&symfile_complaints,
11455 _("child DW_TAG_imported_declaration expected "
11456 "- DIE at 0x%x [in module %s]"),
11457 to_underlying (child_die->sect_off), objfile_name (objfile));
11461 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11462 if (import_attr == NULL)
11464 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11465 dwarf_tag_name (child_die->tag));
11470 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11472 imported_name = dwarf2_name (imported_die, imported_cu);
11473 if (imported_name == NULL)
11475 complaint (&symfile_complaints,
11476 _("child DW_TAG_imported_declaration has unknown "
11477 "imported name - DIE at 0x%x [in module %s]"),
11478 to_underlying (child_die->sect_off), objfile_name (objfile));
11482 excludes.push_back (imported_name);
11484 process_die (child_die, cu);
11487 add_using_directive (using_directives (cu->language),
11491 imported_declaration,
11494 &objfile->objfile_obstack);
11497 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11498 types, but gives them a size of zero. Starting with version 14,
11499 ICC is compatible with GCC. */
11502 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11504 if (!cu->checked_producer)
11505 check_producer (cu);
11507 return cu->producer_is_icc_lt_14;
11510 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11511 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11512 this, it was first present in GCC release 4.3.0. */
11515 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11517 if (!cu->checked_producer)
11518 check_producer (cu);
11520 return cu->producer_is_gcc_lt_4_3;
11523 static file_and_directory
11524 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11526 file_and_directory res;
11528 /* Find the filename. Do not use dwarf2_name here, since the filename
11529 is not a source language identifier. */
11530 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11531 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11533 if (res.comp_dir == NULL
11534 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11535 && IS_ABSOLUTE_PATH (res.name))
11537 res.comp_dir_storage = ldirname (res.name);
11538 if (!res.comp_dir_storage.empty ())
11539 res.comp_dir = res.comp_dir_storage.c_str ();
11541 if (res.comp_dir != NULL)
11543 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11544 directory, get rid of it. */
11545 const char *cp = strchr (res.comp_dir, ':');
11547 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11548 res.comp_dir = cp + 1;
11551 if (res.name == NULL)
11552 res.name = "<unknown>";
11557 /* Handle DW_AT_stmt_list for a compilation unit.
11558 DIE is the DW_TAG_compile_unit die for CU.
11559 COMP_DIR is the compilation directory. LOWPC is passed to
11560 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11563 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11564 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11566 struct dwarf2_per_objfile *dwarf2_per_objfile
11567 = cu->per_cu->dwarf2_per_objfile;
11568 struct objfile *objfile = dwarf2_per_objfile->objfile;
11569 struct attribute *attr;
11570 struct line_header line_header_local;
11571 hashval_t line_header_local_hash;
11573 int decode_mapping;
11575 gdb_assert (! cu->per_cu->is_debug_types);
11577 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11581 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11583 /* The line header hash table is only created if needed (it exists to
11584 prevent redundant reading of the line table for partial_units).
11585 If we're given a partial_unit, we'll need it. If we're given a
11586 compile_unit, then use the line header hash table if it's already
11587 created, but don't create one just yet. */
11589 if (dwarf2_per_objfile->line_header_hash == NULL
11590 && die->tag == DW_TAG_partial_unit)
11592 dwarf2_per_objfile->line_header_hash
11593 = htab_create_alloc_ex (127, line_header_hash_voidp,
11594 line_header_eq_voidp,
11595 free_line_header_voidp,
11596 &objfile->objfile_obstack,
11597 hashtab_obstack_allocate,
11598 dummy_obstack_deallocate);
11601 line_header_local.sect_off = line_offset;
11602 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11603 line_header_local_hash = line_header_hash (&line_header_local);
11604 if (dwarf2_per_objfile->line_header_hash != NULL)
11606 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11607 &line_header_local,
11608 line_header_local_hash, NO_INSERT);
11610 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11611 is not present in *SLOT (since if there is something in *SLOT then
11612 it will be for a partial_unit). */
11613 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11615 gdb_assert (*slot != NULL);
11616 cu->line_header = (struct line_header *) *slot;
11621 /* dwarf_decode_line_header does not yet provide sufficient information.
11622 We always have to call also dwarf_decode_lines for it. */
11623 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11627 cu->line_header = lh.release ();
11628 cu->line_header_die_owner = die;
11630 if (dwarf2_per_objfile->line_header_hash == NULL)
11634 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11635 &line_header_local,
11636 line_header_local_hash, INSERT);
11637 gdb_assert (slot != NULL);
11639 if (slot != NULL && *slot == NULL)
11641 /* This newly decoded line number information unit will be owned
11642 by line_header_hash hash table. */
11643 *slot = cu->line_header;
11644 cu->line_header_die_owner = NULL;
11648 /* We cannot free any current entry in (*slot) as that struct line_header
11649 may be already used by multiple CUs. Create only temporary decoded
11650 line_header for this CU - it may happen at most once for each line
11651 number information unit. And if we're not using line_header_hash
11652 then this is what we want as well. */
11653 gdb_assert (die->tag != DW_TAG_partial_unit);
11655 decode_mapping = (die->tag != DW_TAG_partial_unit);
11656 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11661 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11664 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11666 struct dwarf2_per_objfile *dwarf2_per_objfile
11667 = cu->per_cu->dwarf2_per_objfile;
11668 struct objfile *objfile = dwarf2_per_objfile->objfile;
11669 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11670 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11671 CORE_ADDR highpc = ((CORE_ADDR) 0);
11672 struct attribute *attr;
11673 struct die_info *child_die;
11674 CORE_ADDR baseaddr;
11676 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11678 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11680 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11681 from finish_block. */
11682 if (lowpc == ((CORE_ADDR) -1))
11684 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11686 file_and_directory fnd = find_file_and_directory (die, cu);
11688 prepare_one_comp_unit (cu, die, cu->language);
11690 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11691 standardised yet. As a workaround for the language detection we fall
11692 back to the DW_AT_producer string. */
11693 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11694 cu->language = language_opencl;
11696 /* Similar hack for Go. */
11697 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11698 set_cu_language (DW_LANG_Go, cu);
11700 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11702 /* Decode line number information if present. We do this before
11703 processing child DIEs, so that the line header table is available
11704 for DW_AT_decl_file. */
11705 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11707 /* Process all dies in compilation unit. */
11708 if (die->child != NULL)
11710 child_die = die->child;
11711 while (child_die && child_die->tag)
11713 process_die (child_die, cu);
11714 child_die = sibling_die (child_die);
11718 /* Decode macro information, if present. Dwarf 2 macro information
11719 refers to information in the line number info statement program
11720 header, so we can only read it if we've read the header
11722 attr = dwarf2_attr (die, DW_AT_macros, cu);
11724 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11725 if (attr && cu->line_header)
11727 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11728 complaint (&symfile_complaints,
11729 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11731 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11735 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11736 if (attr && cu->line_header)
11738 unsigned int macro_offset = DW_UNSND (attr);
11740 dwarf_decode_macros (cu, macro_offset, 0);
11745 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11746 Create the set of symtabs used by this TU, or if this TU is sharing
11747 symtabs with another TU and the symtabs have already been created
11748 then restore those symtabs in the line header.
11749 We don't need the pc/line-number mapping for type units. */
11752 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11754 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11755 struct type_unit_group *tu_group;
11757 struct attribute *attr;
11759 struct signatured_type *sig_type;
11761 gdb_assert (per_cu->is_debug_types);
11762 sig_type = (struct signatured_type *) per_cu;
11764 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11766 /* If we're using .gdb_index (includes -readnow) then
11767 per_cu->type_unit_group may not have been set up yet. */
11768 if (sig_type->type_unit_group == NULL)
11769 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11770 tu_group = sig_type->type_unit_group;
11772 /* If we've already processed this stmt_list there's no real need to
11773 do it again, we could fake it and just recreate the part we need
11774 (file name,index -> symtab mapping). If data shows this optimization
11775 is useful we can do it then. */
11776 first_time = tu_group->compunit_symtab == NULL;
11778 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11783 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11784 lh = dwarf_decode_line_header (line_offset, cu);
11789 dwarf2_start_symtab (cu, "", NULL, 0);
11792 gdb_assert (tu_group->symtabs == NULL);
11793 restart_symtab (tu_group->compunit_symtab, "", 0);
11798 cu->line_header = lh.release ();
11799 cu->line_header_die_owner = die;
11803 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11805 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11806 still initializing it, and our caller (a few levels up)
11807 process_full_type_unit still needs to know if this is the first
11810 tu_group->num_symtabs = cu->line_header->file_names.size ();
11811 tu_group->symtabs = XNEWVEC (struct symtab *,
11812 cu->line_header->file_names.size ());
11814 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11816 file_entry &fe = cu->line_header->file_names[i];
11818 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11820 if (current_subfile->symtab == NULL)
11822 /* NOTE: start_subfile will recognize when it's been
11823 passed a file it has already seen. So we can't
11824 assume there's a simple mapping from
11825 cu->line_header->file_names to subfiles, plus
11826 cu->line_header->file_names may contain dups. */
11827 current_subfile->symtab
11828 = allocate_symtab (cust, current_subfile->name);
11831 fe.symtab = current_subfile->symtab;
11832 tu_group->symtabs[i] = fe.symtab;
11837 restart_symtab (tu_group->compunit_symtab, "", 0);
11839 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11841 file_entry &fe = cu->line_header->file_names[i];
11843 fe.symtab = tu_group->symtabs[i];
11847 /* The main symtab is allocated last. Type units don't have DW_AT_name
11848 so they don't have a "real" (so to speak) symtab anyway.
11849 There is later code that will assign the main symtab to all symbols
11850 that don't have one. We need to handle the case of a symbol with a
11851 missing symtab (DW_AT_decl_file) anyway. */
11854 /* Process DW_TAG_type_unit.
11855 For TUs we want to skip the first top level sibling if it's not the
11856 actual type being defined by this TU. In this case the first top
11857 level sibling is there to provide context only. */
11860 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11862 struct die_info *child_die;
11864 prepare_one_comp_unit (cu, die, language_minimal);
11866 /* Initialize (or reinitialize) the machinery for building symtabs.
11867 We do this before processing child DIEs, so that the line header table
11868 is available for DW_AT_decl_file. */
11869 setup_type_unit_groups (die, cu);
11871 if (die->child != NULL)
11873 child_die = die->child;
11874 while (child_die && child_die->tag)
11876 process_die (child_die, cu);
11877 child_die = sibling_die (child_die);
11884 http://gcc.gnu.org/wiki/DebugFission
11885 http://gcc.gnu.org/wiki/DebugFissionDWP
11887 To simplify handling of both DWO files ("object" files with the DWARF info)
11888 and DWP files (a file with the DWOs packaged up into one file), we treat
11889 DWP files as having a collection of virtual DWO files. */
11892 hash_dwo_file (const void *item)
11894 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11897 hash = htab_hash_string (dwo_file->dwo_name);
11898 if (dwo_file->comp_dir != NULL)
11899 hash += htab_hash_string (dwo_file->comp_dir);
11904 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11906 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11907 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11909 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11911 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11912 return lhs->comp_dir == rhs->comp_dir;
11913 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11916 /* Allocate a hash table for DWO files. */
11919 allocate_dwo_file_hash_table (struct objfile *objfile)
11921 return htab_create_alloc_ex (41,
11925 &objfile->objfile_obstack,
11926 hashtab_obstack_allocate,
11927 dummy_obstack_deallocate);
11930 /* Lookup DWO file DWO_NAME. */
11933 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11934 const char *dwo_name,
11935 const char *comp_dir)
11937 struct dwo_file find_entry;
11940 if (dwarf2_per_objfile->dwo_files == NULL)
11941 dwarf2_per_objfile->dwo_files
11942 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11944 memset (&find_entry, 0, sizeof (find_entry));
11945 find_entry.dwo_name = dwo_name;
11946 find_entry.comp_dir = comp_dir;
11947 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11953 hash_dwo_unit (const void *item)
11955 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11957 /* This drops the top 32 bits of the id, but is ok for a hash. */
11958 return dwo_unit->signature;
11962 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11964 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11965 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11967 /* The signature is assumed to be unique within the DWO file.
11968 So while object file CU dwo_id's always have the value zero,
11969 that's OK, assuming each object file DWO file has only one CU,
11970 and that's the rule for now. */
11971 return lhs->signature == rhs->signature;
11974 /* Allocate a hash table for DWO CUs,TUs.
11975 There is one of these tables for each of CUs,TUs for each DWO file. */
11978 allocate_dwo_unit_table (struct objfile *objfile)
11980 /* Start out with a pretty small number.
11981 Generally DWO files contain only one CU and maybe some TUs. */
11982 return htab_create_alloc_ex (3,
11986 &objfile->objfile_obstack,
11987 hashtab_obstack_allocate,
11988 dummy_obstack_deallocate);
11991 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11993 struct create_dwo_cu_data
11995 struct dwo_file *dwo_file;
11996 struct dwo_unit dwo_unit;
11999 /* die_reader_func for create_dwo_cu. */
12002 create_dwo_cu_reader (const struct die_reader_specs *reader,
12003 const gdb_byte *info_ptr,
12004 struct die_info *comp_unit_die,
12008 struct dwarf2_cu *cu = reader->cu;
12009 sect_offset sect_off = cu->per_cu->sect_off;
12010 struct dwarf2_section_info *section = cu->per_cu->section;
12011 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12012 struct dwo_file *dwo_file = data->dwo_file;
12013 struct dwo_unit *dwo_unit = &data->dwo_unit;
12014 struct attribute *attr;
12016 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12019 complaint (&symfile_complaints,
12020 _("Dwarf Error: debug entry at offset 0x%x is missing"
12021 " its dwo_id [in module %s]"),
12022 to_underlying (sect_off), dwo_file->dwo_name);
12026 dwo_unit->dwo_file = dwo_file;
12027 dwo_unit->signature = DW_UNSND (attr);
12028 dwo_unit->section = section;
12029 dwo_unit->sect_off = sect_off;
12030 dwo_unit->length = cu->per_cu->length;
12032 if (dwarf_read_debug)
12033 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
12034 to_underlying (sect_off),
12035 hex_string (dwo_unit->signature));
12038 /* Create the dwo_units for the CUs in a DWO_FILE.
12039 Note: This function processes DWO files only, not DWP files. */
12042 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12043 struct dwo_file &dwo_file, dwarf2_section_info §ion,
12046 struct objfile *objfile = dwarf2_per_objfile->objfile;
12047 const gdb_byte *info_ptr, *end_ptr;
12049 dwarf2_read_section (objfile, §ion);
12050 info_ptr = section.buffer;
12052 if (info_ptr == NULL)
12055 if (dwarf_read_debug)
12057 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12058 get_section_name (§ion),
12059 get_section_file_name (§ion));
12062 end_ptr = info_ptr + section.size;
12063 while (info_ptr < end_ptr)
12065 struct dwarf2_per_cu_data per_cu;
12066 struct create_dwo_cu_data create_dwo_cu_data;
12067 struct dwo_unit *dwo_unit;
12069 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12071 memset (&create_dwo_cu_data.dwo_unit, 0,
12072 sizeof (create_dwo_cu_data.dwo_unit));
12073 memset (&per_cu, 0, sizeof (per_cu));
12074 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12075 per_cu.is_debug_types = 0;
12076 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12077 per_cu.section = §ion;
12078 create_dwo_cu_data.dwo_file = &dwo_file;
12080 init_cutu_and_read_dies_no_follow (
12081 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12082 info_ptr += per_cu.length;
12084 // If the unit could not be parsed, skip it.
12085 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12088 if (cus_htab == NULL)
12089 cus_htab = allocate_dwo_unit_table (objfile);
12091 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12092 *dwo_unit = create_dwo_cu_data.dwo_unit;
12093 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12094 gdb_assert (slot != NULL);
12097 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12098 sect_offset dup_sect_off = dup_cu->sect_off;
12100 complaint (&symfile_complaints,
12101 _("debug cu entry at offset 0x%x is duplicate to"
12102 " the entry at offset 0x%x, signature %s"),
12103 to_underlying (sect_off), to_underlying (dup_sect_off),
12104 hex_string (dwo_unit->signature));
12106 *slot = (void *)dwo_unit;
12110 /* DWP file .debug_{cu,tu}_index section format:
12111 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12115 Both index sections have the same format, and serve to map a 64-bit
12116 signature to a set of section numbers. Each section begins with a header,
12117 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12118 indexes, and a pool of 32-bit section numbers. The index sections will be
12119 aligned at 8-byte boundaries in the file.
12121 The index section header consists of:
12123 V, 32 bit version number
12125 N, 32 bit number of compilation units or type units in the index
12126 M, 32 bit number of slots in the hash table
12128 Numbers are recorded using the byte order of the application binary.
12130 The hash table begins at offset 16 in the section, and consists of an array
12131 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12132 order of the application binary). Unused slots in the hash table are 0.
12133 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12135 The parallel table begins immediately after the hash table
12136 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12137 array of 32-bit indexes (using the byte order of the application binary),
12138 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12139 table contains a 32-bit index into the pool of section numbers. For unused
12140 hash table slots, the corresponding entry in the parallel table will be 0.
12142 The pool of section numbers begins immediately following the hash table
12143 (at offset 16 + 12 * M from the beginning of the section). The pool of
12144 section numbers consists of an array of 32-bit words (using the byte order
12145 of the application binary). Each item in the array is indexed starting
12146 from 0. The hash table entry provides the index of the first section
12147 number in the set. Additional section numbers in the set follow, and the
12148 set is terminated by a 0 entry (section number 0 is not used in ELF).
12150 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12151 section must be the first entry in the set, and the .debug_abbrev.dwo must
12152 be the second entry. Other members of the set may follow in any order.
12158 DWP Version 2 combines all the .debug_info, etc. sections into one,
12159 and the entries in the index tables are now offsets into these sections.
12160 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12163 Index Section Contents:
12165 Hash Table of Signatures dwp_hash_table.hash_table
12166 Parallel Table of Indices dwp_hash_table.unit_table
12167 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12168 Table of Section Sizes dwp_hash_table.v2.sizes
12170 The index section header consists of:
12172 V, 32 bit version number
12173 L, 32 bit number of columns in the table of section offsets
12174 N, 32 bit number of compilation units or type units in the index
12175 M, 32 bit number of slots in the hash table
12177 Numbers are recorded using the byte order of the application binary.
12179 The hash table has the same format as version 1.
12180 The parallel table of indices has the same format as version 1,
12181 except that the entries are origin-1 indices into the table of sections
12182 offsets and the table of section sizes.
12184 The table of offsets begins immediately following the parallel table
12185 (at offset 16 + 12 * M from the beginning of the section). The table is
12186 a two-dimensional array of 32-bit words (using the byte order of the
12187 application binary), with L columns and N+1 rows, in row-major order.
12188 Each row in the array is indexed starting from 0. The first row provides
12189 a key to the remaining rows: each column in this row provides an identifier
12190 for a debug section, and the offsets in the same column of subsequent rows
12191 refer to that section. The section identifiers are:
12193 DW_SECT_INFO 1 .debug_info.dwo
12194 DW_SECT_TYPES 2 .debug_types.dwo
12195 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12196 DW_SECT_LINE 4 .debug_line.dwo
12197 DW_SECT_LOC 5 .debug_loc.dwo
12198 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12199 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12200 DW_SECT_MACRO 8 .debug_macro.dwo
12202 The offsets provided by the CU and TU index sections are the base offsets
12203 for the contributions made by each CU or TU to the corresponding section
12204 in the package file. Each CU and TU header contains an abbrev_offset
12205 field, used to find the abbreviations table for that CU or TU within the
12206 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12207 be interpreted as relative to the base offset given in the index section.
12208 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12209 should be interpreted as relative to the base offset for .debug_line.dwo,
12210 and offsets into other debug sections obtained from DWARF attributes should
12211 also be interpreted as relative to the corresponding base offset.
12213 The table of sizes begins immediately following the table of offsets.
12214 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12215 with L columns and N rows, in row-major order. Each row in the array is
12216 indexed starting from 1 (row 0 is shared by the two tables).
12220 Hash table lookup is handled the same in version 1 and 2:
12222 We assume that N and M will not exceed 2^32 - 1.
12223 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12225 Given a 64-bit compilation unit signature or a type signature S, an entry
12226 in the hash table is located as follows:
12228 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12229 the low-order k bits all set to 1.
12231 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12233 3) If the hash table entry at index H matches the signature, use that
12234 entry. If the hash table entry at index H is unused (all zeroes),
12235 terminate the search: the signature is not present in the table.
12237 4) Let H = (H + H') modulo M. Repeat at Step 3.
12239 Because M > N and H' and M are relatively prime, the search is guaranteed
12240 to stop at an unused slot or find the match. */
12242 /* Create a hash table to map DWO IDs to their CU/TU entry in
12243 .debug_{info,types}.dwo in DWP_FILE.
12244 Returns NULL if there isn't one.
12245 Note: This function processes DWP files only, not DWO files. */
12247 static struct dwp_hash_table *
12248 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12249 struct dwp_file *dwp_file, int is_debug_types)
12251 struct objfile *objfile = dwarf2_per_objfile->objfile;
12252 bfd *dbfd = dwp_file->dbfd;
12253 const gdb_byte *index_ptr, *index_end;
12254 struct dwarf2_section_info *index;
12255 uint32_t version, nr_columns, nr_units, nr_slots;
12256 struct dwp_hash_table *htab;
12258 if (is_debug_types)
12259 index = &dwp_file->sections.tu_index;
12261 index = &dwp_file->sections.cu_index;
12263 if (dwarf2_section_empty_p (index))
12265 dwarf2_read_section (objfile, index);
12267 index_ptr = index->buffer;
12268 index_end = index_ptr + index->size;
12270 version = read_4_bytes (dbfd, index_ptr);
12273 nr_columns = read_4_bytes (dbfd, index_ptr);
12277 nr_units = read_4_bytes (dbfd, index_ptr);
12279 nr_slots = read_4_bytes (dbfd, index_ptr);
12282 if (version != 1 && version != 2)
12284 error (_("Dwarf Error: unsupported DWP file version (%s)"
12285 " [in module %s]"),
12286 pulongest (version), dwp_file->name);
12288 if (nr_slots != (nr_slots & -nr_slots))
12290 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12291 " is not power of 2 [in module %s]"),
12292 pulongest (nr_slots), dwp_file->name);
12295 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12296 htab->version = version;
12297 htab->nr_columns = nr_columns;
12298 htab->nr_units = nr_units;
12299 htab->nr_slots = nr_slots;
12300 htab->hash_table = index_ptr;
12301 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12303 /* Exit early if the table is empty. */
12304 if (nr_slots == 0 || nr_units == 0
12305 || (version == 2 && nr_columns == 0))
12307 /* All must be zero. */
12308 if (nr_slots != 0 || nr_units != 0
12309 || (version == 2 && nr_columns != 0))
12311 complaint (&symfile_complaints,
12312 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12313 " all zero [in modules %s]"),
12321 htab->section_pool.v1.indices =
12322 htab->unit_table + sizeof (uint32_t) * nr_slots;
12323 /* It's harder to decide whether the section is too small in v1.
12324 V1 is deprecated anyway so we punt. */
12328 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12329 int *ids = htab->section_pool.v2.section_ids;
12330 /* Reverse map for error checking. */
12331 int ids_seen[DW_SECT_MAX + 1];
12334 if (nr_columns < 2)
12336 error (_("Dwarf Error: bad DWP hash table, too few columns"
12337 " in section table [in module %s]"),
12340 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12342 error (_("Dwarf Error: bad DWP hash table, too many columns"
12343 " in section table [in module %s]"),
12346 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12347 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12348 for (i = 0; i < nr_columns; ++i)
12350 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12352 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12354 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12355 " in section table [in module %s]"),
12356 id, dwp_file->name);
12358 if (ids_seen[id] != -1)
12360 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12361 " id %d in section table [in module %s]"),
12362 id, dwp_file->name);
12367 /* Must have exactly one info or types section. */
12368 if (((ids_seen[DW_SECT_INFO] != -1)
12369 + (ids_seen[DW_SECT_TYPES] != -1))
12372 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12373 " DWO info/types section [in module %s]"),
12376 /* Must have an abbrev section. */
12377 if (ids_seen[DW_SECT_ABBREV] == -1)
12379 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12380 " section [in module %s]"),
12383 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12384 htab->section_pool.v2.sizes =
12385 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12386 * nr_units * nr_columns);
12387 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12388 * nr_units * nr_columns))
12391 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12392 " [in module %s]"),
12400 /* Update SECTIONS with the data from SECTP.
12402 This function is like the other "locate" section routines that are
12403 passed to bfd_map_over_sections, but in this context the sections to
12404 read comes from the DWP V1 hash table, not the full ELF section table.
12406 The result is non-zero for success, or zero if an error was found. */
12409 locate_v1_virtual_dwo_sections (asection *sectp,
12410 struct virtual_v1_dwo_sections *sections)
12412 const struct dwop_section_names *names = &dwop_section_names;
12414 if (section_is_p (sectp->name, &names->abbrev_dwo))
12416 /* There can be only one. */
12417 if (sections->abbrev.s.section != NULL)
12419 sections->abbrev.s.section = sectp;
12420 sections->abbrev.size = bfd_get_section_size (sectp);
12422 else if (section_is_p (sectp->name, &names->info_dwo)
12423 || section_is_p (sectp->name, &names->types_dwo))
12425 /* There can be only one. */
12426 if (sections->info_or_types.s.section != NULL)
12428 sections->info_or_types.s.section = sectp;
12429 sections->info_or_types.size = bfd_get_section_size (sectp);
12431 else if (section_is_p (sectp->name, &names->line_dwo))
12433 /* There can be only one. */
12434 if (sections->line.s.section != NULL)
12436 sections->line.s.section = sectp;
12437 sections->line.size = bfd_get_section_size (sectp);
12439 else if (section_is_p (sectp->name, &names->loc_dwo))
12441 /* There can be only one. */
12442 if (sections->loc.s.section != NULL)
12444 sections->loc.s.section = sectp;
12445 sections->loc.size = bfd_get_section_size (sectp);
12447 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12449 /* There can be only one. */
12450 if (sections->macinfo.s.section != NULL)
12452 sections->macinfo.s.section = sectp;
12453 sections->macinfo.size = bfd_get_section_size (sectp);
12455 else if (section_is_p (sectp->name, &names->macro_dwo))
12457 /* There can be only one. */
12458 if (sections->macro.s.section != NULL)
12460 sections->macro.s.section = sectp;
12461 sections->macro.size = bfd_get_section_size (sectp);
12463 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12465 /* There can be only one. */
12466 if (sections->str_offsets.s.section != NULL)
12468 sections->str_offsets.s.section = sectp;
12469 sections->str_offsets.size = bfd_get_section_size (sectp);
12473 /* No other kind of section is valid. */
12480 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12481 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12482 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12483 This is for DWP version 1 files. */
12485 static struct dwo_unit *
12486 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12487 struct dwp_file *dwp_file,
12488 uint32_t unit_index,
12489 const char *comp_dir,
12490 ULONGEST signature, int is_debug_types)
12492 struct objfile *objfile = dwarf2_per_objfile->objfile;
12493 const struct dwp_hash_table *dwp_htab =
12494 is_debug_types ? dwp_file->tus : dwp_file->cus;
12495 bfd *dbfd = dwp_file->dbfd;
12496 const char *kind = is_debug_types ? "TU" : "CU";
12497 struct dwo_file *dwo_file;
12498 struct dwo_unit *dwo_unit;
12499 struct virtual_v1_dwo_sections sections;
12500 void **dwo_file_slot;
12503 gdb_assert (dwp_file->version == 1);
12505 if (dwarf_read_debug)
12507 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12509 pulongest (unit_index), hex_string (signature),
12513 /* Fetch the sections of this DWO unit.
12514 Put a limit on the number of sections we look for so that bad data
12515 doesn't cause us to loop forever. */
12517 #define MAX_NR_V1_DWO_SECTIONS \
12518 (1 /* .debug_info or .debug_types */ \
12519 + 1 /* .debug_abbrev */ \
12520 + 1 /* .debug_line */ \
12521 + 1 /* .debug_loc */ \
12522 + 1 /* .debug_str_offsets */ \
12523 + 1 /* .debug_macro or .debug_macinfo */ \
12524 + 1 /* trailing zero */)
12526 memset (§ions, 0, sizeof (sections));
12528 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12531 uint32_t section_nr =
12532 read_4_bytes (dbfd,
12533 dwp_htab->section_pool.v1.indices
12534 + (unit_index + i) * sizeof (uint32_t));
12536 if (section_nr == 0)
12538 if (section_nr >= dwp_file->num_sections)
12540 error (_("Dwarf Error: bad DWP hash table, section number too large"
12541 " [in module %s]"),
12545 sectp = dwp_file->elf_sections[section_nr];
12546 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12548 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12549 " [in module %s]"),
12555 || dwarf2_section_empty_p (§ions.info_or_types)
12556 || dwarf2_section_empty_p (§ions.abbrev))
12558 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12559 " [in module %s]"),
12562 if (i == MAX_NR_V1_DWO_SECTIONS)
12564 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12565 " [in module %s]"),
12569 /* It's easier for the rest of the code if we fake a struct dwo_file and
12570 have dwo_unit "live" in that. At least for now.
12572 The DWP file can be made up of a random collection of CUs and TUs.
12573 However, for each CU + set of TUs that came from the same original DWO
12574 file, we can combine them back into a virtual DWO file to save space
12575 (fewer struct dwo_file objects to allocate). Remember that for really
12576 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12578 std::string virtual_dwo_name =
12579 string_printf ("virtual-dwo/%d-%d-%d-%d",
12580 get_section_id (§ions.abbrev),
12581 get_section_id (§ions.line),
12582 get_section_id (§ions.loc),
12583 get_section_id (§ions.str_offsets));
12584 /* Can we use an existing virtual DWO file? */
12585 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12586 virtual_dwo_name.c_str (),
12588 /* Create one if necessary. */
12589 if (*dwo_file_slot == NULL)
12591 if (dwarf_read_debug)
12593 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12594 virtual_dwo_name.c_str ());
12596 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12598 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12599 virtual_dwo_name.c_str (),
12600 virtual_dwo_name.size ());
12601 dwo_file->comp_dir = comp_dir;
12602 dwo_file->sections.abbrev = sections.abbrev;
12603 dwo_file->sections.line = sections.line;
12604 dwo_file->sections.loc = sections.loc;
12605 dwo_file->sections.macinfo = sections.macinfo;
12606 dwo_file->sections.macro = sections.macro;
12607 dwo_file->sections.str_offsets = sections.str_offsets;
12608 /* The "str" section is global to the entire DWP file. */
12609 dwo_file->sections.str = dwp_file->sections.str;
12610 /* The info or types section is assigned below to dwo_unit,
12611 there's no need to record it in dwo_file.
12612 Also, we can't simply record type sections in dwo_file because
12613 we record a pointer into the vector in dwo_unit. As we collect more
12614 types we'll grow the vector and eventually have to reallocate space
12615 for it, invalidating all copies of pointers into the previous
12617 *dwo_file_slot = dwo_file;
12621 if (dwarf_read_debug)
12623 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12624 virtual_dwo_name.c_str ());
12626 dwo_file = (struct dwo_file *) *dwo_file_slot;
12629 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12630 dwo_unit->dwo_file = dwo_file;
12631 dwo_unit->signature = signature;
12632 dwo_unit->section =
12633 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12634 *dwo_unit->section = sections.info_or_types;
12635 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12640 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12641 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12642 piece within that section used by a TU/CU, return a virtual section
12643 of just that piece. */
12645 static struct dwarf2_section_info
12646 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12647 struct dwarf2_section_info *section,
12648 bfd_size_type offset, bfd_size_type size)
12650 struct dwarf2_section_info result;
12653 gdb_assert (section != NULL);
12654 gdb_assert (!section->is_virtual);
12656 memset (&result, 0, sizeof (result));
12657 result.s.containing_section = section;
12658 result.is_virtual = 1;
12663 sectp = get_section_bfd_section (section);
12665 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12666 bounds of the real section. This is a pretty-rare event, so just
12667 flag an error (easier) instead of a warning and trying to cope. */
12669 || offset + size > bfd_get_section_size (sectp))
12671 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12672 " in section %s [in module %s]"),
12673 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12674 objfile_name (dwarf2_per_objfile->objfile));
12677 result.virtual_offset = offset;
12678 result.size = size;
12682 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12683 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12684 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12685 This is for DWP version 2 files. */
12687 static struct dwo_unit *
12688 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12689 struct dwp_file *dwp_file,
12690 uint32_t unit_index,
12691 const char *comp_dir,
12692 ULONGEST signature, int is_debug_types)
12694 struct objfile *objfile = dwarf2_per_objfile->objfile;
12695 const struct dwp_hash_table *dwp_htab =
12696 is_debug_types ? dwp_file->tus : dwp_file->cus;
12697 bfd *dbfd = dwp_file->dbfd;
12698 const char *kind = is_debug_types ? "TU" : "CU";
12699 struct dwo_file *dwo_file;
12700 struct dwo_unit *dwo_unit;
12701 struct virtual_v2_dwo_sections sections;
12702 void **dwo_file_slot;
12705 gdb_assert (dwp_file->version == 2);
12707 if (dwarf_read_debug)
12709 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12711 pulongest (unit_index), hex_string (signature),
12715 /* Fetch the section offsets of this DWO unit. */
12717 memset (§ions, 0, sizeof (sections));
12719 for (i = 0; i < dwp_htab->nr_columns; ++i)
12721 uint32_t offset = read_4_bytes (dbfd,
12722 dwp_htab->section_pool.v2.offsets
12723 + (((unit_index - 1) * dwp_htab->nr_columns
12725 * sizeof (uint32_t)));
12726 uint32_t size = read_4_bytes (dbfd,
12727 dwp_htab->section_pool.v2.sizes
12728 + (((unit_index - 1) * dwp_htab->nr_columns
12730 * sizeof (uint32_t)));
12732 switch (dwp_htab->section_pool.v2.section_ids[i])
12735 case DW_SECT_TYPES:
12736 sections.info_or_types_offset = offset;
12737 sections.info_or_types_size = size;
12739 case DW_SECT_ABBREV:
12740 sections.abbrev_offset = offset;
12741 sections.abbrev_size = size;
12744 sections.line_offset = offset;
12745 sections.line_size = size;
12748 sections.loc_offset = offset;
12749 sections.loc_size = size;
12751 case DW_SECT_STR_OFFSETS:
12752 sections.str_offsets_offset = offset;
12753 sections.str_offsets_size = size;
12755 case DW_SECT_MACINFO:
12756 sections.macinfo_offset = offset;
12757 sections.macinfo_size = size;
12759 case DW_SECT_MACRO:
12760 sections.macro_offset = offset;
12761 sections.macro_size = size;
12766 /* It's easier for the rest of the code if we fake a struct dwo_file and
12767 have dwo_unit "live" in that. At least for now.
12769 The DWP file can be made up of a random collection of CUs and TUs.
12770 However, for each CU + set of TUs that came from the same original DWO
12771 file, we can combine them back into a virtual DWO file to save space
12772 (fewer struct dwo_file objects to allocate). Remember that for really
12773 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12775 std::string virtual_dwo_name =
12776 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12777 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12778 (long) (sections.line_size ? sections.line_offset : 0),
12779 (long) (sections.loc_size ? sections.loc_offset : 0),
12780 (long) (sections.str_offsets_size
12781 ? sections.str_offsets_offset : 0));
12782 /* Can we use an existing virtual DWO file? */
12783 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12784 virtual_dwo_name.c_str (),
12786 /* Create one if necessary. */
12787 if (*dwo_file_slot == NULL)
12789 if (dwarf_read_debug)
12791 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12792 virtual_dwo_name.c_str ());
12794 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12796 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12797 virtual_dwo_name.c_str (),
12798 virtual_dwo_name.size ());
12799 dwo_file->comp_dir = comp_dir;
12800 dwo_file->sections.abbrev =
12801 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12802 sections.abbrev_offset, sections.abbrev_size);
12803 dwo_file->sections.line =
12804 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12805 sections.line_offset, sections.line_size);
12806 dwo_file->sections.loc =
12807 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12808 sections.loc_offset, sections.loc_size);
12809 dwo_file->sections.macinfo =
12810 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12811 sections.macinfo_offset, sections.macinfo_size);
12812 dwo_file->sections.macro =
12813 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12814 sections.macro_offset, sections.macro_size);
12815 dwo_file->sections.str_offsets =
12816 create_dwp_v2_section (dwarf2_per_objfile,
12817 &dwp_file->sections.str_offsets,
12818 sections.str_offsets_offset,
12819 sections.str_offsets_size);
12820 /* The "str" section is global to the entire DWP file. */
12821 dwo_file->sections.str = dwp_file->sections.str;
12822 /* The info or types section is assigned below to dwo_unit,
12823 there's no need to record it in dwo_file.
12824 Also, we can't simply record type sections in dwo_file because
12825 we record a pointer into the vector in dwo_unit. As we collect more
12826 types we'll grow the vector and eventually have to reallocate space
12827 for it, invalidating all copies of pointers into the previous
12829 *dwo_file_slot = dwo_file;
12833 if (dwarf_read_debug)
12835 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12836 virtual_dwo_name.c_str ());
12838 dwo_file = (struct dwo_file *) *dwo_file_slot;
12841 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12842 dwo_unit->dwo_file = dwo_file;
12843 dwo_unit->signature = signature;
12844 dwo_unit->section =
12845 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12846 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12848 ? &dwp_file->sections.types
12849 : &dwp_file->sections.info,
12850 sections.info_or_types_offset,
12851 sections.info_or_types_size);
12852 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12857 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12858 Returns NULL if the signature isn't found. */
12860 static struct dwo_unit *
12861 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12862 struct dwp_file *dwp_file, const char *comp_dir,
12863 ULONGEST signature, int is_debug_types)
12865 const struct dwp_hash_table *dwp_htab =
12866 is_debug_types ? dwp_file->tus : dwp_file->cus;
12867 bfd *dbfd = dwp_file->dbfd;
12868 uint32_t mask = dwp_htab->nr_slots - 1;
12869 uint32_t hash = signature & mask;
12870 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12873 struct dwo_unit find_dwo_cu;
12875 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12876 find_dwo_cu.signature = signature;
12877 slot = htab_find_slot (is_debug_types
12878 ? dwp_file->loaded_tus
12879 : dwp_file->loaded_cus,
12880 &find_dwo_cu, INSERT);
12883 return (struct dwo_unit *) *slot;
12885 /* Use a for loop so that we don't loop forever on bad debug info. */
12886 for (i = 0; i < dwp_htab->nr_slots; ++i)
12888 ULONGEST signature_in_table;
12890 signature_in_table =
12891 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12892 if (signature_in_table == signature)
12894 uint32_t unit_index =
12895 read_4_bytes (dbfd,
12896 dwp_htab->unit_table + hash * sizeof (uint32_t));
12898 if (dwp_file->version == 1)
12900 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12901 dwp_file, unit_index,
12902 comp_dir, signature,
12907 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12908 dwp_file, unit_index,
12909 comp_dir, signature,
12912 return (struct dwo_unit *) *slot;
12914 if (signature_in_table == 0)
12916 hash = (hash + hash2) & mask;
12919 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12920 " [in module %s]"),
12924 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12925 Open the file specified by FILE_NAME and hand it off to BFD for
12926 preliminary analysis. Return a newly initialized bfd *, which
12927 includes a canonicalized copy of FILE_NAME.
12928 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12929 SEARCH_CWD is true if the current directory is to be searched.
12930 It will be searched before debug-file-directory.
12931 If successful, the file is added to the bfd include table of the
12932 objfile's bfd (see gdb_bfd_record_inclusion).
12933 If unable to find/open the file, return NULL.
12934 NOTE: This function is derived from symfile_bfd_open. */
12936 static gdb_bfd_ref_ptr
12937 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12938 const char *file_name, int is_dwp, int search_cwd)
12941 char *absolute_name;
12942 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12943 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12944 to debug_file_directory. */
12946 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12950 if (*debug_file_directory != '\0')
12951 search_path = concat (".", dirname_separator_string,
12952 debug_file_directory, (char *) NULL);
12954 search_path = xstrdup (".");
12957 search_path = xstrdup (debug_file_directory);
12959 flags = OPF_RETURN_REALPATH;
12961 flags |= OPF_SEARCH_IN_PATH;
12962 desc = openp (search_path, flags, file_name,
12963 O_RDONLY | O_BINARY, &absolute_name);
12964 xfree (search_path);
12968 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12969 xfree (absolute_name);
12970 if (sym_bfd == NULL)
12972 bfd_set_cacheable (sym_bfd.get (), 1);
12974 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12977 /* Success. Record the bfd as having been included by the objfile's bfd.
12978 This is important because things like demangled_names_hash lives in the
12979 objfile's per_bfd space and may have references to things like symbol
12980 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12981 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12986 /* Try to open DWO file FILE_NAME.
12987 COMP_DIR is the DW_AT_comp_dir attribute.
12988 The result is the bfd handle of the file.
12989 If there is a problem finding or opening the file, return NULL.
12990 Upon success, the canonicalized path of the file is stored in the bfd,
12991 same as symfile_bfd_open. */
12993 static gdb_bfd_ref_ptr
12994 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12995 const char *file_name, const char *comp_dir)
12997 if (IS_ABSOLUTE_PATH (file_name))
12998 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12999 0 /*is_dwp*/, 0 /*search_cwd*/);
13001 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13003 if (comp_dir != NULL)
13005 char *path_to_try = concat (comp_dir, SLASH_STRING,
13006 file_name, (char *) NULL);
13008 /* NOTE: If comp_dir is a relative path, this will also try the
13009 search path, which seems useful. */
13010 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13013 1 /*search_cwd*/));
13014 xfree (path_to_try);
13019 /* That didn't work, try debug-file-directory, which, despite its name,
13020 is a list of paths. */
13022 if (*debug_file_directory == '\0')
13025 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13026 0 /*is_dwp*/, 1 /*search_cwd*/);
13029 /* This function is mapped across the sections and remembers the offset and
13030 size of each of the DWO debugging sections we are interested in. */
13033 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13035 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13036 const struct dwop_section_names *names = &dwop_section_names;
13038 if (section_is_p (sectp->name, &names->abbrev_dwo))
13040 dwo_sections->abbrev.s.section = sectp;
13041 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13043 else if (section_is_p (sectp->name, &names->info_dwo))
13045 dwo_sections->info.s.section = sectp;
13046 dwo_sections->info.size = bfd_get_section_size (sectp);
13048 else if (section_is_p (sectp->name, &names->line_dwo))
13050 dwo_sections->line.s.section = sectp;
13051 dwo_sections->line.size = bfd_get_section_size (sectp);
13053 else if (section_is_p (sectp->name, &names->loc_dwo))
13055 dwo_sections->loc.s.section = sectp;
13056 dwo_sections->loc.size = bfd_get_section_size (sectp);
13058 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13060 dwo_sections->macinfo.s.section = sectp;
13061 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13063 else if (section_is_p (sectp->name, &names->macro_dwo))
13065 dwo_sections->macro.s.section = sectp;
13066 dwo_sections->macro.size = bfd_get_section_size (sectp);
13068 else if (section_is_p (sectp->name, &names->str_dwo))
13070 dwo_sections->str.s.section = sectp;
13071 dwo_sections->str.size = bfd_get_section_size (sectp);
13073 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13075 dwo_sections->str_offsets.s.section = sectp;
13076 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13078 else if (section_is_p (sectp->name, &names->types_dwo))
13080 struct dwarf2_section_info type_section;
13082 memset (&type_section, 0, sizeof (type_section));
13083 type_section.s.section = sectp;
13084 type_section.size = bfd_get_section_size (sectp);
13085 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13090 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13091 by PER_CU. This is for the non-DWP case.
13092 The result is NULL if DWO_NAME can't be found. */
13094 static struct dwo_file *
13095 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13096 const char *dwo_name, const char *comp_dir)
13098 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13099 struct objfile *objfile = dwarf2_per_objfile->objfile;
13100 struct dwo_file *dwo_file;
13101 struct cleanup *cleanups;
13103 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13106 if (dwarf_read_debug)
13107 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13110 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13111 dwo_file->dwo_name = dwo_name;
13112 dwo_file->comp_dir = comp_dir;
13113 dwo_file->dbfd = dbfd.release ();
13115 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13116 cleanup_data->dwo_file = dwo_file;
13117 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13119 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13121 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13122 &dwo_file->sections);
13124 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13127 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13128 dwo_file->sections.types, dwo_file->tus);
13130 discard_cleanups (cleanups);
13132 if (dwarf_read_debug)
13133 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13138 /* This function is mapped across the sections and remembers the offset and
13139 size of each of the DWP debugging sections common to version 1 and 2 that
13140 we are interested in. */
13143 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13144 void *dwp_file_ptr)
13146 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13147 const struct dwop_section_names *names = &dwop_section_names;
13148 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13150 /* Record the ELF section number for later lookup: this is what the
13151 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13152 gdb_assert (elf_section_nr < dwp_file->num_sections);
13153 dwp_file->elf_sections[elf_section_nr] = sectp;
13155 /* Look for specific sections that we need. */
13156 if (section_is_p (sectp->name, &names->str_dwo))
13158 dwp_file->sections.str.s.section = sectp;
13159 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13161 else if (section_is_p (sectp->name, &names->cu_index))
13163 dwp_file->sections.cu_index.s.section = sectp;
13164 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13166 else if (section_is_p (sectp->name, &names->tu_index))
13168 dwp_file->sections.tu_index.s.section = sectp;
13169 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13173 /* This function is mapped across the sections and remembers the offset and
13174 size of each of the DWP version 2 debugging sections that we are interested
13175 in. This is split into a separate function because we don't know if we
13176 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13179 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13181 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13182 const struct dwop_section_names *names = &dwop_section_names;
13183 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13185 /* Record the ELF section number for later lookup: this is what the
13186 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13187 gdb_assert (elf_section_nr < dwp_file->num_sections);
13188 dwp_file->elf_sections[elf_section_nr] = sectp;
13190 /* Look for specific sections that we need. */
13191 if (section_is_p (sectp->name, &names->abbrev_dwo))
13193 dwp_file->sections.abbrev.s.section = sectp;
13194 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13196 else if (section_is_p (sectp->name, &names->info_dwo))
13198 dwp_file->sections.info.s.section = sectp;
13199 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13201 else if (section_is_p (sectp->name, &names->line_dwo))
13203 dwp_file->sections.line.s.section = sectp;
13204 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13206 else if (section_is_p (sectp->name, &names->loc_dwo))
13208 dwp_file->sections.loc.s.section = sectp;
13209 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13211 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13213 dwp_file->sections.macinfo.s.section = sectp;
13214 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13216 else if (section_is_p (sectp->name, &names->macro_dwo))
13218 dwp_file->sections.macro.s.section = sectp;
13219 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13221 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13223 dwp_file->sections.str_offsets.s.section = sectp;
13224 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13226 else if (section_is_p (sectp->name, &names->types_dwo))
13228 dwp_file->sections.types.s.section = sectp;
13229 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13233 /* Hash function for dwp_file loaded CUs/TUs. */
13236 hash_dwp_loaded_cutus (const void *item)
13238 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13240 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13241 return dwo_unit->signature;
13244 /* Equality function for dwp_file loaded CUs/TUs. */
13247 eq_dwp_loaded_cutus (const void *a, const void *b)
13249 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13250 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13252 return dua->signature == dub->signature;
13255 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13258 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13260 return htab_create_alloc_ex (3,
13261 hash_dwp_loaded_cutus,
13262 eq_dwp_loaded_cutus,
13264 &objfile->objfile_obstack,
13265 hashtab_obstack_allocate,
13266 dummy_obstack_deallocate);
13269 /* Try to open DWP file FILE_NAME.
13270 The result is the bfd handle of the file.
13271 If there is a problem finding or opening the file, return NULL.
13272 Upon success, the canonicalized path of the file is stored in the bfd,
13273 same as symfile_bfd_open. */
13275 static gdb_bfd_ref_ptr
13276 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13277 const char *file_name)
13279 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13281 1 /*search_cwd*/));
13285 /* Work around upstream bug 15652.
13286 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13287 [Whether that's a "bug" is debatable, but it is getting in our way.]
13288 We have no real idea where the dwp file is, because gdb's realpath-ing
13289 of the executable's path may have discarded the needed info.
13290 [IWBN if the dwp file name was recorded in the executable, akin to
13291 .gnu_debuglink, but that doesn't exist yet.]
13292 Strip the directory from FILE_NAME and search again. */
13293 if (*debug_file_directory != '\0')
13295 /* Don't implicitly search the current directory here.
13296 If the user wants to search "." to handle this case,
13297 it must be added to debug-file-directory. */
13298 return try_open_dwop_file (dwarf2_per_objfile,
13299 lbasename (file_name), 1 /*is_dwp*/,
13306 /* Initialize the use of the DWP file for the current objfile.
13307 By convention the name of the DWP file is ${objfile}.dwp.
13308 The result is NULL if it can't be found. */
13310 static struct dwp_file *
13311 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13313 struct objfile *objfile = dwarf2_per_objfile->objfile;
13314 struct dwp_file *dwp_file;
13316 /* Try to find first .dwp for the binary file before any symbolic links
13319 /* If the objfile is a debug file, find the name of the real binary
13320 file and get the name of dwp file from there. */
13321 std::string dwp_name;
13322 if (objfile->separate_debug_objfile_backlink != NULL)
13324 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13325 const char *backlink_basename = lbasename (backlink->original_name);
13327 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13330 dwp_name = objfile->original_name;
13332 dwp_name += ".dwp";
13334 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13336 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13338 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13339 dwp_name = objfile_name (objfile);
13340 dwp_name += ".dwp";
13341 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13346 if (dwarf_read_debug)
13347 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13350 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13351 dwp_file->name = bfd_get_filename (dbfd.get ());
13352 dwp_file->dbfd = dbfd.release ();
13354 /* +1: section 0 is unused */
13355 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13356 dwp_file->elf_sections =
13357 OBSTACK_CALLOC (&objfile->objfile_obstack,
13358 dwp_file->num_sections, asection *);
13360 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13363 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13365 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13367 /* The DWP file version is stored in the hash table. Oh well. */
13368 if (dwp_file->cus && dwp_file->tus
13369 && dwp_file->cus->version != dwp_file->tus->version)
13371 /* Technically speaking, we should try to limp along, but this is
13372 pretty bizarre. We use pulongest here because that's the established
13373 portability solution (e.g, we cannot use %u for uint32_t). */
13374 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13375 " TU version %s [in DWP file %s]"),
13376 pulongest (dwp_file->cus->version),
13377 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13381 dwp_file->version = dwp_file->cus->version;
13382 else if (dwp_file->tus)
13383 dwp_file->version = dwp_file->tus->version;
13385 dwp_file->version = 2;
13387 if (dwp_file->version == 2)
13388 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13391 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13392 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13394 if (dwarf_read_debug)
13396 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13397 fprintf_unfiltered (gdb_stdlog,
13398 " %s CUs, %s TUs\n",
13399 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13400 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13406 /* Wrapper around open_and_init_dwp_file, only open it once. */
13408 static struct dwp_file *
13409 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13411 if (! dwarf2_per_objfile->dwp_checked)
13413 dwarf2_per_objfile->dwp_file
13414 = open_and_init_dwp_file (dwarf2_per_objfile);
13415 dwarf2_per_objfile->dwp_checked = 1;
13417 return dwarf2_per_objfile->dwp_file;
13420 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13421 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13422 or in the DWP file for the objfile, referenced by THIS_UNIT.
13423 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13424 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13426 This is called, for example, when wanting to read a variable with a
13427 complex location. Therefore we don't want to do file i/o for every call.
13428 Therefore we don't want to look for a DWO file on every call.
13429 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13430 then we check if we've already seen DWO_NAME, and only THEN do we check
13433 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13434 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13436 static struct dwo_unit *
13437 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13438 const char *dwo_name, const char *comp_dir,
13439 ULONGEST signature, int is_debug_types)
13441 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13442 struct objfile *objfile = dwarf2_per_objfile->objfile;
13443 const char *kind = is_debug_types ? "TU" : "CU";
13444 void **dwo_file_slot;
13445 struct dwo_file *dwo_file;
13446 struct dwp_file *dwp_file;
13448 /* First see if there's a DWP file.
13449 If we have a DWP file but didn't find the DWO inside it, don't
13450 look for the original DWO file. It makes gdb behave differently
13451 depending on whether one is debugging in the build tree. */
13453 dwp_file = get_dwp_file (dwarf2_per_objfile);
13454 if (dwp_file != NULL)
13456 const struct dwp_hash_table *dwp_htab =
13457 is_debug_types ? dwp_file->tus : dwp_file->cus;
13459 if (dwp_htab != NULL)
13461 struct dwo_unit *dwo_cutu =
13462 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13463 signature, is_debug_types);
13465 if (dwo_cutu != NULL)
13467 if (dwarf_read_debug)
13469 fprintf_unfiltered (gdb_stdlog,
13470 "Virtual DWO %s %s found: @%s\n",
13471 kind, hex_string (signature),
13472 host_address_to_string (dwo_cutu));
13480 /* No DWP file, look for the DWO file. */
13482 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13483 dwo_name, comp_dir);
13484 if (*dwo_file_slot == NULL)
13486 /* Read in the file and build a table of the CUs/TUs it contains. */
13487 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13489 /* NOTE: This will be NULL if unable to open the file. */
13490 dwo_file = (struct dwo_file *) *dwo_file_slot;
13492 if (dwo_file != NULL)
13494 struct dwo_unit *dwo_cutu = NULL;
13496 if (is_debug_types && dwo_file->tus)
13498 struct dwo_unit find_dwo_cutu;
13500 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13501 find_dwo_cutu.signature = signature;
13503 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13505 else if (!is_debug_types && dwo_file->cus)
13507 struct dwo_unit find_dwo_cutu;
13509 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13510 find_dwo_cutu.signature = signature;
13511 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13515 if (dwo_cutu != NULL)
13517 if (dwarf_read_debug)
13519 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13520 kind, dwo_name, hex_string (signature),
13521 host_address_to_string (dwo_cutu));
13528 /* We didn't find it. This could mean a dwo_id mismatch, or
13529 someone deleted the DWO/DWP file, or the search path isn't set up
13530 correctly to find the file. */
13532 if (dwarf_read_debug)
13534 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13535 kind, dwo_name, hex_string (signature));
13538 /* This is a warning and not a complaint because it can be caused by
13539 pilot error (e.g., user accidentally deleting the DWO). */
13541 /* Print the name of the DWP file if we looked there, helps the user
13542 better diagnose the problem. */
13543 std::string dwp_text;
13545 if (dwp_file != NULL)
13546 dwp_text = string_printf (" [in DWP file %s]",
13547 lbasename (dwp_file->name));
13549 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13550 " [in module %s]"),
13551 kind, dwo_name, hex_string (signature),
13553 this_unit->is_debug_types ? "TU" : "CU",
13554 to_underlying (this_unit->sect_off), objfile_name (objfile));
13559 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13560 See lookup_dwo_cutu_unit for details. */
13562 static struct dwo_unit *
13563 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13564 const char *dwo_name, const char *comp_dir,
13565 ULONGEST signature)
13567 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13570 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13571 See lookup_dwo_cutu_unit for details. */
13573 static struct dwo_unit *
13574 lookup_dwo_type_unit (struct signatured_type *this_tu,
13575 const char *dwo_name, const char *comp_dir)
13577 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13580 /* Traversal function for queue_and_load_all_dwo_tus. */
13583 queue_and_load_dwo_tu (void **slot, void *info)
13585 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13586 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13587 ULONGEST signature = dwo_unit->signature;
13588 struct signatured_type *sig_type =
13589 lookup_dwo_signatured_type (per_cu->cu, signature);
13591 if (sig_type != NULL)
13593 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13595 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13596 a real dependency of PER_CU on SIG_TYPE. That is detected later
13597 while processing PER_CU. */
13598 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13599 load_full_type_unit (sig_cu);
13600 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13606 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13607 The DWO may have the only definition of the type, though it may not be
13608 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13609 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13612 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13614 struct dwo_unit *dwo_unit;
13615 struct dwo_file *dwo_file;
13617 gdb_assert (!per_cu->is_debug_types);
13618 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13619 gdb_assert (per_cu->cu != NULL);
13621 dwo_unit = per_cu->cu->dwo_unit;
13622 gdb_assert (dwo_unit != NULL);
13624 dwo_file = dwo_unit->dwo_file;
13625 if (dwo_file->tus != NULL)
13626 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13629 /* Free all resources associated with DWO_FILE.
13630 Close the DWO file and munmap the sections.
13631 All memory should be on the objfile obstack. */
13634 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13637 /* Note: dbfd is NULL for virtual DWO files. */
13638 gdb_bfd_unref (dwo_file->dbfd);
13640 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13643 /* Wrapper for free_dwo_file for use in cleanups. */
13646 free_dwo_file_cleanup (void *arg)
13648 struct free_dwo_file_cleanup_data *data
13649 = (struct free_dwo_file_cleanup_data *) arg;
13650 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13652 free_dwo_file (data->dwo_file, objfile);
13657 /* Traversal function for free_dwo_files. */
13660 free_dwo_file_from_slot (void **slot, void *info)
13662 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13663 struct objfile *objfile = (struct objfile *) info;
13665 free_dwo_file (dwo_file, objfile);
13670 /* Free all resources associated with DWO_FILES. */
13673 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13675 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13678 /* Read in various DIEs. */
13680 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13681 Inherit only the children of the DW_AT_abstract_origin DIE not being
13682 already referenced by DW_AT_abstract_origin from the children of the
13686 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13688 struct die_info *child_die;
13689 sect_offset *offsetp;
13690 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13691 struct die_info *origin_die;
13692 /* Iterator of the ORIGIN_DIE children. */
13693 struct die_info *origin_child_die;
13694 struct attribute *attr;
13695 struct dwarf2_cu *origin_cu;
13696 struct pending **origin_previous_list_in_scope;
13698 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13702 /* Note that following die references may follow to a die in a
13706 origin_die = follow_die_ref (die, attr, &origin_cu);
13708 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13710 origin_previous_list_in_scope = origin_cu->list_in_scope;
13711 origin_cu->list_in_scope = cu->list_in_scope;
13713 if (die->tag != origin_die->tag
13714 && !(die->tag == DW_TAG_inlined_subroutine
13715 && origin_die->tag == DW_TAG_subprogram))
13716 complaint (&symfile_complaints,
13717 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13718 to_underlying (die->sect_off),
13719 to_underlying (origin_die->sect_off));
13721 std::vector<sect_offset> offsets;
13723 for (child_die = die->child;
13724 child_die && child_die->tag;
13725 child_die = sibling_die (child_die))
13727 struct die_info *child_origin_die;
13728 struct dwarf2_cu *child_origin_cu;
13730 /* We are trying to process concrete instance entries:
13731 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13732 it's not relevant to our analysis here. i.e. detecting DIEs that are
13733 present in the abstract instance but not referenced in the concrete
13735 if (child_die->tag == DW_TAG_call_site
13736 || child_die->tag == DW_TAG_GNU_call_site)
13739 /* For each CHILD_DIE, find the corresponding child of
13740 ORIGIN_DIE. If there is more than one layer of
13741 DW_AT_abstract_origin, follow them all; there shouldn't be,
13742 but GCC versions at least through 4.4 generate this (GCC PR
13744 child_origin_die = child_die;
13745 child_origin_cu = cu;
13748 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13752 child_origin_die = follow_die_ref (child_origin_die, attr,
13756 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13757 counterpart may exist. */
13758 if (child_origin_die != child_die)
13760 if (child_die->tag != child_origin_die->tag
13761 && !(child_die->tag == DW_TAG_inlined_subroutine
13762 && child_origin_die->tag == DW_TAG_subprogram))
13763 complaint (&symfile_complaints,
13764 _("Child DIE 0x%x and its abstract origin 0x%x have "
13766 to_underlying (child_die->sect_off),
13767 to_underlying (child_origin_die->sect_off));
13768 if (child_origin_die->parent != origin_die)
13769 complaint (&symfile_complaints,
13770 _("Child DIE 0x%x and its abstract origin 0x%x have "
13771 "different parents"),
13772 to_underlying (child_die->sect_off),
13773 to_underlying (child_origin_die->sect_off));
13775 offsets.push_back (child_origin_die->sect_off);
13778 std::sort (offsets.begin (), offsets.end ());
13779 sect_offset *offsets_end = offsets.data () + offsets.size ();
13780 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13781 if (offsetp[-1] == *offsetp)
13782 complaint (&symfile_complaints,
13783 _("Multiple children of DIE 0x%x refer "
13784 "to DIE 0x%x as their abstract origin"),
13785 to_underlying (die->sect_off), to_underlying (*offsetp));
13787 offsetp = offsets.data ();
13788 origin_child_die = origin_die->child;
13789 while (origin_child_die && origin_child_die->tag)
13791 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13792 while (offsetp < offsets_end
13793 && *offsetp < origin_child_die->sect_off)
13795 if (offsetp >= offsets_end
13796 || *offsetp > origin_child_die->sect_off)
13798 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13799 Check whether we're already processing ORIGIN_CHILD_DIE.
13800 This can happen with mutually referenced abstract_origins.
13802 if (!origin_child_die->in_process)
13803 process_die (origin_child_die, origin_cu);
13805 origin_child_die = sibling_die (origin_child_die);
13807 origin_cu->list_in_scope = origin_previous_list_in_scope;
13811 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13813 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13814 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13815 struct context_stack *newobj;
13818 struct die_info *child_die;
13819 struct attribute *attr, *call_line, *call_file;
13821 CORE_ADDR baseaddr;
13822 struct block *block;
13823 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13824 std::vector<struct symbol *> template_args;
13825 struct template_symbol *templ_func = NULL;
13829 /* If we do not have call site information, we can't show the
13830 caller of this inlined function. That's too confusing, so
13831 only use the scope for local variables. */
13832 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13833 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13834 if (call_line == NULL || call_file == NULL)
13836 read_lexical_block_scope (die, cu);
13841 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13843 name = dwarf2_name (die, cu);
13845 /* Ignore functions with missing or empty names. These are actually
13846 illegal according to the DWARF standard. */
13849 complaint (&symfile_complaints,
13850 _("missing name for subprogram DIE at %d"),
13851 to_underlying (die->sect_off));
13855 /* Ignore functions with missing or invalid low and high pc attributes. */
13856 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13857 <= PC_BOUNDS_INVALID)
13859 attr = dwarf2_attr (die, DW_AT_external, cu);
13860 if (!attr || !DW_UNSND (attr))
13861 complaint (&symfile_complaints,
13862 _("cannot get low and high bounds "
13863 "for subprogram DIE at %d"),
13864 to_underlying (die->sect_off));
13868 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13869 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13871 /* If we have any template arguments, then we must allocate a
13872 different sort of symbol. */
13873 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13875 if (child_die->tag == DW_TAG_template_type_param
13876 || child_die->tag == DW_TAG_template_value_param)
13878 templ_func = allocate_template_symbol (objfile);
13879 templ_func->subclass = SYMBOL_TEMPLATE;
13884 newobj = push_context (0, lowpc);
13885 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
13886 (struct symbol *) templ_func);
13888 /* If there is a location expression for DW_AT_frame_base, record
13890 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13892 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13894 /* If there is a location for the static link, record it. */
13895 newobj->static_link = NULL;
13896 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13899 newobj->static_link
13900 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13901 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13904 cu->list_in_scope = &local_symbols;
13906 if (die->child != NULL)
13908 child_die = die->child;
13909 while (child_die && child_die->tag)
13911 if (child_die->tag == DW_TAG_template_type_param
13912 || child_die->tag == DW_TAG_template_value_param)
13914 struct symbol *arg = new_symbol (child_die, NULL, cu);
13917 template_args.push_back (arg);
13920 process_die (child_die, cu);
13921 child_die = sibling_die (child_die);
13925 inherit_abstract_dies (die, cu);
13927 /* If we have a DW_AT_specification, we might need to import using
13928 directives from the context of the specification DIE. See the
13929 comment in determine_prefix. */
13930 if (cu->language == language_cplus
13931 && dwarf2_attr (die, DW_AT_specification, cu))
13933 struct dwarf2_cu *spec_cu = cu;
13934 struct die_info *spec_die = die_specification (die, &spec_cu);
13938 child_die = spec_die->child;
13939 while (child_die && child_die->tag)
13941 if (child_die->tag == DW_TAG_imported_module)
13942 process_die (child_die, spec_cu);
13943 child_die = sibling_die (child_die);
13946 /* In some cases, GCC generates specification DIEs that
13947 themselves contain DW_AT_specification attributes. */
13948 spec_die = die_specification (spec_die, &spec_cu);
13952 newobj = pop_context ();
13953 /* Make a block for the local symbols within. */
13954 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13955 newobj->static_link, lowpc, highpc);
13957 /* For C++, set the block's scope. */
13958 if ((cu->language == language_cplus
13959 || cu->language == language_fortran
13960 || cu->language == language_d
13961 || cu->language == language_rust)
13962 && cu->processing_has_namespace_info)
13963 block_set_scope (block, determine_prefix (die, cu),
13964 &objfile->objfile_obstack);
13966 /* If we have address ranges, record them. */
13967 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13969 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13971 /* Attach template arguments to function. */
13972 if (!template_args.empty ())
13974 gdb_assert (templ_func != NULL);
13976 templ_func->n_template_arguments = template_args.size ();
13977 templ_func->template_arguments
13978 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13979 templ_func->n_template_arguments);
13980 memcpy (templ_func->template_arguments,
13981 template_args.data (),
13982 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13985 /* In C++, we can have functions nested inside functions (e.g., when
13986 a function declares a class that has methods). This means that
13987 when we finish processing a function scope, we may need to go
13988 back to building a containing block's symbol lists. */
13989 local_symbols = newobj->locals;
13990 local_using_directives = newobj->local_using_directives;
13992 /* If we've finished processing a top-level function, subsequent
13993 symbols go in the file symbol list. */
13994 if (outermost_context_p ())
13995 cu->list_in_scope = &file_symbols;
13998 /* Process all the DIES contained within a lexical block scope. Start
13999 a new scope, process the dies, and then close the scope. */
14002 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
14004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14006 struct context_stack *newobj;
14007 CORE_ADDR lowpc, highpc;
14008 struct die_info *child_die;
14009 CORE_ADDR baseaddr;
14011 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14013 /* Ignore blocks with missing or invalid low and high pc attributes. */
14014 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14015 as multiple lexical blocks? Handling children in a sane way would
14016 be nasty. Might be easier to properly extend generic blocks to
14017 describe ranges. */
14018 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14020 case PC_BOUNDS_NOT_PRESENT:
14021 /* DW_TAG_lexical_block has no attributes, process its children as if
14022 there was no wrapping by that DW_TAG_lexical_block.
14023 GCC does no longer produces such DWARF since GCC r224161. */
14024 for (child_die = die->child;
14025 child_die != NULL && child_die->tag;
14026 child_die = sibling_die (child_die))
14027 process_die (child_die, cu);
14029 case PC_BOUNDS_INVALID:
14032 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14033 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14035 push_context (0, lowpc);
14036 if (die->child != NULL)
14038 child_die = die->child;
14039 while (child_die && child_die->tag)
14041 process_die (child_die, cu);
14042 child_die = sibling_die (child_die);
14045 inherit_abstract_dies (die, cu);
14046 newobj = pop_context ();
14048 if (local_symbols != NULL || local_using_directives != NULL)
14050 struct block *block
14051 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14052 newobj->start_addr, highpc);
14054 /* Note that recording ranges after traversing children, as we
14055 do here, means that recording a parent's ranges entails
14056 walking across all its children's ranges as they appear in
14057 the address map, which is quadratic behavior.
14059 It would be nicer to record the parent's ranges before
14060 traversing its children, simply overriding whatever you find
14061 there. But since we don't even decide whether to create a
14062 block until after we've traversed its children, that's hard
14064 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14066 local_symbols = newobj->locals;
14067 local_using_directives = newobj->local_using_directives;
14070 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14073 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14076 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14077 CORE_ADDR pc, baseaddr;
14078 struct attribute *attr;
14079 struct call_site *call_site, call_site_local;
14082 struct die_info *child_die;
14084 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14086 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14089 /* This was a pre-DWARF-5 GNU extension alias
14090 for DW_AT_call_return_pc. */
14091 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14095 complaint (&symfile_complaints,
14096 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14097 "DIE 0x%x [in module %s]"),
14098 to_underlying (die->sect_off), objfile_name (objfile));
14101 pc = attr_value_as_address (attr) + baseaddr;
14102 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14104 if (cu->call_site_htab == NULL)
14105 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14106 NULL, &objfile->objfile_obstack,
14107 hashtab_obstack_allocate, NULL);
14108 call_site_local.pc = pc;
14109 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14112 complaint (&symfile_complaints,
14113 _("Duplicate PC %s for DW_TAG_call_site "
14114 "DIE 0x%x [in module %s]"),
14115 paddress (gdbarch, pc), to_underlying (die->sect_off),
14116 objfile_name (objfile));
14120 /* Count parameters at the caller. */
14123 for (child_die = die->child; child_die && child_die->tag;
14124 child_die = sibling_die (child_die))
14126 if (child_die->tag != DW_TAG_call_site_parameter
14127 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14129 complaint (&symfile_complaints,
14130 _("Tag %d is not DW_TAG_call_site_parameter in "
14131 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14132 child_die->tag, to_underlying (child_die->sect_off),
14133 objfile_name (objfile));
14141 = ((struct call_site *)
14142 obstack_alloc (&objfile->objfile_obstack,
14143 sizeof (*call_site)
14144 + (sizeof (*call_site->parameter) * (nparams - 1))));
14146 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14147 call_site->pc = pc;
14149 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14150 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14152 struct die_info *func_die;
14154 /* Skip also over DW_TAG_inlined_subroutine. */
14155 for (func_die = die->parent;
14156 func_die && func_die->tag != DW_TAG_subprogram
14157 && func_die->tag != DW_TAG_subroutine_type;
14158 func_die = func_die->parent);
14160 /* DW_AT_call_all_calls is a superset
14161 of DW_AT_call_all_tail_calls. */
14163 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14164 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14165 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14166 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14168 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14169 not complete. But keep CALL_SITE for look ups via call_site_htab,
14170 both the initial caller containing the real return address PC and
14171 the final callee containing the current PC of a chain of tail
14172 calls do not need to have the tail call list complete. But any
14173 function candidate for a virtual tail call frame searched via
14174 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14175 determined unambiguously. */
14179 struct type *func_type = NULL;
14182 func_type = get_die_type (func_die, cu);
14183 if (func_type != NULL)
14185 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14187 /* Enlist this call site to the function. */
14188 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14189 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14192 complaint (&symfile_complaints,
14193 _("Cannot find function owning DW_TAG_call_site "
14194 "DIE 0x%x [in module %s]"),
14195 to_underlying (die->sect_off), objfile_name (objfile));
14199 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14201 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14203 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14206 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14207 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14209 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14210 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14211 /* Keep NULL DWARF_BLOCK. */;
14212 else if (attr_form_is_block (attr))
14214 struct dwarf2_locexpr_baton *dlbaton;
14216 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14217 dlbaton->data = DW_BLOCK (attr)->data;
14218 dlbaton->size = DW_BLOCK (attr)->size;
14219 dlbaton->per_cu = cu->per_cu;
14221 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14223 else if (attr_form_is_ref (attr))
14225 struct dwarf2_cu *target_cu = cu;
14226 struct die_info *target_die;
14228 target_die = follow_die_ref (die, attr, &target_cu);
14229 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14230 if (die_is_declaration (target_die, target_cu))
14232 const char *target_physname;
14234 /* Prefer the mangled name; otherwise compute the demangled one. */
14235 target_physname = dw2_linkage_name (target_die, target_cu);
14236 if (target_physname == NULL)
14237 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14238 if (target_physname == NULL)
14239 complaint (&symfile_complaints,
14240 _("DW_AT_call_target target DIE has invalid "
14241 "physname, for referencing DIE 0x%x [in module %s]"),
14242 to_underlying (die->sect_off), objfile_name (objfile));
14244 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14250 /* DW_AT_entry_pc should be preferred. */
14251 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14252 <= PC_BOUNDS_INVALID)
14253 complaint (&symfile_complaints,
14254 _("DW_AT_call_target target DIE has invalid "
14255 "low pc, for referencing DIE 0x%x [in module %s]"),
14256 to_underlying (die->sect_off), objfile_name (objfile));
14259 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14260 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14265 complaint (&symfile_complaints,
14266 _("DW_TAG_call_site DW_AT_call_target is neither "
14267 "block nor reference, for DIE 0x%x [in module %s]"),
14268 to_underlying (die->sect_off), objfile_name (objfile));
14270 call_site->per_cu = cu->per_cu;
14272 for (child_die = die->child;
14273 child_die && child_die->tag;
14274 child_die = sibling_die (child_die))
14276 struct call_site_parameter *parameter;
14277 struct attribute *loc, *origin;
14279 if (child_die->tag != DW_TAG_call_site_parameter
14280 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14282 /* Already printed the complaint above. */
14286 gdb_assert (call_site->parameter_count < nparams);
14287 parameter = &call_site->parameter[call_site->parameter_count];
14289 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14290 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14291 register is contained in DW_AT_call_value. */
14293 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14294 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14295 if (origin == NULL)
14297 /* This was a pre-DWARF-5 GNU extension alias
14298 for DW_AT_call_parameter. */
14299 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14301 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14303 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14305 sect_offset sect_off
14306 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14307 if (!offset_in_cu_p (&cu->header, sect_off))
14309 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14310 binding can be done only inside one CU. Such referenced DIE
14311 therefore cannot be even moved to DW_TAG_partial_unit. */
14312 complaint (&symfile_complaints,
14313 _("DW_AT_call_parameter offset is not in CU for "
14314 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14315 to_underlying (child_die->sect_off),
14316 objfile_name (objfile));
14319 parameter->u.param_cu_off
14320 = (cu_offset) (sect_off - cu->header.sect_off);
14322 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14324 complaint (&symfile_complaints,
14325 _("No DW_FORM_block* DW_AT_location for "
14326 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14327 to_underlying (child_die->sect_off), objfile_name (objfile));
14332 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14333 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14334 if (parameter->u.dwarf_reg != -1)
14335 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14336 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14337 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14338 ¶meter->u.fb_offset))
14339 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14342 complaint (&symfile_complaints,
14343 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14344 "for DW_FORM_block* DW_AT_location is supported for "
14345 "DW_TAG_call_site child DIE 0x%x "
14347 to_underlying (child_die->sect_off),
14348 objfile_name (objfile));
14353 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14355 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14356 if (!attr_form_is_block (attr))
14358 complaint (&symfile_complaints,
14359 _("No DW_FORM_block* DW_AT_call_value for "
14360 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14361 to_underlying (child_die->sect_off),
14362 objfile_name (objfile));
14365 parameter->value = DW_BLOCK (attr)->data;
14366 parameter->value_size = DW_BLOCK (attr)->size;
14368 /* Parameters are not pre-cleared by memset above. */
14369 parameter->data_value = NULL;
14370 parameter->data_value_size = 0;
14371 call_site->parameter_count++;
14373 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14375 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14378 if (!attr_form_is_block (attr))
14379 complaint (&symfile_complaints,
14380 _("No DW_FORM_block* DW_AT_call_data_value for "
14381 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14382 to_underlying (child_die->sect_off),
14383 objfile_name (objfile));
14386 parameter->data_value = DW_BLOCK (attr)->data;
14387 parameter->data_value_size = DW_BLOCK (attr)->size;
14393 /* Helper function for read_variable. If DIE represents a virtual
14394 table, then return the type of the concrete object that is
14395 associated with the virtual table. Otherwise, return NULL. */
14397 static struct type *
14398 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14400 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14404 /* Find the type DIE. */
14405 struct die_info *type_die = NULL;
14406 struct dwarf2_cu *type_cu = cu;
14408 if (attr_form_is_ref (attr))
14409 type_die = follow_die_ref (die, attr, &type_cu);
14410 if (type_die == NULL)
14413 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14415 return die_containing_type (type_die, type_cu);
14418 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14421 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14423 struct rust_vtable_symbol *storage = NULL;
14425 if (cu->language == language_rust)
14427 struct type *containing_type = rust_containing_type (die, cu);
14429 if (containing_type != NULL)
14431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14433 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14434 struct rust_vtable_symbol);
14435 initialize_objfile_symbol (storage);
14436 storage->concrete_type = containing_type;
14437 storage->subclass = SYMBOL_RUST_VTABLE;
14441 new_symbol_full (die, NULL, cu, storage);
14444 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14445 reading .debug_rnglists.
14446 Callback's type should be:
14447 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14448 Return true if the attributes are present and valid, otherwise,
14451 template <typename Callback>
14453 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14454 Callback &&callback)
14456 struct dwarf2_per_objfile *dwarf2_per_objfile
14457 = cu->per_cu->dwarf2_per_objfile;
14458 struct objfile *objfile = dwarf2_per_objfile->objfile;
14459 bfd *obfd = objfile->obfd;
14460 /* Base address selection entry. */
14463 const gdb_byte *buffer;
14464 CORE_ADDR baseaddr;
14465 bool overflow = false;
14467 found_base = cu->base_known;
14468 base = cu->base_address;
14470 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14471 if (offset >= dwarf2_per_objfile->rnglists.size)
14473 complaint (&symfile_complaints,
14474 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14478 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14480 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14484 /* Initialize it due to a false compiler warning. */
14485 CORE_ADDR range_beginning = 0, range_end = 0;
14486 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14487 + dwarf2_per_objfile->rnglists.size);
14488 unsigned int bytes_read;
14490 if (buffer == buf_end)
14495 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14498 case DW_RLE_end_of_list:
14500 case DW_RLE_base_address:
14501 if (buffer + cu->header.addr_size > buf_end)
14506 base = read_address (obfd, buffer, cu, &bytes_read);
14508 buffer += bytes_read;
14510 case DW_RLE_start_length:
14511 if (buffer + cu->header.addr_size > buf_end)
14516 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14517 buffer += bytes_read;
14518 range_end = (range_beginning
14519 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14520 buffer += bytes_read;
14521 if (buffer > buf_end)
14527 case DW_RLE_offset_pair:
14528 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14529 buffer += bytes_read;
14530 if (buffer > buf_end)
14535 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14536 buffer += bytes_read;
14537 if (buffer > buf_end)
14543 case DW_RLE_start_end:
14544 if (buffer + 2 * cu->header.addr_size > buf_end)
14549 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14550 buffer += bytes_read;
14551 range_end = read_address (obfd, buffer, cu, &bytes_read);
14552 buffer += bytes_read;
14555 complaint (&symfile_complaints,
14556 _("Invalid .debug_rnglists data (no base address)"));
14559 if (rlet == DW_RLE_end_of_list || overflow)
14561 if (rlet == DW_RLE_base_address)
14566 /* We have no valid base address for the ranges
14568 complaint (&symfile_complaints,
14569 _("Invalid .debug_rnglists data (no base address)"));
14573 if (range_beginning > range_end)
14575 /* Inverted range entries are invalid. */
14576 complaint (&symfile_complaints,
14577 _("Invalid .debug_rnglists data (inverted range)"));
14581 /* Empty range entries have no effect. */
14582 if (range_beginning == range_end)
14585 range_beginning += base;
14588 /* A not-uncommon case of bad debug info.
14589 Don't pollute the addrmap with bad data. */
14590 if (range_beginning + baseaddr == 0
14591 && !dwarf2_per_objfile->has_section_at_zero)
14593 complaint (&symfile_complaints,
14594 _(".debug_rnglists entry has start address of zero"
14595 " [in module %s]"), objfile_name (objfile));
14599 callback (range_beginning, range_end);
14604 complaint (&symfile_complaints,
14605 _("Offset %d is not terminated "
14606 "for DW_AT_ranges attribute"),
14614 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14615 Callback's type should be:
14616 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14617 Return 1 if the attributes are present and valid, otherwise, return 0. */
14619 template <typename Callback>
14621 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14622 Callback &&callback)
14624 struct dwarf2_per_objfile *dwarf2_per_objfile
14625 = cu->per_cu->dwarf2_per_objfile;
14626 struct objfile *objfile = dwarf2_per_objfile->objfile;
14627 struct comp_unit_head *cu_header = &cu->header;
14628 bfd *obfd = objfile->obfd;
14629 unsigned int addr_size = cu_header->addr_size;
14630 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14631 /* Base address selection entry. */
14634 unsigned int dummy;
14635 const gdb_byte *buffer;
14636 CORE_ADDR baseaddr;
14638 if (cu_header->version >= 5)
14639 return dwarf2_rnglists_process (offset, cu, callback);
14641 found_base = cu->base_known;
14642 base = cu->base_address;
14644 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14645 if (offset >= dwarf2_per_objfile->ranges.size)
14647 complaint (&symfile_complaints,
14648 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14652 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14654 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14658 CORE_ADDR range_beginning, range_end;
14660 range_beginning = read_address (obfd, buffer, cu, &dummy);
14661 buffer += addr_size;
14662 range_end = read_address (obfd, buffer, cu, &dummy);
14663 buffer += addr_size;
14664 offset += 2 * addr_size;
14666 /* An end of list marker is a pair of zero addresses. */
14667 if (range_beginning == 0 && range_end == 0)
14668 /* Found the end of list entry. */
14671 /* Each base address selection entry is a pair of 2 values.
14672 The first is the largest possible address, the second is
14673 the base address. Check for a base address here. */
14674 if ((range_beginning & mask) == mask)
14676 /* If we found the largest possible address, then we already
14677 have the base address in range_end. */
14685 /* We have no valid base address for the ranges
14687 complaint (&symfile_complaints,
14688 _("Invalid .debug_ranges data (no base address)"));
14692 if (range_beginning > range_end)
14694 /* Inverted range entries are invalid. */
14695 complaint (&symfile_complaints,
14696 _("Invalid .debug_ranges data (inverted range)"));
14700 /* Empty range entries have no effect. */
14701 if (range_beginning == range_end)
14704 range_beginning += base;
14707 /* A not-uncommon case of bad debug info.
14708 Don't pollute the addrmap with bad data. */
14709 if (range_beginning + baseaddr == 0
14710 && !dwarf2_per_objfile->has_section_at_zero)
14712 complaint (&symfile_complaints,
14713 _(".debug_ranges entry has start address of zero"
14714 " [in module %s]"), objfile_name (objfile));
14718 callback (range_beginning, range_end);
14724 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14725 Return 1 if the attributes are present and valid, otherwise, return 0.
14726 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14729 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14730 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14731 struct partial_symtab *ranges_pst)
14733 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14734 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14735 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14736 SECT_OFF_TEXT (objfile));
14739 CORE_ADDR high = 0;
14742 retval = dwarf2_ranges_process (offset, cu,
14743 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14745 if (ranges_pst != NULL)
14750 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14751 range_beginning + baseaddr);
14752 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14753 range_end + baseaddr);
14754 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14758 /* FIXME: This is recording everything as a low-high
14759 segment of consecutive addresses. We should have a
14760 data structure for discontiguous block ranges
14764 low = range_beginning;
14770 if (range_beginning < low)
14771 low = range_beginning;
14772 if (range_end > high)
14780 /* If the first entry is an end-of-list marker, the range
14781 describes an empty scope, i.e. no instructions. */
14787 *high_return = high;
14791 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14792 definition for the return value. *LOWPC and *HIGHPC are set iff
14793 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14795 static enum pc_bounds_kind
14796 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14797 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14798 struct partial_symtab *pst)
14800 struct dwarf2_per_objfile *dwarf2_per_objfile
14801 = cu->per_cu->dwarf2_per_objfile;
14802 struct attribute *attr;
14803 struct attribute *attr_high;
14805 CORE_ADDR high = 0;
14806 enum pc_bounds_kind ret;
14808 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14811 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14814 low = attr_value_as_address (attr);
14815 high = attr_value_as_address (attr_high);
14816 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14820 /* Found high w/o low attribute. */
14821 return PC_BOUNDS_INVALID;
14823 /* Found consecutive range of addresses. */
14824 ret = PC_BOUNDS_HIGH_LOW;
14828 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14831 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14832 We take advantage of the fact that DW_AT_ranges does not appear
14833 in DW_TAG_compile_unit of DWO files. */
14834 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14835 unsigned int ranges_offset = (DW_UNSND (attr)
14836 + (need_ranges_base
14840 /* Value of the DW_AT_ranges attribute is the offset in the
14841 .debug_ranges section. */
14842 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14843 return PC_BOUNDS_INVALID;
14844 /* Found discontinuous range of addresses. */
14845 ret = PC_BOUNDS_RANGES;
14848 return PC_BOUNDS_NOT_PRESENT;
14851 /* read_partial_die has also the strict LOW < HIGH requirement. */
14853 return PC_BOUNDS_INVALID;
14855 /* When using the GNU linker, .gnu.linkonce. sections are used to
14856 eliminate duplicate copies of functions and vtables and such.
14857 The linker will arbitrarily choose one and discard the others.
14858 The AT_*_pc values for such functions refer to local labels in
14859 these sections. If the section from that file was discarded, the
14860 labels are not in the output, so the relocs get a value of 0.
14861 If this is a discarded function, mark the pc bounds as invalid,
14862 so that GDB will ignore it. */
14863 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14864 return PC_BOUNDS_INVALID;
14872 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14873 its low and high PC addresses. Do nothing if these addresses could not
14874 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14875 and HIGHPC to the high address if greater than HIGHPC. */
14878 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14879 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14880 struct dwarf2_cu *cu)
14882 CORE_ADDR low, high;
14883 struct die_info *child = die->child;
14885 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14887 *lowpc = std::min (*lowpc, low);
14888 *highpc = std::max (*highpc, high);
14891 /* If the language does not allow nested subprograms (either inside
14892 subprograms or lexical blocks), we're done. */
14893 if (cu->language != language_ada)
14896 /* Check all the children of the given DIE. If it contains nested
14897 subprograms, then check their pc bounds. Likewise, we need to
14898 check lexical blocks as well, as they may also contain subprogram
14900 while (child && child->tag)
14902 if (child->tag == DW_TAG_subprogram
14903 || child->tag == DW_TAG_lexical_block)
14904 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14905 child = sibling_die (child);
14909 /* Get the low and high pc's represented by the scope DIE, and store
14910 them in *LOWPC and *HIGHPC. If the correct values can't be
14911 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14914 get_scope_pc_bounds (struct die_info *die,
14915 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14916 struct dwarf2_cu *cu)
14918 CORE_ADDR best_low = (CORE_ADDR) -1;
14919 CORE_ADDR best_high = (CORE_ADDR) 0;
14920 CORE_ADDR current_low, current_high;
14922 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14923 >= PC_BOUNDS_RANGES)
14925 best_low = current_low;
14926 best_high = current_high;
14930 struct die_info *child = die->child;
14932 while (child && child->tag)
14934 switch (child->tag) {
14935 case DW_TAG_subprogram:
14936 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14938 case DW_TAG_namespace:
14939 case DW_TAG_module:
14940 /* FIXME: carlton/2004-01-16: Should we do this for
14941 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14942 that current GCC's always emit the DIEs corresponding
14943 to definitions of methods of classes as children of a
14944 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14945 the DIEs giving the declarations, which could be
14946 anywhere). But I don't see any reason why the
14947 standards says that they have to be there. */
14948 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14950 if (current_low != ((CORE_ADDR) -1))
14952 best_low = std::min (best_low, current_low);
14953 best_high = std::max (best_high, current_high);
14961 child = sibling_die (child);
14966 *highpc = best_high;
14969 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14973 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14974 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14976 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14977 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14978 struct attribute *attr;
14979 struct attribute *attr_high;
14981 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14984 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14987 CORE_ADDR low = attr_value_as_address (attr);
14988 CORE_ADDR high = attr_value_as_address (attr_high);
14990 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14993 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14994 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14995 record_block_range (block, low, high - 1);
14999 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15002 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15003 We take advantage of the fact that DW_AT_ranges does not appear
15004 in DW_TAG_compile_unit of DWO files. */
15005 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15007 /* The value of the DW_AT_ranges attribute is the offset of the
15008 address range list in the .debug_ranges section. */
15009 unsigned long offset = (DW_UNSND (attr)
15010 + (need_ranges_base ? cu->ranges_base : 0));
15011 const gdb_byte *buffer;
15013 /* For some target architectures, but not others, the
15014 read_address function sign-extends the addresses it returns.
15015 To recognize base address selection entries, we need a
15017 unsigned int addr_size = cu->header.addr_size;
15018 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15020 /* The base address, to which the next pair is relative. Note
15021 that this 'base' is a DWARF concept: most entries in a range
15022 list are relative, to reduce the number of relocs against the
15023 debugging information. This is separate from this function's
15024 'baseaddr' argument, which GDB uses to relocate debugging
15025 information from a shared library based on the address at
15026 which the library was loaded. */
15027 CORE_ADDR base = cu->base_address;
15028 int base_known = cu->base_known;
15030 dwarf2_ranges_process (offset, cu,
15031 [&] (CORE_ADDR start, CORE_ADDR end)
15035 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15036 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15037 record_block_range (block, start, end - 1);
15042 /* Check whether the producer field indicates either of GCC < 4.6, or the
15043 Intel C/C++ compiler, and cache the result in CU. */
15046 check_producer (struct dwarf2_cu *cu)
15050 if (cu->producer == NULL)
15052 /* For unknown compilers expect their behavior is DWARF version
15055 GCC started to support .debug_types sections by -gdwarf-4 since
15056 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15057 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15058 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15059 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15061 else if (producer_is_gcc (cu->producer, &major, &minor))
15063 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15064 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15066 else if (producer_is_icc (cu->producer, &major, &minor))
15067 cu->producer_is_icc_lt_14 = major < 14;
15070 /* For other non-GCC compilers, expect their behavior is DWARF version
15074 cu->checked_producer = 1;
15077 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15078 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15079 during 4.6.0 experimental. */
15082 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15084 if (!cu->checked_producer)
15085 check_producer (cu);
15087 return cu->producer_is_gxx_lt_4_6;
15090 /* Return the default accessibility type if it is not overriden by
15091 DW_AT_accessibility. */
15093 static enum dwarf_access_attribute
15094 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15096 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15098 /* The default DWARF 2 accessibility for members is public, the default
15099 accessibility for inheritance is private. */
15101 if (die->tag != DW_TAG_inheritance)
15102 return DW_ACCESS_public;
15104 return DW_ACCESS_private;
15108 /* DWARF 3+ defines the default accessibility a different way. The same
15109 rules apply now for DW_TAG_inheritance as for the members and it only
15110 depends on the container kind. */
15112 if (die->parent->tag == DW_TAG_class_type)
15113 return DW_ACCESS_private;
15115 return DW_ACCESS_public;
15119 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15120 offset. If the attribute was not found return 0, otherwise return
15121 1. If it was found but could not properly be handled, set *OFFSET
15125 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15128 struct attribute *attr;
15130 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15135 /* Note that we do not check for a section offset first here.
15136 This is because DW_AT_data_member_location is new in DWARF 4,
15137 so if we see it, we can assume that a constant form is really
15138 a constant and not a section offset. */
15139 if (attr_form_is_constant (attr))
15140 *offset = dwarf2_get_attr_constant_value (attr, 0);
15141 else if (attr_form_is_section_offset (attr))
15142 dwarf2_complex_location_expr_complaint ();
15143 else if (attr_form_is_block (attr))
15144 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15146 dwarf2_complex_location_expr_complaint ();
15154 /* Add an aggregate field to the field list. */
15157 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15158 struct dwarf2_cu *cu)
15160 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15161 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15162 struct nextfield *new_field;
15163 struct attribute *attr;
15165 const char *fieldname = "";
15167 /* Allocate a new field list entry and link it in. */
15168 new_field = XNEW (struct nextfield);
15169 make_cleanup (xfree, new_field);
15170 memset (new_field, 0, sizeof (struct nextfield));
15172 if (die->tag == DW_TAG_inheritance)
15174 new_field->next = fip->baseclasses;
15175 fip->baseclasses = new_field;
15179 new_field->next = fip->fields;
15180 fip->fields = new_field;
15184 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15186 new_field->accessibility = DW_UNSND (attr);
15188 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15189 if (new_field->accessibility != DW_ACCESS_public)
15190 fip->non_public_fields = 1;
15192 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15194 new_field->virtuality = DW_UNSND (attr);
15196 new_field->virtuality = DW_VIRTUALITY_none;
15198 fp = &new_field->field;
15200 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15204 /* Data member other than a C++ static data member. */
15206 /* Get type of field. */
15207 fp->type = die_type (die, cu);
15209 SET_FIELD_BITPOS (*fp, 0);
15211 /* Get bit size of field (zero if none). */
15212 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15215 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15219 FIELD_BITSIZE (*fp) = 0;
15222 /* Get bit offset of field. */
15223 if (handle_data_member_location (die, cu, &offset))
15224 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15225 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15228 if (gdbarch_bits_big_endian (gdbarch))
15230 /* For big endian bits, the DW_AT_bit_offset gives the
15231 additional bit offset from the MSB of the containing
15232 anonymous object to the MSB of the field. We don't
15233 have to do anything special since we don't need to
15234 know the size of the anonymous object. */
15235 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15239 /* For little endian bits, compute the bit offset to the
15240 MSB of the anonymous object, subtract off the number of
15241 bits from the MSB of the field to the MSB of the
15242 object, and then subtract off the number of bits of
15243 the field itself. The result is the bit offset of
15244 the LSB of the field. */
15245 int anonymous_size;
15246 int bit_offset = DW_UNSND (attr);
15248 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15251 /* The size of the anonymous object containing
15252 the bit field is explicit, so use the
15253 indicated size (in bytes). */
15254 anonymous_size = DW_UNSND (attr);
15258 /* The size of the anonymous object containing
15259 the bit field must be inferred from the type
15260 attribute of the data member containing the
15262 anonymous_size = TYPE_LENGTH (fp->type);
15264 SET_FIELD_BITPOS (*fp,
15265 (FIELD_BITPOS (*fp)
15266 + anonymous_size * bits_per_byte
15267 - bit_offset - FIELD_BITSIZE (*fp)));
15270 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15272 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15273 + dwarf2_get_attr_constant_value (attr, 0)));
15275 /* Get name of field. */
15276 fieldname = dwarf2_name (die, cu);
15277 if (fieldname == NULL)
15280 /* The name is already allocated along with this objfile, so we don't
15281 need to duplicate it for the type. */
15282 fp->name = fieldname;
15284 /* Change accessibility for artificial fields (e.g. virtual table
15285 pointer or virtual base class pointer) to private. */
15286 if (dwarf2_attr (die, DW_AT_artificial, cu))
15288 FIELD_ARTIFICIAL (*fp) = 1;
15289 new_field->accessibility = DW_ACCESS_private;
15290 fip->non_public_fields = 1;
15293 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15295 /* C++ static member. */
15297 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15298 is a declaration, but all versions of G++ as of this writing
15299 (so through at least 3.2.1) incorrectly generate
15300 DW_TAG_variable tags. */
15302 const char *physname;
15304 /* Get name of field. */
15305 fieldname = dwarf2_name (die, cu);
15306 if (fieldname == NULL)
15309 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15311 /* Only create a symbol if this is an external value.
15312 new_symbol checks this and puts the value in the global symbol
15313 table, which we want. If it is not external, new_symbol
15314 will try to put the value in cu->list_in_scope which is wrong. */
15315 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15317 /* A static const member, not much different than an enum as far as
15318 we're concerned, except that we can support more types. */
15319 new_symbol (die, NULL, cu);
15322 /* Get physical name. */
15323 physname = dwarf2_physname (fieldname, die, cu);
15325 /* The name is already allocated along with this objfile, so we don't
15326 need to duplicate it for the type. */
15327 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15328 FIELD_TYPE (*fp) = die_type (die, cu);
15329 FIELD_NAME (*fp) = fieldname;
15331 else if (die->tag == DW_TAG_inheritance)
15335 /* C++ base class field. */
15336 if (handle_data_member_location (die, cu, &offset))
15337 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15338 FIELD_BITSIZE (*fp) = 0;
15339 FIELD_TYPE (*fp) = die_type (die, cu);
15340 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15341 fip->nbaseclasses++;
15345 /* Can the type given by DIE define another type? */
15348 type_can_define_types (const struct die_info *die)
15352 case DW_TAG_typedef:
15353 case DW_TAG_class_type:
15354 case DW_TAG_structure_type:
15355 case DW_TAG_union_type:
15356 case DW_TAG_enumeration_type:
15364 /* Add a type definition defined in the scope of the FIP's class. */
15367 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15368 struct dwarf2_cu *cu)
15370 struct decl_field_list *new_field;
15371 struct decl_field *fp;
15373 /* Allocate a new field list entry and link it in. */
15374 new_field = XCNEW (struct decl_field_list);
15375 make_cleanup (xfree, new_field);
15377 gdb_assert (type_can_define_types (die));
15379 fp = &new_field->field;
15381 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15382 fp->name = dwarf2_name (die, cu);
15383 fp->type = read_type_die (die, cu);
15385 /* Save accessibility. */
15386 enum dwarf_access_attribute accessibility;
15387 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15389 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15391 accessibility = dwarf2_default_access_attribute (die, cu);
15392 switch (accessibility)
15394 case DW_ACCESS_public:
15395 /* The assumed value if neither private nor protected. */
15397 case DW_ACCESS_private:
15398 fp->is_private = 1;
15400 case DW_ACCESS_protected:
15401 fp->is_protected = 1;
15404 complaint (&symfile_complaints,
15405 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15408 if (die->tag == DW_TAG_typedef)
15410 new_field->next = fip->typedef_field_list;
15411 fip->typedef_field_list = new_field;
15412 fip->typedef_field_list_count++;
15416 new_field->next = fip->nested_types_list;
15417 fip->nested_types_list = new_field;
15418 fip->nested_types_list_count++;
15422 /* Create the vector of fields, and attach it to the type. */
15425 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15426 struct dwarf2_cu *cu)
15428 int nfields = fip->nfields;
15430 /* Record the field count, allocate space for the array of fields,
15431 and create blank accessibility bitfields if necessary. */
15432 TYPE_NFIELDS (type) = nfields;
15433 TYPE_FIELDS (type) = (struct field *)
15434 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15435 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15437 if (fip->non_public_fields && cu->language != language_ada)
15439 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15441 TYPE_FIELD_PRIVATE_BITS (type) =
15442 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15443 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15445 TYPE_FIELD_PROTECTED_BITS (type) =
15446 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15447 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15449 TYPE_FIELD_IGNORE_BITS (type) =
15450 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15451 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15454 /* If the type has baseclasses, allocate and clear a bit vector for
15455 TYPE_FIELD_VIRTUAL_BITS. */
15456 if (fip->nbaseclasses && cu->language != language_ada)
15458 int num_bytes = B_BYTES (fip->nbaseclasses);
15459 unsigned char *pointer;
15461 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15462 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15463 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15464 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15465 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15468 /* Copy the saved-up fields into the field vector. Start from the head of
15469 the list, adding to the tail of the field array, so that they end up in
15470 the same order in the array in which they were added to the list. */
15471 while (nfields-- > 0)
15473 struct nextfield *fieldp;
15477 fieldp = fip->fields;
15478 fip->fields = fieldp->next;
15482 fieldp = fip->baseclasses;
15483 fip->baseclasses = fieldp->next;
15486 TYPE_FIELD (type, nfields) = fieldp->field;
15487 switch (fieldp->accessibility)
15489 case DW_ACCESS_private:
15490 if (cu->language != language_ada)
15491 SET_TYPE_FIELD_PRIVATE (type, nfields);
15494 case DW_ACCESS_protected:
15495 if (cu->language != language_ada)
15496 SET_TYPE_FIELD_PROTECTED (type, nfields);
15499 case DW_ACCESS_public:
15503 /* Unknown accessibility. Complain and treat it as public. */
15505 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15506 fieldp->accessibility);
15510 if (nfields < fip->nbaseclasses)
15512 switch (fieldp->virtuality)
15514 case DW_VIRTUALITY_virtual:
15515 case DW_VIRTUALITY_pure_virtual:
15516 if (cu->language == language_ada)
15517 error (_("unexpected virtuality in component of Ada type"));
15518 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15525 /* Return true if this member function is a constructor, false
15529 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15531 const char *fieldname;
15532 const char *type_name;
15535 if (die->parent == NULL)
15538 if (die->parent->tag != DW_TAG_structure_type
15539 && die->parent->tag != DW_TAG_union_type
15540 && die->parent->tag != DW_TAG_class_type)
15543 fieldname = dwarf2_name (die, cu);
15544 type_name = dwarf2_name (die->parent, cu);
15545 if (fieldname == NULL || type_name == NULL)
15548 len = strlen (fieldname);
15549 return (strncmp (fieldname, type_name, len) == 0
15550 && (type_name[len] == '\0' || type_name[len] == '<'));
15553 /* Add a member function to the proper fieldlist. */
15556 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15557 struct type *type, struct dwarf2_cu *cu)
15559 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15560 struct attribute *attr;
15561 struct fnfieldlist *flp;
15563 struct fn_field *fnp;
15564 const char *fieldname;
15565 struct nextfnfield *new_fnfield;
15566 struct type *this_type;
15567 enum dwarf_access_attribute accessibility;
15569 if (cu->language == language_ada)
15570 error (_("unexpected member function in Ada type"));
15572 /* Get name of member function. */
15573 fieldname = dwarf2_name (die, cu);
15574 if (fieldname == NULL)
15577 /* Look up member function name in fieldlist. */
15578 for (i = 0; i < fip->nfnfields; i++)
15580 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15584 /* Create new list element if necessary. */
15585 if (i < fip->nfnfields)
15586 flp = &fip->fnfieldlists[i];
15589 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15591 fip->fnfieldlists = (struct fnfieldlist *)
15592 xrealloc (fip->fnfieldlists,
15593 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15594 * sizeof (struct fnfieldlist));
15595 if (fip->nfnfields == 0)
15596 make_cleanup (free_current_contents, &fip->fnfieldlists);
15598 flp = &fip->fnfieldlists[fip->nfnfields];
15599 flp->name = fieldname;
15602 i = fip->nfnfields++;
15605 /* Create a new member function field and chain it to the field list
15607 new_fnfield = XNEW (struct nextfnfield);
15608 make_cleanup (xfree, new_fnfield);
15609 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15610 new_fnfield->next = flp->head;
15611 flp->head = new_fnfield;
15614 /* Fill in the member function field info. */
15615 fnp = &new_fnfield->fnfield;
15617 /* Delay processing of the physname until later. */
15618 if (cu->language == language_cplus)
15620 add_to_method_list (type, i, flp->length - 1, fieldname,
15625 const char *physname = dwarf2_physname (fieldname, die, cu);
15626 fnp->physname = physname ? physname : "";
15629 fnp->type = alloc_type (objfile);
15630 this_type = read_type_die (die, cu);
15631 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15633 int nparams = TYPE_NFIELDS (this_type);
15635 /* TYPE is the domain of this method, and THIS_TYPE is the type
15636 of the method itself (TYPE_CODE_METHOD). */
15637 smash_to_method_type (fnp->type, type,
15638 TYPE_TARGET_TYPE (this_type),
15639 TYPE_FIELDS (this_type),
15640 TYPE_NFIELDS (this_type),
15641 TYPE_VARARGS (this_type));
15643 /* Handle static member functions.
15644 Dwarf2 has no clean way to discern C++ static and non-static
15645 member functions. G++ helps GDB by marking the first
15646 parameter for non-static member functions (which is the this
15647 pointer) as artificial. We obtain this information from
15648 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15649 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15650 fnp->voffset = VOFFSET_STATIC;
15653 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15654 dwarf2_full_name (fieldname, die, cu));
15656 /* Get fcontext from DW_AT_containing_type if present. */
15657 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15658 fnp->fcontext = die_containing_type (die, cu);
15660 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15661 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15663 /* Get accessibility. */
15664 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15666 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15668 accessibility = dwarf2_default_access_attribute (die, cu);
15669 switch (accessibility)
15671 case DW_ACCESS_private:
15672 fnp->is_private = 1;
15674 case DW_ACCESS_protected:
15675 fnp->is_protected = 1;
15679 /* Check for artificial methods. */
15680 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15681 if (attr && DW_UNSND (attr) != 0)
15682 fnp->is_artificial = 1;
15684 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15686 /* Get index in virtual function table if it is a virtual member
15687 function. For older versions of GCC, this is an offset in the
15688 appropriate virtual table, as specified by DW_AT_containing_type.
15689 For everyone else, it is an expression to be evaluated relative
15690 to the object address. */
15692 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15695 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15697 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15699 /* Old-style GCC. */
15700 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15702 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15703 || (DW_BLOCK (attr)->size > 1
15704 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15705 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15707 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15708 if ((fnp->voffset % cu->header.addr_size) != 0)
15709 dwarf2_complex_location_expr_complaint ();
15711 fnp->voffset /= cu->header.addr_size;
15715 dwarf2_complex_location_expr_complaint ();
15717 if (!fnp->fcontext)
15719 /* If there is no `this' field and no DW_AT_containing_type,
15720 we cannot actually find a base class context for the
15722 if (TYPE_NFIELDS (this_type) == 0
15723 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15725 complaint (&symfile_complaints,
15726 _("cannot determine context for virtual member "
15727 "function \"%s\" (offset %d)"),
15728 fieldname, to_underlying (die->sect_off));
15733 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15737 else if (attr_form_is_section_offset (attr))
15739 dwarf2_complex_location_expr_complaint ();
15743 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15749 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15750 if (attr && DW_UNSND (attr))
15752 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15753 complaint (&symfile_complaints,
15754 _("Member function \"%s\" (offset %d) is virtual "
15755 "but the vtable offset is not specified"),
15756 fieldname, to_underlying (die->sect_off));
15757 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15758 TYPE_CPLUS_DYNAMIC (type) = 1;
15763 /* Create the vector of member function fields, and attach it to the type. */
15766 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15767 struct dwarf2_cu *cu)
15769 struct fnfieldlist *flp;
15772 if (cu->language == language_ada)
15773 error (_("unexpected member functions in Ada type"));
15775 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15776 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15777 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15779 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15781 struct nextfnfield *nfp = flp->head;
15782 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15785 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15786 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15787 fn_flp->fn_fields = (struct fn_field *)
15788 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15789 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15790 fn_flp->fn_fields[k] = nfp->fnfield;
15793 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15796 /* Returns non-zero if NAME is the name of a vtable member in CU's
15797 language, zero otherwise. */
15799 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15801 static const char vptr[] = "_vptr";
15803 /* Look for the C++ form of the vtable. */
15804 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15810 /* GCC outputs unnamed structures that are really pointers to member
15811 functions, with the ABI-specified layout. If TYPE describes
15812 such a structure, smash it into a member function type.
15814 GCC shouldn't do this; it should just output pointer to member DIEs.
15815 This is GCC PR debug/28767. */
15818 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15820 struct type *pfn_type, *self_type, *new_type;
15822 /* Check for a structure with no name and two children. */
15823 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15826 /* Check for __pfn and __delta members. */
15827 if (TYPE_FIELD_NAME (type, 0) == NULL
15828 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15829 || TYPE_FIELD_NAME (type, 1) == NULL
15830 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15833 /* Find the type of the method. */
15834 pfn_type = TYPE_FIELD_TYPE (type, 0);
15835 if (pfn_type == NULL
15836 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15837 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15840 /* Look for the "this" argument. */
15841 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15842 if (TYPE_NFIELDS (pfn_type) == 0
15843 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15844 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15847 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15848 new_type = alloc_type (objfile);
15849 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15850 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15851 TYPE_VARARGS (pfn_type));
15852 smash_to_methodptr_type (type, new_type);
15856 /* Called when we find the DIE that starts a structure or union scope
15857 (definition) to create a type for the structure or union. Fill in
15858 the type's name and general properties; the members will not be
15859 processed until process_structure_scope. A symbol table entry for
15860 the type will also not be done until process_structure_scope (assuming
15861 the type has a name).
15863 NOTE: we need to call these functions regardless of whether or not the
15864 DIE has a DW_AT_name attribute, since it might be an anonymous
15865 structure or union. This gets the type entered into our set of
15866 user defined types. */
15868 static struct type *
15869 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15873 struct attribute *attr;
15876 /* If the definition of this type lives in .debug_types, read that type.
15877 Don't follow DW_AT_specification though, that will take us back up
15878 the chain and we want to go down. */
15879 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15882 type = get_DW_AT_signature_type (die, attr, cu);
15884 /* The type's CU may not be the same as CU.
15885 Ensure TYPE is recorded with CU in die_type_hash. */
15886 return set_die_type (die, type, cu);
15889 type = alloc_type (objfile);
15890 INIT_CPLUS_SPECIFIC (type);
15892 name = dwarf2_name (die, cu);
15895 if (cu->language == language_cplus
15896 || cu->language == language_d
15897 || cu->language == language_rust)
15899 const char *full_name = dwarf2_full_name (name, die, cu);
15901 /* dwarf2_full_name might have already finished building the DIE's
15902 type. If so, there is no need to continue. */
15903 if (get_die_type (die, cu) != NULL)
15904 return get_die_type (die, cu);
15906 TYPE_TAG_NAME (type) = full_name;
15907 if (die->tag == DW_TAG_structure_type
15908 || die->tag == DW_TAG_class_type)
15909 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15913 /* The name is already allocated along with this objfile, so
15914 we don't need to duplicate it for the type. */
15915 TYPE_TAG_NAME (type) = name;
15916 if (die->tag == DW_TAG_class_type)
15917 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15921 if (die->tag == DW_TAG_structure_type)
15923 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15925 else if (die->tag == DW_TAG_union_type)
15927 TYPE_CODE (type) = TYPE_CODE_UNION;
15931 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15934 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15935 TYPE_DECLARED_CLASS (type) = 1;
15937 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15940 if (attr_form_is_constant (attr))
15941 TYPE_LENGTH (type) = DW_UNSND (attr);
15944 /* For the moment, dynamic type sizes are not supported
15945 by GDB's struct type. The actual size is determined
15946 on-demand when resolving the type of a given object,
15947 so set the type's length to zero for now. Otherwise,
15948 we record an expression as the length, and that expression
15949 could lead to a very large value, which could eventually
15950 lead to us trying to allocate that much memory when creating
15951 a value of that type. */
15952 TYPE_LENGTH (type) = 0;
15957 TYPE_LENGTH (type) = 0;
15960 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15962 /* ICC<14 does not output the required DW_AT_declaration on
15963 incomplete types, but gives them a size of zero. */
15964 TYPE_STUB (type) = 1;
15967 TYPE_STUB_SUPPORTED (type) = 1;
15969 if (die_is_declaration (die, cu))
15970 TYPE_STUB (type) = 1;
15971 else if (attr == NULL && die->child == NULL
15972 && producer_is_realview (cu->producer))
15973 /* RealView does not output the required DW_AT_declaration
15974 on incomplete types. */
15975 TYPE_STUB (type) = 1;
15977 /* We need to add the type field to the die immediately so we don't
15978 infinitely recurse when dealing with pointers to the structure
15979 type within the structure itself. */
15980 set_die_type (die, type, cu);
15982 /* set_die_type should be already done. */
15983 set_descriptive_type (type, die, cu);
15988 /* Finish creating a structure or union type, including filling in
15989 its members and creating a symbol for it. */
15992 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15995 struct die_info *child_die;
15998 type = get_die_type (die, cu);
16000 type = read_structure_type (die, cu);
16002 if (die->child != NULL && ! die_is_declaration (die, cu))
16004 struct field_info fi;
16005 std::vector<struct symbol *> template_args;
16006 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
16008 memset (&fi, 0, sizeof (struct field_info));
16010 child_die = die->child;
16012 while (child_die && child_die->tag)
16014 if (child_die->tag == DW_TAG_member
16015 || child_die->tag == DW_TAG_variable)
16017 /* NOTE: carlton/2002-11-05: A C++ static data member
16018 should be a DW_TAG_member that is a declaration, but
16019 all versions of G++ as of this writing (so through at
16020 least 3.2.1) incorrectly generate DW_TAG_variable
16021 tags for them instead. */
16022 dwarf2_add_field (&fi, child_die, cu);
16024 else if (child_die->tag == DW_TAG_subprogram)
16026 /* Rust doesn't have member functions in the C++ sense.
16027 However, it does emit ordinary functions as children
16028 of a struct DIE. */
16029 if (cu->language == language_rust)
16030 read_func_scope (child_die, cu);
16033 /* C++ member function. */
16034 dwarf2_add_member_fn (&fi, child_die, type, cu);
16037 else if (child_die->tag == DW_TAG_inheritance)
16039 /* C++ base class field. */
16040 dwarf2_add_field (&fi, child_die, cu);
16042 else if (type_can_define_types (child_die))
16043 dwarf2_add_type_defn (&fi, child_die, cu);
16044 else if (child_die->tag == DW_TAG_template_type_param
16045 || child_die->tag == DW_TAG_template_value_param)
16047 struct symbol *arg = new_symbol (child_die, NULL, cu);
16050 template_args.push_back (arg);
16053 child_die = sibling_die (child_die);
16056 /* Attach template arguments to type. */
16057 if (!template_args.empty ())
16059 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16060 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16061 TYPE_TEMPLATE_ARGUMENTS (type)
16062 = XOBNEWVEC (&objfile->objfile_obstack,
16064 TYPE_N_TEMPLATE_ARGUMENTS (type));
16065 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16066 template_args.data (),
16067 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16068 * sizeof (struct symbol *)));
16071 /* Attach fields and member functions to the type. */
16073 dwarf2_attach_fields_to_type (&fi, type, cu);
16076 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16078 /* Get the type which refers to the base class (possibly this
16079 class itself) which contains the vtable pointer for the current
16080 class from the DW_AT_containing_type attribute. This use of
16081 DW_AT_containing_type is a GNU extension. */
16083 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16085 struct type *t = die_containing_type (die, cu);
16087 set_type_vptr_basetype (type, t);
16092 /* Our own class provides vtbl ptr. */
16093 for (i = TYPE_NFIELDS (t) - 1;
16094 i >= TYPE_N_BASECLASSES (t);
16097 const char *fieldname = TYPE_FIELD_NAME (t, i);
16099 if (is_vtable_name (fieldname, cu))
16101 set_type_vptr_fieldno (type, i);
16106 /* Complain if virtual function table field not found. */
16107 if (i < TYPE_N_BASECLASSES (t))
16108 complaint (&symfile_complaints,
16109 _("virtual function table pointer "
16110 "not found when defining class '%s'"),
16111 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16116 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16119 else if (cu->producer
16120 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16122 /* The IBM XLC compiler does not provide direct indication
16123 of the containing type, but the vtable pointer is
16124 always named __vfp. */
16128 for (i = TYPE_NFIELDS (type) - 1;
16129 i >= TYPE_N_BASECLASSES (type);
16132 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16134 set_type_vptr_fieldno (type, i);
16135 set_type_vptr_basetype (type, type);
16142 /* Copy fi.typedef_field_list linked list elements content into the
16143 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16144 if (fi.typedef_field_list)
16146 int i = fi.typedef_field_list_count;
16148 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16149 TYPE_TYPEDEF_FIELD_ARRAY (type)
16150 = ((struct decl_field *)
16151 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16152 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16154 /* Reverse the list order to keep the debug info elements order. */
16157 struct decl_field *dest, *src;
16159 dest = &TYPE_TYPEDEF_FIELD (type, i);
16160 src = &fi.typedef_field_list->field;
16161 fi.typedef_field_list = fi.typedef_field_list->next;
16166 /* Copy fi.nested_types_list linked list elements content into the
16167 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16168 if (fi.nested_types_list != NULL && cu->language != language_ada)
16170 int i = fi.nested_types_list_count;
16172 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16173 TYPE_NESTED_TYPES_ARRAY (type)
16174 = ((struct decl_field *)
16175 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16176 TYPE_NESTED_TYPES_COUNT (type) = i;
16178 /* Reverse the list order to keep the debug info elements order. */
16181 struct decl_field *dest, *src;
16183 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16184 src = &fi.nested_types_list->field;
16185 fi.nested_types_list = fi.nested_types_list->next;
16190 do_cleanups (back_to);
16193 quirk_gcc_member_function_pointer (type, objfile);
16195 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16196 snapshots) has been known to create a die giving a declaration
16197 for a class that has, as a child, a die giving a definition for a
16198 nested class. So we have to process our children even if the
16199 current die is a declaration. Normally, of course, a declaration
16200 won't have any children at all. */
16202 child_die = die->child;
16204 while (child_die != NULL && child_die->tag)
16206 if (child_die->tag == DW_TAG_member
16207 || child_die->tag == DW_TAG_variable
16208 || child_die->tag == DW_TAG_inheritance
16209 || child_die->tag == DW_TAG_template_value_param
16210 || child_die->tag == DW_TAG_template_type_param)
16215 process_die (child_die, cu);
16217 child_die = sibling_die (child_die);
16220 /* Do not consider external references. According to the DWARF standard,
16221 these DIEs are identified by the fact that they have no byte_size
16222 attribute, and a declaration attribute. */
16223 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16224 || !die_is_declaration (die, cu))
16225 new_symbol (die, type, cu);
16228 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16229 update TYPE using some information only available in DIE's children. */
16232 update_enumeration_type_from_children (struct die_info *die,
16234 struct dwarf2_cu *cu)
16236 struct die_info *child_die;
16237 int unsigned_enum = 1;
16241 auto_obstack obstack;
16243 for (child_die = die->child;
16244 child_die != NULL && child_die->tag;
16245 child_die = sibling_die (child_die))
16247 struct attribute *attr;
16249 const gdb_byte *bytes;
16250 struct dwarf2_locexpr_baton *baton;
16253 if (child_die->tag != DW_TAG_enumerator)
16256 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16260 name = dwarf2_name (child_die, cu);
16262 name = "<anonymous enumerator>";
16264 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16265 &value, &bytes, &baton);
16271 else if ((mask & value) != 0)
16276 /* If we already know that the enum type is neither unsigned, nor
16277 a flag type, no need to look at the rest of the enumerates. */
16278 if (!unsigned_enum && !flag_enum)
16283 TYPE_UNSIGNED (type) = 1;
16285 TYPE_FLAG_ENUM (type) = 1;
16288 /* Given a DW_AT_enumeration_type die, set its type. We do not
16289 complete the type's fields yet, or create any symbols. */
16291 static struct type *
16292 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16294 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16296 struct attribute *attr;
16299 /* If the definition of this type lives in .debug_types, read that type.
16300 Don't follow DW_AT_specification though, that will take us back up
16301 the chain and we want to go down. */
16302 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16305 type = get_DW_AT_signature_type (die, attr, cu);
16307 /* The type's CU may not be the same as CU.
16308 Ensure TYPE is recorded with CU in die_type_hash. */
16309 return set_die_type (die, type, cu);
16312 type = alloc_type (objfile);
16314 TYPE_CODE (type) = TYPE_CODE_ENUM;
16315 name = dwarf2_full_name (NULL, die, cu);
16317 TYPE_TAG_NAME (type) = name;
16319 attr = dwarf2_attr (die, DW_AT_type, cu);
16322 struct type *underlying_type = die_type (die, cu);
16324 TYPE_TARGET_TYPE (type) = underlying_type;
16327 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16330 TYPE_LENGTH (type) = DW_UNSND (attr);
16334 TYPE_LENGTH (type) = 0;
16337 /* The enumeration DIE can be incomplete. In Ada, any type can be
16338 declared as private in the package spec, and then defined only
16339 inside the package body. Such types are known as Taft Amendment
16340 Types. When another package uses such a type, an incomplete DIE
16341 may be generated by the compiler. */
16342 if (die_is_declaration (die, cu))
16343 TYPE_STUB (type) = 1;
16345 /* Finish the creation of this type by using the enum's children.
16346 We must call this even when the underlying type has been provided
16347 so that we can determine if we're looking at a "flag" enum. */
16348 update_enumeration_type_from_children (die, type, cu);
16350 /* If this type has an underlying type that is not a stub, then we
16351 may use its attributes. We always use the "unsigned" attribute
16352 in this situation, because ordinarily we guess whether the type
16353 is unsigned -- but the guess can be wrong and the underlying type
16354 can tell us the reality. However, we defer to a local size
16355 attribute if one exists, because this lets the compiler override
16356 the underlying type if needed. */
16357 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16359 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16360 if (TYPE_LENGTH (type) == 0)
16361 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16364 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16366 return set_die_type (die, type, cu);
16369 /* Given a pointer to a die which begins an enumeration, process all
16370 the dies that define the members of the enumeration, and create the
16371 symbol for the enumeration type.
16373 NOTE: We reverse the order of the element list. */
16376 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16378 struct type *this_type;
16380 this_type = get_die_type (die, cu);
16381 if (this_type == NULL)
16382 this_type = read_enumeration_type (die, cu);
16384 if (die->child != NULL)
16386 struct die_info *child_die;
16387 struct symbol *sym;
16388 struct field *fields = NULL;
16389 int num_fields = 0;
16392 child_die = die->child;
16393 while (child_die && child_die->tag)
16395 if (child_die->tag != DW_TAG_enumerator)
16397 process_die (child_die, cu);
16401 name = dwarf2_name (child_die, cu);
16404 sym = new_symbol (child_die, this_type, cu);
16406 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16408 fields = (struct field *)
16410 (num_fields + DW_FIELD_ALLOC_CHUNK)
16411 * sizeof (struct field));
16414 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16415 FIELD_TYPE (fields[num_fields]) = NULL;
16416 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16417 FIELD_BITSIZE (fields[num_fields]) = 0;
16423 child_die = sibling_die (child_die);
16428 TYPE_NFIELDS (this_type) = num_fields;
16429 TYPE_FIELDS (this_type) = (struct field *)
16430 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16431 memcpy (TYPE_FIELDS (this_type), fields,
16432 sizeof (struct field) * num_fields);
16437 /* If we are reading an enum from a .debug_types unit, and the enum
16438 is a declaration, and the enum is not the signatured type in the
16439 unit, then we do not want to add a symbol for it. Adding a
16440 symbol would in some cases obscure the true definition of the
16441 enum, giving users an incomplete type when the definition is
16442 actually available. Note that we do not want to do this for all
16443 enums which are just declarations, because C++0x allows forward
16444 enum declarations. */
16445 if (cu->per_cu->is_debug_types
16446 && die_is_declaration (die, cu))
16448 struct signatured_type *sig_type;
16450 sig_type = (struct signatured_type *) cu->per_cu;
16451 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16452 if (sig_type->type_offset_in_section != die->sect_off)
16456 new_symbol (die, this_type, cu);
16459 /* Extract all information from a DW_TAG_array_type DIE and put it in
16460 the DIE's type field. For now, this only handles one dimensional
16463 static struct type *
16464 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16466 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16467 struct die_info *child_die;
16469 struct type *element_type, *range_type, *index_type;
16470 struct attribute *attr;
16472 struct dynamic_prop *byte_stride_prop = NULL;
16473 unsigned int bit_stride = 0;
16475 element_type = die_type (die, cu);
16477 /* The die_type call above may have already set the type for this DIE. */
16478 type = get_die_type (die, cu);
16482 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16488 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16489 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16492 complaint (&symfile_complaints,
16493 _("unable to read array DW_AT_byte_stride "
16494 " - DIE at 0x%x [in module %s]"),
16495 to_underlying (die->sect_off),
16496 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16497 /* Ignore this attribute. We will likely not be able to print
16498 arrays of this type correctly, but there is little we can do
16499 to help if we cannot read the attribute's value. */
16500 byte_stride_prop = NULL;
16504 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16506 bit_stride = DW_UNSND (attr);
16508 /* Irix 6.2 native cc creates array types without children for
16509 arrays with unspecified length. */
16510 if (die->child == NULL)
16512 index_type = objfile_type (objfile)->builtin_int;
16513 range_type = create_static_range_type (NULL, index_type, 0, -1);
16514 type = create_array_type_with_stride (NULL, element_type, range_type,
16515 byte_stride_prop, bit_stride);
16516 return set_die_type (die, type, cu);
16519 std::vector<struct type *> range_types;
16520 child_die = die->child;
16521 while (child_die && child_die->tag)
16523 if (child_die->tag == DW_TAG_subrange_type)
16525 struct type *child_type = read_type_die (child_die, cu);
16527 if (child_type != NULL)
16529 /* The range type was succesfully read. Save it for the
16530 array type creation. */
16531 range_types.push_back (child_type);
16534 child_die = sibling_die (child_die);
16537 /* Dwarf2 dimensions are output from left to right, create the
16538 necessary array types in backwards order. */
16540 type = element_type;
16542 if (read_array_order (die, cu) == DW_ORD_col_major)
16546 while (i < range_types.size ())
16547 type = create_array_type_with_stride (NULL, type, range_types[i++],
16548 byte_stride_prop, bit_stride);
16552 size_t ndim = range_types.size ();
16554 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16555 byte_stride_prop, bit_stride);
16558 /* Understand Dwarf2 support for vector types (like they occur on
16559 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16560 array type. This is not part of the Dwarf2/3 standard yet, but a
16561 custom vendor extension. The main difference between a regular
16562 array and the vector variant is that vectors are passed by value
16564 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16566 make_vector_type (type);
16568 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16569 implementation may choose to implement triple vectors using this
16571 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16574 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16575 TYPE_LENGTH (type) = DW_UNSND (attr);
16577 complaint (&symfile_complaints,
16578 _("DW_AT_byte_size for array type smaller "
16579 "than the total size of elements"));
16582 name = dwarf2_name (die, cu);
16584 TYPE_NAME (type) = name;
16586 /* Install the type in the die. */
16587 set_die_type (die, type, cu);
16589 /* set_die_type should be already done. */
16590 set_descriptive_type (type, die, cu);
16595 static enum dwarf_array_dim_ordering
16596 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16598 struct attribute *attr;
16600 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16603 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16605 /* GNU F77 is a special case, as at 08/2004 array type info is the
16606 opposite order to the dwarf2 specification, but data is still
16607 laid out as per normal fortran.
16609 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16610 version checking. */
16612 if (cu->language == language_fortran
16613 && cu->producer && strstr (cu->producer, "GNU F77"))
16615 return DW_ORD_row_major;
16618 switch (cu->language_defn->la_array_ordering)
16620 case array_column_major:
16621 return DW_ORD_col_major;
16622 case array_row_major:
16624 return DW_ORD_row_major;
16628 /* Extract all information from a DW_TAG_set_type DIE and put it in
16629 the DIE's type field. */
16631 static struct type *
16632 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16634 struct type *domain_type, *set_type;
16635 struct attribute *attr;
16637 domain_type = die_type (die, cu);
16639 /* The die_type call above may have already set the type for this DIE. */
16640 set_type = get_die_type (die, cu);
16644 set_type = create_set_type (NULL, domain_type);
16646 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16648 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16650 return set_die_type (die, set_type, cu);
16653 /* A helper for read_common_block that creates a locexpr baton.
16654 SYM is the symbol which we are marking as computed.
16655 COMMON_DIE is the DIE for the common block.
16656 COMMON_LOC is the location expression attribute for the common
16658 MEMBER_LOC is the location expression attribute for the particular
16659 member of the common block that we are processing.
16660 CU is the CU from which the above come. */
16663 mark_common_block_symbol_computed (struct symbol *sym,
16664 struct die_info *common_die,
16665 struct attribute *common_loc,
16666 struct attribute *member_loc,
16667 struct dwarf2_cu *cu)
16669 struct dwarf2_per_objfile *dwarf2_per_objfile
16670 = cu->per_cu->dwarf2_per_objfile;
16671 struct objfile *objfile = dwarf2_per_objfile->objfile;
16672 struct dwarf2_locexpr_baton *baton;
16674 unsigned int cu_off;
16675 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16676 LONGEST offset = 0;
16678 gdb_assert (common_loc && member_loc);
16679 gdb_assert (attr_form_is_block (common_loc));
16680 gdb_assert (attr_form_is_block (member_loc)
16681 || attr_form_is_constant (member_loc));
16683 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16684 baton->per_cu = cu->per_cu;
16685 gdb_assert (baton->per_cu);
16687 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16689 if (attr_form_is_constant (member_loc))
16691 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16692 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16695 baton->size += DW_BLOCK (member_loc)->size;
16697 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16700 *ptr++ = DW_OP_call4;
16701 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16702 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16705 if (attr_form_is_constant (member_loc))
16707 *ptr++ = DW_OP_addr;
16708 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16709 ptr += cu->header.addr_size;
16713 /* We have to copy the data here, because DW_OP_call4 will only
16714 use a DW_AT_location attribute. */
16715 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16716 ptr += DW_BLOCK (member_loc)->size;
16719 *ptr++ = DW_OP_plus;
16720 gdb_assert (ptr - baton->data == baton->size);
16722 SYMBOL_LOCATION_BATON (sym) = baton;
16723 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16726 /* Create appropriate locally-scoped variables for all the
16727 DW_TAG_common_block entries. Also create a struct common_block
16728 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16729 is used to sepate the common blocks name namespace from regular
16733 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16735 struct attribute *attr;
16737 attr = dwarf2_attr (die, DW_AT_location, cu);
16740 /* Support the .debug_loc offsets. */
16741 if (attr_form_is_block (attr))
16745 else if (attr_form_is_section_offset (attr))
16747 dwarf2_complex_location_expr_complaint ();
16752 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16753 "common block member");
16758 if (die->child != NULL)
16760 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16761 struct die_info *child_die;
16762 size_t n_entries = 0, size;
16763 struct common_block *common_block;
16764 struct symbol *sym;
16766 for (child_die = die->child;
16767 child_die && child_die->tag;
16768 child_die = sibling_die (child_die))
16771 size = (sizeof (struct common_block)
16772 + (n_entries - 1) * sizeof (struct symbol *));
16774 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16776 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16777 common_block->n_entries = 0;
16779 for (child_die = die->child;
16780 child_die && child_die->tag;
16781 child_die = sibling_die (child_die))
16783 /* Create the symbol in the DW_TAG_common_block block in the current
16785 sym = new_symbol (child_die, NULL, cu);
16788 struct attribute *member_loc;
16790 common_block->contents[common_block->n_entries++] = sym;
16792 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16796 /* GDB has handled this for a long time, but it is
16797 not specified by DWARF. It seems to have been
16798 emitted by gfortran at least as recently as:
16799 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16800 complaint (&symfile_complaints,
16801 _("Variable in common block has "
16802 "DW_AT_data_member_location "
16803 "- DIE at 0x%x [in module %s]"),
16804 to_underlying (child_die->sect_off),
16805 objfile_name (objfile));
16807 if (attr_form_is_section_offset (member_loc))
16808 dwarf2_complex_location_expr_complaint ();
16809 else if (attr_form_is_constant (member_loc)
16810 || attr_form_is_block (member_loc))
16813 mark_common_block_symbol_computed (sym, die, attr,
16817 dwarf2_complex_location_expr_complaint ();
16822 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16823 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16827 /* Create a type for a C++ namespace. */
16829 static struct type *
16830 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16833 const char *previous_prefix, *name;
16837 /* For extensions, reuse the type of the original namespace. */
16838 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16840 struct die_info *ext_die;
16841 struct dwarf2_cu *ext_cu = cu;
16843 ext_die = dwarf2_extension (die, &ext_cu);
16844 type = read_type_die (ext_die, ext_cu);
16846 /* EXT_CU may not be the same as CU.
16847 Ensure TYPE is recorded with CU in die_type_hash. */
16848 return set_die_type (die, type, cu);
16851 name = namespace_name (die, &is_anonymous, cu);
16853 /* Now build the name of the current namespace. */
16855 previous_prefix = determine_prefix (die, cu);
16856 if (previous_prefix[0] != '\0')
16857 name = typename_concat (&objfile->objfile_obstack,
16858 previous_prefix, name, 0, cu);
16860 /* Create the type. */
16861 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16862 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16864 return set_die_type (die, type, cu);
16867 /* Read a namespace scope. */
16870 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16872 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16875 /* Add a symbol associated to this if we haven't seen the namespace
16876 before. Also, add a using directive if it's an anonymous
16879 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16883 type = read_type_die (die, cu);
16884 new_symbol (die, type, cu);
16886 namespace_name (die, &is_anonymous, cu);
16889 const char *previous_prefix = determine_prefix (die, cu);
16891 std::vector<const char *> excludes;
16892 add_using_directive (using_directives (cu->language),
16893 previous_prefix, TYPE_NAME (type), NULL,
16894 NULL, excludes, 0, &objfile->objfile_obstack);
16898 if (die->child != NULL)
16900 struct die_info *child_die = die->child;
16902 while (child_die && child_die->tag)
16904 process_die (child_die, cu);
16905 child_die = sibling_die (child_die);
16910 /* Read a Fortran module as type. This DIE can be only a declaration used for
16911 imported module. Still we need that type as local Fortran "use ... only"
16912 declaration imports depend on the created type in determine_prefix. */
16914 static struct type *
16915 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16917 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16918 const char *module_name;
16921 module_name = dwarf2_name (die, cu);
16923 complaint (&symfile_complaints,
16924 _("DW_TAG_module has no name, offset 0x%x"),
16925 to_underlying (die->sect_off));
16926 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16928 /* determine_prefix uses TYPE_TAG_NAME. */
16929 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16931 return set_die_type (die, type, cu);
16934 /* Read a Fortran module. */
16937 read_module (struct die_info *die, struct dwarf2_cu *cu)
16939 struct die_info *child_die = die->child;
16942 type = read_type_die (die, cu);
16943 new_symbol (die, type, cu);
16945 while (child_die && child_die->tag)
16947 process_die (child_die, cu);
16948 child_die = sibling_die (child_die);
16952 /* Return the name of the namespace represented by DIE. Set
16953 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16956 static const char *
16957 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16959 struct die_info *current_die;
16960 const char *name = NULL;
16962 /* Loop through the extensions until we find a name. */
16964 for (current_die = die;
16965 current_die != NULL;
16966 current_die = dwarf2_extension (die, &cu))
16968 /* We don't use dwarf2_name here so that we can detect the absence
16969 of a name -> anonymous namespace. */
16970 name = dwarf2_string_attr (die, DW_AT_name, cu);
16976 /* Is it an anonymous namespace? */
16978 *is_anonymous = (name == NULL);
16980 name = CP_ANONYMOUS_NAMESPACE_STR;
16985 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16986 the user defined type vector. */
16988 static struct type *
16989 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16991 struct gdbarch *gdbarch
16992 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16993 struct comp_unit_head *cu_header = &cu->header;
16995 struct attribute *attr_byte_size;
16996 struct attribute *attr_address_class;
16997 int byte_size, addr_class;
16998 struct type *target_type;
17000 target_type = die_type (die, cu);
17002 /* The die_type call above may have already set the type for this DIE. */
17003 type = get_die_type (die, cu);
17007 type = lookup_pointer_type (target_type);
17009 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17010 if (attr_byte_size)
17011 byte_size = DW_UNSND (attr_byte_size);
17013 byte_size = cu_header->addr_size;
17015 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17016 if (attr_address_class)
17017 addr_class = DW_UNSND (attr_address_class);
17019 addr_class = DW_ADDR_none;
17021 /* If the pointer size or address class is different than the
17022 default, create a type variant marked as such and set the
17023 length accordingly. */
17024 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17026 if (gdbarch_address_class_type_flags_p (gdbarch))
17030 type_flags = gdbarch_address_class_type_flags
17031 (gdbarch, byte_size, addr_class);
17032 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17034 type = make_type_with_address_space (type, type_flags);
17036 else if (TYPE_LENGTH (type) != byte_size)
17038 complaint (&symfile_complaints,
17039 _("invalid pointer size %d"), byte_size);
17043 /* Should we also complain about unhandled address classes? */
17047 TYPE_LENGTH (type) = byte_size;
17048 return set_die_type (die, type, cu);
17051 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17052 the user defined type vector. */
17054 static struct type *
17055 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17058 struct type *to_type;
17059 struct type *domain;
17061 to_type = die_type (die, cu);
17062 domain = die_containing_type (die, cu);
17064 /* The calls above may have already set the type for this DIE. */
17065 type = get_die_type (die, cu);
17069 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17070 type = lookup_methodptr_type (to_type);
17071 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17073 struct type *new_type
17074 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17076 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17077 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17078 TYPE_VARARGS (to_type));
17079 type = lookup_methodptr_type (new_type);
17082 type = lookup_memberptr_type (to_type, domain);
17084 return set_die_type (die, type, cu);
17087 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17088 the user defined type vector. */
17090 static struct type *
17091 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17092 enum type_code refcode)
17094 struct comp_unit_head *cu_header = &cu->header;
17095 struct type *type, *target_type;
17096 struct attribute *attr;
17098 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17100 target_type = die_type (die, cu);
17102 /* The die_type call above may have already set the type for this DIE. */
17103 type = get_die_type (die, cu);
17107 type = lookup_reference_type (target_type, refcode);
17108 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17111 TYPE_LENGTH (type) = DW_UNSND (attr);
17115 TYPE_LENGTH (type) = cu_header->addr_size;
17117 return set_die_type (die, type, cu);
17120 /* Add the given cv-qualifiers to the element type of the array. GCC
17121 outputs DWARF type qualifiers that apply to an array, not the
17122 element type. But GDB relies on the array element type to carry
17123 the cv-qualifiers. This mimics section 6.7.3 of the C99
17126 static struct type *
17127 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17128 struct type *base_type, int cnst, int voltl)
17130 struct type *el_type, *inner_array;
17132 base_type = copy_type (base_type);
17133 inner_array = base_type;
17135 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17137 TYPE_TARGET_TYPE (inner_array) =
17138 copy_type (TYPE_TARGET_TYPE (inner_array));
17139 inner_array = TYPE_TARGET_TYPE (inner_array);
17142 el_type = TYPE_TARGET_TYPE (inner_array);
17143 cnst |= TYPE_CONST (el_type);
17144 voltl |= TYPE_VOLATILE (el_type);
17145 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17147 return set_die_type (die, base_type, cu);
17150 static struct type *
17151 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17153 struct type *base_type, *cv_type;
17155 base_type = die_type (die, cu);
17157 /* The die_type call above may have already set the type for this DIE. */
17158 cv_type = get_die_type (die, cu);
17162 /* In case the const qualifier is applied to an array type, the element type
17163 is so qualified, not the array type (section 6.7.3 of C99). */
17164 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17165 return add_array_cv_type (die, cu, base_type, 1, 0);
17167 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17168 return set_die_type (die, cv_type, cu);
17171 static struct type *
17172 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17174 struct type *base_type, *cv_type;
17176 base_type = die_type (die, cu);
17178 /* The die_type call above may have already set the type for this DIE. */
17179 cv_type = get_die_type (die, cu);
17183 /* In case the volatile qualifier is applied to an array type, the
17184 element type is so qualified, not the array type (section 6.7.3
17186 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17187 return add_array_cv_type (die, cu, base_type, 0, 1);
17189 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17190 return set_die_type (die, cv_type, cu);
17193 /* Handle DW_TAG_restrict_type. */
17195 static struct type *
17196 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17198 struct type *base_type, *cv_type;
17200 base_type = die_type (die, cu);
17202 /* The die_type call above may have already set the type for this DIE. */
17203 cv_type = get_die_type (die, cu);
17207 cv_type = make_restrict_type (base_type);
17208 return set_die_type (die, cv_type, cu);
17211 /* Handle DW_TAG_atomic_type. */
17213 static struct type *
17214 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17216 struct type *base_type, *cv_type;
17218 base_type = die_type (die, cu);
17220 /* The die_type call above may have already set the type for this DIE. */
17221 cv_type = get_die_type (die, cu);
17225 cv_type = make_atomic_type (base_type);
17226 return set_die_type (die, cv_type, cu);
17229 /* Extract all information from a DW_TAG_string_type DIE and add to
17230 the user defined type vector. It isn't really a user defined type,
17231 but it behaves like one, with other DIE's using an AT_user_def_type
17232 attribute to reference it. */
17234 static struct type *
17235 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17237 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17238 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17239 struct type *type, *range_type, *index_type, *char_type;
17240 struct attribute *attr;
17241 unsigned int length;
17243 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17246 length = DW_UNSND (attr);
17250 /* Check for the DW_AT_byte_size attribute. */
17251 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17254 length = DW_UNSND (attr);
17262 index_type = objfile_type (objfile)->builtin_int;
17263 range_type = create_static_range_type (NULL, index_type, 1, length);
17264 char_type = language_string_char_type (cu->language_defn, gdbarch);
17265 type = create_string_type (NULL, char_type, range_type);
17267 return set_die_type (die, type, cu);
17270 /* Assuming that DIE corresponds to a function, returns nonzero
17271 if the function is prototyped. */
17274 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17276 struct attribute *attr;
17278 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17279 if (attr && (DW_UNSND (attr) != 0))
17282 /* The DWARF standard implies that the DW_AT_prototyped attribute
17283 is only meaninful for C, but the concept also extends to other
17284 languages that allow unprototyped functions (Eg: Objective C).
17285 For all other languages, assume that functions are always
17287 if (cu->language != language_c
17288 && cu->language != language_objc
17289 && cu->language != language_opencl)
17292 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17293 prototyped and unprototyped functions; default to prototyped,
17294 since that is more common in modern code (and RealView warns
17295 about unprototyped functions). */
17296 if (producer_is_realview (cu->producer))
17302 /* Handle DIES due to C code like:
17306 int (*funcp)(int a, long l);
17310 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17312 static struct type *
17313 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17315 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17316 struct type *type; /* Type that this function returns. */
17317 struct type *ftype; /* Function that returns above type. */
17318 struct attribute *attr;
17320 type = die_type (die, cu);
17322 /* The die_type call above may have already set the type for this DIE. */
17323 ftype = get_die_type (die, cu);
17327 ftype = lookup_function_type (type);
17329 if (prototyped_function_p (die, cu))
17330 TYPE_PROTOTYPED (ftype) = 1;
17332 /* Store the calling convention in the type if it's available in
17333 the subroutine die. Otherwise set the calling convention to
17334 the default value DW_CC_normal. */
17335 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17337 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17338 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17339 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17341 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17343 /* Record whether the function returns normally to its caller or not
17344 if the DWARF producer set that information. */
17345 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17346 if (attr && (DW_UNSND (attr) != 0))
17347 TYPE_NO_RETURN (ftype) = 1;
17349 /* We need to add the subroutine type to the die immediately so
17350 we don't infinitely recurse when dealing with parameters
17351 declared as the same subroutine type. */
17352 set_die_type (die, ftype, cu);
17354 if (die->child != NULL)
17356 struct type *void_type = objfile_type (objfile)->builtin_void;
17357 struct die_info *child_die;
17358 int nparams, iparams;
17360 /* Count the number of parameters.
17361 FIXME: GDB currently ignores vararg functions, but knows about
17362 vararg member functions. */
17364 child_die = die->child;
17365 while (child_die && child_die->tag)
17367 if (child_die->tag == DW_TAG_formal_parameter)
17369 else if (child_die->tag == DW_TAG_unspecified_parameters)
17370 TYPE_VARARGS (ftype) = 1;
17371 child_die = sibling_die (child_die);
17374 /* Allocate storage for parameters and fill them in. */
17375 TYPE_NFIELDS (ftype) = nparams;
17376 TYPE_FIELDS (ftype) = (struct field *)
17377 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17379 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17380 even if we error out during the parameters reading below. */
17381 for (iparams = 0; iparams < nparams; iparams++)
17382 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17385 child_die = die->child;
17386 while (child_die && child_die->tag)
17388 if (child_die->tag == DW_TAG_formal_parameter)
17390 struct type *arg_type;
17392 /* DWARF version 2 has no clean way to discern C++
17393 static and non-static member functions. G++ helps
17394 GDB by marking the first parameter for non-static
17395 member functions (which is the this pointer) as
17396 artificial. We pass this information to
17397 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17399 DWARF version 3 added DW_AT_object_pointer, which GCC
17400 4.5 does not yet generate. */
17401 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17403 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17405 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17406 arg_type = die_type (child_die, cu);
17408 /* RealView does not mark THIS as const, which the testsuite
17409 expects. GCC marks THIS as const in method definitions,
17410 but not in the class specifications (GCC PR 43053). */
17411 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17412 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17415 struct dwarf2_cu *arg_cu = cu;
17416 const char *name = dwarf2_name (child_die, cu);
17418 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17421 /* If the compiler emits this, use it. */
17422 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17425 else if (name && strcmp (name, "this") == 0)
17426 /* Function definitions will have the argument names. */
17428 else if (name == NULL && iparams == 0)
17429 /* Declarations may not have the names, so like
17430 elsewhere in GDB, assume an artificial first
17431 argument is "this". */
17435 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17439 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17442 child_die = sibling_die (child_die);
17449 static struct type *
17450 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17453 const char *name = NULL;
17454 struct type *this_type, *target_type;
17456 name = dwarf2_full_name (NULL, die, cu);
17457 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17458 TYPE_TARGET_STUB (this_type) = 1;
17459 set_die_type (die, this_type, cu);
17460 target_type = die_type (die, cu);
17461 if (target_type != this_type)
17462 TYPE_TARGET_TYPE (this_type) = target_type;
17465 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17466 spec and cause infinite loops in GDB. */
17467 complaint (&symfile_complaints,
17468 _("Self-referential DW_TAG_typedef "
17469 "- DIE at 0x%x [in module %s]"),
17470 to_underlying (die->sect_off), objfile_name (objfile));
17471 TYPE_TARGET_TYPE (this_type) = NULL;
17476 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17477 (which may be different from NAME) to the architecture back-end to allow
17478 it to guess the correct format if necessary. */
17480 static struct type *
17481 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17482 const char *name_hint)
17484 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17485 const struct floatformat **format;
17488 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17490 type = init_float_type (objfile, bits, name, format);
17492 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17497 /* Find a representation of a given base type and install
17498 it in the TYPE field of the die. */
17500 static struct type *
17501 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17503 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17505 struct attribute *attr;
17506 int encoding = 0, bits = 0;
17509 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17512 encoding = DW_UNSND (attr);
17514 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17517 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17519 name = dwarf2_name (die, cu);
17522 complaint (&symfile_complaints,
17523 _("DW_AT_name missing from DW_TAG_base_type"));
17528 case DW_ATE_address:
17529 /* Turn DW_ATE_address into a void * pointer. */
17530 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17531 type = init_pointer_type (objfile, bits, name, type);
17533 case DW_ATE_boolean:
17534 type = init_boolean_type (objfile, bits, 1, name);
17536 case DW_ATE_complex_float:
17537 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17538 type = init_complex_type (objfile, name, type);
17540 case DW_ATE_decimal_float:
17541 type = init_decfloat_type (objfile, bits, name);
17544 type = dwarf2_init_float_type (objfile, bits, name, name);
17546 case DW_ATE_signed:
17547 type = init_integer_type (objfile, bits, 0, name);
17549 case DW_ATE_unsigned:
17550 if (cu->language == language_fortran
17552 && startswith (name, "character("))
17553 type = init_character_type (objfile, bits, 1, name);
17555 type = init_integer_type (objfile, bits, 1, name);
17557 case DW_ATE_signed_char:
17558 if (cu->language == language_ada || cu->language == language_m2
17559 || cu->language == language_pascal
17560 || cu->language == language_fortran)
17561 type = init_character_type (objfile, bits, 0, name);
17563 type = init_integer_type (objfile, bits, 0, name);
17565 case DW_ATE_unsigned_char:
17566 if (cu->language == language_ada || cu->language == language_m2
17567 || cu->language == language_pascal
17568 || cu->language == language_fortran
17569 || cu->language == language_rust)
17570 type = init_character_type (objfile, bits, 1, name);
17572 type = init_integer_type (objfile, bits, 1, name);
17576 gdbarch *arch = get_objfile_arch (objfile);
17579 type = builtin_type (arch)->builtin_char16;
17580 else if (bits == 32)
17581 type = builtin_type (arch)->builtin_char32;
17584 complaint (&symfile_complaints,
17585 _("unsupported DW_ATE_UTF bit size: '%d'"),
17587 type = init_integer_type (objfile, bits, 1, name);
17589 return set_die_type (die, type, cu);
17594 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17595 dwarf_type_encoding_name (encoding));
17596 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17600 if (name && strcmp (name, "char") == 0)
17601 TYPE_NOSIGN (type) = 1;
17603 return set_die_type (die, type, cu);
17606 /* Parse dwarf attribute if it's a block, reference or constant and put the
17607 resulting value of the attribute into struct bound_prop.
17608 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17611 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17612 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17614 struct dwarf2_property_baton *baton;
17615 struct obstack *obstack
17616 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17618 if (attr == NULL || prop == NULL)
17621 if (attr_form_is_block (attr))
17623 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17624 baton->referenced_type = NULL;
17625 baton->locexpr.per_cu = cu->per_cu;
17626 baton->locexpr.size = DW_BLOCK (attr)->size;
17627 baton->locexpr.data = DW_BLOCK (attr)->data;
17628 prop->data.baton = baton;
17629 prop->kind = PROP_LOCEXPR;
17630 gdb_assert (prop->data.baton != NULL);
17632 else if (attr_form_is_ref (attr))
17634 struct dwarf2_cu *target_cu = cu;
17635 struct die_info *target_die;
17636 struct attribute *target_attr;
17638 target_die = follow_die_ref (die, attr, &target_cu);
17639 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17640 if (target_attr == NULL)
17641 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17643 if (target_attr == NULL)
17646 switch (target_attr->name)
17648 case DW_AT_location:
17649 if (attr_form_is_section_offset (target_attr))
17651 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17652 baton->referenced_type = die_type (target_die, target_cu);
17653 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17654 prop->data.baton = baton;
17655 prop->kind = PROP_LOCLIST;
17656 gdb_assert (prop->data.baton != NULL);
17658 else if (attr_form_is_block (target_attr))
17660 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17661 baton->referenced_type = die_type (target_die, target_cu);
17662 baton->locexpr.per_cu = cu->per_cu;
17663 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17664 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17665 prop->data.baton = baton;
17666 prop->kind = PROP_LOCEXPR;
17667 gdb_assert (prop->data.baton != NULL);
17671 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17672 "dynamic property");
17676 case DW_AT_data_member_location:
17680 if (!handle_data_member_location (target_die, target_cu,
17684 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17685 baton->referenced_type = read_type_die (target_die->parent,
17687 baton->offset_info.offset = offset;
17688 baton->offset_info.type = die_type (target_die, target_cu);
17689 prop->data.baton = baton;
17690 prop->kind = PROP_ADDR_OFFSET;
17695 else if (attr_form_is_constant (attr))
17697 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17698 prop->kind = PROP_CONST;
17702 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17703 dwarf2_name (die, cu));
17710 /* Read the given DW_AT_subrange DIE. */
17712 static struct type *
17713 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17715 struct type *base_type, *orig_base_type;
17716 struct type *range_type;
17717 struct attribute *attr;
17718 struct dynamic_prop low, high;
17719 int low_default_is_valid;
17720 int high_bound_is_count = 0;
17722 LONGEST negative_mask;
17724 orig_base_type = die_type (die, cu);
17725 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17726 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17727 creating the range type, but we use the result of check_typedef
17728 when examining properties of the type. */
17729 base_type = check_typedef (orig_base_type);
17731 /* The die_type call above may have already set the type for this DIE. */
17732 range_type = get_die_type (die, cu);
17736 low.kind = PROP_CONST;
17737 high.kind = PROP_CONST;
17738 high.data.const_val = 0;
17740 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17741 omitting DW_AT_lower_bound. */
17742 switch (cu->language)
17745 case language_cplus:
17746 low.data.const_val = 0;
17747 low_default_is_valid = 1;
17749 case language_fortran:
17750 low.data.const_val = 1;
17751 low_default_is_valid = 1;
17754 case language_objc:
17755 case language_rust:
17756 low.data.const_val = 0;
17757 low_default_is_valid = (cu->header.version >= 4);
17761 case language_pascal:
17762 low.data.const_val = 1;
17763 low_default_is_valid = (cu->header.version >= 4);
17766 low.data.const_val = 0;
17767 low_default_is_valid = 0;
17771 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17773 attr_to_dynamic_prop (attr, die, cu, &low);
17774 else if (!low_default_is_valid)
17775 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17776 "- DIE at 0x%x [in module %s]"),
17777 to_underlying (die->sect_off),
17778 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17780 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17781 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17783 attr = dwarf2_attr (die, DW_AT_count, cu);
17784 if (attr_to_dynamic_prop (attr, die, cu, &high))
17786 /* If bounds are constant do the final calculation here. */
17787 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17788 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17790 high_bound_is_count = 1;
17794 /* Dwarf-2 specifications explicitly allows to create subrange types
17795 without specifying a base type.
17796 In that case, the base type must be set to the type of
17797 the lower bound, upper bound or count, in that order, if any of these
17798 three attributes references an object that has a type.
17799 If no base type is found, the Dwarf-2 specifications say that
17800 a signed integer type of size equal to the size of an address should
17802 For the following C code: `extern char gdb_int [];'
17803 GCC produces an empty range DIE.
17804 FIXME: muller/2010-05-28: Possible references to object for low bound,
17805 high bound or count are not yet handled by this code. */
17806 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17808 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17809 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17810 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17811 struct type *int_type = objfile_type (objfile)->builtin_int;
17813 /* Test "int", "long int", and "long long int" objfile types,
17814 and select the first one having a size above or equal to the
17815 architecture address size. */
17816 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17817 base_type = int_type;
17820 int_type = objfile_type (objfile)->builtin_long;
17821 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17822 base_type = int_type;
17825 int_type = objfile_type (objfile)->builtin_long_long;
17826 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17827 base_type = int_type;
17832 /* Normally, the DWARF producers are expected to use a signed
17833 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17834 But this is unfortunately not always the case, as witnessed
17835 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17836 is used instead. To work around that ambiguity, we treat
17837 the bounds as signed, and thus sign-extend their values, when
17838 the base type is signed. */
17840 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17841 if (low.kind == PROP_CONST
17842 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17843 low.data.const_val |= negative_mask;
17844 if (high.kind == PROP_CONST
17845 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17846 high.data.const_val |= negative_mask;
17848 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17850 if (high_bound_is_count)
17851 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17853 /* Ada expects an empty array on no boundary attributes. */
17854 if (attr == NULL && cu->language != language_ada)
17855 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17857 name = dwarf2_name (die, cu);
17859 TYPE_NAME (range_type) = name;
17861 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17863 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17865 set_die_type (die, range_type, cu);
17867 /* set_die_type should be already done. */
17868 set_descriptive_type (range_type, die, cu);
17873 static struct type *
17874 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17878 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17880 TYPE_NAME (type) = dwarf2_name (die, cu);
17882 /* In Ada, an unspecified type is typically used when the description
17883 of the type is defered to a different unit. When encountering
17884 such a type, we treat it as a stub, and try to resolve it later on,
17886 if (cu->language == language_ada)
17887 TYPE_STUB (type) = 1;
17889 return set_die_type (die, type, cu);
17892 /* Read a single die and all its descendents. Set the die's sibling
17893 field to NULL; set other fields in the die correctly, and set all
17894 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17895 location of the info_ptr after reading all of those dies. PARENT
17896 is the parent of the die in question. */
17898 static struct die_info *
17899 read_die_and_children (const struct die_reader_specs *reader,
17900 const gdb_byte *info_ptr,
17901 const gdb_byte **new_info_ptr,
17902 struct die_info *parent)
17904 struct die_info *die;
17905 const gdb_byte *cur_ptr;
17908 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17911 *new_info_ptr = cur_ptr;
17914 store_in_ref_table (die, reader->cu);
17917 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17921 *new_info_ptr = cur_ptr;
17924 die->sibling = NULL;
17925 die->parent = parent;
17929 /* Read a die, all of its descendents, and all of its siblings; set
17930 all of the fields of all of the dies correctly. Arguments are as
17931 in read_die_and_children. */
17933 static struct die_info *
17934 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17935 const gdb_byte *info_ptr,
17936 const gdb_byte **new_info_ptr,
17937 struct die_info *parent)
17939 struct die_info *first_die, *last_sibling;
17940 const gdb_byte *cur_ptr;
17942 cur_ptr = info_ptr;
17943 first_die = last_sibling = NULL;
17947 struct die_info *die
17948 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17952 *new_info_ptr = cur_ptr;
17959 last_sibling->sibling = die;
17961 last_sibling = die;
17965 /* Read a die, all of its descendents, and all of its siblings; set
17966 all of the fields of all of the dies correctly. Arguments are as
17967 in read_die_and_children.
17968 This the main entry point for reading a DIE and all its children. */
17970 static struct die_info *
17971 read_die_and_siblings (const struct die_reader_specs *reader,
17972 const gdb_byte *info_ptr,
17973 const gdb_byte **new_info_ptr,
17974 struct die_info *parent)
17976 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17977 new_info_ptr, parent);
17979 if (dwarf_die_debug)
17981 fprintf_unfiltered (gdb_stdlog,
17982 "Read die from %s@0x%x of %s:\n",
17983 get_section_name (reader->die_section),
17984 (unsigned) (info_ptr - reader->die_section->buffer),
17985 bfd_get_filename (reader->abfd));
17986 dump_die (die, dwarf_die_debug);
17992 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17994 The caller is responsible for filling in the extra attributes
17995 and updating (*DIEP)->num_attrs.
17996 Set DIEP to point to a newly allocated die with its information,
17997 except for its child, sibling, and parent fields.
17998 Set HAS_CHILDREN to tell whether the die has children or not. */
18000 static const gdb_byte *
18001 read_full_die_1 (const struct die_reader_specs *reader,
18002 struct die_info **diep, const gdb_byte *info_ptr,
18003 int *has_children, int num_extra_attrs)
18005 unsigned int abbrev_number, bytes_read, i;
18006 struct abbrev_info *abbrev;
18007 struct die_info *die;
18008 struct dwarf2_cu *cu = reader->cu;
18009 bfd *abfd = reader->abfd;
18011 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18012 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18013 info_ptr += bytes_read;
18014 if (!abbrev_number)
18021 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
18023 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18025 bfd_get_filename (abfd));
18027 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18028 die->sect_off = sect_off;
18029 die->tag = abbrev->tag;
18030 die->abbrev = abbrev_number;
18032 /* Make the result usable.
18033 The caller needs to update num_attrs after adding the extra
18035 die->num_attrs = abbrev->num_attrs;
18037 for (i = 0; i < abbrev->num_attrs; ++i)
18038 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18042 *has_children = abbrev->has_children;
18046 /* Read a die and all its attributes.
18047 Set DIEP to point to a newly allocated die with its information,
18048 except for its child, sibling, and parent fields.
18049 Set HAS_CHILDREN to tell whether the die has children or not. */
18051 static const gdb_byte *
18052 read_full_die (const struct die_reader_specs *reader,
18053 struct die_info **diep, const gdb_byte *info_ptr,
18056 const gdb_byte *result;
18058 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18060 if (dwarf_die_debug)
18062 fprintf_unfiltered (gdb_stdlog,
18063 "Read die from %s@0x%x of %s:\n",
18064 get_section_name (reader->die_section),
18065 (unsigned) (info_ptr - reader->die_section->buffer),
18066 bfd_get_filename (reader->abfd));
18067 dump_die (*diep, dwarf_die_debug);
18073 /* Abbreviation tables.
18075 In DWARF version 2, the description of the debugging information is
18076 stored in a separate .debug_abbrev section. Before we read any
18077 dies from a section we read in all abbreviations and install them
18078 in a hash table. */
18080 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18082 static struct abbrev_info *
18083 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
18085 struct abbrev_info *abbrev;
18087 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
18088 memset (abbrev, 0, sizeof (struct abbrev_info));
18093 /* Add an abbreviation to the table. */
18096 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
18097 unsigned int abbrev_number,
18098 struct abbrev_info *abbrev)
18100 unsigned int hash_number;
18102 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18103 abbrev->next = abbrev_table->abbrevs[hash_number];
18104 abbrev_table->abbrevs[hash_number] = abbrev;
18107 /* Look up an abbrev in the table.
18108 Returns NULL if the abbrev is not found. */
18110 static struct abbrev_info *
18111 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
18112 unsigned int abbrev_number)
18114 unsigned int hash_number;
18115 struct abbrev_info *abbrev;
18117 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18118 abbrev = abbrev_table->abbrevs[hash_number];
18122 if (abbrev->number == abbrev_number)
18124 abbrev = abbrev->next;
18129 /* Read in an abbrev table. */
18131 static struct abbrev_table *
18132 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18133 struct dwarf2_section_info *section,
18134 sect_offset sect_off)
18136 struct objfile *objfile = dwarf2_per_objfile->objfile;
18137 bfd *abfd = get_section_bfd_owner (section);
18138 struct abbrev_table *abbrev_table;
18139 const gdb_byte *abbrev_ptr;
18140 struct abbrev_info *cur_abbrev;
18141 unsigned int abbrev_number, bytes_read, abbrev_name;
18142 unsigned int abbrev_form;
18143 struct attr_abbrev *cur_attrs;
18144 unsigned int allocated_attrs;
18146 abbrev_table = XNEW (struct abbrev_table);
18147 abbrev_table->sect_off = sect_off;
18148 obstack_init (&abbrev_table->abbrev_obstack);
18149 abbrev_table->abbrevs =
18150 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
18152 memset (abbrev_table->abbrevs, 0,
18153 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
18155 dwarf2_read_section (objfile, section);
18156 abbrev_ptr = section->buffer + to_underlying (sect_off);
18157 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18158 abbrev_ptr += bytes_read;
18160 allocated_attrs = ATTR_ALLOC_CHUNK;
18161 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18163 /* Loop until we reach an abbrev number of 0. */
18164 while (abbrev_number)
18166 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
18168 /* read in abbrev header */
18169 cur_abbrev->number = abbrev_number;
18171 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18172 abbrev_ptr += bytes_read;
18173 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18176 /* now read in declarations */
18179 LONGEST implicit_const;
18181 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18182 abbrev_ptr += bytes_read;
18183 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18184 abbrev_ptr += bytes_read;
18185 if (abbrev_form == DW_FORM_implicit_const)
18187 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18189 abbrev_ptr += bytes_read;
18193 /* Initialize it due to a false compiler warning. */
18194 implicit_const = -1;
18197 if (abbrev_name == 0)
18200 if (cur_abbrev->num_attrs == allocated_attrs)
18202 allocated_attrs += ATTR_ALLOC_CHUNK;
18204 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18207 cur_attrs[cur_abbrev->num_attrs].name
18208 = (enum dwarf_attribute) abbrev_name;
18209 cur_attrs[cur_abbrev->num_attrs].form
18210 = (enum dwarf_form) abbrev_form;
18211 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18212 ++cur_abbrev->num_attrs;
18215 cur_abbrev->attrs =
18216 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18217 cur_abbrev->num_attrs);
18218 memcpy (cur_abbrev->attrs, cur_attrs,
18219 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18221 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
18223 /* Get next abbreviation.
18224 Under Irix6 the abbreviations for a compilation unit are not
18225 always properly terminated with an abbrev number of 0.
18226 Exit loop if we encounter an abbreviation which we have
18227 already read (which means we are about to read the abbreviations
18228 for the next compile unit) or if the end of the abbreviation
18229 table is reached. */
18230 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18232 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18233 abbrev_ptr += bytes_read;
18234 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
18239 return abbrev_table;
18242 /* Free the resources held by ABBREV_TABLE. */
18245 abbrev_table_free (struct abbrev_table *abbrev_table)
18247 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18248 xfree (abbrev_table);
18251 /* Same as abbrev_table_free but as a cleanup.
18252 We pass in a pointer to the pointer to the table so that we can
18253 set the pointer to NULL when we're done. It also simplifies
18254 build_type_psymtabs_1. */
18257 abbrev_table_free_cleanup (void *table_ptr)
18259 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
18261 if (*abbrev_table_ptr != NULL)
18262 abbrev_table_free (*abbrev_table_ptr);
18263 *abbrev_table_ptr = NULL;
18266 /* Read the abbrev table for CU from ABBREV_SECTION. */
18269 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18270 struct dwarf2_section_info *abbrev_section)
18273 abbrev_table_read_table (cu->per_cu->dwarf2_per_objfile, abbrev_section,
18274 cu->header.abbrev_sect_off);
18277 /* Release the memory used by the abbrev table for a compilation unit. */
18280 dwarf2_free_abbrev_table (void *ptr_to_cu)
18282 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
18284 if (cu->abbrev_table != NULL)
18285 abbrev_table_free (cu->abbrev_table);
18286 /* Set this to NULL so that we SEGV if we try to read it later,
18287 and also because free_comp_unit verifies this is NULL. */
18288 cu->abbrev_table = NULL;
18291 /* Returns nonzero if TAG represents a type that we might generate a partial
18295 is_type_tag_for_partial (int tag)
18300 /* Some types that would be reasonable to generate partial symbols for,
18301 that we don't at present. */
18302 case DW_TAG_array_type:
18303 case DW_TAG_file_type:
18304 case DW_TAG_ptr_to_member_type:
18305 case DW_TAG_set_type:
18306 case DW_TAG_string_type:
18307 case DW_TAG_subroutine_type:
18309 case DW_TAG_base_type:
18310 case DW_TAG_class_type:
18311 case DW_TAG_interface_type:
18312 case DW_TAG_enumeration_type:
18313 case DW_TAG_structure_type:
18314 case DW_TAG_subrange_type:
18315 case DW_TAG_typedef:
18316 case DW_TAG_union_type:
18323 /* Load all DIEs that are interesting for partial symbols into memory. */
18325 static struct partial_die_info *
18326 load_partial_dies (const struct die_reader_specs *reader,
18327 const gdb_byte *info_ptr, int building_psymtab)
18329 struct dwarf2_cu *cu = reader->cu;
18330 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18331 struct partial_die_info *part_die;
18332 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18333 struct abbrev_info *abbrev;
18334 unsigned int bytes_read;
18335 unsigned int load_all = 0;
18336 int nesting_level = 1;
18341 gdb_assert (cu->per_cu != NULL);
18342 if (cu->per_cu->load_all_dies)
18346 = htab_create_alloc_ex (cu->header.length / 12,
18350 &cu->comp_unit_obstack,
18351 hashtab_obstack_allocate,
18352 dummy_obstack_deallocate);
18354 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18358 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18360 /* A NULL abbrev means the end of a series of children. */
18361 if (abbrev == NULL)
18363 if (--nesting_level == 0)
18365 /* PART_DIE was probably the last thing allocated on the
18366 comp_unit_obstack, so we could call obstack_free
18367 here. We don't do that because the waste is small,
18368 and will be cleaned up when we're done with this
18369 compilation unit. This way, we're also more robust
18370 against other users of the comp_unit_obstack. */
18373 info_ptr += bytes_read;
18374 last_die = parent_die;
18375 parent_die = parent_die->die_parent;
18379 /* Check for template arguments. We never save these; if
18380 they're seen, we just mark the parent, and go on our way. */
18381 if (parent_die != NULL
18382 && cu->language == language_cplus
18383 && (abbrev->tag == DW_TAG_template_type_param
18384 || abbrev->tag == DW_TAG_template_value_param))
18386 parent_die->has_template_arguments = 1;
18390 /* We don't need a partial DIE for the template argument. */
18391 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18396 /* We only recurse into c++ subprograms looking for template arguments.
18397 Skip their other children. */
18399 && cu->language == language_cplus
18400 && parent_die != NULL
18401 && parent_die->tag == DW_TAG_subprogram)
18403 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18407 /* Check whether this DIE is interesting enough to save. Normally
18408 we would not be interested in members here, but there may be
18409 later variables referencing them via DW_AT_specification (for
18410 static members). */
18412 && !is_type_tag_for_partial (abbrev->tag)
18413 && abbrev->tag != DW_TAG_constant
18414 && abbrev->tag != DW_TAG_enumerator
18415 && abbrev->tag != DW_TAG_subprogram
18416 && abbrev->tag != DW_TAG_inlined_subroutine
18417 && abbrev->tag != DW_TAG_lexical_block
18418 && abbrev->tag != DW_TAG_variable
18419 && abbrev->tag != DW_TAG_namespace
18420 && abbrev->tag != DW_TAG_module
18421 && abbrev->tag != DW_TAG_member
18422 && abbrev->tag != DW_TAG_imported_unit
18423 && abbrev->tag != DW_TAG_imported_declaration)
18425 /* Otherwise we skip to the next sibling, if any. */
18426 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18430 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18433 /* This two-pass algorithm for processing partial symbols has a
18434 high cost in cache pressure. Thus, handle some simple cases
18435 here which cover the majority of C partial symbols. DIEs
18436 which neither have specification tags in them, nor could have
18437 specification tags elsewhere pointing at them, can simply be
18438 processed and discarded.
18440 This segment is also optional; scan_partial_symbols and
18441 add_partial_symbol will handle these DIEs if we chain
18442 them in normally. When compilers which do not emit large
18443 quantities of duplicate debug information are more common,
18444 this code can probably be removed. */
18446 /* Any complete simple types at the top level (pretty much all
18447 of them, for a language without namespaces), can be processed
18449 if (parent_die == NULL
18450 && part_die->has_specification == 0
18451 && part_die->is_declaration == 0
18452 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18453 || part_die->tag == DW_TAG_base_type
18454 || part_die->tag == DW_TAG_subrange_type))
18456 if (building_psymtab && part_die->name != NULL)
18457 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18458 VAR_DOMAIN, LOC_TYPEDEF,
18459 &objfile->static_psymbols,
18460 0, cu->language, objfile);
18461 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18465 /* The exception for DW_TAG_typedef with has_children above is
18466 a workaround of GCC PR debug/47510. In the case of this complaint
18467 type_name_no_tag_or_error will error on such types later.
18469 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18470 it could not find the child DIEs referenced later, this is checked
18471 above. In correct DWARF DW_TAG_typedef should have no children. */
18473 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18474 complaint (&symfile_complaints,
18475 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18476 "- DIE at 0x%x [in module %s]"),
18477 to_underlying (part_die->sect_off), objfile_name (objfile));
18479 /* If we're at the second level, and we're an enumerator, and
18480 our parent has no specification (meaning possibly lives in a
18481 namespace elsewhere), then we can add the partial symbol now
18482 instead of queueing it. */
18483 if (part_die->tag == DW_TAG_enumerator
18484 && parent_die != NULL
18485 && parent_die->die_parent == NULL
18486 && parent_die->tag == DW_TAG_enumeration_type
18487 && parent_die->has_specification == 0)
18489 if (part_die->name == NULL)
18490 complaint (&symfile_complaints,
18491 _("malformed enumerator DIE ignored"));
18492 else if (building_psymtab)
18493 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18494 VAR_DOMAIN, LOC_CONST,
18495 cu->language == language_cplus
18496 ? &objfile->global_psymbols
18497 : &objfile->static_psymbols,
18498 0, cu->language, objfile);
18500 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18504 /* We'll save this DIE so link it in. */
18505 part_die->die_parent = parent_die;
18506 part_die->die_sibling = NULL;
18507 part_die->die_child = NULL;
18509 if (last_die && last_die == parent_die)
18510 last_die->die_child = part_die;
18512 last_die->die_sibling = part_die;
18514 last_die = part_die;
18516 if (first_die == NULL)
18517 first_die = part_die;
18519 /* Maybe add the DIE to the hash table. Not all DIEs that we
18520 find interesting need to be in the hash table, because we
18521 also have the parent/sibling/child chains; only those that we
18522 might refer to by offset later during partial symbol reading.
18524 For now this means things that might have be the target of a
18525 DW_AT_specification, DW_AT_abstract_origin, or
18526 DW_AT_extension. DW_AT_extension will refer only to
18527 namespaces; DW_AT_abstract_origin refers to functions (and
18528 many things under the function DIE, but we do not recurse
18529 into function DIEs during partial symbol reading) and
18530 possibly variables as well; DW_AT_specification refers to
18531 declarations. Declarations ought to have the DW_AT_declaration
18532 flag. It happens that GCC forgets to put it in sometimes, but
18533 only for functions, not for types.
18535 Adding more things than necessary to the hash table is harmless
18536 except for the performance cost. Adding too few will result in
18537 wasted time in find_partial_die, when we reread the compilation
18538 unit with load_all_dies set. */
18541 || abbrev->tag == DW_TAG_constant
18542 || abbrev->tag == DW_TAG_subprogram
18543 || abbrev->tag == DW_TAG_variable
18544 || abbrev->tag == DW_TAG_namespace
18545 || part_die->is_declaration)
18549 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18550 to_underlying (part_die->sect_off),
18555 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18557 /* For some DIEs we want to follow their children (if any). For C
18558 we have no reason to follow the children of structures; for other
18559 languages we have to, so that we can get at method physnames
18560 to infer fully qualified class names, for DW_AT_specification,
18561 and for C++ template arguments. For C++, we also look one level
18562 inside functions to find template arguments (if the name of the
18563 function does not already contain the template arguments).
18565 For Ada, we need to scan the children of subprograms and lexical
18566 blocks as well because Ada allows the definition of nested
18567 entities that could be interesting for the debugger, such as
18568 nested subprograms for instance. */
18569 if (last_die->has_children
18571 || last_die->tag == DW_TAG_namespace
18572 || last_die->tag == DW_TAG_module
18573 || last_die->tag == DW_TAG_enumeration_type
18574 || (cu->language == language_cplus
18575 && last_die->tag == DW_TAG_subprogram
18576 && (last_die->name == NULL
18577 || strchr (last_die->name, '<') == NULL))
18578 || (cu->language != language_c
18579 && (last_die->tag == DW_TAG_class_type
18580 || last_die->tag == DW_TAG_interface_type
18581 || last_die->tag == DW_TAG_structure_type
18582 || last_die->tag == DW_TAG_union_type))
18583 || (cu->language == language_ada
18584 && (last_die->tag == DW_TAG_subprogram
18585 || last_die->tag == DW_TAG_lexical_block))))
18588 parent_die = last_die;
18592 /* Otherwise we skip to the next sibling, if any. */
18593 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18595 /* Back to the top, do it again. */
18599 /* Read a minimal amount of information into the minimal die structure. */
18601 static const gdb_byte *
18602 read_partial_die (const struct die_reader_specs *reader,
18603 struct partial_die_info *part_die,
18604 struct abbrev_info *abbrev, unsigned int abbrev_len,
18605 const gdb_byte *info_ptr)
18607 struct dwarf2_cu *cu = reader->cu;
18608 struct dwarf2_per_objfile *dwarf2_per_objfile
18609 = cu->per_cu->dwarf2_per_objfile;
18610 struct objfile *objfile = dwarf2_per_objfile->objfile;
18611 const gdb_byte *buffer = reader->buffer;
18613 struct attribute attr;
18614 int has_low_pc_attr = 0;
18615 int has_high_pc_attr = 0;
18616 int high_pc_relative = 0;
18618 memset (part_die, 0, sizeof (struct partial_die_info));
18620 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18622 info_ptr += abbrev_len;
18624 if (abbrev == NULL)
18627 part_die->tag = abbrev->tag;
18628 part_die->has_children = abbrev->has_children;
18630 for (i = 0; i < abbrev->num_attrs; ++i)
18632 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18634 /* Store the data if it is of an attribute we want to keep in a
18635 partial symbol table. */
18639 switch (part_die->tag)
18641 case DW_TAG_compile_unit:
18642 case DW_TAG_partial_unit:
18643 case DW_TAG_type_unit:
18644 /* Compilation units have a DW_AT_name that is a filename, not
18645 a source language identifier. */
18646 case DW_TAG_enumeration_type:
18647 case DW_TAG_enumerator:
18648 /* These tags always have simple identifiers already; no need
18649 to canonicalize them. */
18650 part_die->name = DW_STRING (&attr);
18654 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18655 &objfile->per_bfd->storage_obstack);
18659 case DW_AT_linkage_name:
18660 case DW_AT_MIPS_linkage_name:
18661 /* Note that both forms of linkage name might appear. We
18662 assume they will be the same, and we only store the last
18664 if (cu->language == language_ada)
18665 part_die->name = DW_STRING (&attr);
18666 part_die->linkage_name = DW_STRING (&attr);
18669 has_low_pc_attr = 1;
18670 part_die->lowpc = attr_value_as_address (&attr);
18672 case DW_AT_high_pc:
18673 has_high_pc_attr = 1;
18674 part_die->highpc = attr_value_as_address (&attr);
18675 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18676 high_pc_relative = 1;
18678 case DW_AT_location:
18679 /* Support the .debug_loc offsets. */
18680 if (attr_form_is_block (&attr))
18682 part_die->d.locdesc = DW_BLOCK (&attr);
18684 else if (attr_form_is_section_offset (&attr))
18686 dwarf2_complex_location_expr_complaint ();
18690 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18691 "partial symbol information");
18694 case DW_AT_external:
18695 part_die->is_external = DW_UNSND (&attr);
18697 case DW_AT_declaration:
18698 part_die->is_declaration = DW_UNSND (&attr);
18701 part_die->has_type = 1;
18703 case DW_AT_abstract_origin:
18704 case DW_AT_specification:
18705 case DW_AT_extension:
18706 part_die->has_specification = 1;
18707 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18708 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18709 || cu->per_cu->is_dwz);
18711 case DW_AT_sibling:
18712 /* Ignore absolute siblings, they might point outside of
18713 the current compile unit. */
18714 if (attr.form == DW_FORM_ref_addr)
18715 complaint (&symfile_complaints,
18716 _("ignoring absolute DW_AT_sibling"));
18719 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18720 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18722 if (sibling_ptr < info_ptr)
18723 complaint (&symfile_complaints,
18724 _("DW_AT_sibling points backwards"));
18725 else if (sibling_ptr > reader->buffer_end)
18726 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18728 part_die->sibling = sibling_ptr;
18731 case DW_AT_byte_size:
18732 part_die->has_byte_size = 1;
18734 case DW_AT_const_value:
18735 part_die->has_const_value = 1;
18737 case DW_AT_calling_convention:
18738 /* DWARF doesn't provide a way to identify a program's source-level
18739 entry point. DW_AT_calling_convention attributes are only meant
18740 to describe functions' calling conventions.
18742 However, because it's a necessary piece of information in
18743 Fortran, and before DWARF 4 DW_CC_program was the only
18744 piece of debugging information whose definition refers to
18745 a 'main program' at all, several compilers marked Fortran
18746 main programs with DW_CC_program --- even when those
18747 functions use the standard calling conventions.
18749 Although DWARF now specifies a way to provide this
18750 information, we support this practice for backward
18752 if (DW_UNSND (&attr) == DW_CC_program
18753 && cu->language == language_fortran)
18754 part_die->main_subprogram = 1;
18757 if (DW_UNSND (&attr) == DW_INL_inlined
18758 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18759 part_die->may_be_inlined = 1;
18763 if (part_die->tag == DW_TAG_imported_unit)
18765 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18766 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18767 || cu->per_cu->is_dwz);
18771 case DW_AT_main_subprogram:
18772 part_die->main_subprogram = DW_UNSND (&attr);
18780 if (high_pc_relative)
18781 part_die->highpc += part_die->lowpc;
18783 if (has_low_pc_attr && has_high_pc_attr)
18785 /* When using the GNU linker, .gnu.linkonce. sections are used to
18786 eliminate duplicate copies of functions and vtables and such.
18787 The linker will arbitrarily choose one and discard the others.
18788 The AT_*_pc values for such functions refer to local labels in
18789 these sections. If the section from that file was discarded, the
18790 labels are not in the output, so the relocs get a value of 0.
18791 If this is a discarded function, mark the pc bounds as invalid,
18792 so that GDB will ignore it. */
18793 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18797 complaint (&symfile_complaints,
18798 _("DW_AT_low_pc %s is zero "
18799 "for DIE at 0x%x [in module %s]"),
18800 paddress (gdbarch, part_die->lowpc),
18801 to_underlying (part_die->sect_off), objfile_name (objfile));
18803 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18804 else if (part_die->lowpc >= part_die->highpc)
18806 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18808 complaint (&symfile_complaints,
18809 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18810 "for DIE at 0x%x [in module %s]"),
18811 paddress (gdbarch, part_die->lowpc),
18812 paddress (gdbarch, part_die->highpc),
18813 to_underlying (part_die->sect_off),
18814 objfile_name (objfile));
18817 part_die->has_pc_info = 1;
18823 /* Find a cached partial DIE at OFFSET in CU. */
18825 static struct partial_die_info *
18826 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18828 struct partial_die_info *lookup_die = NULL;
18829 struct partial_die_info part_die;
18831 part_die.sect_off = sect_off;
18832 lookup_die = ((struct partial_die_info *)
18833 htab_find_with_hash (cu->partial_dies, &part_die,
18834 to_underlying (sect_off)));
18839 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18840 except in the case of .debug_types DIEs which do not reference
18841 outside their CU (they do however referencing other types via
18842 DW_FORM_ref_sig8). */
18844 static struct partial_die_info *
18845 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18847 struct dwarf2_per_objfile *dwarf2_per_objfile
18848 = cu->per_cu->dwarf2_per_objfile;
18849 struct objfile *objfile = dwarf2_per_objfile->objfile;
18850 struct dwarf2_per_cu_data *per_cu = NULL;
18851 struct partial_die_info *pd = NULL;
18853 if (offset_in_dwz == cu->per_cu->is_dwz
18854 && offset_in_cu_p (&cu->header, sect_off))
18856 pd = find_partial_die_in_comp_unit (sect_off, cu);
18859 /* We missed recording what we needed.
18860 Load all dies and try again. */
18861 per_cu = cu->per_cu;
18865 /* TUs don't reference other CUs/TUs (except via type signatures). */
18866 if (cu->per_cu->is_debug_types)
18868 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18869 " external reference to offset 0x%x [in module %s].\n"),
18870 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18871 bfd_get_filename (objfile->obfd));
18873 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18874 dwarf2_per_objfile);
18876 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18877 load_partial_comp_unit (per_cu);
18879 per_cu->cu->last_used = 0;
18880 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18883 /* If we didn't find it, and not all dies have been loaded,
18884 load them all and try again. */
18886 if (pd == NULL && per_cu->load_all_dies == 0)
18888 per_cu->load_all_dies = 1;
18890 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18891 THIS_CU->cu may already be in use. So we can't just free it and
18892 replace its DIEs with the ones we read in. Instead, we leave those
18893 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18894 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18896 load_partial_comp_unit (per_cu);
18898 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18902 internal_error (__FILE__, __LINE__,
18903 _("could not find partial DIE 0x%x "
18904 "in cache [from module %s]\n"),
18905 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18909 /* See if we can figure out if the class lives in a namespace. We do
18910 this by looking for a member function; its demangled name will
18911 contain namespace info, if there is any. */
18914 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18915 struct dwarf2_cu *cu)
18917 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18918 what template types look like, because the demangler
18919 frequently doesn't give the same name as the debug info. We
18920 could fix this by only using the demangled name to get the
18921 prefix (but see comment in read_structure_type). */
18923 struct partial_die_info *real_pdi;
18924 struct partial_die_info *child_pdi;
18926 /* If this DIE (this DIE's specification, if any) has a parent, then
18927 we should not do this. We'll prepend the parent's fully qualified
18928 name when we create the partial symbol. */
18930 real_pdi = struct_pdi;
18931 while (real_pdi->has_specification)
18932 real_pdi = find_partial_die (real_pdi->spec_offset,
18933 real_pdi->spec_is_dwz, cu);
18935 if (real_pdi->die_parent != NULL)
18938 for (child_pdi = struct_pdi->die_child;
18940 child_pdi = child_pdi->die_sibling)
18942 if (child_pdi->tag == DW_TAG_subprogram
18943 && child_pdi->linkage_name != NULL)
18945 char *actual_class_name
18946 = language_class_name_from_physname (cu->language_defn,
18947 child_pdi->linkage_name);
18948 if (actual_class_name != NULL)
18950 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18953 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18955 strlen (actual_class_name)));
18956 xfree (actual_class_name);
18963 /* Adjust PART_DIE before generating a symbol for it. This function
18964 may set the is_external flag or change the DIE's name. */
18967 fixup_partial_die (struct partial_die_info *part_die,
18968 struct dwarf2_cu *cu)
18970 /* Once we've fixed up a die, there's no point in doing so again.
18971 This also avoids a memory leak if we were to call
18972 guess_partial_die_structure_name multiple times. */
18973 if (part_die->fixup_called)
18976 /* If we found a reference attribute and the DIE has no name, try
18977 to find a name in the referred to DIE. */
18979 if (part_die->name == NULL && part_die->has_specification)
18981 struct partial_die_info *spec_die;
18983 spec_die = find_partial_die (part_die->spec_offset,
18984 part_die->spec_is_dwz, cu);
18986 fixup_partial_die (spec_die, cu);
18988 if (spec_die->name)
18990 part_die->name = spec_die->name;
18992 /* Copy DW_AT_external attribute if it is set. */
18993 if (spec_die->is_external)
18994 part_die->is_external = spec_die->is_external;
18998 /* Set default names for some unnamed DIEs. */
19000 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
19001 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
19003 /* If there is no parent die to provide a namespace, and there are
19004 children, see if we can determine the namespace from their linkage
19006 if (cu->language == language_cplus
19007 && !VEC_empty (dwarf2_section_info_def,
19008 cu->per_cu->dwarf2_per_objfile->types)
19009 && part_die->die_parent == NULL
19010 && part_die->has_children
19011 && (part_die->tag == DW_TAG_class_type
19012 || part_die->tag == DW_TAG_structure_type
19013 || part_die->tag == DW_TAG_union_type))
19014 guess_partial_die_structure_name (part_die, cu);
19016 /* GCC might emit a nameless struct or union that has a linkage
19017 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19018 if (part_die->name == NULL
19019 && (part_die->tag == DW_TAG_class_type
19020 || part_die->tag == DW_TAG_interface_type
19021 || part_die->tag == DW_TAG_structure_type
19022 || part_die->tag == DW_TAG_union_type)
19023 && part_die->linkage_name != NULL)
19027 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
19032 /* Strip any leading namespaces/classes, keep only the base name.
19033 DW_AT_name for named DIEs does not contain the prefixes. */
19034 base = strrchr (demangled, ':');
19035 if (base && base > demangled && base[-1] == ':')
19040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19043 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19044 base, strlen (base)));
19049 part_die->fixup_called = 1;
19052 /* Read an attribute value described by an attribute form. */
19054 static const gdb_byte *
19055 read_attribute_value (const struct die_reader_specs *reader,
19056 struct attribute *attr, unsigned form,
19057 LONGEST implicit_const, const gdb_byte *info_ptr)
19059 struct dwarf2_cu *cu = reader->cu;
19060 struct dwarf2_per_objfile *dwarf2_per_objfile
19061 = cu->per_cu->dwarf2_per_objfile;
19062 struct objfile *objfile = dwarf2_per_objfile->objfile;
19063 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19064 bfd *abfd = reader->abfd;
19065 struct comp_unit_head *cu_header = &cu->header;
19066 unsigned int bytes_read;
19067 struct dwarf_block *blk;
19069 attr->form = (enum dwarf_form) form;
19072 case DW_FORM_ref_addr:
19073 if (cu->header.version == 2)
19074 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19076 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19077 &cu->header, &bytes_read);
19078 info_ptr += bytes_read;
19080 case DW_FORM_GNU_ref_alt:
19081 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19082 info_ptr += bytes_read;
19085 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19086 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19087 info_ptr += bytes_read;
19089 case DW_FORM_block2:
19090 blk = dwarf_alloc_block (cu);
19091 blk->size = read_2_bytes (abfd, info_ptr);
19093 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19094 info_ptr += blk->size;
19095 DW_BLOCK (attr) = blk;
19097 case DW_FORM_block4:
19098 blk = dwarf_alloc_block (cu);
19099 blk->size = read_4_bytes (abfd, info_ptr);
19101 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19102 info_ptr += blk->size;
19103 DW_BLOCK (attr) = blk;
19105 case DW_FORM_data2:
19106 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19109 case DW_FORM_data4:
19110 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19113 case DW_FORM_data8:
19114 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19117 case DW_FORM_data16:
19118 blk = dwarf_alloc_block (cu);
19120 blk->data = read_n_bytes (abfd, info_ptr, 16);
19122 DW_BLOCK (attr) = blk;
19124 case DW_FORM_sec_offset:
19125 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19126 info_ptr += bytes_read;
19128 case DW_FORM_string:
19129 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19130 DW_STRING_IS_CANONICAL (attr) = 0;
19131 info_ptr += bytes_read;
19134 if (!cu->per_cu->is_dwz)
19136 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19137 abfd, info_ptr, cu_header,
19139 DW_STRING_IS_CANONICAL (attr) = 0;
19140 info_ptr += bytes_read;
19144 case DW_FORM_line_strp:
19145 if (!cu->per_cu->is_dwz)
19147 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19149 cu_header, &bytes_read);
19150 DW_STRING_IS_CANONICAL (attr) = 0;
19151 info_ptr += bytes_read;
19155 case DW_FORM_GNU_strp_alt:
19157 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19158 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19161 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19163 DW_STRING_IS_CANONICAL (attr) = 0;
19164 info_ptr += bytes_read;
19167 case DW_FORM_exprloc:
19168 case DW_FORM_block:
19169 blk = dwarf_alloc_block (cu);
19170 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19171 info_ptr += bytes_read;
19172 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19173 info_ptr += blk->size;
19174 DW_BLOCK (attr) = blk;
19176 case DW_FORM_block1:
19177 blk = dwarf_alloc_block (cu);
19178 blk->size = read_1_byte (abfd, info_ptr);
19180 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19181 info_ptr += blk->size;
19182 DW_BLOCK (attr) = blk;
19184 case DW_FORM_data1:
19185 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19189 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19192 case DW_FORM_flag_present:
19193 DW_UNSND (attr) = 1;
19195 case DW_FORM_sdata:
19196 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19197 info_ptr += bytes_read;
19199 case DW_FORM_udata:
19200 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19201 info_ptr += bytes_read;
19204 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19205 + read_1_byte (abfd, info_ptr));
19209 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19210 + read_2_bytes (abfd, info_ptr));
19214 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19215 + read_4_bytes (abfd, info_ptr));
19219 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19220 + read_8_bytes (abfd, info_ptr));
19223 case DW_FORM_ref_sig8:
19224 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19227 case DW_FORM_ref_udata:
19228 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19229 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19230 info_ptr += bytes_read;
19232 case DW_FORM_indirect:
19233 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19234 info_ptr += bytes_read;
19235 if (form == DW_FORM_implicit_const)
19237 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19238 info_ptr += bytes_read;
19240 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19243 case DW_FORM_implicit_const:
19244 DW_SND (attr) = implicit_const;
19246 case DW_FORM_GNU_addr_index:
19247 if (reader->dwo_file == NULL)
19249 /* For now flag a hard error.
19250 Later we can turn this into a complaint. */
19251 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19252 dwarf_form_name (form),
19253 bfd_get_filename (abfd));
19255 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19256 info_ptr += bytes_read;
19258 case DW_FORM_GNU_str_index:
19259 if (reader->dwo_file == NULL)
19261 /* For now flag a hard error.
19262 Later we can turn this into a complaint if warranted. */
19263 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19264 dwarf_form_name (form),
19265 bfd_get_filename (abfd));
19268 ULONGEST str_index =
19269 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19271 DW_STRING (attr) = read_str_index (reader, str_index);
19272 DW_STRING_IS_CANONICAL (attr) = 0;
19273 info_ptr += bytes_read;
19277 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19278 dwarf_form_name (form),
19279 bfd_get_filename (abfd));
19283 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19284 attr->form = DW_FORM_GNU_ref_alt;
19286 /* We have seen instances where the compiler tried to emit a byte
19287 size attribute of -1 which ended up being encoded as an unsigned
19288 0xffffffff. Although 0xffffffff is technically a valid size value,
19289 an object of this size seems pretty unlikely so we can relatively
19290 safely treat these cases as if the size attribute was invalid and
19291 treat them as zero by default. */
19292 if (attr->name == DW_AT_byte_size
19293 && form == DW_FORM_data4
19294 && DW_UNSND (attr) >= 0xffffffff)
19297 (&symfile_complaints,
19298 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19299 hex_string (DW_UNSND (attr)));
19300 DW_UNSND (attr) = 0;
19306 /* Read an attribute described by an abbreviated attribute. */
19308 static const gdb_byte *
19309 read_attribute (const struct die_reader_specs *reader,
19310 struct attribute *attr, struct attr_abbrev *abbrev,
19311 const gdb_byte *info_ptr)
19313 attr->name = abbrev->name;
19314 return read_attribute_value (reader, attr, abbrev->form,
19315 abbrev->implicit_const, info_ptr);
19318 /* Read dwarf information from a buffer. */
19320 static unsigned int
19321 read_1_byte (bfd *abfd, const gdb_byte *buf)
19323 return bfd_get_8 (abfd, buf);
19327 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19329 return bfd_get_signed_8 (abfd, buf);
19332 static unsigned int
19333 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19335 return bfd_get_16 (abfd, buf);
19339 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19341 return bfd_get_signed_16 (abfd, buf);
19344 static unsigned int
19345 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19347 return bfd_get_32 (abfd, buf);
19351 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19353 return bfd_get_signed_32 (abfd, buf);
19357 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19359 return bfd_get_64 (abfd, buf);
19363 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19364 unsigned int *bytes_read)
19366 struct comp_unit_head *cu_header = &cu->header;
19367 CORE_ADDR retval = 0;
19369 if (cu_header->signed_addr_p)
19371 switch (cu_header->addr_size)
19374 retval = bfd_get_signed_16 (abfd, buf);
19377 retval = bfd_get_signed_32 (abfd, buf);
19380 retval = bfd_get_signed_64 (abfd, buf);
19383 internal_error (__FILE__, __LINE__,
19384 _("read_address: bad switch, signed [in module %s]"),
19385 bfd_get_filename (abfd));
19390 switch (cu_header->addr_size)
19393 retval = bfd_get_16 (abfd, buf);
19396 retval = bfd_get_32 (abfd, buf);
19399 retval = bfd_get_64 (abfd, buf);
19402 internal_error (__FILE__, __LINE__,
19403 _("read_address: bad switch, "
19404 "unsigned [in module %s]"),
19405 bfd_get_filename (abfd));
19409 *bytes_read = cu_header->addr_size;
19413 /* Read the initial length from a section. The (draft) DWARF 3
19414 specification allows the initial length to take up either 4 bytes
19415 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19416 bytes describe the length and all offsets will be 8 bytes in length
19419 An older, non-standard 64-bit format is also handled by this
19420 function. The older format in question stores the initial length
19421 as an 8-byte quantity without an escape value. Lengths greater
19422 than 2^32 aren't very common which means that the initial 4 bytes
19423 is almost always zero. Since a length value of zero doesn't make
19424 sense for the 32-bit format, this initial zero can be considered to
19425 be an escape value which indicates the presence of the older 64-bit
19426 format. As written, the code can't detect (old format) lengths
19427 greater than 4GB. If it becomes necessary to handle lengths
19428 somewhat larger than 4GB, we could allow other small values (such
19429 as the non-sensical values of 1, 2, and 3) to also be used as
19430 escape values indicating the presence of the old format.
19432 The value returned via bytes_read should be used to increment the
19433 relevant pointer after calling read_initial_length().
19435 [ Note: read_initial_length() and read_offset() are based on the
19436 document entitled "DWARF Debugging Information Format", revision
19437 3, draft 8, dated November 19, 2001. This document was obtained
19440 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19442 This document is only a draft and is subject to change. (So beware.)
19444 Details regarding the older, non-standard 64-bit format were
19445 determined empirically by examining 64-bit ELF files produced by
19446 the SGI toolchain on an IRIX 6.5 machine.
19448 - Kevin, July 16, 2002
19452 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19454 LONGEST length = bfd_get_32 (abfd, buf);
19456 if (length == 0xffffffff)
19458 length = bfd_get_64 (abfd, buf + 4);
19461 else if (length == 0)
19463 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19464 length = bfd_get_64 (abfd, buf);
19475 /* Cover function for read_initial_length.
19476 Returns the length of the object at BUF, and stores the size of the
19477 initial length in *BYTES_READ and stores the size that offsets will be in
19479 If the initial length size is not equivalent to that specified in
19480 CU_HEADER then issue a complaint.
19481 This is useful when reading non-comp-unit headers. */
19484 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19485 const struct comp_unit_head *cu_header,
19486 unsigned int *bytes_read,
19487 unsigned int *offset_size)
19489 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19491 gdb_assert (cu_header->initial_length_size == 4
19492 || cu_header->initial_length_size == 8
19493 || cu_header->initial_length_size == 12);
19495 if (cu_header->initial_length_size != *bytes_read)
19496 complaint (&symfile_complaints,
19497 _("intermixed 32-bit and 64-bit DWARF sections"));
19499 *offset_size = (*bytes_read == 4) ? 4 : 8;
19503 /* Read an offset from the data stream. The size of the offset is
19504 given by cu_header->offset_size. */
19507 read_offset (bfd *abfd, const gdb_byte *buf,
19508 const struct comp_unit_head *cu_header,
19509 unsigned int *bytes_read)
19511 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19513 *bytes_read = cu_header->offset_size;
19517 /* Read an offset from the data stream. */
19520 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19522 LONGEST retval = 0;
19524 switch (offset_size)
19527 retval = bfd_get_32 (abfd, buf);
19530 retval = bfd_get_64 (abfd, buf);
19533 internal_error (__FILE__, __LINE__,
19534 _("read_offset_1: bad switch [in module %s]"),
19535 bfd_get_filename (abfd));
19541 static const gdb_byte *
19542 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19544 /* If the size of a host char is 8 bits, we can return a pointer
19545 to the buffer, otherwise we have to copy the data to a buffer
19546 allocated on the temporary obstack. */
19547 gdb_assert (HOST_CHAR_BIT == 8);
19551 static const char *
19552 read_direct_string (bfd *abfd, const gdb_byte *buf,
19553 unsigned int *bytes_read_ptr)
19555 /* If the size of a host char is 8 bits, we can return a pointer
19556 to the string, otherwise we have to copy the string to a buffer
19557 allocated on the temporary obstack. */
19558 gdb_assert (HOST_CHAR_BIT == 8);
19561 *bytes_read_ptr = 1;
19564 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19565 return (const char *) buf;
19568 /* Return pointer to string at section SECT offset STR_OFFSET with error
19569 reporting strings FORM_NAME and SECT_NAME. */
19571 static const char *
19572 read_indirect_string_at_offset_from (struct objfile *objfile,
19573 bfd *abfd, LONGEST str_offset,
19574 struct dwarf2_section_info *sect,
19575 const char *form_name,
19576 const char *sect_name)
19578 dwarf2_read_section (objfile, sect);
19579 if (sect->buffer == NULL)
19580 error (_("%s used without %s section [in module %s]"),
19581 form_name, sect_name, bfd_get_filename (abfd));
19582 if (str_offset >= sect->size)
19583 error (_("%s pointing outside of %s section [in module %s]"),
19584 form_name, sect_name, bfd_get_filename (abfd));
19585 gdb_assert (HOST_CHAR_BIT == 8);
19586 if (sect->buffer[str_offset] == '\0')
19588 return (const char *) (sect->buffer + str_offset);
19591 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19593 static const char *
19594 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19595 bfd *abfd, LONGEST str_offset)
19597 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19599 &dwarf2_per_objfile->str,
19600 "DW_FORM_strp", ".debug_str");
19603 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19605 static const char *
19606 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19607 bfd *abfd, LONGEST str_offset)
19609 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19611 &dwarf2_per_objfile->line_str,
19612 "DW_FORM_line_strp",
19613 ".debug_line_str");
19616 /* Read a string at offset STR_OFFSET in the .debug_str section from
19617 the .dwz file DWZ. Throw an error if the offset is too large. If
19618 the string consists of a single NUL byte, return NULL; otherwise
19619 return a pointer to the string. */
19621 static const char *
19622 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19623 LONGEST str_offset)
19625 dwarf2_read_section (objfile, &dwz->str);
19627 if (dwz->str.buffer == NULL)
19628 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19629 "section [in module %s]"),
19630 bfd_get_filename (dwz->dwz_bfd));
19631 if (str_offset >= dwz->str.size)
19632 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19633 ".debug_str section [in module %s]"),
19634 bfd_get_filename (dwz->dwz_bfd));
19635 gdb_assert (HOST_CHAR_BIT == 8);
19636 if (dwz->str.buffer[str_offset] == '\0')
19638 return (const char *) (dwz->str.buffer + str_offset);
19641 /* Return pointer to string at .debug_str offset as read from BUF.
19642 BUF is assumed to be in a compilation unit described by CU_HEADER.
19643 Return *BYTES_READ_PTR count of bytes read from BUF. */
19645 static const char *
19646 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19647 const gdb_byte *buf,
19648 const struct comp_unit_head *cu_header,
19649 unsigned int *bytes_read_ptr)
19651 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19653 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19656 /* Return pointer to string at .debug_line_str offset as read from BUF.
19657 BUF is assumed to be in a compilation unit described by CU_HEADER.
19658 Return *BYTES_READ_PTR count of bytes read from BUF. */
19660 static const char *
19661 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19662 bfd *abfd, const gdb_byte *buf,
19663 const struct comp_unit_head *cu_header,
19664 unsigned int *bytes_read_ptr)
19666 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19668 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19673 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19674 unsigned int *bytes_read_ptr)
19677 unsigned int num_read;
19679 unsigned char byte;
19686 byte = bfd_get_8 (abfd, buf);
19689 result |= ((ULONGEST) (byte & 127) << shift);
19690 if ((byte & 128) == 0)
19696 *bytes_read_ptr = num_read;
19701 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19702 unsigned int *bytes_read_ptr)
19705 int shift, num_read;
19706 unsigned char byte;
19713 byte = bfd_get_8 (abfd, buf);
19716 result |= ((LONGEST) (byte & 127) << shift);
19718 if ((byte & 128) == 0)
19723 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19724 result |= -(((LONGEST) 1) << shift);
19725 *bytes_read_ptr = num_read;
19729 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19730 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19731 ADDR_SIZE is the size of addresses from the CU header. */
19734 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19735 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19737 struct objfile *objfile = dwarf2_per_objfile->objfile;
19738 bfd *abfd = objfile->obfd;
19739 const gdb_byte *info_ptr;
19741 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19742 if (dwarf2_per_objfile->addr.buffer == NULL)
19743 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19744 objfile_name (objfile));
19745 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19746 error (_("DW_FORM_addr_index pointing outside of "
19747 ".debug_addr section [in module %s]"),
19748 objfile_name (objfile));
19749 info_ptr = (dwarf2_per_objfile->addr.buffer
19750 + addr_base + addr_index * addr_size);
19751 if (addr_size == 4)
19752 return bfd_get_32 (abfd, info_ptr);
19754 return bfd_get_64 (abfd, info_ptr);
19757 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19760 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19762 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19763 cu->addr_base, cu->header.addr_size);
19766 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19769 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19770 unsigned int *bytes_read)
19772 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19773 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19775 return read_addr_index (cu, addr_index);
19778 /* Data structure to pass results from dwarf2_read_addr_index_reader
19779 back to dwarf2_read_addr_index. */
19781 struct dwarf2_read_addr_index_data
19783 ULONGEST addr_base;
19787 /* die_reader_func for dwarf2_read_addr_index. */
19790 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19791 const gdb_byte *info_ptr,
19792 struct die_info *comp_unit_die,
19796 struct dwarf2_cu *cu = reader->cu;
19797 struct dwarf2_read_addr_index_data *aidata =
19798 (struct dwarf2_read_addr_index_data *) data;
19800 aidata->addr_base = cu->addr_base;
19801 aidata->addr_size = cu->header.addr_size;
19804 /* Given an index in .debug_addr, fetch the value.
19805 NOTE: This can be called during dwarf expression evaluation,
19806 long after the debug information has been read, and thus per_cu->cu
19807 may no longer exist. */
19810 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19811 unsigned int addr_index)
19813 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19814 struct objfile *objfile = dwarf2_per_objfile->objfile;
19815 struct dwarf2_cu *cu = per_cu->cu;
19816 ULONGEST addr_base;
19819 /* We need addr_base and addr_size.
19820 If we don't have PER_CU->cu, we have to get it.
19821 Nasty, but the alternative is storing the needed info in PER_CU,
19822 which at this point doesn't seem justified: it's not clear how frequently
19823 it would get used and it would increase the size of every PER_CU.
19824 Entry points like dwarf2_per_cu_addr_size do a similar thing
19825 so we're not in uncharted territory here.
19826 Alas we need to be a bit more complicated as addr_base is contained
19829 We don't need to read the entire CU(/TU).
19830 We just need the header and top level die.
19832 IWBN to use the aging mechanism to let us lazily later discard the CU.
19833 For now we skip this optimization. */
19837 addr_base = cu->addr_base;
19838 addr_size = cu->header.addr_size;
19842 struct dwarf2_read_addr_index_data aidata;
19844 /* Note: We can't use init_cutu_and_read_dies_simple here,
19845 we need addr_base. */
19846 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19847 dwarf2_read_addr_index_reader, &aidata);
19848 addr_base = aidata.addr_base;
19849 addr_size = aidata.addr_size;
19852 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19856 /* Given a DW_FORM_GNU_str_index, fetch the string.
19857 This is only used by the Fission support. */
19859 static const char *
19860 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19862 struct dwarf2_cu *cu = reader->cu;
19863 struct dwarf2_per_objfile *dwarf2_per_objfile
19864 = cu->per_cu->dwarf2_per_objfile;
19865 struct objfile *objfile = dwarf2_per_objfile->objfile;
19866 const char *objf_name = objfile_name (objfile);
19867 bfd *abfd = objfile->obfd;
19868 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19869 struct dwarf2_section_info *str_offsets_section =
19870 &reader->dwo_file->sections.str_offsets;
19871 const gdb_byte *info_ptr;
19872 ULONGEST str_offset;
19873 static const char form_name[] = "DW_FORM_GNU_str_index";
19875 dwarf2_read_section (objfile, str_section);
19876 dwarf2_read_section (objfile, str_offsets_section);
19877 if (str_section->buffer == NULL)
19878 error (_("%s used without .debug_str.dwo section"
19879 " in CU at offset 0x%x [in module %s]"),
19880 form_name, to_underlying (cu->header.sect_off), objf_name);
19881 if (str_offsets_section->buffer == NULL)
19882 error (_("%s used without .debug_str_offsets.dwo section"
19883 " in CU at offset 0x%x [in module %s]"),
19884 form_name, to_underlying (cu->header.sect_off), objf_name);
19885 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19886 error (_("%s pointing outside of .debug_str_offsets.dwo"
19887 " section in CU at offset 0x%x [in module %s]"),
19888 form_name, to_underlying (cu->header.sect_off), objf_name);
19889 info_ptr = (str_offsets_section->buffer
19890 + str_index * cu->header.offset_size);
19891 if (cu->header.offset_size == 4)
19892 str_offset = bfd_get_32 (abfd, info_ptr);
19894 str_offset = bfd_get_64 (abfd, info_ptr);
19895 if (str_offset >= str_section->size)
19896 error (_("Offset from %s pointing outside of"
19897 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19898 form_name, to_underlying (cu->header.sect_off), objf_name);
19899 return (const char *) (str_section->buffer + str_offset);
19902 /* Return the length of an LEB128 number in BUF. */
19905 leb128_size (const gdb_byte *buf)
19907 const gdb_byte *begin = buf;
19913 if ((byte & 128) == 0)
19914 return buf - begin;
19919 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19928 cu->language = language_c;
19931 case DW_LANG_C_plus_plus:
19932 case DW_LANG_C_plus_plus_11:
19933 case DW_LANG_C_plus_plus_14:
19934 cu->language = language_cplus;
19937 cu->language = language_d;
19939 case DW_LANG_Fortran77:
19940 case DW_LANG_Fortran90:
19941 case DW_LANG_Fortran95:
19942 case DW_LANG_Fortran03:
19943 case DW_LANG_Fortran08:
19944 cu->language = language_fortran;
19947 cu->language = language_go;
19949 case DW_LANG_Mips_Assembler:
19950 cu->language = language_asm;
19952 case DW_LANG_Ada83:
19953 case DW_LANG_Ada95:
19954 cu->language = language_ada;
19956 case DW_LANG_Modula2:
19957 cu->language = language_m2;
19959 case DW_LANG_Pascal83:
19960 cu->language = language_pascal;
19963 cu->language = language_objc;
19966 case DW_LANG_Rust_old:
19967 cu->language = language_rust;
19969 case DW_LANG_Cobol74:
19970 case DW_LANG_Cobol85:
19972 cu->language = language_minimal;
19975 cu->language_defn = language_def (cu->language);
19978 /* Return the named attribute or NULL if not there. */
19980 static struct attribute *
19981 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19986 struct attribute *spec = NULL;
19988 for (i = 0; i < die->num_attrs; ++i)
19990 if (die->attrs[i].name == name)
19991 return &die->attrs[i];
19992 if (die->attrs[i].name == DW_AT_specification
19993 || die->attrs[i].name == DW_AT_abstract_origin)
19994 spec = &die->attrs[i];
20000 die = follow_die_ref (die, spec, &cu);
20006 /* Return the named attribute or NULL if not there,
20007 but do not follow DW_AT_specification, etc.
20008 This is for use in contexts where we're reading .debug_types dies.
20009 Following DW_AT_specification, DW_AT_abstract_origin will take us
20010 back up the chain, and we want to go down. */
20012 static struct attribute *
20013 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20017 for (i = 0; i < die->num_attrs; ++i)
20018 if (die->attrs[i].name == name)
20019 return &die->attrs[i];
20024 /* Return the string associated with a string-typed attribute, or NULL if it
20025 is either not found or is of an incorrect type. */
20027 static const char *
20028 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20030 struct attribute *attr;
20031 const char *str = NULL;
20033 attr = dwarf2_attr (die, name, cu);
20037 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20038 || attr->form == DW_FORM_string
20039 || attr->form == DW_FORM_GNU_str_index
20040 || attr->form == DW_FORM_GNU_strp_alt)
20041 str = DW_STRING (attr);
20043 complaint (&symfile_complaints,
20044 _("string type expected for attribute %s for "
20045 "DIE at 0x%x in module %s"),
20046 dwarf_attr_name (name), to_underlying (die->sect_off),
20047 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20053 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20054 and holds a non-zero value. This function should only be used for
20055 DW_FORM_flag or DW_FORM_flag_present attributes. */
20058 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20060 struct attribute *attr = dwarf2_attr (die, name, cu);
20062 return (attr && DW_UNSND (attr));
20066 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20068 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20069 which value is non-zero. However, we have to be careful with
20070 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20071 (via dwarf2_flag_true_p) follows this attribute. So we may
20072 end up accidently finding a declaration attribute that belongs
20073 to a different DIE referenced by the specification attribute,
20074 even though the given DIE does not have a declaration attribute. */
20075 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20076 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20079 /* Return the die giving the specification for DIE, if there is
20080 one. *SPEC_CU is the CU containing DIE on input, and the CU
20081 containing the return value on output. If there is no
20082 specification, but there is an abstract origin, that is
20085 static struct die_info *
20086 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20088 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20091 if (spec_attr == NULL)
20092 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20094 if (spec_attr == NULL)
20097 return follow_die_ref (die, spec_attr, spec_cu);
20100 /* Stub for free_line_header to match void * callback types. */
20103 free_line_header_voidp (void *arg)
20105 struct line_header *lh = (struct line_header *) arg;
20111 line_header::add_include_dir (const char *include_dir)
20113 if (dwarf_line_debug >= 2)
20114 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20115 include_dirs.size () + 1, include_dir);
20117 include_dirs.push_back (include_dir);
20121 line_header::add_file_name (const char *name,
20123 unsigned int mod_time,
20124 unsigned int length)
20126 if (dwarf_line_debug >= 2)
20127 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20128 (unsigned) file_names.size () + 1, name);
20130 file_names.emplace_back (name, d_index, mod_time, length);
20133 /* A convenience function to find the proper .debug_line section for a CU. */
20135 static struct dwarf2_section_info *
20136 get_debug_line_section (struct dwarf2_cu *cu)
20138 struct dwarf2_section_info *section;
20139 struct dwarf2_per_objfile *dwarf2_per_objfile
20140 = cu->per_cu->dwarf2_per_objfile;
20142 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20144 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20145 section = &cu->dwo_unit->dwo_file->sections.line;
20146 else if (cu->per_cu->is_dwz)
20148 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20150 section = &dwz->line;
20153 section = &dwarf2_per_objfile->line;
20158 /* Read directory or file name entry format, starting with byte of
20159 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20160 entries count and the entries themselves in the described entry
20164 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20165 bfd *abfd, const gdb_byte **bufp,
20166 struct line_header *lh,
20167 const struct comp_unit_head *cu_header,
20168 void (*callback) (struct line_header *lh,
20171 unsigned int mod_time,
20172 unsigned int length))
20174 gdb_byte format_count, formati;
20175 ULONGEST data_count, datai;
20176 const gdb_byte *buf = *bufp;
20177 const gdb_byte *format_header_data;
20178 unsigned int bytes_read;
20180 format_count = read_1_byte (abfd, buf);
20182 format_header_data = buf;
20183 for (formati = 0; formati < format_count; formati++)
20185 read_unsigned_leb128 (abfd, buf, &bytes_read);
20187 read_unsigned_leb128 (abfd, buf, &bytes_read);
20191 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20193 for (datai = 0; datai < data_count; datai++)
20195 const gdb_byte *format = format_header_data;
20196 struct file_entry fe;
20198 for (formati = 0; formati < format_count; formati++)
20200 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20201 format += bytes_read;
20203 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20204 format += bytes_read;
20206 gdb::optional<const char *> string;
20207 gdb::optional<unsigned int> uint;
20211 case DW_FORM_string:
20212 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20216 case DW_FORM_line_strp:
20217 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20224 case DW_FORM_data1:
20225 uint.emplace (read_1_byte (abfd, buf));
20229 case DW_FORM_data2:
20230 uint.emplace (read_2_bytes (abfd, buf));
20234 case DW_FORM_data4:
20235 uint.emplace (read_4_bytes (abfd, buf));
20239 case DW_FORM_data8:
20240 uint.emplace (read_8_bytes (abfd, buf));
20244 case DW_FORM_udata:
20245 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20249 case DW_FORM_block:
20250 /* It is valid only for DW_LNCT_timestamp which is ignored by
20255 switch (content_type)
20258 if (string.has_value ())
20261 case DW_LNCT_directory_index:
20262 if (uint.has_value ())
20263 fe.d_index = (dir_index) *uint;
20265 case DW_LNCT_timestamp:
20266 if (uint.has_value ())
20267 fe.mod_time = *uint;
20270 if (uint.has_value ())
20276 complaint (&symfile_complaints,
20277 _("Unknown format content type %s"),
20278 pulongest (content_type));
20282 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20288 /* Read the statement program header starting at OFFSET in
20289 .debug_line, or .debug_line.dwo. Return a pointer
20290 to a struct line_header, allocated using xmalloc.
20291 Returns NULL if there is a problem reading the header, e.g., if it
20292 has a version we don't understand.
20294 NOTE: the strings in the include directory and file name tables of
20295 the returned object point into the dwarf line section buffer,
20296 and must not be freed. */
20298 static line_header_up
20299 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20301 const gdb_byte *line_ptr;
20302 unsigned int bytes_read, offset_size;
20304 const char *cur_dir, *cur_file;
20305 struct dwarf2_section_info *section;
20307 struct dwarf2_per_objfile *dwarf2_per_objfile
20308 = cu->per_cu->dwarf2_per_objfile;
20310 section = get_debug_line_section (cu);
20311 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20312 if (section->buffer == NULL)
20314 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20315 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20317 complaint (&symfile_complaints, _("missing .debug_line section"));
20321 /* We can't do this until we know the section is non-empty.
20322 Only then do we know we have such a section. */
20323 abfd = get_section_bfd_owner (section);
20325 /* Make sure that at least there's room for the total_length field.
20326 That could be 12 bytes long, but we're just going to fudge that. */
20327 if (to_underlying (sect_off) + 4 >= section->size)
20329 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20333 line_header_up lh (new line_header ());
20335 lh->sect_off = sect_off;
20336 lh->offset_in_dwz = cu->per_cu->is_dwz;
20338 line_ptr = section->buffer + to_underlying (sect_off);
20340 /* Read in the header. */
20342 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20343 &bytes_read, &offset_size);
20344 line_ptr += bytes_read;
20345 if (line_ptr + lh->total_length > (section->buffer + section->size))
20347 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20350 lh->statement_program_end = line_ptr + lh->total_length;
20351 lh->version = read_2_bytes (abfd, line_ptr);
20353 if (lh->version > 5)
20355 /* This is a version we don't understand. The format could have
20356 changed in ways we don't handle properly so just punt. */
20357 complaint (&symfile_complaints,
20358 _("unsupported version in .debug_line section"));
20361 if (lh->version >= 5)
20363 gdb_byte segment_selector_size;
20365 /* Skip address size. */
20366 read_1_byte (abfd, line_ptr);
20369 segment_selector_size = read_1_byte (abfd, line_ptr);
20371 if (segment_selector_size != 0)
20373 complaint (&symfile_complaints,
20374 _("unsupported segment selector size %u "
20375 "in .debug_line section"),
20376 segment_selector_size);
20380 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20381 line_ptr += offset_size;
20382 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20384 if (lh->version >= 4)
20386 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20390 lh->maximum_ops_per_instruction = 1;
20392 if (lh->maximum_ops_per_instruction == 0)
20394 lh->maximum_ops_per_instruction = 1;
20395 complaint (&symfile_complaints,
20396 _("invalid maximum_ops_per_instruction "
20397 "in `.debug_line' section"));
20400 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20402 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20404 lh->line_range = read_1_byte (abfd, line_ptr);
20406 lh->opcode_base = read_1_byte (abfd, line_ptr);
20408 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20410 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20411 for (i = 1; i < lh->opcode_base; ++i)
20413 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20417 if (lh->version >= 5)
20419 /* Read directory table. */
20420 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20422 [] (struct line_header *lh, const char *name,
20423 dir_index d_index, unsigned int mod_time,
20424 unsigned int length)
20426 lh->add_include_dir (name);
20429 /* Read file name table. */
20430 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20432 [] (struct line_header *lh, const char *name,
20433 dir_index d_index, unsigned int mod_time,
20434 unsigned int length)
20436 lh->add_file_name (name, d_index, mod_time, length);
20441 /* Read directory table. */
20442 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20444 line_ptr += bytes_read;
20445 lh->add_include_dir (cur_dir);
20447 line_ptr += bytes_read;
20449 /* Read file name table. */
20450 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20452 unsigned int mod_time, length;
20455 line_ptr += bytes_read;
20456 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20457 line_ptr += bytes_read;
20458 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20459 line_ptr += bytes_read;
20460 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20461 line_ptr += bytes_read;
20463 lh->add_file_name (cur_file, d_index, mod_time, length);
20465 line_ptr += bytes_read;
20467 lh->statement_program_start = line_ptr;
20469 if (line_ptr > (section->buffer + section->size))
20470 complaint (&symfile_complaints,
20471 _("line number info header doesn't "
20472 "fit in `.debug_line' section"));
20477 /* Subroutine of dwarf_decode_lines to simplify it.
20478 Return the file name of the psymtab for included file FILE_INDEX
20479 in line header LH of PST.
20480 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20481 If space for the result is malloc'd, it will be freed by a cleanup.
20482 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20484 The function creates dangling cleanup registration. */
20486 static const char *
20487 psymtab_include_file_name (const struct line_header *lh, int file_index,
20488 const struct partial_symtab *pst,
20489 const char *comp_dir)
20491 const file_entry &fe = lh->file_names[file_index];
20492 const char *include_name = fe.name;
20493 const char *include_name_to_compare = include_name;
20494 const char *pst_filename;
20495 char *copied_name = NULL;
20498 const char *dir_name = fe.include_dir (lh);
20500 if (!IS_ABSOLUTE_PATH (include_name)
20501 && (dir_name != NULL || comp_dir != NULL))
20503 /* Avoid creating a duplicate psymtab for PST.
20504 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20505 Before we do the comparison, however, we need to account
20506 for DIR_NAME and COMP_DIR.
20507 First prepend dir_name (if non-NULL). If we still don't
20508 have an absolute path prepend comp_dir (if non-NULL).
20509 However, the directory we record in the include-file's
20510 psymtab does not contain COMP_DIR (to match the
20511 corresponding symtab(s)).
20516 bash$ gcc -g ./hello.c
20517 include_name = "hello.c"
20519 DW_AT_comp_dir = comp_dir = "/tmp"
20520 DW_AT_name = "./hello.c"
20524 if (dir_name != NULL)
20526 char *tem = concat (dir_name, SLASH_STRING,
20527 include_name, (char *)NULL);
20529 make_cleanup (xfree, tem);
20530 include_name = tem;
20531 include_name_to_compare = include_name;
20533 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20535 char *tem = concat (comp_dir, SLASH_STRING,
20536 include_name, (char *)NULL);
20538 make_cleanup (xfree, tem);
20539 include_name_to_compare = tem;
20543 pst_filename = pst->filename;
20544 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20546 copied_name = concat (pst->dirname, SLASH_STRING,
20547 pst_filename, (char *)NULL);
20548 pst_filename = copied_name;
20551 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20553 if (copied_name != NULL)
20554 xfree (copied_name);
20558 return include_name;
20561 /* State machine to track the state of the line number program. */
20563 class lnp_state_machine
20566 /* Initialize a machine state for the start of a line number
20568 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20570 file_entry *current_file ()
20572 /* lh->file_names is 0-based, but the file name numbers in the
20573 statement program are 1-based. */
20574 return m_line_header->file_name_at (m_file);
20577 /* Record the line in the state machine. END_SEQUENCE is true if
20578 we're processing the end of a sequence. */
20579 void record_line (bool end_sequence);
20581 /* Check address and if invalid nop-out the rest of the lines in this
20583 void check_line_address (struct dwarf2_cu *cu,
20584 const gdb_byte *line_ptr,
20585 CORE_ADDR lowpc, CORE_ADDR address);
20587 void handle_set_discriminator (unsigned int discriminator)
20589 m_discriminator = discriminator;
20590 m_line_has_non_zero_discriminator |= discriminator != 0;
20593 /* Handle DW_LNE_set_address. */
20594 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20597 address += baseaddr;
20598 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20601 /* Handle DW_LNS_advance_pc. */
20602 void handle_advance_pc (CORE_ADDR adjust);
20604 /* Handle a special opcode. */
20605 void handle_special_opcode (unsigned char op_code);
20607 /* Handle DW_LNS_advance_line. */
20608 void handle_advance_line (int line_delta)
20610 advance_line (line_delta);
20613 /* Handle DW_LNS_set_file. */
20614 void handle_set_file (file_name_index file);
20616 /* Handle DW_LNS_negate_stmt. */
20617 void handle_negate_stmt ()
20619 m_is_stmt = !m_is_stmt;
20622 /* Handle DW_LNS_const_add_pc. */
20623 void handle_const_add_pc ();
20625 /* Handle DW_LNS_fixed_advance_pc. */
20626 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20628 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20632 /* Handle DW_LNS_copy. */
20633 void handle_copy ()
20635 record_line (false);
20636 m_discriminator = 0;
20639 /* Handle DW_LNE_end_sequence. */
20640 void handle_end_sequence ()
20642 m_record_line_callback = ::record_line;
20646 /* Advance the line by LINE_DELTA. */
20647 void advance_line (int line_delta)
20649 m_line += line_delta;
20651 if (line_delta != 0)
20652 m_line_has_non_zero_discriminator = m_discriminator != 0;
20655 gdbarch *m_gdbarch;
20657 /* True if we're recording lines.
20658 Otherwise we're building partial symtabs and are just interested in
20659 finding include files mentioned by the line number program. */
20660 bool m_record_lines_p;
20662 /* The line number header. */
20663 line_header *m_line_header;
20665 /* These are part of the standard DWARF line number state machine,
20666 and initialized according to the DWARF spec. */
20668 unsigned char m_op_index = 0;
20669 /* The line table index (1-based) of the current file. */
20670 file_name_index m_file = (file_name_index) 1;
20671 unsigned int m_line = 1;
20673 /* These are initialized in the constructor. */
20675 CORE_ADDR m_address;
20677 unsigned int m_discriminator;
20679 /* Additional bits of state we need to track. */
20681 /* The last file that we called dwarf2_start_subfile for.
20682 This is only used for TLLs. */
20683 unsigned int m_last_file = 0;
20684 /* The last file a line number was recorded for. */
20685 struct subfile *m_last_subfile = NULL;
20687 /* The function to call to record a line. */
20688 record_line_ftype *m_record_line_callback = NULL;
20690 /* The last line number that was recorded, used to coalesce
20691 consecutive entries for the same line. This can happen, for
20692 example, when discriminators are present. PR 17276. */
20693 unsigned int m_last_line = 0;
20694 bool m_line_has_non_zero_discriminator = false;
20698 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20700 CORE_ADDR addr_adj = (((m_op_index + adjust)
20701 / m_line_header->maximum_ops_per_instruction)
20702 * m_line_header->minimum_instruction_length);
20703 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20704 m_op_index = ((m_op_index + adjust)
20705 % m_line_header->maximum_ops_per_instruction);
20709 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20711 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20712 CORE_ADDR addr_adj = (((m_op_index
20713 + (adj_opcode / m_line_header->line_range))
20714 / m_line_header->maximum_ops_per_instruction)
20715 * m_line_header->minimum_instruction_length);
20716 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20717 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20718 % m_line_header->maximum_ops_per_instruction);
20720 int line_delta = (m_line_header->line_base
20721 + (adj_opcode % m_line_header->line_range));
20722 advance_line (line_delta);
20723 record_line (false);
20724 m_discriminator = 0;
20728 lnp_state_machine::handle_set_file (file_name_index file)
20732 const file_entry *fe = current_file ();
20734 dwarf2_debug_line_missing_file_complaint ();
20735 else if (m_record_lines_p)
20737 const char *dir = fe->include_dir (m_line_header);
20739 m_last_subfile = current_subfile;
20740 m_line_has_non_zero_discriminator = m_discriminator != 0;
20741 dwarf2_start_subfile (fe->name, dir);
20746 lnp_state_machine::handle_const_add_pc ()
20749 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20752 = (((m_op_index + adjust)
20753 / m_line_header->maximum_ops_per_instruction)
20754 * m_line_header->minimum_instruction_length);
20756 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20757 m_op_index = ((m_op_index + adjust)
20758 % m_line_header->maximum_ops_per_instruction);
20761 /* Ignore this record_line request. */
20764 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20769 /* Return non-zero if we should add LINE to the line number table.
20770 LINE is the line to add, LAST_LINE is the last line that was added,
20771 LAST_SUBFILE is the subfile for LAST_LINE.
20772 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20773 had a non-zero discriminator.
20775 We have to be careful in the presence of discriminators.
20776 E.g., for this line:
20778 for (i = 0; i < 100000; i++);
20780 clang can emit four line number entries for that one line,
20781 each with a different discriminator.
20782 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20784 However, we want gdb to coalesce all four entries into one.
20785 Otherwise the user could stepi into the middle of the line and
20786 gdb would get confused about whether the pc really was in the
20787 middle of the line.
20789 Things are further complicated by the fact that two consecutive
20790 line number entries for the same line is a heuristic used by gcc
20791 to denote the end of the prologue. So we can't just discard duplicate
20792 entries, we have to be selective about it. The heuristic we use is
20793 that we only collapse consecutive entries for the same line if at least
20794 one of those entries has a non-zero discriminator. PR 17276.
20796 Note: Addresses in the line number state machine can never go backwards
20797 within one sequence, thus this coalescing is ok. */
20800 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20801 int line_has_non_zero_discriminator,
20802 struct subfile *last_subfile)
20804 if (current_subfile != last_subfile)
20806 if (line != last_line)
20808 /* Same line for the same file that we've seen already.
20809 As a last check, for pr 17276, only record the line if the line
20810 has never had a non-zero discriminator. */
20811 if (!line_has_non_zero_discriminator)
20816 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20817 in the line table of subfile SUBFILE. */
20820 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20821 unsigned int line, CORE_ADDR address,
20822 record_line_ftype p_record_line)
20824 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20826 if (dwarf_line_debug)
20828 fprintf_unfiltered (gdb_stdlog,
20829 "Recording line %u, file %s, address %s\n",
20830 line, lbasename (subfile->name),
20831 paddress (gdbarch, address));
20834 (*p_record_line) (subfile, line, addr);
20837 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20838 Mark the end of a set of line number records.
20839 The arguments are the same as for dwarf_record_line_1.
20840 If SUBFILE is NULL the request is ignored. */
20843 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20844 CORE_ADDR address, record_line_ftype p_record_line)
20846 if (subfile == NULL)
20849 if (dwarf_line_debug)
20851 fprintf_unfiltered (gdb_stdlog,
20852 "Finishing current line, file %s, address %s\n",
20853 lbasename (subfile->name),
20854 paddress (gdbarch, address));
20857 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20861 lnp_state_machine::record_line (bool end_sequence)
20863 if (dwarf_line_debug)
20865 fprintf_unfiltered (gdb_stdlog,
20866 "Processing actual line %u: file %u,"
20867 " address %s, is_stmt %u, discrim %u\n",
20868 m_line, to_underlying (m_file),
20869 paddress (m_gdbarch, m_address),
20870 m_is_stmt, m_discriminator);
20873 file_entry *fe = current_file ();
20876 dwarf2_debug_line_missing_file_complaint ();
20877 /* For now we ignore lines not starting on an instruction boundary.
20878 But not when processing end_sequence for compatibility with the
20879 previous version of the code. */
20880 else if (m_op_index == 0 || end_sequence)
20882 fe->included_p = 1;
20883 if (m_record_lines_p && m_is_stmt)
20885 if (m_last_subfile != current_subfile || end_sequence)
20887 dwarf_finish_line (m_gdbarch, m_last_subfile,
20888 m_address, m_record_line_callback);
20893 if (dwarf_record_line_p (m_line, m_last_line,
20894 m_line_has_non_zero_discriminator,
20897 dwarf_record_line_1 (m_gdbarch, current_subfile,
20899 m_record_line_callback);
20901 m_last_subfile = current_subfile;
20902 m_last_line = m_line;
20908 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20909 bool record_lines_p)
20912 m_record_lines_p = record_lines_p;
20913 m_line_header = lh;
20915 m_record_line_callback = ::record_line;
20917 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20918 was a line entry for it so that the backend has a chance to adjust it
20919 and also record it in case it needs it. This is currently used by MIPS
20920 code, cf. `mips_adjust_dwarf2_line'. */
20921 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20922 m_is_stmt = lh->default_is_stmt;
20923 m_discriminator = 0;
20927 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20928 const gdb_byte *line_ptr,
20929 CORE_ADDR lowpc, CORE_ADDR address)
20931 /* If address < lowpc then it's not a usable value, it's outside the
20932 pc range of the CU. However, we restrict the test to only address
20933 values of zero to preserve GDB's previous behaviour which is to
20934 handle the specific case of a function being GC'd by the linker. */
20936 if (address == 0 && address < lowpc)
20938 /* This line table is for a function which has been
20939 GCd by the linker. Ignore it. PR gdb/12528 */
20941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20942 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20944 complaint (&symfile_complaints,
20945 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20946 line_offset, objfile_name (objfile));
20947 m_record_line_callback = noop_record_line;
20948 /* Note: record_line_callback is left as noop_record_line until
20949 we see DW_LNE_end_sequence. */
20953 /* Subroutine of dwarf_decode_lines to simplify it.
20954 Process the line number information in LH.
20955 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20956 program in order to set included_p for every referenced header. */
20959 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20960 const int decode_for_pst_p, CORE_ADDR lowpc)
20962 const gdb_byte *line_ptr, *extended_end;
20963 const gdb_byte *line_end;
20964 unsigned int bytes_read, extended_len;
20965 unsigned char op_code, extended_op;
20966 CORE_ADDR baseaddr;
20967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20968 bfd *abfd = objfile->obfd;
20969 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20970 /* True if we're recording line info (as opposed to building partial
20971 symtabs and just interested in finding include files mentioned by
20972 the line number program). */
20973 bool record_lines_p = !decode_for_pst_p;
20975 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20977 line_ptr = lh->statement_program_start;
20978 line_end = lh->statement_program_end;
20980 /* Read the statement sequences until there's nothing left. */
20981 while (line_ptr < line_end)
20983 /* The DWARF line number program state machine. Reset the state
20984 machine at the start of each sequence. */
20985 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20986 bool end_sequence = false;
20988 if (record_lines_p)
20990 /* Start a subfile for the current file of the state
20992 const file_entry *fe = state_machine.current_file ();
20995 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20998 /* Decode the table. */
20999 while (line_ptr < line_end && !end_sequence)
21001 op_code = read_1_byte (abfd, line_ptr);
21004 if (op_code >= lh->opcode_base)
21006 /* Special opcode. */
21007 state_machine.handle_special_opcode (op_code);
21009 else switch (op_code)
21011 case DW_LNS_extended_op:
21012 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21014 line_ptr += bytes_read;
21015 extended_end = line_ptr + extended_len;
21016 extended_op = read_1_byte (abfd, line_ptr);
21018 switch (extended_op)
21020 case DW_LNE_end_sequence:
21021 state_machine.handle_end_sequence ();
21022 end_sequence = true;
21024 case DW_LNE_set_address:
21027 = read_address (abfd, line_ptr, cu, &bytes_read);
21028 line_ptr += bytes_read;
21030 state_machine.check_line_address (cu, line_ptr,
21032 state_machine.handle_set_address (baseaddr, address);
21035 case DW_LNE_define_file:
21037 const char *cur_file;
21038 unsigned int mod_time, length;
21041 cur_file = read_direct_string (abfd, line_ptr,
21043 line_ptr += bytes_read;
21044 dindex = (dir_index)
21045 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21046 line_ptr += bytes_read;
21048 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21049 line_ptr += bytes_read;
21051 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21052 line_ptr += bytes_read;
21053 lh->add_file_name (cur_file, dindex, mod_time, length);
21056 case DW_LNE_set_discriminator:
21058 /* The discriminator is not interesting to the
21059 debugger; just ignore it. We still need to
21060 check its value though:
21061 if there are consecutive entries for the same
21062 (non-prologue) line we want to coalesce them.
21065 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21066 line_ptr += bytes_read;
21068 state_machine.handle_set_discriminator (discr);
21072 complaint (&symfile_complaints,
21073 _("mangled .debug_line section"));
21076 /* Make sure that we parsed the extended op correctly. If e.g.
21077 we expected a different address size than the producer used,
21078 we may have read the wrong number of bytes. */
21079 if (line_ptr != extended_end)
21081 complaint (&symfile_complaints,
21082 _("mangled .debug_line section"));
21087 state_machine.handle_copy ();
21089 case DW_LNS_advance_pc:
21092 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21093 line_ptr += bytes_read;
21095 state_machine.handle_advance_pc (adjust);
21098 case DW_LNS_advance_line:
21101 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21102 line_ptr += bytes_read;
21104 state_machine.handle_advance_line (line_delta);
21107 case DW_LNS_set_file:
21109 file_name_index file
21110 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21112 line_ptr += bytes_read;
21114 state_machine.handle_set_file (file);
21117 case DW_LNS_set_column:
21118 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21119 line_ptr += bytes_read;
21121 case DW_LNS_negate_stmt:
21122 state_machine.handle_negate_stmt ();
21124 case DW_LNS_set_basic_block:
21126 /* Add to the address register of the state machine the
21127 address increment value corresponding to special opcode
21128 255. I.e., this value is scaled by the minimum
21129 instruction length since special opcode 255 would have
21130 scaled the increment. */
21131 case DW_LNS_const_add_pc:
21132 state_machine.handle_const_add_pc ();
21134 case DW_LNS_fixed_advance_pc:
21136 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21139 state_machine.handle_fixed_advance_pc (addr_adj);
21144 /* Unknown standard opcode, ignore it. */
21147 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21149 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21150 line_ptr += bytes_read;
21157 dwarf2_debug_line_missing_end_sequence_complaint ();
21159 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21160 in which case we still finish recording the last line). */
21161 state_machine.record_line (true);
21165 /* Decode the Line Number Program (LNP) for the given line_header
21166 structure and CU. The actual information extracted and the type
21167 of structures created from the LNP depends on the value of PST.
21169 1. If PST is NULL, then this procedure uses the data from the program
21170 to create all necessary symbol tables, and their linetables.
21172 2. If PST is not NULL, this procedure reads the program to determine
21173 the list of files included by the unit represented by PST, and
21174 builds all the associated partial symbol tables.
21176 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21177 It is used for relative paths in the line table.
21178 NOTE: When processing partial symtabs (pst != NULL),
21179 comp_dir == pst->dirname.
21181 NOTE: It is important that psymtabs have the same file name (via strcmp)
21182 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21183 symtab we don't use it in the name of the psymtabs we create.
21184 E.g. expand_line_sal requires this when finding psymtabs to expand.
21185 A good testcase for this is mb-inline.exp.
21187 LOWPC is the lowest address in CU (or 0 if not known).
21189 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21190 for its PC<->lines mapping information. Otherwise only the filename
21191 table is read in. */
21194 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21195 struct dwarf2_cu *cu, struct partial_symtab *pst,
21196 CORE_ADDR lowpc, int decode_mapping)
21198 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21199 const int decode_for_pst_p = (pst != NULL);
21201 if (decode_mapping)
21202 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21204 if (decode_for_pst_p)
21208 /* Now that we're done scanning the Line Header Program, we can
21209 create the psymtab of each included file. */
21210 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21211 if (lh->file_names[file_index].included_p == 1)
21213 const char *include_name =
21214 psymtab_include_file_name (lh, file_index, pst, comp_dir);
21215 if (include_name != NULL)
21216 dwarf2_create_include_psymtab (include_name, pst, objfile);
21221 /* Make sure a symtab is created for every file, even files
21222 which contain only variables (i.e. no code with associated
21224 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21227 for (i = 0; i < lh->file_names.size (); i++)
21229 file_entry &fe = lh->file_names[i];
21231 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21233 if (current_subfile->symtab == NULL)
21235 current_subfile->symtab
21236 = allocate_symtab (cust, current_subfile->name);
21238 fe.symtab = current_subfile->symtab;
21243 /* Start a subfile for DWARF. FILENAME is the name of the file and
21244 DIRNAME the name of the source directory which contains FILENAME
21245 or NULL if not known.
21246 This routine tries to keep line numbers from identical absolute and
21247 relative file names in a common subfile.
21249 Using the `list' example from the GDB testsuite, which resides in
21250 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21251 of /srcdir/list0.c yields the following debugging information for list0.c:
21253 DW_AT_name: /srcdir/list0.c
21254 DW_AT_comp_dir: /compdir
21255 files.files[0].name: list0.h
21256 files.files[0].dir: /srcdir
21257 files.files[1].name: list0.c
21258 files.files[1].dir: /srcdir
21260 The line number information for list0.c has to end up in a single
21261 subfile, so that `break /srcdir/list0.c:1' works as expected.
21262 start_subfile will ensure that this happens provided that we pass the
21263 concatenation of files.files[1].dir and files.files[1].name as the
21267 dwarf2_start_subfile (const char *filename, const char *dirname)
21271 /* In order not to lose the line information directory,
21272 we concatenate it to the filename when it makes sense.
21273 Note that the Dwarf3 standard says (speaking of filenames in line
21274 information): ``The directory index is ignored for file names
21275 that represent full path names''. Thus ignoring dirname in the
21276 `else' branch below isn't an issue. */
21278 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21280 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21284 start_subfile (filename);
21290 /* Start a symtab for DWARF.
21291 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21293 static struct compunit_symtab *
21294 dwarf2_start_symtab (struct dwarf2_cu *cu,
21295 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21297 struct compunit_symtab *cust
21298 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21299 low_pc, cu->language);
21301 record_debugformat ("DWARF 2");
21302 record_producer (cu->producer);
21304 /* We assume that we're processing GCC output. */
21305 processing_gcc_compilation = 2;
21307 cu->processing_has_namespace_info = 0;
21313 var_decode_location (struct attribute *attr, struct symbol *sym,
21314 struct dwarf2_cu *cu)
21316 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21317 struct comp_unit_head *cu_header = &cu->header;
21319 /* NOTE drow/2003-01-30: There used to be a comment and some special
21320 code here to turn a symbol with DW_AT_external and a
21321 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21322 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21323 with some versions of binutils) where shared libraries could have
21324 relocations against symbols in their debug information - the
21325 minimal symbol would have the right address, but the debug info
21326 would not. It's no longer necessary, because we will explicitly
21327 apply relocations when we read in the debug information now. */
21329 /* A DW_AT_location attribute with no contents indicates that a
21330 variable has been optimized away. */
21331 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21333 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21337 /* Handle one degenerate form of location expression specially, to
21338 preserve GDB's previous behavior when section offsets are
21339 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21340 then mark this symbol as LOC_STATIC. */
21342 if (attr_form_is_block (attr)
21343 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21344 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21345 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21346 && (DW_BLOCK (attr)->size
21347 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21349 unsigned int dummy;
21351 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21352 SYMBOL_VALUE_ADDRESS (sym) =
21353 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21355 SYMBOL_VALUE_ADDRESS (sym) =
21356 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21357 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21358 fixup_symbol_section (sym, objfile);
21359 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21360 SYMBOL_SECTION (sym));
21364 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21365 expression evaluator, and use LOC_COMPUTED only when necessary
21366 (i.e. when the value of a register or memory location is
21367 referenced, or a thread-local block, etc.). Then again, it might
21368 not be worthwhile. I'm assuming that it isn't unless performance
21369 or memory numbers show me otherwise. */
21371 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21373 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21374 cu->has_loclist = 1;
21377 /* Given a pointer to a DWARF information entry, figure out if we need
21378 to make a symbol table entry for it, and if so, create a new entry
21379 and return a pointer to it.
21380 If TYPE is NULL, determine symbol type from the die, otherwise
21381 used the passed type.
21382 If SPACE is not NULL, use it to hold the new symbol. If it is
21383 NULL, allocate a new symbol on the objfile's obstack. */
21385 static struct symbol *
21386 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21387 struct symbol *space)
21389 struct dwarf2_per_objfile *dwarf2_per_objfile
21390 = cu->per_cu->dwarf2_per_objfile;
21391 struct objfile *objfile = dwarf2_per_objfile->objfile;
21392 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21393 struct symbol *sym = NULL;
21395 struct attribute *attr = NULL;
21396 struct attribute *attr2 = NULL;
21397 CORE_ADDR baseaddr;
21398 struct pending **list_to_add = NULL;
21400 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21402 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21404 name = dwarf2_name (die, cu);
21407 const char *linkagename;
21408 int suppress_add = 0;
21413 sym = allocate_symbol (objfile);
21414 OBJSTAT (objfile, n_syms++);
21416 /* Cache this symbol's name and the name's demangled form (if any). */
21417 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21418 linkagename = dwarf2_physname (name, die, cu);
21419 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21421 /* Fortran does not have mangling standard and the mangling does differ
21422 between gfortran, iFort etc. */
21423 if (cu->language == language_fortran
21424 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21425 symbol_set_demangled_name (&(sym->ginfo),
21426 dwarf2_full_name (name, die, cu),
21429 /* Default assumptions.
21430 Use the passed type or decode it from the die. */
21431 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21432 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21434 SYMBOL_TYPE (sym) = type;
21436 SYMBOL_TYPE (sym) = die_type (die, cu);
21437 attr = dwarf2_attr (die,
21438 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21442 SYMBOL_LINE (sym) = DW_UNSND (attr);
21445 attr = dwarf2_attr (die,
21446 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21450 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21451 struct file_entry *fe;
21453 if (cu->line_header != NULL)
21454 fe = cu->line_header->file_name_at (file_index);
21459 complaint (&symfile_complaints,
21460 _("file index out of range"));
21462 symbol_set_symtab (sym, fe->symtab);
21468 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21473 addr = attr_value_as_address (attr);
21474 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21475 SYMBOL_VALUE_ADDRESS (sym) = addr;
21477 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21478 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21479 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21480 add_symbol_to_list (sym, cu->list_in_scope);
21482 case DW_TAG_subprogram:
21483 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21485 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21486 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21487 if ((attr2 && (DW_UNSND (attr2) != 0))
21488 || cu->language == language_ada)
21490 /* Subprograms marked external are stored as a global symbol.
21491 Ada subprograms, whether marked external or not, are always
21492 stored as a global symbol, because we want to be able to
21493 access them globally. For instance, we want to be able
21494 to break on a nested subprogram without having to
21495 specify the context. */
21496 list_to_add = &global_symbols;
21500 list_to_add = cu->list_in_scope;
21503 case DW_TAG_inlined_subroutine:
21504 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21506 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21507 SYMBOL_INLINED (sym) = 1;
21508 list_to_add = cu->list_in_scope;
21510 case DW_TAG_template_value_param:
21512 /* Fall through. */
21513 case DW_TAG_constant:
21514 case DW_TAG_variable:
21515 case DW_TAG_member:
21516 /* Compilation with minimal debug info may result in
21517 variables with missing type entries. Change the
21518 misleading `void' type to something sensible. */
21519 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21520 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21522 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21523 /* In the case of DW_TAG_member, we should only be called for
21524 static const members. */
21525 if (die->tag == DW_TAG_member)
21527 /* dwarf2_add_field uses die_is_declaration,
21528 so we do the same. */
21529 gdb_assert (die_is_declaration (die, cu));
21534 dwarf2_const_value (attr, sym, cu);
21535 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21538 if (attr2 && (DW_UNSND (attr2) != 0))
21539 list_to_add = &global_symbols;
21541 list_to_add = cu->list_in_scope;
21545 attr = dwarf2_attr (die, DW_AT_location, cu);
21548 var_decode_location (attr, sym, cu);
21549 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21551 /* Fortran explicitly imports any global symbols to the local
21552 scope by DW_TAG_common_block. */
21553 if (cu->language == language_fortran && die->parent
21554 && die->parent->tag == DW_TAG_common_block)
21557 if (SYMBOL_CLASS (sym) == LOC_STATIC
21558 && SYMBOL_VALUE_ADDRESS (sym) == 0
21559 && !dwarf2_per_objfile->has_section_at_zero)
21561 /* When a static variable is eliminated by the linker,
21562 the corresponding debug information is not stripped
21563 out, but the variable address is set to null;
21564 do not add such variables into symbol table. */
21566 else if (attr2 && (DW_UNSND (attr2) != 0))
21568 /* Workaround gfortran PR debug/40040 - it uses
21569 DW_AT_location for variables in -fPIC libraries which may
21570 get overriden by other libraries/executable and get
21571 a different address. Resolve it by the minimal symbol
21572 which may come from inferior's executable using copy
21573 relocation. Make this workaround only for gfortran as for
21574 other compilers GDB cannot guess the minimal symbol
21575 Fortran mangling kind. */
21576 if (cu->language == language_fortran && die->parent
21577 && die->parent->tag == DW_TAG_module
21579 && startswith (cu->producer, "GNU Fortran"))
21580 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21582 /* A variable with DW_AT_external is never static,
21583 but it may be block-scoped. */
21584 list_to_add = (cu->list_in_scope == &file_symbols
21585 ? &global_symbols : cu->list_in_scope);
21588 list_to_add = cu->list_in_scope;
21592 /* We do not know the address of this symbol.
21593 If it is an external symbol and we have type information
21594 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21595 The address of the variable will then be determined from
21596 the minimal symbol table whenever the variable is
21598 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21600 /* Fortran explicitly imports any global symbols to the local
21601 scope by DW_TAG_common_block. */
21602 if (cu->language == language_fortran && die->parent
21603 && die->parent->tag == DW_TAG_common_block)
21605 /* SYMBOL_CLASS doesn't matter here because
21606 read_common_block is going to reset it. */
21608 list_to_add = cu->list_in_scope;
21610 else if (attr2 && (DW_UNSND (attr2) != 0)
21611 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21613 /* A variable with DW_AT_external is never static, but it
21614 may be block-scoped. */
21615 list_to_add = (cu->list_in_scope == &file_symbols
21616 ? &global_symbols : cu->list_in_scope);
21618 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21620 else if (!die_is_declaration (die, cu))
21622 /* Use the default LOC_OPTIMIZED_OUT class. */
21623 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21625 list_to_add = cu->list_in_scope;
21629 case DW_TAG_formal_parameter:
21630 /* If we are inside a function, mark this as an argument. If
21631 not, we might be looking at an argument to an inlined function
21632 when we do not have enough information to show inlined frames;
21633 pretend it's a local variable in that case so that the user can
21635 if (context_stack_depth > 0
21636 && context_stack[context_stack_depth - 1].name != NULL)
21637 SYMBOL_IS_ARGUMENT (sym) = 1;
21638 attr = dwarf2_attr (die, DW_AT_location, cu);
21641 var_decode_location (attr, sym, cu);
21643 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21646 dwarf2_const_value (attr, sym, cu);
21649 list_to_add = cu->list_in_scope;
21651 case DW_TAG_unspecified_parameters:
21652 /* From varargs functions; gdb doesn't seem to have any
21653 interest in this information, so just ignore it for now.
21656 case DW_TAG_template_type_param:
21658 /* Fall through. */
21659 case DW_TAG_class_type:
21660 case DW_TAG_interface_type:
21661 case DW_TAG_structure_type:
21662 case DW_TAG_union_type:
21663 case DW_TAG_set_type:
21664 case DW_TAG_enumeration_type:
21665 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21666 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21669 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21670 really ever be static objects: otherwise, if you try
21671 to, say, break of a class's method and you're in a file
21672 which doesn't mention that class, it won't work unless
21673 the check for all static symbols in lookup_symbol_aux
21674 saves you. See the OtherFileClass tests in
21675 gdb.c++/namespace.exp. */
21679 list_to_add = (cu->list_in_scope == &file_symbols
21680 && cu->language == language_cplus
21681 ? &global_symbols : cu->list_in_scope);
21683 /* The semantics of C++ state that "struct foo {
21684 ... }" also defines a typedef for "foo". */
21685 if (cu->language == language_cplus
21686 || cu->language == language_ada
21687 || cu->language == language_d
21688 || cu->language == language_rust)
21690 /* The symbol's name is already allocated along
21691 with this objfile, so we don't need to
21692 duplicate it for the type. */
21693 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21694 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21699 case DW_TAG_typedef:
21700 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21701 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21702 list_to_add = cu->list_in_scope;
21704 case DW_TAG_base_type:
21705 case DW_TAG_subrange_type:
21706 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21707 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21708 list_to_add = cu->list_in_scope;
21710 case DW_TAG_enumerator:
21711 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21714 dwarf2_const_value (attr, sym, cu);
21717 /* NOTE: carlton/2003-11-10: See comment above in the
21718 DW_TAG_class_type, etc. block. */
21720 list_to_add = (cu->list_in_scope == &file_symbols
21721 && cu->language == language_cplus
21722 ? &global_symbols : cu->list_in_scope);
21725 case DW_TAG_imported_declaration:
21726 case DW_TAG_namespace:
21727 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21728 list_to_add = &global_symbols;
21730 case DW_TAG_module:
21731 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21732 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21733 list_to_add = &global_symbols;
21735 case DW_TAG_common_block:
21736 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21737 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21738 add_symbol_to_list (sym, cu->list_in_scope);
21741 /* Not a tag we recognize. Hopefully we aren't processing
21742 trash data, but since we must specifically ignore things
21743 we don't recognize, there is nothing else we should do at
21745 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21746 dwarf_tag_name (die->tag));
21752 sym->hash_next = objfile->template_symbols;
21753 objfile->template_symbols = sym;
21754 list_to_add = NULL;
21757 if (list_to_add != NULL)
21758 add_symbol_to_list (sym, list_to_add);
21760 /* For the benefit of old versions of GCC, check for anonymous
21761 namespaces based on the demangled name. */
21762 if (!cu->processing_has_namespace_info
21763 && cu->language == language_cplus)
21764 cp_scan_for_anonymous_namespaces (sym, objfile);
21769 /* A wrapper for new_symbol_full that always allocates a new symbol. */
21771 static struct symbol *
21772 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21774 return new_symbol_full (die, type, cu, NULL);
21777 /* Given an attr with a DW_FORM_dataN value in host byte order,
21778 zero-extend it as appropriate for the symbol's type. The DWARF
21779 standard (v4) is not entirely clear about the meaning of using
21780 DW_FORM_dataN for a constant with a signed type, where the type is
21781 wider than the data. The conclusion of a discussion on the DWARF
21782 list was that this is unspecified. We choose to always zero-extend
21783 because that is the interpretation long in use by GCC. */
21786 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21787 struct dwarf2_cu *cu, LONGEST *value, int bits)
21789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21790 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21791 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21792 LONGEST l = DW_UNSND (attr);
21794 if (bits < sizeof (*value) * 8)
21796 l &= ((LONGEST) 1 << bits) - 1;
21799 else if (bits == sizeof (*value) * 8)
21803 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21804 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21811 /* Read a constant value from an attribute. Either set *VALUE, or if
21812 the value does not fit in *VALUE, set *BYTES - either already
21813 allocated on the objfile obstack, or newly allocated on OBSTACK,
21814 or, set *BATON, if we translated the constant to a location
21818 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21819 const char *name, struct obstack *obstack,
21820 struct dwarf2_cu *cu,
21821 LONGEST *value, const gdb_byte **bytes,
21822 struct dwarf2_locexpr_baton **baton)
21824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21825 struct comp_unit_head *cu_header = &cu->header;
21826 struct dwarf_block *blk;
21827 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21828 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21834 switch (attr->form)
21837 case DW_FORM_GNU_addr_index:
21841 if (TYPE_LENGTH (type) != cu_header->addr_size)
21842 dwarf2_const_value_length_mismatch_complaint (name,
21843 cu_header->addr_size,
21844 TYPE_LENGTH (type));
21845 /* Symbols of this form are reasonably rare, so we just
21846 piggyback on the existing location code rather than writing
21847 a new implementation of symbol_computed_ops. */
21848 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21849 (*baton)->per_cu = cu->per_cu;
21850 gdb_assert ((*baton)->per_cu);
21852 (*baton)->size = 2 + cu_header->addr_size;
21853 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21854 (*baton)->data = data;
21856 data[0] = DW_OP_addr;
21857 store_unsigned_integer (&data[1], cu_header->addr_size,
21858 byte_order, DW_ADDR (attr));
21859 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21862 case DW_FORM_string:
21864 case DW_FORM_GNU_str_index:
21865 case DW_FORM_GNU_strp_alt:
21866 /* DW_STRING is already allocated on the objfile obstack, point
21868 *bytes = (const gdb_byte *) DW_STRING (attr);
21870 case DW_FORM_block1:
21871 case DW_FORM_block2:
21872 case DW_FORM_block4:
21873 case DW_FORM_block:
21874 case DW_FORM_exprloc:
21875 case DW_FORM_data16:
21876 blk = DW_BLOCK (attr);
21877 if (TYPE_LENGTH (type) != blk->size)
21878 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21879 TYPE_LENGTH (type));
21880 *bytes = blk->data;
21883 /* The DW_AT_const_value attributes are supposed to carry the
21884 symbol's value "represented as it would be on the target
21885 architecture." By the time we get here, it's already been
21886 converted to host endianness, so we just need to sign- or
21887 zero-extend it as appropriate. */
21888 case DW_FORM_data1:
21889 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21891 case DW_FORM_data2:
21892 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21894 case DW_FORM_data4:
21895 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21897 case DW_FORM_data8:
21898 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21901 case DW_FORM_sdata:
21902 case DW_FORM_implicit_const:
21903 *value = DW_SND (attr);
21906 case DW_FORM_udata:
21907 *value = DW_UNSND (attr);
21911 complaint (&symfile_complaints,
21912 _("unsupported const value attribute form: '%s'"),
21913 dwarf_form_name (attr->form));
21920 /* Copy constant value from an attribute to a symbol. */
21923 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21924 struct dwarf2_cu *cu)
21926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21928 const gdb_byte *bytes;
21929 struct dwarf2_locexpr_baton *baton;
21931 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21932 SYMBOL_PRINT_NAME (sym),
21933 &objfile->objfile_obstack, cu,
21934 &value, &bytes, &baton);
21938 SYMBOL_LOCATION_BATON (sym) = baton;
21939 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21941 else if (bytes != NULL)
21943 SYMBOL_VALUE_BYTES (sym) = bytes;
21944 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21948 SYMBOL_VALUE (sym) = value;
21949 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21953 /* Return the type of the die in question using its DW_AT_type attribute. */
21955 static struct type *
21956 die_type (struct die_info *die, struct dwarf2_cu *cu)
21958 struct attribute *type_attr;
21960 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21963 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21964 /* A missing DW_AT_type represents a void type. */
21965 return objfile_type (objfile)->builtin_void;
21968 return lookup_die_type (die, type_attr, cu);
21971 /* True iff CU's producer generates GNAT Ada auxiliary information
21972 that allows to find parallel types through that information instead
21973 of having to do expensive parallel lookups by type name. */
21976 need_gnat_info (struct dwarf2_cu *cu)
21978 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21979 of GNAT produces this auxiliary information, without any indication
21980 that it is produced. Part of enhancing the FSF version of GNAT
21981 to produce that information will be to put in place an indicator
21982 that we can use in order to determine whether the descriptive type
21983 info is available or not. One suggestion that has been made is
21984 to use a new attribute, attached to the CU die. For now, assume
21985 that the descriptive type info is not available. */
21989 /* Return the auxiliary type of the die in question using its
21990 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21991 attribute is not present. */
21993 static struct type *
21994 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21996 struct attribute *type_attr;
21998 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22002 return lookup_die_type (die, type_attr, cu);
22005 /* If DIE has a descriptive_type attribute, then set the TYPE's
22006 descriptive type accordingly. */
22009 set_descriptive_type (struct type *type, struct die_info *die,
22010 struct dwarf2_cu *cu)
22012 struct type *descriptive_type = die_descriptive_type (die, cu);
22014 if (descriptive_type)
22016 ALLOCATE_GNAT_AUX_TYPE (type);
22017 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22021 /* Return the containing type of the die in question using its
22022 DW_AT_containing_type attribute. */
22024 static struct type *
22025 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22027 struct attribute *type_attr;
22028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22030 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22032 error (_("Dwarf Error: Problem turning containing type into gdb type "
22033 "[in module %s]"), objfile_name (objfile));
22035 return lookup_die_type (die, type_attr, cu);
22038 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22040 static struct type *
22041 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22043 struct dwarf2_per_objfile *dwarf2_per_objfile
22044 = cu->per_cu->dwarf2_per_objfile;
22045 struct objfile *objfile = dwarf2_per_objfile->objfile;
22046 char *message, *saved;
22048 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
22049 objfile_name (objfile),
22050 to_underlying (cu->header.sect_off),
22051 to_underlying (die->sect_off));
22052 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22053 message, strlen (message));
22056 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22059 /* Look up the type of DIE in CU using its type attribute ATTR.
22060 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22061 DW_AT_containing_type.
22062 If there is no type substitute an error marker. */
22064 static struct type *
22065 lookup_die_type (struct die_info *die, const struct attribute *attr,
22066 struct dwarf2_cu *cu)
22068 struct dwarf2_per_objfile *dwarf2_per_objfile
22069 = cu->per_cu->dwarf2_per_objfile;
22070 struct objfile *objfile = dwarf2_per_objfile->objfile;
22071 struct type *this_type;
22073 gdb_assert (attr->name == DW_AT_type
22074 || attr->name == DW_AT_GNAT_descriptive_type
22075 || attr->name == DW_AT_containing_type);
22077 /* First see if we have it cached. */
22079 if (attr->form == DW_FORM_GNU_ref_alt)
22081 struct dwarf2_per_cu_data *per_cu;
22082 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22084 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22085 dwarf2_per_objfile);
22086 this_type = get_die_type_at_offset (sect_off, per_cu);
22088 else if (attr_form_is_ref (attr))
22090 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22092 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22094 else if (attr->form == DW_FORM_ref_sig8)
22096 ULONGEST signature = DW_SIGNATURE (attr);
22098 return get_signatured_type (die, signature, cu);
22102 complaint (&symfile_complaints,
22103 _("Dwarf Error: Bad type attribute %s in DIE"
22104 " at 0x%x [in module %s]"),
22105 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
22106 objfile_name (objfile));
22107 return build_error_marker_type (cu, die);
22110 /* If not cached we need to read it in. */
22112 if (this_type == NULL)
22114 struct die_info *type_die = NULL;
22115 struct dwarf2_cu *type_cu = cu;
22117 if (attr_form_is_ref (attr))
22118 type_die = follow_die_ref (die, attr, &type_cu);
22119 if (type_die == NULL)
22120 return build_error_marker_type (cu, die);
22121 /* If we find the type now, it's probably because the type came
22122 from an inter-CU reference and the type's CU got expanded before
22124 this_type = read_type_die (type_die, type_cu);
22127 /* If we still don't have a type use an error marker. */
22129 if (this_type == NULL)
22130 return build_error_marker_type (cu, die);
22135 /* Return the type in DIE, CU.
22136 Returns NULL for invalid types.
22138 This first does a lookup in die_type_hash,
22139 and only reads the die in if necessary.
22141 NOTE: This can be called when reading in partial or full symbols. */
22143 static struct type *
22144 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22146 struct type *this_type;
22148 this_type = get_die_type (die, cu);
22152 return read_type_die_1 (die, cu);
22155 /* Read the type in DIE, CU.
22156 Returns NULL for invalid types. */
22158 static struct type *
22159 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22161 struct type *this_type = NULL;
22165 case DW_TAG_class_type:
22166 case DW_TAG_interface_type:
22167 case DW_TAG_structure_type:
22168 case DW_TAG_union_type:
22169 this_type = read_structure_type (die, cu);
22171 case DW_TAG_enumeration_type:
22172 this_type = read_enumeration_type (die, cu);
22174 case DW_TAG_subprogram:
22175 case DW_TAG_subroutine_type:
22176 case DW_TAG_inlined_subroutine:
22177 this_type = read_subroutine_type (die, cu);
22179 case DW_TAG_array_type:
22180 this_type = read_array_type (die, cu);
22182 case DW_TAG_set_type:
22183 this_type = read_set_type (die, cu);
22185 case DW_TAG_pointer_type:
22186 this_type = read_tag_pointer_type (die, cu);
22188 case DW_TAG_ptr_to_member_type:
22189 this_type = read_tag_ptr_to_member_type (die, cu);
22191 case DW_TAG_reference_type:
22192 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22194 case DW_TAG_rvalue_reference_type:
22195 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22197 case DW_TAG_const_type:
22198 this_type = read_tag_const_type (die, cu);
22200 case DW_TAG_volatile_type:
22201 this_type = read_tag_volatile_type (die, cu);
22203 case DW_TAG_restrict_type:
22204 this_type = read_tag_restrict_type (die, cu);
22206 case DW_TAG_string_type:
22207 this_type = read_tag_string_type (die, cu);
22209 case DW_TAG_typedef:
22210 this_type = read_typedef (die, cu);
22212 case DW_TAG_subrange_type:
22213 this_type = read_subrange_type (die, cu);
22215 case DW_TAG_base_type:
22216 this_type = read_base_type (die, cu);
22218 case DW_TAG_unspecified_type:
22219 this_type = read_unspecified_type (die, cu);
22221 case DW_TAG_namespace:
22222 this_type = read_namespace_type (die, cu);
22224 case DW_TAG_module:
22225 this_type = read_module_type (die, cu);
22227 case DW_TAG_atomic_type:
22228 this_type = read_tag_atomic_type (die, cu);
22231 complaint (&symfile_complaints,
22232 _("unexpected tag in read_type_die: '%s'"),
22233 dwarf_tag_name (die->tag));
22240 /* See if we can figure out if the class lives in a namespace. We do
22241 this by looking for a member function; its demangled name will
22242 contain namespace info, if there is any.
22243 Return the computed name or NULL.
22244 Space for the result is allocated on the objfile's obstack.
22245 This is the full-die version of guess_partial_die_structure_name.
22246 In this case we know DIE has no useful parent. */
22249 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22251 struct die_info *spec_die;
22252 struct dwarf2_cu *spec_cu;
22253 struct die_info *child;
22254 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22257 spec_die = die_specification (die, &spec_cu);
22258 if (spec_die != NULL)
22264 for (child = die->child;
22266 child = child->sibling)
22268 if (child->tag == DW_TAG_subprogram)
22270 const char *linkage_name = dw2_linkage_name (child, cu);
22272 if (linkage_name != NULL)
22275 = language_class_name_from_physname (cu->language_defn,
22279 if (actual_name != NULL)
22281 const char *die_name = dwarf2_name (die, cu);
22283 if (die_name != NULL
22284 && strcmp (die_name, actual_name) != 0)
22286 /* Strip off the class name from the full name.
22287 We want the prefix. */
22288 int die_name_len = strlen (die_name);
22289 int actual_name_len = strlen (actual_name);
22291 /* Test for '::' as a sanity check. */
22292 if (actual_name_len > die_name_len + 2
22293 && actual_name[actual_name_len
22294 - die_name_len - 1] == ':')
22295 name = (char *) obstack_copy0 (
22296 &objfile->per_bfd->storage_obstack,
22297 actual_name, actual_name_len - die_name_len - 2);
22300 xfree (actual_name);
22309 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22310 prefix part in such case. See
22311 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22313 static const char *
22314 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22316 struct attribute *attr;
22319 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22320 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22323 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22326 attr = dw2_linkage_name_attr (die, cu);
22327 if (attr == NULL || DW_STRING (attr) == NULL)
22330 /* dwarf2_name had to be already called. */
22331 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22333 /* Strip the base name, keep any leading namespaces/classes. */
22334 base = strrchr (DW_STRING (attr), ':');
22335 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22338 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22339 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22341 &base[-1] - DW_STRING (attr));
22344 /* Return the name of the namespace/class that DIE is defined within,
22345 or "" if we can't tell. The caller should not xfree the result.
22347 For example, if we're within the method foo() in the following
22357 then determine_prefix on foo's die will return "N::C". */
22359 static const char *
22360 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22362 struct dwarf2_per_objfile *dwarf2_per_objfile
22363 = cu->per_cu->dwarf2_per_objfile;
22364 struct die_info *parent, *spec_die;
22365 struct dwarf2_cu *spec_cu;
22366 struct type *parent_type;
22367 const char *retval;
22369 if (cu->language != language_cplus
22370 && cu->language != language_fortran && cu->language != language_d
22371 && cu->language != language_rust)
22374 retval = anonymous_struct_prefix (die, cu);
22378 /* We have to be careful in the presence of DW_AT_specification.
22379 For example, with GCC 3.4, given the code
22383 // Definition of N::foo.
22387 then we'll have a tree of DIEs like this:
22389 1: DW_TAG_compile_unit
22390 2: DW_TAG_namespace // N
22391 3: DW_TAG_subprogram // declaration of N::foo
22392 4: DW_TAG_subprogram // definition of N::foo
22393 DW_AT_specification // refers to die #3
22395 Thus, when processing die #4, we have to pretend that we're in
22396 the context of its DW_AT_specification, namely the contex of die
22399 spec_die = die_specification (die, &spec_cu);
22400 if (spec_die == NULL)
22401 parent = die->parent;
22404 parent = spec_die->parent;
22408 if (parent == NULL)
22410 else if (parent->building_fullname)
22413 const char *parent_name;
22415 /* It has been seen on RealView 2.2 built binaries,
22416 DW_TAG_template_type_param types actually _defined_ as
22417 children of the parent class:
22420 template class <class Enum> Class{};
22421 Class<enum E> class_e;
22423 1: DW_TAG_class_type (Class)
22424 2: DW_TAG_enumeration_type (E)
22425 3: DW_TAG_enumerator (enum1:0)
22426 3: DW_TAG_enumerator (enum2:1)
22428 2: DW_TAG_template_type_param
22429 DW_AT_type DW_FORM_ref_udata (E)
22431 Besides being broken debug info, it can put GDB into an
22432 infinite loop. Consider:
22434 When we're building the full name for Class<E>, we'll start
22435 at Class, and go look over its template type parameters,
22436 finding E. We'll then try to build the full name of E, and
22437 reach here. We're now trying to build the full name of E,
22438 and look over the parent DIE for containing scope. In the
22439 broken case, if we followed the parent DIE of E, we'd again
22440 find Class, and once again go look at its template type
22441 arguments, etc., etc. Simply don't consider such parent die
22442 as source-level parent of this die (it can't be, the language
22443 doesn't allow it), and break the loop here. */
22444 name = dwarf2_name (die, cu);
22445 parent_name = dwarf2_name (parent, cu);
22446 complaint (&symfile_complaints,
22447 _("template param type '%s' defined within parent '%s'"),
22448 name ? name : "<unknown>",
22449 parent_name ? parent_name : "<unknown>");
22453 switch (parent->tag)
22455 case DW_TAG_namespace:
22456 parent_type = read_type_die (parent, cu);
22457 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22458 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22459 Work around this problem here. */
22460 if (cu->language == language_cplus
22461 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22463 /* We give a name to even anonymous namespaces. */
22464 return TYPE_TAG_NAME (parent_type);
22465 case DW_TAG_class_type:
22466 case DW_TAG_interface_type:
22467 case DW_TAG_structure_type:
22468 case DW_TAG_union_type:
22469 case DW_TAG_module:
22470 parent_type = read_type_die (parent, cu);
22471 if (TYPE_TAG_NAME (parent_type) != NULL)
22472 return TYPE_TAG_NAME (parent_type);
22474 /* An anonymous structure is only allowed non-static data
22475 members; no typedefs, no member functions, et cetera.
22476 So it does not need a prefix. */
22478 case DW_TAG_compile_unit:
22479 case DW_TAG_partial_unit:
22480 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22481 if (cu->language == language_cplus
22482 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22483 && die->child != NULL
22484 && (die->tag == DW_TAG_class_type
22485 || die->tag == DW_TAG_structure_type
22486 || die->tag == DW_TAG_union_type))
22488 char *name = guess_full_die_structure_name (die, cu);
22493 case DW_TAG_enumeration_type:
22494 parent_type = read_type_die (parent, cu);
22495 if (TYPE_DECLARED_CLASS (parent_type))
22497 if (TYPE_TAG_NAME (parent_type) != NULL)
22498 return TYPE_TAG_NAME (parent_type);
22501 /* Fall through. */
22503 return determine_prefix (parent, cu);
22507 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22508 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22509 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22510 an obconcat, otherwise allocate storage for the result. The CU argument is
22511 used to determine the language and hence, the appropriate separator. */
22513 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22516 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22517 int physname, struct dwarf2_cu *cu)
22519 const char *lead = "";
22522 if (suffix == NULL || suffix[0] == '\0'
22523 || prefix == NULL || prefix[0] == '\0')
22525 else if (cu->language == language_d)
22527 /* For D, the 'main' function could be defined in any module, but it
22528 should never be prefixed. */
22529 if (strcmp (suffix, "D main") == 0)
22537 else if (cu->language == language_fortran && physname)
22539 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22540 DW_AT_MIPS_linkage_name is preferred and used instead. */
22548 if (prefix == NULL)
22550 if (suffix == NULL)
22557 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22559 strcpy (retval, lead);
22560 strcat (retval, prefix);
22561 strcat (retval, sep);
22562 strcat (retval, suffix);
22567 /* We have an obstack. */
22568 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22572 /* Return sibling of die, NULL if no sibling. */
22574 static struct die_info *
22575 sibling_die (struct die_info *die)
22577 return die->sibling;
22580 /* Get name of a die, return NULL if not found. */
22582 static const char *
22583 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22584 struct obstack *obstack)
22586 if (name && cu->language == language_cplus)
22588 std::string canon_name = cp_canonicalize_string (name);
22590 if (!canon_name.empty ())
22592 if (canon_name != name)
22593 name = (const char *) obstack_copy0 (obstack,
22594 canon_name.c_str (),
22595 canon_name.length ());
22602 /* Get name of a die, return NULL if not found.
22603 Anonymous namespaces are converted to their magic string. */
22605 static const char *
22606 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22608 struct attribute *attr;
22609 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22611 attr = dwarf2_attr (die, DW_AT_name, cu);
22612 if ((!attr || !DW_STRING (attr))
22613 && die->tag != DW_TAG_namespace
22614 && die->tag != DW_TAG_class_type
22615 && die->tag != DW_TAG_interface_type
22616 && die->tag != DW_TAG_structure_type
22617 && die->tag != DW_TAG_union_type)
22622 case DW_TAG_compile_unit:
22623 case DW_TAG_partial_unit:
22624 /* Compilation units have a DW_AT_name that is a filename, not
22625 a source language identifier. */
22626 case DW_TAG_enumeration_type:
22627 case DW_TAG_enumerator:
22628 /* These tags always have simple identifiers already; no need
22629 to canonicalize them. */
22630 return DW_STRING (attr);
22632 case DW_TAG_namespace:
22633 if (attr != NULL && DW_STRING (attr) != NULL)
22634 return DW_STRING (attr);
22635 return CP_ANONYMOUS_NAMESPACE_STR;
22637 case DW_TAG_class_type:
22638 case DW_TAG_interface_type:
22639 case DW_TAG_structure_type:
22640 case DW_TAG_union_type:
22641 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22642 structures or unions. These were of the form "._%d" in GCC 4.1,
22643 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22644 and GCC 4.4. We work around this problem by ignoring these. */
22645 if (attr && DW_STRING (attr)
22646 && (startswith (DW_STRING (attr), "._")
22647 || startswith (DW_STRING (attr), "<anonymous")))
22650 /* GCC might emit a nameless typedef that has a linkage name. See
22651 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22652 if (!attr || DW_STRING (attr) == NULL)
22654 char *demangled = NULL;
22656 attr = dw2_linkage_name_attr (die, cu);
22657 if (attr == NULL || DW_STRING (attr) == NULL)
22660 /* Avoid demangling DW_STRING (attr) the second time on a second
22661 call for the same DIE. */
22662 if (!DW_STRING_IS_CANONICAL (attr))
22663 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22669 /* FIXME: we already did this for the partial symbol... */
22672 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22673 demangled, strlen (demangled)));
22674 DW_STRING_IS_CANONICAL (attr) = 1;
22677 /* Strip any leading namespaces/classes, keep only the base name.
22678 DW_AT_name for named DIEs does not contain the prefixes. */
22679 base = strrchr (DW_STRING (attr), ':');
22680 if (base && base > DW_STRING (attr) && base[-1] == ':')
22683 return DW_STRING (attr);
22692 if (!DW_STRING_IS_CANONICAL (attr))
22695 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22696 &objfile->per_bfd->storage_obstack);
22697 DW_STRING_IS_CANONICAL (attr) = 1;
22699 return DW_STRING (attr);
22702 /* Return the die that this die in an extension of, or NULL if there
22703 is none. *EXT_CU is the CU containing DIE on input, and the CU
22704 containing the return value on output. */
22706 static struct die_info *
22707 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22709 struct attribute *attr;
22711 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22715 return follow_die_ref (die, attr, ext_cu);
22718 /* Convert a DIE tag into its string name. */
22720 static const char *
22721 dwarf_tag_name (unsigned tag)
22723 const char *name = get_DW_TAG_name (tag);
22726 return "DW_TAG_<unknown>";
22731 /* Convert a DWARF attribute code into its string name. */
22733 static const char *
22734 dwarf_attr_name (unsigned attr)
22738 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22739 if (attr == DW_AT_MIPS_fde)
22740 return "DW_AT_MIPS_fde";
22742 if (attr == DW_AT_HP_block_index)
22743 return "DW_AT_HP_block_index";
22746 name = get_DW_AT_name (attr);
22749 return "DW_AT_<unknown>";
22754 /* Convert a DWARF value form code into its string name. */
22756 static const char *
22757 dwarf_form_name (unsigned form)
22759 const char *name = get_DW_FORM_name (form);
22762 return "DW_FORM_<unknown>";
22767 static const char *
22768 dwarf_bool_name (unsigned mybool)
22776 /* Convert a DWARF type code into its string name. */
22778 static const char *
22779 dwarf_type_encoding_name (unsigned enc)
22781 const char *name = get_DW_ATE_name (enc);
22784 return "DW_ATE_<unknown>";
22790 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22794 print_spaces (indent, f);
22795 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22796 dwarf_tag_name (die->tag), die->abbrev,
22797 to_underlying (die->sect_off));
22799 if (die->parent != NULL)
22801 print_spaces (indent, f);
22802 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22803 to_underlying (die->parent->sect_off));
22806 print_spaces (indent, f);
22807 fprintf_unfiltered (f, " has children: %s\n",
22808 dwarf_bool_name (die->child != NULL));
22810 print_spaces (indent, f);
22811 fprintf_unfiltered (f, " attributes:\n");
22813 for (i = 0; i < die->num_attrs; ++i)
22815 print_spaces (indent, f);
22816 fprintf_unfiltered (f, " %s (%s) ",
22817 dwarf_attr_name (die->attrs[i].name),
22818 dwarf_form_name (die->attrs[i].form));
22820 switch (die->attrs[i].form)
22823 case DW_FORM_GNU_addr_index:
22824 fprintf_unfiltered (f, "address: ");
22825 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22827 case DW_FORM_block2:
22828 case DW_FORM_block4:
22829 case DW_FORM_block:
22830 case DW_FORM_block1:
22831 fprintf_unfiltered (f, "block: size %s",
22832 pulongest (DW_BLOCK (&die->attrs[i])->size));
22834 case DW_FORM_exprloc:
22835 fprintf_unfiltered (f, "expression: size %s",
22836 pulongest (DW_BLOCK (&die->attrs[i])->size));
22838 case DW_FORM_data16:
22839 fprintf_unfiltered (f, "constant of 16 bytes");
22841 case DW_FORM_ref_addr:
22842 fprintf_unfiltered (f, "ref address: ");
22843 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22845 case DW_FORM_GNU_ref_alt:
22846 fprintf_unfiltered (f, "alt ref address: ");
22847 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22853 case DW_FORM_ref_udata:
22854 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22855 (long) (DW_UNSND (&die->attrs[i])));
22857 case DW_FORM_data1:
22858 case DW_FORM_data2:
22859 case DW_FORM_data4:
22860 case DW_FORM_data8:
22861 case DW_FORM_udata:
22862 case DW_FORM_sdata:
22863 fprintf_unfiltered (f, "constant: %s",
22864 pulongest (DW_UNSND (&die->attrs[i])));
22866 case DW_FORM_sec_offset:
22867 fprintf_unfiltered (f, "section offset: %s",
22868 pulongest (DW_UNSND (&die->attrs[i])));
22870 case DW_FORM_ref_sig8:
22871 fprintf_unfiltered (f, "signature: %s",
22872 hex_string (DW_SIGNATURE (&die->attrs[i])));
22874 case DW_FORM_string:
22876 case DW_FORM_line_strp:
22877 case DW_FORM_GNU_str_index:
22878 case DW_FORM_GNU_strp_alt:
22879 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22880 DW_STRING (&die->attrs[i])
22881 ? DW_STRING (&die->attrs[i]) : "",
22882 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22885 if (DW_UNSND (&die->attrs[i]))
22886 fprintf_unfiltered (f, "flag: TRUE");
22888 fprintf_unfiltered (f, "flag: FALSE");
22890 case DW_FORM_flag_present:
22891 fprintf_unfiltered (f, "flag: TRUE");
22893 case DW_FORM_indirect:
22894 /* The reader will have reduced the indirect form to
22895 the "base form" so this form should not occur. */
22896 fprintf_unfiltered (f,
22897 "unexpected attribute form: DW_FORM_indirect");
22899 case DW_FORM_implicit_const:
22900 fprintf_unfiltered (f, "constant: %s",
22901 plongest (DW_SND (&die->attrs[i])));
22904 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22905 die->attrs[i].form);
22908 fprintf_unfiltered (f, "\n");
22913 dump_die_for_error (struct die_info *die)
22915 dump_die_shallow (gdb_stderr, 0, die);
22919 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22921 int indent = level * 4;
22923 gdb_assert (die != NULL);
22925 if (level >= max_level)
22928 dump_die_shallow (f, indent, die);
22930 if (die->child != NULL)
22932 print_spaces (indent, f);
22933 fprintf_unfiltered (f, " Children:");
22934 if (level + 1 < max_level)
22936 fprintf_unfiltered (f, "\n");
22937 dump_die_1 (f, level + 1, max_level, die->child);
22941 fprintf_unfiltered (f,
22942 " [not printed, max nesting level reached]\n");
22946 if (die->sibling != NULL && level > 0)
22948 dump_die_1 (f, level, max_level, die->sibling);
22952 /* This is called from the pdie macro in gdbinit.in.
22953 It's not static so gcc will keep a copy callable from gdb. */
22956 dump_die (struct die_info *die, int max_level)
22958 dump_die_1 (gdb_stdlog, 0, max_level, die);
22962 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22966 slot = htab_find_slot_with_hash (cu->die_hash, die,
22967 to_underlying (die->sect_off),
22973 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22977 dwarf2_get_ref_die_offset (const struct attribute *attr)
22979 if (attr_form_is_ref (attr))
22980 return (sect_offset) DW_UNSND (attr);
22982 complaint (&symfile_complaints,
22983 _("unsupported die ref attribute form: '%s'"),
22984 dwarf_form_name (attr->form));
22988 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22989 * the value held by the attribute is not constant. */
22992 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22994 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22995 return DW_SND (attr);
22996 else if (attr->form == DW_FORM_udata
22997 || attr->form == DW_FORM_data1
22998 || attr->form == DW_FORM_data2
22999 || attr->form == DW_FORM_data4
23000 || attr->form == DW_FORM_data8)
23001 return DW_UNSND (attr);
23004 /* For DW_FORM_data16 see attr_form_is_constant. */
23005 complaint (&symfile_complaints,
23006 _("Attribute value is not a constant (%s)"),
23007 dwarf_form_name (attr->form));
23008 return default_value;
23012 /* Follow reference or signature attribute ATTR of SRC_DIE.
23013 On entry *REF_CU is the CU of SRC_DIE.
23014 On exit *REF_CU is the CU of the result. */
23016 static struct die_info *
23017 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23018 struct dwarf2_cu **ref_cu)
23020 struct die_info *die;
23022 if (attr_form_is_ref (attr))
23023 die = follow_die_ref (src_die, attr, ref_cu);
23024 else if (attr->form == DW_FORM_ref_sig8)
23025 die = follow_die_sig (src_die, attr, ref_cu);
23028 dump_die_for_error (src_die);
23029 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23030 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23036 /* Follow reference OFFSET.
23037 On entry *REF_CU is the CU of the source die referencing OFFSET.
23038 On exit *REF_CU is the CU of the result.
23039 Returns NULL if OFFSET is invalid. */
23041 static struct die_info *
23042 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23043 struct dwarf2_cu **ref_cu)
23045 struct die_info temp_die;
23046 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23047 struct dwarf2_per_objfile *dwarf2_per_objfile
23048 = cu->per_cu->dwarf2_per_objfile;
23049 struct objfile *objfile = dwarf2_per_objfile->objfile;
23051 gdb_assert (cu->per_cu != NULL);
23055 if (cu->per_cu->is_debug_types)
23057 /* .debug_types CUs cannot reference anything outside their CU.
23058 If they need to, they have to reference a signatured type via
23059 DW_FORM_ref_sig8. */
23060 if (!offset_in_cu_p (&cu->header, sect_off))
23063 else if (offset_in_dwz != cu->per_cu->is_dwz
23064 || !offset_in_cu_p (&cu->header, sect_off))
23066 struct dwarf2_per_cu_data *per_cu;
23068 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23069 dwarf2_per_objfile);
23071 /* If necessary, add it to the queue and load its DIEs. */
23072 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23073 load_full_comp_unit (per_cu, cu->language);
23075 target_cu = per_cu->cu;
23077 else if (cu->dies == NULL)
23079 /* We're loading full DIEs during partial symbol reading. */
23080 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23081 load_full_comp_unit (cu->per_cu, language_minimal);
23084 *ref_cu = target_cu;
23085 temp_die.sect_off = sect_off;
23086 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23088 to_underlying (sect_off));
23091 /* Follow reference attribute ATTR of SRC_DIE.
23092 On entry *REF_CU is the CU of SRC_DIE.
23093 On exit *REF_CU is the CU of the result. */
23095 static struct die_info *
23096 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23097 struct dwarf2_cu **ref_cu)
23099 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23100 struct dwarf2_cu *cu = *ref_cu;
23101 struct die_info *die;
23103 die = follow_die_offset (sect_off,
23104 (attr->form == DW_FORM_GNU_ref_alt
23105 || cu->per_cu->is_dwz),
23108 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
23109 "at 0x%x [in module %s]"),
23110 to_underlying (sect_off), to_underlying (src_die->sect_off),
23111 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23116 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23117 Returned value is intended for DW_OP_call*. Returned
23118 dwarf2_locexpr_baton->data has lifetime of
23119 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23121 struct dwarf2_locexpr_baton
23122 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23123 struct dwarf2_per_cu_data *per_cu,
23124 CORE_ADDR (*get_frame_pc) (void *baton),
23127 struct dwarf2_cu *cu;
23128 struct die_info *die;
23129 struct attribute *attr;
23130 struct dwarf2_locexpr_baton retval;
23131 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23132 struct dwarf2_per_objfile *dwarf2_per_objfile
23133 = get_dwarf2_per_objfile (objfile);
23135 if (per_cu->cu == NULL)
23140 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23141 Instead just throw an error, not much else we can do. */
23142 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23143 to_underlying (sect_off), objfile_name (objfile));
23146 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23148 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23149 to_underlying (sect_off), objfile_name (objfile));
23151 attr = dwarf2_attr (die, DW_AT_location, cu);
23154 /* DWARF: "If there is no such attribute, then there is no effect.".
23155 DATA is ignored if SIZE is 0. */
23157 retval.data = NULL;
23160 else if (attr_form_is_section_offset (attr))
23162 struct dwarf2_loclist_baton loclist_baton;
23163 CORE_ADDR pc = (*get_frame_pc) (baton);
23166 fill_in_loclist_baton (cu, &loclist_baton, attr);
23168 retval.data = dwarf2_find_location_expression (&loclist_baton,
23170 retval.size = size;
23174 if (!attr_form_is_block (attr))
23175 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23176 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23177 to_underlying (sect_off), objfile_name (objfile));
23179 retval.data = DW_BLOCK (attr)->data;
23180 retval.size = DW_BLOCK (attr)->size;
23182 retval.per_cu = cu->per_cu;
23184 age_cached_comp_units (dwarf2_per_objfile);
23189 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23192 struct dwarf2_locexpr_baton
23193 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23194 struct dwarf2_per_cu_data *per_cu,
23195 CORE_ADDR (*get_frame_pc) (void *baton),
23198 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23200 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23203 /* Write a constant of a given type as target-ordered bytes into
23206 static const gdb_byte *
23207 write_constant_as_bytes (struct obstack *obstack,
23208 enum bfd_endian byte_order,
23215 *len = TYPE_LENGTH (type);
23216 result = (gdb_byte *) obstack_alloc (obstack, *len);
23217 store_unsigned_integer (result, *len, byte_order, value);
23222 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23223 pointer to the constant bytes and set LEN to the length of the
23224 data. If memory is needed, allocate it on OBSTACK. If the DIE
23225 does not have a DW_AT_const_value, return NULL. */
23228 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23229 struct dwarf2_per_cu_data *per_cu,
23230 struct obstack *obstack,
23233 struct dwarf2_cu *cu;
23234 struct die_info *die;
23235 struct attribute *attr;
23236 const gdb_byte *result = NULL;
23239 enum bfd_endian byte_order;
23240 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23242 if (per_cu->cu == NULL)
23247 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23248 Instead just throw an error, not much else we can do. */
23249 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23250 to_underlying (sect_off), objfile_name (objfile));
23253 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23255 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23256 to_underlying (sect_off), objfile_name (objfile));
23259 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23263 byte_order = (bfd_big_endian (objfile->obfd)
23264 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23266 switch (attr->form)
23269 case DW_FORM_GNU_addr_index:
23273 *len = cu->header.addr_size;
23274 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23275 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23279 case DW_FORM_string:
23281 case DW_FORM_GNU_str_index:
23282 case DW_FORM_GNU_strp_alt:
23283 /* DW_STRING is already allocated on the objfile obstack, point
23285 result = (const gdb_byte *) DW_STRING (attr);
23286 *len = strlen (DW_STRING (attr));
23288 case DW_FORM_block1:
23289 case DW_FORM_block2:
23290 case DW_FORM_block4:
23291 case DW_FORM_block:
23292 case DW_FORM_exprloc:
23293 case DW_FORM_data16:
23294 result = DW_BLOCK (attr)->data;
23295 *len = DW_BLOCK (attr)->size;
23298 /* The DW_AT_const_value attributes are supposed to carry the
23299 symbol's value "represented as it would be on the target
23300 architecture." By the time we get here, it's already been
23301 converted to host endianness, so we just need to sign- or
23302 zero-extend it as appropriate. */
23303 case DW_FORM_data1:
23304 type = die_type (die, cu);
23305 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23306 if (result == NULL)
23307 result = write_constant_as_bytes (obstack, byte_order,
23310 case DW_FORM_data2:
23311 type = die_type (die, cu);
23312 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23313 if (result == NULL)
23314 result = write_constant_as_bytes (obstack, byte_order,
23317 case DW_FORM_data4:
23318 type = die_type (die, cu);
23319 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23320 if (result == NULL)
23321 result = write_constant_as_bytes (obstack, byte_order,
23324 case DW_FORM_data8:
23325 type = die_type (die, cu);
23326 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23327 if (result == NULL)
23328 result = write_constant_as_bytes (obstack, byte_order,
23332 case DW_FORM_sdata:
23333 case DW_FORM_implicit_const:
23334 type = die_type (die, cu);
23335 result = write_constant_as_bytes (obstack, byte_order,
23336 type, DW_SND (attr), len);
23339 case DW_FORM_udata:
23340 type = die_type (die, cu);
23341 result = write_constant_as_bytes (obstack, byte_order,
23342 type, DW_UNSND (attr), len);
23346 complaint (&symfile_complaints,
23347 _("unsupported const value attribute form: '%s'"),
23348 dwarf_form_name (attr->form));
23355 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23356 valid type for this die is found. */
23359 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23360 struct dwarf2_per_cu_data *per_cu)
23362 struct dwarf2_cu *cu;
23363 struct die_info *die;
23365 if (per_cu->cu == NULL)
23371 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23375 return die_type (die, cu);
23378 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23382 dwarf2_get_die_type (cu_offset die_offset,
23383 struct dwarf2_per_cu_data *per_cu)
23385 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23386 return get_die_type_at_offset (die_offset_sect, per_cu);
23389 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23390 On entry *REF_CU is the CU of SRC_DIE.
23391 On exit *REF_CU is the CU of the result.
23392 Returns NULL if the referenced DIE isn't found. */
23394 static struct die_info *
23395 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23396 struct dwarf2_cu **ref_cu)
23398 struct die_info temp_die;
23399 struct dwarf2_cu *sig_cu;
23400 struct die_info *die;
23402 /* While it might be nice to assert sig_type->type == NULL here,
23403 we can get here for DW_AT_imported_declaration where we need
23404 the DIE not the type. */
23406 /* If necessary, add it to the queue and load its DIEs. */
23408 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23409 read_signatured_type (sig_type);
23411 sig_cu = sig_type->per_cu.cu;
23412 gdb_assert (sig_cu != NULL);
23413 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23414 temp_die.sect_off = sig_type->type_offset_in_section;
23415 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23416 to_underlying (temp_die.sect_off));
23419 struct dwarf2_per_objfile *dwarf2_per_objfile
23420 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23422 /* For .gdb_index version 7 keep track of included TUs.
23423 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23424 if (dwarf2_per_objfile->index_table != NULL
23425 && dwarf2_per_objfile->index_table->version <= 7)
23427 VEC_safe_push (dwarf2_per_cu_ptr,
23428 (*ref_cu)->per_cu->imported_symtabs,
23439 /* Follow signatured type referenced by ATTR in SRC_DIE.
23440 On entry *REF_CU is the CU of SRC_DIE.
23441 On exit *REF_CU is the CU of the result.
23442 The result is the DIE of the type.
23443 If the referenced type cannot be found an error is thrown. */
23445 static struct die_info *
23446 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23447 struct dwarf2_cu **ref_cu)
23449 ULONGEST signature = DW_SIGNATURE (attr);
23450 struct signatured_type *sig_type;
23451 struct die_info *die;
23453 gdb_assert (attr->form == DW_FORM_ref_sig8);
23455 sig_type = lookup_signatured_type (*ref_cu, signature);
23456 /* sig_type will be NULL if the signatured type is missing from
23458 if (sig_type == NULL)
23460 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23461 " from DIE at 0x%x [in module %s]"),
23462 hex_string (signature), to_underlying (src_die->sect_off),
23463 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23466 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23469 dump_die_for_error (src_die);
23470 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23471 " from DIE at 0x%x [in module %s]"),
23472 hex_string (signature), to_underlying (src_die->sect_off),
23473 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23479 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23480 reading in and processing the type unit if necessary. */
23482 static struct type *
23483 get_signatured_type (struct die_info *die, ULONGEST signature,
23484 struct dwarf2_cu *cu)
23486 struct dwarf2_per_objfile *dwarf2_per_objfile
23487 = cu->per_cu->dwarf2_per_objfile;
23488 struct signatured_type *sig_type;
23489 struct dwarf2_cu *type_cu;
23490 struct die_info *type_die;
23493 sig_type = lookup_signatured_type (cu, signature);
23494 /* sig_type will be NULL if the signatured type is missing from
23496 if (sig_type == NULL)
23498 complaint (&symfile_complaints,
23499 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23500 " from DIE at 0x%x [in module %s]"),
23501 hex_string (signature), to_underlying (die->sect_off),
23502 objfile_name (dwarf2_per_objfile->objfile));
23503 return build_error_marker_type (cu, die);
23506 /* If we already know the type we're done. */
23507 if (sig_type->type != NULL)
23508 return sig_type->type;
23511 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23512 if (type_die != NULL)
23514 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23515 is created. This is important, for example, because for c++ classes
23516 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23517 type = read_type_die (type_die, type_cu);
23520 complaint (&symfile_complaints,
23521 _("Dwarf Error: Cannot build signatured type %s"
23522 " referenced from DIE at 0x%x [in module %s]"),
23523 hex_string (signature), to_underlying (die->sect_off),
23524 objfile_name (dwarf2_per_objfile->objfile));
23525 type = build_error_marker_type (cu, die);
23530 complaint (&symfile_complaints,
23531 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23532 " from DIE at 0x%x [in module %s]"),
23533 hex_string (signature), to_underlying (die->sect_off),
23534 objfile_name (dwarf2_per_objfile->objfile));
23535 type = build_error_marker_type (cu, die);
23537 sig_type->type = type;
23542 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23543 reading in and processing the type unit if necessary. */
23545 static struct type *
23546 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23547 struct dwarf2_cu *cu) /* ARI: editCase function */
23549 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23550 if (attr_form_is_ref (attr))
23552 struct dwarf2_cu *type_cu = cu;
23553 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23555 return read_type_die (type_die, type_cu);
23557 else if (attr->form == DW_FORM_ref_sig8)
23559 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23563 struct dwarf2_per_objfile *dwarf2_per_objfile
23564 = cu->per_cu->dwarf2_per_objfile;
23566 complaint (&symfile_complaints,
23567 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23568 " at 0x%x [in module %s]"),
23569 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23570 objfile_name (dwarf2_per_objfile->objfile));
23571 return build_error_marker_type (cu, die);
23575 /* Load the DIEs associated with type unit PER_CU into memory. */
23578 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23580 struct signatured_type *sig_type;
23582 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23583 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23585 /* We have the per_cu, but we need the signatured_type.
23586 Fortunately this is an easy translation. */
23587 gdb_assert (per_cu->is_debug_types);
23588 sig_type = (struct signatured_type *) per_cu;
23590 gdb_assert (per_cu->cu == NULL);
23592 read_signatured_type (sig_type);
23594 gdb_assert (per_cu->cu != NULL);
23597 /* die_reader_func for read_signatured_type.
23598 This is identical to load_full_comp_unit_reader,
23599 but is kept separate for now. */
23602 read_signatured_type_reader (const struct die_reader_specs *reader,
23603 const gdb_byte *info_ptr,
23604 struct die_info *comp_unit_die,
23608 struct dwarf2_cu *cu = reader->cu;
23610 gdb_assert (cu->die_hash == NULL);
23612 htab_create_alloc_ex (cu->header.length / 12,
23616 &cu->comp_unit_obstack,
23617 hashtab_obstack_allocate,
23618 dummy_obstack_deallocate);
23621 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23622 &info_ptr, comp_unit_die);
23623 cu->dies = comp_unit_die;
23624 /* comp_unit_die is not stored in die_hash, no need. */
23626 /* We try not to read any attributes in this function, because not
23627 all CUs needed for references have been loaded yet, and symbol
23628 table processing isn't initialized. But we have to set the CU language,
23629 or we won't be able to build types correctly.
23630 Similarly, if we do not read the producer, we can not apply
23631 producer-specific interpretation. */
23632 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23635 /* Read in a signatured type and build its CU and DIEs.
23636 If the type is a stub for the real type in a DWO file,
23637 read in the real type from the DWO file as well. */
23640 read_signatured_type (struct signatured_type *sig_type)
23642 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23644 gdb_assert (per_cu->is_debug_types);
23645 gdb_assert (per_cu->cu == NULL);
23647 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23648 read_signatured_type_reader, NULL);
23649 sig_type->per_cu.tu_read = 1;
23652 /* Decode simple location descriptions.
23653 Given a pointer to a dwarf block that defines a location, compute
23654 the location and return the value.
23656 NOTE drow/2003-11-18: This function is called in two situations
23657 now: for the address of static or global variables (partial symbols
23658 only) and for offsets into structures which are expected to be
23659 (more or less) constant. The partial symbol case should go away,
23660 and only the constant case should remain. That will let this
23661 function complain more accurately. A few special modes are allowed
23662 without complaint for global variables (for instance, global
23663 register values and thread-local values).
23665 A location description containing no operations indicates that the
23666 object is optimized out. The return value is 0 for that case.
23667 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23668 callers will only want a very basic result and this can become a
23671 Note that stack[0] is unused except as a default error return. */
23674 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23676 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23678 size_t size = blk->size;
23679 const gdb_byte *data = blk->data;
23680 CORE_ADDR stack[64];
23682 unsigned int bytes_read, unsnd;
23688 stack[++stacki] = 0;
23727 stack[++stacki] = op - DW_OP_lit0;
23762 stack[++stacki] = op - DW_OP_reg0;
23764 dwarf2_complex_location_expr_complaint ();
23768 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23770 stack[++stacki] = unsnd;
23772 dwarf2_complex_location_expr_complaint ();
23776 stack[++stacki] = read_address (objfile->obfd, &data[i],
23781 case DW_OP_const1u:
23782 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23786 case DW_OP_const1s:
23787 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23791 case DW_OP_const2u:
23792 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23796 case DW_OP_const2s:
23797 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23801 case DW_OP_const4u:
23802 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23806 case DW_OP_const4s:
23807 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23811 case DW_OP_const8u:
23812 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23817 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23823 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23828 stack[stacki + 1] = stack[stacki];
23833 stack[stacki - 1] += stack[stacki];
23837 case DW_OP_plus_uconst:
23838 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23844 stack[stacki - 1] -= stack[stacki];
23849 /* If we're not the last op, then we definitely can't encode
23850 this using GDB's address_class enum. This is valid for partial
23851 global symbols, although the variable's address will be bogus
23854 dwarf2_complex_location_expr_complaint ();
23857 case DW_OP_GNU_push_tls_address:
23858 case DW_OP_form_tls_address:
23859 /* The top of the stack has the offset from the beginning
23860 of the thread control block at which the variable is located. */
23861 /* Nothing should follow this operator, so the top of stack would
23863 /* This is valid for partial global symbols, but the variable's
23864 address will be bogus in the psymtab. Make it always at least
23865 non-zero to not look as a variable garbage collected by linker
23866 which have DW_OP_addr 0. */
23868 dwarf2_complex_location_expr_complaint ();
23872 case DW_OP_GNU_uninit:
23875 case DW_OP_GNU_addr_index:
23876 case DW_OP_GNU_const_index:
23877 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23884 const char *name = get_DW_OP_name (op);
23887 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23890 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23894 return (stack[stacki]);
23897 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23898 outside of the allocated space. Also enforce minimum>0. */
23899 if (stacki >= ARRAY_SIZE (stack) - 1)
23901 complaint (&symfile_complaints,
23902 _("location description stack overflow"));
23908 complaint (&symfile_complaints,
23909 _("location description stack underflow"));
23913 return (stack[stacki]);
23916 /* memory allocation interface */
23918 static struct dwarf_block *
23919 dwarf_alloc_block (struct dwarf2_cu *cu)
23921 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23924 static struct die_info *
23925 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23927 struct die_info *die;
23928 size_t size = sizeof (struct die_info);
23931 size += (num_attrs - 1) * sizeof (struct attribute);
23933 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23934 memset (die, 0, sizeof (struct die_info));
23939 /* Macro support. */
23941 /* Return file name relative to the compilation directory of file number I in
23942 *LH's file name table. The result is allocated using xmalloc; the caller is
23943 responsible for freeing it. */
23946 file_file_name (int file, struct line_header *lh)
23948 /* Is the file number a valid index into the line header's file name
23949 table? Remember that file numbers start with one, not zero. */
23950 if (1 <= file && file <= lh->file_names.size ())
23952 const file_entry &fe = lh->file_names[file - 1];
23954 if (!IS_ABSOLUTE_PATH (fe.name))
23956 const char *dir = fe.include_dir (lh);
23958 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23960 return xstrdup (fe.name);
23964 /* The compiler produced a bogus file number. We can at least
23965 record the macro definitions made in the file, even if we
23966 won't be able to find the file by name. */
23967 char fake_name[80];
23969 xsnprintf (fake_name, sizeof (fake_name),
23970 "<bad macro file number %d>", file);
23972 complaint (&symfile_complaints,
23973 _("bad file number in macro information (%d)"),
23976 return xstrdup (fake_name);
23980 /* Return the full name of file number I in *LH's file name table.
23981 Use COMP_DIR as the name of the current directory of the
23982 compilation. The result is allocated using xmalloc; the caller is
23983 responsible for freeing it. */
23985 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23987 /* Is the file number a valid index into the line header's file name
23988 table? Remember that file numbers start with one, not zero. */
23989 if (1 <= file && file <= lh->file_names.size ())
23991 char *relative = file_file_name (file, lh);
23993 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23995 return reconcat (relative, comp_dir, SLASH_STRING,
23996 relative, (char *) NULL);
23999 return file_file_name (file, lh);
24003 static struct macro_source_file *
24004 macro_start_file (int file, int line,
24005 struct macro_source_file *current_file,
24006 struct line_header *lh)
24008 /* File name relative to the compilation directory of this source file. */
24009 char *file_name = file_file_name (file, lh);
24011 if (! current_file)
24013 /* Note: We don't create a macro table for this compilation unit
24014 at all until we actually get a filename. */
24015 struct macro_table *macro_table = get_macro_table ();
24017 /* If we have no current file, then this must be the start_file
24018 directive for the compilation unit's main source file. */
24019 current_file = macro_set_main (macro_table, file_name);
24020 macro_define_special (macro_table);
24023 current_file = macro_include (current_file, line, file_name);
24027 return current_file;
24030 static const char *
24031 consume_improper_spaces (const char *p, const char *body)
24035 complaint (&symfile_complaints,
24036 _("macro definition contains spaces "
24037 "in formal argument list:\n`%s'"),
24049 parse_macro_definition (struct macro_source_file *file, int line,
24054 /* The body string takes one of two forms. For object-like macro
24055 definitions, it should be:
24057 <macro name> " " <definition>
24059 For function-like macro definitions, it should be:
24061 <macro name> "() " <definition>
24063 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24065 Spaces may appear only where explicitly indicated, and in the
24068 The Dwarf 2 spec says that an object-like macro's name is always
24069 followed by a space, but versions of GCC around March 2002 omit
24070 the space when the macro's definition is the empty string.
24072 The Dwarf 2 spec says that there should be no spaces between the
24073 formal arguments in a function-like macro's formal argument list,
24074 but versions of GCC around March 2002 include spaces after the
24078 /* Find the extent of the macro name. The macro name is terminated
24079 by either a space or null character (for an object-like macro) or
24080 an opening paren (for a function-like macro). */
24081 for (p = body; *p; p++)
24082 if (*p == ' ' || *p == '(')
24085 if (*p == ' ' || *p == '\0')
24087 /* It's an object-like macro. */
24088 int name_len = p - body;
24089 char *name = savestring (body, name_len);
24090 const char *replacement;
24093 replacement = body + name_len + 1;
24096 dwarf2_macro_malformed_definition_complaint (body);
24097 replacement = body + name_len;
24100 macro_define_object (file, line, name, replacement);
24104 else if (*p == '(')
24106 /* It's a function-like macro. */
24107 char *name = savestring (body, p - body);
24110 char **argv = XNEWVEC (char *, argv_size);
24114 p = consume_improper_spaces (p, body);
24116 /* Parse the formal argument list. */
24117 while (*p && *p != ')')
24119 /* Find the extent of the current argument name. */
24120 const char *arg_start = p;
24122 while (*p && *p != ',' && *p != ')' && *p != ' ')
24125 if (! *p || p == arg_start)
24126 dwarf2_macro_malformed_definition_complaint (body);
24129 /* Make sure argv has room for the new argument. */
24130 if (argc >= argv_size)
24133 argv = XRESIZEVEC (char *, argv, argv_size);
24136 argv[argc++] = savestring (arg_start, p - arg_start);
24139 p = consume_improper_spaces (p, body);
24141 /* Consume the comma, if present. */
24146 p = consume_improper_spaces (p, body);
24155 /* Perfectly formed definition, no complaints. */
24156 macro_define_function (file, line, name,
24157 argc, (const char **) argv,
24159 else if (*p == '\0')
24161 /* Complain, but do define it. */
24162 dwarf2_macro_malformed_definition_complaint (body);
24163 macro_define_function (file, line, name,
24164 argc, (const char **) argv,
24168 /* Just complain. */
24169 dwarf2_macro_malformed_definition_complaint (body);
24172 /* Just complain. */
24173 dwarf2_macro_malformed_definition_complaint (body);
24179 for (i = 0; i < argc; i++)
24185 dwarf2_macro_malformed_definition_complaint (body);
24188 /* Skip some bytes from BYTES according to the form given in FORM.
24189 Returns the new pointer. */
24191 static const gdb_byte *
24192 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24193 enum dwarf_form form,
24194 unsigned int offset_size,
24195 struct dwarf2_section_info *section)
24197 unsigned int bytes_read;
24201 case DW_FORM_data1:
24206 case DW_FORM_data2:
24210 case DW_FORM_data4:
24214 case DW_FORM_data8:
24218 case DW_FORM_data16:
24222 case DW_FORM_string:
24223 read_direct_string (abfd, bytes, &bytes_read);
24224 bytes += bytes_read;
24227 case DW_FORM_sec_offset:
24229 case DW_FORM_GNU_strp_alt:
24230 bytes += offset_size;
24233 case DW_FORM_block:
24234 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24235 bytes += bytes_read;
24238 case DW_FORM_block1:
24239 bytes += 1 + read_1_byte (abfd, bytes);
24241 case DW_FORM_block2:
24242 bytes += 2 + read_2_bytes (abfd, bytes);
24244 case DW_FORM_block4:
24245 bytes += 4 + read_4_bytes (abfd, bytes);
24248 case DW_FORM_sdata:
24249 case DW_FORM_udata:
24250 case DW_FORM_GNU_addr_index:
24251 case DW_FORM_GNU_str_index:
24252 bytes = gdb_skip_leb128 (bytes, buffer_end);
24255 dwarf2_section_buffer_overflow_complaint (section);
24260 case DW_FORM_implicit_const:
24265 complaint (&symfile_complaints,
24266 _("invalid form 0x%x in `%s'"),
24267 form, get_section_name (section));
24275 /* A helper for dwarf_decode_macros that handles skipping an unknown
24276 opcode. Returns an updated pointer to the macro data buffer; or,
24277 on error, issues a complaint and returns NULL. */
24279 static const gdb_byte *
24280 skip_unknown_opcode (unsigned int opcode,
24281 const gdb_byte **opcode_definitions,
24282 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24284 unsigned int offset_size,
24285 struct dwarf2_section_info *section)
24287 unsigned int bytes_read, i;
24289 const gdb_byte *defn;
24291 if (opcode_definitions[opcode] == NULL)
24293 complaint (&symfile_complaints,
24294 _("unrecognized DW_MACFINO opcode 0x%x"),
24299 defn = opcode_definitions[opcode];
24300 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24301 defn += bytes_read;
24303 for (i = 0; i < arg; ++i)
24305 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24306 (enum dwarf_form) defn[i], offset_size,
24308 if (mac_ptr == NULL)
24310 /* skip_form_bytes already issued the complaint. */
24318 /* A helper function which parses the header of a macro section.
24319 If the macro section is the extended (for now called "GNU") type,
24320 then this updates *OFFSET_SIZE. Returns a pointer to just after
24321 the header, or issues a complaint and returns NULL on error. */
24323 static const gdb_byte *
24324 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24326 const gdb_byte *mac_ptr,
24327 unsigned int *offset_size,
24328 int section_is_gnu)
24330 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24332 if (section_is_gnu)
24334 unsigned int version, flags;
24336 version = read_2_bytes (abfd, mac_ptr);
24337 if (version != 4 && version != 5)
24339 complaint (&symfile_complaints,
24340 _("unrecognized version `%d' in .debug_macro section"),
24346 flags = read_1_byte (abfd, mac_ptr);
24348 *offset_size = (flags & 1) ? 8 : 4;
24350 if ((flags & 2) != 0)
24351 /* We don't need the line table offset. */
24352 mac_ptr += *offset_size;
24354 /* Vendor opcode descriptions. */
24355 if ((flags & 4) != 0)
24357 unsigned int i, count;
24359 count = read_1_byte (abfd, mac_ptr);
24361 for (i = 0; i < count; ++i)
24363 unsigned int opcode, bytes_read;
24366 opcode = read_1_byte (abfd, mac_ptr);
24368 opcode_definitions[opcode] = mac_ptr;
24369 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24370 mac_ptr += bytes_read;
24379 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24380 including DW_MACRO_import. */
24383 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24385 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24386 struct macro_source_file *current_file,
24387 struct line_header *lh,
24388 struct dwarf2_section_info *section,
24389 int section_is_gnu, int section_is_dwz,
24390 unsigned int offset_size,
24391 htab_t include_hash)
24393 struct objfile *objfile = dwarf2_per_objfile->objfile;
24394 enum dwarf_macro_record_type macinfo_type;
24395 int at_commandline;
24396 const gdb_byte *opcode_definitions[256];
24398 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24399 &offset_size, section_is_gnu);
24400 if (mac_ptr == NULL)
24402 /* We already issued a complaint. */
24406 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24407 GDB is still reading the definitions from command line. First
24408 DW_MACINFO_start_file will need to be ignored as it was already executed
24409 to create CURRENT_FILE for the main source holding also the command line
24410 definitions. On first met DW_MACINFO_start_file this flag is reset to
24411 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24413 at_commandline = 1;
24417 /* Do we at least have room for a macinfo type byte? */
24418 if (mac_ptr >= mac_end)
24420 dwarf2_section_buffer_overflow_complaint (section);
24424 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24427 /* Note that we rely on the fact that the corresponding GNU and
24428 DWARF constants are the same. */
24430 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24431 switch (macinfo_type)
24433 /* A zero macinfo type indicates the end of the macro
24438 case DW_MACRO_define:
24439 case DW_MACRO_undef:
24440 case DW_MACRO_define_strp:
24441 case DW_MACRO_undef_strp:
24442 case DW_MACRO_define_sup:
24443 case DW_MACRO_undef_sup:
24445 unsigned int bytes_read;
24450 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24451 mac_ptr += bytes_read;
24453 if (macinfo_type == DW_MACRO_define
24454 || macinfo_type == DW_MACRO_undef)
24456 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24457 mac_ptr += bytes_read;
24461 LONGEST str_offset;
24463 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24464 mac_ptr += offset_size;
24466 if (macinfo_type == DW_MACRO_define_sup
24467 || macinfo_type == DW_MACRO_undef_sup
24470 struct dwz_file *dwz
24471 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24473 body = read_indirect_string_from_dwz (objfile,
24477 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24481 is_define = (macinfo_type == DW_MACRO_define
24482 || macinfo_type == DW_MACRO_define_strp
24483 || macinfo_type == DW_MACRO_define_sup);
24484 if (! current_file)
24486 /* DWARF violation as no main source is present. */
24487 complaint (&symfile_complaints,
24488 _("debug info with no main source gives macro %s "
24490 is_define ? _("definition") : _("undefinition"),
24494 if ((line == 0 && !at_commandline)
24495 || (line != 0 && at_commandline))
24496 complaint (&symfile_complaints,
24497 _("debug info gives %s macro %s with %s line %d: %s"),
24498 at_commandline ? _("command-line") : _("in-file"),
24499 is_define ? _("definition") : _("undefinition"),
24500 line == 0 ? _("zero") : _("non-zero"), line, body);
24503 parse_macro_definition (current_file, line, body);
24506 gdb_assert (macinfo_type == DW_MACRO_undef
24507 || macinfo_type == DW_MACRO_undef_strp
24508 || macinfo_type == DW_MACRO_undef_sup);
24509 macro_undef (current_file, line, body);
24514 case DW_MACRO_start_file:
24516 unsigned int bytes_read;
24519 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24520 mac_ptr += bytes_read;
24521 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24522 mac_ptr += bytes_read;
24524 if ((line == 0 && !at_commandline)
24525 || (line != 0 && at_commandline))
24526 complaint (&symfile_complaints,
24527 _("debug info gives source %d included "
24528 "from %s at %s line %d"),
24529 file, at_commandline ? _("command-line") : _("file"),
24530 line == 0 ? _("zero") : _("non-zero"), line);
24532 if (at_commandline)
24534 /* This DW_MACRO_start_file was executed in the
24536 at_commandline = 0;
24539 current_file = macro_start_file (file, line, current_file, lh);
24543 case DW_MACRO_end_file:
24544 if (! current_file)
24545 complaint (&symfile_complaints,
24546 _("macro debug info has an unmatched "
24547 "`close_file' directive"));
24550 current_file = current_file->included_by;
24551 if (! current_file)
24553 enum dwarf_macro_record_type next_type;
24555 /* GCC circa March 2002 doesn't produce the zero
24556 type byte marking the end of the compilation
24557 unit. Complain if it's not there, but exit no
24560 /* Do we at least have room for a macinfo type byte? */
24561 if (mac_ptr >= mac_end)
24563 dwarf2_section_buffer_overflow_complaint (section);
24567 /* We don't increment mac_ptr here, so this is just
24570 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24572 if (next_type != 0)
24573 complaint (&symfile_complaints,
24574 _("no terminating 0-type entry for "
24575 "macros in `.debug_macinfo' section"));
24582 case DW_MACRO_import:
24583 case DW_MACRO_import_sup:
24587 bfd *include_bfd = abfd;
24588 struct dwarf2_section_info *include_section = section;
24589 const gdb_byte *include_mac_end = mac_end;
24590 int is_dwz = section_is_dwz;
24591 const gdb_byte *new_mac_ptr;
24593 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24594 mac_ptr += offset_size;
24596 if (macinfo_type == DW_MACRO_import_sup)
24598 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24600 dwarf2_read_section (objfile, &dwz->macro);
24602 include_section = &dwz->macro;
24603 include_bfd = get_section_bfd_owner (include_section);
24604 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24608 new_mac_ptr = include_section->buffer + offset;
24609 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24613 /* This has actually happened; see
24614 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24615 complaint (&symfile_complaints,
24616 _("recursive DW_MACRO_import in "
24617 ".debug_macro section"));
24621 *slot = (void *) new_mac_ptr;
24623 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24624 include_bfd, new_mac_ptr,
24625 include_mac_end, current_file, lh,
24626 section, section_is_gnu, is_dwz,
24627 offset_size, include_hash);
24629 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24634 case DW_MACINFO_vendor_ext:
24635 if (!section_is_gnu)
24637 unsigned int bytes_read;
24639 /* This reads the constant, but since we don't recognize
24640 any vendor extensions, we ignore it. */
24641 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24642 mac_ptr += bytes_read;
24643 read_direct_string (abfd, mac_ptr, &bytes_read);
24644 mac_ptr += bytes_read;
24646 /* We don't recognize any vendor extensions. */
24652 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24653 mac_ptr, mac_end, abfd, offset_size,
24655 if (mac_ptr == NULL)
24660 } while (macinfo_type != 0);
24664 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24665 int section_is_gnu)
24667 struct dwarf2_per_objfile *dwarf2_per_objfile
24668 = cu->per_cu->dwarf2_per_objfile;
24669 struct objfile *objfile = dwarf2_per_objfile->objfile;
24670 struct line_header *lh = cu->line_header;
24672 const gdb_byte *mac_ptr, *mac_end;
24673 struct macro_source_file *current_file = 0;
24674 enum dwarf_macro_record_type macinfo_type;
24675 unsigned int offset_size = cu->header.offset_size;
24676 const gdb_byte *opcode_definitions[256];
24678 struct dwarf2_section_info *section;
24679 const char *section_name;
24681 if (cu->dwo_unit != NULL)
24683 if (section_is_gnu)
24685 section = &cu->dwo_unit->dwo_file->sections.macro;
24686 section_name = ".debug_macro.dwo";
24690 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24691 section_name = ".debug_macinfo.dwo";
24696 if (section_is_gnu)
24698 section = &dwarf2_per_objfile->macro;
24699 section_name = ".debug_macro";
24703 section = &dwarf2_per_objfile->macinfo;
24704 section_name = ".debug_macinfo";
24708 dwarf2_read_section (objfile, section);
24709 if (section->buffer == NULL)
24711 complaint (&symfile_complaints, _("missing %s section"), section_name);
24714 abfd = get_section_bfd_owner (section);
24716 /* First pass: Find the name of the base filename.
24717 This filename is needed in order to process all macros whose definition
24718 (or undefinition) comes from the command line. These macros are defined
24719 before the first DW_MACINFO_start_file entry, and yet still need to be
24720 associated to the base file.
24722 To determine the base file name, we scan the macro definitions until we
24723 reach the first DW_MACINFO_start_file entry. We then initialize
24724 CURRENT_FILE accordingly so that any macro definition found before the
24725 first DW_MACINFO_start_file can still be associated to the base file. */
24727 mac_ptr = section->buffer + offset;
24728 mac_end = section->buffer + section->size;
24730 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24731 &offset_size, section_is_gnu);
24732 if (mac_ptr == NULL)
24734 /* We already issued a complaint. */
24740 /* Do we at least have room for a macinfo type byte? */
24741 if (mac_ptr >= mac_end)
24743 /* Complaint is printed during the second pass as GDB will probably
24744 stop the first pass earlier upon finding
24745 DW_MACINFO_start_file. */
24749 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24752 /* Note that we rely on the fact that the corresponding GNU and
24753 DWARF constants are the same. */
24755 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24756 switch (macinfo_type)
24758 /* A zero macinfo type indicates the end of the macro
24763 case DW_MACRO_define:
24764 case DW_MACRO_undef:
24765 /* Only skip the data by MAC_PTR. */
24767 unsigned int bytes_read;
24769 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24770 mac_ptr += bytes_read;
24771 read_direct_string (abfd, mac_ptr, &bytes_read);
24772 mac_ptr += bytes_read;
24776 case DW_MACRO_start_file:
24778 unsigned int bytes_read;
24781 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24782 mac_ptr += bytes_read;
24783 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24784 mac_ptr += bytes_read;
24786 current_file = macro_start_file (file, line, current_file, lh);
24790 case DW_MACRO_end_file:
24791 /* No data to skip by MAC_PTR. */
24794 case DW_MACRO_define_strp:
24795 case DW_MACRO_undef_strp:
24796 case DW_MACRO_define_sup:
24797 case DW_MACRO_undef_sup:
24799 unsigned int bytes_read;
24801 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24802 mac_ptr += bytes_read;
24803 mac_ptr += offset_size;
24807 case DW_MACRO_import:
24808 case DW_MACRO_import_sup:
24809 /* Note that, according to the spec, a transparent include
24810 chain cannot call DW_MACRO_start_file. So, we can just
24811 skip this opcode. */
24812 mac_ptr += offset_size;
24815 case DW_MACINFO_vendor_ext:
24816 /* Only skip the data by MAC_PTR. */
24817 if (!section_is_gnu)
24819 unsigned int bytes_read;
24821 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24822 mac_ptr += bytes_read;
24823 read_direct_string (abfd, mac_ptr, &bytes_read);
24824 mac_ptr += bytes_read;
24829 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24830 mac_ptr, mac_end, abfd, offset_size,
24832 if (mac_ptr == NULL)
24837 } while (macinfo_type != 0 && current_file == NULL);
24839 /* Second pass: Process all entries.
24841 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24842 command-line macro definitions/undefinitions. This flag is unset when we
24843 reach the first DW_MACINFO_start_file entry. */
24845 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24847 NULL, xcalloc, xfree));
24848 mac_ptr = section->buffer + offset;
24849 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24850 *slot = (void *) mac_ptr;
24851 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24852 abfd, mac_ptr, mac_end,
24853 current_file, lh, section,
24854 section_is_gnu, 0, offset_size,
24855 include_hash.get ());
24858 /* Check if the attribute's form is a DW_FORM_block*
24859 if so return true else false. */
24862 attr_form_is_block (const struct attribute *attr)
24864 return (attr == NULL ? 0 :
24865 attr->form == DW_FORM_block1
24866 || attr->form == DW_FORM_block2
24867 || attr->form == DW_FORM_block4
24868 || attr->form == DW_FORM_block
24869 || attr->form == DW_FORM_exprloc);
24872 /* Return non-zero if ATTR's value is a section offset --- classes
24873 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24874 You may use DW_UNSND (attr) to retrieve such offsets.
24876 Section 7.5.4, "Attribute Encodings", explains that no attribute
24877 may have a value that belongs to more than one of these classes; it
24878 would be ambiguous if we did, because we use the same forms for all
24882 attr_form_is_section_offset (const struct attribute *attr)
24884 return (attr->form == DW_FORM_data4
24885 || attr->form == DW_FORM_data8
24886 || attr->form == DW_FORM_sec_offset);
24889 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24890 zero otherwise. When this function returns true, you can apply
24891 dwarf2_get_attr_constant_value to it.
24893 However, note that for some attributes you must check
24894 attr_form_is_section_offset before using this test. DW_FORM_data4
24895 and DW_FORM_data8 are members of both the constant class, and of
24896 the classes that contain offsets into other debug sections
24897 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24898 that, if an attribute's can be either a constant or one of the
24899 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24900 taken as section offsets, not constants.
24902 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24903 cannot handle that. */
24906 attr_form_is_constant (const struct attribute *attr)
24908 switch (attr->form)
24910 case DW_FORM_sdata:
24911 case DW_FORM_udata:
24912 case DW_FORM_data1:
24913 case DW_FORM_data2:
24914 case DW_FORM_data4:
24915 case DW_FORM_data8:
24916 case DW_FORM_implicit_const:
24924 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24925 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24928 attr_form_is_ref (const struct attribute *attr)
24930 switch (attr->form)
24932 case DW_FORM_ref_addr:
24937 case DW_FORM_ref_udata:
24938 case DW_FORM_GNU_ref_alt:
24945 /* Return the .debug_loc section to use for CU.
24946 For DWO files use .debug_loc.dwo. */
24948 static struct dwarf2_section_info *
24949 cu_debug_loc_section (struct dwarf2_cu *cu)
24951 struct dwarf2_per_objfile *dwarf2_per_objfile
24952 = cu->per_cu->dwarf2_per_objfile;
24956 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24958 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
24960 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24961 : &dwarf2_per_objfile->loc);
24964 /* A helper function that fills in a dwarf2_loclist_baton. */
24967 fill_in_loclist_baton (struct dwarf2_cu *cu,
24968 struct dwarf2_loclist_baton *baton,
24969 const struct attribute *attr)
24971 struct dwarf2_per_objfile *dwarf2_per_objfile
24972 = cu->per_cu->dwarf2_per_objfile;
24973 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24975 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24977 baton->per_cu = cu->per_cu;
24978 gdb_assert (baton->per_cu);
24979 /* We don't know how long the location list is, but make sure we
24980 don't run off the edge of the section. */
24981 baton->size = section->size - DW_UNSND (attr);
24982 baton->data = section->buffer + DW_UNSND (attr);
24983 baton->base_address = cu->base_address;
24984 baton->from_dwo = cu->dwo_unit != NULL;
24988 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24989 struct dwarf2_cu *cu, int is_block)
24991 struct dwarf2_per_objfile *dwarf2_per_objfile
24992 = cu->per_cu->dwarf2_per_objfile;
24993 struct objfile *objfile = dwarf2_per_objfile->objfile;
24994 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24996 if (attr_form_is_section_offset (attr)
24997 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24998 the section. If so, fall through to the complaint in the
25000 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25002 struct dwarf2_loclist_baton *baton;
25004 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25006 fill_in_loclist_baton (cu, baton, attr);
25008 if (cu->base_known == 0)
25009 complaint (&symfile_complaints,
25010 _("Location list used without "
25011 "specifying the CU base address."));
25013 SYMBOL_ACLASS_INDEX (sym) = (is_block
25014 ? dwarf2_loclist_block_index
25015 : dwarf2_loclist_index);
25016 SYMBOL_LOCATION_BATON (sym) = baton;
25020 struct dwarf2_locexpr_baton *baton;
25022 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25023 baton->per_cu = cu->per_cu;
25024 gdb_assert (baton->per_cu);
25026 if (attr_form_is_block (attr))
25028 /* Note that we're just copying the block's data pointer
25029 here, not the actual data. We're still pointing into the
25030 info_buffer for SYM's objfile; right now we never release
25031 that buffer, but when we do clean up properly this may
25033 baton->size = DW_BLOCK (attr)->size;
25034 baton->data = DW_BLOCK (attr)->data;
25038 dwarf2_invalid_attrib_class_complaint ("location description",
25039 SYMBOL_NATURAL_NAME (sym));
25043 SYMBOL_ACLASS_INDEX (sym) = (is_block
25044 ? dwarf2_locexpr_block_index
25045 : dwarf2_locexpr_index);
25046 SYMBOL_LOCATION_BATON (sym) = baton;
25050 /* Return the OBJFILE associated with the compilation unit CU. If CU
25051 came from a separate debuginfo file, then the master objfile is
25055 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25057 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25059 /* Return the master objfile, so that we can report and look up the
25060 correct file containing this variable. */
25061 if (objfile->separate_debug_objfile_backlink)
25062 objfile = objfile->separate_debug_objfile_backlink;
25067 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25068 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25069 CU_HEADERP first. */
25071 static const struct comp_unit_head *
25072 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25073 struct dwarf2_per_cu_data *per_cu)
25075 const gdb_byte *info_ptr;
25078 return &per_cu->cu->header;
25080 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25082 memset (cu_headerp, 0, sizeof (*cu_headerp));
25083 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25084 rcuh_kind::COMPILE);
25089 /* Return the address size given in the compilation unit header for CU. */
25092 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25094 struct comp_unit_head cu_header_local;
25095 const struct comp_unit_head *cu_headerp;
25097 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25099 return cu_headerp->addr_size;
25102 /* Return the offset size given in the compilation unit header for CU. */
25105 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25107 struct comp_unit_head cu_header_local;
25108 const struct comp_unit_head *cu_headerp;
25110 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25112 return cu_headerp->offset_size;
25115 /* See its dwarf2loc.h declaration. */
25118 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25120 struct comp_unit_head cu_header_local;
25121 const struct comp_unit_head *cu_headerp;
25123 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25125 if (cu_headerp->version == 2)
25126 return cu_headerp->addr_size;
25128 return cu_headerp->offset_size;
25131 /* Return the text offset of the CU. The returned offset comes from
25132 this CU's objfile. If this objfile came from a separate debuginfo
25133 file, then the offset may be different from the corresponding
25134 offset in the parent objfile. */
25137 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25139 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25141 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25144 /* Return DWARF version number of PER_CU. */
25147 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25149 return per_cu->dwarf_version;
25152 /* Locate the .debug_info compilation unit from CU's objfile which contains
25153 the DIE at OFFSET. Raises an error on failure. */
25155 static struct dwarf2_per_cu_data *
25156 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25157 unsigned int offset_in_dwz,
25158 struct dwarf2_per_objfile *dwarf2_per_objfile)
25160 struct dwarf2_per_cu_data *this_cu;
25162 const sect_offset *cu_off;
25165 high = dwarf2_per_objfile->n_comp_units - 1;
25168 struct dwarf2_per_cu_data *mid_cu;
25169 int mid = low + (high - low) / 2;
25171 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25172 cu_off = &mid_cu->sect_off;
25173 if (mid_cu->is_dwz > offset_in_dwz
25174 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25179 gdb_assert (low == high);
25180 this_cu = dwarf2_per_objfile->all_comp_units[low];
25181 cu_off = &this_cu->sect_off;
25182 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25184 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25185 error (_("Dwarf Error: could not find partial DIE containing "
25186 "offset 0x%x [in module %s]"),
25187 to_underlying (sect_off),
25188 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25190 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25192 return dwarf2_per_objfile->all_comp_units[low-1];
25196 this_cu = dwarf2_per_objfile->all_comp_units[low];
25197 if (low == dwarf2_per_objfile->n_comp_units - 1
25198 && sect_off >= this_cu->sect_off + this_cu->length)
25199 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25200 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25205 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25208 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
25210 memset (cu, 0, sizeof (*cu));
25212 cu->per_cu = per_cu;
25213 obstack_init (&cu->comp_unit_obstack);
25216 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25219 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25220 enum language pretend_language)
25222 struct attribute *attr;
25224 /* Set the language we're debugging. */
25225 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25227 set_cu_language (DW_UNSND (attr), cu);
25230 cu->language = pretend_language;
25231 cu->language_defn = language_def (cu->language);
25234 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25237 /* Release one cached compilation unit, CU. We unlink it from the tree
25238 of compilation units, but we don't remove it from the read_in_chain;
25239 the caller is responsible for that.
25240 NOTE: DATA is a void * because this function is also used as a
25241 cleanup routine. */
25244 free_heap_comp_unit (void *data)
25246 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
25248 gdb_assert (cu->per_cu != NULL);
25249 cu->per_cu->cu = NULL;
25252 obstack_free (&cu->comp_unit_obstack, NULL);
25257 /* This cleanup function is passed the address of a dwarf2_cu on the stack
25258 when we're finished with it. We can't free the pointer itself, but be
25259 sure to unlink it from the cache. Also release any associated storage. */
25262 free_stack_comp_unit (void *data)
25264 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
25266 gdb_assert (cu->per_cu != NULL);
25267 cu->per_cu->cu = NULL;
25270 obstack_free (&cu->comp_unit_obstack, NULL);
25271 cu->partial_dies = NULL;
25274 /* Free all cached compilation units. */
25277 free_cached_comp_units (void *data)
25279 struct dwarf2_per_objfile *dwarf2_per_objfile
25280 = (struct dwarf2_per_objfile *) data;
25282 dwarf2_per_objfile->free_cached_comp_units ();
25285 /* Increase the age counter on each cached compilation unit, and free
25286 any that are too old. */
25289 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25291 struct dwarf2_per_cu_data *per_cu, **last_chain;
25293 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25294 per_cu = dwarf2_per_objfile->read_in_chain;
25295 while (per_cu != NULL)
25297 per_cu->cu->last_used ++;
25298 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25299 dwarf2_mark (per_cu->cu);
25300 per_cu = per_cu->cu->read_in_chain;
25303 per_cu = dwarf2_per_objfile->read_in_chain;
25304 last_chain = &dwarf2_per_objfile->read_in_chain;
25305 while (per_cu != NULL)
25307 struct dwarf2_per_cu_data *next_cu;
25309 next_cu = per_cu->cu->read_in_chain;
25311 if (!per_cu->cu->mark)
25313 free_heap_comp_unit (per_cu->cu);
25314 *last_chain = next_cu;
25317 last_chain = &per_cu->cu->read_in_chain;
25323 /* Remove a single compilation unit from the cache. */
25326 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25328 struct dwarf2_per_cu_data *per_cu, **last_chain;
25329 struct dwarf2_per_objfile *dwarf2_per_objfile
25330 = target_per_cu->dwarf2_per_objfile;
25332 per_cu = dwarf2_per_objfile->read_in_chain;
25333 last_chain = &dwarf2_per_objfile->read_in_chain;
25334 while (per_cu != NULL)
25336 struct dwarf2_per_cu_data *next_cu;
25338 next_cu = per_cu->cu->read_in_chain;
25340 if (per_cu == target_per_cu)
25342 free_heap_comp_unit (per_cu->cu);
25344 *last_chain = next_cu;
25348 last_chain = &per_cu->cu->read_in_chain;
25354 /* Release all extra memory associated with OBJFILE. */
25357 dwarf2_free_objfile (struct objfile *objfile)
25359 struct dwarf2_per_objfile *dwarf2_per_objfile
25360 = get_dwarf2_per_objfile (objfile);
25362 if (dwarf2_per_objfile == NULL)
25365 dwarf2_per_objfile->~dwarf2_per_objfile ();
25368 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25369 We store these in a hash table separate from the DIEs, and preserve them
25370 when the DIEs are flushed out of cache.
25372 The CU "per_cu" pointer is needed because offset alone is not enough to
25373 uniquely identify the type. A file may have multiple .debug_types sections,
25374 or the type may come from a DWO file. Furthermore, while it's more logical
25375 to use per_cu->section+offset, with Fission the section with the data is in
25376 the DWO file but we don't know that section at the point we need it.
25377 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25378 because we can enter the lookup routine, get_die_type_at_offset, from
25379 outside this file, and thus won't necessarily have PER_CU->cu.
25380 Fortunately, PER_CU is stable for the life of the objfile. */
25382 struct dwarf2_per_cu_offset_and_type
25384 const struct dwarf2_per_cu_data *per_cu;
25385 sect_offset sect_off;
25389 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25392 per_cu_offset_and_type_hash (const void *item)
25394 const struct dwarf2_per_cu_offset_and_type *ofs
25395 = (const struct dwarf2_per_cu_offset_and_type *) item;
25397 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25400 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25403 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25405 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25406 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25407 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25408 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25410 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25411 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25414 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25415 table if necessary. For convenience, return TYPE.
25417 The DIEs reading must have careful ordering to:
25418 * Not cause infite loops trying to read in DIEs as a prerequisite for
25419 reading current DIE.
25420 * Not trying to dereference contents of still incompletely read in types
25421 while reading in other DIEs.
25422 * Enable referencing still incompletely read in types just by a pointer to
25423 the type without accessing its fields.
25425 Therefore caller should follow these rules:
25426 * Try to fetch any prerequisite types we may need to build this DIE type
25427 before building the type and calling set_die_type.
25428 * After building type call set_die_type for current DIE as soon as
25429 possible before fetching more types to complete the current type.
25430 * Make the type as complete as possible before fetching more types. */
25432 static struct type *
25433 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25435 struct dwarf2_per_objfile *dwarf2_per_objfile
25436 = cu->per_cu->dwarf2_per_objfile;
25437 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25438 struct objfile *objfile = dwarf2_per_objfile->objfile;
25439 struct attribute *attr;
25440 struct dynamic_prop prop;
25442 /* For Ada types, make sure that the gnat-specific data is always
25443 initialized (if not already set). There are a few types where
25444 we should not be doing so, because the type-specific area is
25445 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25446 where the type-specific area is used to store the floatformat).
25447 But this is not a problem, because the gnat-specific information
25448 is actually not needed for these types. */
25449 if (need_gnat_info (cu)
25450 && TYPE_CODE (type) != TYPE_CODE_FUNC
25451 && TYPE_CODE (type) != TYPE_CODE_FLT
25452 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25453 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25454 && TYPE_CODE (type) != TYPE_CODE_METHOD
25455 && !HAVE_GNAT_AUX_INFO (type))
25456 INIT_GNAT_SPECIFIC (type);
25458 /* Read DW_AT_allocated and set in type. */
25459 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25460 if (attr_form_is_block (attr))
25462 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25463 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25465 else if (attr != NULL)
25467 complaint (&symfile_complaints,
25468 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25469 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25470 to_underlying (die->sect_off));
25473 /* Read DW_AT_associated and set in type. */
25474 attr = dwarf2_attr (die, DW_AT_associated, cu);
25475 if (attr_form_is_block (attr))
25477 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25478 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25480 else if (attr != NULL)
25482 complaint (&symfile_complaints,
25483 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25484 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25485 to_underlying (die->sect_off));
25488 /* Read DW_AT_data_location and set in type. */
25489 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25490 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25491 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25493 if (dwarf2_per_objfile->die_type_hash == NULL)
25495 dwarf2_per_objfile->die_type_hash =
25496 htab_create_alloc_ex (127,
25497 per_cu_offset_and_type_hash,
25498 per_cu_offset_and_type_eq,
25500 &objfile->objfile_obstack,
25501 hashtab_obstack_allocate,
25502 dummy_obstack_deallocate);
25505 ofs.per_cu = cu->per_cu;
25506 ofs.sect_off = die->sect_off;
25508 slot = (struct dwarf2_per_cu_offset_and_type **)
25509 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25511 complaint (&symfile_complaints,
25512 _("A problem internal to GDB: DIE 0x%x has type already set"),
25513 to_underlying (die->sect_off));
25514 *slot = XOBNEW (&objfile->objfile_obstack,
25515 struct dwarf2_per_cu_offset_and_type);
25520 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25521 or return NULL if the die does not have a saved type. */
25523 static struct type *
25524 get_die_type_at_offset (sect_offset sect_off,
25525 struct dwarf2_per_cu_data *per_cu)
25527 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25528 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25530 if (dwarf2_per_objfile->die_type_hash == NULL)
25533 ofs.per_cu = per_cu;
25534 ofs.sect_off = sect_off;
25535 slot = ((struct dwarf2_per_cu_offset_and_type *)
25536 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25543 /* Look up the type for DIE in CU in die_type_hash,
25544 or return NULL if DIE does not have a saved type. */
25546 static struct type *
25547 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25549 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25552 /* Add a dependence relationship from CU to REF_PER_CU. */
25555 dwarf2_add_dependence (struct dwarf2_cu *cu,
25556 struct dwarf2_per_cu_data *ref_per_cu)
25560 if (cu->dependencies == NULL)
25562 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25563 NULL, &cu->comp_unit_obstack,
25564 hashtab_obstack_allocate,
25565 dummy_obstack_deallocate);
25567 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25569 *slot = ref_per_cu;
25572 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25573 Set the mark field in every compilation unit in the
25574 cache that we must keep because we are keeping CU. */
25577 dwarf2_mark_helper (void **slot, void *data)
25579 struct dwarf2_per_cu_data *per_cu;
25581 per_cu = (struct dwarf2_per_cu_data *) *slot;
25583 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25584 reading of the chain. As such dependencies remain valid it is not much
25585 useful to track and undo them during QUIT cleanups. */
25586 if (per_cu->cu == NULL)
25589 if (per_cu->cu->mark)
25591 per_cu->cu->mark = 1;
25593 if (per_cu->cu->dependencies != NULL)
25594 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25599 /* Set the mark field in CU and in every other compilation unit in the
25600 cache that we must keep because we are keeping CU. */
25603 dwarf2_mark (struct dwarf2_cu *cu)
25608 if (cu->dependencies != NULL)
25609 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25613 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25617 per_cu->cu->mark = 0;
25618 per_cu = per_cu->cu->read_in_chain;
25622 /* Trivial hash function for partial_die_info: the hash value of a DIE
25623 is its offset in .debug_info for this objfile. */
25626 partial_die_hash (const void *item)
25628 const struct partial_die_info *part_die
25629 = (const struct partial_die_info *) item;
25631 return to_underlying (part_die->sect_off);
25634 /* Trivial comparison function for partial_die_info structures: two DIEs
25635 are equal if they have the same offset. */
25638 partial_die_eq (const void *item_lhs, const void *item_rhs)
25640 const struct partial_die_info *part_die_lhs
25641 = (const struct partial_die_info *) item_lhs;
25642 const struct partial_die_info *part_die_rhs
25643 = (const struct partial_die_info *) item_rhs;
25645 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25648 static struct cmd_list_element *set_dwarf_cmdlist;
25649 static struct cmd_list_element *show_dwarf_cmdlist;
25652 set_dwarf_cmd (const char *args, int from_tty)
25654 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25659 show_dwarf_cmd (const char *args, int from_tty)
25661 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25664 /* Free data associated with OBJFILE, if necessary. */
25667 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25669 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25672 for (ix = 0; ix < data->n_comp_units; ++ix)
25673 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25675 for (ix = 0; ix < data->n_type_units; ++ix)
25676 VEC_free (dwarf2_per_cu_ptr,
25677 data->all_type_units[ix]->per_cu.imported_symtabs);
25678 xfree (data->all_type_units);
25680 VEC_free (dwarf2_section_info_def, data->types);
25682 if (data->dwo_files)
25683 free_dwo_files (data->dwo_files, objfile);
25684 if (data->dwp_file)
25685 gdb_bfd_unref (data->dwp_file->dbfd);
25687 if (data->dwz_file && data->dwz_file->dwz_bfd)
25688 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25690 if (data->index_table != NULL)
25691 data->index_table->~mapped_index ();
25695 /* The "save gdb-index" command. */
25697 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25701 file_write (FILE *file, const void *data, size_t size)
25703 if (fwrite (data, 1, size, file) != size)
25704 error (_("couldn't data write to file"));
25707 /* Write the contents of VEC to FILE, with error checking. */
25709 template<typename Elem, typename Alloc>
25711 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25713 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25716 /* In-memory buffer to prepare data to be written later to a file. */
25720 /* Copy DATA to the end of the buffer. */
25721 template<typename T>
25722 void append_data (const T &data)
25724 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25725 reinterpret_cast<const gdb_byte *> (&data + 1),
25726 grow (sizeof (data)));
25729 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25730 terminating zero is appended too. */
25731 void append_cstr0 (const char *cstr)
25733 const size_t size = strlen (cstr) + 1;
25734 std::copy (cstr, cstr + size, grow (size));
25737 /* Store INPUT as ULEB128 to the end of buffer. */
25738 void append_unsigned_leb128 (ULONGEST input)
25742 gdb_byte output = input & 0x7f;
25746 append_data (output);
25752 /* Accept a host-format integer in VAL and append it to the buffer
25753 as a target-format integer which is LEN bytes long. */
25754 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25756 ::store_unsigned_integer (grow (len), len, byte_order, val);
25759 /* Return the size of the buffer. */
25760 size_t size () const
25762 return m_vec.size ();
25765 /* Return true iff the buffer is empty. */
25766 bool empty () const
25768 return m_vec.empty ();
25771 /* Write the buffer to FILE. */
25772 void file_write (FILE *file) const
25774 ::file_write (file, m_vec);
25778 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25779 the start of the new block. */
25780 gdb_byte *grow (size_t size)
25782 m_vec.resize (m_vec.size () + size);
25783 return &*m_vec.end () - size;
25786 gdb::byte_vector m_vec;
25789 /* An entry in the symbol table. */
25790 struct symtab_index_entry
25792 /* The name of the symbol. */
25794 /* The offset of the name in the constant pool. */
25795 offset_type index_offset;
25796 /* A sorted vector of the indices of all the CUs that hold an object
25798 std::vector<offset_type> cu_indices;
25801 /* The symbol table. This is a power-of-2-sized hash table. */
25802 struct mapped_symtab
25806 data.resize (1024);
25809 offset_type n_elements = 0;
25810 std::vector<symtab_index_entry> data;
25813 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25816 Function is used only during write_hash_table so no index format backward
25817 compatibility is needed. */
25819 static symtab_index_entry &
25820 find_slot (struct mapped_symtab *symtab, const char *name)
25822 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25824 index = hash & (symtab->data.size () - 1);
25825 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25829 if (symtab->data[index].name == NULL
25830 || strcmp (name, symtab->data[index].name) == 0)
25831 return symtab->data[index];
25832 index = (index + step) & (symtab->data.size () - 1);
25836 /* Expand SYMTAB's hash table. */
25839 hash_expand (struct mapped_symtab *symtab)
25841 auto old_entries = std::move (symtab->data);
25843 symtab->data.clear ();
25844 symtab->data.resize (old_entries.size () * 2);
25846 for (auto &it : old_entries)
25847 if (it.name != NULL)
25849 auto &ref = find_slot (symtab, it.name);
25850 ref = std::move (it);
25854 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25855 CU_INDEX is the index of the CU in which the symbol appears.
25856 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25859 add_index_entry (struct mapped_symtab *symtab, const char *name,
25860 int is_static, gdb_index_symbol_kind kind,
25861 offset_type cu_index)
25863 offset_type cu_index_and_attrs;
25865 ++symtab->n_elements;
25866 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25867 hash_expand (symtab);
25869 symtab_index_entry &slot = find_slot (symtab, name);
25870 if (slot.name == NULL)
25873 /* index_offset is set later. */
25876 cu_index_and_attrs = 0;
25877 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25878 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25879 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25881 /* We don't want to record an index value twice as we want to avoid the
25883 We process all global symbols and then all static symbols
25884 (which would allow us to avoid the duplication by only having to check
25885 the last entry pushed), but a symbol could have multiple kinds in one CU.
25886 To keep things simple we don't worry about the duplication here and
25887 sort and uniqufy the list after we've processed all symbols. */
25888 slot.cu_indices.push_back (cu_index_and_attrs);
25891 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25894 uniquify_cu_indices (struct mapped_symtab *symtab)
25896 for (auto &entry : symtab->data)
25898 if (entry.name != NULL && !entry.cu_indices.empty ())
25900 auto &cu_indices = entry.cu_indices;
25901 std::sort (cu_indices.begin (), cu_indices.end ());
25902 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25903 cu_indices.erase (from, cu_indices.end ());
25908 /* A form of 'const char *' suitable for container keys. Only the
25909 pointer is stored. The strings themselves are compared, not the
25914 c_str_view (const char *cstr)
25918 bool operator== (const c_str_view &other) const
25920 return strcmp (m_cstr, other.m_cstr) == 0;
25923 /* Return the underlying C string. Note, the returned string is
25924 only a reference with lifetime of this object. */
25925 const char *c_str () const
25931 friend class c_str_view_hasher;
25932 const char *const m_cstr;
25935 /* A std::unordered_map::hasher for c_str_view that uses the right
25936 hash function for strings in a mapped index. */
25937 class c_str_view_hasher
25940 size_t operator () (const c_str_view &x) const
25942 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25946 /* A std::unordered_map::hasher for std::vector<>. */
25947 template<typename T>
25948 class vector_hasher
25951 size_t operator () (const std::vector<T> &key) const
25953 return iterative_hash (key.data (),
25954 sizeof (key.front ()) * key.size (), 0);
25958 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25959 constant pool entries going into the data buffer CPOOL. */
25962 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25965 /* Elements are sorted vectors of the indices of all the CUs that
25966 hold an object of this name. */
25967 std::unordered_map<std::vector<offset_type>, offset_type,
25968 vector_hasher<offset_type>>
25971 /* We add all the index vectors to the constant pool first, to
25972 ensure alignment is ok. */
25973 for (symtab_index_entry &entry : symtab->data)
25975 if (entry.name == NULL)
25977 gdb_assert (entry.index_offset == 0);
25979 /* Finding before inserting is faster than always trying to
25980 insert, because inserting always allocates a node, does the
25981 lookup, and then destroys the new node if another node
25982 already had the same key. C++17 try_emplace will avoid
25985 = symbol_hash_table.find (entry.cu_indices);
25986 if (found != symbol_hash_table.end ())
25988 entry.index_offset = found->second;
25992 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25993 entry.index_offset = cpool.size ();
25994 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25995 for (const auto index : entry.cu_indices)
25996 cpool.append_data (MAYBE_SWAP (index));
26000 /* Now write out the hash table. */
26001 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
26002 for (const auto &entry : symtab->data)
26004 offset_type str_off, vec_off;
26006 if (entry.name != NULL)
26008 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
26009 if (insertpair.second)
26010 cpool.append_cstr0 (entry.name);
26011 str_off = insertpair.first->second;
26012 vec_off = entry.index_offset;
26016 /* While 0 is a valid constant pool index, it is not valid
26017 to have 0 for both offsets. */
26022 output.append_data (MAYBE_SWAP (str_off));
26023 output.append_data (MAYBE_SWAP (vec_off));
26027 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
26029 /* Helper struct for building the address table. */
26030 struct addrmap_index_data
26032 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26033 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26036 struct objfile *objfile;
26037 data_buf &addr_vec;
26038 psym_index_map &cu_index_htab;
26040 /* Non-zero if the previous_* fields are valid.
26041 We can't write an entry until we see the next entry (since it is only then
26042 that we know the end of the entry). */
26043 int previous_valid;
26044 /* Index of the CU in the table of all CUs in the index file. */
26045 unsigned int previous_cu_index;
26046 /* Start address of the CU. */
26047 CORE_ADDR previous_cu_start;
26050 /* Write an address entry to ADDR_VEC. */
26053 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26054 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26056 CORE_ADDR baseaddr;
26058 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26060 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26061 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26062 addr_vec.append_data (MAYBE_SWAP (cu_index));
26065 /* Worker function for traversing an addrmap to build the address table. */
26068 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26070 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26071 struct partial_symtab *pst = (struct partial_symtab *) obj;
26073 if (data->previous_valid)
26074 add_address_entry (data->objfile, data->addr_vec,
26075 data->previous_cu_start, start_addr,
26076 data->previous_cu_index);
26078 data->previous_cu_start = start_addr;
26081 const auto it = data->cu_index_htab.find (pst);
26082 gdb_assert (it != data->cu_index_htab.cend ());
26083 data->previous_cu_index = it->second;
26084 data->previous_valid = 1;
26087 data->previous_valid = 0;
26092 /* Write OBJFILE's address map to ADDR_VEC.
26093 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26094 in the index file. */
26097 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26098 psym_index_map &cu_index_htab)
26100 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26102 /* When writing the address table, we have to cope with the fact that
26103 the addrmap iterator only provides the start of a region; we have to
26104 wait until the next invocation to get the start of the next region. */
26106 addrmap_index_data.objfile = objfile;
26107 addrmap_index_data.previous_valid = 0;
26109 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26110 &addrmap_index_data);
26112 /* It's highly unlikely the last entry (end address = 0xff...ff)
26113 is valid, but we should still handle it.
26114 The end address is recorded as the start of the next region, but that
26115 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26117 if (addrmap_index_data.previous_valid)
26118 add_address_entry (objfile, addr_vec,
26119 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26120 addrmap_index_data.previous_cu_index);
26123 /* Return the symbol kind of PSYM. */
26125 static gdb_index_symbol_kind
26126 symbol_kind (struct partial_symbol *psym)
26128 domain_enum domain = PSYMBOL_DOMAIN (psym);
26129 enum address_class aclass = PSYMBOL_CLASS (psym);
26137 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26139 return GDB_INDEX_SYMBOL_KIND_TYPE;
26141 case LOC_CONST_BYTES:
26142 case LOC_OPTIMIZED_OUT:
26144 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26146 /* Note: It's currently impossible to recognize psyms as enum values
26147 short of reading the type info. For now punt. */
26148 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26150 /* There are other LOC_FOO values that one might want to classify
26151 as variables, but dwarf2read.c doesn't currently use them. */
26152 return GDB_INDEX_SYMBOL_KIND_OTHER;
26154 case STRUCT_DOMAIN:
26155 return GDB_INDEX_SYMBOL_KIND_TYPE;
26157 return GDB_INDEX_SYMBOL_KIND_OTHER;
26161 /* Add a list of partial symbols to SYMTAB. */
26164 write_psymbols (struct mapped_symtab *symtab,
26165 std::unordered_set<partial_symbol *> &psyms_seen,
26166 struct partial_symbol **psymp,
26168 offset_type cu_index,
26171 for (; count-- > 0; ++psymp)
26173 struct partial_symbol *psym = *psymp;
26175 if (SYMBOL_LANGUAGE (psym) == language_ada)
26176 error (_("Ada is not currently supported by the index"));
26178 /* Only add a given psymbol once. */
26179 if (psyms_seen.insert (psym).second)
26181 gdb_index_symbol_kind kind = symbol_kind (psym);
26183 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26184 is_static, kind, cu_index);
26189 /* A helper struct used when iterating over debug_types. */
26190 struct signatured_type_index_data
26192 signatured_type_index_data (data_buf &types_list_,
26193 std::unordered_set<partial_symbol *> &psyms_seen_)
26194 : types_list (types_list_), psyms_seen (psyms_seen_)
26197 struct objfile *objfile;
26198 struct mapped_symtab *symtab;
26199 data_buf &types_list;
26200 std::unordered_set<partial_symbol *> &psyms_seen;
26204 /* A helper function that writes a single signatured_type to an
26208 write_one_signatured_type (void **slot, void *d)
26210 struct signatured_type_index_data *info
26211 = (struct signatured_type_index_data *) d;
26212 struct signatured_type *entry = (struct signatured_type *) *slot;
26213 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26215 write_psymbols (info->symtab,
26217 &info->objfile->global_psymbols[psymtab->globals_offset],
26218 psymtab->n_global_syms, info->cu_index,
26220 write_psymbols (info->symtab,
26222 &info->objfile->static_psymbols[psymtab->statics_offset],
26223 psymtab->n_static_syms, info->cu_index,
26226 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26227 to_underlying (entry->per_cu.sect_off));
26228 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26229 to_underlying (entry->type_offset_in_tu));
26230 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26237 /* Recurse into all "included" dependencies and count their symbols as
26238 if they appeared in this psymtab. */
26241 recursively_count_psymbols (struct partial_symtab *psymtab,
26242 size_t &psyms_seen)
26244 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26245 if (psymtab->dependencies[i]->user != NULL)
26246 recursively_count_psymbols (psymtab->dependencies[i],
26249 psyms_seen += psymtab->n_global_syms;
26250 psyms_seen += psymtab->n_static_syms;
26253 /* Recurse into all "included" dependencies and write their symbols as
26254 if they appeared in this psymtab. */
26257 recursively_write_psymbols (struct objfile *objfile,
26258 struct partial_symtab *psymtab,
26259 struct mapped_symtab *symtab,
26260 std::unordered_set<partial_symbol *> &psyms_seen,
26261 offset_type cu_index)
26265 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26266 if (psymtab->dependencies[i]->user != NULL)
26267 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26268 symtab, psyms_seen, cu_index);
26270 write_psymbols (symtab,
26272 &objfile->global_psymbols[psymtab->globals_offset],
26273 psymtab->n_global_syms, cu_index,
26275 write_psymbols (symtab,
26277 &objfile->static_psymbols[psymtab->statics_offset],
26278 psymtab->n_static_syms, cu_index,
26282 /* DWARF-5 .debug_names builder. */
26286 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26287 bfd_endian dwarf5_byte_order)
26288 : m_dwarf5_byte_order (dwarf5_byte_order),
26289 m_dwarf32 (dwarf5_byte_order),
26290 m_dwarf64 (dwarf5_byte_order),
26291 m_dwarf (is_dwarf64
26292 ? static_cast<dwarf &> (m_dwarf64)
26293 : static_cast<dwarf &> (m_dwarf32)),
26294 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26295 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26296 m_debugstrlookup (dwarf2_per_objfile)
26299 int dwarf5_offset_size () const
26301 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26302 return dwarf5_is_dwarf64 ? 8 : 4;
26305 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26306 enum class unit_kind { cu, tu };
26308 /* Insert one symbol. */
26309 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26312 const int dwarf_tag = psymbol_tag (psym);
26313 if (dwarf_tag == 0)
26315 const char *const name = SYMBOL_SEARCH_NAME (psym);
26316 const auto insertpair
26317 = m_name_to_value_set.emplace (c_str_view (name),
26318 std::set<symbol_value> ());
26319 std::set<symbol_value> &value_set = insertpair.first->second;
26320 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26323 /* Build all the tables. All symbols must be already inserted.
26324 This function does not call file_write, caller has to do it
26328 /* Verify the build method has not be called twice. */
26329 gdb_assert (m_abbrev_table.empty ());
26330 const size_t name_count = m_name_to_value_set.size ();
26331 m_bucket_table.resize
26332 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26333 m_hash_table.reserve (name_count);
26334 m_name_table_string_offs.reserve (name_count);
26335 m_name_table_entry_offs.reserve (name_count);
26337 /* Map each hash of symbol to its name and value. */
26338 struct hash_it_pair
26341 decltype (m_name_to_value_set)::const_iterator it;
26343 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26344 bucket_hash.resize (m_bucket_table.size ());
26345 for (decltype (m_name_to_value_set)::const_iterator it
26346 = m_name_to_value_set.cbegin ();
26347 it != m_name_to_value_set.cend ();
26350 const char *const name = it->first.c_str ();
26351 const uint32_t hash = dwarf5_djb_hash (name);
26352 hash_it_pair hashitpair;
26353 hashitpair.hash = hash;
26354 hashitpair.it = it;
26355 auto &slot = bucket_hash[hash % bucket_hash.size()];
26356 slot.push_front (std::move (hashitpair));
26358 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26360 const std::forward_list<hash_it_pair> &hashitlist
26361 = bucket_hash[bucket_ix];
26362 if (hashitlist.empty ())
26364 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26365 /* The hashes array is indexed starting at 1. */
26366 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26367 sizeof (bucket_slot), m_dwarf5_byte_order,
26368 m_hash_table.size () + 1);
26369 for (const hash_it_pair &hashitpair : hashitlist)
26371 m_hash_table.push_back (0);
26372 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26373 (&m_hash_table.back ()),
26374 sizeof (m_hash_table.back ()),
26375 m_dwarf5_byte_order, hashitpair.hash);
26376 const c_str_view &name = hashitpair.it->first;
26377 const std::set<symbol_value> &value_set = hashitpair.it->second;
26378 m_name_table_string_offs.push_back_reorder
26379 (m_debugstrlookup.lookup (name.c_str ()));
26380 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26381 gdb_assert (!value_set.empty ());
26382 for (const symbol_value &value : value_set)
26384 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26389 idx = m_idx_next++;
26390 m_abbrev_table.append_unsigned_leb128 (idx);
26391 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26392 m_abbrev_table.append_unsigned_leb128
26393 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26394 : DW_IDX_type_unit);
26395 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26396 m_abbrev_table.append_unsigned_leb128 (value.is_static
26397 ? DW_IDX_GNU_internal
26398 : DW_IDX_GNU_external);
26399 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26401 /* Terminate attributes list. */
26402 m_abbrev_table.append_unsigned_leb128 (0);
26403 m_abbrev_table.append_unsigned_leb128 (0);
26406 m_entry_pool.append_unsigned_leb128 (idx);
26407 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26410 /* Terminate the list of CUs. */
26411 m_entry_pool.append_unsigned_leb128 (0);
26414 gdb_assert (m_hash_table.size () == name_count);
26416 /* Terminate tags list. */
26417 m_abbrev_table.append_unsigned_leb128 (0);
26420 /* Return .debug_names bucket count. This must be called only after
26421 calling the build method. */
26422 uint32_t bucket_count () const
26424 /* Verify the build method has been already called. */
26425 gdb_assert (!m_abbrev_table.empty ());
26426 const uint32_t retval = m_bucket_table.size ();
26428 /* Check for overflow. */
26429 gdb_assert (retval == m_bucket_table.size ());
26433 /* Return .debug_names names count. This must be called only after
26434 calling the build method. */
26435 uint32_t name_count () const
26437 /* Verify the build method has been already called. */
26438 gdb_assert (!m_abbrev_table.empty ());
26439 const uint32_t retval = m_hash_table.size ();
26441 /* Check for overflow. */
26442 gdb_assert (retval == m_hash_table.size ());
26446 /* Return number of bytes of .debug_names abbreviation table. This
26447 must be called only after calling the build method. */
26448 uint32_t abbrev_table_bytes () const
26450 gdb_assert (!m_abbrev_table.empty ());
26451 return m_abbrev_table.size ();
26454 /* Recurse into all "included" dependencies and store their symbols
26455 as if they appeared in this psymtab. */
26456 void recursively_write_psymbols
26457 (struct objfile *objfile,
26458 struct partial_symtab *psymtab,
26459 std::unordered_set<partial_symbol *> &psyms_seen,
26462 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26463 if (psymtab->dependencies[i]->user != NULL)
26464 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26465 psyms_seen, cu_index);
26467 write_psymbols (psyms_seen,
26468 &objfile->global_psymbols[psymtab->globals_offset],
26469 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26470 write_psymbols (psyms_seen,
26471 &objfile->static_psymbols[psymtab->statics_offset],
26472 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26475 /* Return number of bytes the .debug_names section will have. This
26476 must be called only after calling the build method. */
26477 size_t bytes () const
26479 /* Verify the build method has been already called. */
26480 gdb_assert (!m_abbrev_table.empty ());
26481 size_t expected_bytes = 0;
26482 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26483 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26484 expected_bytes += m_name_table_string_offs.bytes ();
26485 expected_bytes += m_name_table_entry_offs.bytes ();
26486 expected_bytes += m_abbrev_table.size ();
26487 expected_bytes += m_entry_pool.size ();
26488 return expected_bytes;
26491 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26492 FILE_STR. This must be called only after calling the build
26494 void file_write (FILE *file_names, FILE *file_str) const
26496 /* Verify the build method has been already called. */
26497 gdb_assert (!m_abbrev_table.empty ());
26498 ::file_write (file_names, m_bucket_table);
26499 ::file_write (file_names, m_hash_table);
26500 m_name_table_string_offs.file_write (file_names);
26501 m_name_table_entry_offs.file_write (file_names);
26502 m_abbrev_table.file_write (file_names);
26503 m_entry_pool.file_write (file_names);
26504 m_debugstrlookup.file_write (file_str);
26507 /* A helper user data for write_one_signatured_type. */
26508 class write_one_signatured_type_data
26511 write_one_signatured_type_data (debug_names &nametable_,
26512 signatured_type_index_data &&info_)
26513 : nametable (nametable_), info (std::move (info_))
26515 debug_names &nametable;
26516 struct signatured_type_index_data info;
26519 /* A helper function to pass write_one_signatured_type to
26520 htab_traverse_noresize. */
26522 write_one_signatured_type (void **slot, void *d)
26524 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26525 struct signatured_type_index_data *info = &data->info;
26526 struct signatured_type *entry = (struct signatured_type *) *slot;
26528 data->nametable.write_one_signatured_type (entry, info);
26535 /* Storage for symbol names mapping them to their .debug_str section
26537 class debug_str_lookup
26541 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26542 All .debug_str section strings are automatically stored. */
26543 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26544 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26545 m_dwarf2_per_objfile (dwarf2_per_objfile)
26547 dwarf2_read_section (dwarf2_per_objfile->objfile,
26548 &dwarf2_per_objfile->str);
26549 if (dwarf2_per_objfile->str.buffer == NULL)
26551 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26552 data < (dwarf2_per_objfile->str.buffer
26553 + dwarf2_per_objfile->str.size);)
26555 const char *const s = reinterpret_cast<const char *> (data);
26556 const auto insertpair
26557 = m_str_table.emplace (c_str_view (s),
26558 data - dwarf2_per_objfile->str.buffer);
26559 if (!insertpair.second)
26560 complaint (&symfile_complaints,
26561 _("Duplicate string \"%s\" in "
26562 ".debug_str section [in module %s]"),
26563 s, bfd_get_filename (m_abfd));
26564 data += strlen (s) + 1;
26568 /* Return offset of symbol name S in the .debug_str section. Add
26569 such symbol to the section's end if it does not exist there
26571 size_t lookup (const char *s)
26573 const auto it = m_str_table.find (c_str_view (s));
26574 if (it != m_str_table.end ())
26576 const size_t offset = (m_dwarf2_per_objfile->str.size
26577 + m_str_add_buf.size ());
26578 m_str_table.emplace (c_str_view (s), offset);
26579 m_str_add_buf.append_cstr0 (s);
26583 /* Append the end of the .debug_str section to FILE. */
26584 void file_write (FILE *file) const
26586 m_str_add_buf.file_write (file);
26590 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26592 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26594 /* Data to add at the end of .debug_str for new needed symbol names. */
26595 data_buf m_str_add_buf;
26598 /* Container to map used DWARF tags to their .debug_names abbreviation
26603 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26604 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26609 operator== (const index_key &other) const
26611 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26612 && kind == other.kind);
26615 const int dwarf_tag;
26616 const bool is_static;
26617 const unit_kind kind;
26620 /* Provide std::unordered_map::hasher for index_key. */
26621 class index_key_hasher
26625 operator () (const index_key &key) const
26627 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26631 /* Parameters of one symbol entry. */
26635 const int dwarf_tag, cu_index;
26636 const bool is_static;
26637 const unit_kind kind;
26639 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26641 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26646 operator< (const symbol_value &other) const
26666 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26671 const bfd_endian dwarf5_byte_order;
26673 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26674 : dwarf5_byte_order (dwarf5_byte_order_)
26677 /* Call std::vector::reserve for NELEM elements. */
26678 virtual void reserve (size_t nelem) = 0;
26680 /* Call std::vector::push_back with store_unsigned_integer byte
26681 reordering for ELEM. */
26682 virtual void push_back_reorder (size_t elem) = 0;
26684 /* Return expected output size in bytes. */
26685 virtual size_t bytes () const = 0;
26687 /* Write name table to FILE. */
26688 virtual void file_write (FILE *file) const = 0;
26691 /* Template to unify DWARF-32 and DWARF-64 output. */
26692 template<typename OffsetSize>
26693 class offset_vec_tmpl : public offset_vec
26696 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26697 : offset_vec (dwarf5_byte_order_)
26700 /* Implement offset_vec::reserve. */
26701 void reserve (size_t nelem) override
26703 m_vec.reserve (nelem);
26706 /* Implement offset_vec::push_back_reorder. */
26707 void push_back_reorder (size_t elem) override
26709 m_vec.push_back (elem);
26710 /* Check for overflow. */
26711 gdb_assert (m_vec.back () == elem);
26712 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26713 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26716 /* Implement offset_vec::bytes. */
26717 size_t bytes () const override
26719 return m_vec.size () * sizeof (m_vec[0]);
26722 /* Implement offset_vec::file_write. */
26723 void file_write (FILE *file) const override
26725 ::file_write (file, m_vec);
26729 std::vector<OffsetSize> m_vec;
26732 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26733 respecting name table width. */
26737 offset_vec &name_table_string_offs, &name_table_entry_offs;
26739 dwarf (offset_vec &name_table_string_offs_,
26740 offset_vec &name_table_entry_offs_)
26741 : name_table_string_offs (name_table_string_offs_),
26742 name_table_entry_offs (name_table_entry_offs_)
26747 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26748 respecting name table width. */
26749 template<typename OffsetSize>
26750 class dwarf_tmpl : public dwarf
26753 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26754 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26755 m_name_table_string_offs (dwarf5_byte_order_),
26756 m_name_table_entry_offs (dwarf5_byte_order_)
26760 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26761 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26764 /* Try to reconstruct original DWARF tag for given partial_symbol.
26765 This function is not DWARF-5 compliant but it is sufficient for
26766 GDB as a DWARF-5 index consumer. */
26767 static int psymbol_tag (const struct partial_symbol *psym)
26769 domain_enum domain = PSYMBOL_DOMAIN (psym);
26770 enum address_class aclass = PSYMBOL_CLASS (psym);
26778 return DW_TAG_subprogram;
26780 return DW_TAG_typedef;
26782 case LOC_CONST_BYTES:
26783 case LOC_OPTIMIZED_OUT:
26785 return DW_TAG_variable;
26787 /* Note: It's currently impossible to recognize psyms as enum values
26788 short of reading the type info. For now punt. */
26789 return DW_TAG_variable;
26791 /* There are other LOC_FOO values that one might want to classify
26792 as variables, but dwarf2read.c doesn't currently use them. */
26793 return DW_TAG_variable;
26795 case STRUCT_DOMAIN:
26796 return DW_TAG_structure_type;
26802 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26803 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26804 struct partial_symbol **psymp, int count, int cu_index,
26805 bool is_static, unit_kind kind)
26807 for (; count-- > 0; ++psymp)
26809 struct partial_symbol *psym = *psymp;
26811 if (SYMBOL_LANGUAGE (psym) == language_ada)
26812 error (_("Ada is not currently supported by the index"));
26814 /* Only add a given psymbol once. */
26815 if (psyms_seen.insert (psym).second)
26816 insert (psym, cu_index, is_static, kind);
26820 /* A helper function that writes a single signatured_type
26821 to a debug_names. */
26823 write_one_signatured_type (struct signatured_type *entry,
26824 struct signatured_type_index_data *info)
26826 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26828 write_psymbols (info->psyms_seen,
26829 &info->objfile->global_psymbols[psymtab->globals_offset],
26830 psymtab->n_global_syms, info->cu_index, false,
26832 write_psymbols (info->psyms_seen,
26833 &info->objfile->static_psymbols[psymtab->statics_offset],
26834 psymtab->n_static_syms, info->cu_index, true,
26837 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26838 to_underlying (entry->per_cu.sect_off));
26843 /* Store value of each symbol. */
26844 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26845 m_name_to_value_set;
26847 /* Tables of DWARF-5 .debug_names. They are in object file byte
26849 std::vector<uint32_t> m_bucket_table;
26850 std::vector<uint32_t> m_hash_table;
26852 const bfd_endian m_dwarf5_byte_order;
26853 dwarf_tmpl<uint32_t> m_dwarf32;
26854 dwarf_tmpl<uint64_t> m_dwarf64;
26856 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26857 debug_str_lookup m_debugstrlookup;
26859 /* Map each used .debug_names abbreviation tag parameter to its
26861 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26863 /* Next unused .debug_names abbreviation tag for
26864 m_indexkey_to_idx. */
26865 int m_idx_next = 1;
26867 /* .debug_names abbreviation table. */
26868 data_buf m_abbrev_table;
26870 /* .debug_names entry pool. */
26871 data_buf m_entry_pool;
26874 /* Return iff any of the needed offsets does not fit into 32-bit
26875 .debug_names section. */
26878 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26880 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26882 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26884 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26887 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26889 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26890 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26892 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26898 /* The psyms_seen set is potentially going to be largish (~40k
26899 elements when indexing a -g3 build of GDB itself). Estimate the
26900 number of elements in order to avoid too many rehashes, which
26901 require rebuilding buckets and thus many trips to
26905 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26907 size_t psyms_count = 0;
26908 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26910 struct dwarf2_per_cu_data *per_cu
26911 = dwarf2_per_objfile->all_comp_units[i];
26912 struct partial_symtab *psymtab = per_cu->v.psymtab;
26914 if (psymtab != NULL && psymtab->user == NULL)
26915 recursively_count_psymbols (psymtab, psyms_count);
26917 /* Generating an index for gdb itself shows a ratio of
26918 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26919 return psyms_count / 4;
26922 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26923 Return how many bytes were expected to be written into OUT_FILE. */
26926 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26928 struct objfile *objfile = dwarf2_per_objfile->objfile;
26929 mapped_symtab symtab;
26932 /* While we're scanning CU's create a table that maps a psymtab pointer
26933 (which is what addrmap records) to its index (which is what is recorded
26934 in the index file). This will later be needed to write the address
26936 psym_index_map cu_index_htab;
26937 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26939 /* The CU list is already sorted, so we don't need to do additional
26940 work here. Also, the debug_types entries do not appear in
26941 all_comp_units, but only in their own hash table. */
26943 std::unordered_set<partial_symbol *> psyms_seen
26944 (psyms_seen_size (dwarf2_per_objfile));
26945 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26947 struct dwarf2_per_cu_data *per_cu
26948 = dwarf2_per_objfile->all_comp_units[i];
26949 struct partial_symtab *psymtab = per_cu->v.psymtab;
26951 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26952 It may be referenced from a local scope but in such case it does not
26953 need to be present in .gdb_index. */
26954 if (psymtab == NULL)
26957 if (psymtab->user == NULL)
26958 recursively_write_psymbols (objfile, psymtab, &symtab,
26961 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26962 gdb_assert (insertpair.second);
26964 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26965 to_underlying (per_cu->sect_off));
26966 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26969 /* Dump the address map. */
26971 write_address_map (objfile, addr_vec, cu_index_htab);
26973 /* Write out the .debug_type entries, if any. */
26974 data_buf types_cu_list;
26975 if (dwarf2_per_objfile->signatured_types)
26977 signatured_type_index_data sig_data (types_cu_list,
26980 sig_data.objfile = objfile;
26981 sig_data.symtab = &symtab;
26982 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26983 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26984 write_one_signatured_type, &sig_data);
26987 /* Now that we've processed all symbols we can shrink their cu_indices
26989 uniquify_cu_indices (&symtab);
26991 data_buf symtab_vec, constant_pool;
26992 write_hash_table (&symtab, symtab_vec, constant_pool);
26995 const offset_type size_of_contents = 6 * sizeof (offset_type);
26996 offset_type total_len = size_of_contents;
26998 /* The version number. */
26999 contents.append_data (MAYBE_SWAP (8));
27001 /* The offset of the CU list from the start of the file. */
27002 contents.append_data (MAYBE_SWAP (total_len));
27003 total_len += cu_list.size ();
27005 /* The offset of the types CU list from the start of the file. */
27006 contents.append_data (MAYBE_SWAP (total_len));
27007 total_len += types_cu_list.size ();
27009 /* The offset of the address table from the start of the file. */
27010 contents.append_data (MAYBE_SWAP (total_len));
27011 total_len += addr_vec.size ();
27013 /* The offset of the symbol table from the start of the file. */
27014 contents.append_data (MAYBE_SWAP (total_len));
27015 total_len += symtab_vec.size ();
27017 /* The offset of the constant pool from the start of the file. */
27018 contents.append_data (MAYBE_SWAP (total_len));
27019 total_len += constant_pool.size ();
27021 gdb_assert (contents.size () == size_of_contents);
27023 contents.file_write (out_file);
27024 cu_list.file_write (out_file);
27025 types_cu_list.file_write (out_file);
27026 addr_vec.file_write (out_file);
27027 symtab_vec.file_write (out_file);
27028 constant_pool.file_write (out_file);
27033 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27034 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27036 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27037 needed addition to .debug_str section to OUT_FILE_STR. Return how
27038 many bytes were expected to be written into OUT_FILE. */
27041 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27042 FILE *out_file, FILE *out_file_str)
27044 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27045 struct objfile *objfile = dwarf2_per_objfile->objfile;
27046 const enum bfd_endian dwarf5_byte_order
27047 = gdbarch_byte_order (get_objfile_arch (objfile));
27049 /* The CU list is already sorted, so we don't need to do additional
27050 work here. Also, the debug_types entries do not appear in
27051 all_comp_units, but only in their own hash table. */
27053 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27054 dwarf5_byte_order);
27055 std::unordered_set<partial_symbol *>
27056 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27057 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27059 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27060 partial_symtab *psymtab = per_cu->v.psymtab;
27062 /* CU of a shared file from 'dwz -m' may be unused by this main
27063 file. It may be referenced from a local scope but in such
27064 case it does not need to be present in .debug_names. */
27065 if (psymtab == NULL)
27068 if (psymtab->user == NULL)
27069 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27071 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27072 to_underlying (per_cu->sect_off));
27075 /* Write out the .debug_type entries, if any. */
27076 data_buf types_cu_list;
27077 if (dwarf2_per_objfile->signatured_types)
27079 debug_names::write_one_signatured_type_data sig_data (nametable,
27080 signatured_type_index_data (types_cu_list, psyms_seen));
27082 sig_data.info.objfile = objfile;
27083 /* It is used only for gdb_index. */
27084 sig_data.info.symtab = nullptr;
27085 sig_data.info.cu_index = 0;
27086 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27087 debug_names::write_one_signatured_type,
27091 nametable.build ();
27093 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27095 const offset_type bytes_of_header
27096 = ((dwarf5_is_dwarf64 ? 12 : 4)
27098 + sizeof (dwarf5_gdb_augmentation));
27099 size_t expected_bytes = 0;
27100 expected_bytes += bytes_of_header;
27101 expected_bytes += cu_list.size ();
27102 expected_bytes += types_cu_list.size ();
27103 expected_bytes += nametable.bytes ();
27106 if (!dwarf5_is_dwarf64)
27108 const uint64_t size64 = expected_bytes - 4;
27109 gdb_assert (size64 < 0xfffffff0);
27110 header.append_uint (4, dwarf5_byte_order, size64);
27114 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27115 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27118 /* The version number. */
27119 header.append_uint (2, dwarf5_byte_order, 5);
27122 header.append_uint (2, dwarf5_byte_order, 0);
27124 /* comp_unit_count - The number of CUs in the CU list. */
27125 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27127 /* local_type_unit_count - The number of TUs in the local TU
27129 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27131 /* foreign_type_unit_count - The number of TUs in the foreign TU
27133 header.append_uint (4, dwarf5_byte_order, 0);
27135 /* bucket_count - The number of hash buckets in the hash lookup
27137 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27139 /* name_count - The number of unique names in the index. */
27140 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27142 /* abbrev_table_size - The size in bytes of the abbreviations
27144 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27146 /* augmentation_string_size - The size in bytes of the augmentation
27147 string. This value is rounded up to a multiple of 4. */
27148 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27149 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27150 header.append_data (dwarf5_gdb_augmentation);
27152 gdb_assert (header.size () == bytes_of_header);
27154 header.file_write (out_file);
27155 cu_list.file_write (out_file);
27156 types_cu_list.file_write (out_file);
27157 nametable.file_write (out_file, out_file_str);
27159 return expected_bytes;
27162 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27163 position is at the end of the file. */
27166 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27168 const auto file_size = ftell (file);
27169 if (file_size == -1)
27170 error (_("Can't get `%s' size"), filename);
27171 gdb_assert (file_size == expected_size);
27174 /* Create an index file for OBJFILE in the directory DIR. */
27177 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27179 dw_index_kind index_kind)
27181 struct objfile *objfile = dwarf2_per_objfile->objfile;
27183 if (dwarf2_per_objfile->using_index)
27184 error (_("Cannot use an index to create the index"));
27186 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27187 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27189 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27193 if (stat (objfile_name (objfile), &st) < 0)
27194 perror_with_name (objfile_name (objfile));
27196 std::string filename (std::string (dir) + SLASH_STRING
27197 + lbasename (objfile_name (objfile))
27198 + (index_kind == dw_index_kind::DEBUG_NAMES
27199 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27201 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27203 error (_("Can't open `%s' for writing"), filename.c_str ());
27205 /* Order matters here; we want FILE to be closed before FILENAME is
27206 unlinked, because on MS-Windows one cannot delete a file that is
27207 still open. (Don't call anything here that might throw until
27208 file_closer is created.) */
27209 gdb::unlinker unlink_file (filename.c_str ());
27210 gdb_file_up close_out_file (out_file);
27212 if (index_kind == dw_index_kind::DEBUG_NAMES)
27214 std::string filename_str (std::string (dir) + SLASH_STRING
27215 + lbasename (objfile_name (objfile))
27216 + DEBUG_STR_SUFFIX);
27218 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27220 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27221 gdb::unlinker unlink_file_str (filename_str.c_str ());
27222 gdb_file_up close_out_file_str (out_file_str);
27224 const size_t total_len
27225 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27226 assert_file_size (out_file, filename.c_str (), total_len);
27228 /* We want to keep the file .debug_str file too. */
27229 unlink_file_str.keep ();
27233 const size_t total_len
27234 = write_gdbindex (dwarf2_per_objfile, out_file);
27235 assert_file_size (out_file, filename.c_str (), total_len);
27238 /* We want to keep the file. */
27239 unlink_file.keep ();
27242 /* Implementation of the `save gdb-index' command.
27244 Note that the .gdb_index file format used by this command is
27245 documented in the GDB manual. Any changes here must be documented
27249 save_gdb_index_command (const char *arg, int from_tty)
27251 struct objfile *objfile;
27252 const char dwarf5space[] = "-dwarf-5 ";
27253 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27258 arg = skip_spaces (arg);
27259 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27261 index_kind = dw_index_kind::DEBUG_NAMES;
27262 arg += strlen (dwarf5space);
27263 arg = skip_spaces (arg);
27267 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27269 ALL_OBJFILES (objfile)
27273 /* If the objfile does not correspond to an actual file, skip it. */
27274 if (stat (objfile_name (objfile), &st) < 0)
27277 struct dwarf2_per_objfile *dwarf2_per_objfile
27278 = get_dwarf2_per_objfile (objfile);
27280 if (dwarf2_per_objfile != NULL)
27284 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27286 CATCH (except, RETURN_MASK_ERROR)
27288 exception_fprintf (gdb_stderr, except,
27289 _("Error while writing index for `%s': "),
27290 objfile_name (objfile));
27300 int dwarf_always_disassemble;
27303 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27304 struct cmd_list_element *c, const char *value)
27306 fprintf_filtered (file,
27307 _("Whether to always disassemble "
27308 "DWARF expressions is %s.\n"),
27313 show_check_physname (struct ui_file *file, int from_tty,
27314 struct cmd_list_element *c, const char *value)
27316 fprintf_filtered (file,
27317 _("Whether to check \"physname\" is %s.\n"),
27322 _initialize_dwarf2_read (void)
27324 struct cmd_list_element *c;
27326 dwarf2_objfile_data_key
27327 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
27329 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27330 Set DWARF specific variables.\n\
27331 Configure DWARF variables such as the cache size"),
27332 &set_dwarf_cmdlist, "maintenance set dwarf ",
27333 0/*allow-unknown*/, &maintenance_set_cmdlist);
27335 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27336 Show DWARF specific variables\n\
27337 Show DWARF variables such as the cache size"),
27338 &show_dwarf_cmdlist, "maintenance show dwarf ",
27339 0/*allow-unknown*/, &maintenance_show_cmdlist);
27341 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27342 &dwarf_max_cache_age, _("\
27343 Set the upper bound on the age of cached DWARF compilation units."), _("\
27344 Show the upper bound on the age of cached DWARF compilation units."), _("\
27345 A higher limit means that cached compilation units will be stored\n\
27346 in memory longer, and more total memory will be used. Zero disables\n\
27347 caching, which can slow down startup."),
27349 show_dwarf_max_cache_age,
27350 &set_dwarf_cmdlist,
27351 &show_dwarf_cmdlist);
27353 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27354 &dwarf_always_disassemble, _("\
27355 Set whether `info address' always disassembles DWARF expressions."), _("\
27356 Show whether `info address' always disassembles DWARF expressions."), _("\
27357 When enabled, DWARF expressions are always printed in an assembly-like\n\
27358 syntax. When disabled, expressions will be printed in a more\n\
27359 conversational style, when possible."),
27361 show_dwarf_always_disassemble,
27362 &set_dwarf_cmdlist,
27363 &show_dwarf_cmdlist);
27365 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27366 Set debugging of the DWARF reader."), _("\
27367 Show debugging of the DWARF reader."), _("\
27368 When enabled (non-zero), debugging messages are printed during DWARF\n\
27369 reading and symtab expansion. A value of 1 (one) provides basic\n\
27370 information. A value greater than 1 provides more verbose information."),
27373 &setdebuglist, &showdebuglist);
27375 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27376 Set debugging of the DWARF DIE reader."), _("\
27377 Show debugging of the DWARF DIE reader."), _("\
27378 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27379 The value is the maximum depth to print."),
27382 &setdebuglist, &showdebuglist);
27384 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27385 Set debugging of the dwarf line reader."), _("\
27386 Show debugging of the dwarf line reader."), _("\
27387 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27388 A value of 1 (one) provides basic information.\n\
27389 A value greater than 1 provides more verbose information."),
27392 &setdebuglist, &showdebuglist);
27394 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27395 Set cross-checking of \"physname\" code against demangler."), _("\
27396 Show cross-checking of \"physname\" code against demangler."), _("\
27397 When enabled, GDB's internal \"physname\" code is checked against\n\
27399 NULL, show_check_physname,
27400 &setdebuglist, &showdebuglist);
27402 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27403 no_class, &use_deprecated_index_sections, _("\
27404 Set whether to use deprecated gdb_index sections."), _("\
27405 Show whether to use deprecated gdb_index sections."), _("\
27406 When enabled, deprecated .gdb_index sections are used anyway.\n\
27407 Normally they are ignored either because of a missing feature or\n\
27408 performance issue.\n\
27409 Warning: This option must be enabled before gdb reads the file."),
27412 &setlist, &showlist);
27414 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27416 Save a gdb-index file.\n\
27417 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27419 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27420 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27421 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27423 set_cmd_completer (c, filename_completer);
27425 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27426 &dwarf2_locexpr_funcs);
27427 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27428 &dwarf2_loclist_funcs);
27430 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27431 &dwarf2_block_frame_base_locexpr_funcs);
27432 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27433 &dwarf2_block_frame_base_loclist_funcs);
27436 selftests::register_test ("dw2_expand_symtabs_matching",
27437 selftests::dw2_expand_symtabs_matching::run_test);