1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
45 #include "complaints.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
55 #include "typeprint.h"
58 #include "completer.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
69 #include "filestuff.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "filename-seen-cache.h"
80 #include <sys/types.h>
82 #include <unordered_set>
83 #include <unordered_map>
86 /* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89 static unsigned int dwarf_read_debug = 0;
91 /* When non-zero, dump DIEs after they are read in. */
92 static unsigned int dwarf_die_debug = 0;
94 /* When non-zero, dump line number entries as they are read in. */
95 static unsigned int dwarf_line_debug = 0;
97 /* When non-zero, cross-check physname against demangler. */
98 static int check_physname = 0;
100 /* When non-zero, do not reject deprecated .gdb_index sections. */
101 static int use_deprecated_index_sections = 0;
103 static const struct objfile_data *dwarf2_objfile_data_key;
105 /* The "aclass" indices for various kinds of computed DWARF symbols. */
107 static int dwarf2_locexpr_index;
108 static int dwarf2_loclist_index;
109 static int dwarf2_locexpr_block_index;
110 static int dwarf2_loclist_block_index;
112 /* A descriptor for dwarf sections.
114 S.ASECTION, SIZE are typically initialized when the objfile is first
115 scanned. BUFFER, READIN are filled in later when the section is read.
116 If the section contained compressed data then SIZE is updated to record
117 the uncompressed size of the section.
119 DWP file format V2 introduces a wrinkle that is easiest to handle by
120 creating the concept of virtual sections contained within a real section.
121 In DWP V2 the sections of the input DWO files are concatenated together
122 into one section, but section offsets are kept relative to the original
124 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
125 the real section this "virtual" section is contained in, and BUFFER,SIZE
126 describe the virtual section. */
128 struct dwarf2_section_info
132 /* If this is a real section, the bfd section. */
134 /* If this is a virtual section, pointer to the containing ("real")
136 struct dwarf2_section_info *containing_section;
138 /* Pointer to section data, only valid if readin. */
139 const gdb_byte *buffer;
140 /* The size of the section, real or virtual. */
142 /* If this is a virtual section, the offset in the real section.
143 Only valid if is_virtual. */
144 bfd_size_type virtual_offset;
145 /* True if we have tried to read this section. */
147 /* True if this is a virtual section, False otherwise.
148 This specifies which of s.section and s.containing_section to use. */
152 typedef struct dwarf2_section_info dwarf2_section_info_def;
153 DEF_VEC_O (dwarf2_section_info_def);
155 /* All offsets in the index are of this type. It must be
156 architecture-independent. */
157 typedef uint32_t offset_type;
159 DEF_VEC_I (offset_type);
161 /* Ensure only legit values are used. */
162 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
164 gdb_assert ((unsigned int) (value) <= 1); \
165 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
168 /* Ensure only legit values are used. */
169 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
171 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
172 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
173 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
176 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
177 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
179 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
180 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 /* Convert VALUE between big- and little-endian. */
188 byte_swap (offset_type value)
192 result = (value & 0xff) << 24;
193 result |= (value & 0xff00) << 8;
194 result |= (value & 0xff0000) >> 8;
195 result |= (value & 0xff000000) >> 24;
199 #define MAYBE_SWAP(V) byte_swap (V)
202 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
203 #endif /* WORDS_BIGENDIAN */
205 /* An index into a (C++) symbol name component in a symbol name as
206 recorded in the mapped_index's symbol table. For each C++ symbol
207 in the symbol table, we record one entry for the start of each
208 component in the symbol in a table of name components, and then
209 sort the table, in order to be able to binary search symbol names,
210 ignoring leading namespaces, both completion and regular look up.
211 For example, for symbol "A::B::C", we'll have an entry that points
212 to "A::B::C", another that points to "B::C", and another for "C".
213 Note that function symbols in GDB index have no parameter
214 information, just the function/method names. You can convert a
215 name_component to a "const char *" using the
216 'mapped_index::symbol_name_at(offset_type)' method. */
218 struct name_component
220 /* Offset in the symbol name where the component starts. Stored as
221 a (32-bit) offset instead of a pointer to save memory and improve
222 locality on 64-bit architectures. */
223 offset_type name_offset;
225 /* The symbol's index in the symbol and constant pool tables of a
230 /* A description of the mapped index. The file format is described in
231 a comment by the code that writes the index. */
234 /* Index data format version. */
237 /* The total length of the buffer. */
240 /* A pointer to the address table data. */
241 const gdb_byte *address_table;
243 /* Size of the address table data in bytes. */
244 offset_type address_table_size;
246 /* The symbol table, implemented as a hash table. */
247 const offset_type *symbol_table;
249 /* Size in slots, each slot is 2 offset_types. */
250 offset_type symbol_table_slots;
252 /* A pointer to the constant pool. */
253 const char *constant_pool;
255 /* The name_component table (a sorted vector). See name_component's
256 description above. */
257 std::vector<name_component> name_components;
259 /* Convenience method to get at the name of the symbol at IDX in the
261 const char *symbol_name_at (offset_type idx) const
262 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
265 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
266 DEF_VEC_P (dwarf2_per_cu_ptr);
270 int nr_uniq_abbrev_tables;
272 int nr_symtab_sharers;
273 int nr_stmt_less_type_units;
274 int nr_all_type_units_reallocs;
277 /* Collection of data recorded per objfile.
278 This hangs off of dwarf2_objfile_data_key. */
280 struct dwarf2_per_objfile
282 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
283 dwarf2 section names, or is NULL if the standard ELF names are
285 dwarf2_per_objfile (struct objfile *objfile,
286 const dwarf2_debug_sections *names);
288 ~dwarf2_per_objfile ();
290 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
292 /* Free all cached compilation units. */
293 void free_cached_comp_units ();
295 /* This function is mapped across the sections and remembers the
296 offset and size of each of the debugging sections we are
298 void locate_sections (bfd *abfd, asection *sectp,
299 const dwarf2_debug_sections &names);
302 dwarf2_section_info info {};
303 dwarf2_section_info abbrev {};
304 dwarf2_section_info line {};
305 dwarf2_section_info loc {};
306 dwarf2_section_info loclists {};
307 dwarf2_section_info macinfo {};
308 dwarf2_section_info macro {};
309 dwarf2_section_info str {};
310 dwarf2_section_info line_str {};
311 dwarf2_section_info ranges {};
312 dwarf2_section_info rnglists {};
313 dwarf2_section_info addr {};
314 dwarf2_section_info frame {};
315 dwarf2_section_info eh_frame {};
316 dwarf2_section_info gdb_index {};
318 VEC (dwarf2_section_info_def) *types = NULL;
321 struct objfile *objfile = NULL;
323 /* Table of all the compilation units. This is used to locate
324 the target compilation unit of a particular reference. */
325 struct dwarf2_per_cu_data **all_comp_units = NULL;
327 /* The number of compilation units in ALL_COMP_UNITS. */
328 int n_comp_units = 0;
330 /* The number of .debug_types-related CUs. */
331 int n_type_units = 0;
333 /* The number of elements allocated in all_type_units.
334 If there are skeleton-less TUs, we add them to all_type_units lazily. */
335 int n_allocated_type_units = 0;
337 /* The .debug_types-related CUs (TUs).
338 This is stored in malloc space because we may realloc it. */
339 struct signatured_type **all_type_units = NULL;
341 /* Table of struct type_unit_group objects.
342 The hash key is the DW_AT_stmt_list value. */
343 htab_t type_unit_groups {};
345 /* A table mapping .debug_types signatures to its signatured_type entry.
346 This is NULL if the .debug_types section hasn't been read in yet. */
347 htab_t signatured_types {};
349 /* Type unit statistics, to see how well the scaling improvements
351 struct tu_stats tu_stats {};
353 /* A chain of compilation units that are currently read in, so that
354 they can be freed later. */
355 dwarf2_per_cu_data *read_in_chain = NULL;
357 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
358 This is NULL if the table hasn't been allocated yet. */
361 /* True if we've checked for whether there is a DWP file. */
362 bool dwp_checked = false;
364 /* The DWP file if there is one, or NULL. */
365 struct dwp_file *dwp_file = NULL;
367 /* The shared '.dwz' file, if one exists. This is used when the
368 original data was compressed using 'dwz -m'. */
369 struct dwz_file *dwz_file = NULL;
371 /* A flag indicating whether this objfile has a section loaded at a
373 bool has_section_at_zero = false;
375 /* True if we are using the mapped index,
376 or we are faking it for OBJF_READNOW's sake. */
377 bool using_index = false;
379 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
380 mapped_index *index_table = NULL;
382 /* When using index_table, this keeps track of all quick_file_names entries.
383 TUs typically share line table entries with a CU, so we maintain a
384 separate table of all line table entries to support the sharing.
385 Note that while there can be way more TUs than CUs, we've already
386 sorted all the TUs into "type unit groups", grouped by their
387 DW_AT_stmt_list value. Therefore the only sharing done here is with a
388 CU and its associated TU group if there is one. */
389 htab_t quick_file_names_table {};
391 /* Set during partial symbol reading, to prevent queueing of full
393 bool reading_partial_symbols = false;
395 /* Table mapping type DIEs to their struct type *.
396 This is NULL if not allocated yet.
397 The mapping is done via (CU/TU + DIE offset) -> type. */
398 htab_t die_type_hash {};
400 /* The CUs we recently read. */
401 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
403 /* Table containing line_header indexed by offset and offset_in_dwz. */
404 htab_t line_header_hash {};
406 /* Table containing all filenames. This is an optional because the
407 table is lazily constructed on first access. */
408 gdb::optional<filename_seen_cache> filenames_cache;
411 static struct dwarf2_per_objfile *dwarf2_per_objfile;
413 /* Default names of the debugging sections. */
415 /* Note that if the debugging section has been compressed, it might
416 have a name like .zdebug_info. */
418 static const struct dwarf2_debug_sections dwarf2_elf_names =
420 { ".debug_info", ".zdebug_info" },
421 { ".debug_abbrev", ".zdebug_abbrev" },
422 { ".debug_line", ".zdebug_line" },
423 { ".debug_loc", ".zdebug_loc" },
424 { ".debug_loclists", ".zdebug_loclists" },
425 { ".debug_macinfo", ".zdebug_macinfo" },
426 { ".debug_macro", ".zdebug_macro" },
427 { ".debug_str", ".zdebug_str" },
428 { ".debug_line_str", ".zdebug_line_str" },
429 { ".debug_ranges", ".zdebug_ranges" },
430 { ".debug_rnglists", ".zdebug_rnglists" },
431 { ".debug_types", ".zdebug_types" },
432 { ".debug_addr", ".zdebug_addr" },
433 { ".debug_frame", ".zdebug_frame" },
434 { ".eh_frame", NULL },
435 { ".gdb_index", ".zgdb_index" },
439 /* List of DWO/DWP sections. */
441 static const struct dwop_section_names
443 struct dwarf2_section_names abbrev_dwo;
444 struct dwarf2_section_names info_dwo;
445 struct dwarf2_section_names line_dwo;
446 struct dwarf2_section_names loc_dwo;
447 struct dwarf2_section_names loclists_dwo;
448 struct dwarf2_section_names macinfo_dwo;
449 struct dwarf2_section_names macro_dwo;
450 struct dwarf2_section_names str_dwo;
451 struct dwarf2_section_names str_offsets_dwo;
452 struct dwarf2_section_names types_dwo;
453 struct dwarf2_section_names cu_index;
454 struct dwarf2_section_names tu_index;
458 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
459 { ".debug_info.dwo", ".zdebug_info.dwo" },
460 { ".debug_line.dwo", ".zdebug_line.dwo" },
461 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
462 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
463 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
464 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
465 { ".debug_str.dwo", ".zdebug_str.dwo" },
466 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
467 { ".debug_types.dwo", ".zdebug_types.dwo" },
468 { ".debug_cu_index", ".zdebug_cu_index" },
469 { ".debug_tu_index", ".zdebug_tu_index" },
472 /* local data types */
474 /* The data in a compilation unit header, after target2host
475 translation, looks like this. */
476 struct comp_unit_head
480 unsigned char addr_size;
481 unsigned char signed_addr_p;
482 sect_offset abbrev_sect_off;
484 /* Size of file offsets; either 4 or 8. */
485 unsigned int offset_size;
487 /* Size of the length field; either 4 or 12. */
488 unsigned int initial_length_size;
490 enum dwarf_unit_type unit_type;
492 /* Offset to the first byte of this compilation unit header in the
493 .debug_info section, for resolving relative reference dies. */
494 sect_offset sect_off;
496 /* Offset to first die in this cu from the start of the cu.
497 This will be the first byte following the compilation unit header. */
498 cu_offset first_die_cu_offset;
500 /* 64-bit signature of this type unit - it is valid only for
501 UNIT_TYPE DW_UT_type. */
504 /* For types, offset in the type's DIE of the type defined by this TU. */
505 cu_offset type_cu_offset_in_tu;
508 /* Type used for delaying computation of method physnames.
509 See comments for compute_delayed_physnames. */
510 struct delayed_method_info
512 /* The type to which the method is attached, i.e., its parent class. */
515 /* The index of the method in the type's function fieldlists. */
518 /* The index of the method in the fieldlist. */
521 /* The name of the DIE. */
524 /* The DIE associated with this method. */
525 struct die_info *die;
528 typedef struct delayed_method_info delayed_method_info;
529 DEF_VEC_O (delayed_method_info);
531 /* Internal state when decoding a particular compilation unit. */
534 /* The objfile containing this compilation unit. */
535 struct objfile *objfile;
537 /* The header of the compilation unit. */
538 struct comp_unit_head header;
540 /* Base address of this compilation unit. */
541 CORE_ADDR base_address;
543 /* Non-zero if base_address has been set. */
546 /* The language we are debugging. */
547 enum language language;
548 const struct language_defn *language_defn;
550 const char *producer;
552 /* The generic symbol table building routines have separate lists for
553 file scope symbols and all all other scopes (local scopes). So
554 we need to select the right one to pass to add_symbol_to_list().
555 We do it by keeping a pointer to the correct list in list_in_scope.
557 FIXME: The original dwarf code just treated the file scope as the
558 first local scope, and all other local scopes as nested local
559 scopes, and worked fine. Check to see if we really need to
560 distinguish these in buildsym.c. */
561 struct pending **list_in_scope;
563 /* The abbrev table for this CU.
564 Normally this points to the abbrev table in the objfile.
565 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
566 struct abbrev_table *abbrev_table;
568 /* Hash table holding all the loaded partial DIEs
569 with partial_die->offset.SECT_OFF as hash. */
572 /* Storage for things with the same lifetime as this read-in compilation
573 unit, including partial DIEs. */
574 struct obstack comp_unit_obstack;
576 /* When multiple dwarf2_cu structures are living in memory, this field
577 chains them all together, so that they can be released efficiently.
578 We will probably also want a generation counter so that most-recently-used
579 compilation units are cached... */
580 struct dwarf2_per_cu_data *read_in_chain;
582 /* Backlink to our per_cu entry. */
583 struct dwarf2_per_cu_data *per_cu;
585 /* How many compilation units ago was this CU last referenced? */
588 /* A hash table of DIE cu_offset for following references with
589 die_info->offset.sect_off as hash. */
592 /* Full DIEs if read in. */
593 struct die_info *dies;
595 /* A set of pointers to dwarf2_per_cu_data objects for compilation
596 units referenced by this one. Only set during full symbol processing;
597 partial symbol tables do not have dependencies. */
600 /* Header data from the line table, during full symbol processing. */
601 struct line_header *line_header;
602 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
603 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
604 this is the DW_TAG_compile_unit die for this CU. We'll hold on
605 to the line header as long as this DIE is being processed. See
606 process_die_scope. */
607 die_info *line_header_die_owner;
609 /* A list of methods which need to have physnames computed
610 after all type information has been read. */
611 VEC (delayed_method_info) *method_list;
613 /* To be copied to symtab->call_site_htab. */
614 htab_t call_site_htab;
616 /* Non-NULL if this CU came from a DWO file.
617 There is an invariant here that is important to remember:
618 Except for attributes copied from the top level DIE in the "main"
619 (or "stub") file in preparation for reading the DWO file
620 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
621 Either there isn't a DWO file (in which case this is NULL and the point
622 is moot), or there is and either we're not going to read it (in which
623 case this is NULL) or there is and we are reading it (in which case this
625 struct dwo_unit *dwo_unit;
627 /* The DW_AT_addr_base attribute if present, zero otherwise
628 (zero is a valid value though).
629 Note this value comes from the Fission stub CU/TU's DIE. */
632 /* The DW_AT_ranges_base attribute if present, zero otherwise
633 (zero is a valid value though).
634 Note this value comes from the Fission stub CU/TU's DIE.
635 Also note that the value is zero in the non-DWO case so this value can
636 be used without needing to know whether DWO files are in use or not.
637 N.B. This does not apply to DW_AT_ranges appearing in
638 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
639 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
640 DW_AT_ranges_base *would* have to be applied, and we'd have to care
641 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
642 ULONGEST ranges_base;
644 /* Mark used when releasing cached dies. */
645 unsigned int mark : 1;
647 /* This CU references .debug_loc. See the symtab->locations_valid field.
648 This test is imperfect as there may exist optimized debug code not using
649 any location list and still facing inlining issues if handled as
650 unoptimized code. For a future better test see GCC PR other/32998. */
651 unsigned int has_loclist : 1;
653 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
654 if all the producer_is_* fields are valid. This information is cached
655 because profiling CU expansion showed excessive time spent in
656 producer_is_gxx_lt_4_6. */
657 unsigned int checked_producer : 1;
658 unsigned int producer_is_gxx_lt_4_6 : 1;
659 unsigned int producer_is_gcc_lt_4_3 : 1;
660 unsigned int producer_is_icc_lt_14 : 1;
662 /* When set, the file that we're processing is known to have
663 debugging info for C++ namespaces. GCC 3.3.x did not produce
664 this information, but later versions do. */
666 unsigned int processing_has_namespace_info : 1;
669 /* Persistent data held for a compilation unit, even when not
670 processing it. We put a pointer to this structure in the
671 read_symtab_private field of the psymtab. */
673 struct dwarf2_per_cu_data
675 /* The start offset and length of this compilation unit.
676 NOTE: Unlike comp_unit_head.length, this length includes
678 If the DIE refers to a DWO file, this is always of the original die,
680 sect_offset sect_off;
683 /* DWARF standard version this data has been read from (such as 4 or 5). */
686 /* Flag indicating this compilation unit will be read in before
687 any of the current compilation units are processed. */
688 unsigned int queued : 1;
690 /* This flag will be set when reading partial DIEs if we need to load
691 absolutely all DIEs for this compilation unit, instead of just the ones
692 we think are interesting. It gets set if we look for a DIE in the
693 hash table and don't find it. */
694 unsigned int load_all_dies : 1;
696 /* Non-zero if this CU is from .debug_types.
697 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
699 unsigned int is_debug_types : 1;
701 /* Non-zero if this CU is from the .dwz file. */
702 unsigned int is_dwz : 1;
704 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
705 This flag is only valid if is_debug_types is true.
706 We can't read a CU directly from a DWO file: There are required
707 attributes in the stub. */
708 unsigned int reading_dwo_directly : 1;
710 /* Non-zero if the TU has been read.
711 This is used to assist the "Stay in DWO Optimization" for Fission:
712 When reading a DWO, it's faster to read TUs from the DWO instead of
713 fetching them from random other DWOs (due to comdat folding).
714 If the TU has already been read, the optimization is unnecessary
715 (and unwise - we don't want to change where gdb thinks the TU lives
717 This flag is only valid if is_debug_types is true. */
718 unsigned int tu_read : 1;
720 /* The section this CU/TU lives in.
721 If the DIE refers to a DWO file, this is always the original die,
723 struct dwarf2_section_info *section;
725 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
726 of the CU cache it gets reset to NULL again. This is left as NULL for
727 dummy CUs (a CU header, but nothing else). */
728 struct dwarf2_cu *cu;
730 /* The corresponding objfile.
731 Normally we can get the objfile from dwarf2_per_objfile.
732 However we can enter this file with just a "per_cu" handle. */
733 struct objfile *objfile;
735 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
736 is active. Otherwise, the 'psymtab' field is active. */
739 /* The partial symbol table associated with this compilation unit,
740 or NULL for unread partial units. */
741 struct partial_symtab *psymtab;
743 /* Data needed by the "quick" functions. */
744 struct dwarf2_per_cu_quick_data *quick;
747 /* The CUs we import using DW_TAG_imported_unit. This is filled in
748 while reading psymtabs, used to compute the psymtab dependencies,
749 and then cleared. Then it is filled in again while reading full
750 symbols, and only deleted when the objfile is destroyed.
752 This is also used to work around a difference between the way gold
753 generates .gdb_index version <=7 and the way gdb does. Arguably this
754 is a gold bug. For symbols coming from TUs, gold records in the index
755 the CU that includes the TU instead of the TU itself. This breaks
756 dw2_lookup_symbol: It assumes that if the index says symbol X lives
757 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
758 will find X. Alas TUs live in their own symtab, so after expanding CU Y
759 we need to look in TU Z to find X. Fortunately, this is akin to
760 DW_TAG_imported_unit, so we just use the same mechanism: For
761 .gdb_index version <=7 this also records the TUs that the CU referred
762 to. Concurrently with this change gdb was modified to emit version 8
763 indices so we only pay a price for gold generated indices.
764 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
765 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
768 /* Entry in the signatured_types hash table. */
770 struct signatured_type
772 /* The "per_cu" object of this type.
773 This struct is used iff per_cu.is_debug_types.
774 N.B.: This is the first member so that it's easy to convert pointers
776 struct dwarf2_per_cu_data per_cu;
778 /* The type's signature. */
781 /* Offset in the TU of the type's DIE, as read from the TU header.
782 If this TU is a DWO stub and the definition lives in a DWO file
783 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
784 cu_offset type_offset_in_tu;
786 /* Offset in the section of the type's DIE.
787 If the definition lives in a DWO file, this is the offset in the
788 .debug_types.dwo section.
789 The value is zero until the actual value is known.
790 Zero is otherwise not a valid section offset. */
791 sect_offset type_offset_in_section;
793 /* Type units are grouped by their DW_AT_stmt_list entry so that they
794 can share them. This points to the containing symtab. */
795 struct type_unit_group *type_unit_group;
798 The first time we encounter this type we fully read it in and install it
799 in the symbol tables. Subsequent times we only need the type. */
802 /* Containing DWO unit.
803 This field is valid iff per_cu.reading_dwo_directly. */
804 struct dwo_unit *dwo_unit;
807 typedef struct signatured_type *sig_type_ptr;
808 DEF_VEC_P (sig_type_ptr);
810 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
811 This includes type_unit_group and quick_file_names. */
813 struct stmt_list_hash
815 /* The DWO unit this table is from or NULL if there is none. */
816 struct dwo_unit *dwo_unit;
818 /* Offset in .debug_line or .debug_line.dwo. */
819 sect_offset line_sect_off;
822 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
823 an object of this type. */
825 struct type_unit_group
827 /* dwarf2read.c's main "handle" on a TU symtab.
828 To simplify things we create an artificial CU that "includes" all the
829 type units using this stmt_list so that the rest of the code still has
830 a "per_cu" handle on the symtab.
831 This PER_CU is recognized by having no section. */
832 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
833 struct dwarf2_per_cu_data per_cu;
835 /* The TUs that share this DW_AT_stmt_list entry.
836 This is added to while parsing type units to build partial symtabs,
837 and is deleted afterwards and not used again. */
838 VEC (sig_type_ptr) *tus;
840 /* The compunit symtab.
841 Type units in a group needn't all be defined in the same source file,
842 so we create an essentially anonymous symtab as the compunit symtab. */
843 struct compunit_symtab *compunit_symtab;
845 /* The data used to construct the hash key. */
846 struct stmt_list_hash hash;
848 /* The number of symtabs from the line header.
849 The value here must match line_header.num_file_names. */
850 unsigned int num_symtabs;
852 /* The symbol tables for this TU (obtained from the files listed in
854 WARNING: The order of entries here must match the order of entries
855 in the line header. After the first TU using this type_unit_group, the
856 line header for the subsequent TUs is recreated from this. This is done
857 because we need to use the same symtabs for each TU using the same
858 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
859 there's no guarantee the line header doesn't have duplicate entries. */
860 struct symtab **symtabs;
863 /* These sections are what may appear in a (real or virtual) DWO file. */
867 struct dwarf2_section_info abbrev;
868 struct dwarf2_section_info line;
869 struct dwarf2_section_info loc;
870 struct dwarf2_section_info loclists;
871 struct dwarf2_section_info macinfo;
872 struct dwarf2_section_info macro;
873 struct dwarf2_section_info str;
874 struct dwarf2_section_info str_offsets;
875 /* In the case of a virtual DWO file, these two are unused. */
876 struct dwarf2_section_info info;
877 VEC (dwarf2_section_info_def) *types;
880 /* CUs/TUs in DWP/DWO files. */
884 /* Backlink to the containing struct dwo_file. */
885 struct dwo_file *dwo_file;
887 /* The "id" that distinguishes this CU/TU.
888 .debug_info calls this "dwo_id", .debug_types calls this "signature".
889 Since signatures came first, we stick with it for consistency. */
892 /* The section this CU/TU lives in, in the DWO file. */
893 struct dwarf2_section_info *section;
895 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
896 sect_offset sect_off;
899 /* For types, offset in the type's DIE of the type defined by this TU. */
900 cu_offset type_offset_in_tu;
903 /* include/dwarf2.h defines the DWP section codes.
904 It defines a max value but it doesn't define a min value, which we
905 use for error checking, so provide one. */
907 enum dwp_v2_section_ids
912 /* Data for one DWO file.
914 This includes virtual DWO files (a virtual DWO file is a DWO file as it
915 appears in a DWP file). DWP files don't really have DWO files per se -
916 comdat folding of types "loses" the DWO file they came from, and from
917 a high level view DWP files appear to contain a mass of random types.
918 However, to maintain consistency with the non-DWP case we pretend DWP
919 files contain virtual DWO files, and we assign each TU with one virtual
920 DWO file (generally based on the line and abbrev section offsets -
921 a heuristic that seems to work in practice). */
925 /* The DW_AT_GNU_dwo_name attribute.
926 For virtual DWO files the name is constructed from the section offsets
927 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
928 from related CU+TUs. */
929 const char *dwo_name;
931 /* The DW_AT_comp_dir attribute. */
932 const char *comp_dir;
934 /* The bfd, when the file is open. Otherwise this is NULL.
935 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
938 /* The sections that make up this DWO file.
939 Remember that for virtual DWO files in DWP V2, these are virtual
940 sections (for lack of a better name). */
941 struct dwo_sections sections;
943 /* The CUs in the file.
944 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
945 an extension to handle LLVM's Link Time Optimization output (where
946 multiple source files may be compiled into a single object/dwo pair). */
949 /* Table of TUs in the file.
950 Each element is a struct dwo_unit. */
954 /* These sections are what may appear in a DWP file. */
958 /* These are used by both DWP version 1 and 2. */
959 struct dwarf2_section_info str;
960 struct dwarf2_section_info cu_index;
961 struct dwarf2_section_info tu_index;
963 /* These are only used by DWP version 2 files.
964 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
965 sections are referenced by section number, and are not recorded here.
966 In DWP version 2 there is at most one copy of all these sections, each
967 section being (effectively) comprised of the concatenation of all of the
968 individual sections that exist in the version 1 format.
969 To keep the code simple we treat each of these concatenated pieces as a
970 section itself (a virtual section?). */
971 struct dwarf2_section_info abbrev;
972 struct dwarf2_section_info info;
973 struct dwarf2_section_info line;
974 struct dwarf2_section_info loc;
975 struct dwarf2_section_info macinfo;
976 struct dwarf2_section_info macro;
977 struct dwarf2_section_info str_offsets;
978 struct dwarf2_section_info types;
981 /* These sections are what may appear in a virtual DWO file in DWP version 1.
982 A virtual DWO file is a DWO file as it appears in a DWP file. */
984 struct virtual_v1_dwo_sections
986 struct dwarf2_section_info abbrev;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
989 struct dwarf2_section_info macinfo;
990 struct dwarf2_section_info macro;
991 struct dwarf2_section_info str_offsets;
992 /* Each DWP hash table entry records one CU or one TU.
993 That is recorded here, and copied to dwo_unit.section. */
994 struct dwarf2_section_info info_or_types;
997 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
998 In version 2, the sections of the DWO files are concatenated together
999 and stored in one section of that name. Thus each ELF section contains
1000 several "virtual" sections. */
1002 struct virtual_v2_dwo_sections
1004 bfd_size_type abbrev_offset;
1005 bfd_size_type abbrev_size;
1007 bfd_size_type line_offset;
1008 bfd_size_type line_size;
1010 bfd_size_type loc_offset;
1011 bfd_size_type loc_size;
1013 bfd_size_type macinfo_offset;
1014 bfd_size_type macinfo_size;
1016 bfd_size_type macro_offset;
1017 bfd_size_type macro_size;
1019 bfd_size_type str_offsets_offset;
1020 bfd_size_type str_offsets_size;
1022 /* Each DWP hash table entry records one CU or one TU.
1023 That is recorded here, and copied to dwo_unit.section. */
1024 bfd_size_type info_or_types_offset;
1025 bfd_size_type info_or_types_size;
1028 /* Contents of DWP hash tables. */
1030 struct dwp_hash_table
1032 uint32_t version, nr_columns;
1033 uint32_t nr_units, nr_slots;
1034 const gdb_byte *hash_table, *unit_table;
1039 const gdb_byte *indices;
1043 /* This is indexed by column number and gives the id of the section
1045 #define MAX_NR_V2_DWO_SECTIONS \
1046 (1 /* .debug_info or .debug_types */ \
1047 + 1 /* .debug_abbrev */ \
1048 + 1 /* .debug_line */ \
1049 + 1 /* .debug_loc */ \
1050 + 1 /* .debug_str_offsets */ \
1051 + 1 /* .debug_macro or .debug_macinfo */)
1052 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1053 const gdb_byte *offsets;
1054 const gdb_byte *sizes;
1059 /* Data for one DWP file. */
1063 /* Name of the file. */
1066 /* File format version. */
1072 /* Section info for this file. */
1073 struct dwp_sections sections;
1075 /* Table of CUs in the file. */
1076 const struct dwp_hash_table *cus;
1078 /* Table of TUs in the file. */
1079 const struct dwp_hash_table *tus;
1081 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1085 /* Table to map ELF section numbers to their sections.
1086 This is only needed for the DWP V1 file format. */
1087 unsigned int num_sections;
1088 asection **elf_sections;
1091 /* This represents a '.dwz' file. */
1095 /* A dwz file can only contain a few sections. */
1096 struct dwarf2_section_info abbrev;
1097 struct dwarf2_section_info info;
1098 struct dwarf2_section_info str;
1099 struct dwarf2_section_info line;
1100 struct dwarf2_section_info macro;
1101 struct dwarf2_section_info gdb_index;
1103 /* The dwz's BFD. */
1107 /* Struct used to pass misc. parameters to read_die_and_children, et
1108 al. which are used for both .debug_info and .debug_types dies.
1109 All parameters here are unchanging for the life of the call. This
1110 struct exists to abstract away the constant parameters of die reading. */
1112 struct die_reader_specs
1114 /* The bfd of die_section. */
1117 /* The CU of the DIE we are parsing. */
1118 struct dwarf2_cu *cu;
1120 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1121 struct dwo_file *dwo_file;
1123 /* The section the die comes from.
1124 This is either .debug_info or .debug_types, or the .dwo variants. */
1125 struct dwarf2_section_info *die_section;
1127 /* die_section->buffer. */
1128 const gdb_byte *buffer;
1130 /* The end of the buffer. */
1131 const gdb_byte *buffer_end;
1133 /* The value of the DW_AT_comp_dir attribute. */
1134 const char *comp_dir;
1137 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1138 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1139 const gdb_byte *info_ptr,
1140 struct die_info *comp_unit_die,
1144 /* A 1-based directory index. This is a strong typedef to prevent
1145 accidentally using a directory index as a 0-based index into an
1147 enum class dir_index : unsigned int {};
1149 /* Likewise, a 1-based file name index. */
1150 enum class file_name_index : unsigned int {};
1154 file_entry () = default;
1156 file_entry (const char *name_, dir_index d_index_,
1157 unsigned int mod_time_, unsigned int length_)
1160 mod_time (mod_time_),
1164 /* Return the include directory at D_INDEX stored in LH. Returns
1165 NULL if D_INDEX is out of bounds. */
1166 const char *include_dir (const line_header *lh) const;
1168 /* The file name. Note this is an observing pointer. The memory is
1169 owned by debug_line_buffer. */
1170 const char *name {};
1172 /* The directory index (1-based). */
1173 dir_index d_index {};
1175 unsigned int mod_time {};
1177 unsigned int length {};
1179 /* True if referenced by the Line Number Program. */
1182 /* The associated symbol table, if any. */
1183 struct symtab *symtab {};
1186 /* The line number information for a compilation unit (found in the
1187 .debug_line section) begins with a "statement program header",
1188 which contains the following information. */
1195 /* Add an entry to the include directory table. */
1196 void add_include_dir (const char *include_dir);
1198 /* Add an entry to the file name table. */
1199 void add_file_name (const char *name, dir_index d_index,
1200 unsigned int mod_time, unsigned int length);
1202 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1203 is out of bounds. */
1204 const char *include_dir_at (dir_index index) const
1206 /* Convert directory index number (1-based) to vector index
1208 size_t vec_index = to_underlying (index) - 1;
1210 if (vec_index >= include_dirs.size ())
1212 return include_dirs[vec_index];
1215 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1216 is out of bounds. */
1217 file_entry *file_name_at (file_name_index index)
1219 /* Convert file name index number (1-based) to vector index
1221 size_t vec_index = to_underlying (index) - 1;
1223 if (vec_index >= file_names.size ())
1225 return &file_names[vec_index];
1228 /* Const version of the above. */
1229 const file_entry *file_name_at (unsigned int index) const
1231 if (index >= file_names.size ())
1233 return &file_names[index];
1236 /* Offset of line number information in .debug_line section. */
1237 sect_offset sect_off {};
1239 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1240 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1242 unsigned int total_length {};
1243 unsigned short version {};
1244 unsigned int header_length {};
1245 unsigned char minimum_instruction_length {};
1246 unsigned char maximum_ops_per_instruction {};
1247 unsigned char default_is_stmt {};
1249 unsigned char line_range {};
1250 unsigned char opcode_base {};
1252 /* standard_opcode_lengths[i] is the number of operands for the
1253 standard opcode whose value is i. This means that
1254 standard_opcode_lengths[0] is unused, and the last meaningful
1255 element is standard_opcode_lengths[opcode_base - 1]. */
1256 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1258 /* The include_directories table. Note these are observing
1259 pointers. The memory is owned by debug_line_buffer. */
1260 std::vector<const char *> include_dirs;
1262 /* The file_names table. */
1263 std::vector<file_entry> file_names;
1265 /* The start and end of the statement program following this
1266 header. These point into dwarf2_per_objfile->line_buffer. */
1267 const gdb_byte *statement_program_start {}, *statement_program_end {};
1270 typedef std::unique_ptr<line_header> line_header_up;
1273 file_entry::include_dir (const line_header *lh) const
1275 return lh->include_dir_at (d_index);
1278 /* When we construct a partial symbol table entry we only
1279 need this much information. */
1280 struct partial_die_info
1282 /* Offset of this DIE. */
1283 sect_offset sect_off;
1285 /* DWARF-2 tag for this DIE. */
1286 ENUM_BITFIELD(dwarf_tag) tag : 16;
1288 /* Assorted flags describing the data found in this DIE. */
1289 unsigned int has_children : 1;
1290 unsigned int is_external : 1;
1291 unsigned int is_declaration : 1;
1292 unsigned int has_type : 1;
1293 unsigned int has_specification : 1;
1294 unsigned int has_pc_info : 1;
1295 unsigned int may_be_inlined : 1;
1297 /* This DIE has been marked DW_AT_main_subprogram. */
1298 unsigned int main_subprogram : 1;
1300 /* Flag set if the SCOPE field of this structure has been
1302 unsigned int scope_set : 1;
1304 /* Flag set if the DIE has a byte_size attribute. */
1305 unsigned int has_byte_size : 1;
1307 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1308 unsigned int has_const_value : 1;
1310 /* Flag set if any of the DIE's children are template arguments. */
1311 unsigned int has_template_arguments : 1;
1313 /* Flag set if fixup_partial_die has been called on this die. */
1314 unsigned int fixup_called : 1;
1316 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1317 unsigned int is_dwz : 1;
1319 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1320 unsigned int spec_is_dwz : 1;
1322 /* The name of this DIE. Normally the value of DW_AT_name, but
1323 sometimes a default name for unnamed DIEs. */
1326 /* The linkage name, if present. */
1327 const char *linkage_name;
1329 /* The scope to prepend to our children. This is generally
1330 allocated on the comp_unit_obstack, so will disappear
1331 when this compilation unit leaves the cache. */
1334 /* Some data associated with the partial DIE. The tag determines
1335 which field is live. */
1338 /* The location description associated with this DIE, if any. */
1339 struct dwarf_block *locdesc;
1340 /* The offset of an import, for DW_TAG_imported_unit. */
1341 sect_offset sect_off;
1344 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1348 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1349 DW_AT_sibling, if any. */
1350 /* NOTE: This member isn't strictly necessary, read_partial_die could
1351 return DW_AT_sibling values to its caller load_partial_dies. */
1352 const gdb_byte *sibling;
1354 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1355 DW_AT_specification (or DW_AT_abstract_origin or
1356 DW_AT_extension). */
1357 sect_offset spec_offset;
1359 /* Pointers to this DIE's parent, first child, and next sibling,
1361 struct partial_die_info *die_parent, *die_child, *die_sibling;
1364 /* This data structure holds the information of an abbrev. */
1367 unsigned int number; /* number identifying abbrev */
1368 enum dwarf_tag tag; /* dwarf tag */
1369 unsigned short has_children; /* boolean */
1370 unsigned short num_attrs; /* number of attributes */
1371 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1372 struct abbrev_info *next; /* next in chain */
1377 ENUM_BITFIELD(dwarf_attribute) name : 16;
1378 ENUM_BITFIELD(dwarf_form) form : 16;
1380 /* It is valid only if FORM is DW_FORM_implicit_const. */
1381 LONGEST implicit_const;
1384 /* Size of abbrev_table.abbrev_hash_table. */
1385 #define ABBREV_HASH_SIZE 121
1387 /* Top level data structure to contain an abbreviation table. */
1391 /* Where the abbrev table came from.
1392 This is used as a sanity check when the table is used. */
1393 sect_offset sect_off;
1395 /* Storage for the abbrev table. */
1396 struct obstack abbrev_obstack;
1398 /* Hash table of abbrevs.
1399 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1400 It could be statically allocated, but the previous code didn't so we
1402 struct abbrev_info **abbrevs;
1405 /* Attributes have a name and a value. */
1408 ENUM_BITFIELD(dwarf_attribute) name : 16;
1409 ENUM_BITFIELD(dwarf_form) form : 15;
1411 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1412 field should be in u.str (existing only for DW_STRING) but it is kept
1413 here for better struct attribute alignment. */
1414 unsigned int string_is_canonical : 1;
1419 struct dwarf_block *blk;
1428 /* This data structure holds a complete die structure. */
1431 /* DWARF-2 tag for this DIE. */
1432 ENUM_BITFIELD(dwarf_tag) tag : 16;
1434 /* Number of attributes */
1435 unsigned char num_attrs;
1437 /* True if we're presently building the full type name for the
1438 type derived from this DIE. */
1439 unsigned char building_fullname : 1;
1441 /* True if this die is in process. PR 16581. */
1442 unsigned char in_process : 1;
1445 unsigned int abbrev;
1447 /* Offset in .debug_info or .debug_types section. */
1448 sect_offset sect_off;
1450 /* The dies in a compilation unit form an n-ary tree. PARENT
1451 points to this die's parent; CHILD points to the first child of
1452 this node; and all the children of a given node are chained
1453 together via their SIBLING fields. */
1454 struct die_info *child; /* Its first child, if any. */
1455 struct die_info *sibling; /* Its next sibling, if any. */
1456 struct die_info *parent; /* Its parent, if any. */
1458 /* An array of attributes, with NUM_ATTRS elements. There may be
1459 zero, but it's not common and zero-sized arrays are not
1460 sufficiently portable C. */
1461 struct attribute attrs[1];
1464 /* Get at parts of an attribute structure. */
1466 #define DW_STRING(attr) ((attr)->u.str)
1467 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1468 #define DW_UNSND(attr) ((attr)->u.unsnd)
1469 #define DW_BLOCK(attr) ((attr)->u.blk)
1470 #define DW_SND(attr) ((attr)->u.snd)
1471 #define DW_ADDR(attr) ((attr)->u.addr)
1472 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1474 /* Blocks are a bunch of untyped bytes. */
1479 /* Valid only if SIZE is not zero. */
1480 const gdb_byte *data;
1483 #ifndef ATTR_ALLOC_CHUNK
1484 #define ATTR_ALLOC_CHUNK 4
1487 /* Allocate fields for structs, unions and enums in this size. */
1488 #ifndef DW_FIELD_ALLOC_CHUNK
1489 #define DW_FIELD_ALLOC_CHUNK 4
1492 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1493 but this would require a corresponding change in unpack_field_as_long
1495 static int bits_per_byte = 8;
1499 struct nextfield *next;
1507 struct nextfnfield *next;
1508 struct fn_field fnfield;
1515 struct nextfnfield *head;
1518 struct typedef_field_list
1520 struct typedef_field field;
1521 struct typedef_field_list *next;
1524 /* The routines that read and process dies for a C struct or C++ class
1525 pass lists of data member fields and lists of member function fields
1526 in an instance of a field_info structure, as defined below. */
1529 /* List of data member and baseclasses fields. */
1530 struct nextfield *fields, *baseclasses;
1532 /* Number of fields (including baseclasses). */
1535 /* Number of baseclasses. */
1538 /* Set if the accesibility of one of the fields is not public. */
1539 int non_public_fields;
1541 /* Member function fieldlist array, contains name of possibly overloaded
1542 member function, number of overloaded member functions and a pointer
1543 to the head of the member function field chain. */
1544 struct fnfieldlist *fnfieldlists;
1546 /* Number of entries in the fnfieldlists array. */
1549 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1550 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1551 struct typedef_field_list *typedef_field_list;
1552 unsigned typedef_field_list_count;
1555 /* One item on the queue of compilation units to read in full symbols
1557 struct dwarf2_queue_item
1559 struct dwarf2_per_cu_data *per_cu;
1560 enum language pretend_language;
1561 struct dwarf2_queue_item *next;
1564 /* The current queue. */
1565 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1567 /* Loaded secondary compilation units are kept in memory until they
1568 have not been referenced for the processing of this many
1569 compilation units. Set this to zero to disable caching. Cache
1570 sizes of up to at least twenty will improve startup time for
1571 typical inter-CU-reference binaries, at an obvious memory cost. */
1572 static int dwarf_max_cache_age = 5;
1574 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1575 struct cmd_list_element *c, const char *value)
1577 fprintf_filtered (file, _("The upper bound on the age of cached "
1578 "DWARF compilation units is %s.\n"),
1582 /* local function prototypes */
1584 static const char *get_section_name (const struct dwarf2_section_info *);
1586 static const char *get_section_file_name (const struct dwarf2_section_info *);
1588 static void dwarf2_find_base_address (struct die_info *die,
1589 struct dwarf2_cu *cu);
1591 static struct partial_symtab *create_partial_symtab
1592 (struct dwarf2_per_cu_data *per_cu, const char *name);
1594 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1595 const gdb_byte *info_ptr,
1596 struct die_info *type_unit_die,
1597 int has_children, void *data);
1599 static void dwarf2_build_psymtabs_hard (struct objfile *);
1601 static void scan_partial_symbols (struct partial_die_info *,
1602 CORE_ADDR *, CORE_ADDR *,
1603 int, struct dwarf2_cu *);
1605 static void add_partial_symbol (struct partial_die_info *,
1606 struct dwarf2_cu *);
1608 static void add_partial_namespace (struct partial_die_info *pdi,
1609 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1610 int set_addrmap, struct dwarf2_cu *cu);
1612 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1613 CORE_ADDR *highpc, int set_addrmap,
1614 struct dwarf2_cu *cu);
1616 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1617 struct dwarf2_cu *cu);
1619 static void add_partial_subprogram (struct partial_die_info *pdi,
1620 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1621 int need_pc, struct dwarf2_cu *cu);
1623 static void dwarf2_read_symtab (struct partial_symtab *,
1626 static void psymtab_to_symtab_1 (struct partial_symtab *);
1628 static struct abbrev_info *abbrev_table_lookup_abbrev
1629 (const struct abbrev_table *, unsigned int);
1631 static struct abbrev_table *abbrev_table_read_table
1632 (struct dwarf2_section_info *, sect_offset);
1634 static void abbrev_table_free (struct abbrev_table *);
1636 static void abbrev_table_free_cleanup (void *);
1638 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1639 struct dwarf2_section_info *);
1641 static void dwarf2_free_abbrev_table (void *);
1643 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1645 static struct partial_die_info *load_partial_dies
1646 (const struct die_reader_specs *, const gdb_byte *, int);
1648 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1649 struct partial_die_info *,
1650 struct abbrev_info *,
1654 static struct partial_die_info *find_partial_die (sect_offset, int,
1655 struct dwarf2_cu *);
1657 static void fixup_partial_die (struct partial_die_info *,
1658 struct dwarf2_cu *);
1660 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1661 struct attribute *, struct attr_abbrev *,
1664 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1666 static int read_1_signed_byte (bfd *, const gdb_byte *);
1668 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1670 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1672 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1674 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1677 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1679 static LONGEST read_checked_initial_length_and_offset
1680 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1681 unsigned int *, unsigned int *);
1683 static LONGEST read_offset (bfd *, const gdb_byte *,
1684 const struct comp_unit_head *,
1687 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1689 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1692 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1694 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1696 static const char *read_indirect_string (bfd *, const gdb_byte *,
1697 const struct comp_unit_head *,
1700 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1701 const struct comp_unit_head *,
1704 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1706 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1708 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1712 static const char *read_str_index (const struct die_reader_specs *reader,
1713 ULONGEST str_index);
1715 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1717 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1718 struct dwarf2_cu *);
1720 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1723 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1724 struct dwarf2_cu *cu);
1726 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1727 struct dwarf2_cu *cu);
1729 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1731 static struct die_info *die_specification (struct die_info *die,
1732 struct dwarf2_cu **);
1734 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1735 struct dwarf2_cu *cu);
1737 static void dwarf_decode_lines (struct line_header *, const char *,
1738 struct dwarf2_cu *, struct partial_symtab *,
1739 CORE_ADDR, int decode_mapping);
1741 static void dwarf2_start_subfile (const char *, const char *);
1743 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1744 const char *, const char *,
1747 static struct symbol *new_symbol (struct die_info *, struct type *,
1748 struct dwarf2_cu *);
1750 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1751 struct dwarf2_cu *, struct symbol *);
1753 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1754 struct dwarf2_cu *);
1756 static void dwarf2_const_value_attr (const struct attribute *attr,
1759 struct obstack *obstack,
1760 struct dwarf2_cu *cu, LONGEST *value,
1761 const gdb_byte **bytes,
1762 struct dwarf2_locexpr_baton **baton);
1764 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1766 static int need_gnat_info (struct dwarf2_cu *);
1768 static struct type *die_descriptive_type (struct die_info *,
1769 struct dwarf2_cu *);
1771 static void set_descriptive_type (struct type *, struct die_info *,
1772 struct dwarf2_cu *);
1774 static struct type *die_containing_type (struct die_info *,
1775 struct dwarf2_cu *);
1777 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1778 struct dwarf2_cu *);
1780 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1782 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1784 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1786 static char *typename_concat (struct obstack *obs, const char *prefix,
1787 const char *suffix, int physname,
1788 struct dwarf2_cu *cu);
1790 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1792 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1794 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1796 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1798 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1800 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1802 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1803 struct dwarf2_cu *, struct partial_symtab *);
1805 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1806 values. Keep the items ordered with increasing constraints compliance. */
1809 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1810 PC_BOUNDS_NOT_PRESENT,
1812 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1813 were present but they do not form a valid range of PC addresses. */
1816 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1819 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1823 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1824 CORE_ADDR *, CORE_ADDR *,
1826 struct partial_symtab *);
1828 static void get_scope_pc_bounds (struct die_info *,
1829 CORE_ADDR *, CORE_ADDR *,
1830 struct dwarf2_cu *);
1832 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1833 CORE_ADDR, struct dwarf2_cu *);
1835 static void dwarf2_add_field (struct field_info *, struct die_info *,
1836 struct dwarf2_cu *);
1838 static void dwarf2_attach_fields_to_type (struct field_info *,
1839 struct type *, struct dwarf2_cu *);
1841 static void dwarf2_add_member_fn (struct field_info *,
1842 struct die_info *, struct type *,
1843 struct dwarf2_cu *);
1845 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1847 struct dwarf2_cu *);
1849 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1851 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1853 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1855 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1857 static struct using_direct **using_directives (enum language);
1859 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1861 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1863 static struct type *read_module_type (struct die_info *die,
1864 struct dwarf2_cu *cu);
1866 static const char *namespace_name (struct die_info *die,
1867 int *is_anonymous, struct dwarf2_cu *);
1869 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1871 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1873 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1874 struct dwarf2_cu *);
1876 static struct die_info *read_die_and_siblings_1
1877 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1880 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1881 const gdb_byte *info_ptr,
1882 const gdb_byte **new_info_ptr,
1883 struct die_info *parent);
1885 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1886 struct die_info **, const gdb_byte *,
1889 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1890 struct die_info **, const gdb_byte *,
1893 static void process_die (struct die_info *, struct dwarf2_cu *);
1895 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1898 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1900 static const char *dwarf2_full_name (const char *name,
1901 struct die_info *die,
1902 struct dwarf2_cu *cu);
1904 static const char *dwarf2_physname (const char *name, struct die_info *die,
1905 struct dwarf2_cu *cu);
1907 static struct die_info *dwarf2_extension (struct die_info *die,
1908 struct dwarf2_cu **);
1910 static const char *dwarf_tag_name (unsigned int);
1912 static const char *dwarf_attr_name (unsigned int);
1914 static const char *dwarf_form_name (unsigned int);
1916 static const char *dwarf_bool_name (unsigned int);
1918 static const char *dwarf_type_encoding_name (unsigned int);
1920 static struct die_info *sibling_die (struct die_info *);
1922 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1924 static void dump_die_for_error (struct die_info *);
1926 static void dump_die_1 (struct ui_file *, int level, int max_level,
1929 /*static*/ void dump_die (struct die_info *, int max_level);
1931 static void store_in_ref_table (struct die_info *,
1932 struct dwarf2_cu *);
1934 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1936 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1938 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1939 const struct attribute *,
1940 struct dwarf2_cu **);
1942 static struct die_info *follow_die_ref (struct die_info *,
1943 const struct attribute *,
1944 struct dwarf2_cu **);
1946 static struct die_info *follow_die_sig (struct die_info *,
1947 const struct attribute *,
1948 struct dwarf2_cu **);
1950 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1951 struct dwarf2_cu *);
1953 static struct type *get_DW_AT_signature_type (struct die_info *,
1954 const struct attribute *,
1955 struct dwarf2_cu *);
1957 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1959 static void read_signatured_type (struct signatured_type *);
1961 static int attr_to_dynamic_prop (const struct attribute *attr,
1962 struct die_info *die, struct dwarf2_cu *cu,
1963 struct dynamic_prop *prop);
1965 /* memory allocation interface */
1967 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1969 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1971 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1973 static int attr_form_is_block (const struct attribute *);
1975 static int attr_form_is_section_offset (const struct attribute *);
1977 static int attr_form_is_constant (const struct attribute *);
1979 static int attr_form_is_ref (const struct attribute *);
1981 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1982 struct dwarf2_loclist_baton *baton,
1983 const struct attribute *attr);
1985 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1987 struct dwarf2_cu *cu,
1990 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1991 const gdb_byte *info_ptr,
1992 struct abbrev_info *abbrev);
1994 static void free_stack_comp_unit (void *);
1996 static hashval_t partial_die_hash (const void *item);
1998 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2000 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2001 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2003 static void init_one_comp_unit (struct dwarf2_cu *cu,
2004 struct dwarf2_per_cu_data *per_cu);
2006 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2007 struct die_info *comp_unit_die,
2008 enum language pretend_language);
2010 static void free_heap_comp_unit (void *);
2012 static void free_cached_comp_units (void *);
2014 static void age_cached_comp_units (void);
2016 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2018 static struct type *set_die_type (struct die_info *, struct type *,
2019 struct dwarf2_cu *);
2021 static void create_all_comp_units (struct objfile *);
2023 static int create_all_type_units (struct objfile *);
2025 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2028 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2031 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2034 static void dwarf2_add_dependence (struct dwarf2_cu *,
2035 struct dwarf2_per_cu_data *);
2037 static void dwarf2_mark (struct dwarf2_cu *);
2039 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2041 static struct type *get_die_type_at_offset (sect_offset,
2042 struct dwarf2_per_cu_data *);
2044 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2046 static void dwarf2_release_queue (void *dummy);
2048 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2049 enum language pretend_language);
2051 static void process_queue (void);
2053 /* The return type of find_file_and_directory. Note, the enclosed
2054 string pointers are only valid while this object is valid. */
2056 struct file_and_directory
2058 /* The filename. This is never NULL. */
2061 /* The compilation directory. NULL if not known. If we needed to
2062 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2063 points directly to the DW_AT_comp_dir string attribute owned by
2064 the obstack that owns the DIE. */
2065 const char *comp_dir;
2067 /* If we needed to build a new string for comp_dir, this is what
2068 owns the storage. */
2069 std::string comp_dir_storage;
2072 static file_and_directory find_file_and_directory (struct die_info *die,
2073 struct dwarf2_cu *cu);
2075 static char *file_full_name (int file, struct line_header *lh,
2076 const char *comp_dir);
2078 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2079 enum class rcuh_kind { COMPILE, TYPE };
2081 static const gdb_byte *read_and_check_comp_unit_head
2082 (struct comp_unit_head *header,
2083 struct dwarf2_section_info *section,
2084 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2085 rcuh_kind section_kind);
2087 static void init_cutu_and_read_dies
2088 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2089 int use_existing_cu, int keep,
2090 die_reader_func_ftype *die_reader_func, void *data);
2092 static void init_cutu_and_read_dies_simple
2093 (struct dwarf2_per_cu_data *this_cu,
2094 die_reader_func_ftype *die_reader_func, void *data);
2096 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2098 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2100 static struct dwo_unit *lookup_dwo_unit_in_dwp
2101 (struct dwp_file *dwp_file, const char *comp_dir,
2102 ULONGEST signature, int is_debug_types);
2104 static struct dwp_file *get_dwp_file (void);
2106 static struct dwo_unit *lookup_dwo_comp_unit
2107 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2109 static struct dwo_unit *lookup_dwo_type_unit
2110 (struct signatured_type *, const char *, const char *);
2112 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2114 static void free_dwo_file_cleanup (void *);
2116 static void process_cu_includes (void);
2118 static void check_producer (struct dwarf2_cu *cu);
2120 static void free_line_header_voidp (void *arg);
2122 /* Various complaints about symbol reading that don't abort the process. */
2125 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2127 complaint (&symfile_complaints,
2128 _("statement list doesn't fit in .debug_line section"));
2132 dwarf2_debug_line_missing_file_complaint (void)
2134 complaint (&symfile_complaints,
2135 _(".debug_line section has line data without a file"));
2139 dwarf2_debug_line_missing_end_sequence_complaint (void)
2141 complaint (&symfile_complaints,
2142 _(".debug_line section has line "
2143 "program sequence without an end"));
2147 dwarf2_complex_location_expr_complaint (void)
2149 complaint (&symfile_complaints, _("location expression too complex"));
2153 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2156 complaint (&symfile_complaints,
2157 _("const value length mismatch for '%s', got %d, expected %d"),
2162 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2164 complaint (&symfile_complaints,
2165 _("debug info runs off end of %s section"
2167 get_section_name (section),
2168 get_section_file_name (section));
2172 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2174 complaint (&symfile_complaints,
2175 _("macro debug info contains a "
2176 "malformed macro definition:\n`%s'"),
2181 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2183 complaint (&symfile_complaints,
2184 _("invalid attribute class or form for '%s' in '%s'"),
2188 /* Hash function for line_header_hash. */
2191 line_header_hash (const struct line_header *ofs)
2193 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2196 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2199 line_header_hash_voidp (const void *item)
2201 const struct line_header *ofs = (const struct line_header *) item;
2203 return line_header_hash (ofs);
2206 /* Equality function for line_header_hash. */
2209 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2211 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2212 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2214 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2215 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2220 /* Read the given attribute value as an address, taking the attribute's
2221 form into account. */
2224 attr_value_as_address (struct attribute *attr)
2228 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2230 /* Aside from a few clearly defined exceptions, attributes that
2231 contain an address must always be in DW_FORM_addr form.
2232 Unfortunately, some compilers happen to be violating this
2233 requirement by encoding addresses using other forms, such
2234 as DW_FORM_data4 for example. For those broken compilers,
2235 we try to do our best, without any guarantee of success,
2236 to interpret the address correctly. It would also be nice
2237 to generate a complaint, but that would require us to maintain
2238 a list of legitimate cases where a non-address form is allowed,
2239 as well as update callers to pass in at least the CU's DWARF
2240 version. This is more overhead than what we're willing to
2241 expand for a pretty rare case. */
2242 addr = DW_UNSND (attr);
2245 addr = DW_ADDR (attr);
2250 /* The suffix for an index file. */
2251 #define INDEX_SUFFIX ".gdb-index"
2253 /* See declaration. */
2255 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2256 const dwarf2_debug_sections *names)
2257 : objfile (objfile_)
2260 names = &dwarf2_elf_names;
2262 bfd *obfd = objfile->obfd;
2264 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2265 locate_sections (obfd, sec, *names);
2268 dwarf2_per_objfile::~dwarf2_per_objfile ()
2270 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2271 free_cached_comp_units ();
2273 if (quick_file_names_table)
2274 htab_delete (quick_file_names_table);
2276 if (line_header_hash)
2277 htab_delete (line_header_hash);
2279 /* Everything else should be on the objfile obstack. */
2282 /* See declaration. */
2285 dwarf2_per_objfile::free_cached_comp_units ()
2287 dwarf2_per_cu_data *per_cu = read_in_chain;
2288 dwarf2_per_cu_data **last_chain = &read_in_chain;
2289 while (per_cu != NULL)
2291 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2293 free_heap_comp_unit (per_cu->cu);
2294 *last_chain = next_cu;
2299 /* Try to locate the sections we need for DWARF 2 debugging
2300 information and return true if we have enough to do something.
2301 NAMES points to the dwarf2 section names, or is NULL if the standard
2302 ELF names are used. */
2305 dwarf2_has_info (struct objfile *objfile,
2306 const struct dwarf2_debug_sections *names)
2308 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2309 objfile_data (objfile, dwarf2_objfile_data_key));
2310 if (!dwarf2_per_objfile)
2312 /* Initialize per-objfile state. */
2313 struct dwarf2_per_objfile *data
2314 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2316 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2317 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2319 return (!dwarf2_per_objfile->info.is_virtual
2320 && dwarf2_per_objfile->info.s.section != NULL
2321 && !dwarf2_per_objfile->abbrev.is_virtual
2322 && dwarf2_per_objfile->abbrev.s.section != NULL);
2325 /* Return the containing section of virtual section SECTION. */
2327 static struct dwarf2_section_info *
2328 get_containing_section (const struct dwarf2_section_info *section)
2330 gdb_assert (section->is_virtual);
2331 return section->s.containing_section;
2334 /* Return the bfd owner of SECTION. */
2337 get_section_bfd_owner (const struct dwarf2_section_info *section)
2339 if (section->is_virtual)
2341 section = get_containing_section (section);
2342 gdb_assert (!section->is_virtual);
2344 return section->s.section->owner;
2347 /* Return the bfd section of SECTION.
2348 Returns NULL if the section is not present. */
2351 get_section_bfd_section (const struct dwarf2_section_info *section)
2353 if (section->is_virtual)
2355 section = get_containing_section (section);
2356 gdb_assert (!section->is_virtual);
2358 return section->s.section;
2361 /* Return the name of SECTION. */
2364 get_section_name (const struct dwarf2_section_info *section)
2366 asection *sectp = get_section_bfd_section (section);
2368 gdb_assert (sectp != NULL);
2369 return bfd_section_name (get_section_bfd_owner (section), sectp);
2372 /* Return the name of the file SECTION is in. */
2375 get_section_file_name (const struct dwarf2_section_info *section)
2377 bfd *abfd = get_section_bfd_owner (section);
2379 return bfd_get_filename (abfd);
2382 /* Return the id of SECTION.
2383 Returns 0 if SECTION doesn't exist. */
2386 get_section_id (const struct dwarf2_section_info *section)
2388 asection *sectp = get_section_bfd_section (section);
2395 /* Return the flags of SECTION.
2396 SECTION (or containing section if this is a virtual section) must exist. */
2399 get_section_flags (const struct dwarf2_section_info *section)
2401 asection *sectp = get_section_bfd_section (section);
2403 gdb_assert (sectp != NULL);
2404 return bfd_get_section_flags (sectp->owner, sectp);
2407 /* When loading sections, we look either for uncompressed section or for
2408 compressed section names. */
2411 section_is_p (const char *section_name,
2412 const struct dwarf2_section_names *names)
2414 if (names->normal != NULL
2415 && strcmp (section_name, names->normal) == 0)
2417 if (names->compressed != NULL
2418 && strcmp (section_name, names->compressed) == 0)
2423 /* See declaration. */
2426 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2427 const dwarf2_debug_sections &names)
2429 flagword aflag = bfd_get_section_flags (abfd, sectp);
2431 if ((aflag & SEC_HAS_CONTENTS) == 0)
2434 else if (section_is_p (sectp->name, &names.info))
2436 this->info.s.section = sectp;
2437 this->info.size = bfd_get_section_size (sectp);
2439 else if (section_is_p (sectp->name, &names.abbrev))
2441 this->abbrev.s.section = sectp;
2442 this->abbrev.size = bfd_get_section_size (sectp);
2444 else if (section_is_p (sectp->name, &names.line))
2446 this->line.s.section = sectp;
2447 this->line.size = bfd_get_section_size (sectp);
2449 else if (section_is_p (sectp->name, &names.loc))
2451 this->loc.s.section = sectp;
2452 this->loc.size = bfd_get_section_size (sectp);
2454 else if (section_is_p (sectp->name, &names.loclists))
2456 this->loclists.s.section = sectp;
2457 this->loclists.size = bfd_get_section_size (sectp);
2459 else if (section_is_p (sectp->name, &names.macinfo))
2461 this->macinfo.s.section = sectp;
2462 this->macinfo.size = bfd_get_section_size (sectp);
2464 else if (section_is_p (sectp->name, &names.macro))
2466 this->macro.s.section = sectp;
2467 this->macro.size = bfd_get_section_size (sectp);
2469 else if (section_is_p (sectp->name, &names.str))
2471 this->str.s.section = sectp;
2472 this->str.size = bfd_get_section_size (sectp);
2474 else if (section_is_p (sectp->name, &names.line_str))
2476 this->line_str.s.section = sectp;
2477 this->line_str.size = bfd_get_section_size (sectp);
2479 else if (section_is_p (sectp->name, &names.addr))
2481 this->addr.s.section = sectp;
2482 this->addr.size = bfd_get_section_size (sectp);
2484 else if (section_is_p (sectp->name, &names.frame))
2486 this->frame.s.section = sectp;
2487 this->frame.size = bfd_get_section_size (sectp);
2489 else if (section_is_p (sectp->name, &names.eh_frame))
2491 this->eh_frame.s.section = sectp;
2492 this->eh_frame.size = bfd_get_section_size (sectp);
2494 else if (section_is_p (sectp->name, &names.ranges))
2496 this->ranges.s.section = sectp;
2497 this->ranges.size = bfd_get_section_size (sectp);
2499 else if (section_is_p (sectp->name, &names.rnglists))
2501 this->rnglists.s.section = sectp;
2502 this->rnglists.size = bfd_get_section_size (sectp);
2504 else if (section_is_p (sectp->name, &names.types))
2506 struct dwarf2_section_info type_section;
2508 memset (&type_section, 0, sizeof (type_section));
2509 type_section.s.section = sectp;
2510 type_section.size = bfd_get_section_size (sectp);
2512 VEC_safe_push (dwarf2_section_info_def, this->types,
2515 else if (section_is_p (sectp->name, &names.gdb_index))
2517 this->gdb_index.s.section = sectp;
2518 this->gdb_index.size = bfd_get_section_size (sectp);
2521 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2522 && bfd_section_vma (abfd, sectp) == 0)
2523 this->has_section_at_zero = true;
2526 /* A helper function that decides whether a section is empty,
2530 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2532 if (section->is_virtual)
2533 return section->size == 0;
2534 return section->s.section == NULL || section->size == 0;
2537 /* Read the contents of the section INFO.
2538 OBJFILE is the main object file, but not necessarily the file where
2539 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2541 If the section is compressed, uncompress it before returning. */
2544 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2548 gdb_byte *buf, *retbuf;
2552 info->buffer = NULL;
2555 if (dwarf2_section_empty_p (info))
2558 sectp = get_section_bfd_section (info);
2560 /* If this is a virtual section we need to read in the real one first. */
2561 if (info->is_virtual)
2563 struct dwarf2_section_info *containing_section =
2564 get_containing_section (info);
2566 gdb_assert (sectp != NULL);
2567 if ((sectp->flags & SEC_RELOC) != 0)
2569 error (_("Dwarf Error: DWP format V2 with relocations is not"
2570 " supported in section %s [in module %s]"),
2571 get_section_name (info), get_section_file_name (info));
2573 dwarf2_read_section (objfile, containing_section);
2574 /* Other code should have already caught virtual sections that don't
2576 gdb_assert (info->virtual_offset + info->size
2577 <= containing_section->size);
2578 /* If the real section is empty or there was a problem reading the
2579 section we shouldn't get here. */
2580 gdb_assert (containing_section->buffer != NULL);
2581 info->buffer = containing_section->buffer + info->virtual_offset;
2585 /* If the section has relocations, we must read it ourselves.
2586 Otherwise we attach it to the BFD. */
2587 if ((sectp->flags & SEC_RELOC) == 0)
2589 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2593 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2596 /* When debugging .o files, we may need to apply relocations; see
2597 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2598 We never compress sections in .o files, so we only need to
2599 try this when the section is not compressed. */
2600 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2603 info->buffer = retbuf;
2607 abfd = get_section_bfd_owner (info);
2608 gdb_assert (abfd != NULL);
2610 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2611 || bfd_bread (buf, info->size, abfd) != info->size)
2613 error (_("Dwarf Error: Can't read DWARF data"
2614 " in section %s [in module %s]"),
2615 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2619 /* A helper function that returns the size of a section in a safe way.
2620 If you are positive that the section has been read before using the
2621 size, then it is safe to refer to the dwarf2_section_info object's
2622 "size" field directly. In other cases, you must call this
2623 function, because for compressed sections the size field is not set
2624 correctly until the section has been read. */
2626 static bfd_size_type
2627 dwarf2_section_size (struct objfile *objfile,
2628 struct dwarf2_section_info *info)
2631 dwarf2_read_section (objfile, info);
2635 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2639 dwarf2_get_section_info (struct objfile *objfile,
2640 enum dwarf2_section_enum sect,
2641 asection **sectp, const gdb_byte **bufp,
2642 bfd_size_type *sizep)
2644 struct dwarf2_per_objfile *data
2645 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2646 dwarf2_objfile_data_key);
2647 struct dwarf2_section_info *info;
2649 /* We may see an objfile without any DWARF, in which case we just
2660 case DWARF2_DEBUG_FRAME:
2661 info = &data->frame;
2663 case DWARF2_EH_FRAME:
2664 info = &data->eh_frame;
2667 gdb_assert_not_reached ("unexpected section");
2670 dwarf2_read_section (objfile, info);
2672 *sectp = get_section_bfd_section (info);
2673 *bufp = info->buffer;
2674 *sizep = info->size;
2677 /* A helper function to find the sections for a .dwz file. */
2680 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2682 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2684 /* Note that we only support the standard ELF names, because .dwz
2685 is ELF-only (at the time of writing). */
2686 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2688 dwz_file->abbrev.s.section = sectp;
2689 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2691 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2693 dwz_file->info.s.section = sectp;
2694 dwz_file->info.size = bfd_get_section_size (sectp);
2696 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2698 dwz_file->str.s.section = sectp;
2699 dwz_file->str.size = bfd_get_section_size (sectp);
2701 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2703 dwz_file->line.s.section = sectp;
2704 dwz_file->line.size = bfd_get_section_size (sectp);
2706 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2708 dwz_file->macro.s.section = sectp;
2709 dwz_file->macro.size = bfd_get_section_size (sectp);
2711 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2713 dwz_file->gdb_index.s.section = sectp;
2714 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2718 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2719 there is no .gnu_debugaltlink section in the file. Error if there
2720 is such a section but the file cannot be found. */
2722 static struct dwz_file *
2723 dwarf2_get_dwz_file (void)
2725 const char *filename;
2726 struct dwz_file *result;
2727 bfd_size_type buildid_len_arg;
2731 if (dwarf2_per_objfile->dwz_file != NULL)
2732 return dwarf2_per_objfile->dwz_file;
2734 bfd_set_error (bfd_error_no_error);
2735 gdb::unique_xmalloc_ptr<char> data
2736 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2737 &buildid_len_arg, &buildid));
2740 if (bfd_get_error () == bfd_error_no_error)
2742 error (_("could not read '.gnu_debugaltlink' section: %s"),
2743 bfd_errmsg (bfd_get_error ()));
2746 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2748 buildid_len = (size_t) buildid_len_arg;
2750 filename = data.get ();
2752 std::string abs_storage;
2753 if (!IS_ABSOLUTE_PATH (filename))
2755 gdb::unique_xmalloc_ptr<char> abs
2756 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2758 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2759 filename = abs_storage.c_str ();
2762 /* First try the file name given in the section. If that doesn't
2763 work, try to use the build-id instead. */
2764 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2765 if (dwz_bfd != NULL)
2767 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2771 if (dwz_bfd == NULL)
2772 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2774 if (dwz_bfd == NULL)
2775 error (_("could not find '.gnu_debugaltlink' file for %s"),
2776 objfile_name (dwarf2_per_objfile->objfile));
2778 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2780 result->dwz_bfd = dwz_bfd.release ();
2782 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2784 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2785 dwarf2_per_objfile->dwz_file = result;
2789 /* DWARF quick_symbols_functions support. */
2791 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2792 unique line tables, so we maintain a separate table of all .debug_line
2793 derived entries to support the sharing.
2794 All the quick functions need is the list of file names. We discard the
2795 line_header when we're done and don't need to record it here. */
2796 struct quick_file_names
2798 /* The data used to construct the hash key. */
2799 struct stmt_list_hash hash;
2801 /* The number of entries in file_names, real_names. */
2802 unsigned int num_file_names;
2804 /* The file names from the line table, after being run through
2806 const char **file_names;
2808 /* The file names from the line table after being run through
2809 gdb_realpath. These are computed lazily. */
2810 const char **real_names;
2813 /* When using the index (and thus not using psymtabs), each CU has an
2814 object of this type. This is used to hold information needed by
2815 the various "quick" methods. */
2816 struct dwarf2_per_cu_quick_data
2818 /* The file table. This can be NULL if there was no file table
2819 or it's currently not read in.
2820 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2821 struct quick_file_names *file_names;
2823 /* The corresponding symbol table. This is NULL if symbols for this
2824 CU have not yet been read. */
2825 struct compunit_symtab *compunit_symtab;
2827 /* A temporary mark bit used when iterating over all CUs in
2828 expand_symtabs_matching. */
2829 unsigned int mark : 1;
2831 /* True if we've tried to read the file table and found there isn't one.
2832 There will be no point in trying to read it again next time. */
2833 unsigned int no_file_data : 1;
2836 /* Utility hash function for a stmt_list_hash. */
2839 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2843 if (stmt_list_hash->dwo_unit != NULL)
2844 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2845 v += to_underlying (stmt_list_hash->line_sect_off);
2849 /* Utility equality function for a stmt_list_hash. */
2852 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2853 const struct stmt_list_hash *rhs)
2855 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2857 if (lhs->dwo_unit != NULL
2858 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2861 return lhs->line_sect_off == rhs->line_sect_off;
2864 /* Hash function for a quick_file_names. */
2867 hash_file_name_entry (const void *e)
2869 const struct quick_file_names *file_data
2870 = (const struct quick_file_names *) e;
2872 return hash_stmt_list_entry (&file_data->hash);
2875 /* Equality function for a quick_file_names. */
2878 eq_file_name_entry (const void *a, const void *b)
2880 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2881 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2883 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2886 /* Delete function for a quick_file_names. */
2889 delete_file_name_entry (void *e)
2891 struct quick_file_names *file_data = (struct quick_file_names *) e;
2894 for (i = 0; i < file_data->num_file_names; ++i)
2896 xfree ((void*) file_data->file_names[i]);
2897 if (file_data->real_names)
2898 xfree ((void*) file_data->real_names[i]);
2901 /* The space for the struct itself lives on objfile_obstack,
2902 so we don't free it here. */
2905 /* Create a quick_file_names hash table. */
2908 create_quick_file_names_table (unsigned int nr_initial_entries)
2910 return htab_create_alloc (nr_initial_entries,
2911 hash_file_name_entry, eq_file_name_entry,
2912 delete_file_name_entry, xcalloc, xfree);
2915 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2916 have to be created afterwards. You should call age_cached_comp_units after
2917 processing PER_CU->CU. dw2_setup must have been already called. */
2920 load_cu (struct dwarf2_per_cu_data *per_cu)
2922 if (per_cu->is_debug_types)
2923 load_full_type_unit (per_cu);
2925 load_full_comp_unit (per_cu, language_minimal);
2927 if (per_cu->cu == NULL)
2928 return; /* Dummy CU. */
2930 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2933 /* Read in the symbols for PER_CU. */
2936 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2938 struct cleanup *back_to;
2940 /* Skip type_unit_groups, reading the type units they contain
2941 is handled elsewhere. */
2942 if (IS_TYPE_UNIT_GROUP (per_cu))
2945 back_to = make_cleanup (dwarf2_release_queue, NULL);
2947 if (dwarf2_per_objfile->using_index
2948 ? per_cu->v.quick->compunit_symtab == NULL
2949 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2951 queue_comp_unit (per_cu, language_minimal);
2954 /* If we just loaded a CU from a DWO, and we're working with an index
2955 that may badly handle TUs, load all the TUs in that DWO as well.
2956 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2957 if (!per_cu->is_debug_types
2958 && per_cu->cu != NULL
2959 && per_cu->cu->dwo_unit != NULL
2960 && dwarf2_per_objfile->index_table != NULL
2961 && dwarf2_per_objfile->index_table->version <= 7
2962 /* DWP files aren't supported yet. */
2963 && get_dwp_file () == NULL)
2964 queue_and_load_all_dwo_tus (per_cu);
2969 /* Age the cache, releasing compilation units that have not
2970 been used recently. */
2971 age_cached_comp_units ();
2973 do_cleanups (back_to);
2976 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2977 the objfile from which this CU came. Returns the resulting symbol
2980 static struct compunit_symtab *
2981 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2983 gdb_assert (dwarf2_per_objfile->using_index);
2984 if (!per_cu->v.quick->compunit_symtab)
2986 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2987 scoped_restore decrementer = increment_reading_symtab ();
2988 dw2_do_instantiate_symtab (per_cu);
2989 process_cu_includes ();
2990 do_cleanups (back_to);
2993 return per_cu->v.quick->compunit_symtab;
2996 /* Return the CU/TU given its index.
2998 This is intended for loops like:
3000 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3001 + dwarf2_per_objfile->n_type_units); ++i)
3003 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3009 static struct dwarf2_per_cu_data *
3010 dw2_get_cutu (int index)
3012 if (index >= dwarf2_per_objfile->n_comp_units)
3014 index -= dwarf2_per_objfile->n_comp_units;
3015 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3016 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3019 return dwarf2_per_objfile->all_comp_units[index];
3022 /* Return the CU given its index.
3023 This differs from dw2_get_cutu in that it's for when you know INDEX
3026 static struct dwarf2_per_cu_data *
3027 dw2_get_cu (int index)
3029 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3031 return dwarf2_per_objfile->all_comp_units[index];
3034 /* A helper for create_cus_from_index that handles a given list of
3038 create_cus_from_index_list (struct objfile *objfile,
3039 const gdb_byte *cu_list, offset_type n_elements,
3040 struct dwarf2_section_info *section,
3046 for (i = 0; i < n_elements; i += 2)
3048 gdb_static_assert (sizeof (ULONGEST) >= 8);
3050 sect_offset sect_off
3051 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3052 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3055 dwarf2_per_cu_data *the_cu
3056 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3057 struct dwarf2_per_cu_data);
3058 the_cu->sect_off = sect_off;
3059 the_cu->length = length;
3060 the_cu->objfile = objfile;
3061 the_cu->section = section;
3062 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3063 struct dwarf2_per_cu_quick_data);
3064 the_cu->is_dwz = is_dwz;
3065 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
3069 /* Read the CU list from the mapped index, and use it to create all
3070 the CU objects for this objfile. */
3073 create_cus_from_index (struct objfile *objfile,
3074 const gdb_byte *cu_list, offset_type cu_list_elements,
3075 const gdb_byte *dwz_list, offset_type dwz_elements)
3077 struct dwz_file *dwz;
3079 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3080 dwarf2_per_objfile->all_comp_units =
3081 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3082 dwarf2_per_objfile->n_comp_units);
3084 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3085 &dwarf2_per_objfile->info, 0, 0);
3087 if (dwz_elements == 0)
3090 dwz = dwarf2_get_dwz_file ();
3091 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3092 cu_list_elements / 2);
3095 /* Create the signatured type hash table from the index. */
3098 create_signatured_type_table_from_index (struct objfile *objfile,
3099 struct dwarf2_section_info *section,
3100 const gdb_byte *bytes,
3101 offset_type elements)
3104 htab_t sig_types_hash;
3106 dwarf2_per_objfile->n_type_units
3107 = dwarf2_per_objfile->n_allocated_type_units
3109 dwarf2_per_objfile->all_type_units =
3110 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3112 sig_types_hash = allocate_signatured_type_table (objfile);
3114 for (i = 0; i < elements; i += 3)
3116 struct signatured_type *sig_type;
3119 cu_offset type_offset_in_tu;
3121 gdb_static_assert (sizeof (ULONGEST) >= 8);
3122 sect_offset sect_off
3123 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3125 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3127 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3130 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3131 struct signatured_type);
3132 sig_type->signature = signature;
3133 sig_type->type_offset_in_tu = type_offset_in_tu;
3134 sig_type->per_cu.is_debug_types = 1;
3135 sig_type->per_cu.section = section;
3136 sig_type->per_cu.sect_off = sect_off;
3137 sig_type->per_cu.objfile = objfile;
3138 sig_type->per_cu.v.quick
3139 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3140 struct dwarf2_per_cu_quick_data);
3142 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3145 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3148 dwarf2_per_objfile->signatured_types = sig_types_hash;
3151 /* Read the address map data from the mapped index, and use it to
3152 populate the objfile's psymtabs_addrmap. */
3155 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3157 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3158 const gdb_byte *iter, *end;
3159 struct addrmap *mutable_map;
3162 auto_obstack temp_obstack;
3164 mutable_map = addrmap_create_mutable (&temp_obstack);
3166 iter = index->address_table;
3167 end = iter + index->address_table_size;
3169 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3173 ULONGEST hi, lo, cu_index;
3174 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3176 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3178 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3183 complaint (&symfile_complaints,
3184 _(".gdb_index address table has invalid range (%s - %s)"),
3185 hex_string (lo), hex_string (hi));
3189 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3191 complaint (&symfile_complaints,
3192 _(".gdb_index address table has invalid CU number %u"),
3193 (unsigned) cu_index);
3197 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3198 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3199 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3202 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3203 &objfile->objfile_obstack);
3206 /* The hash function for strings in the mapped index. This is the same as
3207 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3208 implementation. This is necessary because the hash function is tied to the
3209 format of the mapped index file. The hash values do not have to match with
3212 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3215 mapped_index_string_hash (int index_version, const void *p)
3217 const unsigned char *str = (const unsigned char *) p;
3221 while ((c = *str++) != 0)
3223 if (index_version >= 5)
3225 r = r * 67 + c - 113;
3231 /* Find a slot in the mapped index INDEX for the object named NAME.
3232 If NAME is found, set *VEC_OUT to point to the CU vector in the
3233 constant pool and return true. If NAME cannot be found, return
3237 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3238 offset_type **vec_out)
3241 offset_type slot, step;
3242 int (*cmp) (const char *, const char *);
3244 gdb::unique_xmalloc_ptr<char> without_params;
3245 if (current_language->la_language == language_cplus
3246 || current_language->la_language == language_fortran
3247 || current_language->la_language == language_d)
3249 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3252 if (strchr (name, '(') != NULL)
3254 without_params = cp_remove_params (name);
3256 if (without_params != NULL)
3257 name = without_params.get ();
3261 /* Index version 4 did not support case insensitive searches. But the
3262 indices for case insensitive languages are built in lowercase, therefore
3263 simulate our NAME being searched is also lowercased. */
3264 hash = mapped_index_string_hash ((index->version == 4
3265 && case_sensitivity == case_sensitive_off
3266 ? 5 : index->version),
3269 slot = hash & (index->symbol_table_slots - 1);
3270 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3271 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3275 /* Convert a slot number to an offset into the table. */
3276 offset_type i = 2 * slot;
3278 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3281 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3282 if (!cmp (name, str))
3284 *vec_out = (offset_type *) (index->constant_pool
3285 + MAYBE_SWAP (index->symbol_table[i + 1]));
3289 slot = (slot + step) & (index->symbol_table_slots - 1);
3293 /* A helper function that reads the .gdb_index from SECTION and fills
3294 in MAP. FILENAME is the name of the file containing the section;
3295 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3296 ok to use deprecated sections.
3298 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3299 out parameters that are filled in with information about the CU and
3300 TU lists in the section.
3302 Returns 1 if all went well, 0 otherwise. */
3305 read_index_from_section (struct objfile *objfile,
3306 const char *filename,
3308 struct dwarf2_section_info *section,
3309 struct mapped_index *map,
3310 const gdb_byte **cu_list,
3311 offset_type *cu_list_elements,
3312 const gdb_byte **types_list,
3313 offset_type *types_list_elements)
3315 const gdb_byte *addr;
3316 offset_type version;
3317 offset_type *metadata;
3320 if (dwarf2_section_empty_p (section))
3323 /* Older elfutils strip versions could keep the section in the main
3324 executable while splitting it for the separate debug info file. */
3325 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3328 dwarf2_read_section (objfile, section);
3330 addr = section->buffer;
3331 /* Version check. */
3332 version = MAYBE_SWAP (*(offset_type *) addr);
3333 /* Versions earlier than 3 emitted every copy of a psymbol. This
3334 causes the index to behave very poorly for certain requests. Version 3
3335 contained incomplete addrmap. So, it seems better to just ignore such
3339 static int warning_printed = 0;
3340 if (!warning_printed)
3342 warning (_("Skipping obsolete .gdb_index section in %s."),
3344 warning_printed = 1;
3348 /* Index version 4 uses a different hash function than index version
3351 Versions earlier than 6 did not emit psymbols for inlined
3352 functions. Using these files will cause GDB not to be able to
3353 set breakpoints on inlined functions by name, so we ignore these
3354 indices unless the user has done
3355 "set use-deprecated-index-sections on". */
3356 if (version < 6 && !deprecated_ok)
3358 static int warning_printed = 0;
3359 if (!warning_printed)
3362 Skipping deprecated .gdb_index section in %s.\n\
3363 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3364 to use the section anyway."),
3366 warning_printed = 1;
3370 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3371 of the TU (for symbols coming from TUs),
3372 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3373 Plus gold-generated indices can have duplicate entries for global symbols,
3374 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3375 These are just performance bugs, and we can't distinguish gdb-generated
3376 indices from gold-generated ones, so issue no warning here. */
3378 /* Indexes with higher version than the one supported by GDB may be no
3379 longer backward compatible. */
3383 map->version = version;
3384 map->total_size = section->size;
3386 metadata = (offset_type *) (addr + sizeof (offset_type));
3389 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3390 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3394 *types_list = addr + MAYBE_SWAP (metadata[i]);
3395 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3396 - MAYBE_SWAP (metadata[i]))
3400 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3401 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3402 - MAYBE_SWAP (metadata[i]));
3405 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3406 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3407 - MAYBE_SWAP (metadata[i]))
3408 / (2 * sizeof (offset_type)));
3411 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3417 /* Read the index file. If everything went ok, initialize the "quick"
3418 elements of all the CUs and return 1. Otherwise, return 0. */
3421 dwarf2_read_index (struct objfile *objfile)
3423 struct mapped_index local_map, *map;
3424 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3425 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3426 struct dwz_file *dwz;
3428 if (!read_index_from_section (objfile, objfile_name (objfile),
3429 use_deprecated_index_sections,
3430 &dwarf2_per_objfile->gdb_index, &local_map,
3431 &cu_list, &cu_list_elements,
3432 &types_list, &types_list_elements))
3435 /* Don't use the index if it's empty. */
3436 if (local_map.symbol_table_slots == 0)
3439 /* If there is a .dwz file, read it so we can get its CU list as
3441 dwz = dwarf2_get_dwz_file ();
3444 struct mapped_index dwz_map;
3445 const gdb_byte *dwz_types_ignore;
3446 offset_type dwz_types_elements_ignore;
3448 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3450 &dwz->gdb_index, &dwz_map,
3451 &dwz_list, &dwz_list_elements,
3453 &dwz_types_elements_ignore))
3455 warning (_("could not read '.gdb_index' section from %s; skipping"),
3456 bfd_get_filename (dwz->dwz_bfd));
3461 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3464 if (types_list_elements)
3466 struct dwarf2_section_info *section;
3468 /* We can only handle a single .debug_types when we have an
3470 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3473 section = VEC_index (dwarf2_section_info_def,
3474 dwarf2_per_objfile->types, 0);
3476 create_signatured_type_table_from_index (objfile, section, types_list,
3477 types_list_elements);
3480 create_addrmap_from_index (objfile, &local_map);
3482 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3483 map = new (map) mapped_index ();
3486 dwarf2_per_objfile->index_table = map;
3487 dwarf2_per_objfile->using_index = 1;
3488 dwarf2_per_objfile->quick_file_names_table =
3489 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3494 /* A helper for the "quick" functions which sets the global
3495 dwarf2_per_objfile according to OBJFILE. */
3498 dw2_setup (struct objfile *objfile)
3500 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3501 objfile_data (objfile, dwarf2_objfile_data_key));
3502 gdb_assert (dwarf2_per_objfile);
3505 /* die_reader_func for dw2_get_file_names. */
3508 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3509 const gdb_byte *info_ptr,
3510 struct die_info *comp_unit_die,
3514 struct dwarf2_cu *cu = reader->cu;
3515 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3516 struct objfile *objfile = dwarf2_per_objfile->objfile;
3517 struct dwarf2_per_cu_data *lh_cu;
3518 struct attribute *attr;
3521 struct quick_file_names *qfn;
3523 gdb_assert (! this_cu->is_debug_types);
3525 /* Our callers never want to match partial units -- instead they
3526 will match the enclosing full CU. */
3527 if (comp_unit_die->tag == DW_TAG_partial_unit)
3529 this_cu->v.quick->no_file_data = 1;
3537 sect_offset line_offset {};
3539 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3542 struct quick_file_names find_entry;
3544 line_offset = (sect_offset) DW_UNSND (attr);
3546 /* We may have already read in this line header (TU line header sharing).
3547 If we have we're done. */
3548 find_entry.hash.dwo_unit = cu->dwo_unit;
3549 find_entry.hash.line_sect_off = line_offset;
3550 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3551 &find_entry, INSERT);
3554 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3558 lh = dwarf_decode_line_header (line_offset, cu);
3562 lh_cu->v.quick->no_file_data = 1;
3566 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3567 qfn->hash.dwo_unit = cu->dwo_unit;
3568 qfn->hash.line_sect_off = line_offset;
3569 gdb_assert (slot != NULL);
3572 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3574 qfn->num_file_names = lh->file_names.size ();
3576 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3577 for (i = 0; i < lh->file_names.size (); ++i)
3578 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3579 qfn->real_names = NULL;
3581 lh_cu->v.quick->file_names = qfn;
3584 /* A helper for the "quick" functions which attempts to read the line
3585 table for THIS_CU. */
3587 static struct quick_file_names *
3588 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3590 /* This should never be called for TUs. */
3591 gdb_assert (! this_cu->is_debug_types);
3592 /* Nor type unit groups. */
3593 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3595 if (this_cu->v.quick->file_names != NULL)
3596 return this_cu->v.quick->file_names;
3597 /* If we know there is no line data, no point in looking again. */
3598 if (this_cu->v.quick->no_file_data)
3601 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3603 if (this_cu->v.quick->no_file_data)
3605 return this_cu->v.quick->file_names;
3608 /* A helper for the "quick" functions which computes and caches the
3609 real path for a given file name from the line table. */
3612 dw2_get_real_path (struct objfile *objfile,
3613 struct quick_file_names *qfn, int index)
3615 if (qfn->real_names == NULL)
3616 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3617 qfn->num_file_names, const char *);
3619 if (qfn->real_names[index] == NULL)
3620 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3622 return qfn->real_names[index];
3625 static struct symtab *
3626 dw2_find_last_source_symtab (struct objfile *objfile)
3628 struct compunit_symtab *cust;
3631 dw2_setup (objfile);
3632 index = dwarf2_per_objfile->n_comp_units - 1;
3633 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3636 return compunit_primary_filetab (cust);
3639 /* Traversal function for dw2_forget_cached_source_info. */
3642 dw2_free_cached_file_names (void **slot, void *info)
3644 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3646 if (file_data->real_names)
3650 for (i = 0; i < file_data->num_file_names; ++i)
3652 xfree ((void*) file_data->real_names[i]);
3653 file_data->real_names[i] = NULL;
3661 dw2_forget_cached_source_info (struct objfile *objfile)
3663 dw2_setup (objfile);
3665 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3666 dw2_free_cached_file_names, NULL);
3669 /* Helper function for dw2_map_symtabs_matching_filename that expands
3670 the symtabs and calls the iterator. */
3673 dw2_map_expand_apply (struct objfile *objfile,
3674 struct dwarf2_per_cu_data *per_cu,
3675 const char *name, const char *real_path,
3676 gdb::function_view<bool (symtab *)> callback)
3678 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3680 /* Don't visit already-expanded CUs. */
3681 if (per_cu->v.quick->compunit_symtab)
3684 /* This may expand more than one symtab, and we want to iterate over
3686 dw2_instantiate_symtab (per_cu);
3688 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3689 last_made, callback);
3692 /* Implementation of the map_symtabs_matching_filename method. */
3695 dw2_map_symtabs_matching_filename
3696 (struct objfile *objfile, const char *name, const char *real_path,
3697 gdb::function_view<bool (symtab *)> callback)
3700 const char *name_basename = lbasename (name);
3702 dw2_setup (objfile);
3704 /* The rule is CUs specify all the files, including those used by
3705 any TU, so there's no need to scan TUs here. */
3707 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3711 struct quick_file_names *file_data;
3713 /* We only need to look at symtabs not already expanded. */
3714 if (per_cu->v.quick->compunit_symtab)
3717 file_data = dw2_get_file_names (per_cu);
3718 if (file_data == NULL)
3721 for (j = 0; j < file_data->num_file_names; ++j)
3723 const char *this_name = file_data->file_names[j];
3724 const char *this_real_name;
3726 if (compare_filenames_for_search (this_name, name))
3728 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3734 /* Before we invoke realpath, which can get expensive when many
3735 files are involved, do a quick comparison of the basenames. */
3736 if (! basenames_may_differ
3737 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3740 this_real_name = dw2_get_real_path (objfile, file_data, j);
3741 if (compare_filenames_for_search (this_real_name, name))
3743 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3749 if (real_path != NULL)
3751 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3752 gdb_assert (IS_ABSOLUTE_PATH (name));
3753 if (this_real_name != NULL
3754 && FILENAME_CMP (real_path, this_real_name) == 0)
3756 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3768 /* Struct used to manage iterating over all CUs looking for a symbol. */
3770 struct dw2_symtab_iterator
3772 /* The internalized form of .gdb_index. */
3773 struct mapped_index *index;
3774 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3775 int want_specific_block;
3776 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3777 Unused if !WANT_SPECIFIC_BLOCK. */
3779 /* The kind of symbol we're looking for. */
3781 /* The list of CUs from the index entry of the symbol,
3782 or NULL if not found. */
3784 /* The next element in VEC to look at. */
3786 /* The number of elements in VEC, or zero if there is no match. */
3788 /* Have we seen a global version of the symbol?
3789 If so we can ignore all further global instances.
3790 This is to work around gold/15646, inefficient gold-generated
3795 /* Initialize the index symtab iterator ITER.
3796 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3797 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3800 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3801 struct mapped_index *index,
3802 int want_specific_block,
3807 iter->index = index;
3808 iter->want_specific_block = want_specific_block;
3809 iter->block_index = block_index;
3810 iter->domain = domain;
3812 iter->global_seen = 0;
3814 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3815 iter->length = MAYBE_SWAP (*iter->vec);
3823 /* Return the next matching CU or NULL if there are no more. */
3825 static struct dwarf2_per_cu_data *
3826 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3828 for ( ; iter->next < iter->length; ++iter->next)
3830 offset_type cu_index_and_attrs =
3831 MAYBE_SWAP (iter->vec[iter->next + 1]);
3832 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3833 struct dwarf2_per_cu_data *per_cu;
3834 int want_static = iter->block_index != GLOBAL_BLOCK;
3835 /* This value is only valid for index versions >= 7. */
3836 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3837 gdb_index_symbol_kind symbol_kind =
3838 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3839 /* Only check the symbol attributes if they're present.
3840 Indices prior to version 7 don't record them,
3841 and indices >= 7 may elide them for certain symbols
3842 (gold does this). */
3844 (iter->index->version >= 7
3845 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3847 /* Don't crash on bad data. */
3848 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3849 + dwarf2_per_objfile->n_type_units))
3851 complaint (&symfile_complaints,
3852 _(".gdb_index entry has bad CU index"
3854 objfile_name (dwarf2_per_objfile->objfile));
3858 per_cu = dw2_get_cutu (cu_index);
3860 /* Skip if already read in. */
3861 if (per_cu->v.quick->compunit_symtab)
3864 /* Check static vs global. */
3867 if (iter->want_specific_block
3868 && want_static != is_static)
3870 /* Work around gold/15646. */
3871 if (!is_static && iter->global_seen)
3874 iter->global_seen = 1;
3877 /* Only check the symbol's kind if it has one. */
3880 switch (iter->domain)
3883 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3884 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3885 /* Some types are also in VAR_DOMAIN. */
3886 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3890 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3894 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3909 static struct compunit_symtab *
3910 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3911 const char *name, domain_enum domain)
3913 struct compunit_symtab *stab_best = NULL;
3914 struct mapped_index *index;
3916 dw2_setup (objfile);
3918 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3920 index = dwarf2_per_objfile->index_table;
3922 /* index is NULL if OBJF_READNOW. */
3925 struct dw2_symtab_iterator iter;
3926 struct dwarf2_per_cu_data *per_cu;
3928 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3930 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3932 struct symbol *sym, *with_opaque = NULL;
3933 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3934 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3935 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3937 sym = block_find_symbol (block, name, domain,
3938 block_find_non_opaque_type_preferred,
3941 /* Some caution must be observed with overloaded functions
3942 and methods, since the index will not contain any overload
3943 information (but NAME might contain it). */
3946 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3948 if (with_opaque != NULL
3949 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3952 /* Keep looking through other CUs. */
3960 dw2_print_stats (struct objfile *objfile)
3962 int i, total, count;
3964 dw2_setup (objfile);
3965 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3967 for (i = 0; i < total; ++i)
3969 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3971 if (!per_cu->v.quick->compunit_symtab)
3974 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3975 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3978 /* This dumps minimal information about the index.
3979 It is called via "mt print objfiles".
3980 One use is to verify .gdb_index has been loaded by the
3981 gdb.dwarf2/gdb-index.exp testcase. */
3984 dw2_dump (struct objfile *objfile)
3986 dw2_setup (objfile);
3987 gdb_assert (dwarf2_per_objfile->using_index);
3988 printf_filtered (".gdb_index:");
3989 if (dwarf2_per_objfile->index_table != NULL)
3991 printf_filtered (" version %d\n",
3992 dwarf2_per_objfile->index_table->version);
3995 printf_filtered (" faked for \"readnow\"\n");
3996 printf_filtered ("\n");
4000 dw2_relocate (struct objfile *objfile,
4001 const struct section_offsets *new_offsets,
4002 const struct section_offsets *delta)
4004 /* There's nothing to relocate here. */
4008 dw2_expand_symtabs_for_function (struct objfile *objfile,
4009 const char *func_name)
4011 struct mapped_index *index;
4013 dw2_setup (objfile);
4015 index = dwarf2_per_objfile->index_table;
4017 /* index is NULL if OBJF_READNOW. */
4020 struct dw2_symtab_iterator iter;
4021 struct dwarf2_per_cu_data *per_cu;
4023 /* Note: It doesn't matter what we pass for block_index here. */
4024 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4027 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4028 dw2_instantiate_symtab (per_cu);
4033 dw2_expand_all_symtabs (struct objfile *objfile)
4037 dw2_setup (objfile);
4039 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4040 + dwarf2_per_objfile->n_type_units); ++i)
4042 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4044 dw2_instantiate_symtab (per_cu);
4049 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4050 const char *fullname)
4054 dw2_setup (objfile);
4056 /* We don't need to consider type units here.
4057 This is only called for examining code, e.g. expand_line_sal.
4058 There can be an order of magnitude (or more) more type units
4059 than comp units, and we avoid them if we can. */
4061 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4064 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4065 struct quick_file_names *file_data;
4067 /* We only need to look at symtabs not already expanded. */
4068 if (per_cu->v.quick->compunit_symtab)
4071 file_data = dw2_get_file_names (per_cu);
4072 if (file_data == NULL)
4075 for (j = 0; j < file_data->num_file_names; ++j)
4077 const char *this_fullname = file_data->file_names[j];
4079 if (filename_cmp (this_fullname, fullname) == 0)
4081 dw2_instantiate_symtab (per_cu);
4089 dw2_map_matching_symbols (struct objfile *objfile,
4090 const char * name, domain_enum domain,
4092 int (*callback) (struct block *,
4093 struct symbol *, void *),
4094 void *data, symbol_name_match_type match,
4095 symbol_compare_ftype *ordered_compare)
4097 /* Currently unimplemented; used for Ada. The function can be called if the
4098 current language is Ada for a non-Ada objfile using GNU index. As Ada
4099 does not look for non-Ada symbols this function should just return. */
4102 /* Symbol name matcher for .gdb_index names.
4104 Symbol names in .gdb_index have a few particularities:
4106 - There's no indication of which is the language of each symbol.
4108 Since each language has its own symbol name matching algorithm,
4109 and we don't know which language is the right one, we must match
4110 each symbol against all languages. This would be a potential
4111 performance problem if it were not mitigated by the
4112 mapped_index::name_components lookup table, which significantly
4113 reduces the number of times we need to call into this matcher,
4114 making it a non-issue.
4116 - Symbol names in the index have no overload (parameter)
4117 information. I.e., in C++, "foo(int)" and "foo(long)" both
4118 appear as "foo" in the index, for example.
4120 This means that the lookup names passed to the symbol name
4121 matcher functions must have no parameter information either
4122 because (e.g.) symbol search name "foo" does not match
4123 lookup-name "foo(int)" [while swapping search name for lookup
4126 class gdb_index_symbol_name_matcher
4129 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4130 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4132 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4133 Returns true if any matcher matches. */
4134 bool matches (const char *symbol_name);
4137 /* A reference to the lookup name we're matching against. */
4138 const lookup_name_info &m_lookup_name;
4140 /* A vector holding all the different symbol name matchers, for all
4142 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4145 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4146 (const lookup_name_info &lookup_name)
4147 : m_lookup_name (lookup_name)
4149 /* Prepare the vector of comparison functions upfront, to avoid
4150 doing the same work for each symbol. Care is taken to avoid
4151 matching with the same matcher more than once if/when multiple
4152 languages use the same matcher function. */
4153 auto &matchers = m_symbol_name_matcher_funcs;
4154 matchers.reserve (nr_languages);
4156 matchers.push_back (default_symbol_name_matcher);
4158 for (int i = 0; i < nr_languages; i++)
4160 const language_defn *lang = language_def ((enum language) i);
4161 if (lang->la_get_symbol_name_matcher != NULL)
4163 symbol_name_matcher_ftype *name_matcher
4164 = lang->la_get_symbol_name_matcher (m_lookup_name);
4166 /* Don't insert the same comparison routine more than once.
4167 Note that we do this linear walk instead of a cheaper
4168 sorted insert, or use a std::set or something like that,
4169 because relative order of function addresses is not
4170 stable. This is not a problem in practice because the
4171 number of supported languages is low, and the cost here
4172 is tiny compared to the number of searches we'll do
4173 afterwards using this object. */
4174 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4176 matchers.push_back (name_matcher);
4182 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4184 for (auto matches_name : m_symbol_name_matcher_funcs)
4185 if (matches_name (symbol_name, m_lookup_name, NULL))
4191 /* Starting from a search name, return the string that finds the upper
4192 bound of all strings that start with SEARCH_NAME in a sorted name
4193 list. Returns the empty string to indicate that the upper bound is
4194 the end of the list. */
4197 make_sort_after_prefix_name (const char *search_name)
4199 /* When looking to complete "func", we find the upper bound of all
4200 symbols that start with "func" by looking for where we'd insert
4201 the closest string that would follow "func" in lexicographical
4202 order. Usually, that's "func"-with-last-character-incremented,
4203 i.e. "fund". Mind non-ASCII characters, though. Usually those
4204 will be UTF-8 multi-byte sequences, but we can't be certain.
4205 Especially mind the 0xff character, which is a valid character in
4206 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4207 rule out compilers allowing it in identifiers. Note that
4208 conveniently, strcmp/strcasecmp are specified to compare
4209 characters interpreted as unsigned char. So what we do is treat
4210 the whole string as a base 256 number composed of a sequence of
4211 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4212 to 0, and carries 1 to the following more-significant position.
4213 If the very first character in SEARCH_NAME ends up incremented
4214 and carries/overflows, then the upper bound is the end of the
4215 list. The string after the empty string is also the empty
4218 Some examples of this operation:
4220 SEARCH_NAME => "+1" RESULT
4224 "\xff" "a" "\xff" => "\xff" "b"
4229 Then, with these symbols for example:
4235 completing "func" looks for symbols between "func" and
4236 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4237 which finds "func" and "func1", but not "fund".
4241 funcÿ (Latin1 'ÿ' [0xff])
4245 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4246 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4250 ÿÿ (Latin1 'ÿ' [0xff])
4253 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4254 the end of the list.
4256 std::string after = search_name;
4257 while (!after.empty () && (unsigned char) after.back () == 0xff)
4259 if (!after.empty ())
4260 after.back () = (unsigned char) after.back () + 1;
4264 /* Helper for dw2_expand_symtabs_matching that works with a
4265 mapped_index instead of the containing objfile. This is split to a
4266 separate function in order to be able to unit test the
4267 name_components matching using a mock mapped_index. For each
4268 symbol name that matches, calls MATCH_CALLBACK, passing it the
4269 symbol's index in the mapped_index symbol table. */
4272 dw2_expand_symtabs_matching_symbol
4273 (mapped_index &index,
4274 const lookup_name_info &lookup_name_in,
4275 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4276 enum search_domain kind,
4277 gdb::function_view<void (offset_type)> match_callback)
4279 lookup_name_info lookup_name_without_params
4280 = lookup_name_in.make_ignore_params ();
4281 gdb_index_symbol_name_matcher lookup_name_matcher
4282 (lookup_name_without_params);
4284 auto *name_cmp = case_sensitivity == case_sensitive_on ? strcmp : strcasecmp;
4286 /* Build the symbol name component sorted vector, if we haven't yet.
4287 The code below only knows how to break apart components of C++
4288 symbol names (and other languages that use '::' as
4289 namespace/module separator). If we add support for wild matching
4290 to some language that uses some other operator (E.g., Ada, Go and
4291 D use '.'), then we'll need to try splitting the symbol name
4292 according to that language too. Note that Ada does support wild
4293 matching, but doesn't currently support .gdb_index. */
4294 if (index.name_components.empty ())
4296 for (size_t iter = 0; iter < index.symbol_table_slots; ++iter)
4298 offset_type idx = 2 * iter;
4300 if (index.symbol_table[idx] == 0
4301 && index.symbol_table[idx + 1] == 0)
4304 const char *name = index.symbol_name_at (idx);
4306 /* Add each name component to the name component table. */
4307 unsigned int previous_len = 0;
4308 for (unsigned int current_len = cp_find_first_component (name);
4309 name[current_len] != '\0';
4310 current_len += cp_find_first_component (name + current_len))
4312 gdb_assert (name[current_len] == ':');
4313 index.name_components.push_back ({previous_len, idx});
4314 /* Skip the '::'. */
4316 previous_len = current_len;
4318 index.name_components.push_back ({previous_len, idx});
4321 /* Sort name_components elements by name. */
4322 auto name_comp_compare = [&] (const name_component &left,
4323 const name_component &right)
4325 const char *left_qualified = index.symbol_name_at (left.idx);
4326 const char *right_qualified = index.symbol_name_at (right.idx);
4328 const char *left_name = left_qualified + left.name_offset;
4329 const char *right_name = right_qualified + right.name_offset;
4331 return name_cmp (left_name, right_name) < 0;
4334 std::sort (index.name_components.begin (),
4335 index.name_components.end (),
4340 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4342 /* Comparison function object for lower_bound that matches against a
4343 given symbol name. */
4344 auto lookup_compare_lower = [&] (const name_component &elem,
4347 const char *elem_qualified = index.symbol_name_at (elem.idx);
4348 const char *elem_name = elem_qualified + elem.name_offset;
4349 return name_cmp (elem_name, name) < 0;
4352 /* Comparison function object for upper_bound that matches against a
4353 given symbol name. */
4354 auto lookup_compare_upper = [&] (const char *name,
4355 const name_component &elem)
4357 const char *elem_qualified = index.symbol_name_at (elem.idx);
4358 const char *elem_name = elem_qualified + elem.name_offset;
4359 return name_cmp (name, elem_name) < 0;
4362 auto begin = index.name_components.begin ();
4363 auto end = index.name_components.end ();
4365 /* Find the lower bound. */
4368 if (lookup_name_in.completion_mode () && cplus[0] == '\0')
4371 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4374 /* Find the upper bound. */
4377 if (lookup_name_in.completion_mode ())
4379 /* In completion mode, we want UPPER to point past all
4380 symbols names that have the same prefix. I.e., with
4381 these symbols, and completing "func":
4383 function << lower bound
4385 other_function << upper bound
4387 We find the upper bound by looking for the insertion
4388 point of "func"-with-last-character-incremented,
4390 std::string after = make_sort_after_prefix_name (cplus);
4393 return std::upper_bound (lower, end, after.c_str (),
4394 lookup_compare_upper);
4397 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4400 /* Now for each symbol name in range, check to see if we have a name
4401 match, and if so, call the MATCH_CALLBACK callback. */
4403 /* The same symbol may appear more than once in the range though.
4404 E.g., if we're looking for symbols that complete "w", and we have
4405 a symbol named "w1::w2", we'll find the two name components for
4406 that same symbol in the range. To be sure we only call the
4407 callback once per symbol, we first collect the symbol name
4408 indexes that matched in a temporary vector and ignore
4410 std::vector<offset_type> matches;
4411 matches.reserve (std::distance (lower, upper));
4413 for (;lower != upper; ++lower)
4415 const char *qualified = index.symbol_name_at (lower->idx);
4417 if (!lookup_name_matcher.matches (qualified)
4418 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4421 matches.push_back (lower->idx);
4424 std::sort (matches.begin (), matches.end ());
4426 /* Finally call the callback, once per match. */
4428 for (offset_type idx : matches)
4432 match_callback (idx);
4437 /* Above we use a type wider than idx's for 'prev', since 0 and
4438 (offset_type)-1 are both possible values. */
4439 static_assert (sizeof (prev) > sizeof (offset_type), "");
4444 namespace selftests { namespace dw2_expand_symtabs_matching {
4446 /* A wrapper around mapped_index that builds a mock mapped_index, from
4447 the symbol list passed as parameter to the constructor. */
4448 class mock_mapped_index
4452 mock_mapped_index (const char *(&symbols)[N])
4453 : mock_mapped_index (symbols, N)
4456 /* Access the built index. */
4457 mapped_index &index ()
4461 mock_mapped_index(const mock_mapped_index &) = delete;
4462 void operator= (const mock_mapped_index &) = delete;
4465 mock_mapped_index (const char **symbols, size_t symbols_size)
4467 /* No string can live at offset zero. Add a dummy entry. */
4468 obstack_grow_str0 (&m_constant_pool, "");
4470 for (size_t i = 0; i < symbols_size; i++)
4472 const char *sym = symbols[i];
4473 size_t offset = obstack_object_size (&m_constant_pool);
4474 obstack_grow_str0 (&m_constant_pool, sym);
4475 m_symbol_table.push_back (offset);
4476 m_symbol_table.push_back (0);
4479 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4480 m_index.symbol_table = m_symbol_table.data ();
4481 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4485 /* The built mapped_index. */
4486 mapped_index m_index{};
4488 /* The storage that the built mapped_index uses for symbol and
4489 constant pool tables. */
4490 std::vector<offset_type> m_symbol_table;
4491 auto_obstack m_constant_pool;
4494 /* Convenience function that converts a NULL pointer to a "<null>"
4495 string, to pass to print routines. */
4498 string_or_null (const char *str)
4500 return str != NULL ? str : "<null>";
4503 /* Check if a lookup_name_info built from
4504 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4505 index. EXPECTED_LIST is the list of expected matches, in expected
4506 matching order. If no match expected, then an empty list is
4507 specified. Returns true on success. On failure prints a warning
4508 indicating the file:line that failed, and returns false. */
4511 check_match (const char *file, int line,
4512 mock_mapped_index &mock_index,
4513 const char *name, symbol_name_match_type match_type,
4514 bool completion_mode,
4515 std::initializer_list<const char *> expected_list)
4517 lookup_name_info lookup_name (name, match_type, completion_mode);
4519 bool matched = true;
4521 auto mismatch = [&] (const char *expected_str,
4524 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4525 "expected=\"%s\", got=\"%s\"\n"),
4527 (match_type == symbol_name_match_type::FULL
4529 name, string_or_null (expected_str), string_or_null (got));
4533 auto expected_it = expected_list.begin ();
4534 auto expected_end = expected_list.end ();
4536 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4538 [&] (offset_type idx)
4540 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4541 const char *expected_str
4542 = expected_it == expected_end ? NULL : *expected_it++;
4544 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4545 mismatch (expected_str, matched_name);
4548 const char *expected_str
4549 = expected_it == expected_end ? NULL : *expected_it++;
4550 if (expected_str != NULL)
4551 mismatch (expected_str, NULL);
4556 /* The symbols added to the mock mapped_index for testing (in
4558 static const char *test_symbols[] = {
4568 /* These are used to check that the increment-last-char in the
4569 matching algorithm for completion doesn't match "t1_fund" when
4570 completing "t1_func". */
4576 /* A UTF-8 name with multi-byte sequences to make sure that
4577 cp-name-parser understands this as a single identifier ("função"
4578 is "function" in PT). */
4581 /* \377 (0xff) is Latin1 'ÿ'. */
4584 /* \377 (0xff) is Latin1 'ÿ'. */
4588 /* A name with all sorts of complications. Starts with "z" to make
4589 it easier for the completion tests below. */
4590 #define Z_SYM_NAME \
4591 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4592 "::tuple<(anonymous namespace)::ui*, " \
4593 "std::default_delete<(anonymous namespace)::ui>, void>"
4601 mock_mapped_index mock_index (test_symbols);
4603 /* We let all tests run until the end even if some fails, for debug
4605 bool any_mismatch = false;
4607 /* Create the expected symbols list (an initializer_list). Needed
4608 because lists have commas, and we need to pass them to CHECK,
4609 which is a macro. */
4610 #define EXPECT(...) { __VA_ARGS__ }
4612 /* Wrapper for check_match that passes down the current
4613 __FILE__/__LINE__. */
4614 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4615 any_mismatch |= !check_match (__FILE__, __LINE__, \
4617 NAME, MATCH_TYPE, COMPLETION_MODE, \
4620 /* Identity checks. */
4621 for (const char *sym : test_symbols)
4623 /* Should be able to match all existing symbols. */
4624 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4627 /* Should be able to match all existing symbols with
4629 std::string with_params = std::string (sym) + "(int)";
4630 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4633 /* Should be able to match all existing symbols with
4634 parameters and qualifiers. */
4635 with_params = std::string (sym) + " ( int ) const";
4636 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4639 /* This should really find sym, but cp-name-parser.y doesn't
4640 know about lvalue/rvalue qualifiers yet. */
4641 with_params = std::string (sym) + " ( int ) &&";
4642 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4646 /* Check that the name matching algorithm for completion doesn't get
4647 confused with Latin1 'ÿ' / 0xff. */
4649 static const char str[] = "\377";
4650 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4651 EXPECT ("\377", "\377\377123"));
4654 /* Check that the increment-last-char in the matching algorithm for
4655 completion doesn't match "t1_fund" when completing "t1_func". */
4657 static const char str[] = "t1_func";
4658 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4659 EXPECT ("t1_func", "t1_func1"));
4662 /* Check that completion mode works at each prefix of the expected
4665 static const char str[] = "function(int)";
4666 size_t len = strlen (str);
4669 for (size_t i = 1; i < len; i++)
4671 lookup.assign (str, i);
4672 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4673 EXPECT ("function"));
4677 /* While "w" is a prefix of both components, the match function
4678 should still only be called once. */
4680 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4684 /* Same, with a "complicated" symbol. */
4686 static const char str[] = Z_SYM_NAME;
4687 size_t len = strlen (str);
4690 for (size_t i = 1; i < len; i++)
4692 lookup.assign (str, i);
4693 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4694 EXPECT (Z_SYM_NAME));
4698 /* In FULL mode, an incomplete symbol doesn't match. */
4700 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4704 /* A complete symbol with parameters matches any overload, since the
4705 index has no overload info. */
4707 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4708 EXPECT ("std::zfunction", "std::zfunction2"));
4711 /* Check that whitespace is ignored appropriately. A symbol with a
4712 template argument list. */
4714 static const char expected[] = "ns::foo<int>";
4715 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4719 /* Check that whitespace is ignored appropriately. A symbol with a
4720 template argument list that includes a pointer. */
4722 static const char expected[] = "ns::foo<char*>";
4723 /* Try both completion and non-completion modes. */
4724 static const bool completion_mode[2] = {false, true};
4725 for (size_t i = 0; i < 2; i++)
4727 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4728 completion_mode[i], EXPECT (expected));
4730 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4731 completion_mode[i], EXPECT (expected));
4736 /* Check method qualifiers are ignored. */
4737 static const char expected[] = "ns::foo<char*>";
4738 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4739 symbol_name_match_type::FULL, true, EXPECT (expected));
4740 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4741 symbol_name_match_type::FULL, true, EXPECT (expected));
4744 /* Test lookup names that don't match anything. */
4746 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4750 SELF_CHECK (!any_mismatch);
4756 }} // namespace selftests::dw2_expand_symtabs_matching
4758 #endif /* GDB_SELF_TEST */
4760 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4761 matched, to expand corresponding CUs that were marked. IDX is the
4762 index of the symbol name that matched. */
4765 dw2_expand_marked_cus
4766 (mapped_index &index, offset_type idx,
4767 struct objfile *objfile,
4768 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4769 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4773 offset_type *vec, vec_len, vec_idx;
4774 bool global_seen = false;
4776 vec = (offset_type *) (index.constant_pool
4777 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4778 vec_len = MAYBE_SWAP (vec[0]);
4779 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4781 struct dwarf2_per_cu_data *per_cu;
4782 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4783 /* This value is only valid for index versions >= 7. */
4784 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4785 gdb_index_symbol_kind symbol_kind =
4786 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4787 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4788 /* Only check the symbol attributes if they're present.
4789 Indices prior to version 7 don't record them,
4790 and indices >= 7 may elide them for certain symbols
4791 (gold does this). */
4794 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4796 /* Work around gold/15646. */
4799 if (!is_static && global_seen)
4805 /* Only check the symbol's kind if it has one. */
4810 case VARIABLES_DOMAIN:
4811 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4814 case FUNCTIONS_DOMAIN:
4815 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4819 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4827 /* Don't crash on bad data. */
4828 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4829 + dwarf2_per_objfile->n_type_units))
4831 complaint (&symfile_complaints,
4832 _(".gdb_index entry has bad CU index"
4833 " [in module %s]"), objfile_name (objfile));
4837 per_cu = dw2_get_cutu (cu_index);
4838 if (file_matcher == NULL || per_cu->v.quick->mark)
4840 int symtab_was_null =
4841 (per_cu->v.quick->compunit_symtab == NULL);
4843 dw2_instantiate_symtab (per_cu);
4845 if (expansion_notify != NULL
4847 && per_cu->v.quick->compunit_symtab != NULL)
4848 expansion_notify (per_cu->v.quick->compunit_symtab);
4854 dw2_expand_symtabs_matching
4855 (struct objfile *objfile,
4856 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4857 const lookup_name_info &lookup_name,
4858 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4859 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4860 enum search_domain kind)
4865 dw2_setup (objfile);
4867 /* index_table is NULL if OBJF_READNOW. */
4868 if (!dwarf2_per_objfile->index_table)
4871 if (file_matcher != NULL)
4873 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4875 NULL, xcalloc, xfree));
4876 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4878 NULL, xcalloc, xfree));
4880 /* The rule is CUs specify all the files, including those used by
4881 any TU, so there's no need to scan TUs here. */
4883 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4886 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4887 struct quick_file_names *file_data;
4892 per_cu->v.quick->mark = 0;
4894 /* We only need to look at symtabs not already expanded. */
4895 if (per_cu->v.quick->compunit_symtab)
4898 file_data = dw2_get_file_names (per_cu);
4899 if (file_data == NULL)
4902 if (htab_find (visited_not_found.get (), file_data) != NULL)
4904 else if (htab_find (visited_found.get (), file_data) != NULL)
4906 per_cu->v.quick->mark = 1;
4910 for (j = 0; j < file_data->num_file_names; ++j)
4912 const char *this_real_name;
4914 if (file_matcher (file_data->file_names[j], false))
4916 per_cu->v.quick->mark = 1;
4920 /* Before we invoke realpath, which can get expensive when many
4921 files are involved, do a quick comparison of the basenames. */
4922 if (!basenames_may_differ
4923 && !file_matcher (lbasename (file_data->file_names[j]),
4927 this_real_name = dw2_get_real_path (objfile, file_data, j);
4928 if (file_matcher (this_real_name, false))
4930 per_cu->v.quick->mark = 1;
4935 slot = htab_find_slot (per_cu->v.quick->mark
4936 ? visited_found.get ()
4937 : visited_not_found.get (),
4943 mapped_index &index = *dwarf2_per_objfile->index_table;
4945 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4947 kind, [&] (offset_type idx)
4949 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
4950 expansion_notify, kind);
4954 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4957 static struct compunit_symtab *
4958 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4963 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4964 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4967 if (cust->includes == NULL)
4970 for (i = 0; cust->includes[i]; ++i)
4972 struct compunit_symtab *s = cust->includes[i];
4974 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4982 static struct compunit_symtab *
4983 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4984 struct bound_minimal_symbol msymbol,
4986 struct obj_section *section,
4989 struct dwarf2_per_cu_data *data;
4990 struct compunit_symtab *result;
4992 dw2_setup (objfile);
4994 if (!objfile->psymtabs_addrmap)
4997 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5002 if (warn_if_readin && data->v.quick->compunit_symtab)
5003 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5004 paddress (get_objfile_arch (objfile), pc));
5007 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5009 gdb_assert (result != NULL);
5014 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5015 void *data, int need_fullname)
5017 dw2_setup (objfile);
5019 if (!dwarf2_per_objfile->filenames_cache)
5021 dwarf2_per_objfile->filenames_cache.emplace ();
5023 htab_up visited (htab_create_alloc (10,
5024 htab_hash_pointer, htab_eq_pointer,
5025 NULL, xcalloc, xfree));
5027 /* The rule is CUs specify all the files, including those used
5028 by any TU, so there's no need to scan TUs here. We can
5029 ignore file names coming from already-expanded CUs. */
5031 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5033 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5035 if (per_cu->v.quick->compunit_symtab)
5037 void **slot = htab_find_slot (visited.get (),
5038 per_cu->v.quick->file_names,
5041 *slot = per_cu->v.quick->file_names;
5045 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5048 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5049 struct quick_file_names *file_data;
5052 /* We only need to look at symtabs not already expanded. */
5053 if (per_cu->v.quick->compunit_symtab)
5056 file_data = dw2_get_file_names (per_cu);
5057 if (file_data == NULL)
5060 slot = htab_find_slot (visited.get (), file_data, INSERT);
5063 /* Already visited. */
5068 for (int j = 0; j < file_data->num_file_names; ++j)
5070 const char *filename = file_data->file_names[j];
5071 dwarf2_per_objfile->filenames_cache->seen (filename);
5076 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5078 gdb::unique_xmalloc_ptr<char> this_real_name;
5081 this_real_name = gdb_realpath (filename);
5082 (*fun) (filename, this_real_name.get (), data);
5087 dw2_has_symbols (struct objfile *objfile)
5092 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5095 dw2_find_last_source_symtab,
5096 dw2_forget_cached_source_info,
5097 dw2_map_symtabs_matching_filename,
5102 dw2_expand_symtabs_for_function,
5103 dw2_expand_all_symtabs,
5104 dw2_expand_symtabs_with_fullname,
5105 dw2_map_matching_symbols,
5106 dw2_expand_symtabs_matching,
5107 dw2_find_pc_sect_compunit_symtab,
5109 dw2_map_symbol_filenames
5112 /* Initialize for reading DWARF for this objfile. Return 0 if this
5113 file will use psymtabs, or 1 if using the GNU index. */
5116 dwarf2_initialize_objfile (struct objfile *objfile)
5118 /* If we're about to read full symbols, don't bother with the
5119 indices. In this case we also don't care if some other debug
5120 format is making psymtabs, because they are all about to be
5122 if ((objfile->flags & OBJF_READNOW))
5126 dwarf2_per_objfile->using_index = 1;
5127 create_all_comp_units (objfile);
5128 create_all_type_units (objfile);
5129 dwarf2_per_objfile->quick_file_names_table =
5130 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5132 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
5133 + dwarf2_per_objfile->n_type_units); ++i)
5135 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5137 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5138 struct dwarf2_per_cu_quick_data);
5141 /* Return 1 so that gdb sees the "quick" functions. However,
5142 these functions will be no-ops because we will have expanded
5147 if (dwarf2_read_index (objfile))
5155 /* Build a partial symbol table. */
5158 dwarf2_build_psymtabs (struct objfile *objfile)
5161 if (objfile->global_psymbols.capacity () == 0
5162 && objfile->static_psymbols.capacity () == 0)
5163 init_psymbol_list (objfile, 1024);
5167 /* This isn't really ideal: all the data we allocate on the
5168 objfile's obstack is still uselessly kept around. However,
5169 freeing it seems unsafe. */
5170 psymtab_discarder psymtabs (objfile);
5171 dwarf2_build_psymtabs_hard (objfile);
5174 CATCH (except, RETURN_MASK_ERROR)
5176 exception_print (gdb_stderr, except);
5181 /* Return the total length of the CU described by HEADER. */
5184 get_cu_length (const struct comp_unit_head *header)
5186 return header->initial_length_size + header->length;
5189 /* Return TRUE if SECT_OFF is within CU_HEADER. */
5192 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
5194 sect_offset bottom = cu_header->sect_off;
5195 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
5197 return sect_off >= bottom && sect_off < top;
5200 /* Find the base address of the compilation unit for range lists and
5201 location lists. It will normally be specified by DW_AT_low_pc.
5202 In DWARF-3 draft 4, the base address could be overridden by
5203 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5204 compilation units with discontinuous ranges. */
5207 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5209 struct attribute *attr;
5212 cu->base_address = 0;
5214 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5217 cu->base_address = attr_value_as_address (attr);
5222 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5225 cu->base_address = attr_value_as_address (attr);
5231 /* Read in the comp unit header information from the debug_info at info_ptr.
5232 Use rcuh_kind::COMPILE as the default type if not known by the caller.
5233 NOTE: This leaves members offset, first_die_offset to be filled in
5236 static const gdb_byte *
5237 read_comp_unit_head (struct comp_unit_head *cu_header,
5238 const gdb_byte *info_ptr,
5239 struct dwarf2_section_info *section,
5240 rcuh_kind section_kind)
5243 unsigned int bytes_read;
5244 const char *filename = get_section_file_name (section);
5245 bfd *abfd = get_section_bfd_owner (section);
5247 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5248 cu_header->initial_length_size = bytes_read;
5249 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
5250 info_ptr += bytes_read;
5251 cu_header->version = read_2_bytes (abfd, info_ptr);
5253 if (cu_header->version < 5)
5254 switch (section_kind)
5256 case rcuh_kind::COMPILE:
5257 cu_header->unit_type = DW_UT_compile;
5259 case rcuh_kind::TYPE:
5260 cu_header->unit_type = DW_UT_type;
5263 internal_error (__FILE__, __LINE__,
5264 _("read_comp_unit_head: invalid section_kind"));
5268 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5269 (read_1_byte (abfd, info_ptr));
5271 switch (cu_header->unit_type)
5274 if (section_kind != rcuh_kind::COMPILE)
5275 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5276 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5280 section_kind = rcuh_kind::TYPE;
5283 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5284 "(is %d, should be %d or %d) [in module %s]"),
5285 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5288 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5291 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5294 info_ptr += bytes_read;
5295 if (cu_header->version < 5)
5297 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5300 signed_addr = bfd_get_sign_extend_vma (abfd);
5301 if (signed_addr < 0)
5302 internal_error (__FILE__, __LINE__,
5303 _("read_comp_unit_head: dwarf from non elf file"));
5304 cu_header->signed_addr_p = signed_addr;
5306 if (section_kind == rcuh_kind::TYPE)
5308 LONGEST type_offset;
5310 cu_header->signature = read_8_bytes (abfd, info_ptr);
5313 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5314 info_ptr += bytes_read;
5315 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5316 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
5317 error (_("Dwarf Error: Too big type_offset in compilation unit "
5318 "header (is %s) [in module %s]"), plongest (type_offset),
5325 /* Helper function that returns the proper abbrev section for
5328 static struct dwarf2_section_info *
5329 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5331 struct dwarf2_section_info *abbrev;
5333 if (this_cu->is_dwz)
5334 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5336 abbrev = &dwarf2_per_objfile->abbrev;
5341 /* Subroutine of read_and_check_comp_unit_head and
5342 read_and_check_type_unit_head to simplify them.
5343 Perform various error checking on the header. */
5346 error_check_comp_unit_head (struct comp_unit_head *header,
5347 struct dwarf2_section_info *section,
5348 struct dwarf2_section_info *abbrev_section)
5350 const char *filename = get_section_file_name (section);
5352 if (header->version < 2 || header->version > 5)
5353 error (_("Dwarf Error: wrong version in compilation unit header "
5354 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
5357 if (to_underlying (header->abbrev_sect_off)
5358 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
5359 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5360 "(offset 0x%x + 6) [in module %s]"),
5361 to_underlying (header->abbrev_sect_off),
5362 to_underlying (header->sect_off),
5365 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
5366 avoid potential 32-bit overflow. */
5367 if (((ULONGEST) header->sect_off + get_cu_length (header))
5369 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5370 "(offset 0x%x + 0) [in module %s]"),
5371 header->length, to_underlying (header->sect_off),
5375 /* Read in a CU/TU header and perform some basic error checking.
5376 The contents of the header are stored in HEADER.
5377 The result is a pointer to the start of the first DIE. */
5379 static const gdb_byte *
5380 read_and_check_comp_unit_head (struct comp_unit_head *header,
5381 struct dwarf2_section_info *section,
5382 struct dwarf2_section_info *abbrev_section,
5383 const gdb_byte *info_ptr,
5384 rcuh_kind section_kind)
5386 const gdb_byte *beg_of_comp_unit = info_ptr;
5387 bfd *abfd = get_section_bfd_owner (section);
5389 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
5391 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
5393 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
5395 error_check_comp_unit_head (header, section, abbrev_section);
5400 /* Fetch the abbreviation table offset from a comp or type unit header. */
5403 read_abbrev_offset (struct dwarf2_section_info *section,
5404 sect_offset sect_off)
5406 bfd *abfd = get_section_bfd_owner (section);
5407 const gdb_byte *info_ptr;
5408 unsigned int initial_length_size, offset_size;
5411 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
5412 info_ptr = section->buffer + to_underlying (sect_off);
5413 read_initial_length (abfd, info_ptr, &initial_length_size);
5414 offset_size = initial_length_size == 4 ? 4 : 8;
5415 info_ptr += initial_length_size;
5417 version = read_2_bytes (abfd, info_ptr);
5421 /* Skip unit type and address size. */
5425 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
5428 /* Allocate a new partial symtab for file named NAME and mark this new
5429 partial symtab as being an include of PST. */
5432 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
5433 struct objfile *objfile)
5435 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5437 if (!IS_ABSOLUTE_PATH (subpst->filename))
5439 /* It shares objfile->objfile_obstack. */
5440 subpst->dirname = pst->dirname;
5443 subpst->textlow = 0;
5444 subpst->texthigh = 0;
5446 subpst->dependencies
5447 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
5448 subpst->dependencies[0] = pst;
5449 subpst->number_of_dependencies = 1;
5451 subpst->globals_offset = 0;
5452 subpst->n_global_syms = 0;
5453 subpst->statics_offset = 0;
5454 subpst->n_static_syms = 0;
5455 subpst->compunit_symtab = NULL;
5456 subpst->read_symtab = pst->read_symtab;
5459 /* No private part is necessary for include psymtabs. This property
5460 can be used to differentiate between such include psymtabs and
5461 the regular ones. */
5462 subpst->read_symtab_private = NULL;
5465 /* Read the Line Number Program data and extract the list of files
5466 included by the source file represented by PST. Build an include
5467 partial symtab for each of these included files. */
5470 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5471 struct die_info *die,
5472 struct partial_symtab *pst)
5475 struct attribute *attr;
5477 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5479 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5481 return; /* No linetable, so no includes. */
5483 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
5484 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
5488 hash_signatured_type (const void *item)
5490 const struct signatured_type *sig_type
5491 = (const struct signatured_type *) item;
5493 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5494 return sig_type->signature;
5498 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5500 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5501 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5503 return lhs->signature == rhs->signature;
5506 /* Allocate a hash table for signatured types. */
5509 allocate_signatured_type_table (struct objfile *objfile)
5511 return htab_create_alloc_ex (41,
5512 hash_signatured_type,
5515 &objfile->objfile_obstack,
5516 hashtab_obstack_allocate,
5517 dummy_obstack_deallocate);
5520 /* A helper function to add a signatured type CU to a table. */
5523 add_signatured_type_cu_to_table (void **slot, void *datum)
5525 struct signatured_type *sigt = (struct signatured_type *) *slot;
5526 struct signatured_type ***datap = (struct signatured_type ***) datum;
5534 /* A helper for create_debug_types_hash_table. Read types from SECTION
5535 and fill them into TYPES_HTAB. It will process only type units,
5536 therefore DW_UT_type. */
5539 create_debug_type_hash_table (struct dwo_file *dwo_file,
5540 dwarf2_section_info *section, htab_t &types_htab,
5541 rcuh_kind section_kind)
5543 struct objfile *objfile = dwarf2_per_objfile->objfile;
5544 struct dwarf2_section_info *abbrev_section;
5546 const gdb_byte *info_ptr, *end_ptr;
5548 abbrev_section = (dwo_file != NULL
5549 ? &dwo_file->sections.abbrev
5550 : &dwarf2_per_objfile->abbrev);
5552 if (dwarf_read_debug)
5553 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5554 get_section_name (section),
5555 get_section_file_name (abbrev_section));
5557 dwarf2_read_section (objfile, section);
5558 info_ptr = section->buffer;
5560 if (info_ptr == NULL)
5563 /* We can't set abfd until now because the section may be empty or
5564 not present, in which case the bfd is unknown. */
5565 abfd = get_section_bfd_owner (section);
5567 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5568 because we don't need to read any dies: the signature is in the
5571 end_ptr = info_ptr + section->size;
5572 while (info_ptr < end_ptr)
5574 struct signatured_type *sig_type;
5575 struct dwo_unit *dwo_tu;
5577 const gdb_byte *ptr = info_ptr;
5578 struct comp_unit_head header;
5579 unsigned int length;
5581 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5583 /* Initialize it due to a false compiler warning. */
5584 header.signature = -1;
5585 header.type_cu_offset_in_tu = (cu_offset) -1;
5587 /* We need to read the type's signature in order to build the hash
5588 table, but we don't need anything else just yet. */
5590 ptr = read_and_check_comp_unit_head (&header, section,
5591 abbrev_section, ptr, section_kind);
5593 length = get_cu_length (&header);
5595 /* Skip dummy type units. */
5596 if (ptr >= info_ptr + length
5597 || peek_abbrev_code (abfd, ptr) == 0
5598 || header.unit_type != DW_UT_type)
5604 if (types_htab == NULL)
5607 types_htab = allocate_dwo_unit_table (objfile);
5609 types_htab = allocate_signatured_type_table (objfile);
5615 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5617 dwo_tu->dwo_file = dwo_file;
5618 dwo_tu->signature = header.signature;
5619 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5620 dwo_tu->section = section;
5621 dwo_tu->sect_off = sect_off;
5622 dwo_tu->length = length;
5626 /* N.B.: type_offset is not usable if this type uses a DWO file.
5627 The real type_offset is in the DWO file. */
5629 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5630 struct signatured_type);
5631 sig_type->signature = header.signature;
5632 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
5633 sig_type->per_cu.objfile = objfile;
5634 sig_type->per_cu.is_debug_types = 1;
5635 sig_type->per_cu.section = section;
5636 sig_type->per_cu.sect_off = sect_off;
5637 sig_type->per_cu.length = length;
5640 slot = htab_find_slot (types_htab,
5641 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5643 gdb_assert (slot != NULL);
5646 sect_offset dup_sect_off;
5650 const struct dwo_unit *dup_tu
5651 = (const struct dwo_unit *) *slot;
5653 dup_sect_off = dup_tu->sect_off;
5657 const struct signatured_type *dup_tu
5658 = (const struct signatured_type *) *slot;
5660 dup_sect_off = dup_tu->per_cu.sect_off;
5663 complaint (&symfile_complaints,
5664 _("debug type entry at offset 0x%x is duplicate to"
5665 " the entry at offset 0x%x, signature %s"),
5666 to_underlying (sect_off), to_underlying (dup_sect_off),
5667 hex_string (header.signature));
5669 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
5671 if (dwarf_read_debug > 1)
5672 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
5673 to_underlying (sect_off),
5674 hex_string (header.signature));
5680 /* Create the hash table of all entries in the .debug_types
5681 (or .debug_types.dwo) section(s).
5682 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5683 otherwise it is NULL.
5685 The result is a pointer to the hash table or NULL if there are no types.
5687 Note: This function processes DWO files only, not DWP files. */
5690 create_debug_types_hash_table (struct dwo_file *dwo_file,
5691 VEC (dwarf2_section_info_def) *types,
5695 struct dwarf2_section_info *section;
5697 if (VEC_empty (dwarf2_section_info_def, types))
5701 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5703 create_debug_type_hash_table (dwo_file, section, types_htab,
5707 /* Create the hash table of all entries in the .debug_types section,
5708 and initialize all_type_units.
5709 The result is zero if there is an error (e.g. missing .debug_types section),
5710 otherwise non-zero. */
5713 create_all_type_units (struct objfile *objfile)
5715 htab_t types_htab = NULL;
5716 struct signatured_type **iter;
5718 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5719 rcuh_kind::COMPILE);
5720 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5721 if (types_htab == NULL)
5723 dwarf2_per_objfile->signatured_types = NULL;
5727 dwarf2_per_objfile->signatured_types = types_htab;
5729 dwarf2_per_objfile->n_type_units
5730 = dwarf2_per_objfile->n_allocated_type_units
5731 = htab_elements (types_htab);
5732 dwarf2_per_objfile->all_type_units =
5733 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5734 iter = &dwarf2_per_objfile->all_type_units[0];
5735 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5736 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5737 == dwarf2_per_objfile->n_type_units);
5742 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5743 If SLOT is non-NULL, it is the entry to use in the hash table.
5744 Otherwise we find one. */
5746 static struct signatured_type *
5747 add_type_unit (ULONGEST sig, void **slot)
5749 struct objfile *objfile = dwarf2_per_objfile->objfile;
5750 int n_type_units = dwarf2_per_objfile->n_type_units;
5751 struct signatured_type *sig_type;
5753 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5755 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5757 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5758 dwarf2_per_objfile->n_allocated_type_units = 1;
5759 dwarf2_per_objfile->n_allocated_type_units *= 2;
5760 dwarf2_per_objfile->all_type_units
5761 = XRESIZEVEC (struct signatured_type *,
5762 dwarf2_per_objfile->all_type_units,
5763 dwarf2_per_objfile->n_allocated_type_units);
5764 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5766 dwarf2_per_objfile->n_type_units = n_type_units;
5768 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5769 struct signatured_type);
5770 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5771 sig_type->signature = sig;
5772 sig_type->per_cu.is_debug_types = 1;
5773 if (dwarf2_per_objfile->using_index)
5775 sig_type->per_cu.v.quick =
5776 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5777 struct dwarf2_per_cu_quick_data);
5782 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5785 gdb_assert (*slot == NULL);
5787 /* The rest of sig_type must be filled in by the caller. */
5791 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5792 Fill in SIG_ENTRY with DWO_ENTRY. */
5795 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5796 struct signatured_type *sig_entry,
5797 struct dwo_unit *dwo_entry)
5799 /* Make sure we're not clobbering something we don't expect to. */
5800 gdb_assert (! sig_entry->per_cu.queued);
5801 gdb_assert (sig_entry->per_cu.cu == NULL);
5802 if (dwarf2_per_objfile->using_index)
5804 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5805 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5808 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5809 gdb_assert (sig_entry->signature == dwo_entry->signature);
5810 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5811 gdb_assert (sig_entry->type_unit_group == NULL);
5812 gdb_assert (sig_entry->dwo_unit == NULL);
5814 sig_entry->per_cu.section = dwo_entry->section;
5815 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5816 sig_entry->per_cu.length = dwo_entry->length;
5817 sig_entry->per_cu.reading_dwo_directly = 1;
5818 sig_entry->per_cu.objfile = objfile;
5819 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5820 sig_entry->dwo_unit = dwo_entry;
5823 /* Subroutine of lookup_signatured_type.
5824 If we haven't read the TU yet, create the signatured_type data structure
5825 for a TU to be read in directly from a DWO file, bypassing the stub.
5826 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5827 using .gdb_index, then when reading a CU we want to stay in the DWO file
5828 containing that CU. Otherwise we could end up reading several other DWO
5829 files (due to comdat folding) to process the transitive closure of all the
5830 mentioned TUs, and that can be slow. The current DWO file will have every
5831 type signature that it needs.
5832 We only do this for .gdb_index because in the psymtab case we already have
5833 to read all the DWOs to build the type unit groups. */
5835 static struct signatured_type *
5836 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5838 struct objfile *objfile = dwarf2_per_objfile->objfile;
5839 struct dwo_file *dwo_file;
5840 struct dwo_unit find_dwo_entry, *dwo_entry;
5841 struct signatured_type find_sig_entry, *sig_entry;
5844 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5846 /* If TU skeletons have been removed then we may not have read in any
5848 if (dwarf2_per_objfile->signatured_types == NULL)
5850 dwarf2_per_objfile->signatured_types
5851 = allocate_signatured_type_table (objfile);
5854 /* We only ever need to read in one copy of a signatured type.
5855 Use the global signatured_types array to do our own comdat-folding
5856 of types. If this is the first time we're reading this TU, and
5857 the TU has an entry in .gdb_index, replace the recorded data from
5858 .gdb_index with this TU. */
5860 find_sig_entry.signature = sig;
5861 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5862 &find_sig_entry, INSERT);
5863 sig_entry = (struct signatured_type *) *slot;
5865 /* We can get here with the TU already read, *or* in the process of being
5866 read. Don't reassign the global entry to point to this DWO if that's
5867 the case. Also note that if the TU is already being read, it may not
5868 have come from a DWO, the program may be a mix of Fission-compiled
5869 code and non-Fission-compiled code. */
5871 /* Have we already tried to read this TU?
5872 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5873 needn't exist in the global table yet). */
5874 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5877 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5878 dwo_unit of the TU itself. */
5879 dwo_file = cu->dwo_unit->dwo_file;
5881 /* Ok, this is the first time we're reading this TU. */
5882 if (dwo_file->tus == NULL)
5884 find_dwo_entry.signature = sig;
5885 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5886 if (dwo_entry == NULL)
5889 /* If the global table doesn't have an entry for this TU, add one. */
5890 if (sig_entry == NULL)
5891 sig_entry = add_type_unit (sig, slot);
5893 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5894 sig_entry->per_cu.tu_read = 1;
5898 /* Subroutine of lookup_signatured_type.
5899 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5900 then try the DWP file. If the TU stub (skeleton) has been removed then
5901 it won't be in .gdb_index. */
5903 static struct signatured_type *
5904 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5906 struct objfile *objfile = dwarf2_per_objfile->objfile;
5907 struct dwp_file *dwp_file = get_dwp_file ();
5908 struct dwo_unit *dwo_entry;
5909 struct signatured_type find_sig_entry, *sig_entry;
5912 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5913 gdb_assert (dwp_file != NULL);
5915 /* If TU skeletons have been removed then we may not have read in any
5917 if (dwarf2_per_objfile->signatured_types == NULL)
5919 dwarf2_per_objfile->signatured_types
5920 = allocate_signatured_type_table (objfile);
5923 find_sig_entry.signature = sig;
5924 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5925 &find_sig_entry, INSERT);
5926 sig_entry = (struct signatured_type *) *slot;
5928 /* Have we already tried to read this TU?
5929 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5930 needn't exist in the global table yet). */
5931 if (sig_entry != NULL)
5934 if (dwp_file->tus == NULL)
5936 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5937 sig, 1 /* is_debug_types */);
5938 if (dwo_entry == NULL)
5941 sig_entry = add_type_unit (sig, slot);
5942 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5947 /* Lookup a signature based type for DW_FORM_ref_sig8.
5948 Returns NULL if signature SIG is not present in the table.
5949 It is up to the caller to complain about this. */
5951 static struct signatured_type *
5952 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5955 && dwarf2_per_objfile->using_index)
5957 /* We're in a DWO/DWP file, and we're using .gdb_index.
5958 These cases require special processing. */
5959 if (get_dwp_file () == NULL)
5960 return lookup_dwo_signatured_type (cu, sig);
5962 return lookup_dwp_signatured_type (cu, sig);
5966 struct signatured_type find_entry, *entry;
5968 if (dwarf2_per_objfile->signatured_types == NULL)
5970 find_entry.signature = sig;
5971 entry = ((struct signatured_type *)
5972 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5977 /* Low level DIE reading support. */
5979 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5982 init_cu_die_reader (struct die_reader_specs *reader,
5983 struct dwarf2_cu *cu,
5984 struct dwarf2_section_info *section,
5985 struct dwo_file *dwo_file)
5987 gdb_assert (section->readin && section->buffer != NULL);
5988 reader->abfd = get_section_bfd_owner (section);
5990 reader->dwo_file = dwo_file;
5991 reader->die_section = section;
5992 reader->buffer = section->buffer;
5993 reader->buffer_end = section->buffer + section->size;
5994 reader->comp_dir = NULL;
5997 /* Subroutine of init_cutu_and_read_dies to simplify it.
5998 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5999 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6002 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6003 from it to the DIE in the DWO. If NULL we are skipping the stub.
6004 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6005 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6006 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6007 STUB_COMP_DIR may be non-NULL.
6008 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6009 are filled in with the info of the DIE from the DWO file.
6010 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6011 provided an abbrev table to use.
6012 The result is non-zero if a valid (non-dummy) DIE was found. */
6015 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6016 struct dwo_unit *dwo_unit,
6017 int abbrev_table_provided,
6018 struct die_info *stub_comp_unit_die,
6019 const char *stub_comp_dir,
6020 struct die_reader_specs *result_reader,
6021 const gdb_byte **result_info_ptr,
6022 struct die_info **result_comp_unit_die,
6023 int *result_has_children)
6025 struct objfile *objfile = dwarf2_per_objfile->objfile;
6026 struct dwarf2_cu *cu = this_cu->cu;
6027 struct dwarf2_section_info *section;
6029 const gdb_byte *begin_info_ptr, *info_ptr;
6030 ULONGEST signature; /* Or dwo_id. */
6031 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6032 int i,num_extra_attrs;
6033 struct dwarf2_section_info *dwo_abbrev_section;
6034 struct attribute *attr;
6035 struct die_info *comp_unit_die;
6037 /* At most one of these may be provided. */
6038 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6040 /* These attributes aren't processed until later:
6041 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6042 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6043 referenced later. However, these attributes are found in the stub
6044 which we won't have later. In order to not impose this complication
6045 on the rest of the code, we read them here and copy them to the
6054 if (stub_comp_unit_die != NULL)
6056 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6058 if (! this_cu->is_debug_types)
6059 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6060 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6061 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6062 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6063 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6065 /* There should be a DW_AT_addr_base attribute here (if needed).
6066 We need the value before we can process DW_FORM_GNU_addr_index. */
6068 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6070 cu->addr_base = DW_UNSND (attr);
6072 /* There should be a DW_AT_ranges_base attribute here (if needed).
6073 We need the value before we can process DW_AT_ranges. */
6074 cu->ranges_base = 0;
6075 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6077 cu->ranges_base = DW_UNSND (attr);
6079 else if (stub_comp_dir != NULL)
6081 /* Reconstruct the comp_dir attribute to simplify the code below. */
6082 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6083 comp_dir->name = DW_AT_comp_dir;
6084 comp_dir->form = DW_FORM_string;
6085 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6086 DW_STRING (comp_dir) = stub_comp_dir;
6089 /* Set up for reading the DWO CU/TU. */
6090 cu->dwo_unit = dwo_unit;
6091 section = dwo_unit->section;
6092 dwarf2_read_section (objfile, section);
6093 abfd = get_section_bfd_owner (section);
6094 begin_info_ptr = info_ptr = (section->buffer
6095 + to_underlying (dwo_unit->sect_off));
6096 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6097 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6099 if (this_cu->is_debug_types)
6101 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6103 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6105 info_ptr, rcuh_kind::TYPE);
6106 /* This is not an assert because it can be caused by bad debug info. */
6107 if (sig_type->signature != cu->header.signature)
6109 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6110 " TU at offset 0x%x [in module %s]"),
6111 hex_string (sig_type->signature),
6112 hex_string (cu->header.signature),
6113 to_underlying (dwo_unit->sect_off),
6114 bfd_get_filename (abfd));
6116 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6117 /* For DWOs coming from DWP files, we don't know the CU length
6118 nor the type's offset in the TU until now. */
6119 dwo_unit->length = get_cu_length (&cu->header);
6120 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6122 /* Establish the type offset that can be used to lookup the type.
6123 For DWO files, we don't know it until now. */
6124 sig_type->type_offset_in_section
6125 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6129 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6131 info_ptr, rcuh_kind::COMPILE);
6132 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6133 /* For DWOs coming from DWP files, we don't know the CU length
6135 dwo_unit->length = get_cu_length (&cu->header);
6138 /* Replace the CU's original abbrev table with the DWO's.
6139 Reminder: We can't read the abbrev table until we've read the header. */
6140 if (abbrev_table_provided)
6142 /* Don't free the provided abbrev table, the caller of
6143 init_cutu_and_read_dies owns it. */
6144 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6145 /* Ensure the DWO abbrev table gets freed. */
6146 make_cleanup (dwarf2_free_abbrev_table, cu);
6150 dwarf2_free_abbrev_table (cu);
6151 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6152 /* Leave any existing abbrev table cleanup as is. */
6155 /* Read in the die, but leave space to copy over the attributes
6156 from the stub. This has the benefit of simplifying the rest of
6157 the code - all the work to maintain the illusion of a single
6158 DW_TAG_{compile,type}_unit DIE is done here. */
6159 num_extra_attrs = ((stmt_list != NULL)
6163 + (comp_dir != NULL));
6164 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6165 result_has_children, num_extra_attrs);
6167 /* Copy over the attributes from the stub to the DIE we just read in. */
6168 comp_unit_die = *result_comp_unit_die;
6169 i = comp_unit_die->num_attrs;
6170 if (stmt_list != NULL)
6171 comp_unit_die->attrs[i++] = *stmt_list;
6173 comp_unit_die->attrs[i++] = *low_pc;
6174 if (high_pc != NULL)
6175 comp_unit_die->attrs[i++] = *high_pc;
6177 comp_unit_die->attrs[i++] = *ranges;
6178 if (comp_dir != NULL)
6179 comp_unit_die->attrs[i++] = *comp_dir;
6180 comp_unit_die->num_attrs += num_extra_attrs;
6182 if (dwarf_die_debug)
6184 fprintf_unfiltered (gdb_stdlog,
6185 "Read die from %s@0x%x of %s:\n",
6186 get_section_name (section),
6187 (unsigned) (begin_info_ptr - section->buffer),
6188 bfd_get_filename (abfd));
6189 dump_die (comp_unit_die, dwarf_die_debug);
6192 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6193 TUs by skipping the stub and going directly to the entry in the DWO file.
6194 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6195 to get it via circuitous means. Blech. */
6196 if (comp_dir != NULL)
6197 result_reader->comp_dir = DW_STRING (comp_dir);
6199 /* Skip dummy compilation units. */
6200 if (info_ptr >= begin_info_ptr + dwo_unit->length
6201 || peek_abbrev_code (abfd, info_ptr) == 0)
6204 *result_info_ptr = info_ptr;
6208 /* Subroutine of init_cutu_and_read_dies to simplify it.
6209 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6210 Returns NULL if the specified DWO unit cannot be found. */
6212 static struct dwo_unit *
6213 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6214 struct die_info *comp_unit_die)
6216 struct dwarf2_cu *cu = this_cu->cu;
6217 struct attribute *attr;
6219 struct dwo_unit *dwo_unit;
6220 const char *comp_dir, *dwo_name;
6222 gdb_assert (cu != NULL);
6224 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6225 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6226 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6228 if (this_cu->is_debug_types)
6230 struct signatured_type *sig_type;
6232 /* Since this_cu is the first member of struct signatured_type,
6233 we can go from a pointer to one to a pointer to the other. */
6234 sig_type = (struct signatured_type *) this_cu;
6235 signature = sig_type->signature;
6236 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6240 struct attribute *attr;
6242 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6244 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6246 dwo_name, objfile_name (this_cu->objfile));
6247 signature = DW_UNSND (attr);
6248 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6255 /* Subroutine of init_cutu_and_read_dies to simplify it.
6256 See it for a description of the parameters.
6257 Read a TU directly from a DWO file, bypassing the stub.
6259 Note: This function could be a little bit simpler if we shared cleanups
6260 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6261 to do, so we keep this function self-contained. Or we could move this
6262 into our caller, but it's complex enough already. */
6265 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6266 int use_existing_cu, int keep,
6267 die_reader_func_ftype *die_reader_func,
6270 struct dwarf2_cu *cu;
6271 struct signatured_type *sig_type;
6272 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6273 struct die_reader_specs reader;
6274 const gdb_byte *info_ptr;
6275 struct die_info *comp_unit_die;
6278 /* Verify we can do the following downcast, and that we have the
6280 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6281 sig_type = (struct signatured_type *) this_cu;
6282 gdb_assert (sig_type->dwo_unit != NULL);
6284 cleanups = make_cleanup (null_cleanup, NULL);
6286 if (use_existing_cu && this_cu->cu != NULL)
6288 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6290 /* There's no need to do the rereading_dwo_cu handling that
6291 init_cutu_and_read_dies does since we don't read the stub. */
6295 /* If !use_existing_cu, this_cu->cu must be NULL. */
6296 gdb_assert (this_cu->cu == NULL);
6297 cu = XNEW (struct dwarf2_cu);
6298 init_one_comp_unit (cu, this_cu);
6299 /* If an error occurs while loading, release our storage. */
6300 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6303 /* A future optimization, if needed, would be to use an existing
6304 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6305 could share abbrev tables. */
6307 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6308 0 /* abbrev_table_provided */,
6309 NULL /* stub_comp_unit_die */,
6310 sig_type->dwo_unit->dwo_file->comp_dir,
6312 &comp_unit_die, &has_children) == 0)
6315 do_cleanups (cleanups);
6319 /* All the "real" work is done here. */
6320 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6322 /* This duplicates the code in init_cutu_and_read_dies,
6323 but the alternative is making the latter more complex.
6324 This function is only for the special case of using DWO files directly:
6325 no point in overly complicating the general case just to handle this. */
6326 if (free_cu_cleanup != NULL)
6330 /* We've successfully allocated this compilation unit. Let our
6331 caller clean it up when finished with it. */
6332 discard_cleanups (free_cu_cleanup);
6334 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6335 So we have to manually free the abbrev table. */
6336 dwarf2_free_abbrev_table (cu);
6338 /* Link this CU into read_in_chain. */
6339 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6340 dwarf2_per_objfile->read_in_chain = this_cu;
6343 do_cleanups (free_cu_cleanup);
6346 do_cleanups (cleanups);
6349 /* Initialize a CU (or TU) and read its DIEs.
6350 If the CU defers to a DWO file, read the DWO file as well.
6352 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6353 Otherwise the table specified in the comp unit header is read in and used.
6354 This is an optimization for when we already have the abbrev table.
6356 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6357 Otherwise, a new CU is allocated with xmalloc.
6359 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6360 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6362 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6363 linker) then DIE_READER_FUNC will not get called. */
6366 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
6367 struct abbrev_table *abbrev_table,
6368 int use_existing_cu, int keep,
6369 die_reader_func_ftype *die_reader_func,
6372 struct objfile *objfile = dwarf2_per_objfile->objfile;
6373 struct dwarf2_section_info *section = this_cu->section;
6374 bfd *abfd = get_section_bfd_owner (section);
6375 struct dwarf2_cu *cu;
6376 const gdb_byte *begin_info_ptr, *info_ptr;
6377 struct die_reader_specs reader;
6378 struct die_info *comp_unit_die;
6380 struct attribute *attr;
6381 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6382 struct signatured_type *sig_type = NULL;
6383 struct dwarf2_section_info *abbrev_section;
6384 /* Non-zero if CU currently points to a DWO file and we need to
6385 reread it. When this happens we need to reread the skeleton die
6386 before we can reread the DWO file (this only applies to CUs, not TUs). */
6387 int rereading_dwo_cu = 0;
6389 if (dwarf_die_debug)
6390 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6391 this_cu->is_debug_types ? "type" : "comp",
6392 to_underlying (this_cu->sect_off));
6394 if (use_existing_cu)
6397 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6398 file (instead of going through the stub), short-circuit all of this. */
6399 if (this_cu->reading_dwo_directly)
6401 /* Narrow down the scope of possibilities to have to understand. */
6402 gdb_assert (this_cu->is_debug_types);
6403 gdb_assert (abbrev_table == NULL);
6404 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6405 die_reader_func, data);
6409 cleanups = make_cleanup (null_cleanup, NULL);
6411 /* This is cheap if the section is already read in. */
6412 dwarf2_read_section (objfile, section);
6414 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6416 abbrev_section = get_abbrev_section_for_cu (this_cu);
6418 if (use_existing_cu && this_cu->cu != NULL)
6421 /* If this CU is from a DWO file we need to start over, we need to
6422 refetch the attributes from the skeleton CU.
6423 This could be optimized by retrieving those attributes from when we
6424 were here the first time: the previous comp_unit_die was stored in
6425 comp_unit_obstack. But there's no data yet that we need this
6427 if (cu->dwo_unit != NULL)
6428 rereading_dwo_cu = 1;
6432 /* If !use_existing_cu, this_cu->cu must be NULL. */
6433 gdb_assert (this_cu->cu == NULL);
6434 cu = XNEW (struct dwarf2_cu);
6435 init_one_comp_unit (cu, this_cu);
6436 /* If an error occurs while loading, release our storage. */
6437 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6440 /* Get the header. */
6441 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6443 /* We already have the header, there's no need to read it in again. */
6444 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6448 if (this_cu->is_debug_types)
6450 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6451 abbrev_section, info_ptr,
6454 /* Since per_cu is the first member of struct signatured_type,
6455 we can go from a pointer to one to a pointer to the other. */
6456 sig_type = (struct signatured_type *) this_cu;
6457 gdb_assert (sig_type->signature == cu->header.signature);
6458 gdb_assert (sig_type->type_offset_in_tu
6459 == cu->header.type_cu_offset_in_tu);
6460 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6462 /* LENGTH has not been set yet for type units if we're
6463 using .gdb_index. */
6464 this_cu->length = get_cu_length (&cu->header);
6466 /* Establish the type offset that can be used to lookup the type. */
6467 sig_type->type_offset_in_section =
6468 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6470 this_cu->dwarf_version = cu->header.version;
6474 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6477 rcuh_kind::COMPILE);
6479 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6480 gdb_assert (this_cu->length == get_cu_length (&cu->header));
6481 this_cu->dwarf_version = cu->header.version;
6485 /* Skip dummy compilation units. */
6486 if (info_ptr >= begin_info_ptr + this_cu->length
6487 || peek_abbrev_code (abfd, info_ptr) == 0)
6489 do_cleanups (cleanups);
6493 /* If we don't have them yet, read the abbrevs for this compilation unit.
6494 And if we need to read them now, make sure they're freed when we're
6495 done. Note that it's important that if the CU had an abbrev table
6496 on entry we don't free it when we're done: Somewhere up the call stack
6497 it may be in use. */
6498 if (abbrev_table != NULL)
6500 gdb_assert (cu->abbrev_table == NULL);
6501 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6502 cu->abbrev_table = abbrev_table;
6504 else if (cu->abbrev_table == NULL)
6506 dwarf2_read_abbrevs (cu, abbrev_section);
6507 make_cleanup (dwarf2_free_abbrev_table, cu);
6509 else if (rereading_dwo_cu)
6511 dwarf2_free_abbrev_table (cu);
6512 dwarf2_read_abbrevs (cu, abbrev_section);
6515 /* Read the top level CU/TU die. */
6516 init_cu_die_reader (&reader, cu, section, NULL);
6517 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6519 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6521 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6522 DWO CU, that this test will fail (the attribute will not be present). */
6523 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6526 struct dwo_unit *dwo_unit;
6527 struct die_info *dwo_comp_unit_die;
6531 complaint (&symfile_complaints,
6532 _("compilation unit with DW_AT_GNU_dwo_name"
6533 " has children (offset 0x%x) [in module %s]"),
6534 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6536 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6537 if (dwo_unit != NULL)
6539 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6540 abbrev_table != NULL,
6541 comp_unit_die, NULL,
6543 &dwo_comp_unit_die, &has_children) == 0)
6546 do_cleanups (cleanups);
6549 comp_unit_die = dwo_comp_unit_die;
6553 /* Yikes, we couldn't find the rest of the DIE, we only have
6554 the stub. A complaint has already been logged. There's
6555 not much more we can do except pass on the stub DIE to
6556 die_reader_func. We don't want to throw an error on bad
6561 /* All of the above is setup for this call. Yikes. */
6562 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6564 /* Done, clean up. */
6565 if (free_cu_cleanup != NULL)
6569 /* We've successfully allocated this compilation unit. Let our
6570 caller clean it up when finished with it. */
6571 discard_cleanups (free_cu_cleanup);
6573 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6574 So we have to manually free the abbrev table. */
6575 dwarf2_free_abbrev_table (cu);
6577 /* Link this CU into read_in_chain. */
6578 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6579 dwarf2_per_objfile->read_in_chain = this_cu;
6582 do_cleanups (free_cu_cleanup);
6585 do_cleanups (cleanups);
6588 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6589 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6590 to have already done the lookup to find the DWO file).
6592 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6593 THIS_CU->is_debug_types, but nothing else.
6595 We fill in THIS_CU->length.
6597 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6598 linker) then DIE_READER_FUNC will not get called.
6600 THIS_CU->cu is always freed when done.
6601 This is done in order to not leave THIS_CU->cu in a state where we have
6602 to care whether it refers to the "main" CU or the DWO CU. */
6605 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
6606 struct dwo_file *dwo_file,
6607 die_reader_func_ftype *die_reader_func,
6610 struct objfile *objfile = dwarf2_per_objfile->objfile;
6611 struct dwarf2_section_info *section = this_cu->section;
6612 bfd *abfd = get_section_bfd_owner (section);
6613 struct dwarf2_section_info *abbrev_section;
6614 struct dwarf2_cu cu;
6615 const gdb_byte *begin_info_ptr, *info_ptr;
6616 struct die_reader_specs reader;
6617 struct cleanup *cleanups;
6618 struct die_info *comp_unit_die;
6621 if (dwarf_die_debug)
6622 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6623 this_cu->is_debug_types ? "type" : "comp",
6624 to_underlying (this_cu->sect_off));
6626 gdb_assert (this_cu->cu == NULL);
6628 abbrev_section = (dwo_file != NULL
6629 ? &dwo_file->sections.abbrev
6630 : get_abbrev_section_for_cu (this_cu));
6632 /* This is cheap if the section is already read in. */
6633 dwarf2_read_section (objfile, section);
6635 init_one_comp_unit (&cu, this_cu);
6637 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6639 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6640 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6641 abbrev_section, info_ptr,
6642 (this_cu->is_debug_types
6644 : rcuh_kind::COMPILE));
6646 this_cu->length = get_cu_length (&cu.header);
6648 /* Skip dummy compilation units. */
6649 if (info_ptr >= begin_info_ptr + this_cu->length
6650 || peek_abbrev_code (abfd, info_ptr) == 0)
6652 do_cleanups (cleanups);
6656 dwarf2_read_abbrevs (&cu, abbrev_section);
6657 make_cleanup (dwarf2_free_abbrev_table, &cu);
6659 init_cu_die_reader (&reader, &cu, section, dwo_file);
6660 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6662 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6664 do_cleanups (cleanups);
6667 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6668 does not lookup the specified DWO file.
6669 This cannot be used to read DWO files.
6671 THIS_CU->cu is always freed when done.
6672 This is done in order to not leave THIS_CU->cu in a state where we have
6673 to care whether it refers to the "main" CU or the DWO CU.
6674 We can revisit this if the data shows there's a performance issue. */
6677 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6678 die_reader_func_ftype *die_reader_func,
6681 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
6684 /* Type Unit Groups.
6686 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6687 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6688 so that all types coming from the same compilation (.o file) are grouped
6689 together. A future step could be to put the types in the same symtab as
6690 the CU the types ultimately came from. */
6693 hash_type_unit_group (const void *item)
6695 const struct type_unit_group *tu_group
6696 = (const struct type_unit_group *) item;
6698 return hash_stmt_list_entry (&tu_group->hash);
6702 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6704 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6705 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6707 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6710 /* Allocate a hash table for type unit groups. */
6713 allocate_type_unit_groups_table (void)
6715 return htab_create_alloc_ex (3,
6716 hash_type_unit_group,
6719 &dwarf2_per_objfile->objfile->objfile_obstack,
6720 hashtab_obstack_allocate,
6721 dummy_obstack_deallocate);
6724 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6725 partial symtabs. We combine several TUs per psymtab to not let the size
6726 of any one psymtab grow too big. */
6727 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6728 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6730 /* Helper routine for get_type_unit_group.
6731 Create the type_unit_group object used to hold one or more TUs. */
6733 static struct type_unit_group *
6734 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6736 struct objfile *objfile = dwarf2_per_objfile->objfile;
6737 struct dwarf2_per_cu_data *per_cu;
6738 struct type_unit_group *tu_group;
6740 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6741 struct type_unit_group);
6742 per_cu = &tu_group->per_cu;
6743 per_cu->objfile = objfile;
6745 if (dwarf2_per_objfile->using_index)
6747 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6748 struct dwarf2_per_cu_quick_data);
6752 unsigned int line_offset = to_underlying (line_offset_struct);
6753 struct partial_symtab *pst;
6756 /* Give the symtab a useful name for debug purposes. */
6757 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6758 name = xstrprintf ("<type_units_%d>",
6759 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6761 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6763 pst = create_partial_symtab (per_cu, name);
6769 tu_group->hash.dwo_unit = cu->dwo_unit;
6770 tu_group->hash.line_sect_off = line_offset_struct;
6775 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6776 STMT_LIST is a DW_AT_stmt_list attribute. */
6778 static struct type_unit_group *
6779 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6781 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6782 struct type_unit_group *tu_group;
6784 unsigned int line_offset;
6785 struct type_unit_group type_unit_group_for_lookup;
6787 if (dwarf2_per_objfile->type_unit_groups == NULL)
6789 dwarf2_per_objfile->type_unit_groups =
6790 allocate_type_unit_groups_table ();
6793 /* Do we need to create a new group, or can we use an existing one? */
6797 line_offset = DW_UNSND (stmt_list);
6798 ++tu_stats->nr_symtab_sharers;
6802 /* Ugh, no stmt_list. Rare, but we have to handle it.
6803 We can do various things here like create one group per TU or
6804 spread them over multiple groups to split up the expansion work.
6805 To avoid worst case scenarios (too many groups or too large groups)
6806 we, umm, group them in bunches. */
6807 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6808 | (tu_stats->nr_stmt_less_type_units
6809 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6810 ++tu_stats->nr_stmt_less_type_units;
6813 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6814 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6815 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6816 &type_unit_group_for_lookup, INSERT);
6819 tu_group = (struct type_unit_group *) *slot;
6820 gdb_assert (tu_group != NULL);
6824 sect_offset line_offset_struct = (sect_offset) line_offset;
6825 tu_group = create_type_unit_group (cu, line_offset_struct);
6827 ++tu_stats->nr_symtabs;
6833 /* Partial symbol tables. */
6835 /* Create a psymtab named NAME and assign it to PER_CU.
6837 The caller must fill in the following details:
6838 dirname, textlow, texthigh. */
6840 static struct partial_symtab *
6841 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6843 struct objfile *objfile = per_cu->objfile;
6844 struct partial_symtab *pst;
6846 pst = start_psymtab_common (objfile, name, 0,
6847 objfile->global_psymbols,
6848 objfile->static_psymbols);
6850 pst->psymtabs_addrmap_supported = 1;
6852 /* This is the glue that links PST into GDB's symbol API. */
6853 pst->read_symtab_private = per_cu;
6854 pst->read_symtab = dwarf2_read_symtab;
6855 per_cu->v.psymtab = pst;
6860 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6863 struct process_psymtab_comp_unit_data
6865 /* True if we are reading a DW_TAG_partial_unit. */
6867 int want_partial_unit;
6869 /* The "pretend" language that is used if the CU doesn't declare a
6872 enum language pretend_language;
6875 /* die_reader_func for process_psymtab_comp_unit. */
6878 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6879 const gdb_byte *info_ptr,
6880 struct die_info *comp_unit_die,
6884 struct dwarf2_cu *cu = reader->cu;
6885 struct objfile *objfile = cu->objfile;
6886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6887 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6889 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6890 struct partial_symtab *pst;
6891 enum pc_bounds_kind cu_bounds_kind;
6892 const char *filename;
6893 struct process_psymtab_comp_unit_data *info
6894 = (struct process_psymtab_comp_unit_data *) data;
6896 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6899 gdb_assert (! per_cu->is_debug_types);
6901 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6903 cu->list_in_scope = &file_symbols;
6905 /* Allocate a new partial symbol table structure. */
6906 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6907 if (filename == NULL)
6910 pst = create_partial_symtab (per_cu, filename);
6912 /* This must be done before calling dwarf2_build_include_psymtabs. */
6913 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6917 dwarf2_find_base_address (comp_unit_die, cu);
6919 /* Possibly set the default values of LOWPC and HIGHPC from
6921 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6922 &best_highpc, cu, pst);
6923 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6924 /* Store the contiguous range if it is not empty; it can be empty for
6925 CUs with no code. */
6926 addrmap_set_empty (objfile->psymtabs_addrmap,
6927 gdbarch_adjust_dwarf2_addr (gdbarch,
6928 best_lowpc + baseaddr),
6929 gdbarch_adjust_dwarf2_addr (gdbarch,
6930 best_highpc + baseaddr) - 1,
6933 /* Check if comp unit has_children.
6934 If so, read the rest of the partial symbols from this comp unit.
6935 If not, there's no more debug_info for this comp unit. */
6938 struct partial_die_info *first_die;
6939 CORE_ADDR lowpc, highpc;
6941 lowpc = ((CORE_ADDR) -1);
6942 highpc = ((CORE_ADDR) 0);
6944 first_die = load_partial_dies (reader, info_ptr, 1);
6946 scan_partial_symbols (first_die, &lowpc, &highpc,
6947 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6949 /* If we didn't find a lowpc, set it to highpc to avoid
6950 complaints from `maint check'. */
6951 if (lowpc == ((CORE_ADDR) -1))
6954 /* If the compilation unit didn't have an explicit address range,
6955 then use the information extracted from its child dies. */
6956 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6959 best_highpc = highpc;
6962 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6963 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6965 end_psymtab_common (objfile, pst);
6967 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6970 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6971 struct dwarf2_per_cu_data *iter;
6973 /* Fill in 'dependencies' here; we fill in 'users' in a
6975 pst->number_of_dependencies = len;
6977 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6979 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6982 pst->dependencies[i] = iter->v.psymtab;
6984 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6987 /* Get the list of files included in the current compilation unit,
6988 and build a psymtab for each of them. */
6989 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6991 if (dwarf_read_debug)
6993 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6995 fprintf_unfiltered (gdb_stdlog,
6996 "Psymtab for %s unit @0x%x: %s - %s"
6997 ", %d global, %d static syms\n",
6998 per_cu->is_debug_types ? "type" : "comp",
6999 to_underlying (per_cu->sect_off),
7000 paddress (gdbarch, pst->textlow),
7001 paddress (gdbarch, pst->texthigh),
7002 pst->n_global_syms, pst->n_static_syms);
7006 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7007 Process compilation unit THIS_CU for a psymtab. */
7010 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7011 int want_partial_unit,
7012 enum language pretend_language)
7014 /* If this compilation unit was already read in, free the
7015 cached copy in order to read it in again. This is
7016 necessary because we skipped some symbols when we first
7017 read in the compilation unit (see load_partial_dies).
7018 This problem could be avoided, but the benefit is unclear. */
7019 if (this_cu->cu != NULL)
7020 free_one_cached_comp_unit (this_cu);
7022 if (this_cu->is_debug_types)
7023 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7027 process_psymtab_comp_unit_data info;
7028 info.want_partial_unit = want_partial_unit;
7029 info.pretend_language = pretend_language;
7030 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7031 process_psymtab_comp_unit_reader, &info);
7034 /* Age out any secondary CUs. */
7035 age_cached_comp_units ();
7038 /* Reader function for build_type_psymtabs. */
7041 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7042 const gdb_byte *info_ptr,
7043 struct die_info *type_unit_die,
7047 struct objfile *objfile = dwarf2_per_objfile->objfile;
7048 struct dwarf2_cu *cu = reader->cu;
7049 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7050 struct signatured_type *sig_type;
7051 struct type_unit_group *tu_group;
7052 struct attribute *attr;
7053 struct partial_die_info *first_die;
7054 CORE_ADDR lowpc, highpc;
7055 struct partial_symtab *pst;
7057 gdb_assert (data == NULL);
7058 gdb_assert (per_cu->is_debug_types);
7059 sig_type = (struct signatured_type *) per_cu;
7064 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7065 tu_group = get_type_unit_group (cu, attr);
7067 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
7069 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7070 cu->list_in_scope = &file_symbols;
7071 pst = create_partial_symtab (per_cu, "");
7074 first_die = load_partial_dies (reader, info_ptr, 1);
7076 lowpc = (CORE_ADDR) -1;
7077 highpc = (CORE_ADDR) 0;
7078 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7080 end_psymtab_common (objfile, pst);
7083 /* Struct used to sort TUs by their abbreviation table offset. */
7085 struct tu_abbrev_offset
7087 struct signatured_type *sig_type;
7088 sect_offset abbrev_offset;
7091 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
7094 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7096 const struct tu_abbrev_offset * const *a
7097 = (const struct tu_abbrev_offset * const*) ap;
7098 const struct tu_abbrev_offset * const *b
7099 = (const struct tu_abbrev_offset * const*) bp;
7100 sect_offset aoff = (*a)->abbrev_offset;
7101 sect_offset boff = (*b)->abbrev_offset;
7103 return (aoff > boff) - (aoff < boff);
7106 /* Efficiently read all the type units.
7107 This does the bulk of the work for build_type_psymtabs.
7109 The efficiency is because we sort TUs by the abbrev table they use and
7110 only read each abbrev table once. In one program there are 200K TUs
7111 sharing 8K abbrev tables.
7113 The main purpose of this function is to support building the
7114 dwarf2_per_objfile->type_unit_groups table.
7115 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7116 can collapse the search space by grouping them by stmt_list.
7117 The savings can be significant, in the same program from above the 200K TUs
7118 share 8K stmt_list tables.
7120 FUNC is expected to call get_type_unit_group, which will create the
7121 struct type_unit_group if necessary and add it to
7122 dwarf2_per_objfile->type_unit_groups. */
7125 build_type_psymtabs_1 (void)
7127 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7128 struct cleanup *cleanups;
7129 struct abbrev_table *abbrev_table;
7130 sect_offset abbrev_offset;
7131 struct tu_abbrev_offset *sorted_by_abbrev;
7134 /* It's up to the caller to not call us multiple times. */
7135 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7137 if (dwarf2_per_objfile->n_type_units == 0)
7140 /* TUs typically share abbrev tables, and there can be way more TUs than
7141 abbrev tables. Sort by abbrev table to reduce the number of times we
7142 read each abbrev table in.
7143 Alternatives are to punt or to maintain a cache of abbrev tables.
7144 This is simpler and efficient enough for now.
7146 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7147 symtab to use). Typically TUs with the same abbrev offset have the same
7148 stmt_list value too so in practice this should work well.
7150 The basic algorithm here is:
7152 sort TUs by abbrev table
7153 for each TU with same abbrev table:
7154 read abbrev table if first user
7155 read TU top level DIE
7156 [IWBN if DWO skeletons had DW_AT_stmt_list]
7159 if (dwarf_read_debug)
7160 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7162 /* Sort in a separate table to maintain the order of all_type_units
7163 for .gdb_index: TU indices directly index all_type_units. */
7164 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7165 dwarf2_per_objfile->n_type_units);
7166 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7168 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7170 sorted_by_abbrev[i].sig_type = sig_type;
7171 sorted_by_abbrev[i].abbrev_offset =
7172 read_abbrev_offset (sig_type->per_cu.section,
7173 sig_type->per_cu.sect_off);
7175 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7176 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7177 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7179 abbrev_offset = (sect_offset) ~(unsigned) 0;
7180 abbrev_table = NULL;
7181 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7183 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7185 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7187 /* Switch to the next abbrev table if necessary. */
7188 if (abbrev_table == NULL
7189 || tu->abbrev_offset != abbrev_offset)
7191 if (abbrev_table != NULL)
7193 abbrev_table_free (abbrev_table);
7194 /* Reset to NULL in case abbrev_table_read_table throws
7195 an error: abbrev_table_free_cleanup will get called. */
7196 abbrev_table = NULL;
7198 abbrev_offset = tu->abbrev_offset;
7200 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7202 ++tu_stats->nr_uniq_abbrev_tables;
7205 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7206 build_type_psymtabs_reader, NULL);
7209 do_cleanups (cleanups);
7212 /* Print collected type unit statistics. */
7215 print_tu_stats (void)
7217 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7219 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7220 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7221 dwarf2_per_objfile->n_type_units);
7222 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7223 tu_stats->nr_uniq_abbrev_tables);
7224 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7225 tu_stats->nr_symtabs);
7226 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7227 tu_stats->nr_symtab_sharers);
7228 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7229 tu_stats->nr_stmt_less_type_units);
7230 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7231 tu_stats->nr_all_type_units_reallocs);
7234 /* Traversal function for build_type_psymtabs. */
7237 build_type_psymtab_dependencies (void **slot, void *info)
7239 struct objfile *objfile = dwarf2_per_objfile->objfile;
7240 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7241 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7242 struct partial_symtab *pst = per_cu->v.psymtab;
7243 int len = VEC_length (sig_type_ptr, tu_group->tus);
7244 struct signatured_type *iter;
7247 gdb_assert (len > 0);
7248 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7250 pst->number_of_dependencies = len;
7252 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7254 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
7257 gdb_assert (iter->per_cu.is_debug_types);
7258 pst->dependencies[i] = iter->per_cu.v.psymtab;
7259 iter->type_unit_group = tu_group;
7262 VEC_free (sig_type_ptr, tu_group->tus);
7267 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7268 Build partial symbol tables for the .debug_types comp-units. */
7271 build_type_psymtabs (struct objfile *objfile)
7273 if (! create_all_type_units (objfile))
7276 build_type_psymtabs_1 ();
7279 /* Traversal function for process_skeletonless_type_unit.
7280 Read a TU in a DWO file and build partial symbols for it. */
7283 process_skeletonless_type_unit (void **slot, void *info)
7285 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7286 struct objfile *objfile = (struct objfile *) info;
7287 struct signatured_type find_entry, *entry;
7289 /* If this TU doesn't exist in the global table, add it and read it in. */
7291 if (dwarf2_per_objfile->signatured_types == NULL)
7293 dwarf2_per_objfile->signatured_types
7294 = allocate_signatured_type_table (objfile);
7297 find_entry.signature = dwo_unit->signature;
7298 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7300 /* If we've already seen this type there's nothing to do. What's happening
7301 is we're doing our own version of comdat-folding here. */
7305 /* This does the job that create_all_type_units would have done for
7307 entry = add_type_unit (dwo_unit->signature, slot);
7308 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7311 /* This does the job that build_type_psymtabs_1 would have done. */
7312 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7313 build_type_psymtabs_reader, NULL);
7318 /* Traversal function for process_skeletonless_type_units. */
7321 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7323 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7325 if (dwo_file->tus != NULL)
7327 htab_traverse_noresize (dwo_file->tus,
7328 process_skeletonless_type_unit, info);
7334 /* Scan all TUs of DWO files, verifying we've processed them.
7335 This is needed in case a TU was emitted without its skeleton.
7336 Note: This can't be done until we know what all the DWO files are. */
7339 process_skeletonless_type_units (struct objfile *objfile)
7341 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7342 if (get_dwp_file () == NULL
7343 && dwarf2_per_objfile->dwo_files != NULL)
7345 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7346 process_dwo_file_for_skeletonless_type_units,
7351 /* Compute the 'user' field for each psymtab in OBJFILE. */
7354 set_partial_user (struct objfile *objfile)
7358 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7360 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7361 struct partial_symtab *pst = per_cu->v.psymtab;
7367 for (j = 0; j < pst->number_of_dependencies; ++j)
7369 /* Set the 'user' field only if it is not already set. */
7370 if (pst->dependencies[j]->user == NULL)
7371 pst->dependencies[j]->user = pst;
7376 /* Build the partial symbol table by doing a quick pass through the
7377 .debug_info and .debug_abbrev sections. */
7380 dwarf2_build_psymtabs_hard (struct objfile *objfile)
7382 struct cleanup *back_to;
7385 if (dwarf_read_debug)
7387 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7388 objfile_name (objfile));
7391 dwarf2_per_objfile->reading_partial_symbols = 1;
7393 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
7395 /* Any cached compilation units will be linked by the per-objfile
7396 read_in_chain. Make sure to free them when we're done. */
7397 back_to = make_cleanup (free_cached_comp_units, NULL);
7399 build_type_psymtabs (objfile);
7401 create_all_comp_units (objfile);
7403 /* Create a temporary address map on a temporary obstack. We later
7404 copy this to the final obstack. */
7405 auto_obstack temp_obstack;
7407 scoped_restore save_psymtabs_addrmap
7408 = make_scoped_restore (&objfile->psymtabs_addrmap,
7409 addrmap_create_mutable (&temp_obstack));
7411 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7413 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7415 process_psymtab_comp_unit (per_cu, 0, language_minimal);
7418 /* This has to wait until we read the CUs, we need the list of DWOs. */
7419 process_skeletonless_type_units (objfile);
7421 /* Now that all TUs have been processed we can fill in the dependencies. */
7422 if (dwarf2_per_objfile->type_unit_groups != NULL)
7424 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7425 build_type_psymtab_dependencies, NULL);
7428 if (dwarf_read_debug)
7431 set_partial_user (objfile);
7433 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7434 &objfile->objfile_obstack);
7435 /* At this point we want to keep the address map. */
7436 save_psymtabs_addrmap.release ();
7438 do_cleanups (back_to);
7440 if (dwarf_read_debug)
7441 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7442 objfile_name (objfile));
7445 /* die_reader_func for load_partial_comp_unit. */
7448 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
7449 const gdb_byte *info_ptr,
7450 struct die_info *comp_unit_die,
7454 struct dwarf2_cu *cu = reader->cu;
7456 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
7458 /* Check if comp unit has_children.
7459 If so, read the rest of the partial symbols from this comp unit.
7460 If not, there's no more debug_info for this comp unit. */
7462 load_partial_dies (reader, info_ptr, 0);
7465 /* Load the partial DIEs for a secondary CU into memory.
7466 This is also used when rereading a primary CU with load_all_dies. */
7469 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7471 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7472 load_partial_comp_unit_reader, NULL);
7476 read_comp_units_from_section (struct objfile *objfile,
7477 struct dwarf2_section_info *section,
7478 struct dwarf2_section_info *abbrev_section,
7479 unsigned int is_dwz,
7482 struct dwarf2_per_cu_data ***all_comp_units)
7484 const gdb_byte *info_ptr;
7485 bfd *abfd = get_section_bfd_owner (section);
7487 if (dwarf_read_debug)
7488 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7489 get_section_name (section),
7490 get_section_file_name (section));
7492 dwarf2_read_section (objfile, section);
7494 info_ptr = section->buffer;
7496 while (info_ptr < section->buffer + section->size)
7498 struct dwarf2_per_cu_data *this_cu;
7500 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7502 comp_unit_head cu_header;
7503 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7504 info_ptr, rcuh_kind::COMPILE);
7506 /* Save the compilation unit for later lookup. */
7507 if (cu_header.unit_type != DW_UT_type)
7509 this_cu = XOBNEW (&objfile->objfile_obstack,
7510 struct dwarf2_per_cu_data);
7511 memset (this_cu, 0, sizeof (*this_cu));
7515 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7516 struct signatured_type);
7517 memset (sig_type, 0, sizeof (*sig_type));
7518 sig_type->signature = cu_header.signature;
7519 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7520 this_cu = &sig_type->per_cu;
7522 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7523 this_cu->sect_off = sect_off;
7524 this_cu->length = cu_header.length + cu_header.initial_length_size;
7525 this_cu->is_dwz = is_dwz;
7526 this_cu->objfile = objfile;
7527 this_cu->section = section;
7529 if (*n_comp_units == *n_allocated)
7532 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7533 *all_comp_units, *n_allocated);
7535 (*all_comp_units)[*n_comp_units] = this_cu;
7538 info_ptr = info_ptr + this_cu->length;
7542 /* Create a list of all compilation units in OBJFILE.
7543 This is only done for -readnow and building partial symtabs. */
7546 create_all_comp_units (struct objfile *objfile)
7550 struct dwarf2_per_cu_data **all_comp_units;
7551 struct dwz_file *dwz;
7555 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
7557 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7558 &dwarf2_per_objfile->abbrev, 0,
7559 &n_allocated, &n_comp_units, &all_comp_units);
7561 dwz = dwarf2_get_dwz_file ();
7563 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
7564 &n_allocated, &n_comp_units,
7567 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7568 struct dwarf2_per_cu_data *,
7570 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7571 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7572 xfree (all_comp_units);
7573 dwarf2_per_objfile->n_comp_units = n_comp_units;
7576 /* Process all loaded DIEs for compilation unit CU, starting at
7577 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7578 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7579 DW_AT_ranges). See the comments of add_partial_subprogram on how
7580 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7583 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7584 CORE_ADDR *highpc, int set_addrmap,
7585 struct dwarf2_cu *cu)
7587 struct partial_die_info *pdi;
7589 /* Now, march along the PDI's, descending into ones which have
7590 interesting children but skipping the children of the other ones,
7591 until we reach the end of the compilation unit. */
7597 fixup_partial_die (pdi, cu);
7599 /* Anonymous namespaces or modules have no name but have interesting
7600 children, so we need to look at them. Ditto for anonymous
7603 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7604 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7605 || pdi->tag == DW_TAG_imported_unit)
7609 case DW_TAG_subprogram:
7610 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7612 case DW_TAG_constant:
7613 case DW_TAG_variable:
7614 case DW_TAG_typedef:
7615 case DW_TAG_union_type:
7616 if (!pdi->is_declaration)
7618 add_partial_symbol (pdi, cu);
7621 case DW_TAG_class_type:
7622 case DW_TAG_interface_type:
7623 case DW_TAG_structure_type:
7624 if (!pdi->is_declaration)
7626 add_partial_symbol (pdi, cu);
7628 if (cu->language == language_rust && pdi->has_children)
7629 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7632 case DW_TAG_enumeration_type:
7633 if (!pdi->is_declaration)
7634 add_partial_enumeration (pdi, cu);
7636 case DW_TAG_base_type:
7637 case DW_TAG_subrange_type:
7638 /* File scope base type definitions are added to the partial
7640 add_partial_symbol (pdi, cu);
7642 case DW_TAG_namespace:
7643 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7646 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7648 case DW_TAG_imported_unit:
7650 struct dwarf2_per_cu_data *per_cu;
7652 /* For now we don't handle imported units in type units. */
7653 if (cu->per_cu->is_debug_types)
7655 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7656 " supported in type units [in module %s]"),
7657 objfile_name (cu->objfile));
7660 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
7664 /* Go read the partial unit, if needed. */
7665 if (per_cu->v.psymtab == NULL)
7666 process_psymtab_comp_unit (per_cu, 1, cu->language);
7668 VEC_safe_push (dwarf2_per_cu_ptr,
7669 cu->per_cu->imported_symtabs, per_cu);
7672 case DW_TAG_imported_declaration:
7673 add_partial_symbol (pdi, cu);
7680 /* If the die has a sibling, skip to the sibling. */
7682 pdi = pdi->die_sibling;
7686 /* Functions used to compute the fully scoped name of a partial DIE.
7688 Normally, this is simple. For C++, the parent DIE's fully scoped
7689 name is concatenated with "::" and the partial DIE's name.
7690 Enumerators are an exception; they use the scope of their parent
7691 enumeration type, i.e. the name of the enumeration type is not
7692 prepended to the enumerator.
7694 There are two complexities. One is DW_AT_specification; in this
7695 case "parent" means the parent of the target of the specification,
7696 instead of the direct parent of the DIE. The other is compilers
7697 which do not emit DW_TAG_namespace; in this case we try to guess
7698 the fully qualified name of structure types from their members'
7699 linkage names. This must be done using the DIE's children rather
7700 than the children of any DW_AT_specification target. We only need
7701 to do this for structures at the top level, i.e. if the target of
7702 any DW_AT_specification (if any; otherwise the DIE itself) does not
7705 /* Compute the scope prefix associated with PDI's parent, in
7706 compilation unit CU. The result will be allocated on CU's
7707 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7708 field. NULL is returned if no prefix is necessary. */
7710 partial_die_parent_scope (struct partial_die_info *pdi,
7711 struct dwarf2_cu *cu)
7713 const char *grandparent_scope;
7714 struct partial_die_info *parent, *real_pdi;
7716 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7717 then this means the parent of the specification DIE. */
7720 while (real_pdi->has_specification)
7721 real_pdi = find_partial_die (real_pdi->spec_offset,
7722 real_pdi->spec_is_dwz, cu);
7724 parent = real_pdi->die_parent;
7728 if (parent->scope_set)
7729 return parent->scope;
7731 fixup_partial_die (parent, cu);
7733 grandparent_scope = partial_die_parent_scope (parent, cu);
7735 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7736 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7737 Work around this problem here. */
7738 if (cu->language == language_cplus
7739 && parent->tag == DW_TAG_namespace
7740 && strcmp (parent->name, "::") == 0
7741 && grandparent_scope == NULL)
7743 parent->scope = NULL;
7744 parent->scope_set = 1;
7748 if (pdi->tag == DW_TAG_enumerator)
7749 /* Enumerators should not get the name of the enumeration as a prefix. */
7750 parent->scope = grandparent_scope;
7751 else if (parent->tag == DW_TAG_namespace
7752 || parent->tag == DW_TAG_module
7753 || parent->tag == DW_TAG_structure_type
7754 || parent->tag == DW_TAG_class_type
7755 || parent->tag == DW_TAG_interface_type
7756 || parent->tag == DW_TAG_union_type
7757 || parent->tag == DW_TAG_enumeration_type)
7759 if (grandparent_scope == NULL)
7760 parent->scope = parent->name;
7762 parent->scope = typename_concat (&cu->comp_unit_obstack,
7764 parent->name, 0, cu);
7768 /* FIXME drow/2004-04-01: What should we be doing with
7769 function-local names? For partial symbols, we should probably be
7771 complaint (&symfile_complaints,
7772 _("unhandled containing DIE tag %d for DIE at %d"),
7773 parent->tag, to_underlying (pdi->sect_off));
7774 parent->scope = grandparent_scope;
7777 parent->scope_set = 1;
7778 return parent->scope;
7781 /* Return the fully scoped name associated with PDI, from compilation unit
7782 CU. The result will be allocated with malloc. */
7785 partial_die_full_name (struct partial_die_info *pdi,
7786 struct dwarf2_cu *cu)
7788 const char *parent_scope;
7790 /* If this is a template instantiation, we can not work out the
7791 template arguments from partial DIEs. So, unfortunately, we have
7792 to go through the full DIEs. At least any work we do building
7793 types here will be reused if full symbols are loaded later. */
7794 if (pdi->has_template_arguments)
7796 fixup_partial_die (pdi, cu);
7798 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7800 struct die_info *die;
7801 struct attribute attr;
7802 struct dwarf2_cu *ref_cu = cu;
7804 /* DW_FORM_ref_addr is using section offset. */
7805 attr.name = (enum dwarf_attribute) 0;
7806 attr.form = DW_FORM_ref_addr;
7807 attr.u.unsnd = to_underlying (pdi->sect_off);
7808 die = follow_die_ref (NULL, &attr, &ref_cu);
7810 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7814 parent_scope = partial_die_parent_scope (pdi, cu);
7815 if (parent_scope == NULL)
7818 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7822 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7824 struct objfile *objfile = cu->objfile;
7825 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7827 const char *actual_name = NULL;
7829 char *built_actual_name;
7831 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7833 built_actual_name = partial_die_full_name (pdi, cu);
7834 if (built_actual_name != NULL)
7835 actual_name = built_actual_name;
7837 if (actual_name == NULL)
7838 actual_name = pdi->name;
7842 case DW_TAG_subprogram:
7843 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7844 if (pdi->is_external || cu->language == language_ada)
7846 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7847 of the global scope. But in Ada, we want to be able to access
7848 nested procedures globally. So all Ada subprograms are stored
7849 in the global scope. */
7850 add_psymbol_to_list (actual_name, strlen (actual_name),
7851 built_actual_name != NULL,
7852 VAR_DOMAIN, LOC_BLOCK,
7853 &objfile->global_psymbols,
7854 addr, cu->language, objfile);
7858 add_psymbol_to_list (actual_name, strlen (actual_name),
7859 built_actual_name != NULL,
7860 VAR_DOMAIN, LOC_BLOCK,
7861 &objfile->static_psymbols,
7862 addr, cu->language, objfile);
7865 if (pdi->main_subprogram && actual_name != NULL)
7866 set_objfile_main_name (objfile, actual_name, cu->language);
7868 case DW_TAG_constant:
7870 std::vector<partial_symbol *> *list;
7872 if (pdi->is_external)
7873 list = &objfile->global_psymbols;
7875 list = &objfile->static_psymbols;
7876 add_psymbol_to_list (actual_name, strlen (actual_name),
7877 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7878 list, 0, cu->language, objfile);
7881 case DW_TAG_variable:
7883 addr = decode_locdesc (pdi->d.locdesc, cu);
7887 && !dwarf2_per_objfile->has_section_at_zero)
7889 /* A global or static variable may also have been stripped
7890 out by the linker if unused, in which case its address
7891 will be nullified; do not add such variables into partial
7892 symbol table then. */
7894 else if (pdi->is_external)
7897 Don't enter into the minimal symbol tables as there is
7898 a minimal symbol table entry from the ELF symbols already.
7899 Enter into partial symbol table if it has a location
7900 descriptor or a type.
7901 If the location descriptor is missing, new_symbol will create
7902 a LOC_UNRESOLVED symbol, the address of the variable will then
7903 be determined from the minimal symbol table whenever the variable
7905 The address for the partial symbol table entry is not
7906 used by GDB, but it comes in handy for debugging partial symbol
7909 if (pdi->d.locdesc || pdi->has_type)
7910 add_psymbol_to_list (actual_name, strlen (actual_name),
7911 built_actual_name != NULL,
7912 VAR_DOMAIN, LOC_STATIC,
7913 &objfile->global_psymbols,
7915 cu->language, objfile);
7919 int has_loc = pdi->d.locdesc != NULL;
7921 /* Static Variable. Skip symbols whose value we cannot know (those
7922 without location descriptors or constant values). */
7923 if (!has_loc && !pdi->has_const_value)
7925 xfree (built_actual_name);
7929 add_psymbol_to_list (actual_name, strlen (actual_name),
7930 built_actual_name != NULL,
7931 VAR_DOMAIN, LOC_STATIC,
7932 &objfile->static_psymbols,
7933 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7934 cu->language, objfile);
7937 case DW_TAG_typedef:
7938 case DW_TAG_base_type:
7939 case DW_TAG_subrange_type:
7940 add_psymbol_to_list (actual_name, strlen (actual_name),
7941 built_actual_name != NULL,
7942 VAR_DOMAIN, LOC_TYPEDEF,
7943 &objfile->static_psymbols,
7944 0, cu->language, objfile);
7946 case DW_TAG_imported_declaration:
7947 case DW_TAG_namespace:
7948 add_psymbol_to_list (actual_name, strlen (actual_name),
7949 built_actual_name != NULL,
7950 VAR_DOMAIN, LOC_TYPEDEF,
7951 &objfile->global_psymbols,
7952 0, cu->language, objfile);
7955 add_psymbol_to_list (actual_name, strlen (actual_name),
7956 built_actual_name != NULL,
7957 MODULE_DOMAIN, LOC_TYPEDEF,
7958 &objfile->global_psymbols,
7959 0, cu->language, objfile);
7961 case DW_TAG_class_type:
7962 case DW_TAG_interface_type:
7963 case DW_TAG_structure_type:
7964 case DW_TAG_union_type:
7965 case DW_TAG_enumeration_type:
7966 /* Skip external references. The DWARF standard says in the section
7967 about "Structure, Union, and Class Type Entries": "An incomplete
7968 structure, union or class type is represented by a structure,
7969 union or class entry that does not have a byte size attribute
7970 and that has a DW_AT_declaration attribute." */
7971 if (!pdi->has_byte_size && pdi->is_declaration)
7973 xfree (built_actual_name);
7977 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7978 static vs. global. */
7979 add_psymbol_to_list (actual_name, strlen (actual_name),
7980 built_actual_name != NULL,
7981 STRUCT_DOMAIN, LOC_TYPEDEF,
7982 cu->language == language_cplus
7983 ? &objfile->global_psymbols
7984 : &objfile->static_psymbols,
7985 0, cu->language, objfile);
7988 case DW_TAG_enumerator:
7989 add_psymbol_to_list (actual_name, strlen (actual_name),
7990 built_actual_name != NULL,
7991 VAR_DOMAIN, LOC_CONST,
7992 cu->language == language_cplus
7993 ? &objfile->global_psymbols
7994 : &objfile->static_psymbols,
7995 0, cu->language, objfile);
8001 xfree (built_actual_name);
8004 /* Read a partial die corresponding to a namespace; also, add a symbol
8005 corresponding to that namespace to the symbol table. NAMESPACE is
8006 the name of the enclosing namespace. */
8009 add_partial_namespace (struct partial_die_info *pdi,
8010 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8011 int set_addrmap, struct dwarf2_cu *cu)
8013 /* Add a symbol for the namespace. */
8015 add_partial_symbol (pdi, cu);
8017 /* Now scan partial symbols in that namespace. */
8019 if (pdi->has_children)
8020 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8023 /* Read a partial die corresponding to a Fortran module. */
8026 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8027 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8029 /* Add a symbol for the namespace. */
8031 add_partial_symbol (pdi, cu);
8033 /* Now scan partial symbols in that module. */
8035 if (pdi->has_children)
8036 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8039 /* Read a partial die corresponding to a subprogram and create a partial
8040 symbol for that subprogram. When the CU language allows it, this
8041 routine also defines a partial symbol for each nested subprogram
8042 that this subprogram contains. If SET_ADDRMAP is true, record the
8043 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8044 and highest PC values found in PDI.
8046 PDI may also be a lexical block, in which case we simply search
8047 recursively for subprograms defined inside that lexical block.
8048 Again, this is only performed when the CU language allows this
8049 type of definitions. */
8052 add_partial_subprogram (struct partial_die_info *pdi,
8053 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8054 int set_addrmap, struct dwarf2_cu *cu)
8056 if (pdi->tag == DW_TAG_subprogram)
8058 if (pdi->has_pc_info)
8060 if (pdi->lowpc < *lowpc)
8061 *lowpc = pdi->lowpc;
8062 if (pdi->highpc > *highpc)
8063 *highpc = pdi->highpc;
8066 struct objfile *objfile = cu->objfile;
8067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8072 baseaddr = ANOFFSET (objfile->section_offsets,
8073 SECT_OFF_TEXT (objfile));
8074 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8075 pdi->lowpc + baseaddr);
8076 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8077 pdi->highpc + baseaddr);
8078 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
8079 cu->per_cu->v.psymtab);
8083 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8085 if (!pdi->is_declaration)
8086 /* Ignore subprogram DIEs that do not have a name, they are
8087 illegal. Do not emit a complaint at this point, we will
8088 do so when we convert this psymtab into a symtab. */
8090 add_partial_symbol (pdi, cu);
8094 if (! pdi->has_children)
8097 if (cu->language == language_ada)
8099 pdi = pdi->die_child;
8102 fixup_partial_die (pdi, cu);
8103 if (pdi->tag == DW_TAG_subprogram
8104 || pdi->tag == DW_TAG_lexical_block)
8105 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8106 pdi = pdi->die_sibling;
8111 /* Read a partial die corresponding to an enumeration type. */
8114 add_partial_enumeration (struct partial_die_info *enum_pdi,
8115 struct dwarf2_cu *cu)
8117 struct partial_die_info *pdi;
8119 if (enum_pdi->name != NULL)
8120 add_partial_symbol (enum_pdi, cu);
8122 pdi = enum_pdi->die_child;
8125 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8126 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8128 add_partial_symbol (pdi, cu);
8129 pdi = pdi->die_sibling;
8133 /* Return the initial uleb128 in the die at INFO_PTR. */
8136 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8138 unsigned int bytes_read;
8140 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8143 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8144 Return the corresponding abbrev, or NULL if the number is zero (indicating
8145 an empty DIE). In either case *BYTES_READ will be set to the length of
8146 the initial number. */
8148 static struct abbrev_info *
8149 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
8150 struct dwarf2_cu *cu)
8152 bfd *abfd = cu->objfile->obfd;
8153 unsigned int abbrev_number;
8154 struct abbrev_info *abbrev;
8156 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8158 if (abbrev_number == 0)
8161 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
8164 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8165 " at offset 0x%x [in module %s]"),
8166 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8167 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
8173 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8174 Returns a pointer to the end of a series of DIEs, terminated by an empty
8175 DIE. Any children of the skipped DIEs will also be skipped. */
8177 static const gdb_byte *
8178 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8180 struct dwarf2_cu *cu = reader->cu;
8181 struct abbrev_info *abbrev;
8182 unsigned int bytes_read;
8186 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8188 return info_ptr + bytes_read;
8190 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8194 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8195 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8196 abbrev corresponding to that skipped uleb128 should be passed in
8197 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8200 static const gdb_byte *
8201 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8202 struct abbrev_info *abbrev)
8204 unsigned int bytes_read;
8205 struct attribute attr;
8206 bfd *abfd = reader->abfd;
8207 struct dwarf2_cu *cu = reader->cu;
8208 const gdb_byte *buffer = reader->buffer;
8209 const gdb_byte *buffer_end = reader->buffer_end;
8210 unsigned int form, i;
8212 for (i = 0; i < abbrev->num_attrs; i++)
8214 /* The only abbrev we care about is DW_AT_sibling. */
8215 if (abbrev->attrs[i].name == DW_AT_sibling)
8217 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8218 if (attr.form == DW_FORM_ref_addr)
8219 complaint (&symfile_complaints,
8220 _("ignoring absolute DW_AT_sibling"));
8223 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8224 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8226 if (sibling_ptr < info_ptr)
8227 complaint (&symfile_complaints,
8228 _("DW_AT_sibling points backwards"));
8229 else if (sibling_ptr > reader->buffer_end)
8230 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8236 /* If it isn't DW_AT_sibling, skip this attribute. */
8237 form = abbrev->attrs[i].form;
8241 case DW_FORM_ref_addr:
8242 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8243 and later it is offset sized. */
8244 if (cu->header.version == 2)
8245 info_ptr += cu->header.addr_size;
8247 info_ptr += cu->header.offset_size;
8249 case DW_FORM_GNU_ref_alt:
8250 info_ptr += cu->header.offset_size;
8253 info_ptr += cu->header.addr_size;
8260 case DW_FORM_flag_present:
8261 case DW_FORM_implicit_const:
8273 case DW_FORM_ref_sig8:
8276 case DW_FORM_data16:
8279 case DW_FORM_string:
8280 read_direct_string (abfd, info_ptr, &bytes_read);
8281 info_ptr += bytes_read;
8283 case DW_FORM_sec_offset:
8285 case DW_FORM_GNU_strp_alt:
8286 info_ptr += cu->header.offset_size;
8288 case DW_FORM_exprloc:
8290 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8291 info_ptr += bytes_read;
8293 case DW_FORM_block1:
8294 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8296 case DW_FORM_block2:
8297 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8299 case DW_FORM_block4:
8300 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8304 case DW_FORM_ref_udata:
8305 case DW_FORM_GNU_addr_index:
8306 case DW_FORM_GNU_str_index:
8307 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8309 case DW_FORM_indirect:
8310 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8311 info_ptr += bytes_read;
8312 /* We need to continue parsing from here, so just go back to
8314 goto skip_attribute;
8317 error (_("Dwarf Error: Cannot handle %s "
8318 "in DWARF reader [in module %s]"),
8319 dwarf_form_name (form),
8320 bfd_get_filename (abfd));
8324 if (abbrev->has_children)
8325 return skip_children (reader, info_ptr);
8330 /* Locate ORIG_PDI's sibling.
8331 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8333 static const gdb_byte *
8334 locate_pdi_sibling (const struct die_reader_specs *reader,
8335 struct partial_die_info *orig_pdi,
8336 const gdb_byte *info_ptr)
8338 /* Do we know the sibling already? */
8340 if (orig_pdi->sibling)
8341 return orig_pdi->sibling;
8343 /* Are there any children to deal with? */
8345 if (!orig_pdi->has_children)
8348 /* Skip the children the long way. */
8350 return skip_children (reader, info_ptr);
8353 /* Expand this partial symbol table into a full symbol table. SELF is
8357 dwarf2_read_symtab (struct partial_symtab *self,
8358 struct objfile *objfile)
8362 warning (_("bug: psymtab for %s is already read in."),
8369 printf_filtered (_("Reading in symbols for %s..."),
8371 gdb_flush (gdb_stdout);
8374 /* Restore our global data. */
8376 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8377 dwarf2_objfile_data_key);
8379 /* If this psymtab is constructed from a debug-only objfile, the
8380 has_section_at_zero flag will not necessarily be correct. We
8381 can get the correct value for this flag by looking at the data
8382 associated with the (presumably stripped) associated objfile. */
8383 if (objfile->separate_debug_objfile_backlink)
8385 struct dwarf2_per_objfile *dpo_backlink
8386 = ((struct dwarf2_per_objfile *)
8387 objfile_data (objfile->separate_debug_objfile_backlink,
8388 dwarf2_objfile_data_key));
8390 dwarf2_per_objfile->has_section_at_zero
8391 = dpo_backlink->has_section_at_zero;
8394 dwarf2_per_objfile->reading_partial_symbols = 0;
8396 psymtab_to_symtab_1 (self);
8398 /* Finish up the debug error message. */
8400 printf_filtered (_("done.\n"));
8403 process_cu_includes ();
8406 /* Reading in full CUs. */
8408 /* Add PER_CU to the queue. */
8411 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8412 enum language pretend_language)
8414 struct dwarf2_queue_item *item;
8417 item = XNEW (struct dwarf2_queue_item);
8418 item->per_cu = per_cu;
8419 item->pretend_language = pretend_language;
8422 if (dwarf2_queue == NULL)
8423 dwarf2_queue = item;
8425 dwarf2_queue_tail->next = item;
8427 dwarf2_queue_tail = item;
8430 /* If PER_CU is not yet queued, add it to the queue.
8431 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8433 The result is non-zero if PER_CU was queued, otherwise the result is zero
8434 meaning either PER_CU is already queued or it is already loaded.
8436 N.B. There is an invariant here that if a CU is queued then it is loaded.
8437 The caller is required to load PER_CU if we return non-zero. */
8440 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8441 struct dwarf2_per_cu_data *per_cu,
8442 enum language pretend_language)
8444 /* We may arrive here during partial symbol reading, if we need full
8445 DIEs to process an unusual case (e.g. template arguments). Do
8446 not queue PER_CU, just tell our caller to load its DIEs. */
8447 if (dwarf2_per_objfile->reading_partial_symbols)
8449 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8454 /* Mark the dependence relation so that we don't flush PER_CU
8456 if (dependent_cu != NULL)
8457 dwarf2_add_dependence (dependent_cu, per_cu);
8459 /* If it's already on the queue, we have nothing to do. */
8463 /* If the compilation unit is already loaded, just mark it as
8465 if (per_cu->cu != NULL)
8467 per_cu->cu->last_used = 0;
8471 /* Add it to the queue. */
8472 queue_comp_unit (per_cu, pretend_language);
8477 /* Process the queue. */
8480 process_queue (void)
8482 struct dwarf2_queue_item *item, *next_item;
8484 if (dwarf_read_debug)
8486 fprintf_unfiltered (gdb_stdlog,
8487 "Expanding one or more symtabs of objfile %s ...\n",
8488 objfile_name (dwarf2_per_objfile->objfile));
8491 /* The queue starts out with one item, but following a DIE reference
8492 may load a new CU, adding it to the end of the queue. */
8493 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8495 if ((dwarf2_per_objfile->using_index
8496 ? !item->per_cu->v.quick->compunit_symtab
8497 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8498 /* Skip dummy CUs. */
8499 && item->per_cu->cu != NULL)
8501 struct dwarf2_per_cu_data *per_cu = item->per_cu;
8502 unsigned int debug_print_threshold;
8505 if (per_cu->is_debug_types)
8507 struct signatured_type *sig_type =
8508 (struct signatured_type *) per_cu;
8510 sprintf (buf, "TU %s at offset 0x%x",
8511 hex_string (sig_type->signature),
8512 to_underlying (per_cu->sect_off));
8513 /* There can be 100s of TUs.
8514 Only print them in verbose mode. */
8515 debug_print_threshold = 2;
8519 sprintf (buf, "CU at offset 0x%x",
8520 to_underlying (per_cu->sect_off));
8521 debug_print_threshold = 1;
8524 if (dwarf_read_debug >= debug_print_threshold)
8525 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8527 if (per_cu->is_debug_types)
8528 process_full_type_unit (per_cu, item->pretend_language);
8530 process_full_comp_unit (per_cu, item->pretend_language);
8532 if (dwarf_read_debug >= debug_print_threshold)
8533 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8536 item->per_cu->queued = 0;
8537 next_item = item->next;
8541 dwarf2_queue_tail = NULL;
8543 if (dwarf_read_debug)
8545 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8546 objfile_name (dwarf2_per_objfile->objfile));
8550 /* Free all allocated queue entries. This function only releases anything if
8551 an error was thrown; if the queue was processed then it would have been
8552 freed as we went along. */
8555 dwarf2_release_queue (void *dummy)
8557 struct dwarf2_queue_item *item, *last;
8559 item = dwarf2_queue;
8562 /* Anything still marked queued is likely to be in an
8563 inconsistent state, so discard it. */
8564 if (item->per_cu->queued)
8566 if (item->per_cu->cu != NULL)
8567 free_one_cached_comp_unit (item->per_cu);
8568 item->per_cu->queued = 0;
8576 dwarf2_queue = dwarf2_queue_tail = NULL;
8579 /* Read in full symbols for PST, and anything it depends on. */
8582 psymtab_to_symtab_1 (struct partial_symtab *pst)
8584 struct dwarf2_per_cu_data *per_cu;
8590 for (i = 0; i < pst->number_of_dependencies; i++)
8591 if (!pst->dependencies[i]->readin
8592 && pst->dependencies[i]->user == NULL)
8594 /* Inform about additional files that need to be read in. */
8597 /* FIXME: i18n: Need to make this a single string. */
8598 fputs_filtered (" ", gdb_stdout);
8600 fputs_filtered ("and ", gdb_stdout);
8602 printf_filtered ("%s...", pst->dependencies[i]->filename);
8603 wrap_here (""); /* Flush output. */
8604 gdb_flush (gdb_stdout);
8606 psymtab_to_symtab_1 (pst->dependencies[i]);
8609 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
8613 /* It's an include file, no symbols to read for it.
8614 Everything is in the parent symtab. */
8619 dw2_do_instantiate_symtab (per_cu);
8622 /* Trivial hash function for die_info: the hash value of a DIE
8623 is its offset in .debug_info for this objfile. */
8626 die_hash (const void *item)
8628 const struct die_info *die = (const struct die_info *) item;
8630 return to_underlying (die->sect_off);
8633 /* Trivial comparison function for die_info structures: two DIEs
8634 are equal if they have the same offset. */
8637 die_eq (const void *item_lhs, const void *item_rhs)
8639 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8640 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8642 return die_lhs->sect_off == die_rhs->sect_off;
8645 /* die_reader_func for load_full_comp_unit.
8646 This is identical to read_signatured_type_reader,
8647 but is kept separate for now. */
8650 load_full_comp_unit_reader (const struct die_reader_specs *reader,
8651 const gdb_byte *info_ptr,
8652 struct die_info *comp_unit_die,
8656 struct dwarf2_cu *cu = reader->cu;
8657 enum language *language_ptr = (enum language *) data;
8659 gdb_assert (cu->die_hash == NULL);
8661 htab_create_alloc_ex (cu->header.length / 12,
8665 &cu->comp_unit_obstack,
8666 hashtab_obstack_allocate,
8667 dummy_obstack_deallocate);
8670 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8671 &info_ptr, comp_unit_die);
8672 cu->dies = comp_unit_die;
8673 /* comp_unit_die is not stored in die_hash, no need. */
8675 /* We try not to read any attributes in this function, because not
8676 all CUs needed for references have been loaded yet, and symbol
8677 table processing isn't initialized. But we have to set the CU language,
8678 or we won't be able to build types correctly.
8679 Similarly, if we do not read the producer, we can not apply
8680 producer-specific interpretation. */
8681 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
8684 /* Load the DIEs associated with PER_CU into memory. */
8687 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8688 enum language pretend_language)
8690 gdb_assert (! this_cu->is_debug_types);
8692 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8693 load_full_comp_unit_reader, &pretend_language);
8696 /* Add a DIE to the delayed physname list. */
8699 add_to_method_list (struct type *type, int fnfield_index, int index,
8700 const char *name, struct die_info *die,
8701 struct dwarf2_cu *cu)
8703 struct delayed_method_info mi;
8705 mi.fnfield_index = fnfield_index;
8709 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8712 /* A cleanup for freeing the delayed method list. */
8715 free_delayed_list (void *ptr)
8717 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8718 if (cu->method_list != NULL)
8720 VEC_free (delayed_method_info, cu->method_list);
8721 cu->method_list = NULL;
8725 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8726 "const" / "volatile". If so, decrements LEN by the length of the
8727 modifier and return true. Otherwise return false. */
8731 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8733 size_t mod_len = sizeof (mod) - 1;
8734 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8742 /* Compute the physnames of any methods on the CU's method list.
8744 The computation of method physnames is delayed in order to avoid the
8745 (bad) condition that one of the method's formal parameters is of an as yet
8749 compute_delayed_physnames (struct dwarf2_cu *cu)
8752 struct delayed_method_info *mi;
8754 /* Only C++ delays computing physnames. */
8755 if (VEC_empty (delayed_method_info, cu->method_list))
8757 gdb_assert (cu->language == language_cplus);
8759 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8761 const char *physname;
8762 struct fn_fieldlist *fn_flp
8763 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8764 physname = dwarf2_physname (mi->name, mi->die, cu);
8765 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8766 = physname ? physname : "";
8768 /* Since there's no tag to indicate whether a method is a
8769 const/volatile overload, extract that information out of the
8771 if (physname != NULL)
8773 size_t len = strlen (physname);
8777 if (physname[len] == ')') /* shortcut */
8779 else if (check_modifier (physname, len, " const"))
8780 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8781 else if (check_modifier (physname, len, " volatile"))
8782 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8790 /* Go objects should be embedded in a DW_TAG_module DIE,
8791 and it's not clear if/how imported objects will appear.
8792 To keep Go support simple until that's worked out,
8793 go back through what we've read and create something usable.
8794 We could do this while processing each DIE, and feels kinda cleaner,
8795 but that way is more invasive.
8796 This is to, for example, allow the user to type "p var" or "b main"
8797 without having to specify the package name, and allow lookups
8798 of module.object to work in contexts that use the expression
8802 fixup_go_packaging (struct dwarf2_cu *cu)
8804 char *package_name = NULL;
8805 struct pending *list;
8808 for (list = global_symbols; list != NULL; list = list->next)
8810 for (i = 0; i < list->nsyms; ++i)
8812 struct symbol *sym = list->symbol[i];
8814 if (SYMBOL_LANGUAGE (sym) == language_go
8815 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8817 char *this_package_name = go_symbol_package_name (sym);
8819 if (this_package_name == NULL)
8821 if (package_name == NULL)
8822 package_name = this_package_name;
8825 if (strcmp (package_name, this_package_name) != 0)
8826 complaint (&symfile_complaints,
8827 _("Symtab %s has objects from two different Go packages: %s and %s"),
8828 (symbol_symtab (sym) != NULL
8829 ? symtab_to_filename_for_display
8830 (symbol_symtab (sym))
8831 : objfile_name (cu->objfile)),
8832 this_package_name, package_name);
8833 xfree (this_package_name);
8839 if (package_name != NULL)
8841 struct objfile *objfile = cu->objfile;
8842 const char *saved_package_name
8843 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8845 strlen (package_name));
8846 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8847 saved_package_name);
8850 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8852 sym = allocate_symbol (objfile);
8853 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8854 SYMBOL_SET_NAMES (sym, saved_package_name,
8855 strlen (saved_package_name), 0, objfile);
8856 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8857 e.g., "main" finds the "main" module and not C's main(). */
8858 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8859 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8860 SYMBOL_TYPE (sym) = type;
8862 add_symbol_to_list (sym, &global_symbols);
8864 xfree (package_name);
8868 /* Return the symtab for PER_CU. This works properly regardless of
8869 whether we're using the index or psymtabs. */
8871 static struct compunit_symtab *
8872 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8874 return (dwarf2_per_objfile->using_index
8875 ? per_cu->v.quick->compunit_symtab
8876 : per_cu->v.psymtab->compunit_symtab);
8879 /* A helper function for computing the list of all symbol tables
8880 included by PER_CU. */
8883 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8884 htab_t all_children, htab_t all_type_symtabs,
8885 struct dwarf2_per_cu_data *per_cu,
8886 struct compunit_symtab *immediate_parent)
8890 struct compunit_symtab *cust;
8891 struct dwarf2_per_cu_data *iter;
8893 slot = htab_find_slot (all_children, per_cu, INSERT);
8896 /* This inclusion and its children have been processed. */
8901 /* Only add a CU if it has a symbol table. */
8902 cust = get_compunit_symtab (per_cu);
8905 /* If this is a type unit only add its symbol table if we haven't
8906 seen it yet (type unit per_cu's can share symtabs). */
8907 if (per_cu->is_debug_types)
8909 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8913 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8914 if (cust->user == NULL)
8915 cust->user = immediate_parent;
8920 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8921 if (cust->user == NULL)
8922 cust->user = immediate_parent;
8927 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8930 recursively_compute_inclusions (result, all_children,
8931 all_type_symtabs, iter, cust);
8935 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8939 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8941 gdb_assert (! per_cu->is_debug_types);
8943 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8946 struct dwarf2_per_cu_data *per_cu_iter;
8947 struct compunit_symtab *compunit_symtab_iter;
8948 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8949 htab_t all_children, all_type_symtabs;
8950 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8952 /* If we don't have a symtab, we can just skip this case. */
8956 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8957 NULL, xcalloc, xfree);
8958 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8959 NULL, xcalloc, xfree);
8962 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8966 recursively_compute_inclusions (&result_symtabs, all_children,
8967 all_type_symtabs, per_cu_iter,
8971 /* Now we have a transitive closure of all the included symtabs. */
8972 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8974 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8975 struct compunit_symtab *, len + 1);
8977 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8978 compunit_symtab_iter);
8980 cust->includes[ix] = compunit_symtab_iter;
8981 cust->includes[len] = NULL;
8983 VEC_free (compunit_symtab_ptr, result_symtabs);
8984 htab_delete (all_children);
8985 htab_delete (all_type_symtabs);
8989 /* Compute the 'includes' field for the symtabs of all the CUs we just
8993 process_cu_includes (void)
8996 struct dwarf2_per_cu_data *iter;
8999 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9003 if (! iter->is_debug_types)
9004 compute_compunit_symtab_includes (iter);
9007 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9010 /* Generate full symbol information for PER_CU, whose DIEs have
9011 already been loaded into memory. */
9014 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9015 enum language pretend_language)
9017 struct dwarf2_cu *cu = per_cu->cu;
9018 struct objfile *objfile = per_cu->objfile;
9019 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9020 CORE_ADDR lowpc, highpc;
9021 struct compunit_symtab *cust;
9022 struct cleanup *delayed_list_cleanup;
9024 struct block *static_block;
9027 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9030 scoped_free_pendings free_pending;
9031 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9033 cu->list_in_scope = &file_symbols;
9035 cu->language = pretend_language;
9036 cu->language_defn = language_def (cu->language);
9038 /* Do line number decoding in read_file_scope () */
9039 process_die (cu->dies, cu);
9041 /* For now fudge the Go package. */
9042 if (cu->language == language_go)
9043 fixup_go_packaging (cu);
9045 /* Now that we have processed all the DIEs in the CU, all the types
9046 should be complete, and it should now be safe to compute all of the
9048 compute_delayed_physnames (cu);
9049 do_cleanups (delayed_list_cleanup);
9051 /* Some compilers don't define a DW_AT_high_pc attribute for the
9052 compilation unit. If the DW_AT_high_pc is missing, synthesize
9053 it, by scanning the DIE's below the compilation unit. */
9054 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9056 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9057 static_block = end_symtab_get_static_block (addr, 0, 1);
9059 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9060 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9061 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9062 addrmap to help ensure it has an accurate map of pc values belonging to
9064 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9066 cust = end_symtab_from_static_block (static_block,
9067 SECT_OFF_TEXT (objfile), 0);
9071 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9073 /* Set symtab language to language from DW_AT_language. If the
9074 compilation is from a C file generated by language preprocessors, do
9075 not set the language if it was already deduced by start_subfile. */
9076 if (!(cu->language == language_c
9077 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9078 COMPUNIT_FILETABS (cust)->language = cu->language;
9080 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9081 produce DW_AT_location with location lists but it can be possibly
9082 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9083 there were bugs in prologue debug info, fixed later in GCC-4.5
9084 by "unwind info for epilogues" patch (which is not directly related).
9086 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9087 needed, it would be wrong due to missing DW_AT_producer there.
9089 Still one can confuse GDB by using non-standard GCC compilation
9090 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9092 if (cu->has_loclist && gcc_4_minor >= 5)
9093 cust->locations_valid = 1;
9095 if (gcc_4_minor >= 5)
9096 cust->epilogue_unwind_valid = 1;
9098 cust->call_site_htab = cu->call_site_htab;
9101 if (dwarf2_per_objfile->using_index)
9102 per_cu->v.quick->compunit_symtab = cust;
9105 struct partial_symtab *pst = per_cu->v.psymtab;
9106 pst->compunit_symtab = cust;
9110 /* Push it for inclusion processing later. */
9111 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
9114 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9115 already been loaded into memory. */
9118 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9119 enum language pretend_language)
9121 struct dwarf2_cu *cu = per_cu->cu;
9122 struct objfile *objfile = per_cu->objfile;
9123 struct compunit_symtab *cust;
9124 struct cleanup *delayed_list_cleanup;
9125 struct signatured_type *sig_type;
9127 gdb_assert (per_cu->is_debug_types);
9128 sig_type = (struct signatured_type *) per_cu;
9131 scoped_free_pendings free_pending;
9132 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9134 cu->list_in_scope = &file_symbols;
9136 cu->language = pretend_language;
9137 cu->language_defn = language_def (cu->language);
9139 /* The symbol tables are set up in read_type_unit_scope. */
9140 process_die (cu->dies, cu);
9142 /* For now fudge the Go package. */
9143 if (cu->language == language_go)
9144 fixup_go_packaging (cu);
9146 /* Now that we have processed all the DIEs in the CU, all the types
9147 should be complete, and it should now be safe to compute all of the
9149 compute_delayed_physnames (cu);
9150 do_cleanups (delayed_list_cleanup);
9152 /* TUs share symbol tables.
9153 If this is the first TU to use this symtab, complete the construction
9154 of it with end_expandable_symtab. Otherwise, complete the addition of
9155 this TU's symbols to the existing symtab. */
9156 if (sig_type->type_unit_group->compunit_symtab == NULL)
9158 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9159 sig_type->type_unit_group->compunit_symtab = cust;
9163 /* Set symtab language to language from DW_AT_language. If the
9164 compilation is from a C file generated by language preprocessors,
9165 do not set the language if it was already deduced by
9167 if (!(cu->language == language_c
9168 && COMPUNIT_FILETABS (cust)->language != language_c))
9169 COMPUNIT_FILETABS (cust)->language = cu->language;
9174 augment_type_symtab ();
9175 cust = sig_type->type_unit_group->compunit_symtab;
9178 if (dwarf2_per_objfile->using_index)
9179 per_cu->v.quick->compunit_symtab = cust;
9182 struct partial_symtab *pst = per_cu->v.psymtab;
9183 pst->compunit_symtab = cust;
9188 /* Process an imported unit DIE. */
9191 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9193 struct attribute *attr;
9195 /* For now we don't handle imported units in type units. */
9196 if (cu->per_cu->is_debug_types)
9198 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9199 " supported in type units [in module %s]"),
9200 objfile_name (cu->objfile));
9203 attr = dwarf2_attr (die, DW_AT_import, cu);
9206 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9207 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9208 dwarf2_per_cu_data *per_cu
9209 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
9211 /* If necessary, add it to the queue and load its DIEs. */
9212 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9213 load_full_comp_unit (per_cu, cu->language);
9215 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
9220 /* RAII object that represents a process_die scope: i.e.,
9221 starts/finishes processing a DIE. */
9222 class process_die_scope
9225 process_die_scope (die_info *die, dwarf2_cu *cu)
9226 : m_die (die), m_cu (cu)
9228 /* We should only be processing DIEs not already in process. */
9229 gdb_assert (!m_die->in_process);
9230 m_die->in_process = true;
9233 ~process_die_scope ()
9235 m_die->in_process = false;
9237 /* If we're done processing the DIE for the CU that owns the line
9238 header, we don't need the line header anymore. */
9239 if (m_cu->line_header_die_owner == m_die)
9241 delete m_cu->line_header;
9242 m_cu->line_header = NULL;
9243 m_cu->line_header_die_owner = NULL;
9252 /* Process a die and its children. */
9255 process_die (struct die_info *die, struct dwarf2_cu *cu)
9257 process_die_scope scope (die, cu);
9261 case DW_TAG_padding:
9263 case DW_TAG_compile_unit:
9264 case DW_TAG_partial_unit:
9265 read_file_scope (die, cu);
9267 case DW_TAG_type_unit:
9268 read_type_unit_scope (die, cu);
9270 case DW_TAG_subprogram:
9271 case DW_TAG_inlined_subroutine:
9272 read_func_scope (die, cu);
9274 case DW_TAG_lexical_block:
9275 case DW_TAG_try_block:
9276 case DW_TAG_catch_block:
9277 read_lexical_block_scope (die, cu);
9279 case DW_TAG_call_site:
9280 case DW_TAG_GNU_call_site:
9281 read_call_site_scope (die, cu);
9283 case DW_TAG_class_type:
9284 case DW_TAG_interface_type:
9285 case DW_TAG_structure_type:
9286 case DW_TAG_union_type:
9287 process_structure_scope (die, cu);
9289 case DW_TAG_enumeration_type:
9290 process_enumeration_scope (die, cu);
9293 /* These dies have a type, but processing them does not create
9294 a symbol or recurse to process the children. Therefore we can
9295 read them on-demand through read_type_die. */
9296 case DW_TAG_subroutine_type:
9297 case DW_TAG_set_type:
9298 case DW_TAG_array_type:
9299 case DW_TAG_pointer_type:
9300 case DW_TAG_ptr_to_member_type:
9301 case DW_TAG_reference_type:
9302 case DW_TAG_rvalue_reference_type:
9303 case DW_TAG_string_type:
9306 case DW_TAG_base_type:
9307 case DW_TAG_subrange_type:
9308 case DW_TAG_typedef:
9309 /* Add a typedef symbol for the type definition, if it has a
9311 new_symbol (die, read_type_die (die, cu), cu);
9313 case DW_TAG_common_block:
9314 read_common_block (die, cu);
9316 case DW_TAG_common_inclusion:
9318 case DW_TAG_namespace:
9319 cu->processing_has_namespace_info = 1;
9320 read_namespace (die, cu);
9323 cu->processing_has_namespace_info = 1;
9324 read_module (die, cu);
9326 case DW_TAG_imported_declaration:
9327 cu->processing_has_namespace_info = 1;
9328 if (read_namespace_alias (die, cu))
9330 /* The declaration is not a global namespace alias: fall through. */
9331 case DW_TAG_imported_module:
9332 cu->processing_has_namespace_info = 1;
9333 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9334 || cu->language != language_fortran))
9335 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9336 dwarf_tag_name (die->tag));
9337 read_import_statement (die, cu);
9340 case DW_TAG_imported_unit:
9341 process_imported_unit_die (die, cu);
9344 case DW_TAG_variable:
9345 read_variable (die, cu);
9349 new_symbol (die, NULL, cu);
9354 /* DWARF name computation. */
9356 /* A helper function for dwarf2_compute_name which determines whether DIE
9357 needs to have the name of the scope prepended to the name listed in the
9361 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9363 struct attribute *attr;
9367 case DW_TAG_namespace:
9368 case DW_TAG_typedef:
9369 case DW_TAG_class_type:
9370 case DW_TAG_interface_type:
9371 case DW_TAG_structure_type:
9372 case DW_TAG_union_type:
9373 case DW_TAG_enumeration_type:
9374 case DW_TAG_enumerator:
9375 case DW_TAG_subprogram:
9376 case DW_TAG_inlined_subroutine:
9378 case DW_TAG_imported_declaration:
9381 case DW_TAG_variable:
9382 case DW_TAG_constant:
9383 /* We only need to prefix "globally" visible variables. These include
9384 any variable marked with DW_AT_external or any variable that
9385 lives in a namespace. [Variables in anonymous namespaces
9386 require prefixing, but they are not DW_AT_external.] */
9388 if (dwarf2_attr (die, DW_AT_specification, cu))
9390 struct dwarf2_cu *spec_cu = cu;
9392 return die_needs_namespace (die_specification (die, &spec_cu),
9396 attr = dwarf2_attr (die, DW_AT_external, cu);
9397 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9398 && die->parent->tag != DW_TAG_module)
9400 /* A variable in a lexical block of some kind does not need a
9401 namespace, even though in C++ such variables may be external
9402 and have a mangled name. */
9403 if (die->parent->tag == DW_TAG_lexical_block
9404 || die->parent->tag == DW_TAG_try_block
9405 || die->parent->tag == DW_TAG_catch_block
9406 || die->parent->tag == DW_TAG_subprogram)
9415 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9416 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9417 defined for the given DIE. */
9419 static struct attribute *
9420 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9422 struct attribute *attr;
9424 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9426 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9431 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9432 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9433 defined for the given DIE. */
9436 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9438 const char *linkage_name;
9440 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9441 if (linkage_name == NULL)
9442 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9444 return linkage_name;
9447 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9448 compute the physname for the object, which include a method's:
9449 - formal parameters (C++),
9450 - receiver type (Go),
9452 The term "physname" is a bit confusing.
9453 For C++, for example, it is the demangled name.
9454 For Go, for example, it's the mangled name.
9456 For Ada, return the DIE's linkage name rather than the fully qualified
9457 name. PHYSNAME is ignored..
9459 The result is allocated on the objfile_obstack and canonicalized. */
9462 dwarf2_compute_name (const char *name,
9463 struct die_info *die, struct dwarf2_cu *cu,
9466 struct objfile *objfile = cu->objfile;
9469 name = dwarf2_name (die, cu);
9471 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9472 but otherwise compute it by typename_concat inside GDB.
9473 FIXME: Actually this is not really true, or at least not always true.
9474 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9475 Fortran names because there is no mangling standard. So new_symbol_full
9476 will set the demangled name to the result of dwarf2_full_name, and it is
9477 the demangled name that GDB uses if it exists. */
9478 if (cu->language == language_ada
9479 || (cu->language == language_fortran && physname))
9481 /* For Ada unit, we prefer the linkage name over the name, as
9482 the former contains the exported name, which the user expects
9483 to be able to reference. Ideally, we want the user to be able
9484 to reference this entity using either natural or linkage name,
9485 but we haven't started looking at this enhancement yet. */
9486 const char *linkage_name = dw2_linkage_name (die, cu);
9488 if (linkage_name != NULL)
9489 return linkage_name;
9492 /* These are the only languages we know how to qualify names in. */
9494 && (cu->language == language_cplus
9495 || cu->language == language_fortran || cu->language == language_d
9496 || cu->language == language_rust))
9498 if (die_needs_namespace (die, cu))
9502 const char *canonical_name = NULL;
9506 prefix = determine_prefix (die, cu);
9507 if (*prefix != '\0')
9509 char *prefixed_name = typename_concat (NULL, prefix, name,
9512 buf.puts (prefixed_name);
9513 xfree (prefixed_name);
9518 /* Template parameters may be specified in the DIE's DW_AT_name, or
9519 as children with DW_TAG_template_type_param or
9520 DW_TAG_value_type_param. If the latter, add them to the name
9521 here. If the name already has template parameters, then
9522 skip this step; some versions of GCC emit both, and
9523 it is more efficient to use the pre-computed name.
9525 Something to keep in mind about this process: it is very
9526 unlikely, or in some cases downright impossible, to produce
9527 something that will match the mangled name of a function.
9528 If the definition of the function has the same debug info,
9529 we should be able to match up with it anyway. But fallbacks
9530 using the minimal symbol, for instance to find a method
9531 implemented in a stripped copy of libstdc++, will not work.
9532 If we do not have debug info for the definition, we will have to
9533 match them up some other way.
9535 When we do name matching there is a related problem with function
9536 templates; two instantiated function templates are allowed to
9537 differ only by their return types, which we do not add here. */
9539 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9541 struct attribute *attr;
9542 struct die_info *child;
9545 die->building_fullname = 1;
9547 for (child = die->child; child != NULL; child = child->sibling)
9551 const gdb_byte *bytes;
9552 struct dwarf2_locexpr_baton *baton;
9555 if (child->tag != DW_TAG_template_type_param
9556 && child->tag != DW_TAG_template_value_param)
9567 attr = dwarf2_attr (child, DW_AT_type, cu);
9570 complaint (&symfile_complaints,
9571 _("template parameter missing DW_AT_type"));
9572 buf.puts ("UNKNOWN_TYPE");
9575 type = die_type (child, cu);
9577 if (child->tag == DW_TAG_template_type_param)
9579 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
9583 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9586 complaint (&symfile_complaints,
9587 _("template parameter missing "
9588 "DW_AT_const_value"));
9589 buf.puts ("UNKNOWN_VALUE");
9593 dwarf2_const_value_attr (attr, type, name,
9594 &cu->comp_unit_obstack, cu,
9595 &value, &bytes, &baton);
9597 if (TYPE_NOSIGN (type))
9598 /* GDB prints characters as NUMBER 'CHAR'. If that's
9599 changed, this can use value_print instead. */
9600 c_printchar (value, type, &buf);
9603 struct value_print_options opts;
9606 v = dwarf2_evaluate_loc_desc (type, NULL,
9610 else if (bytes != NULL)
9612 v = allocate_value (type);
9613 memcpy (value_contents_writeable (v), bytes,
9614 TYPE_LENGTH (type));
9617 v = value_from_longest (type, value);
9619 /* Specify decimal so that we do not depend on
9621 get_formatted_print_options (&opts, 'd');
9623 value_print (v, &buf, &opts);
9629 die->building_fullname = 0;
9633 /* Close the argument list, with a space if necessary
9634 (nested templates). */
9635 if (!buf.empty () && buf.string ().back () == '>')
9642 /* For C++ methods, append formal parameter type
9643 information, if PHYSNAME. */
9645 if (physname && die->tag == DW_TAG_subprogram
9646 && cu->language == language_cplus)
9648 struct type *type = read_type_die (die, cu);
9650 c_type_print_args (type, &buf, 1, cu->language,
9651 &type_print_raw_options);
9653 if (cu->language == language_cplus)
9655 /* Assume that an artificial first parameter is
9656 "this", but do not crash if it is not. RealView
9657 marks unnamed (and thus unused) parameters as
9658 artificial; there is no way to differentiate
9660 if (TYPE_NFIELDS (type) > 0
9661 && TYPE_FIELD_ARTIFICIAL (type, 0)
9662 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
9663 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9665 buf.puts (" const");
9669 const std::string &intermediate_name = buf.string ();
9671 if (cu->language == language_cplus)
9673 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9674 &objfile->per_bfd->storage_obstack);
9676 /* If we only computed INTERMEDIATE_NAME, or if
9677 INTERMEDIATE_NAME is already canonical, then we need to
9678 copy it to the appropriate obstack. */
9679 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9680 name = ((const char *)
9681 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9682 intermediate_name.c_str (),
9683 intermediate_name.length ()));
9685 name = canonical_name;
9692 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9693 If scope qualifiers are appropriate they will be added. The result
9694 will be allocated on the storage_obstack, or NULL if the DIE does
9695 not have a name. NAME may either be from a previous call to
9696 dwarf2_name or NULL.
9698 The output string will be canonicalized (if C++). */
9701 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9703 return dwarf2_compute_name (name, die, cu, 0);
9706 /* Construct a physname for the given DIE in CU. NAME may either be
9707 from a previous call to dwarf2_name or NULL. The result will be
9708 allocated on the objfile_objstack or NULL if the DIE does not have a
9711 The output string will be canonicalized (if C++). */
9714 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9716 struct objfile *objfile = cu->objfile;
9717 const char *retval, *mangled = NULL, *canon = NULL;
9720 /* In this case dwarf2_compute_name is just a shortcut not building anything
9722 if (!die_needs_namespace (die, cu))
9723 return dwarf2_compute_name (name, die, cu, 1);
9725 mangled = dw2_linkage_name (die, cu);
9727 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9728 See https://github.com/rust-lang/rust/issues/32925. */
9729 if (cu->language == language_rust && mangled != NULL
9730 && strchr (mangled, '{') != NULL)
9733 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9735 gdb::unique_xmalloc_ptr<char> demangled;
9736 if (mangled != NULL)
9738 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9739 type. It is easier for GDB users to search for such functions as
9740 `name(params)' than `long name(params)'. In such case the minimal
9741 symbol names do not match the full symbol names but for template
9742 functions there is never a need to look up their definition from their
9743 declaration so the only disadvantage remains the minimal symbol
9744 variant `long name(params)' does not have the proper inferior type.
9747 if (cu->language == language_go)
9749 /* This is a lie, but we already lie to the caller new_symbol_full.
9750 new_symbol_full assumes we return the mangled name.
9751 This just undoes that lie until things are cleaned up. */
9755 demangled.reset (gdb_demangle (mangled,
9756 (DMGL_PARAMS | DMGL_ANSI
9760 canon = demangled.get ();
9768 if (canon == NULL || check_physname)
9770 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9772 if (canon != NULL && strcmp (physname, canon) != 0)
9774 /* It may not mean a bug in GDB. The compiler could also
9775 compute DW_AT_linkage_name incorrectly. But in such case
9776 GDB would need to be bug-to-bug compatible. */
9778 complaint (&symfile_complaints,
9779 _("Computed physname <%s> does not match demangled <%s> "
9780 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9781 physname, canon, mangled, to_underlying (die->sect_off),
9782 objfile_name (objfile));
9784 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9785 is available here - over computed PHYSNAME. It is safer
9786 against both buggy GDB and buggy compilers. */
9800 retval = ((const char *)
9801 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9802 retval, strlen (retval)));
9807 /* Inspect DIE in CU for a namespace alias. If one exists, record
9808 a new symbol for it.
9810 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9813 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9815 struct attribute *attr;
9817 /* If the die does not have a name, this is not a namespace
9819 attr = dwarf2_attr (die, DW_AT_name, cu);
9823 struct die_info *d = die;
9824 struct dwarf2_cu *imported_cu = cu;
9826 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9827 keep inspecting DIEs until we hit the underlying import. */
9828 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9829 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9831 attr = dwarf2_attr (d, DW_AT_import, cu);
9835 d = follow_die_ref (d, attr, &imported_cu);
9836 if (d->tag != DW_TAG_imported_declaration)
9840 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9842 complaint (&symfile_complaints,
9843 _("DIE at 0x%x has too many recursively imported "
9844 "declarations"), to_underlying (d->sect_off));
9851 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9853 type = get_die_type_at_offset (sect_off, cu->per_cu);
9854 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9856 /* This declaration is a global namespace alias. Add
9857 a symbol for it whose type is the aliased namespace. */
9858 new_symbol (die, type, cu);
9867 /* Return the using directives repository (global or local?) to use in the
9868 current context for LANGUAGE.
9870 For Ada, imported declarations can materialize renamings, which *may* be
9871 global. However it is impossible (for now?) in DWARF to distinguish
9872 "external" imported declarations and "static" ones. As all imported
9873 declarations seem to be static in all other languages, make them all CU-wide
9874 global only in Ada. */
9876 static struct using_direct **
9877 using_directives (enum language language)
9879 if (language == language_ada && context_stack_depth == 0)
9880 return &global_using_directives;
9882 return &local_using_directives;
9885 /* Read the import statement specified by the given die and record it. */
9888 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9890 struct objfile *objfile = cu->objfile;
9891 struct attribute *import_attr;
9892 struct die_info *imported_die, *child_die;
9893 struct dwarf2_cu *imported_cu;
9894 const char *imported_name;
9895 const char *imported_name_prefix;
9896 const char *canonical_name;
9897 const char *import_alias;
9898 const char *imported_declaration = NULL;
9899 const char *import_prefix;
9900 std::vector<const char *> excludes;
9902 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9903 if (import_attr == NULL)
9905 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9906 dwarf_tag_name (die->tag));
9911 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9912 imported_name = dwarf2_name (imported_die, imported_cu);
9913 if (imported_name == NULL)
9915 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9917 The import in the following code:
9931 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9932 <52> DW_AT_decl_file : 1
9933 <53> DW_AT_decl_line : 6
9934 <54> DW_AT_import : <0x75>
9935 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9937 <5b> DW_AT_decl_file : 1
9938 <5c> DW_AT_decl_line : 2
9939 <5d> DW_AT_type : <0x6e>
9941 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9942 <76> DW_AT_byte_size : 4
9943 <77> DW_AT_encoding : 5 (signed)
9945 imports the wrong die ( 0x75 instead of 0x58 ).
9946 This case will be ignored until the gcc bug is fixed. */
9950 /* Figure out the local name after import. */
9951 import_alias = dwarf2_name (die, cu);
9953 /* Figure out where the statement is being imported to. */
9954 import_prefix = determine_prefix (die, cu);
9956 /* Figure out what the scope of the imported die is and prepend it
9957 to the name of the imported die. */
9958 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9960 if (imported_die->tag != DW_TAG_namespace
9961 && imported_die->tag != DW_TAG_module)
9963 imported_declaration = imported_name;
9964 canonical_name = imported_name_prefix;
9966 else if (strlen (imported_name_prefix) > 0)
9967 canonical_name = obconcat (&objfile->objfile_obstack,
9968 imported_name_prefix,
9969 (cu->language == language_d ? "." : "::"),
9970 imported_name, (char *) NULL);
9972 canonical_name = imported_name;
9974 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9975 for (child_die = die->child; child_die && child_die->tag;
9976 child_die = sibling_die (child_die))
9978 /* DWARF-4: A Fortran use statement with a “rename list” may be
9979 represented by an imported module entry with an import attribute
9980 referring to the module and owned entries corresponding to those
9981 entities that are renamed as part of being imported. */
9983 if (child_die->tag != DW_TAG_imported_declaration)
9985 complaint (&symfile_complaints,
9986 _("child DW_TAG_imported_declaration expected "
9987 "- DIE at 0x%x [in module %s]"),
9988 to_underlying (child_die->sect_off), objfile_name (objfile));
9992 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9993 if (import_attr == NULL)
9995 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9996 dwarf_tag_name (child_die->tag));
10001 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10003 imported_name = dwarf2_name (imported_die, imported_cu);
10004 if (imported_name == NULL)
10006 complaint (&symfile_complaints,
10007 _("child DW_TAG_imported_declaration has unknown "
10008 "imported name - DIE at 0x%x [in module %s]"),
10009 to_underlying (child_die->sect_off), objfile_name (objfile));
10013 excludes.push_back (imported_name);
10015 process_die (child_die, cu);
10018 add_using_directive (using_directives (cu->language),
10022 imported_declaration,
10025 &objfile->objfile_obstack);
10028 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10029 types, but gives them a size of zero. Starting with version 14,
10030 ICC is compatible with GCC. */
10033 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10035 if (!cu->checked_producer)
10036 check_producer (cu);
10038 return cu->producer_is_icc_lt_14;
10041 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10042 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10043 this, it was first present in GCC release 4.3.0. */
10046 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10048 if (!cu->checked_producer)
10049 check_producer (cu);
10051 return cu->producer_is_gcc_lt_4_3;
10054 static file_and_directory
10055 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10057 file_and_directory res;
10059 /* Find the filename. Do not use dwarf2_name here, since the filename
10060 is not a source language identifier. */
10061 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10062 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10064 if (res.comp_dir == NULL
10065 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10066 && IS_ABSOLUTE_PATH (res.name))
10068 res.comp_dir_storage = ldirname (res.name);
10069 if (!res.comp_dir_storage.empty ())
10070 res.comp_dir = res.comp_dir_storage.c_str ();
10072 if (res.comp_dir != NULL)
10074 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10075 directory, get rid of it. */
10076 const char *cp = strchr (res.comp_dir, ':');
10078 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10079 res.comp_dir = cp + 1;
10082 if (res.name == NULL)
10083 res.name = "<unknown>";
10088 /* Handle DW_AT_stmt_list for a compilation unit.
10089 DIE is the DW_TAG_compile_unit die for CU.
10090 COMP_DIR is the compilation directory. LOWPC is passed to
10091 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10094 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10095 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10097 struct objfile *objfile = dwarf2_per_objfile->objfile;
10098 struct attribute *attr;
10099 struct line_header line_header_local;
10100 hashval_t line_header_local_hash;
10103 int decode_mapping;
10105 gdb_assert (! cu->per_cu->is_debug_types);
10107 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10111 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10113 /* The line header hash table is only created if needed (it exists to
10114 prevent redundant reading of the line table for partial_units).
10115 If we're given a partial_unit, we'll need it. If we're given a
10116 compile_unit, then use the line header hash table if it's already
10117 created, but don't create one just yet. */
10119 if (dwarf2_per_objfile->line_header_hash == NULL
10120 && die->tag == DW_TAG_partial_unit)
10122 dwarf2_per_objfile->line_header_hash
10123 = htab_create_alloc_ex (127, line_header_hash_voidp,
10124 line_header_eq_voidp,
10125 free_line_header_voidp,
10126 &objfile->objfile_obstack,
10127 hashtab_obstack_allocate,
10128 dummy_obstack_deallocate);
10131 line_header_local.sect_off = line_offset;
10132 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10133 line_header_local_hash = line_header_hash (&line_header_local);
10134 if (dwarf2_per_objfile->line_header_hash != NULL)
10136 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10137 &line_header_local,
10138 line_header_local_hash, NO_INSERT);
10140 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10141 is not present in *SLOT (since if there is something in *SLOT then
10142 it will be for a partial_unit). */
10143 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10145 gdb_assert (*slot != NULL);
10146 cu->line_header = (struct line_header *) *slot;
10151 /* dwarf_decode_line_header does not yet provide sufficient information.
10152 We always have to call also dwarf_decode_lines for it. */
10153 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10157 cu->line_header = lh.release ();
10158 cu->line_header_die_owner = die;
10160 if (dwarf2_per_objfile->line_header_hash == NULL)
10164 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10165 &line_header_local,
10166 line_header_local_hash, INSERT);
10167 gdb_assert (slot != NULL);
10169 if (slot != NULL && *slot == NULL)
10171 /* This newly decoded line number information unit will be owned
10172 by line_header_hash hash table. */
10173 *slot = cu->line_header;
10174 cu->line_header_die_owner = NULL;
10178 /* We cannot free any current entry in (*slot) as that struct line_header
10179 may be already used by multiple CUs. Create only temporary decoded
10180 line_header for this CU - it may happen at most once for each line
10181 number information unit. And if we're not using line_header_hash
10182 then this is what we want as well. */
10183 gdb_assert (die->tag != DW_TAG_partial_unit);
10185 decode_mapping = (die->tag != DW_TAG_partial_unit);
10186 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10191 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10194 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10196 struct objfile *objfile = dwarf2_per_objfile->objfile;
10197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10198 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10199 CORE_ADDR highpc = ((CORE_ADDR) 0);
10200 struct attribute *attr;
10201 struct die_info *child_die;
10202 CORE_ADDR baseaddr;
10204 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10206 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10208 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10209 from finish_block. */
10210 if (lowpc == ((CORE_ADDR) -1))
10212 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10214 file_and_directory fnd = find_file_and_directory (die, cu);
10216 prepare_one_comp_unit (cu, die, cu->language);
10218 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10219 standardised yet. As a workaround for the language detection we fall
10220 back to the DW_AT_producer string. */
10221 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10222 cu->language = language_opencl;
10224 /* Similar hack for Go. */
10225 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10226 set_cu_language (DW_LANG_Go, cu);
10228 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
10230 /* Decode line number information if present. We do this before
10231 processing child DIEs, so that the line header table is available
10232 for DW_AT_decl_file. */
10233 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10235 /* Process all dies in compilation unit. */
10236 if (die->child != NULL)
10238 child_die = die->child;
10239 while (child_die && child_die->tag)
10241 process_die (child_die, cu);
10242 child_die = sibling_die (child_die);
10246 /* Decode macro information, if present. Dwarf 2 macro information
10247 refers to information in the line number info statement program
10248 header, so we can only read it if we've read the header
10250 attr = dwarf2_attr (die, DW_AT_macros, cu);
10252 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10253 if (attr && cu->line_header)
10255 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10256 complaint (&symfile_complaints,
10257 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10259 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10263 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10264 if (attr && cu->line_header)
10266 unsigned int macro_offset = DW_UNSND (attr);
10268 dwarf_decode_macros (cu, macro_offset, 0);
10273 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10274 Create the set of symtabs used by this TU, or if this TU is sharing
10275 symtabs with another TU and the symtabs have already been created
10276 then restore those symtabs in the line header.
10277 We don't need the pc/line-number mapping for type units. */
10280 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
10282 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10283 struct type_unit_group *tu_group;
10285 struct attribute *attr;
10287 struct signatured_type *sig_type;
10289 gdb_assert (per_cu->is_debug_types);
10290 sig_type = (struct signatured_type *) per_cu;
10292 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10294 /* If we're using .gdb_index (includes -readnow) then
10295 per_cu->type_unit_group may not have been set up yet. */
10296 if (sig_type->type_unit_group == NULL)
10297 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10298 tu_group = sig_type->type_unit_group;
10300 /* If we've already processed this stmt_list there's no real need to
10301 do it again, we could fake it and just recreate the part we need
10302 (file name,index -> symtab mapping). If data shows this optimization
10303 is useful we can do it then. */
10304 first_time = tu_group->compunit_symtab == NULL;
10306 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10311 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10312 lh = dwarf_decode_line_header (line_offset, cu);
10317 dwarf2_start_symtab (cu, "", NULL, 0);
10320 gdb_assert (tu_group->symtabs == NULL);
10321 restart_symtab (tu_group->compunit_symtab, "", 0);
10326 cu->line_header = lh.release ();
10327 cu->line_header_die_owner = die;
10331 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
10333 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10334 still initializing it, and our caller (a few levels up)
10335 process_full_type_unit still needs to know if this is the first
10338 tu_group->num_symtabs = cu->line_header->file_names.size ();
10339 tu_group->symtabs = XNEWVEC (struct symtab *,
10340 cu->line_header->file_names.size ());
10342 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10344 file_entry &fe = cu->line_header->file_names[i];
10346 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
10348 if (current_subfile->symtab == NULL)
10350 /* NOTE: start_subfile will recognize when it's been
10351 passed a file it has already seen. So we can't
10352 assume there's a simple mapping from
10353 cu->line_header->file_names to subfiles, plus
10354 cu->line_header->file_names may contain dups. */
10355 current_subfile->symtab
10356 = allocate_symtab (cust, current_subfile->name);
10359 fe.symtab = current_subfile->symtab;
10360 tu_group->symtabs[i] = fe.symtab;
10365 restart_symtab (tu_group->compunit_symtab, "", 0);
10367 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10369 file_entry &fe = cu->line_header->file_names[i];
10371 fe.symtab = tu_group->symtabs[i];
10375 /* The main symtab is allocated last. Type units don't have DW_AT_name
10376 so they don't have a "real" (so to speak) symtab anyway.
10377 There is later code that will assign the main symtab to all symbols
10378 that don't have one. We need to handle the case of a symbol with a
10379 missing symtab (DW_AT_decl_file) anyway. */
10382 /* Process DW_TAG_type_unit.
10383 For TUs we want to skip the first top level sibling if it's not the
10384 actual type being defined by this TU. In this case the first top
10385 level sibling is there to provide context only. */
10388 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10390 struct die_info *child_die;
10392 prepare_one_comp_unit (cu, die, language_minimal);
10394 /* Initialize (or reinitialize) the machinery for building symtabs.
10395 We do this before processing child DIEs, so that the line header table
10396 is available for DW_AT_decl_file. */
10397 setup_type_unit_groups (die, cu);
10399 if (die->child != NULL)
10401 child_die = die->child;
10402 while (child_die && child_die->tag)
10404 process_die (child_die, cu);
10405 child_die = sibling_die (child_die);
10412 http://gcc.gnu.org/wiki/DebugFission
10413 http://gcc.gnu.org/wiki/DebugFissionDWP
10415 To simplify handling of both DWO files ("object" files with the DWARF info)
10416 and DWP files (a file with the DWOs packaged up into one file), we treat
10417 DWP files as having a collection of virtual DWO files. */
10420 hash_dwo_file (const void *item)
10422 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10425 hash = htab_hash_string (dwo_file->dwo_name);
10426 if (dwo_file->comp_dir != NULL)
10427 hash += htab_hash_string (dwo_file->comp_dir);
10432 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10434 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10435 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10437 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10439 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10440 return lhs->comp_dir == rhs->comp_dir;
10441 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10444 /* Allocate a hash table for DWO files. */
10447 allocate_dwo_file_hash_table (void)
10449 struct objfile *objfile = dwarf2_per_objfile->objfile;
10451 return htab_create_alloc_ex (41,
10455 &objfile->objfile_obstack,
10456 hashtab_obstack_allocate,
10457 dummy_obstack_deallocate);
10460 /* Lookup DWO file DWO_NAME. */
10463 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
10465 struct dwo_file find_entry;
10468 if (dwarf2_per_objfile->dwo_files == NULL)
10469 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10471 memset (&find_entry, 0, sizeof (find_entry));
10472 find_entry.dwo_name = dwo_name;
10473 find_entry.comp_dir = comp_dir;
10474 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10480 hash_dwo_unit (const void *item)
10482 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10484 /* This drops the top 32 bits of the id, but is ok for a hash. */
10485 return dwo_unit->signature;
10489 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10491 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10492 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10494 /* The signature is assumed to be unique within the DWO file.
10495 So while object file CU dwo_id's always have the value zero,
10496 that's OK, assuming each object file DWO file has only one CU,
10497 and that's the rule for now. */
10498 return lhs->signature == rhs->signature;
10501 /* Allocate a hash table for DWO CUs,TUs.
10502 There is one of these tables for each of CUs,TUs for each DWO file. */
10505 allocate_dwo_unit_table (struct objfile *objfile)
10507 /* Start out with a pretty small number.
10508 Generally DWO files contain only one CU and maybe some TUs. */
10509 return htab_create_alloc_ex (3,
10513 &objfile->objfile_obstack,
10514 hashtab_obstack_allocate,
10515 dummy_obstack_deallocate);
10518 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
10520 struct create_dwo_cu_data
10522 struct dwo_file *dwo_file;
10523 struct dwo_unit dwo_unit;
10526 /* die_reader_func for create_dwo_cu. */
10529 create_dwo_cu_reader (const struct die_reader_specs *reader,
10530 const gdb_byte *info_ptr,
10531 struct die_info *comp_unit_die,
10535 struct dwarf2_cu *cu = reader->cu;
10536 sect_offset sect_off = cu->per_cu->sect_off;
10537 struct dwarf2_section_info *section = cu->per_cu->section;
10538 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
10539 struct dwo_file *dwo_file = data->dwo_file;
10540 struct dwo_unit *dwo_unit = &data->dwo_unit;
10541 struct attribute *attr;
10543 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10546 complaint (&symfile_complaints,
10547 _("Dwarf Error: debug entry at offset 0x%x is missing"
10548 " its dwo_id [in module %s]"),
10549 to_underlying (sect_off), dwo_file->dwo_name);
10553 dwo_unit->dwo_file = dwo_file;
10554 dwo_unit->signature = DW_UNSND (attr);
10555 dwo_unit->section = section;
10556 dwo_unit->sect_off = sect_off;
10557 dwo_unit->length = cu->per_cu->length;
10559 if (dwarf_read_debug)
10560 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
10561 to_underlying (sect_off),
10562 hex_string (dwo_unit->signature));
10565 /* Create the dwo_units for the CUs in a DWO_FILE.
10566 Note: This function processes DWO files only, not DWP files. */
10569 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info §ion,
10572 struct objfile *objfile = dwarf2_per_objfile->objfile;
10573 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
10574 const gdb_byte *info_ptr, *end_ptr;
10576 dwarf2_read_section (objfile, §ion);
10577 info_ptr = section.buffer;
10579 if (info_ptr == NULL)
10582 if (dwarf_read_debug)
10584 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
10585 get_section_name (§ion),
10586 get_section_file_name (§ion));
10589 end_ptr = info_ptr + section.size;
10590 while (info_ptr < end_ptr)
10592 struct dwarf2_per_cu_data per_cu;
10593 struct create_dwo_cu_data create_dwo_cu_data;
10594 struct dwo_unit *dwo_unit;
10596 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10598 memset (&create_dwo_cu_data.dwo_unit, 0,
10599 sizeof (create_dwo_cu_data.dwo_unit));
10600 memset (&per_cu, 0, sizeof (per_cu));
10601 per_cu.objfile = objfile;
10602 per_cu.is_debug_types = 0;
10603 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10604 per_cu.section = §ion;
10605 create_dwo_cu_data.dwo_file = &dwo_file;
10607 init_cutu_and_read_dies_no_follow (
10608 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10609 info_ptr += per_cu.length;
10611 // If the unit could not be parsed, skip it.
10612 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10615 if (cus_htab == NULL)
10616 cus_htab = allocate_dwo_unit_table (objfile);
10618 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10619 *dwo_unit = create_dwo_cu_data.dwo_unit;
10620 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10621 gdb_assert (slot != NULL);
10624 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10625 sect_offset dup_sect_off = dup_cu->sect_off;
10627 complaint (&symfile_complaints,
10628 _("debug cu entry at offset 0x%x is duplicate to"
10629 " the entry at offset 0x%x, signature %s"),
10630 to_underlying (sect_off), to_underlying (dup_sect_off),
10631 hex_string (dwo_unit->signature));
10633 *slot = (void *)dwo_unit;
10637 /* DWP file .debug_{cu,tu}_index section format:
10638 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10642 Both index sections have the same format, and serve to map a 64-bit
10643 signature to a set of section numbers. Each section begins with a header,
10644 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10645 indexes, and a pool of 32-bit section numbers. The index sections will be
10646 aligned at 8-byte boundaries in the file.
10648 The index section header consists of:
10650 V, 32 bit version number
10652 N, 32 bit number of compilation units or type units in the index
10653 M, 32 bit number of slots in the hash table
10655 Numbers are recorded using the byte order of the application binary.
10657 The hash table begins at offset 16 in the section, and consists of an array
10658 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10659 order of the application binary). Unused slots in the hash table are 0.
10660 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10662 The parallel table begins immediately after the hash table
10663 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10664 array of 32-bit indexes (using the byte order of the application binary),
10665 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10666 table contains a 32-bit index into the pool of section numbers. For unused
10667 hash table slots, the corresponding entry in the parallel table will be 0.
10669 The pool of section numbers begins immediately following the hash table
10670 (at offset 16 + 12 * M from the beginning of the section). The pool of
10671 section numbers consists of an array of 32-bit words (using the byte order
10672 of the application binary). Each item in the array is indexed starting
10673 from 0. The hash table entry provides the index of the first section
10674 number in the set. Additional section numbers in the set follow, and the
10675 set is terminated by a 0 entry (section number 0 is not used in ELF).
10677 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10678 section must be the first entry in the set, and the .debug_abbrev.dwo must
10679 be the second entry. Other members of the set may follow in any order.
10685 DWP Version 2 combines all the .debug_info, etc. sections into one,
10686 and the entries in the index tables are now offsets into these sections.
10687 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10690 Index Section Contents:
10692 Hash Table of Signatures dwp_hash_table.hash_table
10693 Parallel Table of Indices dwp_hash_table.unit_table
10694 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10695 Table of Section Sizes dwp_hash_table.v2.sizes
10697 The index section header consists of:
10699 V, 32 bit version number
10700 L, 32 bit number of columns in the table of section offsets
10701 N, 32 bit number of compilation units or type units in the index
10702 M, 32 bit number of slots in the hash table
10704 Numbers are recorded using the byte order of the application binary.
10706 The hash table has the same format as version 1.
10707 The parallel table of indices has the same format as version 1,
10708 except that the entries are origin-1 indices into the table of sections
10709 offsets and the table of section sizes.
10711 The table of offsets begins immediately following the parallel table
10712 (at offset 16 + 12 * M from the beginning of the section). The table is
10713 a two-dimensional array of 32-bit words (using the byte order of the
10714 application binary), with L columns and N+1 rows, in row-major order.
10715 Each row in the array is indexed starting from 0. The first row provides
10716 a key to the remaining rows: each column in this row provides an identifier
10717 for a debug section, and the offsets in the same column of subsequent rows
10718 refer to that section. The section identifiers are:
10720 DW_SECT_INFO 1 .debug_info.dwo
10721 DW_SECT_TYPES 2 .debug_types.dwo
10722 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10723 DW_SECT_LINE 4 .debug_line.dwo
10724 DW_SECT_LOC 5 .debug_loc.dwo
10725 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10726 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10727 DW_SECT_MACRO 8 .debug_macro.dwo
10729 The offsets provided by the CU and TU index sections are the base offsets
10730 for the contributions made by each CU or TU to the corresponding section
10731 in the package file. Each CU and TU header contains an abbrev_offset
10732 field, used to find the abbreviations table for that CU or TU within the
10733 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10734 be interpreted as relative to the base offset given in the index section.
10735 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10736 should be interpreted as relative to the base offset for .debug_line.dwo,
10737 and offsets into other debug sections obtained from DWARF attributes should
10738 also be interpreted as relative to the corresponding base offset.
10740 The table of sizes begins immediately following the table of offsets.
10741 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10742 with L columns and N rows, in row-major order. Each row in the array is
10743 indexed starting from 1 (row 0 is shared by the two tables).
10747 Hash table lookup is handled the same in version 1 and 2:
10749 We assume that N and M will not exceed 2^32 - 1.
10750 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10752 Given a 64-bit compilation unit signature or a type signature S, an entry
10753 in the hash table is located as follows:
10755 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10756 the low-order k bits all set to 1.
10758 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10760 3) If the hash table entry at index H matches the signature, use that
10761 entry. If the hash table entry at index H is unused (all zeroes),
10762 terminate the search: the signature is not present in the table.
10764 4) Let H = (H + H') modulo M. Repeat at Step 3.
10766 Because M > N and H' and M are relatively prime, the search is guaranteed
10767 to stop at an unused slot or find the match. */
10769 /* Create a hash table to map DWO IDs to their CU/TU entry in
10770 .debug_{info,types}.dwo in DWP_FILE.
10771 Returns NULL if there isn't one.
10772 Note: This function processes DWP files only, not DWO files. */
10774 static struct dwp_hash_table *
10775 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10777 struct objfile *objfile = dwarf2_per_objfile->objfile;
10778 bfd *dbfd = dwp_file->dbfd;
10779 const gdb_byte *index_ptr, *index_end;
10780 struct dwarf2_section_info *index;
10781 uint32_t version, nr_columns, nr_units, nr_slots;
10782 struct dwp_hash_table *htab;
10784 if (is_debug_types)
10785 index = &dwp_file->sections.tu_index;
10787 index = &dwp_file->sections.cu_index;
10789 if (dwarf2_section_empty_p (index))
10791 dwarf2_read_section (objfile, index);
10793 index_ptr = index->buffer;
10794 index_end = index_ptr + index->size;
10796 version = read_4_bytes (dbfd, index_ptr);
10799 nr_columns = read_4_bytes (dbfd, index_ptr);
10803 nr_units = read_4_bytes (dbfd, index_ptr);
10805 nr_slots = read_4_bytes (dbfd, index_ptr);
10808 if (version != 1 && version != 2)
10810 error (_("Dwarf Error: unsupported DWP file version (%s)"
10811 " [in module %s]"),
10812 pulongest (version), dwp_file->name);
10814 if (nr_slots != (nr_slots & -nr_slots))
10816 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10817 " is not power of 2 [in module %s]"),
10818 pulongest (nr_slots), dwp_file->name);
10821 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
10822 htab->version = version;
10823 htab->nr_columns = nr_columns;
10824 htab->nr_units = nr_units;
10825 htab->nr_slots = nr_slots;
10826 htab->hash_table = index_ptr;
10827 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10829 /* Exit early if the table is empty. */
10830 if (nr_slots == 0 || nr_units == 0
10831 || (version == 2 && nr_columns == 0))
10833 /* All must be zero. */
10834 if (nr_slots != 0 || nr_units != 0
10835 || (version == 2 && nr_columns != 0))
10837 complaint (&symfile_complaints,
10838 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10839 " all zero [in modules %s]"),
10847 htab->section_pool.v1.indices =
10848 htab->unit_table + sizeof (uint32_t) * nr_slots;
10849 /* It's harder to decide whether the section is too small in v1.
10850 V1 is deprecated anyway so we punt. */
10854 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10855 int *ids = htab->section_pool.v2.section_ids;
10856 /* Reverse map for error checking. */
10857 int ids_seen[DW_SECT_MAX + 1];
10860 if (nr_columns < 2)
10862 error (_("Dwarf Error: bad DWP hash table, too few columns"
10863 " in section table [in module %s]"),
10866 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10868 error (_("Dwarf Error: bad DWP hash table, too many columns"
10869 " in section table [in module %s]"),
10872 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10873 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10874 for (i = 0; i < nr_columns; ++i)
10876 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10878 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10880 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10881 " in section table [in module %s]"),
10882 id, dwp_file->name);
10884 if (ids_seen[id] != -1)
10886 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10887 " id %d in section table [in module %s]"),
10888 id, dwp_file->name);
10893 /* Must have exactly one info or types section. */
10894 if (((ids_seen[DW_SECT_INFO] != -1)
10895 + (ids_seen[DW_SECT_TYPES] != -1))
10898 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10899 " DWO info/types section [in module %s]"),
10902 /* Must have an abbrev section. */
10903 if (ids_seen[DW_SECT_ABBREV] == -1)
10905 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10906 " section [in module %s]"),
10909 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10910 htab->section_pool.v2.sizes =
10911 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10912 * nr_units * nr_columns);
10913 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10914 * nr_units * nr_columns))
10917 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10918 " [in module %s]"),
10926 /* Update SECTIONS with the data from SECTP.
10928 This function is like the other "locate" section routines that are
10929 passed to bfd_map_over_sections, but in this context the sections to
10930 read comes from the DWP V1 hash table, not the full ELF section table.
10932 The result is non-zero for success, or zero if an error was found. */
10935 locate_v1_virtual_dwo_sections (asection *sectp,
10936 struct virtual_v1_dwo_sections *sections)
10938 const struct dwop_section_names *names = &dwop_section_names;
10940 if (section_is_p (sectp->name, &names->abbrev_dwo))
10942 /* There can be only one. */
10943 if (sections->abbrev.s.section != NULL)
10945 sections->abbrev.s.section = sectp;
10946 sections->abbrev.size = bfd_get_section_size (sectp);
10948 else if (section_is_p (sectp->name, &names->info_dwo)
10949 || section_is_p (sectp->name, &names->types_dwo))
10951 /* There can be only one. */
10952 if (sections->info_or_types.s.section != NULL)
10954 sections->info_or_types.s.section = sectp;
10955 sections->info_or_types.size = bfd_get_section_size (sectp);
10957 else if (section_is_p (sectp->name, &names->line_dwo))
10959 /* There can be only one. */
10960 if (sections->line.s.section != NULL)
10962 sections->line.s.section = sectp;
10963 sections->line.size = bfd_get_section_size (sectp);
10965 else if (section_is_p (sectp->name, &names->loc_dwo))
10967 /* There can be only one. */
10968 if (sections->loc.s.section != NULL)
10970 sections->loc.s.section = sectp;
10971 sections->loc.size = bfd_get_section_size (sectp);
10973 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10975 /* There can be only one. */
10976 if (sections->macinfo.s.section != NULL)
10978 sections->macinfo.s.section = sectp;
10979 sections->macinfo.size = bfd_get_section_size (sectp);
10981 else if (section_is_p (sectp->name, &names->macro_dwo))
10983 /* There can be only one. */
10984 if (sections->macro.s.section != NULL)
10986 sections->macro.s.section = sectp;
10987 sections->macro.size = bfd_get_section_size (sectp);
10989 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10991 /* There can be only one. */
10992 if (sections->str_offsets.s.section != NULL)
10994 sections->str_offsets.s.section = sectp;
10995 sections->str_offsets.size = bfd_get_section_size (sectp);
10999 /* No other kind of section is valid. */
11006 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11007 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11008 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11009 This is for DWP version 1 files. */
11011 static struct dwo_unit *
11012 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11013 uint32_t unit_index,
11014 const char *comp_dir,
11015 ULONGEST signature, int is_debug_types)
11017 struct objfile *objfile = dwarf2_per_objfile->objfile;
11018 const struct dwp_hash_table *dwp_htab =
11019 is_debug_types ? dwp_file->tus : dwp_file->cus;
11020 bfd *dbfd = dwp_file->dbfd;
11021 const char *kind = is_debug_types ? "TU" : "CU";
11022 struct dwo_file *dwo_file;
11023 struct dwo_unit *dwo_unit;
11024 struct virtual_v1_dwo_sections sections;
11025 void **dwo_file_slot;
11028 gdb_assert (dwp_file->version == 1);
11030 if (dwarf_read_debug)
11032 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11034 pulongest (unit_index), hex_string (signature),
11038 /* Fetch the sections of this DWO unit.
11039 Put a limit on the number of sections we look for so that bad data
11040 doesn't cause us to loop forever. */
11042 #define MAX_NR_V1_DWO_SECTIONS \
11043 (1 /* .debug_info or .debug_types */ \
11044 + 1 /* .debug_abbrev */ \
11045 + 1 /* .debug_line */ \
11046 + 1 /* .debug_loc */ \
11047 + 1 /* .debug_str_offsets */ \
11048 + 1 /* .debug_macro or .debug_macinfo */ \
11049 + 1 /* trailing zero */)
11051 memset (§ions, 0, sizeof (sections));
11053 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11056 uint32_t section_nr =
11057 read_4_bytes (dbfd,
11058 dwp_htab->section_pool.v1.indices
11059 + (unit_index + i) * sizeof (uint32_t));
11061 if (section_nr == 0)
11063 if (section_nr >= dwp_file->num_sections)
11065 error (_("Dwarf Error: bad DWP hash table, section number too large"
11066 " [in module %s]"),
11070 sectp = dwp_file->elf_sections[section_nr];
11071 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
11073 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11074 " [in module %s]"),
11080 || dwarf2_section_empty_p (§ions.info_or_types)
11081 || dwarf2_section_empty_p (§ions.abbrev))
11083 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11084 " [in module %s]"),
11087 if (i == MAX_NR_V1_DWO_SECTIONS)
11089 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11090 " [in module %s]"),
11094 /* It's easier for the rest of the code if we fake a struct dwo_file and
11095 have dwo_unit "live" in that. At least for now.
11097 The DWP file can be made up of a random collection of CUs and TUs.
11098 However, for each CU + set of TUs that came from the same original DWO
11099 file, we can combine them back into a virtual DWO file to save space
11100 (fewer struct dwo_file objects to allocate). Remember that for really
11101 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11103 std::string virtual_dwo_name =
11104 string_printf ("virtual-dwo/%d-%d-%d-%d",
11105 get_section_id (§ions.abbrev),
11106 get_section_id (§ions.line),
11107 get_section_id (§ions.loc),
11108 get_section_id (§ions.str_offsets));
11109 /* Can we use an existing virtual DWO file? */
11110 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11111 /* Create one if necessary. */
11112 if (*dwo_file_slot == NULL)
11114 if (dwarf_read_debug)
11116 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11117 virtual_dwo_name.c_str ());
11119 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11121 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11122 virtual_dwo_name.c_str (),
11123 virtual_dwo_name.size ());
11124 dwo_file->comp_dir = comp_dir;
11125 dwo_file->sections.abbrev = sections.abbrev;
11126 dwo_file->sections.line = sections.line;
11127 dwo_file->sections.loc = sections.loc;
11128 dwo_file->sections.macinfo = sections.macinfo;
11129 dwo_file->sections.macro = sections.macro;
11130 dwo_file->sections.str_offsets = sections.str_offsets;
11131 /* The "str" section is global to the entire DWP file. */
11132 dwo_file->sections.str = dwp_file->sections.str;
11133 /* The info or types section is assigned below to dwo_unit,
11134 there's no need to record it in dwo_file.
11135 Also, we can't simply record type sections in dwo_file because
11136 we record a pointer into the vector in dwo_unit. As we collect more
11137 types we'll grow the vector and eventually have to reallocate space
11138 for it, invalidating all copies of pointers into the previous
11140 *dwo_file_slot = dwo_file;
11144 if (dwarf_read_debug)
11146 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11147 virtual_dwo_name.c_str ());
11149 dwo_file = (struct dwo_file *) *dwo_file_slot;
11152 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11153 dwo_unit->dwo_file = dwo_file;
11154 dwo_unit->signature = signature;
11155 dwo_unit->section =
11156 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11157 *dwo_unit->section = sections.info_or_types;
11158 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11163 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11164 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11165 piece within that section used by a TU/CU, return a virtual section
11166 of just that piece. */
11168 static struct dwarf2_section_info
11169 create_dwp_v2_section (struct dwarf2_section_info *section,
11170 bfd_size_type offset, bfd_size_type size)
11172 struct dwarf2_section_info result;
11175 gdb_assert (section != NULL);
11176 gdb_assert (!section->is_virtual);
11178 memset (&result, 0, sizeof (result));
11179 result.s.containing_section = section;
11180 result.is_virtual = 1;
11185 sectp = get_section_bfd_section (section);
11187 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11188 bounds of the real section. This is a pretty-rare event, so just
11189 flag an error (easier) instead of a warning and trying to cope. */
11191 || offset + size > bfd_get_section_size (sectp))
11193 bfd *abfd = sectp->owner;
11195 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11196 " in section %s [in module %s]"),
11197 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11198 objfile_name (dwarf2_per_objfile->objfile));
11201 result.virtual_offset = offset;
11202 result.size = size;
11206 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11207 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11208 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11209 This is for DWP version 2 files. */
11211 static struct dwo_unit *
11212 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11213 uint32_t unit_index,
11214 const char *comp_dir,
11215 ULONGEST signature, int is_debug_types)
11217 struct objfile *objfile = dwarf2_per_objfile->objfile;
11218 const struct dwp_hash_table *dwp_htab =
11219 is_debug_types ? dwp_file->tus : dwp_file->cus;
11220 bfd *dbfd = dwp_file->dbfd;
11221 const char *kind = is_debug_types ? "TU" : "CU";
11222 struct dwo_file *dwo_file;
11223 struct dwo_unit *dwo_unit;
11224 struct virtual_v2_dwo_sections sections;
11225 void **dwo_file_slot;
11228 gdb_assert (dwp_file->version == 2);
11230 if (dwarf_read_debug)
11232 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11234 pulongest (unit_index), hex_string (signature),
11238 /* Fetch the section offsets of this DWO unit. */
11240 memset (§ions, 0, sizeof (sections));
11242 for (i = 0; i < dwp_htab->nr_columns; ++i)
11244 uint32_t offset = read_4_bytes (dbfd,
11245 dwp_htab->section_pool.v2.offsets
11246 + (((unit_index - 1) * dwp_htab->nr_columns
11248 * sizeof (uint32_t)));
11249 uint32_t size = read_4_bytes (dbfd,
11250 dwp_htab->section_pool.v2.sizes
11251 + (((unit_index - 1) * dwp_htab->nr_columns
11253 * sizeof (uint32_t)));
11255 switch (dwp_htab->section_pool.v2.section_ids[i])
11258 case DW_SECT_TYPES:
11259 sections.info_or_types_offset = offset;
11260 sections.info_or_types_size = size;
11262 case DW_SECT_ABBREV:
11263 sections.abbrev_offset = offset;
11264 sections.abbrev_size = size;
11267 sections.line_offset = offset;
11268 sections.line_size = size;
11271 sections.loc_offset = offset;
11272 sections.loc_size = size;
11274 case DW_SECT_STR_OFFSETS:
11275 sections.str_offsets_offset = offset;
11276 sections.str_offsets_size = size;
11278 case DW_SECT_MACINFO:
11279 sections.macinfo_offset = offset;
11280 sections.macinfo_size = size;
11282 case DW_SECT_MACRO:
11283 sections.macro_offset = offset;
11284 sections.macro_size = size;
11289 /* It's easier for the rest of the code if we fake a struct dwo_file and
11290 have dwo_unit "live" in that. At least for now.
11292 The DWP file can be made up of a random collection of CUs and TUs.
11293 However, for each CU + set of TUs that came from the same original DWO
11294 file, we can combine them back into a virtual DWO file to save space
11295 (fewer struct dwo_file objects to allocate). Remember that for really
11296 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11298 std::string virtual_dwo_name =
11299 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11300 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11301 (long) (sections.line_size ? sections.line_offset : 0),
11302 (long) (sections.loc_size ? sections.loc_offset : 0),
11303 (long) (sections.str_offsets_size
11304 ? sections.str_offsets_offset : 0));
11305 /* Can we use an existing virtual DWO file? */
11306 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11307 /* Create one if necessary. */
11308 if (*dwo_file_slot == NULL)
11310 if (dwarf_read_debug)
11312 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11313 virtual_dwo_name.c_str ());
11315 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11317 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11318 virtual_dwo_name.c_str (),
11319 virtual_dwo_name.size ());
11320 dwo_file->comp_dir = comp_dir;
11321 dwo_file->sections.abbrev =
11322 create_dwp_v2_section (&dwp_file->sections.abbrev,
11323 sections.abbrev_offset, sections.abbrev_size);
11324 dwo_file->sections.line =
11325 create_dwp_v2_section (&dwp_file->sections.line,
11326 sections.line_offset, sections.line_size);
11327 dwo_file->sections.loc =
11328 create_dwp_v2_section (&dwp_file->sections.loc,
11329 sections.loc_offset, sections.loc_size);
11330 dwo_file->sections.macinfo =
11331 create_dwp_v2_section (&dwp_file->sections.macinfo,
11332 sections.macinfo_offset, sections.macinfo_size);
11333 dwo_file->sections.macro =
11334 create_dwp_v2_section (&dwp_file->sections.macro,
11335 sections.macro_offset, sections.macro_size);
11336 dwo_file->sections.str_offsets =
11337 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11338 sections.str_offsets_offset,
11339 sections.str_offsets_size);
11340 /* The "str" section is global to the entire DWP file. */
11341 dwo_file->sections.str = dwp_file->sections.str;
11342 /* The info or types section is assigned below to dwo_unit,
11343 there's no need to record it in dwo_file.
11344 Also, we can't simply record type sections in dwo_file because
11345 we record a pointer into the vector in dwo_unit. As we collect more
11346 types we'll grow the vector and eventually have to reallocate space
11347 for it, invalidating all copies of pointers into the previous
11349 *dwo_file_slot = dwo_file;
11353 if (dwarf_read_debug)
11355 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11356 virtual_dwo_name.c_str ());
11358 dwo_file = (struct dwo_file *) *dwo_file_slot;
11361 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11362 dwo_unit->dwo_file = dwo_file;
11363 dwo_unit->signature = signature;
11364 dwo_unit->section =
11365 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11366 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11367 ? &dwp_file->sections.types
11368 : &dwp_file->sections.info,
11369 sections.info_or_types_offset,
11370 sections.info_or_types_size);
11371 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11376 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11377 Returns NULL if the signature isn't found. */
11379 static struct dwo_unit *
11380 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11381 ULONGEST signature, int is_debug_types)
11383 const struct dwp_hash_table *dwp_htab =
11384 is_debug_types ? dwp_file->tus : dwp_file->cus;
11385 bfd *dbfd = dwp_file->dbfd;
11386 uint32_t mask = dwp_htab->nr_slots - 1;
11387 uint32_t hash = signature & mask;
11388 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11391 struct dwo_unit find_dwo_cu;
11393 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11394 find_dwo_cu.signature = signature;
11395 slot = htab_find_slot (is_debug_types
11396 ? dwp_file->loaded_tus
11397 : dwp_file->loaded_cus,
11398 &find_dwo_cu, INSERT);
11401 return (struct dwo_unit *) *slot;
11403 /* Use a for loop so that we don't loop forever on bad debug info. */
11404 for (i = 0; i < dwp_htab->nr_slots; ++i)
11406 ULONGEST signature_in_table;
11408 signature_in_table =
11409 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11410 if (signature_in_table == signature)
11412 uint32_t unit_index =
11413 read_4_bytes (dbfd,
11414 dwp_htab->unit_table + hash * sizeof (uint32_t));
11416 if (dwp_file->version == 1)
11418 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11419 comp_dir, signature,
11424 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11425 comp_dir, signature,
11428 return (struct dwo_unit *) *slot;
11430 if (signature_in_table == 0)
11432 hash = (hash + hash2) & mask;
11435 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11436 " [in module %s]"),
11440 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11441 Open the file specified by FILE_NAME and hand it off to BFD for
11442 preliminary analysis. Return a newly initialized bfd *, which
11443 includes a canonicalized copy of FILE_NAME.
11444 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11445 SEARCH_CWD is true if the current directory is to be searched.
11446 It will be searched before debug-file-directory.
11447 If successful, the file is added to the bfd include table of the
11448 objfile's bfd (see gdb_bfd_record_inclusion).
11449 If unable to find/open the file, return NULL.
11450 NOTE: This function is derived from symfile_bfd_open. */
11452 static gdb_bfd_ref_ptr
11453 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
11456 char *absolute_name;
11457 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11458 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11459 to debug_file_directory. */
11461 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11465 if (*debug_file_directory != '\0')
11466 search_path = concat (".", dirname_separator_string,
11467 debug_file_directory, (char *) NULL);
11469 search_path = xstrdup (".");
11472 search_path = xstrdup (debug_file_directory);
11474 flags = OPF_RETURN_REALPATH;
11476 flags |= OPF_SEARCH_IN_PATH;
11477 desc = openp (search_path, flags, file_name,
11478 O_RDONLY | O_BINARY, &absolute_name);
11479 xfree (search_path);
11483 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
11484 xfree (absolute_name);
11485 if (sym_bfd == NULL)
11487 bfd_set_cacheable (sym_bfd.get (), 1);
11489 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11492 /* Success. Record the bfd as having been included by the objfile's bfd.
11493 This is important because things like demangled_names_hash lives in the
11494 objfile's per_bfd space and may have references to things like symbol
11495 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11496 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11501 /* Try to open DWO file FILE_NAME.
11502 COMP_DIR is the DW_AT_comp_dir attribute.
11503 The result is the bfd handle of the file.
11504 If there is a problem finding or opening the file, return NULL.
11505 Upon success, the canonicalized path of the file is stored in the bfd,
11506 same as symfile_bfd_open. */
11508 static gdb_bfd_ref_ptr
11509 open_dwo_file (const char *file_name, const char *comp_dir)
11511 if (IS_ABSOLUTE_PATH (file_name))
11512 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
11514 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11516 if (comp_dir != NULL)
11518 char *path_to_try = concat (comp_dir, SLASH_STRING,
11519 file_name, (char *) NULL);
11521 /* NOTE: If comp_dir is a relative path, this will also try the
11522 search path, which seems useful. */
11523 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11524 1 /*search_cwd*/));
11525 xfree (path_to_try);
11530 /* That didn't work, try debug-file-directory, which, despite its name,
11531 is a list of paths. */
11533 if (*debug_file_directory == '\0')
11536 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
11539 /* This function is mapped across the sections and remembers the offset and
11540 size of each of the DWO debugging sections we are interested in. */
11543 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11545 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
11546 const struct dwop_section_names *names = &dwop_section_names;
11548 if (section_is_p (sectp->name, &names->abbrev_dwo))
11550 dwo_sections->abbrev.s.section = sectp;
11551 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11553 else if (section_is_p (sectp->name, &names->info_dwo))
11555 dwo_sections->info.s.section = sectp;
11556 dwo_sections->info.size = bfd_get_section_size (sectp);
11558 else if (section_is_p (sectp->name, &names->line_dwo))
11560 dwo_sections->line.s.section = sectp;
11561 dwo_sections->line.size = bfd_get_section_size (sectp);
11563 else if (section_is_p (sectp->name, &names->loc_dwo))
11565 dwo_sections->loc.s.section = sectp;
11566 dwo_sections->loc.size = bfd_get_section_size (sectp);
11568 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11570 dwo_sections->macinfo.s.section = sectp;
11571 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11573 else if (section_is_p (sectp->name, &names->macro_dwo))
11575 dwo_sections->macro.s.section = sectp;
11576 dwo_sections->macro.size = bfd_get_section_size (sectp);
11578 else if (section_is_p (sectp->name, &names->str_dwo))
11580 dwo_sections->str.s.section = sectp;
11581 dwo_sections->str.size = bfd_get_section_size (sectp);
11583 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11585 dwo_sections->str_offsets.s.section = sectp;
11586 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11588 else if (section_is_p (sectp->name, &names->types_dwo))
11590 struct dwarf2_section_info type_section;
11592 memset (&type_section, 0, sizeof (type_section));
11593 type_section.s.section = sectp;
11594 type_section.size = bfd_get_section_size (sectp);
11595 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11600 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11601 by PER_CU. This is for the non-DWP case.
11602 The result is NULL if DWO_NAME can't be found. */
11604 static struct dwo_file *
11605 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11606 const char *dwo_name, const char *comp_dir)
11608 struct objfile *objfile = dwarf2_per_objfile->objfile;
11609 struct dwo_file *dwo_file;
11610 struct cleanup *cleanups;
11612 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
11615 if (dwarf_read_debug)
11616 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11619 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11620 dwo_file->dwo_name = dwo_name;
11621 dwo_file->comp_dir = comp_dir;
11622 dwo_file->dbfd = dbfd.release ();
11624 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11626 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11627 &dwo_file->sections);
11629 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
11631 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11634 discard_cleanups (cleanups);
11636 if (dwarf_read_debug)
11637 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11642 /* This function is mapped across the sections and remembers the offset and
11643 size of each of the DWP debugging sections common to version 1 and 2 that
11644 we are interested in. */
11647 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11648 void *dwp_file_ptr)
11650 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11651 const struct dwop_section_names *names = &dwop_section_names;
11652 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11654 /* Record the ELF section number for later lookup: this is what the
11655 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11656 gdb_assert (elf_section_nr < dwp_file->num_sections);
11657 dwp_file->elf_sections[elf_section_nr] = sectp;
11659 /* Look for specific sections that we need. */
11660 if (section_is_p (sectp->name, &names->str_dwo))
11662 dwp_file->sections.str.s.section = sectp;
11663 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11665 else if (section_is_p (sectp->name, &names->cu_index))
11667 dwp_file->sections.cu_index.s.section = sectp;
11668 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11670 else if (section_is_p (sectp->name, &names->tu_index))
11672 dwp_file->sections.tu_index.s.section = sectp;
11673 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11677 /* This function is mapped across the sections and remembers the offset and
11678 size of each of the DWP version 2 debugging sections that we are interested
11679 in. This is split into a separate function because we don't know if we
11680 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11683 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11685 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11686 const struct dwop_section_names *names = &dwop_section_names;
11687 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11689 /* Record the ELF section number for later lookup: this is what the
11690 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11691 gdb_assert (elf_section_nr < dwp_file->num_sections);
11692 dwp_file->elf_sections[elf_section_nr] = sectp;
11694 /* Look for specific sections that we need. */
11695 if (section_is_p (sectp->name, &names->abbrev_dwo))
11697 dwp_file->sections.abbrev.s.section = sectp;
11698 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11700 else if (section_is_p (sectp->name, &names->info_dwo))
11702 dwp_file->sections.info.s.section = sectp;
11703 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11705 else if (section_is_p (sectp->name, &names->line_dwo))
11707 dwp_file->sections.line.s.section = sectp;
11708 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11710 else if (section_is_p (sectp->name, &names->loc_dwo))
11712 dwp_file->sections.loc.s.section = sectp;
11713 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11715 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11717 dwp_file->sections.macinfo.s.section = sectp;
11718 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11720 else if (section_is_p (sectp->name, &names->macro_dwo))
11722 dwp_file->sections.macro.s.section = sectp;
11723 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11725 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11727 dwp_file->sections.str_offsets.s.section = sectp;
11728 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11730 else if (section_is_p (sectp->name, &names->types_dwo))
11732 dwp_file->sections.types.s.section = sectp;
11733 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11737 /* Hash function for dwp_file loaded CUs/TUs. */
11740 hash_dwp_loaded_cutus (const void *item)
11742 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11744 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11745 return dwo_unit->signature;
11748 /* Equality function for dwp_file loaded CUs/TUs. */
11751 eq_dwp_loaded_cutus (const void *a, const void *b)
11753 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11754 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11756 return dua->signature == dub->signature;
11759 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11762 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11764 return htab_create_alloc_ex (3,
11765 hash_dwp_loaded_cutus,
11766 eq_dwp_loaded_cutus,
11768 &objfile->objfile_obstack,
11769 hashtab_obstack_allocate,
11770 dummy_obstack_deallocate);
11773 /* Try to open DWP file FILE_NAME.
11774 The result is the bfd handle of the file.
11775 If there is a problem finding or opening the file, return NULL.
11776 Upon success, the canonicalized path of the file is stored in the bfd,
11777 same as symfile_bfd_open. */
11779 static gdb_bfd_ref_ptr
11780 open_dwp_file (const char *file_name)
11782 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11783 1 /*search_cwd*/));
11787 /* Work around upstream bug 15652.
11788 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11789 [Whether that's a "bug" is debatable, but it is getting in our way.]
11790 We have no real idea where the dwp file is, because gdb's realpath-ing
11791 of the executable's path may have discarded the needed info.
11792 [IWBN if the dwp file name was recorded in the executable, akin to
11793 .gnu_debuglink, but that doesn't exist yet.]
11794 Strip the directory from FILE_NAME and search again. */
11795 if (*debug_file_directory != '\0')
11797 /* Don't implicitly search the current directory here.
11798 If the user wants to search "." to handle this case,
11799 it must be added to debug-file-directory. */
11800 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11807 /* Initialize the use of the DWP file for the current objfile.
11808 By convention the name of the DWP file is ${objfile}.dwp.
11809 The result is NULL if it can't be found. */
11811 static struct dwp_file *
11812 open_and_init_dwp_file (void)
11814 struct objfile *objfile = dwarf2_per_objfile->objfile;
11815 struct dwp_file *dwp_file;
11817 /* Try to find first .dwp for the binary file before any symbolic links
11820 /* If the objfile is a debug file, find the name of the real binary
11821 file and get the name of dwp file from there. */
11822 std::string dwp_name;
11823 if (objfile->separate_debug_objfile_backlink != NULL)
11825 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11826 const char *backlink_basename = lbasename (backlink->original_name);
11828 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11831 dwp_name = objfile->original_name;
11833 dwp_name += ".dwp";
11835 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
11837 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11839 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11840 dwp_name = objfile_name (objfile);
11841 dwp_name += ".dwp";
11842 dbfd = open_dwp_file (dwp_name.c_str ());
11847 if (dwarf_read_debug)
11848 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
11851 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11852 dwp_file->name = bfd_get_filename (dbfd.get ());
11853 dwp_file->dbfd = dbfd.release ();
11855 /* +1: section 0 is unused */
11856 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11857 dwp_file->elf_sections =
11858 OBSTACK_CALLOC (&objfile->objfile_obstack,
11859 dwp_file->num_sections, asection *);
11861 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11864 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11866 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11868 /* The DWP file version is stored in the hash table. Oh well. */
11869 if (dwp_file->cus && dwp_file->tus
11870 && dwp_file->cus->version != dwp_file->tus->version)
11872 /* Technically speaking, we should try to limp along, but this is
11873 pretty bizarre. We use pulongest here because that's the established
11874 portability solution (e.g, we cannot use %u for uint32_t). */
11875 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11876 " TU version %s [in DWP file %s]"),
11877 pulongest (dwp_file->cus->version),
11878 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11882 dwp_file->version = dwp_file->cus->version;
11883 else if (dwp_file->tus)
11884 dwp_file->version = dwp_file->tus->version;
11886 dwp_file->version = 2;
11888 if (dwp_file->version == 2)
11889 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11892 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11893 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11895 if (dwarf_read_debug)
11897 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11898 fprintf_unfiltered (gdb_stdlog,
11899 " %s CUs, %s TUs\n",
11900 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11901 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11907 /* Wrapper around open_and_init_dwp_file, only open it once. */
11909 static struct dwp_file *
11910 get_dwp_file (void)
11912 if (! dwarf2_per_objfile->dwp_checked)
11914 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11915 dwarf2_per_objfile->dwp_checked = 1;
11917 return dwarf2_per_objfile->dwp_file;
11920 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11921 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11922 or in the DWP file for the objfile, referenced by THIS_UNIT.
11923 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11924 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11926 This is called, for example, when wanting to read a variable with a
11927 complex location. Therefore we don't want to do file i/o for every call.
11928 Therefore we don't want to look for a DWO file on every call.
11929 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11930 then we check if we've already seen DWO_NAME, and only THEN do we check
11933 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11934 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11936 static struct dwo_unit *
11937 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11938 const char *dwo_name, const char *comp_dir,
11939 ULONGEST signature, int is_debug_types)
11941 struct objfile *objfile = dwarf2_per_objfile->objfile;
11942 const char *kind = is_debug_types ? "TU" : "CU";
11943 void **dwo_file_slot;
11944 struct dwo_file *dwo_file;
11945 struct dwp_file *dwp_file;
11947 /* First see if there's a DWP file.
11948 If we have a DWP file but didn't find the DWO inside it, don't
11949 look for the original DWO file. It makes gdb behave differently
11950 depending on whether one is debugging in the build tree. */
11952 dwp_file = get_dwp_file ();
11953 if (dwp_file != NULL)
11955 const struct dwp_hash_table *dwp_htab =
11956 is_debug_types ? dwp_file->tus : dwp_file->cus;
11958 if (dwp_htab != NULL)
11960 struct dwo_unit *dwo_cutu =
11961 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11962 signature, is_debug_types);
11964 if (dwo_cutu != NULL)
11966 if (dwarf_read_debug)
11968 fprintf_unfiltered (gdb_stdlog,
11969 "Virtual DWO %s %s found: @%s\n",
11970 kind, hex_string (signature),
11971 host_address_to_string (dwo_cutu));
11979 /* No DWP file, look for the DWO file. */
11981 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11982 if (*dwo_file_slot == NULL)
11984 /* Read in the file and build a table of the CUs/TUs it contains. */
11985 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11987 /* NOTE: This will be NULL if unable to open the file. */
11988 dwo_file = (struct dwo_file *) *dwo_file_slot;
11990 if (dwo_file != NULL)
11992 struct dwo_unit *dwo_cutu = NULL;
11994 if (is_debug_types && dwo_file->tus)
11996 struct dwo_unit find_dwo_cutu;
11998 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11999 find_dwo_cutu.signature = signature;
12001 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
12003 else if (!is_debug_types && dwo_file->cus)
12005 struct dwo_unit find_dwo_cutu;
12007 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12008 find_dwo_cutu.signature = signature;
12009 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12013 if (dwo_cutu != NULL)
12015 if (dwarf_read_debug)
12017 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12018 kind, dwo_name, hex_string (signature),
12019 host_address_to_string (dwo_cutu));
12026 /* We didn't find it. This could mean a dwo_id mismatch, or
12027 someone deleted the DWO/DWP file, or the search path isn't set up
12028 correctly to find the file. */
12030 if (dwarf_read_debug)
12032 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12033 kind, dwo_name, hex_string (signature));
12036 /* This is a warning and not a complaint because it can be caused by
12037 pilot error (e.g., user accidentally deleting the DWO). */
12039 /* Print the name of the DWP file if we looked there, helps the user
12040 better diagnose the problem. */
12041 std::string dwp_text;
12043 if (dwp_file != NULL)
12044 dwp_text = string_printf (" [in DWP file %s]",
12045 lbasename (dwp_file->name));
12047 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12048 " [in module %s]"),
12049 kind, dwo_name, hex_string (signature),
12051 this_unit->is_debug_types ? "TU" : "CU",
12052 to_underlying (this_unit->sect_off), objfile_name (objfile));
12057 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12058 See lookup_dwo_cutu_unit for details. */
12060 static struct dwo_unit *
12061 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12062 const char *dwo_name, const char *comp_dir,
12063 ULONGEST signature)
12065 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12068 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12069 See lookup_dwo_cutu_unit for details. */
12071 static struct dwo_unit *
12072 lookup_dwo_type_unit (struct signatured_type *this_tu,
12073 const char *dwo_name, const char *comp_dir)
12075 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12078 /* Traversal function for queue_and_load_all_dwo_tus. */
12081 queue_and_load_dwo_tu (void **slot, void *info)
12083 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12084 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12085 ULONGEST signature = dwo_unit->signature;
12086 struct signatured_type *sig_type =
12087 lookup_dwo_signatured_type (per_cu->cu, signature);
12089 if (sig_type != NULL)
12091 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12093 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12094 a real dependency of PER_CU on SIG_TYPE. That is detected later
12095 while processing PER_CU. */
12096 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12097 load_full_type_unit (sig_cu);
12098 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12104 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12105 The DWO may have the only definition of the type, though it may not be
12106 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12107 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12110 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12112 struct dwo_unit *dwo_unit;
12113 struct dwo_file *dwo_file;
12115 gdb_assert (!per_cu->is_debug_types);
12116 gdb_assert (get_dwp_file () == NULL);
12117 gdb_assert (per_cu->cu != NULL);
12119 dwo_unit = per_cu->cu->dwo_unit;
12120 gdb_assert (dwo_unit != NULL);
12122 dwo_file = dwo_unit->dwo_file;
12123 if (dwo_file->tus != NULL)
12124 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12127 /* Free all resources associated with DWO_FILE.
12128 Close the DWO file and munmap the sections.
12129 All memory should be on the objfile obstack. */
12132 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
12135 /* Note: dbfd is NULL for virtual DWO files. */
12136 gdb_bfd_unref (dwo_file->dbfd);
12138 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12141 /* Wrapper for free_dwo_file for use in cleanups. */
12144 free_dwo_file_cleanup (void *arg)
12146 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12147 struct objfile *objfile = dwarf2_per_objfile->objfile;
12149 free_dwo_file (dwo_file, objfile);
12152 /* Traversal function for free_dwo_files. */
12155 free_dwo_file_from_slot (void **slot, void *info)
12157 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12158 struct objfile *objfile = (struct objfile *) info;
12160 free_dwo_file (dwo_file, objfile);
12165 /* Free all resources associated with DWO_FILES. */
12168 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12170 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
12173 /* Read in various DIEs. */
12175 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12176 Inherit only the children of the DW_AT_abstract_origin DIE not being
12177 already referenced by DW_AT_abstract_origin from the children of the
12181 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12183 struct die_info *child_die;
12184 sect_offset *offsetp;
12185 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12186 struct die_info *origin_die;
12187 /* Iterator of the ORIGIN_DIE children. */
12188 struct die_info *origin_child_die;
12189 struct attribute *attr;
12190 struct dwarf2_cu *origin_cu;
12191 struct pending **origin_previous_list_in_scope;
12193 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12197 /* Note that following die references may follow to a die in a
12201 origin_die = follow_die_ref (die, attr, &origin_cu);
12203 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12205 origin_previous_list_in_scope = origin_cu->list_in_scope;
12206 origin_cu->list_in_scope = cu->list_in_scope;
12208 if (die->tag != origin_die->tag
12209 && !(die->tag == DW_TAG_inlined_subroutine
12210 && origin_die->tag == DW_TAG_subprogram))
12211 complaint (&symfile_complaints,
12212 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
12213 to_underlying (die->sect_off),
12214 to_underlying (origin_die->sect_off));
12216 std::vector<sect_offset> offsets;
12218 for (child_die = die->child;
12219 child_die && child_die->tag;
12220 child_die = sibling_die (child_die))
12222 struct die_info *child_origin_die;
12223 struct dwarf2_cu *child_origin_cu;
12225 /* We are trying to process concrete instance entries:
12226 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12227 it's not relevant to our analysis here. i.e. detecting DIEs that are
12228 present in the abstract instance but not referenced in the concrete
12230 if (child_die->tag == DW_TAG_call_site
12231 || child_die->tag == DW_TAG_GNU_call_site)
12234 /* For each CHILD_DIE, find the corresponding child of
12235 ORIGIN_DIE. If there is more than one layer of
12236 DW_AT_abstract_origin, follow them all; there shouldn't be,
12237 but GCC versions at least through 4.4 generate this (GCC PR
12239 child_origin_die = child_die;
12240 child_origin_cu = cu;
12243 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12247 child_origin_die = follow_die_ref (child_origin_die, attr,
12251 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12252 counterpart may exist. */
12253 if (child_origin_die != child_die)
12255 if (child_die->tag != child_origin_die->tag
12256 && !(child_die->tag == DW_TAG_inlined_subroutine
12257 && child_origin_die->tag == DW_TAG_subprogram))
12258 complaint (&symfile_complaints,
12259 _("Child DIE 0x%x and its abstract origin 0x%x have "
12261 to_underlying (child_die->sect_off),
12262 to_underlying (child_origin_die->sect_off));
12263 if (child_origin_die->parent != origin_die)
12264 complaint (&symfile_complaints,
12265 _("Child DIE 0x%x and its abstract origin 0x%x have "
12266 "different parents"),
12267 to_underlying (child_die->sect_off),
12268 to_underlying (child_origin_die->sect_off));
12270 offsets.push_back (child_origin_die->sect_off);
12273 std::sort (offsets.begin (), offsets.end ());
12274 sect_offset *offsets_end = offsets.data () + offsets.size ();
12275 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12276 if (offsetp[-1] == *offsetp)
12277 complaint (&symfile_complaints,
12278 _("Multiple children of DIE 0x%x refer "
12279 "to DIE 0x%x as their abstract origin"),
12280 to_underlying (die->sect_off), to_underlying (*offsetp));
12282 offsetp = offsets.data ();
12283 origin_child_die = origin_die->child;
12284 while (origin_child_die && origin_child_die->tag)
12286 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12287 while (offsetp < offsets_end
12288 && *offsetp < origin_child_die->sect_off)
12290 if (offsetp >= offsets_end
12291 || *offsetp > origin_child_die->sect_off)
12293 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12294 Check whether we're already processing ORIGIN_CHILD_DIE.
12295 This can happen with mutually referenced abstract_origins.
12297 if (!origin_child_die->in_process)
12298 process_die (origin_child_die, origin_cu);
12300 origin_child_die = sibling_die (origin_child_die);
12302 origin_cu->list_in_scope = origin_previous_list_in_scope;
12306 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12308 struct objfile *objfile = cu->objfile;
12309 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12310 struct context_stack *newobj;
12313 struct die_info *child_die;
12314 struct attribute *attr, *call_line, *call_file;
12316 CORE_ADDR baseaddr;
12317 struct block *block;
12318 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12319 std::vector<struct symbol *> template_args;
12320 struct template_symbol *templ_func = NULL;
12324 /* If we do not have call site information, we can't show the
12325 caller of this inlined function. That's too confusing, so
12326 only use the scope for local variables. */
12327 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12328 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12329 if (call_line == NULL || call_file == NULL)
12331 read_lexical_block_scope (die, cu);
12336 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12338 name = dwarf2_name (die, cu);
12340 /* Ignore functions with missing or empty names. These are actually
12341 illegal according to the DWARF standard. */
12344 complaint (&symfile_complaints,
12345 _("missing name for subprogram DIE at %d"),
12346 to_underlying (die->sect_off));
12350 /* Ignore functions with missing or invalid low and high pc attributes. */
12351 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12352 <= PC_BOUNDS_INVALID)
12354 attr = dwarf2_attr (die, DW_AT_external, cu);
12355 if (!attr || !DW_UNSND (attr))
12356 complaint (&symfile_complaints,
12357 _("cannot get low and high bounds "
12358 "for subprogram DIE at %d"),
12359 to_underlying (die->sect_off));
12363 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12364 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12366 /* If we have any template arguments, then we must allocate a
12367 different sort of symbol. */
12368 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12370 if (child_die->tag == DW_TAG_template_type_param
12371 || child_die->tag == DW_TAG_template_value_param)
12373 templ_func = allocate_template_symbol (objfile);
12374 templ_func->subclass = SYMBOL_TEMPLATE;
12379 newobj = push_context (0, lowpc);
12380 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
12381 (struct symbol *) templ_func);
12383 /* If there is a location expression for DW_AT_frame_base, record
12385 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12387 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12389 /* If there is a location for the static link, record it. */
12390 newobj->static_link = NULL;
12391 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12394 newobj->static_link
12395 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12396 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12399 cu->list_in_scope = &local_symbols;
12401 if (die->child != NULL)
12403 child_die = die->child;
12404 while (child_die && child_die->tag)
12406 if (child_die->tag == DW_TAG_template_type_param
12407 || child_die->tag == DW_TAG_template_value_param)
12409 struct symbol *arg = new_symbol (child_die, NULL, cu);
12412 template_args.push_back (arg);
12415 process_die (child_die, cu);
12416 child_die = sibling_die (child_die);
12420 inherit_abstract_dies (die, cu);
12422 /* If we have a DW_AT_specification, we might need to import using
12423 directives from the context of the specification DIE. See the
12424 comment in determine_prefix. */
12425 if (cu->language == language_cplus
12426 && dwarf2_attr (die, DW_AT_specification, cu))
12428 struct dwarf2_cu *spec_cu = cu;
12429 struct die_info *spec_die = die_specification (die, &spec_cu);
12433 child_die = spec_die->child;
12434 while (child_die && child_die->tag)
12436 if (child_die->tag == DW_TAG_imported_module)
12437 process_die (child_die, spec_cu);
12438 child_die = sibling_die (child_die);
12441 /* In some cases, GCC generates specification DIEs that
12442 themselves contain DW_AT_specification attributes. */
12443 spec_die = die_specification (spec_die, &spec_cu);
12447 newobj = pop_context ();
12448 /* Make a block for the local symbols within. */
12449 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
12450 newobj->static_link, lowpc, highpc);
12452 /* For C++, set the block's scope. */
12453 if ((cu->language == language_cplus
12454 || cu->language == language_fortran
12455 || cu->language == language_d
12456 || cu->language == language_rust)
12457 && cu->processing_has_namespace_info)
12458 block_set_scope (block, determine_prefix (die, cu),
12459 &objfile->objfile_obstack);
12461 /* If we have address ranges, record them. */
12462 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12464 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
12466 /* Attach template arguments to function. */
12467 if (!template_args.empty ())
12469 gdb_assert (templ_func != NULL);
12471 templ_func->n_template_arguments = template_args.size ();
12472 templ_func->template_arguments
12473 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12474 templ_func->n_template_arguments);
12475 memcpy (templ_func->template_arguments,
12476 template_args.data (),
12477 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12480 /* In C++, we can have functions nested inside functions (e.g., when
12481 a function declares a class that has methods). This means that
12482 when we finish processing a function scope, we may need to go
12483 back to building a containing block's symbol lists. */
12484 local_symbols = newobj->locals;
12485 local_using_directives = newobj->local_using_directives;
12487 /* If we've finished processing a top-level function, subsequent
12488 symbols go in the file symbol list. */
12489 if (outermost_context_p ())
12490 cu->list_in_scope = &file_symbols;
12493 /* Process all the DIES contained within a lexical block scope. Start
12494 a new scope, process the dies, and then close the scope. */
12497 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12499 struct objfile *objfile = cu->objfile;
12500 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12501 struct context_stack *newobj;
12502 CORE_ADDR lowpc, highpc;
12503 struct die_info *child_die;
12504 CORE_ADDR baseaddr;
12506 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12508 /* Ignore blocks with missing or invalid low and high pc attributes. */
12509 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12510 as multiple lexical blocks? Handling children in a sane way would
12511 be nasty. Might be easier to properly extend generic blocks to
12512 describe ranges. */
12513 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12515 case PC_BOUNDS_NOT_PRESENT:
12516 /* DW_TAG_lexical_block has no attributes, process its children as if
12517 there was no wrapping by that DW_TAG_lexical_block.
12518 GCC does no longer produces such DWARF since GCC r224161. */
12519 for (child_die = die->child;
12520 child_die != NULL && child_die->tag;
12521 child_die = sibling_die (child_die))
12522 process_die (child_die, cu);
12524 case PC_BOUNDS_INVALID:
12527 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12528 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12530 push_context (0, lowpc);
12531 if (die->child != NULL)
12533 child_die = die->child;
12534 while (child_die && child_die->tag)
12536 process_die (child_die, cu);
12537 child_die = sibling_die (child_die);
12540 inherit_abstract_dies (die, cu);
12541 newobj = pop_context ();
12543 if (local_symbols != NULL || local_using_directives != NULL)
12545 struct block *block
12546 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
12547 newobj->start_addr, highpc);
12549 /* Note that recording ranges after traversing children, as we
12550 do here, means that recording a parent's ranges entails
12551 walking across all its children's ranges as they appear in
12552 the address map, which is quadratic behavior.
12554 It would be nicer to record the parent's ranges before
12555 traversing its children, simply overriding whatever you find
12556 there. But since we don't even decide whether to create a
12557 block until after we've traversed its children, that's hard
12559 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12561 local_symbols = newobj->locals;
12562 local_using_directives = newobj->local_using_directives;
12565 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12568 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12570 struct objfile *objfile = cu->objfile;
12571 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12572 CORE_ADDR pc, baseaddr;
12573 struct attribute *attr;
12574 struct call_site *call_site, call_site_local;
12577 struct die_info *child_die;
12579 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12581 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12584 /* This was a pre-DWARF-5 GNU extension alias
12585 for DW_AT_call_return_pc. */
12586 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12590 complaint (&symfile_complaints,
12591 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
12592 "DIE 0x%x [in module %s]"),
12593 to_underlying (die->sect_off), objfile_name (objfile));
12596 pc = attr_value_as_address (attr) + baseaddr;
12597 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12599 if (cu->call_site_htab == NULL)
12600 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12601 NULL, &objfile->objfile_obstack,
12602 hashtab_obstack_allocate, NULL);
12603 call_site_local.pc = pc;
12604 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12607 complaint (&symfile_complaints,
12608 _("Duplicate PC %s for DW_TAG_call_site "
12609 "DIE 0x%x [in module %s]"),
12610 paddress (gdbarch, pc), to_underlying (die->sect_off),
12611 objfile_name (objfile));
12615 /* Count parameters at the caller. */
12618 for (child_die = die->child; child_die && child_die->tag;
12619 child_die = sibling_die (child_die))
12621 if (child_die->tag != DW_TAG_call_site_parameter
12622 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12624 complaint (&symfile_complaints,
12625 _("Tag %d is not DW_TAG_call_site_parameter in "
12626 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12627 child_die->tag, to_underlying (child_die->sect_off),
12628 objfile_name (objfile));
12636 = ((struct call_site *)
12637 obstack_alloc (&objfile->objfile_obstack,
12638 sizeof (*call_site)
12639 + (sizeof (*call_site->parameter) * (nparams - 1))));
12641 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12642 call_site->pc = pc;
12644 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12645 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12647 struct die_info *func_die;
12649 /* Skip also over DW_TAG_inlined_subroutine. */
12650 for (func_die = die->parent;
12651 func_die && func_die->tag != DW_TAG_subprogram
12652 && func_die->tag != DW_TAG_subroutine_type;
12653 func_die = func_die->parent);
12655 /* DW_AT_call_all_calls is a superset
12656 of DW_AT_call_all_tail_calls. */
12658 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12659 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12660 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12661 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12663 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12664 not complete. But keep CALL_SITE for look ups via call_site_htab,
12665 both the initial caller containing the real return address PC and
12666 the final callee containing the current PC of a chain of tail
12667 calls do not need to have the tail call list complete. But any
12668 function candidate for a virtual tail call frame searched via
12669 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12670 determined unambiguously. */
12674 struct type *func_type = NULL;
12677 func_type = get_die_type (func_die, cu);
12678 if (func_type != NULL)
12680 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12682 /* Enlist this call site to the function. */
12683 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12684 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12687 complaint (&symfile_complaints,
12688 _("Cannot find function owning DW_TAG_call_site "
12689 "DIE 0x%x [in module %s]"),
12690 to_underlying (die->sect_off), objfile_name (objfile));
12694 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12696 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12698 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12701 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12702 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12704 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12705 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12706 /* Keep NULL DWARF_BLOCK. */;
12707 else if (attr_form_is_block (attr))
12709 struct dwarf2_locexpr_baton *dlbaton;
12711 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12712 dlbaton->data = DW_BLOCK (attr)->data;
12713 dlbaton->size = DW_BLOCK (attr)->size;
12714 dlbaton->per_cu = cu->per_cu;
12716 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12718 else if (attr_form_is_ref (attr))
12720 struct dwarf2_cu *target_cu = cu;
12721 struct die_info *target_die;
12723 target_die = follow_die_ref (die, attr, &target_cu);
12724 gdb_assert (target_cu->objfile == objfile);
12725 if (die_is_declaration (target_die, target_cu))
12727 const char *target_physname;
12729 /* Prefer the mangled name; otherwise compute the demangled one. */
12730 target_physname = dw2_linkage_name (target_die, target_cu);
12731 if (target_physname == NULL)
12732 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12733 if (target_physname == NULL)
12734 complaint (&symfile_complaints,
12735 _("DW_AT_call_target target DIE has invalid "
12736 "physname, for referencing DIE 0x%x [in module %s]"),
12737 to_underlying (die->sect_off), objfile_name (objfile));
12739 SET_FIELD_PHYSNAME (call_site->target, target_physname);
12745 /* DW_AT_entry_pc should be preferred. */
12746 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
12747 <= PC_BOUNDS_INVALID)
12748 complaint (&symfile_complaints,
12749 _("DW_AT_call_target target DIE has invalid "
12750 "low pc, for referencing DIE 0x%x [in module %s]"),
12751 to_underlying (die->sect_off), objfile_name (objfile));
12754 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12755 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12760 complaint (&symfile_complaints,
12761 _("DW_TAG_call_site DW_AT_call_target is neither "
12762 "block nor reference, for DIE 0x%x [in module %s]"),
12763 to_underlying (die->sect_off), objfile_name (objfile));
12765 call_site->per_cu = cu->per_cu;
12767 for (child_die = die->child;
12768 child_die && child_die->tag;
12769 child_die = sibling_die (child_die))
12771 struct call_site_parameter *parameter;
12772 struct attribute *loc, *origin;
12774 if (child_die->tag != DW_TAG_call_site_parameter
12775 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12777 /* Already printed the complaint above. */
12781 gdb_assert (call_site->parameter_count < nparams);
12782 parameter = &call_site->parameter[call_site->parameter_count];
12784 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12785 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12786 register is contained in DW_AT_call_value. */
12788 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12789 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12790 if (origin == NULL)
12792 /* This was a pre-DWARF-5 GNU extension alias
12793 for DW_AT_call_parameter. */
12794 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12796 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
12798 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12800 sect_offset sect_off
12801 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12802 if (!offset_in_cu_p (&cu->header, sect_off))
12804 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12805 binding can be done only inside one CU. Such referenced DIE
12806 therefore cannot be even moved to DW_TAG_partial_unit. */
12807 complaint (&symfile_complaints,
12808 _("DW_AT_call_parameter offset is not in CU for "
12809 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12810 to_underlying (child_die->sect_off),
12811 objfile_name (objfile));
12814 parameter->u.param_cu_off
12815 = (cu_offset) (sect_off - cu->header.sect_off);
12817 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
12819 complaint (&symfile_complaints,
12820 _("No DW_FORM_block* DW_AT_location for "
12821 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12822 to_underlying (child_die->sect_off), objfile_name (objfile));
12827 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12828 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12829 if (parameter->u.dwarf_reg != -1)
12830 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12831 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12832 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12833 ¶meter->u.fb_offset))
12834 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12837 complaint (&symfile_complaints,
12838 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12839 "for DW_FORM_block* DW_AT_location is supported for "
12840 "DW_TAG_call_site child DIE 0x%x "
12842 to_underlying (child_die->sect_off),
12843 objfile_name (objfile));
12848 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12850 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12851 if (!attr_form_is_block (attr))
12853 complaint (&symfile_complaints,
12854 _("No DW_FORM_block* DW_AT_call_value for "
12855 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12856 to_underlying (child_die->sect_off),
12857 objfile_name (objfile));
12860 parameter->value = DW_BLOCK (attr)->data;
12861 parameter->value_size = DW_BLOCK (attr)->size;
12863 /* Parameters are not pre-cleared by memset above. */
12864 parameter->data_value = NULL;
12865 parameter->data_value_size = 0;
12866 call_site->parameter_count++;
12868 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12870 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12873 if (!attr_form_is_block (attr))
12874 complaint (&symfile_complaints,
12875 _("No DW_FORM_block* DW_AT_call_data_value for "
12876 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12877 to_underlying (child_die->sect_off),
12878 objfile_name (objfile));
12881 parameter->data_value = DW_BLOCK (attr)->data;
12882 parameter->data_value_size = DW_BLOCK (attr)->size;
12888 /* Helper function for read_variable. If DIE represents a virtual
12889 table, then return the type of the concrete object that is
12890 associated with the virtual table. Otherwise, return NULL. */
12892 static struct type *
12893 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12895 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12899 /* Find the type DIE. */
12900 struct die_info *type_die = NULL;
12901 struct dwarf2_cu *type_cu = cu;
12903 if (attr_form_is_ref (attr))
12904 type_die = follow_die_ref (die, attr, &type_cu);
12905 if (type_die == NULL)
12908 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12910 return die_containing_type (type_die, type_cu);
12913 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12916 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12918 struct rust_vtable_symbol *storage = NULL;
12920 if (cu->language == language_rust)
12922 struct type *containing_type = rust_containing_type (die, cu);
12924 if (containing_type != NULL)
12926 struct objfile *objfile = cu->objfile;
12928 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
12929 struct rust_vtable_symbol);
12930 initialize_objfile_symbol (storage);
12931 storage->concrete_type = containing_type;
12932 storage->subclass = SYMBOL_RUST_VTABLE;
12936 new_symbol_full (die, NULL, cu, storage);
12939 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12940 reading .debug_rnglists.
12941 Callback's type should be:
12942 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12943 Return true if the attributes are present and valid, otherwise,
12946 template <typename Callback>
12948 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12949 Callback &&callback)
12951 struct objfile *objfile = cu->objfile;
12952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12953 struct comp_unit_head *cu_header = &cu->header;
12954 bfd *obfd = objfile->obfd;
12955 unsigned int addr_size = cu_header->addr_size;
12956 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12957 /* Base address selection entry. */
12960 unsigned int dummy;
12961 const gdb_byte *buffer;
12963 CORE_ADDR high = 0;
12964 CORE_ADDR baseaddr;
12965 bool overflow = false;
12967 found_base = cu->base_known;
12968 base = cu->base_address;
12970 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12971 if (offset >= dwarf2_per_objfile->rnglists.size)
12973 complaint (&symfile_complaints,
12974 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12978 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12980 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12984 /* Initialize it due to a false compiler warning. */
12985 CORE_ADDR range_beginning = 0, range_end = 0;
12986 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12987 + dwarf2_per_objfile->rnglists.size);
12988 unsigned int bytes_read;
12990 if (buffer == buf_end)
12995 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12998 case DW_RLE_end_of_list:
13000 case DW_RLE_base_address:
13001 if (buffer + cu->header.addr_size > buf_end)
13006 base = read_address (obfd, buffer, cu, &bytes_read);
13008 buffer += bytes_read;
13010 case DW_RLE_start_length:
13011 if (buffer + cu->header.addr_size > buf_end)
13016 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13017 buffer += bytes_read;
13018 range_end = (range_beginning
13019 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13020 buffer += bytes_read;
13021 if (buffer > buf_end)
13027 case DW_RLE_offset_pair:
13028 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13029 buffer += bytes_read;
13030 if (buffer > buf_end)
13035 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13036 buffer += bytes_read;
13037 if (buffer > buf_end)
13043 case DW_RLE_start_end:
13044 if (buffer + 2 * cu->header.addr_size > buf_end)
13049 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13050 buffer += bytes_read;
13051 range_end = read_address (obfd, buffer, cu, &bytes_read);
13052 buffer += bytes_read;
13055 complaint (&symfile_complaints,
13056 _("Invalid .debug_rnglists data (no base address)"));
13059 if (rlet == DW_RLE_end_of_list || overflow)
13061 if (rlet == DW_RLE_base_address)
13066 /* We have no valid base address for the ranges
13068 complaint (&symfile_complaints,
13069 _("Invalid .debug_rnglists data (no base address)"));
13073 if (range_beginning > range_end)
13075 /* Inverted range entries are invalid. */
13076 complaint (&symfile_complaints,
13077 _("Invalid .debug_rnglists data (inverted range)"));
13081 /* Empty range entries have no effect. */
13082 if (range_beginning == range_end)
13085 range_beginning += base;
13088 /* A not-uncommon case of bad debug info.
13089 Don't pollute the addrmap with bad data. */
13090 if (range_beginning + baseaddr == 0
13091 && !dwarf2_per_objfile->has_section_at_zero)
13093 complaint (&symfile_complaints,
13094 _(".debug_rnglists entry has start address of zero"
13095 " [in module %s]"), objfile_name (objfile));
13099 callback (range_beginning, range_end);
13104 complaint (&symfile_complaints,
13105 _("Offset %d is not terminated "
13106 "for DW_AT_ranges attribute"),
13114 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13115 Callback's type should be:
13116 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13117 Return 1 if the attributes are present and valid, otherwise, return 0. */
13119 template <typename Callback>
13121 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13122 Callback &&callback)
13124 struct objfile *objfile = cu->objfile;
13125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13126 struct comp_unit_head *cu_header = &cu->header;
13127 bfd *obfd = objfile->obfd;
13128 unsigned int addr_size = cu_header->addr_size;
13129 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13130 /* Base address selection entry. */
13133 unsigned int dummy;
13134 const gdb_byte *buffer;
13135 CORE_ADDR baseaddr;
13137 if (cu_header->version >= 5)
13138 return dwarf2_rnglists_process (offset, cu, callback);
13140 found_base = cu->base_known;
13141 base = cu->base_address;
13143 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
13144 if (offset >= dwarf2_per_objfile->ranges.size)
13146 complaint (&symfile_complaints,
13147 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13151 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13153 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13157 CORE_ADDR range_beginning, range_end;
13159 range_beginning = read_address (obfd, buffer, cu, &dummy);
13160 buffer += addr_size;
13161 range_end = read_address (obfd, buffer, cu, &dummy);
13162 buffer += addr_size;
13163 offset += 2 * addr_size;
13165 /* An end of list marker is a pair of zero addresses. */
13166 if (range_beginning == 0 && range_end == 0)
13167 /* Found the end of list entry. */
13170 /* Each base address selection entry is a pair of 2 values.
13171 The first is the largest possible address, the second is
13172 the base address. Check for a base address here. */
13173 if ((range_beginning & mask) == mask)
13175 /* If we found the largest possible address, then we already
13176 have the base address in range_end. */
13184 /* We have no valid base address for the ranges
13186 complaint (&symfile_complaints,
13187 _("Invalid .debug_ranges data (no base address)"));
13191 if (range_beginning > range_end)
13193 /* Inverted range entries are invalid. */
13194 complaint (&symfile_complaints,
13195 _("Invalid .debug_ranges data (inverted range)"));
13199 /* Empty range entries have no effect. */
13200 if (range_beginning == range_end)
13203 range_beginning += base;
13206 /* A not-uncommon case of bad debug info.
13207 Don't pollute the addrmap with bad data. */
13208 if (range_beginning + baseaddr == 0
13209 && !dwarf2_per_objfile->has_section_at_zero)
13211 complaint (&symfile_complaints,
13212 _(".debug_ranges entry has start address of zero"
13213 " [in module %s]"), objfile_name (objfile));
13217 callback (range_beginning, range_end);
13223 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13224 Return 1 if the attributes are present and valid, otherwise, return 0.
13225 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13228 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13229 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13230 struct partial_symtab *ranges_pst)
13232 struct objfile *objfile = cu->objfile;
13233 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13234 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13235 SECT_OFF_TEXT (objfile));
13238 CORE_ADDR high = 0;
13241 retval = dwarf2_ranges_process (offset, cu,
13242 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13244 if (ranges_pst != NULL)
13249 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13250 range_beginning + baseaddr);
13251 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13252 range_end + baseaddr);
13253 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13257 /* FIXME: This is recording everything as a low-high
13258 segment of consecutive addresses. We should have a
13259 data structure for discontiguous block ranges
13263 low = range_beginning;
13269 if (range_beginning < low)
13270 low = range_beginning;
13271 if (range_end > high)
13279 /* If the first entry is an end-of-list marker, the range
13280 describes an empty scope, i.e. no instructions. */
13286 *high_return = high;
13290 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13291 definition for the return value. *LOWPC and *HIGHPC are set iff
13292 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13294 static enum pc_bounds_kind
13295 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13296 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13297 struct partial_symtab *pst)
13299 struct attribute *attr;
13300 struct attribute *attr_high;
13302 CORE_ADDR high = 0;
13303 enum pc_bounds_kind ret;
13305 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13308 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13311 low = attr_value_as_address (attr);
13312 high = attr_value_as_address (attr_high);
13313 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13317 /* Found high w/o low attribute. */
13318 return PC_BOUNDS_INVALID;
13320 /* Found consecutive range of addresses. */
13321 ret = PC_BOUNDS_HIGH_LOW;
13325 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13328 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13329 We take advantage of the fact that DW_AT_ranges does not appear
13330 in DW_TAG_compile_unit of DWO files. */
13331 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13332 unsigned int ranges_offset = (DW_UNSND (attr)
13333 + (need_ranges_base
13337 /* Value of the DW_AT_ranges attribute is the offset in the
13338 .debug_ranges section. */
13339 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13340 return PC_BOUNDS_INVALID;
13341 /* Found discontinuous range of addresses. */
13342 ret = PC_BOUNDS_RANGES;
13345 return PC_BOUNDS_NOT_PRESENT;
13348 /* read_partial_die has also the strict LOW < HIGH requirement. */
13350 return PC_BOUNDS_INVALID;
13352 /* When using the GNU linker, .gnu.linkonce. sections are used to
13353 eliminate duplicate copies of functions and vtables and such.
13354 The linker will arbitrarily choose one and discard the others.
13355 The AT_*_pc values for such functions refer to local labels in
13356 these sections. If the section from that file was discarded, the
13357 labels are not in the output, so the relocs get a value of 0.
13358 If this is a discarded function, mark the pc bounds as invalid,
13359 so that GDB will ignore it. */
13360 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13361 return PC_BOUNDS_INVALID;
13369 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13370 its low and high PC addresses. Do nothing if these addresses could not
13371 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13372 and HIGHPC to the high address if greater than HIGHPC. */
13375 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13376 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13377 struct dwarf2_cu *cu)
13379 CORE_ADDR low, high;
13380 struct die_info *child = die->child;
13382 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13384 *lowpc = std::min (*lowpc, low);
13385 *highpc = std::max (*highpc, high);
13388 /* If the language does not allow nested subprograms (either inside
13389 subprograms or lexical blocks), we're done. */
13390 if (cu->language != language_ada)
13393 /* Check all the children of the given DIE. If it contains nested
13394 subprograms, then check their pc bounds. Likewise, we need to
13395 check lexical blocks as well, as they may also contain subprogram
13397 while (child && child->tag)
13399 if (child->tag == DW_TAG_subprogram
13400 || child->tag == DW_TAG_lexical_block)
13401 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13402 child = sibling_die (child);
13406 /* Get the low and high pc's represented by the scope DIE, and store
13407 them in *LOWPC and *HIGHPC. If the correct values can't be
13408 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13411 get_scope_pc_bounds (struct die_info *die,
13412 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13413 struct dwarf2_cu *cu)
13415 CORE_ADDR best_low = (CORE_ADDR) -1;
13416 CORE_ADDR best_high = (CORE_ADDR) 0;
13417 CORE_ADDR current_low, current_high;
13419 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
13420 >= PC_BOUNDS_RANGES)
13422 best_low = current_low;
13423 best_high = current_high;
13427 struct die_info *child = die->child;
13429 while (child && child->tag)
13431 switch (child->tag) {
13432 case DW_TAG_subprogram:
13433 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13435 case DW_TAG_namespace:
13436 case DW_TAG_module:
13437 /* FIXME: carlton/2004-01-16: Should we do this for
13438 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13439 that current GCC's always emit the DIEs corresponding
13440 to definitions of methods of classes as children of a
13441 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13442 the DIEs giving the declarations, which could be
13443 anywhere). But I don't see any reason why the
13444 standards says that they have to be there. */
13445 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
13447 if (current_low != ((CORE_ADDR) -1))
13449 best_low = std::min (best_low, current_low);
13450 best_high = std::max (best_high, current_high);
13458 child = sibling_die (child);
13463 *highpc = best_high;
13466 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13470 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13471 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13473 struct objfile *objfile = cu->objfile;
13474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13475 struct attribute *attr;
13476 struct attribute *attr_high;
13478 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13481 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13484 CORE_ADDR low = attr_value_as_address (attr);
13485 CORE_ADDR high = attr_value_as_address (attr_high);
13487 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13490 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13491 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13492 record_block_range (block, low, high - 1);
13496 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13499 bfd *obfd = objfile->obfd;
13500 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13501 We take advantage of the fact that DW_AT_ranges does not appear
13502 in DW_TAG_compile_unit of DWO files. */
13503 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13505 /* The value of the DW_AT_ranges attribute is the offset of the
13506 address range list in the .debug_ranges section. */
13507 unsigned long offset = (DW_UNSND (attr)
13508 + (need_ranges_base ? cu->ranges_base : 0));
13509 const gdb_byte *buffer;
13511 /* For some target architectures, but not others, the
13512 read_address function sign-extends the addresses it returns.
13513 To recognize base address selection entries, we need a
13515 unsigned int addr_size = cu->header.addr_size;
13516 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13518 /* The base address, to which the next pair is relative. Note
13519 that this 'base' is a DWARF concept: most entries in a range
13520 list are relative, to reduce the number of relocs against the
13521 debugging information. This is separate from this function's
13522 'baseaddr' argument, which GDB uses to relocate debugging
13523 information from a shared library based on the address at
13524 which the library was loaded. */
13525 CORE_ADDR base = cu->base_address;
13526 int base_known = cu->base_known;
13528 dwarf2_ranges_process (offset, cu,
13529 [&] (CORE_ADDR start, CORE_ADDR end)
13533 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13534 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13535 record_block_range (block, start, end - 1);
13540 /* Check whether the producer field indicates either of GCC < 4.6, or the
13541 Intel C/C++ compiler, and cache the result in CU. */
13544 check_producer (struct dwarf2_cu *cu)
13548 if (cu->producer == NULL)
13550 /* For unknown compilers expect their behavior is DWARF version
13553 GCC started to support .debug_types sections by -gdwarf-4 since
13554 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13555 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13556 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13557 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13559 else if (producer_is_gcc (cu->producer, &major, &minor))
13561 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13562 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13564 else if (producer_is_icc (cu->producer, &major, &minor))
13565 cu->producer_is_icc_lt_14 = major < 14;
13568 /* For other non-GCC compilers, expect their behavior is DWARF version
13572 cu->checked_producer = 1;
13575 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13576 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13577 during 4.6.0 experimental. */
13580 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13582 if (!cu->checked_producer)
13583 check_producer (cu);
13585 return cu->producer_is_gxx_lt_4_6;
13588 /* Return the default accessibility type if it is not overriden by
13589 DW_AT_accessibility. */
13591 static enum dwarf_access_attribute
13592 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13594 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13596 /* The default DWARF 2 accessibility for members is public, the default
13597 accessibility for inheritance is private. */
13599 if (die->tag != DW_TAG_inheritance)
13600 return DW_ACCESS_public;
13602 return DW_ACCESS_private;
13606 /* DWARF 3+ defines the default accessibility a different way. The same
13607 rules apply now for DW_TAG_inheritance as for the members and it only
13608 depends on the container kind. */
13610 if (die->parent->tag == DW_TAG_class_type)
13611 return DW_ACCESS_private;
13613 return DW_ACCESS_public;
13617 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13618 offset. If the attribute was not found return 0, otherwise return
13619 1. If it was found but could not properly be handled, set *OFFSET
13623 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13626 struct attribute *attr;
13628 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13633 /* Note that we do not check for a section offset first here.
13634 This is because DW_AT_data_member_location is new in DWARF 4,
13635 so if we see it, we can assume that a constant form is really
13636 a constant and not a section offset. */
13637 if (attr_form_is_constant (attr))
13638 *offset = dwarf2_get_attr_constant_value (attr, 0);
13639 else if (attr_form_is_section_offset (attr))
13640 dwarf2_complex_location_expr_complaint ();
13641 else if (attr_form_is_block (attr))
13642 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13644 dwarf2_complex_location_expr_complaint ();
13652 /* Add an aggregate field to the field list. */
13655 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13656 struct dwarf2_cu *cu)
13658 struct objfile *objfile = cu->objfile;
13659 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13660 struct nextfield *new_field;
13661 struct attribute *attr;
13663 const char *fieldname = "";
13665 /* Allocate a new field list entry and link it in. */
13666 new_field = XNEW (struct nextfield);
13667 make_cleanup (xfree, new_field);
13668 memset (new_field, 0, sizeof (struct nextfield));
13670 if (die->tag == DW_TAG_inheritance)
13672 new_field->next = fip->baseclasses;
13673 fip->baseclasses = new_field;
13677 new_field->next = fip->fields;
13678 fip->fields = new_field;
13682 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13684 new_field->accessibility = DW_UNSND (attr);
13686 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
13687 if (new_field->accessibility != DW_ACCESS_public)
13688 fip->non_public_fields = 1;
13690 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13692 new_field->virtuality = DW_UNSND (attr);
13694 new_field->virtuality = DW_VIRTUALITY_none;
13696 fp = &new_field->field;
13698 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
13702 /* Data member other than a C++ static data member. */
13704 /* Get type of field. */
13705 fp->type = die_type (die, cu);
13707 SET_FIELD_BITPOS (*fp, 0);
13709 /* Get bit size of field (zero if none). */
13710 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13713 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13717 FIELD_BITSIZE (*fp) = 0;
13720 /* Get bit offset of field. */
13721 if (handle_data_member_location (die, cu, &offset))
13722 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13723 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13726 if (gdbarch_bits_big_endian (gdbarch))
13728 /* For big endian bits, the DW_AT_bit_offset gives the
13729 additional bit offset from the MSB of the containing
13730 anonymous object to the MSB of the field. We don't
13731 have to do anything special since we don't need to
13732 know the size of the anonymous object. */
13733 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
13737 /* For little endian bits, compute the bit offset to the
13738 MSB of the anonymous object, subtract off the number of
13739 bits from the MSB of the field to the MSB of the
13740 object, and then subtract off the number of bits of
13741 the field itself. The result is the bit offset of
13742 the LSB of the field. */
13743 int anonymous_size;
13744 int bit_offset = DW_UNSND (attr);
13746 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13749 /* The size of the anonymous object containing
13750 the bit field is explicit, so use the
13751 indicated size (in bytes). */
13752 anonymous_size = DW_UNSND (attr);
13756 /* The size of the anonymous object containing
13757 the bit field must be inferred from the type
13758 attribute of the data member containing the
13760 anonymous_size = TYPE_LENGTH (fp->type);
13762 SET_FIELD_BITPOS (*fp,
13763 (FIELD_BITPOS (*fp)
13764 + anonymous_size * bits_per_byte
13765 - bit_offset - FIELD_BITSIZE (*fp)));
13768 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13770 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13771 + dwarf2_get_attr_constant_value (attr, 0)));
13773 /* Get name of field. */
13774 fieldname = dwarf2_name (die, cu);
13775 if (fieldname == NULL)
13778 /* The name is already allocated along with this objfile, so we don't
13779 need to duplicate it for the type. */
13780 fp->name = fieldname;
13782 /* Change accessibility for artificial fields (e.g. virtual table
13783 pointer or virtual base class pointer) to private. */
13784 if (dwarf2_attr (die, DW_AT_artificial, cu))
13786 FIELD_ARTIFICIAL (*fp) = 1;
13787 new_field->accessibility = DW_ACCESS_private;
13788 fip->non_public_fields = 1;
13791 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13793 /* C++ static member. */
13795 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13796 is a declaration, but all versions of G++ as of this writing
13797 (so through at least 3.2.1) incorrectly generate
13798 DW_TAG_variable tags. */
13800 const char *physname;
13802 /* Get name of field. */
13803 fieldname = dwarf2_name (die, cu);
13804 if (fieldname == NULL)
13807 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13809 /* Only create a symbol if this is an external value.
13810 new_symbol checks this and puts the value in the global symbol
13811 table, which we want. If it is not external, new_symbol
13812 will try to put the value in cu->list_in_scope which is wrong. */
13813 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13815 /* A static const member, not much different than an enum as far as
13816 we're concerned, except that we can support more types. */
13817 new_symbol (die, NULL, cu);
13820 /* Get physical name. */
13821 physname = dwarf2_physname (fieldname, die, cu);
13823 /* The name is already allocated along with this objfile, so we don't
13824 need to duplicate it for the type. */
13825 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
13826 FIELD_TYPE (*fp) = die_type (die, cu);
13827 FIELD_NAME (*fp) = fieldname;
13829 else if (die->tag == DW_TAG_inheritance)
13833 /* C++ base class field. */
13834 if (handle_data_member_location (die, cu, &offset))
13835 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13836 FIELD_BITSIZE (*fp) = 0;
13837 FIELD_TYPE (*fp) = die_type (die, cu);
13838 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13839 fip->nbaseclasses++;
13843 /* Add a typedef defined in the scope of the FIP's class. */
13846 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13847 struct dwarf2_cu *cu)
13849 struct typedef_field_list *new_field;
13850 struct typedef_field *fp;
13852 /* Allocate a new field list entry and link it in. */
13853 new_field = XCNEW (struct typedef_field_list);
13854 make_cleanup (xfree, new_field);
13856 gdb_assert (die->tag == DW_TAG_typedef);
13858 fp = &new_field->field;
13860 /* Get name of field. */
13861 fp->name = dwarf2_name (die, cu);
13862 if (fp->name == NULL)
13865 fp->type = read_type_die (die, cu);
13867 /* Save accessibility. */
13868 enum dwarf_access_attribute accessibility;
13869 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13871 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13873 accessibility = dwarf2_default_access_attribute (die, cu);
13874 switch (accessibility)
13876 case DW_ACCESS_public:
13877 /* The assumed value if neither private nor protected. */
13879 case DW_ACCESS_private:
13880 fp->is_private = 1;
13882 case DW_ACCESS_protected:
13883 fp->is_protected = 1;
13886 complaint (&symfile_complaints,
13887 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
13890 new_field->next = fip->typedef_field_list;
13891 fip->typedef_field_list = new_field;
13892 fip->typedef_field_list_count++;
13895 /* Create the vector of fields, and attach it to the type. */
13898 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13899 struct dwarf2_cu *cu)
13901 int nfields = fip->nfields;
13903 /* Record the field count, allocate space for the array of fields,
13904 and create blank accessibility bitfields if necessary. */
13905 TYPE_NFIELDS (type) = nfields;
13906 TYPE_FIELDS (type) = (struct field *)
13907 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13908 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13910 if (fip->non_public_fields && cu->language != language_ada)
13912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13914 TYPE_FIELD_PRIVATE_BITS (type) =
13915 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13916 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13918 TYPE_FIELD_PROTECTED_BITS (type) =
13919 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13920 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13922 TYPE_FIELD_IGNORE_BITS (type) =
13923 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13924 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13927 /* If the type has baseclasses, allocate and clear a bit vector for
13928 TYPE_FIELD_VIRTUAL_BITS. */
13929 if (fip->nbaseclasses && cu->language != language_ada)
13931 int num_bytes = B_BYTES (fip->nbaseclasses);
13932 unsigned char *pointer;
13934 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13935 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13936 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13937 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13938 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13941 /* Copy the saved-up fields into the field vector. Start from the head of
13942 the list, adding to the tail of the field array, so that they end up in
13943 the same order in the array in which they were added to the list. */
13944 while (nfields-- > 0)
13946 struct nextfield *fieldp;
13950 fieldp = fip->fields;
13951 fip->fields = fieldp->next;
13955 fieldp = fip->baseclasses;
13956 fip->baseclasses = fieldp->next;
13959 TYPE_FIELD (type, nfields) = fieldp->field;
13960 switch (fieldp->accessibility)
13962 case DW_ACCESS_private:
13963 if (cu->language != language_ada)
13964 SET_TYPE_FIELD_PRIVATE (type, nfields);
13967 case DW_ACCESS_protected:
13968 if (cu->language != language_ada)
13969 SET_TYPE_FIELD_PROTECTED (type, nfields);
13972 case DW_ACCESS_public:
13976 /* Unknown accessibility. Complain and treat it as public. */
13978 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13979 fieldp->accessibility);
13983 if (nfields < fip->nbaseclasses)
13985 switch (fieldp->virtuality)
13987 case DW_VIRTUALITY_virtual:
13988 case DW_VIRTUALITY_pure_virtual:
13989 if (cu->language == language_ada)
13990 error (_("unexpected virtuality in component of Ada type"));
13991 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13998 /* Return true if this member function is a constructor, false
14002 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14004 const char *fieldname;
14005 const char *type_name;
14008 if (die->parent == NULL)
14011 if (die->parent->tag != DW_TAG_structure_type
14012 && die->parent->tag != DW_TAG_union_type
14013 && die->parent->tag != DW_TAG_class_type)
14016 fieldname = dwarf2_name (die, cu);
14017 type_name = dwarf2_name (die->parent, cu);
14018 if (fieldname == NULL || type_name == NULL)
14021 len = strlen (fieldname);
14022 return (strncmp (fieldname, type_name, len) == 0
14023 && (type_name[len] == '\0' || type_name[len] == '<'));
14026 /* Add a member function to the proper fieldlist. */
14029 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14030 struct type *type, struct dwarf2_cu *cu)
14032 struct objfile *objfile = cu->objfile;
14033 struct attribute *attr;
14034 struct fnfieldlist *flp;
14036 struct fn_field *fnp;
14037 const char *fieldname;
14038 struct nextfnfield *new_fnfield;
14039 struct type *this_type;
14040 enum dwarf_access_attribute accessibility;
14042 if (cu->language == language_ada)
14043 error (_("unexpected member function in Ada type"));
14045 /* Get name of member function. */
14046 fieldname = dwarf2_name (die, cu);
14047 if (fieldname == NULL)
14050 /* Look up member function name in fieldlist. */
14051 for (i = 0; i < fip->nfnfields; i++)
14053 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14057 /* Create new list element if necessary. */
14058 if (i < fip->nfnfields)
14059 flp = &fip->fnfieldlists[i];
14062 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14064 fip->fnfieldlists = (struct fnfieldlist *)
14065 xrealloc (fip->fnfieldlists,
14066 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
14067 * sizeof (struct fnfieldlist));
14068 if (fip->nfnfields == 0)
14069 make_cleanup (free_current_contents, &fip->fnfieldlists);
14071 flp = &fip->fnfieldlists[fip->nfnfields];
14072 flp->name = fieldname;
14075 i = fip->nfnfields++;
14078 /* Create a new member function field and chain it to the field list
14080 new_fnfield = XNEW (struct nextfnfield);
14081 make_cleanup (xfree, new_fnfield);
14082 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14083 new_fnfield->next = flp->head;
14084 flp->head = new_fnfield;
14087 /* Fill in the member function field info. */
14088 fnp = &new_fnfield->fnfield;
14090 /* Delay processing of the physname until later. */
14091 if (cu->language == language_cplus)
14093 add_to_method_list (type, i, flp->length - 1, fieldname,
14098 const char *physname = dwarf2_physname (fieldname, die, cu);
14099 fnp->physname = physname ? physname : "";
14102 fnp->type = alloc_type (objfile);
14103 this_type = read_type_die (die, cu);
14104 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14106 int nparams = TYPE_NFIELDS (this_type);
14108 /* TYPE is the domain of this method, and THIS_TYPE is the type
14109 of the method itself (TYPE_CODE_METHOD). */
14110 smash_to_method_type (fnp->type, type,
14111 TYPE_TARGET_TYPE (this_type),
14112 TYPE_FIELDS (this_type),
14113 TYPE_NFIELDS (this_type),
14114 TYPE_VARARGS (this_type));
14116 /* Handle static member functions.
14117 Dwarf2 has no clean way to discern C++ static and non-static
14118 member functions. G++ helps GDB by marking the first
14119 parameter for non-static member functions (which is the this
14120 pointer) as artificial. We obtain this information from
14121 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14122 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14123 fnp->voffset = VOFFSET_STATIC;
14126 complaint (&symfile_complaints, _("member function type missing for '%s'"),
14127 dwarf2_full_name (fieldname, die, cu));
14129 /* Get fcontext from DW_AT_containing_type if present. */
14130 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14131 fnp->fcontext = die_containing_type (die, cu);
14133 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14134 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14136 /* Get accessibility. */
14137 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14139 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14141 accessibility = dwarf2_default_access_attribute (die, cu);
14142 switch (accessibility)
14144 case DW_ACCESS_private:
14145 fnp->is_private = 1;
14147 case DW_ACCESS_protected:
14148 fnp->is_protected = 1;
14152 /* Check for artificial methods. */
14153 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14154 if (attr && DW_UNSND (attr) != 0)
14155 fnp->is_artificial = 1;
14157 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14159 /* Get index in virtual function table if it is a virtual member
14160 function. For older versions of GCC, this is an offset in the
14161 appropriate virtual table, as specified by DW_AT_containing_type.
14162 For everyone else, it is an expression to be evaluated relative
14163 to the object address. */
14165 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14168 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
14170 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14172 /* Old-style GCC. */
14173 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14175 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14176 || (DW_BLOCK (attr)->size > 1
14177 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14178 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14180 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14181 if ((fnp->voffset % cu->header.addr_size) != 0)
14182 dwarf2_complex_location_expr_complaint ();
14184 fnp->voffset /= cu->header.addr_size;
14188 dwarf2_complex_location_expr_complaint ();
14190 if (!fnp->fcontext)
14192 /* If there is no `this' field and no DW_AT_containing_type,
14193 we cannot actually find a base class context for the
14195 if (TYPE_NFIELDS (this_type) == 0
14196 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14198 complaint (&symfile_complaints,
14199 _("cannot determine context for virtual member "
14200 "function \"%s\" (offset %d)"),
14201 fieldname, to_underlying (die->sect_off));
14206 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14210 else if (attr_form_is_section_offset (attr))
14212 dwarf2_complex_location_expr_complaint ();
14216 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14222 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14223 if (attr && DW_UNSND (attr))
14225 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14226 complaint (&symfile_complaints,
14227 _("Member function \"%s\" (offset %d) is virtual "
14228 "but the vtable offset is not specified"),
14229 fieldname, to_underlying (die->sect_off));
14230 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14231 TYPE_CPLUS_DYNAMIC (type) = 1;
14236 /* Create the vector of member function fields, and attach it to the type. */
14239 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14240 struct dwarf2_cu *cu)
14242 struct fnfieldlist *flp;
14245 if (cu->language == language_ada)
14246 error (_("unexpected member functions in Ada type"));
14248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14249 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14250 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14252 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14254 struct nextfnfield *nfp = flp->head;
14255 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14258 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14259 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14260 fn_flp->fn_fields = (struct fn_field *)
14261 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14262 for (k = flp->length; (k--, nfp); nfp = nfp->next)
14263 fn_flp->fn_fields[k] = nfp->fnfield;
14266 TYPE_NFN_FIELDS (type) = fip->nfnfields;
14269 /* Returns non-zero if NAME is the name of a vtable member in CU's
14270 language, zero otherwise. */
14272 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14274 static const char vptr[] = "_vptr";
14275 static const char vtable[] = "vtable";
14277 /* Look for the C++ form of the vtable. */
14278 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14284 /* GCC outputs unnamed structures that are really pointers to member
14285 functions, with the ABI-specified layout. If TYPE describes
14286 such a structure, smash it into a member function type.
14288 GCC shouldn't do this; it should just output pointer to member DIEs.
14289 This is GCC PR debug/28767. */
14292 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14294 struct type *pfn_type, *self_type, *new_type;
14296 /* Check for a structure with no name and two children. */
14297 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14300 /* Check for __pfn and __delta members. */
14301 if (TYPE_FIELD_NAME (type, 0) == NULL
14302 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14303 || TYPE_FIELD_NAME (type, 1) == NULL
14304 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14307 /* Find the type of the method. */
14308 pfn_type = TYPE_FIELD_TYPE (type, 0);
14309 if (pfn_type == NULL
14310 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14311 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14314 /* Look for the "this" argument. */
14315 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14316 if (TYPE_NFIELDS (pfn_type) == 0
14317 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14318 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14321 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14322 new_type = alloc_type (objfile);
14323 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14324 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14325 TYPE_VARARGS (pfn_type));
14326 smash_to_methodptr_type (type, new_type);
14330 /* Called when we find the DIE that starts a structure or union scope
14331 (definition) to create a type for the structure or union. Fill in
14332 the type's name and general properties; the members will not be
14333 processed until process_structure_scope. A symbol table entry for
14334 the type will also not be done until process_structure_scope (assuming
14335 the type has a name).
14337 NOTE: we need to call these functions regardless of whether or not the
14338 DIE has a DW_AT_name attribute, since it might be an anonymous
14339 structure or union. This gets the type entered into our set of
14340 user defined types. */
14342 static struct type *
14343 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14345 struct objfile *objfile = cu->objfile;
14347 struct attribute *attr;
14350 /* If the definition of this type lives in .debug_types, read that type.
14351 Don't follow DW_AT_specification though, that will take us back up
14352 the chain and we want to go down. */
14353 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14356 type = get_DW_AT_signature_type (die, attr, cu);
14358 /* The type's CU may not be the same as CU.
14359 Ensure TYPE is recorded with CU in die_type_hash. */
14360 return set_die_type (die, type, cu);
14363 type = alloc_type (objfile);
14364 INIT_CPLUS_SPECIFIC (type);
14366 name = dwarf2_name (die, cu);
14369 if (cu->language == language_cplus
14370 || cu->language == language_d
14371 || cu->language == language_rust)
14373 const char *full_name = dwarf2_full_name (name, die, cu);
14375 /* dwarf2_full_name might have already finished building the DIE's
14376 type. If so, there is no need to continue. */
14377 if (get_die_type (die, cu) != NULL)
14378 return get_die_type (die, cu);
14380 TYPE_TAG_NAME (type) = full_name;
14381 if (die->tag == DW_TAG_structure_type
14382 || die->tag == DW_TAG_class_type)
14383 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14387 /* The name is already allocated along with this objfile, so
14388 we don't need to duplicate it for the type. */
14389 TYPE_TAG_NAME (type) = name;
14390 if (die->tag == DW_TAG_class_type)
14391 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14395 if (die->tag == DW_TAG_structure_type)
14397 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14399 else if (die->tag == DW_TAG_union_type)
14401 TYPE_CODE (type) = TYPE_CODE_UNION;
14405 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14408 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14409 TYPE_DECLARED_CLASS (type) = 1;
14411 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14414 if (attr_form_is_constant (attr))
14415 TYPE_LENGTH (type) = DW_UNSND (attr);
14418 /* For the moment, dynamic type sizes are not supported
14419 by GDB's struct type. The actual size is determined
14420 on-demand when resolving the type of a given object,
14421 so set the type's length to zero for now. Otherwise,
14422 we record an expression as the length, and that expression
14423 could lead to a very large value, which could eventually
14424 lead to us trying to allocate that much memory when creating
14425 a value of that type. */
14426 TYPE_LENGTH (type) = 0;
14431 TYPE_LENGTH (type) = 0;
14434 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14436 /* ICC<14 does not output the required DW_AT_declaration on
14437 incomplete types, but gives them a size of zero. */
14438 TYPE_STUB (type) = 1;
14441 TYPE_STUB_SUPPORTED (type) = 1;
14443 if (die_is_declaration (die, cu))
14444 TYPE_STUB (type) = 1;
14445 else if (attr == NULL && die->child == NULL
14446 && producer_is_realview (cu->producer))
14447 /* RealView does not output the required DW_AT_declaration
14448 on incomplete types. */
14449 TYPE_STUB (type) = 1;
14451 /* We need to add the type field to the die immediately so we don't
14452 infinitely recurse when dealing with pointers to the structure
14453 type within the structure itself. */
14454 set_die_type (die, type, cu);
14456 /* set_die_type should be already done. */
14457 set_descriptive_type (type, die, cu);
14462 /* Finish creating a structure or union type, including filling in
14463 its members and creating a symbol for it. */
14466 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14468 struct objfile *objfile = cu->objfile;
14469 struct die_info *child_die;
14472 type = get_die_type (die, cu);
14474 type = read_structure_type (die, cu);
14476 if (die->child != NULL && ! die_is_declaration (die, cu))
14478 struct field_info fi;
14479 std::vector<struct symbol *> template_args;
14480 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
14482 memset (&fi, 0, sizeof (struct field_info));
14484 child_die = die->child;
14486 while (child_die && child_die->tag)
14488 if (child_die->tag == DW_TAG_member
14489 || child_die->tag == DW_TAG_variable)
14491 /* NOTE: carlton/2002-11-05: A C++ static data member
14492 should be a DW_TAG_member that is a declaration, but
14493 all versions of G++ as of this writing (so through at
14494 least 3.2.1) incorrectly generate DW_TAG_variable
14495 tags for them instead. */
14496 dwarf2_add_field (&fi, child_die, cu);
14498 else if (child_die->tag == DW_TAG_subprogram)
14500 /* Rust doesn't have member functions in the C++ sense.
14501 However, it does emit ordinary functions as children
14502 of a struct DIE. */
14503 if (cu->language == language_rust)
14504 read_func_scope (child_die, cu);
14507 /* C++ member function. */
14508 dwarf2_add_member_fn (&fi, child_die, type, cu);
14511 else if (child_die->tag == DW_TAG_inheritance)
14513 /* C++ base class field. */
14514 dwarf2_add_field (&fi, child_die, cu);
14516 else if (child_die->tag == DW_TAG_typedef)
14517 dwarf2_add_typedef (&fi, child_die, cu);
14518 else if (child_die->tag == DW_TAG_template_type_param
14519 || child_die->tag == DW_TAG_template_value_param)
14521 struct symbol *arg = new_symbol (child_die, NULL, cu);
14524 template_args.push_back (arg);
14527 child_die = sibling_die (child_die);
14530 /* Attach template arguments to type. */
14531 if (!template_args.empty ())
14533 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14534 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14535 TYPE_TEMPLATE_ARGUMENTS (type)
14536 = XOBNEWVEC (&objfile->objfile_obstack,
14538 TYPE_N_TEMPLATE_ARGUMENTS (type));
14539 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14540 template_args.data (),
14541 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14542 * sizeof (struct symbol *)));
14545 /* Attach fields and member functions to the type. */
14547 dwarf2_attach_fields_to_type (&fi, type, cu);
14550 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14552 /* Get the type which refers to the base class (possibly this
14553 class itself) which contains the vtable pointer for the current
14554 class from the DW_AT_containing_type attribute. This use of
14555 DW_AT_containing_type is a GNU extension. */
14557 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14559 struct type *t = die_containing_type (die, cu);
14561 set_type_vptr_basetype (type, t);
14566 /* Our own class provides vtbl ptr. */
14567 for (i = TYPE_NFIELDS (t) - 1;
14568 i >= TYPE_N_BASECLASSES (t);
14571 const char *fieldname = TYPE_FIELD_NAME (t, i);
14573 if (is_vtable_name (fieldname, cu))
14575 set_type_vptr_fieldno (type, i);
14580 /* Complain if virtual function table field not found. */
14581 if (i < TYPE_N_BASECLASSES (t))
14582 complaint (&symfile_complaints,
14583 _("virtual function table pointer "
14584 "not found when defining class '%s'"),
14585 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14590 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14593 else if (cu->producer
14594 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14596 /* The IBM XLC compiler does not provide direct indication
14597 of the containing type, but the vtable pointer is
14598 always named __vfp. */
14602 for (i = TYPE_NFIELDS (type) - 1;
14603 i >= TYPE_N_BASECLASSES (type);
14606 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14608 set_type_vptr_fieldno (type, i);
14609 set_type_vptr_basetype (type, type);
14616 /* Copy fi.typedef_field_list linked list elements content into the
14617 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14618 if (fi.typedef_field_list)
14620 int i = fi.typedef_field_list_count;
14622 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14623 TYPE_TYPEDEF_FIELD_ARRAY (type)
14624 = ((struct typedef_field *)
14625 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
14626 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14628 /* Reverse the list order to keep the debug info elements order. */
14631 struct typedef_field *dest, *src;
14633 dest = &TYPE_TYPEDEF_FIELD (type, i);
14634 src = &fi.typedef_field_list->field;
14635 fi.typedef_field_list = fi.typedef_field_list->next;
14640 do_cleanups (back_to);
14643 quirk_gcc_member_function_pointer (type, objfile);
14645 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14646 snapshots) has been known to create a die giving a declaration
14647 for a class that has, as a child, a die giving a definition for a
14648 nested class. So we have to process our children even if the
14649 current die is a declaration. Normally, of course, a declaration
14650 won't have any children at all. */
14652 child_die = die->child;
14654 while (child_die != NULL && child_die->tag)
14656 if (child_die->tag == DW_TAG_member
14657 || child_die->tag == DW_TAG_variable
14658 || child_die->tag == DW_TAG_inheritance
14659 || child_die->tag == DW_TAG_template_value_param
14660 || child_die->tag == DW_TAG_template_type_param)
14665 process_die (child_die, cu);
14667 child_die = sibling_die (child_die);
14670 /* Do not consider external references. According to the DWARF standard,
14671 these DIEs are identified by the fact that they have no byte_size
14672 attribute, and a declaration attribute. */
14673 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14674 || !die_is_declaration (die, cu))
14675 new_symbol (die, type, cu);
14678 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
14679 update TYPE using some information only available in DIE's children. */
14682 update_enumeration_type_from_children (struct die_info *die,
14684 struct dwarf2_cu *cu)
14686 struct die_info *child_die;
14687 int unsigned_enum = 1;
14691 auto_obstack obstack;
14693 for (child_die = die->child;
14694 child_die != NULL && child_die->tag;
14695 child_die = sibling_die (child_die))
14697 struct attribute *attr;
14699 const gdb_byte *bytes;
14700 struct dwarf2_locexpr_baton *baton;
14703 if (child_die->tag != DW_TAG_enumerator)
14706 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14710 name = dwarf2_name (child_die, cu);
14712 name = "<anonymous enumerator>";
14714 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14715 &value, &bytes, &baton);
14721 else if ((mask & value) != 0)
14726 /* If we already know that the enum type is neither unsigned, nor
14727 a flag type, no need to look at the rest of the enumerates. */
14728 if (!unsigned_enum && !flag_enum)
14733 TYPE_UNSIGNED (type) = 1;
14735 TYPE_FLAG_ENUM (type) = 1;
14738 /* Given a DW_AT_enumeration_type die, set its type. We do not
14739 complete the type's fields yet, or create any symbols. */
14741 static struct type *
14742 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
14744 struct objfile *objfile = cu->objfile;
14746 struct attribute *attr;
14749 /* If the definition of this type lives in .debug_types, read that type.
14750 Don't follow DW_AT_specification though, that will take us back up
14751 the chain and we want to go down. */
14752 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14755 type = get_DW_AT_signature_type (die, attr, cu);
14757 /* The type's CU may not be the same as CU.
14758 Ensure TYPE is recorded with CU in die_type_hash. */
14759 return set_die_type (die, type, cu);
14762 type = alloc_type (objfile);
14764 TYPE_CODE (type) = TYPE_CODE_ENUM;
14765 name = dwarf2_full_name (NULL, die, cu);
14767 TYPE_TAG_NAME (type) = name;
14769 attr = dwarf2_attr (die, DW_AT_type, cu);
14772 struct type *underlying_type = die_type (die, cu);
14774 TYPE_TARGET_TYPE (type) = underlying_type;
14777 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14780 TYPE_LENGTH (type) = DW_UNSND (attr);
14784 TYPE_LENGTH (type) = 0;
14787 /* The enumeration DIE can be incomplete. In Ada, any type can be
14788 declared as private in the package spec, and then defined only
14789 inside the package body. Such types are known as Taft Amendment
14790 Types. When another package uses such a type, an incomplete DIE
14791 may be generated by the compiler. */
14792 if (die_is_declaration (die, cu))
14793 TYPE_STUB (type) = 1;
14795 /* Finish the creation of this type by using the enum's children.
14796 We must call this even when the underlying type has been provided
14797 so that we can determine if we're looking at a "flag" enum. */
14798 update_enumeration_type_from_children (die, type, cu);
14800 /* If this type has an underlying type that is not a stub, then we
14801 may use its attributes. We always use the "unsigned" attribute
14802 in this situation, because ordinarily we guess whether the type
14803 is unsigned -- but the guess can be wrong and the underlying type
14804 can tell us the reality. However, we defer to a local size
14805 attribute if one exists, because this lets the compiler override
14806 the underlying type if needed. */
14807 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14809 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14810 if (TYPE_LENGTH (type) == 0)
14811 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14814 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14816 return set_die_type (die, type, cu);
14819 /* Given a pointer to a die which begins an enumeration, process all
14820 the dies that define the members of the enumeration, and create the
14821 symbol for the enumeration type.
14823 NOTE: We reverse the order of the element list. */
14826 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14828 struct type *this_type;
14830 this_type = get_die_type (die, cu);
14831 if (this_type == NULL)
14832 this_type = read_enumeration_type (die, cu);
14834 if (die->child != NULL)
14836 struct die_info *child_die;
14837 struct symbol *sym;
14838 struct field *fields = NULL;
14839 int num_fields = 0;
14842 child_die = die->child;
14843 while (child_die && child_die->tag)
14845 if (child_die->tag != DW_TAG_enumerator)
14847 process_die (child_die, cu);
14851 name = dwarf2_name (child_die, cu);
14854 sym = new_symbol (child_die, this_type, cu);
14856 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14858 fields = (struct field *)
14860 (num_fields + DW_FIELD_ALLOC_CHUNK)
14861 * sizeof (struct field));
14864 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
14865 FIELD_TYPE (fields[num_fields]) = NULL;
14866 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
14867 FIELD_BITSIZE (fields[num_fields]) = 0;
14873 child_die = sibling_die (child_die);
14878 TYPE_NFIELDS (this_type) = num_fields;
14879 TYPE_FIELDS (this_type) = (struct field *)
14880 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14881 memcpy (TYPE_FIELDS (this_type), fields,
14882 sizeof (struct field) * num_fields);
14887 /* If we are reading an enum from a .debug_types unit, and the enum
14888 is a declaration, and the enum is not the signatured type in the
14889 unit, then we do not want to add a symbol for it. Adding a
14890 symbol would in some cases obscure the true definition of the
14891 enum, giving users an incomplete type when the definition is
14892 actually available. Note that we do not want to do this for all
14893 enums which are just declarations, because C++0x allows forward
14894 enum declarations. */
14895 if (cu->per_cu->is_debug_types
14896 && die_is_declaration (die, cu))
14898 struct signatured_type *sig_type;
14900 sig_type = (struct signatured_type *) cu->per_cu;
14901 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14902 if (sig_type->type_offset_in_section != die->sect_off)
14906 new_symbol (die, this_type, cu);
14909 /* Extract all information from a DW_TAG_array_type DIE and put it in
14910 the DIE's type field. For now, this only handles one dimensional
14913 static struct type *
14914 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14916 struct objfile *objfile = cu->objfile;
14917 struct die_info *child_die;
14919 struct type *element_type, *range_type, *index_type;
14920 struct attribute *attr;
14922 unsigned int bit_stride = 0;
14924 element_type = die_type (die, cu);
14926 /* The die_type call above may have already set the type for this DIE. */
14927 type = get_die_type (die, cu);
14931 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14933 bit_stride = DW_UNSND (attr) * 8;
14935 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14937 bit_stride = DW_UNSND (attr);
14939 /* Irix 6.2 native cc creates array types without children for
14940 arrays with unspecified length. */
14941 if (die->child == NULL)
14943 index_type = objfile_type (objfile)->builtin_int;
14944 range_type = create_static_range_type (NULL, index_type, 0, -1);
14945 type = create_array_type_with_stride (NULL, element_type, range_type,
14947 return set_die_type (die, type, cu);
14950 std::vector<struct type *> range_types;
14951 child_die = die->child;
14952 while (child_die && child_die->tag)
14954 if (child_die->tag == DW_TAG_subrange_type)
14956 struct type *child_type = read_type_die (child_die, cu);
14958 if (child_type != NULL)
14960 /* The range type was succesfully read. Save it for the
14961 array type creation. */
14962 range_types.push_back (child_type);
14965 child_die = sibling_die (child_die);
14968 /* Dwarf2 dimensions are output from left to right, create the
14969 necessary array types in backwards order. */
14971 type = element_type;
14973 if (read_array_order (die, cu) == DW_ORD_col_major)
14977 while (i < range_types.size ())
14978 type = create_array_type_with_stride (NULL, type, range_types[i++],
14983 size_t ndim = range_types.size ();
14985 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14989 /* Understand Dwarf2 support for vector types (like they occur on
14990 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14991 array type. This is not part of the Dwarf2/3 standard yet, but a
14992 custom vendor extension. The main difference between a regular
14993 array and the vector variant is that vectors are passed by value
14995 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14997 make_vector_type (type);
14999 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15000 implementation may choose to implement triple vectors using this
15002 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15005 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15006 TYPE_LENGTH (type) = DW_UNSND (attr);
15008 complaint (&symfile_complaints,
15009 _("DW_AT_byte_size for array type smaller "
15010 "than the total size of elements"));
15013 name = dwarf2_name (die, cu);
15015 TYPE_NAME (type) = name;
15017 /* Install the type in the die. */
15018 set_die_type (die, type, cu);
15020 /* set_die_type should be already done. */
15021 set_descriptive_type (type, die, cu);
15026 static enum dwarf_array_dim_ordering
15027 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15029 struct attribute *attr;
15031 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15034 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15036 /* GNU F77 is a special case, as at 08/2004 array type info is the
15037 opposite order to the dwarf2 specification, but data is still
15038 laid out as per normal fortran.
15040 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15041 version checking. */
15043 if (cu->language == language_fortran
15044 && cu->producer && strstr (cu->producer, "GNU F77"))
15046 return DW_ORD_row_major;
15049 switch (cu->language_defn->la_array_ordering)
15051 case array_column_major:
15052 return DW_ORD_col_major;
15053 case array_row_major:
15055 return DW_ORD_row_major;
15059 /* Extract all information from a DW_TAG_set_type DIE and put it in
15060 the DIE's type field. */
15062 static struct type *
15063 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15065 struct type *domain_type, *set_type;
15066 struct attribute *attr;
15068 domain_type = die_type (die, cu);
15070 /* The die_type call above may have already set the type for this DIE. */
15071 set_type = get_die_type (die, cu);
15075 set_type = create_set_type (NULL, domain_type);
15077 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15079 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15081 return set_die_type (die, set_type, cu);
15084 /* A helper for read_common_block that creates a locexpr baton.
15085 SYM is the symbol which we are marking as computed.
15086 COMMON_DIE is the DIE for the common block.
15087 COMMON_LOC is the location expression attribute for the common
15089 MEMBER_LOC is the location expression attribute for the particular
15090 member of the common block that we are processing.
15091 CU is the CU from which the above come. */
15094 mark_common_block_symbol_computed (struct symbol *sym,
15095 struct die_info *common_die,
15096 struct attribute *common_loc,
15097 struct attribute *member_loc,
15098 struct dwarf2_cu *cu)
15100 struct objfile *objfile = dwarf2_per_objfile->objfile;
15101 struct dwarf2_locexpr_baton *baton;
15103 unsigned int cu_off;
15104 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15105 LONGEST offset = 0;
15107 gdb_assert (common_loc && member_loc);
15108 gdb_assert (attr_form_is_block (common_loc));
15109 gdb_assert (attr_form_is_block (member_loc)
15110 || attr_form_is_constant (member_loc));
15112 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15113 baton->per_cu = cu->per_cu;
15114 gdb_assert (baton->per_cu);
15116 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15118 if (attr_form_is_constant (member_loc))
15120 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15121 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15124 baton->size += DW_BLOCK (member_loc)->size;
15126 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15129 *ptr++ = DW_OP_call4;
15130 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15131 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15134 if (attr_form_is_constant (member_loc))
15136 *ptr++ = DW_OP_addr;
15137 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15138 ptr += cu->header.addr_size;
15142 /* We have to copy the data here, because DW_OP_call4 will only
15143 use a DW_AT_location attribute. */
15144 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15145 ptr += DW_BLOCK (member_loc)->size;
15148 *ptr++ = DW_OP_plus;
15149 gdb_assert (ptr - baton->data == baton->size);
15151 SYMBOL_LOCATION_BATON (sym) = baton;
15152 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15155 /* Create appropriate locally-scoped variables for all the
15156 DW_TAG_common_block entries. Also create a struct common_block
15157 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15158 is used to sepate the common blocks name namespace from regular
15162 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15164 struct attribute *attr;
15166 attr = dwarf2_attr (die, DW_AT_location, cu);
15169 /* Support the .debug_loc offsets. */
15170 if (attr_form_is_block (attr))
15174 else if (attr_form_is_section_offset (attr))
15176 dwarf2_complex_location_expr_complaint ();
15181 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15182 "common block member");
15187 if (die->child != NULL)
15189 struct objfile *objfile = cu->objfile;
15190 struct die_info *child_die;
15191 size_t n_entries = 0, size;
15192 struct common_block *common_block;
15193 struct symbol *sym;
15195 for (child_die = die->child;
15196 child_die && child_die->tag;
15197 child_die = sibling_die (child_die))
15200 size = (sizeof (struct common_block)
15201 + (n_entries - 1) * sizeof (struct symbol *));
15203 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15205 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15206 common_block->n_entries = 0;
15208 for (child_die = die->child;
15209 child_die && child_die->tag;
15210 child_die = sibling_die (child_die))
15212 /* Create the symbol in the DW_TAG_common_block block in the current
15214 sym = new_symbol (child_die, NULL, cu);
15217 struct attribute *member_loc;
15219 common_block->contents[common_block->n_entries++] = sym;
15221 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15225 /* GDB has handled this for a long time, but it is
15226 not specified by DWARF. It seems to have been
15227 emitted by gfortran at least as recently as:
15228 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15229 complaint (&symfile_complaints,
15230 _("Variable in common block has "
15231 "DW_AT_data_member_location "
15232 "- DIE at 0x%x [in module %s]"),
15233 to_underlying (child_die->sect_off),
15234 objfile_name (cu->objfile));
15236 if (attr_form_is_section_offset (member_loc))
15237 dwarf2_complex_location_expr_complaint ();
15238 else if (attr_form_is_constant (member_loc)
15239 || attr_form_is_block (member_loc))
15242 mark_common_block_symbol_computed (sym, die, attr,
15246 dwarf2_complex_location_expr_complaint ();
15251 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15252 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15256 /* Create a type for a C++ namespace. */
15258 static struct type *
15259 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15261 struct objfile *objfile = cu->objfile;
15262 const char *previous_prefix, *name;
15266 /* For extensions, reuse the type of the original namespace. */
15267 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15269 struct die_info *ext_die;
15270 struct dwarf2_cu *ext_cu = cu;
15272 ext_die = dwarf2_extension (die, &ext_cu);
15273 type = read_type_die (ext_die, ext_cu);
15275 /* EXT_CU may not be the same as CU.
15276 Ensure TYPE is recorded with CU in die_type_hash. */
15277 return set_die_type (die, type, cu);
15280 name = namespace_name (die, &is_anonymous, cu);
15282 /* Now build the name of the current namespace. */
15284 previous_prefix = determine_prefix (die, cu);
15285 if (previous_prefix[0] != '\0')
15286 name = typename_concat (&objfile->objfile_obstack,
15287 previous_prefix, name, 0, cu);
15289 /* Create the type. */
15290 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15291 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15293 return set_die_type (die, type, cu);
15296 /* Read a namespace scope. */
15299 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15301 struct objfile *objfile = cu->objfile;
15304 /* Add a symbol associated to this if we haven't seen the namespace
15305 before. Also, add a using directive if it's an anonymous
15308 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15312 type = read_type_die (die, cu);
15313 new_symbol (die, type, cu);
15315 namespace_name (die, &is_anonymous, cu);
15318 const char *previous_prefix = determine_prefix (die, cu);
15320 std::vector<const char *> excludes;
15321 add_using_directive (using_directives (cu->language),
15322 previous_prefix, TYPE_NAME (type), NULL,
15323 NULL, excludes, 0, &objfile->objfile_obstack);
15327 if (die->child != NULL)
15329 struct die_info *child_die = die->child;
15331 while (child_die && child_die->tag)
15333 process_die (child_die, cu);
15334 child_die = sibling_die (child_die);
15339 /* Read a Fortran module as type. This DIE can be only a declaration used for
15340 imported module. Still we need that type as local Fortran "use ... only"
15341 declaration imports depend on the created type in determine_prefix. */
15343 static struct type *
15344 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15346 struct objfile *objfile = cu->objfile;
15347 const char *module_name;
15350 module_name = dwarf2_name (die, cu);
15352 complaint (&symfile_complaints,
15353 _("DW_TAG_module has no name, offset 0x%x"),
15354 to_underlying (die->sect_off));
15355 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
15357 /* determine_prefix uses TYPE_TAG_NAME. */
15358 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15360 return set_die_type (die, type, cu);
15363 /* Read a Fortran module. */
15366 read_module (struct die_info *die, struct dwarf2_cu *cu)
15368 struct die_info *child_die = die->child;
15371 type = read_type_die (die, cu);
15372 new_symbol (die, type, cu);
15374 while (child_die && child_die->tag)
15376 process_die (child_die, cu);
15377 child_die = sibling_die (child_die);
15381 /* Return the name of the namespace represented by DIE. Set
15382 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15385 static const char *
15386 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
15388 struct die_info *current_die;
15389 const char *name = NULL;
15391 /* Loop through the extensions until we find a name. */
15393 for (current_die = die;
15394 current_die != NULL;
15395 current_die = dwarf2_extension (die, &cu))
15397 /* We don't use dwarf2_name here so that we can detect the absence
15398 of a name -> anonymous namespace. */
15399 name = dwarf2_string_attr (die, DW_AT_name, cu);
15405 /* Is it an anonymous namespace? */
15407 *is_anonymous = (name == NULL);
15409 name = CP_ANONYMOUS_NAMESPACE_STR;
15414 /* Extract all information from a DW_TAG_pointer_type DIE and add to
15415 the user defined type vector. */
15417 static struct type *
15418 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
15420 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
15421 struct comp_unit_head *cu_header = &cu->header;
15423 struct attribute *attr_byte_size;
15424 struct attribute *attr_address_class;
15425 int byte_size, addr_class;
15426 struct type *target_type;
15428 target_type = die_type (die, cu);
15430 /* The die_type call above may have already set the type for this DIE. */
15431 type = get_die_type (die, cu);
15435 type = lookup_pointer_type (target_type);
15437 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
15438 if (attr_byte_size)
15439 byte_size = DW_UNSND (attr_byte_size);
15441 byte_size = cu_header->addr_size;
15443 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
15444 if (attr_address_class)
15445 addr_class = DW_UNSND (attr_address_class);
15447 addr_class = DW_ADDR_none;
15449 /* If the pointer size or address class is different than the
15450 default, create a type variant marked as such and set the
15451 length accordingly. */
15452 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
15454 if (gdbarch_address_class_type_flags_p (gdbarch))
15458 type_flags = gdbarch_address_class_type_flags
15459 (gdbarch, byte_size, addr_class);
15460 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15462 type = make_type_with_address_space (type, type_flags);
15464 else if (TYPE_LENGTH (type) != byte_size)
15466 complaint (&symfile_complaints,
15467 _("invalid pointer size %d"), byte_size);
15471 /* Should we also complain about unhandled address classes? */
15475 TYPE_LENGTH (type) = byte_size;
15476 return set_die_type (die, type, cu);
15479 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15480 the user defined type vector. */
15482 static struct type *
15483 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
15486 struct type *to_type;
15487 struct type *domain;
15489 to_type = die_type (die, cu);
15490 domain = die_containing_type (die, cu);
15492 /* The calls above may have already set the type for this DIE. */
15493 type = get_die_type (die, cu);
15497 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15498 type = lookup_methodptr_type (to_type);
15499 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15501 struct type *new_type = alloc_type (cu->objfile);
15503 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15504 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15505 TYPE_VARARGS (to_type));
15506 type = lookup_methodptr_type (new_type);
15509 type = lookup_memberptr_type (to_type, domain);
15511 return set_die_type (die, type, cu);
15514 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
15515 the user defined type vector. */
15517 static struct type *
15518 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15519 enum type_code refcode)
15521 struct comp_unit_head *cu_header = &cu->header;
15522 struct type *type, *target_type;
15523 struct attribute *attr;
15525 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15527 target_type = die_type (die, cu);
15529 /* The die_type call above may have already set the type for this DIE. */
15530 type = get_die_type (die, cu);
15534 type = lookup_reference_type (target_type, refcode);
15535 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15538 TYPE_LENGTH (type) = DW_UNSND (attr);
15542 TYPE_LENGTH (type) = cu_header->addr_size;
15544 return set_die_type (die, type, cu);
15547 /* Add the given cv-qualifiers to the element type of the array. GCC
15548 outputs DWARF type qualifiers that apply to an array, not the
15549 element type. But GDB relies on the array element type to carry
15550 the cv-qualifiers. This mimics section 6.7.3 of the C99
15553 static struct type *
15554 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15555 struct type *base_type, int cnst, int voltl)
15557 struct type *el_type, *inner_array;
15559 base_type = copy_type (base_type);
15560 inner_array = base_type;
15562 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15564 TYPE_TARGET_TYPE (inner_array) =
15565 copy_type (TYPE_TARGET_TYPE (inner_array));
15566 inner_array = TYPE_TARGET_TYPE (inner_array);
15569 el_type = TYPE_TARGET_TYPE (inner_array);
15570 cnst |= TYPE_CONST (el_type);
15571 voltl |= TYPE_VOLATILE (el_type);
15572 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15574 return set_die_type (die, base_type, cu);
15577 static struct type *
15578 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
15580 struct type *base_type, *cv_type;
15582 base_type = die_type (die, cu);
15584 /* The die_type call above may have already set the type for this DIE. */
15585 cv_type = get_die_type (die, cu);
15589 /* In case the const qualifier is applied to an array type, the element type
15590 is so qualified, not the array type (section 6.7.3 of C99). */
15591 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15592 return add_array_cv_type (die, cu, base_type, 1, 0);
15594 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15595 return set_die_type (die, cv_type, cu);
15598 static struct type *
15599 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
15601 struct type *base_type, *cv_type;
15603 base_type = die_type (die, cu);
15605 /* The die_type call above may have already set the type for this DIE. */
15606 cv_type = get_die_type (die, cu);
15610 /* In case the volatile qualifier is applied to an array type, the
15611 element type is so qualified, not the array type (section 6.7.3
15613 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15614 return add_array_cv_type (die, cu, base_type, 0, 1);
15616 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15617 return set_die_type (die, cv_type, cu);
15620 /* Handle DW_TAG_restrict_type. */
15622 static struct type *
15623 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15625 struct type *base_type, *cv_type;
15627 base_type = die_type (die, cu);
15629 /* The die_type call above may have already set the type for this DIE. */
15630 cv_type = get_die_type (die, cu);
15634 cv_type = make_restrict_type (base_type);
15635 return set_die_type (die, cv_type, cu);
15638 /* Handle DW_TAG_atomic_type. */
15640 static struct type *
15641 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15643 struct type *base_type, *cv_type;
15645 base_type = die_type (die, cu);
15647 /* The die_type call above may have already set the type for this DIE. */
15648 cv_type = get_die_type (die, cu);
15652 cv_type = make_atomic_type (base_type);
15653 return set_die_type (die, cv_type, cu);
15656 /* Extract all information from a DW_TAG_string_type DIE and add to
15657 the user defined type vector. It isn't really a user defined type,
15658 but it behaves like one, with other DIE's using an AT_user_def_type
15659 attribute to reference it. */
15661 static struct type *
15662 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
15664 struct objfile *objfile = cu->objfile;
15665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15666 struct type *type, *range_type, *index_type, *char_type;
15667 struct attribute *attr;
15668 unsigned int length;
15670 attr = dwarf2_attr (die, DW_AT_string_length, cu);
15673 length = DW_UNSND (attr);
15677 /* Check for the DW_AT_byte_size attribute. */
15678 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15681 length = DW_UNSND (attr);
15689 index_type = objfile_type (objfile)->builtin_int;
15690 range_type = create_static_range_type (NULL, index_type, 1, length);
15691 char_type = language_string_char_type (cu->language_defn, gdbarch);
15692 type = create_string_type (NULL, char_type, range_type);
15694 return set_die_type (die, type, cu);
15697 /* Assuming that DIE corresponds to a function, returns nonzero
15698 if the function is prototyped. */
15701 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15703 struct attribute *attr;
15705 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15706 if (attr && (DW_UNSND (attr) != 0))
15709 /* The DWARF standard implies that the DW_AT_prototyped attribute
15710 is only meaninful for C, but the concept also extends to other
15711 languages that allow unprototyped functions (Eg: Objective C).
15712 For all other languages, assume that functions are always
15714 if (cu->language != language_c
15715 && cu->language != language_objc
15716 && cu->language != language_opencl)
15719 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15720 prototyped and unprototyped functions; default to prototyped,
15721 since that is more common in modern code (and RealView warns
15722 about unprototyped functions). */
15723 if (producer_is_realview (cu->producer))
15729 /* Handle DIES due to C code like:
15733 int (*funcp)(int a, long l);
15737 ('funcp' generates a DW_TAG_subroutine_type DIE). */
15739 static struct type *
15740 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
15742 struct objfile *objfile = cu->objfile;
15743 struct type *type; /* Type that this function returns. */
15744 struct type *ftype; /* Function that returns above type. */
15745 struct attribute *attr;
15747 type = die_type (die, cu);
15749 /* The die_type call above may have already set the type for this DIE. */
15750 ftype = get_die_type (die, cu);
15754 ftype = lookup_function_type (type);
15756 if (prototyped_function_p (die, cu))
15757 TYPE_PROTOTYPED (ftype) = 1;
15759 /* Store the calling convention in the type if it's available in
15760 the subroutine die. Otherwise set the calling convention to
15761 the default value DW_CC_normal. */
15762 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15764 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15765 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15766 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15768 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
15770 /* Record whether the function returns normally to its caller or not
15771 if the DWARF producer set that information. */
15772 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15773 if (attr && (DW_UNSND (attr) != 0))
15774 TYPE_NO_RETURN (ftype) = 1;
15776 /* We need to add the subroutine type to the die immediately so
15777 we don't infinitely recurse when dealing with parameters
15778 declared as the same subroutine type. */
15779 set_die_type (die, ftype, cu);
15781 if (die->child != NULL)
15783 struct type *void_type = objfile_type (objfile)->builtin_void;
15784 struct die_info *child_die;
15785 int nparams, iparams;
15787 /* Count the number of parameters.
15788 FIXME: GDB currently ignores vararg functions, but knows about
15789 vararg member functions. */
15791 child_die = die->child;
15792 while (child_die && child_die->tag)
15794 if (child_die->tag == DW_TAG_formal_parameter)
15796 else if (child_die->tag == DW_TAG_unspecified_parameters)
15797 TYPE_VARARGS (ftype) = 1;
15798 child_die = sibling_die (child_die);
15801 /* Allocate storage for parameters and fill them in. */
15802 TYPE_NFIELDS (ftype) = nparams;
15803 TYPE_FIELDS (ftype) = (struct field *)
15804 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
15806 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15807 even if we error out during the parameters reading below. */
15808 for (iparams = 0; iparams < nparams; iparams++)
15809 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15812 child_die = die->child;
15813 while (child_die && child_die->tag)
15815 if (child_die->tag == DW_TAG_formal_parameter)
15817 struct type *arg_type;
15819 /* DWARF version 2 has no clean way to discern C++
15820 static and non-static member functions. G++ helps
15821 GDB by marking the first parameter for non-static
15822 member functions (which is the this pointer) as
15823 artificial. We pass this information to
15824 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15826 DWARF version 3 added DW_AT_object_pointer, which GCC
15827 4.5 does not yet generate. */
15828 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
15830 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15832 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
15833 arg_type = die_type (child_die, cu);
15835 /* RealView does not mark THIS as const, which the testsuite
15836 expects. GCC marks THIS as const in method definitions,
15837 but not in the class specifications (GCC PR 43053). */
15838 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15839 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15842 struct dwarf2_cu *arg_cu = cu;
15843 const char *name = dwarf2_name (child_die, cu);
15845 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15848 /* If the compiler emits this, use it. */
15849 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15852 else if (name && strcmp (name, "this") == 0)
15853 /* Function definitions will have the argument names. */
15855 else if (name == NULL && iparams == 0)
15856 /* Declarations may not have the names, so like
15857 elsewhere in GDB, assume an artificial first
15858 argument is "this". */
15862 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15866 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
15869 child_die = sibling_die (child_die);
15876 static struct type *
15877 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15879 struct objfile *objfile = cu->objfile;
15880 const char *name = NULL;
15881 struct type *this_type, *target_type;
15883 name = dwarf2_full_name (NULL, die, cu);
15884 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15885 TYPE_TARGET_STUB (this_type) = 1;
15886 set_die_type (die, this_type, cu);
15887 target_type = die_type (die, cu);
15888 if (target_type != this_type)
15889 TYPE_TARGET_TYPE (this_type) = target_type;
15892 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15893 spec and cause infinite loops in GDB. */
15894 complaint (&symfile_complaints,
15895 _("Self-referential DW_TAG_typedef "
15896 "- DIE at 0x%x [in module %s]"),
15897 to_underlying (die->sect_off), objfile_name (objfile));
15898 TYPE_TARGET_TYPE (this_type) = NULL;
15903 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15904 (which may be different from NAME) to the architecture back-end to allow
15905 it to guess the correct format if necessary. */
15907 static struct type *
15908 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15909 const char *name_hint)
15911 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15912 const struct floatformat **format;
15915 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15917 type = init_float_type (objfile, bits, name, format);
15919 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15924 /* Find a representation of a given base type and install
15925 it in the TYPE field of the die. */
15927 static struct type *
15928 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15930 struct objfile *objfile = cu->objfile;
15932 struct attribute *attr;
15933 int encoding = 0, bits = 0;
15936 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15939 encoding = DW_UNSND (attr);
15941 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15944 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15946 name = dwarf2_name (die, cu);
15949 complaint (&symfile_complaints,
15950 _("DW_AT_name missing from DW_TAG_base_type"));
15955 case DW_ATE_address:
15956 /* Turn DW_ATE_address into a void * pointer. */
15957 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15958 type = init_pointer_type (objfile, bits, name, type);
15960 case DW_ATE_boolean:
15961 type = init_boolean_type (objfile, bits, 1, name);
15963 case DW_ATE_complex_float:
15964 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15965 type = init_complex_type (objfile, name, type);
15967 case DW_ATE_decimal_float:
15968 type = init_decfloat_type (objfile, bits, name);
15971 type = dwarf2_init_float_type (objfile, bits, name, name);
15973 case DW_ATE_signed:
15974 type = init_integer_type (objfile, bits, 0, name);
15976 case DW_ATE_unsigned:
15977 if (cu->language == language_fortran
15979 && startswith (name, "character("))
15980 type = init_character_type (objfile, bits, 1, name);
15982 type = init_integer_type (objfile, bits, 1, name);
15984 case DW_ATE_signed_char:
15985 if (cu->language == language_ada || cu->language == language_m2
15986 || cu->language == language_pascal
15987 || cu->language == language_fortran)
15988 type = init_character_type (objfile, bits, 0, name);
15990 type = init_integer_type (objfile, bits, 0, name);
15992 case DW_ATE_unsigned_char:
15993 if (cu->language == language_ada || cu->language == language_m2
15994 || cu->language == language_pascal
15995 || cu->language == language_fortran
15996 || cu->language == language_rust)
15997 type = init_character_type (objfile, bits, 1, name);
15999 type = init_integer_type (objfile, bits, 1, name);
16003 gdbarch *arch = get_objfile_arch (objfile);
16006 type = builtin_type (arch)->builtin_char16;
16007 else if (bits == 32)
16008 type = builtin_type (arch)->builtin_char32;
16011 complaint (&symfile_complaints,
16012 _("unsupported DW_ATE_UTF bit size: '%d'"),
16014 type = init_integer_type (objfile, bits, 1, name);
16016 return set_die_type (die, type, cu);
16021 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16022 dwarf_type_encoding_name (encoding));
16023 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16027 if (name && strcmp (name, "char") == 0)
16028 TYPE_NOSIGN (type) = 1;
16030 return set_die_type (die, type, cu);
16033 /* Parse dwarf attribute if it's a block, reference or constant and put the
16034 resulting value of the attribute into struct bound_prop.
16035 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16038 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16039 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16041 struct dwarf2_property_baton *baton;
16042 struct obstack *obstack = &cu->objfile->objfile_obstack;
16044 if (attr == NULL || prop == NULL)
16047 if (attr_form_is_block (attr))
16049 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16050 baton->referenced_type = NULL;
16051 baton->locexpr.per_cu = cu->per_cu;
16052 baton->locexpr.size = DW_BLOCK (attr)->size;
16053 baton->locexpr.data = DW_BLOCK (attr)->data;
16054 prop->data.baton = baton;
16055 prop->kind = PROP_LOCEXPR;
16056 gdb_assert (prop->data.baton != NULL);
16058 else if (attr_form_is_ref (attr))
16060 struct dwarf2_cu *target_cu = cu;
16061 struct die_info *target_die;
16062 struct attribute *target_attr;
16064 target_die = follow_die_ref (die, attr, &target_cu);
16065 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
16066 if (target_attr == NULL)
16067 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16069 if (target_attr == NULL)
16072 switch (target_attr->name)
16074 case DW_AT_location:
16075 if (attr_form_is_section_offset (target_attr))
16077 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16078 baton->referenced_type = die_type (target_die, target_cu);
16079 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16080 prop->data.baton = baton;
16081 prop->kind = PROP_LOCLIST;
16082 gdb_assert (prop->data.baton != NULL);
16084 else if (attr_form_is_block (target_attr))
16086 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16087 baton->referenced_type = die_type (target_die, target_cu);
16088 baton->locexpr.per_cu = cu->per_cu;
16089 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16090 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16091 prop->data.baton = baton;
16092 prop->kind = PROP_LOCEXPR;
16093 gdb_assert (prop->data.baton != NULL);
16097 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16098 "dynamic property");
16102 case DW_AT_data_member_location:
16106 if (!handle_data_member_location (target_die, target_cu,
16110 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16111 baton->referenced_type = read_type_die (target_die->parent,
16113 baton->offset_info.offset = offset;
16114 baton->offset_info.type = die_type (target_die, target_cu);
16115 prop->data.baton = baton;
16116 prop->kind = PROP_ADDR_OFFSET;
16121 else if (attr_form_is_constant (attr))
16123 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16124 prop->kind = PROP_CONST;
16128 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16129 dwarf2_name (die, cu));
16136 /* Read the given DW_AT_subrange DIE. */
16138 static struct type *
16139 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16141 struct type *base_type, *orig_base_type;
16142 struct type *range_type;
16143 struct attribute *attr;
16144 struct dynamic_prop low, high;
16145 int low_default_is_valid;
16146 int high_bound_is_count = 0;
16148 LONGEST negative_mask;
16150 orig_base_type = die_type (die, cu);
16151 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16152 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16153 creating the range type, but we use the result of check_typedef
16154 when examining properties of the type. */
16155 base_type = check_typedef (orig_base_type);
16157 /* The die_type call above may have already set the type for this DIE. */
16158 range_type = get_die_type (die, cu);
16162 low.kind = PROP_CONST;
16163 high.kind = PROP_CONST;
16164 high.data.const_val = 0;
16166 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16167 omitting DW_AT_lower_bound. */
16168 switch (cu->language)
16171 case language_cplus:
16172 low.data.const_val = 0;
16173 low_default_is_valid = 1;
16175 case language_fortran:
16176 low.data.const_val = 1;
16177 low_default_is_valid = 1;
16180 case language_objc:
16181 case language_rust:
16182 low.data.const_val = 0;
16183 low_default_is_valid = (cu->header.version >= 4);
16187 case language_pascal:
16188 low.data.const_val = 1;
16189 low_default_is_valid = (cu->header.version >= 4);
16192 low.data.const_val = 0;
16193 low_default_is_valid = 0;
16197 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
16199 attr_to_dynamic_prop (attr, die, cu, &low);
16200 else if (!low_default_is_valid)
16201 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16202 "- DIE at 0x%x [in module %s]"),
16203 to_underlying (die->sect_off), objfile_name (cu->objfile));
16205 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
16206 if (!attr_to_dynamic_prop (attr, die, cu, &high))
16208 attr = dwarf2_attr (die, DW_AT_count, cu);
16209 if (attr_to_dynamic_prop (attr, die, cu, &high))
16211 /* If bounds are constant do the final calculation here. */
16212 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16213 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16215 high_bound_is_count = 1;
16219 /* Dwarf-2 specifications explicitly allows to create subrange types
16220 without specifying a base type.
16221 In that case, the base type must be set to the type of
16222 the lower bound, upper bound or count, in that order, if any of these
16223 three attributes references an object that has a type.
16224 If no base type is found, the Dwarf-2 specifications say that
16225 a signed integer type of size equal to the size of an address should
16227 For the following C code: `extern char gdb_int [];'
16228 GCC produces an empty range DIE.
16229 FIXME: muller/2010-05-28: Possible references to object for low bound,
16230 high bound or count are not yet handled by this code. */
16231 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16233 struct objfile *objfile = cu->objfile;
16234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16235 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16236 struct type *int_type = objfile_type (objfile)->builtin_int;
16238 /* Test "int", "long int", and "long long int" objfile types,
16239 and select the first one having a size above or equal to the
16240 architecture address size. */
16241 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16242 base_type = int_type;
16245 int_type = objfile_type (objfile)->builtin_long;
16246 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16247 base_type = int_type;
16250 int_type = objfile_type (objfile)->builtin_long_long;
16251 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16252 base_type = int_type;
16257 /* Normally, the DWARF producers are expected to use a signed
16258 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16259 But this is unfortunately not always the case, as witnessed
16260 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16261 is used instead. To work around that ambiguity, we treat
16262 the bounds as signed, and thus sign-extend their values, when
16263 the base type is signed. */
16265 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
16266 if (low.kind == PROP_CONST
16267 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16268 low.data.const_val |= negative_mask;
16269 if (high.kind == PROP_CONST
16270 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16271 high.data.const_val |= negative_mask;
16273 range_type = create_range_type (NULL, orig_base_type, &low, &high);
16275 if (high_bound_is_count)
16276 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16278 /* Ada expects an empty array on no boundary attributes. */
16279 if (attr == NULL && cu->language != language_ada)
16280 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
16282 name = dwarf2_name (die, cu);
16284 TYPE_NAME (range_type) = name;
16286 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16288 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16290 set_die_type (die, range_type, cu);
16292 /* set_die_type should be already done. */
16293 set_descriptive_type (range_type, die, cu);
16298 static struct type *
16299 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16303 /* For now, we only support the C meaning of an unspecified type: void. */
16305 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
16306 TYPE_NAME (type) = dwarf2_name (die, cu);
16308 return set_die_type (die, type, cu);
16311 /* Read a single die and all its descendents. Set the die's sibling
16312 field to NULL; set other fields in the die correctly, and set all
16313 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16314 location of the info_ptr after reading all of those dies. PARENT
16315 is the parent of the die in question. */
16317 static struct die_info *
16318 read_die_and_children (const struct die_reader_specs *reader,
16319 const gdb_byte *info_ptr,
16320 const gdb_byte **new_info_ptr,
16321 struct die_info *parent)
16323 struct die_info *die;
16324 const gdb_byte *cur_ptr;
16327 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
16330 *new_info_ptr = cur_ptr;
16333 store_in_ref_table (die, reader->cu);
16336 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
16340 *new_info_ptr = cur_ptr;
16343 die->sibling = NULL;
16344 die->parent = parent;
16348 /* Read a die, all of its descendents, and all of its siblings; set
16349 all of the fields of all of the dies correctly. Arguments are as
16350 in read_die_and_children. */
16352 static struct die_info *
16353 read_die_and_siblings_1 (const struct die_reader_specs *reader,
16354 const gdb_byte *info_ptr,
16355 const gdb_byte **new_info_ptr,
16356 struct die_info *parent)
16358 struct die_info *first_die, *last_sibling;
16359 const gdb_byte *cur_ptr;
16361 cur_ptr = info_ptr;
16362 first_die = last_sibling = NULL;
16366 struct die_info *die
16367 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
16371 *new_info_ptr = cur_ptr;
16378 last_sibling->sibling = die;
16380 last_sibling = die;
16384 /* Read a die, all of its descendents, and all of its siblings; set
16385 all of the fields of all of the dies correctly. Arguments are as
16386 in read_die_and_children.
16387 This the main entry point for reading a DIE and all its children. */
16389 static struct die_info *
16390 read_die_and_siblings (const struct die_reader_specs *reader,
16391 const gdb_byte *info_ptr,
16392 const gdb_byte **new_info_ptr,
16393 struct die_info *parent)
16395 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16396 new_info_ptr, parent);
16398 if (dwarf_die_debug)
16400 fprintf_unfiltered (gdb_stdlog,
16401 "Read die from %s@0x%x of %s:\n",
16402 get_section_name (reader->die_section),
16403 (unsigned) (info_ptr - reader->die_section->buffer),
16404 bfd_get_filename (reader->abfd));
16405 dump_die (die, dwarf_die_debug);
16411 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16413 The caller is responsible for filling in the extra attributes
16414 and updating (*DIEP)->num_attrs.
16415 Set DIEP to point to a newly allocated die with its information,
16416 except for its child, sibling, and parent fields.
16417 Set HAS_CHILDREN to tell whether the die has children or not. */
16419 static const gdb_byte *
16420 read_full_die_1 (const struct die_reader_specs *reader,
16421 struct die_info **diep, const gdb_byte *info_ptr,
16422 int *has_children, int num_extra_attrs)
16424 unsigned int abbrev_number, bytes_read, i;
16425 struct abbrev_info *abbrev;
16426 struct die_info *die;
16427 struct dwarf2_cu *cu = reader->cu;
16428 bfd *abfd = reader->abfd;
16430 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
16431 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16432 info_ptr += bytes_read;
16433 if (!abbrev_number)
16440 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
16442 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16444 bfd_get_filename (abfd));
16446 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
16447 die->sect_off = sect_off;
16448 die->tag = abbrev->tag;
16449 die->abbrev = abbrev_number;
16451 /* Make the result usable.
16452 The caller needs to update num_attrs after adding the extra
16454 die->num_attrs = abbrev->num_attrs;
16456 for (i = 0; i < abbrev->num_attrs; ++i)
16457 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16461 *has_children = abbrev->has_children;
16465 /* Read a die and all its attributes.
16466 Set DIEP to point to a newly allocated die with its information,
16467 except for its child, sibling, and parent fields.
16468 Set HAS_CHILDREN to tell whether the die has children or not. */
16470 static const gdb_byte *
16471 read_full_die (const struct die_reader_specs *reader,
16472 struct die_info **diep, const gdb_byte *info_ptr,
16475 const gdb_byte *result;
16477 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16479 if (dwarf_die_debug)
16481 fprintf_unfiltered (gdb_stdlog,
16482 "Read die from %s@0x%x of %s:\n",
16483 get_section_name (reader->die_section),
16484 (unsigned) (info_ptr - reader->die_section->buffer),
16485 bfd_get_filename (reader->abfd));
16486 dump_die (*diep, dwarf_die_debug);
16492 /* Abbreviation tables.
16494 In DWARF version 2, the description of the debugging information is
16495 stored in a separate .debug_abbrev section. Before we read any
16496 dies from a section we read in all abbreviations and install them
16497 in a hash table. */
16499 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16501 static struct abbrev_info *
16502 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16504 struct abbrev_info *abbrev;
16506 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
16507 memset (abbrev, 0, sizeof (struct abbrev_info));
16512 /* Add an abbreviation to the table. */
16515 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16516 unsigned int abbrev_number,
16517 struct abbrev_info *abbrev)
16519 unsigned int hash_number;
16521 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16522 abbrev->next = abbrev_table->abbrevs[hash_number];
16523 abbrev_table->abbrevs[hash_number] = abbrev;
16526 /* Look up an abbrev in the table.
16527 Returns NULL if the abbrev is not found. */
16529 static struct abbrev_info *
16530 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16531 unsigned int abbrev_number)
16533 unsigned int hash_number;
16534 struct abbrev_info *abbrev;
16536 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16537 abbrev = abbrev_table->abbrevs[hash_number];
16541 if (abbrev->number == abbrev_number)
16543 abbrev = abbrev->next;
16548 /* Read in an abbrev table. */
16550 static struct abbrev_table *
16551 abbrev_table_read_table (struct dwarf2_section_info *section,
16552 sect_offset sect_off)
16554 struct objfile *objfile = dwarf2_per_objfile->objfile;
16555 bfd *abfd = get_section_bfd_owner (section);
16556 struct abbrev_table *abbrev_table;
16557 const gdb_byte *abbrev_ptr;
16558 struct abbrev_info *cur_abbrev;
16559 unsigned int abbrev_number, bytes_read, abbrev_name;
16560 unsigned int abbrev_form;
16561 struct attr_abbrev *cur_attrs;
16562 unsigned int allocated_attrs;
16564 abbrev_table = XNEW (struct abbrev_table);
16565 abbrev_table->sect_off = sect_off;
16566 obstack_init (&abbrev_table->abbrev_obstack);
16567 abbrev_table->abbrevs =
16568 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16570 memset (abbrev_table->abbrevs, 0,
16571 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
16573 dwarf2_read_section (objfile, section);
16574 abbrev_ptr = section->buffer + to_underlying (sect_off);
16575 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16576 abbrev_ptr += bytes_read;
16578 allocated_attrs = ATTR_ALLOC_CHUNK;
16579 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
16581 /* Loop until we reach an abbrev number of 0. */
16582 while (abbrev_number)
16584 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
16586 /* read in abbrev header */
16587 cur_abbrev->number = abbrev_number;
16589 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16590 abbrev_ptr += bytes_read;
16591 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16594 /* now read in declarations */
16597 LONGEST implicit_const;
16599 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16600 abbrev_ptr += bytes_read;
16601 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16602 abbrev_ptr += bytes_read;
16603 if (abbrev_form == DW_FORM_implicit_const)
16605 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16607 abbrev_ptr += bytes_read;
16611 /* Initialize it due to a false compiler warning. */
16612 implicit_const = -1;
16615 if (abbrev_name == 0)
16618 if (cur_abbrev->num_attrs == allocated_attrs)
16620 allocated_attrs += ATTR_ALLOC_CHUNK;
16622 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
16625 cur_attrs[cur_abbrev->num_attrs].name
16626 = (enum dwarf_attribute) abbrev_name;
16627 cur_attrs[cur_abbrev->num_attrs].form
16628 = (enum dwarf_form) abbrev_form;
16629 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
16630 ++cur_abbrev->num_attrs;
16633 cur_abbrev->attrs =
16634 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16635 cur_abbrev->num_attrs);
16636 memcpy (cur_abbrev->attrs, cur_attrs,
16637 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16639 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
16641 /* Get next abbreviation.
16642 Under Irix6 the abbreviations for a compilation unit are not
16643 always properly terminated with an abbrev number of 0.
16644 Exit loop if we encounter an abbreviation which we have
16645 already read (which means we are about to read the abbreviations
16646 for the next compile unit) or if the end of the abbreviation
16647 table is reached. */
16648 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
16650 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16651 abbrev_ptr += bytes_read;
16652 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
16657 return abbrev_table;
16660 /* Free the resources held by ABBREV_TABLE. */
16663 abbrev_table_free (struct abbrev_table *abbrev_table)
16665 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16666 xfree (abbrev_table);
16669 /* Same as abbrev_table_free but as a cleanup.
16670 We pass in a pointer to the pointer to the table so that we can
16671 set the pointer to NULL when we're done. It also simplifies
16672 build_type_psymtabs_1. */
16675 abbrev_table_free_cleanup (void *table_ptr)
16677 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
16679 if (*abbrev_table_ptr != NULL)
16680 abbrev_table_free (*abbrev_table_ptr);
16681 *abbrev_table_ptr = NULL;
16684 /* Read the abbrev table for CU from ABBREV_SECTION. */
16687 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16688 struct dwarf2_section_info *abbrev_section)
16691 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
16694 /* Release the memory used by the abbrev table for a compilation unit. */
16697 dwarf2_free_abbrev_table (void *ptr_to_cu)
16699 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
16701 if (cu->abbrev_table != NULL)
16702 abbrev_table_free (cu->abbrev_table);
16703 /* Set this to NULL so that we SEGV if we try to read it later,
16704 and also because free_comp_unit verifies this is NULL. */
16705 cu->abbrev_table = NULL;
16708 /* Returns nonzero if TAG represents a type that we might generate a partial
16712 is_type_tag_for_partial (int tag)
16717 /* Some types that would be reasonable to generate partial symbols for,
16718 that we don't at present. */
16719 case DW_TAG_array_type:
16720 case DW_TAG_file_type:
16721 case DW_TAG_ptr_to_member_type:
16722 case DW_TAG_set_type:
16723 case DW_TAG_string_type:
16724 case DW_TAG_subroutine_type:
16726 case DW_TAG_base_type:
16727 case DW_TAG_class_type:
16728 case DW_TAG_interface_type:
16729 case DW_TAG_enumeration_type:
16730 case DW_TAG_structure_type:
16731 case DW_TAG_subrange_type:
16732 case DW_TAG_typedef:
16733 case DW_TAG_union_type:
16740 /* Load all DIEs that are interesting for partial symbols into memory. */
16742 static struct partial_die_info *
16743 load_partial_dies (const struct die_reader_specs *reader,
16744 const gdb_byte *info_ptr, int building_psymtab)
16746 struct dwarf2_cu *cu = reader->cu;
16747 struct objfile *objfile = cu->objfile;
16748 struct partial_die_info *part_die;
16749 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16750 struct abbrev_info *abbrev;
16751 unsigned int bytes_read;
16752 unsigned int load_all = 0;
16753 int nesting_level = 1;
16758 gdb_assert (cu->per_cu != NULL);
16759 if (cu->per_cu->load_all_dies)
16763 = htab_create_alloc_ex (cu->header.length / 12,
16767 &cu->comp_unit_obstack,
16768 hashtab_obstack_allocate,
16769 dummy_obstack_deallocate);
16771 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16775 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16777 /* A NULL abbrev means the end of a series of children. */
16778 if (abbrev == NULL)
16780 if (--nesting_level == 0)
16782 /* PART_DIE was probably the last thing allocated on the
16783 comp_unit_obstack, so we could call obstack_free
16784 here. We don't do that because the waste is small,
16785 and will be cleaned up when we're done with this
16786 compilation unit. This way, we're also more robust
16787 against other users of the comp_unit_obstack. */
16790 info_ptr += bytes_read;
16791 last_die = parent_die;
16792 parent_die = parent_die->die_parent;
16796 /* Check for template arguments. We never save these; if
16797 they're seen, we just mark the parent, and go on our way. */
16798 if (parent_die != NULL
16799 && cu->language == language_cplus
16800 && (abbrev->tag == DW_TAG_template_type_param
16801 || abbrev->tag == DW_TAG_template_value_param))
16803 parent_die->has_template_arguments = 1;
16807 /* We don't need a partial DIE for the template argument. */
16808 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16813 /* We only recurse into c++ subprograms looking for template arguments.
16814 Skip their other children. */
16816 && cu->language == language_cplus
16817 && parent_die != NULL
16818 && parent_die->tag == DW_TAG_subprogram)
16820 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16824 /* Check whether this DIE is interesting enough to save. Normally
16825 we would not be interested in members here, but there may be
16826 later variables referencing them via DW_AT_specification (for
16827 static members). */
16829 && !is_type_tag_for_partial (abbrev->tag)
16830 && abbrev->tag != DW_TAG_constant
16831 && abbrev->tag != DW_TAG_enumerator
16832 && abbrev->tag != DW_TAG_subprogram
16833 && abbrev->tag != DW_TAG_lexical_block
16834 && abbrev->tag != DW_TAG_variable
16835 && abbrev->tag != DW_TAG_namespace
16836 && abbrev->tag != DW_TAG_module
16837 && abbrev->tag != DW_TAG_member
16838 && abbrev->tag != DW_TAG_imported_unit
16839 && abbrev->tag != DW_TAG_imported_declaration)
16841 /* Otherwise we skip to the next sibling, if any. */
16842 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16846 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16849 /* This two-pass algorithm for processing partial symbols has a
16850 high cost in cache pressure. Thus, handle some simple cases
16851 here which cover the majority of C partial symbols. DIEs
16852 which neither have specification tags in them, nor could have
16853 specification tags elsewhere pointing at them, can simply be
16854 processed and discarded.
16856 This segment is also optional; scan_partial_symbols and
16857 add_partial_symbol will handle these DIEs if we chain
16858 them in normally. When compilers which do not emit large
16859 quantities of duplicate debug information are more common,
16860 this code can probably be removed. */
16862 /* Any complete simple types at the top level (pretty much all
16863 of them, for a language without namespaces), can be processed
16865 if (parent_die == NULL
16866 && part_die->has_specification == 0
16867 && part_die->is_declaration == 0
16868 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
16869 || part_die->tag == DW_TAG_base_type
16870 || part_die->tag == DW_TAG_subrange_type))
16872 if (building_psymtab && part_die->name != NULL)
16873 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16874 VAR_DOMAIN, LOC_TYPEDEF,
16875 &objfile->static_psymbols,
16876 0, cu->language, objfile);
16877 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16881 /* The exception for DW_TAG_typedef with has_children above is
16882 a workaround of GCC PR debug/47510. In the case of this complaint
16883 type_name_no_tag_or_error will error on such types later.
16885 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16886 it could not find the child DIEs referenced later, this is checked
16887 above. In correct DWARF DW_TAG_typedef should have no children. */
16889 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16890 complaint (&symfile_complaints,
16891 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16892 "- DIE at 0x%x [in module %s]"),
16893 to_underlying (part_die->sect_off), objfile_name (objfile));
16895 /* If we're at the second level, and we're an enumerator, and
16896 our parent has no specification (meaning possibly lives in a
16897 namespace elsewhere), then we can add the partial symbol now
16898 instead of queueing it. */
16899 if (part_die->tag == DW_TAG_enumerator
16900 && parent_die != NULL
16901 && parent_die->die_parent == NULL
16902 && parent_die->tag == DW_TAG_enumeration_type
16903 && parent_die->has_specification == 0)
16905 if (part_die->name == NULL)
16906 complaint (&symfile_complaints,
16907 _("malformed enumerator DIE ignored"));
16908 else if (building_psymtab)
16909 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16910 VAR_DOMAIN, LOC_CONST,
16911 cu->language == language_cplus
16912 ? &objfile->global_psymbols
16913 : &objfile->static_psymbols,
16914 0, cu->language, objfile);
16916 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16920 /* We'll save this DIE so link it in. */
16921 part_die->die_parent = parent_die;
16922 part_die->die_sibling = NULL;
16923 part_die->die_child = NULL;
16925 if (last_die && last_die == parent_die)
16926 last_die->die_child = part_die;
16928 last_die->die_sibling = part_die;
16930 last_die = part_die;
16932 if (first_die == NULL)
16933 first_die = part_die;
16935 /* Maybe add the DIE to the hash table. Not all DIEs that we
16936 find interesting need to be in the hash table, because we
16937 also have the parent/sibling/child chains; only those that we
16938 might refer to by offset later during partial symbol reading.
16940 For now this means things that might have be the target of a
16941 DW_AT_specification, DW_AT_abstract_origin, or
16942 DW_AT_extension. DW_AT_extension will refer only to
16943 namespaces; DW_AT_abstract_origin refers to functions (and
16944 many things under the function DIE, but we do not recurse
16945 into function DIEs during partial symbol reading) and
16946 possibly variables as well; DW_AT_specification refers to
16947 declarations. Declarations ought to have the DW_AT_declaration
16948 flag. It happens that GCC forgets to put it in sometimes, but
16949 only for functions, not for types.
16951 Adding more things than necessary to the hash table is harmless
16952 except for the performance cost. Adding too few will result in
16953 wasted time in find_partial_die, when we reread the compilation
16954 unit with load_all_dies set. */
16957 || abbrev->tag == DW_TAG_constant
16958 || abbrev->tag == DW_TAG_subprogram
16959 || abbrev->tag == DW_TAG_variable
16960 || abbrev->tag == DW_TAG_namespace
16961 || part_die->is_declaration)
16965 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16966 to_underlying (part_die->sect_off),
16971 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16973 /* For some DIEs we want to follow their children (if any). For C
16974 we have no reason to follow the children of structures; for other
16975 languages we have to, so that we can get at method physnames
16976 to infer fully qualified class names, for DW_AT_specification,
16977 and for C++ template arguments. For C++, we also look one level
16978 inside functions to find template arguments (if the name of the
16979 function does not already contain the template arguments).
16981 For Ada, we need to scan the children of subprograms and lexical
16982 blocks as well because Ada allows the definition of nested
16983 entities that could be interesting for the debugger, such as
16984 nested subprograms for instance. */
16985 if (last_die->has_children
16987 || last_die->tag == DW_TAG_namespace
16988 || last_die->tag == DW_TAG_module
16989 || last_die->tag == DW_TAG_enumeration_type
16990 || (cu->language == language_cplus
16991 && last_die->tag == DW_TAG_subprogram
16992 && (last_die->name == NULL
16993 || strchr (last_die->name, '<') == NULL))
16994 || (cu->language != language_c
16995 && (last_die->tag == DW_TAG_class_type
16996 || last_die->tag == DW_TAG_interface_type
16997 || last_die->tag == DW_TAG_structure_type
16998 || last_die->tag == DW_TAG_union_type))
16999 || (cu->language == language_ada
17000 && (last_die->tag == DW_TAG_subprogram
17001 || last_die->tag == DW_TAG_lexical_block))))
17004 parent_die = last_die;
17008 /* Otherwise we skip to the next sibling, if any. */
17009 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17011 /* Back to the top, do it again. */
17015 /* Read a minimal amount of information into the minimal die structure. */
17017 static const gdb_byte *
17018 read_partial_die (const struct die_reader_specs *reader,
17019 struct partial_die_info *part_die,
17020 struct abbrev_info *abbrev, unsigned int abbrev_len,
17021 const gdb_byte *info_ptr)
17023 struct dwarf2_cu *cu = reader->cu;
17024 struct objfile *objfile = cu->objfile;
17025 const gdb_byte *buffer = reader->buffer;
17027 struct attribute attr;
17028 int has_low_pc_attr = 0;
17029 int has_high_pc_attr = 0;
17030 int high_pc_relative = 0;
17032 memset (part_die, 0, sizeof (struct partial_die_info));
17034 part_die->sect_off = (sect_offset) (info_ptr - buffer);
17036 info_ptr += abbrev_len;
17038 if (abbrev == NULL)
17041 part_die->tag = abbrev->tag;
17042 part_die->has_children = abbrev->has_children;
17044 for (i = 0; i < abbrev->num_attrs; ++i)
17046 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
17048 /* Store the data if it is of an attribute we want to keep in a
17049 partial symbol table. */
17053 switch (part_die->tag)
17055 case DW_TAG_compile_unit:
17056 case DW_TAG_partial_unit:
17057 case DW_TAG_type_unit:
17058 /* Compilation units have a DW_AT_name that is a filename, not
17059 a source language identifier. */
17060 case DW_TAG_enumeration_type:
17061 case DW_TAG_enumerator:
17062 /* These tags always have simple identifiers already; no need
17063 to canonicalize them. */
17064 part_die->name = DW_STRING (&attr);
17068 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17069 &objfile->per_bfd->storage_obstack);
17073 case DW_AT_linkage_name:
17074 case DW_AT_MIPS_linkage_name:
17075 /* Note that both forms of linkage name might appear. We
17076 assume they will be the same, and we only store the last
17078 if (cu->language == language_ada)
17079 part_die->name = DW_STRING (&attr);
17080 part_die->linkage_name = DW_STRING (&attr);
17083 has_low_pc_attr = 1;
17084 part_die->lowpc = attr_value_as_address (&attr);
17086 case DW_AT_high_pc:
17087 has_high_pc_attr = 1;
17088 part_die->highpc = attr_value_as_address (&attr);
17089 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17090 high_pc_relative = 1;
17092 case DW_AT_location:
17093 /* Support the .debug_loc offsets. */
17094 if (attr_form_is_block (&attr))
17096 part_die->d.locdesc = DW_BLOCK (&attr);
17098 else if (attr_form_is_section_offset (&attr))
17100 dwarf2_complex_location_expr_complaint ();
17104 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17105 "partial symbol information");
17108 case DW_AT_external:
17109 part_die->is_external = DW_UNSND (&attr);
17111 case DW_AT_declaration:
17112 part_die->is_declaration = DW_UNSND (&attr);
17115 part_die->has_type = 1;
17117 case DW_AT_abstract_origin:
17118 case DW_AT_specification:
17119 case DW_AT_extension:
17120 part_die->has_specification = 1;
17121 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
17122 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17123 || cu->per_cu->is_dwz);
17125 case DW_AT_sibling:
17126 /* Ignore absolute siblings, they might point outside of
17127 the current compile unit. */
17128 if (attr.form == DW_FORM_ref_addr)
17129 complaint (&symfile_complaints,
17130 _("ignoring absolute DW_AT_sibling"));
17133 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17134 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17136 if (sibling_ptr < info_ptr)
17137 complaint (&symfile_complaints,
17138 _("DW_AT_sibling points backwards"));
17139 else if (sibling_ptr > reader->buffer_end)
17140 dwarf2_section_buffer_overflow_complaint (reader->die_section);
17142 part_die->sibling = sibling_ptr;
17145 case DW_AT_byte_size:
17146 part_die->has_byte_size = 1;
17148 case DW_AT_const_value:
17149 part_die->has_const_value = 1;
17151 case DW_AT_calling_convention:
17152 /* DWARF doesn't provide a way to identify a program's source-level
17153 entry point. DW_AT_calling_convention attributes are only meant
17154 to describe functions' calling conventions.
17156 However, because it's a necessary piece of information in
17157 Fortran, and before DWARF 4 DW_CC_program was the only
17158 piece of debugging information whose definition refers to
17159 a 'main program' at all, several compilers marked Fortran
17160 main programs with DW_CC_program --- even when those
17161 functions use the standard calling conventions.
17163 Although DWARF now specifies a way to provide this
17164 information, we support this practice for backward
17166 if (DW_UNSND (&attr) == DW_CC_program
17167 && cu->language == language_fortran)
17168 part_die->main_subprogram = 1;
17171 if (DW_UNSND (&attr) == DW_INL_inlined
17172 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17173 part_die->may_be_inlined = 1;
17177 if (part_die->tag == DW_TAG_imported_unit)
17179 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
17180 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17181 || cu->per_cu->is_dwz);
17185 case DW_AT_main_subprogram:
17186 part_die->main_subprogram = DW_UNSND (&attr);
17194 if (high_pc_relative)
17195 part_die->highpc += part_die->lowpc;
17197 if (has_low_pc_attr && has_high_pc_attr)
17199 /* When using the GNU linker, .gnu.linkonce. sections are used to
17200 eliminate duplicate copies of functions and vtables and such.
17201 The linker will arbitrarily choose one and discard the others.
17202 The AT_*_pc values for such functions refer to local labels in
17203 these sections. If the section from that file was discarded, the
17204 labels are not in the output, so the relocs get a value of 0.
17205 If this is a discarded function, mark the pc bounds as invalid,
17206 so that GDB will ignore it. */
17207 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17209 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17211 complaint (&symfile_complaints,
17212 _("DW_AT_low_pc %s is zero "
17213 "for DIE at 0x%x [in module %s]"),
17214 paddress (gdbarch, part_die->lowpc),
17215 to_underlying (part_die->sect_off), objfile_name (objfile));
17217 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17218 else if (part_die->lowpc >= part_die->highpc)
17220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17222 complaint (&symfile_complaints,
17223 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17224 "for DIE at 0x%x [in module %s]"),
17225 paddress (gdbarch, part_die->lowpc),
17226 paddress (gdbarch, part_die->highpc),
17227 to_underlying (part_die->sect_off),
17228 objfile_name (objfile));
17231 part_die->has_pc_info = 1;
17237 /* Find a cached partial DIE at OFFSET in CU. */
17239 static struct partial_die_info *
17240 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
17242 struct partial_die_info *lookup_die = NULL;
17243 struct partial_die_info part_die;
17245 part_die.sect_off = sect_off;
17246 lookup_die = ((struct partial_die_info *)
17247 htab_find_with_hash (cu->partial_dies, &part_die,
17248 to_underlying (sect_off)));
17253 /* Find a partial DIE at OFFSET, which may or may not be in CU,
17254 except in the case of .debug_types DIEs which do not reference
17255 outside their CU (they do however referencing other types via
17256 DW_FORM_ref_sig8). */
17258 static struct partial_die_info *
17259 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
17261 struct objfile *objfile = cu->objfile;
17262 struct dwarf2_per_cu_data *per_cu = NULL;
17263 struct partial_die_info *pd = NULL;
17265 if (offset_in_dwz == cu->per_cu->is_dwz
17266 && offset_in_cu_p (&cu->header, sect_off))
17268 pd = find_partial_die_in_comp_unit (sect_off, cu);
17271 /* We missed recording what we needed.
17272 Load all dies and try again. */
17273 per_cu = cu->per_cu;
17277 /* TUs don't reference other CUs/TUs (except via type signatures). */
17278 if (cu->per_cu->is_debug_types)
17280 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17281 " external reference to offset 0x%x [in module %s].\n"),
17282 to_underlying (cu->header.sect_off), to_underlying (sect_off),
17283 bfd_get_filename (objfile->obfd));
17285 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
17288 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17289 load_partial_comp_unit (per_cu);
17291 per_cu->cu->last_used = 0;
17292 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17295 /* If we didn't find it, and not all dies have been loaded,
17296 load them all and try again. */
17298 if (pd == NULL && per_cu->load_all_dies == 0)
17300 per_cu->load_all_dies = 1;
17302 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17303 THIS_CU->cu may already be in use. So we can't just free it and
17304 replace its DIEs with the ones we read in. Instead, we leave those
17305 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17306 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17308 load_partial_comp_unit (per_cu);
17310 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17314 internal_error (__FILE__, __LINE__,
17315 _("could not find partial DIE 0x%x "
17316 "in cache [from module %s]\n"),
17317 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
17321 /* See if we can figure out if the class lives in a namespace. We do
17322 this by looking for a member function; its demangled name will
17323 contain namespace info, if there is any. */
17326 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17327 struct dwarf2_cu *cu)
17329 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17330 what template types look like, because the demangler
17331 frequently doesn't give the same name as the debug info. We
17332 could fix this by only using the demangled name to get the
17333 prefix (but see comment in read_structure_type). */
17335 struct partial_die_info *real_pdi;
17336 struct partial_die_info *child_pdi;
17338 /* If this DIE (this DIE's specification, if any) has a parent, then
17339 we should not do this. We'll prepend the parent's fully qualified
17340 name when we create the partial symbol. */
17342 real_pdi = struct_pdi;
17343 while (real_pdi->has_specification)
17344 real_pdi = find_partial_die (real_pdi->spec_offset,
17345 real_pdi->spec_is_dwz, cu);
17347 if (real_pdi->die_parent != NULL)
17350 for (child_pdi = struct_pdi->die_child;
17352 child_pdi = child_pdi->die_sibling)
17354 if (child_pdi->tag == DW_TAG_subprogram
17355 && child_pdi->linkage_name != NULL)
17357 char *actual_class_name
17358 = language_class_name_from_physname (cu->language_defn,
17359 child_pdi->linkage_name);
17360 if (actual_class_name != NULL)
17364 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17366 strlen (actual_class_name)));
17367 xfree (actual_class_name);
17374 /* Adjust PART_DIE before generating a symbol for it. This function
17375 may set the is_external flag or change the DIE's name. */
17378 fixup_partial_die (struct partial_die_info *part_die,
17379 struct dwarf2_cu *cu)
17381 /* Once we've fixed up a die, there's no point in doing so again.
17382 This also avoids a memory leak if we were to call
17383 guess_partial_die_structure_name multiple times. */
17384 if (part_die->fixup_called)
17387 /* If we found a reference attribute and the DIE has no name, try
17388 to find a name in the referred to DIE. */
17390 if (part_die->name == NULL && part_die->has_specification)
17392 struct partial_die_info *spec_die;
17394 spec_die = find_partial_die (part_die->spec_offset,
17395 part_die->spec_is_dwz, cu);
17397 fixup_partial_die (spec_die, cu);
17399 if (spec_die->name)
17401 part_die->name = spec_die->name;
17403 /* Copy DW_AT_external attribute if it is set. */
17404 if (spec_die->is_external)
17405 part_die->is_external = spec_die->is_external;
17409 /* Set default names for some unnamed DIEs. */
17411 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
17412 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
17414 /* If there is no parent die to provide a namespace, and there are
17415 children, see if we can determine the namespace from their linkage
17417 if (cu->language == language_cplus
17418 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17419 && part_die->die_parent == NULL
17420 && part_die->has_children
17421 && (part_die->tag == DW_TAG_class_type
17422 || part_die->tag == DW_TAG_structure_type
17423 || part_die->tag == DW_TAG_union_type))
17424 guess_partial_die_structure_name (part_die, cu);
17426 /* GCC might emit a nameless struct or union that has a linkage
17427 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17428 if (part_die->name == NULL
17429 && (part_die->tag == DW_TAG_class_type
17430 || part_die->tag == DW_TAG_interface_type
17431 || part_die->tag == DW_TAG_structure_type
17432 || part_die->tag == DW_TAG_union_type)
17433 && part_die->linkage_name != NULL)
17437 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
17442 /* Strip any leading namespaces/classes, keep only the base name.
17443 DW_AT_name for named DIEs does not contain the prefixes. */
17444 base = strrchr (demangled, ':');
17445 if (base && base > demangled && base[-1] == ':')
17452 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17453 base, strlen (base)));
17458 part_die->fixup_called = 1;
17461 /* Read an attribute value described by an attribute form. */
17463 static const gdb_byte *
17464 read_attribute_value (const struct die_reader_specs *reader,
17465 struct attribute *attr, unsigned form,
17466 LONGEST implicit_const, const gdb_byte *info_ptr)
17468 struct dwarf2_cu *cu = reader->cu;
17469 struct objfile *objfile = cu->objfile;
17470 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17471 bfd *abfd = reader->abfd;
17472 struct comp_unit_head *cu_header = &cu->header;
17473 unsigned int bytes_read;
17474 struct dwarf_block *blk;
17476 attr->form = (enum dwarf_form) form;
17479 case DW_FORM_ref_addr:
17480 if (cu->header.version == 2)
17481 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17483 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17484 &cu->header, &bytes_read);
17485 info_ptr += bytes_read;
17487 case DW_FORM_GNU_ref_alt:
17488 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17489 info_ptr += bytes_read;
17492 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17493 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
17494 info_ptr += bytes_read;
17496 case DW_FORM_block2:
17497 blk = dwarf_alloc_block (cu);
17498 blk->size = read_2_bytes (abfd, info_ptr);
17500 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17501 info_ptr += blk->size;
17502 DW_BLOCK (attr) = blk;
17504 case DW_FORM_block4:
17505 blk = dwarf_alloc_block (cu);
17506 blk->size = read_4_bytes (abfd, info_ptr);
17508 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17509 info_ptr += blk->size;
17510 DW_BLOCK (attr) = blk;
17512 case DW_FORM_data2:
17513 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17516 case DW_FORM_data4:
17517 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17520 case DW_FORM_data8:
17521 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17524 case DW_FORM_data16:
17525 blk = dwarf_alloc_block (cu);
17527 blk->data = read_n_bytes (abfd, info_ptr, 16);
17529 DW_BLOCK (attr) = blk;
17531 case DW_FORM_sec_offset:
17532 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17533 info_ptr += bytes_read;
17535 case DW_FORM_string:
17536 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
17537 DW_STRING_IS_CANONICAL (attr) = 0;
17538 info_ptr += bytes_read;
17541 if (!cu->per_cu->is_dwz)
17543 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17545 DW_STRING_IS_CANONICAL (attr) = 0;
17546 info_ptr += bytes_read;
17550 case DW_FORM_line_strp:
17551 if (!cu->per_cu->is_dwz)
17553 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17554 cu_header, &bytes_read);
17555 DW_STRING_IS_CANONICAL (attr) = 0;
17556 info_ptr += bytes_read;
17560 case DW_FORM_GNU_strp_alt:
17562 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17563 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17566 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17567 DW_STRING_IS_CANONICAL (attr) = 0;
17568 info_ptr += bytes_read;
17571 case DW_FORM_exprloc:
17572 case DW_FORM_block:
17573 blk = dwarf_alloc_block (cu);
17574 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17575 info_ptr += bytes_read;
17576 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17577 info_ptr += blk->size;
17578 DW_BLOCK (attr) = blk;
17580 case DW_FORM_block1:
17581 blk = dwarf_alloc_block (cu);
17582 blk->size = read_1_byte (abfd, info_ptr);
17584 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17585 info_ptr += blk->size;
17586 DW_BLOCK (attr) = blk;
17588 case DW_FORM_data1:
17589 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17593 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17596 case DW_FORM_flag_present:
17597 DW_UNSND (attr) = 1;
17599 case DW_FORM_sdata:
17600 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17601 info_ptr += bytes_read;
17603 case DW_FORM_udata:
17604 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17605 info_ptr += bytes_read;
17608 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17609 + read_1_byte (abfd, info_ptr));
17613 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17614 + read_2_bytes (abfd, info_ptr));
17618 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17619 + read_4_bytes (abfd, info_ptr));
17623 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17624 + read_8_bytes (abfd, info_ptr));
17627 case DW_FORM_ref_sig8:
17628 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
17631 case DW_FORM_ref_udata:
17632 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17633 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17634 info_ptr += bytes_read;
17636 case DW_FORM_indirect:
17637 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17638 info_ptr += bytes_read;
17639 if (form == DW_FORM_implicit_const)
17641 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17642 info_ptr += bytes_read;
17644 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17647 case DW_FORM_implicit_const:
17648 DW_SND (attr) = implicit_const;
17650 case DW_FORM_GNU_addr_index:
17651 if (reader->dwo_file == NULL)
17653 /* For now flag a hard error.
17654 Later we can turn this into a complaint. */
17655 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17656 dwarf_form_name (form),
17657 bfd_get_filename (abfd));
17659 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17660 info_ptr += bytes_read;
17662 case DW_FORM_GNU_str_index:
17663 if (reader->dwo_file == NULL)
17665 /* For now flag a hard error.
17666 Later we can turn this into a complaint if warranted. */
17667 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17668 dwarf_form_name (form),
17669 bfd_get_filename (abfd));
17672 ULONGEST str_index =
17673 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17675 DW_STRING (attr) = read_str_index (reader, str_index);
17676 DW_STRING_IS_CANONICAL (attr) = 0;
17677 info_ptr += bytes_read;
17681 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17682 dwarf_form_name (form),
17683 bfd_get_filename (abfd));
17687 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
17688 attr->form = DW_FORM_GNU_ref_alt;
17690 /* We have seen instances where the compiler tried to emit a byte
17691 size attribute of -1 which ended up being encoded as an unsigned
17692 0xffffffff. Although 0xffffffff is technically a valid size value,
17693 an object of this size seems pretty unlikely so we can relatively
17694 safely treat these cases as if the size attribute was invalid and
17695 treat them as zero by default. */
17696 if (attr->name == DW_AT_byte_size
17697 && form == DW_FORM_data4
17698 && DW_UNSND (attr) >= 0xffffffff)
17701 (&symfile_complaints,
17702 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17703 hex_string (DW_UNSND (attr)));
17704 DW_UNSND (attr) = 0;
17710 /* Read an attribute described by an abbreviated attribute. */
17712 static const gdb_byte *
17713 read_attribute (const struct die_reader_specs *reader,
17714 struct attribute *attr, struct attr_abbrev *abbrev,
17715 const gdb_byte *info_ptr)
17717 attr->name = abbrev->name;
17718 return read_attribute_value (reader, attr, abbrev->form,
17719 abbrev->implicit_const, info_ptr);
17722 /* Read dwarf information from a buffer. */
17724 static unsigned int
17725 read_1_byte (bfd *abfd, const gdb_byte *buf)
17727 return bfd_get_8 (abfd, buf);
17731 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
17733 return bfd_get_signed_8 (abfd, buf);
17736 static unsigned int
17737 read_2_bytes (bfd *abfd, const gdb_byte *buf)
17739 return bfd_get_16 (abfd, buf);
17743 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
17745 return bfd_get_signed_16 (abfd, buf);
17748 static unsigned int
17749 read_4_bytes (bfd *abfd, const gdb_byte *buf)
17751 return bfd_get_32 (abfd, buf);
17755 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
17757 return bfd_get_signed_32 (abfd, buf);
17761 read_8_bytes (bfd *abfd, const gdb_byte *buf)
17763 return bfd_get_64 (abfd, buf);
17767 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
17768 unsigned int *bytes_read)
17770 struct comp_unit_head *cu_header = &cu->header;
17771 CORE_ADDR retval = 0;
17773 if (cu_header->signed_addr_p)
17775 switch (cu_header->addr_size)
17778 retval = bfd_get_signed_16 (abfd, buf);
17781 retval = bfd_get_signed_32 (abfd, buf);
17784 retval = bfd_get_signed_64 (abfd, buf);
17787 internal_error (__FILE__, __LINE__,
17788 _("read_address: bad switch, signed [in module %s]"),
17789 bfd_get_filename (abfd));
17794 switch (cu_header->addr_size)
17797 retval = bfd_get_16 (abfd, buf);
17800 retval = bfd_get_32 (abfd, buf);
17803 retval = bfd_get_64 (abfd, buf);
17806 internal_error (__FILE__, __LINE__,
17807 _("read_address: bad switch, "
17808 "unsigned [in module %s]"),
17809 bfd_get_filename (abfd));
17813 *bytes_read = cu_header->addr_size;
17817 /* Read the initial length from a section. The (draft) DWARF 3
17818 specification allows the initial length to take up either 4 bytes
17819 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17820 bytes describe the length and all offsets will be 8 bytes in length
17823 An older, non-standard 64-bit format is also handled by this
17824 function. The older format in question stores the initial length
17825 as an 8-byte quantity without an escape value. Lengths greater
17826 than 2^32 aren't very common which means that the initial 4 bytes
17827 is almost always zero. Since a length value of zero doesn't make
17828 sense for the 32-bit format, this initial zero can be considered to
17829 be an escape value which indicates the presence of the older 64-bit
17830 format. As written, the code can't detect (old format) lengths
17831 greater than 4GB. If it becomes necessary to handle lengths
17832 somewhat larger than 4GB, we could allow other small values (such
17833 as the non-sensical values of 1, 2, and 3) to also be used as
17834 escape values indicating the presence of the old format.
17836 The value returned via bytes_read should be used to increment the
17837 relevant pointer after calling read_initial_length().
17839 [ Note: read_initial_length() and read_offset() are based on the
17840 document entitled "DWARF Debugging Information Format", revision
17841 3, draft 8, dated November 19, 2001. This document was obtained
17844 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
17846 This document is only a draft and is subject to change. (So beware.)
17848 Details regarding the older, non-standard 64-bit format were
17849 determined empirically by examining 64-bit ELF files produced by
17850 the SGI toolchain on an IRIX 6.5 machine.
17852 - Kevin, July 16, 2002
17856 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
17858 LONGEST length = bfd_get_32 (abfd, buf);
17860 if (length == 0xffffffff)
17862 length = bfd_get_64 (abfd, buf + 4);
17865 else if (length == 0)
17867 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
17868 length = bfd_get_64 (abfd, buf);
17879 /* Cover function for read_initial_length.
17880 Returns the length of the object at BUF, and stores the size of the
17881 initial length in *BYTES_READ and stores the size that offsets will be in
17883 If the initial length size is not equivalent to that specified in
17884 CU_HEADER then issue a complaint.
17885 This is useful when reading non-comp-unit headers. */
17888 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17889 const struct comp_unit_head *cu_header,
17890 unsigned int *bytes_read,
17891 unsigned int *offset_size)
17893 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17895 gdb_assert (cu_header->initial_length_size == 4
17896 || cu_header->initial_length_size == 8
17897 || cu_header->initial_length_size == 12);
17899 if (cu_header->initial_length_size != *bytes_read)
17900 complaint (&symfile_complaints,
17901 _("intermixed 32-bit and 64-bit DWARF sections"));
17903 *offset_size = (*bytes_read == 4) ? 4 : 8;
17907 /* Read an offset from the data stream. The size of the offset is
17908 given by cu_header->offset_size. */
17911 read_offset (bfd *abfd, const gdb_byte *buf,
17912 const struct comp_unit_head *cu_header,
17913 unsigned int *bytes_read)
17915 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17917 *bytes_read = cu_header->offset_size;
17921 /* Read an offset from the data stream. */
17924 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17926 LONGEST retval = 0;
17928 switch (offset_size)
17931 retval = bfd_get_32 (abfd, buf);
17934 retval = bfd_get_64 (abfd, buf);
17937 internal_error (__FILE__, __LINE__,
17938 _("read_offset_1: bad switch [in module %s]"),
17939 bfd_get_filename (abfd));
17945 static const gdb_byte *
17946 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17948 /* If the size of a host char is 8 bits, we can return a pointer
17949 to the buffer, otherwise we have to copy the data to a buffer
17950 allocated on the temporary obstack. */
17951 gdb_assert (HOST_CHAR_BIT == 8);
17955 static const char *
17956 read_direct_string (bfd *abfd, const gdb_byte *buf,
17957 unsigned int *bytes_read_ptr)
17959 /* If the size of a host char is 8 bits, we can return a pointer
17960 to the string, otherwise we have to copy the string to a buffer
17961 allocated on the temporary obstack. */
17962 gdb_assert (HOST_CHAR_BIT == 8);
17965 *bytes_read_ptr = 1;
17968 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17969 return (const char *) buf;
17972 /* Return pointer to string at section SECT offset STR_OFFSET with error
17973 reporting strings FORM_NAME and SECT_NAME. */
17975 static const char *
17976 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17977 struct dwarf2_section_info *sect,
17978 const char *form_name,
17979 const char *sect_name)
17981 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17982 if (sect->buffer == NULL)
17983 error (_("%s used without %s section [in module %s]"),
17984 form_name, sect_name, bfd_get_filename (abfd));
17985 if (str_offset >= sect->size)
17986 error (_("%s pointing outside of %s section [in module %s]"),
17987 form_name, sect_name, bfd_get_filename (abfd));
17988 gdb_assert (HOST_CHAR_BIT == 8);
17989 if (sect->buffer[str_offset] == '\0')
17991 return (const char *) (sect->buffer + str_offset);
17994 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17996 static const char *
17997 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17999 return read_indirect_string_at_offset_from (abfd, str_offset,
18000 &dwarf2_per_objfile->str,
18001 "DW_FORM_strp", ".debug_str");
18004 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18006 static const char *
18007 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18009 return read_indirect_string_at_offset_from (abfd, str_offset,
18010 &dwarf2_per_objfile->line_str,
18011 "DW_FORM_line_strp",
18012 ".debug_line_str");
18015 /* Read a string at offset STR_OFFSET in the .debug_str section from
18016 the .dwz file DWZ. Throw an error if the offset is too large. If
18017 the string consists of a single NUL byte, return NULL; otherwise
18018 return a pointer to the string. */
18020 static const char *
18021 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18023 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18025 if (dwz->str.buffer == NULL)
18026 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18027 "section [in module %s]"),
18028 bfd_get_filename (dwz->dwz_bfd));
18029 if (str_offset >= dwz->str.size)
18030 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18031 ".debug_str section [in module %s]"),
18032 bfd_get_filename (dwz->dwz_bfd));
18033 gdb_assert (HOST_CHAR_BIT == 8);
18034 if (dwz->str.buffer[str_offset] == '\0')
18036 return (const char *) (dwz->str.buffer + str_offset);
18039 /* Return pointer to string at .debug_str offset as read from BUF.
18040 BUF is assumed to be in a compilation unit described by CU_HEADER.
18041 Return *BYTES_READ_PTR count of bytes read from BUF. */
18043 static const char *
18044 read_indirect_string (bfd *abfd, const gdb_byte *buf,
18045 const struct comp_unit_head *cu_header,
18046 unsigned int *bytes_read_ptr)
18048 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18050 return read_indirect_string_at_offset (abfd, str_offset);
18053 /* Return pointer to string at .debug_line_str offset as read from BUF.
18054 BUF is assumed to be in a compilation unit described by CU_HEADER.
18055 Return *BYTES_READ_PTR count of bytes read from BUF. */
18057 static const char *
18058 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18059 const struct comp_unit_head *cu_header,
18060 unsigned int *bytes_read_ptr)
18062 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18064 return read_indirect_line_string_at_offset (abfd, str_offset);
18068 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
18069 unsigned int *bytes_read_ptr)
18072 unsigned int num_read;
18074 unsigned char byte;
18081 byte = bfd_get_8 (abfd, buf);
18084 result |= ((ULONGEST) (byte & 127) << shift);
18085 if ((byte & 128) == 0)
18091 *bytes_read_ptr = num_read;
18096 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18097 unsigned int *bytes_read_ptr)
18100 int shift, num_read;
18101 unsigned char byte;
18108 byte = bfd_get_8 (abfd, buf);
18111 result |= ((LONGEST) (byte & 127) << shift);
18113 if ((byte & 128) == 0)
18118 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
18119 result |= -(((LONGEST) 1) << shift);
18120 *bytes_read_ptr = num_read;
18124 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18125 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18126 ADDR_SIZE is the size of addresses from the CU header. */
18129 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18131 struct objfile *objfile = dwarf2_per_objfile->objfile;
18132 bfd *abfd = objfile->obfd;
18133 const gdb_byte *info_ptr;
18135 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18136 if (dwarf2_per_objfile->addr.buffer == NULL)
18137 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18138 objfile_name (objfile));
18139 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18140 error (_("DW_FORM_addr_index pointing outside of "
18141 ".debug_addr section [in module %s]"),
18142 objfile_name (objfile));
18143 info_ptr = (dwarf2_per_objfile->addr.buffer
18144 + addr_base + addr_index * addr_size);
18145 if (addr_size == 4)
18146 return bfd_get_32 (abfd, info_ptr);
18148 return bfd_get_64 (abfd, info_ptr);
18151 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18154 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18156 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18159 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18162 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18163 unsigned int *bytes_read)
18165 bfd *abfd = cu->objfile->obfd;
18166 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18168 return read_addr_index (cu, addr_index);
18171 /* Data structure to pass results from dwarf2_read_addr_index_reader
18172 back to dwarf2_read_addr_index. */
18174 struct dwarf2_read_addr_index_data
18176 ULONGEST addr_base;
18180 /* die_reader_func for dwarf2_read_addr_index. */
18183 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
18184 const gdb_byte *info_ptr,
18185 struct die_info *comp_unit_die,
18189 struct dwarf2_cu *cu = reader->cu;
18190 struct dwarf2_read_addr_index_data *aidata =
18191 (struct dwarf2_read_addr_index_data *) data;
18193 aidata->addr_base = cu->addr_base;
18194 aidata->addr_size = cu->header.addr_size;
18197 /* Given an index in .debug_addr, fetch the value.
18198 NOTE: This can be called during dwarf expression evaluation,
18199 long after the debug information has been read, and thus per_cu->cu
18200 may no longer exist. */
18203 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18204 unsigned int addr_index)
18206 struct objfile *objfile = per_cu->objfile;
18207 struct dwarf2_cu *cu = per_cu->cu;
18208 ULONGEST addr_base;
18211 /* This is intended to be called from outside this file. */
18212 dw2_setup (objfile);
18214 /* We need addr_base and addr_size.
18215 If we don't have PER_CU->cu, we have to get it.
18216 Nasty, but the alternative is storing the needed info in PER_CU,
18217 which at this point doesn't seem justified: it's not clear how frequently
18218 it would get used and it would increase the size of every PER_CU.
18219 Entry points like dwarf2_per_cu_addr_size do a similar thing
18220 so we're not in uncharted territory here.
18221 Alas we need to be a bit more complicated as addr_base is contained
18224 We don't need to read the entire CU(/TU).
18225 We just need the header and top level die.
18227 IWBN to use the aging mechanism to let us lazily later discard the CU.
18228 For now we skip this optimization. */
18232 addr_base = cu->addr_base;
18233 addr_size = cu->header.addr_size;
18237 struct dwarf2_read_addr_index_data aidata;
18239 /* Note: We can't use init_cutu_and_read_dies_simple here,
18240 we need addr_base. */
18241 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18242 dwarf2_read_addr_index_reader, &aidata);
18243 addr_base = aidata.addr_base;
18244 addr_size = aidata.addr_size;
18247 return read_addr_index_1 (addr_index, addr_base, addr_size);
18250 /* Given a DW_FORM_GNU_str_index, fetch the string.
18251 This is only used by the Fission support. */
18253 static const char *
18254 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18256 struct objfile *objfile = dwarf2_per_objfile->objfile;
18257 const char *objf_name = objfile_name (objfile);
18258 bfd *abfd = objfile->obfd;
18259 struct dwarf2_cu *cu = reader->cu;
18260 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18261 struct dwarf2_section_info *str_offsets_section =
18262 &reader->dwo_file->sections.str_offsets;
18263 const gdb_byte *info_ptr;
18264 ULONGEST str_offset;
18265 static const char form_name[] = "DW_FORM_GNU_str_index";
18267 dwarf2_read_section (objfile, str_section);
18268 dwarf2_read_section (objfile, str_offsets_section);
18269 if (str_section->buffer == NULL)
18270 error (_("%s used without .debug_str.dwo section"
18271 " in CU at offset 0x%x [in module %s]"),
18272 form_name, to_underlying (cu->header.sect_off), objf_name);
18273 if (str_offsets_section->buffer == NULL)
18274 error (_("%s used without .debug_str_offsets.dwo section"
18275 " in CU at offset 0x%x [in module %s]"),
18276 form_name, to_underlying (cu->header.sect_off), objf_name);
18277 if (str_index * cu->header.offset_size >= str_offsets_section->size)
18278 error (_("%s pointing outside of .debug_str_offsets.dwo"
18279 " section in CU at offset 0x%x [in module %s]"),
18280 form_name, to_underlying (cu->header.sect_off), objf_name);
18281 info_ptr = (str_offsets_section->buffer
18282 + str_index * cu->header.offset_size);
18283 if (cu->header.offset_size == 4)
18284 str_offset = bfd_get_32 (abfd, info_ptr);
18286 str_offset = bfd_get_64 (abfd, info_ptr);
18287 if (str_offset >= str_section->size)
18288 error (_("Offset from %s pointing outside of"
18289 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18290 form_name, to_underlying (cu->header.sect_off), objf_name);
18291 return (const char *) (str_section->buffer + str_offset);
18294 /* Return the length of an LEB128 number in BUF. */
18297 leb128_size (const gdb_byte *buf)
18299 const gdb_byte *begin = buf;
18305 if ((byte & 128) == 0)
18306 return buf - begin;
18311 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18320 cu->language = language_c;
18323 case DW_LANG_C_plus_plus:
18324 case DW_LANG_C_plus_plus_11:
18325 case DW_LANG_C_plus_plus_14:
18326 cu->language = language_cplus;
18329 cu->language = language_d;
18331 case DW_LANG_Fortran77:
18332 case DW_LANG_Fortran90:
18333 case DW_LANG_Fortran95:
18334 case DW_LANG_Fortran03:
18335 case DW_LANG_Fortran08:
18336 cu->language = language_fortran;
18339 cu->language = language_go;
18341 case DW_LANG_Mips_Assembler:
18342 cu->language = language_asm;
18344 case DW_LANG_Ada83:
18345 case DW_LANG_Ada95:
18346 cu->language = language_ada;
18348 case DW_LANG_Modula2:
18349 cu->language = language_m2;
18351 case DW_LANG_Pascal83:
18352 cu->language = language_pascal;
18355 cu->language = language_objc;
18358 case DW_LANG_Rust_old:
18359 cu->language = language_rust;
18361 case DW_LANG_Cobol74:
18362 case DW_LANG_Cobol85:
18364 cu->language = language_minimal;
18367 cu->language_defn = language_def (cu->language);
18370 /* Return the named attribute or NULL if not there. */
18372 static struct attribute *
18373 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18378 struct attribute *spec = NULL;
18380 for (i = 0; i < die->num_attrs; ++i)
18382 if (die->attrs[i].name == name)
18383 return &die->attrs[i];
18384 if (die->attrs[i].name == DW_AT_specification
18385 || die->attrs[i].name == DW_AT_abstract_origin)
18386 spec = &die->attrs[i];
18392 die = follow_die_ref (die, spec, &cu);
18398 /* Return the named attribute or NULL if not there,
18399 but do not follow DW_AT_specification, etc.
18400 This is for use in contexts where we're reading .debug_types dies.
18401 Following DW_AT_specification, DW_AT_abstract_origin will take us
18402 back up the chain, and we want to go down. */
18404 static struct attribute *
18405 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
18409 for (i = 0; i < die->num_attrs; ++i)
18410 if (die->attrs[i].name == name)
18411 return &die->attrs[i];
18416 /* Return the string associated with a string-typed attribute, or NULL if it
18417 is either not found or is of an incorrect type. */
18419 static const char *
18420 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18422 struct attribute *attr;
18423 const char *str = NULL;
18425 attr = dwarf2_attr (die, name, cu);
18429 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18430 || attr->form == DW_FORM_string
18431 || attr->form == DW_FORM_GNU_str_index
18432 || attr->form == DW_FORM_GNU_strp_alt)
18433 str = DW_STRING (attr);
18435 complaint (&symfile_complaints,
18436 _("string type expected for attribute %s for "
18437 "DIE at 0x%x in module %s"),
18438 dwarf_attr_name (name), to_underlying (die->sect_off),
18439 objfile_name (cu->objfile));
18445 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18446 and holds a non-zero value. This function should only be used for
18447 DW_FORM_flag or DW_FORM_flag_present attributes. */
18450 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18452 struct attribute *attr = dwarf2_attr (die, name, cu);
18454 return (attr && DW_UNSND (attr));
18458 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
18460 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18461 which value is non-zero. However, we have to be careful with
18462 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18463 (via dwarf2_flag_true_p) follows this attribute. So we may
18464 end up accidently finding a declaration attribute that belongs
18465 to a different DIE referenced by the specification attribute,
18466 even though the given DIE does not have a declaration attribute. */
18467 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18468 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
18471 /* Return the die giving the specification for DIE, if there is
18472 one. *SPEC_CU is the CU containing DIE on input, and the CU
18473 containing the return value on output. If there is no
18474 specification, but there is an abstract origin, that is
18477 static struct die_info *
18478 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
18480 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18483 if (spec_attr == NULL)
18484 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18486 if (spec_attr == NULL)
18489 return follow_die_ref (die, spec_attr, spec_cu);
18492 /* Stub for free_line_header to match void * callback types. */
18495 free_line_header_voidp (void *arg)
18497 struct line_header *lh = (struct line_header *) arg;
18503 line_header::add_include_dir (const char *include_dir)
18505 if (dwarf_line_debug >= 2)
18506 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18507 include_dirs.size () + 1, include_dir);
18509 include_dirs.push_back (include_dir);
18513 line_header::add_file_name (const char *name,
18515 unsigned int mod_time,
18516 unsigned int length)
18518 if (dwarf_line_debug >= 2)
18519 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
18520 (unsigned) file_names.size () + 1, name);
18522 file_names.emplace_back (name, d_index, mod_time, length);
18525 /* A convenience function to find the proper .debug_line section for a CU. */
18527 static struct dwarf2_section_info *
18528 get_debug_line_section (struct dwarf2_cu *cu)
18530 struct dwarf2_section_info *section;
18532 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18534 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18535 section = &cu->dwo_unit->dwo_file->sections.line;
18536 else if (cu->per_cu->is_dwz)
18538 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18540 section = &dwz->line;
18543 section = &dwarf2_per_objfile->line;
18548 /* Read directory or file name entry format, starting with byte of
18549 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18550 entries count and the entries themselves in the described entry
18554 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18555 struct line_header *lh,
18556 const struct comp_unit_head *cu_header,
18557 void (*callback) (struct line_header *lh,
18560 unsigned int mod_time,
18561 unsigned int length))
18563 gdb_byte format_count, formati;
18564 ULONGEST data_count, datai;
18565 const gdb_byte *buf = *bufp;
18566 const gdb_byte *format_header_data;
18568 unsigned int bytes_read;
18570 format_count = read_1_byte (abfd, buf);
18572 format_header_data = buf;
18573 for (formati = 0; formati < format_count; formati++)
18575 read_unsigned_leb128 (abfd, buf, &bytes_read);
18577 read_unsigned_leb128 (abfd, buf, &bytes_read);
18581 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18583 for (datai = 0; datai < data_count; datai++)
18585 const gdb_byte *format = format_header_data;
18586 struct file_entry fe;
18588 for (formati = 0; formati < format_count; formati++)
18590 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
18591 format += bytes_read;
18593 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
18594 format += bytes_read;
18596 gdb::optional<const char *> string;
18597 gdb::optional<unsigned int> uint;
18601 case DW_FORM_string:
18602 string.emplace (read_direct_string (abfd, buf, &bytes_read));
18606 case DW_FORM_line_strp:
18607 string.emplace (read_indirect_line_string (abfd, buf,
18613 case DW_FORM_data1:
18614 uint.emplace (read_1_byte (abfd, buf));
18618 case DW_FORM_data2:
18619 uint.emplace (read_2_bytes (abfd, buf));
18623 case DW_FORM_data4:
18624 uint.emplace (read_4_bytes (abfd, buf));
18628 case DW_FORM_data8:
18629 uint.emplace (read_8_bytes (abfd, buf));
18633 case DW_FORM_udata:
18634 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
18638 case DW_FORM_block:
18639 /* It is valid only for DW_LNCT_timestamp which is ignored by
18644 switch (content_type)
18647 if (string.has_value ())
18650 case DW_LNCT_directory_index:
18651 if (uint.has_value ())
18652 fe.d_index = (dir_index) *uint;
18654 case DW_LNCT_timestamp:
18655 if (uint.has_value ())
18656 fe.mod_time = *uint;
18659 if (uint.has_value ())
18665 complaint (&symfile_complaints,
18666 _("Unknown format content type %s"),
18667 pulongest (content_type));
18671 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
18677 /* Read the statement program header starting at OFFSET in
18678 .debug_line, or .debug_line.dwo. Return a pointer
18679 to a struct line_header, allocated using xmalloc.
18680 Returns NULL if there is a problem reading the header, e.g., if it
18681 has a version we don't understand.
18683 NOTE: the strings in the include directory and file name tables of
18684 the returned object point into the dwarf line section buffer,
18685 and must not be freed. */
18687 static line_header_up
18688 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
18690 const gdb_byte *line_ptr;
18691 unsigned int bytes_read, offset_size;
18693 const char *cur_dir, *cur_file;
18694 struct dwarf2_section_info *section;
18697 section = get_debug_line_section (cu);
18698 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18699 if (section->buffer == NULL)
18701 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18702 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18704 complaint (&symfile_complaints, _("missing .debug_line section"));
18708 /* We can't do this until we know the section is non-empty.
18709 Only then do we know we have such a section. */
18710 abfd = get_section_bfd_owner (section);
18712 /* Make sure that at least there's room for the total_length field.
18713 That could be 12 bytes long, but we're just going to fudge that. */
18714 if (to_underlying (sect_off) + 4 >= section->size)
18716 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18720 line_header_up lh (new line_header ());
18722 lh->sect_off = sect_off;
18723 lh->offset_in_dwz = cu->per_cu->is_dwz;
18725 line_ptr = section->buffer + to_underlying (sect_off);
18727 /* Read in the header. */
18729 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18730 &bytes_read, &offset_size);
18731 line_ptr += bytes_read;
18732 if (line_ptr + lh->total_length > (section->buffer + section->size))
18734 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18737 lh->statement_program_end = line_ptr + lh->total_length;
18738 lh->version = read_2_bytes (abfd, line_ptr);
18740 if (lh->version > 5)
18742 /* This is a version we don't understand. The format could have
18743 changed in ways we don't handle properly so just punt. */
18744 complaint (&symfile_complaints,
18745 _("unsupported version in .debug_line section"));
18748 if (lh->version >= 5)
18750 gdb_byte segment_selector_size;
18752 /* Skip address size. */
18753 read_1_byte (abfd, line_ptr);
18756 segment_selector_size = read_1_byte (abfd, line_ptr);
18758 if (segment_selector_size != 0)
18760 complaint (&symfile_complaints,
18761 _("unsupported segment selector size %u "
18762 "in .debug_line section"),
18763 segment_selector_size);
18767 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18768 line_ptr += offset_size;
18769 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18771 if (lh->version >= 4)
18773 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18777 lh->maximum_ops_per_instruction = 1;
18779 if (lh->maximum_ops_per_instruction == 0)
18781 lh->maximum_ops_per_instruction = 1;
18782 complaint (&symfile_complaints,
18783 _("invalid maximum_ops_per_instruction "
18784 "in `.debug_line' section"));
18787 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18789 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18791 lh->line_range = read_1_byte (abfd, line_ptr);
18793 lh->opcode_base = read_1_byte (abfd, line_ptr);
18795 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
18797 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18798 for (i = 1; i < lh->opcode_base; ++i)
18800 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18804 if (lh->version >= 5)
18806 /* Read directory table. */
18807 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18808 [] (struct line_header *lh, const char *name,
18809 dir_index d_index, unsigned int mod_time,
18810 unsigned int length)
18812 lh->add_include_dir (name);
18815 /* Read file name table. */
18816 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18817 [] (struct line_header *lh, const char *name,
18818 dir_index d_index, unsigned int mod_time,
18819 unsigned int length)
18821 lh->add_file_name (name, d_index, mod_time, length);
18826 /* Read directory table. */
18827 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18829 line_ptr += bytes_read;
18830 lh->add_include_dir (cur_dir);
18832 line_ptr += bytes_read;
18834 /* Read file name table. */
18835 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18837 unsigned int mod_time, length;
18840 line_ptr += bytes_read;
18841 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18842 line_ptr += bytes_read;
18843 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18844 line_ptr += bytes_read;
18845 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18846 line_ptr += bytes_read;
18848 lh->add_file_name (cur_file, d_index, mod_time, length);
18850 line_ptr += bytes_read;
18852 lh->statement_program_start = line_ptr;
18854 if (line_ptr > (section->buffer + section->size))
18855 complaint (&symfile_complaints,
18856 _("line number info header doesn't "
18857 "fit in `.debug_line' section"));
18862 /* Subroutine of dwarf_decode_lines to simplify it.
18863 Return the file name of the psymtab for included file FILE_INDEX
18864 in line header LH of PST.
18865 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18866 If space for the result is malloc'd, it will be freed by a cleanup.
18867 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18869 The function creates dangling cleanup registration. */
18871 static const char *
18872 psymtab_include_file_name (const struct line_header *lh, int file_index,
18873 const struct partial_symtab *pst,
18874 const char *comp_dir)
18876 const file_entry &fe = lh->file_names[file_index];
18877 const char *include_name = fe.name;
18878 const char *include_name_to_compare = include_name;
18879 const char *pst_filename;
18880 char *copied_name = NULL;
18883 const char *dir_name = fe.include_dir (lh);
18885 if (!IS_ABSOLUTE_PATH (include_name)
18886 && (dir_name != NULL || comp_dir != NULL))
18888 /* Avoid creating a duplicate psymtab for PST.
18889 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18890 Before we do the comparison, however, we need to account
18891 for DIR_NAME and COMP_DIR.
18892 First prepend dir_name (if non-NULL). If we still don't
18893 have an absolute path prepend comp_dir (if non-NULL).
18894 However, the directory we record in the include-file's
18895 psymtab does not contain COMP_DIR (to match the
18896 corresponding symtab(s)).
18901 bash$ gcc -g ./hello.c
18902 include_name = "hello.c"
18904 DW_AT_comp_dir = comp_dir = "/tmp"
18905 DW_AT_name = "./hello.c"
18909 if (dir_name != NULL)
18911 char *tem = concat (dir_name, SLASH_STRING,
18912 include_name, (char *)NULL);
18914 make_cleanup (xfree, tem);
18915 include_name = tem;
18916 include_name_to_compare = include_name;
18918 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18920 char *tem = concat (comp_dir, SLASH_STRING,
18921 include_name, (char *)NULL);
18923 make_cleanup (xfree, tem);
18924 include_name_to_compare = tem;
18928 pst_filename = pst->filename;
18929 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18931 copied_name = concat (pst->dirname, SLASH_STRING,
18932 pst_filename, (char *)NULL);
18933 pst_filename = copied_name;
18936 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18938 if (copied_name != NULL)
18939 xfree (copied_name);
18943 return include_name;
18946 /* State machine to track the state of the line number program. */
18948 class lnp_state_machine
18951 /* Initialize a machine state for the start of a line number
18953 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18955 file_entry *current_file ()
18957 /* lh->file_names is 0-based, but the file name numbers in the
18958 statement program are 1-based. */
18959 return m_line_header->file_name_at (m_file);
18962 /* Record the line in the state machine. END_SEQUENCE is true if
18963 we're processing the end of a sequence. */
18964 void record_line (bool end_sequence);
18966 /* Check address and if invalid nop-out the rest of the lines in this
18968 void check_line_address (struct dwarf2_cu *cu,
18969 const gdb_byte *line_ptr,
18970 CORE_ADDR lowpc, CORE_ADDR address);
18972 void handle_set_discriminator (unsigned int discriminator)
18974 m_discriminator = discriminator;
18975 m_line_has_non_zero_discriminator |= discriminator != 0;
18978 /* Handle DW_LNE_set_address. */
18979 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18982 address += baseaddr;
18983 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18986 /* Handle DW_LNS_advance_pc. */
18987 void handle_advance_pc (CORE_ADDR adjust);
18989 /* Handle a special opcode. */
18990 void handle_special_opcode (unsigned char op_code);
18992 /* Handle DW_LNS_advance_line. */
18993 void handle_advance_line (int line_delta)
18995 advance_line (line_delta);
18998 /* Handle DW_LNS_set_file. */
18999 void handle_set_file (file_name_index file);
19001 /* Handle DW_LNS_negate_stmt. */
19002 void handle_negate_stmt ()
19004 m_is_stmt = !m_is_stmt;
19007 /* Handle DW_LNS_const_add_pc. */
19008 void handle_const_add_pc ();
19010 /* Handle DW_LNS_fixed_advance_pc. */
19011 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19013 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19017 /* Handle DW_LNS_copy. */
19018 void handle_copy ()
19020 record_line (false);
19021 m_discriminator = 0;
19024 /* Handle DW_LNE_end_sequence. */
19025 void handle_end_sequence ()
19027 m_record_line_callback = ::record_line;
19031 /* Advance the line by LINE_DELTA. */
19032 void advance_line (int line_delta)
19034 m_line += line_delta;
19036 if (line_delta != 0)
19037 m_line_has_non_zero_discriminator = m_discriminator != 0;
19040 gdbarch *m_gdbarch;
19042 /* True if we're recording lines.
19043 Otherwise we're building partial symtabs and are just interested in
19044 finding include files mentioned by the line number program. */
19045 bool m_record_lines_p;
19047 /* The line number header. */
19048 line_header *m_line_header;
19050 /* These are part of the standard DWARF line number state machine,
19051 and initialized according to the DWARF spec. */
19053 unsigned char m_op_index = 0;
19054 /* The line table index (1-based) of the current file. */
19055 file_name_index m_file = (file_name_index) 1;
19056 unsigned int m_line = 1;
19058 /* These are initialized in the constructor. */
19060 CORE_ADDR m_address;
19062 unsigned int m_discriminator;
19064 /* Additional bits of state we need to track. */
19066 /* The last file that we called dwarf2_start_subfile for.
19067 This is only used for TLLs. */
19068 unsigned int m_last_file = 0;
19069 /* The last file a line number was recorded for. */
19070 struct subfile *m_last_subfile = NULL;
19072 /* The function to call to record a line. */
19073 record_line_ftype *m_record_line_callback = NULL;
19075 /* The last line number that was recorded, used to coalesce
19076 consecutive entries for the same line. This can happen, for
19077 example, when discriminators are present. PR 17276. */
19078 unsigned int m_last_line = 0;
19079 bool m_line_has_non_zero_discriminator = false;
19083 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19085 CORE_ADDR addr_adj = (((m_op_index + adjust)
19086 / m_line_header->maximum_ops_per_instruction)
19087 * m_line_header->minimum_instruction_length);
19088 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19089 m_op_index = ((m_op_index + adjust)
19090 % m_line_header->maximum_ops_per_instruction);
19094 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19096 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19097 CORE_ADDR addr_adj = (((m_op_index
19098 + (adj_opcode / m_line_header->line_range))
19099 / m_line_header->maximum_ops_per_instruction)
19100 * m_line_header->minimum_instruction_length);
19101 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19102 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19103 % m_line_header->maximum_ops_per_instruction);
19105 int line_delta = (m_line_header->line_base
19106 + (adj_opcode % m_line_header->line_range));
19107 advance_line (line_delta);
19108 record_line (false);
19109 m_discriminator = 0;
19113 lnp_state_machine::handle_set_file (file_name_index file)
19117 const file_entry *fe = current_file ();
19119 dwarf2_debug_line_missing_file_complaint ();
19120 else if (m_record_lines_p)
19122 const char *dir = fe->include_dir (m_line_header);
19124 m_last_subfile = current_subfile;
19125 m_line_has_non_zero_discriminator = m_discriminator != 0;
19126 dwarf2_start_subfile (fe->name, dir);
19131 lnp_state_machine::handle_const_add_pc ()
19134 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19137 = (((m_op_index + adjust)
19138 / m_line_header->maximum_ops_per_instruction)
19139 * m_line_header->minimum_instruction_length);
19141 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19142 m_op_index = ((m_op_index + adjust)
19143 % m_line_header->maximum_ops_per_instruction);
19146 /* Ignore this record_line request. */
19149 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19154 /* Return non-zero if we should add LINE to the line number table.
19155 LINE is the line to add, LAST_LINE is the last line that was added,
19156 LAST_SUBFILE is the subfile for LAST_LINE.
19157 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19158 had a non-zero discriminator.
19160 We have to be careful in the presence of discriminators.
19161 E.g., for this line:
19163 for (i = 0; i < 100000; i++);
19165 clang can emit four line number entries for that one line,
19166 each with a different discriminator.
19167 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19169 However, we want gdb to coalesce all four entries into one.
19170 Otherwise the user could stepi into the middle of the line and
19171 gdb would get confused about whether the pc really was in the
19172 middle of the line.
19174 Things are further complicated by the fact that two consecutive
19175 line number entries for the same line is a heuristic used by gcc
19176 to denote the end of the prologue. So we can't just discard duplicate
19177 entries, we have to be selective about it. The heuristic we use is
19178 that we only collapse consecutive entries for the same line if at least
19179 one of those entries has a non-zero discriminator. PR 17276.
19181 Note: Addresses in the line number state machine can never go backwards
19182 within one sequence, thus this coalescing is ok. */
19185 dwarf_record_line_p (unsigned int line, unsigned int last_line,
19186 int line_has_non_zero_discriminator,
19187 struct subfile *last_subfile)
19189 if (current_subfile != last_subfile)
19191 if (line != last_line)
19193 /* Same line for the same file that we've seen already.
19194 As a last check, for pr 17276, only record the line if the line
19195 has never had a non-zero discriminator. */
19196 if (!line_has_non_zero_discriminator)
19201 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19202 in the line table of subfile SUBFILE. */
19205 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19206 unsigned int line, CORE_ADDR address,
19207 record_line_ftype p_record_line)
19209 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19211 if (dwarf_line_debug)
19213 fprintf_unfiltered (gdb_stdlog,
19214 "Recording line %u, file %s, address %s\n",
19215 line, lbasename (subfile->name),
19216 paddress (gdbarch, address));
19219 (*p_record_line) (subfile, line, addr);
19222 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19223 Mark the end of a set of line number records.
19224 The arguments are the same as for dwarf_record_line_1.
19225 If SUBFILE is NULL the request is ignored. */
19228 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19229 CORE_ADDR address, record_line_ftype p_record_line)
19231 if (subfile == NULL)
19234 if (dwarf_line_debug)
19236 fprintf_unfiltered (gdb_stdlog,
19237 "Finishing current line, file %s, address %s\n",
19238 lbasename (subfile->name),
19239 paddress (gdbarch, address));
19242 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19246 lnp_state_machine::record_line (bool end_sequence)
19248 if (dwarf_line_debug)
19250 fprintf_unfiltered (gdb_stdlog,
19251 "Processing actual line %u: file %u,"
19252 " address %s, is_stmt %u, discrim %u\n",
19253 m_line, to_underlying (m_file),
19254 paddress (m_gdbarch, m_address),
19255 m_is_stmt, m_discriminator);
19258 file_entry *fe = current_file ();
19261 dwarf2_debug_line_missing_file_complaint ();
19262 /* For now we ignore lines not starting on an instruction boundary.
19263 But not when processing end_sequence for compatibility with the
19264 previous version of the code. */
19265 else if (m_op_index == 0 || end_sequence)
19267 fe->included_p = 1;
19268 if (m_record_lines_p && m_is_stmt)
19270 if (m_last_subfile != current_subfile || end_sequence)
19272 dwarf_finish_line (m_gdbarch, m_last_subfile,
19273 m_address, m_record_line_callback);
19278 if (dwarf_record_line_p (m_line, m_last_line,
19279 m_line_has_non_zero_discriminator,
19282 dwarf_record_line_1 (m_gdbarch, current_subfile,
19284 m_record_line_callback);
19286 m_last_subfile = current_subfile;
19287 m_last_line = m_line;
19293 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19294 bool record_lines_p)
19297 m_record_lines_p = record_lines_p;
19298 m_line_header = lh;
19300 m_record_line_callback = ::record_line;
19302 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19303 was a line entry for it so that the backend has a chance to adjust it
19304 and also record it in case it needs it. This is currently used by MIPS
19305 code, cf. `mips_adjust_dwarf2_line'. */
19306 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19307 m_is_stmt = lh->default_is_stmt;
19308 m_discriminator = 0;
19312 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19313 const gdb_byte *line_ptr,
19314 CORE_ADDR lowpc, CORE_ADDR address)
19316 /* If address < lowpc then it's not a usable value, it's outside the
19317 pc range of the CU. However, we restrict the test to only address
19318 values of zero to preserve GDB's previous behaviour which is to
19319 handle the specific case of a function being GC'd by the linker. */
19321 if (address == 0 && address < lowpc)
19323 /* This line table is for a function which has been
19324 GCd by the linker. Ignore it. PR gdb/12528 */
19326 struct objfile *objfile = cu->objfile;
19327 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19329 complaint (&symfile_complaints,
19330 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19331 line_offset, objfile_name (objfile));
19332 m_record_line_callback = noop_record_line;
19333 /* Note: record_line_callback is left as noop_record_line until
19334 we see DW_LNE_end_sequence. */
19338 /* Subroutine of dwarf_decode_lines to simplify it.
19339 Process the line number information in LH.
19340 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19341 program in order to set included_p for every referenced header. */
19344 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19345 const int decode_for_pst_p, CORE_ADDR lowpc)
19347 const gdb_byte *line_ptr, *extended_end;
19348 const gdb_byte *line_end;
19349 unsigned int bytes_read, extended_len;
19350 unsigned char op_code, extended_op;
19351 CORE_ADDR baseaddr;
19352 struct objfile *objfile = cu->objfile;
19353 bfd *abfd = objfile->obfd;
19354 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19355 /* True if we're recording line info (as opposed to building partial
19356 symtabs and just interested in finding include files mentioned by
19357 the line number program). */
19358 bool record_lines_p = !decode_for_pst_p;
19360 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19362 line_ptr = lh->statement_program_start;
19363 line_end = lh->statement_program_end;
19365 /* Read the statement sequences until there's nothing left. */
19366 while (line_ptr < line_end)
19368 /* The DWARF line number program state machine. Reset the state
19369 machine at the start of each sequence. */
19370 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19371 bool end_sequence = false;
19373 if (record_lines_p)
19375 /* Start a subfile for the current file of the state
19377 const file_entry *fe = state_machine.current_file ();
19380 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
19383 /* Decode the table. */
19384 while (line_ptr < line_end && !end_sequence)
19386 op_code = read_1_byte (abfd, line_ptr);
19389 if (op_code >= lh->opcode_base)
19391 /* Special opcode. */
19392 state_machine.handle_special_opcode (op_code);
19394 else switch (op_code)
19396 case DW_LNS_extended_op:
19397 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19399 line_ptr += bytes_read;
19400 extended_end = line_ptr + extended_len;
19401 extended_op = read_1_byte (abfd, line_ptr);
19403 switch (extended_op)
19405 case DW_LNE_end_sequence:
19406 state_machine.handle_end_sequence ();
19407 end_sequence = true;
19409 case DW_LNE_set_address:
19412 = read_address (abfd, line_ptr, cu, &bytes_read);
19413 line_ptr += bytes_read;
19415 state_machine.check_line_address (cu, line_ptr,
19417 state_machine.handle_set_address (baseaddr, address);
19420 case DW_LNE_define_file:
19422 const char *cur_file;
19423 unsigned int mod_time, length;
19426 cur_file = read_direct_string (abfd, line_ptr,
19428 line_ptr += bytes_read;
19429 dindex = (dir_index)
19430 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19431 line_ptr += bytes_read;
19433 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19434 line_ptr += bytes_read;
19436 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19437 line_ptr += bytes_read;
19438 lh->add_file_name (cur_file, dindex, mod_time, length);
19441 case DW_LNE_set_discriminator:
19443 /* The discriminator is not interesting to the
19444 debugger; just ignore it. We still need to
19445 check its value though:
19446 if there are consecutive entries for the same
19447 (non-prologue) line we want to coalesce them.
19450 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19451 line_ptr += bytes_read;
19453 state_machine.handle_set_discriminator (discr);
19457 complaint (&symfile_complaints,
19458 _("mangled .debug_line section"));
19461 /* Make sure that we parsed the extended op correctly. If e.g.
19462 we expected a different address size than the producer used,
19463 we may have read the wrong number of bytes. */
19464 if (line_ptr != extended_end)
19466 complaint (&symfile_complaints,
19467 _("mangled .debug_line section"));
19472 state_machine.handle_copy ();
19474 case DW_LNS_advance_pc:
19477 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19478 line_ptr += bytes_read;
19480 state_machine.handle_advance_pc (adjust);
19483 case DW_LNS_advance_line:
19486 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19487 line_ptr += bytes_read;
19489 state_machine.handle_advance_line (line_delta);
19492 case DW_LNS_set_file:
19494 file_name_index file
19495 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19497 line_ptr += bytes_read;
19499 state_machine.handle_set_file (file);
19502 case DW_LNS_set_column:
19503 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19504 line_ptr += bytes_read;
19506 case DW_LNS_negate_stmt:
19507 state_machine.handle_negate_stmt ();
19509 case DW_LNS_set_basic_block:
19511 /* Add to the address register of the state machine the
19512 address increment value corresponding to special opcode
19513 255. I.e., this value is scaled by the minimum
19514 instruction length since special opcode 255 would have
19515 scaled the increment. */
19516 case DW_LNS_const_add_pc:
19517 state_machine.handle_const_add_pc ();
19519 case DW_LNS_fixed_advance_pc:
19521 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19524 state_machine.handle_fixed_advance_pc (addr_adj);
19529 /* Unknown standard opcode, ignore it. */
19532 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19534 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19535 line_ptr += bytes_read;
19542 dwarf2_debug_line_missing_end_sequence_complaint ();
19544 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19545 in which case we still finish recording the last line). */
19546 state_machine.record_line (true);
19550 /* Decode the Line Number Program (LNP) for the given line_header
19551 structure and CU. The actual information extracted and the type
19552 of structures created from the LNP depends on the value of PST.
19554 1. If PST is NULL, then this procedure uses the data from the program
19555 to create all necessary symbol tables, and their linetables.
19557 2. If PST is not NULL, this procedure reads the program to determine
19558 the list of files included by the unit represented by PST, and
19559 builds all the associated partial symbol tables.
19561 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19562 It is used for relative paths in the line table.
19563 NOTE: When processing partial symtabs (pst != NULL),
19564 comp_dir == pst->dirname.
19566 NOTE: It is important that psymtabs have the same file name (via strcmp)
19567 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19568 symtab we don't use it in the name of the psymtabs we create.
19569 E.g. expand_line_sal requires this when finding psymtabs to expand.
19570 A good testcase for this is mb-inline.exp.
19572 LOWPC is the lowest address in CU (or 0 if not known).
19574 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19575 for its PC<->lines mapping information. Otherwise only the filename
19576 table is read in. */
19579 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19580 struct dwarf2_cu *cu, struct partial_symtab *pst,
19581 CORE_ADDR lowpc, int decode_mapping)
19583 struct objfile *objfile = cu->objfile;
19584 const int decode_for_pst_p = (pst != NULL);
19586 if (decode_mapping)
19587 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19589 if (decode_for_pst_p)
19593 /* Now that we're done scanning the Line Header Program, we can
19594 create the psymtab of each included file. */
19595 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
19596 if (lh->file_names[file_index].included_p == 1)
19598 const char *include_name =
19599 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19600 if (include_name != NULL)
19601 dwarf2_create_include_psymtab (include_name, pst, objfile);
19606 /* Make sure a symtab is created for every file, even files
19607 which contain only variables (i.e. no code with associated
19609 struct compunit_symtab *cust = buildsym_compunit_symtab ();
19612 for (i = 0; i < lh->file_names.size (); i++)
19614 file_entry &fe = lh->file_names[i];
19616 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
19618 if (current_subfile->symtab == NULL)
19620 current_subfile->symtab
19621 = allocate_symtab (cust, current_subfile->name);
19623 fe.symtab = current_subfile->symtab;
19628 /* Start a subfile for DWARF. FILENAME is the name of the file and
19629 DIRNAME the name of the source directory which contains FILENAME
19630 or NULL if not known.
19631 This routine tries to keep line numbers from identical absolute and
19632 relative file names in a common subfile.
19634 Using the `list' example from the GDB testsuite, which resides in
19635 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19636 of /srcdir/list0.c yields the following debugging information for list0.c:
19638 DW_AT_name: /srcdir/list0.c
19639 DW_AT_comp_dir: /compdir
19640 files.files[0].name: list0.h
19641 files.files[0].dir: /srcdir
19642 files.files[1].name: list0.c
19643 files.files[1].dir: /srcdir
19645 The line number information for list0.c has to end up in a single
19646 subfile, so that `break /srcdir/list0.c:1' works as expected.
19647 start_subfile will ensure that this happens provided that we pass the
19648 concatenation of files.files[1].dir and files.files[1].name as the
19652 dwarf2_start_subfile (const char *filename, const char *dirname)
19656 /* In order not to lose the line information directory,
19657 we concatenate it to the filename when it makes sense.
19658 Note that the Dwarf3 standard says (speaking of filenames in line
19659 information): ``The directory index is ignored for file names
19660 that represent full path names''. Thus ignoring dirname in the
19661 `else' branch below isn't an issue. */
19663 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19665 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19669 start_subfile (filename);
19675 /* Start a symtab for DWARF.
19676 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19678 static struct compunit_symtab *
19679 dwarf2_start_symtab (struct dwarf2_cu *cu,
19680 const char *name, const char *comp_dir, CORE_ADDR low_pc)
19682 struct compunit_symtab *cust
19683 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
19685 record_debugformat ("DWARF 2");
19686 record_producer (cu->producer);
19688 /* We assume that we're processing GCC output. */
19689 processing_gcc_compilation = 2;
19691 cu->processing_has_namespace_info = 0;
19697 var_decode_location (struct attribute *attr, struct symbol *sym,
19698 struct dwarf2_cu *cu)
19700 struct objfile *objfile = cu->objfile;
19701 struct comp_unit_head *cu_header = &cu->header;
19703 /* NOTE drow/2003-01-30: There used to be a comment and some special
19704 code here to turn a symbol with DW_AT_external and a
19705 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19706 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19707 with some versions of binutils) where shared libraries could have
19708 relocations against symbols in their debug information - the
19709 minimal symbol would have the right address, but the debug info
19710 would not. It's no longer necessary, because we will explicitly
19711 apply relocations when we read in the debug information now. */
19713 /* A DW_AT_location attribute with no contents indicates that a
19714 variable has been optimized away. */
19715 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19717 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19721 /* Handle one degenerate form of location expression specially, to
19722 preserve GDB's previous behavior when section offsets are
19723 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19724 then mark this symbol as LOC_STATIC. */
19726 if (attr_form_is_block (attr)
19727 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19728 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19729 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19730 && (DW_BLOCK (attr)->size
19731 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19733 unsigned int dummy;
19735 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19736 SYMBOL_VALUE_ADDRESS (sym) =
19737 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19739 SYMBOL_VALUE_ADDRESS (sym) =
19740 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
19741 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19742 fixup_symbol_section (sym, objfile);
19743 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19744 SYMBOL_SECTION (sym));
19748 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19749 expression evaluator, and use LOC_COMPUTED only when necessary
19750 (i.e. when the value of a register or memory location is
19751 referenced, or a thread-local block, etc.). Then again, it might
19752 not be worthwhile. I'm assuming that it isn't unless performance
19753 or memory numbers show me otherwise. */
19755 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19757 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19758 cu->has_loclist = 1;
19761 /* Given a pointer to a DWARF information entry, figure out if we need
19762 to make a symbol table entry for it, and if so, create a new entry
19763 and return a pointer to it.
19764 If TYPE is NULL, determine symbol type from the die, otherwise
19765 used the passed type.
19766 If SPACE is not NULL, use it to hold the new symbol. If it is
19767 NULL, allocate a new symbol on the objfile's obstack. */
19769 static struct symbol *
19770 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19771 struct symbol *space)
19773 struct objfile *objfile = cu->objfile;
19774 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19775 struct symbol *sym = NULL;
19777 struct attribute *attr = NULL;
19778 struct attribute *attr2 = NULL;
19779 CORE_ADDR baseaddr;
19780 struct pending **list_to_add = NULL;
19782 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
19784 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19786 name = dwarf2_name (die, cu);
19789 const char *linkagename;
19790 int suppress_add = 0;
19795 sym = allocate_symbol (objfile);
19796 OBJSTAT (objfile, n_syms++);
19798 /* Cache this symbol's name and the name's demangled form (if any). */
19799 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
19800 linkagename = dwarf2_physname (name, die, cu);
19801 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
19803 /* Fortran does not have mangling standard and the mangling does differ
19804 between gfortran, iFort etc. */
19805 if (cu->language == language_fortran
19806 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
19807 symbol_set_demangled_name (&(sym->ginfo),
19808 dwarf2_full_name (name, die, cu),
19811 /* Default assumptions.
19812 Use the passed type or decode it from the die. */
19813 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19814 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19816 SYMBOL_TYPE (sym) = type;
19818 SYMBOL_TYPE (sym) = die_type (die, cu);
19819 attr = dwarf2_attr (die,
19820 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19824 SYMBOL_LINE (sym) = DW_UNSND (attr);
19827 attr = dwarf2_attr (die,
19828 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19832 file_name_index file_index = (file_name_index) DW_UNSND (attr);
19833 struct file_entry *fe;
19835 if (cu->line_header != NULL)
19836 fe = cu->line_header->file_name_at (file_index);
19841 complaint (&symfile_complaints,
19842 _("file index out of range"));
19844 symbol_set_symtab (sym, fe->symtab);
19850 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
19855 addr = attr_value_as_address (attr);
19856 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19857 SYMBOL_VALUE_ADDRESS (sym) = addr;
19859 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19860 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
19861 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
19862 add_symbol_to_list (sym, cu->list_in_scope);
19864 case DW_TAG_subprogram:
19865 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19867 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19868 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19869 if ((attr2 && (DW_UNSND (attr2) != 0))
19870 || cu->language == language_ada)
19872 /* Subprograms marked external are stored as a global symbol.
19873 Ada subprograms, whether marked external or not, are always
19874 stored as a global symbol, because we want to be able to
19875 access them globally. For instance, we want to be able
19876 to break on a nested subprogram without having to
19877 specify the context. */
19878 list_to_add = &global_symbols;
19882 list_to_add = cu->list_in_scope;
19885 case DW_TAG_inlined_subroutine:
19886 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19888 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19889 SYMBOL_INLINED (sym) = 1;
19890 list_to_add = cu->list_in_scope;
19892 case DW_TAG_template_value_param:
19894 /* Fall through. */
19895 case DW_TAG_constant:
19896 case DW_TAG_variable:
19897 case DW_TAG_member:
19898 /* Compilation with minimal debug info may result in
19899 variables with missing type entries. Change the
19900 misleading `void' type to something sensible. */
19901 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19902 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
19904 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19905 /* In the case of DW_TAG_member, we should only be called for
19906 static const members. */
19907 if (die->tag == DW_TAG_member)
19909 /* dwarf2_add_field uses die_is_declaration,
19910 so we do the same. */
19911 gdb_assert (die_is_declaration (die, cu));
19916 dwarf2_const_value (attr, sym, cu);
19917 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19920 if (attr2 && (DW_UNSND (attr2) != 0))
19921 list_to_add = &global_symbols;
19923 list_to_add = cu->list_in_scope;
19927 attr = dwarf2_attr (die, DW_AT_location, cu);
19930 var_decode_location (attr, sym, cu);
19931 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19933 /* Fortran explicitly imports any global symbols to the local
19934 scope by DW_TAG_common_block. */
19935 if (cu->language == language_fortran && die->parent
19936 && die->parent->tag == DW_TAG_common_block)
19939 if (SYMBOL_CLASS (sym) == LOC_STATIC
19940 && SYMBOL_VALUE_ADDRESS (sym) == 0
19941 && !dwarf2_per_objfile->has_section_at_zero)
19943 /* When a static variable is eliminated by the linker,
19944 the corresponding debug information is not stripped
19945 out, but the variable address is set to null;
19946 do not add such variables into symbol table. */
19948 else if (attr2 && (DW_UNSND (attr2) != 0))
19950 /* Workaround gfortran PR debug/40040 - it uses
19951 DW_AT_location for variables in -fPIC libraries which may
19952 get overriden by other libraries/executable and get
19953 a different address. Resolve it by the minimal symbol
19954 which may come from inferior's executable using copy
19955 relocation. Make this workaround only for gfortran as for
19956 other compilers GDB cannot guess the minimal symbol
19957 Fortran mangling kind. */
19958 if (cu->language == language_fortran && die->parent
19959 && die->parent->tag == DW_TAG_module
19961 && startswith (cu->producer, "GNU Fortran"))
19962 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19964 /* A variable with DW_AT_external is never static,
19965 but it may be block-scoped. */
19966 list_to_add = (cu->list_in_scope == &file_symbols
19967 ? &global_symbols : cu->list_in_scope);
19970 list_to_add = cu->list_in_scope;
19974 /* We do not know the address of this symbol.
19975 If it is an external symbol and we have type information
19976 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19977 The address of the variable will then be determined from
19978 the minimal symbol table whenever the variable is
19980 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19982 /* Fortran explicitly imports any global symbols to the local
19983 scope by DW_TAG_common_block. */
19984 if (cu->language == language_fortran && die->parent
19985 && die->parent->tag == DW_TAG_common_block)
19987 /* SYMBOL_CLASS doesn't matter here because
19988 read_common_block is going to reset it. */
19990 list_to_add = cu->list_in_scope;
19992 else if (attr2 && (DW_UNSND (attr2) != 0)
19993 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19995 /* A variable with DW_AT_external is never static, but it
19996 may be block-scoped. */
19997 list_to_add = (cu->list_in_scope == &file_symbols
19998 ? &global_symbols : cu->list_in_scope);
20000 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20002 else if (!die_is_declaration (die, cu))
20004 /* Use the default LOC_OPTIMIZED_OUT class. */
20005 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20007 list_to_add = cu->list_in_scope;
20011 case DW_TAG_formal_parameter:
20012 /* If we are inside a function, mark this as an argument. If
20013 not, we might be looking at an argument to an inlined function
20014 when we do not have enough information to show inlined frames;
20015 pretend it's a local variable in that case so that the user can
20017 if (context_stack_depth > 0
20018 && context_stack[context_stack_depth - 1].name != NULL)
20019 SYMBOL_IS_ARGUMENT (sym) = 1;
20020 attr = dwarf2_attr (die, DW_AT_location, cu);
20023 var_decode_location (attr, sym, cu);
20025 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20028 dwarf2_const_value (attr, sym, cu);
20031 list_to_add = cu->list_in_scope;
20033 case DW_TAG_unspecified_parameters:
20034 /* From varargs functions; gdb doesn't seem to have any
20035 interest in this information, so just ignore it for now.
20038 case DW_TAG_template_type_param:
20040 /* Fall through. */
20041 case DW_TAG_class_type:
20042 case DW_TAG_interface_type:
20043 case DW_TAG_structure_type:
20044 case DW_TAG_union_type:
20045 case DW_TAG_set_type:
20046 case DW_TAG_enumeration_type:
20047 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20048 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20051 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20052 really ever be static objects: otherwise, if you try
20053 to, say, break of a class's method and you're in a file
20054 which doesn't mention that class, it won't work unless
20055 the check for all static symbols in lookup_symbol_aux
20056 saves you. See the OtherFileClass tests in
20057 gdb.c++/namespace.exp. */
20061 list_to_add = (cu->list_in_scope == &file_symbols
20062 && cu->language == language_cplus
20063 ? &global_symbols : cu->list_in_scope);
20065 /* The semantics of C++ state that "struct foo {
20066 ... }" also defines a typedef for "foo". */
20067 if (cu->language == language_cplus
20068 || cu->language == language_ada
20069 || cu->language == language_d
20070 || cu->language == language_rust)
20072 /* The symbol's name is already allocated along
20073 with this objfile, so we don't need to
20074 duplicate it for the type. */
20075 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20076 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20081 case DW_TAG_typedef:
20082 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20083 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20084 list_to_add = cu->list_in_scope;
20086 case DW_TAG_base_type:
20087 case DW_TAG_subrange_type:
20088 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20089 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20090 list_to_add = cu->list_in_scope;
20092 case DW_TAG_enumerator:
20093 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20096 dwarf2_const_value (attr, sym, cu);
20099 /* NOTE: carlton/2003-11-10: See comment above in the
20100 DW_TAG_class_type, etc. block. */
20102 list_to_add = (cu->list_in_scope == &file_symbols
20103 && cu->language == language_cplus
20104 ? &global_symbols : cu->list_in_scope);
20107 case DW_TAG_imported_declaration:
20108 case DW_TAG_namespace:
20109 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20110 list_to_add = &global_symbols;
20112 case DW_TAG_module:
20113 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20114 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20115 list_to_add = &global_symbols;
20117 case DW_TAG_common_block:
20118 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20119 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20120 add_symbol_to_list (sym, cu->list_in_scope);
20123 /* Not a tag we recognize. Hopefully we aren't processing
20124 trash data, but since we must specifically ignore things
20125 we don't recognize, there is nothing else we should do at
20127 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
20128 dwarf_tag_name (die->tag));
20134 sym->hash_next = objfile->template_symbols;
20135 objfile->template_symbols = sym;
20136 list_to_add = NULL;
20139 if (list_to_add != NULL)
20140 add_symbol_to_list (sym, list_to_add);
20142 /* For the benefit of old versions of GCC, check for anonymous
20143 namespaces based on the demangled name. */
20144 if (!cu->processing_has_namespace_info
20145 && cu->language == language_cplus)
20146 cp_scan_for_anonymous_namespaces (sym, objfile);
20151 /* A wrapper for new_symbol_full that always allocates a new symbol. */
20153 static struct symbol *
20154 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20156 return new_symbol_full (die, type, cu, NULL);
20159 /* Given an attr with a DW_FORM_dataN value in host byte order,
20160 zero-extend it as appropriate for the symbol's type. The DWARF
20161 standard (v4) is not entirely clear about the meaning of using
20162 DW_FORM_dataN for a constant with a signed type, where the type is
20163 wider than the data. The conclusion of a discussion on the DWARF
20164 list was that this is unspecified. We choose to always zero-extend
20165 because that is the interpretation long in use by GCC. */
20168 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20169 struct dwarf2_cu *cu, LONGEST *value, int bits)
20171 struct objfile *objfile = cu->objfile;
20172 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20173 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20174 LONGEST l = DW_UNSND (attr);
20176 if (bits < sizeof (*value) * 8)
20178 l &= ((LONGEST) 1 << bits) - 1;
20181 else if (bits == sizeof (*value) * 8)
20185 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20186 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20193 /* Read a constant value from an attribute. Either set *VALUE, or if
20194 the value does not fit in *VALUE, set *BYTES - either already
20195 allocated on the objfile obstack, or newly allocated on OBSTACK,
20196 or, set *BATON, if we translated the constant to a location
20200 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20201 const char *name, struct obstack *obstack,
20202 struct dwarf2_cu *cu,
20203 LONGEST *value, const gdb_byte **bytes,
20204 struct dwarf2_locexpr_baton **baton)
20206 struct objfile *objfile = cu->objfile;
20207 struct comp_unit_head *cu_header = &cu->header;
20208 struct dwarf_block *blk;
20209 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20210 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20216 switch (attr->form)
20219 case DW_FORM_GNU_addr_index:
20223 if (TYPE_LENGTH (type) != cu_header->addr_size)
20224 dwarf2_const_value_length_mismatch_complaint (name,
20225 cu_header->addr_size,
20226 TYPE_LENGTH (type));
20227 /* Symbols of this form are reasonably rare, so we just
20228 piggyback on the existing location code rather than writing
20229 a new implementation of symbol_computed_ops. */
20230 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20231 (*baton)->per_cu = cu->per_cu;
20232 gdb_assert ((*baton)->per_cu);
20234 (*baton)->size = 2 + cu_header->addr_size;
20235 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20236 (*baton)->data = data;
20238 data[0] = DW_OP_addr;
20239 store_unsigned_integer (&data[1], cu_header->addr_size,
20240 byte_order, DW_ADDR (attr));
20241 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20244 case DW_FORM_string:
20246 case DW_FORM_GNU_str_index:
20247 case DW_FORM_GNU_strp_alt:
20248 /* DW_STRING is already allocated on the objfile obstack, point
20250 *bytes = (const gdb_byte *) DW_STRING (attr);
20252 case DW_FORM_block1:
20253 case DW_FORM_block2:
20254 case DW_FORM_block4:
20255 case DW_FORM_block:
20256 case DW_FORM_exprloc:
20257 case DW_FORM_data16:
20258 blk = DW_BLOCK (attr);
20259 if (TYPE_LENGTH (type) != blk->size)
20260 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20261 TYPE_LENGTH (type));
20262 *bytes = blk->data;
20265 /* The DW_AT_const_value attributes are supposed to carry the
20266 symbol's value "represented as it would be on the target
20267 architecture." By the time we get here, it's already been
20268 converted to host endianness, so we just need to sign- or
20269 zero-extend it as appropriate. */
20270 case DW_FORM_data1:
20271 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20273 case DW_FORM_data2:
20274 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20276 case DW_FORM_data4:
20277 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20279 case DW_FORM_data8:
20280 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20283 case DW_FORM_sdata:
20284 case DW_FORM_implicit_const:
20285 *value = DW_SND (attr);
20288 case DW_FORM_udata:
20289 *value = DW_UNSND (attr);
20293 complaint (&symfile_complaints,
20294 _("unsupported const value attribute form: '%s'"),
20295 dwarf_form_name (attr->form));
20302 /* Copy constant value from an attribute to a symbol. */
20305 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20306 struct dwarf2_cu *cu)
20308 struct objfile *objfile = cu->objfile;
20310 const gdb_byte *bytes;
20311 struct dwarf2_locexpr_baton *baton;
20313 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20314 SYMBOL_PRINT_NAME (sym),
20315 &objfile->objfile_obstack, cu,
20316 &value, &bytes, &baton);
20320 SYMBOL_LOCATION_BATON (sym) = baton;
20321 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20323 else if (bytes != NULL)
20325 SYMBOL_VALUE_BYTES (sym) = bytes;
20326 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20330 SYMBOL_VALUE (sym) = value;
20331 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20335 /* Return the type of the die in question using its DW_AT_type attribute. */
20337 static struct type *
20338 die_type (struct die_info *die, struct dwarf2_cu *cu)
20340 struct attribute *type_attr;
20342 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20345 /* A missing DW_AT_type represents a void type. */
20346 return objfile_type (cu->objfile)->builtin_void;
20349 return lookup_die_type (die, type_attr, cu);
20352 /* True iff CU's producer generates GNAT Ada auxiliary information
20353 that allows to find parallel types through that information instead
20354 of having to do expensive parallel lookups by type name. */
20357 need_gnat_info (struct dwarf2_cu *cu)
20359 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20360 of GNAT produces this auxiliary information, without any indication
20361 that it is produced. Part of enhancing the FSF version of GNAT
20362 to produce that information will be to put in place an indicator
20363 that we can use in order to determine whether the descriptive type
20364 info is available or not. One suggestion that has been made is
20365 to use a new attribute, attached to the CU die. For now, assume
20366 that the descriptive type info is not available. */
20370 /* Return the auxiliary type of the die in question using its
20371 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20372 attribute is not present. */
20374 static struct type *
20375 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20377 struct attribute *type_attr;
20379 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20383 return lookup_die_type (die, type_attr, cu);
20386 /* If DIE has a descriptive_type attribute, then set the TYPE's
20387 descriptive type accordingly. */
20390 set_descriptive_type (struct type *type, struct die_info *die,
20391 struct dwarf2_cu *cu)
20393 struct type *descriptive_type = die_descriptive_type (die, cu);
20395 if (descriptive_type)
20397 ALLOCATE_GNAT_AUX_TYPE (type);
20398 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20402 /* Return the containing type of the die in question using its
20403 DW_AT_containing_type attribute. */
20405 static struct type *
20406 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20408 struct attribute *type_attr;
20410 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20412 error (_("Dwarf Error: Problem turning containing type into gdb type "
20413 "[in module %s]"), objfile_name (cu->objfile));
20415 return lookup_die_type (die, type_attr, cu);
20418 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20420 static struct type *
20421 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20423 struct objfile *objfile = dwarf2_per_objfile->objfile;
20424 char *message, *saved;
20426 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
20427 objfile_name (objfile),
20428 to_underlying (cu->header.sect_off),
20429 to_underlying (die->sect_off));
20430 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20431 message, strlen (message));
20434 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20437 /* Look up the type of DIE in CU using its type attribute ATTR.
20438 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20439 DW_AT_containing_type.
20440 If there is no type substitute an error marker. */
20442 static struct type *
20443 lookup_die_type (struct die_info *die, const struct attribute *attr,
20444 struct dwarf2_cu *cu)
20446 struct objfile *objfile = cu->objfile;
20447 struct type *this_type;
20449 gdb_assert (attr->name == DW_AT_type
20450 || attr->name == DW_AT_GNAT_descriptive_type
20451 || attr->name == DW_AT_containing_type);
20453 /* First see if we have it cached. */
20455 if (attr->form == DW_FORM_GNU_ref_alt)
20457 struct dwarf2_per_cu_data *per_cu;
20458 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20460 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20461 this_type = get_die_type_at_offset (sect_off, per_cu);
20463 else if (attr_form_is_ref (attr))
20465 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20467 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20469 else if (attr->form == DW_FORM_ref_sig8)
20471 ULONGEST signature = DW_SIGNATURE (attr);
20473 return get_signatured_type (die, signature, cu);
20477 complaint (&symfile_complaints,
20478 _("Dwarf Error: Bad type attribute %s in DIE"
20479 " at 0x%x [in module %s]"),
20480 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
20481 objfile_name (objfile));
20482 return build_error_marker_type (cu, die);
20485 /* If not cached we need to read it in. */
20487 if (this_type == NULL)
20489 struct die_info *type_die = NULL;
20490 struct dwarf2_cu *type_cu = cu;
20492 if (attr_form_is_ref (attr))
20493 type_die = follow_die_ref (die, attr, &type_cu);
20494 if (type_die == NULL)
20495 return build_error_marker_type (cu, die);
20496 /* If we find the type now, it's probably because the type came
20497 from an inter-CU reference and the type's CU got expanded before
20499 this_type = read_type_die (type_die, type_cu);
20502 /* If we still don't have a type use an error marker. */
20504 if (this_type == NULL)
20505 return build_error_marker_type (cu, die);
20510 /* Return the type in DIE, CU.
20511 Returns NULL for invalid types.
20513 This first does a lookup in die_type_hash,
20514 and only reads the die in if necessary.
20516 NOTE: This can be called when reading in partial or full symbols. */
20518 static struct type *
20519 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20521 struct type *this_type;
20523 this_type = get_die_type (die, cu);
20527 return read_type_die_1 (die, cu);
20530 /* Read the type in DIE, CU.
20531 Returns NULL for invalid types. */
20533 static struct type *
20534 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20536 struct type *this_type = NULL;
20540 case DW_TAG_class_type:
20541 case DW_TAG_interface_type:
20542 case DW_TAG_structure_type:
20543 case DW_TAG_union_type:
20544 this_type = read_structure_type (die, cu);
20546 case DW_TAG_enumeration_type:
20547 this_type = read_enumeration_type (die, cu);
20549 case DW_TAG_subprogram:
20550 case DW_TAG_subroutine_type:
20551 case DW_TAG_inlined_subroutine:
20552 this_type = read_subroutine_type (die, cu);
20554 case DW_TAG_array_type:
20555 this_type = read_array_type (die, cu);
20557 case DW_TAG_set_type:
20558 this_type = read_set_type (die, cu);
20560 case DW_TAG_pointer_type:
20561 this_type = read_tag_pointer_type (die, cu);
20563 case DW_TAG_ptr_to_member_type:
20564 this_type = read_tag_ptr_to_member_type (die, cu);
20566 case DW_TAG_reference_type:
20567 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20569 case DW_TAG_rvalue_reference_type:
20570 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20572 case DW_TAG_const_type:
20573 this_type = read_tag_const_type (die, cu);
20575 case DW_TAG_volatile_type:
20576 this_type = read_tag_volatile_type (die, cu);
20578 case DW_TAG_restrict_type:
20579 this_type = read_tag_restrict_type (die, cu);
20581 case DW_TAG_string_type:
20582 this_type = read_tag_string_type (die, cu);
20584 case DW_TAG_typedef:
20585 this_type = read_typedef (die, cu);
20587 case DW_TAG_subrange_type:
20588 this_type = read_subrange_type (die, cu);
20590 case DW_TAG_base_type:
20591 this_type = read_base_type (die, cu);
20593 case DW_TAG_unspecified_type:
20594 this_type = read_unspecified_type (die, cu);
20596 case DW_TAG_namespace:
20597 this_type = read_namespace_type (die, cu);
20599 case DW_TAG_module:
20600 this_type = read_module_type (die, cu);
20602 case DW_TAG_atomic_type:
20603 this_type = read_tag_atomic_type (die, cu);
20606 complaint (&symfile_complaints,
20607 _("unexpected tag in read_type_die: '%s'"),
20608 dwarf_tag_name (die->tag));
20615 /* See if we can figure out if the class lives in a namespace. We do
20616 this by looking for a member function; its demangled name will
20617 contain namespace info, if there is any.
20618 Return the computed name or NULL.
20619 Space for the result is allocated on the objfile's obstack.
20620 This is the full-die version of guess_partial_die_structure_name.
20621 In this case we know DIE has no useful parent. */
20624 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20626 struct die_info *spec_die;
20627 struct dwarf2_cu *spec_cu;
20628 struct die_info *child;
20631 spec_die = die_specification (die, &spec_cu);
20632 if (spec_die != NULL)
20638 for (child = die->child;
20640 child = child->sibling)
20642 if (child->tag == DW_TAG_subprogram)
20644 const char *linkage_name = dw2_linkage_name (child, cu);
20646 if (linkage_name != NULL)
20649 = language_class_name_from_physname (cu->language_defn,
20653 if (actual_name != NULL)
20655 const char *die_name = dwarf2_name (die, cu);
20657 if (die_name != NULL
20658 && strcmp (die_name, actual_name) != 0)
20660 /* Strip off the class name from the full name.
20661 We want the prefix. */
20662 int die_name_len = strlen (die_name);
20663 int actual_name_len = strlen (actual_name);
20665 /* Test for '::' as a sanity check. */
20666 if (actual_name_len > die_name_len + 2
20667 && actual_name[actual_name_len
20668 - die_name_len - 1] == ':')
20669 name = (char *) obstack_copy0 (
20670 &cu->objfile->per_bfd->storage_obstack,
20671 actual_name, actual_name_len - die_name_len - 2);
20674 xfree (actual_name);
20683 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20684 prefix part in such case. See
20685 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20687 static const char *
20688 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20690 struct attribute *attr;
20693 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20694 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20697 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20700 attr = dw2_linkage_name_attr (die, cu);
20701 if (attr == NULL || DW_STRING (attr) == NULL)
20704 /* dwarf2_name had to be already called. */
20705 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20707 /* Strip the base name, keep any leading namespaces/classes. */
20708 base = strrchr (DW_STRING (attr), ':');
20709 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20712 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20714 &base[-1] - DW_STRING (attr));
20717 /* Return the name of the namespace/class that DIE is defined within,
20718 or "" if we can't tell. The caller should not xfree the result.
20720 For example, if we're within the method foo() in the following
20730 then determine_prefix on foo's die will return "N::C". */
20732 static const char *
20733 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20735 struct die_info *parent, *spec_die;
20736 struct dwarf2_cu *spec_cu;
20737 struct type *parent_type;
20738 const char *retval;
20740 if (cu->language != language_cplus
20741 && cu->language != language_fortran && cu->language != language_d
20742 && cu->language != language_rust)
20745 retval = anonymous_struct_prefix (die, cu);
20749 /* We have to be careful in the presence of DW_AT_specification.
20750 For example, with GCC 3.4, given the code
20754 // Definition of N::foo.
20758 then we'll have a tree of DIEs like this:
20760 1: DW_TAG_compile_unit
20761 2: DW_TAG_namespace // N
20762 3: DW_TAG_subprogram // declaration of N::foo
20763 4: DW_TAG_subprogram // definition of N::foo
20764 DW_AT_specification // refers to die #3
20766 Thus, when processing die #4, we have to pretend that we're in
20767 the context of its DW_AT_specification, namely the contex of die
20770 spec_die = die_specification (die, &spec_cu);
20771 if (spec_die == NULL)
20772 parent = die->parent;
20775 parent = spec_die->parent;
20779 if (parent == NULL)
20781 else if (parent->building_fullname)
20784 const char *parent_name;
20786 /* It has been seen on RealView 2.2 built binaries,
20787 DW_TAG_template_type_param types actually _defined_ as
20788 children of the parent class:
20791 template class <class Enum> Class{};
20792 Class<enum E> class_e;
20794 1: DW_TAG_class_type (Class)
20795 2: DW_TAG_enumeration_type (E)
20796 3: DW_TAG_enumerator (enum1:0)
20797 3: DW_TAG_enumerator (enum2:1)
20799 2: DW_TAG_template_type_param
20800 DW_AT_type DW_FORM_ref_udata (E)
20802 Besides being broken debug info, it can put GDB into an
20803 infinite loop. Consider:
20805 When we're building the full name for Class<E>, we'll start
20806 at Class, and go look over its template type parameters,
20807 finding E. We'll then try to build the full name of E, and
20808 reach here. We're now trying to build the full name of E,
20809 and look over the parent DIE for containing scope. In the
20810 broken case, if we followed the parent DIE of E, we'd again
20811 find Class, and once again go look at its template type
20812 arguments, etc., etc. Simply don't consider such parent die
20813 as source-level parent of this die (it can't be, the language
20814 doesn't allow it), and break the loop here. */
20815 name = dwarf2_name (die, cu);
20816 parent_name = dwarf2_name (parent, cu);
20817 complaint (&symfile_complaints,
20818 _("template param type '%s' defined within parent '%s'"),
20819 name ? name : "<unknown>",
20820 parent_name ? parent_name : "<unknown>");
20824 switch (parent->tag)
20826 case DW_TAG_namespace:
20827 parent_type = read_type_die (parent, cu);
20828 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20829 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20830 Work around this problem here. */
20831 if (cu->language == language_cplus
20832 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20834 /* We give a name to even anonymous namespaces. */
20835 return TYPE_TAG_NAME (parent_type);
20836 case DW_TAG_class_type:
20837 case DW_TAG_interface_type:
20838 case DW_TAG_structure_type:
20839 case DW_TAG_union_type:
20840 case DW_TAG_module:
20841 parent_type = read_type_die (parent, cu);
20842 if (TYPE_TAG_NAME (parent_type) != NULL)
20843 return TYPE_TAG_NAME (parent_type);
20845 /* An anonymous structure is only allowed non-static data
20846 members; no typedefs, no member functions, et cetera.
20847 So it does not need a prefix. */
20849 case DW_TAG_compile_unit:
20850 case DW_TAG_partial_unit:
20851 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20852 if (cu->language == language_cplus
20853 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
20854 && die->child != NULL
20855 && (die->tag == DW_TAG_class_type
20856 || die->tag == DW_TAG_structure_type
20857 || die->tag == DW_TAG_union_type))
20859 char *name = guess_full_die_structure_name (die, cu);
20864 case DW_TAG_enumeration_type:
20865 parent_type = read_type_die (parent, cu);
20866 if (TYPE_DECLARED_CLASS (parent_type))
20868 if (TYPE_TAG_NAME (parent_type) != NULL)
20869 return TYPE_TAG_NAME (parent_type);
20872 /* Fall through. */
20874 return determine_prefix (parent, cu);
20878 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20879 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20880 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20881 an obconcat, otherwise allocate storage for the result. The CU argument is
20882 used to determine the language and hence, the appropriate separator. */
20884 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20887 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20888 int physname, struct dwarf2_cu *cu)
20890 const char *lead = "";
20893 if (suffix == NULL || suffix[0] == '\0'
20894 || prefix == NULL || prefix[0] == '\0')
20896 else if (cu->language == language_d)
20898 /* For D, the 'main' function could be defined in any module, but it
20899 should never be prefixed. */
20900 if (strcmp (suffix, "D main") == 0)
20908 else if (cu->language == language_fortran && physname)
20910 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20911 DW_AT_MIPS_linkage_name is preferred and used instead. */
20919 if (prefix == NULL)
20921 if (suffix == NULL)
20928 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20930 strcpy (retval, lead);
20931 strcat (retval, prefix);
20932 strcat (retval, sep);
20933 strcat (retval, suffix);
20938 /* We have an obstack. */
20939 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20943 /* Return sibling of die, NULL if no sibling. */
20945 static struct die_info *
20946 sibling_die (struct die_info *die)
20948 return die->sibling;
20951 /* Get name of a die, return NULL if not found. */
20953 static const char *
20954 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20955 struct obstack *obstack)
20957 if (name && cu->language == language_cplus)
20959 std::string canon_name = cp_canonicalize_string (name);
20961 if (!canon_name.empty ())
20963 if (canon_name != name)
20964 name = (const char *) obstack_copy0 (obstack,
20965 canon_name.c_str (),
20966 canon_name.length ());
20973 /* Get name of a die, return NULL if not found.
20974 Anonymous namespaces are converted to their magic string. */
20976 static const char *
20977 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20979 struct attribute *attr;
20981 attr = dwarf2_attr (die, DW_AT_name, cu);
20982 if ((!attr || !DW_STRING (attr))
20983 && die->tag != DW_TAG_namespace
20984 && die->tag != DW_TAG_class_type
20985 && die->tag != DW_TAG_interface_type
20986 && die->tag != DW_TAG_structure_type
20987 && die->tag != DW_TAG_union_type)
20992 case DW_TAG_compile_unit:
20993 case DW_TAG_partial_unit:
20994 /* Compilation units have a DW_AT_name that is a filename, not
20995 a source language identifier. */
20996 case DW_TAG_enumeration_type:
20997 case DW_TAG_enumerator:
20998 /* These tags always have simple identifiers already; no need
20999 to canonicalize them. */
21000 return DW_STRING (attr);
21002 case DW_TAG_namespace:
21003 if (attr != NULL && DW_STRING (attr) != NULL)
21004 return DW_STRING (attr);
21005 return CP_ANONYMOUS_NAMESPACE_STR;
21007 case DW_TAG_class_type:
21008 case DW_TAG_interface_type:
21009 case DW_TAG_structure_type:
21010 case DW_TAG_union_type:
21011 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21012 structures or unions. These were of the form "._%d" in GCC 4.1,
21013 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21014 and GCC 4.4. We work around this problem by ignoring these. */
21015 if (attr && DW_STRING (attr)
21016 && (startswith (DW_STRING (attr), "._")
21017 || startswith (DW_STRING (attr), "<anonymous")))
21020 /* GCC might emit a nameless typedef that has a linkage name. See
21021 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21022 if (!attr || DW_STRING (attr) == NULL)
21024 char *demangled = NULL;
21026 attr = dw2_linkage_name_attr (die, cu);
21027 if (attr == NULL || DW_STRING (attr) == NULL)
21030 /* Avoid demangling DW_STRING (attr) the second time on a second
21031 call for the same DIE. */
21032 if (!DW_STRING_IS_CANONICAL (attr))
21033 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
21039 /* FIXME: we already did this for the partial symbol... */
21042 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21043 demangled, strlen (demangled)));
21044 DW_STRING_IS_CANONICAL (attr) = 1;
21047 /* Strip any leading namespaces/classes, keep only the base name.
21048 DW_AT_name for named DIEs does not contain the prefixes. */
21049 base = strrchr (DW_STRING (attr), ':');
21050 if (base && base > DW_STRING (attr) && base[-1] == ':')
21053 return DW_STRING (attr);
21062 if (!DW_STRING_IS_CANONICAL (attr))
21065 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21066 &cu->objfile->per_bfd->storage_obstack);
21067 DW_STRING_IS_CANONICAL (attr) = 1;
21069 return DW_STRING (attr);
21072 /* Return the die that this die in an extension of, or NULL if there
21073 is none. *EXT_CU is the CU containing DIE on input, and the CU
21074 containing the return value on output. */
21076 static struct die_info *
21077 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21079 struct attribute *attr;
21081 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21085 return follow_die_ref (die, attr, ext_cu);
21088 /* Convert a DIE tag into its string name. */
21090 static const char *
21091 dwarf_tag_name (unsigned tag)
21093 const char *name = get_DW_TAG_name (tag);
21096 return "DW_TAG_<unknown>";
21101 /* Convert a DWARF attribute code into its string name. */
21103 static const char *
21104 dwarf_attr_name (unsigned attr)
21108 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21109 if (attr == DW_AT_MIPS_fde)
21110 return "DW_AT_MIPS_fde";
21112 if (attr == DW_AT_HP_block_index)
21113 return "DW_AT_HP_block_index";
21116 name = get_DW_AT_name (attr);
21119 return "DW_AT_<unknown>";
21124 /* Convert a DWARF value form code into its string name. */
21126 static const char *
21127 dwarf_form_name (unsigned form)
21129 const char *name = get_DW_FORM_name (form);
21132 return "DW_FORM_<unknown>";
21137 static const char *
21138 dwarf_bool_name (unsigned mybool)
21146 /* Convert a DWARF type code into its string name. */
21148 static const char *
21149 dwarf_type_encoding_name (unsigned enc)
21151 const char *name = get_DW_ATE_name (enc);
21154 return "DW_ATE_<unknown>";
21160 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21164 print_spaces (indent, f);
21165 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
21166 dwarf_tag_name (die->tag), die->abbrev,
21167 to_underlying (die->sect_off));
21169 if (die->parent != NULL)
21171 print_spaces (indent, f);
21172 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
21173 to_underlying (die->parent->sect_off));
21176 print_spaces (indent, f);
21177 fprintf_unfiltered (f, " has children: %s\n",
21178 dwarf_bool_name (die->child != NULL));
21180 print_spaces (indent, f);
21181 fprintf_unfiltered (f, " attributes:\n");
21183 for (i = 0; i < die->num_attrs; ++i)
21185 print_spaces (indent, f);
21186 fprintf_unfiltered (f, " %s (%s) ",
21187 dwarf_attr_name (die->attrs[i].name),
21188 dwarf_form_name (die->attrs[i].form));
21190 switch (die->attrs[i].form)
21193 case DW_FORM_GNU_addr_index:
21194 fprintf_unfiltered (f, "address: ");
21195 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21197 case DW_FORM_block2:
21198 case DW_FORM_block4:
21199 case DW_FORM_block:
21200 case DW_FORM_block1:
21201 fprintf_unfiltered (f, "block: size %s",
21202 pulongest (DW_BLOCK (&die->attrs[i])->size));
21204 case DW_FORM_exprloc:
21205 fprintf_unfiltered (f, "expression: size %s",
21206 pulongest (DW_BLOCK (&die->attrs[i])->size));
21208 case DW_FORM_data16:
21209 fprintf_unfiltered (f, "constant of 16 bytes");
21211 case DW_FORM_ref_addr:
21212 fprintf_unfiltered (f, "ref address: ");
21213 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21215 case DW_FORM_GNU_ref_alt:
21216 fprintf_unfiltered (f, "alt ref address: ");
21217 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21223 case DW_FORM_ref_udata:
21224 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21225 (long) (DW_UNSND (&die->attrs[i])));
21227 case DW_FORM_data1:
21228 case DW_FORM_data2:
21229 case DW_FORM_data4:
21230 case DW_FORM_data8:
21231 case DW_FORM_udata:
21232 case DW_FORM_sdata:
21233 fprintf_unfiltered (f, "constant: %s",
21234 pulongest (DW_UNSND (&die->attrs[i])));
21236 case DW_FORM_sec_offset:
21237 fprintf_unfiltered (f, "section offset: %s",
21238 pulongest (DW_UNSND (&die->attrs[i])));
21240 case DW_FORM_ref_sig8:
21241 fprintf_unfiltered (f, "signature: %s",
21242 hex_string (DW_SIGNATURE (&die->attrs[i])));
21244 case DW_FORM_string:
21246 case DW_FORM_line_strp:
21247 case DW_FORM_GNU_str_index:
21248 case DW_FORM_GNU_strp_alt:
21249 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21250 DW_STRING (&die->attrs[i])
21251 ? DW_STRING (&die->attrs[i]) : "",
21252 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21255 if (DW_UNSND (&die->attrs[i]))
21256 fprintf_unfiltered (f, "flag: TRUE");
21258 fprintf_unfiltered (f, "flag: FALSE");
21260 case DW_FORM_flag_present:
21261 fprintf_unfiltered (f, "flag: TRUE");
21263 case DW_FORM_indirect:
21264 /* The reader will have reduced the indirect form to
21265 the "base form" so this form should not occur. */
21266 fprintf_unfiltered (f,
21267 "unexpected attribute form: DW_FORM_indirect");
21269 case DW_FORM_implicit_const:
21270 fprintf_unfiltered (f, "constant: %s",
21271 plongest (DW_SND (&die->attrs[i])));
21274 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21275 die->attrs[i].form);
21278 fprintf_unfiltered (f, "\n");
21283 dump_die_for_error (struct die_info *die)
21285 dump_die_shallow (gdb_stderr, 0, die);
21289 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21291 int indent = level * 4;
21293 gdb_assert (die != NULL);
21295 if (level >= max_level)
21298 dump_die_shallow (f, indent, die);
21300 if (die->child != NULL)
21302 print_spaces (indent, f);
21303 fprintf_unfiltered (f, " Children:");
21304 if (level + 1 < max_level)
21306 fprintf_unfiltered (f, "\n");
21307 dump_die_1 (f, level + 1, max_level, die->child);
21311 fprintf_unfiltered (f,
21312 " [not printed, max nesting level reached]\n");
21316 if (die->sibling != NULL && level > 0)
21318 dump_die_1 (f, level, max_level, die->sibling);
21322 /* This is called from the pdie macro in gdbinit.in.
21323 It's not static so gcc will keep a copy callable from gdb. */
21326 dump_die (struct die_info *die, int max_level)
21328 dump_die_1 (gdb_stdlog, 0, max_level, die);
21332 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21336 slot = htab_find_slot_with_hash (cu->die_hash, die,
21337 to_underlying (die->sect_off),
21343 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21347 dwarf2_get_ref_die_offset (const struct attribute *attr)
21349 if (attr_form_is_ref (attr))
21350 return (sect_offset) DW_UNSND (attr);
21352 complaint (&symfile_complaints,
21353 _("unsupported die ref attribute form: '%s'"),
21354 dwarf_form_name (attr->form));
21358 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21359 * the value held by the attribute is not constant. */
21362 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21364 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21365 return DW_SND (attr);
21366 else if (attr->form == DW_FORM_udata
21367 || attr->form == DW_FORM_data1
21368 || attr->form == DW_FORM_data2
21369 || attr->form == DW_FORM_data4
21370 || attr->form == DW_FORM_data8)
21371 return DW_UNSND (attr);
21374 /* For DW_FORM_data16 see attr_form_is_constant. */
21375 complaint (&symfile_complaints,
21376 _("Attribute value is not a constant (%s)"),
21377 dwarf_form_name (attr->form));
21378 return default_value;
21382 /* Follow reference or signature attribute ATTR of SRC_DIE.
21383 On entry *REF_CU is the CU of SRC_DIE.
21384 On exit *REF_CU is the CU of the result. */
21386 static struct die_info *
21387 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21388 struct dwarf2_cu **ref_cu)
21390 struct die_info *die;
21392 if (attr_form_is_ref (attr))
21393 die = follow_die_ref (src_die, attr, ref_cu);
21394 else if (attr->form == DW_FORM_ref_sig8)
21395 die = follow_die_sig (src_die, attr, ref_cu);
21398 dump_die_for_error (src_die);
21399 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21400 objfile_name ((*ref_cu)->objfile));
21406 /* Follow reference OFFSET.
21407 On entry *REF_CU is the CU of the source die referencing OFFSET.
21408 On exit *REF_CU is the CU of the result.
21409 Returns NULL if OFFSET is invalid. */
21411 static struct die_info *
21412 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21413 struct dwarf2_cu **ref_cu)
21415 struct die_info temp_die;
21416 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21418 gdb_assert (cu->per_cu != NULL);
21422 if (cu->per_cu->is_debug_types)
21424 /* .debug_types CUs cannot reference anything outside their CU.
21425 If they need to, they have to reference a signatured type via
21426 DW_FORM_ref_sig8. */
21427 if (!offset_in_cu_p (&cu->header, sect_off))
21430 else if (offset_in_dwz != cu->per_cu->is_dwz
21431 || !offset_in_cu_p (&cu->header, sect_off))
21433 struct dwarf2_per_cu_data *per_cu;
21435 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21438 /* If necessary, add it to the queue and load its DIEs. */
21439 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21440 load_full_comp_unit (per_cu, cu->language);
21442 target_cu = per_cu->cu;
21444 else if (cu->dies == NULL)
21446 /* We're loading full DIEs during partial symbol reading. */
21447 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21448 load_full_comp_unit (cu->per_cu, language_minimal);
21451 *ref_cu = target_cu;
21452 temp_die.sect_off = sect_off;
21453 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21455 to_underlying (sect_off));
21458 /* Follow reference attribute ATTR of SRC_DIE.
21459 On entry *REF_CU is the CU of SRC_DIE.
21460 On exit *REF_CU is the CU of the result. */
21462 static struct die_info *
21463 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21464 struct dwarf2_cu **ref_cu)
21466 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21467 struct dwarf2_cu *cu = *ref_cu;
21468 struct die_info *die;
21470 die = follow_die_offset (sect_off,
21471 (attr->form == DW_FORM_GNU_ref_alt
21472 || cu->per_cu->is_dwz),
21475 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21476 "at 0x%x [in module %s]"),
21477 to_underlying (sect_off), to_underlying (src_die->sect_off),
21478 objfile_name (cu->objfile));
21483 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
21484 Returned value is intended for DW_OP_call*. Returned
21485 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
21487 struct dwarf2_locexpr_baton
21488 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21489 struct dwarf2_per_cu_data *per_cu,
21490 CORE_ADDR (*get_frame_pc) (void *baton),
21493 struct dwarf2_cu *cu;
21494 struct die_info *die;
21495 struct attribute *attr;
21496 struct dwarf2_locexpr_baton retval;
21498 dw2_setup (per_cu->objfile);
21500 if (per_cu->cu == NULL)
21505 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21506 Instead just throw an error, not much else we can do. */
21507 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21508 to_underlying (sect_off), objfile_name (per_cu->objfile));
21511 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21513 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21514 to_underlying (sect_off), objfile_name (per_cu->objfile));
21516 attr = dwarf2_attr (die, DW_AT_location, cu);
21519 /* DWARF: "If there is no such attribute, then there is no effect.".
21520 DATA is ignored if SIZE is 0. */
21522 retval.data = NULL;
21525 else if (attr_form_is_section_offset (attr))
21527 struct dwarf2_loclist_baton loclist_baton;
21528 CORE_ADDR pc = (*get_frame_pc) (baton);
21531 fill_in_loclist_baton (cu, &loclist_baton, attr);
21533 retval.data = dwarf2_find_location_expression (&loclist_baton,
21535 retval.size = size;
21539 if (!attr_form_is_block (attr))
21540 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21541 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21542 to_underlying (sect_off), objfile_name (per_cu->objfile));
21544 retval.data = DW_BLOCK (attr)->data;
21545 retval.size = DW_BLOCK (attr)->size;
21547 retval.per_cu = cu->per_cu;
21549 age_cached_comp_units ();
21554 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21557 struct dwarf2_locexpr_baton
21558 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21559 struct dwarf2_per_cu_data *per_cu,
21560 CORE_ADDR (*get_frame_pc) (void *baton),
21563 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21565 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21568 /* Write a constant of a given type as target-ordered bytes into
21571 static const gdb_byte *
21572 write_constant_as_bytes (struct obstack *obstack,
21573 enum bfd_endian byte_order,
21580 *len = TYPE_LENGTH (type);
21581 result = (gdb_byte *) obstack_alloc (obstack, *len);
21582 store_unsigned_integer (result, *len, byte_order, value);
21587 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21588 pointer to the constant bytes and set LEN to the length of the
21589 data. If memory is needed, allocate it on OBSTACK. If the DIE
21590 does not have a DW_AT_const_value, return NULL. */
21593 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21594 struct dwarf2_per_cu_data *per_cu,
21595 struct obstack *obstack,
21598 struct dwarf2_cu *cu;
21599 struct die_info *die;
21600 struct attribute *attr;
21601 const gdb_byte *result = NULL;
21604 enum bfd_endian byte_order;
21606 dw2_setup (per_cu->objfile);
21608 if (per_cu->cu == NULL)
21613 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21614 Instead just throw an error, not much else we can do. */
21615 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21616 to_underlying (sect_off), objfile_name (per_cu->objfile));
21619 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21621 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21622 to_underlying (sect_off), objfile_name (per_cu->objfile));
21625 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21629 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21630 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21632 switch (attr->form)
21635 case DW_FORM_GNU_addr_index:
21639 *len = cu->header.addr_size;
21640 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21641 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21645 case DW_FORM_string:
21647 case DW_FORM_GNU_str_index:
21648 case DW_FORM_GNU_strp_alt:
21649 /* DW_STRING is already allocated on the objfile obstack, point
21651 result = (const gdb_byte *) DW_STRING (attr);
21652 *len = strlen (DW_STRING (attr));
21654 case DW_FORM_block1:
21655 case DW_FORM_block2:
21656 case DW_FORM_block4:
21657 case DW_FORM_block:
21658 case DW_FORM_exprloc:
21659 case DW_FORM_data16:
21660 result = DW_BLOCK (attr)->data;
21661 *len = DW_BLOCK (attr)->size;
21664 /* The DW_AT_const_value attributes are supposed to carry the
21665 symbol's value "represented as it would be on the target
21666 architecture." By the time we get here, it's already been
21667 converted to host endianness, so we just need to sign- or
21668 zero-extend it as appropriate. */
21669 case DW_FORM_data1:
21670 type = die_type (die, cu);
21671 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21672 if (result == NULL)
21673 result = write_constant_as_bytes (obstack, byte_order,
21676 case DW_FORM_data2:
21677 type = die_type (die, cu);
21678 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21679 if (result == NULL)
21680 result = write_constant_as_bytes (obstack, byte_order,
21683 case DW_FORM_data4:
21684 type = die_type (die, cu);
21685 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21686 if (result == NULL)
21687 result = write_constant_as_bytes (obstack, byte_order,
21690 case DW_FORM_data8:
21691 type = die_type (die, cu);
21692 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21693 if (result == NULL)
21694 result = write_constant_as_bytes (obstack, byte_order,
21698 case DW_FORM_sdata:
21699 case DW_FORM_implicit_const:
21700 type = die_type (die, cu);
21701 result = write_constant_as_bytes (obstack, byte_order,
21702 type, DW_SND (attr), len);
21705 case DW_FORM_udata:
21706 type = die_type (die, cu);
21707 result = write_constant_as_bytes (obstack, byte_order,
21708 type, DW_UNSND (attr), len);
21712 complaint (&symfile_complaints,
21713 _("unsupported const value attribute form: '%s'"),
21714 dwarf_form_name (attr->form));
21721 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21722 valid type for this die is found. */
21725 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21726 struct dwarf2_per_cu_data *per_cu)
21728 struct dwarf2_cu *cu;
21729 struct die_info *die;
21731 dw2_setup (per_cu->objfile);
21733 if (per_cu->cu == NULL)
21739 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21743 return die_type (die, cu);
21746 /* Return the type of the DIE at DIE_OFFSET in the CU named by
21750 dwarf2_get_die_type (cu_offset die_offset,
21751 struct dwarf2_per_cu_data *per_cu)
21753 dw2_setup (per_cu->objfile);
21755 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21756 return get_die_type_at_offset (die_offset_sect, per_cu);
21759 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21760 On entry *REF_CU is the CU of SRC_DIE.
21761 On exit *REF_CU is the CU of the result.
21762 Returns NULL if the referenced DIE isn't found. */
21764 static struct die_info *
21765 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21766 struct dwarf2_cu **ref_cu)
21768 struct die_info temp_die;
21769 struct dwarf2_cu *sig_cu;
21770 struct die_info *die;
21772 /* While it might be nice to assert sig_type->type == NULL here,
21773 we can get here for DW_AT_imported_declaration where we need
21774 the DIE not the type. */
21776 /* If necessary, add it to the queue and load its DIEs. */
21778 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21779 read_signatured_type (sig_type);
21781 sig_cu = sig_type->per_cu.cu;
21782 gdb_assert (sig_cu != NULL);
21783 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21784 temp_die.sect_off = sig_type->type_offset_in_section;
21785 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21786 to_underlying (temp_die.sect_off));
21789 /* For .gdb_index version 7 keep track of included TUs.
21790 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21791 if (dwarf2_per_objfile->index_table != NULL
21792 && dwarf2_per_objfile->index_table->version <= 7)
21794 VEC_safe_push (dwarf2_per_cu_ptr,
21795 (*ref_cu)->per_cu->imported_symtabs,
21806 /* Follow signatured type referenced by ATTR in SRC_DIE.
21807 On entry *REF_CU is the CU of SRC_DIE.
21808 On exit *REF_CU is the CU of the result.
21809 The result is the DIE of the type.
21810 If the referenced type cannot be found an error is thrown. */
21812 static struct die_info *
21813 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21814 struct dwarf2_cu **ref_cu)
21816 ULONGEST signature = DW_SIGNATURE (attr);
21817 struct signatured_type *sig_type;
21818 struct die_info *die;
21820 gdb_assert (attr->form == DW_FORM_ref_sig8);
21822 sig_type = lookup_signatured_type (*ref_cu, signature);
21823 /* sig_type will be NULL if the signatured type is missing from
21825 if (sig_type == NULL)
21827 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21828 " from DIE at 0x%x [in module %s]"),
21829 hex_string (signature), to_underlying (src_die->sect_off),
21830 objfile_name ((*ref_cu)->objfile));
21833 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21836 dump_die_for_error (src_die);
21837 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21838 " from DIE at 0x%x [in module %s]"),
21839 hex_string (signature), to_underlying (src_die->sect_off),
21840 objfile_name ((*ref_cu)->objfile));
21846 /* Get the type specified by SIGNATURE referenced in DIE/CU,
21847 reading in and processing the type unit if necessary. */
21849 static struct type *
21850 get_signatured_type (struct die_info *die, ULONGEST signature,
21851 struct dwarf2_cu *cu)
21853 struct signatured_type *sig_type;
21854 struct dwarf2_cu *type_cu;
21855 struct die_info *type_die;
21858 sig_type = lookup_signatured_type (cu, signature);
21859 /* sig_type will be NULL if the signatured type is missing from
21861 if (sig_type == NULL)
21863 complaint (&symfile_complaints,
21864 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21865 " from DIE at 0x%x [in module %s]"),
21866 hex_string (signature), to_underlying (die->sect_off),
21867 objfile_name (dwarf2_per_objfile->objfile));
21868 return build_error_marker_type (cu, die);
21871 /* If we already know the type we're done. */
21872 if (sig_type->type != NULL)
21873 return sig_type->type;
21876 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21877 if (type_die != NULL)
21879 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21880 is created. This is important, for example, because for c++ classes
21881 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21882 type = read_type_die (type_die, type_cu);
21885 complaint (&symfile_complaints,
21886 _("Dwarf Error: Cannot build signatured type %s"
21887 " referenced from DIE at 0x%x [in module %s]"),
21888 hex_string (signature), to_underlying (die->sect_off),
21889 objfile_name (dwarf2_per_objfile->objfile));
21890 type = build_error_marker_type (cu, die);
21895 complaint (&symfile_complaints,
21896 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21897 " from DIE at 0x%x [in module %s]"),
21898 hex_string (signature), to_underlying (die->sect_off),
21899 objfile_name (dwarf2_per_objfile->objfile));
21900 type = build_error_marker_type (cu, die);
21902 sig_type->type = type;
21907 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21908 reading in and processing the type unit if necessary. */
21910 static struct type *
21911 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21912 struct dwarf2_cu *cu) /* ARI: editCase function */
21914 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21915 if (attr_form_is_ref (attr))
21917 struct dwarf2_cu *type_cu = cu;
21918 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21920 return read_type_die (type_die, type_cu);
21922 else if (attr->form == DW_FORM_ref_sig8)
21924 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21928 complaint (&symfile_complaints,
21929 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21930 " at 0x%x [in module %s]"),
21931 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21932 objfile_name (dwarf2_per_objfile->objfile));
21933 return build_error_marker_type (cu, die);
21937 /* Load the DIEs associated with type unit PER_CU into memory. */
21940 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21942 struct signatured_type *sig_type;
21944 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21945 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21947 /* We have the per_cu, but we need the signatured_type.
21948 Fortunately this is an easy translation. */
21949 gdb_assert (per_cu->is_debug_types);
21950 sig_type = (struct signatured_type *) per_cu;
21952 gdb_assert (per_cu->cu == NULL);
21954 read_signatured_type (sig_type);
21956 gdb_assert (per_cu->cu != NULL);
21959 /* die_reader_func for read_signatured_type.
21960 This is identical to load_full_comp_unit_reader,
21961 but is kept separate for now. */
21964 read_signatured_type_reader (const struct die_reader_specs *reader,
21965 const gdb_byte *info_ptr,
21966 struct die_info *comp_unit_die,
21970 struct dwarf2_cu *cu = reader->cu;
21972 gdb_assert (cu->die_hash == NULL);
21974 htab_create_alloc_ex (cu->header.length / 12,
21978 &cu->comp_unit_obstack,
21979 hashtab_obstack_allocate,
21980 dummy_obstack_deallocate);
21983 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21984 &info_ptr, comp_unit_die);
21985 cu->dies = comp_unit_die;
21986 /* comp_unit_die is not stored in die_hash, no need. */
21988 /* We try not to read any attributes in this function, because not
21989 all CUs needed for references have been loaded yet, and symbol
21990 table processing isn't initialized. But we have to set the CU language,
21991 or we won't be able to build types correctly.
21992 Similarly, if we do not read the producer, we can not apply
21993 producer-specific interpretation. */
21994 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21997 /* Read in a signatured type and build its CU and DIEs.
21998 If the type is a stub for the real type in a DWO file,
21999 read in the real type from the DWO file as well. */
22002 read_signatured_type (struct signatured_type *sig_type)
22004 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22006 gdb_assert (per_cu->is_debug_types);
22007 gdb_assert (per_cu->cu == NULL);
22009 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22010 read_signatured_type_reader, NULL);
22011 sig_type->per_cu.tu_read = 1;
22014 /* Decode simple location descriptions.
22015 Given a pointer to a dwarf block that defines a location, compute
22016 the location and return the value.
22018 NOTE drow/2003-11-18: This function is called in two situations
22019 now: for the address of static or global variables (partial symbols
22020 only) and for offsets into structures which are expected to be
22021 (more or less) constant. The partial symbol case should go away,
22022 and only the constant case should remain. That will let this
22023 function complain more accurately. A few special modes are allowed
22024 without complaint for global variables (for instance, global
22025 register values and thread-local values).
22027 A location description containing no operations indicates that the
22028 object is optimized out. The return value is 0 for that case.
22029 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22030 callers will only want a very basic result and this can become a
22033 Note that stack[0] is unused except as a default error return. */
22036 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22038 struct objfile *objfile = cu->objfile;
22040 size_t size = blk->size;
22041 const gdb_byte *data = blk->data;
22042 CORE_ADDR stack[64];
22044 unsigned int bytes_read, unsnd;
22050 stack[++stacki] = 0;
22089 stack[++stacki] = op - DW_OP_lit0;
22124 stack[++stacki] = op - DW_OP_reg0;
22126 dwarf2_complex_location_expr_complaint ();
22130 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22132 stack[++stacki] = unsnd;
22134 dwarf2_complex_location_expr_complaint ();
22138 stack[++stacki] = read_address (objfile->obfd, &data[i],
22143 case DW_OP_const1u:
22144 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22148 case DW_OP_const1s:
22149 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22153 case DW_OP_const2u:
22154 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22158 case DW_OP_const2s:
22159 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22163 case DW_OP_const4u:
22164 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22168 case DW_OP_const4s:
22169 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22173 case DW_OP_const8u:
22174 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22179 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22185 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22190 stack[stacki + 1] = stack[stacki];
22195 stack[stacki - 1] += stack[stacki];
22199 case DW_OP_plus_uconst:
22200 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22206 stack[stacki - 1] -= stack[stacki];
22211 /* If we're not the last op, then we definitely can't encode
22212 this using GDB's address_class enum. This is valid for partial
22213 global symbols, although the variable's address will be bogus
22216 dwarf2_complex_location_expr_complaint ();
22219 case DW_OP_GNU_push_tls_address:
22220 case DW_OP_form_tls_address:
22221 /* The top of the stack has the offset from the beginning
22222 of the thread control block at which the variable is located. */
22223 /* Nothing should follow this operator, so the top of stack would
22225 /* This is valid for partial global symbols, but the variable's
22226 address will be bogus in the psymtab. Make it always at least
22227 non-zero to not look as a variable garbage collected by linker
22228 which have DW_OP_addr 0. */
22230 dwarf2_complex_location_expr_complaint ();
22234 case DW_OP_GNU_uninit:
22237 case DW_OP_GNU_addr_index:
22238 case DW_OP_GNU_const_index:
22239 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22246 const char *name = get_DW_OP_name (op);
22249 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22252 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22256 return (stack[stacki]);
22259 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22260 outside of the allocated space. Also enforce minimum>0. */
22261 if (stacki >= ARRAY_SIZE (stack) - 1)
22263 complaint (&symfile_complaints,
22264 _("location description stack overflow"));
22270 complaint (&symfile_complaints,
22271 _("location description stack underflow"));
22275 return (stack[stacki]);
22278 /* memory allocation interface */
22280 static struct dwarf_block *
22281 dwarf_alloc_block (struct dwarf2_cu *cu)
22283 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22286 static struct die_info *
22287 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22289 struct die_info *die;
22290 size_t size = sizeof (struct die_info);
22293 size += (num_attrs - 1) * sizeof (struct attribute);
22295 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22296 memset (die, 0, sizeof (struct die_info));
22301 /* Macro support. */
22303 /* Return file name relative to the compilation directory of file number I in
22304 *LH's file name table. The result is allocated using xmalloc; the caller is
22305 responsible for freeing it. */
22308 file_file_name (int file, struct line_header *lh)
22310 /* Is the file number a valid index into the line header's file name
22311 table? Remember that file numbers start with one, not zero. */
22312 if (1 <= file && file <= lh->file_names.size ())
22314 const file_entry &fe = lh->file_names[file - 1];
22316 if (!IS_ABSOLUTE_PATH (fe.name))
22318 const char *dir = fe.include_dir (lh);
22320 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22322 return xstrdup (fe.name);
22326 /* The compiler produced a bogus file number. We can at least
22327 record the macro definitions made in the file, even if we
22328 won't be able to find the file by name. */
22329 char fake_name[80];
22331 xsnprintf (fake_name, sizeof (fake_name),
22332 "<bad macro file number %d>", file);
22334 complaint (&symfile_complaints,
22335 _("bad file number in macro information (%d)"),
22338 return xstrdup (fake_name);
22342 /* Return the full name of file number I in *LH's file name table.
22343 Use COMP_DIR as the name of the current directory of the
22344 compilation. The result is allocated using xmalloc; the caller is
22345 responsible for freeing it. */
22347 file_full_name (int file, struct line_header *lh, const char *comp_dir)
22349 /* Is the file number a valid index into the line header's file name
22350 table? Remember that file numbers start with one, not zero. */
22351 if (1 <= file && file <= lh->file_names.size ())
22353 char *relative = file_file_name (file, lh);
22355 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22357 return reconcat (relative, comp_dir, SLASH_STRING,
22358 relative, (char *) NULL);
22361 return file_file_name (file, lh);
22365 static struct macro_source_file *
22366 macro_start_file (int file, int line,
22367 struct macro_source_file *current_file,
22368 struct line_header *lh)
22370 /* File name relative to the compilation directory of this source file. */
22371 char *file_name = file_file_name (file, lh);
22373 if (! current_file)
22375 /* Note: We don't create a macro table for this compilation unit
22376 at all until we actually get a filename. */
22377 struct macro_table *macro_table = get_macro_table ();
22379 /* If we have no current file, then this must be the start_file
22380 directive for the compilation unit's main source file. */
22381 current_file = macro_set_main (macro_table, file_name);
22382 macro_define_special (macro_table);
22385 current_file = macro_include (current_file, line, file_name);
22389 return current_file;
22392 static const char *
22393 consume_improper_spaces (const char *p, const char *body)
22397 complaint (&symfile_complaints,
22398 _("macro definition contains spaces "
22399 "in formal argument list:\n`%s'"),
22411 parse_macro_definition (struct macro_source_file *file, int line,
22416 /* The body string takes one of two forms. For object-like macro
22417 definitions, it should be:
22419 <macro name> " " <definition>
22421 For function-like macro definitions, it should be:
22423 <macro name> "() " <definition>
22425 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22427 Spaces may appear only where explicitly indicated, and in the
22430 The Dwarf 2 spec says that an object-like macro's name is always
22431 followed by a space, but versions of GCC around March 2002 omit
22432 the space when the macro's definition is the empty string.
22434 The Dwarf 2 spec says that there should be no spaces between the
22435 formal arguments in a function-like macro's formal argument list,
22436 but versions of GCC around March 2002 include spaces after the
22440 /* Find the extent of the macro name. The macro name is terminated
22441 by either a space or null character (for an object-like macro) or
22442 an opening paren (for a function-like macro). */
22443 for (p = body; *p; p++)
22444 if (*p == ' ' || *p == '(')
22447 if (*p == ' ' || *p == '\0')
22449 /* It's an object-like macro. */
22450 int name_len = p - body;
22451 char *name = savestring (body, name_len);
22452 const char *replacement;
22455 replacement = body + name_len + 1;
22458 dwarf2_macro_malformed_definition_complaint (body);
22459 replacement = body + name_len;
22462 macro_define_object (file, line, name, replacement);
22466 else if (*p == '(')
22468 /* It's a function-like macro. */
22469 char *name = savestring (body, p - body);
22472 char **argv = XNEWVEC (char *, argv_size);
22476 p = consume_improper_spaces (p, body);
22478 /* Parse the formal argument list. */
22479 while (*p && *p != ')')
22481 /* Find the extent of the current argument name. */
22482 const char *arg_start = p;
22484 while (*p && *p != ',' && *p != ')' && *p != ' ')
22487 if (! *p || p == arg_start)
22488 dwarf2_macro_malformed_definition_complaint (body);
22491 /* Make sure argv has room for the new argument. */
22492 if (argc >= argv_size)
22495 argv = XRESIZEVEC (char *, argv, argv_size);
22498 argv[argc++] = savestring (arg_start, p - arg_start);
22501 p = consume_improper_spaces (p, body);
22503 /* Consume the comma, if present. */
22508 p = consume_improper_spaces (p, body);
22517 /* Perfectly formed definition, no complaints. */
22518 macro_define_function (file, line, name,
22519 argc, (const char **) argv,
22521 else if (*p == '\0')
22523 /* Complain, but do define it. */
22524 dwarf2_macro_malformed_definition_complaint (body);
22525 macro_define_function (file, line, name,
22526 argc, (const char **) argv,
22530 /* Just complain. */
22531 dwarf2_macro_malformed_definition_complaint (body);
22534 /* Just complain. */
22535 dwarf2_macro_malformed_definition_complaint (body);
22541 for (i = 0; i < argc; i++)
22547 dwarf2_macro_malformed_definition_complaint (body);
22550 /* Skip some bytes from BYTES according to the form given in FORM.
22551 Returns the new pointer. */
22553 static const gdb_byte *
22554 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
22555 enum dwarf_form form,
22556 unsigned int offset_size,
22557 struct dwarf2_section_info *section)
22559 unsigned int bytes_read;
22563 case DW_FORM_data1:
22568 case DW_FORM_data2:
22572 case DW_FORM_data4:
22576 case DW_FORM_data8:
22580 case DW_FORM_data16:
22584 case DW_FORM_string:
22585 read_direct_string (abfd, bytes, &bytes_read);
22586 bytes += bytes_read;
22589 case DW_FORM_sec_offset:
22591 case DW_FORM_GNU_strp_alt:
22592 bytes += offset_size;
22595 case DW_FORM_block:
22596 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22597 bytes += bytes_read;
22600 case DW_FORM_block1:
22601 bytes += 1 + read_1_byte (abfd, bytes);
22603 case DW_FORM_block2:
22604 bytes += 2 + read_2_bytes (abfd, bytes);
22606 case DW_FORM_block4:
22607 bytes += 4 + read_4_bytes (abfd, bytes);
22610 case DW_FORM_sdata:
22611 case DW_FORM_udata:
22612 case DW_FORM_GNU_addr_index:
22613 case DW_FORM_GNU_str_index:
22614 bytes = gdb_skip_leb128 (bytes, buffer_end);
22617 dwarf2_section_buffer_overflow_complaint (section);
22622 case DW_FORM_implicit_const:
22628 complaint (&symfile_complaints,
22629 _("invalid form 0x%x in `%s'"),
22630 form, get_section_name (section));
22638 /* A helper for dwarf_decode_macros that handles skipping an unknown
22639 opcode. Returns an updated pointer to the macro data buffer; or,
22640 on error, issues a complaint and returns NULL. */
22642 static const gdb_byte *
22643 skip_unknown_opcode (unsigned int opcode,
22644 const gdb_byte **opcode_definitions,
22645 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22647 unsigned int offset_size,
22648 struct dwarf2_section_info *section)
22650 unsigned int bytes_read, i;
22652 const gdb_byte *defn;
22654 if (opcode_definitions[opcode] == NULL)
22656 complaint (&symfile_complaints,
22657 _("unrecognized DW_MACFINO opcode 0x%x"),
22662 defn = opcode_definitions[opcode];
22663 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22664 defn += bytes_read;
22666 for (i = 0; i < arg; ++i)
22668 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22669 (enum dwarf_form) defn[i], offset_size,
22671 if (mac_ptr == NULL)
22673 /* skip_form_bytes already issued the complaint. */
22681 /* A helper function which parses the header of a macro section.
22682 If the macro section is the extended (for now called "GNU") type,
22683 then this updates *OFFSET_SIZE. Returns a pointer to just after
22684 the header, or issues a complaint and returns NULL on error. */
22686 static const gdb_byte *
22687 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
22689 const gdb_byte *mac_ptr,
22690 unsigned int *offset_size,
22691 int section_is_gnu)
22693 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
22695 if (section_is_gnu)
22697 unsigned int version, flags;
22699 version = read_2_bytes (abfd, mac_ptr);
22700 if (version != 4 && version != 5)
22702 complaint (&symfile_complaints,
22703 _("unrecognized version `%d' in .debug_macro section"),
22709 flags = read_1_byte (abfd, mac_ptr);
22711 *offset_size = (flags & 1) ? 8 : 4;
22713 if ((flags & 2) != 0)
22714 /* We don't need the line table offset. */
22715 mac_ptr += *offset_size;
22717 /* Vendor opcode descriptions. */
22718 if ((flags & 4) != 0)
22720 unsigned int i, count;
22722 count = read_1_byte (abfd, mac_ptr);
22724 for (i = 0; i < count; ++i)
22726 unsigned int opcode, bytes_read;
22729 opcode = read_1_byte (abfd, mac_ptr);
22731 opcode_definitions[opcode] = mac_ptr;
22732 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22733 mac_ptr += bytes_read;
22742 /* A helper for dwarf_decode_macros that handles the GNU extensions,
22743 including DW_MACRO_import. */
22746 dwarf_decode_macro_bytes (bfd *abfd,
22747 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22748 struct macro_source_file *current_file,
22749 struct line_header *lh,
22750 struct dwarf2_section_info *section,
22751 int section_is_gnu, int section_is_dwz,
22752 unsigned int offset_size,
22753 htab_t include_hash)
22755 struct objfile *objfile = dwarf2_per_objfile->objfile;
22756 enum dwarf_macro_record_type macinfo_type;
22757 int at_commandline;
22758 const gdb_byte *opcode_definitions[256];
22760 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22761 &offset_size, section_is_gnu);
22762 if (mac_ptr == NULL)
22764 /* We already issued a complaint. */
22768 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22769 GDB is still reading the definitions from command line. First
22770 DW_MACINFO_start_file will need to be ignored as it was already executed
22771 to create CURRENT_FILE for the main source holding also the command line
22772 definitions. On first met DW_MACINFO_start_file this flag is reset to
22773 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22775 at_commandline = 1;
22779 /* Do we at least have room for a macinfo type byte? */
22780 if (mac_ptr >= mac_end)
22782 dwarf2_section_buffer_overflow_complaint (section);
22786 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22789 /* Note that we rely on the fact that the corresponding GNU and
22790 DWARF constants are the same. */
22791 switch (macinfo_type)
22793 /* A zero macinfo type indicates the end of the macro
22798 case DW_MACRO_define:
22799 case DW_MACRO_undef:
22800 case DW_MACRO_define_strp:
22801 case DW_MACRO_undef_strp:
22802 case DW_MACRO_define_sup:
22803 case DW_MACRO_undef_sup:
22805 unsigned int bytes_read;
22810 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22811 mac_ptr += bytes_read;
22813 if (macinfo_type == DW_MACRO_define
22814 || macinfo_type == DW_MACRO_undef)
22816 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22817 mac_ptr += bytes_read;
22821 LONGEST str_offset;
22823 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22824 mac_ptr += offset_size;
22826 if (macinfo_type == DW_MACRO_define_sup
22827 || macinfo_type == DW_MACRO_undef_sup
22830 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22832 body = read_indirect_string_from_dwz (dwz, str_offset);
22835 body = read_indirect_string_at_offset (abfd, str_offset);
22838 is_define = (macinfo_type == DW_MACRO_define
22839 || macinfo_type == DW_MACRO_define_strp
22840 || macinfo_type == DW_MACRO_define_sup);
22841 if (! current_file)
22843 /* DWARF violation as no main source is present. */
22844 complaint (&symfile_complaints,
22845 _("debug info with no main source gives macro %s "
22847 is_define ? _("definition") : _("undefinition"),
22851 if ((line == 0 && !at_commandline)
22852 || (line != 0 && at_commandline))
22853 complaint (&symfile_complaints,
22854 _("debug info gives %s macro %s with %s line %d: %s"),
22855 at_commandline ? _("command-line") : _("in-file"),
22856 is_define ? _("definition") : _("undefinition"),
22857 line == 0 ? _("zero") : _("non-zero"), line, body);
22860 parse_macro_definition (current_file, line, body);
22863 gdb_assert (macinfo_type == DW_MACRO_undef
22864 || macinfo_type == DW_MACRO_undef_strp
22865 || macinfo_type == DW_MACRO_undef_sup);
22866 macro_undef (current_file, line, body);
22871 case DW_MACRO_start_file:
22873 unsigned int bytes_read;
22876 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22877 mac_ptr += bytes_read;
22878 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22879 mac_ptr += bytes_read;
22881 if ((line == 0 && !at_commandline)
22882 || (line != 0 && at_commandline))
22883 complaint (&symfile_complaints,
22884 _("debug info gives source %d included "
22885 "from %s at %s line %d"),
22886 file, at_commandline ? _("command-line") : _("file"),
22887 line == 0 ? _("zero") : _("non-zero"), line);
22889 if (at_commandline)
22891 /* This DW_MACRO_start_file was executed in the
22893 at_commandline = 0;
22896 current_file = macro_start_file (file, line, current_file, lh);
22900 case DW_MACRO_end_file:
22901 if (! current_file)
22902 complaint (&symfile_complaints,
22903 _("macro debug info has an unmatched "
22904 "`close_file' directive"));
22907 current_file = current_file->included_by;
22908 if (! current_file)
22910 enum dwarf_macro_record_type next_type;
22912 /* GCC circa March 2002 doesn't produce the zero
22913 type byte marking the end of the compilation
22914 unit. Complain if it's not there, but exit no
22917 /* Do we at least have room for a macinfo type byte? */
22918 if (mac_ptr >= mac_end)
22920 dwarf2_section_buffer_overflow_complaint (section);
22924 /* We don't increment mac_ptr here, so this is just
22927 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22929 if (next_type != 0)
22930 complaint (&symfile_complaints,
22931 _("no terminating 0-type entry for "
22932 "macros in `.debug_macinfo' section"));
22939 case DW_MACRO_import:
22940 case DW_MACRO_import_sup:
22944 bfd *include_bfd = abfd;
22945 struct dwarf2_section_info *include_section = section;
22946 const gdb_byte *include_mac_end = mac_end;
22947 int is_dwz = section_is_dwz;
22948 const gdb_byte *new_mac_ptr;
22950 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22951 mac_ptr += offset_size;
22953 if (macinfo_type == DW_MACRO_import_sup)
22955 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22957 dwarf2_read_section (objfile, &dwz->macro);
22959 include_section = &dwz->macro;
22960 include_bfd = get_section_bfd_owner (include_section);
22961 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22965 new_mac_ptr = include_section->buffer + offset;
22966 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22970 /* This has actually happened; see
22971 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22972 complaint (&symfile_complaints,
22973 _("recursive DW_MACRO_import in "
22974 ".debug_macro section"));
22978 *slot = (void *) new_mac_ptr;
22980 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22981 include_mac_end, current_file, lh,
22982 section, section_is_gnu, is_dwz,
22983 offset_size, include_hash);
22985 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22990 case DW_MACINFO_vendor_ext:
22991 if (!section_is_gnu)
22993 unsigned int bytes_read;
22995 /* This reads the constant, but since we don't recognize
22996 any vendor extensions, we ignore it. */
22997 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22998 mac_ptr += bytes_read;
22999 read_direct_string (abfd, mac_ptr, &bytes_read);
23000 mac_ptr += bytes_read;
23002 /* We don't recognize any vendor extensions. */
23008 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23009 mac_ptr, mac_end, abfd, offset_size,
23011 if (mac_ptr == NULL)
23015 } while (macinfo_type != 0);
23019 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23020 int section_is_gnu)
23022 struct objfile *objfile = dwarf2_per_objfile->objfile;
23023 struct line_header *lh = cu->line_header;
23025 const gdb_byte *mac_ptr, *mac_end;
23026 struct macro_source_file *current_file = 0;
23027 enum dwarf_macro_record_type macinfo_type;
23028 unsigned int offset_size = cu->header.offset_size;
23029 const gdb_byte *opcode_definitions[256];
23031 struct dwarf2_section_info *section;
23032 const char *section_name;
23034 if (cu->dwo_unit != NULL)
23036 if (section_is_gnu)
23038 section = &cu->dwo_unit->dwo_file->sections.macro;
23039 section_name = ".debug_macro.dwo";
23043 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23044 section_name = ".debug_macinfo.dwo";
23049 if (section_is_gnu)
23051 section = &dwarf2_per_objfile->macro;
23052 section_name = ".debug_macro";
23056 section = &dwarf2_per_objfile->macinfo;
23057 section_name = ".debug_macinfo";
23061 dwarf2_read_section (objfile, section);
23062 if (section->buffer == NULL)
23064 complaint (&symfile_complaints, _("missing %s section"), section_name);
23067 abfd = get_section_bfd_owner (section);
23069 /* First pass: Find the name of the base filename.
23070 This filename is needed in order to process all macros whose definition
23071 (or undefinition) comes from the command line. These macros are defined
23072 before the first DW_MACINFO_start_file entry, and yet still need to be
23073 associated to the base file.
23075 To determine the base file name, we scan the macro definitions until we
23076 reach the first DW_MACINFO_start_file entry. We then initialize
23077 CURRENT_FILE accordingly so that any macro definition found before the
23078 first DW_MACINFO_start_file can still be associated to the base file. */
23080 mac_ptr = section->buffer + offset;
23081 mac_end = section->buffer + section->size;
23083 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23084 &offset_size, section_is_gnu);
23085 if (mac_ptr == NULL)
23087 /* We already issued a complaint. */
23093 /* Do we at least have room for a macinfo type byte? */
23094 if (mac_ptr >= mac_end)
23096 /* Complaint is printed during the second pass as GDB will probably
23097 stop the first pass earlier upon finding
23098 DW_MACINFO_start_file. */
23102 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23105 /* Note that we rely on the fact that the corresponding GNU and
23106 DWARF constants are the same. */
23107 switch (macinfo_type)
23109 /* A zero macinfo type indicates the end of the macro
23114 case DW_MACRO_define:
23115 case DW_MACRO_undef:
23116 /* Only skip the data by MAC_PTR. */
23118 unsigned int bytes_read;
23120 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23121 mac_ptr += bytes_read;
23122 read_direct_string (abfd, mac_ptr, &bytes_read);
23123 mac_ptr += bytes_read;
23127 case DW_MACRO_start_file:
23129 unsigned int bytes_read;
23132 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23133 mac_ptr += bytes_read;
23134 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23135 mac_ptr += bytes_read;
23137 current_file = macro_start_file (file, line, current_file, lh);
23141 case DW_MACRO_end_file:
23142 /* No data to skip by MAC_PTR. */
23145 case DW_MACRO_define_strp:
23146 case DW_MACRO_undef_strp:
23147 case DW_MACRO_define_sup:
23148 case DW_MACRO_undef_sup:
23150 unsigned int bytes_read;
23152 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23153 mac_ptr += bytes_read;
23154 mac_ptr += offset_size;
23158 case DW_MACRO_import:
23159 case DW_MACRO_import_sup:
23160 /* Note that, according to the spec, a transparent include
23161 chain cannot call DW_MACRO_start_file. So, we can just
23162 skip this opcode. */
23163 mac_ptr += offset_size;
23166 case DW_MACINFO_vendor_ext:
23167 /* Only skip the data by MAC_PTR. */
23168 if (!section_is_gnu)
23170 unsigned int bytes_read;
23172 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23173 mac_ptr += bytes_read;
23174 read_direct_string (abfd, mac_ptr, &bytes_read);
23175 mac_ptr += bytes_read;
23180 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23181 mac_ptr, mac_end, abfd, offset_size,
23183 if (mac_ptr == NULL)
23187 } while (macinfo_type != 0 && current_file == NULL);
23189 /* Second pass: Process all entries.
23191 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23192 command-line macro definitions/undefinitions. This flag is unset when we
23193 reach the first DW_MACINFO_start_file entry. */
23195 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23197 NULL, xcalloc, xfree));
23198 mac_ptr = section->buffer + offset;
23199 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23200 *slot = (void *) mac_ptr;
23201 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
23202 current_file, lh, section,
23203 section_is_gnu, 0, offset_size,
23204 include_hash.get ());
23207 /* Check if the attribute's form is a DW_FORM_block*
23208 if so return true else false. */
23211 attr_form_is_block (const struct attribute *attr)
23213 return (attr == NULL ? 0 :
23214 attr->form == DW_FORM_block1
23215 || attr->form == DW_FORM_block2
23216 || attr->form == DW_FORM_block4
23217 || attr->form == DW_FORM_block
23218 || attr->form == DW_FORM_exprloc);
23221 /* Return non-zero if ATTR's value is a section offset --- classes
23222 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23223 You may use DW_UNSND (attr) to retrieve such offsets.
23225 Section 7.5.4, "Attribute Encodings", explains that no attribute
23226 may have a value that belongs to more than one of these classes; it
23227 would be ambiguous if we did, because we use the same forms for all
23231 attr_form_is_section_offset (const struct attribute *attr)
23233 return (attr->form == DW_FORM_data4
23234 || attr->form == DW_FORM_data8
23235 || attr->form == DW_FORM_sec_offset);
23238 /* Return non-zero if ATTR's value falls in the 'constant' class, or
23239 zero otherwise. When this function returns true, you can apply
23240 dwarf2_get_attr_constant_value to it.
23242 However, note that for some attributes you must check
23243 attr_form_is_section_offset before using this test. DW_FORM_data4
23244 and DW_FORM_data8 are members of both the constant class, and of
23245 the classes that contain offsets into other debug sections
23246 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23247 that, if an attribute's can be either a constant or one of the
23248 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
23249 taken as section offsets, not constants.
23251 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23252 cannot handle that. */
23255 attr_form_is_constant (const struct attribute *attr)
23257 switch (attr->form)
23259 case DW_FORM_sdata:
23260 case DW_FORM_udata:
23261 case DW_FORM_data1:
23262 case DW_FORM_data2:
23263 case DW_FORM_data4:
23264 case DW_FORM_data8:
23265 case DW_FORM_implicit_const:
23273 /* DW_ADDR is always stored already as sect_offset; despite for the forms
23274 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23277 attr_form_is_ref (const struct attribute *attr)
23279 switch (attr->form)
23281 case DW_FORM_ref_addr:
23286 case DW_FORM_ref_udata:
23287 case DW_FORM_GNU_ref_alt:
23294 /* Return the .debug_loc section to use for CU.
23295 For DWO files use .debug_loc.dwo. */
23297 static struct dwarf2_section_info *
23298 cu_debug_loc_section (struct dwarf2_cu *cu)
23302 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23304 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
23306 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23307 : &dwarf2_per_objfile->loc);
23310 /* A helper function that fills in a dwarf2_loclist_baton. */
23313 fill_in_loclist_baton (struct dwarf2_cu *cu,
23314 struct dwarf2_loclist_baton *baton,
23315 const struct attribute *attr)
23317 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23319 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
23321 baton->per_cu = cu->per_cu;
23322 gdb_assert (baton->per_cu);
23323 /* We don't know how long the location list is, but make sure we
23324 don't run off the edge of the section. */
23325 baton->size = section->size - DW_UNSND (attr);
23326 baton->data = section->buffer + DW_UNSND (attr);
23327 baton->base_address = cu->base_address;
23328 baton->from_dwo = cu->dwo_unit != NULL;
23332 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23333 struct dwarf2_cu *cu, int is_block)
23335 struct objfile *objfile = dwarf2_per_objfile->objfile;
23336 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23338 if (attr_form_is_section_offset (attr)
23339 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23340 the section. If so, fall through to the complaint in the
23342 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
23344 struct dwarf2_loclist_baton *baton;
23346 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23348 fill_in_loclist_baton (cu, baton, attr);
23350 if (cu->base_known == 0)
23351 complaint (&symfile_complaints,
23352 _("Location list used without "
23353 "specifying the CU base address."));
23355 SYMBOL_ACLASS_INDEX (sym) = (is_block
23356 ? dwarf2_loclist_block_index
23357 : dwarf2_loclist_index);
23358 SYMBOL_LOCATION_BATON (sym) = baton;
23362 struct dwarf2_locexpr_baton *baton;
23364 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23365 baton->per_cu = cu->per_cu;
23366 gdb_assert (baton->per_cu);
23368 if (attr_form_is_block (attr))
23370 /* Note that we're just copying the block's data pointer
23371 here, not the actual data. We're still pointing into the
23372 info_buffer for SYM's objfile; right now we never release
23373 that buffer, but when we do clean up properly this may
23375 baton->size = DW_BLOCK (attr)->size;
23376 baton->data = DW_BLOCK (attr)->data;
23380 dwarf2_invalid_attrib_class_complaint ("location description",
23381 SYMBOL_NATURAL_NAME (sym));
23385 SYMBOL_ACLASS_INDEX (sym) = (is_block
23386 ? dwarf2_locexpr_block_index
23387 : dwarf2_locexpr_index);
23388 SYMBOL_LOCATION_BATON (sym) = baton;
23392 /* Return the OBJFILE associated with the compilation unit CU. If CU
23393 came from a separate debuginfo file, then the master objfile is
23397 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23399 struct objfile *objfile = per_cu->objfile;
23401 /* Return the master objfile, so that we can report and look up the
23402 correct file containing this variable. */
23403 if (objfile->separate_debug_objfile_backlink)
23404 objfile = objfile->separate_debug_objfile_backlink;
23409 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23410 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23411 CU_HEADERP first. */
23413 static const struct comp_unit_head *
23414 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23415 struct dwarf2_per_cu_data *per_cu)
23417 const gdb_byte *info_ptr;
23420 return &per_cu->cu->header;
23422 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23424 memset (cu_headerp, 0, sizeof (*cu_headerp));
23425 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23426 rcuh_kind::COMPILE);
23431 /* Return the address size given in the compilation unit header for CU. */
23434 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23436 struct comp_unit_head cu_header_local;
23437 const struct comp_unit_head *cu_headerp;
23439 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23441 return cu_headerp->addr_size;
23444 /* Return the offset size given in the compilation unit header for CU. */
23447 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23449 struct comp_unit_head cu_header_local;
23450 const struct comp_unit_head *cu_headerp;
23452 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23454 return cu_headerp->offset_size;
23457 /* See its dwarf2loc.h declaration. */
23460 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23462 struct comp_unit_head cu_header_local;
23463 const struct comp_unit_head *cu_headerp;
23465 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23467 if (cu_headerp->version == 2)
23468 return cu_headerp->addr_size;
23470 return cu_headerp->offset_size;
23473 /* Return the text offset of the CU. The returned offset comes from
23474 this CU's objfile. If this objfile came from a separate debuginfo
23475 file, then the offset may be different from the corresponding
23476 offset in the parent objfile. */
23479 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23481 struct objfile *objfile = per_cu->objfile;
23483 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23486 /* Return DWARF version number of PER_CU. */
23489 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23491 return per_cu->dwarf_version;
23494 /* Locate the .debug_info compilation unit from CU's objfile which contains
23495 the DIE at OFFSET. Raises an error on failure. */
23497 static struct dwarf2_per_cu_data *
23498 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23499 unsigned int offset_in_dwz,
23500 struct objfile *objfile)
23502 struct dwarf2_per_cu_data *this_cu;
23504 const sect_offset *cu_off;
23507 high = dwarf2_per_objfile->n_comp_units - 1;
23510 struct dwarf2_per_cu_data *mid_cu;
23511 int mid = low + (high - low) / 2;
23513 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
23514 cu_off = &mid_cu->sect_off;
23515 if (mid_cu->is_dwz > offset_in_dwz
23516 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
23521 gdb_assert (low == high);
23522 this_cu = dwarf2_per_objfile->all_comp_units[low];
23523 cu_off = &this_cu->sect_off;
23524 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
23526 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23527 error (_("Dwarf Error: could not find partial DIE containing "
23528 "offset 0x%x [in module %s]"),
23529 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
23531 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23533 return dwarf2_per_objfile->all_comp_units[low-1];
23537 this_cu = dwarf2_per_objfile->all_comp_units[low];
23538 if (low == dwarf2_per_objfile->n_comp_units - 1
23539 && sect_off >= this_cu->sect_off + this_cu->length)
23540 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23541 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23546 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23549 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
23551 memset (cu, 0, sizeof (*cu));
23553 cu->per_cu = per_cu;
23554 cu->objfile = per_cu->objfile;
23555 obstack_init (&cu->comp_unit_obstack);
23558 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23561 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23562 enum language pretend_language)
23564 struct attribute *attr;
23566 /* Set the language we're debugging. */
23567 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23569 set_cu_language (DW_UNSND (attr), cu);
23572 cu->language = pretend_language;
23573 cu->language_defn = language_def (cu->language);
23576 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23579 /* Release one cached compilation unit, CU. We unlink it from the tree
23580 of compilation units, but we don't remove it from the read_in_chain;
23581 the caller is responsible for that.
23582 NOTE: DATA is a void * because this function is also used as a
23583 cleanup routine. */
23586 free_heap_comp_unit (void *data)
23588 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23590 gdb_assert (cu->per_cu != NULL);
23591 cu->per_cu->cu = NULL;
23594 obstack_free (&cu->comp_unit_obstack, NULL);
23599 /* This cleanup function is passed the address of a dwarf2_cu on the stack
23600 when we're finished with it. We can't free the pointer itself, but be
23601 sure to unlink it from the cache. Also release any associated storage. */
23604 free_stack_comp_unit (void *data)
23606 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23608 gdb_assert (cu->per_cu != NULL);
23609 cu->per_cu->cu = NULL;
23612 obstack_free (&cu->comp_unit_obstack, NULL);
23613 cu->partial_dies = NULL;
23616 /* Free all cached compilation units. */
23619 free_cached_comp_units (void *data)
23621 dwarf2_per_objfile->free_cached_comp_units ();
23624 /* Increase the age counter on each cached compilation unit, and free
23625 any that are too old. */
23628 age_cached_comp_units (void)
23630 struct dwarf2_per_cu_data *per_cu, **last_chain;
23632 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23633 per_cu = dwarf2_per_objfile->read_in_chain;
23634 while (per_cu != NULL)
23636 per_cu->cu->last_used ++;
23637 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23638 dwarf2_mark (per_cu->cu);
23639 per_cu = per_cu->cu->read_in_chain;
23642 per_cu = dwarf2_per_objfile->read_in_chain;
23643 last_chain = &dwarf2_per_objfile->read_in_chain;
23644 while (per_cu != NULL)
23646 struct dwarf2_per_cu_data *next_cu;
23648 next_cu = per_cu->cu->read_in_chain;
23650 if (!per_cu->cu->mark)
23652 free_heap_comp_unit (per_cu->cu);
23653 *last_chain = next_cu;
23656 last_chain = &per_cu->cu->read_in_chain;
23662 /* Remove a single compilation unit from the cache. */
23665 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23667 struct dwarf2_per_cu_data *per_cu, **last_chain;
23669 per_cu = dwarf2_per_objfile->read_in_chain;
23670 last_chain = &dwarf2_per_objfile->read_in_chain;
23671 while (per_cu != NULL)
23673 struct dwarf2_per_cu_data *next_cu;
23675 next_cu = per_cu->cu->read_in_chain;
23677 if (per_cu == target_per_cu)
23679 free_heap_comp_unit (per_cu->cu);
23681 *last_chain = next_cu;
23685 last_chain = &per_cu->cu->read_in_chain;
23691 /* Release all extra memory associated with OBJFILE. */
23694 dwarf2_free_objfile (struct objfile *objfile)
23697 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23698 dwarf2_objfile_data_key);
23700 if (dwarf2_per_objfile == NULL)
23703 dwarf2_per_objfile->~dwarf2_per_objfile ();
23706 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23707 We store these in a hash table separate from the DIEs, and preserve them
23708 when the DIEs are flushed out of cache.
23710 The CU "per_cu" pointer is needed because offset alone is not enough to
23711 uniquely identify the type. A file may have multiple .debug_types sections,
23712 or the type may come from a DWO file. Furthermore, while it's more logical
23713 to use per_cu->section+offset, with Fission the section with the data is in
23714 the DWO file but we don't know that section at the point we need it.
23715 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23716 because we can enter the lookup routine, get_die_type_at_offset, from
23717 outside this file, and thus won't necessarily have PER_CU->cu.
23718 Fortunately, PER_CU is stable for the life of the objfile. */
23720 struct dwarf2_per_cu_offset_and_type
23722 const struct dwarf2_per_cu_data *per_cu;
23723 sect_offset sect_off;
23727 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23730 per_cu_offset_and_type_hash (const void *item)
23732 const struct dwarf2_per_cu_offset_and_type *ofs
23733 = (const struct dwarf2_per_cu_offset_and_type *) item;
23735 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23738 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23741 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23743 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23744 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23745 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23746 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23748 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23749 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23752 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23753 table if necessary. For convenience, return TYPE.
23755 The DIEs reading must have careful ordering to:
23756 * Not cause infite loops trying to read in DIEs as a prerequisite for
23757 reading current DIE.
23758 * Not trying to dereference contents of still incompletely read in types
23759 while reading in other DIEs.
23760 * Enable referencing still incompletely read in types just by a pointer to
23761 the type without accessing its fields.
23763 Therefore caller should follow these rules:
23764 * Try to fetch any prerequisite types we may need to build this DIE type
23765 before building the type and calling set_die_type.
23766 * After building type call set_die_type for current DIE as soon as
23767 possible before fetching more types to complete the current type.
23768 * Make the type as complete as possible before fetching more types. */
23770 static struct type *
23771 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23773 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23774 struct objfile *objfile = cu->objfile;
23775 struct attribute *attr;
23776 struct dynamic_prop prop;
23778 /* For Ada types, make sure that the gnat-specific data is always
23779 initialized (if not already set). There are a few types where
23780 we should not be doing so, because the type-specific area is
23781 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23782 where the type-specific area is used to store the floatformat).
23783 But this is not a problem, because the gnat-specific information
23784 is actually not needed for these types. */
23785 if (need_gnat_info (cu)
23786 && TYPE_CODE (type) != TYPE_CODE_FUNC
23787 && TYPE_CODE (type) != TYPE_CODE_FLT
23788 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23789 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23790 && TYPE_CODE (type) != TYPE_CODE_METHOD
23791 && !HAVE_GNAT_AUX_INFO (type))
23792 INIT_GNAT_SPECIFIC (type);
23794 /* Read DW_AT_allocated and set in type. */
23795 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23796 if (attr_form_is_block (attr))
23798 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23799 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23801 else if (attr != NULL)
23803 complaint (&symfile_complaints,
23804 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23805 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23806 to_underlying (die->sect_off));
23809 /* Read DW_AT_associated and set in type. */
23810 attr = dwarf2_attr (die, DW_AT_associated, cu);
23811 if (attr_form_is_block (attr))
23813 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23814 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23816 else if (attr != NULL)
23818 complaint (&symfile_complaints,
23819 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23820 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23821 to_underlying (die->sect_off));
23824 /* Read DW_AT_data_location and set in type. */
23825 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23826 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23827 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
23829 if (dwarf2_per_objfile->die_type_hash == NULL)
23831 dwarf2_per_objfile->die_type_hash =
23832 htab_create_alloc_ex (127,
23833 per_cu_offset_and_type_hash,
23834 per_cu_offset_and_type_eq,
23836 &objfile->objfile_obstack,
23837 hashtab_obstack_allocate,
23838 dummy_obstack_deallocate);
23841 ofs.per_cu = cu->per_cu;
23842 ofs.sect_off = die->sect_off;
23844 slot = (struct dwarf2_per_cu_offset_and_type **)
23845 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23847 complaint (&symfile_complaints,
23848 _("A problem internal to GDB: DIE 0x%x has type already set"),
23849 to_underlying (die->sect_off));
23850 *slot = XOBNEW (&objfile->objfile_obstack,
23851 struct dwarf2_per_cu_offset_and_type);
23856 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23857 or return NULL if the die does not have a saved type. */
23859 static struct type *
23860 get_die_type_at_offset (sect_offset sect_off,
23861 struct dwarf2_per_cu_data *per_cu)
23863 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23865 if (dwarf2_per_objfile->die_type_hash == NULL)
23868 ofs.per_cu = per_cu;
23869 ofs.sect_off = sect_off;
23870 slot = ((struct dwarf2_per_cu_offset_and_type *)
23871 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23878 /* Look up the type for DIE in CU in die_type_hash,
23879 or return NULL if DIE does not have a saved type. */
23881 static struct type *
23882 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23884 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23887 /* Add a dependence relationship from CU to REF_PER_CU. */
23890 dwarf2_add_dependence (struct dwarf2_cu *cu,
23891 struct dwarf2_per_cu_data *ref_per_cu)
23895 if (cu->dependencies == NULL)
23897 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23898 NULL, &cu->comp_unit_obstack,
23899 hashtab_obstack_allocate,
23900 dummy_obstack_deallocate);
23902 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23904 *slot = ref_per_cu;
23907 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23908 Set the mark field in every compilation unit in the
23909 cache that we must keep because we are keeping CU. */
23912 dwarf2_mark_helper (void **slot, void *data)
23914 struct dwarf2_per_cu_data *per_cu;
23916 per_cu = (struct dwarf2_per_cu_data *) *slot;
23918 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23919 reading of the chain. As such dependencies remain valid it is not much
23920 useful to track and undo them during QUIT cleanups. */
23921 if (per_cu->cu == NULL)
23924 if (per_cu->cu->mark)
23926 per_cu->cu->mark = 1;
23928 if (per_cu->cu->dependencies != NULL)
23929 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23934 /* Set the mark field in CU and in every other compilation unit in the
23935 cache that we must keep because we are keeping CU. */
23938 dwarf2_mark (struct dwarf2_cu *cu)
23943 if (cu->dependencies != NULL)
23944 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23948 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23952 per_cu->cu->mark = 0;
23953 per_cu = per_cu->cu->read_in_chain;
23957 /* Trivial hash function for partial_die_info: the hash value of a DIE
23958 is its offset in .debug_info for this objfile. */
23961 partial_die_hash (const void *item)
23963 const struct partial_die_info *part_die
23964 = (const struct partial_die_info *) item;
23966 return to_underlying (part_die->sect_off);
23969 /* Trivial comparison function for partial_die_info structures: two DIEs
23970 are equal if they have the same offset. */
23973 partial_die_eq (const void *item_lhs, const void *item_rhs)
23975 const struct partial_die_info *part_die_lhs
23976 = (const struct partial_die_info *) item_lhs;
23977 const struct partial_die_info *part_die_rhs
23978 = (const struct partial_die_info *) item_rhs;
23980 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23983 static struct cmd_list_element *set_dwarf_cmdlist;
23984 static struct cmd_list_element *show_dwarf_cmdlist;
23987 set_dwarf_cmd (const char *args, int from_tty)
23989 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23994 show_dwarf_cmd (const char *args, int from_tty)
23996 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23999 /* Free data associated with OBJFILE, if necessary. */
24002 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
24004 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
24007 /* Make sure we don't accidentally use dwarf2_per_objfile while
24009 dwarf2_per_objfile = NULL;
24011 for (ix = 0; ix < data->n_comp_units; ++ix)
24012 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
24014 for (ix = 0; ix < data->n_type_units; ++ix)
24015 VEC_free (dwarf2_per_cu_ptr,
24016 data->all_type_units[ix]->per_cu.imported_symtabs);
24017 xfree (data->all_type_units);
24019 VEC_free (dwarf2_section_info_def, data->types);
24021 if (data->dwo_files)
24022 free_dwo_files (data->dwo_files, objfile);
24023 if (data->dwp_file)
24024 gdb_bfd_unref (data->dwp_file->dbfd);
24026 if (data->dwz_file && data->dwz_file->dwz_bfd)
24027 gdb_bfd_unref (data->dwz_file->dwz_bfd);
24029 if (data->index_table != NULL)
24030 data->index_table->~mapped_index ();
24034 /* The "save gdb-index" command. */
24036 /* In-memory buffer to prepare data to be written later to a file. */
24040 /* Copy DATA to the end of the buffer. */
24041 template<typename T>
24042 void append_data (const T &data)
24044 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24045 reinterpret_cast<const gdb_byte *> (&data + 1),
24046 grow (sizeof (data)));
24049 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24050 terminating zero is appended too. */
24051 void append_cstr0 (const char *cstr)
24053 const size_t size = strlen (cstr) + 1;
24054 std::copy (cstr, cstr + size, grow (size));
24057 /* Accept a host-format integer in VAL and append it to the buffer
24058 as a target-format integer which is LEN bytes long. */
24059 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24061 ::store_unsigned_integer (grow (len), len, byte_order, val);
24064 /* Return the size of the buffer. */
24065 size_t size () const
24067 return m_vec.size ();
24070 /* Write the buffer to FILE. */
24071 void file_write (FILE *file) const
24073 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
24074 error (_("couldn't write data to file"));
24078 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24079 the start of the new block. */
24080 gdb_byte *grow (size_t size)
24082 m_vec.resize (m_vec.size () + size);
24083 return &*m_vec.end () - size;
24086 gdb::byte_vector m_vec;
24089 /* An entry in the symbol table. */
24090 struct symtab_index_entry
24092 /* The name of the symbol. */
24094 /* The offset of the name in the constant pool. */
24095 offset_type index_offset;
24096 /* A sorted vector of the indices of all the CUs that hold an object
24098 std::vector<offset_type> cu_indices;
24101 /* The symbol table. This is a power-of-2-sized hash table. */
24102 struct mapped_symtab
24106 data.resize (1024);
24109 offset_type n_elements = 0;
24110 std::vector<symtab_index_entry> data;
24113 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
24116 Function is used only during write_hash_table so no index format backward
24117 compatibility is needed. */
24119 static symtab_index_entry &
24120 find_slot (struct mapped_symtab *symtab, const char *name)
24122 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
24124 index = hash & (symtab->data.size () - 1);
24125 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
24129 if (symtab->data[index].name == NULL
24130 || strcmp (name, symtab->data[index].name) == 0)
24131 return symtab->data[index];
24132 index = (index + step) & (symtab->data.size () - 1);
24136 /* Expand SYMTAB's hash table. */
24139 hash_expand (struct mapped_symtab *symtab)
24141 auto old_entries = std::move (symtab->data);
24143 symtab->data.clear ();
24144 symtab->data.resize (old_entries.size () * 2);
24146 for (auto &it : old_entries)
24147 if (it.name != NULL)
24149 auto &ref = find_slot (symtab, it.name);
24150 ref = std::move (it);
24154 /* Add an entry to SYMTAB. NAME is the name of the symbol.
24155 CU_INDEX is the index of the CU in which the symbol appears.
24156 IS_STATIC is one if the symbol is static, otherwise zero (global). */
24159 add_index_entry (struct mapped_symtab *symtab, const char *name,
24160 int is_static, gdb_index_symbol_kind kind,
24161 offset_type cu_index)
24163 offset_type cu_index_and_attrs;
24165 ++symtab->n_elements;
24166 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
24167 hash_expand (symtab);
24169 symtab_index_entry &slot = find_slot (symtab, name);
24170 if (slot.name == NULL)
24173 /* index_offset is set later. */
24176 cu_index_and_attrs = 0;
24177 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24178 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24179 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24181 /* We don't want to record an index value twice as we want to avoid the
24183 We process all global symbols and then all static symbols
24184 (which would allow us to avoid the duplication by only having to check
24185 the last entry pushed), but a symbol could have multiple kinds in one CU.
24186 To keep things simple we don't worry about the duplication here and
24187 sort and uniqufy the list after we've processed all symbols. */
24188 slot.cu_indices.push_back (cu_index_and_attrs);
24191 /* Sort and remove duplicates of all symbols' cu_indices lists. */
24194 uniquify_cu_indices (struct mapped_symtab *symtab)
24196 for (auto &entry : symtab->data)
24198 if (entry.name != NULL && !entry.cu_indices.empty ())
24200 auto &cu_indices = entry.cu_indices;
24201 std::sort (cu_indices.begin (), cu_indices.end ());
24202 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24203 cu_indices.erase (from, cu_indices.end ());
24208 /* A form of 'const char *' suitable for container keys. Only the
24209 pointer is stored. The strings themselves are compared, not the
24214 c_str_view (const char *cstr)
24218 bool operator== (const c_str_view &other) const
24220 return strcmp (m_cstr, other.m_cstr) == 0;
24224 friend class c_str_view_hasher;
24225 const char *const m_cstr;
24228 /* A std::unordered_map::hasher for c_str_view that uses the right
24229 hash function for strings in a mapped index. */
24230 class c_str_view_hasher
24233 size_t operator () (const c_str_view &x) const
24235 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24239 /* A std::unordered_map::hasher for std::vector<>. */
24240 template<typename T>
24241 class vector_hasher
24244 size_t operator () (const std::vector<T> &key) const
24246 return iterative_hash (key.data (),
24247 sizeof (key.front ()) * key.size (), 0);
24251 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24252 constant pool entries going into the data buffer CPOOL. */
24255 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24258 /* Elements are sorted vectors of the indices of all the CUs that
24259 hold an object of this name. */
24260 std::unordered_map<std::vector<offset_type>, offset_type,
24261 vector_hasher<offset_type>>
24264 /* We add all the index vectors to the constant pool first, to
24265 ensure alignment is ok. */
24266 for (symtab_index_entry &entry : symtab->data)
24268 if (entry.name == NULL)
24270 gdb_assert (entry.index_offset == 0);
24272 /* Finding before inserting is faster than always trying to
24273 insert, because inserting always allocates a node, does the
24274 lookup, and then destroys the new node if another node
24275 already had the same key. C++17 try_emplace will avoid
24278 = symbol_hash_table.find (entry.cu_indices);
24279 if (found != symbol_hash_table.end ())
24281 entry.index_offset = found->second;
24285 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24286 entry.index_offset = cpool.size ();
24287 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24288 for (const auto index : entry.cu_indices)
24289 cpool.append_data (MAYBE_SWAP (index));
24293 /* Now write out the hash table. */
24294 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
24295 for (const auto &entry : symtab->data)
24297 offset_type str_off, vec_off;
24299 if (entry.name != NULL)
24301 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
24302 if (insertpair.second)
24303 cpool.append_cstr0 (entry.name);
24304 str_off = insertpair.first->second;
24305 vec_off = entry.index_offset;
24309 /* While 0 is a valid constant pool index, it is not valid
24310 to have 0 for both offsets. */
24315 output.append_data (MAYBE_SWAP (str_off));
24316 output.append_data (MAYBE_SWAP (vec_off));
24320 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
24322 /* Helper struct for building the address table. */
24323 struct addrmap_index_data
24325 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24326 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24329 struct objfile *objfile;
24330 data_buf &addr_vec;
24331 psym_index_map &cu_index_htab;
24333 /* Non-zero if the previous_* fields are valid.
24334 We can't write an entry until we see the next entry (since it is only then
24335 that we know the end of the entry). */
24336 int previous_valid;
24337 /* Index of the CU in the table of all CUs in the index file. */
24338 unsigned int previous_cu_index;
24339 /* Start address of the CU. */
24340 CORE_ADDR previous_cu_start;
24343 /* Write an address entry to ADDR_VEC. */
24346 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
24347 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
24349 CORE_ADDR baseaddr;
24351 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24353 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24354 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
24355 addr_vec.append_data (MAYBE_SWAP (cu_index));
24358 /* Worker function for traversing an addrmap to build the address table. */
24361 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24363 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24364 struct partial_symtab *pst = (struct partial_symtab *) obj;
24366 if (data->previous_valid)
24367 add_address_entry (data->objfile, data->addr_vec,
24368 data->previous_cu_start, start_addr,
24369 data->previous_cu_index);
24371 data->previous_cu_start = start_addr;
24374 const auto it = data->cu_index_htab.find (pst);
24375 gdb_assert (it != data->cu_index_htab.cend ());
24376 data->previous_cu_index = it->second;
24377 data->previous_valid = 1;
24380 data->previous_valid = 0;
24385 /* Write OBJFILE's address map to ADDR_VEC.
24386 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24387 in the index file. */
24390 write_address_map (struct objfile *objfile, data_buf &addr_vec,
24391 psym_index_map &cu_index_htab)
24393 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
24395 /* When writing the address table, we have to cope with the fact that
24396 the addrmap iterator only provides the start of a region; we have to
24397 wait until the next invocation to get the start of the next region. */
24399 addrmap_index_data.objfile = objfile;
24400 addrmap_index_data.previous_valid = 0;
24402 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24403 &addrmap_index_data);
24405 /* It's highly unlikely the last entry (end address = 0xff...ff)
24406 is valid, but we should still handle it.
24407 The end address is recorded as the start of the next region, but that
24408 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24410 if (addrmap_index_data.previous_valid)
24411 add_address_entry (objfile, addr_vec,
24412 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24413 addrmap_index_data.previous_cu_index);
24416 /* Return the symbol kind of PSYM. */
24418 static gdb_index_symbol_kind
24419 symbol_kind (struct partial_symbol *psym)
24421 domain_enum domain = PSYMBOL_DOMAIN (psym);
24422 enum address_class aclass = PSYMBOL_CLASS (psym);
24430 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24432 return GDB_INDEX_SYMBOL_KIND_TYPE;
24434 case LOC_CONST_BYTES:
24435 case LOC_OPTIMIZED_OUT:
24437 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24439 /* Note: It's currently impossible to recognize psyms as enum values
24440 short of reading the type info. For now punt. */
24441 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24443 /* There are other LOC_FOO values that one might want to classify
24444 as variables, but dwarf2read.c doesn't currently use them. */
24445 return GDB_INDEX_SYMBOL_KIND_OTHER;
24447 case STRUCT_DOMAIN:
24448 return GDB_INDEX_SYMBOL_KIND_TYPE;
24450 return GDB_INDEX_SYMBOL_KIND_OTHER;
24454 /* Add a list of partial symbols to SYMTAB. */
24457 write_psymbols (struct mapped_symtab *symtab,
24458 std::unordered_set<partial_symbol *> &psyms_seen,
24459 struct partial_symbol **psymp,
24461 offset_type cu_index,
24464 for (; count-- > 0; ++psymp)
24466 struct partial_symbol *psym = *psymp;
24468 if (SYMBOL_LANGUAGE (psym) == language_ada)
24469 error (_("Ada is not currently supported by the index"));
24471 /* Only add a given psymbol once. */
24472 if (psyms_seen.insert (psym).second)
24474 gdb_index_symbol_kind kind = symbol_kind (psym);
24476 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24477 is_static, kind, cu_index);
24482 /* A helper struct used when iterating over debug_types. */
24483 struct signatured_type_index_data
24485 signatured_type_index_data (data_buf &types_list_,
24486 std::unordered_set<partial_symbol *> &psyms_seen_)
24487 : types_list (types_list_), psyms_seen (psyms_seen_)
24490 struct objfile *objfile;
24491 struct mapped_symtab *symtab;
24492 data_buf &types_list;
24493 std::unordered_set<partial_symbol *> &psyms_seen;
24497 /* A helper function that writes a single signatured_type to an
24501 write_one_signatured_type (void **slot, void *d)
24503 struct signatured_type_index_data *info
24504 = (struct signatured_type_index_data *) d;
24505 struct signatured_type *entry = (struct signatured_type *) *slot;
24506 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
24508 write_psymbols (info->symtab,
24510 &info->objfile->global_psymbols[psymtab->globals_offset],
24511 psymtab->n_global_syms, info->cu_index,
24513 write_psymbols (info->symtab,
24515 &info->objfile->static_psymbols[psymtab->statics_offset],
24516 psymtab->n_static_syms, info->cu_index,
24519 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24520 to_underlying (entry->per_cu.sect_off));
24521 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24522 to_underlying (entry->type_offset_in_tu));
24523 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
24530 /* Recurse into all "included" dependencies and count their symbols as
24531 if they appeared in this psymtab. */
24534 recursively_count_psymbols (struct partial_symtab *psymtab,
24535 size_t &psyms_seen)
24537 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24538 if (psymtab->dependencies[i]->user != NULL)
24539 recursively_count_psymbols (psymtab->dependencies[i],
24542 psyms_seen += psymtab->n_global_syms;
24543 psyms_seen += psymtab->n_static_syms;
24546 /* Recurse into all "included" dependencies and write their symbols as
24547 if they appeared in this psymtab. */
24550 recursively_write_psymbols (struct objfile *objfile,
24551 struct partial_symtab *psymtab,
24552 struct mapped_symtab *symtab,
24553 std::unordered_set<partial_symbol *> &psyms_seen,
24554 offset_type cu_index)
24558 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24559 if (psymtab->dependencies[i]->user != NULL)
24560 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24561 symtab, psyms_seen, cu_index);
24563 write_psymbols (symtab,
24565 &objfile->global_psymbols[psymtab->globals_offset],
24566 psymtab->n_global_syms, cu_index,
24568 write_psymbols (symtab,
24570 &objfile->static_psymbols[psymtab->statics_offset],
24571 psymtab->n_static_syms, cu_index,
24575 /* Create an index file for OBJFILE in the directory DIR. */
24578 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
24580 if (dwarf2_per_objfile->using_index)
24581 error (_("Cannot use an index to create the index"));
24583 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
24584 error (_("Cannot make an index when the file has multiple .debug_types sections"));
24586 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
24590 if (stat (objfile_name (objfile), &st) < 0)
24591 perror_with_name (objfile_name (objfile));
24593 std::string filename (std::string (dir) + SLASH_STRING
24594 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
24596 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
24598 error (_("Can't open `%s' for writing"), filename.c_str ());
24600 /* Order matters here; we want FILE to be closed before FILENAME is
24601 unlinked, because on MS-Windows one cannot delete a file that is
24602 still open. (Don't call anything here that might throw until
24603 file_closer is created.) */
24604 gdb::unlinker unlink_file (filename.c_str ());
24605 gdb_file_up close_out_file (out_file);
24607 mapped_symtab symtab;
24610 /* While we're scanning CU's create a table that maps a psymtab pointer
24611 (which is what addrmap records) to its index (which is what is recorded
24612 in the index file). This will later be needed to write the address
24614 psym_index_map cu_index_htab;
24615 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
24617 /* The CU list is already sorted, so we don't need to do additional
24618 work here. Also, the debug_types entries do not appear in
24619 all_comp_units, but only in their own hash table. */
24621 /* The psyms_seen set is potentially going to be largish (~40k
24622 elements when indexing a -g3 build of GDB itself). Estimate the
24623 number of elements in order to avoid too many rehashes, which
24624 require rebuilding buckets and thus many trips to
24626 size_t psyms_count = 0;
24627 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24629 struct dwarf2_per_cu_data *per_cu
24630 = dwarf2_per_objfile->all_comp_units[i];
24631 struct partial_symtab *psymtab = per_cu->v.psymtab;
24633 if (psymtab != NULL && psymtab->user == NULL)
24634 recursively_count_psymbols (psymtab, psyms_count);
24636 /* Generating an index for gdb itself shows a ratio of
24637 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
24638 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
24639 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24641 struct dwarf2_per_cu_data *per_cu
24642 = dwarf2_per_objfile->all_comp_units[i];
24643 struct partial_symtab *psymtab = per_cu->v.psymtab;
24645 /* CU of a shared file from 'dwz -m' may be unused by this main file.
24646 It may be referenced from a local scope but in such case it does not
24647 need to be present in .gdb_index. */
24648 if (psymtab == NULL)
24651 if (psymtab->user == NULL)
24652 recursively_write_psymbols (objfile, psymtab, &symtab,
24655 const auto insertpair = cu_index_htab.emplace (psymtab, i);
24656 gdb_assert (insertpair.second);
24658 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
24659 to_underlying (per_cu->sect_off));
24660 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
24663 /* Dump the address map. */
24665 write_address_map (objfile, addr_vec, cu_index_htab);
24667 /* Write out the .debug_type entries, if any. */
24668 data_buf types_cu_list;
24669 if (dwarf2_per_objfile->signatured_types)
24671 signatured_type_index_data sig_data (types_cu_list,
24674 sig_data.objfile = objfile;
24675 sig_data.symtab = &symtab;
24676 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
24677 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
24678 write_one_signatured_type, &sig_data);
24681 /* Now that we've processed all symbols we can shrink their cu_indices
24683 uniquify_cu_indices (&symtab);
24685 data_buf symtab_vec, constant_pool;
24686 write_hash_table (&symtab, symtab_vec, constant_pool);
24689 const offset_type size_of_contents = 6 * sizeof (offset_type);
24690 offset_type total_len = size_of_contents;
24692 /* The version number. */
24693 contents.append_data (MAYBE_SWAP (8));
24695 /* The offset of the CU list from the start of the file. */
24696 contents.append_data (MAYBE_SWAP (total_len));
24697 total_len += cu_list.size ();
24699 /* The offset of the types CU list from the start of the file. */
24700 contents.append_data (MAYBE_SWAP (total_len));
24701 total_len += types_cu_list.size ();
24703 /* The offset of the address table from the start of the file. */
24704 contents.append_data (MAYBE_SWAP (total_len));
24705 total_len += addr_vec.size ();
24707 /* The offset of the symbol table from the start of the file. */
24708 contents.append_data (MAYBE_SWAP (total_len));
24709 total_len += symtab_vec.size ();
24711 /* The offset of the constant pool from the start of the file. */
24712 contents.append_data (MAYBE_SWAP (total_len));
24713 total_len += constant_pool.size ();
24715 gdb_assert (contents.size () == size_of_contents);
24717 contents.file_write (out_file);
24718 cu_list.file_write (out_file);
24719 types_cu_list.file_write (out_file);
24720 addr_vec.file_write (out_file);
24721 symtab_vec.file_write (out_file);
24722 constant_pool.file_write (out_file);
24724 /* We want to keep the file. */
24725 unlink_file.keep ();
24728 /* Implementation of the `save gdb-index' command.
24730 Note that the file format used by this command is documented in the
24731 GDB manual. Any changes here must be documented there. */
24734 save_gdb_index_command (const char *arg, int from_tty)
24736 struct objfile *objfile;
24739 error (_("usage: save gdb-index DIRECTORY"));
24741 ALL_OBJFILES (objfile)
24745 /* If the objfile does not correspond to an actual file, skip it. */
24746 if (stat (objfile_name (objfile), &st) < 0)
24750 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24751 dwarf2_objfile_data_key);
24752 if (dwarf2_per_objfile)
24757 write_psymtabs_to_index (objfile, arg);
24759 CATCH (except, RETURN_MASK_ERROR)
24761 exception_fprintf (gdb_stderr, except,
24762 _("Error while writing index for `%s': "),
24763 objfile_name (objfile));
24772 int dwarf_always_disassemble;
24775 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24776 struct cmd_list_element *c, const char *value)
24778 fprintf_filtered (file,
24779 _("Whether to always disassemble "
24780 "DWARF expressions is %s.\n"),
24785 show_check_physname (struct ui_file *file, int from_tty,
24786 struct cmd_list_element *c, const char *value)
24788 fprintf_filtered (file,
24789 _("Whether to check \"physname\" is %s.\n"),
24794 _initialize_dwarf2_read (void)
24796 struct cmd_list_element *c;
24798 dwarf2_objfile_data_key
24799 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24801 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24802 Set DWARF specific variables.\n\
24803 Configure DWARF variables such as the cache size"),
24804 &set_dwarf_cmdlist, "maintenance set dwarf ",
24805 0/*allow-unknown*/, &maintenance_set_cmdlist);
24807 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24808 Show DWARF specific variables\n\
24809 Show DWARF variables such as the cache size"),
24810 &show_dwarf_cmdlist, "maintenance show dwarf ",
24811 0/*allow-unknown*/, &maintenance_show_cmdlist);
24813 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24814 &dwarf_max_cache_age, _("\
24815 Set the upper bound on the age of cached DWARF compilation units."), _("\
24816 Show the upper bound on the age of cached DWARF compilation units."), _("\
24817 A higher limit means that cached compilation units will be stored\n\
24818 in memory longer, and more total memory will be used. Zero disables\n\
24819 caching, which can slow down startup."),
24821 show_dwarf_max_cache_age,
24822 &set_dwarf_cmdlist,
24823 &show_dwarf_cmdlist);
24825 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24826 &dwarf_always_disassemble, _("\
24827 Set whether `info address' always disassembles DWARF expressions."), _("\
24828 Show whether `info address' always disassembles DWARF expressions."), _("\
24829 When enabled, DWARF expressions are always printed in an assembly-like\n\
24830 syntax. When disabled, expressions will be printed in a more\n\
24831 conversational style, when possible."),
24833 show_dwarf_always_disassemble,
24834 &set_dwarf_cmdlist,
24835 &show_dwarf_cmdlist);
24837 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24838 Set debugging of the DWARF reader."), _("\
24839 Show debugging of the DWARF reader."), _("\
24840 When enabled (non-zero), debugging messages are printed during DWARF\n\
24841 reading and symtab expansion. A value of 1 (one) provides basic\n\
24842 information. A value greater than 1 provides more verbose information."),
24845 &setdebuglist, &showdebuglist);
24847 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24848 Set debugging of the DWARF DIE reader."), _("\
24849 Show debugging of the DWARF DIE reader."), _("\
24850 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24851 The value is the maximum depth to print."),
24854 &setdebuglist, &showdebuglist);
24856 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24857 Set debugging of the dwarf line reader."), _("\
24858 Show debugging of the dwarf line reader."), _("\
24859 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24860 A value of 1 (one) provides basic information.\n\
24861 A value greater than 1 provides more verbose information."),
24864 &setdebuglist, &showdebuglist);
24866 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24867 Set cross-checking of \"physname\" code against demangler."), _("\
24868 Show cross-checking of \"physname\" code against demangler."), _("\
24869 When enabled, GDB's internal \"physname\" code is checked against\n\
24871 NULL, show_check_physname,
24872 &setdebuglist, &showdebuglist);
24874 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24875 no_class, &use_deprecated_index_sections, _("\
24876 Set whether to use deprecated gdb_index sections."), _("\
24877 Show whether to use deprecated gdb_index sections."), _("\
24878 When enabled, deprecated .gdb_index sections are used anyway.\n\
24879 Normally they are ignored either because of a missing feature or\n\
24880 performance issue.\n\
24881 Warning: This option must be enabled before gdb reads the file."),
24884 &setlist, &showlist);
24886 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24888 Save a gdb-index file.\n\
24889 Usage: save gdb-index DIRECTORY"),
24891 set_cmd_completer (c, filename_completer);
24893 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24894 &dwarf2_locexpr_funcs);
24895 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24896 &dwarf2_loclist_funcs);
24898 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24899 &dwarf2_block_frame_base_locexpr_funcs);
24900 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24901 &dwarf2_block_frame_base_loclist_funcs);
24904 selftests::register_test ("dw2_expand_symtabs_matching",
24905 selftests::dw2_expand_symtabs_matching::run_test);