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 dwarf2_per_objfile containing this compilation unit. */
665 struct dwarf2_per_objfile *dwarf2_per_objfile;
667 /* The header of the compilation unit. */
668 struct comp_unit_head header;
670 /* Base address of this compilation unit. */
671 CORE_ADDR base_address;
673 /* Non-zero if base_address has been set. */
676 /* The language we are debugging. */
677 enum language language;
678 const struct language_defn *language_defn;
680 const char *producer;
682 /* The generic symbol table building routines have separate lists for
683 file scope symbols and all all other scopes (local scopes). So
684 we need to select the right one to pass to add_symbol_to_list().
685 We do it by keeping a pointer to the correct list in list_in_scope.
687 FIXME: The original dwarf code just treated the file scope as the
688 first local scope, and all other local scopes as nested local
689 scopes, and worked fine. Check to see if we really need to
690 distinguish these in buildsym.c. */
691 struct pending **list_in_scope;
693 /* The abbrev table for this CU.
694 Normally this points to the abbrev table in the objfile.
695 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
696 struct abbrev_table *abbrev_table;
698 /* Hash table holding all the loaded partial DIEs
699 with partial_die->offset.SECT_OFF as hash. */
702 /* Storage for things with the same lifetime as this read-in compilation
703 unit, including partial DIEs. */
704 struct obstack comp_unit_obstack;
706 /* When multiple dwarf2_cu structures are living in memory, this field
707 chains them all together, so that they can be released efficiently.
708 We will probably also want a generation counter so that most-recently-used
709 compilation units are cached... */
710 struct dwarf2_per_cu_data *read_in_chain;
712 /* Backlink to our per_cu entry. */
713 struct dwarf2_per_cu_data *per_cu;
715 /* How many compilation units ago was this CU last referenced? */
718 /* A hash table of DIE cu_offset for following references with
719 die_info->offset.sect_off as hash. */
722 /* Full DIEs if read in. */
723 struct die_info *dies;
725 /* A set of pointers to dwarf2_per_cu_data objects for compilation
726 units referenced by this one. Only set during full symbol processing;
727 partial symbol tables do not have dependencies. */
730 /* Header data from the line table, during full symbol processing. */
731 struct line_header *line_header;
732 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
733 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
734 this is the DW_TAG_compile_unit die for this CU. We'll hold on
735 to the line header as long as this DIE is being processed. See
736 process_die_scope. */
737 die_info *line_header_die_owner;
739 /* A list of methods which need to have physnames computed
740 after all type information has been read. */
741 VEC (delayed_method_info) *method_list;
743 /* To be copied to symtab->call_site_htab. */
744 htab_t call_site_htab;
746 /* Non-NULL if this CU came from a DWO file.
747 There is an invariant here that is important to remember:
748 Except for attributes copied from the top level DIE in the "main"
749 (or "stub") file in preparation for reading the DWO file
750 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
751 Either there isn't a DWO file (in which case this is NULL and the point
752 is moot), or there is and either we're not going to read it (in which
753 case this is NULL) or there is and we are reading it (in which case this
755 struct dwo_unit *dwo_unit;
757 /* The DW_AT_addr_base attribute if present, zero otherwise
758 (zero is a valid value though).
759 Note this value comes from the Fission stub CU/TU's DIE. */
762 /* The DW_AT_ranges_base attribute if present, zero otherwise
763 (zero is a valid value though).
764 Note this value comes from the Fission stub CU/TU's DIE.
765 Also note that the value is zero in the non-DWO case so this value can
766 be used without needing to know whether DWO files are in use or not.
767 N.B. This does not apply to DW_AT_ranges appearing in
768 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
769 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
770 DW_AT_ranges_base *would* have to be applied, and we'd have to care
771 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
772 ULONGEST ranges_base;
774 /* Mark used when releasing cached dies. */
775 unsigned int mark : 1;
777 /* This CU references .debug_loc. See the symtab->locations_valid field.
778 This test is imperfect as there may exist optimized debug code not using
779 any location list and still facing inlining issues if handled as
780 unoptimized code. For a future better test see GCC PR other/32998. */
781 unsigned int has_loclist : 1;
783 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
784 if all the producer_is_* fields are valid. This information is cached
785 because profiling CU expansion showed excessive time spent in
786 producer_is_gxx_lt_4_6. */
787 unsigned int checked_producer : 1;
788 unsigned int producer_is_gxx_lt_4_6 : 1;
789 unsigned int producer_is_gcc_lt_4_3 : 1;
790 unsigned int producer_is_icc_lt_14 : 1;
792 /* When set, the file that we're processing is known to have
793 debugging info for C++ namespaces. GCC 3.3.x did not produce
794 this information, but later versions do. */
796 unsigned int processing_has_namespace_info : 1;
799 /* Persistent data held for a compilation unit, even when not
800 processing it. We put a pointer to this structure in the
801 read_symtab_private field of the psymtab. */
803 struct dwarf2_per_cu_data
805 /* The start offset and length of this compilation unit.
806 NOTE: Unlike comp_unit_head.length, this length includes
808 If the DIE refers to a DWO file, this is always of the original die,
810 sect_offset sect_off;
813 /* DWARF standard version this data has been read from (such as 4 or 5). */
816 /* Flag indicating this compilation unit will be read in before
817 any of the current compilation units are processed. */
818 unsigned int queued : 1;
820 /* This flag will be set when reading partial DIEs if we need to load
821 absolutely all DIEs for this compilation unit, instead of just the ones
822 we think are interesting. It gets set if we look for a DIE in the
823 hash table and don't find it. */
824 unsigned int load_all_dies : 1;
826 /* Non-zero if this CU is from .debug_types.
827 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
829 unsigned int is_debug_types : 1;
831 /* Non-zero if this CU is from the .dwz file. */
832 unsigned int is_dwz : 1;
834 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
835 This flag is only valid if is_debug_types is true.
836 We can't read a CU directly from a DWO file: There are required
837 attributes in the stub. */
838 unsigned int reading_dwo_directly : 1;
840 /* Non-zero if the TU has been read.
841 This is used to assist the "Stay in DWO Optimization" for Fission:
842 When reading a DWO, it's faster to read TUs from the DWO instead of
843 fetching them from random other DWOs (due to comdat folding).
844 If the TU has already been read, the optimization is unnecessary
845 (and unwise - we don't want to change where gdb thinks the TU lives
847 This flag is only valid if is_debug_types is true. */
848 unsigned int tu_read : 1;
850 /* The section this CU/TU lives in.
851 If the DIE refers to a DWO file, this is always the original die,
853 struct dwarf2_section_info *section;
855 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
856 of the CU cache it gets reset to NULL again. This is left as NULL for
857 dummy CUs (a CU header, but nothing else). */
858 struct dwarf2_cu *cu;
860 /* The corresponding dwarf2_per_objfile. */
861 struct dwarf2_per_objfile *dwarf2_per_objfile;
863 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
864 is active. Otherwise, the 'psymtab' field is active. */
867 /* The partial symbol table associated with this compilation unit,
868 or NULL for unread partial units. */
869 struct partial_symtab *psymtab;
871 /* Data needed by the "quick" functions. */
872 struct dwarf2_per_cu_quick_data *quick;
875 /* The CUs we import using DW_TAG_imported_unit. This is filled in
876 while reading psymtabs, used to compute the psymtab dependencies,
877 and then cleared. Then it is filled in again while reading full
878 symbols, and only deleted when the objfile is destroyed.
880 This is also used to work around a difference between the way gold
881 generates .gdb_index version <=7 and the way gdb does. Arguably this
882 is a gold bug. For symbols coming from TUs, gold records in the index
883 the CU that includes the TU instead of the TU itself. This breaks
884 dw2_lookup_symbol: It assumes that if the index says symbol X lives
885 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
886 will find X. Alas TUs live in their own symtab, so after expanding CU Y
887 we need to look in TU Z to find X. Fortunately, this is akin to
888 DW_TAG_imported_unit, so we just use the same mechanism: For
889 .gdb_index version <=7 this also records the TUs that the CU referred
890 to. Concurrently with this change gdb was modified to emit version 8
891 indices so we only pay a price for gold generated indices.
892 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
893 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
896 /* Entry in the signatured_types hash table. */
898 struct signatured_type
900 /* The "per_cu" object of this type.
901 This struct is used iff per_cu.is_debug_types.
902 N.B.: This is the first member so that it's easy to convert pointers
904 struct dwarf2_per_cu_data per_cu;
906 /* The type's signature. */
909 /* Offset in the TU of the type's DIE, as read from the TU header.
910 If this TU is a DWO stub and the definition lives in a DWO file
911 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
912 cu_offset type_offset_in_tu;
914 /* Offset in the section of the type's DIE.
915 If the definition lives in a DWO file, this is the offset in the
916 .debug_types.dwo section.
917 The value is zero until the actual value is known.
918 Zero is otherwise not a valid section offset. */
919 sect_offset type_offset_in_section;
921 /* Type units are grouped by their DW_AT_stmt_list entry so that they
922 can share them. This points to the containing symtab. */
923 struct type_unit_group *type_unit_group;
926 The first time we encounter this type we fully read it in and install it
927 in the symbol tables. Subsequent times we only need the type. */
930 /* Containing DWO unit.
931 This field is valid iff per_cu.reading_dwo_directly. */
932 struct dwo_unit *dwo_unit;
935 typedef struct signatured_type *sig_type_ptr;
936 DEF_VEC_P (sig_type_ptr);
938 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
939 This includes type_unit_group and quick_file_names. */
941 struct stmt_list_hash
943 /* The DWO unit this table is from or NULL if there is none. */
944 struct dwo_unit *dwo_unit;
946 /* Offset in .debug_line or .debug_line.dwo. */
947 sect_offset line_sect_off;
950 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
951 an object of this type. */
953 struct type_unit_group
955 /* dwarf2read.c's main "handle" on a TU symtab.
956 To simplify things we create an artificial CU that "includes" all the
957 type units using this stmt_list so that the rest of the code still has
958 a "per_cu" handle on the symtab.
959 This PER_CU is recognized by having no section. */
960 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
961 struct dwarf2_per_cu_data per_cu;
963 /* The TUs that share this DW_AT_stmt_list entry.
964 This is added to while parsing type units to build partial symtabs,
965 and is deleted afterwards and not used again. */
966 VEC (sig_type_ptr) *tus;
968 /* The compunit symtab.
969 Type units in a group needn't all be defined in the same source file,
970 so we create an essentially anonymous symtab as the compunit symtab. */
971 struct compunit_symtab *compunit_symtab;
973 /* The data used to construct the hash key. */
974 struct stmt_list_hash hash;
976 /* The number of symtabs from the line header.
977 The value here must match line_header.num_file_names. */
978 unsigned int num_symtabs;
980 /* The symbol tables for this TU (obtained from the files listed in
982 WARNING: The order of entries here must match the order of entries
983 in the line header. After the first TU using this type_unit_group, the
984 line header for the subsequent TUs is recreated from this. This is done
985 because we need to use the same symtabs for each TU using the same
986 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
987 there's no guarantee the line header doesn't have duplicate entries. */
988 struct symtab **symtabs;
991 /* These sections are what may appear in a (real or virtual) DWO file. */
995 struct dwarf2_section_info abbrev;
996 struct dwarf2_section_info line;
997 struct dwarf2_section_info loc;
998 struct dwarf2_section_info loclists;
999 struct dwarf2_section_info macinfo;
1000 struct dwarf2_section_info macro;
1001 struct dwarf2_section_info str;
1002 struct dwarf2_section_info str_offsets;
1003 /* In the case of a virtual DWO file, these two are unused. */
1004 struct dwarf2_section_info info;
1005 VEC (dwarf2_section_info_def) *types;
1008 /* CUs/TUs in DWP/DWO files. */
1012 /* Backlink to the containing struct dwo_file. */
1013 struct dwo_file *dwo_file;
1015 /* The "id" that distinguishes this CU/TU.
1016 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1017 Since signatures came first, we stick with it for consistency. */
1020 /* The section this CU/TU lives in, in the DWO file. */
1021 struct dwarf2_section_info *section;
1023 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1024 sect_offset sect_off;
1025 unsigned int length;
1027 /* For types, offset in the type's DIE of the type defined by this TU. */
1028 cu_offset type_offset_in_tu;
1031 /* include/dwarf2.h defines the DWP section codes.
1032 It defines a max value but it doesn't define a min value, which we
1033 use for error checking, so provide one. */
1035 enum dwp_v2_section_ids
1040 /* Data for one DWO file.
1042 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1043 appears in a DWP file). DWP files don't really have DWO files per se -
1044 comdat folding of types "loses" the DWO file they came from, and from
1045 a high level view DWP files appear to contain a mass of random types.
1046 However, to maintain consistency with the non-DWP case we pretend DWP
1047 files contain virtual DWO files, and we assign each TU with one virtual
1048 DWO file (generally based on the line and abbrev section offsets -
1049 a heuristic that seems to work in practice). */
1053 /* The DW_AT_GNU_dwo_name attribute.
1054 For virtual DWO files the name is constructed from the section offsets
1055 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1056 from related CU+TUs. */
1057 const char *dwo_name;
1059 /* The DW_AT_comp_dir attribute. */
1060 const char *comp_dir;
1062 /* The bfd, when the file is open. Otherwise this is NULL.
1063 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1066 /* The sections that make up this DWO file.
1067 Remember that for virtual DWO files in DWP V2, these are virtual
1068 sections (for lack of a better name). */
1069 struct dwo_sections sections;
1071 /* The CUs in the file.
1072 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1073 an extension to handle LLVM's Link Time Optimization output (where
1074 multiple source files may be compiled into a single object/dwo pair). */
1077 /* Table of TUs in the file.
1078 Each element is a struct dwo_unit. */
1082 /* These sections are what may appear in a DWP file. */
1086 /* These are used by both DWP version 1 and 2. */
1087 struct dwarf2_section_info str;
1088 struct dwarf2_section_info cu_index;
1089 struct dwarf2_section_info tu_index;
1091 /* These are only used by DWP version 2 files.
1092 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1093 sections are referenced by section number, and are not recorded here.
1094 In DWP version 2 there is at most one copy of all these sections, each
1095 section being (effectively) comprised of the concatenation of all of the
1096 individual sections that exist in the version 1 format.
1097 To keep the code simple we treat each of these concatenated pieces as a
1098 section itself (a virtual section?). */
1099 struct dwarf2_section_info abbrev;
1100 struct dwarf2_section_info info;
1101 struct dwarf2_section_info line;
1102 struct dwarf2_section_info loc;
1103 struct dwarf2_section_info macinfo;
1104 struct dwarf2_section_info macro;
1105 struct dwarf2_section_info str_offsets;
1106 struct dwarf2_section_info types;
1109 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1110 A virtual DWO file is a DWO file as it appears in a DWP file. */
1112 struct virtual_v1_dwo_sections
1114 struct dwarf2_section_info abbrev;
1115 struct dwarf2_section_info line;
1116 struct dwarf2_section_info loc;
1117 struct dwarf2_section_info macinfo;
1118 struct dwarf2_section_info macro;
1119 struct dwarf2_section_info str_offsets;
1120 /* Each DWP hash table entry records one CU or one TU.
1121 That is recorded here, and copied to dwo_unit.section. */
1122 struct dwarf2_section_info info_or_types;
1125 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1126 In version 2, the sections of the DWO files are concatenated together
1127 and stored in one section of that name. Thus each ELF section contains
1128 several "virtual" sections. */
1130 struct virtual_v2_dwo_sections
1132 bfd_size_type abbrev_offset;
1133 bfd_size_type abbrev_size;
1135 bfd_size_type line_offset;
1136 bfd_size_type line_size;
1138 bfd_size_type loc_offset;
1139 bfd_size_type loc_size;
1141 bfd_size_type macinfo_offset;
1142 bfd_size_type macinfo_size;
1144 bfd_size_type macro_offset;
1145 bfd_size_type macro_size;
1147 bfd_size_type str_offsets_offset;
1148 bfd_size_type str_offsets_size;
1150 /* Each DWP hash table entry records one CU or one TU.
1151 That is recorded here, and copied to dwo_unit.section. */
1152 bfd_size_type info_or_types_offset;
1153 bfd_size_type info_or_types_size;
1156 /* Contents of DWP hash tables. */
1158 struct dwp_hash_table
1160 uint32_t version, nr_columns;
1161 uint32_t nr_units, nr_slots;
1162 const gdb_byte *hash_table, *unit_table;
1167 const gdb_byte *indices;
1171 /* This is indexed by column number and gives the id of the section
1173 #define MAX_NR_V2_DWO_SECTIONS \
1174 (1 /* .debug_info or .debug_types */ \
1175 + 1 /* .debug_abbrev */ \
1176 + 1 /* .debug_line */ \
1177 + 1 /* .debug_loc */ \
1178 + 1 /* .debug_str_offsets */ \
1179 + 1 /* .debug_macro or .debug_macinfo */)
1180 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1181 const gdb_byte *offsets;
1182 const gdb_byte *sizes;
1187 /* Data for one DWP file. */
1191 /* Name of the file. */
1194 /* File format version. */
1200 /* Section info for this file. */
1201 struct dwp_sections sections;
1203 /* Table of CUs in the file. */
1204 const struct dwp_hash_table *cus;
1206 /* Table of TUs in the file. */
1207 const struct dwp_hash_table *tus;
1209 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1213 /* Table to map ELF section numbers to their sections.
1214 This is only needed for the DWP V1 file format. */
1215 unsigned int num_sections;
1216 asection **elf_sections;
1219 /* This represents a '.dwz' file. */
1223 /* A dwz file can only contain a few sections. */
1224 struct dwarf2_section_info abbrev;
1225 struct dwarf2_section_info info;
1226 struct dwarf2_section_info str;
1227 struct dwarf2_section_info line;
1228 struct dwarf2_section_info macro;
1229 struct dwarf2_section_info gdb_index;
1230 struct dwarf2_section_info debug_names;
1232 /* The dwz's BFD. */
1236 /* Struct used to pass misc. parameters to read_die_and_children, et
1237 al. which are used for both .debug_info and .debug_types dies.
1238 All parameters here are unchanging for the life of the call. This
1239 struct exists to abstract away the constant parameters of die reading. */
1241 struct die_reader_specs
1243 /* The bfd of die_section. */
1246 /* The CU of the DIE we are parsing. */
1247 struct dwarf2_cu *cu;
1249 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1250 struct dwo_file *dwo_file;
1252 /* The section the die comes from.
1253 This is either .debug_info or .debug_types, or the .dwo variants. */
1254 struct dwarf2_section_info *die_section;
1256 /* die_section->buffer. */
1257 const gdb_byte *buffer;
1259 /* The end of the buffer. */
1260 const gdb_byte *buffer_end;
1262 /* The value of the DW_AT_comp_dir attribute. */
1263 const char *comp_dir;
1266 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1267 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1268 const gdb_byte *info_ptr,
1269 struct die_info *comp_unit_die,
1273 /* A 1-based directory index. This is a strong typedef to prevent
1274 accidentally using a directory index as a 0-based index into an
1276 enum class dir_index : unsigned int {};
1278 /* Likewise, a 1-based file name index. */
1279 enum class file_name_index : unsigned int {};
1283 file_entry () = default;
1285 file_entry (const char *name_, dir_index d_index_,
1286 unsigned int mod_time_, unsigned int length_)
1289 mod_time (mod_time_),
1293 /* Return the include directory at D_INDEX stored in LH. Returns
1294 NULL if D_INDEX is out of bounds. */
1295 const char *include_dir (const line_header *lh) const;
1297 /* The file name. Note this is an observing pointer. The memory is
1298 owned by debug_line_buffer. */
1299 const char *name {};
1301 /* The directory index (1-based). */
1302 dir_index d_index {};
1304 unsigned int mod_time {};
1306 unsigned int length {};
1308 /* True if referenced by the Line Number Program. */
1311 /* The associated symbol table, if any. */
1312 struct symtab *symtab {};
1315 /* The line number information for a compilation unit (found in the
1316 .debug_line section) begins with a "statement program header",
1317 which contains the following information. */
1324 /* Add an entry to the include directory table. */
1325 void add_include_dir (const char *include_dir);
1327 /* Add an entry to the file name table. */
1328 void add_file_name (const char *name, dir_index d_index,
1329 unsigned int mod_time, unsigned int length);
1331 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1332 is out of bounds. */
1333 const char *include_dir_at (dir_index index) const
1335 /* Convert directory index number (1-based) to vector index
1337 size_t vec_index = to_underlying (index) - 1;
1339 if (vec_index >= include_dirs.size ())
1341 return include_dirs[vec_index];
1344 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1345 is out of bounds. */
1346 file_entry *file_name_at (file_name_index index)
1348 /* Convert file name index number (1-based) to vector index
1350 size_t vec_index = to_underlying (index) - 1;
1352 if (vec_index >= file_names.size ())
1354 return &file_names[vec_index];
1357 /* Const version of the above. */
1358 const file_entry *file_name_at (unsigned int index) const
1360 if (index >= file_names.size ())
1362 return &file_names[index];
1365 /* Offset of line number information in .debug_line section. */
1366 sect_offset sect_off {};
1368 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1369 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1371 unsigned int total_length {};
1372 unsigned short version {};
1373 unsigned int header_length {};
1374 unsigned char minimum_instruction_length {};
1375 unsigned char maximum_ops_per_instruction {};
1376 unsigned char default_is_stmt {};
1378 unsigned char line_range {};
1379 unsigned char opcode_base {};
1381 /* standard_opcode_lengths[i] is the number of operands for the
1382 standard opcode whose value is i. This means that
1383 standard_opcode_lengths[0] is unused, and the last meaningful
1384 element is standard_opcode_lengths[opcode_base - 1]. */
1385 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1387 /* The include_directories table. Note these are observing
1388 pointers. The memory is owned by debug_line_buffer. */
1389 std::vector<const char *> include_dirs;
1391 /* The file_names table. */
1392 std::vector<file_entry> file_names;
1394 /* The start and end of the statement program following this
1395 header. These point into dwarf2_per_objfile->line_buffer. */
1396 const gdb_byte *statement_program_start {}, *statement_program_end {};
1399 typedef std::unique_ptr<line_header> line_header_up;
1402 file_entry::include_dir (const line_header *lh) const
1404 return lh->include_dir_at (d_index);
1407 /* When we construct a partial symbol table entry we only
1408 need this much information. */
1409 struct partial_die_info
1411 /* Offset of this DIE. */
1412 sect_offset sect_off;
1414 /* DWARF-2 tag for this DIE. */
1415 ENUM_BITFIELD(dwarf_tag) tag : 16;
1417 /* Assorted flags describing the data found in this DIE. */
1418 unsigned int has_children : 1;
1419 unsigned int is_external : 1;
1420 unsigned int is_declaration : 1;
1421 unsigned int has_type : 1;
1422 unsigned int has_specification : 1;
1423 unsigned int has_pc_info : 1;
1424 unsigned int may_be_inlined : 1;
1426 /* This DIE has been marked DW_AT_main_subprogram. */
1427 unsigned int main_subprogram : 1;
1429 /* Flag set if the SCOPE field of this structure has been
1431 unsigned int scope_set : 1;
1433 /* Flag set if the DIE has a byte_size attribute. */
1434 unsigned int has_byte_size : 1;
1436 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1437 unsigned int has_const_value : 1;
1439 /* Flag set if any of the DIE's children are template arguments. */
1440 unsigned int has_template_arguments : 1;
1442 /* Flag set if fixup_partial_die has been called on this die. */
1443 unsigned int fixup_called : 1;
1445 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1446 unsigned int is_dwz : 1;
1448 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1449 unsigned int spec_is_dwz : 1;
1451 /* The name of this DIE. Normally the value of DW_AT_name, but
1452 sometimes a default name for unnamed DIEs. */
1455 /* The linkage name, if present. */
1456 const char *linkage_name;
1458 /* The scope to prepend to our children. This is generally
1459 allocated on the comp_unit_obstack, so will disappear
1460 when this compilation unit leaves the cache. */
1463 /* Some data associated with the partial DIE. The tag determines
1464 which field is live. */
1467 /* The location description associated with this DIE, if any. */
1468 struct dwarf_block *locdesc;
1469 /* The offset of an import, for DW_TAG_imported_unit. */
1470 sect_offset sect_off;
1473 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1477 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1478 DW_AT_sibling, if any. */
1479 /* NOTE: This member isn't strictly necessary, read_partial_die could
1480 return DW_AT_sibling values to its caller load_partial_dies. */
1481 const gdb_byte *sibling;
1483 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1484 DW_AT_specification (or DW_AT_abstract_origin or
1485 DW_AT_extension). */
1486 sect_offset spec_offset;
1488 /* Pointers to this DIE's parent, first child, and next sibling,
1490 struct partial_die_info *die_parent, *die_child, *die_sibling;
1493 /* This data structure holds the information of an abbrev. */
1496 unsigned int number; /* number identifying abbrev */
1497 enum dwarf_tag tag; /* dwarf tag */
1498 unsigned short has_children; /* boolean */
1499 unsigned short num_attrs; /* number of attributes */
1500 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1501 struct abbrev_info *next; /* next in chain */
1506 ENUM_BITFIELD(dwarf_attribute) name : 16;
1507 ENUM_BITFIELD(dwarf_form) form : 16;
1509 /* It is valid only if FORM is DW_FORM_implicit_const. */
1510 LONGEST implicit_const;
1513 /* Size of abbrev_table.abbrev_hash_table. */
1514 #define ABBREV_HASH_SIZE 121
1516 /* Top level data structure to contain an abbreviation table. */
1520 /* Where the abbrev table came from.
1521 This is used as a sanity check when the table is used. */
1522 sect_offset sect_off;
1524 /* Storage for the abbrev table. */
1525 struct obstack abbrev_obstack;
1527 /* Hash table of abbrevs.
1528 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1529 It could be statically allocated, but the previous code didn't so we
1531 struct abbrev_info **abbrevs;
1534 /* Attributes have a name and a value. */
1537 ENUM_BITFIELD(dwarf_attribute) name : 16;
1538 ENUM_BITFIELD(dwarf_form) form : 15;
1540 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1541 field should be in u.str (existing only for DW_STRING) but it is kept
1542 here for better struct attribute alignment. */
1543 unsigned int string_is_canonical : 1;
1548 struct dwarf_block *blk;
1557 /* This data structure holds a complete die structure. */
1560 /* DWARF-2 tag for this DIE. */
1561 ENUM_BITFIELD(dwarf_tag) tag : 16;
1563 /* Number of attributes */
1564 unsigned char num_attrs;
1566 /* True if we're presently building the full type name for the
1567 type derived from this DIE. */
1568 unsigned char building_fullname : 1;
1570 /* True if this die is in process. PR 16581. */
1571 unsigned char in_process : 1;
1574 unsigned int abbrev;
1576 /* Offset in .debug_info or .debug_types section. */
1577 sect_offset sect_off;
1579 /* The dies in a compilation unit form an n-ary tree. PARENT
1580 points to this die's parent; CHILD points to the first child of
1581 this node; and all the children of a given node are chained
1582 together via their SIBLING fields. */
1583 struct die_info *child; /* Its first child, if any. */
1584 struct die_info *sibling; /* Its next sibling, if any. */
1585 struct die_info *parent; /* Its parent, if any. */
1587 /* An array of attributes, with NUM_ATTRS elements. There may be
1588 zero, but it's not common and zero-sized arrays are not
1589 sufficiently portable C. */
1590 struct attribute attrs[1];
1593 /* Get at parts of an attribute structure. */
1595 #define DW_STRING(attr) ((attr)->u.str)
1596 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1597 #define DW_UNSND(attr) ((attr)->u.unsnd)
1598 #define DW_BLOCK(attr) ((attr)->u.blk)
1599 #define DW_SND(attr) ((attr)->u.snd)
1600 #define DW_ADDR(attr) ((attr)->u.addr)
1601 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1603 /* Blocks are a bunch of untyped bytes. */
1608 /* Valid only if SIZE is not zero. */
1609 const gdb_byte *data;
1612 #ifndef ATTR_ALLOC_CHUNK
1613 #define ATTR_ALLOC_CHUNK 4
1616 /* Allocate fields for structs, unions and enums in this size. */
1617 #ifndef DW_FIELD_ALLOC_CHUNK
1618 #define DW_FIELD_ALLOC_CHUNK 4
1621 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1622 but this would require a corresponding change in unpack_field_as_long
1624 static int bits_per_byte = 8;
1628 struct nextfield *next;
1636 struct nextfnfield *next;
1637 struct fn_field fnfield;
1644 struct nextfnfield *head;
1647 struct decl_field_list
1649 struct decl_field field;
1650 struct decl_field_list *next;
1653 /* The routines that read and process dies for a C struct or C++ class
1654 pass lists of data member fields and lists of member function fields
1655 in an instance of a field_info structure, as defined below. */
1658 /* List of data member and baseclasses fields. */
1659 struct nextfield *fields, *baseclasses;
1661 /* Number of fields (including baseclasses). */
1664 /* Number of baseclasses. */
1667 /* Set if the accesibility of one of the fields is not public. */
1668 int non_public_fields;
1670 /* Member function fieldlist array, contains name of possibly overloaded
1671 member function, number of overloaded member functions and a pointer
1672 to the head of the member function field chain. */
1673 struct fnfieldlist *fnfieldlists;
1675 /* Number of entries in the fnfieldlists array. */
1678 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1679 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1680 struct decl_field_list *typedef_field_list;
1681 unsigned typedef_field_list_count;
1683 /* Nested types defined by this class and the number of elements in this
1685 struct decl_field_list *nested_types_list;
1686 unsigned nested_types_list_count;
1689 /* One item on the queue of compilation units to read in full symbols
1691 struct dwarf2_queue_item
1693 struct dwarf2_per_cu_data *per_cu;
1694 enum language pretend_language;
1695 struct dwarf2_queue_item *next;
1698 /* The current queue. */
1699 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1701 /* Loaded secondary compilation units are kept in memory until they
1702 have not been referenced for the processing of this many
1703 compilation units. Set this to zero to disable caching. Cache
1704 sizes of up to at least twenty will improve startup time for
1705 typical inter-CU-reference binaries, at an obvious memory cost. */
1706 static int dwarf_max_cache_age = 5;
1708 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1709 struct cmd_list_element *c, const char *value)
1711 fprintf_filtered (file, _("The upper bound on the age of cached "
1712 "DWARF compilation units is %s.\n"),
1716 /* local function prototypes */
1718 static const char *get_section_name (const struct dwarf2_section_info *);
1720 static const char *get_section_file_name (const struct dwarf2_section_info *);
1722 static void dwarf2_find_base_address (struct die_info *die,
1723 struct dwarf2_cu *cu);
1725 static struct partial_symtab *create_partial_symtab
1726 (struct dwarf2_per_cu_data *per_cu, const char *name);
1728 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1729 const gdb_byte *info_ptr,
1730 struct die_info *type_unit_die,
1731 int has_children, void *data);
1733 static void dwarf2_build_psymtabs_hard
1734 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1736 static void scan_partial_symbols (struct partial_die_info *,
1737 CORE_ADDR *, CORE_ADDR *,
1738 int, struct dwarf2_cu *);
1740 static void add_partial_symbol (struct partial_die_info *,
1741 struct dwarf2_cu *);
1743 static void add_partial_namespace (struct partial_die_info *pdi,
1744 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1745 int set_addrmap, struct dwarf2_cu *cu);
1747 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1748 CORE_ADDR *highpc, int set_addrmap,
1749 struct dwarf2_cu *cu);
1751 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1752 struct dwarf2_cu *cu);
1754 static void add_partial_subprogram (struct partial_die_info *pdi,
1755 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1756 int need_pc, struct dwarf2_cu *cu);
1758 static void dwarf2_read_symtab (struct partial_symtab *,
1761 static void psymtab_to_symtab_1 (struct partial_symtab *);
1763 static struct abbrev_info *abbrev_table_lookup_abbrev
1764 (const struct abbrev_table *, unsigned int);
1766 static struct abbrev_table *abbrev_table_read_table
1767 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1770 static void abbrev_table_free (struct abbrev_table *);
1772 static void abbrev_table_free_cleanup (void *);
1774 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1775 struct dwarf2_section_info *);
1777 static void dwarf2_free_abbrev_table (void *);
1779 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1781 static struct partial_die_info *load_partial_dies
1782 (const struct die_reader_specs *, const gdb_byte *, int);
1784 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1785 struct partial_die_info *,
1786 struct abbrev_info *,
1790 static struct partial_die_info *find_partial_die (sect_offset, int,
1791 struct dwarf2_cu *);
1793 static void fixup_partial_die (struct partial_die_info *,
1794 struct dwarf2_cu *);
1796 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1797 struct attribute *, struct attr_abbrev *,
1800 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1802 static int read_1_signed_byte (bfd *, const gdb_byte *);
1804 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1806 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1808 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1810 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1813 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1815 static LONGEST read_checked_initial_length_and_offset
1816 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1817 unsigned int *, unsigned int *);
1819 static LONGEST read_offset (bfd *, const gdb_byte *,
1820 const struct comp_unit_head *,
1823 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1825 static sect_offset read_abbrev_offset
1826 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1827 struct dwarf2_section_info *, sect_offset);
1829 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1831 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1833 static const char *read_indirect_string
1834 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1835 const struct comp_unit_head *, unsigned int *);
1837 static const char *read_indirect_line_string
1838 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1839 const struct comp_unit_head *, unsigned int *);
1841 static const char *read_indirect_string_at_offset
1842 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1843 LONGEST str_offset);
1845 static const char *read_indirect_string_from_dwz
1846 (struct objfile *objfile, struct dwz_file *, LONGEST);
1848 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1850 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1854 static const char *read_str_index (const struct die_reader_specs *reader,
1855 ULONGEST str_index);
1857 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1859 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1860 struct dwarf2_cu *);
1862 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1865 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1866 struct dwarf2_cu *cu);
1868 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1869 struct dwarf2_cu *cu);
1871 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1873 static struct die_info *die_specification (struct die_info *die,
1874 struct dwarf2_cu **);
1876 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1877 struct dwarf2_cu *cu);
1879 static void dwarf_decode_lines (struct line_header *, const char *,
1880 struct dwarf2_cu *, struct partial_symtab *,
1881 CORE_ADDR, int decode_mapping);
1883 static void dwarf2_start_subfile (const char *, const char *);
1885 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1886 const char *, const char *,
1889 static struct symbol *new_symbol (struct die_info *, struct type *,
1890 struct dwarf2_cu *);
1892 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1893 struct dwarf2_cu *, struct symbol *);
1895 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1896 struct dwarf2_cu *);
1898 static void dwarf2_const_value_attr (const struct attribute *attr,
1901 struct obstack *obstack,
1902 struct dwarf2_cu *cu, LONGEST *value,
1903 const gdb_byte **bytes,
1904 struct dwarf2_locexpr_baton **baton);
1906 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1908 static int need_gnat_info (struct dwarf2_cu *);
1910 static struct type *die_descriptive_type (struct die_info *,
1911 struct dwarf2_cu *);
1913 static void set_descriptive_type (struct type *, struct die_info *,
1914 struct dwarf2_cu *);
1916 static struct type *die_containing_type (struct die_info *,
1917 struct dwarf2_cu *);
1919 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1920 struct dwarf2_cu *);
1922 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1924 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1926 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1928 static char *typename_concat (struct obstack *obs, const char *prefix,
1929 const char *suffix, int physname,
1930 struct dwarf2_cu *cu);
1932 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1934 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1936 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1938 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1940 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1942 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1944 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1945 struct dwarf2_cu *, struct partial_symtab *);
1947 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1948 values. Keep the items ordered with increasing constraints compliance. */
1951 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1952 PC_BOUNDS_NOT_PRESENT,
1954 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1955 were present but they do not form a valid range of PC addresses. */
1958 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1961 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1965 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1966 CORE_ADDR *, CORE_ADDR *,
1968 struct partial_symtab *);
1970 static void get_scope_pc_bounds (struct die_info *,
1971 CORE_ADDR *, CORE_ADDR *,
1972 struct dwarf2_cu *);
1974 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1975 CORE_ADDR, struct dwarf2_cu *);
1977 static void dwarf2_add_field (struct field_info *, struct die_info *,
1978 struct dwarf2_cu *);
1980 static void dwarf2_attach_fields_to_type (struct field_info *,
1981 struct type *, struct dwarf2_cu *);
1983 static void dwarf2_add_member_fn (struct field_info *,
1984 struct die_info *, struct type *,
1985 struct dwarf2_cu *);
1987 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1989 struct dwarf2_cu *);
1991 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1993 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1995 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1997 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1999 static struct using_direct **using_directives (enum language);
2001 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2003 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2005 static struct type *read_module_type (struct die_info *die,
2006 struct dwarf2_cu *cu);
2008 static const char *namespace_name (struct die_info *die,
2009 int *is_anonymous, struct dwarf2_cu *);
2011 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2013 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2015 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2016 struct dwarf2_cu *);
2018 static struct die_info *read_die_and_siblings_1
2019 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2022 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2023 const gdb_byte *info_ptr,
2024 const gdb_byte **new_info_ptr,
2025 struct die_info *parent);
2027 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2028 struct die_info **, const gdb_byte *,
2031 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2032 struct die_info **, const gdb_byte *,
2035 static void process_die (struct die_info *, struct dwarf2_cu *);
2037 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2040 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2042 static const char *dwarf2_full_name (const char *name,
2043 struct die_info *die,
2044 struct dwarf2_cu *cu);
2046 static const char *dwarf2_physname (const char *name, struct die_info *die,
2047 struct dwarf2_cu *cu);
2049 static struct die_info *dwarf2_extension (struct die_info *die,
2050 struct dwarf2_cu **);
2052 static const char *dwarf_tag_name (unsigned int);
2054 static const char *dwarf_attr_name (unsigned int);
2056 static const char *dwarf_form_name (unsigned int);
2058 static const char *dwarf_bool_name (unsigned int);
2060 static const char *dwarf_type_encoding_name (unsigned int);
2062 static struct die_info *sibling_die (struct die_info *);
2064 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2066 static void dump_die_for_error (struct die_info *);
2068 static void dump_die_1 (struct ui_file *, int level, int max_level,
2071 /*static*/ void dump_die (struct die_info *, int max_level);
2073 static void store_in_ref_table (struct die_info *,
2074 struct dwarf2_cu *);
2076 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2078 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2080 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2081 const struct attribute *,
2082 struct dwarf2_cu **);
2084 static struct die_info *follow_die_ref (struct die_info *,
2085 const struct attribute *,
2086 struct dwarf2_cu **);
2088 static struct die_info *follow_die_sig (struct die_info *,
2089 const struct attribute *,
2090 struct dwarf2_cu **);
2092 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2093 struct dwarf2_cu *);
2095 static struct type *get_DW_AT_signature_type (struct die_info *,
2096 const struct attribute *,
2097 struct dwarf2_cu *);
2099 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2101 static void read_signatured_type (struct signatured_type *);
2103 static int attr_to_dynamic_prop (const struct attribute *attr,
2104 struct die_info *die, struct dwarf2_cu *cu,
2105 struct dynamic_prop *prop);
2107 /* memory allocation interface */
2109 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2111 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2113 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2115 static int attr_form_is_block (const struct attribute *);
2117 static int attr_form_is_section_offset (const struct attribute *);
2119 static int attr_form_is_constant (const struct attribute *);
2121 static int attr_form_is_ref (const struct attribute *);
2123 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2124 struct dwarf2_loclist_baton *baton,
2125 const struct attribute *attr);
2127 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2129 struct dwarf2_cu *cu,
2132 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2133 const gdb_byte *info_ptr,
2134 struct abbrev_info *abbrev);
2136 static void free_stack_comp_unit (void *);
2138 static hashval_t partial_die_hash (const void *item);
2140 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2142 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2143 (sect_offset sect_off, unsigned int offset_in_dwz,
2144 struct dwarf2_per_objfile *dwarf2_per_objfile);
2146 static void init_one_comp_unit (struct dwarf2_cu *cu,
2147 struct dwarf2_per_cu_data *per_cu);
2149 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2150 struct die_info *comp_unit_die,
2151 enum language pretend_language);
2153 static void free_heap_comp_unit (void *);
2155 static void free_cached_comp_units (void *);
2157 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2159 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2161 static struct type *set_die_type (struct die_info *, struct type *,
2162 struct dwarf2_cu *);
2164 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2166 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2168 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2171 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2174 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2177 static void dwarf2_add_dependence (struct dwarf2_cu *,
2178 struct dwarf2_per_cu_data *);
2180 static void dwarf2_mark (struct dwarf2_cu *);
2182 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2184 static struct type *get_die_type_at_offset (sect_offset,
2185 struct dwarf2_per_cu_data *);
2187 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2189 static void dwarf2_release_queue (void *dummy);
2191 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2192 enum language pretend_language);
2194 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2196 /* The return type of find_file_and_directory. Note, the enclosed
2197 string pointers are only valid while this object is valid. */
2199 struct file_and_directory
2201 /* The filename. This is never NULL. */
2204 /* The compilation directory. NULL if not known. If we needed to
2205 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2206 points directly to the DW_AT_comp_dir string attribute owned by
2207 the obstack that owns the DIE. */
2208 const char *comp_dir;
2210 /* If we needed to build a new string for comp_dir, this is what
2211 owns the storage. */
2212 std::string comp_dir_storage;
2215 static file_and_directory find_file_and_directory (struct die_info *die,
2216 struct dwarf2_cu *cu);
2218 static char *file_full_name (int file, struct line_header *lh,
2219 const char *comp_dir);
2221 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2222 enum class rcuh_kind { COMPILE, TYPE };
2224 static const gdb_byte *read_and_check_comp_unit_head
2225 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2226 struct comp_unit_head *header,
2227 struct dwarf2_section_info *section,
2228 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2229 rcuh_kind section_kind);
2231 static void init_cutu_and_read_dies
2232 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2233 int use_existing_cu, int keep,
2234 die_reader_func_ftype *die_reader_func, void *data);
2236 static void init_cutu_and_read_dies_simple
2237 (struct dwarf2_per_cu_data *this_cu,
2238 die_reader_func_ftype *die_reader_func, void *data);
2240 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2242 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2244 static struct dwo_unit *lookup_dwo_unit_in_dwp
2245 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2246 struct dwp_file *dwp_file, const char *comp_dir,
2247 ULONGEST signature, int is_debug_types);
2249 static struct dwp_file *get_dwp_file
2250 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2252 static struct dwo_unit *lookup_dwo_comp_unit
2253 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2255 static struct dwo_unit *lookup_dwo_type_unit
2256 (struct signatured_type *, const char *, const char *);
2258 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2260 static void free_dwo_file_cleanup (void *);
2262 struct free_dwo_file_cleanup_data
2264 struct dwo_file *dwo_file;
2265 struct dwarf2_per_objfile *dwarf2_per_objfile;
2268 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2270 static void check_producer (struct dwarf2_cu *cu);
2272 static void free_line_header_voidp (void *arg);
2274 /* Various complaints about symbol reading that don't abort the process. */
2277 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2279 complaint (&symfile_complaints,
2280 _("statement list doesn't fit in .debug_line section"));
2284 dwarf2_debug_line_missing_file_complaint (void)
2286 complaint (&symfile_complaints,
2287 _(".debug_line section has line data without a file"));
2291 dwarf2_debug_line_missing_end_sequence_complaint (void)
2293 complaint (&symfile_complaints,
2294 _(".debug_line section has line "
2295 "program sequence without an end"));
2299 dwarf2_complex_location_expr_complaint (void)
2301 complaint (&symfile_complaints, _("location expression too complex"));
2305 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2308 complaint (&symfile_complaints,
2309 _("const value length mismatch for '%s', got %d, expected %d"),
2314 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2316 complaint (&symfile_complaints,
2317 _("debug info runs off end of %s section"
2319 get_section_name (section),
2320 get_section_file_name (section));
2324 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2326 complaint (&symfile_complaints,
2327 _("macro debug info contains a "
2328 "malformed macro definition:\n`%s'"),
2333 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2335 complaint (&symfile_complaints,
2336 _("invalid attribute class or form for '%s' in '%s'"),
2340 /* Hash function for line_header_hash. */
2343 line_header_hash (const struct line_header *ofs)
2345 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2348 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2351 line_header_hash_voidp (const void *item)
2353 const struct line_header *ofs = (const struct line_header *) item;
2355 return line_header_hash (ofs);
2358 /* Equality function for line_header_hash. */
2361 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2363 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2364 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2366 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2367 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2372 /* Read the given attribute value as an address, taking the attribute's
2373 form into account. */
2376 attr_value_as_address (struct attribute *attr)
2380 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2382 /* Aside from a few clearly defined exceptions, attributes that
2383 contain an address must always be in DW_FORM_addr form.
2384 Unfortunately, some compilers happen to be violating this
2385 requirement by encoding addresses using other forms, such
2386 as DW_FORM_data4 for example. For those broken compilers,
2387 we try to do our best, without any guarantee of success,
2388 to interpret the address correctly. It would also be nice
2389 to generate a complaint, but that would require us to maintain
2390 a list of legitimate cases where a non-address form is allowed,
2391 as well as update callers to pass in at least the CU's DWARF
2392 version. This is more overhead than what we're willing to
2393 expand for a pretty rare case. */
2394 addr = DW_UNSND (attr);
2397 addr = DW_ADDR (attr);
2402 /* The suffix for an index file. */
2403 #define INDEX4_SUFFIX ".gdb-index"
2404 #define INDEX5_SUFFIX ".debug_names"
2405 #define DEBUG_STR_SUFFIX ".debug_str"
2407 /* See declaration. */
2409 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2410 const dwarf2_debug_sections *names)
2411 : objfile (objfile_)
2414 names = &dwarf2_elf_names;
2416 bfd *obfd = objfile->obfd;
2418 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2419 locate_sections (obfd, sec, *names);
2422 dwarf2_per_objfile::~dwarf2_per_objfile ()
2424 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2425 free_cached_comp_units ();
2427 if (quick_file_names_table)
2428 htab_delete (quick_file_names_table);
2430 if (line_header_hash)
2431 htab_delete (line_header_hash);
2433 /* Everything else should be on the objfile obstack. */
2436 /* See declaration. */
2439 dwarf2_per_objfile::free_cached_comp_units ()
2441 dwarf2_per_cu_data *per_cu = read_in_chain;
2442 dwarf2_per_cu_data **last_chain = &read_in_chain;
2443 while (per_cu != NULL)
2445 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2447 free_heap_comp_unit (per_cu->cu);
2448 *last_chain = next_cu;
2453 /* Try to locate the sections we need for DWARF 2 debugging
2454 information and return true if we have enough to do something.
2455 NAMES points to the dwarf2 section names, or is NULL if the standard
2456 ELF names are used. */
2459 dwarf2_has_info (struct objfile *objfile,
2460 const struct dwarf2_debug_sections *names)
2462 if (objfile->flags & OBJF_READNEVER)
2465 struct dwarf2_per_objfile *dwarf2_per_objfile
2466 = get_dwarf2_per_objfile (objfile);
2468 if (dwarf2_per_objfile == NULL)
2470 /* Initialize per-objfile state. */
2471 struct dwarf2_per_objfile *data
2472 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2474 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2475 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2477 return (!dwarf2_per_objfile->info.is_virtual
2478 && dwarf2_per_objfile->info.s.section != NULL
2479 && !dwarf2_per_objfile->abbrev.is_virtual
2480 && dwarf2_per_objfile->abbrev.s.section != NULL);
2483 /* Return the containing section of virtual section SECTION. */
2485 static struct dwarf2_section_info *
2486 get_containing_section (const struct dwarf2_section_info *section)
2488 gdb_assert (section->is_virtual);
2489 return section->s.containing_section;
2492 /* Return the bfd owner of SECTION. */
2495 get_section_bfd_owner (const struct dwarf2_section_info *section)
2497 if (section->is_virtual)
2499 section = get_containing_section (section);
2500 gdb_assert (!section->is_virtual);
2502 return section->s.section->owner;
2505 /* Return the bfd section of SECTION.
2506 Returns NULL if the section is not present. */
2509 get_section_bfd_section (const struct dwarf2_section_info *section)
2511 if (section->is_virtual)
2513 section = get_containing_section (section);
2514 gdb_assert (!section->is_virtual);
2516 return section->s.section;
2519 /* Return the name of SECTION. */
2522 get_section_name (const struct dwarf2_section_info *section)
2524 asection *sectp = get_section_bfd_section (section);
2526 gdb_assert (sectp != NULL);
2527 return bfd_section_name (get_section_bfd_owner (section), sectp);
2530 /* Return the name of the file SECTION is in. */
2533 get_section_file_name (const struct dwarf2_section_info *section)
2535 bfd *abfd = get_section_bfd_owner (section);
2537 return bfd_get_filename (abfd);
2540 /* Return the id of SECTION.
2541 Returns 0 if SECTION doesn't exist. */
2544 get_section_id (const struct dwarf2_section_info *section)
2546 asection *sectp = get_section_bfd_section (section);
2553 /* Return the flags of SECTION.
2554 SECTION (or containing section if this is a virtual section) must exist. */
2557 get_section_flags (const struct dwarf2_section_info *section)
2559 asection *sectp = get_section_bfd_section (section);
2561 gdb_assert (sectp != NULL);
2562 return bfd_get_section_flags (sectp->owner, sectp);
2565 /* When loading sections, we look either for uncompressed section or for
2566 compressed section names. */
2569 section_is_p (const char *section_name,
2570 const struct dwarf2_section_names *names)
2572 if (names->normal != NULL
2573 && strcmp (section_name, names->normal) == 0)
2575 if (names->compressed != NULL
2576 && strcmp (section_name, names->compressed) == 0)
2581 /* See declaration. */
2584 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2585 const dwarf2_debug_sections &names)
2587 flagword aflag = bfd_get_section_flags (abfd, sectp);
2589 if ((aflag & SEC_HAS_CONTENTS) == 0)
2592 else if (section_is_p (sectp->name, &names.info))
2594 this->info.s.section = sectp;
2595 this->info.size = bfd_get_section_size (sectp);
2597 else if (section_is_p (sectp->name, &names.abbrev))
2599 this->abbrev.s.section = sectp;
2600 this->abbrev.size = bfd_get_section_size (sectp);
2602 else if (section_is_p (sectp->name, &names.line))
2604 this->line.s.section = sectp;
2605 this->line.size = bfd_get_section_size (sectp);
2607 else if (section_is_p (sectp->name, &names.loc))
2609 this->loc.s.section = sectp;
2610 this->loc.size = bfd_get_section_size (sectp);
2612 else if (section_is_p (sectp->name, &names.loclists))
2614 this->loclists.s.section = sectp;
2615 this->loclists.size = bfd_get_section_size (sectp);
2617 else if (section_is_p (sectp->name, &names.macinfo))
2619 this->macinfo.s.section = sectp;
2620 this->macinfo.size = bfd_get_section_size (sectp);
2622 else if (section_is_p (sectp->name, &names.macro))
2624 this->macro.s.section = sectp;
2625 this->macro.size = bfd_get_section_size (sectp);
2627 else if (section_is_p (sectp->name, &names.str))
2629 this->str.s.section = sectp;
2630 this->str.size = bfd_get_section_size (sectp);
2632 else if (section_is_p (sectp->name, &names.line_str))
2634 this->line_str.s.section = sectp;
2635 this->line_str.size = bfd_get_section_size (sectp);
2637 else if (section_is_p (sectp->name, &names.addr))
2639 this->addr.s.section = sectp;
2640 this->addr.size = bfd_get_section_size (sectp);
2642 else if (section_is_p (sectp->name, &names.frame))
2644 this->frame.s.section = sectp;
2645 this->frame.size = bfd_get_section_size (sectp);
2647 else if (section_is_p (sectp->name, &names.eh_frame))
2649 this->eh_frame.s.section = sectp;
2650 this->eh_frame.size = bfd_get_section_size (sectp);
2652 else if (section_is_p (sectp->name, &names.ranges))
2654 this->ranges.s.section = sectp;
2655 this->ranges.size = bfd_get_section_size (sectp);
2657 else if (section_is_p (sectp->name, &names.rnglists))
2659 this->rnglists.s.section = sectp;
2660 this->rnglists.size = bfd_get_section_size (sectp);
2662 else if (section_is_p (sectp->name, &names.types))
2664 struct dwarf2_section_info type_section;
2666 memset (&type_section, 0, sizeof (type_section));
2667 type_section.s.section = sectp;
2668 type_section.size = bfd_get_section_size (sectp);
2670 VEC_safe_push (dwarf2_section_info_def, this->types,
2673 else if (section_is_p (sectp->name, &names.gdb_index))
2675 this->gdb_index.s.section = sectp;
2676 this->gdb_index.size = bfd_get_section_size (sectp);
2678 else if (section_is_p (sectp->name, &names.debug_names))
2680 this->debug_names.s.section = sectp;
2681 this->debug_names.size = bfd_get_section_size (sectp);
2683 else if (section_is_p (sectp->name, &names.debug_aranges))
2685 this->debug_aranges.s.section = sectp;
2686 this->debug_aranges.size = bfd_get_section_size (sectp);
2689 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2690 && bfd_section_vma (abfd, sectp) == 0)
2691 this->has_section_at_zero = true;
2694 /* A helper function that decides whether a section is empty,
2698 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2700 if (section->is_virtual)
2701 return section->size == 0;
2702 return section->s.section == NULL || section->size == 0;
2705 /* Read the contents of the section INFO.
2706 OBJFILE is the main object file, but not necessarily the file where
2707 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2709 If the section is compressed, uncompress it before returning. */
2712 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2716 gdb_byte *buf, *retbuf;
2720 info->buffer = NULL;
2723 if (dwarf2_section_empty_p (info))
2726 sectp = get_section_bfd_section (info);
2728 /* If this is a virtual section we need to read in the real one first. */
2729 if (info->is_virtual)
2731 struct dwarf2_section_info *containing_section =
2732 get_containing_section (info);
2734 gdb_assert (sectp != NULL);
2735 if ((sectp->flags & SEC_RELOC) != 0)
2737 error (_("Dwarf Error: DWP format V2 with relocations is not"
2738 " supported in section %s [in module %s]"),
2739 get_section_name (info), get_section_file_name (info));
2741 dwarf2_read_section (objfile, containing_section);
2742 /* Other code should have already caught virtual sections that don't
2744 gdb_assert (info->virtual_offset + info->size
2745 <= containing_section->size);
2746 /* If the real section is empty or there was a problem reading the
2747 section we shouldn't get here. */
2748 gdb_assert (containing_section->buffer != NULL);
2749 info->buffer = containing_section->buffer + info->virtual_offset;
2753 /* If the section has relocations, we must read it ourselves.
2754 Otherwise we attach it to the BFD. */
2755 if ((sectp->flags & SEC_RELOC) == 0)
2757 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2761 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2764 /* When debugging .o files, we may need to apply relocations; see
2765 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2766 We never compress sections in .o files, so we only need to
2767 try this when the section is not compressed. */
2768 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2771 info->buffer = retbuf;
2775 abfd = get_section_bfd_owner (info);
2776 gdb_assert (abfd != NULL);
2778 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2779 || bfd_bread (buf, info->size, abfd) != info->size)
2781 error (_("Dwarf Error: Can't read DWARF data"
2782 " in section %s [in module %s]"),
2783 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2787 /* A helper function that returns the size of a section in a safe way.
2788 If you are positive that the section has been read before using the
2789 size, then it is safe to refer to the dwarf2_section_info object's
2790 "size" field directly. In other cases, you must call this
2791 function, because for compressed sections the size field is not set
2792 correctly until the section has been read. */
2794 static bfd_size_type
2795 dwarf2_section_size (struct objfile *objfile,
2796 struct dwarf2_section_info *info)
2799 dwarf2_read_section (objfile, info);
2803 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2807 dwarf2_get_section_info (struct objfile *objfile,
2808 enum dwarf2_section_enum sect,
2809 asection **sectp, const gdb_byte **bufp,
2810 bfd_size_type *sizep)
2812 struct dwarf2_per_objfile *data
2813 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2814 dwarf2_objfile_data_key);
2815 struct dwarf2_section_info *info;
2817 /* We may see an objfile without any DWARF, in which case we just
2828 case DWARF2_DEBUG_FRAME:
2829 info = &data->frame;
2831 case DWARF2_EH_FRAME:
2832 info = &data->eh_frame;
2835 gdb_assert_not_reached ("unexpected section");
2838 dwarf2_read_section (objfile, info);
2840 *sectp = get_section_bfd_section (info);
2841 *bufp = info->buffer;
2842 *sizep = info->size;
2845 /* A helper function to find the sections for a .dwz file. */
2848 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2850 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2852 /* Note that we only support the standard ELF names, because .dwz
2853 is ELF-only (at the time of writing). */
2854 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2856 dwz_file->abbrev.s.section = sectp;
2857 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2859 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2861 dwz_file->info.s.section = sectp;
2862 dwz_file->info.size = bfd_get_section_size (sectp);
2864 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2866 dwz_file->str.s.section = sectp;
2867 dwz_file->str.size = bfd_get_section_size (sectp);
2869 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2871 dwz_file->line.s.section = sectp;
2872 dwz_file->line.size = bfd_get_section_size (sectp);
2874 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2876 dwz_file->macro.s.section = sectp;
2877 dwz_file->macro.size = bfd_get_section_size (sectp);
2879 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2881 dwz_file->gdb_index.s.section = sectp;
2882 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2884 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2886 dwz_file->debug_names.s.section = sectp;
2887 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2891 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2892 there is no .gnu_debugaltlink section in the file. Error if there
2893 is such a section but the file cannot be found. */
2895 static struct dwz_file *
2896 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2898 const char *filename;
2899 struct dwz_file *result;
2900 bfd_size_type buildid_len_arg;
2904 if (dwarf2_per_objfile->dwz_file != NULL)
2905 return dwarf2_per_objfile->dwz_file;
2907 bfd_set_error (bfd_error_no_error);
2908 gdb::unique_xmalloc_ptr<char> data
2909 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2910 &buildid_len_arg, &buildid));
2913 if (bfd_get_error () == bfd_error_no_error)
2915 error (_("could not read '.gnu_debugaltlink' section: %s"),
2916 bfd_errmsg (bfd_get_error ()));
2919 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2921 buildid_len = (size_t) buildid_len_arg;
2923 filename = data.get ();
2925 std::string abs_storage;
2926 if (!IS_ABSOLUTE_PATH (filename))
2928 gdb::unique_xmalloc_ptr<char> abs
2929 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2931 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2932 filename = abs_storage.c_str ();
2935 /* First try the file name given in the section. If that doesn't
2936 work, try to use the build-id instead. */
2937 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2938 if (dwz_bfd != NULL)
2940 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2944 if (dwz_bfd == NULL)
2945 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2947 if (dwz_bfd == NULL)
2948 error (_("could not find '.gnu_debugaltlink' file for %s"),
2949 objfile_name (dwarf2_per_objfile->objfile));
2951 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2953 result->dwz_bfd = dwz_bfd.release ();
2955 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2957 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2958 dwarf2_per_objfile->dwz_file = result;
2962 /* DWARF quick_symbols_functions support. */
2964 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2965 unique line tables, so we maintain a separate table of all .debug_line
2966 derived entries to support the sharing.
2967 All the quick functions need is the list of file names. We discard the
2968 line_header when we're done and don't need to record it here. */
2969 struct quick_file_names
2971 /* The data used to construct the hash key. */
2972 struct stmt_list_hash hash;
2974 /* The number of entries in file_names, real_names. */
2975 unsigned int num_file_names;
2977 /* The file names from the line table, after being run through
2979 const char **file_names;
2981 /* The file names from the line table after being run through
2982 gdb_realpath. These are computed lazily. */
2983 const char **real_names;
2986 /* When using the index (and thus not using psymtabs), each CU has an
2987 object of this type. This is used to hold information needed by
2988 the various "quick" methods. */
2989 struct dwarf2_per_cu_quick_data
2991 /* The file table. This can be NULL if there was no file table
2992 or it's currently not read in.
2993 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2994 struct quick_file_names *file_names;
2996 /* The corresponding symbol table. This is NULL if symbols for this
2997 CU have not yet been read. */
2998 struct compunit_symtab *compunit_symtab;
3000 /* A temporary mark bit used when iterating over all CUs in
3001 expand_symtabs_matching. */
3002 unsigned int mark : 1;
3004 /* True if we've tried to read the file table and found there isn't one.
3005 There will be no point in trying to read it again next time. */
3006 unsigned int no_file_data : 1;
3009 /* Utility hash function for a stmt_list_hash. */
3012 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3016 if (stmt_list_hash->dwo_unit != NULL)
3017 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3018 v += to_underlying (stmt_list_hash->line_sect_off);
3022 /* Utility equality function for a stmt_list_hash. */
3025 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3026 const struct stmt_list_hash *rhs)
3028 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3030 if (lhs->dwo_unit != NULL
3031 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3034 return lhs->line_sect_off == rhs->line_sect_off;
3037 /* Hash function for a quick_file_names. */
3040 hash_file_name_entry (const void *e)
3042 const struct quick_file_names *file_data
3043 = (const struct quick_file_names *) e;
3045 return hash_stmt_list_entry (&file_data->hash);
3048 /* Equality function for a quick_file_names. */
3051 eq_file_name_entry (const void *a, const void *b)
3053 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3054 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3056 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3059 /* Delete function for a quick_file_names. */
3062 delete_file_name_entry (void *e)
3064 struct quick_file_names *file_data = (struct quick_file_names *) e;
3067 for (i = 0; i < file_data->num_file_names; ++i)
3069 xfree ((void*) file_data->file_names[i]);
3070 if (file_data->real_names)
3071 xfree ((void*) file_data->real_names[i]);
3074 /* The space for the struct itself lives on objfile_obstack,
3075 so we don't free it here. */
3078 /* Create a quick_file_names hash table. */
3081 create_quick_file_names_table (unsigned int nr_initial_entries)
3083 return htab_create_alloc (nr_initial_entries,
3084 hash_file_name_entry, eq_file_name_entry,
3085 delete_file_name_entry, xcalloc, xfree);
3088 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3089 have to be created afterwards. You should call age_cached_comp_units after
3090 processing PER_CU->CU. dw2_setup must have been already called. */
3093 load_cu (struct dwarf2_per_cu_data *per_cu)
3095 if (per_cu->is_debug_types)
3096 load_full_type_unit (per_cu);
3098 load_full_comp_unit (per_cu, language_minimal);
3100 if (per_cu->cu == NULL)
3101 return; /* Dummy CU. */
3103 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3106 /* Read in the symbols for PER_CU. */
3109 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3111 struct cleanup *back_to;
3112 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3114 /* Skip type_unit_groups, reading the type units they contain
3115 is handled elsewhere. */
3116 if (IS_TYPE_UNIT_GROUP (per_cu))
3119 back_to = make_cleanup (dwarf2_release_queue, NULL);
3121 if (dwarf2_per_objfile->using_index
3122 ? per_cu->v.quick->compunit_symtab == NULL
3123 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3125 queue_comp_unit (per_cu, language_minimal);
3128 /* If we just loaded a CU from a DWO, and we're working with an index
3129 that may badly handle TUs, load all the TUs in that DWO as well.
3130 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3131 if (!per_cu->is_debug_types
3132 && per_cu->cu != NULL
3133 && per_cu->cu->dwo_unit != NULL
3134 && dwarf2_per_objfile->index_table != NULL
3135 && dwarf2_per_objfile->index_table->version <= 7
3136 /* DWP files aren't supported yet. */
3137 && get_dwp_file (dwarf2_per_objfile) == NULL)
3138 queue_and_load_all_dwo_tus (per_cu);
3141 process_queue (dwarf2_per_objfile);
3143 /* Age the cache, releasing compilation units that have not
3144 been used recently. */
3145 age_cached_comp_units (dwarf2_per_objfile);
3147 do_cleanups (back_to);
3150 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3151 the objfile from which this CU came. Returns the resulting symbol
3154 static struct compunit_symtab *
3155 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3157 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3159 gdb_assert (dwarf2_per_objfile->using_index);
3160 if (!per_cu->v.quick->compunit_symtab)
3162 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3163 dwarf2_per_objfile);
3164 scoped_restore decrementer = increment_reading_symtab ();
3165 dw2_do_instantiate_symtab (per_cu);
3166 process_cu_includes (dwarf2_per_objfile);
3167 do_cleanups (back_to);
3170 return per_cu->v.quick->compunit_symtab;
3173 /* Return the CU/TU given its index.
3175 This is intended for loops like:
3177 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3178 + dwarf2_per_objfile->n_type_units); ++i)
3180 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3186 static struct dwarf2_per_cu_data *
3187 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3190 if (index >= dwarf2_per_objfile->n_comp_units)
3192 index -= dwarf2_per_objfile->n_comp_units;
3193 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3194 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3197 return dwarf2_per_objfile->all_comp_units[index];
3200 /* Return the CU given its index.
3201 This differs from dw2_get_cutu in that it's for when you know INDEX
3204 static struct dwarf2_per_cu_data *
3205 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3207 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3209 return dwarf2_per_objfile->all_comp_units[index];
3212 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3213 objfile_obstack, and constructed with the specified field
3216 static dwarf2_per_cu_data *
3217 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3218 struct dwarf2_section_info *section,
3220 sect_offset sect_off, ULONGEST length)
3222 struct objfile *objfile = dwarf2_per_objfile->objfile;
3223 dwarf2_per_cu_data *the_cu
3224 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3225 struct dwarf2_per_cu_data);
3226 the_cu->sect_off = sect_off;
3227 the_cu->length = length;
3228 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3229 the_cu->section = section;
3230 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3231 struct dwarf2_per_cu_quick_data);
3232 the_cu->is_dwz = is_dwz;
3236 /* A helper for create_cus_from_index that handles a given list of
3240 create_cus_from_index_list (struct objfile *objfile,
3241 const gdb_byte *cu_list, offset_type n_elements,
3242 struct dwarf2_section_info *section,
3247 struct dwarf2_per_objfile *dwarf2_per_objfile
3248 = get_dwarf2_per_objfile (objfile);
3250 for (i = 0; i < n_elements; i += 2)
3252 gdb_static_assert (sizeof (ULONGEST) >= 8);
3254 sect_offset sect_off
3255 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3256 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3259 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3260 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3265 /* Read the CU list from the mapped index, and use it to create all
3266 the CU objects for this objfile. */
3269 create_cus_from_index (struct objfile *objfile,
3270 const gdb_byte *cu_list, offset_type cu_list_elements,
3271 const gdb_byte *dwz_list, offset_type dwz_elements)
3273 struct dwz_file *dwz;
3274 struct dwarf2_per_objfile *dwarf2_per_objfile
3275 = get_dwarf2_per_objfile (objfile);
3277 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3278 dwarf2_per_objfile->all_comp_units =
3279 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3280 dwarf2_per_objfile->n_comp_units);
3282 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3283 &dwarf2_per_objfile->info, 0, 0);
3285 if (dwz_elements == 0)
3288 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3289 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3290 cu_list_elements / 2);
3293 /* Create the signatured type hash table from the index. */
3296 create_signatured_type_table_from_index (struct objfile *objfile,
3297 struct dwarf2_section_info *section,
3298 const gdb_byte *bytes,
3299 offset_type elements)
3302 htab_t sig_types_hash;
3303 struct dwarf2_per_objfile *dwarf2_per_objfile
3304 = get_dwarf2_per_objfile (objfile);
3306 dwarf2_per_objfile->n_type_units
3307 = dwarf2_per_objfile->n_allocated_type_units
3309 dwarf2_per_objfile->all_type_units =
3310 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3312 sig_types_hash = allocate_signatured_type_table (objfile);
3314 for (i = 0; i < elements; i += 3)
3316 struct signatured_type *sig_type;
3319 cu_offset type_offset_in_tu;
3321 gdb_static_assert (sizeof (ULONGEST) >= 8);
3322 sect_offset sect_off
3323 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3325 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3327 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3330 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3331 struct signatured_type);
3332 sig_type->signature = signature;
3333 sig_type->type_offset_in_tu = type_offset_in_tu;
3334 sig_type->per_cu.is_debug_types = 1;
3335 sig_type->per_cu.section = section;
3336 sig_type->per_cu.sect_off = sect_off;
3337 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3338 sig_type->per_cu.v.quick
3339 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3340 struct dwarf2_per_cu_quick_data);
3342 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3345 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3348 dwarf2_per_objfile->signatured_types = sig_types_hash;
3351 /* Create the signatured type hash table from .debug_names. */
3354 create_signatured_type_table_from_debug_names
3355 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3356 const mapped_debug_names &map,
3357 struct dwarf2_section_info *section,
3358 struct dwarf2_section_info *abbrev_section)
3360 struct objfile *objfile = dwarf2_per_objfile->objfile;
3362 dwarf2_read_section (objfile, section);
3363 dwarf2_read_section (objfile, abbrev_section);
3365 dwarf2_per_objfile->n_type_units
3366 = dwarf2_per_objfile->n_allocated_type_units
3368 dwarf2_per_objfile->all_type_units
3369 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3371 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3373 for (uint32_t i = 0; i < map.tu_count; ++i)
3375 struct signatured_type *sig_type;
3378 cu_offset type_offset_in_tu;
3380 sect_offset sect_off
3381 = (sect_offset) (extract_unsigned_integer
3382 (map.tu_table_reordered + i * map.offset_size,
3384 map.dwarf5_byte_order));
3386 comp_unit_head cu_header;
3387 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3389 section->buffer + to_underlying (sect_off),
3392 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3393 struct signatured_type);
3394 sig_type->signature = cu_header.signature;
3395 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3396 sig_type->per_cu.is_debug_types = 1;
3397 sig_type->per_cu.section = section;
3398 sig_type->per_cu.sect_off = sect_off;
3399 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3400 sig_type->per_cu.v.quick
3401 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3402 struct dwarf2_per_cu_quick_data);
3404 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3407 dwarf2_per_objfile->all_type_units[i] = sig_type;
3410 dwarf2_per_objfile->signatured_types = sig_types_hash;
3413 /* Read the address map data from the mapped index, and use it to
3414 populate the objfile's psymtabs_addrmap. */
3417 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3418 struct mapped_index *index)
3420 struct objfile *objfile = dwarf2_per_objfile->objfile;
3421 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3422 const gdb_byte *iter, *end;
3423 struct addrmap *mutable_map;
3426 auto_obstack temp_obstack;
3428 mutable_map = addrmap_create_mutable (&temp_obstack);
3430 iter = index->address_table.data ();
3431 end = iter + index->address_table.size ();
3433 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3437 ULONGEST hi, lo, cu_index;
3438 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3440 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3442 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3447 complaint (&symfile_complaints,
3448 _(".gdb_index address table has invalid range (%s - %s)"),
3449 hex_string (lo), hex_string (hi));
3453 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3455 complaint (&symfile_complaints,
3456 _(".gdb_index address table has invalid CU number %u"),
3457 (unsigned) cu_index);
3461 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3462 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3463 addrmap_set_empty (mutable_map, lo, hi - 1,
3464 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3467 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3468 &objfile->objfile_obstack);
3471 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3472 populate the objfile's psymtabs_addrmap. */
3475 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3476 struct dwarf2_section_info *section)
3478 struct objfile *objfile = dwarf2_per_objfile->objfile;
3479 bfd *abfd = objfile->obfd;
3480 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3481 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3482 SECT_OFF_TEXT (objfile));
3484 auto_obstack temp_obstack;
3485 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3487 std::unordered_map<sect_offset,
3488 dwarf2_per_cu_data *,
3489 gdb::hash_enum<sect_offset>>
3490 debug_info_offset_to_per_cu;
3491 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3493 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3494 const auto insertpair
3495 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3496 if (!insertpair.second)
3498 warning (_("Section .debug_aranges in %s has duplicate "
3499 "debug_info_offset %u, ignoring .debug_aranges."),
3500 objfile_name (objfile), to_underlying (per_cu->sect_off));
3505 dwarf2_read_section (objfile, section);
3507 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3509 const gdb_byte *addr = section->buffer;
3511 while (addr < section->buffer + section->size)
3513 const gdb_byte *const entry_addr = addr;
3514 unsigned int bytes_read;
3516 const LONGEST entry_length = read_initial_length (abfd, addr,
3520 const gdb_byte *const entry_end = addr + entry_length;
3521 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3522 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3523 if (addr + entry_length > section->buffer + section->size)
3525 warning (_("Section .debug_aranges in %s entry at offset %zu "
3526 "length %s exceeds section length %s, "
3527 "ignoring .debug_aranges."),
3528 objfile_name (objfile), entry_addr - section->buffer,
3529 plongest (bytes_read + entry_length),
3530 pulongest (section->size));
3534 /* The version number. */
3535 const uint16_t version = read_2_bytes (abfd, addr);
3539 warning (_("Section .debug_aranges in %s entry at offset %zu "
3540 "has unsupported version %d, ignoring .debug_aranges."),
3541 objfile_name (objfile), entry_addr - section->buffer,
3546 const uint64_t debug_info_offset
3547 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3548 addr += offset_size;
3549 const auto per_cu_it
3550 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3551 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3553 warning (_("Section .debug_aranges in %s entry at offset %zu "
3554 "debug_info_offset %s does not exists, "
3555 "ignoring .debug_aranges."),
3556 objfile_name (objfile), entry_addr - section->buffer,
3557 pulongest (debug_info_offset));
3560 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3562 const uint8_t address_size = *addr++;
3563 if (address_size < 1 || address_size > 8)
3565 warning (_("Section .debug_aranges in %s entry at offset %zu "
3566 "address_size %u is invalid, ignoring .debug_aranges."),
3567 objfile_name (objfile), entry_addr - section->buffer,
3572 const uint8_t segment_selector_size = *addr++;
3573 if (segment_selector_size != 0)
3575 warning (_("Section .debug_aranges in %s entry at offset %zu "
3576 "segment_selector_size %u is not supported, "
3577 "ignoring .debug_aranges."),
3578 objfile_name (objfile), entry_addr - section->buffer,
3579 segment_selector_size);
3583 /* Must pad to an alignment boundary that is twice the address
3584 size. It is undocumented by the DWARF standard but GCC does
3586 for (size_t padding = ((-(addr - section->buffer))
3587 & (2 * address_size - 1));
3588 padding > 0; padding--)
3591 warning (_("Section .debug_aranges in %s entry at offset %zu "
3592 "padding is not zero, ignoring .debug_aranges."),
3593 objfile_name (objfile), entry_addr - section->buffer);
3599 if (addr + 2 * address_size > entry_end)
3601 warning (_("Section .debug_aranges in %s entry at offset %zu "
3602 "address list is not properly terminated, "
3603 "ignoring .debug_aranges."),
3604 objfile_name (objfile), entry_addr - section->buffer);
3607 ULONGEST start = extract_unsigned_integer (addr, address_size,
3609 addr += address_size;
3610 ULONGEST length = extract_unsigned_integer (addr, address_size,
3612 addr += address_size;
3613 if (start == 0 && length == 0)
3615 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3617 /* Symbol was eliminated due to a COMDAT group. */
3620 ULONGEST end = start + length;
3621 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3622 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3623 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3627 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3628 &objfile->objfile_obstack);
3631 /* The hash function for strings in the mapped index. This is the same as
3632 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3633 implementation. This is necessary because the hash function is tied to the
3634 format of the mapped index file. The hash values do not have to match with
3637 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3640 mapped_index_string_hash (int index_version, const void *p)
3642 const unsigned char *str = (const unsigned char *) p;
3646 while ((c = *str++) != 0)
3648 if (index_version >= 5)
3650 r = r * 67 + c - 113;
3656 /* Find a slot in the mapped index INDEX for the object named NAME.
3657 If NAME is found, set *VEC_OUT to point to the CU vector in the
3658 constant pool and return true. If NAME cannot be found, return
3662 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3663 offset_type **vec_out)
3666 offset_type slot, step;
3667 int (*cmp) (const char *, const char *);
3669 gdb::unique_xmalloc_ptr<char> without_params;
3670 if (current_language->la_language == language_cplus
3671 || current_language->la_language == language_fortran
3672 || current_language->la_language == language_d)
3674 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3677 if (strchr (name, '(') != NULL)
3679 without_params = cp_remove_params (name);
3681 if (without_params != NULL)
3682 name = without_params.get ();
3686 /* Index version 4 did not support case insensitive searches. But the
3687 indices for case insensitive languages are built in lowercase, therefore
3688 simulate our NAME being searched is also lowercased. */
3689 hash = mapped_index_string_hash ((index->version == 4
3690 && case_sensitivity == case_sensitive_off
3691 ? 5 : index->version),
3694 slot = hash & (index->symbol_table.size () - 1);
3695 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3696 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3702 const auto &bucket = index->symbol_table[slot];
3703 if (bucket.name == 0 && bucket.vec == 0)
3706 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3707 if (!cmp (name, str))
3709 *vec_out = (offset_type *) (index->constant_pool
3710 + MAYBE_SWAP (bucket.vec));
3714 slot = (slot + step) & (index->symbol_table.size () - 1);
3718 /* A helper function that reads the .gdb_index from SECTION and fills
3719 in MAP. FILENAME is the name of the file containing the section;
3720 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3721 ok to use deprecated sections.
3723 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3724 out parameters that are filled in with information about the CU and
3725 TU lists in the section.
3727 Returns 1 if all went well, 0 otherwise. */
3730 read_index_from_section (struct objfile *objfile,
3731 const char *filename,
3733 struct dwarf2_section_info *section,
3734 struct mapped_index *map,
3735 const gdb_byte **cu_list,
3736 offset_type *cu_list_elements,
3737 const gdb_byte **types_list,
3738 offset_type *types_list_elements)
3740 const gdb_byte *addr;
3741 offset_type version;
3742 offset_type *metadata;
3745 if (dwarf2_section_empty_p (section))
3748 /* Older elfutils strip versions could keep the section in the main
3749 executable while splitting it for the separate debug info file. */
3750 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3753 dwarf2_read_section (objfile, section);
3755 addr = section->buffer;
3756 /* Version check. */
3757 version = MAYBE_SWAP (*(offset_type *) addr);
3758 /* Versions earlier than 3 emitted every copy of a psymbol. This
3759 causes the index to behave very poorly for certain requests. Version 3
3760 contained incomplete addrmap. So, it seems better to just ignore such
3764 static int warning_printed = 0;
3765 if (!warning_printed)
3767 warning (_("Skipping obsolete .gdb_index section in %s."),
3769 warning_printed = 1;
3773 /* Index version 4 uses a different hash function than index version
3776 Versions earlier than 6 did not emit psymbols for inlined
3777 functions. Using these files will cause GDB not to be able to
3778 set breakpoints on inlined functions by name, so we ignore these
3779 indices unless the user has done
3780 "set use-deprecated-index-sections on". */
3781 if (version < 6 && !deprecated_ok)
3783 static int warning_printed = 0;
3784 if (!warning_printed)
3787 Skipping deprecated .gdb_index section in %s.\n\
3788 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3789 to use the section anyway."),
3791 warning_printed = 1;
3795 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3796 of the TU (for symbols coming from TUs),
3797 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3798 Plus gold-generated indices can have duplicate entries for global symbols,
3799 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3800 These are just performance bugs, and we can't distinguish gdb-generated
3801 indices from gold-generated ones, so issue no warning here. */
3803 /* Indexes with higher version than the one supported by GDB may be no
3804 longer backward compatible. */
3808 map->version = version;
3809 map->total_size = section->size;
3811 metadata = (offset_type *) (addr + sizeof (offset_type));
3814 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3815 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3819 *types_list = addr + MAYBE_SWAP (metadata[i]);
3820 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3821 - MAYBE_SWAP (metadata[i]))
3825 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3826 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3828 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3831 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3832 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3834 = gdb::array_view<mapped_index::symbol_table_slot>
3835 ((mapped_index::symbol_table_slot *) symbol_table,
3836 (mapped_index::symbol_table_slot *) symbol_table_end);
3839 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3844 /* Read .gdb_index. If everything went ok, initialize the "quick"
3845 elements of all the CUs and return 1. Otherwise, return 0. */
3848 dwarf2_read_index (struct objfile *objfile)
3850 struct mapped_index local_map, *map;
3851 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3852 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3853 struct dwz_file *dwz;
3854 struct dwarf2_per_objfile *dwarf2_per_objfile
3855 = get_dwarf2_per_objfile (objfile);
3857 if (!read_index_from_section (objfile, objfile_name (objfile),
3858 use_deprecated_index_sections,
3859 &dwarf2_per_objfile->gdb_index, &local_map,
3860 &cu_list, &cu_list_elements,
3861 &types_list, &types_list_elements))
3864 /* Don't use the index if it's empty. */
3865 if (local_map.symbol_table.empty ())
3868 /* If there is a .dwz file, read it so we can get its CU list as
3870 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3873 struct mapped_index dwz_map;
3874 const gdb_byte *dwz_types_ignore;
3875 offset_type dwz_types_elements_ignore;
3877 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3879 &dwz->gdb_index, &dwz_map,
3880 &dwz_list, &dwz_list_elements,
3882 &dwz_types_elements_ignore))
3884 warning (_("could not read '.gdb_index' section from %s; skipping"),
3885 bfd_get_filename (dwz->dwz_bfd));
3890 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3893 if (types_list_elements)
3895 struct dwarf2_section_info *section;
3897 /* We can only handle a single .debug_types when we have an
3899 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3902 section = VEC_index (dwarf2_section_info_def,
3903 dwarf2_per_objfile->types, 0);
3905 create_signatured_type_table_from_index (objfile, section, types_list,
3906 types_list_elements);
3909 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3911 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3912 map = new (map) mapped_index ();
3915 dwarf2_per_objfile->index_table = map;
3916 dwarf2_per_objfile->using_index = 1;
3917 dwarf2_per_objfile->quick_file_names_table =
3918 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3923 /* die_reader_func for dw2_get_file_names. */
3926 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3927 const gdb_byte *info_ptr,
3928 struct die_info *comp_unit_die,
3932 struct dwarf2_cu *cu = reader->cu;
3933 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3934 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
3935 struct objfile *objfile = dwarf2_per_objfile->objfile;
3936 struct dwarf2_per_cu_data *lh_cu;
3937 struct attribute *attr;
3940 struct quick_file_names *qfn;
3942 gdb_assert (! this_cu->is_debug_types);
3944 /* Our callers never want to match partial units -- instead they
3945 will match the enclosing full CU. */
3946 if (comp_unit_die->tag == DW_TAG_partial_unit)
3948 this_cu->v.quick->no_file_data = 1;
3956 sect_offset line_offset {};
3958 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3961 struct quick_file_names find_entry;
3963 line_offset = (sect_offset) DW_UNSND (attr);
3965 /* We may have already read in this line header (TU line header sharing).
3966 If we have we're done. */
3967 find_entry.hash.dwo_unit = cu->dwo_unit;
3968 find_entry.hash.line_sect_off = line_offset;
3969 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3970 &find_entry, INSERT);
3973 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3977 lh = dwarf_decode_line_header (line_offset, cu);
3981 lh_cu->v.quick->no_file_data = 1;
3985 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3986 qfn->hash.dwo_unit = cu->dwo_unit;
3987 qfn->hash.line_sect_off = line_offset;
3988 gdb_assert (slot != NULL);
3991 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3993 qfn->num_file_names = lh->file_names.size ();
3995 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3996 for (i = 0; i < lh->file_names.size (); ++i)
3997 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3998 qfn->real_names = NULL;
4000 lh_cu->v.quick->file_names = qfn;
4003 /* A helper for the "quick" functions which attempts to read the line
4004 table for THIS_CU. */
4006 static struct quick_file_names *
4007 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4009 /* This should never be called for TUs. */
4010 gdb_assert (! this_cu->is_debug_types);
4011 /* Nor type unit groups. */
4012 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4014 if (this_cu->v.quick->file_names != NULL)
4015 return this_cu->v.quick->file_names;
4016 /* If we know there is no line data, no point in looking again. */
4017 if (this_cu->v.quick->no_file_data)
4020 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4022 if (this_cu->v.quick->no_file_data)
4024 return this_cu->v.quick->file_names;
4027 /* A helper for the "quick" functions which computes and caches the
4028 real path for a given file name from the line table. */
4031 dw2_get_real_path (struct objfile *objfile,
4032 struct quick_file_names *qfn, int index)
4034 if (qfn->real_names == NULL)
4035 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4036 qfn->num_file_names, const char *);
4038 if (qfn->real_names[index] == NULL)
4039 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4041 return qfn->real_names[index];
4044 static struct symtab *
4045 dw2_find_last_source_symtab (struct objfile *objfile)
4047 struct dwarf2_per_objfile *dwarf2_per_objfile
4048 = get_dwarf2_per_objfile (objfile);
4049 int index = dwarf2_per_objfile->n_comp_units - 1;
4050 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4051 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4056 return compunit_primary_filetab (cust);
4059 /* Traversal function for dw2_forget_cached_source_info. */
4062 dw2_free_cached_file_names (void **slot, void *info)
4064 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4066 if (file_data->real_names)
4070 for (i = 0; i < file_data->num_file_names; ++i)
4072 xfree ((void*) file_data->real_names[i]);
4073 file_data->real_names[i] = NULL;
4081 dw2_forget_cached_source_info (struct objfile *objfile)
4083 struct dwarf2_per_objfile *dwarf2_per_objfile
4084 = get_dwarf2_per_objfile (objfile);
4086 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4087 dw2_free_cached_file_names, NULL);
4090 /* Helper function for dw2_map_symtabs_matching_filename that expands
4091 the symtabs and calls the iterator. */
4094 dw2_map_expand_apply (struct objfile *objfile,
4095 struct dwarf2_per_cu_data *per_cu,
4096 const char *name, const char *real_path,
4097 gdb::function_view<bool (symtab *)> callback)
4099 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4101 /* Don't visit already-expanded CUs. */
4102 if (per_cu->v.quick->compunit_symtab)
4105 /* This may expand more than one symtab, and we want to iterate over
4107 dw2_instantiate_symtab (per_cu);
4109 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4110 last_made, callback);
4113 /* Implementation of the map_symtabs_matching_filename method. */
4116 dw2_map_symtabs_matching_filename
4117 (struct objfile *objfile, const char *name, const char *real_path,
4118 gdb::function_view<bool (symtab *)> callback)
4121 const char *name_basename = lbasename (name);
4122 struct dwarf2_per_objfile *dwarf2_per_objfile
4123 = get_dwarf2_per_objfile (objfile);
4125 /* The rule is CUs specify all the files, including those used by
4126 any TU, so there's no need to scan TUs here. */
4128 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4131 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4132 struct quick_file_names *file_data;
4134 /* We only need to look at symtabs not already expanded. */
4135 if (per_cu->v.quick->compunit_symtab)
4138 file_data = dw2_get_file_names (per_cu);
4139 if (file_data == NULL)
4142 for (j = 0; j < file_data->num_file_names; ++j)
4144 const char *this_name = file_data->file_names[j];
4145 const char *this_real_name;
4147 if (compare_filenames_for_search (this_name, name))
4149 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4155 /* Before we invoke realpath, which can get expensive when many
4156 files are involved, do a quick comparison of the basenames. */
4157 if (! basenames_may_differ
4158 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4161 this_real_name = dw2_get_real_path (objfile, file_data, j);
4162 if (compare_filenames_for_search (this_real_name, name))
4164 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4170 if (real_path != NULL)
4172 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4173 gdb_assert (IS_ABSOLUTE_PATH (name));
4174 if (this_real_name != NULL
4175 && FILENAME_CMP (real_path, this_real_name) == 0)
4177 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4189 /* Struct used to manage iterating over all CUs looking for a symbol. */
4191 struct dw2_symtab_iterator
4193 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4194 struct dwarf2_per_objfile *dwarf2_per_objfile;
4195 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4196 int want_specific_block;
4197 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4198 Unused if !WANT_SPECIFIC_BLOCK. */
4200 /* The kind of symbol we're looking for. */
4202 /* The list of CUs from the index entry of the symbol,
4203 or NULL if not found. */
4205 /* The next element in VEC to look at. */
4207 /* The number of elements in VEC, or zero if there is no match. */
4209 /* Have we seen a global version of the symbol?
4210 If so we can ignore all further global instances.
4211 This is to work around gold/15646, inefficient gold-generated
4216 /* Initialize the index symtab iterator ITER.
4217 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4218 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4221 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4222 struct dwarf2_per_objfile *dwarf2_per_objfile,
4223 int want_specific_block,
4228 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4229 iter->want_specific_block = want_specific_block;
4230 iter->block_index = block_index;
4231 iter->domain = domain;
4233 iter->global_seen = 0;
4235 mapped_index *index = dwarf2_per_objfile->index_table;
4237 /* index is NULL if OBJF_READNOW. */
4238 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4239 iter->length = MAYBE_SWAP (*iter->vec);
4247 /* Return the next matching CU or NULL if there are no more. */
4249 static struct dwarf2_per_cu_data *
4250 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4252 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4254 for ( ; iter->next < iter->length; ++iter->next)
4256 offset_type cu_index_and_attrs =
4257 MAYBE_SWAP (iter->vec[iter->next + 1]);
4258 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4259 struct dwarf2_per_cu_data *per_cu;
4260 int want_static = iter->block_index != GLOBAL_BLOCK;
4261 /* This value is only valid for index versions >= 7. */
4262 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4263 gdb_index_symbol_kind symbol_kind =
4264 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4265 /* Only check the symbol attributes if they're present.
4266 Indices prior to version 7 don't record them,
4267 and indices >= 7 may elide them for certain symbols
4268 (gold does this). */
4270 (dwarf2_per_objfile->index_table->version >= 7
4271 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4273 /* Don't crash on bad data. */
4274 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4275 + dwarf2_per_objfile->n_type_units))
4277 complaint (&symfile_complaints,
4278 _(".gdb_index entry has bad CU index"
4280 objfile_name (dwarf2_per_objfile->objfile));
4284 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4286 /* Skip if already read in. */
4287 if (per_cu->v.quick->compunit_symtab)
4290 /* Check static vs global. */
4293 if (iter->want_specific_block
4294 && want_static != is_static)
4296 /* Work around gold/15646. */
4297 if (!is_static && iter->global_seen)
4300 iter->global_seen = 1;
4303 /* Only check the symbol's kind if it has one. */
4306 switch (iter->domain)
4309 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4310 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4311 /* Some types are also in VAR_DOMAIN. */
4312 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4316 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4320 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4335 static struct compunit_symtab *
4336 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4337 const char *name, domain_enum domain)
4339 struct compunit_symtab *stab_best = NULL;
4340 struct dwarf2_per_objfile *dwarf2_per_objfile
4341 = get_dwarf2_per_objfile (objfile);
4343 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4345 struct dw2_symtab_iterator iter;
4346 struct dwarf2_per_cu_data *per_cu;
4348 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4350 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4352 struct symbol *sym, *with_opaque = NULL;
4353 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4354 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4355 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4357 sym = block_find_symbol (block, name, domain,
4358 block_find_non_opaque_type_preferred,
4361 /* Some caution must be observed with overloaded functions
4362 and methods, since the index will not contain any overload
4363 information (but NAME might contain it). */
4366 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4368 if (with_opaque != NULL
4369 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4372 /* Keep looking through other CUs. */
4379 dw2_print_stats (struct objfile *objfile)
4381 struct dwarf2_per_objfile *dwarf2_per_objfile
4382 = get_dwarf2_per_objfile (objfile);
4383 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4386 for (int i = 0; i < total; ++i)
4388 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4390 if (!per_cu->v.quick->compunit_symtab)
4393 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4394 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4397 /* This dumps minimal information about the index.
4398 It is called via "mt print objfiles".
4399 One use is to verify .gdb_index has been loaded by the
4400 gdb.dwarf2/gdb-index.exp testcase. */
4403 dw2_dump (struct objfile *objfile)
4405 struct dwarf2_per_objfile *dwarf2_per_objfile
4406 = get_dwarf2_per_objfile (objfile);
4408 gdb_assert (dwarf2_per_objfile->using_index);
4409 printf_filtered (".gdb_index:");
4410 if (dwarf2_per_objfile->index_table != NULL)
4412 printf_filtered (" version %d\n",
4413 dwarf2_per_objfile->index_table->version);
4416 printf_filtered (" faked for \"readnow\"\n");
4417 printf_filtered ("\n");
4421 dw2_relocate (struct objfile *objfile,
4422 const struct section_offsets *new_offsets,
4423 const struct section_offsets *delta)
4425 /* There's nothing to relocate here. */
4429 dw2_expand_symtabs_for_function (struct objfile *objfile,
4430 const char *func_name)
4432 struct dwarf2_per_objfile *dwarf2_per_objfile
4433 = get_dwarf2_per_objfile (objfile);
4435 struct dw2_symtab_iterator iter;
4436 struct dwarf2_per_cu_data *per_cu;
4438 /* Note: It doesn't matter what we pass for block_index here. */
4439 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4442 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4443 dw2_instantiate_symtab (per_cu);
4448 dw2_expand_all_symtabs (struct objfile *objfile)
4450 struct dwarf2_per_objfile *dwarf2_per_objfile
4451 = get_dwarf2_per_objfile (objfile);
4452 int total_units = (dwarf2_per_objfile->n_comp_units
4453 + dwarf2_per_objfile->n_type_units);
4455 for (int i = 0; i < total_units; ++i)
4457 struct dwarf2_per_cu_data *per_cu
4458 = dw2_get_cutu (dwarf2_per_objfile, i);
4460 dw2_instantiate_symtab (per_cu);
4465 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4466 const char *fullname)
4468 struct dwarf2_per_objfile *dwarf2_per_objfile
4469 = get_dwarf2_per_objfile (objfile);
4471 /* We don't need to consider type units here.
4472 This is only called for examining code, e.g. expand_line_sal.
4473 There can be an order of magnitude (or more) more type units
4474 than comp units, and we avoid them if we can. */
4476 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4479 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4480 struct quick_file_names *file_data;
4482 /* We only need to look at symtabs not already expanded. */
4483 if (per_cu->v.quick->compunit_symtab)
4486 file_data = dw2_get_file_names (per_cu);
4487 if (file_data == NULL)
4490 for (j = 0; j < file_data->num_file_names; ++j)
4492 const char *this_fullname = file_data->file_names[j];
4494 if (filename_cmp (this_fullname, fullname) == 0)
4496 dw2_instantiate_symtab (per_cu);
4504 dw2_map_matching_symbols (struct objfile *objfile,
4505 const char * name, domain_enum domain,
4507 int (*callback) (struct block *,
4508 struct symbol *, void *),
4509 void *data, symbol_name_match_type match,
4510 symbol_compare_ftype *ordered_compare)
4512 /* Currently unimplemented; used for Ada. The function can be called if the
4513 current language is Ada for a non-Ada objfile using GNU index. As Ada
4514 does not look for non-Ada symbols this function should just return. */
4517 /* Symbol name matcher for .gdb_index names.
4519 Symbol names in .gdb_index have a few particularities:
4521 - There's no indication of which is the language of each symbol.
4523 Since each language has its own symbol name matching algorithm,
4524 and we don't know which language is the right one, we must match
4525 each symbol against all languages. This would be a potential
4526 performance problem if it were not mitigated by the
4527 mapped_index::name_components lookup table, which significantly
4528 reduces the number of times we need to call into this matcher,
4529 making it a non-issue.
4531 - Symbol names in the index have no overload (parameter)
4532 information. I.e., in C++, "foo(int)" and "foo(long)" both
4533 appear as "foo" in the index, for example.
4535 This means that the lookup names passed to the symbol name
4536 matcher functions must have no parameter information either
4537 because (e.g.) symbol search name "foo" does not match
4538 lookup-name "foo(int)" [while swapping search name for lookup
4541 class gdb_index_symbol_name_matcher
4544 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4545 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4547 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4548 Returns true if any matcher matches. */
4549 bool matches (const char *symbol_name);
4552 /* A reference to the lookup name we're matching against. */
4553 const lookup_name_info &m_lookup_name;
4555 /* A vector holding all the different symbol name matchers, for all
4557 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4560 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4561 (const lookup_name_info &lookup_name)
4562 : m_lookup_name (lookup_name)
4564 /* Prepare the vector of comparison functions upfront, to avoid
4565 doing the same work for each symbol. Care is taken to avoid
4566 matching with the same matcher more than once if/when multiple
4567 languages use the same matcher function. */
4568 auto &matchers = m_symbol_name_matcher_funcs;
4569 matchers.reserve (nr_languages);
4571 matchers.push_back (default_symbol_name_matcher);
4573 for (int i = 0; i < nr_languages; i++)
4575 const language_defn *lang = language_def ((enum language) i);
4576 if (lang->la_get_symbol_name_matcher != NULL)
4578 symbol_name_matcher_ftype *name_matcher
4579 = lang->la_get_symbol_name_matcher (m_lookup_name);
4581 /* Don't insert the same comparison routine more than once.
4582 Note that we do this linear walk instead of a cheaper
4583 sorted insert, or use a std::set or something like that,
4584 because relative order of function addresses is not
4585 stable. This is not a problem in practice because the
4586 number of supported languages is low, and the cost here
4587 is tiny compared to the number of searches we'll do
4588 afterwards using this object. */
4589 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4591 matchers.push_back (name_matcher);
4597 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4599 for (auto matches_name : m_symbol_name_matcher_funcs)
4600 if (matches_name (symbol_name, m_lookup_name, NULL))
4606 /* Starting from a search name, return the string that finds the upper
4607 bound of all strings that start with SEARCH_NAME in a sorted name
4608 list. Returns the empty string to indicate that the upper bound is
4609 the end of the list. */
4612 make_sort_after_prefix_name (const char *search_name)
4614 /* When looking to complete "func", we find the upper bound of all
4615 symbols that start with "func" by looking for where we'd insert
4616 the closest string that would follow "func" in lexicographical
4617 order. Usually, that's "func"-with-last-character-incremented,
4618 i.e. "fund". Mind non-ASCII characters, though. Usually those
4619 will be UTF-8 multi-byte sequences, but we can't be certain.
4620 Especially mind the 0xff character, which is a valid character in
4621 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4622 rule out compilers allowing it in identifiers. Note that
4623 conveniently, strcmp/strcasecmp are specified to compare
4624 characters interpreted as unsigned char. So what we do is treat
4625 the whole string as a base 256 number composed of a sequence of
4626 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4627 to 0, and carries 1 to the following more-significant position.
4628 If the very first character in SEARCH_NAME ends up incremented
4629 and carries/overflows, then the upper bound is the end of the
4630 list. The string after the empty string is also the empty
4633 Some examples of this operation:
4635 SEARCH_NAME => "+1" RESULT
4639 "\xff" "a" "\xff" => "\xff" "b"
4644 Then, with these symbols for example:
4650 completing "func" looks for symbols between "func" and
4651 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4652 which finds "func" and "func1", but not "fund".
4656 funcÿ (Latin1 'ÿ' [0xff])
4660 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4661 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4665 ÿÿ (Latin1 'ÿ' [0xff])
4668 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4669 the end of the list.
4671 std::string after = search_name;
4672 while (!after.empty () && (unsigned char) after.back () == 0xff)
4674 if (!after.empty ())
4675 after.back () = (unsigned char) after.back () + 1;
4679 /* See declaration. */
4681 std::pair<std::vector<name_component>::const_iterator,
4682 std::vector<name_component>::const_iterator>
4683 mapped_index_base::find_name_components_bounds
4684 (const lookup_name_info &lookup_name_without_params) const
4687 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4690 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4692 /* Comparison function object for lower_bound that matches against a
4693 given symbol name. */
4694 auto lookup_compare_lower = [&] (const name_component &elem,
4697 const char *elem_qualified = this->symbol_name_at (elem.idx);
4698 const char *elem_name = elem_qualified + elem.name_offset;
4699 return name_cmp (elem_name, name) < 0;
4702 /* Comparison function object for upper_bound that matches against a
4703 given symbol name. */
4704 auto lookup_compare_upper = [&] (const char *name,
4705 const name_component &elem)
4707 const char *elem_qualified = this->symbol_name_at (elem.idx);
4708 const char *elem_name = elem_qualified + elem.name_offset;
4709 return name_cmp (name, elem_name) < 0;
4712 auto begin = this->name_components.begin ();
4713 auto end = this->name_components.end ();
4715 /* Find the lower bound. */
4718 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4721 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4724 /* Find the upper bound. */
4727 if (lookup_name_without_params.completion_mode ())
4729 /* In completion mode, we want UPPER to point past all
4730 symbols names that have the same prefix. I.e., with
4731 these symbols, and completing "func":
4733 function << lower bound
4735 other_function << upper bound
4737 We find the upper bound by looking for the insertion
4738 point of "func"-with-last-character-incremented,
4740 std::string after = make_sort_after_prefix_name (cplus);
4743 return std::lower_bound (lower, end, after.c_str (),
4744 lookup_compare_lower);
4747 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4750 return {lower, upper};
4753 /* See declaration. */
4756 mapped_index_base::build_name_components ()
4758 if (!this->name_components.empty ())
4761 this->name_components_casing = case_sensitivity;
4763 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4765 /* The code below only knows how to break apart components of C++
4766 symbol names (and other languages that use '::' as
4767 namespace/module separator). If we add support for wild matching
4768 to some language that uses some other operator (E.g., Ada, Go and
4769 D use '.'), then we'll need to try splitting the symbol name
4770 according to that language too. Note that Ada does support wild
4771 matching, but doesn't currently support .gdb_index. */
4772 auto count = this->symbol_name_count ();
4773 for (offset_type idx = 0; idx < count; idx++)
4775 if (this->symbol_name_slot_invalid (idx))
4778 const char *name = this->symbol_name_at (idx);
4780 /* Add each name component to the name component table. */
4781 unsigned int previous_len = 0;
4782 for (unsigned int current_len = cp_find_first_component (name);
4783 name[current_len] != '\0';
4784 current_len += cp_find_first_component (name + current_len))
4786 gdb_assert (name[current_len] == ':');
4787 this->name_components.push_back ({previous_len, idx});
4788 /* Skip the '::'. */
4790 previous_len = current_len;
4792 this->name_components.push_back ({previous_len, idx});
4795 /* Sort name_components elements by name. */
4796 auto name_comp_compare = [&] (const name_component &left,
4797 const name_component &right)
4799 const char *left_qualified = this->symbol_name_at (left.idx);
4800 const char *right_qualified = this->symbol_name_at (right.idx);
4802 const char *left_name = left_qualified + left.name_offset;
4803 const char *right_name = right_qualified + right.name_offset;
4805 return name_cmp (left_name, right_name) < 0;
4808 std::sort (this->name_components.begin (),
4809 this->name_components.end (),
4813 /* Helper for dw2_expand_symtabs_matching that works with a
4814 mapped_index_base instead of the containing objfile. This is split
4815 to a separate function in order to be able to unit test the
4816 name_components matching using a mock mapped_index_base. For each
4817 symbol name that matches, calls MATCH_CALLBACK, passing it the
4818 symbol's index in the mapped_index_base symbol table. */
4821 dw2_expand_symtabs_matching_symbol
4822 (mapped_index_base &index,
4823 const lookup_name_info &lookup_name_in,
4824 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4825 enum search_domain kind,
4826 gdb::function_view<void (offset_type)> match_callback)
4828 lookup_name_info lookup_name_without_params
4829 = lookup_name_in.make_ignore_params ();
4830 gdb_index_symbol_name_matcher lookup_name_matcher
4831 (lookup_name_without_params);
4833 /* Build the symbol name component sorted vector, if we haven't
4835 index.build_name_components ();
4837 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4839 /* Now for each symbol name in range, check to see if we have a name
4840 match, and if so, call the MATCH_CALLBACK callback. */
4842 /* The same symbol may appear more than once in the range though.
4843 E.g., if we're looking for symbols that complete "w", and we have
4844 a symbol named "w1::w2", we'll find the two name components for
4845 that same symbol in the range. To be sure we only call the
4846 callback once per symbol, we first collect the symbol name
4847 indexes that matched in a temporary vector and ignore
4849 std::vector<offset_type> matches;
4850 matches.reserve (std::distance (bounds.first, bounds.second));
4852 for (; bounds.first != bounds.second; ++bounds.first)
4854 const char *qualified = index.symbol_name_at (bounds.first->idx);
4856 if (!lookup_name_matcher.matches (qualified)
4857 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4860 matches.push_back (bounds.first->idx);
4863 std::sort (matches.begin (), matches.end ());
4865 /* Finally call the callback, once per match. */
4867 for (offset_type idx : matches)
4871 match_callback (idx);
4876 /* Above we use a type wider than idx's for 'prev', since 0 and
4877 (offset_type)-1 are both possible values. */
4878 static_assert (sizeof (prev) > sizeof (offset_type), "");
4883 namespace selftests { namespace dw2_expand_symtabs_matching {
4885 /* A mock .gdb_index/.debug_names-like name index table, enough to
4886 exercise dw2_expand_symtabs_matching_symbol, which works with the
4887 mapped_index_base interface. Builds an index from the symbol list
4888 passed as parameter to the constructor. */
4889 class mock_mapped_index : public mapped_index_base
4892 mock_mapped_index (gdb::array_view<const char *> symbols)
4893 : m_symbol_table (symbols)
4896 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4898 /* Return the number of names in the symbol table. */
4899 virtual size_t symbol_name_count () const
4901 return m_symbol_table.size ();
4904 /* Get the name of the symbol at IDX in the symbol table. */
4905 virtual const char *symbol_name_at (offset_type idx) const
4907 return m_symbol_table[idx];
4911 gdb::array_view<const char *> m_symbol_table;
4914 /* Convenience function that converts a NULL pointer to a "<null>"
4915 string, to pass to print routines. */
4918 string_or_null (const char *str)
4920 return str != NULL ? str : "<null>";
4923 /* Check if a lookup_name_info built from
4924 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4925 index. EXPECTED_LIST is the list of expected matches, in expected
4926 matching order. If no match expected, then an empty list is
4927 specified. Returns true on success. On failure prints a warning
4928 indicating the file:line that failed, and returns false. */
4931 check_match (const char *file, int line,
4932 mock_mapped_index &mock_index,
4933 const char *name, symbol_name_match_type match_type,
4934 bool completion_mode,
4935 std::initializer_list<const char *> expected_list)
4937 lookup_name_info lookup_name (name, match_type, completion_mode);
4939 bool matched = true;
4941 auto mismatch = [&] (const char *expected_str,
4944 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4945 "expected=\"%s\", got=\"%s\"\n"),
4947 (match_type == symbol_name_match_type::FULL
4949 name, string_or_null (expected_str), string_or_null (got));
4953 auto expected_it = expected_list.begin ();
4954 auto expected_end = expected_list.end ();
4956 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4958 [&] (offset_type idx)
4960 const char *matched_name = mock_index.symbol_name_at (idx);
4961 const char *expected_str
4962 = expected_it == expected_end ? NULL : *expected_it++;
4964 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4965 mismatch (expected_str, matched_name);
4968 const char *expected_str
4969 = expected_it == expected_end ? NULL : *expected_it++;
4970 if (expected_str != NULL)
4971 mismatch (expected_str, NULL);
4976 /* The symbols added to the mock mapped_index for testing (in
4978 static const char *test_symbols[] = {
4987 "ns2::tmpl<int>::foo2",
4988 "(anonymous namespace)::A::B::C",
4990 /* These are used to check that the increment-last-char in the
4991 matching algorithm for completion doesn't match "t1_fund" when
4992 completing "t1_func". */
4998 /* A UTF-8 name with multi-byte sequences to make sure that
4999 cp-name-parser understands this as a single identifier ("função"
5000 is "function" in PT). */
5003 /* \377 (0xff) is Latin1 'ÿ'. */
5006 /* \377 (0xff) is Latin1 'ÿ'. */
5010 /* A name with all sorts of complications. Starts with "z" to make
5011 it easier for the completion tests below. */
5012 #define Z_SYM_NAME \
5013 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5014 "::tuple<(anonymous namespace)::ui*, " \
5015 "std::default_delete<(anonymous namespace)::ui>, void>"
5020 /* Returns true if the mapped_index_base::find_name_component_bounds
5021 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5022 in completion mode. */
5025 check_find_bounds_finds (mapped_index_base &index,
5026 const char *search_name,
5027 gdb::array_view<const char *> expected_syms)
5029 lookup_name_info lookup_name (search_name,
5030 symbol_name_match_type::FULL, true);
5032 auto bounds = index.find_name_components_bounds (lookup_name);
5034 size_t distance = std::distance (bounds.first, bounds.second);
5035 if (distance != expected_syms.size ())
5038 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5040 auto nc_elem = bounds.first + exp_elem;
5041 const char *qualified = index.symbol_name_at (nc_elem->idx);
5042 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5049 /* Test the lower-level mapped_index::find_name_component_bounds
5053 test_mapped_index_find_name_component_bounds ()
5055 mock_mapped_index mock_index (test_symbols);
5057 mock_index.build_name_components ();
5059 /* Test the lower-level mapped_index::find_name_component_bounds
5060 method in completion mode. */
5062 static const char *expected_syms[] = {
5067 SELF_CHECK (check_find_bounds_finds (mock_index,
5068 "t1_func", expected_syms));
5071 /* Check that the increment-last-char in the name matching algorithm
5072 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5074 static const char *expected_syms1[] = {
5078 SELF_CHECK (check_find_bounds_finds (mock_index,
5079 "\377", expected_syms1));
5081 static const char *expected_syms2[] = {
5084 SELF_CHECK (check_find_bounds_finds (mock_index,
5085 "\377\377", expected_syms2));
5089 /* Test dw2_expand_symtabs_matching_symbol. */
5092 test_dw2_expand_symtabs_matching_symbol ()
5094 mock_mapped_index mock_index (test_symbols);
5096 /* We let all tests run until the end even if some fails, for debug
5098 bool any_mismatch = false;
5100 /* Create the expected symbols list (an initializer_list). Needed
5101 because lists have commas, and we need to pass them to CHECK,
5102 which is a macro. */
5103 #define EXPECT(...) { __VA_ARGS__ }
5105 /* Wrapper for check_match that passes down the current
5106 __FILE__/__LINE__. */
5107 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5108 any_mismatch |= !check_match (__FILE__, __LINE__, \
5110 NAME, MATCH_TYPE, COMPLETION_MODE, \
5113 /* Identity checks. */
5114 for (const char *sym : test_symbols)
5116 /* Should be able to match all existing symbols. */
5117 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5120 /* Should be able to match all existing symbols with
5122 std::string with_params = std::string (sym) + "(int)";
5123 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5126 /* Should be able to match all existing symbols with
5127 parameters and qualifiers. */
5128 with_params = std::string (sym) + " ( int ) const";
5129 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5132 /* This should really find sym, but cp-name-parser.y doesn't
5133 know about lvalue/rvalue qualifiers yet. */
5134 with_params = std::string (sym) + " ( int ) &&";
5135 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5139 /* Check that the name matching algorithm for completion doesn't get
5140 confused with Latin1 'ÿ' / 0xff. */
5142 static const char str[] = "\377";
5143 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5144 EXPECT ("\377", "\377\377123"));
5147 /* Check that the increment-last-char in the matching algorithm for
5148 completion doesn't match "t1_fund" when completing "t1_func". */
5150 static const char str[] = "t1_func";
5151 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5152 EXPECT ("t1_func", "t1_func1"));
5155 /* Check that completion mode works at each prefix of the expected
5158 static const char str[] = "function(int)";
5159 size_t len = strlen (str);
5162 for (size_t i = 1; i < len; i++)
5164 lookup.assign (str, i);
5165 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5166 EXPECT ("function"));
5170 /* While "w" is a prefix of both components, the match function
5171 should still only be called once. */
5173 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5175 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5179 /* Same, with a "complicated" symbol. */
5181 static const char str[] = Z_SYM_NAME;
5182 size_t len = strlen (str);
5185 for (size_t i = 1; i < len; i++)
5187 lookup.assign (str, i);
5188 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5189 EXPECT (Z_SYM_NAME));
5193 /* In FULL mode, an incomplete symbol doesn't match. */
5195 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5199 /* A complete symbol with parameters matches any overload, since the
5200 index has no overload info. */
5202 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5203 EXPECT ("std::zfunction", "std::zfunction2"));
5204 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5205 EXPECT ("std::zfunction", "std::zfunction2"));
5206 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5207 EXPECT ("std::zfunction", "std::zfunction2"));
5210 /* Check that whitespace is ignored appropriately. A symbol with a
5211 template argument list. */
5213 static const char expected[] = "ns::foo<int>";
5214 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5216 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5220 /* Check that whitespace is ignored appropriately. A symbol with a
5221 template argument list that includes a pointer. */
5223 static const char expected[] = "ns::foo<char*>";
5224 /* Try both completion and non-completion modes. */
5225 static const bool completion_mode[2] = {false, true};
5226 for (size_t i = 0; i < 2; i++)
5228 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5229 completion_mode[i], EXPECT (expected));
5230 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5231 completion_mode[i], EXPECT (expected));
5233 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5234 completion_mode[i], EXPECT (expected));
5235 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5236 completion_mode[i], EXPECT (expected));
5241 /* Check method qualifiers are ignored. */
5242 static const char expected[] = "ns::foo<char*>";
5243 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5244 symbol_name_match_type::FULL, true, EXPECT (expected));
5245 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5246 symbol_name_match_type::FULL, true, EXPECT (expected));
5247 CHECK_MATCH ("foo < char * > ( int ) const",
5248 symbol_name_match_type::WILD, true, EXPECT (expected));
5249 CHECK_MATCH ("foo < char * > ( int ) &&",
5250 symbol_name_match_type::WILD, true, EXPECT (expected));
5253 /* Test lookup names that don't match anything. */
5255 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5258 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5262 /* Some wild matching tests, exercising "(anonymous namespace)",
5263 which should not be confused with a parameter list. */
5265 static const char *syms[] = {
5269 "A :: B :: C ( int )",
5274 for (const char *s : syms)
5276 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5277 EXPECT ("(anonymous namespace)::A::B::C"));
5282 static const char expected[] = "ns2::tmpl<int>::foo2";
5283 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5285 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5289 SELF_CHECK (!any_mismatch);
5298 test_mapped_index_find_name_component_bounds ();
5299 test_dw2_expand_symtabs_matching_symbol ();
5302 }} // namespace selftests::dw2_expand_symtabs_matching
5304 #endif /* GDB_SELF_TEST */
5306 /* If FILE_MATCHER is NULL or if PER_CU has
5307 dwarf2_per_cu_quick_data::MARK set (see
5308 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5309 EXPANSION_NOTIFY on it. */
5312 dw2_expand_symtabs_matching_one
5313 (struct dwarf2_per_cu_data *per_cu,
5314 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5315 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5317 if (file_matcher == NULL || per_cu->v.quick->mark)
5319 bool symtab_was_null
5320 = (per_cu->v.quick->compunit_symtab == NULL);
5322 dw2_instantiate_symtab (per_cu);
5324 if (expansion_notify != NULL
5326 && per_cu->v.quick->compunit_symtab != NULL)
5327 expansion_notify (per_cu->v.quick->compunit_symtab);
5331 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5332 matched, to expand corresponding CUs that were marked. IDX is the
5333 index of the symbol name that matched. */
5336 dw2_expand_marked_cus
5337 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5338 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5339 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5342 offset_type *vec, vec_len, vec_idx;
5343 bool global_seen = false;
5344 mapped_index &index = *dwarf2_per_objfile->index_table;
5346 vec = (offset_type *) (index.constant_pool
5347 + MAYBE_SWAP (index.symbol_table[idx].vec));
5348 vec_len = MAYBE_SWAP (vec[0]);
5349 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5351 struct dwarf2_per_cu_data *per_cu;
5352 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5353 /* This value is only valid for index versions >= 7. */
5354 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5355 gdb_index_symbol_kind symbol_kind =
5356 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5357 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5358 /* Only check the symbol attributes if they're present.
5359 Indices prior to version 7 don't record them,
5360 and indices >= 7 may elide them for certain symbols
5361 (gold does this). */
5364 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5366 /* Work around gold/15646. */
5369 if (!is_static && global_seen)
5375 /* Only check the symbol's kind if it has one. */
5380 case VARIABLES_DOMAIN:
5381 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5384 case FUNCTIONS_DOMAIN:
5385 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5389 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5397 /* Don't crash on bad data. */
5398 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5399 + dwarf2_per_objfile->n_type_units))
5401 complaint (&symfile_complaints,
5402 _(".gdb_index entry has bad CU index"
5404 objfile_name (dwarf2_per_objfile->objfile));
5408 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5409 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5414 /* If FILE_MATCHER is non-NULL, set all the
5415 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5416 that match FILE_MATCHER. */
5419 dw_expand_symtabs_matching_file_matcher
5420 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5421 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5423 if (file_matcher == NULL)
5426 objfile *const objfile = dwarf2_per_objfile->objfile;
5428 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5430 NULL, xcalloc, xfree));
5431 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5433 NULL, xcalloc, xfree));
5435 /* The rule is CUs specify all the files, including those used by
5436 any TU, so there's no need to scan TUs here. */
5438 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5441 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5442 struct quick_file_names *file_data;
5447 per_cu->v.quick->mark = 0;
5449 /* We only need to look at symtabs not already expanded. */
5450 if (per_cu->v.quick->compunit_symtab)
5453 file_data = dw2_get_file_names (per_cu);
5454 if (file_data == NULL)
5457 if (htab_find (visited_not_found.get (), file_data) != NULL)
5459 else if (htab_find (visited_found.get (), file_data) != NULL)
5461 per_cu->v.quick->mark = 1;
5465 for (j = 0; j < file_data->num_file_names; ++j)
5467 const char *this_real_name;
5469 if (file_matcher (file_data->file_names[j], false))
5471 per_cu->v.quick->mark = 1;
5475 /* Before we invoke realpath, which can get expensive when many
5476 files are involved, do a quick comparison of the basenames. */
5477 if (!basenames_may_differ
5478 && !file_matcher (lbasename (file_data->file_names[j]),
5482 this_real_name = dw2_get_real_path (objfile, file_data, j);
5483 if (file_matcher (this_real_name, false))
5485 per_cu->v.quick->mark = 1;
5490 slot = htab_find_slot (per_cu->v.quick->mark
5491 ? visited_found.get ()
5492 : visited_not_found.get (),
5499 dw2_expand_symtabs_matching
5500 (struct objfile *objfile,
5501 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5502 const lookup_name_info &lookup_name,
5503 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5504 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5505 enum search_domain kind)
5507 struct dwarf2_per_objfile *dwarf2_per_objfile
5508 = get_dwarf2_per_objfile (objfile);
5510 /* index_table is NULL if OBJF_READNOW. */
5511 if (!dwarf2_per_objfile->index_table)
5514 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5516 mapped_index &index = *dwarf2_per_objfile->index_table;
5518 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5520 kind, [&] (offset_type idx)
5522 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5523 expansion_notify, kind);
5527 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5530 static struct compunit_symtab *
5531 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5536 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5537 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5540 if (cust->includes == NULL)
5543 for (i = 0; cust->includes[i]; ++i)
5545 struct compunit_symtab *s = cust->includes[i];
5547 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5555 static struct compunit_symtab *
5556 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5557 struct bound_minimal_symbol msymbol,
5559 struct obj_section *section,
5562 struct dwarf2_per_cu_data *data;
5563 struct compunit_symtab *result;
5565 if (!objfile->psymtabs_addrmap)
5568 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5573 if (warn_if_readin && data->v.quick->compunit_symtab)
5574 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5575 paddress (get_objfile_arch (objfile), pc));
5578 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5580 gdb_assert (result != NULL);
5585 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5586 void *data, int need_fullname)
5588 struct dwarf2_per_objfile *dwarf2_per_objfile
5589 = get_dwarf2_per_objfile (objfile);
5591 if (!dwarf2_per_objfile->filenames_cache)
5593 dwarf2_per_objfile->filenames_cache.emplace ();
5595 htab_up visited (htab_create_alloc (10,
5596 htab_hash_pointer, htab_eq_pointer,
5597 NULL, xcalloc, xfree));
5599 /* The rule is CUs specify all the files, including those used
5600 by any TU, so there's no need to scan TUs here. We can
5601 ignore file names coming from already-expanded CUs. */
5603 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5605 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5607 if (per_cu->v.quick->compunit_symtab)
5609 void **slot = htab_find_slot (visited.get (),
5610 per_cu->v.quick->file_names,
5613 *slot = per_cu->v.quick->file_names;
5617 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5619 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5620 struct quick_file_names *file_data;
5623 /* We only need to look at symtabs not already expanded. */
5624 if (per_cu->v.quick->compunit_symtab)
5627 file_data = dw2_get_file_names (per_cu);
5628 if (file_data == NULL)
5631 slot = htab_find_slot (visited.get (), file_data, INSERT);
5634 /* Already visited. */
5639 for (int j = 0; j < file_data->num_file_names; ++j)
5641 const char *filename = file_data->file_names[j];
5642 dwarf2_per_objfile->filenames_cache->seen (filename);
5647 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5649 gdb::unique_xmalloc_ptr<char> this_real_name;
5652 this_real_name = gdb_realpath (filename);
5653 (*fun) (filename, this_real_name.get (), data);
5658 dw2_has_symbols (struct objfile *objfile)
5663 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5666 dw2_find_last_source_symtab,
5667 dw2_forget_cached_source_info,
5668 dw2_map_symtabs_matching_filename,
5673 dw2_expand_symtabs_for_function,
5674 dw2_expand_all_symtabs,
5675 dw2_expand_symtabs_with_fullname,
5676 dw2_map_matching_symbols,
5677 dw2_expand_symtabs_matching,
5678 dw2_find_pc_sect_compunit_symtab,
5680 dw2_map_symbol_filenames
5683 /* DWARF-5 debug_names reader. */
5685 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5686 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5688 /* A helper function that reads the .debug_names section in SECTION
5689 and fills in MAP. FILENAME is the name of the file containing the
5690 section; it is used for error reporting.
5692 Returns true if all went well, false otherwise. */
5695 read_debug_names_from_section (struct objfile *objfile,
5696 const char *filename,
5697 struct dwarf2_section_info *section,
5698 mapped_debug_names &map)
5700 if (dwarf2_section_empty_p (section))
5703 /* Older elfutils strip versions could keep the section in the main
5704 executable while splitting it for the separate debug info file. */
5705 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5708 dwarf2_read_section (objfile, section);
5710 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5712 const gdb_byte *addr = section->buffer;
5714 bfd *const abfd = get_section_bfd_owner (section);
5716 unsigned int bytes_read;
5717 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5720 map.dwarf5_is_dwarf64 = bytes_read != 4;
5721 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5722 if (bytes_read + length != section->size)
5724 /* There may be multiple per-CU indices. */
5725 warning (_("Section .debug_names in %s length %s does not match "
5726 "section length %s, ignoring .debug_names."),
5727 filename, plongest (bytes_read + length),
5728 pulongest (section->size));
5732 /* The version number. */
5733 uint16_t version = read_2_bytes (abfd, addr);
5737 warning (_("Section .debug_names in %s has unsupported version %d, "
5738 "ignoring .debug_names."),
5744 uint16_t padding = read_2_bytes (abfd, addr);
5748 warning (_("Section .debug_names in %s has unsupported padding %d, "
5749 "ignoring .debug_names."),
5754 /* comp_unit_count - The number of CUs in the CU list. */
5755 map.cu_count = read_4_bytes (abfd, addr);
5758 /* local_type_unit_count - The number of TUs in the local TU
5760 map.tu_count = read_4_bytes (abfd, addr);
5763 /* foreign_type_unit_count - The number of TUs in the foreign TU
5765 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5767 if (foreign_tu_count != 0)
5769 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5770 "ignoring .debug_names."),
5771 filename, static_cast<unsigned long> (foreign_tu_count));
5775 /* bucket_count - The number of hash buckets in the hash lookup
5777 map.bucket_count = read_4_bytes (abfd, addr);
5780 /* name_count - The number of unique names in the index. */
5781 map.name_count = read_4_bytes (abfd, addr);
5784 /* abbrev_table_size - The size in bytes of the abbreviations
5786 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5789 /* augmentation_string_size - The size in bytes of the augmentation
5790 string. This value is rounded up to a multiple of 4. */
5791 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5793 map.augmentation_is_gdb = ((augmentation_string_size
5794 == sizeof (dwarf5_augmentation))
5795 && memcmp (addr, dwarf5_augmentation,
5796 sizeof (dwarf5_augmentation)) == 0);
5797 augmentation_string_size += (-augmentation_string_size) & 3;
5798 addr += augmentation_string_size;
5801 map.cu_table_reordered = addr;
5802 addr += map.cu_count * map.offset_size;
5804 /* List of Local TUs */
5805 map.tu_table_reordered = addr;
5806 addr += map.tu_count * map.offset_size;
5808 /* Hash Lookup Table */
5809 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5810 addr += map.bucket_count * 4;
5811 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5812 addr += map.name_count * 4;
5815 map.name_table_string_offs_reordered = addr;
5816 addr += map.name_count * map.offset_size;
5817 map.name_table_entry_offs_reordered = addr;
5818 addr += map.name_count * map.offset_size;
5820 const gdb_byte *abbrev_table_start = addr;
5823 unsigned int bytes_read;
5824 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5829 const auto insertpair
5830 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5831 if (!insertpair.second)
5833 warning (_("Section .debug_names in %s has duplicate index %s, "
5834 "ignoring .debug_names."),
5835 filename, pulongest (index_num));
5838 mapped_debug_names::index_val &indexval = insertpair.first->second;
5839 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5844 mapped_debug_names::index_val::attr attr;
5845 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5847 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5849 if (attr.form == DW_FORM_implicit_const)
5851 attr.implicit_const = read_signed_leb128 (abfd, addr,
5855 if (attr.dw_idx == 0 && attr.form == 0)
5857 indexval.attr_vec.push_back (std::move (attr));
5860 if (addr != abbrev_table_start + abbrev_table_size)
5862 warning (_("Section .debug_names in %s has abbreviation_table "
5863 "of size %zu vs. written as %u, ignoring .debug_names."),
5864 filename, addr - abbrev_table_start, abbrev_table_size);
5867 map.entry_pool = addr;
5872 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5876 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5877 const mapped_debug_names &map,
5878 dwarf2_section_info §ion,
5879 bool is_dwz, int base_offset)
5881 sect_offset sect_off_prev;
5882 for (uint32_t i = 0; i <= map.cu_count; ++i)
5884 sect_offset sect_off_next;
5885 if (i < map.cu_count)
5888 = (sect_offset) (extract_unsigned_integer
5889 (map.cu_table_reordered + i * map.offset_size,
5891 map.dwarf5_byte_order));
5894 sect_off_next = (sect_offset) section.size;
5897 const ULONGEST length = sect_off_next - sect_off_prev;
5898 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5899 = create_cu_from_index_list (dwarf2_per_objfile, §ion, is_dwz,
5900 sect_off_prev, length);
5902 sect_off_prev = sect_off_next;
5906 /* Read the CU list from the mapped index, and use it to create all
5907 the CU objects for this dwarf2_per_objfile. */
5910 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5911 const mapped_debug_names &map,
5912 const mapped_debug_names &dwz_map)
5914 struct objfile *objfile = dwarf2_per_objfile->objfile;
5916 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5917 dwarf2_per_objfile->all_comp_units
5918 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5919 dwarf2_per_objfile->n_comp_units);
5921 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5922 dwarf2_per_objfile->info,
5924 0 /* base_offset */);
5926 if (dwz_map.cu_count == 0)
5929 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5930 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5932 map.cu_count /* base_offset */);
5935 /* Read .debug_names. If everything went ok, initialize the "quick"
5936 elements of all the CUs and return true. Otherwise, return false. */
5939 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5941 mapped_debug_names local_map (dwarf2_per_objfile);
5942 mapped_debug_names dwz_map (dwarf2_per_objfile);
5943 struct objfile *objfile = dwarf2_per_objfile->objfile;
5945 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5946 &dwarf2_per_objfile->debug_names,
5950 /* Don't use the index if it's empty. */
5951 if (local_map.name_count == 0)
5954 /* If there is a .dwz file, read it so we can get its CU list as
5956 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5959 if (!read_debug_names_from_section (objfile,
5960 bfd_get_filename (dwz->dwz_bfd),
5961 &dwz->debug_names, dwz_map))
5963 warning (_("could not read '.debug_names' section from %s; skipping"),
5964 bfd_get_filename (dwz->dwz_bfd));
5969 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5971 if (local_map.tu_count != 0)
5973 /* We can only handle a single .debug_types when we have an
5975 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5978 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5979 dwarf2_per_objfile->types, 0);
5981 create_signatured_type_table_from_debug_names
5982 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5985 create_addrmap_from_aranges (dwarf2_per_objfile,
5986 &dwarf2_per_objfile->debug_aranges);
5988 dwarf2_per_objfile->debug_names_table.reset
5989 (new mapped_debug_names (dwarf2_per_objfile));
5990 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5991 dwarf2_per_objfile->using_index = 1;
5992 dwarf2_per_objfile->quick_file_names_table =
5993 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5998 /* Symbol name hashing function as specified by DWARF-5. */
6001 dwarf5_djb_hash (const char *str_)
6003 const unsigned char *str = (const unsigned char *) str_;
6005 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6006 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6008 uint32_t hash = 5381;
6009 while (int c = *str++)
6010 hash = hash * 33 + tolower (c);
6014 /* Type used to manage iterating over all CUs looking for a symbol for
6017 class dw2_debug_names_iterator
6020 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6021 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6022 dw2_debug_names_iterator (const mapped_debug_names &map,
6023 bool want_specific_block,
6024 block_enum block_index, domain_enum domain,
6026 : m_map (map), m_want_specific_block (want_specific_block),
6027 m_block_index (block_index), m_domain (domain),
6028 m_addr (find_vec_in_debug_names (map, name))
6031 dw2_debug_names_iterator (const mapped_debug_names &map,
6032 search_domain search, uint32_t namei)
6035 m_addr (find_vec_in_debug_names (map, namei))
6038 /* Return the next matching CU or NULL if there are no more. */
6039 dwarf2_per_cu_data *next ();
6042 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6044 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6047 /* The internalized form of .debug_names. */
6048 const mapped_debug_names &m_map;
6050 /* If true, only look for symbols that match BLOCK_INDEX. */
6051 const bool m_want_specific_block = false;
6053 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6054 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6056 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6058 /* The kind of symbol we're looking for. */
6059 const domain_enum m_domain = UNDEF_DOMAIN;
6060 const search_domain m_search = ALL_DOMAIN;
6062 /* The list of CUs from the index entry of the symbol, or NULL if
6064 const gdb_byte *m_addr;
6068 mapped_debug_names::namei_to_name (uint32_t namei) const
6070 const ULONGEST namei_string_offs
6071 = extract_unsigned_integer ((name_table_string_offs_reordered
6072 + namei * offset_size),
6075 return read_indirect_string_at_offset
6076 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6079 /* Find a slot in .debug_names for the object named NAME. If NAME is
6080 found, return pointer to its pool data. If NAME cannot be found,
6084 dw2_debug_names_iterator::find_vec_in_debug_names
6085 (const mapped_debug_names &map, const char *name)
6087 int (*cmp) (const char *, const char *);
6089 if (current_language->la_language == language_cplus
6090 || current_language->la_language == language_fortran
6091 || current_language->la_language == language_d)
6093 /* NAME is already canonical. Drop any qualifiers as
6094 .debug_names does not contain any. */
6096 if (strchr (name, '(') != NULL)
6098 gdb::unique_xmalloc_ptr<char> without_params
6099 = cp_remove_params (name);
6101 if (without_params != NULL)
6103 name = without_params.get();
6108 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6110 const uint32_t full_hash = dwarf5_djb_hash (name);
6112 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6113 (map.bucket_table_reordered
6114 + (full_hash % map.bucket_count)), 4,
6115 map.dwarf5_byte_order);
6119 if (namei >= map.name_count)
6121 complaint (&symfile_complaints,
6122 _("Wrong .debug_names with name index %u but name_count=%u "
6124 namei, map.name_count,
6125 objfile_name (map.dwarf2_per_objfile->objfile));
6131 const uint32_t namei_full_hash
6132 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6133 (map.hash_table_reordered + namei), 4,
6134 map.dwarf5_byte_order);
6135 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6138 if (full_hash == namei_full_hash)
6140 const char *const namei_string = map.namei_to_name (namei);
6142 #if 0 /* An expensive sanity check. */
6143 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6145 complaint (&symfile_complaints,
6146 _("Wrong .debug_names hash for string at index %u "
6148 namei, objfile_name (dwarf2_per_objfile->objfile));
6153 if (cmp (namei_string, name) == 0)
6155 const ULONGEST namei_entry_offs
6156 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6157 + namei * map.offset_size),
6158 map.offset_size, map.dwarf5_byte_order);
6159 return map.entry_pool + namei_entry_offs;
6164 if (namei >= map.name_count)
6170 dw2_debug_names_iterator::find_vec_in_debug_names
6171 (const mapped_debug_names &map, uint32_t namei)
6173 if (namei >= map.name_count)
6175 complaint (&symfile_complaints,
6176 _("Wrong .debug_names with name index %u but name_count=%u "
6178 namei, map.name_count,
6179 objfile_name (map.dwarf2_per_objfile->objfile));
6183 const ULONGEST namei_entry_offs
6184 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6185 + namei * map.offset_size),
6186 map.offset_size, map.dwarf5_byte_order);
6187 return map.entry_pool + namei_entry_offs;
6190 /* See dw2_debug_names_iterator. */
6192 dwarf2_per_cu_data *
6193 dw2_debug_names_iterator::next ()
6198 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6199 struct objfile *objfile = dwarf2_per_objfile->objfile;
6200 bfd *const abfd = objfile->obfd;
6204 unsigned int bytes_read;
6205 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6206 m_addr += bytes_read;
6210 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6211 if (indexval_it == m_map.abbrev_map.cend ())
6213 complaint (&symfile_complaints,
6214 _("Wrong .debug_names undefined abbrev code %s "
6216 pulongest (abbrev), objfile_name (objfile));
6219 const mapped_debug_names::index_val &indexval = indexval_it->second;
6220 bool have_is_static = false;
6222 dwarf2_per_cu_data *per_cu = NULL;
6223 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6228 case DW_FORM_implicit_const:
6229 ull = attr.implicit_const;
6231 case DW_FORM_flag_present:
6235 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6236 m_addr += bytes_read;
6239 complaint (&symfile_complaints,
6240 _("Unsupported .debug_names form %s [in module %s]"),
6241 dwarf_form_name (attr.form),
6242 objfile_name (objfile));
6245 switch (attr.dw_idx)
6247 case DW_IDX_compile_unit:
6248 /* Don't crash on bad data. */
6249 if (ull >= dwarf2_per_objfile->n_comp_units)
6251 complaint (&symfile_complaints,
6252 _(".debug_names entry has bad CU index %s"
6255 objfile_name (dwarf2_per_objfile->objfile));
6258 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6260 case DW_IDX_type_unit:
6261 /* Don't crash on bad data. */
6262 if (ull >= dwarf2_per_objfile->n_type_units)
6264 complaint (&symfile_complaints,
6265 _(".debug_names entry has bad TU index %s"
6268 objfile_name (dwarf2_per_objfile->objfile));
6271 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6272 dwarf2_per_objfile->n_comp_units + ull);
6274 case DW_IDX_GNU_internal:
6275 if (!m_map.augmentation_is_gdb)
6277 have_is_static = true;
6280 case DW_IDX_GNU_external:
6281 if (!m_map.augmentation_is_gdb)
6283 have_is_static = true;
6289 /* Skip if already read in. */
6290 if (per_cu->v.quick->compunit_symtab)
6293 /* Check static vs global. */
6296 const bool want_static = m_block_index != GLOBAL_BLOCK;
6297 if (m_want_specific_block && want_static != is_static)
6301 /* Match dw2_symtab_iter_next, symbol_kind
6302 and debug_names::psymbol_tag. */
6306 switch (indexval.dwarf_tag)
6308 case DW_TAG_variable:
6309 case DW_TAG_subprogram:
6310 /* Some types are also in VAR_DOMAIN. */
6311 case DW_TAG_typedef:
6312 case DW_TAG_structure_type:
6319 switch (indexval.dwarf_tag)
6321 case DW_TAG_typedef:
6322 case DW_TAG_structure_type:
6329 switch (indexval.dwarf_tag)
6332 case DW_TAG_variable:
6342 /* Match dw2_expand_symtabs_matching, symbol_kind and
6343 debug_names::psymbol_tag. */
6346 case VARIABLES_DOMAIN:
6347 switch (indexval.dwarf_tag)
6349 case DW_TAG_variable:
6355 case FUNCTIONS_DOMAIN:
6356 switch (indexval.dwarf_tag)
6358 case DW_TAG_subprogram:
6365 switch (indexval.dwarf_tag)
6367 case DW_TAG_typedef:
6368 case DW_TAG_structure_type:
6381 static struct compunit_symtab *
6382 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6383 const char *name, domain_enum domain)
6385 const block_enum block_index = static_cast<block_enum> (block_index_int);
6386 struct dwarf2_per_objfile *dwarf2_per_objfile
6387 = get_dwarf2_per_objfile (objfile);
6389 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6392 /* index is NULL if OBJF_READNOW. */
6395 const auto &map = *mapp;
6397 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6398 block_index, domain, name);
6400 struct compunit_symtab *stab_best = NULL;
6401 struct dwarf2_per_cu_data *per_cu;
6402 while ((per_cu = iter.next ()) != NULL)
6404 struct symbol *sym, *with_opaque = NULL;
6405 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6406 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6407 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6409 sym = block_find_symbol (block, name, domain,
6410 block_find_non_opaque_type_preferred,
6413 /* Some caution must be observed with overloaded functions and
6414 methods, since the index will not contain any overload
6415 information (but NAME might contain it). */
6418 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6420 if (with_opaque != NULL
6421 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6424 /* Keep looking through other CUs. */
6430 /* This dumps minimal information about .debug_names. It is called
6431 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6432 uses this to verify that .debug_names has been loaded. */
6435 dw2_debug_names_dump (struct objfile *objfile)
6437 struct dwarf2_per_objfile *dwarf2_per_objfile
6438 = get_dwarf2_per_objfile (objfile);
6440 gdb_assert (dwarf2_per_objfile->using_index);
6441 printf_filtered (".debug_names:");
6442 if (dwarf2_per_objfile->debug_names_table)
6443 printf_filtered (" exists\n");
6445 printf_filtered (" faked for \"readnow\"\n");
6446 printf_filtered ("\n");
6450 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6451 const char *func_name)
6453 struct dwarf2_per_objfile *dwarf2_per_objfile
6454 = get_dwarf2_per_objfile (objfile);
6456 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6457 if (dwarf2_per_objfile->debug_names_table)
6459 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6461 /* Note: It doesn't matter what we pass for block_index here. */
6462 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6463 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6465 struct dwarf2_per_cu_data *per_cu;
6466 while ((per_cu = iter.next ()) != NULL)
6467 dw2_instantiate_symtab (per_cu);
6472 dw2_debug_names_expand_symtabs_matching
6473 (struct objfile *objfile,
6474 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6475 const lookup_name_info &lookup_name,
6476 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6478 enum search_domain kind)
6480 struct dwarf2_per_objfile *dwarf2_per_objfile
6481 = get_dwarf2_per_objfile (objfile);
6483 /* debug_names_table is NULL if OBJF_READNOW. */
6484 if (!dwarf2_per_objfile->debug_names_table)
6487 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6489 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6491 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6493 kind, [&] (offset_type namei)
6495 /* The name was matched, now expand corresponding CUs that were
6497 dw2_debug_names_iterator iter (map, kind, namei);
6499 struct dwarf2_per_cu_data *per_cu;
6500 while ((per_cu = iter.next ()) != NULL)
6501 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6506 const struct quick_symbol_functions dwarf2_debug_names_functions =
6509 dw2_find_last_source_symtab,
6510 dw2_forget_cached_source_info,
6511 dw2_map_symtabs_matching_filename,
6512 dw2_debug_names_lookup_symbol,
6514 dw2_debug_names_dump,
6516 dw2_debug_names_expand_symtabs_for_function,
6517 dw2_expand_all_symtabs,
6518 dw2_expand_symtabs_with_fullname,
6519 dw2_map_matching_symbols,
6520 dw2_debug_names_expand_symtabs_matching,
6521 dw2_find_pc_sect_compunit_symtab,
6523 dw2_map_symbol_filenames
6526 /* See symfile.h. */
6529 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6531 struct dwarf2_per_objfile *dwarf2_per_objfile
6532 = get_dwarf2_per_objfile (objfile);
6534 /* If we're about to read full symbols, don't bother with the
6535 indices. In this case we also don't care if some other debug
6536 format is making psymtabs, because they are all about to be
6538 if ((objfile->flags & OBJF_READNOW))
6542 dwarf2_per_objfile->using_index = 1;
6543 create_all_comp_units (dwarf2_per_objfile);
6544 create_all_type_units (dwarf2_per_objfile);
6545 dwarf2_per_objfile->quick_file_names_table =
6546 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6548 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6549 + dwarf2_per_objfile->n_type_units); ++i)
6551 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6553 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6554 struct dwarf2_per_cu_quick_data);
6557 /* Return 1 so that gdb sees the "quick" functions. However,
6558 these functions will be no-ops because we will have expanded
6560 *index_kind = dw_index_kind::GDB_INDEX;
6564 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6566 *index_kind = dw_index_kind::DEBUG_NAMES;
6570 if (dwarf2_read_index (objfile))
6572 *index_kind = dw_index_kind::GDB_INDEX;
6581 /* Build a partial symbol table. */
6584 dwarf2_build_psymtabs (struct objfile *objfile)
6586 struct dwarf2_per_objfile *dwarf2_per_objfile
6587 = get_dwarf2_per_objfile (objfile);
6589 if (objfile->global_psymbols.capacity () == 0
6590 && objfile->static_psymbols.capacity () == 0)
6591 init_psymbol_list (objfile, 1024);
6595 /* This isn't really ideal: all the data we allocate on the
6596 objfile's obstack is still uselessly kept around. However,
6597 freeing it seems unsafe. */
6598 psymtab_discarder psymtabs (objfile);
6599 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6602 CATCH (except, RETURN_MASK_ERROR)
6604 exception_print (gdb_stderr, except);
6609 /* Return the total length of the CU described by HEADER. */
6612 get_cu_length (const struct comp_unit_head *header)
6614 return header->initial_length_size + header->length;
6617 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6620 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6622 sect_offset bottom = cu_header->sect_off;
6623 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6625 return sect_off >= bottom && sect_off < top;
6628 /* Find the base address of the compilation unit for range lists and
6629 location lists. It will normally be specified by DW_AT_low_pc.
6630 In DWARF-3 draft 4, the base address could be overridden by
6631 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6632 compilation units with discontinuous ranges. */
6635 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6637 struct attribute *attr;
6640 cu->base_address = 0;
6642 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6645 cu->base_address = attr_value_as_address (attr);
6650 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6653 cu->base_address = attr_value_as_address (attr);
6659 /* Read in the comp unit header information from the debug_info at info_ptr.
6660 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6661 NOTE: This leaves members offset, first_die_offset to be filled in
6664 static const gdb_byte *
6665 read_comp_unit_head (struct comp_unit_head *cu_header,
6666 const gdb_byte *info_ptr,
6667 struct dwarf2_section_info *section,
6668 rcuh_kind section_kind)
6671 unsigned int bytes_read;
6672 const char *filename = get_section_file_name (section);
6673 bfd *abfd = get_section_bfd_owner (section);
6675 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6676 cu_header->initial_length_size = bytes_read;
6677 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6678 info_ptr += bytes_read;
6679 cu_header->version = read_2_bytes (abfd, info_ptr);
6681 if (cu_header->version < 5)
6682 switch (section_kind)
6684 case rcuh_kind::COMPILE:
6685 cu_header->unit_type = DW_UT_compile;
6687 case rcuh_kind::TYPE:
6688 cu_header->unit_type = DW_UT_type;
6691 internal_error (__FILE__, __LINE__,
6692 _("read_comp_unit_head: invalid section_kind"));
6696 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6697 (read_1_byte (abfd, info_ptr));
6699 switch (cu_header->unit_type)
6702 if (section_kind != rcuh_kind::COMPILE)
6703 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6704 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6708 section_kind = rcuh_kind::TYPE;
6711 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6712 "(is %d, should be %d or %d) [in module %s]"),
6713 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6716 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6719 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6722 info_ptr += bytes_read;
6723 if (cu_header->version < 5)
6725 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6728 signed_addr = bfd_get_sign_extend_vma (abfd);
6729 if (signed_addr < 0)
6730 internal_error (__FILE__, __LINE__,
6731 _("read_comp_unit_head: dwarf from non elf file"));
6732 cu_header->signed_addr_p = signed_addr;
6734 if (section_kind == rcuh_kind::TYPE)
6736 LONGEST type_offset;
6738 cu_header->signature = read_8_bytes (abfd, info_ptr);
6741 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6742 info_ptr += bytes_read;
6743 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6744 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6745 error (_("Dwarf Error: Too big type_offset in compilation unit "
6746 "header (is %s) [in module %s]"), plongest (type_offset),
6753 /* Helper function that returns the proper abbrev section for
6756 static struct dwarf2_section_info *
6757 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6759 struct dwarf2_section_info *abbrev;
6760 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6762 if (this_cu->is_dwz)
6763 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6765 abbrev = &dwarf2_per_objfile->abbrev;
6770 /* Subroutine of read_and_check_comp_unit_head and
6771 read_and_check_type_unit_head to simplify them.
6772 Perform various error checking on the header. */
6775 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6776 struct comp_unit_head *header,
6777 struct dwarf2_section_info *section,
6778 struct dwarf2_section_info *abbrev_section)
6780 const char *filename = get_section_file_name (section);
6782 if (header->version < 2 || header->version > 5)
6783 error (_("Dwarf Error: wrong version in compilation unit header "
6784 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6787 if (to_underlying (header->abbrev_sect_off)
6788 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6789 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6790 "(offset 0x%x + 6) [in module %s]"),
6791 to_underlying (header->abbrev_sect_off),
6792 to_underlying (header->sect_off),
6795 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6796 avoid potential 32-bit overflow. */
6797 if (((ULONGEST) header->sect_off + get_cu_length (header))
6799 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6800 "(offset 0x%x + 0) [in module %s]"),
6801 header->length, to_underlying (header->sect_off),
6805 /* Read in a CU/TU header and perform some basic error checking.
6806 The contents of the header are stored in HEADER.
6807 The result is a pointer to the start of the first DIE. */
6809 static const gdb_byte *
6810 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6811 struct comp_unit_head *header,
6812 struct dwarf2_section_info *section,
6813 struct dwarf2_section_info *abbrev_section,
6814 const gdb_byte *info_ptr,
6815 rcuh_kind section_kind)
6817 const gdb_byte *beg_of_comp_unit = info_ptr;
6819 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6821 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6823 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6825 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6831 /* Fetch the abbreviation table offset from a comp or type unit header. */
6834 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6835 struct dwarf2_section_info *section,
6836 sect_offset sect_off)
6838 bfd *abfd = get_section_bfd_owner (section);
6839 const gdb_byte *info_ptr;
6840 unsigned int initial_length_size, offset_size;
6843 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6844 info_ptr = section->buffer + to_underlying (sect_off);
6845 read_initial_length (abfd, info_ptr, &initial_length_size);
6846 offset_size = initial_length_size == 4 ? 4 : 8;
6847 info_ptr += initial_length_size;
6849 version = read_2_bytes (abfd, info_ptr);
6853 /* Skip unit type and address size. */
6857 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6860 /* Allocate a new partial symtab for file named NAME and mark this new
6861 partial symtab as being an include of PST. */
6864 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6865 struct objfile *objfile)
6867 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6869 if (!IS_ABSOLUTE_PATH (subpst->filename))
6871 /* It shares objfile->objfile_obstack. */
6872 subpst->dirname = pst->dirname;
6875 subpst->textlow = 0;
6876 subpst->texthigh = 0;
6878 subpst->dependencies
6879 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6880 subpst->dependencies[0] = pst;
6881 subpst->number_of_dependencies = 1;
6883 subpst->globals_offset = 0;
6884 subpst->n_global_syms = 0;
6885 subpst->statics_offset = 0;
6886 subpst->n_static_syms = 0;
6887 subpst->compunit_symtab = NULL;
6888 subpst->read_symtab = pst->read_symtab;
6891 /* No private part is necessary for include psymtabs. This property
6892 can be used to differentiate between such include psymtabs and
6893 the regular ones. */
6894 subpst->read_symtab_private = NULL;
6897 /* Read the Line Number Program data and extract the list of files
6898 included by the source file represented by PST. Build an include
6899 partial symtab for each of these included files. */
6902 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6903 struct die_info *die,
6904 struct partial_symtab *pst)
6907 struct attribute *attr;
6909 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6911 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6913 return; /* No linetable, so no includes. */
6915 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6916 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6920 hash_signatured_type (const void *item)
6922 const struct signatured_type *sig_type
6923 = (const struct signatured_type *) item;
6925 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6926 return sig_type->signature;
6930 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6932 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6933 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6935 return lhs->signature == rhs->signature;
6938 /* Allocate a hash table for signatured types. */
6941 allocate_signatured_type_table (struct objfile *objfile)
6943 return htab_create_alloc_ex (41,
6944 hash_signatured_type,
6947 &objfile->objfile_obstack,
6948 hashtab_obstack_allocate,
6949 dummy_obstack_deallocate);
6952 /* A helper function to add a signatured type CU to a table. */
6955 add_signatured_type_cu_to_table (void **slot, void *datum)
6957 struct signatured_type *sigt = (struct signatured_type *) *slot;
6958 struct signatured_type ***datap = (struct signatured_type ***) datum;
6966 /* A helper for create_debug_types_hash_table. Read types from SECTION
6967 and fill them into TYPES_HTAB. It will process only type units,
6968 therefore DW_UT_type. */
6971 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6972 struct dwo_file *dwo_file,
6973 dwarf2_section_info *section, htab_t &types_htab,
6974 rcuh_kind section_kind)
6976 struct objfile *objfile = dwarf2_per_objfile->objfile;
6977 struct dwarf2_section_info *abbrev_section;
6979 const gdb_byte *info_ptr, *end_ptr;
6981 abbrev_section = (dwo_file != NULL
6982 ? &dwo_file->sections.abbrev
6983 : &dwarf2_per_objfile->abbrev);
6985 if (dwarf_read_debug)
6986 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6987 get_section_name (section),
6988 get_section_file_name (abbrev_section));
6990 dwarf2_read_section (objfile, section);
6991 info_ptr = section->buffer;
6993 if (info_ptr == NULL)
6996 /* We can't set abfd until now because the section may be empty or
6997 not present, in which case the bfd is unknown. */
6998 abfd = get_section_bfd_owner (section);
7000 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7001 because we don't need to read any dies: the signature is in the
7004 end_ptr = info_ptr + section->size;
7005 while (info_ptr < end_ptr)
7007 struct signatured_type *sig_type;
7008 struct dwo_unit *dwo_tu;
7010 const gdb_byte *ptr = info_ptr;
7011 struct comp_unit_head header;
7012 unsigned int length;
7014 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7016 /* Initialize it due to a false compiler warning. */
7017 header.signature = -1;
7018 header.type_cu_offset_in_tu = (cu_offset) -1;
7020 /* We need to read the type's signature in order to build the hash
7021 table, but we don't need anything else just yet. */
7023 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7024 abbrev_section, ptr, section_kind);
7026 length = get_cu_length (&header);
7028 /* Skip dummy type units. */
7029 if (ptr >= info_ptr + length
7030 || peek_abbrev_code (abfd, ptr) == 0
7031 || header.unit_type != DW_UT_type)
7037 if (types_htab == NULL)
7040 types_htab = allocate_dwo_unit_table (objfile);
7042 types_htab = allocate_signatured_type_table (objfile);
7048 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7050 dwo_tu->dwo_file = dwo_file;
7051 dwo_tu->signature = header.signature;
7052 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7053 dwo_tu->section = section;
7054 dwo_tu->sect_off = sect_off;
7055 dwo_tu->length = length;
7059 /* N.B.: type_offset is not usable if this type uses a DWO file.
7060 The real type_offset is in the DWO file. */
7062 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7063 struct signatured_type);
7064 sig_type->signature = header.signature;
7065 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7066 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7067 sig_type->per_cu.is_debug_types = 1;
7068 sig_type->per_cu.section = section;
7069 sig_type->per_cu.sect_off = sect_off;
7070 sig_type->per_cu.length = length;
7073 slot = htab_find_slot (types_htab,
7074 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7076 gdb_assert (slot != NULL);
7079 sect_offset dup_sect_off;
7083 const struct dwo_unit *dup_tu
7084 = (const struct dwo_unit *) *slot;
7086 dup_sect_off = dup_tu->sect_off;
7090 const struct signatured_type *dup_tu
7091 = (const struct signatured_type *) *slot;
7093 dup_sect_off = dup_tu->per_cu.sect_off;
7096 complaint (&symfile_complaints,
7097 _("debug type entry at offset 0x%x is duplicate to"
7098 " the entry at offset 0x%x, signature %s"),
7099 to_underlying (sect_off), to_underlying (dup_sect_off),
7100 hex_string (header.signature));
7102 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7104 if (dwarf_read_debug > 1)
7105 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7106 to_underlying (sect_off),
7107 hex_string (header.signature));
7113 /* Create the hash table of all entries in the .debug_types
7114 (or .debug_types.dwo) section(s).
7115 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7116 otherwise it is NULL.
7118 The result is a pointer to the hash table or NULL if there are no types.
7120 Note: This function processes DWO files only, not DWP files. */
7123 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7124 struct dwo_file *dwo_file,
7125 VEC (dwarf2_section_info_def) *types,
7129 struct dwarf2_section_info *section;
7131 if (VEC_empty (dwarf2_section_info_def, types))
7135 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7137 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7138 types_htab, rcuh_kind::TYPE);
7141 /* Create the hash table of all entries in the .debug_types section,
7142 and initialize all_type_units.
7143 The result is zero if there is an error (e.g. missing .debug_types section),
7144 otherwise non-zero. */
7147 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7149 htab_t types_htab = NULL;
7150 struct signatured_type **iter;
7152 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7153 &dwarf2_per_objfile->info, types_htab,
7154 rcuh_kind::COMPILE);
7155 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7156 dwarf2_per_objfile->types, types_htab);
7157 if (types_htab == NULL)
7159 dwarf2_per_objfile->signatured_types = NULL;
7163 dwarf2_per_objfile->signatured_types = types_htab;
7165 dwarf2_per_objfile->n_type_units
7166 = dwarf2_per_objfile->n_allocated_type_units
7167 = htab_elements (types_htab);
7168 dwarf2_per_objfile->all_type_units =
7169 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7170 iter = &dwarf2_per_objfile->all_type_units[0];
7171 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7172 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7173 == dwarf2_per_objfile->n_type_units);
7178 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7179 If SLOT is non-NULL, it is the entry to use in the hash table.
7180 Otherwise we find one. */
7182 static struct signatured_type *
7183 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7186 struct objfile *objfile = dwarf2_per_objfile->objfile;
7187 int n_type_units = dwarf2_per_objfile->n_type_units;
7188 struct signatured_type *sig_type;
7190 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7192 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7194 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7195 dwarf2_per_objfile->n_allocated_type_units = 1;
7196 dwarf2_per_objfile->n_allocated_type_units *= 2;
7197 dwarf2_per_objfile->all_type_units
7198 = XRESIZEVEC (struct signatured_type *,
7199 dwarf2_per_objfile->all_type_units,
7200 dwarf2_per_objfile->n_allocated_type_units);
7201 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7203 dwarf2_per_objfile->n_type_units = n_type_units;
7205 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7206 struct signatured_type);
7207 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7208 sig_type->signature = sig;
7209 sig_type->per_cu.is_debug_types = 1;
7210 if (dwarf2_per_objfile->using_index)
7212 sig_type->per_cu.v.quick =
7213 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7214 struct dwarf2_per_cu_quick_data);
7219 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7222 gdb_assert (*slot == NULL);
7224 /* The rest of sig_type must be filled in by the caller. */
7228 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7229 Fill in SIG_ENTRY with DWO_ENTRY. */
7232 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7233 struct signatured_type *sig_entry,
7234 struct dwo_unit *dwo_entry)
7236 /* Make sure we're not clobbering something we don't expect to. */
7237 gdb_assert (! sig_entry->per_cu.queued);
7238 gdb_assert (sig_entry->per_cu.cu == NULL);
7239 if (dwarf2_per_objfile->using_index)
7241 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7242 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7245 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7246 gdb_assert (sig_entry->signature == dwo_entry->signature);
7247 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7248 gdb_assert (sig_entry->type_unit_group == NULL);
7249 gdb_assert (sig_entry->dwo_unit == NULL);
7251 sig_entry->per_cu.section = dwo_entry->section;
7252 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7253 sig_entry->per_cu.length = dwo_entry->length;
7254 sig_entry->per_cu.reading_dwo_directly = 1;
7255 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7256 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7257 sig_entry->dwo_unit = dwo_entry;
7260 /* Subroutine of lookup_signatured_type.
7261 If we haven't read the TU yet, create the signatured_type data structure
7262 for a TU to be read in directly from a DWO file, bypassing the stub.
7263 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7264 using .gdb_index, then when reading a CU we want to stay in the DWO file
7265 containing that CU. Otherwise we could end up reading several other DWO
7266 files (due to comdat folding) to process the transitive closure of all the
7267 mentioned TUs, and that can be slow. The current DWO file will have every
7268 type signature that it needs.
7269 We only do this for .gdb_index because in the psymtab case we already have
7270 to read all the DWOs to build the type unit groups. */
7272 static struct signatured_type *
7273 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7275 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
7276 struct objfile *objfile = dwarf2_per_objfile->objfile;
7277 struct dwo_file *dwo_file;
7278 struct dwo_unit find_dwo_entry, *dwo_entry;
7279 struct signatured_type find_sig_entry, *sig_entry;
7282 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7284 /* If TU skeletons have been removed then we may not have read in any
7286 if (dwarf2_per_objfile->signatured_types == NULL)
7288 dwarf2_per_objfile->signatured_types
7289 = allocate_signatured_type_table (objfile);
7292 /* We only ever need to read in one copy of a signatured type.
7293 Use the global signatured_types array to do our own comdat-folding
7294 of types. If this is the first time we're reading this TU, and
7295 the TU has an entry in .gdb_index, replace the recorded data from
7296 .gdb_index with this TU. */
7298 find_sig_entry.signature = sig;
7299 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7300 &find_sig_entry, INSERT);
7301 sig_entry = (struct signatured_type *) *slot;
7303 /* We can get here with the TU already read, *or* in the process of being
7304 read. Don't reassign the global entry to point to this DWO if that's
7305 the case. Also note that if the TU is already being read, it may not
7306 have come from a DWO, the program may be a mix of Fission-compiled
7307 code and non-Fission-compiled code. */
7309 /* Have we already tried to read this TU?
7310 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7311 needn't exist in the global table yet). */
7312 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7315 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7316 dwo_unit of the TU itself. */
7317 dwo_file = cu->dwo_unit->dwo_file;
7319 /* Ok, this is the first time we're reading this TU. */
7320 if (dwo_file->tus == NULL)
7322 find_dwo_entry.signature = sig;
7323 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7324 if (dwo_entry == NULL)
7327 /* If the global table doesn't have an entry for this TU, add one. */
7328 if (sig_entry == NULL)
7329 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7331 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7332 sig_entry->per_cu.tu_read = 1;
7336 /* Subroutine of lookup_signatured_type.
7337 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7338 then try the DWP file. If the TU stub (skeleton) has been removed then
7339 it won't be in .gdb_index. */
7341 static struct signatured_type *
7342 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7344 struct dwarf2_per_objfile *dwarf2_per_objfile = 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 = cu->dwarf2_per_objfile;
7396 && dwarf2_per_objfile->using_index)
7398 /* We're in a DWO/DWP file, and we're using .gdb_index.
7399 These cases require special processing. */
7400 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7401 return lookup_dwo_signatured_type (cu, sig);
7403 return lookup_dwp_signatured_type (cu, sig);
7407 struct signatured_type find_entry, *entry;
7409 if (dwarf2_per_objfile->signatured_types == NULL)
7411 find_entry.signature = sig;
7412 entry = ((struct signatured_type *)
7413 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7418 /* Low level DIE reading support. */
7420 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7423 init_cu_die_reader (struct die_reader_specs *reader,
7424 struct dwarf2_cu *cu,
7425 struct dwarf2_section_info *section,
7426 struct dwo_file *dwo_file)
7428 gdb_assert (section->readin && section->buffer != NULL);
7429 reader->abfd = get_section_bfd_owner (section);
7431 reader->dwo_file = dwo_file;
7432 reader->die_section = section;
7433 reader->buffer = section->buffer;
7434 reader->buffer_end = section->buffer + section->size;
7435 reader->comp_dir = NULL;
7438 /* Subroutine of init_cutu_and_read_dies to simplify it.
7439 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7440 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7443 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7444 from it to the DIE in the DWO. If NULL we are skipping the stub.
7445 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7446 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7447 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7448 STUB_COMP_DIR may be non-NULL.
7449 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7450 are filled in with the info of the DIE from the DWO file.
7451 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7452 provided an abbrev table to use.
7453 The result is non-zero if a valid (non-dummy) DIE was found. */
7456 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7457 struct dwo_unit *dwo_unit,
7458 int abbrev_table_provided,
7459 struct die_info *stub_comp_unit_die,
7460 const char *stub_comp_dir,
7461 struct die_reader_specs *result_reader,
7462 const gdb_byte **result_info_ptr,
7463 struct die_info **result_comp_unit_die,
7464 int *result_has_children)
7466 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7467 struct objfile *objfile = dwarf2_per_objfile->objfile;
7468 struct dwarf2_cu *cu = this_cu->cu;
7469 struct dwarf2_section_info *section;
7471 const gdb_byte *begin_info_ptr, *info_ptr;
7472 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7473 int i,num_extra_attrs;
7474 struct dwarf2_section_info *dwo_abbrev_section;
7475 struct attribute *attr;
7476 struct die_info *comp_unit_die;
7478 /* At most one of these may be provided. */
7479 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7481 /* These attributes aren't processed until later:
7482 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7483 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7484 referenced later. However, these attributes are found in the stub
7485 which we won't have later. In order to not impose this complication
7486 on the rest of the code, we read them here and copy them to the
7495 if (stub_comp_unit_die != NULL)
7497 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7499 if (! this_cu->is_debug_types)
7500 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7501 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7502 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7503 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7504 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7506 /* There should be a DW_AT_addr_base attribute here (if needed).
7507 We need the value before we can process DW_FORM_GNU_addr_index. */
7509 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7511 cu->addr_base = DW_UNSND (attr);
7513 /* There should be a DW_AT_ranges_base attribute here (if needed).
7514 We need the value before we can process DW_AT_ranges. */
7515 cu->ranges_base = 0;
7516 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7518 cu->ranges_base = DW_UNSND (attr);
7520 else if (stub_comp_dir != NULL)
7522 /* Reconstruct the comp_dir attribute to simplify the code below. */
7523 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7524 comp_dir->name = DW_AT_comp_dir;
7525 comp_dir->form = DW_FORM_string;
7526 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7527 DW_STRING (comp_dir) = stub_comp_dir;
7530 /* Set up for reading the DWO CU/TU. */
7531 cu->dwo_unit = dwo_unit;
7532 section = dwo_unit->section;
7533 dwarf2_read_section (objfile, section);
7534 abfd = get_section_bfd_owner (section);
7535 begin_info_ptr = info_ptr = (section->buffer
7536 + to_underlying (dwo_unit->sect_off));
7537 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7538 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7540 if (this_cu->is_debug_types)
7542 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7544 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7545 &cu->header, section,
7547 info_ptr, rcuh_kind::TYPE);
7548 /* This is not an assert because it can be caused by bad debug info. */
7549 if (sig_type->signature != cu->header.signature)
7551 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7552 " TU at offset 0x%x [in module %s]"),
7553 hex_string (sig_type->signature),
7554 hex_string (cu->header.signature),
7555 to_underlying (dwo_unit->sect_off),
7556 bfd_get_filename (abfd));
7558 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7559 /* For DWOs coming from DWP files, we don't know the CU length
7560 nor the type's offset in the TU until now. */
7561 dwo_unit->length = get_cu_length (&cu->header);
7562 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7564 /* Establish the type offset that can be used to lookup the type.
7565 For DWO files, we don't know it until now. */
7566 sig_type->type_offset_in_section
7567 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7571 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7572 &cu->header, section,
7574 info_ptr, rcuh_kind::COMPILE);
7575 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7576 /* For DWOs coming from DWP files, we don't know the CU length
7578 dwo_unit->length = get_cu_length (&cu->header);
7581 /* Replace the CU's original abbrev table with the DWO's.
7582 Reminder: We can't read the abbrev table until we've read the header. */
7583 if (abbrev_table_provided)
7585 /* Don't free the provided abbrev table, the caller of
7586 init_cutu_and_read_dies owns it. */
7587 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7588 /* Ensure the DWO abbrev table gets freed. */
7589 make_cleanup (dwarf2_free_abbrev_table, cu);
7593 dwarf2_free_abbrev_table (cu);
7594 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7595 /* Leave any existing abbrev table cleanup as is. */
7598 /* Read in the die, but leave space to copy over the attributes
7599 from the stub. This has the benefit of simplifying the rest of
7600 the code - all the work to maintain the illusion of a single
7601 DW_TAG_{compile,type}_unit DIE is done here. */
7602 num_extra_attrs = ((stmt_list != NULL)
7606 + (comp_dir != NULL));
7607 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7608 result_has_children, num_extra_attrs);
7610 /* Copy over the attributes from the stub to the DIE we just read in. */
7611 comp_unit_die = *result_comp_unit_die;
7612 i = comp_unit_die->num_attrs;
7613 if (stmt_list != NULL)
7614 comp_unit_die->attrs[i++] = *stmt_list;
7616 comp_unit_die->attrs[i++] = *low_pc;
7617 if (high_pc != NULL)
7618 comp_unit_die->attrs[i++] = *high_pc;
7620 comp_unit_die->attrs[i++] = *ranges;
7621 if (comp_dir != NULL)
7622 comp_unit_die->attrs[i++] = *comp_dir;
7623 comp_unit_die->num_attrs += num_extra_attrs;
7625 if (dwarf_die_debug)
7627 fprintf_unfiltered (gdb_stdlog,
7628 "Read die from %s@0x%x of %s:\n",
7629 get_section_name (section),
7630 (unsigned) (begin_info_ptr - section->buffer),
7631 bfd_get_filename (abfd));
7632 dump_die (comp_unit_die, dwarf_die_debug);
7635 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7636 TUs by skipping the stub and going directly to the entry in the DWO file.
7637 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7638 to get it via circuitous means. Blech. */
7639 if (comp_dir != NULL)
7640 result_reader->comp_dir = DW_STRING (comp_dir);
7642 /* Skip dummy compilation units. */
7643 if (info_ptr >= begin_info_ptr + dwo_unit->length
7644 || peek_abbrev_code (abfd, info_ptr) == 0)
7647 *result_info_ptr = info_ptr;
7651 /* Subroutine of init_cutu_and_read_dies to simplify it.
7652 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7653 Returns NULL if the specified DWO unit cannot be found. */
7655 static struct dwo_unit *
7656 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7657 struct die_info *comp_unit_die)
7659 struct dwarf2_cu *cu = this_cu->cu;
7661 struct dwo_unit *dwo_unit;
7662 const char *comp_dir, *dwo_name;
7664 gdb_assert (cu != NULL);
7666 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7667 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7668 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7670 if (this_cu->is_debug_types)
7672 struct signatured_type *sig_type;
7674 /* Since this_cu is the first member of struct signatured_type,
7675 we can go from a pointer to one to a pointer to the other. */
7676 sig_type = (struct signatured_type *) this_cu;
7677 signature = sig_type->signature;
7678 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7682 struct attribute *attr;
7684 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7686 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7688 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7689 signature = DW_UNSND (attr);
7690 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7697 /* Subroutine of init_cutu_and_read_dies to simplify it.
7698 See it for a description of the parameters.
7699 Read a TU directly from a DWO file, bypassing the stub.
7701 Note: This function could be a little bit simpler if we shared cleanups
7702 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7703 to do, so we keep this function self-contained. Or we could move this
7704 into our caller, but it's complex enough already. */
7707 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7708 int use_existing_cu, int keep,
7709 die_reader_func_ftype *die_reader_func,
7712 struct dwarf2_cu *cu;
7713 struct signatured_type *sig_type;
7714 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7715 struct die_reader_specs reader;
7716 const gdb_byte *info_ptr;
7717 struct die_info *comp_unit_die;
7719 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7721 /* Verify we can do the following downcast, and that we have the
7723 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7724 sig_type = (struct signatured_type *) this_cu;
7725 gdb_assert (sig_type->dwo_unit != NULL);
7727 cleanups = make_cleanup (null_cleanup, NULL);
7729 if (use_existing_cu && this_cu->cu != NULL)
7731 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7733 /* There's no need to do the rereading_dwo_cu handling that
7734 init_cutu_and_read_dies does since we don't read the stub. */
7738 /* If !use_existing_cu, this_cu->cu must be NULL. */
7739 gdb_assert (this_cu->cu == NULL);
7740 cu = XNEW (struct dwarf2_cu);
7741 init_one_comp_unit (cu, this_cu);
7742 /* If an error occurs while loading, release our storage. */
7743 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7746 /* A future optimization, if needed, would be to use an existing
7747 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7748 could share abbrev tables. */
7750 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7751 0 /* abbrev_table_provided */,
7752 NULL /* stub_comp_unit_die */,
7753 sig_type->dwo_unit->dwo_file->comp_dir,
7755 &comp_unit_die, &has_children) == 0)
7758 do_cleanups (cleanups);
7762 /* All the "real" work is done here. */
7763 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7765 /* This duplicates the code in init_cutu_and_read_dies,
7766 but the alternative is making the latter more complex.
7767 This function is only for the special case of using DWO files directly:
7768 no point in overly complicating the general case just to handle this. */
7769 if (free_cu_cleanup != NULL)
7773 /* We've successfully allocated this compilation unit. Let our
7774 caller clean it up when finished with it. */
7775 discard_cleanups (free_cu_cleanup);
7777 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7778 So we have to manually free the abbrev table. */
7779 dwarf2_free_abbrev_table (cu);
7781 /* Link this CU into read_in_chain. */
7782 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7783 dwarf2_per_objfile->read_in_chain = this_cu;
7786 do_cleanups (free_cu_cleanup);
7789 do_cleanups (cleanups);
7792 /* Initialize a CU (or TU) and read its DIEs.
7793 If the CU defers to a DWO file, read the DWO file as well.
7795 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7796 Otherwise the table specified in the comp unit header is read in and used.
7797 This is an optimization for when we already have the abbrev table.
7799 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7800 Otherwise, a new CU is allocated with xmalloc.
7802 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7803 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7805 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7806 linker) then DIE_READER_FUNC will not get called. */
7809 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7810 struct abbrev_table *abbrev_table,
7811 int use_existing_cu, int keep,
7812 die_reader_func_ftype *die_reader_func,
7815 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7816 struct objfile *objfile = dwarf2_per_objfile->objfile;
7817 struct dwarf2_section_info *section = this_cu->section;
7818 bfd *abfd = get_section_bfd_owner (section);
7819 struct dwarf2_cu *cu;
7820 const gdb_byte *begin_info_ptr, *info_ptr;
7821 struct die_reader_specs reader;
7822 struct die_info *comp_unit_die;
7824 struct attribute *attr;
7825 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7826 struct signatured_type *sig_type = NULL;
7827 struct dwarf2_section_info *abbrev_section;
7828 /* Non-zero if CU currently points to a DWO file and we need to
7829 reread it. When this happens we need to reread the skeleton die
7830 before we can reread the DWO file (this only applies to CUs, not TUs). */
7831 int rereading_dwo_cu = 0;
7833 if (dwarf_die_debug)
7834 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7835 this_cu->is_debug_types ? "type" : "comp",
7836 to_underlying (this_cu->sect_off));
7838 if (use_existing_cu)
7841 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7842 file (instead of going through the stub), short-circuit all of this. */
7843 if (this_cu->reading_dwo_directly)
7845 /* Narrow down the scope of possibilities to have to understand. */
7846 gdb_assert (this_cu->is_debug_types);
7847 gdb_assert (abbrev_table == NULL);
7848 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7849 die_reader_func, data);
7853 cleanups = make_cleanup (null_cleanup, NULL);
7855 /* This is cheap if the section is already read in. */
7856 dwarf2_read_section (objfile, section);
7858 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7860 abbrev_section = get_abbrev_section_for_cu (this_cu);
7862 if (use_existing_cu && this_cu->cu != NULL)
7865 /* If this CU is from a DWO file we need to start over, we need to
7866 refetch the attributes from the skeleton CU.
7867 This could be optimized by retrieving those attributes from when we
7868 were here the first time: the previous comp_unit_die was stored in
7869 comp_unit_obstack. But there's no data yet that we need this
7871 if (cu->dwo_unit != NULL)
7872 rereading_dwo_cu = 1;
7876 /* If !use_existing_cu, this_cu->cu must be NULL. */
7877 gdb_assert (this_cu->cu == NULL);
7878 cu = XNEW (struct dwarf2_cu);
7879 init_one_comp_unit (cu, this_cu);
7880 /* If an error occurs while loading, release our storage. */
7881 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7884 /* Get the header. */
7885 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7887 /* We already have the header, there's no need to read it in again. */
7888 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7892 if (this_cu->is_debug_types)
7894 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7895 &cu->header, section,
7896 abbrev_section, info_ptr,
7899 /* Since per_cu is the first member of struct signatured_type,
7900 we can go from a pointer to one to a pointer to the other. */
7901 sig_type = (struct signatured_type *) this_cu;
7902 gdb_assert (sig_type->signature == cu->header.signature);
7903 gdb_assert (sig_type->type_offset_in_tu
7904 == cu->header.type_cu_offset_in_tu);
7905 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7907 /* LENGTH has not been set yet for type units if we're
7908 using .gdb_index. */
7909 this_cu->length = get_cu_length (&cu->header);
7911 /* Establish the type offset that can be used to lookup the type. */
7912 sig_type->type_offset_in_section =
7913 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7915 this_cu->dwarf_version = cu->header.version;
7919 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7920 &cu->header, section,
7923 rcuh_kind::COMPILE);
7925 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7926 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7927 this_cu->dwarf_version = cu->header.version;
7931 /* Skip dummy compilation units. */
7932 if (info_ptr >= begin_info_ptr + this_cu->length
7933 || peek_abbrev_code (abfd, info_ptr) == 0)
7935 do_cleanups (cleanups);
7939 /* If we don't have them yet, read the abbrevs for this compilation unit.
7940 And if we need to read them now, make sure they're freed when we're
7941 done. Note that it's important that if the CU had an abbrev table
7942 on entry we don't free it when we're done: Somewhere up the call stack
7943 it may be in use. */
7944 if (abbrev_table != NULL)
7946 gdb_assert (cu->abbrev_table == NULL);
7947 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7948 cu->abbrev_table = abbrev_table;
7950 else if (cu->abbrev_table == NULL)
7952 dwarf2_read_abbrevs (cu, abbrev_section);
7953 make_cleanup (dwarf2_free_abbrev_table, cu);
7955 else if (rereading_dwo_cu)
7957 dwarf2_free_abbrev_table (cu);
7958 dwarf2_read_abbrevs (cu, abbrev_section);
7961 /* Read the top level CU/TU die. */
7962 init_cu_die_reader (&reader, cu, section, NULL);
7963 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7965 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7967 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7968 DWO CU, that this test will fail (the attribute will not be present). */
7969 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7972 struct dwo_unit *dwo_unit;
7973 struct die_info *dwo_comp_unit_die;
7977 complaint (&symfile_complaints,
7978 _("compilation unit with DW_AT_GNU_dwo_name"
7979 " has children (offset 0x%x) [in module %s]"),
7980 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7982 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7983 if (dwo_unit != NULL)
7985 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7986 abbrev_table != NULL,
7987 comp_unit_die, NULL,
7989 &dwo_comp_unit_die, &has_children) == 0)
7992 do_cleanups (cleanups);
7995 comp_unit_die = dwo_comp_unit_die;
7999 /* Yikes, we couldn't find the rest of the DIE, we only have
8000 the stub. A complaint has already been logged. There's
8001 not much more we can do except pass on the stub DIE to
8002 die_reader_func. We don't want to throw an error on bad
8007 /* All of the above is setup for this call. Yikes. */
8008 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8010 /* Done, clean up. */
8011 if (free_cu_cleanup != NULL)
8015 /* We've successfully allocated this compilation unit. Let our
8016 caller clean it up when finished with it. */
8017 discard_cleanups (free_cu_cleanup);
8019 /* We can only discard free_cu_cleanup and all subsequent cleanups.
8020 So we have to manually free the abbrev table. */
8021 dwarf2_free_abbrev_table (cu);
8023 /* Link this CU into read_in_chain. */
8024 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8025 dwarf2_per_objfile->read_in_chain = this_cu;
8028 do_cleanups (free_cu_cleanup);
8031 do_cleanups (cleanups);
8034 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8035 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8036 to have already done the lookup to find the DWO file).
8038 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8039 THIS_CU->is_debug_types, but nothing else.
8041 We fill in THIS_CU->length.
8043 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8044 linker) then DIE_READER_FUNC will not get called.
8046 THIS_CU->cu is always freed when done.
8047 This is done in order to not leave THIS_CU->cu in a state where we have
8048 to care whether it refers to the "main" CU or the DWO CU. */
8051 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8052 struct dwo_file *dwo_file,
8053 die_reader_func_ftype *die_reader_func,
8056 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8057 struct objfile *objfile = dwarf2_per_objfile->objfile;
8058 struct dwarf2_section_info *section = this_cu->section;
8059 bfd *abfd = get_section_bfd_owner (section);
8060 struct dwarf2_section_info *abbrev_section;
8061 struct dwarf2_cu cu;
8062 const gdb_byte *begin_info_ptr, *info_ptr;
8063 struct die_reader_specs reader;
8064 struct cleanup *cleanups;
8065 struct die_info *comp_unit_die;
8068 if (dwarf_die_debug)
8069 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8070 this_cu->is_debug_types ? "type" : "comp",
8071 to_underlying (this_cu->sect_off));
8073 gdb_assert (this_cu->cu == NULL);
8075 abbrev_section = (dwo_file != NULL
8076 ? &dwo_file->sections.abbrev
8077 : get_abbrev_section_for_cu (this_cu));
8079 /* This is cheap if the section is already read in. */
8080 dwarf2_read_section (objfile, section);
8082 init_one_comp_unit (&cu, this_cu);
8084 cleanups = make_cleanup (free_stack_comp_unit, &cu);
8086 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8087 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8088 &cu.header, section,
8089 abbrev_section, info_ptr,
8090 (this_cu->is_debug_types
8092 : rcuh_kind::COMPILE));
8094 this_cu->length = get_cu_length (&cu.header);
8096 /* Skip dummy compilation units. */
8097 if (info_ptr >= begin_info_ptr + this_cu->length
8098 || peek_abbrev_code (abfd, info_ptr) == 0)
8100 do_cleanups (cleanups);
8104 dwarf2_read_abbrevs (&cu, abbrev_section);
8105 make_cleanup (dwarf2_free_abbrev_table, &cu);
8107 init_cu_die_reader (&reader, &cu, section, dwo_file);
8108 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8110 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8112 do_cleanups (cleanups);
8115 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8116 does not lookup the specified DWO file.
8117 This cannot be used to read DWO files.
8119 THIS_CU->cu is always freed when done.
8120 This is done in order to not leave THIS_CU->cu in a state where we have
8121 to care whether it refers to the "main" CU or the DWO CU.
8122 We can revisit this if the data shows there's a performance issue. */
8125 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8126 die_reader_func_ftype *die_reader_func,
8129 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8132 /* Type Unit Groups.
8134 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8135 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8136 so that all types coming from the same compilation (.o file) are grouped
8137 together. A future step could be to put the types in the same symtab as
8138 the CU the types ultimately came from. */
8141 hash_type_unit_group (const void *item)
8143 const struct type_unit_group *tu_group
8144 = (const struct type_unit_group *) item;
8146 return hash_stmt_list_entry (&tu_group->hash);
8150 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8152 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8153 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8155 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8158 /* Allocate a hash table for type unit groups. */
8161 allocate_type_unit_groups_table (struct objfile *objfile)
8163 return htab_create_alloc_ex (3,
8164 hash_type_unit_group,
8167 &objfile->objfile_obstack,
8168 hashtab_obstack_allocate,
8169 dummy_obstack_deallocate);
8172 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8173 partial symtabs. We combine several TUs per psymtab to not let the size
8174 of any one psymtab grow too big. */
8175 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8176 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8178 /* Helper routine for get_type_unit_group.
8179 Create the type_unit_group object used to hold one or more TUs. */
8181 static struct type_unit_group *
8182 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8184 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
8185 struct objfile *objfile = dwarf2_per_objfile->objfile;
8186 struct dwarf2_per_cu_data *per_cu;
8187 struct type_unit_group *tu_group;
8189 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8190 struct type_unit_group);
8191 per_cu = &tu_group->per_cu;
8192 per_cu->dwarf2_per_objfile = cu->dwarf2_per_objfile;
8194 if (dwarf2_per_objfile->using_index)
8196 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8197 struct dwarf2_per_cu_quick_data);
8201 unsigned int line_offset = to_underlying (line_offset_struct);
8202 struct partial_symtab *pst;
8205 /* Give the symtab a useful name for debug purposes. */
8206 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8207 name = xstrprintf ("<type_units_%d>",
8208 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8210 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8212 pst = create_partial_symtab (per_cu, name);
8218 tu_group->hash.dwo_unit = cu->dwo_unit;
8219 tu_group->hash.line_sect_off = line_offset_struct;
8224 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8225 STMT_LIST is a DW_AT_stmt_list attribute. */
8227 static struct type_unit_group *
8228 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8230 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
8231 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8232 struct type_unit_group *tu_group;
8234 unsigned int line_offset;
8235 struct type_unit_group type_unit_group_for_lookup;
8237 if (dwarf2_per_objfile->type_unit_groups == NULL)
8239 dwarf2_per_objfile->type_unit_groups =
8240 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8243 /* Do we need to create a new group, or can we use an existing one? */
8247 line_offset = DW_UNSND (stmt_list);
8248 ++tu_stats->nr_symtab_sharers;
8252 /* Ugh, no stmt_list. Rare, but we have to handle it.
8253 We can do various things here like create one group per TU or
8254 spread them over multiple groups to split up the expansion work.
8255 To avoid worst case scenarios (too many groups or too large groups)
8256 we, umm, group them in bunches. */
8257 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8258 | (tu_stats->nr_stmt_less_type_units
8259 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8260 ++tu_stats->nr_stmt_less_type_units;
8263 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8264 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8265 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8266 &type_unit_group_for_lookup, INSERT);
8269 tu_group = (struct type_unit_group *) *slot;
8270 gdb_assert (tu_group != NULL);
8274 sect_offset line_offset_struct = (sect_offset) line_offset;
8275 tu_group = create_type_unit_group (cu, line_offset_struct);
8277 ++tu_stats->nr_symtabs;
8283 /* Partial symbol tables. */
8285 /* Create a psymtab named NAME and assign it to PER_CU.
8287 The caller must fill in the following details:
8288 dirname, textlow, texthigh. */
8290 static struct partial_symtab *
8291 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8293 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8294 struct partial_symtab *pst;
8296 pst = start_psymtab_common (objfile, name, 0,
8297 objfile->global_psymbols,
8298 objfile->static_psymbols);
8300 pst->psymtabs_addrmap_supported = 1;
8302 /* This is the glue that links PST into GDB's symbol API. */
8303 pst->read_symtab_private = per_cu;
8304 pst->read_symtab = dwarf2_read_symtab;
8305 per_cu->v.psymtab = pst;
8310 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8313 struct process_psymtab_comp_unit_data
8315 /* True if we are reading a DW_TAG_partial_unit. */
8317 int want_partial_unit;
8319 /* The "pretend" language that is used if the CU doesn't declare a
8322 enum language pretend_language;
8325 /* die_reader_func for process_psymtab_comp_unit. */
8328 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8329 const gdb_byte *info_ptr,
8330 struct die_info *comp_unit_die,
8334 struct dwarf2_cu *cu = reader->cu;
8335 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
8336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8337 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8339 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8340 struct partial_symtab *pst;
8341 enum pc_bounds_kind cu_bounds_kind;
8342 const char *filename;
8343 struct process_psymtab_comp_unit_data *info
8344 = (struct process_psymtab_comp_unit_data *) data;
8346 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8349 gdb_assert (! per_cu->is_debug_types);
8351 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8353 cu->list_in_scope = &file_symbols;
8355 /* Allocate a new partial symbol table structure. */
8356 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8357 if (filename == NULL)
8360 pst = create_partial_symtab (per_cu, filename);
8362 /* This must be done before calling dwarf2_build_include_psymtabs. */
8363 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8365 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8367 dwarf2_find_base_address (comp_unit_die, cu);
8369 /* Possibly set the default values of LOWPC and HIGHPC from
8371 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8372 &best_highpc, cu, pst);
8373 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8374 /* Store the contiguous range if it is not empty; it can be empty for
8375 CUs with no code. */
8376 addrmap_set_empty (objfile->psymtabs_addrmap,
8377 gdbarch_adjust_dwarf2_addr (gdbarch,
8378 best_lowpc + baseaddr),
8379 gdbarch_adjust_dwarf2_addr (gdbarch,
8380 best_highpc + baseaddr) - 1,
8383 /* Check if comp unit has_children.
8384 If so, read the rest of the partial symbols from this comp unit.
8385 If not, there's no more debug_info for this comp unit. */
8388 struct partial_die_info *first_die;
8389 CORE_ADDR lowpc, highpc;
8391 lowpc = ((CORE_ADDR) -1);
8392 highpc = ((CORE_ADDR) 0);
8394 first_die = load_partial_dies (reader, info_ptr, 1);
8396 scan_partial_symbols (first_die, &lowpc, &highpc,
8397 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8399 /* If we didn't find a lowpc, set it to highpc to avoid
8400 complaints from `maint check'. */
8401 if (lowpc == ((CORE_ADDR) -1))
8404 /* If the compilation unit didn't have an explicit address range,
8405 then use the information extracted from its child dies. */
8406 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8409 best_highpc = highpc;
8412 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8413 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8415 end_psymtab_common (objfile, pst);
8417 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8420 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8421 struct dwarf2_per_cu_data *iter;
8423 /* Fill in 'dependencies' here; we fill in 'users' in a
8425 pst->number_of_dependencies = len;
8427 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8429 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8432 pst->dependencies[i] = iter->v.psymtab;
8434 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8437 /* Get the list of files included in the current compilation unit,
8438 and build a psymtab for each of them. */
8439 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8441 if (dwarf_read_debug)
8443 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8445 fprintf_unfiltered (gdb_stdlog,
8446 "Psymtab for %s unit @0x%x: %s - %s"
8447 ", %d global, %d static syms\n",
8448 per_cu->is_debug_types ? "type" : "comp",
8449 to_underlying (per_cu->sect_off),
8450 paddress (gdbarch, pst->textlow),
8451 paddress (gdbarch, pst->texthigh),
8452 pst->n_global_syms, pst->n_static_syms);
8456 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8457 Process compilation unit THIS_CU for a psymtab. */
8460 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8461 int want_partial_unit,
8462 enum language pretend_language)
8464 /* If this compilation unit was already read in, free the
8465 cached copy in order to read it in again. This is
8466 necessary because we skipped some symbols when we first
8467 read in the compilation unit (see load_partial_dies).
8468 This problem could be avoided, but the benefit is unclear. */
8469 if (this_cu->cu != NULL)
8470 free_one_cached_comp_unit (this_cu);
8472 if (this_cu->is_debug_types)
8473 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8477 process_psymtab_comp_unit_data info;
8478 info.want_partial_unit = want_partial_unit;
8479 info.pretend_language = pretend_language;
8480 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8481 process_psymtab_comp_unit_reader, &info);
8484 /* Age out any secondary CUs. */
8485 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8488 /* Reader function for build_type_psymtabs. */
8491 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8492 const gdb_byte *info_ptr,
8493 struct die_info *type_unit_die,
8497 struct dwarf2_per_objfile *dwarf2_per_objfile
8498 = reader->cu->dwarf2_per_objfile;
8499 struct objfile *objfile = dwarf2_per_objfile->objfile;
8500 struct dwarf2_cu *cu = reader->cu;
8501 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8502 struct signatured_type *sig_type;
8503 struct type_unit_group *tu_group;
8504 struct attribute *attr;
8505 struct partial_die_info *first_die;
8506 CORE_ADDR lowpc, highpc;
8507 struct partial_symtab *pst;
8509 gdb_assert (data == NULL);
8510 gdb_assert (per_cu->is_debug_types);
8511 sig_type = (struct signatured_type *) per_cu;
8516 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8517 tu_group = get_type_unit_group (cu, attr);
8519 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8521 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8522 cu->list_in_scope = &file_symbols;
8523 pst = create_partial_symtab (per_cu, "");
8526 first_die = load_partial_dies (reader, info_ptr, 1);
8528 lowpc = (CORE_ADDR) -1;
8529 highpc = (CORE_ADDR) 0;
8530 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8532 end_psymtab_common (objfile, pst);
8535 /* Struct used to sort TUs by their abbreviation table offset. */
8537 struct tu_abbrev_offset
8539 struct signatured_type *sig_type;
8540 sect_offset abbrev_offset;
8543 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8546 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8548 const struct tu_abbrev_offset * const *a
8549 = (const struct tu_abbrev_offset * const*) ap;
8550 const struct tu_abbrev_offset * const *b
8551 = (const struct tu_abbrev_offset * const*) bp;
8552 sect_offset aoff = (*a)->abbrev_offset;
8553 sect_offset boff = (*b)->abbrev_offset;
8555 return (aoff > boff) - (aoff < boff);
8558 /* Efficiently read all the type units.
8559 This does the bulk of the work for build_type_psymtabs.
8561 The efficiency is because we sort TUs by the abbrev table they use and
8562 only read each abbrev table once. In one program there are 200K TUs
8563 sharing 8K abbrev tables.
8565 The main purpose of this function is to support building the
8566 dwarf2_per_objfile->type_unit_groups table.
8567 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8568 can collapse the search space by grouping them by stmt_list.
8569 The savings can be significant, in the same program from above the 200K TUs
8570 share 8K stmt_list tables.
8572 FUNC is expected to call get_type_unit_group, which will create the
8573 struct type_unit_group if necessary and add it to
8574 dwarf2_per_objfile->type_unit_groups. */
8577 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8579 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8580 struct cleanup *cleanups;
8581 struct abbrev_table *abbrev_table;
8582 sect_offset abbrev_offset;
8583 struct tu_abbrev_offset *sorted_by_abbrev;
8586 /* It's up to the caller to not call us multiple times. */
8587 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8589 if (dwarf2_per_objfile->n_type_units == 0)
8592 /* TUs typically share abbrev tables, and there can be way more TUs than
8593 abbrev tables. Sort by abbrev table to reduce the number of times we
8594 read each abbrev table in.
8595 Alternatives are to punt or to maintain a cache of abbrev tables.
8596 This is simpler and efficient enough for now.
8598 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8599 symtab to use). Typically TUs with the same abbrev offset have the same
8600 stmt_list value too so in practice this should work well.
8602 The basic algorithm here is:
8604 sort TUs by abbrev table
8605 for each TU with same abbrev table:
8606 read abbrev table if first user
8607 read TU top level DIE
8608 [IWBN if DWO skeletons had DW_AT_stmt_list]
8611 if (dwarf_read_debug)
8612 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8614 /* Sort in a separate table to maintain the order of all_type_units
8615 for .gdb_index: TU indices directly index all_type_units. */
8616 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8617 dwarf2_per_objfile->n_type_units);
8618 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8620 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8622 sorted_by_abbrev[i].sig_type = sig_type;
8623 sorted_by_abbrev[i].abbrev_offset =
8624 read_abbrev_offset (dwarf2_per_objfile,
8625 sig_type->per_cu.section,
8626 sig_type->per_cu.sect_off);
8628 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8629 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8630 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8632 abbrev_offset = (sect_offset) ~(unsigned) 0;
8633 abbrev_table = NULL;
8634 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8636 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8638 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8640 /* Switch to the next abbrev table if necessary. */
8641 if (abbrev_table == NULL
8642 || tu->abbrev_offset != abbrev_offset)
8644 if (abbrev_table != NULL)
8646 abbrev_table_free (abbrev_table);
8647 /* Reset to NULL in case abbrev_table_read_table throws
8648 an error: abbrev_table_free_cleanup will get called. */
8649 abbrev_table = NULL;
8651 abbrev_offset = tu->abbrev_offset;
8653 abbrev_table_read_table (dwarf2_per_objfile,
8654 &dwarf2_per_objfile->abbrev,
8656 ++tu_stats->nr_uniq_abbrev_tables;
8659 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8660 build_type_psymtabs_reader, NULL);
8663 do_cleanups (cleanups);
8666 /* Print collected type unit statistics. */
8669 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8671 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8673 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8674 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8675 dwarf2_per_objfile->n_type_units);
8676 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8677 tu_stats->nr_uniq_abbrev_tables);
8678 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8679 tu_stats->nr_symtabs);
8680 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8681 tu_stats->nr_symtab_sharers);
8682 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8683 tu_stats->nr_stmt_less_type_units);
8684 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8685 tu_stats->nr_all_type_units_reallocs);
8688 /* Traversal function for build_type_psymtabs. */
8691 build_type_psymtab_dependencies (void **slot, void *info)
8693 struct dwarf2_per_objfile *dwarf2_per_objfile
8694 = (struct dwarf2_per_objfile *) info;
8695 struct objfile *objfile = dwarf2_per_objfile->objfile;
8696 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8697 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8698 struct partial_symtab *pst = per_cu->v.psymtab;
8699 int len = VEC_length (sig_type_ptr, tu_group->tus);
8700 struct signatured_type *iter;
8703 gdb_assert (len > 0);
8704 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8706 pst->number_of_dependencies = len;
8708 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8710 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8713 gdb_assert (iter->per_cu.is_debug_types);
8714 pst->dependencies[i] = iter->per_cu.v.psymtab;
8715 iter->type_unit_group = tu_group;
8718 VEC_free (sig_type_ptr, tu_group->tus);
8723 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8724 Build partial symbol tables for the .debug_types comp-units. */
8727 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8729 if (! create_all_type_units (dwarf2_per_objfile))
8732 build_type_psymtabs_1 (dwarf2_per_objfile);
8735 /* Traversal function for process_skeletonless_type_unit.
8736 Read a TU in a DWO file and build partial symbols for it. */
8739 process_skeletonless_type_unit (void **slot, void *info)
8741 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8742 struct dwarf2_per_objfile *dwarf2_per_objfile
8743 = (struct dwarf2_per_objfile *) info;
8744 struct signatured_type find_entry, *entry;
8746 /* If this TU doesn't exist in the global table, add it and read it in. */
8748 if (dwarf2_per_objfile->signatured_types == NULL)
8750 dwarf2_per_objfile->signatured_types
8751 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8754 find_entry.signature = dwo_unit->signature;
8755 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8757 /* If we've already seen this type there's nothing to do. What's happening
8758 is we're doing our own version of comdat-folding here. */
8762 /* This does the job that create_all_type_units would have done for
8764 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8765 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8768 /* This does the job that build_type_psymtabs_1 would have done. */
8769 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8770 build_type_psymtabs_reader, NULL);
8775 /* Traversal function for process_skeletonless_type_units. */
8778 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8780 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8782 if (dwo_file->tus != NULL)
8784 htab_traverse_noresize (dwo_file->tus,
8785 process_skeletonless_type_unit, info);
8791 /* Scan all TUs of DWO files, verifying we've processed them.
8792 This is needed in case a TU was emitted without its skeleton.
8793 Note: This can't be done until we know what all the DWO files are. */
8796 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8798 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8799 if (get_dwp_file (dwarf2_per_objfile) == NULL
8800 && dwarf2_per_objfile->dwo_files != NULL)
8802 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8803 process_dwo_file_for_skeletonless_type_units,
8804 dwarf2_per_objfile);
8808 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8811 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8815 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8817 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8818 struct partial_symtab *pst = per_cu->v.psymtab;
8824 for (j = 0; j < pst->number_of_dependencies; ++j)
8826 /* Set the 'user' field only if it is not already set. */
8827 if (pst->dependencies[j]->user == NULL)
8828 pst->dependencies[j]->user = pst;
8833 /* Build the partial symbol table by doing a quick pass through the
8834 .debug_info and .debug_abbrev sections. */
8837 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8839 struct cleanup *back_to;
8841 struct objfile *objfile = dwarf2_per_objfile->objfile;
8843 if (dwarf_read_debug)
8845 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8846 objfile_name (objfile));
8849 dwarf2_per_objfile->reading_partial_symbols = 1;
8851 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8853 /* Any cached compilation units will be linked by the per-objfile
8854 read_in_chain. Make sure to free them when we're done. */
8855 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8857 build_type_psymtabs (dwarf2_per_objfile);
8859 create_all_comp_units (dwarf2_per_objfile);
8861 /* Create a temporary address map on a temporary obstack. We later
8862 copy this to the final obstack. */
8863 auto_obstack temp_obstack;
8865 scoped_restore save_psymtabs_addrmap
8866 = make_scoped_restore (&objfile->psymtabs_addrmap,
8867 addrmap_create_mutable (&temp_obstack));
8869 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8871 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8873 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8876 /* This has to wait until we read the CUs, we need the list of DWOs. */
8877 process_skeletonless_type_units (dwarf2_per_objfile);
8879 /* Now that all TUs have been processed we can fill in the dependencies. */
8880 if (dwarf2_per_objfile->type_unit_groups != NULL)
8882 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8883 build_type_psymtab_dependencies, dwarf2_per_objfile);
8886 if (dwarf_read_debug)
8887 print_tu_stats (dwarf2_per_objfile);
8889 set_partial_user (dwarf2_per_objfile);
8891 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8892 &objfile->objfile_obstack);
8893 /* At this point we want to keep the address map. */
8894 save_psymtabs_addrmap.release ();
8896 do_cleanups (back_to);
8898 if (dwarf_read_debug)
8899 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8900 objfile_name (objfile));
8903 /* die_reader_func for load_partial_comp_unit. */
8906 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8907 const gdb_byte *info_ptr,
8908 struct die_info *comp_unit_die,
8912 struct dwarf2_cu *cu = reader->cu;
8914 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8916 /* Check if comp unit has_children.
8917 If so, read the rest of the partial symbols from this comp unit.
8918 If not, there's no more debug_info for this comp unit. */
8920 load_partial_dies (reader, info_ptr, 0);
8923 /* Load the partial DIEs for a secondary CU into memory.
8924 This is also used when rereading a primary CU with load_all_dies. */
8927 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8929 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8930 load_partial_comp_unit_reader, NULL);
8934 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8935 struct dwarf2_section_info *section,
8936 struct dwarf2_section_info *abbrev_section,
8937 unsigned int is_dwz,
8940 struct dwarf2_per_cu_data ***all_comp_units)
8942 const gdb_byte *info_ptr;
8943 struct objfile *objfile = dwarf2_per_objfile->objfile;
8945 if (dwarf_read_debug)
8946 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8947 get_section_name (section),
8948 get_section_file_name (section));
8950 dwarf2_read_section (objfile, section);
8952 info_ptr = section->buffer;
8954 while (info_ptr < section->buffer + section->size)
8956 struct dwarf2_per_cu_data *this_cu;
8958 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8960 comp_unit_head cu_header;
8961 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8962 abbrev_section, info_ptr,
8963 rcuh_kind::COMPILE);
8965 /* Save the compilation unit for later lookup. */
8966 if (cu_header.unit_type != DW_UT_type)
8968 this_cu = XOBNEW (&objfile->objfile_obstack,
8969 struct dwarf2_per_cu_data);
8970 memset (this_cu, 0, sizeof (*this_cu));
8974 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8975 struct signatured_type);
8976 memset (sig_type, 0, sizeof (*sig_type));
8977 sig_type->signature = cu_header.signature;
8978 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8979 this_cu = &sig_type->per_cu;
8981 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8982 this_cu->sect_off = sect_off;
8983 this_cu->length = cu_header.length + cu_header.initial_length_size;
8984 this_cu->is_dwz = is_dwz;
8985 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8986 this_cu->section = section;
8988 if (*n_comp_units == *n_allocated)
8991 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8992 *all_comp_units, *n_allocated);
8994 (*all_comp_units)[*n_comp_units] = this_cu;
8997 info_ptr = info_ptr + this_cu->length;
9001 /* Create a list of all compilation units in OBJFILE.
9002 This is only done for -readnow and building partial symtabs. */
9005 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9009 struct dwarf2_per_cu_data **all_comp_units;
9010 struct dwz_file *dwz;
9011 struct objfile *objfile = dwarf2_per_objfile->objfile;
9015 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9017 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9018 &dwarf2_per_objfile->abbrev, 0,
9019 &n_allocated, &n_comp_units, &all_comp_units);
9021 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9023 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9024 1, &n_allocated, &n_comp_units,
9027 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9028 struct dwarf2_per_cu_data *,
9030 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9031 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9032 xfree (all_comp_units);
9033 dwarf2_per_objfile->n_comp_units = n_comp_units;
9036 /* Process all loaded DIEs for compilation unit CU, starting at
9037 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9038 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9039 DW_AT_ranges). See the comments of add_partial_subprogram on how
9040 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9043 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9044 CORE_ADDR *highpc, int set_addrmap,
9045 struct dwarf2_cu *cu)
9047 struct partial_die_info *pdi;
9049 /* Now, march along the PDI's, descending into ones which have
9050 interesting children but skipping the children of the other ones,
9051 until we reach the end of the compilation unit. */
9057 fixup_partial_die (pdi, cu);
9059 /* Anonymous namespaces or modules have no name but have interesting
9060 children, so we need to look at them. Ditto for anonymous
9063 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9064 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9065 || pdi->tag == DW_TAG_imported_unit)
9069 case DW_TAG_subprogram:
9070 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9072 case DW_TAG_constant:
9073 case DW_TAG_variable:
9074 case DW_TAG_typedef:
9075 case DW_TAG_union_type:
9076 if (!pdi->is_declaration)
9078 add_partial_symbol (pdi, cu);
9081 case DW_TAG_class_type:
9082 case DW_TAG_interface_type:
9083 case DW_TAG_structure_type:
9084 if (!pdi->is_declaration)
9086 add_partial_symbol (pdi, cu);
9088 if (cu->language == language_rust && pdi->has_children)
9089 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9092 case DW_TAG_enumeration_type:
9093 if (!pdi->is_declaration)
9094 add_partial_enumeration (pdi, cu);
9096 case DW_TAG_base_type:
9097 case DW_TAG_subrange_type:
9098 /* File scope base type definitions are added to the partial
9100 add_partial_symbol (pdi, cu);
9102 case DW_TAG_namespace:
9103 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9106 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9108 case DW_TAG_imported_unit:
9110 struct dwarf2_per_cu_data *per_cu;
9112 /* For now we don't handle imported units in type units. */
9113 if (cu->per_cu->is_debug_types)
9115 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9116 " supported in type units [in module %s]"),
9117 objfile_name (cu->dwarf2_per_objfile->objfile));
9120 per_cu = dwarf2_find_containing_comp_unit
9121 (pdi->d.sect_off, pdi->is_dwz,
9122 cu->dwarf2_per_objfile);
9124 /* Go read the partial unit, if needed. */
9125 if (per_cu->v.psymtab == NULL)
9126 process_psymtab_comp_unit (per_cu, 1, cu->language);
9128 VEC_safe_push (dwarf2_per_cu_ptr,
9129 cu->per_cu->imported_symtabs, per_cu);
9132 case DW_TAG_imported_declaration:
9133 add_partial_symbol (pdi, cu);
9140 /* If the die has a sibling, skip to the sibling. */
9142 pdi = pdi->die_sibling;
9146 /* Functions used to compute the fully scoped name of a partial DIE.
9148 Normally, this is simple. For C++, the parent DIE's fully scoped
9149 name is concatenated with "::" and the partial DIE's name.
9150 Enumerators are an exception; they use the scope of their parent
9151 enumeration type, i.e. the name of the enumeration type is not
9152 prepended to the enumerator.
9154 There are two complexities. One is DW_AT_specification; in this
9155 case "parent" means the parent of the target of the specification,
9156 instead of the direct parent of the DIE. The other is compilers
9157 which do not emit DW_TAG_namespace; in this case we try to guess
9158 the fully qualified name of structure types from their members'
9159 linkage names. This must be done using the DIE's children rather
9160 than the children of any DW_AT_specification target. We only need
9161 to do this for structures at the top level, i.e. if the target of
9162 any DW_AT_specification (if any; otherwise the DIE itself) does not
9165 /* Compute the scope prefix associated with PDI's parent, in
9166 compilation unit CU. The result will be allocated on CU's
9167 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9168 field. NULL is returned if no prefix is necessary. */
9170 partial_die_parent_scope (struct partial_die_info *pdi,
9171 struct dwarf2_cu *cu)
9173 const char *grandparent_scope;
9174 struct partial_die_info *parent, *real_pdi;
9176 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9177 then this means the parent of the specification DIE. */
9180 while (real_pdi->has_specification)
9181 real_pdi = find_partial_die (real_pdi->spec_offset,
9182 real_pdi->spec_is_dwz, cu);
9184 parent = real_pdi->die_parent;
9188 if (parent->scope_set)
9189 return parent->scope;
9191 fixup_partial_die (parent, cu);
9193 grandparent_scope = partial_die_parent_scope (parent, cu);
9195 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9196 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9197 Work around this problem here. */
9198 if (cu->language == language_cplus
9199 && parent->tag == DW_TAG_namespace
9200 && strcmp (parent->name, "::") == 0
9201 && grandparent_scope == NULL)
9203 parent->scope = NULL;
9204 parent->scope_set = 1;
9208 if (pdi->tag == DW_TAG_enumerator)
9209 /* Enumerators should not get the name of the enumeration as a prefix. */
9210 parent->scope = grandparent_scope;
9211 else if (parent->tag == DW_TAG_namespace
9212 || parent->tag == DW_TAG_module
9213 || parent->tag == DW_TAG_structure_type
9214 || parent->tag == DW_TAG_class_type
9215 || parent->tag == DW_TAG_interface_type
9216 || parent->tag == DW_TAG_union_type
9217 || parent->tag == DW_TAG_enumeration_type)
9219 if (grandparent_scope == NULL)
9220 parent->scope = parent->name;
9222 parent->scope = typename_concat (&cu->comp_unit_obstack,
9224 parent->name, 0, cu);
9228 /* FIXME drow/2004-04-01: What should we be doing with
9229 function-local names? For partial symbols, we should probably be
9231 complaint (&symfile_complaints,
9232 _("unhandled containing DIE tag %d for DIE at %d"),
9233 parent->tag, to_underlying (pdi->sect_off));
9234 parent->scope = grandparent_scope;
9237 parent->scope_set = 1;
9238 return parent->scope;
9241 /* Return the fully scoped name associated with PDI, from compilation unit
9242 CU. The result will be allocated with malloc. */
9245 partial_die_full_name (struct partial_die_info *pdi,
9246 struct dwarf2_cu *cu)
9248 const char *parent_scope;
9250 /* If this is a template instantiation, we can not work out the
9251 template arguments from partial DIEs. So, unfortunately, we have
9252 to go through the full DIEs. At least any work we do building
9253 types here will be reused if full symbols are loaded later. */
9254 if (pdi->has_template_arguments)
9256 fixup_partial_die (pdi, cu);
9258 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9260 struct die_info *die;
9261 struct attribute attr;
9262 struct dwarf2_cu *ref_cu = cu;
9264 /* DW_FORM_ref_addr is using section offset. */
9265 attr.name = (enum dwarf_attribute) 0;
9266 attr.form = DW_FORM_ref_addr;
9267 attr.u.unsnd = to_underlying (pdi->sect_off);
9268 die = follow_die_ref (NULL, &attr, &ref_cu);
9270 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9274 parent_scope = partial_die_parent_scope (pdi, cu);
9275 if (parent_scope == NULL)
9278 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9282 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9284 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
9285 struct objfile *objfile = dwarf2_per_objfile->objfile;
9286 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9288 const char *actual_name = NULL;
9290 char *built_actual_name;
9292 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9294 built_actual_name = partial_die_full_name (pdi, cu);
9295 if (built_actual_name != NULL)
9296 actual_name = built_actual_name;
9298 if (actual_name == NULL)
9299 actual_name = pdi->name;
9303 case DW_TAG_subprogram:
9304 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9305 if (pdi->is_external || cu->language == language_ada)
9307 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9308 of the global scope. But in Ada, we want to be able to access
9309 nested procedures globally. So all Ada subprograms are stored
9310 in the global scope. */
9311 add_psymbol_to_list (actual_name, strlen (actual_name),
9312 built_actual_name != NULL,
9313 VAR_DOMAIN, LOC_BLOCK,
9314 &objfile->global_psymbols,
9315 addr, cu->language, objfile);
9319 add_psymbol_to_list (actual_name, strlen (actual_name),
9320 built_actual_name != NULL,
9321 VAR_DOMAIN, LOC_BLOCK,
9322 &objfile->static_psymbols,
9323 addr, cu->language, objfile);
9326 if (pdi->main_subprogram && actual_name != NULL)
9327 set_objfile_main_name (objfile, actual_name, cu->language);
9329 case DW_TAG_constant:
9331 std::vector<partial_symbol *> *list;
9333 if (pdi->is_external)
9334 list = &objfile->global_psymbols;
9336 list = &objfile->static_psymbols;
9337 add_psymbol_to_list (actual_name, strlen (actual_name),
9338 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9339 list, 0, cu->language, objfile);
9342 case DW_TAG_variable:
9344 addr = decode_locdesc (pdi->d.locdesc, cu);
9348 && !dwarf2_per_objfile->has_section_at_zero)
9350 /* A global or static variable may also have been stripped
9351 out by the linker if unused, in which case its address
9352 will be nullified; do not add such variables into partial
9353 symbol table then. */
9355 else if (pdi->is_external)
9358 Don't enter into the minimal symbol tables as there is
9359 a minimal symbol table entry from the ELF symbols already.
9360 Enter into partial symbol table if it has a location
9361 descriptor or a type.
9362 If the location descriptor is missing, new_symbol will create
9363 a LOC_UNRESOLVED symbol, the address of the variable will then
9364 be determined from the minimal symbol table whenever the variable
9366 The address for the partial symbol table entry is not
9367 used by GDB, but it comes in handy for debugging partial symbol
9370 if (pdi->d.locdesc || pdi->has_type)
9371 add_psymbol_to_list (actual_name, strlen (actual_name),
9372 built_actual_name != NULL,
9373 VAR_DOMAIN, LOC_STATIC,
9374 &objfile->global_psymbols,
9376 cu->language, objfile);
9380 int has_loc = pdi->d.locdesc != NULL;
9382 /* Static Variable. Skip symbols whose value we cannot know (those
9383 without location descriptors or constant values). */
9384 if (!has_loc && !pdi->has_const_value)
9386 xfree (built_actual_name);
9390 add_psymbol_to_list (actual_name, strlen (actual_name),
9391 built_actual_name != NULL,
9392 VAR_DOMAIN, LOC_STATIC,
9393 &objfile->static_psymbols,
9394 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9395 cu->language, objfile);
9398 case DW_TAG_typedef:
9399 case DW_TAG_base_type:
9400 case DW_TAG_subrange_type:
9401 add_psymbol_to_list (actual_name, strlen (actual_name),
9402 built_actual_name != NULL,
9403 VAR_DOMAIN, LOC_TYPEDEF,
9404 &objfile->static_psymbols,
9405 0, cu->language, objfile);
9407 case DW_TAG_imported_declaration:
9408 case DW_TAG_namespace:
9409 add_psymbol_to_list (actual_name, strlen (actual_name),
9410 built_actual_name != NULL,
9411 VAR_DOMAIN, LOC_TYPEDEF,
9412 &objfile->global_psymbols,
9413 0, cu->language, objfile);
9416 add_psymbol_to_list (actual_name, strlen (actual_name),
9417 built_actual_name != NULL,
9418 MODULE_DOMAIN, LOC_TYPEDEF,
9419 &objfile->global_psymbols,
9420 0, cu->language, objfile);
9422 case DW_TAG_class_type:
9423 case DW_TAG_interface_type:
9424 case DW_TAG_structure_type:
9425 case DW_TAG_union_type:
9426 case DW_TAG_enumeration_type:
9427 /* Skip external references. The DWARF standard says in the section
9428 about "Structure, Union, and Class Type Entries": "An incomplete
9429 structure, union or class type is represented by a structure,
9430 union or class entry that does not have a byte size attribute
9431 and that has a DW_AT_declaration attribute." */
9432 if (!pdi->has_byte_size && pdi->is_declaration)
9434 xfree (built_actual_name);
9438 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9439 static vs. global. */
9440 add_psymbol_to_list (actual_name, strlen (actual_name),
9441 built_actual_name != NULL,
9442 STRUCT_DOMAIN, LOC_TYPEDEF,
9443 cu->language == language_cplus
9444 ? &objfile->global_psymbols
9445 : &objfile->static_psymbols,
9446 0, cu->language, objfile);
9449 case DW_TAG_enumerator:
9450 add_psymbol_to_list (actual_name, strlen (actual_name),
9451 built_actual_name != NULL,
9452 VAR_DOMAIN, LOC_CONST,
9453 cu->language == language_cplus
9454 ? &objfile->global_psymbols
9455 : &objfile->static_psymbols,
9456 0, cu->language, objfile);
9462 xfree (built_actual_name);
9465 /* Read a partial die corresponding to a namespace; also, add a symbol
9466 corresponding to that namespace to the symbol table. NAMESPACE is
9467 the name of the enclosing namespace. */
9470 add_partial_namespace (struct partial_die_info *pdi,
9471 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9472 int set_addrmap, struct dwarf2_cu *cu)
9474 /* Add a symbol for the namespace. */
9476 add_partial_symbol (pdi, cu);
9478 /* Now scan partial symbols in that namespace. */
9480 if (pdi->has_children)
9481 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9484 /* Read a partial die corresponding to a Fortran module. */
9487 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9488 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9490 /* Add a symbol for the namespace. */
9492 add_partial_symbol (pdi, cu);
9494 /* Now scan partial symbols in that module. */
9496 if (pdi->has_children)
9497 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9500 /* Read a partial die corresponding to a subprogram and create a partial
9501 symbol for that subprogram. When the CU language allows it, this
9502 routine also defines a partial symbol for each nested subprogram
9503 that this subprogram contains. If SET_ADDRMAP is true, record the
9504 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9505 and highest PC values found in PDI.
9507 PDI may also be a lexical block, in which case we simply search
9508 recursively for subprograms defined inside that lexical block.
9509 Again, this is only performed when the CU language allows this
9510 type of definitions. */
9513 add_partial_subprogram (struct partial_die_info *pdi,
9514 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9515 int set_addrmap, struct dwarf2_cu *cu)
9517 if (pdi->tag == DW_TAG_subprogram)
9519 if (pdi->has_pc_info)
9521 if (pdi->lowpc < *lowpc)
9522 *lowpc = pdi->lowpc;
9523 if (pdi->highpc > *highpc)
9524 *highpc = pdi->highpc;
9527 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
9528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9533 baseaddr = ANOFFSET (objfile->section_offsets,
9534 SECT_OFF_TEXT (objfile));
9535 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9536 pdi->lowpc + baseaddr);
9537 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9538 pdi->highpc + baseaddr);
9539 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9540 cu->per_cu->v.psymtab);
9544 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9546 if (!pdi->is_declaration)
9547 /* Ignore subprogram DIEs that do not have a name, they are
9548 illegal. Do not emit a complaint at this point, we will
9549 do so when we convert this psymtab into a symtab. */
9551 add_partial_symbol (pdi, cu);
9555 if (! pdi->has_children)
9558 if (cu->language == language_ada)
9560 pdi = pdi->die_child;
9563 fixup_partial_die (pdi, cu);
9564 if (pdi->tag == DW_TAG_subprogram
9565 || pdi->tag == DW_TAG_lexical_block)
9566 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9567 pdi = pdi->die_sibling;
9572 /* Read a partial die corresponding to an enumeration type. */
9575 add_partial_enumeration (struct partial_die_info *enum_pdi,
9576 struct dwarf2_cu *cu)
9578 struct partial_die_info *pdi;
9580 if (enum_pdi->name != NULL)
9581 add_partial_symbol (enum_pdi, cu);
9583 pdi = enum_pdi->die_child;
9586 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9587 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9589 add_partial_symbol (pdi, cu);
9590 pdi = pdi->die_sibling;
9594 /* Return the initial uleb128 in the die at INFO_PTR. */
9597 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9599 unsigned int bytes_read;
9601 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9604 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9605 Return the corresponding abbrev, or NULL if the number is zero (indicating
9606 an empty DIE). In either case *BYTES_READ will be set to the length of
9607 the initial number. */
9609 static struct abbrev_info *
9610 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
9611 struct dwarf2_cu *cu)
9613 bfd *abfd = cu->dwarf2_per_objfile->objfile->obfd;
9614 unsigned int abbrev_number;
9615 struct abbrev_info *abbrev;
9617 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9619 if (abbrev_number == 0)
9622 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
9625 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9626 " at offset 0x%x [in module %s]"),
9627 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9628 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9634 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9635 Returns a pointer to the end of a series of DIEs, terminated by an empty
9636 DIE. Any children of the skipped DIEs will also be skipped. */
9638 static const gdb_byte *
9639 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9641 struct dwarf2_cu *cu = reader->cu;
9642 struct abbrev_info *abbrev;
9643 unsigned int bytes_read;
9647 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9649 return info_ptr + bytes_read;
9651 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9655 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9656 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9657 abbrev corresponding to that skipped uleb128 should be passed in
9658 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9661 static const gdb_byte *
9662 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9663 struct abbrev_info *abbrev)
9665 unsigned int bytes_read;
9666 struct attribute attr;
9667 bfd *abfd = reader->abfd;
9668 struct dwarf2_cu *cu = reader->cu;
9669 const gdb_byte *buffer = reader->buffer;
9670 const gdb_byte *buffer_end = reader->buffer_end;
9671 unsigned int form, i;
9673 for (i = 0; i < abbrev->num_attrs; i++)
9675 /* The only abbrev we care about is DW_AT_sibling. */
9676 if (abbrev->attrs[i].name == DW_AT_sibling)
9678 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9679 if (attr.form == DW_FORM_ref_addr)
9680 complaint (&symfile_complaints,
9681 _("ignoring absolute DW_AT_sibling"));
9684 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9685 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9687 if (sibling_ptr < info_ptr)
9688 complaint (&symfile_complaints,
9689 _("DW_AT_sibling points backwards"));
9690 else if (sibling_ptr > reader->buffer_end)
9691 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9697 /* If it isn't DW_AT_sibling, skip this attribute. */
9698 form = abbrev->attrs[i].form;
9702 case DW_FORM_ref_addr:
9703 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9704 and later it is offset sized. */
9705 if (cu->header.version == 2)
9706 info_ptr += cu->header.addr_size;
9708 info_ptr += cu->header.offset_size;
9710 case DW_FORM_GNU_ref_alt:
9711 info_ptr += cu->header.offset_size;
9714 info_ptr += cu->header.addr_size;
9721 case DW_FORM_flag_present:
9722 case DW_FORM_implicit_const:
9734 case DW_FORM_ref_sig8:
9737 case DW_FORM_data16:
9740 case DW_FORM_string:
9741 read_direct_string (abfd, info_ptr, &bytes_read);
9742 info_ptr += bytes_read;
9744 case DW_FORM_sec_offset:
9746 case DW_FORM_GNU_strp_alt:
9747 info_ptr += cu->header.offset_size;
9749 case DW_FORM_exprloc:
9751 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9752 info_ptr += bytes_read;
9754 case DW_FORM_block1:
9755 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9757 case DW_FORM_block2:
9758 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9760 case DW_FORM_block4:
9761 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9765 case DW_FORM_ref_udata:
9766 case DW_FORM_GNU_addr_index:
9767 case DW_FORM_GNU_str_index:
9768 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9770 case DW_FORM_indirect:
9771 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9772 info_ptr += bytes_read;
9773 /* We need to continue parsing from here, so just go back to
9775 goto skip_attribute;
9778 error (_("Dwarf Error: Cannot handle %s "
9779 "in DWARF reader [in module %s]"),
9780 dwarf_form_name (form),
9781 bfd_get_filename (abfd));
9785 if (abbrev->has_children)
9786 return skip_children (reader, info_ptr);
9791 /* Locate ORIG_PDI's sibling.
9792 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9794 static const gdb_byte *
9795 locate_pdi_sibling (const struct die_reader_specs *reader,
9796 struct partial_die_info *orig_pdi,
9797 const gdb_byte *info_ptr)
9799 /* Do we know the sibling already? */
9801 if (orig_pdi->sibling)
9802 return orig_pdi->sibling;
9804 /* Are there any children to deal with? */
9806 if (!orig_pdi->has_children)
9809 /* Skip the children the long way. */
9811 return skip_children (reader, info_ptr);
9814 /* Expand this partial symbol table into a full symbol table. SELF is
9818 dwarf2_read_symtab (struct partial_symtab *self,
9819 struct objfile *objfile)
9821 struct dwarf2_per_objfile *dwarf2_per_objfile
9822 = get_dwarf2_per_objfile (objfile);
9826 warning (_("bug: psymtab for %s is already read in."),
9833 printf_filtered (_("Reading in symbols for %s..."),
9835 gdb_flush (gdb_stdout);
9838 /* If this psymtab is constructed from a debug-only objfile, the
9839 has_section_at_zero flag will not necessarily be correct. We
9840 can get the correct value for this flag by looking at the data
9841 associated with the (presumably stripped) associated objfile. */
9842 if (objfile->separate_debug_objfile_backlink)
9844 struct dwarf2_per_objfile *dpo_backlink
9845 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9847 dwarf2_per_objfile->has_section_at_zero
9848 = dpo_backlink->has_section_at_zero;
9851 dwarf2_per_objfile->reading_partial_symbols = 0;
9853 psymtab_to_symtab_1 (self);
9855 /* Finish up the debug error message. */
9857 printf_filtered (_("done.\n"));
9860 process_cu_includes (dwarf2_per_objfile);
9863 /* Reading in full CUs. */
9865 /* Add PER_CU to the queue. */
9868 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9869 enum language pretend_language)
9871 struct dwarf2_queue_item *item;
9874 item = XNEW (struct dwarf2_queue_item);
9875 item->per_cu = per_cu;
9876 item->pretend_language = pretend_language;
9879 if (dwarf2_queue == NULL)
9880 dwarf2_queue = item;
9882 dwarf2_queue_tail->next = item;
9884 dwarf2_queue_tail = item;
9887 /* If PER_CU is not yet queued, add it to the queue.
9888 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9890 The result is non-zero if PER_CU was queued, otherwise the result is zero
9891 meaning either PER_CU is already queued or it is already loaded.
9893 N.B. There is an invariant here that if a CU is queued then it is loaded.
9894 The caller is required to load PER_CU if we return non-zero. */
9897 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9898 struct dwarf2_per_cu_data *per_cu,
9899 enum language pretend_language)
9901 /* We may arrive here during partial symbol reading, if we need full
9902 DIEs to process an unusual case (e.g. template arguments). Do
9903 not queue PER_CU, just tell our caller to load its DIEs. */
9904 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9906 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9911 /* Mark the dependence relation so that we don't flush PER_CU
9913 if (dependent_cu != NULL)
9914 dwarf2_add_dependence (dependent_cu, per_cu);
9916 /* If it's already on the queue, we have nothing to do. */
9920 /* If the compilation unit is already loaded, just mark it as
9922 if (per_cu->cu != NULL)
9924 per_cu->cu->last_used = 0;
9928 /* Add it to the queue. */
9929 queue_comp_unit (per_cu, pretend_language);
9934 /* Process the queue. */
9937 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9939 struct dwarf2_queue_item *item, *next_item;
9941 if (dwarf_read_debug)
9943 fprintf_unfiltered (gdb_stdlog,
9944 "Expanding one or more symtabs of objfile %s ...\n",
9945 objfile_name (dwarf2_per_objfile->objfile));
9948 /* The queue starts out with one item, but following a DIE reference
9949 may load a new CU, adding it to the end of the queue. */
9950 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9952 if ((dwarf2_per_objfile->using_index
9953 ? !item->per_cu->v.quick->compunit_symtab
9954 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9955 /* Skip dummy CUs. */
9956 && item->per_cu->cu != NULL)
9958 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9959 unsigned int debug_print_threshold;
9962 if (per_cu->is_debug_types)
9964 struct signatured_type *sig_type =
9965 (struct signatured_type *) per_cu;
9967 sprintf (buf, "TU %s at offset 0x%x",
9968 hex_string (sig_type->signature),
9969 to_underlying (per_cu->sect_off));
9970 /* There can be 100s of TUs.
9971 Only print them in verbose mode. */
9972 debug_print_threshold = 2;
9976 sprintf (buf, "CU at offset 0x%x",
9977 to_underlying (per_cu->sect_off));
9978 debug_print_threshold = 1;
9981 if (dwarf_read_debug >= debug_print_threshold)
9982 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9984 if (per_cu->is_debug_types)
9985 process_full_type_unit (per_cu, item->pretend_language);
9987 process_full_comp_unit (per_cu, item->pretend_language);
9989 if (dwarf_read_debug >= debug_print_threshold)
9990 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9993 item->per_cu->queued = 0;
9994 next_item = item->next;
9998 dwarf2_queue_tail = NULL;
10000 if (dwarf_read_debug)
10002 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10003 objfile_name (dwarf2_per_objfile->objfile));
10007 /* Free all allocated queue entries. This function only releases anything if
10008 an error was thrown; if the queue was processed then it would have been
10009 freed as we went along. */
10012 dwarf2_release_queue (void *dummy)
10014 struct dwarf2_queue_item *item, *last;
10016 item = dwarf2_queue;
10019 /* Anything still marked queued is likely to be in an
10020 inconsistent state, so discard it. */
10021 if (item->per_cu->queued)
10023 if (item->per_cu->cu != NULL)
10024 free_one_cached_comp_unit (item->per_cu);
10025 item->per_cu->queued = 0;
10033 dwarf2_queue = dwarf2_queue_tail = NULL;
10036 /* Read in full symbols for PST, and anything it depends on. */
10039 psymtab_to_symtab_1 (struct partial_symtab *pst)
10041 struct dwarf2_per_cu_data *per_cu;
10047 for (i = 0; i < pst->number_of_dependencies; i++)
10048 if (!pst->dependencies[i]->readin
10049 && pst->dependencies[i]->user == NULL)
10051 /* Inform about additional files that need to be read in. */
10054 /* FIXME: i18n: Need to make this a single string. */
10055 fputs_filtered (" ", gdb_stdout);
10057 fputs_filtered ("and ", gdb_stdout);
10059 printf_filtered ("%s...", pst->dependencies[i]->filename);
10060 wrap_here (""); /* Flush output. */
10061 gdb_flush (gdb_stdout);
10063 psymtab_to_symtab_1 (pst->dependencies[i]);
10066 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10068 if (per_cu == NULL)
10070 /* It's an include file, no symbols to read for it.
10071 Everything is in the parent symtab. */
10076 dw2_do_instantiate_symtab (per_cu);
10079 /* Trivial hash function for die_info: the hash value of a DIE
10080 is its offset in .debug_info for this objfile. */
10083 die_hash (const void *item)
10085 const struct die_info *die = (const struct die_info *) item;
10087 return to_underlying (die->sect_off);
10090 /* Trivial comparison function for die_info structures: two DIEs
10091 are equal if they have the same offset. */
10094 die_eq (const void *item_lhs, const void *item_rhs)
10096 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10097 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10099 return die_lhs->sect_off == die_rhs->sect_off;
10102 /* die_reader_func for load_full_comp_unit.
10103 This is identical to read_signatured_type_reader,
10104 but is kept separate for now. */
10107 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10108 const gdb_byte *info_ptr,
10109 struct die_info *comp_unit_die,
10113 struct dwarf2_cu *cu = reader->cu;
10114 enum language *language_ptr = (enum language *) data;
10116 gdb_assert (cu->die_hash == NULL);
10118 htab_create_alloc_ex (cu->header.length / 12,
10122 &cu->comp_unit_obstack,
10123 hashtab_obstack_allocate,
10124 dummy_obstack_deallocate);
10127 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10128 &info_ptr, comp_unit_die);
10129 cu->dies = comp_unit_die;
10130 /* comp_unit_die is not stored in die_hash, no need. */
10132 /* We try not to read any attributes in this function, because not
10133 all CUs needed for references have been loaded yet, and symbol
10134 table processing isn't initialized. But we have to set the CU language,
10135 or we won't be able to build types correctly.
10136 Similarly, if we do not read the producer, we can not apply
10137 producer-specific interpretation. */
10138 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10141 /* Load the DIEs associated with PER_CU into memory. */
10144 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10145 enum language pretend_language)
10147 gdb_assert (! this_cu->is_debug_types);
10149 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10150 load_full_comp_unit_reader, &pretend_language);
10153 /* Add a DIE to the delayed physname list. */
10156 add_to_method_list (struct type *type, int fnfield_index, int index,
10157 const char *name, struct die_info *die,
10158 struct dwarf2_cu *cu)
10160 struct delayed_method_info mi;
10162 mi.fnfield_index = fnfield_index;
10166 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10169 /* A cleanup for freeing the delayed method list. */
10172 free_delayed_list (void *ptr)
10174 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10175 if (cu->method_list != NULL)
10177 VEC_free (delayed_method_info, cu->method_list);
10178 cu->method_list = NULL;
10182 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10183 "const" / "volatile". If so, decrements LEN by the length of the
10184 modifier and return true. Otherwise return false. */
10188 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10190 size_t mod_len = sizeof (mod) - 1;
10191 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10199 /* Compute the physnames of any methods on the CU's method list.
10201 The computation of method physnames is delayed in order to avoid the
10202 (bad) condition that one of the method's formal parameters is of an as yet
10203 incomplete type. */
10206 compute_delayed_physnames (struct dwarf2_cu *cu)
10209 struct delayed_method_info *mi;
10211 /* Only C++ delays computing physnames. */
10212 if (VEC_empty (delayed_method_info, cu->method_list))
10214 gdb_assert (cu->language == language_cplus);
10216 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10218 const char *physname;
10219 struct fn_fieldlist *fn_flp
10220 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
10221 physname = dwarf2_physname (mi->name, mi->die, cu);
10222 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10223 = physname ? physname : "";
10225 /* Since there's no tag to indicate whether a method is a
10226 const/volatile overload, extract that information out of the
10228 if (physname != NULL)
10230 size_t len = strlen (physname);
10234 if (physname[len] == ')') /* shortcut */
10236 else if (check_modifier (physname, len, " const"))
10237 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10238 else if (check_modifier (physname, len, " volatile"))
10239 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10247 /* Go objects should be embedded in a DW_TAG_module DIE,
10248 and it's not clear if/how imported objects will appear.
10249 To keep Go support simple until that's worked out,
10250 go back through what we've read and create something usable.
10251 We could do this while processing each DIE, and feels kinda cleaner,
10252 but that way is more invasive.
10253 This is to, for example, allow the user to type "p var" or "b main"
10254 without having to specify the package name, and allow lookups
10255 of module.object to work in contexts that use the expression
10259 fixup_go_packaging (struct dwarf2_cu *cu)
10261 char *package_name = NULL;
10262 struct pending *list;
10265 for (list = global_symbols; list != NULL; list = list->next)
10267 for (i = 0; i < list->nsyms; ++i)
10269 struct symbol *sym = list->symbol[i];
10271 if (SYMBOL_LANGUAGE (sym) == language_go
10272 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10274 char *this_package_name = go_symbol_package_name (sym);
10276 if (this_package_name == NULL)
10278 if (package_name == NULL)
10279 package_name = this_package_name;
10282 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
10283 if (strcmp (package_name, this_package_name) != 0)
10284 complaint (&symfile_complaints,
10285 _("Symtab %s has objects from two different Go packages: %s and %s"),
10286 (symbol_symtab (sym) != NULL
10287 ? symtab_to_filename_for_display
10288 (symbol_symtab (sym))
10289 : objfile_name (objfile)),
10290 this_package_name, package_name);
10291 xfree (this_package_name);
10297 if (package_name != NULL)
10299 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
10300 const char *saved_package_name
10301 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10303 strlen (package_name));
10304 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10305 saved_package_name);
10306 struct symbol *sym;
10308 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10310 sym = allocate_symbol (objfile);
10311 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10312 SYMBOL_SET_NAMES (sym, saved_package_name,
10313 strlen (saved_package_name), 0, objfile);
10314 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10315 e.g., "main" finds the "main" module and not C's main(). */
10316 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10317 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10318 SYMBOL_TYPE (sym) = type;
10320 add_symbol_to_list (sym, &global_symbols);
10322 xfree (package_name);
10326 /* Return the symtab for PER_CU. This works properly regardless of
10327 whether we're using the index or psymtabs. */
10329 static struct compunit_symtab *
10330 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10332 return (per_cu->dwarf2_per_objfile->using_index
10333 ? per_cu->v.quick->compunit_symtab
10334 : per_cu->v.psymtab->compunit_symtab);
10337 /* A helper function for computing the list of all symbol tables
10338 included by PER_CU. */
10341 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10342 htab_t all_children, htab_t all_type_symtabs,
10343 struct dwarf2_per_cu_data *per_cu,
10344 struct compunit_symtab *immediate_parent)
10348 struct compunit_symtab *cust;
10349 struct dwarf2_per_cu_data *iter;
10351 slot = htab_find_slot (all_children, per_cu, INSERT);
10354 /* This inclusion and its children have been processed. */
10359 /* Only add a CU if it has a symbol table. */
10360 cust = get_compunit_symtab (per_cu);
10363 /* If this is a type unit only add its symbol table if we haven't
10364 seen it yet (type unit per_cu's can share symtabs). */
10365 if (per_cu->is_debug_types)
10367 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10371 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10372 if (cust->user == NULL)
10373 cust->user = immediate_parent;
10378 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10379 if (cust->user == NULL)
10380 cust->user = immediate_parent;
10385 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10388 recursively_compute_inclusions (result, all_children,
10389 all_type_symtabs, iter, cust);
10393 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10397 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10399 gdb_assert (! per_cu->is_debug_types);
10401 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10404 struct dwarf2_per_cu_data *per_cu_iter;
10405 struct compunit_symtab *compunit_symtab_iter;
10406 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10407 htab_t all_children, all_type_symtabs;
10408 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10410 /* If we don't have a symtab, we can just skip this case. */
10414 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10415 NULL, xcalloc, xfree);
10416 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10417 NULL, xcalloc, xfree);
10420 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10424 recursively_compute_inclusions (&result_symtabs, all_children,
10425 all_type_symtabs, per_cu_iter,
10429 /* Now we have a transitive closure of all the included symtabs. */
10430 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10432 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10433 struct compunit_symtab *, len + 1);
10435 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10436 compunit_symtab_iter);
10438 cust->includes[ix] = compunit_symtab_iter;
10439 cust->includes[len] = NULL;
10441 VEC_free (compunit_symtab_ptr, result_symtabs);
10442 htab_delete (all_children);
10443 htab_delete (all_type_symtabs);
10447 /* Compute the 'includes' field for the symtabs of all the CUs we just
10451 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10454 struct dwarf2_per_cu_data *iter;
10457 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10461 if (! iter->is_debug_types)
10462 compute_compunit_symtab_includes (iter);
10465 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10468 /* Generate full symbol information for PER_CU, whose DIEs have
10469 already been loaded into memory. */
10472 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10473 enum language pretend_language)
10475 struct dwarf2_cu *cu = per_cu->cu;
10476 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10477 struct objfile *objfile = dwarf2_per_objfile->objfile;
10478 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10479 CORE_ADDR lowpc, highpc;
10480 struct compunit_symtab *cust;
10481 struct cleanup *delayed_list_cleanup;
10482 CORE_ADDR baseaddr;
10483 struct block *static_block;
10486 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10489 scoped_free_pendings free_pending;
10490 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10492 cu->list_in_scope = &file_symbols;
10494 cu->language = pretend_language;
10495 cu->language_defn = language_def (cu->language);
10497 /* Do line number decoding in read_file_scope () */
10498 process_die (cu->dies, cu);
10500 /* For now fudge the Go package. */
10501 if (cu->language == language_go)
10502 fixup_go_packaging (cu);
10504 /* Now that we have processed all the DIEs in the CU, all the types
10505 should be complete, and it should now be safe to compute all of the
10507 compute_delayed_physnames (cu);
10508 do_cleanups (delayed_list_cleanup);
10510 /* Some compilers don't define a DW_AT_high_pc attribute for the
10511 compilation unit. If the DW_AT_high_pc is missing, synthesize
10512 it, by scanning the DIE's below the compilation unit. */
10513 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10515 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10516 static_block = end_symtab_get_static_block (addr, 0, 1);
10518 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10519 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10520 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10521 addrmap to help ensure it has an accurate map of pc values belonging to
10523 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10525 cust = end_symtab_from_static_block (static_block,
10526 SECT_OFF_TEXT (objfile), 0);
10530 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10532 /* Set symtab language to language from DW_AT_language. If the
10533 compilation is from a C file generated by language preprocessors, do
10534 not set the language if it was already deduced by start_subfile. */
10535 if (!(cu->language == language_c
10536 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10537 COMPUNIT_FILETABS (cust)->language = cu->language;
10539 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10540 produce DW_AT_location with location lists but it can be possibly
10541 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10542 there were bugs in prologue debug info, fixed later in GCC-4.5
10543 by "unwind info for epilogues" patch (which is not directly related).
10545 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10546 needed, it would be wrong due to missing DW_AT_producer there.
10548 Still one can confuse GDB by using non-standard GCC compilation
10549 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10551 if (cu->has_loclist && gcc_4_minor >= 5)
10552 cust->locations_valid = 1;
10554 if (gcc_4_minor >= 5)
10555 cust->epilogue_unwind_valid = 1;
10557 cust->call_site_htab = cu->call_site_htab;
10560 if (dwarf2_per_objfile->using_index)
10561 per_cu->v.quick->compunit_symtab = cust;
10564 struct partial_symtab *pst = per_cu->v.psymtab;
10565 pst->compunit_symtab = cust;
10569 /* Push it for inclusion processing later. */
10570 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10573 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10574 already been loaded into memory. */
10577 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10578 enum language pretend_language)
10580 struct dwarf2_cu *cu = per_cu->cu;
10581 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10582 struct objfile *objfile = dwarf2_per_objfile->objfile;
10583 struct compunit_symtab *cust;
10584 struct cleanup *delayed_list_cleanup;
10585 struct signatured_type *sig_type;
10587 gdb_assert (per_cu->is_debug_types);
10588 sig_type = (struct signatured_type *) per_cu;
10591 scoped_free_pendings free_pending;
10592 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10594 cu->list_in_scope = &file_symbols;
10596 cu->language = pretend_language;
10597 cu->language_defn = language_def (cu->language);
10599 /* The symbol tables are set up in read_type_unit_scope. */
10600 process_die (cu->dies, cu);
10602 /* For now fudge the Go package. */
10603 if (cu->language == language_go)
10604 fixup_go_packaging (cu);
10606 /* Now that we have processed all the DIEs in the CU, all the types
10607 should be complete, and it should now be safe to compute all of the
10609 compute_delayed_physnames (cu);
10610 do_cleanups (delayed_list_cleanup);
10612 /* TUs share symbol tables.
10613 If this is the first TU to use this symtab, complete the construction
10614 of it with end_expandable_symtab. Otherwise, complete the addition of
10615 this TU's symbols to the existing symtab. */
10616 if (sig_type->type_unit_group->compunit_symtab == NULL)
10618 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10619 sig_type->type_unit_group->compunit_symtab = cust;
10623 /* Set symtab language to language from DW_AT_language. If the
10624 compilation is from a C file generated by language preprocessors,
10625 do not set the language if it was already deduced by
10627 if (!(cu->language == language_c
10628 && COMPUNIT_FILETABS (cust)->language != language_c))
10629 COMPUNIT_FILETABS (cust)->language = cu->language;
10634 augment_type_symtab ();
10635 cust = sig_type->type_unit_group->compunit_symtab;
10638 if (dwarf2_per_objfile->using_index)
10639 per_cu->v.quick->compunit_symtab = cust;
10642 struct partial_symtab *pst = per_cu->v.psymtab;
10643 pst->compunit_symtab = cust;
10648 /* Process an imported unit DIE. */
10651 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10653 struct attribute *attr;
10655 /* For now we don't handle imported units in type units. */
10656 if (cu->per_cu->is_debug_types)
10658 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10659 " supported in type units [in module %s]"),
10660 objfile_name (cu->dwarf2_per_objfile->objfile));
10663 attr = dwarf2_attr (die, DW_AT_import, cu);
10666 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10667 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10668 dwarf2_per_cu_data *per_cu
10669 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10670 cu->dwarf2_per_objfile);
10672 /* If necessary, add it to the queue and load its DIEs. */
10673 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10674 load_full_comp_unit (per_cu, cu->language);
10676 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10681 /* RAII object that represents a process_die scope: i.e.,
10682 starts/finishes processing a DIE. */
10683 class process_die_scope
10686 process_die_scope (die_info *die, dwarf2_cu *cu)
10687 : m_die (die), m_cu (cu)
10689 /* We should only be processing DIEs not already in process. */
10690 gdb_assert (!m_die->in_process);
10691 m_die->in_process = true;
10694 ~process_die_scope ()
10696 m_die->in_process = false;
10698 /* If we're done processing the DIE for the CU that owns the line
10699 header, we don't need the line header anymore. */
10700 if (m_cu->line_header_die_owner == m_die)
10702 delete m_cu->line_header;
10703 m_cu->line_header = NULL;
10704 m_cu->line_header_die_owner = NULL;
10713 /* Process a die and its children. */
10716 process_die (struct die_info *die, struct dwarf2_cu *cu)
10718 process_die_scope scope (die, cu);
10722 case DW_TAG_padding:
10724 case DW_TAG_compile_unit:
10725 case DW_TAG_partial_unit:
10726 read_file_scope (die, cu);
10728 case DW_TAG_type_unit:
10729 read_type_unit_scope (die, cu);
10731 case DW_TAG_subprogram:
10732 case DW_TAG_inlined_subroutine:
10733 read_func_scope (die, cu);
10735 case DW_TAG_lexical_block:
10736 case DW_TAG_try_block:
10737 case DW_TAG_catch_block:
10738 read_lexical_block_scope (die, cu);
10740 case DW_TAG_call_site:
10741 case DW_TAG_GNU_call_site:
10742 read_call_site_scope (die, cu);
10744 case DW_TAG_class_type:
10745 case DW_TAG_interface_type:
10746 case DW_TAG_structure_type:
10747 case DW_TAG_union_type:
10748 process_structure_scope (die, cu);
10750 case DW_TAG_enumeration_type:
10751 process_enumeration_scope (die, cu);
10754 /* These dies have a type, but processing them does not create
10755 a symbol or recurse to process the children. Therefore we can
10756 read them on-demand through read_type_die. */
10757 case DW_TAG_subroutine_type:
10758 case DW_TAG_set_type:
10759 case DW_TAG_array_type:
10760 case DW_TAG_pointer_type:
10761 case DW_TAG_ptr_to_member_type:
10762 case DW_TAG_reference_type:
10763 case DW_TAG_rvalue_reference_type:
10764 case DW_TAG_string_type:
10767 case DW_TAG_base_type:
10768 case DW_TAG_subrange_type:
10769 case DW_TAG_typedef:
10770 /* Add a typedef symbol for the type definition, if it has a
10772 new_symbol (die, read_type_die (die, cu), cu);
10774 case DW_TAG_common_block:
10775 read_common_block (die, cu);
10777 case DW_TAG_common_inclusion:
10779 case DW_TAG_namespace:
10780 cu->processing_has_namespace_info = 1;
10781 read_namespace (die, cu);
10783 case DW_TAG_module:
10784 cu->processing_has_namespace_info = 1;
10785 read_module (die, cu);
10787 case DW_TAG_imported_declaration:
10788 cu->processing_has_namespace_info = 1;
10789 if (read_namespace_alias (die, cu))
10791 /* The declaration is not a global namespace alias: fall through. */
10792 case DW_TAG_imported_module:
10793 cu->processing_has_namespace_info = 1;
10794 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10795 || cu->language != language_fortran))
10796 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10797 dwarf_tag_name (die->tag));
10798 read_import_statement (die, cu);
10801 case DW_TAG_imported_unit:
10802 process_imported_unit_die (die, cu);
10805 case DW_TAG_variable:
10806 read_variable (die, cu);
10810 new_symbol (die, NULL, cu);
10815 /* DWARF name computation. */
10817 /* A helper function for dwarf2_compute_name which determines whether DIE
10818 needs to have the name of the scope prepended to the name listed in the
10822 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10824 struct attribute *attr;
10828 case DW_TAG_namespace:
10829 case DW_TAG_typedef:
10830 case DW_TAG_class_type:
10831 case DW_TAG_interface_type:
10832 case DW_TAG_structure_type:
10833 case DW_TAG_union_type:
10834 case DW_TAG_enumeration_type:
10835 case DW_TAG_enumerator:
10836 case DW_TAG_subprogram:
10837 case DW_TAG_inlined_subroutine:
10838 case DW_TAG_member:
10839 case DW_TAG_imported_declaration:
10842 case DW_TAG_variable:
10843 case DW_TAG_constant:
10844 /* We only need to prefix "globally" visible variables. These include
10845 any variable marked with DW_AT_external or any variable that
10846 lives in a namespace. [Variables in anonymous namespaces
10847 require prefixing, but they are not DW_AT_external.] */
10849 if (dwarf2_attr (die, DW_AT_specification, cu))
10851 struct dwarf2_cu *spec_cu = cu;
10853 return die_needs_namespace (die_specification (die, &spec_cu),
10857 attr = dwarf2_attr (die, DW_AT_external, cu);
10858 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10859 && die->parent->tag != DW_TAG_module)
10861 /* A variable in a lexical block of some kind does not need a
10862 namespace, even though in C++ such variables may be external
10863 and have a mangled name. */
10864 if (die->parent->tag == DW_TAG_lexical_block
10865 || die->parent->tag == DW_TAG_try_block
10866 || die->parent->tag == DW_TAG_catch_block
10867 || die->parent->tag == DW_TAG_subprogram)
10876 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10877 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10878 defined for the given DIE. */
10880 static struct attribute *
10881 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10883 struct attribute *attr;
10885 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10887 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10892 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10893 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10894 defined for the given DIE. */
10896 static const char *
10897 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10899 const char *linkage_name;
10901 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10902 if (linkage_name == NULL)
10903 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10905 return linkage_name;
10908 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10909 compute the physname for the object, which include a method's:
10910 - formal parameters (C++),
10911 - receiver type (Go),
10913 The term "physname" is a bit confusing.
10914 For C++, for example, it is the demangled name.
10915 For Go, for example, it's the mangled name.
10917 For Ada, return the DIE's linkage name rather than the fully qualified
10918 name. PHYSNAME is ignored..
10920 The result is allocated on the objfile_obstack and canonicalized. */
10922 static const char *
10923 dwarf2_compute_name (const char *name,
10924 struct die_info *die, struct dwarf2_cu *cu,
10927 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
10930 name = dwarf2_name (die, cu);
10932 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10933 but otherwise compute it by typename_concat inside GDB.
10934 FIXME: Actually this is not really true, or at least not always true.
10935 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10936 Fortran names because there is no mangling standard. So new_symbol_full
10937 will set the demangled name to the result of dwarf2_full_name, and it is
10938 the demangled name that GDB uses if it exists. */
10939 if (cu->language == language_ada
10940 || (cu->language == language_fortran && physname))
10942 /* For Ada unit, we prefer the linkage name over the name, as
10943 the former contains the exported name, which the user expects
10944 to be able to reference. Ideally, we want the user to be able
10945 to reference this entity using either natural or linkage name,
10946 but we haven't started looking at this enhancement yet. */
10947 const char *linkage_name = dw2_linkage_name (die, cu);
10949 if (linkage_name != NULL)
10950 return linkage_name;
10953 /* These are the only languages we know how to qualify names in. */
10955 && (cu->language == language_cplus
10956 || cu->language == language_fortran || cu->language == language_d
10957 || cu->language == language_rust))
10959 if (die_needs_namespace (die, cu))
10961 const char *prefix;
10962 const char *canonical_name = NULL;
10966 prefix = determine_prefix (die, cu);
10967 if (*prefix != '\0')
10969 char *prefixed_name = typename_concat (NULL, prefix, name,
10972 buf.puts (prefixed_name);
10973 xfree (prefixed_name);
10978 /* Template parameters may be specified in the DIE's DW_AT_name, or
10979 as children with DW_TAG_template_type_param or
10980 DW_TAG_value_type_param. If the latter, add them to the name
10981 here. If the name already has template parameters, then
10982 skip this step; some versions of GCC emit both, and
10983 it is more efficient to use the pre-computed name.
10985 Something to keep in mind about this process: it is very
10986 unlikely, or in some cases downright impossible, to produce
10987 something that will match the mangled name of a function.
10988 If the definition of the function has the same debug info,
10989 we should be able to match up with it anyway. But fallbacks
10990 using the minimal symbol, for instance to find a method
10991 implemented in a stripped copy of libstdc++, will not work.
10992 If we do not have debug info for the definition, we will have to
10993 match them up some other way.
10995 When we do name matching there is a related problem with function
10996 templates; two instantiated function templates are allowed to
10997 differ only by their return types, which we do not add here. */
10999 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11001 struct attribute *attr;
11002 struct die_info *child;
11005 die->building_fullname = 1;
11007 for (child = die->child; child != NULL; child = child->sibling)
11011 const gdb_byte *bytes;
11012 struct dwarf2_locexpr_baton *baton;
11015 if (child->tag != DW_TAG_template_type_param
11016 && child->tag != DW_TAG_template_value_param)
11027 attr = dwarf2_attr (child, DW_AT_type, cu);
11030 complaint (&symfile_complaints,
11031 _("template parameter missing DW_AT_type"));
11032 buf.puts ("UNKNOWN_TYPE");
11035 type = die_type (child, cu);
11037 if (child->tag == DW_TAG_template_type_param)
11039 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11043 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11046 complaint (&symfile_complaints,
11047 _("template parameter missing "
11048 "DW_AT_const_value"));
11049 buf.puts ("UNKNOWN_VALUE");
11053 dwarf2_const_value_attr (attr, type, name,
11054 &cu->comp_unit_obstack, cu,
11055 &value, &bytes, &baton);
11057 if (TYPE_NOSIGN (type))
11058 /* GDB prints characters as NUMBER 'CHAR'. If that's
11059 changed, this can use value_print instead. */
11060 c_printchar (value, type, &buf);
11063 struct value_print_options opts;
11066 v = dwarf2_evaluate_loc_desc (type, NULL,
11070 else if (bytes != NULL)
11072 v = allocate_value (type);
11073 memcpy (value_contents_writeable (v), bytes,
11074 TYPE_LENGTH (type));
11077 v = value_from_longest (type, value);
11079 /* Specify decimal so that we do not depend on
11081 get_formatted_print_options (&opts, 'd');
11083 value_print (v, &buf, &opts);
11089 die->building_fullname = 0;
11093 /* Close the argument list, with a space if necessary
11094 (nested templates). */
11095 if (!buf.empty () && buf.string ().back () == '>')
11102 /* For C++ methods, append formal parameter type
11103 information, if PHYSNAME. */
11105 if (physname && die->tag == DW_TAG_subprogram
11106 && cu->language == language_cplus)
11108 struct type *type = read_type_die (die, cu);
11110 c_type_print_args (type, &buf, 1, cu->language,
11111 &type_print_raw_options);
11113 if (cu->language == language_cplus)
11115 /* Assume that an artificial first parameter is
11116 "this", but do not crash if it is not. RealView
11117 marks unnamed (and thus unused) parameters as
11118 artificial; there is no way to differentiate
11120 if (TYPE_NFIELDS (type) > 0
11121 && TYPE_FIELD_ARTIFICIAL (type, 0)
11122 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11123 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11125 buf.puts (" const");
11129 const std::string &intermediate_name = buf.string ();
11131 if (cu->language == language_cplus)
11133 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11134 &objfile->per_bfd->storage_obstack);
11136 /* If we only computed INTERMEDIATE_NAME, or if
11137 INTERMEDIATE_NAME is already canonical, then we need to
11138 copy it to the appropriate obstack. */
11139 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11140 name = ((const char *)
11141 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11142 intermediate_name.c_str (),
11143 intermediate_name.length ()));
11145 name = canonical_name;
11152 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11153 If scope qualifiers are appropriate they will be added. The result
11154 will be allocated on the storage_obstack, or NULL if the DIE does
11155 not have a name. NAME may either be from a previous call to
11156 dwarf2_name or NULL.
11158 The output string will be canonicalized (if C++). */
11160 static const char *
11161 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11163 return dwarf2_compute_name (name, die, cu, 0);
11166 /* Construct a physname for the given DIE in CU. NAME may either be
11167 from a previous call to dwarf2_name or NULL. The result will be
11168 allocated on the objfile_objstack or NULL if the DIE does not have a
11171 The output string will be canonicalized (if C++). */
11173 static const char *
11174 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11176 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
11177 const char *retval, *mangled = NULL, *canon = NULL;
11180 /* In this case dwarf2_compute_name is just a shortcut not building anything
11182 if (!die_needs_namespace (die, cu))
11183 return dwarf2_compute_name (name, die, cu, 1);
11185 mangled = dw2_linkage_name (die, cu);
11187 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11188 See https://github.com/rust-lang/rust/issues/32925. */
11189 if (cu->language == language_rust && mangled != NULL
11190 && strchr (mangled, '{') != NULL)
11193 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11195 gdb::unique_xmalloc_ptr<char> demangled;
11196 if (mangled != NULL)
11198 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11199 type. It is easier for GDB users to search for such functions as
11200 `name(params)' than `long name(params)'. In such case the minimal
11201 symbol names do not match the full symbol names but for template
11202 functions there is never a need to look up their definition from their
11203 declaration so the only disadvantage remains the minimal symbol
11204 variant `long name(params)' does not have the proper inferior type.
11207 if (cu->language == language_go)
11209 /* This is a lie, but we already lie to the caller new_symbol_full.
11210 new_symbol_full assumes we return the mangled name.
11211 This just undoes that lie until things are cleaned up. */
11215 demangled.reset (gdb_demangle (mangled,
11216 (DMGL_PARAMS | DMGL_ANSI
11217 | DMGL_RET_DROP)));
11220 canon = demangled.get ();
11228 if (canon == NULL || check_physname)
11230 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11232 if (canon != NULL && strcmp (physname, canon) != 0)
11234 /* It may not mean a bug in GDB. The compiler could also
11235 compute DW_AT_linkage_name incorrectly. But in such case
11236 GDB would need to be bug-to-bug compatible. */
11238 complaint (&symfile_complaints,
11239 _("Computed physname <%s> does not match demangled <%s> "
11240 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11241 physname, canon, mangled, to_underlying (die->sect_off),
11242 objfile_name (objfile));
11244 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11245 is available here - over computed PHYSNAME. It is safer
11246 against both buggy GDB and buggy compilers. */
11260 retval = ((const char *)
11261 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11262 retval, strlen (retval)));
11267 /* Inspect DIE in CU for a namespace alias. If one exists, record
11268 a new symbol for it.
11270 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11273 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11275 struct attribute *attr;
11277 /* If the die does not have a name, this is not a namespace
11279 attr = dwarf2_attr (die, DW_AT_name, cu);
11283 struct die_info *d = die;
11284 struct dwarf2_cu *imported_cu = cu;
11286 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11287 keep inspecting DIEs until we hit the underlying import. */
11288 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11289 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11291 attr = dwarf2_attr (d, DW_AT_import, cu);
11295 d = follow_die_ref (d, attr, &imported_cu);
11296 if (d->tag != DW_TAG_imported_declaration)
11300 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11302 complaint (&symfile_complaints,
11303 _("DIE at 0x%x has too many recursively imported "
11304 "declarations"), to_underlying (d->sect_off));
11311 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11313 type = get_die_type_at_offset (sect_off, cu->per_cu);
11314 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11316 /* This declaration is a global namespace alias. Add
11317 a symbol for it whose type is the aliased namespace. */
11318 new_symbol (die, type, cu);
11327 /* Return the using directives repository (global or local?) to use in the
11328 current context for LANGUAGE.
11330 For Ada, imported declarations can materialize renamings, which *may* be
11331 global. However it is impossible (for now?) in DWARF to distinguish
11332 "external" imported declarations and "static" ones. As all imported
11333 declarations seem to be static in all other languages, make them all CU-wide
11334 global only in Ada. */
11336 static struct using_direct **
11337 using_directives (enum language language)
11339 if (language == language_ada && context_stack_depth == 0)
11340 return &global_using_directives;
11342 return &local_using_directives;
11345 /* Read the import statement specified by the given die and record it. */
11348 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11350 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
11351 struct attribute *import_attr;
11352 struct die_info *imported_die, *child_die;
11353 struct dwarf2_cu *imported_cu;
11354 const char *imported_name;
11355 const char *imported_name_prefix;
11356 const char *canonical_name;
11357 const char *import_alias;
11358 const char *imported_declaration = NULL;
11359 const char *import_prefix;
11360 std::vector<const char *> excludes;
11362 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11363 if (import_attr == NULL)
11365 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11366 dwarf_tag_name (die->tag));
11371 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11372 imported_name = dwarf2_name (imported_die, imported_cu);
11373 if (imported_name == NULL)
11375 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11377 The import in the following code:
11391 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11392 <52> DW_AT_decl_file : 1
11393 <53> DW_AT_decl_line : 6
11394 <54> DW_AT_import : <0x75>
11395 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11396 <59> DW_AT_name : B
11397 <5b> DW_AT_decl_file : 1
11398 <5c> DW_AT_decl_line : 2
11399 <5d> DW_AT_type : <0x6e>
11401 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11402 <76> DW_AT_byte_size : 4
11403 <77> DW_AT_encoding : 5 (signed)
11405 imports the wrong die ( 0x75 instead of 0x58 ).
11406 This case will be ignored until the gcc bug is fixed. */
11410 /* Figure out the local name after import. */
11411 import_alias = dwarf2_name (die, cu);
11413 /* Figure out where the statement is being imported to. */
11414 import_prefix = determine_prefix (die, cu);
11416 /* Figure out what the scope of the imported die is and prepend it
11417 to the name of the imported die. */
11418 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11420 if (imported_die->tag != DW_TAG_namespace
11421 && imported_die->tag != DW_TAG_module)
11423 imported_declaration = imported_name;
11424 canonical_name = imported_name_prefix;
11426 else if (strlen (imported_name_prefix) > 0)
11427 canonical_name = obconcat (&objfile->objfile_obstack,
11428 imported_name_prefix,
11429 (cu->language == language_d ? "." : "::"),
11430 imported_name, (char *) NULL);
11432 canonical_name = imported_name;
11434 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11435 for (child_die = die->child; child_die && child_die->tag;
11436 child_die = sibling_die (child_die))
11438 /* DWARF-4: A Fortran use statement with a “rename list” may be
11439 represented by an imported module entry with an import attribute
11440 referring to the module and owned entries corresponding to those
11441 entities that are renamed as part of being imported. */
11443 if (child_die->tag != DW_TAG_imported_declaration)
11445 complaint (&symfile_complaints,
11446 _("child DW_TAG_imported_declaration expected "
11447 "- DIE at 0x%x [in module %s]"),
11448 to_underlying (child_die->sect_off), objfile_name (objfile));
11452 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11453 if (import_attr == NULL)
11455 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11456 dwarf_tag_name (child_die->tag));
11461 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11463 imported_name = dwarf2_name (imported_die, imported_cu);
11464 if (imported_name == NULL)
11466 complaint (&symfile_complaints,
11467 _("child DW_TAG_imported_declaration has unknown "
11468 "imported name - DIE at 0x%x [in module %s]"),
11469 to_underlying (child_die->sect_off), objfile_name (objfile));
11473 excludes.push_back (imported_name);
11475 process_die (child_die, cu);
11478 add_using_directive (using_directives (cu->language),
11482 imported_declaration,
11485 &objfile->objfile_obstack);
11488 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11489 types, but gives them a size of zero. Starting with version 14,
11490 ICC is compatible with GCC. */
11493 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11495 if (!cu->checked_producer)
11496 check_producer (cu);
11498 return cu->producer_is_icc_lt_14;
11501 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11502 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11503 this, it was first present in GCC release 4.3.0. */
11506 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11508 if (!cu->checked_producer)
11509 check_producer (cu);
11511 return cu->producer_is_gcc_lt_4_3;
11514 static file_and_directory
11515 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11517 file_and_directory res;
11519 /* Find the filename. Do not use dwarf2_name here, since the filename
11520 is not a source language identifier. */
11521 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11522 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11524 if (res.comp_dir == NULL
11525 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11526 && IS_ABSOLUTE_PATH (res.name))
11528 res.comp_dir_storage = ldirname (res.name);
11529 if (!res.comp_dir_storage.empty ())
11530 res.comp_dir = res.comp_dir_storage.c_str ();
11532 if (res.comp_dir != NULL)
11534 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11535 directory, get rid of it. */
11536 const char *cp = strchr (res.comp_dir, ':');
11538 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11539 res.comp_dir = cp + 1;
11542 if (res.name == NULL)
11543 res.name = "<unknown>";
11548 /* Handle DW_AT_stmt_list for a compilation unit.
11549 DIE is the DW_TAG_compile_unit die for CU.
11550 COMP_DIR is the compilation directory. LOWPC is passed to
11551 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11554 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11555 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11557 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
11558 struct objfile *objfile = dwarf2_per_objfile->objfile;
11559 struct attribute *attr;
11560 struct line_header line_header_local;
11561 hashval_t line_header_local_hash;
11563 int decode_mapping;
11565 gdb_assert (! cu->per_cu->is_debug_types);
11567 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11571 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11573 /* The line header hash table is only created if needed (it exists to
11574 prevent redundant reading of the line table for partial_units).
11575 If we're given a partial_unit, we'll need it. If we're given a
11576 compile_unit, then use the line header hash table if it's already
11577 created, but don't create one just yet. */
11579 if (dwarf2_per_objfile->line_header_hash == NULL
11580 && die->tag == DW_TAG_partial_unit)
11582 dwarf2_per_objfile->line_header_hash
11583 = htab_create_alloc_ex (127, line_header_hash_voidp,
11584 line_header_eq_voidp,
11585 free_line_header_voidp,
11586 &objfile->objfile_obstack,
11587 hashtab_obstack_allocate,
11588 dummy_obstack_deallocate);
11591 line_header_local.sect_off = line_offset;
11592 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11593 line_header_local_hash = line_header_hash (&line_header_local);
11594 if (dwarf2_per_objfile->line_header_hash != NULL)
11596 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11597 &line_header_local,
11598 line_header_local_hash, NO_INSERT);
11600 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11601 is not present in *SLOT (since if there is something in *SLOT then
11602 it will be for a partial_unit). */
11603 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11605 gdb_assert (*slot != NULL);
11606 cu->line_header = (struct line_header *) *slot;
11611 /* dwarf_decode_line_header does not yet provide sufficient information.
11612 We always have to call also dwarf_decode_lines for it. */
11613 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11617 cu->line_header = lh.release ();
11618 cu->line_header_die_owner = die;
11620 if (dwarf2_per_objfile->line_header_hash == NULL)
11624 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11625 &line_header_local,
11626 line_header_local_hash, INSERT);
11627 gdb_assert (slot != NULL);
11629 if (slot != NULL && *slot == NULL)
11631 /* This newly decoded line number information unit will be owned
11632 by line_header_hash hash table. */
11633 *slot = cu->line_header;
11634 cu->line_header_die_owner = NULL;
11638 /* We cannot free any current entry in (*slot) as that struct line_header
11639 may be already used by multiple CUs. Create only temporary decoded
11640 line_header for this CU - it may happen at most once for each line
11641 number information unit. And if we're not using line_header_hash
11642 then this is what we want as well. */
11643 gdb_assert (die->tag != DW_TAG_partial_unit);
11645 decode_mapping = (die->tag != DW_TAG_partial_unit);
11646 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11651 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11654 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11656 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
11657 struct objfile *objfile = dwarf2_per_objfile->objfile;
11658 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11659 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11660 CORE_ADDR highpc = ((CORE_ADDR) 0);
11661 struct attribute *attr;
11662 struct die_info *child_die;
11663 CORE_ADDR baseaddr;
11665 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11667 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11669 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11670 from finish_block. */
11671 if (lowpc == ((CORE_ADDR) -1))
11673 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11675 file_and_directory fnd = find_file_and_directory (die, cu);
11677 prepare_one_comp_unit (cu, die, cu->language);
11679 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11680 standardised yet. As a workaround for the language detection we fall
11681 back to the DW_AT_producer string. */
11682 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11683 cu->language = language_opencl;
11685 /* Similar hack for Go. */
11686 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11687 set_cu_language (DW_LANG_Go, cu);
11689 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11691 /* Decode line number information if present. We do this before
11692 processing child DIEs, so that the line header table is available
11693 for DW_AT_decl_file. */
11694 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11696 /* Process all dies in compilation unit. */
11697 if (die->child != NULL)
11699 child_die = die->child;
11700 while (child_die && child_die->tag)
11702 process_die (child_die, cu);
11703 child_die = sibling_die (child_die);
11707 /* Decode macro information, if present. Dwarf 2 macro information
11708 refers to information in the line number info statement program
11709 header, so we can only read it if we've read the header
11711 attr = dwarf2_attr (die, DW_AT_macros, cu);
11713 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11714 if (attr && cu->line_header)
11716 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11717 complaint (&symfile_complaints,
11718 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11720 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11724 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11725 if (attr && cu->line_header)
11727 unsigned int macro_offset = DW_UNSND (attr);
11729 dwarf_decode_macros (cu, macro_offset, 0);
11734 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11735 Create the set of symtabs used by this TU, or if this TU is sharing
11736 symtabs with another TU and the symtabs have already been created
11737 then restore those symtabs in the line header.
11738 We don't need the pc/line-number mapping for type units. */
11741 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11743 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11744 struct type_unit_group *tu_group;
11746 struct attribute *attr;
11748 struct signatured_type *sig_type;
11750 gdb_assert (per_cu->is_debug_types);
11751 sig_type = (struct signatured_type *) per_cu;
11753 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11755 /* If we're using .gdb_index (includes -readnow) then
11756 per_cu->type_unit_group may not have been set up yet. */
11757 if (sig_type->type_unit_group == NULL)
11758 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11759 tu_group = sig_type->type_unit_group;
11761 /* If we've already processed this stmt_list there's no real need to
11762 do it again, we could fake it and just recreate the part we need
11763 (file name,index -> symtab mapping). If data shows this optimization
11764 is useful we can do it then. */
11765 first_time = tu_group->compunit_symtab == NULL;
11767 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11772 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11773 lh = dwarf_decode_line_header (line_offset, cu);
11778 dwarf2_start_symtab (cu, "", NULL, 0);
11781 gdb_assert (tu_group->symtabs == NULL);
11782 restart_symtab (tu_group->compunit_symtab, "", 0);
11787 cu->line_header = lh.release ();
11788 cu->line_header_die_owner = die;
11792 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11794 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11795 still initializing it, and our caller (a few levels up)
11796 process_full_type_unit still needs to know if this is the first
11799 tu_group->num_symtabs = cu->line_header->file_names.size ();
11800 tu_group->symtabs = XNEWVEC (struct symtab *,
11801 cu->line_header->file_names.size ());
11803 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11805 file_entry &fe = cu->line_header->file_names[i];
11807 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11809 if (current_subfile->symtab == NULL)
11811 /* NOTE: start_subfile will recognize when it's been
11812 passed a file it has already seen. So we can't
11813 assume there's a simple mapping from
11814 cu->line_header->file_names to subfiles, plus
11815 cu->line_header->file_names may contain dups. */
11816 current_subfile->symtab
11817 = allocate_symtab (cust, current_subfile->name);
11820 fe.symtab = current_subfile->symtab;
11821 tu_group->symtabs[i] = fe.symtab;
11826 restart_symtab (tu_group->compunit_symtab, "", 0);
11828 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11830 file_entry &fe = cu->line_header->file_names[i];
11832 fe.symtab = tu_group->symtabs[i];
11836 /* The main symtab is allocated last. Type units don't have DW_AT_name
11837 so they don't have a "real" (so to speak) symtab anyway.
11838 There is later code that will assign the main symtab to all symbols
11839 that don't have one. We need to handle the case of a symbol with a
11840 missing symtab (DW_AT_decl_file) anyway. */
11843 /* Process DW_TAG_type_unit.
11844 For TUs we want to skip the first top level sibling if it's not the
11845 actual type being defined by this TU. In this case the first top
11846 level sibling is there to provide context only. */
11849 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11851 struct die_info *child_die;
11853 prepare_one_comp_unit (cu, die, language_minimal);
11855 /* Initialize (or reinitialize) the machinery for building symtabs.
11856 We do this before processing child DIEs, so that the line header table
11857 is available for DW_AT_decl_file. */
11858 setup_type_unit_groups (die, cu);
11860 if (die->child != NULL)
11862 child_die = die->child;
11863 while (child_die && child_die->tag)
11865 process_die (child_die, cu);
11866 child_die = sibling_die (child_die);
11873 http://gcc.gnu.org/wiki/DebugFission
11874 http://gcc.gnu.org/wiki/DebugFissionDWP
11876 To simplify handling of both DWO files ("object" files with the DWARF info)
11877 and DWP files (a file with the DWOs packaged up into one file), we treat
11878 DWP files as having a collection of virtual DWO files. */
11881 hash_dwo_file (const void *item)
11883 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11886 hash = htab_hash_string (dwo_file->dwo_name);
11887 if (dwo_file->comp_dir != NULL)
11888 hash += htab_hash_string (dwo_file->comp_dir);
11893 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11895 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11896 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11898 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11900 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11901 return lhs->comp_dir == rhs->comp_dir;
11902 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11905 /* Allocate a hash table for DWO files. */
11908 allocate_dwo_file_hash_table (struct objfile *objfile)
11910 return htab_create_alloc_ex (41,
11914 &objfile->objfile_obstack,
11915 hashtab_obstack_allocate,
11916 dummy_obstack_deallocate);
11919 /* Lookup DWO file DWO_NAME. */
11922 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11923 const char *dwo_name,
11924 const char *comp_dir)
11926 struct dwo_file find_entry;
11929 if (dwarf2_per_objfile->dwo_files == NULL)
11930 dwarf2_per_objfile->dwo_files
11931 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11933 memset (&find_entry, 0, sizeof (find_entry));
11934 find_entry.dwo_name = dwo_name;
11935 find_entry.comp_dir = comp_dir;
11936 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11942 hash_dwo_unit (const void *item)
11944 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11946 /* This drops the top 32 bits of the id, but is ok for a hash. */
11947 return dwo_unit->signature;
11951 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11953 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11954 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11956 /* The signature is assumed to be unique within the DWO file.
11957 So while object file CU dwo_id's always have the value zero,
11958 that's OK, assuming each object file DWO file has only one CU,
11959 and that's the rule for now. */
11960 return lhs->signature == rhs->signature;
11963 /* Allocate a hash table for DWO CUs,TUs.
11964 There is one of these tables for each of CUs,TUs for each DWO file. */
11967 allocate_dwo_unit_table (struct objfile *objfile)
11969 /* Start out with a pretty small number.
11970 Generally DWO files contain only one CU and maybe some TUs. */
11971 return htab_create_alloc_ex (3,
11975 &objfile->objfile_obstack,
11976 hashtab_obstack_allocate,
11977 dummy_obstack_deallocate);
11980 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11982 struct create_dwo_cu_data
11984 struct dwo_file *dwo_file;
11985 struct dwo_unit dwo_unit;
11988 /* die_reader_func for create_dwo_cu. */
11991 create_dwo_cu_reader (const struct die_reader_specs *reader,
11992 const gdb_byte *info_ptr,
11993 struct die_info *comp_unit_die,
11997 struct dwarf2_cu *cu = reader->cu;
11998 sect_offset sect_off = cu->per_cu->sect_off;
11999 struct dwarf2_section_info *section = cu->per_cu->section;
12000 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12001 struct dwo_file *dwo_file = data->dwo_file;
12002 struct dwo_unit *dwo_unit = &data->dwo_unit;
12003 struct attribute *attr;
12005 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12008 complaint (&symfile_complaints,
12009 _("Dwarf Error: debug entry at offset 0x%x is missing"
12010 " its dwo_id [in module %s]"),
12011 to_underlying (sect_off), dwo_file->dwo_name);
12015 dwo_unit->dwo_file = dwo_file;
12016 dwo_unit->signature = DW_UNSND (attr);
12017 dwo_unit->section = section;
12018 dwo_unit->sect_off = sect_off;
12019 dwo_unit->length = cu->per_cu->length;
12021 if (dwarf_read_debug)
12022 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
12023 to_underlying (sect_off),
12024 hex_string (dwo_unit->signature));
12027 /* Create the dwo_units for the CUs in a DWO_FILE.
12028 Note: This function processes DWO files only, not DWP files. */
12031 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12032 struct dwo_file &dwo_file, dwarf2_section_info §ion,
12035 struct objfile *objfile = dwarf2_per_objfile->objfile;
12036 const gdb_byte *info_ptr, *end_ptr;
12038 dwarf2_read_section (objfile, §ion);
12039 info_ptr = section.buffer;
12041 if (info_ptr == NULL)
12044 if (dwarf_read_debug)
12046 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12047 get_section_name (§ion),
12048 get_section_file_name (§ion));
12051 end_ptr = info_ptr + section.size;
12052 while (info_ptr < end_ptr)
12054 struct dwarf2_per_cu_data per_cu;
12055 struct create_dwo_cu_data create_dwo_cu_data;
12056 struct dwo_unit *dwo_unit;
12058 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12060 memset (&create_dwo_cu_data.dwo_unit, 0,
12061 sizeof (create_dwo_cu_data.dwo_unit));
12062 memset (&per_cu, 0, sizeof (per_cu));
12063 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12064 per_cu.is_debug_types = 0;
12065 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12066 per_cu.section = §ion;
12067 create_dwo_cu_data.dwo_file = &dwo_file;
12069 init_cutu_and_read_dies_no_follow (
12070 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12071 info_ptr += per_cu.length;
12073 // If the unit could not be parsed, skip it.
12074 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12077 if (cus_htab == NULL)
12078 cus_htab = allocate_dwo_unit_table (objfile);
12080 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12081 *dwo_unit = create_dwo_cu_data.dwo_unit;
12082 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12083 gdb_assert (slot != NULL);
12086 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12087 sect_offset dup_sect_off = dup_cu->sect_off;
12089 complaint (&symfile_complaints,
12090 _("debug cu entry at offset 0x%x is duplicate to"
12091 " the entry at offset 0x%x, signature %s"),
12092 to_underlying (sect_off), to_underlying (dup_sect_off),
12093 hex_string (dwo_unit->signature));
12095 *slot = (void *)dwo_unit;
12099 /* DWP file .debug_{cu,tu}_index section format:
12100 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12104 Both index sections have the same format, and serve to map a 64-bit
12105 signature to a set of section numbers. Each section begins with a header,
12106 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12107 indexes, and a pool of 32-bit section numbers. The index sections will be
12108 aligned at 8-byte boundaries in the file.
12110 The index section header consists of:
12112 V, 32 bit version number
12114 N, 32 bit number of compilation units or type units in the index
12115 M, 32 bit number of slots in the hash table
12117 Numbers are recorded using the byte order of the application binary.
12119 The hash table begins at offset 16 in the section, and consists of an array
12120 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12121 order of the application binary). Unused slots in the hash table are 0.
12122 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12124 The parallel table begins immediately after the hash table
12125 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12126 array of 32-bit indexes (using the byte order of the application binary),
12127 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12128 table contains a 32-bit index into the pool of section numbers. For unused
12129 hash table slots, the corresponding entry in the parallel table will be 0.
12131 The pool of section numbers begins immediately following the hash table
12132 (at offset 16 + 12 * M from the beginning of the section). The pool of
12133 section numbers consists of an array of 32-bit words (using the byte order
12134 of the application binary). Each item in the array is indexed starting
12135 from 0. The hash table entry provides the index of the first section
12136 number in the set. Additional section numbers in the set follow, and the
12137 set is terminated by a 0 entry (section number 0 is not used in ELF).
12139 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12140 section must be the first entry in the set, and the .debug_abbrev.dwo must
12141 be the second entry. Other members of the set may follow in any order.
12147 DWP Version 2 combines all the .debug_info, etc. sections into one,
12148 and the entries in the index tables are now offsets into these sections.
12149 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12152 Index Section Contents:
12154 Hash Table of Signatures dwp_hash_table.hash_table
12155 Parallel Table of Indices dwp_hash_table.unit_table
12156 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12157 Table of Section Sizes dwp_hash_table.v2.sizes
12159 The index section header consists of:
12161 V, 32 bit version number
12162 L, 32 bit number of columns in the table of section offsets
12163 N, 32 bit number of compilation units or type units in the index
12164 M, 32 bit number of slots in the hash table
12166 Numbers are recorded using the byte order of the application binary.
12168 The hash table has the same format as version 1.
12169 The parallel table of indices has the same format as version 1,
12170 except that the entries are origin-1 indices into the table of sections
12171 offsets and the table of section sizes.
12173 The table of offsets begins immediately following the parallel table
12174 (at offset 16 + 12 * M from the beginning of the section). The table is
12175 a two-dimensional array of 32-bit words (using the byte order of the
12176 application binary), with L columns and N+1 rows, in row-major order.
12177 Each row in the array is indexed starting from 0. The first row provides
12178 a key to the remaining rows: each column in this row provides an identifier
12179 for a debug section, and the offsets in the same column of subsequent rows
12180 refer to that section. The section identifiers are:
12182 DW_SECT_INFO 1 .debug_info.dwo
12183 DW_SECT_TYPES 2 .debug_types.dwo
12184 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12185 DW_SECT_LINE 4 .debug_line.dwo
12186 DW_SECT_LOC 5 .debug_loc.dwo
12187 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12188 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12189 DW_SECT_MACRO 8 .debug_macro.dwo
12191 The offsets provided by the CU and TU index sections are the base offsets
12192 for the contributions made by each CU or TU to the corresponding section
12193 in the package file. Each CU and TU header contains an abbrev_offset
12194 field, used to find the abbreviations table for that CU or TU within the
12195 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12196 be interpreted as relative to the base offset given in the index section.
12197 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12198 should be interpreted as relative to the base offset for .debug_line.dwo,
12199 and offsets into other debug sections obtained from DWARF attributes should
12200 also be interpreted as relative to the corresponding base offset.
12202 The table of sizes begins immediately following the table of offsets.
12203 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12204 with L columns and N rows, in row-major order. Each row in the array is
12205 indexed starting from 1 (row 0 is shared by the two tables).
12209 Hash table lookup is handled the same in version 1 and 2:
12211 We assume that N and M will not exceed 2^32 - 1.
12212 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12214 Given a 64-bit compilation unit signature or a type signature S, an entry
12215 in the hash table is located as follows:
12217 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12218 the low-order k bits all set to 1.
12220 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12222 3) If the hash table entry at index H matches the signature, use that
12223 entry. If the hash table entry at index H is unused (all zeroes),
12224 terminate the search: the signature is not present in the table.
12226 4) Let H = (H + H') modulo M. Repeat at Step 3.
12228 Because M > N and H' and M are relatively prime, the search is guaranteed
12229 to stop at an unused slot or find the match. */
12231 /* Create a hash table to map DWO IDs to their CU/TU entry in
12232 .debug_{info,types}.dwo in DWP_FILE.
12233 Returns NULL if there isn't one.
12234 Note: This function processes DWP files only, not DWO files. */
12236 static struct dwp_hash_table *
12237 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12238 struct dwp_file *dwp_file, int is_debug_types)
12240 struct objfile *objfile = dwarf2_per_objfile->objfile;
12241 bfd *dbfd = dwp_file->dbfd;
12242 const gdb_byte *index_ptr, *index_end;
12243 struct dwarf2_section_info *index;
12244 uint32_t version, nr_columns, nr_units, nr_slots;
12245 struct dwp_hash_table *htab;
12247 if (is_debug_types)
12248 index = &dwp_file->sections.tu_index;
12250 index = &dwp_file->sections.cu_index;
12252 if (dwarf2_section_empty_p (index))
12254 dwarf2_read_section (objfile, index);
12256 index_ptr = index->buffer;
12257 index_end = index_ptr + index->size;
12259 version = read_4_bytes (dbfd, index_ptr);
12262 nr_columns = read_4_bytes (dbfd, index_ptr);
12266 nr_units = read_4_bytes (dbfd, index_ptr);
12268 nr_slots = read_4_bytes (dbfd, index_ptr);
12271 if (version != 1 && version != 2)
12273 error (_("Dwarf Error: unsupported DWP file version (%s)"
12274 " [in module %s]"),
12275 pulongest (version), dwp_file->name);
12277 if (nr_slots != (nr_slots & -nr_slots))
12279 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12280 " is not power of 2 [in module %s]"),
12281 pulongest (nr_slots), dwp_file->name);
12284 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12285 htab->version = version;
12286 htab->nr_columns = nr_columns;
12287 htab->nr_units = nr_units;
12288 htab->nr_slots = nr_slots;
12289 htab->hash_table = index_ptr;
12290 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12292 /* Exit early if the table is empty. */
12293 if (nr_slots == 0 || nr_units == 0
12294 || (version == 2 && nr_columns == 0))
12296 /* All must be zero. */
12297 if (nr_slots != 0 || nr_units != 0
12298 || (version == 2 && nr_columns != 0))
12300 complaint (&symfile_complaints,
12301 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12302 " all zero [in modules %s]"),
12310 htab->section_pool.v1.indices =
12311 htab->unit_table + sizeof (uint32_t) * nr_slots;
12312 /* It's harder to decide whether the section is too small in v1.
12313 V1 is deprecated anyway so we punt. */
12317 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12318 int *ids = htab->section_pool.v2.section_ids;
12319 /* Reverse map for error checking. */
12320 int ids_seen[DW_SECT_MAX + 1];
12323 if (nr_columns < 2)
12325 error (_("Dwarf Error: bad DWP hash table, too few columns"
12326 " in section table [in module %s]"),
12329 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12331 error (_("Dwarf Error: bad DWP hash table, too many columns"
12332 " in section table [in module %s]"),
12335 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12336 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12337 for (i = 0; i < nr_columns; ++i)
12339 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12341 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12343 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12344 " in section table [in module %s]"),
12345 id, dwp_file->name);
12347 if (ids_seen[id] != -1)
12349 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12350 " id %d in section table [in module %s]"),
12351 id, dwp_file->name);
12356 /* Must have exactly one info or types section. */
12357 if (((ids_seen[DW_SECT_INFO] != -1)
12358 + (ids_seen[DW_SECT_TYPES] != -1))
12361 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12362 " DWO info/types section [in module %s]"),
12365 /* Must have an abbrev section. */
12366 if (ids_seen[DW_SECT_ABBREV] == -1)
12368 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12369 " section [in module %s]"),
12372 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12373 htab->section_pool.v2.sizes =
12374 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12375 * nr_units * nr_columns);
12376 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12377 * nr_units * nr_columns))
12380 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12381 " [in module %s]"),
12389 /* Update SECTIONS with the data from SECTP.
12391 This function is like the other "locate" section routines that are
12392 passed to bfd_map_over_sections, but in this context the sections to
12393 read comes from the DWP V1 hash table, not the full ELF section table.
12395 The result is non-zero for success, or zero if an error was found. */
12398 locate_v1_virtual_dwo_sections (asection *sectp,
12399 struct virtual_v1_dwo_sections *sections)
12401 const struct dwop_section_names *names = &dwop_section_names;
12403 if (section_is_p (sectp->name, &names->abbrev_dwo))
12405 /* There can be only one. */
12406 if (sections->abbrev.s.section != NULL)
12408 sections->abbrev.s.section = sectp;
12409 sections->abbrev.size = bfd_get_section_size (sectp);
12411 else if (section_is_p (sectp->name, &names->info_dwo)
12412 || section_is_p (sectp->name, &names->types_dwo))
12414 /* There can be only one. */
12415 if (sections->info_or_types.s.section != NULL)
12417 sections->info_or_types.s.section = sectp;
12418 sections->info_or_types.size = bfd_get_section_size (sectp);
12420 else if (section_is_p (sectp->name, &names->line_dwo))
12422 /* There can be only one. */
12423 if (sections->line.s.section != NULL)
12425 sections->line.s.section = sectp;
12426 sections->line.size = bfd_get_section_size (sectp);
12428 else if (section_is_p (sectp->name, &names->loc_dwo))
12430 /* There can be only one. */
12431 if (sections->loc.s.section != NULL)
12433 sections->loc.s.section = sectp;
12434 sections->loc.size = bfd_get_section_size (sectp);
12436 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12438 /* There can be only one. */
12439 if (sections->macinfo.s.section != NULL)
12441 sections->macinfo.s.section = sectp;
12442 sections->macinfo.size = bfd_get_section_size (sectp);
12444 else if (section_is_p (sectp->name, &names->macro_dwo))
12446 /* There can be only one. */
12447 if (sections->macro.s.section != NULL)
12449 sections->macro.s.section = sectp;
12450 sections->macro.size = bfd_get_section_size (sectp);
12452 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12454 /* There can be only one. */
12455 if (sections->str_offsets.s.section != NULL)
12457 sections->str_offsets.s.section = sectp;
12458 sections->str_offsets.size = bfd_get_section_size (sectp);
12462 /* No other kind of section is valid. */
12469 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12470 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12471 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12472 This is for DWP version 1 files. */
12474 static struct dwo_unit *
12475 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12476 struct dwp_file *dwp_file,
12477 uint32_t unit_index,
12478 const char *comp_dir,
12479 ULONGEST signature, int is_debug_types)
12481 struct objfile *objfile = dwarf2_per_objfile->objfile;
12482 const struct dwp_hash_table *dwp_htab =
12483 is_debug_types ? dwp_file->tus : dwp_file->cus;
12484 bfd *dbfd = dwp_file->dbfd;
12485 const char *kind = is_debug_types ? "TU" : "CU";
12486 struct dwo_file *dwo_file;
12487 struct dwo_unit *dwo_unit;
12488 struct virtual_v1_dwo_sections sections;
12489 void **dwo_file_slot;
12492 gdb_assert (dwp_file->version == 1);
12494 if (dwarf_read_debug)
12496 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12498 pulongest (unit_index), hex_string (signature),
12502 /* Fetch the sections of this DWO unit.
12503 Put a limit on the number of sections we look for so that bad data
12504 doesn't cause us to loop forever. */
12506 #define MAX_NR_V1_DWO_SECTIONS \
12507 (1 /* .debug_info or .debug_types */ \
12508 + 1 /* .debug_abbrev */ \
12509 + 1 /* .debug_line */ \
12510 + 1 /* .debug_loc */ \
12511 + 1 /* .debug_str_offsets */ \
12512 + 1 /* .debug_macro or .debug_macinfo */ \
12513 + 1 /* trailing zero */)
12515 memset (§ions, 0, sizeof (sections));
12517 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12520 uint32_t section_nr =
12521 read_4_bytes (dbfd,
12522 dwp_htab->section_pool.v1.indices
12523 + (unit_index + i) * sizeof (uint32_t));
12525 if (section_nr == 0)
12527 if (section_nr >= dwp_file->num_sections)
12529 error (_("Dwarf Error: bad DWP hash table, section number too large"
12530 " [in module %s]"),
12534 sectp = dwp_file->elf_sections[section_nr];
12535 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12537 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12538 " [in module %s]"),
12544 || dwarf2_section_empty_p (§ions.info_or_types)
12545 || dwarf2_section_empty_p (§ions.abbrev))
12547 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12548 " [in module %s]"),
12551 if (i == MAX_NR_V1_DWO_SECTIONS)
12553 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12554 " [in module %s]"),
12558 /* It's easier for the rest of the code if we fake a struct dwo_file and
12559 have dwo_unit "live" in that. At least for now.
12561 The DWP file can be made up of a random collection of CUs and TUs.
12562 However, for each CU + set of TUs that came from the same original DWO
12563 file, we can combine them back into a virtual DWO file to save space
12564 (fewer struct dwo_file objects to allocate). Remember that for really
12565 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12567 std::string virtual_dwo_name =
12568 string_printf ("virtual-dwo/%d-%d-%d-%d",
12569 get_section_id (§ions.abbrev),
12570 get_section_id (§ions.line),
12571 get_section_id (§ions.loc),
12572 get_section_id (§ions.str_offsets));
12573 /* Can we use an existing virtual DWO file? */
12574 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12575 virtual_dwo_name.c_str (),
12577 /* Create one if necessary. */
12578 if (*dwo_file_slot == NULL)
12580 if (dwarf_read_debug)
12582 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12583 virtual_dwo_name.c_str ());
12585 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12587 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12588 virtual_dwo_name.c_str (),
12589 virtual_dwo_name.size ());
12590 dwo_file->comp_dir = comp_dir;
12591 dwo_file->sections.abbrev = sections.abbrev;
12592 dwo_file->sections.line = sections.line;
12593 dwo_file->sections.loc = sections.loc;
12594 dwo_file->sections.macinfo = sections.macinfo;
12595 dwo_file->sections.macro = sections.macro;
12596 dwo_file->sections.str_offsets = sections.str_offsets;
12597 /* The "str" section is global to the entire DWP file. */
12598 dwo_file->sections.str = dwp_file->sections.str;
12599 /* The info or types section is assigned below to dwo_unit,
12600 there's no need to record it in dwo_file.
12601 Also, we can't simply record type sections in dwo_file because
12602 we record a pointer into the vector in dwo_unit. As we collect more
12603 types we'll grow the vector and eventually have to reallocate space
12604 for it, invalidating all copies of pointers into the previous
12606 *dwo_file_slot = dwo_file;
12610 if (dwarf_read_debug)
12612 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12613 virtual_dwo_name.c_str ());
12615 dwo_file = (struct dwo_file *) *dwo_file_slot;
12618 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12619 dwo_unit->dwo_file = dwo_file;
12620 dwo_unit->signature = signature;
12621 dwo_unit->section =
12622 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12623 *dwo_unit->section = sections.info_or_types;
12624 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12629 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12630 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12631 piece within that section used by a TU/CU, return a virtual section
12632 of just that piece. */
12634 static struct dwarf2_section_info
12635 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12636 struct dwarf2_section_info *section,
12637 bfd_size_type offset, bfd_size_type size)
12639 struct dwarf2_section_info result;
12642 gdb_assert (section != NULL);
12643 gdb_assert (!section->is_virtual);
12645 memset (&result, 0, sizeof (result));
12646 result.s.containing_section = section;
12647 result.is_virtual = 1;
12652 sectp = get_section_bfd_section (section);
12654 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12655 bounds of the real section. This is a pretty-rare event, so just
12656 flag an error (easier) instead of a warning and trying to cope. */
12658 || offset + size > bfd_get_section_size (sectp))
12660 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12661 " in section %s [in module %s]"),
12662 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12663 objfile_name (dwarf2_per_objfile->objfile));
12666 result.virtual_offset = offset;
12667 result.size = size;
12671 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12672 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12673 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12674 This is for DWP version 2 files. */
12676 static struct dwo_unit *
12677 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12678 struct dwp_file *dwp_file,
12679 uint32_t unit_index,
12680 const char *comp_dir,
12681 ULONGEST signature, int is_debug_types)
12683 struct objfile *objfile = dwarf2_per_objfile->objfile;
12684 const struct dwp_hash_table *dwp_htab =
12685 is_debug_types ? dwp_file->tus : dwp_file->cus;
12686 bfd *dbfd = dwp_file->dbfd;
12687 const char *kind = is_debug_types ? "TU" : "CU";
12688 struct dwo_file *dwo_file;
12689 struct dwo_unit *dwo_unit;
12690 struct virtual_v2_dwo_sections sections;
12691 void **dwo_file_slot;
12694 gdb_assert (dwp_file->version == 2);
12696 if (dwarf_read_debug)
12698 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12700 pulongest (unit_index), hex_string (signature),
12704 /* Fetch the section offsets of this DWO unit. */
12706 memset (§ions, 0, sizeof (sections));
12708 for (i = 0; i < dwp_htab->nr_columns; ++i)
12710 uint32_t offset = read_4_bytes (dbfd,
12711 dwp_htab->section_pool.v2.offsets
12712 + (((unit_index - 1) * dwp_htab->nr_columns
12714 * sizeof (uint32_t)));
12715 uint32_t size = read_4_bytes (dbfd,
12716 dwp_htab->section_pool.v2.sizes
12717 + (((unit_index - 1) * dwp_htab->nr_columns
12719 * sizeof (uint32_t)));
12721 switch (dwp_htab->section_pool.v2.section_ids[i])
12724 case DW_SECT_TYPES:
12725 sections.info_or_types_offset = offset;
12726 sections.info_or_types_size = size;
12728 case DW_SECT_ABBREV:
12729 sections.abbrev_offset = offset;
12730 sections.abbrev_size = size;
12733 sections.line_offset = offset;
12734 sections.line_size = size;
12737 sections.loc_offset = offset;
12738 sections.loc_size = size;
12740 case DW_SECT_STR_OFFSETS:
12741 sections.str_offsets_offset = offset;
12742 sections.str_offsets_size = size;
12744 case DW_SECT_MACINFO:
12745 sections.macinfo_offset = offset;
12746 sections.macinfo_size = size;
12748 case DW_SECT_MACRO:
12749 sections.macro_offset = offset;
12750 sections.macro_size = size;
12755 /* It's easier for the rest of the code if we fake a struct dwo_file and
12756 have dwo_unit "live" in that. At least for now.
12758 The DWP file can be made up of a random collection of CUs and TUs.
12759 However, for each CU + set of TUs that came from the same original DWO
12760 file, we can combine them back into a virtual DWO file to save space
12761 (fewer struct dwo_file objects to allocate). Remember that for really
12762 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12764 std::string virtual_dwo_name =
12765 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12766 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12767 (long) (sections.line_size ? sections.line_offset : 0),
12768 (long) (sections.loc_size ? sections.loc_offset : 0),
12769 (long) (sections.str_offsets_size
12770 ? sections.str_offsets_offset : 0));
12771 /* Can we use an existing virtual DWO file? */
12772 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12773 virtual_dwo_name.c_str (),
12775 /* Create one if necessary. */
12776 if (*dwo_file_slot == NULL)
12778 if (dwarf_read_debug)
12780 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12781 virtual_dwo_name.c_str ());
12783 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12785 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12786 virtual_dwo_name.c_str (),
12787 virtual_dwo_name.size ());
12788 dwo_file->comp_dir = comp_dir;
12789 dwo_file->sections.abbrev =
12790 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12791 sections.abbrev_offset, sections.abbrev_size);
12792 dwo_file->sections.line =
12793 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12794 sections.line_offset, sections.line_size);
12795 dwo_file->sections.loc =
12796 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12797 sections.loc_offset, sections.loc_size);
12798 dwo_file->sections.macinfo =
12799 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12800 sections.macinfo_offset, sections.macinfo_size);
12801 dwo_file->sections.macro =
12802 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12803 sections.macro_offset, sections.macro_size);
12804 dwo_file->sections.str_offsets =
12805 create_dwp_v2_section (dwarf2_per_objfile,
12806 &dwp_file->sections.str_offsets,
12807 sections.str_offsets_offset,
12808 sections.str_offsets_size);
12809 /* The "str" section is global to the entire DWP file. */
12810 dwo_file->sections.str = dwp_file->sections.str;
12811 /* The info or types section is assigned below to dwo_unit,
12812 there's no need to record it in dwo_file.
12813 Also, we can't simply record type sections in dwo_file because
12814 we record a pointer into the vector in dwo_unit. As we collect more
12815 types we'll grow the vector and eventually have to reallocate space
12816 for it, invalidating all copies of pointers into the previous
12818 *dwo_file_slot = dwo_file;
12822 if (dwarf_read_debug)
12824 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12825 virtual_dwo_name.c_str ());
12827 dwo_file = (struct dwo_file *) *dwo_file_slot;
12830 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12831 dwo_unit->dwo_file = dwo_file;
12832 dwo_unit->signature = signature;
12833 dwo_unit->section =
12834 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12835 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12837 ? &dwp_file->sections.types
12838 : &dwp_file->sections.info,
12839 sections.info_or_types_offset,
12840 sections.info_or_types_size);
12841 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12846 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12847 Returns NULL if the signature isn't found. */
12849 static struct dwo_unit *
12850 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12851 struct dwp_file *dwp_file, const char *comp_dir,
12852 ULONGEST signature, int is_debug_types)
12854 const struct dwp_hash_table *dwp_htab =
12855 is_debug_types ? dwp_file->tus : dwp_file->cus;
12856 bfd *dbfd = dwp_file->dbfd;
12857 uint32_t mask = dwp_htab->nr_slots - 1;
12858 uint32_t hash = signature & mask;
12859 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12862 struct dwo_unit find_dwo_cu;
12864 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12865 find_dwo_cu.signature = signature;
12866 slot = htab_find_slot (is_debug_types
12867 ? dwp_file->loaded_tus
12868 : dwp_file->loaded_cus,
12869 &find_dwo_cu, INSERT);
12872 return (struct dwo_unit *) *slot;
12874 /* Use a for loop so that we don't loop forever on bad debug info. */
12875 for (i = 0; i < dwp_htab->nr_slots; ++i)
12877 ULONGEST signature_in_table;
12879 signature_in_table =
12880 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12881 if (signature_in_table == signature)
12883 uint32_t unit_index =
12884 read_4_bytes (dbfd,
12885 dwp_htab->unit_table + hash * sizeof (uint32_t));
12887 if (dwp_file->version == 1)
12889 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12890 dwp_file, unit_index,
12891 comp_dir, signature,
12896 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12897 dwp_file, unit_index,
12898 comp_dir, signature,
12901 return (struct dwo_unit *) *slot;
12903 if (signature_in_table == 0)
12905 hash = (hash + hash2) & mask;
12908 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12909 " [in module %s]"),
12913 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12914 Open the file specified by FILE_NAME and hand it off to BFD for
12915 preliminary analysis. Return a newly initialized bfd *, which
12916 includes a canonicalized copy of FILE_NAME.
12917 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12918 SEARCH_CWD is true if the current directory is to be searched.
12919 It will be searched before debug-file-directory.
12920 If successful, the file is added to the bfd include table of the
12921 objfile's bfd (see gdb_bfd_record_inclusion).
12922 If unable to find/open the file, return NULL.
12923 NOTE: This function is derived from symfile_bfd_open. */
12925 static gdb_bfd_ref_ptr
12926 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12927 const char *file_name, int is_dwp, int search_cwd)
12930 char *absolute_name;
12931 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12932 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12933 to debug_file_directory. */
12935 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12939 if (*debug_file_directory != '\0')
12940 search_path = concat (".", dirname_separator_string,
12941 debug_file_directory, (char *) NULL);
12943 search_path = xstrdup (".");
12946 search_path = xstrdup (debug_file_directory);
12948 flags = OPF_RETURN_REALPATH;
12950 flags |= OPF_SEARCH_IN_PATH;
12951 desc = openp (search_path, flags, file_name,
12952 O_RDONLY | O_BINARY, &absolute_name);
12953 xfree (search_path);
12957 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12958 xfree (absolute_name);
12959 if (sym_bfd == NULL)
12961 bfd_set_cacheable (sym_bfd.get (), 1);
12963 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12966 /* Success. Record the bfd as having been included by the objfile's bfd.
12967 This is important because things like demangled_names_hash lives in the
12968 objfile's per_bfd space and may have references to things like symbol
12969 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12970 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12975 /* Try to open DWO file FILE_NAME.
12976 COMP_DIR is the DW_AT_comp_dir attribute.
12977 The result is the bfd handle of the file.
12978 If there is a problem finding or opening the file, return NULL.
12979 Upon success, the canonicalized path of the file is stored in the bfd,
12980 same as symfile_bfd_open. */
12982 static gdb_bfd_ref_ptr
12983 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12984 const char *file_name, const char *comp_dir)
12986 if (IS_ABSOLUTE_PATH (file_name))
12987 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12988 0 /*is_dwp*/, 0 /*search_cwd*/);
12990 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12992 if (comp_dir != NULL)
12994 char *path_to_try = concat (comp_dir, SLASH_STRING,
12995 file_name, (char *) NULL);
12997 /* NOTE: If comp_dir is a relative path, this will also try the
12998 search path, which seems useful. */
12999 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13002 1 /*search_cwd*/));
13003 xfree (path_to_try);
13008 /* That didn't work, try debug-file-directory, which, despite its name,
13009 is a list of paths. */
13011 if (*debug_file_directory == '\0')
13014 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13015 0 /*is_dwp*/, 1 /*search_cwd*/);
13018 /* This function is mapped across the sections and remembers the offset and
13019 size of each of the DWO debugging sections we are interested in. */
13022 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13024 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13025 const struct dwop_section_names *names = &dwop_section_names;
13027 if (section_is_p (sectp->name, &names->abbrev_dwo))
13029 dwo_sections->abbrev.s.section = sectp;
13030 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13032 else if (section_is_p (sectp->name, &names->info_dwo))
13034 dwo_sections->info.s.section = sectp;
13035 dwo_sections->info.size = bfd_get_section_size (sectp);
13037 else if (section_is_p (sectp->name, &names->line_dwo))
13039 dwo_sections->line.s.section = sectp;
13040 dwo_sections->line.size = bfd_get_section_size (sectp);
13042 else if (section_is_p (sectp->name, &names->loc_dwo))
13044 dwo_sections->loc.s.section = sectp;
13045 dwo_sections->loc.size = bfd_get_section_size (sectp);
13047 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13049 dwo_sections->macinfo.s.section = sectp;
13050 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13052 else if (section_is_p (sectp->name, &names->macro_dwo))
13054 dwo_sections->macro.s.section = sectp;
13055 dwo_sections->macro.size = bfd_get_section_size (sectp);
13057 else if (section_is_p (sectp->name, &names->str_dwo))
13059 dwo_sections->str.s.section = sectp;
13060 dwo_sections->str.size = bfd_get_section_size (sectp);
13062 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13064 dwo_sections->str_offsets.s.section = sectp;
13065 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13067 else if (section_is_p (sectp->name, &names->types_dwo))
13069 struct dwarf2_section_info type_section;
13071 memset (&type_section, 0, sizeof (type_section));
13072 type_section.s.section = sectp;
13073 type_section.size = bfd_get_section_size (sectp);
13074 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13079 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13080 by PER_CU. This is for the non-DWP case.
13081 The result is NULL if DWO_NAME can't be found. */
13083 static struct dwo_file *
13084 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13085 const char *dwo_name, const char *comp_dir)
13087 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13088 struct objfile *objfile = dwarf2_per_objfile->objfile;
13089 struct dwo_file *dwo_file;
13090 struct cleanup *cleanups;
13092 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13095 if (dwarf_read_debug)
13096 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13099 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13100 dwo_file->dwo_name = dwo_name;
13101 dwo_file->comp_dir = comp_dir;
13102 dwo_file->dbfd = dbfd.release ();
13104 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13105 cleanup_data->dwo_file = dwo_file;
13106 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13108 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13110 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13111 &dwo_file->sections);
13113 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13116 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13117 dwo_file->sections.types, dwo_file->tus);
13119 discard_cleanups (cleanups);
13121 if (dwarf_read_debug)
13122 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13127 /* This function is mapped across the sections and remembers the offset and
13128 size of each of the DWP debugging sections common to version 1 and 2 that
13129 we are interested in. */
13132 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13133 void *dwp_file_ptr)
13135 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13136 const struct dwop_section_names *names = &dwop_section_names;
13137 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13139 /* Record the ELF section number for later lookup: this is what the
13140 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13141 gdb_assert (elf_section_nr < dwp_file->num_sections);
13142 dwp_file->elf_sections[elf_section_nr] = sectp;
13144 /* Look for specific sections that we need. */
13145 if (section_is_p (sectp->name, &names->str_dwo))
13147 dwp_file->sections.str.s.section = sectp;
13148 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13150 else if (section_is_p (sectp->name, &names->cu_index))
13152 dwp_file->sections.cu_index.s.section = sectp;
13153 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13155 else if (section_is_p (sectp->name, &names->tu_index))
13157 dwp_file->sections.tu_index.s.section = sectp;
13158 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13162 /* This function is mapped across the sections and remembers the offset and
13163 size of each of the DWP version 2 debugging sections that we are interested
13164 in. This is split into a separate function because we don't know if we
13165 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13168 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13170 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13171 const struct dwop_section_names *names = &dwop_section_names;
13172 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13174 /* Record the ELF section number for later lookup: this is what the
13175 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13176 gdb_assert (elf_section_nr < dwp_file->num_sections);
13177 dwp_file->elf_sections[elf_section_nr] = sectp;
13179 /* Look for specific sections that we need. */
13180 if (section_is_p (sectp->name, &names->abbrev_dwo))
13182 dwp_file->sections.abbrev.s.section = sectp;
13183 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13185 else if (section_is_p (sectp->name, &names->info_dwo))
13187 dwp_file->sections.info.s.section = sectp;
13188 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13190 else if (section_is_p (sectp->name, &names->line_dwo))
13192 dwp_file->sections.line.s.section = sectp;
13193 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13195 else if (section_is_p (sectp->name, &names->loc_dwo))
13197 dwp_file->sections.loc.s.section = sectp;
13198 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13200 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13202 dwp_file->sections.macinfo.s.section = sectp;
13203 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13205 else if (section_is_p (sectp->name, &names->macro_dwo))
13207 dwp_file->sections.macro.s.section = sectp;
13208 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13210 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13212 dwp_file->sections.str_offsets.s.section = sectp;
13213 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13215 else if (section_is_p (sectp->name, &names->types_dwo))
13217 dwp_file->sections.types.s.section = sectp;
13218 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13222 /* Hash function for dwp_file loaded CUs/TUs. */
13225 hash_dwp_loaded_cutus (const void *item)
13227 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13229 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13230 return dwo_unit->signature;
13233 /* Equality function for dwp_file loaded CUs/TUs. */
13236 eq_dwp_loaded_cutus (const void *a, const void *b)
13238 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13239 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13241 return dua->signature == dub->signature;
13244 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13247 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13249 return htab_create_alloc_ex (3,
13250 hash_dwp_loaded_cutus,
13251 eq_dwp_loaded_cutus,
13253 &objfile->objfile_obstack,
13254 hashtab_obstack_allocate,
13255 dummy_obstack_deallocate);
13258 /* Try to open DWP file FILE_NAME.
13259 The result is the bfd handle of the file.
13260 If there is a problem finding or opening the file, return NULL.
13261 Upon success, the canonicalized path of the file is stored in the bfd,
13262 same as symfile_bfd_open. */
13264 static gdb_bfd_ref_ptr
13265 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13266 const char *file_name)
13268 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13270 1 /*search_cwd*/));
13274 /* Work around upstream bug 15652.
13275 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13276 [Whether that's a "bug" is debatable, but it is getting in our way.]
13277 We have no real idea where the dwp file is, because gdb's realpath-ing
13278 of the executable's path may have discarded the needed info.
13279 [IWBN if the dwp file name was recorded in the executable, akin to
13280 .gnu_debuglink, but that doesn't exist yet.]
13281 Strip the directory from FILE_NAME and search again. */
13282 if (*debug_file_directory != '\0')
13284 /* Don't implicitly search the current directory here.
13285 If the user wants to search "." to handle this case,
13286 it must be added to debug-file-directory. */
13287 return try_open_dwop_file (dwarf2_per_objfile,
13288 lbasename (file_name), 1 /*is_dwp*/,
13295 /* Initialize the use of the DWP file for the current objfile.
13296 By convention the name of the DWP file is ${objfile}.dwp.
13297 The result is NULL if it can't be found. */
13299 static struct dwp_file *
13300 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13302 struct objfile *objfile = dwarf2_per_objfile->objfile;
13303 struct dwp_file *dwp_file;
13305 /* Try to find first .dwp for the binary file before any symbolic links
13308 /* If the objfile is a debug file, find the name of the real binary
13309 file and get the name of dwp file from there. */
13310 std::string dwp_name;
13311 if (objfile->separate_debug_objfile_backlink != NULL)
13313 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13314 const char *backlink_basename = lbasename (backlink->original_name);
13316 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13319 dwp_name = objfile->original_name;
13321 dwp_name += ".dwp";
13323 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13325 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13327 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13328 dwp_name = objfile_name (objfile);
13329 dwp_name += ".dwp";
13330 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13335 if (dwarf_read_debug)
13336 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13339 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13340 dwp_file->name = bfd_get_filename (dbfd.get ());
13341 dwp_file->dbfd = dbfd.release ();
13343 /* +1: section 0 is unused */
13344 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13345 dwp_file->elf_sections =
13346 OBSTACK_CALLOC (&objfile->objfile_obstack,
13347 dwp_file->num_sections, asection *);
13349 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13352 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13354 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13356 /* The DWP file version is stored in the hash table. Oh well. */
13357 if (dwp_file->cus && dwp_file->tus
13358 && dwp_file->cus->version != dwp_file->tus->version)
13360 /* Technically speaking, we should try to limp along, but this is
13361 pretty bizarre. We use pulongest here because that's the established
13362 portability solution (e.g, we cannot use %u for uint32_t). */
13363 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13364 " TU version %s [in DWP file %s]"),
13365 pulongest (dwp_file->cus->version),
13366 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13370 dwp_file->version = dwp_file->cus->version;
13371 else if (dwp_file->tus)
13372 dwp_file->version = dwp_file->tus->version;
13374 dwp_file->version = 2;
13376 if (dwp_file->version == 2)
13377 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13380 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13381 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13383 if (dwarf_read_debug)
13385 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13386 fprintf_unfiltered (gdb_stdlog,
13387 " %s CUs, %s TUs\n",
13388 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13389 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13395 /* Wrapper around open_and_init_dwp_file, only open it once. */
13397 static struct dwp_file *
13398 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13400 if (! dwarf2_per_objfile->dwp_checked)
13402 dwarf2_per_objfile->dwp_file
13403 = open_and_init_dwp_file (dwarf2_per_objfile);
13404 dwarf2_per_objfile->dwp_checked = 1;
13406 return dwarf2_per_objfile->dwp_file;
13409 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13410 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13411 or in the DWP file for the objfile, referenced by THIS_UNIT.
13412 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13413 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13415 This is called, for example, when wanting to read a variable with a
13416 complex location. Therefore we don't want to do file i/o for every call.
13417 Therefore we don't want to look for a DWO file on every call.
13418 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13419 then we check if we've already seen DWO_NAME, and only THEN do we check
13422 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13423 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13425 static struct dwo_unit *
13426 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13427 const char *dwo_name, const char *comp_dir,
13428 ULONGEST signature, int is_debug_types)
13430 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13431 struct objfile *objfile = dwarf2_per_objfile->objfile;
13432 const char *kind = is_debug_types ? "TU" : "CU";
13433 void **dwo_file_slot;
13434 struct dwo_file *dwo_file;
13435 struct dwp_file *dwp_file;
13437 /* First see if there's a DWP file.
13438 If we have a DWP file but didn't find the DWO inside it, don't
13439 look for the original DWO file. It makes gdb behave differently
13440 depending on whether one is debugging in the build tree. */
13442 dwp_file = get_dwp_file (dwarf2_per_objfile);
13443 if (dwp_file != NULL)
13445 const struct dwp_hash_table *dwp_htab =
13446 is_debug_types ? dwp_file->tus : dwp_file->cus;
13448 if (dwp_htab != NULL)
13450 struct dwo_unit *dwo_cutu =
13451 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13452 signature, is_debug_types);
13454 if (dwo_cutu != NULL)
13456 if (dwarf_read_debug)
13458 fprintf_unfiltered (gdb_stdlog,
13459 "Virtual DWO %s %s found: @%s\n",
13460 kind, hex_string (signature),
13461 host_address_to_string (dwo_cutu));
13469 /* No DWP file, look for the DWO file. */
13471 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13472 dwo_name, comp_dir);
13473 if (*dwo_file_slot == NULL)
13475 /* Read in the file and build a table of the CUs/TUs it contains. */
13476 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13478 /* NOTE: This will be NULL if unable to open the file. */
13479 dwo_file = (struct dwo_file *) *dwo_file_slot;
13481 if (dwo_file != NULL)
13483 struct dwo_unit *dwo_cutu = NULL;
13485 if (is_debug_types && dwo_file->tus)
13487 struct dwo_unit find_dwo_cutu;
13489 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13490 find_dwo_cutu.signature = signature;
13492 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13494 else if (!is_debug_types && dwo_file->cus)
13496 struct dwo_unit find_dwo_cutu;
13498 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13499 find_dwo_cutu.signature = signature;
13500 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13504 if (dwo_cutu != NULL)
13506 if (dwarf_read_debug)
13508 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13509 kind, dwo_name, hex_string (signature),
13510 host_address_to_string (dwo_cutu));
13517 /* We didn't find it. This could mean a dwo_id mismatch, or
13518 someone deleted the DWO/DWP file, or the search path isn't set up
13519 correctly to find the file. */
13521 if (dwarf_read_debug)
13523 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13524 kind, dwo_name, hex_string (signature));
13527 /* This is a warning and not a complaint because it can be caused by
13528 pilot error (e.g., user accidentally deleting the DWO). */
13530 /* Print the name of the DWP file if we looked there, helps the user
13531 better diagnose the problem. */
13532 std::string dwp_text;
13534 if (dwp_file != NULL)
13535 dwp_text = string_printf (" [in DWP file %s]",
13536 lbasename (dwp_file->name));
13538 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13539 " [in module %s]"),
13540 kind, dwo_name, hex_string (signature),
13542 this_unit->is_debug_types ? "TU" : "CU",
13543 to_underlying (this_unit->sect_off), objfile_name (objfile));
13548 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13549 See lookup_dwo_cutu_unit for details. */
13551 static struct dwo_unit *
13552 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13553 const char *dwo_name, const char *comp_dir,
13554 ULONGEST signature)
13556 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13559 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13560 See lookup_dwo_cutu_unit for details. */
13562 static struct dwo_unit *
13563 lookup_dwo_type_unit (struct signatured_type *this_tu,
13564 const char *dwo_name, const char *comp_dir)
13566 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13569 /* Traversal function for queue_and_load_all_dwo_tus. */
13572 queue_and_load_dwo_tu (void **slot, void *info)
13574 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13575 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13576 ULONGEST signature = dwo_unit->signature;
13577 struct signatured_type *sig_type =
13578 lookup_dwo_signatured_type (per_cu->cu, signature);
13580 if (sig_type != NULL)
13582 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13584 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13585 a real dependency of PER_CU on SIG_TYPE. That is detected later
13586 while processing PER_CU. */
13587 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13588 load_full_type_unit (sig_cu);
13589 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13595 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13596 The DWO may have the only definition of the type, though it may not be
13597 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13598 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13601 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13603 struct dwo_unit *dwo_unit;
13604 struct dwo_file *dwo_file;
13606 gdb_assert (!per_cu->is_debug_types);
13607 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13608 gdb_assert (per_cu->cu != NULL);
13610 dwo_unit = per_cu->cu->dwo_unit;
13611 gdb_assert (dwo_unit != NULL);
13613 dwo_file = dwo_unit->dwo_file;
13614 if (dwo_file->tus != NULL)
13615 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13618 /* Free all resources associated with DWO_FILE.
13619 Close the DWO file and munmap the sections.
13620 All memory should be on the objfile obstack. */
13623 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13626 /* Note: dbfd is NULL for virtual DWO files. */
13627 gdb_bfd_unref (dwo_file->dbfd);
13629 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13632 /* Wrapper for free_dwo_file for use in cleanups. */
13635 free_dwo_file_cleanup (void *arg)
13637 struct free_dwo_file_cleanup_data *data
13638 = (struct free_dwo_file_cleanup_data *) arg;
13639 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13641 free_dwo_file (data->dwo_file, objfile);
13646 /* Traversal function for free_dwo_files. */
13649 free_dwo_file_from_slot (void **slot, void *info)
13651 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13652 struct objfile *objfile = (struct objfile *) info;
13654 free_dwo_file (dwo_file, objfile);
13659 /* Free all resources associated with DWO_FILES. */
13662 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13664 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13667 /* Read in various DIEs. */
13669 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13670 Inherit only the children of the DW_AT_abstract_origin DIE not being
13671 already referenced by DW_AT_abstract_origin from the children of the
13675 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13677 struct die_info *child_die;
13678 sect_offset *offsetp;
13679 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13680 struct die_info *origin_die;
13681 /* Iterator of the ORIGIN_DIE children. */
13682 struct die_info *origin_child_die;
13683 struct attribute *attr;
13684 struct dwarf2_cu *origin_cu;
13685 struct pending **origin_previous_list_in_scope;
13687 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13691 /* Note that following die references may follow to a die in a
13695 origin_die = follow_die_ref (die, attr, &origin_cu);
13697 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13699 origin_previous_list_in_scope = origin_cu->list_in_scope;
13700 origin_cu->list_in_scope = cu->list_in_scope;
13702 if (die->tag != origin_die->tag
13703 && !(die->tag == DW_TAG_inlined_subroutine
13704 && origin_die->tag == DW_TAG_subprogram))
13705 complaint (&symfile_complaints,
13706 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13707 to_underlying (die->sect_off),
13708 to_underlying (origin_die->sect_off));
13710 std::vector<sect_offset> offsets;
13712 for (child_die = die->child;
13713 child_die && child_die->tag;
13714 child_die = sibling_die (child_die))
13716 struct die_info *child_origin_die;
13717 struct dwarf2_cu *child_origin_cu;
13719 /* We are trying to process concrete instance entries:
13720 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13721 it's not relevant to our analysis here. i.e. detecting DIEs that are
13722 present in the abstract instance but not referenced in the concrete
13724 if (child_die->tag == DW_TAG_call_site
13725 || child_die->tag == DW_TAG_GNU_call_site)
13728 /* For each CHILD_DIE, find the corresponding child of
13729 ORIGIN_DIE. If there is more than one layer of
13730 DW_AT_abstract_origin, follow them all; there shouldn't be,
13731 but GCC versions at least through 4.4 generate this (GCC PR
13733 child_origin_die = child_die;
13734 child_origin_cu = cu;
13737 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13741 child_origin_die = follow_die_ref (child_origin_die, attr,
13745 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13746 counterpart may exist. */
13747 if (child_origin_die != child_die)
13749 if (child_die->tag != child_origin_die->tag
13750 && !(child_die->tag == DW_TAG_inlined_subroutine
13751 && child_origin_die->tag == DW_TAG_subprogram))
13752 complaint (&symfile_complaints,
13753 _("Child DIE 0x%x and its abstract origin 0x%x have "
13755 to_underlying (child_die->sect_off),
13756 to_underlying (child_origin_die->sect_off));
13757 if (child_origin_die->parent != origin_die)
13758 complaint (&symfile_complaints,
13759 _("Child DIE 0x%x and its abstract origin 0x%x have "
13760 "different parents"),
13761 to_underlying (child_die->sect_off),
13762 to_underlying (child_origin_die->sect_off));
13764 offsets.push_back (child_origin_die->sect_off);
13767 std::sort (offsets.begin (), offsets.end ());
13768 sect_offset *offsets_end = offsets.data () + offsets.size ();
13769 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13770 if (offsetp[-1] == *offsetp)
13771 complaint (&symfile_complaints,
13772 _("Multiple children of DIE 0x%x refer "
13773 "to DIE 0x%x as their abstract origin"),
13774 to_underlying (die->sect_off), to_underlying (*offsetp));
13776 offsetp = offsets.data ();
13777 origin_child_die = origin_die->child;
13778 while (origin_child_die && origin_child_die->tag)
13780 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13781 while (offsetp < offsets_end
13782 && *offsetp < origin_child_die->sect_off)
13784 if (offsetp >= offsets_end
13785 || *offsetp > origin_child_die->sect_off)
13787 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13788 Check whether we're already processing ORIGIN_CHILD_DIE.
13789 This can happen with mutually referenced abstract_origins.
13791 if (!origin_child_die->in_process)
13792 process_die (origin_child_die, origin_cu);
13794 origin_child_die = sibling_die (origin_child_die);
13796 origin_cu->list_in_scope = origin_previous_list_in_scope;
13800 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13802 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
13803 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13804 struct context_stack *newobj;
13807 struct die_info *child_die;
13808 struct attribute *attr, *call_line, *call_file;
13810 CORE_ADDR baseaddr;
13811 struct block *block;
13812 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13813 std::vector<struct symbol *> template_args;
13814 struct template_symbol *templ_func = NULL;
13818 /* If we do not have call site information, we can't show the
13819 caller of this inlined function. That's too confusing, so
13820 only use the scope for local variables. */
13821 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13822 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13823 if (call_line == NULL || call_file == NULL)
13825 read_lexical_block_scope (die, cu);
13830 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13832 name = dwarf2_name (die, cu);
13834 /* Ignore functions with missing or empty names. These are actually
13835 illegal according to the DWARF standard. */
13838 complaint (&symfile_complaints,
13839 _("missing name for subprogram DIE at %d"),
13840 to_underlying (die->sect_off));
13844 /* Ignore functions with missing or invalid low and high pc attributes. */
13845 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13846 <= PC_BOUNDS_INVALID)
13848 attr = dwarf2_attr (die, DW_AT_external, cu);
13849 if (!attr || !DW_UNSND (attr))
13850 complaint (&symfile_complaints,
13851 _("cannot get low and high bounds "
13852 "for subprogram DIE at %d"),
13853 to_underlying (die->sect_off));
13857 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13858 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13860 /* If we have any template arguments, then we must allocate a
13861 different sort of symbol. */
13862 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13864 if (child_die->tag == DW_TAG_template_type_param
13865 || child_die->tag == DW_TAG_template_value_param)
13867 templ_func = allocate_template_symbol (objfile);
13868 templ_func->subclass = SYMBOL_TEMPLATE;
13873 newobj = push_context (0, lowpc);
13874 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
13875 (struct symbol *) templ_func);
13877 /* If there is a location expression for DW_AT_frame_base, record
13879 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13881 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13883 /* If there is a location for the static link, record it. */
13884 newobj->static_link = NULL;
13885 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13888 newobj->static_link
13889 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13890 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13893 cu->list_in_scope = &local_symbols;
13895 if (die->child != NULL)
13897 child_die = die->child;
13898 while (child_die && child_die->tag)
13900 if (child_die->tag == DW_TAG_template_type_param
13901 || child_die->tag == DW_TAG_template_value_param)
13903 struct symbol *arg = new_symbol (child_die, NULL, cu);
13906 template_args.push_back (arg);
13909 process_die (child_die, cu);
13910 child_die = sibling_die (child_die);
13914 inherit_abstract_dies (die, cu);
13916 /* If we have a DW_AT_specification, we might need to import using
13917 directives from the context of the specification DIE. See the
13918 comment in determine_prefix. */
13919 if (cu->language == language_cplus
13920 && dwarf2_attr (die, DW_AT_specification, cu))
13922 struct dwarf2_cu *spec_cu = cu;
13923 struct die_info *spec_die = die_specification (die, &spec_cu);
13927 child_die = spec_die->child;
13928 while (child_die && child_die->tag)
13930 if (child_die->tag == DW_TAG_imported_module)
13931 process_die (child_die, spec_cu);
13932 child_die = sibling_die (child_die);
13935 /* In some cases, GCC generates specification DIEs that
13936 themselves contain DW_AT_specification attributes. */
13937 spec_die = die_specification (spec_die, &spec_cu);
13941 newobj = pop_context ();
13942 /* Make a block for the local symbols within. */
13943 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13944 newobj->static_link, lowpc, highpc);
13946 /* For C++, set the block's scope. */
13947 if ((cu->language == language_cplus
13948 || cu->language == language_fortran
13949 || cu->language == language_d
13950 || cu->language == language_rust)
13951 && cu->processing_has_namespace_info)
13952 block_set_scope (block, determine_prefix (die, cu),
13953 &objfile->objfile_obstack);
13955 /* If we have address ranges, record them. */
13956 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13958 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13960 /* Attach template arguments to function. */
13961 if (!template_args.empty ())
13963 gdb_assert (templ_func != NULL);
13965 templ_func->n_template_arguments = template_args.size ();
13966 templ_func->template_arguments
13967 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13968 templ_func->n_template_arguments);
13969 memcpy (templ_func->template_arguments,
13970 template_args.data (),
13971 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13974 /* In C++, we can have functions nested inside functions (e.g., when
13975 a function declares a class that has methods). This means that
13976 when we finish processing a function scope, we may need to go
13977 back to building a containing block's symbol lists. */
13978 local_symbols = newobj->locals;
13979 local_using_directives = newobj->local_using_directives;
13981 /* If we've finished processing a top-level function, subsequent
13982 symbols go in the file symbol list. */
13983 if (outermost_context_p ())
13984 cu->list_in_scope = &file_symbols;
13987 /* Process all the DIES contained within a lexical block scope. Start
13988 a new scope, process the dies, and then close the scope. */
13991 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13993 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
13994 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13995 struct context_stack *newobj;
13996 CORE_ADDR lowpc, highpc;
13997 struct die_info *child_die;
13998 CORE_ADDR baseaddr;
14000 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14002 /* Ignore blocks with missing or invalid low and high pc attributes. */
14003 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14004 as multiple lexical blocks? Handling children in a sane way would
14005 be nasty. Might be easier to properly extend generic blocks to
14006 describe ranges. */
14007 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14009 case PC_BOUNDS_NOT_PRESENT:
14010 /* DW_TAG_lexical_block has no attributes, process its children as if
14011 there was no wrapping by that DW_TAG_lexical_block.
14012 GCC does no longer produces such DWARF since GCC r224161. */
14013 for (child_die = die->child;
14014 child_die != NULL && child_die->tag;
14015 child_die = sibling_die (child_die))
14016 process_die (child_die, cu);
14018 case PC_BOUNDS_INVALID:
14021 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14022 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14024 push_context (0, lowpc);
14025 if (die->child != NULL)
14027 child_die = die->child;
14028 while (child_die && child_die->tag)
14030 process_die (child_die, cu);
14031 child_die = sibling_die (child_die);
14034 inherit_abstract_dies (die, cu);
14035 newobj = pop_context ();
14037 if (local_symbols != NULL || local_using_directives != NULL)
14039 struct block *block
14040 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14041 newobj->start_addr, highpc);
14043 /* Note that recording ranges after traversing children, as we
14044 do here, means that recording a parent's ranges entails
14045 walking across all its children's ranges as they appear in
14046 the address map, which is quadratic behavior.
14048 It would be nicer to record the parent's ranges before
14049 traversing its children, simply overriding whatever you find
14050 there. But since we don't even decide whether to create a
14051 block until after we've traversed its children, that's hard
14053 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14055 local_symbols = newobj->locals;
14056 local_using_directives = newobj->local_using_directives;
14059 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14062 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14064 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
14065 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14066 CORE_ADDR pc, baseaddr;
14067 struct attribute *attr;
14068 struct call_site *call_site, call_site_local;
14071 struct die_info *child_die;
14073 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14075 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14078 /* This was a pre-DWARF-5 GNU extension alias
14079 for DW_AT_call_return_pc. */
14080 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14084 complaint (&symfile_complaints,
14085 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14086 "DIE 0x%x [in module %s]"),
14087 to_underlying (die->sect_off), objfile_name (objfile));
14090 pc = attr_value_as_address (attr) + baseaddr;
14091 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14093 if (cu->call_site_htab == NULL)
14094 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14095 NULL, &objfile->objfile_obstack,
14096 hashtab_obstack_allocate, NULL);
14097 call_site_local.pc = pc;
14098 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14101 complaint (&symfile_complaints,
14102 _("Duplicate PC %s for DW_TAG_call_site "
14103 "DIE 0x%x [in module %s]"),
14104 paddress (gdbarch, pc), to_underlying (die->sect_off),
14105 objfile_name (objfile));
14109 /* Count parameters at the caller. */
14112 for (child_die = die->child; child_die && child_die->tag;
14113 child_die = sibling_die (child_die))
14115 if (child_die->tag != DW_TAG_call_site_parameter
14116 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14118 complaint (&symfile_complaints,
14119 _("Tag %d is not DW_TAG_call_site_parameter in "
14120 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14121 child_die->tag, to_underlying (child_die->sect_off),
14122 objfile_name (objfile));
14130 = ((struct call_site *)
14131 obstack_alloc (&objfile->objfile_obstack,
14132 sizeof (*call_site)
14133 + (sizeof (*call_site->parameter) * (nparams - 1))));
14135 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14136 call_site->pc = pc;
14138 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14139 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14141 struct die_info *func_die;
14143 /* Skip also over DW_TAG_inlined_subroutine. */
14144 for (func_die = die->parent;
14145 func_die && func_die->tag != DW_TAG_subprogram
14146 && func_die->tag != DW_TAG_subroutine_type;
14147 func_die = func_die->parent);
14149 /* DW_AT_call_all_calls is a superset
14150 of DW_AT_call_all_tail_calls. */
14152 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14153 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14154 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14155 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14157 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14158 not complete. But keep CALL_SITE for look ups via call_site_htab,
14159 both the initial caller containing the real return address PC and
14160 the final callee containing the current PC of a chain of tail
14161 calls do not need to have the tail call list complete. But any
14162 function candidate for a virtual tail call frame searched via
14163 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14164 determined unambiguously. */
14168 struct type *func_type = NULL;
14171 func_type = get_die_type (func_die, cu);
14172 if (func_type != NULL)
14174 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14176 /* Enlist this call site to the function. */
14177 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14178 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14181 complaint (&symfile_complaints,
14182 _("Cannot find function owning DW_TAG_call_site "
14183 "DIE 0x%x [in module %s]"),
14184 to_underlying (die->sect_off), objfile_name (objfile));
14188 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14190 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14192 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14195 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14196 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14198 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14199 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14200 /* Keep NULL DWARF_BLOCK. */;
14201 else if (attr_form_is_block (attr))
14203 struct dwarf2_locexpr_baton *dlbaton;
14205 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14206 dlbaton->data = DW_BLOCK (attr)->data;
14207 dlbaton->size = DW_BLOCK (attr)->size;
14208 dlbaton->per_cu = cu->per_cu;
14210 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14212 else if (attr_form_is_ref (attr))
14214 struct dwarf2_cu *target_cu = cu;
14215 struct die_info *target_die;
14217 target_die = follow_die_ref (die, attr, &target_cu);
14218 gdb_assert (target_cu->dwarf2_per_objfile->objfile == objfile);
14219 if (die_is_declaration (target_die, target_cu))
14221 const char *target_physname;
14223 /* Prefer the mangled name; otherwise compute the demangled one. */
14224 target_physname = dw2_linkage_name (target_die, target_cu);
14225 if (target_physname == NULL)
14226 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14227 if (target_physname == NULL)
14228 complaint (&symfile_complaints,
14229 _("DW_AT_call_target target DIE has invalid "
14230 "physname, for referencing DIE 0x%x [in module %s]"),
14231 to_underlying (die->sect_off), objfile_name (objfile));
14233 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14239 /* DW_AT_entry_pc should be preferred. */
14240 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14241 <= PC_BOUNDS_INVALID)
14242 complaint (&symfile_complaints,
14243 _("DW_AT_call_target target DIE has invalid "
14244 "low pc, for referencing DIE 0x%x [in module %s]"),
14245 to_underlying (die->sect_off), objfile_name (objfile));
14248 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14249 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14254 complaint (&symfile_complaints,
14255 _("DW_TAG_call_site DW_AT_call_target is neither "
14256 "block nor reference, for DIE 0x%x [in module %s]"),
14257 to_underlying (die->sect_off), objfile_name (objfile));
14259 call_site->per_cu = cu->per_cu;
14261 for (child_die = die->child;
14262 child_die && child_die->tag;
14263 child_die = sibling_die (child_die))
14265 struct call_site_parameter *parameter;
14266 struct attribute *loc, *origin;
14268 if (child_die->tag != DW_TAG_call_site_parameter
14269 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14271 /* Already printed the complaint above. */
14275 gdb_assert (call_site->parameter_count < nparams);
14276 parameter = &call_site->parameter[call_site->parameter_count];
14278 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14279 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14280 register is contained in DW_AT_call_value. */
14282 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14283 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14284 if (origin == NULL)
14286 /* This was a pre-DWARF-5 GNU extension alias
14287 for DW_AT_call_parameter. */
14288 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14290 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14292 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14294 sect_offset sect_off
14295 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14296 if (!offset_in_cu_p (&cu->header, sect_off))
14298 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14299 binding can be done only inside one CU. Such referenced DIE
14300 therefore cannot be even moved to DW_TAG_partial_unit. */
14301 complaint (&symfile_complaints,
14302 _("DW_AT_call_parameter offset is not in CU for "
14303 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14304 to_underlying (child_die->sect_off),
14305 objfile_name (objfile));
14308 parameter->u.param_cu_off
14309 = (cu_offset) (sect_off - cu->header.sect_off);
14311 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14313 complaint (&symfile_complaints,
14314 _("No DW_FORM_block* DW_AT_location for "
14315 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14316 to_underlying (child_die->sect_off), objfile_name (objfile));
14321 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14322 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14323 if (parameter->u.dwarf_reg != -1)
14324 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14325 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14326 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14327 ¶meter->u.fb_offset))
14328 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14331 complaint (&symfile_complaints,
14332 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14333 "for DW_FORM_block* DW_AT_location is supported for "
14334 "DW_TAG_call_site child DIE 0x%x "
14336 to_underlying (child_die->sect_off),
14337 objfile_name (objfile));
14342 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14344 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14345 if (!attr_form_is_block (attr))
14347 complaint (&symfile_complaints,
14348 _("No DW_FORM_block* DW_AT_call_value for "
14349 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14350 to_underlying (child_die->sect_off),
14351 objfile_name (objfile));
14354 parameter->value = DW_BLOCK (attr)->data;
14355 parameter->value_size = DW_BLOCK (attr)->size;
14357 /* Parameters are not pre-cleared by memset above. */
14358 parameter->data_value = NULL;
14359 parameter->data_value_size = 0;
14360 call_site->parameter_count++;
14362 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14364 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14367 if (!attr_form_is_block (attr))
14368 complaint (&symfile_complaints,
14369 _("No DW_FORM_block* DW_AT_call_data_value for "
14370 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14371 to_underlying (child_die->sect_off),
14372 objfile_name (objfile));
14375 parameter->data_value = DW_BLOCK (attr)->data;
14376 parameter->data_value_size = DW_BLOCK (attr)->size;
14382 /* Helper function for read_variable. If DIE represents a virtual
14383 table, then return the type of the concrete object that is
14384 associated with the virtual table. Otherwise, return NULL. */
14386 static struct type *
14387 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14389 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14393 /* Find the type DIE. */
14394 struct die_info *type_die = NULL;
14395 struct dwarf2_cu *type_cu = cu;
14397 if (attr_form_is_ref (attr))
14398 type_die = follow_die_ref (die, attr, &type_cu);
14399 if (type_die == NULL)
14402 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14404 return die_containing_type (type_die, type_cu);
14407 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14410 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14412 struct rust_vtable_symbol *storage = NULL;
14414 if (cu->language == language_rust)
14416 struct type *containing_type = rust_containing_type (die, cu);
14418 if (containing_type != NULL)
14420 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
14422 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14423 struct rust_vtable_symbol);
14424 initialize_objfile_symbol (storage);
14425 storage->concrete_type = containing_type;
14426 storage->subclass = SYMBOL_RUST_VTABLE;
14430 new_symbol_full (die, NULL, cu, storage);
14433 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14434 reading .debug_rnglists.
14435 Callback's type should be:
14436 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14437 Return true if the attributes are present and valid, otherwise,
14440 template <typename Callback>
14442 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14443 Callback &&callback)
14445 struct dwarf2_per_objfile *dwarf2_per_objfile
14446 = cu->dwarf2_per_objfile;
14447 struct objfile *objfile = dwarf2_per_objfile->objfile;
14448 bfd *obfd = objfile->obfd;
14449 /* Base address selection entry. */
14452 const gdb_byte *buffer;
14453 CORE_ADDR baseaddr;
14454 bool overflow = false;
14456 found_base = cu->base_known;
14457 base = cu->base_address;
14459 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14460 if (offset >= dwarf2_per_objfile->rnglists.size)
14462 complaint (&symfile_complaints,
14463 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14467 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14469 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14473 /* Initialize it due to a false compiler warning. */
14474 CORE_ADDR range_beginning = 0, range_end = 0;
14475 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14476 + dwarf2_per_objfile->rnglists.size);
14477 unsigned int bytes_read;
14479 if (buffer == buf_end)
14484 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14487 case DW_RLE_end_of_list:
14489 case DW_RLE_base_address:
14490 if (buffer + cu->header.addr_size > buf_end)
14495 base = read_address (obfd, buffer, cu, &bytes_read);
14497 buffer += bytes_read;
14499 case DW_RLE_start_length:
14500 if (buffer + cu->header.addr_size > buf_end)
14505 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14506 buffer += bytes_read;
14507 range_end = (range_beginning
14508 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14509 buffer += bytes_read;
14510 if (buffer > buf_end)
14516 case DW_RLE_offset_pair:
14517 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14518 buffer += bytes_read;
14519 if (buffer > buf_end)
14524 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14525 buffer += bytes_read;
14526 if (buffer > buf_end)
14532 case DW_RLE_start_end:
14533 if (buffer + 2 * cu->header.addr_size > buf_end)
14538 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14539 buffer += bytes_read;
14540 range_end = read_address (obfd, buffer, cu, &bytes_read);
14541 buffer += bytes_read;
14544 complaint (&symfile_complaints,
14545 _("Invalid .debug_rnglists data (no base address)"));
14548 if (rlet == DW_RLE_end_of_list || overflow)
14550 if (rlet == DW_RLE_base_address)
14555 /* We have no valid base address for the ranges
14557 complaint (&symfile_complaints,
14558 _("Invalid .debug_rnglists data (no base address)"));
14562 if (range_beginning > range_end)
14564 /* Inverted range entries are invalid. */
14565 complaint (&symfile_complaints,
14566 _("Invalid .debug_rnglists data (inverted range)"));
14570 /* Empty range entries have no effect. */
14571 if (range_beginning == range_end)
14574 range_beginning += base;
14577 /* A not-uncommon case of bad debug info.
14578 Don't pollute the addrmap with bad data. */
14579 if (range_beginning + baseaddr == 0
14580 && !dwarf2_per_objfile->has_section_at_zero)
14582 complaint (&symfile_complaints,
14583 _(".debug_rnglists entry has start address of zero"
14584 " [in module %s]"), objfile_name (objfile));
14588 callback (range_beginning, range_end);
14593 complaint (&symfile_complaints,
14594 _("Offset %d is not terminated "
14595 "for DW_AT_ranges attribute"),
14603 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14604 Callback's type should be:
14605 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14606 Return 1 if the attributes are present and valid, otherwise, return 0. */
14608 template <typename Callback>
14610 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14611 Callback &&callback)
14613 struct dwarf2_per_objfile *dwarf2_per_objfile
14614 = cu->dwarf2_per_objfile;
14615 struct objfile *objfile = dwarf2_per_objfile->objfile;
14616 struct comp_unit_head *cu_header = &cu->header;
14617 bfd *obfd = objfile->obfd;
14618 unsigned int addr_size = cu_header->addr_size;
14619 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14620 /* Base address selection entry. */
14623 unsigned int dummy;
14624 const gdb_byte *buffer;
14625 CORE_ADDR baseaddr;
14627 if (cu_header->version >= 5)
14628 return dwarf2_rnglists_process (offset, cu, callback);
14630 found_base = cu->base_known;
14631 base = cu->base_address;
14633 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14634 if (offset >= dwarf2_per_objfile->ranges.size)
14636 complaint (&symfile_complaints,
14637 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14641 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14643 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14647 CORE_ADDR range_beginning, range_end;
14649 range_beginning = read_address (obfd, buffer, cu, &dummy);
14650 buffer += addr_size;
14651 range_end = read_address (obfd, buffer, cu, &dummy);
14652 buffer += addr_size;
14653 offset += 2 * addr_size;
14655 /* An end of list marker is a pair of zero addresses. */
14656 if (range_beginning == 0 && range_end == 0)
14657 /* Found the end of list entry. */
14660 /* Each base address selection entry is a pair of 2 values.
14661 The first is the largest possible address, the second is
14662 the base address. Check for a base address here. */
14663 if ((range_beginning & mask) == mask)
14665 /* If we found the largest possible address, then we already
14666 have the base address in range_end. */
14674 /* We have no valid base address for the ranges
14676 complaint (&symfile_complaints,
14677 _("Invalid .debug_ranges data (no base address)"));
14681 if (range_beginning > range_end)
14683 /* Inverted range entries are invalid. */
14684 complaint (&symfile_complaints,
14685 _("Invalid .debug_ranges data (inverted range)"));
14689 /* Empty range entries have no effect. */
14690 if (range_beginning == range_end)
14693 range_beginning += base;
14696 /* A not-uncommon case of bad debug info.
14697 Don't pollute the addrmap with bad data. */
14698 if (range_beginning + baseaddr == 0
14699 && !dwarf2_per_objfile->has_section_at_zero)
14701 complaint (&symfile_complaints,
14702 _(".debug_ranges entry has start address of zero"
14703 " [in module %s]"), objfile_name (objfile));
14707 callback (range_beginning, range_end);
14713 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14714 Return 1 if the attributes are present and valid, otherwise, return 0.
14715 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14718 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14719 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14720 struct partial_symtab *ranges_pst)
14722 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
14723 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14724 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14725 SECT_OFF_TEXT (objfile));
14728 CORE_ADDR high = 0;
14731 retval = dwarf2_ranges_process (offset, cu,
14732 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14734 if (ranges_pst != NULL)
14739 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14740 range_beginning + baseaddr);
14741 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14742 range_end + baseaddr);
14743 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14747 /* FIXME: This is recording everything as a low-high
14748 segment of consecutive addresses. We should have a
14749 data structure for discontiguous block ranges
14753 low = range_beginning;
14759 if (range_beginning < low)
14760 low = range_beginning;
14761 if (range_end > high)
14769 /* If the first entry is an end-of-list marker, the range
14770 describes an empty scope, i.e. no instructions. */
14776 *high_return = high;
14780 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14781 definition for the return value. *LOWPC and *HIGHPC are set iff
14782 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14784 static enum pc_bounds_kind
14785 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14786 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14787 struct partial_symtab *pst)
14789 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
14790 struct attribute *attr;
14791 struct attribute *attr_high;
14793 CORE_ADDR high = 0;
14794 enum pc_bounds_kind ret;
14796 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14799 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14802 low = attr_value_as_address (attr);
14803 high = attr_value_as_address (attr_high);
14804 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14808 /* Found high w/o low attribute. */
14809 return PC_BOUNDS_INVALID;
14811 /* Found consecutive range of addresses. */
14812 ret = PC_BOUNDS_HIGH_LOW;
14816 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14819 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14820 We take advantage of the fact that DW_AT_ranges does not appear
14821 in DW_TAG_compile_unit of DWO files. */
14822 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14823 unsigned int ranges_offset = (DW_UNSND (attr)
14824 + (need_ranges_base
14828 /* Value of the DW_AT_ranges attribute is the offset in the
14829 .debug_ranges section. */
14830 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14831 return PC_BOUNDS_INVALID;
14832 /* Found discontinuous range of addresses. */
14833 ret = PC_BOUNDS_RANGES;
14836 return PC_BOUNDS_NOT_PRESENT;
14839 /* read_partial_die has also the strict LOW < HIGH requirement. */
14841 return PC_BOUNDS_INVALID;
14843 /* When using the GNU linker, .gnu.linkonce. sections are used to
14844 eliminate duplicate copies of functions and vtables and such.
14845 The linker will arbitrarily choose one and discard the others.
14846 The AT_*_pc values for such functions refer to local labels in
14847 these sections. If the section from that file was discarded, the
14848 labels are not in the output, so the relocs get a value of 0.
14849 If this is a discarded function, mark the pc bounds as invalid,
14850 so that GDB will ignore it. */
14851 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14852 return PC_BOUNDS_INVALID;
14860 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14861 its low and high PC addresses. Do nothing if these addresses could not
14862 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14863 and HIGHPC to the high address if greater than HIGHPC. */
14866 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14867 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14868 struct dwarf2_cu *cu)
14870 CORE_ADDR low, high;
14871 struct die_info *child = die->child;
14873 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14875 *lowpc = std::min (*lowpc, low);
14876 *highpc = std::max (*highpc, high);
14879 /* If the language does not allow nested subprograms (either inside
14880 subprograms or lexical blocks), we're done. */
14881 if (cu->language != language_ada)
14884 /* Check all the children of the given DIE. If it contains nested
14885 subprograms, then check their pc bounds. Likewise, we need to
14886 check lexical blocks as well, as they may also contain subprogram
14888 while (child && child->tag)
14890 if (child->tag == DW_TAG_subprogram
14891 || child->tag == DW_TAG_lexical_block)
14892 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14893 child = sibling_die (child);
14897 /* Get the low and high pc's represented by the scope DIE, and store
14898 them in *LOWPC and *HIGHPC. If the correct values can't be
14899 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14902 get_scope_pc_bounds (struct die_info *die,
14903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14904 struct dwarf2_cu *cu)
14906 CORE_ADDR best_low = (CORE_ADDR) -1;
14907 CORE_ADDR best_high = (CORE_ADDR) 0;
14908 CORE_ADDR current_low, current_high;
14910 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14911 >= PC_BOUNDS_RANGES)
14913 best_low = current_low;
14914 best_high = current_high;
14918 struct die_info *child = die->child;
14920 while (child && child->tag)
14922 switch (child->tag) {
14923 case DW_TAG_subprogram:
14924 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14926 case DW_TAG_namespace:
14927 case DW_TAG_module:
14928 /* FIXME: carlton/2004-01-16: Should we do this for
14929 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14930 that current GCC's always emit the DIEs corresponding
14931 to definitions of methods of classes as children of a
14932 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14933 the DIEs giving the declarations, which could be
14934 anywhere). But I don't see any reason why the
14935 standards says that they have to be there. */
14936 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14938 if (current_low != ((CORE_ADDR) -1))
14940 best_low = std::min (best_low, current_low);
14941 best_high = std::max (best_high, current_high);
14949 child = sibling_die (child);
14954 *highpc = best_high;
14957 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14961 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14962 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14964 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
14965 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14966 struct attribute *attr;
14967 struct attribute *attr_high;
14969 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14972 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14975 CORE_ADDR low = attr_value_as_address (attr);
14976 CORE_ADDR high = attr_value_as_address (attr_high);
14978 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14981 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14982 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14983 record_block_range (block, low, high - 1);
14987 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14990 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14991 We take advantage of the fact that DW_AT_ranges does not appear
14992 in DW_TAG_compile_unit of DWO files. */
14993 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14995 /* The value of the DW_AT_ranges attribute is the offset of the
14996 address range list in the .debug_ranges section. */
14997 unsigned long offset = (DW_UNSND (attr)
14998 + (need_ranges_base ? cu->ranges_base : 0));
14999 const gdb_byte *buffer;
15001 /* For some target architectures, but not others, the
15002 read_address function sign-extends the addresses it returns.
15003 To recognize base address selection entries, we need a
15005 unsigned int addr_size = cu->header.addr_size;
15006 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15008 /* The base address, to which the next pair is relative. Note
15009 that this 'base' is a DWARF concept: most entries in a range
15010 list are relative, to reduce the number of relocs against the
15011 debugging information. This is separate from this function's
15012 'baseaddr' argument, which GDB uses to relocate debugging
15013 information from a shared library based on the address at
15014 which the library was loaded. */
15015 CORE_ADDR base = cu->base_address;
15016 int base_known = cu->base_known;
15018 dwarf2_ranges_process (offset, cu,
15019 [&] (CORE_ADDR start, CORE_ADDR end)
15023 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15024 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15025 record_block_range (block, start, end - 1);
15030 /* Check whether the producer field indicates either of GCC < 4.6, or the
15031 Intel C/C++ compiler, and cache the result in CU. */
15034 check_producer (struct dwarf2_cu *cu)
15038 if (cu->producer == NULL)
15040 /* For unknown compilers expect their behavior is DWARF version
15043 GCC started to support .debug_types sections by -gdwarf-4 since
15044 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15045 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15046 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15047 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15049 else if (producer_is_gcc (cu->producer, &major, &minor))
15051 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15052 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15054 else if (producer_is_icc (cu->producer, &major, &minor))
15055 cu->producer_is_icc_lt_14 = major < 14;
15058 /* For other non-GCC compilers, expect their behavior is DWARF version
15062 cu->checked_producer = 1;
15065 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15066 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15067 during 4.6.0 experimental. */
15070 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15072 if (!cu->checked_producer)
15073 check_producer (cu);
15075 return cu->producer_is_gxx_lt_4_6;
15078 /* Return the default accessibility type if it is not overriden by
15079 DW_AT_accessibility. */
15081 static enum dwarf_access_attribute
15082 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15084 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15086 /* The default DWARF 2 accessibility for members is public, the default
15087 accessibility for inheritance is private. */
15089 if (die->tag != DW_TAG_inheritance)
15090 return DW_ACCESS_public;
15092 return DW_ACCESS_private;
15096 /* DWARF 3+ defines the default accessibility a different way. The same
15097 rules apply now for DW_TAG_inheritance as for the members and it only
15098 depends on the container kind. */
15100 if (die->parent->tag == DW_TAG_class_type)
15101 return DW_ACCESS_private;
15103 return DW_ACCESS_public;
15107 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15108 offset. If the attribute was not found return 0, otherwise return
15109 1. If it was found but could not properly be handled, set *OFFSET
15113 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15116 struct attribute *attr;
15118 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15123 /* Note that we do not check for a section offset first here.
15124 This is because DW_AT_data_member_location is new in DWARF 4,
15125 so if we see it, we can assume that a constant form is really
15126 a constant and not a section offset. */
15127 if (attr_form_is_constant (attr))
15128 *offset = dwarf2_get_attr_constant_value (attr, 0);
15129 else if (attr_form_is_section_offset (attr))
15130 dwarf2_complex_location_expr_complaint ();
15131 else if (attr_form_is_block (attr))
15132 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15134 dwarf2_complex_location_expr_complaint ();
15142 /* Add an aggregate field to the field list. */
15145 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15146 struct dwarf2_cu *cu)
15148 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
15149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15150 struct nextfield *new_field;
15151 struct attribute *attr;
15153 const char *fieldname = "";
15155 /* Allocate a new field list entry and link it in. */
15156 new_field = XNEW (struct nextfield);
15157 make_cleanup (xfree, new_field);
15158 memset (new_field, 0, sizeof (struct nextfield));
15160 if (die->tag == DW_TAG_inheritance)
15162 new_field->next = fip->baseclasses;
15163 fip->baseclasses = new_field;
15167 new_field->next = fip->fields;
15168 fip->fields = new_field;
15172 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15174 new_field->accessibility = DW_UNSND (attr);
15176 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15177 if (new_field->accessibility != DW_ACCESS_public)
15178 fip->non_public_fields = 1;
15180 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15182 new_field->virtuality = DW_UNSND (attr);
15184 new_field->virtuality = DW_VIRTUALITY_none;
15186 fp = &new_field->field;
15188 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15192 /* Data member other than a C++ static data member. */
15194 /* Get type of field. */
15195 fp->type = die_type (die, cu);
15197 SET_FIELD_BITPOS (*fp, 0);
15199 /* Get bit size of field (zero if none). */
15200 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15203 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15207 FIELD_BITSIZE (*fp) = 0;
15210 /* Get bit offset of field. */
15211 if (handle_data_member_location (die, cu, &offset))
15212 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15213 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15216 if (gdbarch_bits_big_endian (gdbarch))
15218 /* For big endian bits, the DW_AT_bit_offset gives the
15219 additional bit offset from the MSB of the containing
15220 anonymous object to the MSB of the field. We don't
15221 have to do anything special since we don't need to
15222 know the size of the anonymous object. */
15223 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15227 /* For little endian bits, compute the bit offset to the
15228 MSB of the anonymous object, subtract off the number of
15229 bits from the MSB of the field to the MSB of the
15230 object, and then subtract off the number of bits of
15231 the field itself. The result is the bit offset of
15232 the LSB of the field. */
15233 int anonymous_size;
15234 int bit_offset = DW_UNSND (attr);
15236 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15239 /* The size of the anonymous object containing
15240 the bit field is explicit, so use the
15241 indicated size (in bytes). */
15242 anonymous_size = DW_UNSND (attr);
15246 /* The size of the anonymous object containing
15247 the bit field must be inferred from the type
15248 attribute of the data member containing the
15250 anonymous_size = TYPE_LENGTH (fp->type);
15252 SET_FIELD_BITPOS (*fp,
15253 (FIELD_BITPOS (*fp)
15254 + anonymous_size * bits_per_byte
15255 - bit_offset - FIELD_BITSIZE (*fp)));
15258 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15260 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15261 + dwarf2_get_attr_constant_value (attr, 0)));
15263 /* Get name of field. */
15264 fieldname = dwarf2_name (die, cu);
15265 if (fieldname == NULL)
15268 /* The name is already allocated along with this objfile, so we don't
15269 need to duplicate it for the type. */
15270 fp->name = fieldname;
15272 /* Change accessibility for artificial fields (e.g. virtual table
15273 pointer or virtual base class pointer) to private. */
15274 if (dwarf2_attr (die, DW_AT_artificial, cu))
15276 FIELD_ARTIFICIAL (*fp) = 1;
15277 new_field->accessibility = DW_ACCESS_private;
15278 fip->non_public_fields = 1;
15281 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15283 /* C++ static member. */
15285 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15286 is a declaration, but all versions of G++ as of this writing
15287 (so through at least 3.2.1) incorrectly generate
15288 DW_TAG_variable tags. */
15290 const char *physname;
15292 /* Get name of field. */
15293 fieldname = dwarf2_name (die, cu);
15294 if (fieldname == NULL)
15297 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15299 /* Only create a symbol if this is an external value.
15300 new_symbol checks this and puts the value in the global symbol
15301 table, which we want. If it is not external, new_symbol
15302 will try to put the value in cu->list_in_scope which is wrong. */
15303 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15305 /* A static const member, not much different than an enum as far as
15306 we're concerned, except that we can support more types. */
15307 new_symbol (die, NULL, cu);
15310 /* Get physical name. */
15311 physname = dwarf2_physname (fieldname, die, cu);
15313 /* The name is already allocated along with this objfile, so we don't
15314 need to duplicate it for the type. */
15315 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15316 FIELD_TYPE (*fp) = die_type (die, cu);
15317 FIELD_NAME (*fp) = fieldname;
15319 else if (die->tag == DW_TAG_inheritance)
15323 /* C++ base class field. */
15324 if (handle_data_member_location (die, cu, &offset))
15325 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15326 FIELD_BITSIZE (*fp) = 0;
15327 FIELD_TYPE (*fp) = die_type (die, cu);
15328 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15329 fip->nbaseclasses++;
15333 /* Can the type given by DIE define another type? */
15336 type_can_define_types (const struct die_info *die)
15340 case DW_TAG_typedef:
15341 case DW_TAG_class_type:
15342 case DW_TAG_structure_type:
15343 case DW_TAG_union_type:
15344 case DW_TAG_enumeration_type:
15352 /* Add a type definition defined in the scope of the FIP's class. */
15355 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15356 struct dwarf2_cu *cu)
15358 struct decl_field_list *new_field;
15359 struct decl_field *fp;
15361 /* Allocate a new field list entry and link it in. */
15362 new_field = XCNEW (struct decl_field_list);
15363 make_cleanup (xfree, new_field);
15365 gdb_assert (type_can_define_types (die));
15367 fp = &new_field->field;
15369 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15370 fp->name = dwarf2_name (die, cu);
15371 fp->type = read_type_die (die, cu);
15373 /* Save accessibility. */
15374 enum dwarf_access_attribute accessibility;
15375 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15377 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15379 accessibility = dwarf2_default_access_attribute (die, cu);
15380 switch (accessibility)
15382 case DW_ACCESS_public:
15383 /* The assumed value if neither private nor protected. */
15385 case DW_ACCESS_private:
15386 fp->is_private = 1;
15388 case DW_ACCESS_protected:
15389 fp->is_protected = 1;
15392 complaint (&symfile_complaints,
15393 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15396 if (die->tag == DW_TAG_typedef)
15398 new_field->next = fip->typedef_field_list;
15399 fip->typedef_field_list = new_field;
15400 fip->typedef_field_list_count++;
15404 new_field->next = fip->nested_types_list;
15405 fip->nested_types_list = new_field;
15406 fip->nested_types_list_count++;
15410 /* Create the vector of fields, and attach it to the type. */
15413 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15414 struct dwarf2_cu *cu)
15416 int nfields = fip->nfields;
15418 /* Record the field count, allocate space for the array of fields,
15419 and create blank accessibility bitfields if necessary. */
15420 TYPE_NFIELDS (type) = nfields;
15421 TYPE_FIELDS (type) = (struct field *)
15422 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15423 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15425 if (fip->non_public_fields && cu->language != language_ada)
15427 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15429 TYPE_FIELD_PRIVATE_BITS (type) =
15430 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15431 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15433 TYPE_FIELD_PROTECTED_BITS (type) =
15434 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15435 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15437 TYPE_FIELD_IGNORE_BITS (type) =
15438 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15439 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15442 /* If the type has baseclasses, allocate and clear a bit vector for
15443 TYPE_FIELD_VIRTUAL_BITS. */
15444 if (fip->nbaseclasses && cu->language != language_ada)
15446 int num_bytes = B_BYTES (fip->nbaseclasses);
15447 unsigned char *pointer;
15449 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15450 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15451 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15452 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15453 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15456 /* Copy the saved-up fields into the field vector. Start from the head of
15457 the list, adding to the tail of the field array, so that they end up in
15458 the same order in the array in which they were added to the list. */
15459 while (nfields-- > 0)
15461 struct nextfield *fieldp;
15465 fieldp = fip->fields;
15466 fip->fields = fieldp->next;
15470 fieldp = fip->baseclasses;
15471 fip->baseclasses = fieldp->next;
15474 TYPE_FIELD (type, nfields) = fieldp->field;
15475 switch (fieldp->accessibility)
15477 case DW_ACCESS_private:
15478 if (cu->language != language_ada)
15479 SET_TYPE_FIELD_PRIVATE (type, nfields);
15482 case DW_ACCESS_protected:
15483 if (cu->language != language_ada)
15484 SET_TYPE_FIELD_PROTECTED (type, nfields);
15487 case DW_ACCESS_public:
15491 /* Unknown accessibility. Complain and treat it as public. */
15493 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15494 fieldp->accessibility);
15498 if (nfields < fip->nbaseclasses)
15500 switch (fieldp->virtuality)
15502 case DW_VIRTUALITY_virtual:
15503 case DW_VIRTUALITY_pure_virtual:
15504 if (cu->language == language_ada)
15505 error (_("unexpected virtuality in component of Ada type"));
15506 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15513 /* Return true if this member function is a constructor, false
15517 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15519 const char *fieldname;
15520 const char *type_name;
15523 if (die->parent == NULL)
15526 if (die->parent->tag != DW_TAG_structure_type
15527 && die->parent->tag != DW_TAG_union_type
15528 && die->parent->tag != DW_TAG_class_type)
15531 fieldname = dwarf2_name (die, cu);
15532 type_name = dwarf2_name (die->parent, cu);
15533 if (fieldname == NULL || type_name == NULL)
15536 len = strlen (fieldname);
15537 return (strncmp (fieldname, type_name, len) == 0
15538 && (type_name[len] == '\0' || type_name[len] == '<'));
15541 /* Add a member function to the proper fieldlist. */
15544 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15545 struct type *type, struct dwarf2_cu *cu)
15547 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
15548 struct attribute *attr;
15549 struct fnfieldlist *flp;
15551 struct fn_field *fnp;
15552 const char *fieldname;
15553 struct nextfnfield *new_fnfield;
15554 struct type *this_type;
15555 enum dwarf_access_attribute accessibility;
15557 if (cu->language == language_ada)
15558 error (_("unexpected member function in Ada type"));
15560 /* Get name of member function. */
15561 fieldname = dwarf2_name (die, cu);
15562 if (fieldname == NULL)
15565 /* Look up member function name in fieldlist. */
15566 for (i = 0; i < fip->nfnfields; i++)
15568 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15572 /* Create new list element if necessary. */
15573 if (i < fip->nfnfields)
15574 flp = &fip->fnfieldlists[i];
15577 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15579 fip->fnfieldlists = (struct fnfieldlist *)
15580 xrealloc (fip->fnfieldlists,
15581 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15582 * sizeof (struct fnfieldlist));
15583 if (fip->nfnfields == 0)
15584 make_cleanup (free_current_contents, &fip->fnfieldlists);
15586 flp = &fip->fnfieldlists[fip->nfnfields];
15587 flp->name = fieldname;
15590 i = fip->nfnfields++;
15593 /* Create a new member function field and chain it to the field list
15595 new_fnfield = XNEW (struct nextfnfield);
15596 make_cleanup (xfree, new_fnfield);
15597 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15598 new_fnfield->next = flp->head;
15599 flp->head = new_fnfield;
15602 /* Fill in the member function field info. */
15603 fnp = &new_fnfield->fnfield;
15605 /* Delay processing of the physname until later. */
15606 if (cu->language == language_cplus)
15608 add_to_method_list (type, i, flp->length - 1, fieldname,
15613 const char *physname = dwarf2_physname (fieldname, die, cu);
15614 fnp->physname = physname ? physname : "";
15617 fnp->type = alloc_type (objfile);
15618 this_type = read_type_die (die, cu);
15619 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15621 int nparams = TYPE_NFIELDS (this_type);
15623 /* TYPE is the domain of this method, and THIS_TYPE is the type
15624 of the method itself (TYPE_CODE_METHOD). */
15625 smash_to_method_type (fnp->type, type,
15626 TYPE_TARGET_TYPE (this_type),
15627 TYPE_FIELDS (this_type),
15628 TYPE_NFIELDS (this_type),
15629 TYPE_VARARGS (this_type));
15631 /* Handle static member functions.
15632 Dwarf2 has no clean way to discern C++ static and non-static
15633 member functions. G++ helps GDB by marking the first
15634 parameter for non-static member functions (which is the this
15635 pointer) as artificial. We obtain this information from
15636 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15637 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15638 fnp->voffset = VOFFSET_STATIC;
15641 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15642 dwarf2_full_name (fieldname, die, cu));
15644 /* Get fcontext from DW_AT_containing_type if present. */
15645 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15646 fnp->fcontext = die_containing_type (die, cu);
15648 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15649 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15651 /* Get accessibility. */
15652 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15654 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15656 accessibility = dwarf2_default_access_attribute (die, cu);
15657 switch (accessibility)
15659 case DW_ACCESS_private:
15660 fnp->is_private = 1;
15662 case DW_ACCESS_protected:
15663 fnp->is_protected = 1;
15667 /* Check for artificial methods. */
15668 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15669 if (attr && DW_UNSND (attr) != 0)
15670 fnp->is_artificial = 1;
15672 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15674 /* Get index in virtual function table if it is a virtual member
15675 function. For older versions of GCC, this is an offset in the
15676 appropriate virtual table, as specified by DW_AT_containing_type.
15677 For everyone else, it is an expression to be evaluated relative
15678 to the object address. */
15680 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15683 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15685 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15687 /* Old-style GCC. */
15688 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15690 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15691 || (DW_BLOCK (attr)->size > 1
15692 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15693 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15695 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15696 if ((fnp->voffset % cu->header.addr_size) != 0)
15697 dwarf2_complex_location_expr_complaint ();
15699 fnp->voffset /= cu->header.addr_size;
15703 dwarf2_complex_location_expr_complaint ();
15705 if (!fnp->fcontext)
15707 /* If there is no `this' field and no DW_AT_containing_type,
15708 we cannot actually find a base class context for the
15710 if (TYPE_NFIELDS (this_type) == 0
15711 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15713 complaint (&symfile_complaints,
15714 _("cannot determine context for virtual member "
15715 "function \"%s\" (offset %d)"),
15716 fieldname, to_underlying (die->sect_off));
15721 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15725 else if (attr_form_is_section_offset (attr))
15727 dwarf2_complex_location_expr_complaint ();
15731 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15737 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15738 if (attr && DW_UNSND (attr))
15740 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15741 complaint (&symfile_complaints,
15742 _("Member function \"%s\" (offset %d) is virtual "
15743 "but the vtable offset is not specified"),
15744 fieldname, to_underlying (die->sect_off));
15745 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15746 TYPE_CPLUS_DYNAMIC (type) = 1;
15751 /* Create the vector of member function fields, and attach it to the type. */
15754 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15755 struct dwarf2_cu *cu)
15757 struct fnfieldlist *flp;
15760 if (cu->language == language_ada)
15761 error (_("unexpected member functions in Ada type"));
15763 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15764 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15765 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15767 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15769 struct nextfnfield *nfp = flp->head;
15770 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15773 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15774 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15775 fn_flp->fn_fields = (struct fn_field *)
15776 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15777 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15778 fn_flp->fn_fields[k] = nfp->fnfield;
15781 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15784 /* Returns non-zero if NAME is the name of a vtable member in CU's
15785 language, zero otherwise. */
15787 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15789 static const char vptr[] = "_vptr";
15791 /* Look for the C++ form of the vtable. */
15792 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15798 /* GCC outputs unnamed structures that are really pointers to member
15799 functions, with the ABI-specified layout. If TYPE describes
15800 such a structure, smash it into a member function type.
15802 GCC shouldn't do this; it should just output pointer to member DIEs.
15803 This is GCC PR debug/28767. */
15806 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15808 struct type *pfn_type, *self_type, *new_type;
15810 /* Check for a structure with no name and two children. */
15811 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15814 /* Check for __pfn and __delta members. */
15815 if (TYPE_FIELD_NAME (type, 0) == NULL
15816 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15817 || TYPE_FIELD_NAME (type, 1) == NULL
15818 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15821 /* Find the type of the method. */
15822 pfn_type = TYPE_FIELD_TYPE (type, 0);
15823 if (pfn_type == NULL
15824 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15825 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15828 /* Look for the "this" argument. */
15829 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15830 if (TYPE_NFIELDS (pfn_type) == 0
15831 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15832 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15835 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15836 new_type = alloc_type (objfile);
15837 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15838 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15839 TYPE_VARARGS (pfn_type));
15840 smash_to_methodptr_type (type, new_type);
15844 /* Called when we find the DIE that starts a structure or union scope
15845 (definition) to create a type for the structure or union. Fill in
15846 the type's name and general properties; the members will not be
15847 processed until process_structure_scope. A symbol table entry for
15848 the type will also not be done until process_structure_scope (assuming
15849 the type has a name).
15851 NOTE: we need to call these functions regardless of whether or not the
15852 DIE has a DW_AT_name attribute, since it might be an anonymous
15853 structure or union. This gets the type entered into our set of
15854 user defined types. */
15856 static struct type *
15857 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15859 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
15861 struct attribute *attr;
15864 /* If the definition of this type lives in .debug_types, read that type.
15865 Don't follow DW_AT_specification though, that will take us back up
15866 the chain and we want to go down. */
15867 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15870 type = get_DW_AT_signature_type (die, attr, cu);
15872 /* The type's CU may not be the same as CU.
15873 Ensure TYPE is recorded with CU in die_type_hash. */
15874 return set_die_type (die, type, cu);
15877 type = alloc_type (objfile);
15878 INIT_CPLUS_SPECIFIC (type);
15880 name = dwarf2_name (die, cu);
15883 if (cu->language == language_cplus
15884 || cu->language == language_d
15885 || cu->language == language_rust)
15887 const char *full_name = dwarf2_full_name (name, die, cu);
15889 /* dwarf2_full_name might have already finished building the DIE's
15890 type. If so, there is no need to continue. */
15891 if (get_die_type (die, cu) != NULL)
15892 return get_die_type (die, cu);
15894 TYPE_TAG_NAME (type) = full_name;
15895 if (die->tag == DW_TAG_structure_type
15896 || die->tag == DW_TAG_class_type)
15897 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15901 /* The name is already allocated along with this objfile, so
15902 we don't need to duplicate it for the type. */
15903 TYPE_TAG_NAME (type) = name;
15904 if (die->tag == DW_TAG_class_type)
15905 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15909 if (die->tag == DW_TAG_structure_type)
15911 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15913 else if (die->tag == DW_TAG_union_type)
15915 TYPE_CODE (type) = TYPE_CODE_UNION;
15919 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15922 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15923 TYPE_DECLARED_CLASS (type) = 1;
15925 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15928 if (attr_form_is_constant (attr))
15929 TYPE_LENGTH (type) = DW_UNSND (attr);
15932 /* For the moment, dynamic type sizes are not supported
15933 by GDB's struct type. The actual size is determined
15934 on-demand when resolving the type of a given object,
15935 so set the type's length to zero for now. Otherwise,
15936 we record an expression as the length, and that expression
15937 could lead to a very large value, which could eventually
15938 lead to us trying to allocate that much memory when creating
15939 a value of that type. */
15940 TYPE_LENGTH (type) = 0;
15945 TYPE_LENGTH (type) = 0;
15948 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15950 /* ICC<14 does not output the required DW_AT_declaration on
15951 incomplete types, but gives them a size of zero. */
15952 TYPE_STUB (type) = 1;
15955 TYPE_STUB_SUPPORTED (type) = 1;
15957 if (die_is_declaration (die, cu))
15958 TYPE_STUB (type) = 1;
15959 else if (attr == NULL && die->child == NULL
15960 && producer_is_realview (cu->producer))
15961 /* RealView does not output the required DW_AT_declaration
15962 on incomplete types. */
15963 TYPE_STUB (type) = 1;
15965 /* We need to add the type field to the die immediately so we don't
15966 infinitely recurse when dealing with pointers to the structure
15967 type within the structure itself. */
15968 set_die_type (die, type, cu);
15970 /* set_die_type should be already done. */
15971 set_descriptive_type (type, die, cu);
15976 /* Finish creating a structure or union type, including filling in
15977 its members and creating a symbol for it. */
15980 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15982 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
15983 struct die_info *child_die;
15986 type = get_die_type (die, cu);
15988 type = read_structure_type (die, cu);
15990 if (die->child != NULL && ! die_is_declaration (die, cu))
15992 struct field_info fi;
15993 std::vector<struct symbol *> template_args;
15994 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15996 memset (&fi, 0, sizeof (struct field_info));
15998 child_die = die->child;
16000 while (child_die && child_die->tag)
16002 if (child_die->tag == DW_TAG_member
16003 || child_die->tag == DW_TAG_variable)
16005 /* NOTE: carlton/2002-11-05: A C++ static data member
16006 should be a DW_TAG_member that is a declaration, but
16007 all versions of G++ as of this writing (so through at
16008 least 3.2.1) incorrectly generate DW_TAG_variable
16009 tags for them instead. */
16010 dwarf2_add_field (&fi, child_die, cu);
16012 else if (child_die->tag == DW_TAG_subprogram)
16014 /* Rust doesn't have member functions in the C++ sense.
16015 However, it does emit ordinary functions as children
16016 of a struct DIE. */
16017 if (cu->language == language_rust)
16018 read_func_scope (child_die, cu);
16021 /* C++ member function. */
16022 dwarf2_add_member_fn (&fi, child_die, type, cu);
16025 else if (child_die->tag == DW_TAG_inheritance)
16027 /* C++ base class field. */
16028 dwarf2_add_field (&fi, child_die, cu);
16030 else if (type_can_define_types (child_die))
16031 dwarf2_add_type_defn (&fi, child_die, cu);
16032 else if (child_die->tag == DW_TAG_template_type_param
16033 || child_die->tag == DW_TAG_template_value_param)
16035 struct symbol *arg = new_symbol (child_die, NULL, cu);
16038 template_args.push_back (arg);
16041 child_die = sibling_die (child_die);
16044 /* Attach template arguments to type. */
16045 if (!template_args.empty ())
16047 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16048 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16049 TYPE_TEMPLATE_ARGUMENTS (type)
16050 = XOBNEWVEC (&objfile->objfile_obstack,
16052 TYPE_N_TEMPLATE_ARGUMENTS (type));
16053 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16054 template_args.data (),
16055 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16056 * sizeof (struct symbol *)));
16059 /* Attach fields and member functions to the type. */
16061 dwarf2_attach_fields_to_type (&fi, type, cu);
16064 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16066 /* Get the type which refers to the base class (possibly this
16067 class itself) which contains the vtable pointer for the current
16068 class from the DW_AT_containing_type attribute. This use of
16069 DW_AT_containing_type is a GNU extension. */
16071 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16073 struct type *t = die_containing_type (die, cu);
16075 set_type_vptr_basetype (type, t);
16080 /* Our own class provides vtbl ptr. */
16081 for (i = TYPE_NFIELDS (t) - 1;
16082 i >= TYPE_N_BASECLASSES (t);
16085 const char *fieldname = TYPE_FIELD_NAME (t, i);
16087 if (is_vtable_name (fieldname, cu))
16089 set_type_vptr_fieldno (type, i);
16094 /* Complain if virtual function table field not found. */
16095 if (i < TYPE_N_BASECLASSES (t))
16096 complaint (&symfile_complaints,
16097 _("virtual function table pointer "
16098 "not found when defining class '%s'"),
16099 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16104 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16107 else if (cu->producer
16108 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16110 /* The IBM XLC compiler does not provide direct indication
16111 of the containing type, but the vtable pointer is
16112 always named __vfp. */
16116 for (i = TYPE_NFIELDS (type) - 1;
16117 i >= TYPE_N_BASECLASSES (type);
16120 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16122 set_type_vptr_fieldno (type, i);
16123 set_type_vptr_basetype (type, type);
16130 /* Copy fi.typedef_field_list linked list elements content into the
16131 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16132 if (fi.typedef_field_list)
16134 int i = fi.typedef_field_list_count;
16136 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16137 TYPE_TYPEDEF_FIELD_ARRAY (type)
16138 = ((struct decl_field *)
16139 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16140 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16142 /* Reverse the list order to keep the debug info elements order. */
16145 struct decl_field *dest, *src;
16147 dest = &TYPE_TYPEDEF_FIELD (type, i);
16148 src = &fi.typedef_field_list->field;
16149 fi.typedef_field_list = fi.typedef_field_list->next;
16154 /* Copy fi.nested_types_list linked list elements content into the
16155 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16156 if (fi.nested_types_list != NULL && cu->language != language_ada)
16158 int i = fi.nested_types_list_count;
16160 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16161 TYPE_NESTED_TYPES_ARRAY (type)
16162 = ((struct decl_field *)
16163 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16164 TYPE_NESTED_TYPES_COUNT (type) = i;
16166 /* Reverse the list order to keep the debug info elements order. */
16169 struct decl_field *dest, *src;
16171 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16172 src = &fi.nested_types_list->field;
16173 fi.nested_types_list = fi.nested_types_list->next;
16178 do_cleanups (back_to);
16181 quirk_gcc_member_function_pointer (type, objfile);
16183 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16184 snapshots) has been known to create a die giving a declaration
16185 for a class that has, as a child, a die giving a definition for a
16186 nested class. So we have to process our children even if the
16187 current die is a declaration. Normally, of course, a declaration
16188 won't have any children at all. */
16190 child_die = die->child;
16192 while (child_die != NULL && child_die->tag)
16194 if (child_die->tag == DW_TAG_member
16195 || child_die->tag == DW_TAG_variable
16196 || child_die->tag == DW_TAG_inheritance
16197 || child_die->tag == DW_TAG_template_value_param
16198 || child_die->tag == DW_TAG_template_type_param)
16203 process_die (child_die, cu);
16205 child_die = sibling_die (child_die);
16208 /* Do not consider external references. According to the DWARF standard,
16209 these DIEs are identified by the fact that they have no byte_size
16210 attribute, and a declaration attribute. */
16211 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16212 || !die_is_declaration (die, cu))
16213 new_symbol (die, type, cu);
16216 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16217 update TYPE using some information only available in DIE's children. */
16220 update_enumeration_type_from_children (struct die_info *die,
16222 struct dwarf2_cu *cu)
16224 struct die_info *child_die;
16225 int unsigned_enum = 1;
16229 auto_obstack obstack;
16231 for (child_die = die->child;
16232 child_die != NULL && child_die->tag;
16233 child_die = sibling_die (child_die))
16235 struct attribute *attr;
16237 const gdb_byte *bytes;
16238 struct dwarf2_locexpr_baton *baton;
16241 if (child_die->tag != DW_TAG_enumerator)
16244 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16248 name = dwarf2_name (child_die, cu);
16250 name = "<anonymous enumerator>";
16252 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16253 &value, &bytes, &baton);
16259 else if ((mask & value) != 0)
16264 /* If we already know that the enum type is neither unsigned, nor
16265 a flag type, no need to look at the rest of the enumerates. */
16266 if (!unsigned_enum && !flag_enum)
16271 TYPE_UNSIGNED (type) = 1;
16273 TYPE_FLAG_ENUM (type) = 1;
16276 /* Given a DW_AT_enumeration_type die, set its type. We do not
16277 complete the type's fields yet, or create any symbols. */
16279 static struct type *
16280 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16282 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
16284 struct attribute *attr;
16287 /* If the definition of this type lives in .debug_types, read that type.
16288 Don't follow DW_AT_specification though, that will take us back up
16289 the chain and we want to go down. */
16290 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16293 type = get_DW_AT_signature_type (die, attr, cu);
16295 /* The type's CU may not be the same as CU.
16296 Ensure TYPE is recorded with CU in die_type_hash. */
16297 return set_die_type (die, type, cu);
16300 type = alloc_type (objfile);
16302 TYPE_CODE (type) = TYPE_CODE_ENUM;
16303 name = dwarf2_full_name (NULL, die, cu);
16305 TYPE_TAG_NAME (type) = name;
16307 attr = dwarf2_attr (die, DW_AT_type, cu);
16310 struct type *underlying_type = die_type (die, cu);
16312 TYPE_TARGET_TYPE (type) = underlying_type;
16315 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16318 TYPE_LENGTH (type) = DW_UNSND (attr);
16322 TYPE_LENGTH (type) = 0;
16325 /* The enumeration DIE can be incomplete. In Ada, any type can be
16326 declared as private in the package spec, and then defined only
16327 inside the package body. Such types are known as Taft Amendment
16328 Types. When another package uses such a type, an incomplete DIE
16329 may be generated by the compiler. */
16330 if (die_is_declaration (die, cu))
16331 TYPE_STUB (type) = 1;
16333 /* Finish the creation of this type by using the enum's children.
16334 We must call this even when the underlying type has been provided
16335 so that we can determine if we're looking at a "flag" enum. */
16336 update_enumeration_type_from_children (die, type, cu);
16338 /* If this type has an underlying type that is not a stub, then we
16339 may use its attributes. We always use the "unsigned" attribute
16340 in this situation, because ordinarily we guess whether the type
16341 is unsigned -- but the guess can be wrong and the underlying type
16342 can tell us the reality. However, we defer to a local size
16343 attribute if one exists, because this lets the compiler override
16344 the underlying type if needed. */
16345 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16347 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16348 if (TYPE_LENGTH (type) == 0)
16349 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16352 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16354 return set_die_type (die, type, cu);
16357 /* Given a pointer to a die which begins an enumeration, process all
16358 the dies that define the members of the enumeration, and create the
16359 symbol for the enumeration type.
16361 NOTE: We reverse the order of the element list. */
16364 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16366 struct type *this_type;
16368 this_type = get_die_type (die, cu);
16369 if (this_type == NULL)
16370 this_type = read_enumeration_type (die, cu);
16372 if (die->child != NULL)
16374 struct die_info *child_die;
16375 struct symbol *sym;
16376 struct field *fields = NULL;
16377 int num_fields = 0;
16380 child_die = die->child;
16381 while (child_die && child_die->tag)
16383 if (child_die->tag != DW_TAG_enumerator)
16385 process_die (child_die, cu);
16389 name = dwarf2_name (child_die, cu);
16392 sym = new_symbol (child_die, this_type, cu);
16394 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16396 fields = (struct field *)
16398 (num_fields + DW_FIELD_ALLOC_CHUNK)
16399 * sizeof (struct field));
16402 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16403 FIELD_TYPE (fields[num_fields]) = NULL;
16404 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16405 FIELD_BITSIZE (fields[num_fields]) = 0;
16411 child_die = sibling_die (child_die);
16416 TYPE_NFIELDS (this_type) = num_fields;
16417 TYPE_FIELDS (this_type) = (struct field *)
16418 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16419 memcpy (TYPE_FIELDS (this_type), fields,
16420 sizeof (struct field) * num_fields);
16425 /* If we are reading an enum from a .debug_types unit, and the enum
16426 is a declaration, and the enum is not the signatured type in the
16427 unit, then we do not want to add a symbol for it. Adding a
16428 symbol would in some cases obscure the true definition of the
16429 enum, giving users an incomplete type when the definition is
16430 actually available. Note that we do not want to do this for all
16431 enums which are just declarations, because C++0x allows forward
16432 enum declarations. */
16433 if (cu->per_cu->is_debug_types
16434 && die_is_declaration (die, cu))
16436 struct signatured_type *sig_type;
16438 sig_type = (struct signatured_type *) cu->per_cu;
16439 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16440 if (sig_type->type_offset_in_section != die->sect_off)
16444 new_symbol (die, this_type, cu);
16447 /* Extract all information from a DW_TAG_array_type DIE and put it in
16448 the DIE's type field. For now, this only handles one dimensional
16451 static struct type *
16452 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16454 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
16455 struct die_info *child_die;
16457 struct type *element_type, *range_type, *index_type;
16458 struct attribute *attr;
16460 struct dynamic_prop *byte_stride_prop = NULL;
16461 unsigned int bit_stride = 0;
16463 element_type = die_type (die, cu);
16465 /* The die_type call above may have already set the type for this DIE. */
16466 type = get_die_type (die, cu);
16470 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16476 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16477 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16480 complaint (&symfile_complaints,
16481 _("unable to read array DW_AT_byte_stride "
16482 " - DIE at 0x%x [in module %s]"),
16483 to_underlying (die->sect_off),
16484 objfile_name (cu->dwarf2_per_objfile->objfile));
16485 /* Ignore this attribute. We will likely not be able to print
16486 arrays of this type correctly, but there is little we can do
16487 to help if we cannot read the attribute's value. */
16488 byte_stride_prop = NULL;
16492 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16494 bit_stride = DW_UNSND (attr);
16496 /* Irix 6.2 native cc creates array types without children for
16497 arrays with unspecified length. */
16498 if (die->child == NULL)
16500 index_type = objfile_type (objfile)->builtin_int;
16501 range_type = create_static_range_type (NULL, index_type, 0, -1);
16502 type = create_array_type_with_stride (NULL, element_type, range_type,
16503 byte_stride_prop, bit_stride);
16504 return set_die_type (die, type, cu);
16507 std::vector<struct type *> range_types;
16508 child_die = die->child;
16509 while (child_die && child_die->tag)
16511 if (child_die->tag == DW_TAG_subrange_type)
16513 struct type *child_type = read_type_die (child_die, cu);
16515 if (child_type != NULL)
16517 /* The range type was succesfully read. Save it for the
16518 array type creation. */
16519 range_types.push_back (child_type);
16522 child_die = sibling_die (child_die);
16525 /* Dwarf2 dimensions are output from left to right, create the
16526 necessary array types in backwards order. */
16528 type = element_type;
16530 if (read_array_order (die, cu) == DW_ORD_col_major)
16534 while (i < range_types.size ())
16535 type = create_array_type_with_stride (NULL, type, range_types[i++],
16536 byte_stride_prop, bit_stride);
16540 size_t ndim = range_types.size ();
16542 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16543 byte_stride_prop, bit_stride);
16546 /* Understand Dwarf2 support for vector types (like they occur on
16547 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16548 array type. This is not part of the Dwarf2/3 standard yet, but a
16549 custom vendor extension. The main difference between a regular
16550 array and the vector variant is that vectors are passed by value
16552 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16554 make_vector_type (type);
16556 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16557 implementation may choose to implement triple vectors using this
16559 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16562 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16563 TYPE_LENGTH (type) = DW_UNSND (attr);
16565 complaint (&symfile_complaints,
16566 _("DW_AT_byte_size for array type smaller "
16567 "than the total size of elements"));
16570 name = dwarf2_name (die, cu);
16572 TYPE_NAME (type) = name;
16574 /* Install the type in the die. */
16575 set_die_type (die, type, cu);
16577 /* set_die_type should be already done. */
16578 set_descriptive_type (type, die, cu);
16583 static enum dwarf_array_dim_ordering
16584 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16586 struct attribute *attr;
16588 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16591 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16593 /* GNU F77 is a special case, as at 08/2004 array type info is the
16594 opposite order to the dwarf2 specification, but data is still
16595 laid out as per normal fortran.
16597 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16598 version checking. */
16600 if (cu->language == language_fortran
16601 && cu->producer && strstr (cu->producer, "GNU F77"))
16603 return DW_ORD_row_major;
16606 switch (cu->language_defn->la_array_ordering)
16608 case array_column_major:
16609 return DW_ORD_col_major;
16610 case array_row_major:
16612 return DW_ORD_row_major;
16616 /* Extract all information from a DW_TAG_set_type DIE and put it in
16617 the DIE's type field. */
16619 static struct type *
16620 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16622 struct type *domain_type, *set_type;
16623 struct attribute *attr;
16625 domain_type = die_type (die, cu);
16627 /* The die_type call above may have already set the type for this DIE. */
16628 set_type = get_die_type (die, cu);
16632 set_type = create_set_type (NULL, domain_type);
16634 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16636 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16638 return set_die_type (die, set_type, cu);
16641 /* A helper for read_common_block that creates a locexpr baton.
16642 SYM is the symbol which we are marking as computed.
16643 COMMON_DIE is the DIE for the common block.
16644 COMMON_LOC is the location expression attribute for the common
16646 MEMBER_LOC is the location expression attribute for the particular
16647 member of the common block that we are processing.
16648 CU is the CU from which the above come. */
16651 mark_common_block_symbol_computed (struct symbol *sym,
16652 struct die_info *common_die,
16653 struct attribute *common_loc,
16654 struct attribute *member_loc,
16655 struct dwarf2_cu *cu)
16657 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
16658 struct objfile *objfile = dwarf2_per_objfile->objfile;
16659 struct dwarf2_locexpr_baton *baton;
16661 unsigned int cu_off;
16662 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16663 LONGEST offset = 0;
16665 gdb_assert (common_loc && member_loc);
16666 gdb_assert (attr_form_is_block (common_loc));
16667 gdb_assert (attr_form_is_block (member_loc)
16668 || attr_form_is_constant (member_loc));
16670 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16671 baton->per_cu = cu->per_cu;
16672 gdb_assert (baton->per_cu);
16674 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16676 if (attr_form_is_constant (member_loc))
16678 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16679 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16682 baton->size += DW_BLOCK (member_loc)->size;
16684 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16687 *ptr++ = DW_OP_call4;
16688 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16689 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16692 if (attr_form_is_constant (member_loc))
16694 *ptr++ = DW_OP_addr;
16695 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16696 ptr += cu->header.addr_size;
16700 /* We have to copy the data here, because DW_OP_call4 will only
16701 use a DW_AT_location attribute. */
16702 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16703 ptr += DW_BLOCK (member_loc)->size;
16706 *ptr++ = DW_OP_plus;
16707 gdb_assert (ptr - baton->data == baton->size);
16709 SYMBOL_LOCATION_BATON (sym) = baton;
16710 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16713 /* Create appropriate locally-scoped variables for all the
16714 DW_TAG_common_block entries. Also create a struct common_block
16715 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16716 is used to sepate the common blocks name namespace from regular
16720 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16722 struct attribute *attr;
16724 attr = dwarf2_attr (die, DW_AT_location, cu);
16727 /* Support the .debug_loc offsets. */
16728 if (attr_form_is_block (attr))
16732 else if (attr_form_is_section_offset (attr))
16734 dwarf2_complex_location_expr_complaint ();
16739 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16740 "common block member");
16745 if (die->child != NULL)
16747 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
16748 struct die_info *child_die;
16749 size_t n_entries = 0, size;
16750 struct common_block *common_block;
16751 struct symbol *sym;
16753 for (child_die = die->child;
16754 child_die && child_die->tag;
16755 child_die = sibling_die (child_die))
16758 size = (sizeof (struct common_block)
16759 + (n_entries - 1) * sizeof (struct symbol *));
16761 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16763 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16764 common_block->n_entries = 0;
16766 for (child_die = die->child;
16767 child_die && child_die->tag;
16768 child_die = sibling_die (child_die))
16770 /* Create the symbol in the DW_TAG_common_block block in the current
16772 sym = new_symbol (child_die, NULL, cu);
16775 struct attribute *member_loc;
16777 common_block->contents[common_block->n_entries++] = sym;
16779 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16783 /* GDB has handled this for a long time, but it is
16784 not specified by DWARF. It seems to have been
16785 emitted by gfortran at least as recently as:
16786 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16787 complaint (&symfile_complaints,
16788 _("Variable in common block has "
16789 "DW_AT_data_member_location "
16790 "- DIE at 0x%x [in module %s]"),
16791 to_underlying (child_die->sect_off),
16792 objfile_name (cu->dwarf2_per_objfile->objfile));
16794 if (attr_form_is_section_offset (member_loc))
16795 dwarf2_complex_location_expr_complaint ();
16796 else if (attr_form_is_constant (member_loc)
16797 || attr_form_is_block (member_loc))
16800 mark_common_block_symbol_computed (sym, die, attr,
16804 dwarf2_complex_location_expr_complaint ();
16809 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16810 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16814 /* Create a type for a C++ namespace. */
16816 static struct type *
16817 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16819 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
16820 const char *previous_prefix, *name;
16824 /* For extensions, reuse the type of the original namespace. */
16825 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16827 struct die_info *ext_die;
16828 struct dwarf2_cu *ext_cu = cu;
16830 ext_die = dwarf2_extension (die, &ext_cu);
16831 type = read_type_die (ext_die, ext_cu);
16833 /* EXT_CU may not be the same as CU.
16834 Ensure TYPE is recorded with CU in die_type_hash. */
16835 return set_die_type (die, type, cu);
16838 name = namespace_name (die, &is_anonymous, cu);
16840 /* Now build the name of the current namespace. */
16842 previous_prefix = determine_prefix (die, cu);
16843 if (previous_prefix[0] != '\0')
16844 name = typename_concat (&objfile->objfile_obstack,
16845 previous_prefix, name, 0, cu);
16847 /* Create the type. */
16848 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16849 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16851 return set_die_type (die, type, cu);
16854 /* Read a namespace scope. */
16857 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16859 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
16862 /* Add a symbol associated to this if we haven't seen the namespace
16863 before. Also, add a using directive if it's an anonymous
16866 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16870 type = read_type_die (die, cu);
16871 new_symbol (die, type, cu);
16873 namespace_name (die, &is_anonymous, cu);
16876 const char *previous_prefix = determine_prefix (die, cu);
16878 std::vector<const char *> excludes;
16879 add_using_directive (using_directives (cu->language),
16880 previous_prefix, TYPE_NAME (type), NULL,
16881 NULL, excludes, 0, &objfile->objfile_obstack);
16885 if (die->child != NULL)
16887 struct die_info *child_die = die->child;
16889 while (child_die && child_die->tag)
16891 process_die (child_die, cu);
16892 child_die = sibling_die (child_die);
16897 /* Read a Fortran module as type. This DIE can be only a declaration used for
16898 imported module. Still we need that type as local Fortran "use ... only"
16899 declaration imports depend on the created type in determine_prefix. */
16901 static struct type *
16902 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16904 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
16905 const char *module_name;
16908 module_name = dwarf2_name (die, cu);
16910 complaint (&symfile_complaints,
16911 _("DW_TAG_module has no name, offset 0x%x"),
16912 to_underlying (die->sect_off));
16913 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16915 /* determine_prefix uses TYPE_TAG_NAME. */
16916 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16918 return set_die_type (die, type, cu);
16921 /* Read a Fortran module. */
16924 read_module (struct die_info *die, struct dwarf2_cu *cu)
16926 struct die_info *child_die = die->child;
16929 type = read_type_die (die, cu);
16930 new_symbol (die, type, cu);
16932 while (child_die && child_die->tag)
16934 process_die (child_die, cu);
16935 child_die = sibling_die (child_die);
16939 /* Return the name of the namespace represented by DIE. Set
16940 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16943 static const char *
16944 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16946 struct die_info *current_die;
16947 const char *name = NULL;
16949 /* Loop through the extensions until we find a name. */
16951 for (current_die = die;
16952 current_die != NULL;
16953 current_die = dwarf2_extension (die, &cu))
16955 /* We don't use dwarf2_name here so that we can detect the absence
16956 of a name -> anonymous namespace. */
16957 name = dwarf2_string_attr (die, DW_AT_name, cu);
16963 /* Is it an anonymous namespace? */
16965 *is_anonymous = (name == NULL);
16967 name = CP_ANONYMOUS_NAMESPACE_STR;
16972 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16973 the user defined type vector. */
16975 static struct type *
16976 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16978 struct gdbarch *gdbarch = get_objfile_arch (cu->dwarf2_per_objfile->objfile);
16979 struct comp_unit_head *cu_header = &cu->header;
16981 struct attribute *attr_byte_size;
16982 struct attribute *attr_address_class;
16983 int byte_size, addr_class;
16984 struct type *target_type;
16986 target_type = die_type (die, cu);
16988 /* The die_type call above may have already set the type for this DIE. */
16989 type = get_die_type (die, cu);
16993 type = lookup_pointer_type (target_type);
16995 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16996 if (attr_byte_size)
16997 byte_size = DW_UNSND (attr_byte_size);
16999 byte_size = cu_header->addr_size;
17001 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17002 if (attr_address_class)
17003 addr_class = DW_UNSND (attr_address_class);
17005 addr_class = DW_ADDR_none;
17007 /* If the pointer size or address class is different than the
17008 default, create a type variant marked as such and set the
17009 length accordingly. */
17010 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17012 if (gdbarch_address_class_type_flags_p (gdbarch))
17016 type_flags = gdbarch_address_class_type_flags
17017 (gdbarch, byte_size, addr_class);
17018 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17020 type = make_type_with_address_space (type, type_flags);
17022 else if (TYPE_LENGTH (type) != byte_size)
17024 complaint (&symfile_complaints,
17025 _("invalid pointer size %d"), byte_size);
17029 /* Should we also complain about unhandled address classes? */
17033 TYPE_LENGTH (type) = byte_size;
17034 return set_die_type (die, type, cu);
17037 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17038 the user defined type vector. */
17040 static struct type *
17041 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17044 struct type *to_type;
17045 struct type *domain;
17047 to_type = die_type (die, cu);
17048 domain = die_containing_type (die, cu);
17050 /* The calls above may have already set the type for this DIE. */
17051 type = get_die_type (die, cu);
17055 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17056 type = lookup_methodptr_type (to_type);
17057 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17059 struct type *new_type = alloc_type (cu->dwarf2_per_objfile->objfile);
17061 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17062 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17063 TYPE_VARARGS (to_type));
17064 type = lookup_methodptr_type (new_type);
17067 type = lookup_memberptr_type (to_type, domain);
17069 return set_die_type (die, type, cu);
17072 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17073 the user defined type vector. */
17075 static struct type *
17076 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17077 enum type_code refcode)
17079 struct comp_unit_head *cu_header = &cu->header;
17080 struct type *type, *target_type;
17081 struct attribute *attr;
17083 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17085 target_type = die_type (die, cu);
17087 /* The die_type call above may have already set the type for this DIE. */
17088 type = get_die_type (die, cu);
17092 type = lookup_reference_type (target_type, refcode);
17093 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17096 TYPE_LENGTH (type) = DW_UNSND (attr);
17100 TYPE_LENGTH (type) = cu_header->addr_size;
17102 return set_die_type (die, type, cu);
17105 /* Add the given cv-qualifiers to the element type of the array. GCC
17106 outputs DWARF type qualifiers that apply to an array, not the
17107 element type. But GDB relies on the array element type to carry
17108 the cv-qualifiers. This mimics section 6.7.3 of the C99
17111 static struct type *
17112 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17113 struct type *base_type, int cnst, int voltl)
17115 struct type *el_type, *inner_array;
17117 base_type = copy_type (base_type);
17118 inner_array = base_type;
17120 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17122 TYPE_TARGET_TYPE (inner_array) =
17123 copy_type (TYPE_TARGET_TYPE (inner_array));
17124 inner_array = TYPE_TARGET_TYPE (inner_array);
17127 el_type = TYPE_TARGET_TYPE (inner_array);
17128 cnst |= TYPE_CONST (el_type);
17129 voltl |= TYPE_VOLATILE (el_type);
17130 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17132 return set_die_type (die, base_type, cu);
17135 static struct type *
17136 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17138 struct type *base_type, *cv_type;
17140 base_type = die_type (die, cu);
17142 /* The die_type call above may have already set the type for this DIE. */
17143 cv_type = get_die_type (die, cu);
17147 /* In case the const qualifier is applied to an array type, the element type
17148 is so qualified, not the array type (section 6.7.3 of C99). */
17149 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17150 return add_array_cv_type (die, cu, base_type, 1, 0);
17152 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17153 return set_die_type (die, cv_type, cu);
17156 static struct type *
17157 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17159 struct type *base_type, *cv_type;
17161 base_type = die_type (die, cu);
17163 /* The die_type call above may have already set the type for this DIE. */
17164 cv_type = get_die_type (die, cu);
17168 /* In case the volatile qualifier is applied to an array type, the
17169 element type is so qualified, not the array type (section 6.7.3
17171 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17172 return add_array_cv_type (die, cu, base_type, 0, 1);
17174 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17175 return set_die_type (die, cv_type, cu);
17178 /* Handle DW_TAG_restrict_type. */
17180 static struct type *
17181 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17183 struct type *base_type, *cv_type;
17185 base_type = die_type (die, cu);
17187 /* The die_type call above may have already set the type for this DIE. */
17188 cv_type = get_die_type (die, cu);
17192 cv_type = make_restrict_type (base_type);
17193 return set_die_type (die, cv_type, cu);
17196 /* Handle DW_TAG_atomic_type. */
17198 static struct type *
17199 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17201 struct type *base_type, *cv_type;
17203 base_type = die_type (die, cu);
17205 /* The die_type call above may have already set the type for this DIE. */
17206 cv_type = get_die_type (die, cu);
17210 cv_type = make_atomic_type (base_type);
17211 return set_die_type (die, cv_type, cu);
17214 /* Extract all information from a DW_TAG_string_type DIE and add to
17215 the user defined type vector. It isn't really a user defined type,
17216 but it behaves like one, with other DIE's using an AT_user_def_type
17217 attribute to reference it. */
17219 static struct type *
17220 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17222 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
17223 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17224 struct type *type, *range_type, *index_type, *char_type;
17225 struct attribute *attr;
17226 unsigned int length;
17228 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17231 length = DW_UNSND (attr);
17235 /* Check for the DW_AT_byte_size attribute. */
17236 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17239 length = DW_UNSND (attr);
17247 index_type = objfile_type (objfile)->builtin_int;
17248 range_type = create_static_range_type (NULL, index_type, 1, length);
17249 char_type = language_string_char_type (cu->language_defn, gdbarch);
17250 type = create_string_type (NULL, char_type, range_type);
17252 return set_die_type (die, type, cu);
17255 /* Assuming that DIE corresponds to a function, returns nonzero
17256 if the function is prototyped. */
17259 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17261 struct attribute *attr;
17263 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17264 if (attr && (DW_UNSND (attr) != 0))
17267 /* The DWARF standard implies that the DW_AT_prototyped attribute
17268 is only meaninful for C, but the concept also extends to other
17269 languages that allow unprototyped functions (Eg: Objective C).
17270 For all other languages, assume that functions are always
17272 if (cu->language != language_c
17273 && cu->language != language_objc
17274 && cu->language != language_opencl)
17277 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17278 prototyped and unprototyped functions; default to prototyped,
17279 since that is more common in modern code (and RealView warns
17280 about unprototyped functions). */
17281 if (producer_is_realview (cu->producer))
17287 /* Handle DIES due to C code like:
17291 int (*funcp)(int a, long l);
17295 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17297 static struct type *
17298 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17300 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
17301 struct type *type; /* Type that this function returns. */
17302 struct type *ftype; /* Function that returns above type. */
17303 struct attribute *attr;
17305 type = die_type (die, cu);
17307 /* The die_type call above may have already set the type for this DIE. */
17308 ftype = get_die_type (die, cu);
17312 ftype = lookup_function_type (type);
17314 if (prototyped_function_p (die, cu))
17315 TYPE_PROTOTYPED (ftype) = 1;
17317 /* Store the calling convention in the type if it's available in
17318 the subroutine die. Otherwise set the calling convention to
17319 the default value DW_CC_normal. */
17320 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17322 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17323 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17324 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17326 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17328 /* Record whether the function returns normally to its caller or not
17329 if the DWARF producer set that information. */
17330 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17331 if (attr && (DW_UNSND (attr) != 0))
17332 TYPE_NO_RETURN (ftype) = 1;
17334 /* We need to add the subroutine type to the die immediately so
17335 we don't infinitely recurse when dealing with parameters
17336 declared as the same subroutine type. */
17337 set_die_type (die, ftype, cu);
17339 if (die->child != NULL)
17341 struct type *void_type = objfile_type (objfile)->builtin_void;
17342 struct die_info *child_die;
17343 int nparams, iparams;
17345 /* Count the number of parameters.
17346 FIXME: GDB currently ignores vararg functions, but knows about
17347 vararg member functions. */
17349 child_die = die->child;
17350 while (child_die && child_die->tag)
17352 if (child_die->tag == DW_TAG_formal_parameter)
17354 else if (child_die->tag == DW_TAG_unspecified_parameters)
17355 TYPE_VARARGS (ftype) = 1;
17356 child_die = sibling_die (child_die);
17359 /* Allocate storage for parameters and fill them in. */
17360 TYPE_NFIELDS (ftype) = nparams;
17361 TYPE_FIELDS (ftype) = (struct field *)
17362 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17364 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17365 even if we error out during the parameters reading below. */
17366 for (iparams = 0; iparams < nparams; iparams++)
17367 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17370 child_die = die->child;
17371 while (child_die && child_die->tag)
17373 if (child_die->tag == DW_TAG_formal_parameter)
17375 struct type *arg_type;
17377 /* DWARF version 2 has no clean way to discern C++
17378 static and non-static member functions. G++ helps
17379 GDB by marking the first parameter for non-static
17380 member functions (which is the this pointer) as
17381 artificial. We pass this information to
17382 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17384 DWARF version 3 added DW_AT_object_pointer, which GCC
17385 4.5 does not yet generate. */
17386 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17388 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17390 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17391 arg_type = die_type (child_die, cu);
17393 /* RealView does not mark THIS as const, which the testsuite
17394 expects. GCC marks THIS as const in method definitions,
17395 but not in the class specifications (GCC PR 43053). */
17396 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17397 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17400 struct dwarf2_cu *arg_cu = cu;
17401 const char *name = dwarf2_name (child_die, cu);
17403 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17406 /* If the compiler emits this, use it. */
17407 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17410 else if (name && strcmp (name, "this") == 0)
17411 /* Function definitions will have the argument names. */
17413 else if (name == NULL && iparams == 0)
17414 /* Declarations may not have the names, so like
17415 elsewhere in GDB, assume an artificial first
17416 argument is "this". */
17420 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17424 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17427 child_die = sibling_die (child_die);
17434 static struct type *
17435 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17437 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
17438 const char *name = NULL;
17439 struct type *this_type, *target_type;
17441 name = dwarf2_full_name (NULL, die, cu);
17442 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17443 TYPE_TARGET_STUB (this_type) = 1;
17444 set_die_type (die, this_type, cu);
17445 target_type = die_type (die, cu);
17446 if (target_type != this_type)
17447 TYPE_TARGET_TYPE (this_type) = target_type;
17450 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17451 spec and cause infinite loops in GDB. */
17452 complaint (&symfile_complaints,
17453 _("Self-referential DW_TAG_typedef "
17454 "- DIE at 0x%x [in module %s]"),
17455 to_underlying (die->sect_off), objfile_name (objfile));
17456 TYPE_TARGET_TYPE (this_type) = NULL;
17461 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17462 (which may be different from NAME) to the architecture back-end to allow
17463 it to guess the correct format if necessary. */
17465 static struct type *
17466 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17467 const char *name_hint)
17469 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17470 const struct floatformat **format;
17473 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17475 type = init_float_type (objfile, bits, name, format);
17477 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17482 /* Find a representation of a given base type and install
17483 it in the TYPE field of the die. */
17485 static struct type *
17486 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17488 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
17490 struct attribute *attr;
17491 int encoding = 0, bits = 0;
17494 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17497 encoding = DW_UNSND (attr);
17499 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17502 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17504 name = dwarf2_name (die, cu);
17507 complaint (&symfile_complaints,
17508 _("DW_AT_name missing from DW_TAG_base_type"));
17513 case DW_ATE_address:
17514 /* Turn DW_ATE_address into a void * pointer. */
17515 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17516 type = init_pointer_type (objfile, bits, name, type);
17518 case DW_ATE_boolean:
17519 type = init_boolean_type (objfile, bits, 1, name);
17521 case DW_ATE_complex_float:
17522 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17523 type = init_complex_type (objfile, name, type);
17525 case DW_ATE_decimal_float:
17526 type = init_decfloat_type (objfile, bits, name);
17529 type = dwarf2_init_float_type (objfile, bits, name, name);
17531 case DW_ATE_signed:
17532 type = init_integer_type (objfile, bits, 0, name);
17534 case DW_ATE_unsigned:
17535 if (cu->language == language_fortran
17537 && startswith (name, "character("))
17538 type = init_character_type (objfile, bits, 1, name);
17540 type = init_integer_type (objfile, bits, 1, name);
17542 case DW_ATE_signed_char:
17543 if (cu->language == language_ada || cu->language == language_m2
17544 || cu->language == language_pascal
17545 || cu->language == language_fortran)
17546 type = init_character_type (objfile, bits, 0, name);
17548 type = init_integer_type (objfile, bits, 0, name);
17550 case DW_ATE_unsigned_char:
17551 if (cu->language == language_ada || cu->language == language_m2
17552 || cu->language == language_pascal
17553 || cu->language == language_fortran
17554 || cu->language == language_rust)
17555 type = init_character_type (objfile, bits, 1, name);
17557 type = init_integer_type (objfile, bits, 1, name);
17561 gdbarch *arch = get_objfile_arch (objfile);
17564 type = builtin_type (arch)->builtin_char16;
17565 else if (bits == 32)
17566 type = builtin_type (arch)->builtin_char32;
17569 complaint (&symfile_complaints,
17570 _("unsupported DW_ATE_UTF bit size: '%d'"),
17572 type = init_integer_type (objfile, bits, 1, name);
17574 return set_die_type (die, type, cu);
17579 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17580 dwarf_type_encoding_name (encoding));
17581 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17585 if (name && strcmp (name, "char") == 0)
17586 TYPE_NOSIGN (type) = 1;
17588 return set_die_type (die, type, cu);
17591 /* Parse dwarf attribute if it's a block, reference or constant and put the
17592 resulting value of the attribute into struct bound_prop.
17593 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17596 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17597 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17599 struct dwarf2_property_baton *baton;
17600 struct obstack *obstack = &cu->dwarf2_per_objfile->objfile->objfile_obstack;
17602 if (attr == NULL || prop == NULL)
17605 if (attr_form_is_block (attr))
17607 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17608 baton->referenced_type = NULL;
17609 baton->locexpr.per_cu = cu->per_cu;
17610 baton->locexpr.size = DW_BLOCK (attr)->size;
17611 baton->locexpr.data = DW_BLOCK (attr)->data;
17612 prop->data.baton = baton;
17613 prop->kind = PROP_LOCEXPR;
17614 gdb_assert (prop->data.baton != NULL);
17616 else if (attr_form_is_ref (attr))
17618 struct dwarf2_cu *target_cu = cu;
17619 struct die_info *target_die;
17620 struct attribute *target_attr;
17622 target_die = follow_die_ref (die, attr, &target_cu);
17623 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17624 if (target_attr == NULL)
17625 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17627 if (target_attr == NULL)
17630 switch (target_attr->name)
17632 case DW_AT_location:
17633 if (attr_form_is_section_offset (target_attr))
17635 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17636 baton->referenced_type = die_type (target_die, target_cu);
17637 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17638 prop->data.baton = baton;
17639 prop->kind = PROP_LOCLIST;
17640 gdb_assert (prop->data.baton != NULL);
17642 else if (attr_form_is_block (target_attr))
17644 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17645 baton->referenced_type = die_type (target_die, target_cu);
17646 baton->locexpr.per_cu = cu->per_cu;
17647 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17648 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17649 prop->data.baton = baton;
17650 prop->kind = PROP_LOCEXPR;
17651 gdb_assert (prop->data.baton != NULL);
17655 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17656 "dynamic property");
17660 case DW_AT_data_member_location:
17664 if (!handle_data_member_location (target_die, target_cu,
17668 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17669 baton->referenced_type = read_type_die (target_die->parent,
17671 baton->offset_info.offset = offset;
17672 baton->offset_info.type = die_type (target_die, target_cu);
17673 prop->data.baton = baton;
17674 prop->kind = PROP_ADDR_OFFSET;
17679 else if (attr_form_is_constant (attr))
17681 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17682 prop->kind = PROP_CONST;
17686 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17687 dwarf2_name (die, cu));
17694 /* Read the given DW_AT_subrange DIE. */
17696 static struct type *
17697 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17699 struct type *base_type, *orig_base_type;
17700 struct type *range_type;
17701 struct attribute *attr;
17702 struct dynamic_prop low, high;
17703 int low_default_is_valid;
17704 int high_bound_is_count = 0;
17706 LONGEST negative_mask;
17708 orig_base_type = die_type (die, cu);
17709 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17710 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17711 creating the range type, but we use the result of check_typedef
17712 when examining properties of the type. */
17713 base_type = check_typedef (orig_base_type);
17715 /* The die_type call above may have already set the type for this DIE. */
17716 range_type = get_die_type (die, cu);
17720 low.kind = PROP_CONST;
17721 high.kind = PROP_CONST;
17722 high.data.const_val = 0;
17724 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17725 omitting DW_AT_lower_bound. */
17726 switch (cu->language)
17729 case language_cplus:
17730 low.data.const_val = 0;
17731 low_default_is_valid = 1;
17733 case language_fortran:
17734 low.data.const_val = 1;
17735 low_default_is_valid = 1;
17738 case language_objc:
17739 case language_rust:
17740 low.data.const_val = 0;
17741 low_default_is_valid = (cu->header.version >= 4);
17745 case language_pascal:
17746 low.data.const_val = 1;
17747 low_default_is_valid = (cu->header.version >= 4);
17750 low.data.const_val = 0;
17751 low_default_is_valid = 0;
17755 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17757 attr_to_dynamic_prop (attr, die, cu, &low);
17758 else if (!low_default_is_valid)
17759 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17760 "- DIE at 0x%x [in module %s]"),
17761 to_underlying (die->sect_off),
17762 objfile_name (cu->dwarf2_per_objfile->objfile));
17764 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17765 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17767 attr = dwarf2_attr (die, DW_AT_count, cu);
17768 if (attr_to_dynamic_prop (attr, die, cu, &high))
17770 /* If bounds are constant do the final calculation here. */
17771 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17772 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17774 high_bound_is_count = 1;
17778 /* Dwarf-2 specifications explicitly allows to create subrange types
17779 without specifying a base type.
17780 In that case, the base type must be set to the type of
17781 the lower bound, upper bound or count, in that order, if any of these
17782 three attributes references an object that has a type.
17783 If no base type is found, the Dwarf-2 specifications say that
17784 a signed integer type of size equal to the size of an address should
17786 For the following C code: `extern char gdb_int [];'
17787 GCC produces an empty range DIE.
17788 FIXME: muller/2010-05-28: Possible references to object for low bound,
17789 high bound or count are not yet handled by this code. */
17790 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17792 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
17793 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17794 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17795 struct type *int_type = objfile_type (objfile)->builtin_int;
17797 /* Test "int", "long int", and "long long int" objfile types,
17798 and select the first one having a size above or equal to the
17799 architecture address size. */
17800 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17801 base_type = int_type;
17804 int_type = objfile_type (objfile)->builtin_long;
17805 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17806 base_type = int_type;
17809 int_type = objfile_type (objfile)->builtin_long_long;
17810 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17811 base_type = int_type;
17816 /* Normally, the DWARF producers are expected to use a signed
17817 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17818 But this is unfortunately not always the case, as witnessed
17819 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17820 is used instead. To work around that ambiguity, we treat
17821 the bounds as signed, and thus sign-extend their values, when
17822 the base type is signed. */
17824 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17825 if (low.kind == PROP_CONST
17826 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17827 low.data.const_val |= negative_mask;
17828 if (high.kind == PROP_CONST
17829 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17830 high.data.const_val |= negative_mask;
17832 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17834 if (high_bound_is_count)
17835 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17837 /* Ada expects an empty array on no boundary attributes. */
17838 if (attr == NULL && cu->language != language_ada)
17839 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17841 name = dwarf2_name (die, cu);
17843 TYPE_NAME (range_type) = name;
17845 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17847 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17849 set_die_type (die, range_type, cu);
17851 /* set_die_type should be already done. */
17852 set_descriptive_type (range_type, die, cu);
17857 static struct type *
17858 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17862 type = init_type (cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17863 TYPE_NAME (type) = dwarf2_name (die, cu);
17865 /* In Ada, an unspecified type is typically used when the description
17866 of the type is defered to a different unit. When encountering
17867 such a type, we treat it as a stub, and try to resolve it later on,
17869 if (cu->language == language_ada)
17870 TYPE_STUB (type) = 1;
17872 return set_die_type (die, type, cu);
17875 /* Read a single die and all its descendents. Set the die's sibling
17876 field to NULL; set other fields in the die correctly, and set all
17877 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17878 location of the info_ptr after reading all of those dies. PARENT
17879 is the parent of the die in question. */
17881 static struct die_info *
17882 read_die_and_children (const struct die_reader_specs *reader,
17883 const gdb_byte *info_ptr,
17884 const gdb_byte **new_info_ptr,
17885 struct die_info *parent)
17887 struct die_info *die;
17888 const gdb_byte *cur_ptr;
17891 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17894 *new_info_ptr = cur_ptr;
17897 store_in_ref_table (die, reader->cu);
17900 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17904 *new_info_ptr = cur_ptr;
17907 die->sibling = NULL;
17908 die->parent = parent;
17912 /* Read a die, all of its descendents, and all of its siblings; set
17913 all of the fields of all of the dies correctly. Arguments are as
17914 in read_die_and_children. */
17916 static struct die_info *
17917 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17918 const gdb_byte *info_ptr,
17919 const gdb_byte **new_info_ptr,
17920 struct die_info *parent)
17922 struct die_info *first_die, *last_sibling;
17923 const gdb_byte *cur_ptr;
17925 cur_ptr = info_ptr;
17926 first_die = last_sibling = NULL;
17930 struct die_info *die
17931 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17935 *new_info_ptr = cur_ptr;
17942 last_sibling->sibling = die;
17944 last_sibling = die;
17948 /* Read a die, all of its descendents, and all of its siblings; set
17949 all of the fields of all of the dies correctly. Arguments are as
17950 in read_die_and_children.
17951 This the main entry point for reading a DIE and all its children. */
17953 static struct die_info *
17954 read_die_and_siblings (const struct die_reader_specs *reader,
17955 const gdb_byte *info_ptr,
17956 const gdb_byte **new_info_ptr,
17957 struct die_info *parent)
17959 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17960 new_info_ptr, parent);
17962 if (dwarf_die_debug)
17964 fprintf_unfiltered (gdb_stdlog,
17965 "Read die from %s@0x%x of %s:\n",
17966 get_section_name (reader->die_section),
17967 (unsigned) (info_ptr - reader->die_section->buffer),
17968 bfd_get_filename (reader->abfd));
17969 dump_die (die, dwarf_die_debug);
17975 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17977 The caller is responsible for filling in the extra attributes
17978 and updating (*DIEP)->num_attrs.
17979 Set DIEP to point to a newly allocated die with its information,
17980 except for its child, sibling, and parent fields.
17981 Set HAS_CHILDREN to tell whether the die has children or not. */
17983 static const gdb_byte *
17984 read_full_die_1 (const struct die_reader_specs *reader,
17985 struct die_info **diep, const gdb_byte *info_ptr,
17986 int *has_children, int num_extra_attrs)
17988 unsigned int abbrev_number, bytes_read, i;
17989 struct abbrev_info *abbrev;
17990 struct die_info *die;
17991 struct dwarf2_cu *cu = reader->cu;
17992 bfd *abfd = reader->abfd;
17994 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17995 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17996 info_ptr += bytes_read;
17997 if (!abbrev_number)
18004 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
18006 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18008 bfd_get_filename (abfd));
18010 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18011 die->sect_off = sect_off;
18012 die->tag = abbrev->tag;
18013 die->abbrev = abbrev_number;
18015 /* Make the result usable.
18016 The caller needs to update num_attrs after adding the extra
18018 die->num_attrs = abbrev->num_attrs;
18020 for (i = 0; i < abbrev->num_attrs; ++i)
18021 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18025 *has_children = abbrev->has_children;
18029 /* Read a die and all its attributes.
18030 Set DIEP to point to a newly allocated die with its information,
18031 except for its child, sibling, and parent fields.
18032 Set HAS_CHILDREN to tell whether the die has children or not. */
18034 static const gdb_byte *
18035 read_full_die (const struct die_reader_specs *reader,
18036 struct die_info **diep, const gdb_byte *info_ptr,
18039 const gdb_byte *result;
18041 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18043 if (dwarf_die_debug)
18045 fprintf_unfiltered (gdb_stdlog,
18046 "Read die from %s@0x%x of %s:\n",
18047 get_section_name (reader->die_section),
18048 (unsigned) (info_ptr - reader->die_section->buffer),
18049 bfd_get_filename (reader->abfd));
18050 dump_die (*diep, dwarf_die_debug);
18056 /* Abbreviation tables.
18058 In DWARF version 2, the description of the debugging information is
18059 stored in a separate .debug_abbrev section. Before we read any
18060 dies from a section we read in all abbreviations and install them
18061 in a hash table. */
18063 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18065 static struct abbrev_info *
18066 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
18068 struct abbrev_info *abbrev;
18070 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
18071 memset (abbrev, 0, sizeof (struct abbrev_info));
18076 /* Add an abbreviation to the table. */
18079 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
18080 unsigned int abbrev_number,
18081 struct abbrev_info *abbrev)
18083 unsigned int hash_number;
18085 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18086 abbrev->next = abbrev_table->abbrevs[hash_number];
18087 abbrev_table->abbrevs[hash_number] = abbrev;
18090 /* Look up an abbrev in the table.
18091 Returns NULL if the abbrev is not found. */
18093 static struct abbrev_info *
18094 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
18095 unsigned int abbrev_number)
18097 unsigned int hash_number;
18098 struct abbrev_info *abbrev;
18100 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18101 abbrev = abbrev_table->abbrevs[hash_number];
18105 if (abbrev->number == abbrev_number)
18107 abbrev = abbrev->next;
18112 /* Read in an abbrev table. */
18114 static struct abbrev_table *
18115 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18116 struct dwarf2_section_info *section,
18117 sect_offset sect_off)
18119 struct objfile *objfile = dwarf2_per_objfile->objfile;
18120 bfd *abfd = get_section_bfd_owner (section);
18121 struct abbrev_table *abbrev_table;
18122 const gdb_byte *abbrev_ptr;
18123 struct abbrev_info *cur_abbrev;
18124 unsigned int abbrev_number, bytes_read, abbrev_name;
18125 unsigned int abbrev_form;
18126 struct attr_abbrev *cur_attrs;
18127 unsigned int allocated_attrs;
18129 abbrev_table = XNEW (struct abbrev_table);
18130 abbrev_table->sect_off = sect_off;
18131 obstack_init (&abbrev_table->abbrev_obstack);
18132 abbrev_table->abbrevs =
18133 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
18135 memset (abbrev_table->abbrevs, 0,
18136 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
18138 dwarf2_read_section (objfile, section);
18139 abbrev_ptr = section->buffer + to_underlying (sect_off);
18140 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18141 abbrev_ptr += bytes_read;
18143 allocated_attrs = ATTR_ALLOC_CHUNK;
18144 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18146 /* Loop until we reach an abbrev number of 0. */
18147 while (abbrev_number)
18149 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
18151 /* read in abbrev header */
18152 cur_abbrev->number = abbrev_number;
18154 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18155 abbrev_ptr += bytes_read;
18156 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18159 /* now read in declarations */
18162 LONGEST implicit_const;
18164 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18165 abbrev_ptr += bytes_read;
18166 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18167 abbrev_ptr += bytes_read;
18168 if (abbrev_form == DW_FORM_implicit_const)
18170 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18172 abbrev_ptr += bytes_read;
18176 /* Initialize it due to a false compiler warning. */
18177 implicit_const = -1;
18180 if (abbrev_name == 0)
18183 if (cur_abbrev->num_attrs == allocated_attrs)
18185 allocated_attrs += ATTR_ALLOC_CHUNK;
18187 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18190 cur_attrs[cur_abbrev->num_attrs].name
18191 = (enum dwarf_attribute) abbrev_name;
18192 cur_attrs[cur_abbrev->num_attrs].form
18193 = (enum dwarf_form) abbrev_form;
18194 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18195 ++cur_abbrev->num_attrs;
18198 cur_abbrev->attrs =
18199 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18200 cur_abbrev->num_attrs);
18201 memcpy (cur_abbrev->attrs, cur_attrs,
18202 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18204 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
18206 /* Get next abbreviation.
18207 Under Irix6 the abbreviations for a compilation unit are not
18208 always properly terminated with an abbrev number of 0.
18209 Exit loop if we encounter an abbreviation which we have
18210 already read (which means we are about to read the abbreviations
18211 for the next compile unit) or if the end of the abbreviation
18212 table is reached. */
18213 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18215 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18216 abbrev_ptr += bytes_read;
18217 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
18222 return abbrev_table;
18225 /* Free the resources held by ABBREV_TABLE. */
18228 abbrev_table_free (struct abbrev_table *abbrev_table)
18230 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18231 xfree (abbrev_table);
18234 /* Same as abbrev_table_free but as a cleanup.
18235 We pass in a pointer to the pointer to the table so that we can
18236 set the pointer to NULL when we're done. It also simplifies
18237 build_type_psymtabs_1. */
18240 abbrev_table_free_cleanup (void *table_ptr)
18242 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
18244 if (*abbrev_table_ptr != NULL)
18245 abbrev_table_free (*abbrev_table_ptr);
18246 *abbrev_table_ptr = NULL;
18249 /* Read the abbrev table for CU from ABBREV_SECTION. */
18252 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18253 struct dwarf2_section_info *abbrev_section)
18256 abbrev_table_read_table (cu->dwarf2_per_objfile, abbrev_section,
18257 cu->header.abbrev_sect_off);
18260 /* Release the memory used by the abbrev table for a compilation unit. */
18263 dwarf2_free_abbrev_table (void *ptr_to_cu)
18265 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
18267 if (cu->abbrev_table != NULL)
18268 abbrev_table_free (cu->abbrev_table);
18269 /* Set this to NULL so that we SEGV if we try to read it later,
18270 and also because free_comp_unit verifies this is NULL. */
18271 cu->abbrev_table = NULL;
18274 /* Returns nonzero if TAG represents a type that we might generate a partial
18278 is_type_tag_for_partial (int tag)
18283 /* Some types that would be reasonable to generate partial symbols for,
18284 that we don't at present. */
18285 case DW_TAG_array_type:
18286 case DW_TAG_file_type:
18287 case DW_TAG_ptr_to_member_type:
18288 case DW_TAG_set_type:
18289 case DW_TAG_string_type:
18290 case DW_TAG_subroutine_type:
18292 case DW_TAG_base_type:
18293 case DW_TAG_class_type:
18294 case DW_TAG_interface_type:
18295 case DW_TAG_enumeration_type:
18296 case DW_TAG_structure_type:
18297 case DW_TAG_subrange_type:
18298 case DW_TAG_typedef:
18299 case DW_TAG_union_type:
18306 /* Load all DIEs that are interesting for partial symbols into memory. */
18308 static struct partial_die_info *
18309 load_partial_dies (const struct die_reader_specs *reader,
18310 const gdb_byte *info_ptr, int building_psymtab)
18312 struct dwarf2_cu *cu = reader->cu;
18313 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
18314 struct partial_die_info *part_die;
18315 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18316 struct abbrev_info *abbrev;
18317 unsigned int bytes_read;
18318 unsigned int load_all = 0;
18319 int nesting_level = 1;
18324 gdb_assert (cu->per_cu != NULL);
18325 if (cu->per_cu->load_all_dies)
18329 = htab_create_alloc_ex (cu->header.length / 12,
18333 &cu->comp_unit_obstack,
18334 hashtab_obstack_allocate,
18335 dummy_obstack_deallocate);
18337 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18341 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18343 /* A NULL abbrev means the end of a series of children. */
18344 if (abbrev == NULL)
18346 if (--nesting_level == 0)
18348 /* PART_DIE was probably the last thing allocated on the
18349 comp_unit_obstack, so we could call obstack_free
18350 here. We don't do that because the waste is small,
18351 and will be cleaned up when we're done with this
18352 compilation unit. This way, we're also more robust
18353 against other users of the comp_unit_obstack. */
18356 info_ptr += bytes_read;
18357 last_die = parent_die;
18358 parent_die = parent_die->die_parent;
18362 /* Check for template arguments. We never save these; if
18363 they're seen, we just mark the parent, and go on our way. */
18364 if (parent_die != NULL
18365 && cu->language == language_cplus
18366 && (abbrev->tag == DW_TAG_template_type_param
18367 || abbrev->tag == DW_TAG_template_value_param))
18369 parent_die->has_template_arguments = 1;
18373 /* We don't need a partial DIE for the template argument. */
18374 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18379 /* We only recurse into c++ subprograms looking for template arguments.
18380 Skip their other children. */
18382 && cu->language == language_cplus
18383 && parent_die != NULL
18384 && parent_die->tag == DW_TAG_subprogram)
18386 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18390 /* Check whether this DIE is interesting enough to save. Normally
18391 we would not be interested in members here, but there may be
18392 later variables referencing them via DW_AT_specification (for
18393 static members). */
18395 && !is_type_tag_for_partial (abbrev->tag)
18396 && abbrev->tag != DW_TAG_constant
18397 && abbrev->tag != DW_TAG_enumerator
18398 && abbrev->tag != DW_TAG_subprogram
18399 && abbrev->tag != DW_TAG_lexical_block
18400 && abbrev->tag != DW_TAG_variable
18401 && abbrev->tag != DW_TAG_namespace
18402 && abbrev->tag != DW_TAG_module
18403 && abbrev->tag != DW_TAG_member
18404 && abbrev->tag != DW_TAG_imported_unit
18405 && abbrev->tag != DW_TAG_imported_declaration)
18407 /* Otherwise we skip to the next sibling, if any. */
18408 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18412 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18415 /* This two-pass algorithm for processing partial symbols has a
18416 high cost in cache pressure. Thus, handle some simple cases
18417 here which cover the majority of C partial symbols. DIEs
18418 which neither have specification tags in them, nor could have
18419 specification tags elsewhere pointing at them, can simply be
18420 processed and discarded.
18422 This segment is also optional; scan_partial_symbols and
18423 add_partial_symbol will handle these DIEs if we chain
18424 them in normally. When compilers which do not emit large
18425 quantities of duplicate debug information are more common,
18426 this code can probably be removed. */
18428 /* Any complete simple types at the top level (pretty much all
18429 of them, for a language without namespaces), can be processed
18431 if (parent_die == NULL
18432 && part_die->has_specification == 0
18433 && part_die->is_declaration == 0
18434 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18435 || part_die->tag == DW_TAG_base_type
18436 || part_die->tag == DW_TAG_subrange_type))
18438 if (building_psymtab && part_die->name != NULL)
18439 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18440 VAR_DOMAIN, LOC_TYPEDEF,
18441 &objfile->static_psymbols,
18442 0, cu->language, objfile);
18443 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18447 /* The exception for DW_TAG_typedef with has_children above is
18448 a workaround of GCC PR debug/47510. In the case of this complaint
18449 type_name_no_tag_or_error will error on such types later.
18451 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18452 it could not find the child DIEs referenced later, this is checked
18453 above. In correct DWARF DW_TAG_typedef should have no children. */
18455 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18456 complaint (&symfile_complaints,
18457 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18458 "- DIE at 0x%x [in module %s]"),
18459 to_underlying (part_die->sect_off), objfile_name (objfile));
18461 /* If we're at the second level, and we're an enumerator, and
18462 our parent has no specification (meaning possibly lives in a
18463 namespace elsewhere), then we can add the partial symbol now
18464 instead of queueing it. */
18465 if (part_die->tag == DW_TAG_enumerator
18466 && parent_die != NULL
18467 && parent_die->die_parent == NULL
18468 && parent_die->tag == DW_TAG_enumeration_type
18469 && parent_die->has_specification == 0)
18471 if (part_die->name == NULL)
18472 complaint (&symfile_complaints,
18473 _("malformed enumerator DIE ignored"));
18474 else if (building_psymtab)
18475 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18476 VAR_DOMAIN, LOC_CONST,
18477 cu->language == language_cplus
18478 ? &objfile->global_psymbols
18479 : &objfile->static_psymbols,
18480 0, cu->language, objfile);
18482 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18486 /* We'll save this DIE so link it in. */
18487 part_die->die_parent = parent_die;
18488 part_die->die_sibling = NULL;
18489 part_die->die_child = NULL;
18491 if (last_die && last_die == parent_die)
18492 last_die->die_child = part_die;
18494 last_die->die_sibling = part_die;
18496 last_die = part_die;
18498 if (first_die == NULL)
18499 first_die = part_die;
18501 /* Maybe add the DIE to the hash table. Not all DIEs that we
18502 find interesting need to be in the hash table, because we
18503 also have the parent/sibling/child chains; only those that we
18504 might refer to by offset later during partial symbol reading.
18506 For now this means things that might have be the target of a
18507 DW_AT_specification, DW_AT_abstract_origin, or
18508 DW_AT_extension. DW_AT_extension will refer only to
18509 namespaces; DW_AT_abstract_origin refers to functions (and
18510 many things under the function DIE, but we do not recurse
18511 into function DIEs during partial symbol reading) and
18512 possibly variables as well; DW_AT_specification refers to
18513 declarations. Declarations ought to have the DW_AT_declaration
18514 flag. It happens that GCC forgets to put it in sometimes, but
18515 only for functions, not for types.
18517 Adding more things than necessary to the hash table is harmless
18518 except for the performance cost. Adding too few will result in
18519 wasted time in find_partial_die, when we reread the compilation
18520 unit with load_all_dies set. */
18523 || abbrev->tag == DW_TAG_constant
18524 || abbrev->tag == DW_TAG_subprogram
18525 || abbrev->tag == DW_TAG_variable
18526 || abbrev->tag == DW_TAG_namespace
18527 || part_die->is_declaration)
18531 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18532 to_underlying (part_die->sect_off),
18537 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18539 /* For some DIEs we want to follow their children (if any). For C
18540 we have no reason to follow the children of structures; for other
18541 languages we have to, so that we can get at method physnames
18542 to infer fully qualified class names, for DW_AT_specification,
18543 and for C++ template arguments. For C++, we also look one level
18544 inside functions to find template arguments (if the name of the
18545 function does not already contain the template arguments).
18547 For Ada, we need to scan the children of subprograms and lexical
18548 blocks as well because Ada allows the definition of nested
18549 entities that could be interesting for the debugger, such as
18550 nested subprograms for instance. */
18551 if (last_die->has_children
18553 || last_die->tag == DW_TAG_namespace
18554 || last_die->tag == DW_TAG_module
18555 || last_die->tag == DW_TAG_enumeration_type
18556 || (cu->language == language_cplus
18557 && last_die->tag == DW_TAG_subprogram
18558 && (last_die->name == NULL
18559 || strchr (last_die->name, '<') == NULL))
18560 || (cu->language != language_c
18561 && (last_die->tag == DW_TAG_class_type
18562 || last_die->tag == DW_TAG_interface_type
18563 || last_die->tag == DW_TAG_structure_type
18564 || last_die->tag == DW_TAG_union_type))
18565 || (cu->language == language_ada
18566 && (last_die->tag == DW_TAG_subprogram
18567 || last_die->tag == DW_TAG_lexical_block))))
18570 parent_die = last_die;
18574 /* Otherwise we skip to the next sibling, if any. */
18575 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18577 /* Back to the top, do it again. */
18581 /* Read a minimal amount of information into the minimal die structure. */
18583 static const gdb_byte *
18584 read_partial_die (const struct die_reader_specs *reader,
18585 struct partial_die_info *part_die,
18586 struct abbrev_info *abbrev, unsigned int abbrev_len,
18587 const gdb_byte *info_ptr)
18589 struct dwarf2_cu *cu = reader->cu;
18590 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
18591 struct objfile *objfile = dwarf2_per_objfile->objfile;
18592 const gdb_byte *buffer = reader->buffer;
18594 struct attribute attr;
18595 int has_low_pc_attr = 0;
18596 int has_high_pc_attr = 0;
18597 int high_pc_relative = 0;
18599 memset (part_die, 0, sizeof (struct partial_die_info));
18601 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18603 info_ptr += abbrev_len;
18605 if (abbrev == NULL)
18608 part_die->tag = abbrev->tag;
18609 part_die->has_children = abbrev->has_children;
18611 for (i = 0; i < abbrev->num_attrs; ++i)
18613 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18615 /* Store the data if it is of an attribute we want to keep in a
18616 partial symbol table. */
18620 switch (part_die->tag)
18622 case DW_TAG_compile_unit:
18623 case DW_TAG_partial_unit:
18624 case DW_TAG_type_unit:
18625 /* Compilation units have a DW_AT_name that is a filename, not
18626 a source language identifier. */
18627 case DW_TAG_enumeration_type:
18628 case DW_TAG_enumerator:
18629 /* These tags always have simple identifiers already; no need
18630 to canonicalize them. */
18631 part_die->name = DW_STRING (&attr);
18635 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18636 &objfile->per_bfd->storage_obstack);
18640 case DW_AT_linkage_name:
18641 case DW_AT_MIPS_linkage_name:
18642 /* Note that both forms of linkage name might appear. We
18643 assume they will be the same, and we only store the last
18645 if (cu->language == language_ada)
18646 part_die->name = DW_STRING (&attr);
18647 part_die->linkage_name = DW_STRING (&attr);
18650 has_low_pc_attr = 1;
18651 part_die->lowpc = attr_value_as_address (&attr);
18653 case DW_AT_high_pc:
18654 has_high_pc_attr = 1;
18655 part_die->highpc = attr_value_as_address (&attr);
18656 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18657 high_pc_relative = 1;
18659 case DW_AT_location:
18660 /* Support the .debug_loc offsets. */
18661 if (attr_form_is_block (&attr))
18663 part_die->d.locdesc = DW_BLOCK (&attr);
18665 else if (attr_form_is_section_offset (&attr))
18667 dwarf2_complex_location_expr_complaint ();
18671 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18672 "partial symbol information");
18675 case DW_AT_external:
18676 part_die->is_external = DW_UNSND (&attr);
18678 case DW_AT_declaration:
18679 part_die->is_declaration = DW_UNSND (&attr);
18682 part_die->has_type = 1;
18684 case DW_AT_abstract_origin:
18685 case DW_AT_specification:
18686 case DW_AT_extension:
18687 part_die->has_specification = 1;
18688 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18689 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18690 || cu->per_cu->is_dwz);
18692 case DW_AT_sibling:
18693 /* Ignore absolute siblings, they might point outside of
18694 the current compile unit. */
18695 if (attr.form == DW_FORM_ref_addr)
18696 complaint (&symfile_complaints,
18697 _("ignoring absolute DW_AT_sibling"));
18700 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18701 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18703 if (sibling_ptr < info_ptr)
18704 complaint (&symfile_complaints,
18705 _("DW_AT_sibling points backwards"));
18706 else if (sibling_ptr > reader->buffer_end)
18707 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18709 part_die->sibling = sibling_ptr;
18712 case DW_AT_byte_size:
18713 part_die->has_byte_size = 1;
18715 case DW_AT_const_value:
18716 part_die->has_const_value = 1;
18718 case DW_AT_calling_convention:
18719 /* DWARF doesn't provide a way to identify a program's source-level
18720 entry point. DW_AT_calling_convention attributes are only meant
18721 to describe functions' calling conventions.
18723 However, because it's a necessary piece of information in
18724 Fortran, and before DWARF 4 DW_CC_program was the only
18725 piece of debugging information whose definition refers to
18726 a 'main program' at all, several compilers marked Fortran
18727 main programs with DW_CC_program --- even when those
18728 functions use the standard calling conventions.
18730 Although DWARF now specifies a way to provide this
18731 information, we support this practice for backward
18733 if (DW_UNSND (&attr) == DW_CC_program
18734 && cu->language == language_fortran)
18735 part_die->main_subprogram = 1;
18738 if (DW_UNSND (&attr) == DW_INL_inlined
18739 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18740 part_die->may_be_inlined = 1;
18744 if (part_die->tag == DW_TAG_imported_unit)
18746 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18747 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18748 || cu->per_cu->is_dwz);
18752 case DW_AT_main_subprogram:
18753 part_die->main_subprogram = DW_UNSND (&attr);
18761 if (high_pc_relative)
18762 part_die->highpc += part_die->lowpc;
18764 if (has_low_pc_attr && has_high_pc_attr)
18766 /* When using the GNU linker, .gnu.linkonce. sections are used to
18767 eliminate duplicate copies of functions and vtables and such.
18768 The linker will arbitrarily choose one and discard the others.
18769 The AT_*_pc values for such functions refer to local labels in
18770 these sections. If the section from that file was discarded, the
18771 labels are not in the output, so the relocs get a value of 0.
18772 If this is a discarded function, mark the pc bounds as invalid,
18773 so that GDB will ignore it. */
18774 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18776 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18778 complaint (&symfile_complaints,
18779 _("DW_AT_low_pc %s is zero "
18780 "for DIE at 0x%x [in module %s]"),
18781 paddress (gdbarch, part_die->lowpc),
18782 to_underlying (part_die->sect_off), objfile_name (objfile));
18784 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18785 else if (part_die->lowpc >= part_die->highpc)
18787 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18789 complaint (&symfile_complaints,
18790 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18791 "for DIE at 0x%x [in module %s]"),
18792 paddress (gdbarch, part_die->lowpc),
18793 paddress (gdbarch, part_die->highpc),
18794 to_underlying (part_die->sect_off),
18795 objfile_name (objfile));
18798 part_die->has_pc_info = 1;
18804 /* Find a cached partial DIE at OFFSET in CU. */
18806 static struct partial_die_info *
18807 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18809 struct partial_die_info *lookup_die = NULL;
18810 struct partial_die_info part_die;
18812 part_die.sect_off = sect_off;
18813 lookup_die = ((struct partial_die_info *)
18814 htab_find_with_hash (cu->partial_dies, &part_die,
18815 to_underlying (sect_off)));
18820 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18821 except in the case of .debug_types DIEs which do not reference
18822 outside their CU (they do however referencing other types via
18823 DW_FORM_ref_sig8). */
18825 static struct partial_die_info *
18826 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18828 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
18829 struct objfile *objfile = dwarf2_per_objfile->objfile;
18830 struct dwarf2_per_cu_data *per_cu = NULL;
18831 struct partial_die_info *pd = NULL;
18833 if (offset_in_dwz == cu->per_cu->is_dwz
18834 && offset_in_cu_p (&cu->header, sect_off))
18836 pd = find_partial_die_in_comp_unit (sect_off, cu);
18839 /* We missed recording what we needed.
18840 Load all dies and try again. */
18841 per_cu = cu->per_cu;
18845 /* TUs don't reference other CUs/TUs (except via type signatures). */
18846 if (cu->per_cu->is_debug_types)
18848 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18849 " external reference to offset 0x%x [in module %s].\n"),
18850 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18851 bfd_get_filename (objfile->obfd));
18853 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18854 dwarf2_per_objfile);
18856 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18857 load_partial_comp_unit (per_cu);
18859 per_cu->cu->last_used = 0;
18860 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18863 /* If we didn't find it, and not all dies have been loaded,
18864 load them all and try again. */
18866 if (pd == NULL && per_cu->load_all_dies == 0)
18868 per_cu->load_all_dies = 1;
18870 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18871 THIS_CU->cu may already be in use. So we can't just free it and
18872 replace its DIEs with the ones we read in. Instead, we leave those
18873 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18874 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18876 load_partial_comp_unit (per_cu);
18878 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18882 internal_error (__FILE__, __LINE__,
18883 _("could not find partial DIE 0x%x "
18884 "in cache [from module %s]\n"),
18885 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18889 /* See if we can figure out if the class lives in a namespace. We do
18890 this by looking for a member function; its demangled name will
18891 contain namespace info, if there is any. */
18894 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18895 struct dwarf2_cu *cu)
18897 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18898 what template types look like, because the demangler
18899 frequently doesn't give the same name as the debug info. We
18900 could fix this by only using the demangled name to get the
18901 prefix (but see comment in read_structure_type). */
18903 struct partial_die_info *real_pdi;
18904 struct partial_die_info *child_pdi;
18906 /* If this DIE (this DIE's specification, if any) has a parent, then
18907 we should not do this. We'll prepend the parent's fully qualified
18908 name when we create the partial symbol. */
18910 real_pdi = struct_pdi;
18911 while (real_pdi->has_specification)
18912 real_pdi = find_partial_die (real_pdi->spec_offset,
18913 real_pdi->spec_is_dwz, cu);
18915 if (real_pdi->die_parent != NULL)
18918 for (child_pdi = struct_pdi->die_child;
18920 child_pdi = child_pdi->die_sibling)
18922 if (child_pdi->tag == DW_TAG_subprogram
18923 && child_pdi->linkage_name != NULL)
18925 char *actual_class_name
18926 = language_class_name_from_physname (cu->language_defn,
18927 child_pdi->linkage_name);
18928 if (actual_class_name != NULL)
18930 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
18933 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18935 strlen (actual_class_name)));
18936 xfree (actual_class_name);
18943 /* Adjust PART_DIE before generating a symbol for it. This function
18944 may set the is_external flag or change the DIE's name. */
18947 fixup_partial_die (struct partial_die_info *part_die,
18948 struct dwarf2_cu *cu)
18950 /* Once we've fixed up a die, there's no point in doing so again.
18951 This also avoids a memory leak if we were to call
18952 guess_partial_die_structure_name multiple times. */
18953 if (part_die->fixup_called)
18956 /* If we found a reference attribute and the DIE has no name, try
18957 to find a name in the referred to DIE. */
18959 if (part_die->name == NULL && part_die->has_specification)
18961 struct partial_die_info *spec_die;
18963 spec_die = find_partial_die (part_die->spec_offset,
18964 part_die->spec_is_dwz, cu);
18966 fixup_partial_die (spec_die, cu);
18968 if (spec_die->name)
18970 part_die->name = spec_die->name;
18972 /* Copy DW_AT_external attribute if it is set. */
18973 if (spec_die->is_external)
18974 part_die->is_external = spec_die->is_external;
18978 /* Set default names for some unnamed DIEs. */
18980 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18981 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18983 /* If there is no parent die to provide a namespace, and there are
18984 children, see if we can determine the namespace from their linkage
18986 if (cu->language == language_cplus
18987 && !VEC_empty (dwarf2_section_info_def, cu->dwarf2_per_objfile->types)
18988 && part_die->die_parent == NULL
18989 && part_die->has_children
18990 && (part_die->tag == DW_TAG_class_type
18991 || part_die->tag == DW_TAG_structure_type
18992 || part_die->tag == DW_TAG_union_type))
18993 guess_partial_die_structure_name (part_die, cu);
18995 /* GCC might emit a nameless struct or union that has a linkage
18996 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18997 if (part_die->name == NULL
18998 && (part_die->tag == DW_TAG_class_type
18999 || part_die->tag == DW_TAG_interface_type
19000 || part_die->tag == DW_TAG_structure_type
19001 || part_die->tag == DW_TAG_union_type)
19002 && part_die->linkage_name != NULL)
19006 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
19011 /* Strip any leading namespaces/classes, keep only the base name.
19012 DW_AT_name for named DIEs does not contain the prefixes. */
19013 base = strrchr (demangled, ':');
19014 if (base && base > demangled && base[-1] == ':')
19019 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
19022 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19023 base, strlen (base)));
19028 part_die->fixup_called = 1;
19031 /* Read an attribute value described by an attribute form. */
19033 static const gdb_byte *
19034 read_attribute_value (const struct die_reader_specs *reader,
19035 struct attribute *attr, unsigned form,
19036 LONGEST implicit_const, const gdb_byte *info_ptr)
19038 struct dwarf2_cu *cu = reader->cu;
19039 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
19040 struct objfile *objfile = dwarf2_per_objfile->objfile;
19041 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19042 bfd *abfd = reader->abfd;
19043 struct comp_unit_head *cu_header = &cu->header;
19044 unsigned int bytes_read;
19045 struct dwarf_block *blk;
19047 attr->form = (enum dwarf_form) form;
19050 case DW_FORM_ref_addr:
19051 if (cu->header.version == 2)
19052 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19054 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19055 &cu->header, &bytes_read);
19056 info_ptr += bytes_read;
19058 case DW_FORM_GNU_ref_alt:
19059 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19060 info_ptr += bytes_read;
19063 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19064 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19065 info_ptr += bytes_read;
19067 case DW_FORM_block2:
19068 blk = dwarf_alloc_block (cu);
19069 blk->size = read_2_bytes (abfd, info_ptr);
19071 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19072 info_ptr += blk->size;
19073 DW_BLOCK (attr) = blk;
19075 case DW_FORM_block4:
19076 blk = dwarf_alloc_block (cu);
19077 blk->size = read_4_bytes (abfd, info_ptr);
19079 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19080 info_ptr += blk->size;
19081 DW_BLOCK (attr) = blk;
19083 case DW_FORM_data2:
19084 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19087 case DW_FORM_data4:
19088 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19091 case DW_FORM_data8:
19092 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19095 case DW_FORM_data16:
19096 blk = dwarf_alloc_block (cu);
19098 blk->data = read_n_bytes (abfd, info_ptr, 16);
19100 DW_BLOCK (attr) = blk;
19102 case DW_FORM_sec_offset:
19103 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19104 info_ptr += bytes_read;
19106 case DW_FORM_string:
19107 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19108 DW_STRING_IS_CANONICAL (attr) = 0;
19109 info_ptr += bytes_read;
19112 if (!cu->per_cu->is_dwz)
19114 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19115 abfd, info_ptr, cu_header,
19117 DW_STRING_IS_CANONICAL (attr) = 0;
19118 info_ptr += bytes_read;
19122 case DW_FORM_line_strp:
19123 if (!cu->per_cu->is_dwz)
19125 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19127 cu_header, &bytes_read);
19128 DW_STRING_IS_CANONICAL (attr) = 0;
19129 info_ptr += bytes_read;
19133 case DW_FORM_GNU_strp_alt:
19135 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19136 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19139 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19141 DW_STRING_IS_CANONICAL (attr) = 0;
19142 info_ptr += bytes_read;
19145 case DW_FORM_exprloc:
19146 case DW_FORM_block:
19147 blk = dwarf_alloc_block (cu);
19148 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19149 info_ptr += bytes_read;
19150 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19151 info_ptr += blk->size;
19152 DW_BLOCK (attr) = blk;
19154 case DW_FORM_block1:
19155 blk = dwarf_alloc_block (cu);
19156 blk->size = read_1_byte (abfd, info_ptr);
19158 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19159 info_ptr += blk->size;
19160 DW_BLOCK (attr) = blk;
19162 case DW_FORM_data1:
19163 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19167 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19170 case DW_FORM_flag_present:
19171 DW_UNSND (attr) = 1;
19173 case DW_FORM_sdata:
19174 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19175 info_ptr += bytes_read;
19177 case DW_FORM_udata:
19178 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19179 info_ptr += bytes_read;
19182 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19183 + read_1_byte (abfd, info_ptr));
19187 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19188 + read_2_bytes (abfd, info_ptr));
19192 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19193 + read_4_bytes (abfd, info_ptr));
19197 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19198 + read_8_bytes (abfd, info_ptr));
19201 case DW_FORM_ref_sig8:
19202 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19205 case DW_FORM_ref_udata:
19206 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19207 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19208 info_ptr += bytes_read;
19210 case DW_FORM_indirect:
19211 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19212 info_ptr += bytes_read;
19213 if (form == DW_FORM_implicit_const)
19215 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19216 info_ptr += bytes_read;
19218 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19221 case DW_FORM_implicit_const:
19222 DW_SND (attr) = implicit_const;
19224 case DW_FORM_GNU_addr_index:
19225 if (reader->dwo_file == NULL)
19227 /* For now flag a hard error.
19228 Later we can turn this into a complaint. */
19229 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19230 dwarf_form_name (form),
19231 bfd_get_filename (abfd));
19233 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19234 info_ptr += bytes_read;
19236 case DW_FORM_GNU_str_index:
19237 if (reader->dwo_file == NULL)
19239 /* For now flag a hard error.
19240 Later we can turn this into a complaint if warranted. */
19241 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19242 dwarf_form_name (form),
19243 bfd_get_filename (abfd));
19246 ULONGEST str_index =
19247 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19249 DW_STRING (attr) = read_str_index (reader, str_index);
19250 DW_STRING_IS_CANONICAL (attr) = 0;
19251 info_ptr += bytes_read;
19255 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19256 dwarf_form_name (form),
19257 bfd_get_filename (abfd));
19261 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19262 attr->form = DW_FORM_GNU_ref_alt;
19264 /* We have seen instances where the compiler tried to emit a byte
19265 size attribute of -1 which ended up being encoded as an unsigned
19266 0xffffffff. Although 0xffffffff is technically a valid size value,
19267 an object of this size seems pretty unlikely so we can relatively
19268 safely treat these cases as if the size attribute was invalid and
19269 treat them as zero by default. */
19270 if (attr->name == DW_AT_byte_size
19271 && form == DW_FORM_data4
19272 && DW_UNSND (attr) >= 0xffffffff)
19275 (&symfile_complaints,
19276 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19277 hex_string (DW_UNSND (attr)));
19278 DW_UNSND (attr) = 0;
19284 /* Read an attribute described by an abbreviated attribute. */
19286 static const gdb_byte *
19287 read_attribute (const struct die_reader_specs *reader,
19288 struct attribute *attr, struct attr_abbrev *abbrev,
19289 const gdb_byte *info_ptr)
19291 attr->name = abbrev->name;
19292 return read_attribute_value (reader, attr, abbrev->form,
19293 abbrev->implicit_const, info_ptr);
19296 /* Read dwarf information from a buffer. */
19298 static unsigned int
19299 read_1_byte (bfd *abfd, const gdb_byte *buf)
19301 return bfd_get_8 (abfd, buf);
19305 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19307 return bfd_get_signed_8 (abfd, buf);
19310 static unsigned int
19311 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19313 return bfd_get_16 (abfd, buf);
19317 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19319 return bfd_get_signed_16 (abfd, buf);
19322 static unsigned int
19323 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19325 return bfd_get_32 (abfd, buf);
19329 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19331 return bfd_get_signed_32 (abfd, buf);
19335 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19337 return bfd_get_64 (abfd, buf);
19341 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19342 unsigned int *bytes_read)
19344 struct comp_unit_head *cu_header = &cu->header;
19345 CORE_ADDR retval = 0;
19347 if (cu_header->signed_addr_p)
19349 switch (cu_header->addr_size)
19352 retval = bfd_get_signed_16 (abfd, buf);
19355 retval = bfd_get_signed_32 (abfd, buf);
19358 retval = bfd_get_signed_64 (abfd, buf);
19361 internal_error (__FILE__, __LINE__,
19362 _("read_address: bad switch, signed [in module %s]"),
19363 bfd_get_filename (abfd));
19368 switch (cu_header->addr_size)
19371 retval = bfd_get_16 (abfd, buf);
19374 retval = bfd_get_32 (abfd, buf);
19377 retval = bfd_get_64 (abfd, buf);
19380 internal_error (__FILE__, __LINE__,
19381 _("read_address: bad switch, "
19382 "unsigned [in module %s]"),
19383 bfd_get_filename (abfd));
19387 *bytes_read = cu_header->addr_size;
19391 /* Read the initial length from a section. The (draft) DWARF 3
19392 specification allows the initial length to take up either 4 bytes
19393 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19394 bytes describe the length and all offsets will be 8 bytes in length
19397 An older, non-standard 64-bit format is also handled by this
19398 function. The older format in question stores the initial length
19399 as an 8-byte quantity without an escape value. Lengths greater
19400 than 2^32 aren't very common which means that the initial 4 bytes
19401 is almost always zero. Since a length value of zero doesn't make
19402 sense for the 32-bit format, this initial zero can be considered to
19403 be an escape value which indicates the presence of the older 64-bit
19404 format. As written, the code can't detect (old format) lengths
19405 greater than 4GB. If it becomes necessary to handle lengths
19406 somewhat larger than 4GB, we could allow other small values (such
19407 as the non-sensical values of 1, 2, and 3) to also be used as
19408 escape values indicating the presence of the old format.
19410 The value returned via bytes_read should be used to increment the
19411 relevant pointer after calling read_initial_length().
19413 [ Note: read_initial_length() and read_offset() are based on the
19414 document entitled "DWARF Debugging Information Format", revision
19415 3, draft 8, dated November 19, 2001. This document was obtained
19418 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19420 This document is only a draft and is subject to change. (So beware.)
19422 Details regarding the older, non-standard 64-bit format were
19423 determined empirically by examining 64-bit ELF files produced by
19424 the SGI toolchain on an IRIX 6.5 machine.
19426 - Kevin, July 16, 2002
19430 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19432 LONGEST length = bfd_get_32 (abfd, buf);
19434 if (length == 0xffffffff)
19436 length = bfd_get_64 (abfd, buf + 4);
19439 else if (length == 0)
19441 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19442 length = bfd_get_64 (abfd, buf);
19453 /* Cover function for read_initial_length.
19454 Returns the length of the object at BUF, and stores the size of the
19455 initial length in *BYTES_READ and stores the size that offsets will be in
19457 If the initial length size is not equivalent to that specified in
19458 CU_HEADER then issue a complaint.
19459 This is useful when reading non-comp-unit headers. */
19462 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19463 const struct comp_unit_head *cu_header,
19464 unsigned int *bytes_read,
19465 unsigned int *offset_size)
19467 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19469 gdb_assert (cu_header->initial_length_size == 4
19470 || cu_header->initial_length_size == 8
19471 || cu_header->initial_length_size == 12);
19473 if (cu_header->initial_length_size != *bytes_read)
19474 complaint (&symfile_complaints,
19475 _("intermixed 32-bit and 64-bit DWARF sections"));
19477 *offset_size = (*bytes_read == 4) ? 4 : 8;
19481 /* Read an offset from the data stream. The size of the offset is
19482 given by cu_header->offset_size. */
19485 read_offset (bfd *abfd, const gdb_byte *buf,
19486 const struct comp_unit_head *cu_header,
19487 unsigned int *bytes_read)
19489 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19491 *bytes_read = cu_header->offset_size;
19495 /* Read an offset from the data stream. */
19498 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19500 LONGEST retval = 0;
19502 switch (offset_size)
19505 retval = bfd_get_32 (abfd, buf);
19508 retval = bfd_get_64 (abfd, buf);
19511 internal_error (__FILE__, __LINE__,
19512 _("read_offset_1: bad switch [in module %s]"),
19513 bfd_get_filename (abfd));
19519 static const gdb_byte *
19520 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19522 /* If the size of a host char is 8 bits, we can return a pointer
19523 to the buffer, otherwise we have to copy the data to a buffer
19524 allocated on the temporary obstack. */
19525 gdb_assert (HOST_CHAR_BIT == 8);
19529 static const char *
19530 read_direct_string (bfd *abfd, const gdb_byte *buf,
19531 unsigned int *bytes_read_ptr)
19533 /* If the size of a host char is 8 bits, we can return a pointer
19534 to the string, otherwise we have to copy the string to a buffer
19535 allocated on the temporary obstack. */
19536 gdb_assert (HOST_CHAR_BIT == 8);
19539 *bytes_read_ptr = 1;
19542 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19543 return (const char *) buf;
19546 /* Return pointer to string at section SECT offset STR_OFFSET with error
19547 reporting strings FORM_NAME and SECT_NAME. */
19549 static const char *
19550 read_indirect_string_at_offset_from (struct objfile *objfile,
19551 bfd *abfd, LONGEST str_offset,
19552 struct dwarf2_section_info *sect,
19553 const char *form_name,
19554 const char *sect_name)
19556 dwarf2_read_section (objfile, sect);
19557 if (sect->buffer == NULL)
19558 error (_("%s used without %s section [in module %s]"),
19559 form_name, sect_name, bfd_get_filename (abfd));
19560 if (str_offset >= sect->size)
19561 error (_("%s pointing outside of %s section [in module %s]"),
19562 form_name, sect_name, bfd_get_filename (abfd));
19563 gdb_assert (HOST_CHAR_BIT == 8);
19564 if (sect->buffer[str_offset] == '\0')
19566 return (const char *) (sect->buffer + str_offset);
19569 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19571 static const char *
19572 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19573 bfd *abfd, LONGEST str_offset)
19575 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19577 &dwarf2_per_objfile->str,
19578 "DW_FORM_strp", ".debug_str");
19581 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19583 static const char *
19584 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19585 bfd *abfd, LONGEST str_offset)
19587 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19589 &dwarf2_per_objfile->line_str,
19590 "DW_FORM_line_strp",
19591 ".debug_line_str");
19594 /* Read a string at offset STR_OFFSET in the .debug_str section from
19595 the .dwz file DWZ. Throw an error if the offset is too large. If
19596 the string consists of a single NUL byte, return NULL; otherwise
19597 return a pointer to the string. */
19599 static const char *
19600 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19601 LONGEST str_offset)
19603 dwarf2_read_section (objfile, &dwz->str);
19605 if (dwz->str.buffer == NULL)
19606 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19607 "section [in module %s]"),
19608 bfd_get_filename (dwz->dwz_bfd));
19609 if (str_offset >= dwz->str.size)
19610 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19611 ".debug_str section [in module %s]"),
19612 bfd_get_filename (dwz->dwz_bfd));
19613 gdb_assert (HOST_CHAR_BIT == 8);
19614 if (dwz->str.buffer[str_offset] == '\0')
19616 return (const char *) (dwz->str.buffer + str_offset);
19619 /* Return pointer to string at .debug_str offset as read from BUF.
19620 BUF is assumed to be in a compilation unit described by CU_HEADER.
19621 Return *BYTES_READ_PTR count of bytes read from BUF. */
19623 static const char *
19624 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19625 const gdb_byte *buf,
19626 const struct comp_unit_head *cu_header,
19627 unsigned int *bytes_read_ptr)
19629 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19631 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19634 /* Return pointer to string at .debug_line_str offset as read from BUF.
19635 BUF is assumed to be in a compilation unit described by CU_HEADER.
19636 Return *BYTES_READ_PTR count of bytes read from BUF. */
19638 static const char *
19639 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19640 bfd *abfd, const gdb_byte *buf,
19641 const struct comp_unit_head *cu_header,
19642 unsigned int *bytes_read_ptr)
19644 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19646 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19651 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19652 unsigned int *bytes_read_ptr)
19655 unsigned int num_read;
19657 unsigned char byte;
19664 byte = bfd_get_8 (abfd, buf);
19667 result |= ((ULONGEST) (byte & 127) << shift);
19668 if ((byte & 128) == 0)
19674 *bytes_read_ptr = num_read;
19679 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19680 unsigned int *bytes_read_ptr)
19683 int shift, num_read;
19684 unsigned char byte;
19691 byte = bfd_get_8 (abfd, buf);
19694 result |= ((LONGEST) (byte & 127) << shift);
19696 if ((byte & 128) == 0)
19701 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19702 result |= -(((LONGEST) 1) << shift);
19703 *bytes_read_ptr = num_read;
19707 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19708 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19709 ADDR_SIZE is the size of addresses from the CU header. */
19712 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19713 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19715 struct objfile *objfile = dwarf2_per_objfile->objfile;
19716 bfd *abfd = objfile->obfd;
19717 const gdb_byte *info_ptr;
19719 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19720 if (dwarf2_per_objfile->addr.buffer == NULL)
19721 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19722 objfile_name (objfile));
19723 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19724 error (_("DW_FORM_addr_index pointing outside of "
19725 ".debug_addr section [in module %s]"),
19726 objfile_name (objfile));
19727 info_ptr = (dwarf2_per_objfile->addr.buffer
19728 + addr_base + addr_index * addr_size);
19729 if (addr_size == 4)
19730 return bfd_get_32 (abfd, info_ptr);
19732 return bfd_get_64 (abfd, info_ptr);
19735 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19738 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19740 return read_addr_index_1 (cu->dwarf2_per_objfile, addr_index, cu->addr_base,
19741 cu->header.addr_size);
19744 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19747 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19748 unsigned int *bytes_read)
19750 bfd *abfd = cu->dwarf2_per_objfile->objfile->obfd;
19751 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19753 return read_addr_index (cu, addr_index);
19756 /* Data structure to pass results from dwarf2_read_addr_index_reader
19757 back to dwarf2_read_addr_index. */
19759 struct dwarf2_read_addr_index_data
19761 ULONGEST addr_base;
19765 /* die_reader_func for dwarf2_read_addr_index. */
19768 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19769 const gdb_byte *info_ptr,
19770 struct die_info *comp_unit_die,
19774 struct dwarf2_cu *cu = reader->cu;
19775 struct dwarf2_read_addr_index_data *aidata =
19776 (struct dwarf2_read_addr_index_data *) data;
19778 aidata->addr_base = cu->addr_base;
19779 aidata->addr_size = cu->header.addr_size;
19782 /* Given an index in .debug_addr, fetch the value.
19783 NOTE: This can be called during dwarf expression evaluation,
19784 long after the debug information has been read, and thus per_cu->cu
19785 may no longer exist. */
19788 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19789 unsigned int addr_index)
19791 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19792 struct objfile *objfile = dwarf2_per_objfile->objfile;
19793 struct dwarf2_cu *cu = per_cu->cu;
19794 ULONGEST addr_base;
19797 /* We need addr_base and addr_size.
19798 If we don't have PER_CU->cu, we have to get it.
19799 Nasty, but the alternative is storing the needed info in PER_CU,
19800 which at this point doesn't seem justified: it's not clear how frequently
19801 it would get used and it would increase the size of every PER_CU.
19802 Entry points like dwarf2_per_cu_addr_size do a similar thing
19803 so we're not in uncharted territory here.
19804 Alas we need to be a bit more complicated as addr_base is contained
19807 We don't need to read the entire CU(/TU).
19808 We just need the header and top level die.
19810 IWBN to use the aging mechanism to let us lazily later discard the CU.
19811 For now we skip this optimization. */
19815 addr_base = cu->addr_base;
19816 addr_size = cu->header.addr_size;
19820 struct dwarf2_read_addr_index_data aidata;
19822 /* Note: We can't use init_cutu_and_read_dies_simple here,
19823 we need addr_base. */
19824 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19825 dwarf2_read_addr_index_reader, &aidata);
19826 addr_base = aidata.addr_base;
19827 addr_size = aidata.addr_size;
19830 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19834 /* Given a DW_FORM_GNU_str_index, fetch the string.
19835 This is only used by the Fission support. */
19837 static const char *
19838 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19840 struct dwarf2_cu *cu = reader->cu;
19841 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
19842 struct objfile *objfile = dwarf2_per_objfile->objfile;
19843 const char *objf_name = objfile_name (objfile);
19844 bfd *abfd = objfile->obfd;
19845 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19846 struct dwarf2_section_info *str_offsets_section =
19847 &reader->dwo_file->sections.str_offsets;
19848 const gdb_byte *info_ptr;
19849 ULONGEST str_offset;
19850 static const char form_name[] = "DW_FORM_GNU_str_index";
19852 dwarf2_read_section (objfile, str_section);
19853 dwarf2_read_section (objfile, str_offsets_section);
19854 if (str_section->buffer == NULL)
19855 error (_("%s used without .debug_str.dwo section"
19856 " in CU at offset 0x%x [in module %s]"),
19857 form_name, to_underlying (cu->header.sect_off), objf_name);
19858 if (str_offsets_section->buffer == NULL)
19859 error (_("%s used without .debug_str_offsets.dwo section"
19860 " in CU at offset 0x%x [in module %s]"),
19861 form_name, to_underlying (cu->header.sect_off), objf_name);
19862 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19863 error (_("%s pointing outside of .debug_str_offsets.dwo"
19864 " section in CU at offset 0x%x [in module %s]"),
19865 form_name, to_underlying (cu->header.sect_off), objf_name);
19866 info_ptr = (str_offsets_section->buffer
19867 + str_index * cu->header.offset_size);
19868 if (cu->header.offset_size == 4)
19869 str_offset = bfd_get_32 (abfd, info_ptr);
19871 str_offset = bfd_get_64 (abfd, info_ptr);
19872 if (str_offset >= str_section->size)
19873 error (_("Offset from %s pointing outside of"
19874 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19875 form_name, to_underlying (cu->header.sect_off), objf_name);
19876 return (const char *) (str_section->buffer + str_offset);
19879 /* Return the length of an LEB128 number in BUF. */
19882 leb128_size (const gdb_byte *buf)
19884 const gdb_byte *begin = buf;
19890 if ((byte & 128) == 0)
19891 return buf - begin;
19896 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19905 cu->language = language_c;
19908 case DW_LANG_C_plus_plus:
19909 case DW_LANG_C_plus_plus_11:
19910 case DW_LANG_C_plus_plus_14:
19911 cu->language = language_cplus;
19914 cu->language = language_d;
19916 case DW_LANG_Fortran77:
19917 case DW_LANG_Fortran90:
19918 case DW_LANG_Fortran95:
19919 case DW_LANG_Fortran03:
19920 case DW_LANG_Fortran08:
19921 cu->language = language_fortran;
19924 cu->language = language_go;
19926 case DW_LANG_Mips_Assembler:
19927 cu->language = language_asm;
19929 case DW_LANG_Ada83:
19930 case DW_LANG_Ada95:
19931 cu->language = language_ada;
19933 case DW_LANG_Modula2:
19934 cu->language = language_m2;
19936 case DW_LANG_Pascal83:
19937 cu->language = language_pascal;
19940 cu->language = language_objc;
19943 case DW_LANG_Rust_old:
19944 cu->language = language_rust;
19946 case DW_LANG_Cobol74:
19947 case DW_LANG_Cobol85:
19949 cu->language = language_minimal;
19952 cu->language_defn = language_def (cu->language);
19955 /* Return the named attribute or NULL if not there. */
19957 static struct attribute *
19958 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19963 struct attribute *spec = NULL;
19965 for (i = 0; i < die->num_attrs; ++i)
19967 if (die->attrs[i].name == name)
19968 return &die->attrs[i];
19969 if (die->attrs[i].name == DW_AT_specification
19970 || die->attrs[i].name == DW_AT_abstract_origin)
19971 spec = &die->attrs[i];
19977 die = follow_die_ref (die, spec, &cu);
19983 /* Return the named attribute or NULL if not there,
19984 but do not follow DW_AT_specification, etc.
19985 This is for use in contexts where we're reading .debug_types dies.
19986 Following DW_AT_specification, DW_AT_abstract_origin will take us
19987 back up the chain, and we want to go down. */
19989 static struct attribute *
19990 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19994 for (i = 0; i < die->num_attrs; ++i)
19995 if (die->attrs[i].name == name)
19996 return &die->attrs[i];
20001 /* Return the string associated with a string-typed attribute, or NULL if it
20002 is either not found or is of an incorrect type. */
20004 static const char *
20005 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20007 struct attribute *attr;
20008 const char *str = NULL;
20010 attr = dwarf2_attr (die, name, cu);
20014 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20015 || attr->form == DW_FORM_string
20016 || attr->form == DW_FORM_GNU_str_index
20017 || attr->form == DW_FORM_GNU_strp_alt)
20018 str = DW_STRING (attr);
20020 complaint (&symfile_complaints,
20021 _("string type expected for attribute %s for "
20022 "DIE at 0x%x in module %s"),
20023 dwarf_attr_name (name), to_underlying (die->sect_off),
20024 objfile_name (cu->dwarf2_per_objfile->objfile));
20030 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20031 and holds a non-zero value. This function should only be used for
20032 DW_FORM_flag or DW_FORM_flag_present attributes. */
20035 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20037 struct attribute *attr = dwarf2_attr (die, name, cu);
20039 return (attr && DW_UNSND (attr));
20043 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20045 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20046 which value is non-zero. However, we have to be careful with
20047 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20048 (via dwarf2_flag_true_p) follows this attribute. So we may
20049 end up accidently finding a declaration attribute that belongs
20050 to a different DIE referenced by the specification attribute,
20051 even though the given DIE does not have a declaration attribute. */
20052 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20053 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20056 /* Return the die giving the specification for DIE, if there is
20057 one. *SPEC_CU is the CU containing DIE on input, and the CU
20058 containing the return value on output. If there is no
20059 specification, but there is an abstract origin, that is
20062 static struct die_info *
20063 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20065 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20068 if (spec_attr == NULL)
20069 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20071 if (spec_attr == NULL)
20074 return follow_die_ref (die, spec_attr, spec_cu);
20077 /* Stub for free_line_header to match void * callback types. */
20080 free_line_header_voidp (void *arg)
20082 struct line_header *lh = (struct line_header *) arg;
20088 line_header::add_include_dir (const char *include_dir)
20090 if (dwarf_line_debug >= 2)
20091 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20092 include_dirs.size () + 1, include_dir);
20094 include_dirs.push_back (include_dir);
20098 line_header::add_file_name (const char *name,
20100 unsigned int mod_time,
20101 unsigned int length)
20103 if (dwarf_line_debug >= 2)
20104 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20105 (unsigned) file_names.size () + 1, name);
20107 file_names.emplace_back (name, d_index, mod_time, length);
20110 /* A convenience function to find the proper .debug_line section for a CU. */
20112 static struct dwarf2_section_info *
20113 get_debug_line_section (struct dwarf2_cu *cu)
20115 struct dwarf2_section_info *section;
20116 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
20118 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20120 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20121 section = &cu->dwo_unit->dwo_file->sections.line;
20122 else if (cu->per_cu->is_dwz)
20124 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20126 section = &dwz->line;
20129 section = &dwarf2_per_objfile->line;
20134 /* Read directory or file name entry format, starting with byte of
20135 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20136 entries count and the entries themselves in the described entry
20140 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20141 bfd *abfd, const gdb_byte **bufp,
20142 struct line_header *lh,
20143 const struct comp_unit_head *cu_header,
20144 void (*callback) (struct line_header *lh,
20147 unsigned int mod_time,
20148 unsigned int length))
20150 gdb_byte format_count, formati;
20151 ULONGEST data_count, datai;
20152 const gdb_byte *buf = *bufp;
20153 const gdb_byte *format_header_data;
20154 unsigned int bytes_read;
20156 format_count = read_1_byte (abfd, buf);
20158 format_header_data = buf;
20159 for (formati = 0; formati < format_count; formati++)
20161 read_unsigned_leb128 (abfd, buf, &bytes_read);
20163 read_unsigned_leb128 (abfd, buf, &bytes_read);
20167 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20169 for (datai = 0; datai < data_count; datai++)
20171 const gdb_byte *format = format_header_data;
20172 struct file_entry fe;
20174 for (formati = 0; formati < format_count; formati++)
20176 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20177 format += bytes_read;
20179 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20180 format += bytes_read;
20182 gdb::optional<const char *> string;
20183 gdb::optional<unsigned int> uint;
20187 case DW_FORM_string:
20188 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20192 case DW_FORM_line_strp:
20193 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20200 case DW_FORM_data1:
20201 uint.emplace (read_1_byte (abfd, buf));
20205 case DW_FORM_data2:
20206 uint.emplace (read_2_bytes (abfd, buf));
20210 case DW_FORM_data4:
20211 uint.emplace (read_4_bytes (abfd, buf));
20215 case DW_FORM_data8:
20216 uint.emplace (read_8_bytes (abfd, buf));
20220 case DW_FORM_udata:
20221 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20225 case DW_FORM_block:
20226 /* It is valid only for DW_LNCT_timestamp which is ignored by
20231 switch (content_type)
20234 if (string.has_value ())
20237 case DW_LNCT_directory_index:
20238 if (uint.has_value ())
20239 fe.d_index = (dir_index) *uint;
20241 case DW_LNCT_timestamp:
20242 if (uint.has_value ())
20243 fe.mod_time = *uint;
20246 if (uint.has_value ())
20252 complaint (&symfile_complaints,
20253 _("Unknown format content type %s"),
20254 pulongest (content_type));
20258 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20264 /* Read the statement program header starting at OFFSET in
20265 .debug_line, or .debug_line.dwo. Return a pointer
20266 to a struct line_header, allocated using xmalloc.
20267 Returns NULL if there is a problem reading the header, e.g., if it
20268 has a version we don't understand.
20270 NOTE: the strings in the include directory and file name tables of
20271 the returned object point into the dwarf line section buffer,
20272 and must not be freed. */
20274 static line_header_up
20275 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20277 const gdb_byte *line_ptr;
20278 unsigned int bytes_read, offset_size;
20280 const char *cur_dir, *cur_file;
20281 struct dwarf2_section_info *section;
20283 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
20285 section = get_debug_line_section (cu);
20286 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20287 if (section->buffer == NULL)
20289 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20290 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20292 complaint (&symfile_complaints, _("missing .debug_line section"));
20296 /* We can't do this until we know the section is non-empty.
20297 Only then do we know we have such a section. */
20298 abfd = get_section_bfd_owner (section);
20300 /* Make sure that at least there's room for the total_length field.
20301 That could be 12 bytes long, but we're just going to fudge that. */
20302 if (to_underlying (sect_off) + 4 >= section->size)
20304 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20308 line_header_up lh (new line_header ());
20310 lh->sect_off = sect_off;
20311 lh->offset_in_dwz = cu->per_cu->is_dwz;
20313 line_ptr = section->buffer + to_underlying (sect_off);
20315 /* Read in the header. */
20317 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20318 &bytes_read, &offset_size);
20319 line_ptr += bytes_read;
20320 if (line_ptr + lh->total_length > (section->buffer + section->size))
20322 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20325 lh->statement_program_end = line_ptr + lh->total_length;
20326 lh->version = read_2_bytes (abfd, line_ptr);
20328 if (lh->version > 5)
20330 /* This is a version we don't understand. The format could have
20331 changed in ways we don't handle properly so just punt. */
20332 complaint (&symfile_complaints,
20333 _("unsupported version in .debug_line section"));
20336 if (lh->version >= 5)
20338 gdb_byte segment_selector_size;
20340 /* Skip address size. */
20341 read_1_byte (abfd, line_ptr);
20344 segment_selector_size = read_1_byte (abfd, line_ptr);
20346 if (segment_selector_size != 0)
20348 complaint (&symfile_complaints,
20349 _("unsupported segment selector size %u "
20350 "in .debug_line section"),
20351 segment_selector_size);
20355 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20356 line_ptr += offset_size;
20357 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20359 if (lh->version >= 4)
20361 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20365 lh->maximum_ops_per_instruction = 1;
20367 if (lh->maximum_ops_per_instruction == 0)
20369 lh->maximum_ops_per_instruction = 1;
20370 complaint (&symfile_complaints,
20371 _("invalid maximum_ops_per_instruction "
20372 "in `.debug_line' section"));
20375 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20377 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20379 lh->line_range = read_1_byte (abfd, line_ptr);
20381 lh->opcode_base = read_1_byte (abfd, line_ptr);
20383 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20385 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20386 for (i = 1; i < lh->opcode_base; ++i)
20388 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20392 if (lh->version >= 5)
20394 /* Read directory table. */
20395 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20397 [] (struct line_header *lh, const char *name,
20398 dir_index d_index, unsigned int mod_time,
20399 unsigned int length)
20401 lh->add_include_dir (name);
20404 /* Read file name table. */
20405 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20407 [] (struct line_header *lh, const char *name,
20408 dir_index d_index, unsigned int mod_time,
20409 unsigned int length)
20411 lh->add_file_name (name, d_index, mod_time, length);
20416 /* Read directory table. */
20417 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20419 line_ptr += bytes_read;
20420 lh->add_include_dir (cur_dir);
20422 line_ptr += bytes_read;
20424 /* Read file name table. */
20425 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20427 unsigned int mod_time, length;
20430 line_ptr += bytes_read;
20431 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20432 line_ptr += bytes_read;
20433 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20434 line_ptr += bytes_read;
20435 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20436 line_ptr += bytes_read;
20438 lh->add_file_name (cur_file, d_index, mod_time, length);
20440 line_ptr += bytes_read;
20442 lh->statement_program_start = line_ptr;
20444 if (line_ptr > (section->buffer + section->size))
20445 complaint (&symfile_complaints,
20446 _("line number info header doesn't "
20447 "fit in `.debug_line' section"));
20452 /* Subroutine of dwarf_decode_lines to simplify it.
20453 Return the file name of the psymtab for included file FILE_INDEX
20454 in line header LH of PST.
20455 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20456 If space for the result is malloc'd, it will be freed by a cleanup.
20457 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20459 The function creates dangling cleanup registration. */
20461 static const char *
20462 psymtab_include_file_name (const struct line_header *lh, int file_index,
20463 const struct partial_symtab *pst,
20464 const char *comp_dir)
20466 const file_entry &fe = lh->file_names[file_index];
20467 const char *include_name = fe.name;
20468 const char *include_name_to_compare = include_name;
20469 const char *pst_filename;
20470 char *copied_name = NULL;
20473 const char *dir_name = fe.include_dir (lh);
20475 if (!IS_ABSOLUTE_PATH (include_name)
20476 && (dir_name != NULL || comp_dir != NULL))
20478 /* Avoid creating a duplicate psymtab for PST.
20479 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20480 Before we do the comparison, however, we need to account
20481 for DIR_NAME and COMP_DIR.
20482 First prepend dir_name (if non-NULL). If we still don't
20483 have an absolute path prepend comp_dir (if non-NULL).
20484 However, the directory we record in the include-file's
20485 psymtab does not contain COMP_DIR (to match the
20486 corresponding symtab(s)).
20491 bash$ gcc -g ./hello.c
20492 include_name = "hello.c"
20494 DW_AT_comp_dir = comp_dir = "/tmp"
20495 DW_AT_name = "./hello.c"
20499 if (dir_name != NULL)
20501 char *tem = concat (dir_name, SLASH_STRING,
20502 include_name, (char *)NULL);
20504 make_cleanup (xfree, tem);
20505 include_name = tem;
20506 include_name_to_compare = include_name;
20508 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20510 char *tem = concat (comp_dir, SLASH_STRING,
20511 include_name, (char *)NULL);
20513 make_cleanup (xfree, tem);
20514 include_name_to_compare = tem;
20518 pst_filename = pst->filename;
20519 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20521 copied_name = concat (pst->dirname, SLASH_STRING,
20522 pst_filename, (char *)NULL);
20523 pst_filename = copied_name;
20526 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20528 if (copied_name != NULL)
20529 xfree (copied_name);
20533 return include_name;
20536 /* State machine to track the state of the line number program. */
20538 class lnp_state_machine
20541 /* Initialize a machine state for the start of a line number
20543 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20545 file_entry *current_file ()
20547 /* lh->file_names is 0-based, but the file name numbers in the
20548 statement program are 1-based. */
20549 return m_line_header->file_name_at (m_file);
20552 /* Record the line in the state machine. END_SEQUENCE is true if
20553 we're processing the end of a sequence. */
20554 void record_line (bool end_sequence);
20556 /* Check address and if invalid nop-out the rest of the lines in this
20558 void check_line_address (struct dwarf2_cu *cu,
20559 const gdb_byte *line_ptr,
20560 CORE_ADDR lowpc, CORE_ADDR address);
20562 void handle_set_discriminator (unsigned int discriminator)
20564 m_discriminator = discriminator;
20565 m_line_has_non_zero_discriminator |= discriminator != 0;
20568 /* Handle DW_LNE_set_address. */
20569 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20572 address += baseaddr;
20573 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20576 /* Handle DW_LNS_advance_pc. */
20577 void handle_advance_pc (CORE_ADDR adjust);
20579 /* Handle a special opcode. */
20580 void handle_special_opcode (unsigned char op_code);
20582 /* Handle DW_LNS_advance_line. */
20583 void handle_advance_line (int line_delta)
20585 advance_line (line_delta);
20588 /* Handle DW_LNS_set_file. */
20589 void handle_set_file (file_name_index file);
20591 /* Handle DW_LNS_negate_stmt. */
20592 void handle_negate_stmt ()
20594 m_is_stmt = !m_is_stmt;
20597 /* Handle DW_LNS_const_add_pc. */
20598 void handle_const_add_pc ();
20600 /* Handle DW_LNS_fixed_advance_pc. */
20601 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20603 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20607 /* Handle DW_LNS_copy. */
20608 void handle_copy ()
20610 record_line (false);
20611 m_discriminator = 0;
20614 /* Handle DW_LNE_end_sequence. */
20615 void handle_end_sequence ()
20617 m_record_line_callback = ::record_line;
20621 /* Advance the line by LINE_DELTA. */
20622 void advance_line (int line_delta)
20624 m_line += line_delta;
20626 if (line_delta != 0)
20627 m_line_has_non_zero_discriminator = m_discriminator != 0;
20630 gdbarch *m_gdbarch;
20632 /* True if we're recording lines.
20633 Otherwise we're building partial symtabs and are just interested in
20634 finding include files mentioned by the line number program. */
20635 bool m_record_lines_p;
20637 /* The line number header. */
20638 line_header *m_line_header;
20640 /* These are part of the standard DWARF line number state machine,
20641 and initialized according to the DWARF spec. */
20643 unsigned char m_op_index = 0;
20644 /* The line table index (1-based) of the current file. */
20645 file_name_index m_file = (file_name_index) 1;
20646 unsigned int m_line = 1;
20648 /* These are initialized in the constructor. */
20650 CORE_ADDR m_address;
20652 unsigned int m_discriminator;
20654 /* Additional bits of state we need to track. */
20656 /* The last file that we called dwarf2_start_subfile for.
20657 This is only used for TLLs. */
20658 unsigned int m_last_file = 0;
20659 /* The last file a line number was recorded for. */
20660 struct subfile *m_last_subfile = NULL;
20662 /* The function to call to record a line. */
20663 record_line_ftype *m_record_line_callback = NULL;
20665 /* The last line number that was recorded, used to coalesce
20666 consecutive entries for the same line. This can happen, for
20667 example, when discriminators are present. PR 17276. */
20668 unsigned int m_last_line = 0;
20669 bool m_line_has_non_zero_discriminator = false;
20673 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20675 CORE_ADDR addr_adj = (((m_op_index + adjust)
20676 / m_line_header->maximum_ops_per_instruction)
20677 * m_line_header->minimum_instruction_length);
20678 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20679 m_op_index = ((m_op_index + adjust)
20680 % m_line_header->maximum_ops_per_instruction);
20684 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20686 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20687 CORE_ADDR addr_adj = (((m_op_index
20688 + (adj_opcode / m_line_header->line_range))
20689 / m_line_header->maximum_ops_per_instruction)
20690 * m_line_header->minimum_instruction_length);
20691 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20692 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20693 % m_line_header->maximum_ops_per_instruction);
20695 int line_delta = (m_line_header->line_base
20696 + (adj_opcode % m_line_header->line_range));
20697 advance_line (line_delta);
20698 record_line (false);
20699 m_discriminator = 0;
20703 lnp_state_machine::handle_set_file (file_name_index file)
20707 const file_entry *fe = current_file ();
20709 dwarf2_debug_line_missing_file_complaint ();
20710 else if (m_record_lines_p)
20712 const char *dir = fe->include_dir (m_line_header);
20714 m_last_subfile = current_subfile;
20715 m_line_has_non_zero_discriminator = m_discriminator != 0;
20716 dwarf2_start_subfile (fe->name, dir);
20721 lnp_state_machine::handle_const_add_pc ()
20724 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20727 = (((m_op_index + adjust)
20728 / m_line_header->maximum_ops_per_instruction)
20729 * m_line_header->minimum_instruction_length);
20731 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20732 m_op_index = ((m_op_index + adjust)
20733 % m_line_header->maximum_ops_per_instruction);
20736 /* Ignore this record_line request. */
20739 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20744 /* Return non-zero if we should add LINE to the line number table.
20745 LINE is the line to add, LAST_LINE is the last line that was added,
20746 LAST_SUBFILE is the subfile for LAST_LINE.
20747 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20748 had a non-zero discriminator.
20750 We have to be careful in the presence of discriminators.
20751 E.g., for this line:
20753 for (i = 0; i < 100000; i++);
20755 clang can emit four line number entries for that one line,
20756 each with a different discriminator.
20757 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20759 However, we want gdb to coalesce all four entries into one.
20760 Otherwise the user could stepi into the middle of the line and
20761 gdb would get confused about whether the pc really was in the
20762 middle of the line.
20764 Things are further complicated by the fact that two consecutive
20765 line number entries for the same line is a heuristic used by gcc
20766 to denote the end of the prologue. So we can't just discard duplicate
20767 entries, we have to be selective about it. The heuristic we use is
20768 that we only collapse consecutive entries for the same line if at least
20769 one of those entries has a non-zero discriminator. PR 17276.
20771 Note: Addresses in the line number state machine can never go backwards
20772 within one sequence, thus this coalescing is ok. */
20775 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20776 int line_has_non_zero_discriminator,
20777 struct subfile *last_subfile)
20779 if (current_subfile != last_subfile)
20781 if (line != last_line)
20783 /* Same line for the same file that we've seen already.
20784 As a last check, for pr 17276, only record the line if the line
20785 has never had a non-zero discriminator. */
20786 if (!line_has_non_zero_discriminator)
20791 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20792 in the line table of subfile SUBFILE. */
20795 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20796 unsigned int line, CORE_ADDR address,
20797 record_line_ftype p_record_line)
20799 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20801 if (dwarf_line_debug)
20803 fprintf_unfiltered (gdb_stdlog,
20804 "Recording line %u, file %s, address %s\n",
20805 line, lbasename (subfile->name),
20806 paddress (gdbarch, address));
20809 (*p_record_line) (subfile, line, addr);
20812 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20813 Mark the end of a set of line number records.
20814 The arguments are the same as for dwarf_record_line_1.
20815 If SUBFILE is NULL the request is ignored. */
20818 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20819 CORE_ADDR address, record_line_ftype p_record_line)
20821 if (subfile == NULL)
20824 if (dwarf_line_debug)
20826 fprintf_unfiltered (gdb_stdlog,
20827 "Finishing current line, file %s, address %s\n",
20828 lbasename (subfile->name),
20829 paddress (gdbarch, address));
20832 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20836 lnp_state_machine::record_line (bool end_sequence)
20838 if (dwarf_line_debug)
20840 fprintf_unfiltered (gdb_stdlog,
20841 "Processing actual line %u: file %u,"
20842 " address %s, is_stmt %u, discrim %u\n",
20843 m_line, to_underlying (m_file),
20844 paddress (m_gdbarch, m_address),
20845 m_is_stmt, m_discriminator);
20848 file_entry *fe = current_file ();
20851 dwarf2_debug_line_missing_file_complaint ();
20852 /* For now we ignore lines not starting on an instruction boundary.
20853 But not when processing end_sequence for compatibility with the
20854 previous version of the code. */
20855 else if (m_op_index == 0 || end_sequence)
20857 fe->included_p = 1;
20858 if (m_record_lines_p && m_is_stmt)
20860 if (m_last_subfile != current_subfile || end_sequence)
20862 dwarf_finish_line (m_gdbarch, m_last_subfile,
20863 m_address, m_record_line_callback);
20868 if (dwarf_record_line_p (m_line, m_last_line,
20869 m_line_has_non_zero_discriminator,
20872 dwarf_record_line_1 (m_gdbarch, current_subfile,
20874 m_record_line_callback);
20876 m_last_subfile = current_subfile;
20877 m_last_line = m_line;
20883 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20884 bool record_lines_p)
20887 m_record_lines_p = record_lines_p;
20888 m_line_header = lh;
20890 m_record_line_callback = ::record_line;
20892 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20893 was a line entry for it so that the backend has a chance to adjust it
20894 and also record it in case it needs it. This is currently used by MIPS
20895 code, cf. `mips_adjust_dwarf2_line'. */
20896 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20897 m_is_stmt = lh->default_is_stmt;
20898 m_discriminator = 0;
20902 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20903 const gdb_byte *line_ptr,
20904 CORE_ADDR lowpc, CORE_ADDR address)
20906 /* If address < lowpc then it's not a usable value, it's outside the
20907 pc range of the CU. However, we restrict the test to only address
20908 values of zero to preserve GDB's previous behaviour which is to
20909 handle the specific case of a function being GC'd by the linker. */
20911 if (address == 0 && address < lowpc)
20913 /* This line table is for a function which has been
20914 GCd by the linker. Ignore it. PR gdb/12528 */
20916 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
20917 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20919 complaint (&symfile_complaints,
20920 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20921 line_offset, objfile_name (objfile));
20922 m_record_line_callback = noop_record_line;
20923 /* Note: record_line_callback is left as noop_record_line until
20924 we see DW_LNE_end_sequence. */
20928 /* Subroutine of dwarf_decode_lines to simplify it.
20929 Process the line number information in LH.
20930 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20931 program in order to set included_p for every referenced header. */
20934 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20935 const int decode_for_pst_p, CORE_ADDR lowpc)
20937 const gdb_byte *line_ptr, *extended_end;
20938 const gdb_byte *line_end;
20939 unsigned int bytes_read, extended_len;
20940 unsigned char op_code, extended_op;
20941 CORE_ADDR baseaddr;
20942 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
20943 bfd *abfd = objfile->obfd;
20944 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20945 /* True if we're recording line info (as opposed to building partial
20946 symtabs and just interested in finding include files mentioned by
20947 the line number program). */
20948 bool record_lines_p = !decode_for_pst_p;
20950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20952 line_ptr = lh->statement_program_start;
20953 line_end = lh->statement_program_end;
20955 /* Read the statement sequences until there's nothing left. */
20956 while (line_ptr < line_end)
20958 /* The DWARF line number program state machine. Reset the state
20959 machine at the start of each sequence. */
20960 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20961 bool end_sequence = false;
20963 if (record_lines_p)
20965 /* Start a subfile for the current file of the state
20967 const file_entry *fe = state_machine.current_file ();
20970 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20973 /* Decode the table. */
20974 while (line_ptr < line_end && !end_sequence)
20976 op_code = read_1_byte (abfd, line_ptr);
20979 if (op_code >= lh->opcode_base)
20981 /* Special opcode. */
20982 state_machine.handle_special_opcode (op_code);
20984 else switch (op_code)
20986 case DW_LNS_extended_op:
20987 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20989 line_ptr += bytes_read;
20990 extended_end = line_ptr + extended_len;
20991 extended_op = read_1_byte (abfd, line_ptr);
20993 switch (extended_op)
20995 case DW_LNE_end_sequence:
20996 state_machine.handle_end_sequence ();
20997 end_sequence = true;
20999 case DW_LNE_set_address:
21002 = read_address (abfd, line_ptr, cu, &bytes_read);
21003 line_ptr += bytes_read;
21005 state_machine.check_line_address (cu, line_ptr,
21007 state_machine.handle_set_address (baseaddr, address);
21010 case DW_LNE_define_file:
21012 const char *cur_file;
21013 unsigned int mod_time, length;
21016 cur_file = read_direct_string (abfd, line_ptr,
21018 line_ptr += bytes_read;
21019 dindex = (dir_index)
21020 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21021 line_ptr += bytes_read;
21023 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21024 line_ptr += bytes_read;
21026 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21027 line_ptr += bytes_read;
21028 lh->add_file_name (cur_file, dindex, mod_time, length);
21031 case DW_LNE_set_discriminator:
21033 /* The discriminator is not interesting to the
21034 debugger; just ignore it. We still need to
21035 check its value though:
21036 if there are consecutive entries for the same
21037 (non-prologue) line we want to coalesce them.
21040 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21041 line_ptr += bytes_read;
21043 state_machine.handle_set_discriminator (discr);
21047 complaint (&symfile_complaints,
21048 _("mangled .debug_line section"));
21051 /* Make sure that we parsed the extended op correctly. If e.g.
21052 we expected a different address size than the producer used,
21053 we may have read the wrong number of bytes. */
21054 if (line_ptr != extended_end)
21056 complaint (&symfile_complaints,
21057 _("mangled .debug_line section"));
21062 state_machine.handle_copy ();
21064 case DW_LNS_advance_pc:
21067 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21068 line_ptr += bytes_read;
21070 state_machine.handle_advance_pc (adjust);
21073 case DW_LNS_advance_line:
21076 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21077 line_ptr += bytes_read;
21079 state_machine.handle_advance_line (line_delta);
21082 case DW_LNS_set_file:
21084 file_name_index file
21085 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21087 line_ptr += bytes_read;
21089 state_machine.handle_set_file (file);
21092 case DW_LNS_set_column:
21093 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21094 line_ptr += bytes_read;
21096 case DW_LNS_negate_stmt:
21097 state_machine.handle_negate_stmt ();
21099 case DW_LNS_set_basic_block:
21101 /* Add to the address register of the state machine the
21102 address increment value corresponding to special opcode
21103 255. I.e., this value is scaled by the minimum
21104 instruction length since special opcode 255 would have
21105 scaled the increment. */
21106 case DW_LNS_const_add_pc:
21107 state_machine.handle_const_add_pc ();
21109 case DW_LNS_fixed_advance_pc:
21111 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21114 state_machine.handle_fixed_advance_pc (addr_adj);
21119 /* Unknown standard opcode, ignore it. */
21122 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21124 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21125 line_ptr += bytes_read;
21132 dwarf2_debug_line_missing_end_sequence_complaint ();
21134 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21135 in which case we still finish recording the last line). */
21136 state_machine.record_line (true);
21140 /* Decode the Line Number Program (LNP) for the given line_header
21141 structure and CU. The actual information extracted and the type
21142 of structures created from the LNP depends on the value of PST.
21144 1. If PST is NULL, then this procedure uses the data from the program
21145 to create all necessary symbol tables, and their linetables.
21147 2. If PST is not NULL, this procedure reads the program to determine
21148 the list of files included by the unit represented by PST, and
21149 builds all the associated partial symbol tables.
21151 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21152 It is used for relative paths in the line table.
21153 NOTE: When processing partial symtabs (pst != NULL),
21154 comp_dir == pst->dirname.
21156 NOTE: It is important that psymtabs have the same file name (via strcmp)
21157 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21158 symtab we don't use it in the name of the psymtabs we create.
21159 E.g. expand_line_sal requires this when finding psymtabs to expand.
21160 A good testcase for this is mb-inline.exp.
21162 LOWPC is the lowest address in CU (or 0 if not known).
21164 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21165 for its PC<->lines mapping information. Otherwise only the filename
21166 table is read in. */
21169 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21170 struct dwarf2_cu *cu, struct partial_symtab *pst,
21171 CORE_ADDR lowpc, int decode_mapping)
21173 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
21174 const int decode_for_pst_p = (pst != NULL);
21176 if (decode_mapping)
21177 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21179 if (decode_for_pst_p)
21183 /* Now that we're done scanning the Line Header Program, we can
21184 create the psymtab of each included file. */
21185 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21186 if (lh->file_names[file_index].included_p == 1)
21188 const char *include_name =
21189 psymtab_include_file_name (lh, file_index, pst, comp_dir);
21190 if (include_name != NULL)
21191 dwarf2_create_include_psymtab (include_name, pst, objfile);
21196 /* Make sure a symtab is created for every file, even files
21197 which contain only variables (i.e. no code with associated
21199 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21202 for (i = 0; i < lh->file_names.size (); i++)
21204 file_entry &fe = lh->file_names[i];
21206 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21208 if (current_subfile->symtab == NULL)
21210 current_subfile->symtab
21211 = allocate_symtab (cust, current_subfile->name);
21213 fe.symtab = current_subfile->symtab;
21218 /* Start a subfile for DWARF. FILENAME is the name of the file and
21219 DIRNAME the name of the source directory which contains FILENAME
21220 or NULL if not known.
21221 This routine tries to keep line numbers from identical absolute and
21222 relative file names in a common subfile.
21224 Using the `list' example from the GDB testsuite, which resides in
21225 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21226 of /srcdir/list0.c yields the following debugging information for list0.c:
21228 DW_AT_name: /srcdir/list0.c
21229 DW_AT_comp_dir: /compdir
21230 files.files[0].name: list0.h
21231 files.files[0].dir: /srcdir
21232 files.files[1].name: list0.c
21233 files.files[1].dir: /srcdir
21235 The line number information for list0.c has to end up in a single
21236 subfile, so that `break /srcdir/list0.c:1' works as expected.
21237 start_subfile will ensure that this happens provided that we pass the
21238 concatenation of files.files[1].dir and files.files[1].name as the
21242 dwarf2_start_subfile (const char *filename, const char *dirname)
21246 /* In order not to lose the line information directory,
21247 we concatenate it to the filename when it makes sense.
21248 Note that the Dwarf3 standard says (speaking of filenames in line
21249 information): ``The directory index is ignored for file names
21250 that represent full path names''. Thus ignoring dirname in the
21251 `else' branch below isn't an issue. */
21253 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21255 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21259 start_subfile (filename);
21265 /* Start a symtab for DWARF.
21266 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21268 static struct compunit_symtab *
21269 dwarf2_start_symtab (struct dwarf2_cu *cu,
21270 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21272 struct compunit_symtab *cust
21273 = start_symtab (cu->dwarf2_per_objfile->objfile, name, comp_dir, low_pc,
21276 record_debugformat ("DWARF 2");
21277 record_producer (cu->producer);
21279 /* We assume that we're processing GCC output. */
21280 processing_gcc_compilation = 2;
21282 cu->processing_has_namespace_info = 0;
21288 var_decode_location (struct attribute *attr, struct symbol *sym,
21289 struct dwarf2_cu *cu)
21291 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
21292 struct comp_unit_head *cu_header = &cu->header;
21294 /* NOTE drow/2003-01-30: There used to be a comment and some special
21295 code here to turn a symbol with DW_AT_external and a
21296 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21297 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21298 with some versions of binutils) where shared libraries could have
21299 relocations against symbols in their debug information - the
21300 minimal symbol would have the right address, but the debug info
21301 would not. It's no longer necessary, because we will explicitly
21302 apply relocations when we read in the debug information now. */
21304 /* A DW_AT_location attribute with no contents indicates that a
21305 variable has been optimized away. */
21306 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21308 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21312 /* Handle one degenerate form of location expression specially, to
21313 preserve GDB's previous behavior when section offsets are
21314 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21315 then mark this symbol as LOC_STATIC. */
21317 if (attr_form_is_block (attr)
21318 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21319 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21320 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21321 && (DW_BLOCK (attr)->size
21322 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21324 unsigned int dummy;
21326 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21327 SYMBOL_VALUE_ADDRESS (sym) =
21328 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21330 SYMBOL_VALUE_ADDRESS (sym) =
21331 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21332 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21333 fixup_symbol_section (sym, objfile);
21334 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21335 SYMBOL_SECTION (sym));
21339 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21340 expression evaluator, and use LOC_COMPUTED only when necessary
21341 (i.e. when the value of a register or memory location is
21342 referenced, or a thread-local block, etc.). Then again, it might
21343 not be worthwhile. I'm assuming that it isn't unless performance
21344 or memory numbers show me otherwise. */
21346 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21348 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21349 cu->has_loclist = 1;
21352 /* Given a pointer to a DWARF information entry, figure out if we need
21353 to make a symbol table entry for it, and if so, create a new entry
21354 and return a pointer to it.
21355 If TYPE is NULL, determine symbol type from the die, otherwise
21356 used the passed type.
21357 If SPACE is not NULL, use it to hold the new symbol. If it is
21358 NULL, allocate a new symbol on the objfile's obstack. */
21360 static struct symbol *
21361 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21362 struct symbol *space)
21364 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
21365 struct objfile *objfile = dwarf2_per_objfile->objfile;
21366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21367 struct symbol *sym = NULL;
21369 struct attribute *attr = NULL;
21370 struct attribute *attr2 = NULL;
21371 CORE_ADDR baseaddr;
21372 struct pending **list_to_add = NULL;
21374 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21376 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21378 name = dwarf2_name (die, cu);
21381 const char *linkagename;
21382 int suppress_add = 0;
21387 sym = allocate_symbol (objfile);
21388 OBJSTAT (objfile, n_syms++);
21390 /* Cache this symbol's name and the name's demangled form (if any). */
21391 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21392 linkagename = dwarf2_physname (name, die, cu);
21393 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21395 /* Fortran does not have mangling standard and the mangling does differ
21396 between gfortran, iFort etc. */
21397 if (cu->language == language_fortran
21398 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21399 symbol_set_demangled_name (&(sym->ginfo),
21400 dwarf2_full_name (name, die, cu),
21403 /* Default assumptions.
21404 Use the passed type or decode it from the die. */
21405 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21406 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21408 SYMBOL_TYPE (sym) = type;
21410 SYMBOL_TYPE (sym) = die_type (die, cu);
21411 attr = dwarf2_attr (die,
21412 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21416 SYMBOL_LINE (sym) = DW_UNSND (attr);
21419 attr = dwarf2_attr (die,
21420 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21424 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21425 struct file_entry *fe;
21427 if (cu->line_header != NULL)
21428 fe = cu->line_header->file_name_at (file_index);
21433 complaint (&symfile_complaints,
21434 _("file index out of range"));
21436 symbol_set_symtab (sym, fe->symtab);
21442 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21447 addr = attr_value_as_address (attr);
21448 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21449 SYMBOL_VALUE_ADDRESS (sym) = addr;
21451 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21452 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21453 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21454 add_symbol_to_list (sym, cu->list_in_scope);
21456 case DW_TAG_subprogram:
21457 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21459 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21460 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21461 if ((attr2 && (DW_UNSND (attr2) != 0))
21462 || cu->language == language_ada)
21464 /* Subprograms marked external are stored as a global symbol.
21465 Ada subprograms, whether marked external or not, are always
21466 stored as a global symbol, because we want to be able to
21467 access them globally. For instance, we want to be able
21468 to break on a nested subprogram without having to
21469 specify the context. */
21470 list_to_add = &global_symbols;
21474 list_to_add = cu->list_in_scope;
21477 case DW_TAG_inlined_subroutine:
21478 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21480 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21481 SYMBOL_INLINED (sym) = 1;
21482 list_to_add = cu->list_in_scope;
21484 case DW_TAG_template_value_param:
21486 /* Fall through. */
21487 case DW_TAG_constant:
21488 case DW_TAG_variable:
21489 case DW_TAG_member:
21490 /* Compilation with minimal debug info may result in
21491 variables with missing type entries. Change the
21492 misleading `void' type to something sensible. */
21493 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21494 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21496 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21497 /* In the case of DW_TAG_member, we should only be called for
21498 static const members. */
21499 if (die->tag == DW_TAG_member)
21501 /* dwarf2_add_field uses die_is_declaration,
21502 so we do the same. */
21503 gdb_assert (die_is_declaration (die, cu));
21508 dwarf2_const_value (attr, sym, cu);
21509 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21512 if (attr2 && (DW_UNSND (attr2) != 0))
21513 list_to_add = &global_symbols;
21515 list_to_add = cu->list_in_scope;
21519 attr = dwarf2_attr (die, DW_AT_location, cu);
21522 var_decode_location (attr, sym, cu);
21523 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21525 /* Fortran explicitly imports any global symbols to the local
21526 scope by DW_TAG_common_block. */
21527 if (cu->language == language_fortran && die->parent
21528 && die->parent->tag == DW_TAG_common_block)
21531 if (SYMBOL_CLASS (sym) == LOC_STATIC
21532 && SYMBOL_VALUE_ADDRESS (sym) == 0
21533 && !dwarf2_per_objfile->has_section_at_zero)
21535 /* When a static variable is eliminated by the linker,
21536 the corresponding debug information is not stripped
21537 out, but the variable address is set to null;
21538 do not add such variables into symbol table. */
21540 else if (attr2 && (DW_UNSND (attr2) != 0))
21542 /* Workaround gfortran PR debug/40040 - it uses
21543 DW_AT_location for variables in -fPIC libraries which may
21544 get overriden by other libraries/executable and get
21545 a different address. Resolve it by the minimal symbol
21546 which may come from inferior's executable using copy
21547 relocation. Make this workaround only for gfortran as for
21548 other compilers GDB cannot guess the minimal symbol
21549 Fortran mangling kind. */
21550 if (cu->language == language_fortran && die->parent
21551 && die->parent->tag == DW_TAG_module
21553 && startswith (cu->producer, "GNU Fortran"))
21554 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21556 /* A variable with DW_AT_external is never static,
21557 but it may be block-scoped. */
21558 list_to_add = (cu->list_in_scope == &file_symbols
21559 ? &global_symbols : cu->list_in_scope);
21562 list_to_add = cu->list_in_scope;
21566 /* We do not know the address of this symbol.
21567 If it is an external symbol and we have type information
21568 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21569 The address of the variable will then be determined from
21570 the minimal symbol table whenever the variable is
21572 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21574 /* Fortran explicitly imports any global symbols to the local
21575 scope by DW_TAG_common_block. */
21576 if (cu->language == language_fortran && die->parent
21577 && die->parent->tag == DW_TAG_common_block)
21579 /* SYMBOL_CLASS doesn't matter here because
21580 read_common_block is going to reset it. */
21582 list_to_add = cu->list_in_scope;
21584 else if (attr2 && (DW_UNSND (attr2) != 0)
21585 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21587 /* A variable with DW_AT_external is never static, but it
21588 may be block-scoped. */
21589 list_to_add = (cu->list_in_scope == &file_symbols
21590 ? &global_symbols : cu->list_in_scope);
21592 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21594 else if (!die_is_declaration (die, cu))
21596 /* Use the default LOC_OPTIMIZED_OUT class. */
21597 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21599 list_to_add = cu->list_in_scope;
21603 case DW_TAG_formal_parameter:
21604 /* If we are inside a function, mark this as an argument. If
21605 not, we might be looking at an argument to an inlined function
21606 when we do not have enough information to show inlined frames;
21607 pretend it's a local variable in that case so that the user can
21609 if (context_stack_depth > 0
21610 && context_stack[context_stack_depth - 1].name != NULL)
21611 SYMBOL_IS_ARGUMENT (sym) = 1;
21612 attr = dwarf2_attr (die, DW_AT_location, cu);
21615 var_decode_location (attr, sym, cu);
21617 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21620 dwarf2_const_value (attr, sym, cu);
21623 list_to_add = cu->list_in_scope;
21625 case DW_TAG_unspecified_parameters:
21626 /* From varargs functions; gdb doesn't seem to have any
21627 interest in this information, so just ignore it for now.
21630 case DW_TAG_template_type_param:
21632 /* Fall through. */
21633 case DW_TAG_class_type:
21634 case DW_TAG_interface_type:
21635 case DW_TAG_structure_type:
21636 case DW_TAG_union_type:
21637 case DW_TAG_set_type:
21638 case DW_TAG_enumeration_type:
21639 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21640 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21643 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21644 really ever be static objects: otherwise, if you try
21645 to, say, break of a class's method and you're in a file
21646 which doesn't mention that class, it won't work unless
21647 the check for all static symbols in lookup_symbol_aux
21648 saves you. See the OtherFileClass tests in
21649 gdb.c++/namespace.exp. */
21653 list_to_add = (cu->list_in_scope == &file_symbols
21654 && cu->language == language_cplus
21655 ? &global_symbols : cu->list_in_scope);
21657 /* The semantics of C++ state that "struct foo {
21658 ... }" also defines a typedef for "foo". */
21659 if (cu->language == language_cplus
21660 || cu->language == language_ada
21661 || cu->language == language_d
21662 || cu->language == language_rust)
21664 /* The symbol's name is already allocated along
21665 with this objfile, so we don't need to
21666 duplicate it for the type. */
21667 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21668 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21673 case DW_TAG_typedef:
21674 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21675 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21676 list_to_add = cu->list_in_scope;
21678 case DW_TAG_base_type:
21679 case DW_TAG_subrange_type:
21680 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21681 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21682 list_to_add = cu->list_in_scope;
21684 case DW_TAG_enumerator:
21685 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21688 dwarf2_const_value (attr, sym, cu);
21691 /* NOTE: carlton/2003-11-10: See comment above in the
21692 DW_TAG_class_type, etc. block. */
21694 list_to_add = (cu->list_in_scope == &file_symbols
21695 && cu->language == language_cplus
21696 ? &global_symbols : cu->list_in_scope);
21699 case DW_TAG_imported_declaration:
21700 case DW_TAG_namespace:
21701 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21702 list_to_add = &global_symbols;
21704 case DW_TAG_module:
21705 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21706 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21707 list_to_add = &global_symbols;
21709 case DW_TAG_common_block:
21710 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21711 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21712 add_symbol_to_list (sym, cu->list_in_scope);
21715 /* Not a tag we recognize. Hopefully we aren't processing
21716 trash data, but since we must specifically ignore things
21717 we don't recognize, there is nothing else we should do at
21719 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21720 dwarf_tag_name (die->tag));
21726 sym->hash_next = objfile->template_symbols;
21727 objfile->template_symbols = sym;
21728 list_to_add = NULL;
21731 if (list_to_add != NULL)
21732 add_symbol_to_list (sym, list_to_add);
21734 /* For the benefit of old versions of GCC, check for anonymous
21735 namespaces based on the demangled name. */
21736 if (!cu->processing_has_namespace_info
21737 && cu->language == language_cplus)
21738 cp_scan_for_anonymous_namespaces (sym, objfile);
21743 /* A wrapper for new_symbol_full that always allocates a new symbol. */
21745 static struct symbol *
21746 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21748 return new_symbol_full (die, type, cu, NULL);
21751 /* Given an attr with a DW_FORM_dataN value in host byte order,
21752 zero-extend it as appropriate for the symbol's type. The DWARF
21753 standard (v4) is not entirely clear about the meaning of using
21754 DW_FORM_dataN for a constant with a signed type, where the type is
21755 wider than the data. The conclusion of a discussion on the DWARF
21756 list was that this is unspecified. We choose to always zero-extend
21757 because that is the interpretation long in use by GCC. */
21760 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21761 struct dwarf2_cu *cu, LONGEST *value, int bits)
21763 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
21764 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21765 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21766 LONGEST l = DW_UNSND (attr);
21768 if (bits < sizeof (*value) * 8)
21770 l &= ((LONGEST) 1 << bits) - 1;
21773 else if (bits == sizeof (*value) * 8)
21777 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21778 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21785 /* Read a constant value from an attribute. Either set *VALUE, or if
21786 the value does not fit in *VALUE, set *BYTES - either already
21787 allocated on the objfile obstack, or newly allocated on OBSTACK,
21788 or, set *BATON, if we translated the constant to a location
21792 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21793 const char *name, struct obstack *obstack,
21794 struct dwarf2_cu *cu,
21795 LONGEST *value, const gdb_byte **bytes,
21796 struct dwarf2_locexpr_baton **baton)
21798 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
21799 struct comp_unit_head *cu_header = &cu->header;
21800 struct dwarf_block *blk;
21801 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21802 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21808 switch (attr->form)
21811 case DW_FORM_GNU_addr_index:
21815 if (TYPE_LENGTH (type) != cu_header->addr_size)
21816 dwarf2_const_value_length_mismatch_complaint (name,
21817 cu_header->addr_size,
21818 TYPE_LENGTH (type));
21819 /* Symbols of this form are reasonably rare, so we just
21820 piggyback on the existing location code rather than writing
21821 a new implementation of symbol_computed_ops. */
21822 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21823 (*baton)->per_cu = cu->per_cu;
21824 gdb_assert ((*baton)->per_cu);
21826 (*baton)->size = 2 + cu_header->addr_size;
21827 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21828 (*baton)->data = data;
21830 data[0] = DW_OP_addr;
21831 store_unsigned_integer (&data[1], cu_header->addr_size,
21832 byte_order, DW_ADDR (attr));
21833 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21836 case DW_FORM_string:
21838 case DW_FORM_GNU_str_index:
21839 case DW_FORM_GNU_strp_alt:
21840 /* DW_STRING is already allocated on the objfile obstack, point
21842 *bytes = (const gdb_byte *) DW_STRING (attr);
21844 case DW_FORM_block1:
21845 case DW_FORM_block2:
21846 case DW_FORM_block4:
21847 case DW_FORM_block:
21848 case DW_FORM_exprloc:
21849 case DW_FORM_data16:
21850 blk = DW_BLOCK (attr);
21851 if (TYPE_LENGTH (type) != blk->size)
21852 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21853 TYPE_LENGTH (type));
21854 *bytes = blk->data;
21857 /* The DW_AT_const_value attributes are supposed to carry the
21858 symbol's value "represented as it would be on the target
21859 architecture." By the time we get here, it's already been
21860 converted to host endianness, so we just need to sign- or
21861 zero-extend it as appropriate. */
21862 case DW_FORM_data1:
21863 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21865 case DW_FORM_data2:
21866 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21868 case DW_FORM_data4:
21869 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21871 case DW_FORM_data8:
21872 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21875 case DW_FORM_sdata:
21876 case DW_FORM_implicit_const:
21877 *value = DW_SND (attr);
21880 case DW_FORM_udata:
21881 *value = DW_UNSND (attr);
21885 complaint (&symfile_complaints,
21886 _("unsupported const value attribute form: '%s'"),
21887 dwarf_form_name (attr->form));
21894 /* Copy constant value from an attribute to a symbol. */
21897 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21898 struct dwarf2_cu *cu)
21900 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
21902 const gdb_byte *bytes;
21903 struct dwarf2_locexpr_baton *baton;
21905 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21906 SYMBOL_PRINT_NAME (sym),
21907 &objfile->objfile_obstack, cu,
21908 &value, &bytes, &baton);
21912 SYMBOL_LOCATION_BATON (sym) = baton;
21913 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21915 else if (bytes != NULL)
21917 SYMBOL_VALUE_BYTES (sym) = bytes;
21918 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21922 SYMBOL_VALUE (sym) = value;
21923 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21927 /* Return the type of the die in question using its DW_AT_type attribute. */
21929 static struct type *
21930 die_type (struct die_info *die, struct dwarf2_cu *cu)
21932 struct attribute *type_attr;
21934 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21937 /* A missing DW_AT_type represents a void type. */
21938 return objfile_type (cu->dwarf2_per_objfile->objfile)->builtin_void;
21941 return lookup_die_type (die, type_attr, cu);
21944 /* True iff CU's producer generates GNAT Ada auxiliary information
21945 that allows to find parallel types through that information instead
21946 of having to do expensive parallel lookups by type name. */
21949 need_gnat_info (struct dwarf2_cu *cu)
21951 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21952 of GNAT produces this auxiliary information, without any indication
21953 that it is produced. Part of enhancing the FSF version of GNAT
21954 to produce that information will be to put in place an indicator
21955 that we can use in order to determine whether the descriptive type
21956 info is available or not. One suggestion that has been made is
21957 to use a new attribute, attached to the CU die. For now, assume
21958 that the descriptive type info is not available. */
21962 /* Return the auxiliary type of the die in question using its
21963 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21964 attribute is not present. */
21966 static struct type *
21967 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21969 struct attribute *type_attr;
21971 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21975 return lookup_die_type (die, type_attr, cu);
21978 /* If DIE has a descriptive_type attribute, then set the TYPE's
21979 descriptive type accordingly. */
21982 set_descriptive_type (struct type *type, struct die_info *die,
21983 struct dwarf2_cu *cu)
21985 struct type *descriptive_type = die_descriptive_type (die, cu);
21987 if (descriptive_type)
21989 ALLOCATE_GNAT_AUX_TYPE (type);
21990 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21994 /* Return the containing type of the die in question using its
21995 DW_AT_containing_type attribute. */
21997 static struct type *
21998 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22000 struct attribute *type_attr;
22002 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22004 error (_("Dwarf Error: Problem turning containing type into gdb type "
22005 "[in module %s]"), objfile_name (cu->dwarf2_per_objfile->objfile));
22007 return lookup_die_type (die, type_attr, cu);
22010 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22012 static struct type *
22013 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22015 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
22016 struct objfile *objfile = dwarf2_per_objfile->objfile;
22017 char *message, *saved;
22019 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
22020 objfile_name (objfile),
22021 to_underlying (cu->header.sect_off),
22022 to_underlying (die->sect_off));
22023 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22024 message, strlen (message));
22027 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22030 /* Look up the type of DIE in CU using its type attribute ATTR.
22031 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22032 DW_AT_containing_type.
22033 If there is no type substitute an error marker. */
22035 static struct type *
22036 lookup_die_type (struct die_info *die, const struct attribute *attr,
22037 struct dwarf2_cu *cu)
22039 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
22040 struct objfile *objfile = dwarf2_per_objfile->objfile;
22041 struct type *this_type;
22043 gdb_assert (attr->name == DW_AT_type
22044 || attr->name == DW_AT_GNAT_descriptive_type
22045 || attr->name == DW_AT_containing_type);
22047 /* First see if we have it cached. */
22049 if (attr->form == DW_FORM_GNU_ref_alt)
22051 struct dwarf2_per_cu_data *per_cu;
22052 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22054 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22055 dwarf2_per_objfile);
22056 this_type = get_die_type_at_offset (sect_off, per_cu);
22058 else if (attr_form_is_ref (attr))
22060 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22062 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22064 else if (attr->form == DW_FORM_ref_sig8)
22066 ULONGEST signature = DW_SIGNATURE (attr);
22068 return get_signatured_type (die, signature, cu);
22072 complaint (&symfile_complaints,
22073 _("Dwarf Error: Bad type attribute %s in DIE"
22074 " at 0x%x [in module %s]"),
22075 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
22076 objfile_name (objfile));
22077 return build_error_marker_type (cu, die);
22080 /* If not cached we need to read it in. */
22082 if (this_type == NULL)
22084 struct die_info *type_die = NULL;
22085 struct dwarf2_cu *type_cu = cu;
22087 if (attr_form_is_ref (attr))
22088 type_die = follow_die_ref (die, attr, &type_cu);
22089 if (type_die == NULL)
22090 return build_error_marker_type (cu, die);
22091 /* If we find the type now, it's probably because the type came
22092 from an inter-CU reference and the type's CU got expanded before
22094 this_type = read_type_die (type_die, type_cu);
22097 /* If we still don't have a type use an error marker. */
22099 if (this_type == NULL)
22100 return build_error_marker_type (cu, die);
22105 /* Return the type in DIE, CU.
22106 Returns NULL for invalid types.
22108 This first does a lookup in die_type_hash,
22109 and only reads the die in if necessary.
22111 NOTE: This can be called when reading in partial or full symbols. */
22113 static struct type *
22114 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22116 struct type *this_type;
22118 this_type = get_die_type (die, cu);
22122 return read_type_die_1 (die, cu);
22125 /* Read the type in DIE, CU.
22126 Returns NULL for invalid types. */
22128 static struct type *
22129 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22131 struct type *this_type = NULL;
22135 case DW_TAG_class_type:
22136 case DW_TAG_interface_type:
22137 case DW_TAG_structure_type:
22138 case DW_TAG_union_type:
22139 this_type = read_structure_type (die, cu);
22141 case DW_TAG_enumeration_type:
22142 this_type = read_enumeration_type (die, cu);
22144 case DW_TAG_subprogram:
22145 case DW_TAG_subroutine_type:
22146 case DW_TAG_inlined_subroutine:
22147 this_type = read_subroutine_type (die, cu);
22149 case DW_TAG_array_type:
22150 this_type = read_array_type (die, cu);
22152 case DW_TAG_set_type:
22153 this_type = read_set_type (die, cu);
22155 case DW_TAG_pointer_type:
22156 this_type = read_tag_pointer_type (die, cu);
22158 case DW_TAG_ptr_to_member_type:
22159 this_type = read_tag_ptr_to_member_type (die, cu);
22161 case DW_TAG_reference_type:
22162 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22164 case DW_TAG_rvalue_reference_type:
22165 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22167 case DW_TAG_const_type:
22168 this_type = read_tag_const_type (die, cu);
22170 case DW_TAG_volatile_type:
22171 this_type = read_tag_volatile_type (die, cu);
22173 case DW_TAG_restrict_type:
22174 this_type = read_tag_restrict_type (die, cu);
22176 case DW_TAG_string_type:
22177 this_type = read_tag_string_type (die, cu);
22179 case DW_TAG_typedef:
22180 this_type = read_typedef (die, cu);
22182 case DW_TAG_subrange_type:
22183 this_type = read_subrange_type (die, cu);
22185 case DW_TAG_base_type:
22186 this_type = read_base_type (die, cu);
22188 case DW_TAG_unspecified_type:
22189 this_type = read_unspecified_type (die, cu);
22191 case DW_TAG_namespace:
22192 this_type = read_namespace_type (die, cu);
22194 case DW_TAG_module:
22195 this_type = read_module_type (die, cu);
22197 case DW_TAG_atomic_type:
22198 this_type = read_tag_atomic_type (die, cu);
22201 complaint (&symfile_complaints,
22202 _("unexpected tag in read_type_die: '%s'"),
22203 dwarf_tag_name (die->tag));
22210 /* See if we can figure out if the class lives in a namespace. We do
22211 this by looking for a member function; its demangled name will
22212 contain namespace info, if there is any.
22213 Return the computed name or NULL.
22214 Space for the result is allocated on the objfile's obstack.
22215 This is the full-die version of guess_partial_die_structure_name.
22216 In this case we know DIE has no useful parent. */
22219 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22221 struct die_info *spec_die;
22222 struct dwarf2_cu *spec_cu;
22223 struct die_info *child;
22224 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
22227 spec_die = die_specification (die, &spec_cu);
22228 if (spec_die != NULL)
22234 for (child = die->child;
22236 child = child->sibling)
22238 if (child->tag == DW_TAG_subprogram)
22240 const char *linkage_name = dw2_linkage_name (child, cu);
22242 if (linkage_name != NULL)
22245 = language_class_name_from_physname (cu->language_defn,
22249 if (actual_name != NULL)
22251 const char *die_name = dwarf2_name (die, cu);
22253 if (die_name != NULL
22254 && strcmp (die_name, actual_name) != 0)
22256 /* Strip off the class name from the full name.
22257 We want the prefix. */
22258 int die_name_len = strlen (die_name);
22259 int actual_name_len = strlen (actual_name);
22261 /* Test for '::' as a sanity check. */
22262 if (actual_name_len > die_name_len + 2
22263 && actual_name[actual_name_len
22264 - die_name_len - 1] == ':')
22265 name = (char *) obstack_copy0 (
22266 &objfile->per_bfd->storage_obstack,
22267 actual_name, actual_name_len - die_name_len - 2);
22270 xfree (actual_name);
22279 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22280 prefix part in such case. See
22281 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22283 static const char *
22284 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22286 struct attribute *attr;
22289 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22290 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22293 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22296 attr = dw2_linkage_name_attr (die, cu);
22297 if (attr == NULL || DW_STRING (attr) == NULL)
22300 /* dwarf2_name had to be already called. */
22301 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22303 /* Strip the base name, keep any leading namespaces/classes. */
22304 base = strrchr (DW_STRING (attr), ':');
22305 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22308 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
22309 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22311 &base[-1] - DW_STRING (attr));
22314 /* Return the name of the namespace/class that DIE is defined within,
22315 or "" if we can't tell. The caller should not xfree the result.
22317 For example, if we're within the method foo() in the following
22327 then determine_prefix on foo's die will return "N::C". */
22329 static const char *
22330 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22332 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
22333 struct die_info *parent, *spec_die;
22334 struct dwarf2_cu *spec_cu;
22335 struct type *parent_type;
22336 const char *retval;
22338 if (cu->language != language_cplus
22339 && cu->language != language_fortran && cu->language != language_d
22340 && cu->language != language_rust)
22343 retval = anonymous_struct_prefix (die, cu);
22347 /* We have to be careful in the presence of DW_AT_specification.
22348 For example, with GCC 3.4, given the code
22352 // Definition of N::foo.
22356 then we'll have a tree of DIEs like this:
22358 1: DW_TAG_compile_unit
22359 2: DW_TAG_namespace // N
22360 3: DW_TAG_subprogram // declaration of N::foo
22361 4: DW_TAG_subprogram // definition of N::foo
22362 DW_AT_specification // refers to die #3
22364 Thus, when processing die #4, we have to pretend that we're in
22365 the context of its DW_AT_specification, namely the contex of die
22368 spec_die = die_specification (die, &spec_cu);
22369 if (spec_die == NULL)
22370 parent = die->parent;
22373 parent = spec_die->parent;
22377 if (parent == NULL)
22379 else if (parent->building_fullname)
22382 const char *parent_name;
22384 /* It has been seen on RealView 2.2 built binaries,
22385 DW_TAG_template_type_param types actually _defined_ as
22386 children of the parent class:
22389 template class <class Enum> Class{};
22390 Class<enum E> class_e;
22392 1: DW_TAG_class_type (Class)
22393 2: DW_TAG_enumeration_type (E)
22394 3: DW_TAG_enumerator (enum1:0)
22395 3: DW_TAG_enumerator (enum2:1)
22397 2: DW_TAG_template_type_param
22398 DW_AT_type DW_FORM_ref_udata (E)
22400 Besides being broken debug info, it can put GDB into an
22401 infinite loop. Consider:
22403 When we're building the full name for Class<E>, we'll start
22404 at Class, and go look over its template type parameters,
22405 finding E. We'll then try to build the full name of E, and
22406 reach here. We're now trying to build the full name of E,
22407 and look over the parent DIE for containing scope. In the
22408 broken case, if we followed the parent DIE of E, we'd again
22409 find Class, and once again go look at its template type
22410 arguments, etc., etc. Simply don't consider such parent die
22411 as source-level parent of this die (it can't be, the language
22412 doesn't allow it), and break the loop here. */
22413 name = dwarf2_name (die, cu);
22414 parent_name = dwarf2_name (parent, cu);
22415 complaint (&symfile_complaints,
22416 _("template param type '%s' defined within parent '%s'"),
22417 name ? name : "<unknown>",
22418 parent_name ? parent_name : "<unknown>");
22422 switch (parent->tag)
22424 case DW_TAG_namespace:
22425 parent_type = read_type_die (parent, cu);
22426 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22427 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22428 Work around this problem here. */
22429 if (cu->language == language_cplus
22430 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22432 /* We give a name to even anonymous namespaces. */
22433 return TYPE_TAG_NAME (parent_type);
22434 case DW_TAG_class_type:
22435 case DW_TAG_interface_type:
22436 case DW_TAG_structure_type:
22437 case DW_TAG_union_type:
22438 case DW_TAG_module:
22439 parent_type = read_type_die (parent, cu);
22440 if (TYPE_TAG_NAME (parent_type) != NULL)
22441 return TYPE_TAG_NAME (parent_type);
22443 /* An anonymous structure is only allowed non-static data
22444 members; no typedefs, no member functions, et cetera.
22445 So it does not need a prefix. */
22447 case DW_TAG_compile_unit:
22448 case DW_TAG_partial_unit:
22449 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22450 if (cu->language == language_cplus
22451 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22452 && die->child != NULL
22453 && (die->tag == DW_TAG_class_type
22454 || die->tag == DW_TAG_structure_type
22455 || die->tag == DW_TAG_union_type))
22457 char *name = guess_full_die_structure_name (die, cu);
22462 case DW_TAG_enumeration_type:
22463 parent_type = read_type_die (parent, cu);
22464 if (TYPE_DECLARED_CLASS (parent_type))
22466 if (TYPE_TAG_NAME (parent_type) != NULL)
22467 return TYPE_TAG_NAME (parent_type);
22470 /* Fall through. */
22472 return determine_prefix (parent, cu);
22476 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22477 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22478 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22479 an obconcat, otherwise allocate storage for the result. The CU argument is
22480 used to determine the language and hence, the appropriate separator. */
22482 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22485 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22486 int physname, struct dwarf2_cu *cu)
22488 const char *lead = "";
22491 if (suffix == NULL || suffix[0] == '\0'
22492 || prefix == NULL || prefix[0] == '\0')
22494 else if (cu->language == language_d)
22496 /* For D, the 'main' function could be defined in any module, but it
22497 should never be prefixed. */
22498 if (strcmp (suffix, "D main") == 0)
22506 else if (cu->language == language_fortran && physname)
22508 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22509 DW_AT_MIPS_linkage_name is preferred and used instead. */
22517 if (prefix == NULL)
22519 if (suffix == NULL)
22526 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22528 strcpy (retval, lead);
22529 strcat (retval, prefix);
22530 strcat (retval, sep);
22531 strcat (retval, suffix);
22536 /* We have an obstack. */
22537 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22541 /* Return sibling of die, NULL if no sibling. */
22543 static struct die_info *
22544 sibling_die (struct die_info *die)
22546 return die->sibling;
22549 /* Get name of a die, return NULL if not found. */
22551 static const char *
22552 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22553 struct obstack *obstack)
22555 if (name && cu->language == language_cplus)
22557 std::string canon_name = cp_canonicalize_string (name);
22559 if (!canon_name.empty ())
22561 if (canon_name != name)
22562 name = (const char *) obstack_copy0 (obstack,
22563 canon_name.c_str (),
22564 canon_name.length ());
22571 /* Get name of a die, return NULL if not found.
22572 Anonymous namespaces are converted to their magic string. */
22574 static const char *
22575 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22577 struct attribute *attr;
22578 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
22580 attr = dwarf2_attr (die, DW_AT_name, cu);
22581 if ((!attr || !DW_STRING (attr))
22582 && die->tag != DW_TAG_namespace
22583 && die->tag != DW_TAG_class_type
22584 && die->tag != DW_TAG_interface_type
22585 && die->tag != DW_TAG_structure_type
22586 && die->tag != DW_TAG_union_type)
22591 case DW_TAG_compile_unit:
22592 case DW_TAG_partial_unit:
22593 /* Compilation units have a DW_AT_name that is a filename, not
22594 a source language identifier. */
22595 case DW_TAG_enumeration_type:
22596 case DW_TAG_enumerator:
22597 /* These tags always have simple identifiers already; no need
22598 to canonicalize them. */
22599 return DW_STRING (attr);
22601 case DW_TAG_namespace:
22602 if (attr != NULL && DW_STRING (attr) != NULL)
22603 return DW_STRING (attr);
22604 return CP_ANONYMOUS_NAMESPACE_STR;
22606 case DW_TAG_class_type:
22607 case DW_TAG_interface_type:
22608 case DW_TAG_structure_type:
22609 case DW_TAG_union_type:
22610 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22611 structures or unions. These were of the form "._%d" in GCC 4.1,
22612 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22613 and GCC 4.4. We work around this problem by ignoring these. */
22614 if (attr && DW_STRING (attr)
22615 && (startswith (DW_STRING (attr), "._")
22616 || startswith (DW_STRING (attr), "<anonymous")))
22619 /* GCC might emit a nameless typedef that has a linkage name. See
22620 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22621 if (!attr || DW_STRING (attr) == NULL)
22623 char *demangled = NULL;
22625 attr = dw2_linkage_name_attr (die, cu);
22626 if (attr == NULL || DW_STRING (attr) == NULL)
22629 /* Avoid demangling DW_STRING (attr) the second time on a second
22630 call for the same DIE. */
22631 if (!DW_STRING_IS_CANONICAL (attr))
22632 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22638 /* FIXME: we already did this for the partial symbol... */
22641 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22642 demangled, strlen (demangled)));
22643 DW_STRING_IS_CANONICAL (attr) = 1;
22646 /* Strip any leading namespaces/classes, keep only the base name.
22647 DW_AT_name for named DIEs does not contain the prefixes. */
22648 base = strrchr (DW_STRING (attr), ':');
22649 if (base && base > DW_STRING (attr) && base[-1] == ':')
22652 return DW_STRING (attr);
22661 if (!DW_STRING_IS_CANONICAL (attr))
22664 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22665 &objfile->per_bfd->storage_obstack);
22666 DW_STRING_IS_CANONICAL (attr) = 1;
22668 return DW_STRING (attr);
22671 /* Return the die that this die in an extension of, or NULL if there
22672 is none. *EXT_CU is the CU containing DIE on input, and the CU
22673 containing the return value on output. */
22675 static struct die_info *
22676 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22678 struct attribute *attr;
22680 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22684 return follow_die_ref (die, attr, ext_cu);
22687 /* Convert a DIE tag into its string name. */
22689 static const char *
22690 dwarf_tag_name (unsigned tag)
22692 const char *name = get_DW_TAG_name (tag);
22695 return "DW_TAG_<unknown>";
22700 /* Convert a DWARF attribute code into its string name. */
22702 static const char *
22703 dwarf_attr_name (unsigned attr)
22707 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22708 if (attr == DW_AT_MIPS_fde)
22709 return "DW_AT_MIPS_fde";
22711 if (attr == DW_AT_HP_block_index)
22712 return "DW_AT_HP_block_index";
22715 name = get_DW_AT_name (attr);
22718 return "DW_AT_<unknown>";
22723 /* Convert a DWARF value form code into its string name. */
22725 static const char *
22726 dwarf_form_name (unsigned form)
22728 const char *name = get_DW_FORM_name (form);
22731 return "DW_FORM_<unknown>";
22736 static const char *
22737 dwarf_bool_name (unsigned mybool)
22745 /* Convert a DWARF type code into its string name. */
22747 static const char *
22748 dwarf_type_encoding_name (unsigned enc)
22750 const char *name = get_DW_ATE_name (enc);
22753 return "DW_ATE_<unknown>";
22759 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22763 print_spaces (indent, f);
22764 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22765 dwarf_tag_name (die->tag), die->abbrev,
22766 to_underlying (die->sect_off));
22768 if (die->parent != NULL)
22770 print_spaces (indent, f);
22771 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22772 to_underlying (die->parent->sect_off));
22775 print_spaces (indent, f);
22776 fprintf_unfiltered (f, " has children: %s\n",
22777 dwarf_bool_name (die->child != NULL));
22779 print_spaces (indent, f);
22780 fprintf_unfiltered (f, " attributes:\n");
22782 for (i = 0; i < die->num_attrs; ++i)
22784 print_spaces (indent, f);
22785 fprintf_unfiltered (f, " %s (%s) ",
22786 dwarf_attr_name (die->attrs[i].name),
22787 dwarf_form_name (die->attrs[i].form));
22789 switch (die->attrs[i].form)
22792 case DW_FORM_GNU_addr_index:
22793 fprintf_unfiltered (f, "address: ");
22794 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22796 case DW_FORM_block2:
22797 case DW_FORM_block4:
22798 case DW_FORM_block:
22799 case DW_FORM_block1:
22800 fprintf_unfiltered (f, "block: size %s",
22801 pulongest (DW_BLOCK (&die->attrs[i])->size));
22803 case DW_FORM_exprloc:
22804 fprintf_unfiltered (f, "expression: size %s",
22805 pulongest (DW_BLOCK (&die->attrs[i])->size));
22807 case DW_FORM_data16:
22808 fprintf_unfiltered (f, "constant of 16 bytes");
22810 case DW_FORM_ref_addr:
22811 fprintf_unfiltered (f, "ref address: ");
22812 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22814 case DW_FORM_GNU_ref_alt:
22815 fprintf_unfiltered (f, "alt ref address: ");
22816 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22822 case DW_FORM_ref_udata:
22823 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22824 (long) (DW_UNSND (&die->attrs[i])));
22826 case DW_FORM_data1:
22827 case DW_FORM_data2:
22828 case DW_FORM_data4:
22829 case DW_FORM_data8:
22830 case DW_FORM_udata:
22831 case DW_FORM_sdata:
22832 fprintf_unfiltered (f, "constant: %s",
22833 pulongest (DW_UNSND (&die->attrs[i])));
22835 case DW_FORM_sec_offset:
22836 fprintf_unfiltered (f, "section offset: %s",
22837 pulongest (DW_UNSND (&die->attrs[i])));
22839 case DW_FORM_ref_sig8:
22840 fprintf_unfiltered (f, "signature: %s",
22841 hex_string (DW_SIGNATURE (&die->attrs[i])));
22843 case DW_FORM_string:
22845 case DW_FORM_line_strp:
22846 case DW_FORM_GNU_str_index:
22847 case DW_FORM_GNU_strp_alt:
22848 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22849 DW_STRING (&die->attrs[i])
22850 ? DW_STRING (&die->attrs[i]) : "",
22851 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22854 if (DW_UNSND (&die->attrs[i]))
22855 fprintf_unfiltered (f, "flag: TRUE");
22857 fprintf_unfiltered (f, "flag: FALSE");
22859 case DW_FORM_flag_present:
22860 fprintf_unfiltered (f, "flag: TRUE");
22862 case DW_FORM_indirect:
22863 /* The reader will have reduced the indirect form to
22864 the "base form" so this form should not occur. */
22865 fprintf_unfiltered (f,
22866 "unexpected attribute form: DW_FORM_indirect");
22868 case DW_FORM_implicit_const:
22869 fprintf_unfiltered (f, "constant: %s",
22870 plongest (DW_SND (&die->attrs[i])));
22873 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22874 die->attrs[i].form);
22877 fprintf_unfiltered (f, "\n");
22882 dump_die_for_error (struct die_info *die)
22884 dump_die_shallow (gdb_stderr, 0, die);
22888 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22890 int indent = level * 4;
22892 gdb_assert (die != NULL);
22894 if (level >= max_level)
22897 dump_die_shallow (f, indent, die);
22899 if (die->child != NULL)
22901 print_spaces (indent, f);
22902 fprintf_unfiltered (f, " Children:");
22903 if (level + 1 < max_level)
22905 fprintf_unfiltered (f, "\n");
22906 dump_die_1 (f, level + 1, max_level, die->child);
22910 fprintf_unfiltered (f,
22911 " [not printed, max nesting level reached]\n");
22915 if (die->sibling != NULL && level > 0)
22917 dump_die_1 (f, level, max_level, die->sibling);
22921 /* This is called from the pdie macro in gdbinit.in.
22922 It's not static so gcc will keep a copy callable from gdb. */
22925 dump_die (struct die_info *die, int max_level)
22927 dump_die_1 (gdb_stdlog, 0, max_level, die);
22931 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22935 slot = htab_find_slot_with_hash (cu->die_hash, die,
22936 to_underlying (die->sect_off),
22942 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22946 dwarf2_get_ref_die_offset (const struct attribute *attr)
22948 if (attr_form_is_ref (attr))
22949 return (sect_offset) DW_UNSND (attr);
22951 complaint (&symfile_complaints,
22952 _("unsupported die ref attribute form: '%s'"),
22953 dwarf_form_name (attr->form));
22957 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22958 * the value held by the attribute is not constant. */
22961 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22963 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22964 return DW_SND (attr);
22965 else if (attr->form == DW_FORM_udata
22966 || attr->form == DW_FORM_data1
22967 || attr->form == DW_FORM_data2
22968 || attr->form == DW_FORM_data4
22969 || attr->form == DW_FORM_data8)
22970 return DW_UNSND (attr);
22973 /* For DW_FORM_data16 see attr_form_is_constant. */
22974 complaint (&symfile_complaints,
22975 _("Attribute value is not a constant (%s)"),
22976 dwarf_form_name (attr->form));
22977 return default_value;
22981 /* Follow reference or signature attribute ATTR of SRC_DIE.
22982 On entry *REF_CU is the CU of SRC_DIE.
22983 On exit *REF_CU is the CU of the result. */
22985 static struct die_info *
22986 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22987 struct dwarf2_cu **ref_cu)
22989 struct die_info *die;
22991 if (attr_form_is_ref (attr))
22992 die = follow_die_ref (src_die, attr, ref_cu);
22993 else if (attr->form == DW_FORM_ref_sig8)
22994 die = follow_die_sig (src_die, attr, ref_cu);
22997 dump_die_for_error (src_die);
22998 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22999 objfile_name ((*ref_cu)->dwarf2_per_objfile->objfile));
23005 /* Follow reference OFFSET.
23006 On entry *REF_CU is the CU of the source die referencing OFFSET.
23007 On exit *REF_CU is the CU of the result.
23008 Returns NULL if OFFSET is invalid. */
23010 static struct die_info *
23011 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23012 struct dwarf2_cu **ref_cu)
23014 struct die_info temp_die;
23015 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23016 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
23017 struct objfile *objfile = dwarf2_per_objfile->objfile;
23019 gdb_assert (cu->per_cu != NULL);
23023 if (cu->per_cu->is_debug_types)
23025 /* .debug_types CUs cannot reference anything outside their CU.
23026 If they need to, they have to reference a signatured type via
23027 DW_FORM_ref_sig8. */
23028 if (!offset_in_cu_p (&cu->header, sect_off))
23031 else if (offset_in_dwz != cu->per_cu->is_dwz
23032 || !offset_in_cu_p (&cu->header, sect_off))
23034 struct dwarf2_per_cu_data *per_cu;
23036 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23037 dwarf2_per_objfile);
23039 /* If necessary, add it to the queue and load its DIEs. */
23040 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23041 load_full_comp_unit (per_cu, cu->language);
23043 target_cu = per_cu->cu;
23045 else if (cu->dies == NULL)
23047 /* We're loading full DIEs during partial symbol reading. */
23048 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23049 load_full_comp_unit (cu->per_cu, language_minimal);
23052 *ref_cu = target_cu;
23053 temp_die.sect_off = sect_off;
23054 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23056 to_underlying (sect_off));
23059 /* Follow reference attribute ATTR of SRC_DIE.
23060 On entry *REF_CU is the CU of SRC_DIE.
23061 On exit *REF_CU is the CU of the result. */
23063 static struct die_info *
23064 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23065 struct dwarf2_cu **ref_cu)
23067 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23068 struct dwarf2_cu *cu = *ref_cu;
23069 struct die_info *die;
23071 die = follow_die_offset (sect_off,
23072 (attr->form == DW_FORM_GNU_ref_alt
23073 || cu->per_cu->is_dwz),
23076 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
23077 "at 0x%x [in module %s]"),
23078 to_underlying (sect_off), to_underlying (src_die->sect_off),
23079 objfile_name (cu->dwarf2_per_objfile->objfile));
23084 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23085 Returned value is intended for DW_OP_call*. Returned
23086 dwarf2_locexpr_baton->data has lifetime of
23087 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23089 struct dwarf2_locexpr_baton
23090 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23091 struct dwarf2_per_cu_data *per_cu,
23092 CORE_ADDR (*get_frame_pc) (void *baton),
23095 struct dwarf2_cu *cu;
23096 struct die_info *die;
23097 struct attribute *attr;
23098 struct dwarf2_locexpr_baton retval;
23099 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23100 struct dwarf2_per_objfile *dwarf2_per_objfile
23101 = get_dwarf2_per_objfile (objfile);
23103 if (per_cu->cu == NULL)
23108 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23109 Instead just throw an error, not much else we can do. */
23110 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23111 to_underlying (sect_off), objfile_name (objfile));
23114 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23116 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23117 to_underlying (sect_off), objfile_name (objfile));
23119 attr = dwarf2_attr (die, DW_AT_location, cu);
23122 /* DWARF: "If there is no such attribute, then there is no effect.".
23123 DATA is ignored if SIZE is 0. */
23125 retval.data = NULL;
23128 else if (attr_form_is_section_offset (attr))
23130 struct dwarf2_loclist_baton loclist_baton;
23131 CORE_ADDR pc = (*get_frame_pc) (baton);
23134 fill_in_loclist_baton (cu, &loclist_baton, attr);
23136 retval.data = dwarf2_find_location_expression (&loclist_baton,
23138 retval.size = size;
23142 if (!attr_form_is_block (attr))
23143 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23144 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23145 to_underlying (sect_off), objfile_name (objfile));
23147 retval.data = DW_BLOCK (attr)->data;
23148 retval.size = DW_BLOCK (attr)->size;
23150 retval.per_cu = cu->per_cu;
23152 age_cached_comp_units (dwarf2_per_objfile);
23157 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23160 struct dwarf2_locexpr_baton
23161 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23162 struct dwarf2_per_cu_data *per_cu,
23163 CORE_ADDR (*get_frame_pc) (void *baton),
23166 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23168 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23171 /* Write a constant of a given type as target-ordered bytes into
23174 static const gdb_byte *
23175 write_constant_as_bytes (struct obstack *obstack,
23176 enum bfd_endian byte_order,
23183 *len = TYPE_LENGTH (type);
23184 result = (gdb_byte *) obstack_alloc (obstack, *len);
23185 store_unsigned_integer (result, *len, byte_order, value);
23190 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23191 pointer to the constant bytes and set LEN to the length of the
23192 data. If memory is needed, allocate it on OBSTACK. If the DIE
23193 does not have a DW_AT_const_value, return NULL. */
23196 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23197 struct dwarf2_per_cu_data *per_cu,
23198 struct obstack *obstack,
23201 struct dwarf2_cu *cu;
23202 struct die_info *die;
23203 struct attribute *attr;
23204 const gdb_byte *result = NULL;
23207 enum bfd_endian byte_order;
23208 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23210 if (per_cu->cu == NULL)
23215 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23216 Instead just throw an error, not much else we can do. */
23217 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23218 to_underlying (sect_off), objfile_name (objfile));
23221 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23223 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23224 to_underlying (sect_off), objfile_name (objfile));
23227 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23231 byte_order = (bfd_big_endian (objfile->obfd)
23232 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23234 switch (attr->form)
23237 case DW_FORM_GNU_addr_index:
23241 *len = cu->header.addr_size;
23242 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23243 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23247 case DW_FORM_string:
23249 case DW_FORM_GNU_str_index:
23250 case DW_FORM_GNU_strp_alt:
23251 /* DW_STRING is already allocated on the objfile obstack, point
23253 result = (const gdb_byte *) DW_STRING (attr);
23254 *len = strlen (DW_STRING (attr));
23256 case DW_FORM_block1:
23257 case DW_FORM_block2:
23258 case DW_FORM_block4:
23259 case DW_FORM_block:
23260 case DW_FORM_exprloc:
23261 case DW_FORM_data16:
23262 result = DW_BLOCK (attr)->data;
23263 *len = DW_BLOCK (attr)->size;
23266 /* The DW_AT_const_value attributes are supposed to carry the
23267 symbol's value "represented as it would be on the target
23268 architecture." By the time we get here, it's already been
23269 converted to host endianness, so we just need to sign- or
23270 zero-extend it as appropriate. */
23271 case DW_FORM_data1:
23272 type = die_type (die, cu);
23273 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23274 if (result == NULL)
23275 result = write_constant_as_bytes (obstack, byte_order,
23278 case DW_FORM_data2:
23279 type = die_type (die, cu);
23280 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23281 if (result == NULL)
23282 result = write_constant_as_bytes (obstack, byte_order,
23285 case DW_FORM_data4:
23286 type = die_type (die, cu);
23287 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23288 if (result == NULL)
23289 result = write_constant_as_bytes (obstack, byte_order,
23292 case DW_FORM_data8:
23293 type = die_type (die, cu);
23294 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23295 if (result == NULL)
23296 result = write_constant_as_bytes (obstack, byte_order,
23300 case DW_FORM_sdata:
23301 case DW_FORM_implicit_const:
23302 type = die_type (die, cu);
23303 result = write_constant_as_bytes (obstack, byte_order,
23304 type, DW_SND (attr), len);
23307 case DW_FORM_udata:
23308 type = die_type (die, cu);
23309 result = write_constant_as_bytes (obstack, byte_order,
23310 type, DW_UNSND (attr), len);
23314 complaint (&symfile_complaints,
23315 _("unsupported const value attribute form: '%s'"),
23316 dwarf_form_name (attr->form));
23323 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23324 valid type for this die is found. */
23327 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23328 struct dwarf2_per_cu_data *per_cu)
23330 struct dwarf2_cu *cu;
23331 struct die_info *die;
23333 if (per_cu->cu == NULL)
23339 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23343 return die_type (die, cu);
23346 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23350 dwarf2_get_die_type (cu_offset die_offset,
23351 struct dwarf2_per_cu_data *per_cu)
23353 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23354 return get_die_type_at_offset (die_offset_sect, per_cu);
23357 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23358 On entry *REF_CU is the CU of SRC_DIE.
23359 On exit *REF_CU is the CU of the result.
23360 Returns NULL if the referenced DIE isn't found. */
23362 static struct die_info *
23363 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23364 struct dwarf2_cu **ref_cu)
23366 struct die_info temp_die;
23367 struct dwarf2_cu *sig_cu;
23368 struct die_info *die;
23370 /* While it might be nice to assert sig_type->type == NULL here,
23371 we can get here for DW_AT_imported_declaration where we need
23372 the DIE not the type. */
23374 /* If necessary, add it to the queue and load its DIEs. */
23376 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23377 read_signatured_type (sig_type);
23379 sig_cu = sig_type->per_cu.cu;
23380 gdb_assert (sig_cu != NULL);
23381 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23382 temp_die.sect_off = sig_type->type_offset_in_section;
23383 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23384 to_underlying (temp_die.sect_off));
23387 struct dwarf2_per_objfile *dwarf2_per_objfile
23388 = (*ref_cu)->dwarf2_per_objfile;
23390 /* For .gdb_index version 7 keep track of included TUs.
23391 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23392 if (dwarf2_per_objfile->index_table != NULL
23393 && dwarf2_per_objfile->index_table->version <= 7)
23395 VEC_safe_push (dwarf2_per_cu_ptr,
23396 (*ref_cu)->per_cu->imported_symtabs,
23407 /* Follow signatured type referenced by ATTR in SRC_DIE.
23408 On entry *REF_CU is the CU of SRC_DIE.
23409 On exit *REF_CU is the CU of the result.
23410 The result is the DIE of the type.
23411 If the referenced type cannot be found an error is thrown. */
23413 static struct die_info *
23414 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23415 struct dwarf2_cu **ref_cu)
23417 ULONGEST signature = DW_SIGNATURE (attr);
23418 struct signatured_type *sig_type;
23419 struct die_info *die;
23421 gdb_assert (attr->form == DW_FORM_ref_sig8);
23423 sig_type = lookup_signatured_type (*ref_cu, signature);
23424 /* sig_type will be NULL if the signatured type is missing from
23426 if (sig_type == NULL)
23428 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23429 " from DIE at 0x%x [in module %s]"),
23430 hex_string (signature), to_underlying (src_die->sect_off),
23431 objfile_name ((*ref_cu)->dwarf2_per_objfile->objfile));
23434 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23437 dump_die_for_error (src_die);
23438 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23439 " from DIE at 0x%x [in module %s]"),
23440 hex_string (signature), to_underlying (src_die->sect_off),
23441 objfile_name ((*ref_cu)->dwarf2_per_objfile->objfile));
23447 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23448 reading in and processing the type unit if necessary. */
23450 static struct type *
23451 get_signatured_type (struct die_info *die, ULONGEST signature,
23452 struct dwarf2_cu *cu)
23454 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
23455 struct signatured_type *sig_type;
23456 struct dwarf2_cu *type_cu;
23457 struct die_info *type_die;
23460 sig_type = lookup_signatured_type (cu, signature);
23461 /* sig_type will be NULL if the signatured type is missing from
23463 if (sig_type == NULL)
23465 complaint (&symfile_complaints,
23466 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23467 " from DIE at 0x%x [in module %s]"),
23468 hex_string (signature), to_underlying (die->sect_off),
23469 objfile_name (dwarf2_per_objfile->objfile));
23470 return build_error_marker_type (cu, die);
23473 /* If we already know the type we're done. */
23474 if (sig_type->type != NULL)
23475 return sig_type->type;
23478 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23479 if (type_die != NULL)
23481 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23482 is created. This is important, for example, because for c++ classes
23483 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23484 type = read_type_die (type_die, type_cu);
23487 complaint (&symfile_complaints,
23488 _("Dwarf Error: Cannot build signatured type %s"
23489 " referenced from DIE at 0x%x [in module %s]"),
23490 hex_string (signature), to_underlying (die->sect_off),
23491 objfile_name (dwarf2_per_objfile->objfile));
23492 type = build_error_marker_type (cu, die);
23497 complaint (&symfile_complaints,
23498 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23499 " from DIE at 0x%x [in module %s]"),
23500 hex_string (signature), to_underlying (die->sect_off),
23501 objfile_name (dwarf2_per_objfile->objfile));
23502 type = build_error_marker_type (cu, die);
23504 sig_type->type = type;
23509 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23510 reading in and processing the type unit if necessary. */
23512 static struct type *
23513 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23514 struct dwarf2_cu *cu) /* ARI: editCase function */
23516 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23517 if (attr_form_is_ref (attr))
23519 struct dwarf2_cu *type_cu = cu;
23520 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23522 return read_type_die (type_die, type_cu);
23524 else if (attr->form == DW_FORM_ref_sig8)
23526 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23530 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
23532 complaint (&symfile_complaints,
23533 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23534 " at 0x%x [in module %s]"),
23535 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23536 objfile_name (dwarf2_per_objfile->objfile));
23537 return build_error_marker_type (cu, die);
23541 /* Load the DIEs associated with type unit PER_CU into memory. */
23544 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23546 struct signatured_type *sig_type;
23548 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23549 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23551 /* We have the per_cu, but we need the signatured_type.
23552 Fortunately this is an easy translation. */
23553 gdb_assert (per_cu->is_debug_types);
23554 sig_type = (struct signatured_type *) per_cu;
23556 gdb_assert (per_cu->cu == NULL);
23558 read_signatured_type (sig_type);
23560 gdb_assert (per_cu->cu != NULL);
23563 /* die_reader_func for read_signatured_type.
23564 This is identical to load_full_comp_unit_reader,
23565 but is kept separate for now. */
23568 read_signatured_type_reader (const struct die_reader_specs *reader,
23569 const gdb_byte *info_ptr,
23570 struct die_info *comp_unit_die,
23574 struct dwarf2_cu *cu = reader->cu;
23576 gdb_assert (cu->die_hash == NULL);
23578 htab_create_alloc_ex (cu->header.length / 12,
23582 &cu->comp_unit_obstack,
23583 hashtab_obstack_allocate,
23584 dummy_obstack_deallocate);
23587 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23588 &info_ptr, comp_unit_die);
23589 cu->dies = comp_unit_die;
23590 /* comp_unit_die is not stored in die_hash, no need. */
23592 /* We try not to read any attributes in this function, because not
23593 all CUs needed for references have been loaded yet, and symbol
23594 table processing isn't initialized. But we have to set the CU language,
23595 or we won't be able to build types correctly.
23596 Similarly, if we do not read the producer, we can not apply
23597 producer-specific interpretation. */
23598 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23601 /* Read in a signatured type and build its CU and DIEs.
23602 If the type is a stub for the real type in a DWO file,
23603 read in the real type from the DWO file as well. */
23606 read_signatured_type (struct signatured_type *sig_type)
23608 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23610 gdb_assert (per_cu->is_debug_types);
23611 gdb_assert (per_cu->cu == NULL);
23613 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23614 read_signatured_type_reader, NULL);
23615 sig_type->per_cu.tu_read = 1;
23618 /* Decode simple location descriptions.
23619 Given a pointer to a dwarf block that defines a location, compute
23620 the location and return the value.
23622 NOTE drow/2003-11-18: This function is called in two situations
23623 now: for the address of static or global variables (partial symbols
23624 only) and for offsets into structures which are expected to be
23625 (more or less) constant. The partial symbol case should go away,
23626 and only the constant case should remain. That will let this
23627 function complain more accurately. A few special modes are allowed
23628 without complaint for global variables (for instance, global
23629 register values and thread-local values).
23631 A location description containing no operations indicates that the
23632 object is optimized out. The return value is 0 for that case.
23633 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23634 callers will only want a very basic result and this can become a
23637 Note that stack[0] is unused except as a default error return. */
23640 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23642 struct objfile *objfile = cu->dwarf2_per_objfile->objfile;
23644 size_t size = blk->size;
23645 const gdb_byte *data = blk->data;
23646 CORE_ADDR stack[64];
23648 unsigned int bytes_read, unsnd;
23654 stack[++stacki] = 0;
23693 stack[++stacki] = op - DW_OP_lit0;
23728 stack[++stacki] = op - DW_OP_reg0;
23730 dwarf2_complex_location_expr_complaint ();
23734 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23736 stack[++stacki] = unsnd;
23738 dwarf2_complex_location_expr_complaint ();
23742 stack[++stacki] = read_address (objfile->obfd, &data[i],
23747 case DW_OP_const1u:
23748 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23752 case DW_OP_const1s:
23753 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23757 case DW_OP_const2u:
23758 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23762 case DW_OP_const2s:
23763 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23767 case DW_OP_const4u:
23768 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23772 case DW_OP_const4s:
23773 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23777 case DW_OP_const8u:
23778 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23783 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23789 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23794 stack[stacki + 1] = stack[stacki];
23799 stack[stacki - 1] += stack[stacki];
23803 case DW_OP_plus_uconst:
23804 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23810 stack[stacki - 1] -= stack[stacki];
23815 /* If we're not the last op, then we definitely can't encode
23816 this using GDB's address_class enum. This is valid for partial
23817 global symbols, although the variable's address will be bogus
23820 dwarf2_complex_location_expr_complaint ();
23823 case DW_OP_GNU_push_tls_address:
23824 case DW_OP_form_tls_address:
23825 /* The top of the stack has the offset from the beginning
23826 of the thread control block at which the variable is located. */
23827 /* Nothing should follow this operator, so the top of stack would
23829 /* This is valid for partial global symbols, but the variable's
23830 address will be bogus in the psymtab. Make it always at least
23831 non-zero to not look as a variable garbage collected by linker
23832 which have DW_OP_addr 0. */
23834 dwarf2_complex_location_expr_complaint ();
23838 case DW_OP_GNU_uninit:
23841 case DW_OP_GNU_addr_index:
23842 case DW_OP_GNU_const_index:
23843 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23850 const char *name = get_DW_OP_name (op);
23853 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23856 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23860 return (stack[stacki]);
23863 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23864 outside of the allocated space. Also enforce minimum>0. */
23865 if (stacki >= ARRAY_SIZE (stack) - 1)
23867 complaint (&symfile_complaints,
23868 _("location description stack overflow"));
23874 complaint (&symfile_complaints,
23875 _("location description stack underflow"));
23879 return (stack[stacki]);
23882 /* memory allocation interface */
23884 static struct dwarf_block *
23885 dwarf_alloc_block (struct dwarf2_cu *cu)
23887 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23890 static struct die_info *
23891 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23893 struct die_info *die;
23894 size_t size = sizeof (struct die_info);
23897 size += (num_attrs - 1) * sizeof (struct attribute);
23899 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23900 memset (die, 0, sizeof (struct die_info));
23905 /* Macro support. */
23907 /* Return file name relative to the compilation directory of file number I in
23908 *LH's file name table. The result is allocated using xmalloc; the caller is
23909 responsible for freeing it. */
23912 file_file_name (int file, struct line_header *lh)
23914 /* Is the file number a valid index into the line header's file name
23915 table? Remember that file numbers start with one, not zero. */
23916 if (1 <= file && file <= lh->file_names.size ())
23918 const file_entry &fe = lh->file_names[file - 1];
23920 if (!IS_ABSOLUTE_PATH (fe.name))
23922 const char *dir = fe.include_dir (lh);
23924 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23926 return xstrdup (fe.name);
23930 /* The compiler produced a bogus file number. We can at least
23931 record the macro definitions made in the file, even if we
23932 won't be able to find the file by name. */
23933 char fake_name[80];
23935 xsnprintf (fake_name, sizeof (fake_name),
23936 "<bad macro file number %d>", file);
23938 complaint (&symfile_complaints,
23939 _("bad file number in macro information (%d)"),
23942 return xstrdup (fake_name);
23946 /* Return the full name of file number I in *LH's file name table.
23947 Use COMP_DIR as the name of the current directory of the
23948 compilation. The result is allocated using xmalloc; the caller is
23949 responsible for freeing it. */
23951 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23953 /* Is the file number a valid index into the line header's file name
23954 table? Remember that file numbers start with one, not zero. */
23955 if (1 <= file && file <= lh->file_names.size ())
23957 char *relative = file_file_name (file, lh);
23959 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23961 return reconcat (relative, comp_dir, SLASH_STRING,
23962 relative, (char *) NULL);
23965 return file_file_name (file, lh);
23969 static struct macro_source_file *
23970 macro_start_file (int file, int line,
23971 struct macro_source_file *current_file,
23972 struct line_header *lh)
23974 /* File name relative to the compilation directory of this source file. */
23975 char *file_name = file_file_name (file, lh);
23977 if (! current_file)
23979 /* Note: We don't create a macro table for this compilation unit
23980 at all until we actually get a filename. */
23981 struct macro_table *macro_table = get_macro_table ();
23983 /* If we have no current file, then this must be the start_file
23984 directive for the compilation unit's main source file. */
23985 current_file = macro_set_main (macro_table, file_name);
23986 macro_define_special (macro_table);
23989 current_file = macro_include (current_file, line, file_name);
23993 return current_file;
23996 static const char *
23997 consume_improper_spaces (const char *p, const char *body)
24001 complaint (&symfile_complaints,
24002 _("macro definition contains spaces "
24003 "in formal argument list:\n`%s'"),
24015 parse_macro_definition (struct macro_source_file *file, int line,
24020 /* The body string takes one of two forms. For object-like macro
24021 definitions, it should be:
24023 <macro name> " " <definition>
24025 For function-like macro definitions, it should be:
24027 <macro name> "() " <definition>
24029 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24031 Spaces may appear only where explicitly indicated, and in the
24034 The Dwarf 2 spec says that an object-like macro's name is always
24035 followed by a space, but versions of GCC around March 2002 omit
24036 the space when the macro's definition is the empty string.
24038 The Dwarf 2 spec says that there should be no spaces between the
24039 formal arguments in a function-like macro's formal argument list,
24040 but versions of GCC around March 2002 include spaces after the
24044 /* Find the extent of the macro name. The macro name is terminated
24045 by either a space or null character (for an object-like macro) or
24046 an opening paren (for a function-like macro). */
24047 for (p = body; *p; p++)
24048 if (*p == ' ' || *p == '(')
24051 if (*p == ' ' || *p == '\0')
24053 /* It's an object-like macro. */
24054 int name_len = p - body;
24055 char *name = savestring (body, name_len);
24056 const char *replacement;
24059 replacement = body + name_len + 1;
24062 dwarf2_macro_malformed_definition_complaint (body);
24063 replacement = body + name_len;
24066 macro_define_object (file, line, name, replacement);
24070 else if (*p == '(')
24072 /* It's a function-like macro. */
24073 char *name = savestring (body, p - body);
24076 char **argv = XNEWVEC (char *, argv_size);
24080 p = consume_improper_spaces (p, body);
24082 /* Parse the formal argument list. */
24083 while (*p && *p != ')')
24085 /* Find the extent of the current argument name. */
24086 const char *arg_start = p;
24088 while (*p && *p != ',' && *p != ')' && *p != ' ')
24091 if (! *p || p == arg_start)
24092 dwarf2_macro_malformed_definition_complaint (body);
24095 /* Make sure argv has room for the new argument. */
24096 if (argc >= argv_size)
24099 argv = XRESIZEVEC (char *, argv, argv_size);
24102 argv[argc++] = savestring (arg_start, p - arg_start);
24105 p = consume_improper_spaces (p, body);
24107 /* Consume the comma, if present. */
24112 p = consume_improper_spaces (p, body);
24121 /* Perfectly formed definition, no complaints. */
24122 macro_define_function (file, line, name,
24123 argc, (const char **) argv,
24125 else if (*p == '\0')
24127 /* Complain, but do define it. */
24128 dwarf2_macro_malformed_definition_complaint (body);
24129 macro_define_function (file, line, name,
24130 argc, (const char **) argv,
24134 /* Just complain. */
24135 dwarf2_macro_malformed_definition_complaint (body);
24138 /* Just complain. */
24139 dwarf2_macro_malformed_definition_complaint (body);
24145 for (i = 0; i < argc; i++)
24151 dwarf2_macro_malformed_definition_complaint (body);
24154 /* Skip some bytes from BYTES according to the form given in FORM.
24155 Returns the new pointer. */
24157 static const gdb_byte *
24158 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24159 enum dwarf_form form,
24160 unsigned int offset_size,
24161 struct dwarf2_section_info *section)
24163 unsigned int bytes_read;
24167 case DW_FORM_data1:
24172 case DW_FORM_data2:
24176 case DW_FORM_data4:
24180 case DW_FORM_data8:
24184 case DW_FORM_data16:
24188 case DW_FORM_string:
24189 read_direct_string (abfd, bytes, &bytes_read);
24190 bytes += bytes_read;
24193 case DW_FORM_sec_offset:
24195 case DW_FORM_GNU_strp_alt:
24196 bytes += offset_size;
24199 case DW_FORM_block:
24200 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24201 bytes += bytes_read;
24204 case DW_FORM_block1:
24205 bytes += 1 + read_1_byte (abfd, bytes);
24207 case DW_FORM_block2:
24208 bytes += 2 + read_2_bytes (abfd, bytes);
24210 case DW_FORM_block4:
24211 bytes += 4 + read_4_bytes (abfd, bytes);
24214 case DW_FORM_sdata:
24215 case DW_FORM_udata:
24216 case DW_FORM_GNU_addr_index:
24217 case DW_FORM_GNU_str_index:
24218 bytes = gdb_skip_leb128 (bytes, buffer_end);
24221 dwarf2_section_buffer_overflow_complaint (section);
24226 case DW_FORM_implicit_const:
24231 complaint (&symfile_complaints,
24232 _("invalid form 0x%x in `%s'"),
24233 form, get_section_name (section));
24241 /* A helper for dwarf_decode_macros that handles skipping an unknown
24242 opcode. Returns an updated pointer to the macro data buffer; or,
24243 on error, issues a complaint and returns NULL. */
24245 static const gdb_byte *
24246 skip_unknown_opcode (unsigned int opcode,
24247 const gdb_byte **opcode_definitions,
24248 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24250 unsigned int offset_size,
24251 struct dwarf2_section_info *section)
24253 unsigned int bytes_read, i;
24255 const gdb_byte *defn;
24257 if (opcode_definitions[opcode] == NULL)
24259 complaint (&symfile_complaints,
24260 _("unrecognized DW_MACFINO opcode 0x%x"),
24265 defn = opcode_definitions[opcode];
24266 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24267 defn += bytes_read;
24269 for (i = 0; i < arg; ++i)
24271 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24272 (enum dwarf_form) defn[i], offset_size,
24274 if (mac_ptr == NULL)
24276 /* skip_form_bytes already issued the complaint. */
24284 /* A helper function which parses the header of a macro section.
24285 If the macro section is the extended (for now called "GNU") type,
24286 then this updates *OFFSET_SIZE. Returns a pointer to just after
24287 the header, or issues a complaint and returns NULL on error. */
24289 static const gdb_byte *
24290 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24292 const gdb_byte *mac_ptr,
24293 unsigned int *offset_size,
24294 int section_is_gnu)
24296 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24298 if (section_is_gnu)
24300 unsigned int version, flags;
24302 version = read_2_bytes (abfd, mac_ptr);
24303 if (version != 4 && version != 5)
24305 complaint (&symfile_complaints,
24306 _("unrecognized version `%d' in .debug_macro section"),
24312 flags = read_1_byte (abfd, mac_ptr);
24314 *offset_size = (flags & 1) ? 8 : 4;
24316 if ((flags & 2) != 0)
24317 /* We don't need the line table offset. */
24318 mac_ptr += *offset_size;
24320 /* Vendor opcode descriptions. */
24321 if ((flags & 4) != 0)
24323 unsigned int i, count;
24325 count = read_1_byte (abfd, mac_ptr);
24327 for (i = 0; i < count; ++i)
24329 unsigned int opcode, bytes_read;
24332 opcode = read_1_byte (abfd, mac_ptr);
24334 opcode_definitions[opcode] = mac_ptr;
24335 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24336 mac_ptr += bytes_read;
24345 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24346 including DW_MACRO_import. */
24349 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24351 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24352 struct macro_source_file *current_file,
24353 struct line_header *lh,
24354 struct dwarf2_section_info *section,
24355 int section_is_gnu, int section_is_dwz,
24356 unsigned int offset_size,
24357 htab_t include_hash)
24359 struct objfile *objfile = dwarf2_per_objfile->objfile;
24360 enum dwarf_macro_record_type macinfo_type;
24361 int at_commandline;
24362 const gdb_byte *opcode_definitions[256];
24364 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24365 &offset_size, section_is_gnu);
24366 if (mac_ptr == NULL)
24368 /* We already issued a complaint. */
24372 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24373 GDB is still reading the definitions from command line. First
24374 DW_MACINFO_start_file will need to be ignored as it was already executed
24375 to create CURRENT_FILE for the main source holding also the command line
24376 definitions. On first met DW_MACINFO_start_file this flag is reset to
24377 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24379 at_commandline = 1;
24383 /* Do we at least have room for a macinfo type byte? */
24384 if (mac_ptr >= mac_end)
24386 dwarf2_section_buffer_overflow_complaint (section);
24390 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24393 /* Note that we rely on the fact that the corresponding GNU and
24394 DWARF constants are the same. */
24396 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24397 switch (macinfo_type)
24399 /* A zero macinfo type indicates the end of the macro
24404 case DW_MACRO_define:
24405 case DW_MACRO_undef:
24406 case DW_MACRO_define_strp:
24407 case DW_MACRO_undef_strp:
24408 case DW_MACRO_define_sup:
24409 case DW_MACRO_undef_sup:
24411 unsigned int bytes_read;
24416 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24417 mac_ptr += bytes_read;
24419 if (macinfo_type == DW_MACRO_define
24420 || macinfo_type == DW_MACRO_undef)
24422 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24423 mac_ptr += bytes_read;
24427 LONGEST str_offset;
24429 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24430 mac_ptr += offset_size;
24432 if (macinfo_type == DW_MACRO_define_sup
24433 || macinfo_type == DW_MACRO_undef_sup
24436 struct dwz_file *dwz
24437 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24439 body = read_indirect_string_from_dwz (objfile,
24443 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24447 is_define = (macinfo_type == DW_MACRO_define
24448 || macinfo_type == DW_MACRO_define_strp
24449 || macinfo_type == DW_MACRO_define_sup);
24450 if (! current_file)
24452 /* DWARF violation as no main source is present. */
24453 complaint (&symfile_complaints,
24454 _("debug info with no main source gives macro %s "
24456 is_define ? _("definition") : _("undefinition"),
24460 if ((line == 0 && !at_commandline)
24461 || (line != 0 && at_commandline))
24462 complaint (&symfile_complaints,
24463 _("debug info gives %s macro %s with %s line %d: %s"),
24464 at_commandline ? _("command-line") : _("in-file"),
24465 is_define ? _("definition") : _("undefinition"),
24466 line == 0 ? _("zero") : _("non-zero"), line, body);
24469 parse_macro_definition (current_file, line, body);
24472 gdb_assert (macinfo_type == DW_MACRO_undef
24473 || macinfo_type == DW_MACRO_undef_strp
24474 || macinfo_type == DW_MACRO_undef_sup);
24475 macro_undef (current_file, line, body);
24480 case DW_MACRO_start_file:
24482 unsigned int bytes_read;
24485 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24486 mac_ptr += bytes_read;
24487 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24488 mac_ptr += bytes_read;
24490 if ((line == 0 && !at_commandline)
24491 || (line != 0 && at_commandline))
24492 complaint (&symfile_complaints,
24493 _("debug info gives source %d included "
24494 "from %s at %s line %d"),
24495 file, at_commandline ? _("command-line") : _("file"),
24496 line == 0 ? _("zero") : _("non-zero"), line);
24498 if (at_commandline)
24500 /* This DW_MACRO_start_file was executed in the
24502 at_commandline = 0;
24505 current_file = macro_start_file (file, line, current_file, lh);
24509 case DW_MACRO_end_file:
24510 if (! current_file)
24511 complaint (&symfile_complaints,
24512 _("macro debug info has an unmatched "
24513 "`close_file' directive"));
24516 current_file = current_file->included_by;
24517 if (! current_file)
24519 enum dwarf_macro_record_type next_type;
24521 /* GCC circa March 2002 doesn't produce the zero
24522 type byte marking the end of the compilation
24523 unit. Complain if it's not there, but exit no
24526 /* Do we at least have room for a macinfo type byte? */
24527 if (mac_ptr >= mac_end)
24529 dwarf2_section_buffer_overflow_complaint (section);
24533 /* We don't increment mac_ptr here, so this is just
24536 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24538 if (next_type != 0)
24539 complaint (&symfile_complaints,
24540 _("no terminating 0-type entry for "
24541 "macros in `.debug_macinfo' section"));
24548 case DW_MACRO_import:
24549 case DW_MACRO_import_sup:
24553 bfd *include_bfd = abfd;
24554 struct dwarf2_section_info *include_section = section;
24555 const gdb_byte *include_mac_end = mac_end;
24556 int is_dwz = section_is_dwz;
24557 const gdb_byte *new_mac_ptr;
24559 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24560 mac_ptr += offset_size;
24562 if (macinfo_type == DW_MACRO_import_sup)
24564 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24566 dwarf2_read_section (objfile, &dwz->macro);
24568 include_section = &dwz->macro;
24569 include_bfd = get_section_bfd_owner (include_section);
24570 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24574 new_mac_ptr = include_section->buffer + offset;
24575 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24579 /* This has actually happened; see
24580 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24581 complaint (&symfile_complaints,
24582 _("recursive DW_MACRO_import in "
24583 ".debug_macro section"));
24587 *slot = (void *) new_mac_ptr;
24589 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24590 include_bfd, new_mac_ptr,
24591 include_mac_end, current_file, lh,
24592 section, section_is_gnu, is_dwz,
24593 offset_size, include_hash);
24595 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24600 case DW_MACINFO_vendor_ext:
24601 if (!section_is_gnu)
24603 unsigned int bytes_read;
24605 /* This reads the constant, but since we don't recognize
24606 any vendor extensions, we ignore it. */
24607 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24608 mac_ptr += bytes_read;
24609 read_direct_string (abfd, mac_ptr, &bytes_read);
24610 mac_ptr += bytes_read;
24612 /* We don't recognize any vendor extensions. */
24618 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24619 mac_ptr, mac_end, abfd, offset_size,
24621 if (mac_ptr == NULL)
24626 } while (macinfo_type != 0);
24630 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24631 int section_is_gnu)
24633 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
24634 struct objfile *objfile = dwarf2_per_objfile->objfile;
24635 struct line_header *lh = cu->line_header;
24637 const gdb_byte *mac_ptr, *mac_end;
24638 struct macro_source_file *current_file = 0;
24639 enum dwarf_macro_record_type macinfo_type;
24640 unsigned int offset_size = cu->header.offset_size;
24641 const gdb_byte *opcode_definitions[256];
24643 struct dwarf2_section_info *section;
24644 const char *section_name;
24646 if (cu->dwo_unit != NULL)
24648 if (section_is_gnu)
24650 section = &cu->dwo_unit->dwo_file->sections.macro;
24651 section_name = ".debug_macro.dwo";
24655 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24656 section_name = ".debug_macinfo.dwo";
24661 if (section_is_gnu)
24663 section = &dwarf2_per_objfile->macro;
24664 section_name = ".debug_macro";
24668 section = &dwarf2_per_objfile->macinfo;
24669 section_name = ".debug_macinfo";
24673 dwarf2_read_section (objfile, section);
24674 if (section->buffer == NULL)
24676 complaint (&symfile_complaints, _("missing %s section"), section_name);
24679 abfd = get_section_bfd_owner (section);
24681 /* First pass: Find the name of the base filename.
24682 This filename is needed in order to process all macros whose definition
24683 (or undefinition) comes from the command line. These macros are defined
24684 before the first DW_MACINFO_start_file entry, and yet still need to be
24685 associated to the base file.
24687 To determine the base file name, we scan the macro definitions until we
24688 reach the first DW_MACINFO_start_file entry. We then initialize
24689 CURRENT_FILE accordingly so that any macro definition found before the
24690 first DW_MACINFO_start_file can still be associated to the base file. */
24692 mac_ptr = section->buffer + offset;
24693 mac_end = section->buffer + section->size;
24695 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24696 &offset_size, section_is_gnu);
24697 if (mac_ptr == NULL)
24699 /* We already issued a complaint. */
24705 /* Do we at least have room for a macinfo type byte? */
24706 if (mac_ptr >= mac_end)
24708 /* Complaint is printed during the second pass as GDB will probably
24709 stop the first pass earlier upon finding
24710 DW_MACINFO_start_file. */
24714 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24717 /* Note that we rely on the fact that the corresponding GNU and
24718 DWARF constants are the same. */
24720 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24721 switch (macinfo_type)
24723 /* A zero macinfo type indicates the end of the macro
24728 case DW_MACRO_define:
24729 case DW_MACRO_undef:
24730 /* Only skip the data by MAC_PTR. */
24732 unsigned int bytes_read;
24734 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24735 mac_ptr += bytes_read;
24736 read_direct_string (abfd, mac_ptr, &bytes_read);
24737 mac_ptr += bytes_read;
24741 case DW_MACRO_start_file:
24743 unsigned int bytes_read;
24746 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24747 mac_ptr += bytes_read;
24748 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24749 mac_ptr += bytes_read;
24751 current_file = macro_start_file (file, line, current_file, lh);
24755 case DW_MACRO_end_file:
24756 /* No data to skip by MAC_PTR. */
24759 case DW_MACRO_define_strp:
24760 case DW_MACRO_undef_strp:
24761 case DW_MACRO_define_sup:
24762 case DW_MACRO_undef_sup:
24764 unsigned int bytes_read;
24766 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24767 mac_ptr += bytes_read;
24768 mac_ptr += offset_size;
24772 case DW_MACRO_import:
24773 case DW_MACRO_import_sup:
24774 /* Note that, according to the spec, a transparent include
24775 chain cannot call DW_MACRO_start_file. So, we can just
24776 skip this opcode. */
24777 mac_ptr += offset_size;
24780 case DW_MACINFO_vendor_ext:
24781 /* Only skip the data by MAC_PTR. */
24782 if (!section_is_gnu)
24784 unsigned int bytes_read;
24786 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24787 mac_ptr += bytes_read;
24788 read_direct_string (abfd, mac_ptr, &bytes_read);
24789 mac_ptr += bytes_read;
24794 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24795 mac_ptr, mac_end, abfd, offset_size,
24797 if (mac_ptr == NULL)
24802 } while (macinfo_type != 0 && current_file == NULL);
24804 /* Second pass: Process all entries.
24806 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24807 command-line macro definitions/undefinitions. This flag is unset when we
24808 reach the first DW_MACINFO_start_file entry. */
24810 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24812 NULL, xcalloc, xfree));
24813 mac_ptr = section->buffer + offset;
24814 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24815 *slot = (void *) mac_ptr;
24816 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24817 abfd, mac_ptr, mac_end,
24818 current_file, lh, section,
24819 section_is_gnu, 0, offset_size,
24820 include_hash.get ());
24823 /* Check if the attribute's form is a DW_FORM_block*
24824 if so return true else false. */
24827 attr_form_is_block (const struct attribute *attr)
24829 return (attr == NULL ? 0 :
24830 attr->form == DW_FORM_block1
24831 || attr->form == DW_FORM_block2
24832 || attr->form == DW_FORM_block4
24833 || attr->form == DW_FORM_block
24834 || attr->form == DW_FORM_exprloc);
24837 /* Return non-zero if ATTR's value is a section offset --- classes
24838 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24839 You may use DW_UNSND (attr) to retrieve such offsets.
24841 Section 7.5.4, "Attribute Encodings", explains that no attribute
24842 may have a value that belongs to more than one of these classes; it
24843 would be ambiguous if we did, because we use the same forms for all
24847 attr_form_is_section_offset (const struct attribute *attr)
24849 return (attr->form == DW_FORM_data4
24850 || attr->form == DW_FORM_data8
24851 || attr->form == DW_FORM_sec_offset);
24854 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24855 zero otherwise. When this function returns true, you can apply
24856 dwarf2_get_attr_constant_value to it.
24858 However, note that for some attributes you must check
24859 attr_form_is_section_offset before using this test. DW_FORM_data4
24860 and DW_FORM_data8 are members of both the constant class, and of
24861 the classes that contain offsets into other debug sections
24862 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24863 that, if an attribute's can be either a constant or one of the
24864 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24865 taken as section offsets, not constants.
24867 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24868 cannot handle that. */
24871 attr_form_is_constant (const struct attribute *attr)
24873 switch (attr->form)
24875 case DW_FORM_sdata:
24876 case DW_FORM_udata:
24877 case DW_FORM_data1:
24878 case DW_FORM_data2:
24879 case DW_FORM_data4:
24880 case DW_FORM_data8:
24881 case DW_FORM_implicit_const:
24889 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24890 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24893 attr_form_is_ref (const struct attribute *attr)
24895 switch (attr->form)
24897 case DW_FORM_ref_addr:
24902 case DW_FORM_ref_udata:
24903 case DW_FORM_GNU_ref_alt:
24910 /* Return the .debug_loc section to use for CU.
24911 For DWO files use .debug_loc.dwo. */
24913 static struct dwarf2_section_info *
24914 cu_debug_loc_section (struct dwarf2_cu *cu)
24916 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
24920 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24922 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
24924 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24925 : &dwarf2_per_objfile->loc);
24928 /* A helper function that fills in a dwarf2_loclist_baton. */
24931 fill_in_loclist_baton (struct dwarf2_cu *cu,
24932 struct dwarf2_loclist_baton *baton,
24933 const struct attribute *attr)
24935 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
24936 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24938 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24940 baton->per_cu = cu->per_cu;
24941 gdb_assert (baton->per_cu);
24942 /* We don't know how long the location list is, but make sure we
24943 don't run off the edge of the section. */
24944 baton->size = section->size - DW_UNSND (attr);
24945 baton->data = section->buffer + DW_UNSND (attr);
24946 baton->base_address = cu->base_address;
24947 baton->from_dwo = cu->dwo_unit != NULL;
24951 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24952 struct dwarf2_cu *cu, int is_block)
24954 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
24955 struct objfile *objfile = dwarf2_per_objfile->objfile;
24956 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24958 if (attr_form_is_section_offset (attr)
24959 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24960 the section. If so, fall through to the complaint in the
24962 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24964 struct dwarf2_loclist_baton *baton;
24966 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24968 fill_in_loclist_baton (cu, baton, attr);
24970 if (cu->base_known == 0)
24971 complaint (&symfile_complaints,
24972 _("Location list used without "
24973 "specifying the CU base address."));
24975 SYMBOL_ACLASS_INDEX (sym) = (is_block
24976 ? dwarf2_loclist_block_index
24977 : dwarf2_loclist_index);
24978 SYMBOL_LOCATION_BATON (sym) = baton;
24982 struct dwarf2_locexpr_baton *baton;
24984 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24985 baton->per_cu = cu->per_cu;
24986 gdb_assert (baton->per_cu);
24988 if (attr_form_is_block (attr))
24990 /* Note that we're just copying the block's data pointer
24991 here, not the actual data. We're still pointing into the
24992 info_buffer for SYM's objfile; right now we never release
24993 that buffer, but when we do clean up properly this may
24995 baton->size = DW_BLOCK (attr)->size;
24996 baton->data = DW_BLOCK (attr)->data;
25000 dwarf2_invalid_attrib_class_complaint ("location description",
25001 SYMBOL_NATURAL_NAME (sym));
25005 SYMBOL_ACLASS_INDEX (sym) = (is_block
25006 ? dwarf2_locexpr_block_index
25007 : dwarf2_locexpr_index);
25008 SYMBOL_LOCATION_BATON (sym) = baton;
25012 /* Return the OBJFILE associated with the compilation unit CU. If CU
25013 came from a separate debuginfo file, then the master objfile is
25017 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25019 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25021 /* Return the master objfile, so that we can report and look up the
25022 correct file containing this variable. */
25023 if (objfile->separate_debug_objfile_backlink)
25024 objfile = objfile->separate_debug_objfile_backlink;
25029 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25030 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25031 CU_HEADERP first. */
25033 static const struct comp_unit_head *
25034 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25035 struct dwarf2_per_cu_data *per_cu)
25037 const gdb_byte *info_ptr;
25040 return &per_cu->cu->header;
25042 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25044 memset (cu_headerp, 0, sizeof (*cu_headerp));
25045 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25046 rcuh_kind::COMPILE);
25051 /* Return the address size given in the compilation unit header for CU. */
25054 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25056 struct comp_unit_head cu_header_local;
25057 const struct comp_unit_head *cu_headerp;
25059 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25061 return cu_headerp->addr_size;
25064 /* Return the offset size given in the compilation unit header for CU. */
25067 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25069 struct comp_unit_head cu_header_local;
25070 const struct comp_unit_head *cu_headerp;
25072 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25074 return cu_headerp->offset_size;
25077 /* See its dwarf2loc.h declaration. */
25080 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25082 struct comp_unit_head cu_header_local;
25083 const struct comp_unit_head *cu_headerp;
25085 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25087 if (cu_headerp->version == 2)
25088 return cu_headerp->addr_size;
25090 return cu_headerp->offset_size;
25093 /* Return the text offset of the CU. The returned offset comes from
25094 this CU's objfile. If this objfile came from a separate debuginfo
25095 file, then the offset may be different from the corresponding
25096 offset in the parent objfile. */
25099 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25101 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25103 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25106 /* Return DWARF version number of PER_CU. */
25109 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25111 return per_cu->dwarf_version;
25114 /* Locate the .debug_info compilation unit from CU's objfile which contains
25115 the DIE at OFFSET. Raises an error on failure. */
25117 static struct dwarf2_per_cu_data *
25118 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25119 unsigned int offset_in_dwz,
25120 struct dwarf2_per_objfile *dwarf2_per_objfile)
25122 struct dwarf2_per_cu_data *this_cu;
25124 const sect_offset *cu_off;
25127 high = dwarf2_per_objfile->n_comp_units - 1;
25130 struct dwarf2_per_cu_data *mid_cu;
25131 int mid = low + (high - low) / 2;
25133 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25134 cu_off = &mid_cu->sect_off;
25135 if (mid_cu->is_dwz > offset_in_dwz
25136 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25141 gdb_assert (low == high);
25142 this_cu = dwarf2_per_objfile->all_comp_units[low];
25143 cu_off = &this_cu->sect_off;
25144 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25146 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25147 error (_("Dwarf Error: could not find partial DIE containing "
25148 "offset 0x%x [in module %s]"),
25149 to_underlying (sect_off),
25150 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25152 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25154 return dwarf2_per_objfile->all_comp_units[low-1];
25158 this_cu = dwarf2_per_objfile->all_comp_units[low];
25159 if (low == dwarf2_per_objfile->n_comp_units - 1
25160 && sect_off >= this_cu->sect_off + this_cu->length)
25161 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25162 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25167 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25170 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
25172 memset (cu, 0, sizeof (*cu));
25174 cu->per_cu = per_cu;
25175 cu->dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25176 obstack_init (&cu->comp_unit_obstack);
25179 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25182 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25183 enum language pretend_language)
25185 struct attribute *attr;
25187 /* Set the language we're debugging. */
25188 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25190 set_cu_language (DW_UNSND (attr), cu);
25193 cu->language = pretend_language;
25194 cu->language_defn = language_def (cu->language);
25197 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25200 /* Release one cached compilation unit, CU. We unlink it from the tree
25201 of compilation units, but we don't remove it from the read_in_chain;
25202 the caller is responsible for that.
25203 NOTE: DATA is a void * because this function is also used as a
25204 cleanup routine. */
25207 free_heap_comp_unit (void *data)
25209 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
25211 gdb_assert (cu->per_cu != NULL);
25212 cu->per_cu->cu = NULL;
25215 obstack_free (&cu->comp_unit_obstack, NULL);
25220 /* This cleanup function is passed the address of a dwarf2_cu on the stack
25221 when we're finished with it. We can't free the pointer itself, but be
25222 sure to unlink it from the cache. Also release any associated storage. */
25225 free_stack_comp_unit (void *data)
25227 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
25229 gdb_assert (cu->per_cu != NULL);
25230 cu->per_cu->cu = NULL;
25233 obstack_free (&cu->comp_unit_obstack, NULL);
25234 cu->partial_dies = NULL;
25237 /* Free all cached compilation units. */
25240 free_cached_comp_units (void *data)
25242 struct dwarf2_per_objfile *dwarf2_per_objfile
25243 = (struct dwarf2_per_objfile *) data;
25245 dwarf2_per_objfile->free_cached_comp_units ();
25248 /* Increase the age counter on each cached compilation unit, and free
25249 any that are too old. */
25252 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25254 struct dwarf2_per_cu_data *per_cu, **last_chain;
25256 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25257 per_cu = dwarf2_per_objfile->read_in_chain;
25258 while (per_cu != NULL)
25260 per_cu->cu->last_used ++;
25261 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25262 dwarf2_mark (per_cu->cu);
25263 per_cu = per_cu->cu->read_in_chain;
25266 per_cu = dwarf2_per_objfile->read_in_chain;
25267 last_chain = &dwarf2_per_objfile->read_in_chain;
25268 while (per_cu != NULL)
25270 struct dwarf2_per_cu_data *next_cu;
25272 next_cu = per_cu->cu->read_in_chain;
25274 if (!per_cu->cu->mark)
25276 free_heap_comp_unit (per_cu->cu);
25277 *last_chain = next_cu;
25280 last_chain = &per_cu->cu->read_in_chain;
25286 /* Remove a single compilation unit from the cache. */
25289 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25291 struct dwarf2_per_cu_data *per_cu, **last_chain;
25292 struct dwarf2_per_objfile *dwarf2_per_objfile
25293 = target_per_cu->dwarf2_per_objfile;
25295 per_cu = dwarf2_per_objfile->read_in_chain;
25296 last_chain = &dwarf2_per_objfile->read_in_chain;
25297 while (per_cu != NULL)
25299 struct dwarf2_per_cu_data *next_cu;
25301 next_cu = per_cu->cu->read_in_chain;
25303 if (per_cu == target_per_cu)
25305 free_heap_comp_unit (per_cu->cu);
25307 *last_chain = next_cu;
25311 last_chain = &per_cu->cu->read_in_chain;
25317 /* Release all extra memory associated with OBJFILE. */
25320 dwarf2_free_objfile (struct objfile *objfile)
25322 struct dwarf2_per_objfile *dwarf2_per_objfile
25323 = get_dwarf2_per_objfile (objfile);
25325 if (dwarf2_per_objfile == NULL)
25328 dwarf2_per_objfile->~dwarf2_per_objfile ();
25331 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25332 We store these in a hash table separate from the DIEs, and preserve them
25333 when the DIEs are flushed out of cache.
25335 The CU "per_cu" pointer is needed because offset alone is not enough to
25336 uniquely identify the type. A file may have multiple .debug_types sections,
25337 or the type may come from a DWO file. Furthermore, while it's more logical
25338 to use per_cu->section+offset, with Fission the section with the data is in
25339 the DWO file but we don't know that section at the point we need it.
25340 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25341 because we can enter the lookup routine, get_die_type_at_offset, from
25342 outside this file, and thus won't necessarily have PER_CU->cu.
25343 Fortunately, PER_CU is stable for the life of the objfile. */
25345 struct dwarf2_per_cu_offset_and_type
25347 const struct dwarf2_per_cu_data *per_cu;
25348 sect_offset sect_off;
25352 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25355 per_cu_offset_and_type_hash (const void *item)
25357 const struct dwarf2_per_cu_offset_and_type *ofs
25358 = (const struct dwarf2_per_cu_offset_and_type *) item;
25360 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25363 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25366 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25368 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25369 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25370 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25371 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25373 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25374 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25377 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25378 table if necessary. For convenience, return TYPE.
25380 The DIEs reading must have careful ordering to:
25381 * Not cause infite loops trying to read in DIEs as a prerequisite for
25382 reading current DIE.
25383 * Not trying to dereference contents of still incompletely read in types
25384 while reading in other DIEs.
25385 * Enable referencing still incompletely read in types just by a pointer to
25386 the type without accessing its fields.
25388 Therefore caller should follow these rules:
25389 * Try to fetch any prerequisite types we may need to build this DIE type
25390 before building the type and calling set_die_type.
25391 * After building type call set_die_type for current DIE as soon as
25392 possible before fetching more types to complete the current type.
25393 * Make the type as complete as possible before fetching more types. */
25395 static struct type *
25396 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25398 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->dwarf2_per_objfile;
25399 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25400 struct objfile *objfile = dwarf2_per_objfile->objfile;
25401 struct attribute *attr;
25402 struct dynamic_prop prop;
25404 /* For Ada types, make sure that the gnat-specific data is always
25405 initialized (if not already set). There are a few types where
25406 we should not be doing so, because the type-specific area is
25407 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25408 where the type-specific area is used to store the floatformat).
25409 But this is not a problem, because the gnat-specific information
25410 is actually not needed for these types. */
25411 if (need_gnat_info (cu)
25412 && TYPE_CODE (type) != TYPE_CODE_FUNC
25413 && TYPE_CODE (type) != TYPE_CODE_FLT
25414 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25415 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25416 && TYPE_CODE (type) != TYPE_CODE_METHOD
25417 && !HAVE_GNAT_AUX_INFO (type))
25418 INIT_GNAT_SPECIFIC (type);
25420 /* Read DW_AT_allocated and set in type. */
25421 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25422 if (attr_form_is_block (attr))
25424 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25425 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25427 else if (attr != NULL)
25429 complaint (&symfile_complaints,
25430 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25431 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25432 to_underlying (die->sect_off));
25435 /* Read DW_AT_associated and set in type. */
25436 attr = dwarf2_attr (die, DW_AT_associated, cu);
25437 if (attr_form_is_block (attr))
25439 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25440 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25442 else if (attr != NULL)
25444 complaint (&symfile_complaints,
25445 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25446 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25447 to_underlying (die->sect_off));
25450 /* Read DW_AT_data_location and set in type. */
25451 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25452 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25453 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25455 if (dwarf2_per_objfile->die_type_hash == NULL)
25457 dwarf2_per_objfile->die_type_hash =
25458 htab_create_alloc_ex (127,
25459 per_cu_offset_and_type_hash,
25460 per_cu_offset_and_type_eq,
25462 &objfile->objfile_obstack,
25463 hashtab_obstack_allocate,
25464 dummy_obstack_deallocate);
25467 ofs.per_cu = cu->per_cu;
25468 ofs.sect_off = die->sect_off;
25470 slot = (struct dwarf2_per_cu_offset_and_type **)
25471 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25473 complaint (&symfile_complaints,
25474 _("A problem internal to GDB: DIE 0x%x has type already set"),
25475 to_underlying (die->sect_off));
25476 *slot = XOBNEW (&objfile->objfile_obstack,
25477 struct dwarf2_per_cu_offset_and_type);
25482 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25483 or return NULL if the die does not have a saved type. */
25485 static struct type *
25486 get_die_type_at_offset (sect_offset sect_off,
25487 struct dwarf2_per_cu_data *per_cu)
25489 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25490 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25492 if (dwarf2_per_objfile->die_type_hash == NULL)
25495 ofs.per_cu = per_cu;
25496 ofs.sect_off = sect_off;
25497 slot = ((struct dwarf2_per_cu_offset_and_type *)
25498 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25505 /* Look up the type for DIE in CU in die_type_hash,
25506 or return NULL if DIE does not have a saved type. */
25508 static struct type *
25509 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25511 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25514 /* Add a dependence relationship from CU to REF_PER_CU. */
25517 dwarf2_add_dependence (struct dwarf2_cu *cu,
25518 struct dwarf2_per_cu_data *ref_per_cu)
25522 if (cu->dependencies == NULL)
25524 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25525 NULL, &cu->comp_unit_obstack,
25526 hashtab_obstack_allocate,
25527 dummy_obstack_deallocate);
25529 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25531 *slot = ref_per_cu;
25534 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25535 Set the mark field in every compilation unit in the
25536 cache that we must keep because we are keeping CU. */
25539 dwarf2_mark_helper (void **slot, void *data)
25541 struct dwarf2_per_cu_data *per_cu;
25543 per_cu = (struct dwarf2_per_cu_data *) *slot;
25545 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25546 reading of the chain. As such dependencies remain valid it is not much
25547 useful to track and undo them during QUIT cleanups. */
25548 if (per_cu->cu == NULL)
25551 if (per_cu->cu->mark)
25553 per_cu->cu->mark = 1;
25555 if (per_cu->cu->dependencies != NULL)
25556 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25561 /* Set the mark field in CU and in every other compilation unit in the
25562 cache that we must keep because we are keeping CU. */
25565 dwarf2_mark (struct dwarf2_cu *cu)
25570 if (cu->dependencies != NULL)
25571 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25575 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25579 per_cu->cu->mark = 0;
25580 per_cu = per_cu->cu->read_in_chain;
25584 /* Trivial hash function for partial_die_info: the hash value of a DIE
25585 is its offset in .debug_info for this objfile. */
25588 partial_die_hash (const void *item)
25590 const struct partial_die_info *part_die
25591 = (const struct partial_die_info *) item;
25593 return to_underlying (part_die->sect_off);
25596 /* Trivial comparison function for partial_die_info structures: two DIEs
25597 are equal if they have the same offset. */
25600 partial_die_eq (const void *item_lhs, const void *item_rhs)
25602 const struct partial_die_info *part_die_lhs
25603 = (const struct partial_die_info *) item_lhs;
25604 const struct partial_die_info *part_die_rhs
25605 = (const struct partial_die_info *) item_rhs;
25607 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25610 static struct cmd_list_element *set_dwarf_cmdlist;
25611 static struct cmd_list_element *show_dwarf_cmdlist;
25614 set_dwarf_cmd (const char *args, int from_tty)
25616 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25621 show_dwarf_cmd (const char *args, int from_tty)
25623 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25626 /* Free data associated with OBJFILE, if necessary. */
25629 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25631 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25634 for (ix = 0; ix < data->n_comp_units; ++ix)
25635 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25637 for (ix = 0; ix < data->n_type_units; ++ix)
25638 VEC_free (dwarf2_per_cu_ptr,
25639 data->all_type_units[ix]->per_cu.imported_symtabs);
25640 xfree (data->all_type_units);
25642 VEC_free (dwarf2_section_info_def, data->types);
25644 if (data->dwo_files)
25645 free_dwo_files (data->dwo_files, objfile);
25646 if (data->dwp_file)
25647 gdb_bfd_unref (data->dwp_file->dbfd);
25649 if (data->dwz_file && data->dwz_file->dwz_bfd)
25650 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25652 if (data->index_table != NULL)
25653 data->index_table->~mapped_index ();
25657 /* The "save gdb-index" command. */
25659 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25663 file_write (FILE *file, const void *data, size_t size)
25665 if (fwrite (data, 1, size, file) != size)
25666 error (_("couldn't data write to file"));
25669 /* Write the contents of VEC to FILE, with error checking. */
25671 template<typename Elem, typename Alloc>
25673 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25675 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25678 /* In-memory buffer to prepare data to be written later to a file. */
25682 /* Copy DATA to the end of the buffer. */
25683 template<typename T>
25684 void append_data (const T &data)
25686 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25687 reinterpret_cast<const gdb_byte *> (&data + 1),
25688 grow (sizeof (data)));
25691 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25692 terminating zero is appended too. */
25693 void append_cstr0 (const char *cstr)
25695 const size_t size = strlen (cstr) + 1;
25696 std::copy (cstr, cstr + size, grow (size));
25699 /* Store INPUT as ULEB128 to the end of buffer. */
25700 void append_unsigned_leb128 (ULONGEST input)
25704 gdb_byte output = input & 0x7f;
25708 append_data (output);
25714 /* Accept a host-format integer in VAL and append it to the buffer
25715 as a target-format integer which is LEN bytes long. */
25716 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25718 ::store_unsigned_integer (grow (len), len, byte_order, val);
25721 /* Return the size of the buffer. */
25722 size_t size () const
25724 return m_vec.size ();
25727 /* Return true iff the buffer is empty. */
25728 bool empty () const
25730 return m_vec.empty ();
25733 /* Write the buffer to FILE. */
25734 void file_write (FILE *file) const
25736 ::file_write (file, m_vec);
25740 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25741 the start of the new block. */
25742 gdb_byte *grow (size_t size)
25744 m_vec.resize (m_vec.size () + size);
25745 return &*m_vec.end () - size;
25748 gdb::byte_vector m_vec;
25751 /* An entry in the symbol table. */
25752 struct symtab_index_entry
25754 /* The name of the symbol. */
25756 /* The offset of the name in the constant pool. */
25757 offset_type index_offset;
25758 /* A sorted vector of the indices of all the CUs that hold an object
25760 std::vector<offset_type> cu_indices;
25763 /* The symbol table. This is a power-of-2-sized hash table. */
25764 struct mapped_symtab
25768 data.resize (1024);
25771 offset_type n_elements = 0;
25772 std::vector<symtab_index_entry> data;
25775 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25778 Function is used only during write_hash_table so no index format backward
25779 compatibility is needed. */
25781 static symtab_index_entry &
25782 find_slot (struct mapped_symtab *symtab, const char *name)
25784 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25786 index = hash & (symtab->data.size () - 1);
25787 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25791 if (symtab->data[index].name == NULL
25792 || strcmp (name, symtab->data[index].name) == 0)
25793 return symtab->data[index];
25794 index = (index + step) & (symtab->data.size () - 1);
25798 /* Expand SYMTAB's hash table. */
25801 hash_expand (struct mapped_symtab *symtab)
25803 auto old_entries = std::move (symtab->data);
25805 symtab->data.clear ();
25806 symtab->data.resize (old_entries.size () * 2);
25808 for (auto &it : old_entries)
25809 if (it.name != NULL)
25811 auto &ref = find_slot (symtab, it.name);
25812 ref = std::move (it);
25816 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25817 CU_INDEX is the index of the CU in which the symbol appears.
25818 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25821 add_index_entry (struct mapped_symtab *symtab, const char *name,
25822 int is_static, gdb_index_symbol_kind kind,
25823 offset_type cu_index)
25825 offset_type cu_index_and_attrs;
25827 ++symtab->n_elements;
25828 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25829 hash_expand (symtab);
25831 symtab_index_entry &slot = find_slot (symtab, name);
25832 if (slot.name == NULL)
25835 /* index_offset is set later. */
25838 cu_index_and_attrs = 0;
25839 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25840 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25841 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25843 /* We don't want to record an index value twice as we want to avoid the
25845 We process all global symbols and then all static symbols
25846 (which would allow us to avoid the duplication by only having to check
25847 the last entry pushed), but a symbol could have multiple kinds in one CU.
25848 To keep things simple we don't worry about the duplication here and
25849 sort and uniqufy the list after we've processed all symbols. */
25850 slot.cu_indices.push_back (cu_index_and_attrs);
25853 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25856 uniquify_cu_indices (struct mapped_symtab *symtab)
25858 for (auto &entry : symtab->data)
25860 if (entry.name != NULL && !entry.cu_indices.empty ())
25862 auto &cu_indices = entry.cu_indices;
25863 std::sort (cu_indices.begin (), cu_indices.end ());
25864 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25865 cu_indices.erase (from, cu_indices.end ());
25870 /* A form of 'const char *' suitable for container keys. Only the
25871 pointer is stored. The strings themselves are compared, not the
25876 c_str_view (const char *cstr)
25880 bool operator== (const c_str_view &other) const
25882 return strcmp (m_cstr, other.m_cstr) == 0;
25885 /* Return the underlying C string. Note, the returned string is
25886 only a reference with lifetime of this object. */
25887 const char *c_str () const
25893 friend class c_str_view_hasher;
25894 const char *const m_cstr;
25897 /* A std::unordered_map::hasher for c_str_view that uses the right
25898 hash function for strings in a mapped index. */
25899 class c_str_view_hasher
25902 size_t operator () (const c_str_view &x) const
25904 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25908 /* A std::unordered_map::hasher for std::vector<>. */
25909 template<typename T>
25910 class vector_hasher
25913 size_t operator () (const std::vector<T> &key) const
25915 return iterative_hash (key.data (),
25916 sizeof (key.front ()) * key.size (), 0);
25920 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25921 constant pool entries going into the data buffer CPOOL. */
25924 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25927 /* Elements are sorted vectors of the indices of all the CUs that
25928 hold an object of this name. */
25929 std::unordered_map<std::vector<offset_type>, offset_type,
25930 vector_hasher<offset_type>>
25933 /* We add all the index vectors to the constant pool first, to
25934 ensure alignment is ok. */
25935 for (symtab_index_entry &entry : symtab->data)
25937 if (entry.name == NULL)
25939 gdb_assert (entry.index_offset == 0);
25941 /* Finding before inserting is faster than always trying to
25942 insert, because inserting always allocates a node, does the
25943 lookup, and then destroys the new node if another node
25944 already had the same key. C++17 try_emplace will avoid
25947 = symbol_hash_table.find (entry.cu_indices);
25948 if (found != symbol_hash_table.end ())
25950 entry.index_offset = found->second;
25954 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25955 entry.index_offset = cpool.size ();
25956 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25957 for (const auto index : entry.cu_indices)
25958 cpool.append_data (MAYBE_SWAP (index));
25962 /* Now write out the hash table. */
25963 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25964 for (const auto &entry : symtab->data)
25966 offset_type str_off, vec_off;
25968 if (entry.name != NULL)
25970 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25971 if (insertpair.second)
25972 cpool.append_cstr0 (entry.name);
25973 str_off = insertpair.first->second;
25974 vec_off = entry.index_offset;
25978 /* While 0 is a valid constant pool index, it is not valid
25979 to have 0 for both offsets. */
25984 output.append_data (MAYBE_SWAP (str_off));
25985 output.append_data (MAYBE_SWAP (vec_off));
25989 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25991 /* Helper struct for building the address table. */
25992 struct addrmap_index_data
25994 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25995 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25998 struct objfile *objfile;
25999 data_buf &addr_vec;
26000 psym_index_map &cu_index_htab;
26002 /* Non-zero if the previous_* fields are valid.
26003 We can't write an entry until we see the next entry (since it is only then
26004 that we know the end of the entry). */
26005 int previous_valid;
26006 /* Index of the CU in the table of all CUs in the index file. */
26007 unsigned int previous_cu_index;
26008 /* Start address of the CU. */
26009 CORE_ADDR previous_cu_start;
26012 /* Write an address entry to ADDR_VEC. */
26015 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26016 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26018 CORE_ADDR baseaddr;
26020 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26022 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26023 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26024 addr_vec.append_data (MAYBE_SWAP (cu_index));
26027 /* Worker function for traversing an addrmap to build the address table. */
26030 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26032 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26033 struct partial_symtab *pst = (struct partial_symtab *) obj;
26035 if (data->previous_valid)
26036 add_address_entry (data->objfile, data->addr_vec,
26037 data->previous_cu_start, start_addr,
26038 data->previous_cu_index);
26040 data->previous_cu_start = start_addr;
26043 const auto it = data->cu_index_htab.find (pst);
26044 gdb_assert (it != data->cu_index_htab.cend ());
26045 data->previous_cu_index = it->second;
26046 data->previous_valid = 1;
26049 data->previous_valid = 0;
26054 /* Write OBJFILE's address map to ADDR_VEC.
26055 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26056 in the index file. */
26059 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26060 psym_index_map &cu_index_htab)
26062 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26064 /* When writing the address table, we have to cope with the fact that
26065 the addrmap iterator only provides the start of a region; we have to
26066 wait until the next invocation to get the start of the next region. */
26068 addrmap_index_data.objfile = objfile;
26069 addrmap_index_data.previous_valid = 0;
26071 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26072 &addrmap_index_data);
26074 /* It's highly unlikely the last entry (end address = 0xff...ff)
26075 is valid, but we should still handle it.
26076 The end address is recorded as the start of the next region, but that
26077 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26079 if (addrmap_index_data.previous_valid)
26080 add_address_entry (objfile, addr_vec,
26081 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26082 addrmap_index_data.previous_cu_index);
26085 /* Return the symbol kind of PSYM. */
26087 static gdb_index_symbol_kind
26088 symbol_kind (struct partial_symbol *psym)
26090 domain_enum domain = PSYMBOL_DOMAIN (psym);
26091 enum address_class aclass = PSYMBOL_CLASS (psym);
26099 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26101 return GDB_INDEX_SYMBOL_KIND_TYPE;
26103 case LOC_CONST_BYTES:
26104 case LOC_OPTIMIZED_OUT:
26106 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26108 /* Note: It's currently impossible to recognize psyms as enum values
26109 short of reading the type info. For now punt. */
26110 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26112 /* There are other LOC_FOO values that one might want to classify
26113 as variables, but dwarf2read.c doesn't currently use them. */
26114 return GDB_INDEX_SYMBOL_KIND_OTHER;
26116 case STRUCT_DOMAIN:
26117 return GDB_INDEX_SYMBOL_KIND_TYPE;
26119 return GDB_INDEX_SYMBOL_KIND_OTHER;
26123 /* Add a list of partial symbols to SYMTAB. */
26126 write_psymbols (struct mapped_symtab *symtab,
26127 std::unordered_set<partial_symbol *> &psyms_seen,
26128 struct partial_symbol **psymp,
26130 offset_type cu_index,
26133 for (; count-- > 0; ++psymp)
26135 struct partial_symbol *psym = *psymp;
26137 if (SYMBOL_LANGUAGE (psym) == language_ada)
26138 error (_("Ada is not currently supported by the index"));
26140 /* Only add a given psymbol once. */
26141 if (psyms_seen.insert (psym).second)
26143 gdb_index_symbol_kind kind = symbol_kind (psym);
26145 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26146 is_static, kind, cu_index);
26151 /* A helper struct used when iterating over debug_types. */
26152 struct signatured_type_index_data
26154 signatured_type_index_data (data_buf &types_list_,
26155 std::unordered_set<partial_symbol *> &psyms_seen_)
26156 : types_list (types_list_), psyms_seen (psyms_seen_)
26159 struct objfile *objfile;
26160 struct mapped_symtab *symtab;
26161 data_buf &types_list;
26162 std::unordered_set<partial_symbol *> &psyms_seen;
26166 /* A helper function that writes a single signatured_type to an
26170 write_one_signatured_type (void **slot, void *d)
26172 struct signatured_type_index_data *info
26173 = (struct signatured_type_index_data *) d;
26174 struct signatured_type *entry = (struct signatured_type *) *slot;
26175 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26177 write_psymbols (info->symtab,
26179 &info->objfile->global_psymbols[psymtab->globals_offset],
26180 psymtab->n_global_syms, info->cu_index,
26182 write_psymbols (info->symtab,
26184 &info->objfile->static_psymbols[psymtab->statics_offset],
26185 psymtab->n_static_syms, info->cu_index,
26188 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26189 to_underlying (entry->per_cu.sect_off));
26190 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26191 to_underlying (entry->type_offset_in_tu));
26192 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26199 /* Recurse into all "included" dependencies and count their symbols as
26200 if they appeared in this psymtab. */
26203 recursively_count_psymbols (struct partial_symtab *psymtab,
26204 size_t &psyms_seen)
26206 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26207 if (psymtab->dependencies[i]->user != NULL)
26208 recursively_count_psymbols (psymtab->dependencies[i],
26211 psyms_seen += psymtab->n_global_syms;
26212 psyms_seen += psymtab->n_static_syms;
26215 /* Recurse into all "included" dependencies and write their symbols as
26216 if they appeared in this psymtab. */
26219 recursively_write_psymbols (struct objfile *objfile,
26220 struct partial_symtab *psymtab,
26221 struct mapped_symtab *symtab,
26222 std::unordered_set<partial_symbol *> &psyms_seen,
26223 offset_type cu_index)
26227 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26228 if (psymtab->dependencies[i]->user != NULL)
26229 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26230 symtab, psyms_seen, cu_index);
26232 write_psymbols (symtab,
26234 &objfile->global_psymbols[psymtab->globals_offset],
26235 psymtab->n_global_syms, cu_index,
26237 write_psymbols (symtab,
26239 &objfile->static_psymbols[psymtab->statics_offset],
26240 psymtab->n_static_syms, cu_index,
26244 /* DWARF-5 .debug_names builder. */
26248 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26249 bfd_endian dwarf5_byte_order)
26250 : m_dwarf5_byte_order (dwarf5_byte_order),
26251 m_dwarf32 (dwarf5_byte_order),
26252 m_dwarf64 (dwarf5_byte_order),
26253 m_dwarf (is_dwarf64
26254 ? static_cast<dwarf &> (m_dwarf64)
26255 : static_cast<dwarf &> (m_dwarf32)),
26256 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26257 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26258 m_debugstrlookup (dwarf2_per_objfile)
26261 int dwarf5_offset_size () const
26263 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26264 return dwarf5_is_dwarf64 ? 8 : 4;
26267 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26268 enum class unit_kind { cu, tu };
26270 /* Insert one symbol. */
26271 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26274 const int dwarf_tag = psymbol_tag (psym);
26275 if (dwarf_tag == 0)
26277 const char *const name = SYMBOL_SEARCH_NAME (psym);
26278 const auto insertpair
26279 = m_name_to_value_set.emplace (c_str_view (name),
26280 std::set<symbol_value> ());
26281 std::set<symbol_value> &value_set = insertpair.first->second;
26282 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26285 /* Build all the tables. All symbols must be already inserted.
26286 This function does not call file_write, caller has to do it
26290 /* Verify the build method has not be called twice. */
26291 gdb_assert (m_abbrev_table.empty ());
26292 const size_t name_count = m_name_to_value_set.size ();
26293 m_bucket_table.resize
26294 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26295 m_hash_table.reserve (name_count);
26296 m_name_table_string_offs.reserve (name_count);
26297 m_name_table_entry_offs.reserve (name_count);
26299 /* Map each hash of symbol to its name and value. */
26300 struct hash_it_pair
26303 decltype (m_name_to_value_set)::const_iterator it;
26305 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26306 bucket_hash.resize (m_bucket_table.size ());
26307 for (decltype (m_name_to_value_set)::const_iterator it
26308 = m_name_to_value_set.cbegin ();
26309 it != m_name_to_value_set.cend ();
26312 const char *const name = it->first.c_str ();
26313 const uint32_t hash = dwarf5_djb_hash (name);
26314 hash_it_pair hashitpair;
26315 hashitpair.hash = hash;
26316 hashitpair.it = it;
26317 auto &slot = bucket_hash[hash % bucket_hash.size()];
26318 slot.push_front (std::move (hashitpair));
26320 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26322 const std::forward_list<hash_it_pair> &hashitlist
26323 = bucket_hash[bucket_ix];
26324 if (hashitlist.empty ())
26326 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26327 /* The hashes array is indexed starting at 1. */
26328 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26329 sizeof (bucket_slot), m_dwarf5_byte_order,
26330 m_hash_table.size () + 1);
26331 for (const hash_it_pair &hashitpair : hashitlist)
26333 m_hash_table.push_back (0);
26334 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26335 (&m_hash_table.back ()),
26336 sizeof (m_hash_table.back ()),
26337 m_dwarf5_byte_order, hashitpair.hash);
26338 const c_str_view &name = hashitpair.it->first;
26339 const std::set<symbol_value> &value_set = hashitpair.it->second;
26340 m_name_table_string_offs.push_back_reorder
26341 (m_debugstrlookup.lookup (name.c_str ()));
26342 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26343 gdb_assert (!value_set.empty ());
26344 for (const symbol_value &value : value_set)
26346 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26351 idx = m_idx_next++;
26352 m_abbrev_table.append_unsigned_leb128 (idx);
26353 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26354 m_abbrev_table.append_unsigned_leb128
26355 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26356 : DW_IDX_type_unit);
26357 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26358 m_abbrev_table.append_unsigned_leb128 (value.is_static
26359 ? DW_IDX_GNU_internal
26360 : DW_IDX_GNU_external);
26361 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26363 /* Terminate attributes list. */
26364 m_abbrev_table.append_unsigned_leb128 (0);
26365 m_abbrev_table.append_unsigned_leb128 (0);
26368 m_entry_pool.append_unsigned_leb128 (idx);
26369 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26372 /* Terminate the list of CUs. */
26373 m_entry_pool.append_unsigned_leb128 (0);
26376 gdb_assert (m_hash_table.size () == name_count);
26378 /* Terminate tags list. */
26379 m_abbrev_table.append_unsigned_leb128 (0);
26382 /* Return .debug_names bucket count. This must be called only after
26383 calling the build method. */
26384 uint32_t bucket_count () const
26386 /* Verify the build method has been already called. */
26387 gdb_assert (!m_abbrev_table.empty ());
26388 const uint32_t retval = m_bucket_table.size ();
26390 /* Check for overflow. */
26391 gdb_assert (retval == m_bucket_table.size ());
26395 /* Return .debug_names names count. This must be called only after
26396 calling the build method. */
26397 uint32_t name_count () const
26399 /* Verify the build method has been already called. */
26400 gdb_assert (!m_abbrev_table.empty ());
26401 const uint32_t retval = m_hash_table.size ();
26403 /* Check for overflow. */
26404 gdb_assert (retval == m_hash_table.size ());
26408 /* Return number of bytes of .debug_names abbreviation table. This
26409 must be called only after calling the build method. */
26410 uint32_t abbrev_table_bytes () const
26412 gdb_assert (!m_abbrev_table.empty ());
26413 return m_abbrev_table.size ();
26416 /* Recurse into all "included" dependencies and store their symbols
26417 as if they appeared in this psymtab. */
26418 void recursively_write_psymbols
26419 (struct objfile *objfile,
26420 struct partial_symtab *psymtab,
26421 std::unordered_set<partial_symbol *> &psyms_seen,
26424 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26425 if (psymtab->dependencies[i]->user != NULL)
26426 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26427 psyms_seen, cu_index);
26429 write_psymbols (psyms_seen,
26430 &objfile->global_psymbols[psymtab->globals_offset],
26431 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26432 write_psymbols (psyms_seen,
26433 &objfile->static_psymbols[psymtab->statics_offset],
26434 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26437 /* Return number of bytes the .debug_names section will have. This
26438 must be called only after calling the build method. */
26439 size_t bytes () const
26441 /* Verify the build method has been already called. */
26442 gdb_assert (!m_abbrev_table.empty ());
26443 size_t expected_bytes = 0;
26444 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26445 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26446 expected_bytes += m_name_table_string_offs.bytes ();
26447 expected_bytes += m_name_table_entry_offs.bytes ();
26448 expected_bytes += m_abbrev_table.size ();
26449 expected_bytes += m_entry_pool.size ();
26450 return expected_bytes;
26453 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26454 FILE_STR. This must be called only after calling the build
26456 void file_write (FILE *file_names, FILE *file_str) const
26458 /* Verify the build method has been already called. */
26459 gdb_assert (!m_abbrev_table.empty ());
26460 ::file_write (file_names, m_bucket_table);
26461 ::file_write (file_names, m_hash_table);
26462 m_name_table_string_offs.file_write (file_names);
26463 m_name_table_entry_offs.file_write (file_names);
26464 m_abbrev_table.file_write (file_names);
26465 m_entry_pool.file_write (file_names);
26466 m_debugstrlookup.file_write (file_str);
26469 /* A helper user data for write_one_signatured_type. */
26470 class write_one_signatured_type_data
26473 write_one_signatured_type_data (debug_names &nametable_,
26474 signatured_type_index_data &&info_)
26475 : nametable (nametable_), info (std::move (info_))
26477 debug_names &nametable;
26478 struct signatured_type_index_data info;
26481 /* A helper function to pass write_one_signatured_type to
26482 htab_traverse_noresize. */
26484 write_one_signatured_type (void **slot, void *d)
26486 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26487 struct signatured_type_index_data *info = &data->info;
26488 struct signatured_type *entry = (struct signatured_type *) *slot;
26490 data->nametable.write_one_signatured_type (entry, info);
26497 /* Storage for symbol names mapping them to their .debug_str section
26499 class debug_str_lookup
26503 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26504 All .debug_str section strings are automatically stored. */
26505 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26506 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26507 m_dwarf2_per_objfile (dwarf2_per_objfile)
26509 dwarf2_read_section (dwarf2_per_objfile->objfile,
26510 &dwarf2_per_objfile->str);
26511 if (dwarf2_per_objfile->str.buffer == NULL)
26513 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26514 data < (dwarf2_per_objfile->str.buffer
26515 + dwarf2_per_objfile->str.size);)
26517 const char *const s = reinterpret_cast<const char *> (data);
26518 const auto insertpair
26519 = m_str_table.emplace (c_str_view (s),
26520 data - dwarf2_per_objfile->str.buffer);
26521 if (!insertpair.second)
26522 complaint (&symfile_complaints,
26523 _("Duplicate string \"%s\" in "
26524 ".debug_str section [in module %s]"),
26525 s, bfd_get_filename (m_abfd));
26526 data += strlen (s) + 1;
26530 /* Return offset of symbol name S in the .debug_str section. Add
26531 such symbol to the section's end if it does not exist there
26533 size_t lookup (const char *s)
26535 const auto it = m_str_table.find (c_str_view (s));
26536 if (it != m_str_table.end ())
26538 const size_t offset = (m_dwarf2_per_objfile->str.size
26539 + m_str_add_buf.size ());
26540 m_str_table.emplace (c_str_view (s), offset);
26541 m_str_add_buf.append_cstr0 (s);
26545 /* Append the end of the .debug_str section to FILE. */
26546 void file_write (FILE *file) const
26548 m_str_add_buf.file_write (file);
26552 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26554 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26556 /* Data to add at the end of .debug_str for new needed symbol names. */
26557 data_buf m_str_add_buf;
26560 /* Container to map used DWARF tags to their .debug_names abbreviation
26565 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26566 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26571 operator== (const index_key &other) const
26573 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26574 && kind == other.kind);
26577 const int dwarf_tag;
26578 const bool is_static;
26579 const unit_kind kind;
26582 /* Provide std::unordered_map::hasher for index_key. */
26583 class index_key_hasher
26587 operator () (const index_key &key) const
26589 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26593 /* Parameters of one symbol entry. */
26597 const int dwarf_tag, cu_index;
26598 const bool is_static;
26599 const unit_kind kind;
26601 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26603 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26608 operator< (const symbol_value &other) const
26628 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26633 const bfd_endian dwarf5_byte_order;
26635 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26636 : dwarf5_byte_order (dwarf5_byte_order_)
26639 /* Call std::vector::reserve for NELEM elements. */
26640 virtual void reserve (size_t nelem) = 0;
26642 /* Call std::vector::push_back with store_unsigned_integer byte
26643 reordering for ELEM. */
26644 virtual void push_back_reorder (size_t elem) = 0;
26646 /* Return expected output size in bytes. */
26647 virtual size_t bytes () const = 0;
26649 /* Write name table to FILE. */
26650 virtual void file_write (FILE *file) const = 0;
26653 /* Template to unify DWARF-32 and DWARF-64 output. */
26654 template<typename OffsetSize>
26655 class offset_vec_tmpl : public offset_vec
26658 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26659 : offset_vec (dwarf5_byte_order_)
26662 /* Implement offset_vec::reserve. */
26663 void reserve (size_t nelem) override
26665 m_vec.reserve (nelem);
26668 /* Implement offset_vec::push_back_reorder. */
26669 void push_back_reorder (size_t elem) override
26671 m_vec.push_back (elem);
26672 /* Check for overflow. */
26673 gdb_assert (m_vec.back () == elem);
26674 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26675 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26678 /* Implement offset_vec::bytes. */
26679 size_t bytes () const override
26681 return m_vec.size () * sizeof (m_vec[0]);
26684 /* Implement offset_vec::file_write. */
26685 void file_write (FILE *file) const override
26687 ::file_write (file, m_vec);
26691 std::vector<OffsetSize> m_vec;
26694 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26695 respecting name table width. */
26699 offset_vec &name_table_string_offs, &name_table_entry_offs;
26701 dwarf (offset_vec &name_table_string_offs_,
26702 offset_vec &name_table_entry_offs_)
26703 : name_table_string_offs (name_table_string_offs_),
26704 name_table_entry_offs (name_table_entry_offs_)
26709 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26710 respecting name table width. */
26711 template<typename OffsetSize>
26712 class dwarf_tmpl : public dwarf
26715 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26716 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26717 m_name_table_string_offs (dwarf5_byte_order_),
26718 m_name_table_entry_offs (dwarf5_byte_order_)
26722 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26723 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26726 /* Try to reconstruct original DWARF tag for given partial_symbol.
26727 This function is not DWARF-5 compliant but it is sufficient for
26728 GDB as a DWARF-5 index consumer. */
26729 static int psymbol_tag (const struct partial_symbol *psym)
26731 domain_enum domain = PSYMBOL_DOMAIN (psym);
26732 enum address_class aclass = PSYMBOL_CLASS (psym);
26740 return DW_TAG_subprogram;
26742 return DW_TAG_typedef;
26744 case LOC_CONST_BYTES:
26745 case LOC_OPTIMIZED_OUT:
26747 return DW_TAG_variable;
26749 /* Note: It's currently impossible to recognize psyms as enum values
26750 short of reading the type info. For now punt. */
26751 return DW_TAG_variable;
26753 /* There are other LOC_FOO values that one might want to classify
26754 as variables, but dwarf2read.c doesn't currently use them. */
26755 return DW_TAG_variable;
26757 case STRUCT_DOMAIN:
26758 return DW_TAG_structure_type;
26764 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26765 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26766 struct partial_symbol **psymp, int count, int cu_index,
26767 bool is_static, unit_kind kind)
26769 for (; count-- > 0; ++psymp)
26771 struct partial_symbol *psym = *psymp;
26773 if (SYMBOL_LANGUAGE (psym) == language_ada)
26774 error (_("Ada is not currently supported by the index"));
26776 /* Only add a given psymbol once. */
26777 if (psyms_seen.insert (psym).second)
26778 insert (psym, cu_index, is_static, kind);
26782 /* A helper function that writes a single signatured_type
26783 to a debug_names. */
26785 write_one_signatured_type (struct signatured_type *entry,
26786 struct signatured_type_index_data *info)
26788 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26790 write_psymbols (info->psyms_seen,
26791 &info->objfile->global_psymbols[psymtab->globals_offset],
26792 psymtab->n_global_syms, info->cu_index, false,
26794 write_psymbols (info->psyms_seen,
26795 &info->objfile->static_psymbols[psymtab->statics_offset],
26796 psymtab->n_static_syms, info->cu_index, true,
26799 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26800 to_underlying (entry->per_cu.sect_off));
26805 /* Store value of each symbol. */
26806 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26807 m_name_to_value_set;
26809 /* Tables of DWARF-5 .debug_names. They are in object file byte
26811 std::vector<uint32_t> m_bucket_table;
26812 std::vector<uint32_t> m_hash_table;
26814 const bfd_endian m_dwarf5_byte_order;
26815 dwarf_tmpl<uint32_t> m_dwarf32;
26816 dwarf_tmpl<uint64_t> m_dwarf64;
26818 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26819 debug_str_lookup m_debugstrlookup;
26821 /* Map each used .debug_names abbreviation tag parameter to its
26823 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26825 /* Next unused .debug_names abbreviation tag for
26826 m_indexkey_to_idx. */
26827 int m_idx_next = 1;
26829 /* .debug_names abbreviation table. */
26830 data_buf m_abbrev_table;
26832 /* .debug_names entry pool. */
26833 data_buf m_entry_pool;
26836 /* Return iff any of the needed offsets does not fit into 32-bit
26837 .debug_names section. */
26840 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26842 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26844 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26846 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26849 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26851 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26852 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26854 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26860 /* The psyms_seen set is potentially going to be largish (~40k
26861 elements when indexing a -g3 build of GDB itself). Estimate the
26862 number of elements in order to avoid too many rehashes, which
26863 require rebuilding buckets and thus many trips to
26867 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26869 size_t psyms_count = 0;
26870 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26872 struct dwarf2_per_cu_data *per_cu
26873 = dwarf2_per_objfile->all_comp_units[i];
26874 struct partial_symtab *psymtab = per_cu->v.psymtab;
26876 if (psymtab != NULL && psymtab->user == NULL)
26877 recursively_count_psymbols (psymtab, psyms_count);
26879 /* Generating an index for gdb itself shows a ratio of
26880 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26881 return psyms_count / 4;
26884 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26885 Return how many bytes were expected to be written into OUT_FILE. */
26888 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26890 struct objfile *objfile = dwarf2_per_objfile->objfile;
26891 mapped_symtab symtab;
26894 /* While we're scanning CU's create a table that maps a psymtab pointer
26895 (which is what addrmap records) to its index (which is what is recorded
26896 in the index file). This will later be needed to write the address
26898 psym_index_map cu_index_htab;
26899 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26901 /* The CU list is already sorted, so we don't need to do additional
26902 work here. Also, the debug_types entries do not appear in
26903 all_comp_units, but only in their own hash table. */
26905 std::unordered_set<partial_symbol *> psyms_seen
26906 (psyms_seen_size (dwarf2_per_objfile));
26907 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26909 struct dwarf2_per_cu_data *per_cu
26910 = dwarf2_per_objfile->all_comp_units[i];
26911 struct partial_symtab *psymtab = per_cu->v.psymtab;
26913 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26914 It may be referenced from a local scope but in such case it does not
26915 need to be present in .gdb_index. */
26916 if (psymtab == NULL)
26919 if (psymtab->user == NULL)
26920 recursively_write_psymbols (objfile, psymtab, &symtab,
26923 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26924 gdb_assert (insertpair.second);
26926 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26927 to_underlying (per_cu->sect_off));
26928 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26931 /* Dump the address map. */
26933 write_address_map (objfile, addr_vec, cu_index_htab);
26935 /* Write out the .debug_type entries, if any. */
26936 data_buf types_cu_list;
26937 if (dwarf2_per_objfile->signatured_types)
26939 signatured_type_index_data sig_data (types_cu_list,
26942 sig_data.objfile = objfile;
26943 sig_data.symtab = &symtab;
26944 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26945 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26946 write_one_signatured_type, &sig_data);
26949 /* Now that we've processed all symbols we can shrink their cu_indices
26951 uniquify_cu_indices (&symtab);
26953 data_buf symtab_vec, constant_pool;
26954 write_hash_table (&symtab, symtab_vec, constant_pool);
26957 const offset_type size_of_contents = 6 * sizeof (offset_type);
26958 offset_type total_len = size_of_contents;
26960 /* The version number. */
26961 contents.append_data (MAYBE_SWAP (8));
26963 /* The offset of the CU list from the start of the file. */
26964 contents.append_data (MAYBE_SWAP (total_len));
26965 total_len += cu_list.size ();
26967 /* The offset of the types CU list from the start of the file. */
26968 contents.append_data (MAYBE_SWAP (total_len));
26969 total_len += types_cu_list.size ();
26971 /* The offset of the address table from the start of the file. */
26972 contents.append_data (MAYBE_SWAP (total_len));
26973 total_len += addr_vec.size ();
26975 /* The offset of the symbol table from the start of the file. */
26976 contents.append_data (MAYBE_SWAP (total_len));
26977 total_len += symtab_vec.size ();
26979 /* The offset of the constant pool from the start of the file. */
26980 contents.append_data (MAYBE_SWAP (total_len));
26981 total_len += constant_pool.size ();
26983 gdb_assert (contents.size () == size_of_contents);
26985 contents.file_write (out_file);
26986 cu_list.file_write (out_file);
26987 types_cu_list.file_write (out_file);
26988 addr_vec.file_write (out_file);
26989 symtab_vec.file_write (out_file);
26990 constant_pool.file_write (out_file);
26995 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26996 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26998 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26999 needed addition to .debug_str section to OUT_FILE_STR. Return how
27000 many bytes were expected to be written into OUT_FILE. */
27003 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27004 FILE *out_file, FILE *out_file_str)
27006 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27007 struct objfile *objfile = dwarf2_per_objfile->objfile;
27008 const enum bfd_endian dwarf5_byte_order
27009 = gdbarch_byte_order (get_objfile_arch (objfile));
27011 /* The CU list is already sorted, so we don't need to do additional
27012 work here. Also, the debug_types entries do not appear in
27013 all_comp_units, but only in their own hash table. */
27015 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27016 dwarf5_byte_order);
27017 std::unordered_set<partial_symbol *>
27018 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27019 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27021 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27022 partial_symtab *psymtab = per_cu->v.psymtab;
27024 /* CU of a shared file from 'dwz -m' may be unused by this main
27025 file. It may be referenced from a local scope but in such
27026 case it does not need to be present in .debug_names. */
27027 if (psymtab == NULL)
27030 if (psymtab->user == NULL)
27031 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27033 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27034 to_underlying (per_cu->sect_off));
27037 /* Write out the .debug_type entries, if any. */
27038 data_buf types_cu_list;
27039 if (dwarf2_per_objfile->signatured_types)
27041 debug_names::write_one_signatured_type_data sig_data (nametable,
27042 signatured_type_index_data (types_cu_list, psyms_seen));
27044 sig_data.info.objfile = objfile;
27045 /* It is used only for gdb_index. */
27046 sig_data.info.symtab = nullptr;
27047 sig_data.info.cu_index = 0;
27048 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27049 debug_names::write_one_signatured_type,
27053 nametable.build ();
27055 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27057 const offset_type bytes_of_header
27058 = ((dwarf5_is_dwarf64 ? 12 : 4)
27060 + sizeof (dwarf5_gdb_augmentation));
27061 size_t expected_bytes = 0;
27062 expected_bytes += bytes_of_header;
27063 expected_bytes += cu_list.size ();
27064 expected_bytes += types_cu_list.size ();
27065 expected_bytes += nametable.bytes ();
27068 if (!dwarf5_is_dwarf64)
27070 const uint64_t size64 = expected_bytes - 4;
27071 gdb_assert (size64 < 0xfffffff0);
27072 header.append_uint (4, dwarf5_byte_order, size64);
27076 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27077 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27080 /* The version number. */
27081 header.append_uint (2, dwarf5_byte_order, 5);
27084 header.append_uint (2, dwarf5_byte_order, 0);
27086 /* comp_unit_count - The number of CUs in the CU list. */
27087 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27089 /* local_type_unit_count - The number of TUs in the local TU
27091 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27093 /* foreign_type_unit_count - The number of TUs in the foreign TU
27095 header.append_uint (4, dwarf5_byte_order, 0);
27097 /* bucket_count - The number of hash buckets in the hash lookup
27099 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27101 /* name_count - The number of unique names in the index. */
27102 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27104 /* abbrev_table_size - The size in bytes of the abbreviations
27106 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27108 /* augmentation_string_size - The size in bytes of the augmentation
27109 string. This value is rounded up to a multiple of 4. */
27110 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27111 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27112 header.append_data (dwarf5_gdb_augmentation);
27114 gdb_assert (header.size () == bytes_of_header);
27116 header.file_write (out_file);
27117 cu_list.file_write (out_file);
27118 types_cu_list.file_write (out_file);
27119 nametable.file_write (out_file, out_file_str);
27121 return expected_bytes;
27124 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27125 position is at the end of the file. */
27128 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27130 const auto file_size = ftell (file);
27131 if (file_size == -1)
27132 error (_("Can't get `%s' size"), filename);
27133 gdb_assert (file_size == expected_size);
27136 /* Create an index file for OBJFILE in the directory DIR. */
27139 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27141 dw_index_kind index_kind)
27143 struct objfile *objfile = dwarf2_per_objfile->objfile;
27145 if (dwarf2_per_objfile->using_index)
27146 error (_("Cannot use an index to create the index"));
27148 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27149 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27151 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27155 if (stat (objfile_name (objfile), &st) < 0)
27156 perror_with_name (objfile_name (objfile));
27158 std::string filename (std::string (dir) + SLASH_STRING
27159 + lbasename (objfile_name (objfile))
27160 + (index_kind == dw_index_kind::DEBUG_NAMES
27161 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27163 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27165 error (_("Can't open `%s' for writing"), filename.c_str ());
27167 /* Order matters here; we want FILE to be closed before FILENAME is
27168 unlinked, because on MS-Windows one cannot delete a file that is
27169 still open. (Don't call anything here that might throw until
27170 file_closer is created.) */
27171 gdb::unlinker unlink_file (filename.c_str ());
27172 gdb_file_up close_out_file (out_file);
27174 if (index_kind == dw_index_kind::DEBUG_NAMES)
27176 std::string filename_str (std::string (dir) + SLASH_STRING
27177 + lbasename (objfile_name (objfile))
27178 + DEBUG_STR_SUFFIX);
27180 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27182 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27183 gdb::unlinker unlink_file_str (filename_str.c_str ());
27184 gdb_file_up close_out_file_str (out_file_str);
27186 const size_t total_len
27187 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27188 assert_file_size (out_file, filename.c_str (), total_len);
27190 /* We want to keep the file .debug_str file too. */
27191 unlink_file_str.keep ();
27195 const size_t total_len
27196 = write_gdbindex (dwarf2_per_objfile, out_file);
27197 assert_file_size (out_file, filename.c_str (), total_len);
27200 /* We want to keep the file. */
27201 unlink_file.keep ();
27204 /* Implementation of the `save gdb-index' command.
27206 Note that the .gdb_index file format used by this command is
27207 documented in the GDB manual. Any changes here must be documented
27211 save_gdb_index_command (const char *arg, int from_tty)
27213 struct objfile *objfile;
27214 const char dwarf5space[] = "-dwarf-5 ";
27215 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27220 arg = skip_spaces (arg);
27221 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27223 index_kind = dw_index_kind::DEBUG_NAMES;
27224 arg += strlen (dwarf5space);
27225 arg = skip_spaces (arg);
27229 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27231 ALL_OBJFILES (objfile)
27235 /* If the objfile does not correspond to an actual file, skip it. */
27236 if (stat (objfile_name (objfile), &st) < 0)
27239 struct dwarf2_per_objfile *dwarf2_per_objfile
27240 = get_dwarf2_per_objfile (objfile);
27242 if (dwarf2_per_objfile != NULL)
27246 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27248 CATCH (except, RETURN_MASK_ERROR)
27250 exception_fprintf (gdb_stderr, except,
27251 _("Error while writing index for `%s': "),
27252 objfile_name (objfile));
27262 int dwarf_always_disassemble;
27265 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27266 struct cmd_list_element *c, const char *value)
27268 fprintf_filtered (file,
27269 _("Whether to always disassemble "
27270 "DWARF expressions is %s.\n"),
27275 show_check_physname (struct ui_file *file, int from_tty,
27276 struct cmd_list_element *c, const char *value)
27278 fprintf_filtered (file,
27279 _("Whether to check \"physname\" is %s.\n"),
27284 _initialize_dwarf2_read (void)
27286 struct cmd_list_element *c;
27288 dwarf2_objfile_data_key
27289 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
27291 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27292 Set DWARF specific variables.\n\
27293 Configure DWARF variables such as the cache size"),
27294 &set_dwarf_cmdlist, "maintenance set dwarf ",
27295 0/*allow-unknown*/, &maintenance_set_cmdlist);
27297 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27298 Show DWARF specific variables\n\
27299 Show DWARF variables such as the cache size"),
27300 &show_dwarf_cmdlist, "maintenance show dwarf ",
27301 0/*allow-unknown*/, &maintenance_show_cmdlist);
27303 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27304 &dwarf_max_cache_age, _("\
27305 Set the upper bound on the age of cached DWARF compilation units."), _("\
27306 Show the upper bound on the age of cached DWARF compilation units."), _("\
27307 A higher limit means that cached compilation units will be stored\n\
27308 in memory longer, and more total memory will be used. Zero disables\n\
27309 caching, which can slow down startup."),
27311 show_dwarf_max_cache_age,
27312 &set_dwarf_cmdlist,
27313 &show_dwarf_cmdlist);
27315 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27316 &dwarf_always_disassemble, _("\
27317 Set whether `info address' always disassembles DWARF expressions."), _("\
27318 Show whether `info address' always disassembles DWARF expressions."), _("\
27319 When enabled, DWARF expressions are always printed in an assembly-like\n\
27320 syntax. When disabled, expressions will be printed in a more\n\
27321 conversational style, when possible."),
27323 show_dwarf_always_disassemble,
27324 &set_dwarf_cmdlist,
27325 &show_dwarf_cmdlist);
27327 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27328 Set debugging of the DWARF reader."), _("\
27329 Show debugging of the DWARF reader."), _("\
27330 When enabled (non-zero), debugging messages are printed during DWARF\n\
27331 reading and symtab expansion. A value of 1 (one) provides basic\n\
27332 information. A value greater than 1 provides more verbose information."),
27335 &setdebuglist, &showdebuglist);
27337 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27338 Set debugging of the DWARF DIE reader."), _("\
27339 Show debugging of the DWARF DIE reader."), _("\
27340 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27341 The value is the maximum depth to print."),
27344 &setdebuglist, &showdebuglist);
27346 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27347 Set debugging of the dwarf line reader."), _("\
27348 Show debugging of the dwarf line reader."), _("\
27349 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27350 A value of 1 (one) provides basic information.\n\
27351 A value greater than 1 provides more verbose information."),
27354 &setdebuglist, &showdebuglist);
27356 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27357 Set cross-checking of \"physname\" code against demangler."), _("\
27358 Show cross-checking of \"physname\" code against demangler."), _("\
27359 When enabled, GDB's internal \"physname\" code is checked against\n\
27361 NULL, show_check_physname,
27362 &setdebuglist, &showdebuglist);
27364 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27365 no_class, &use_deprecated_index_sections, _("\
27366 Set whether to use deprecated gdb_index sections."), _("\
27367 Show whether to use deprecated gdb_index sections."), _("\
27368 When enabled, deprecated .gdb_index sections are used anyway.\n\
27369 Normally they are ignored either because of a missing feature or\n\
27370 performance issue.\n\
27371 Warning: This option must be enabled before gdb reads the file."),
27374 &setlist, &showlist);
27376 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27378 Save a gdb-index file.\n\
27379 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27381 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27382 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27383 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27385 set_cmd_completer (c, filename_completer);
27387 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27388 &dwarf2_locexpr_funcs);
27389 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27390 &dwarf2_loclist_funcs);
27392 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27393 &dwarf2_block_frame_base_locexpr_funcs);
27394 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27395 &dwarf2_block_frame_base_loclist_funcs);
27398 selftests::register_test ("dw2_expand_symtabs_matching",
27399 selftests::dw2_expand_symtabs_matching::run_test);