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>
87 #include <forward_list>
89 typedef struct symbol *symbolp;
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
97 /* When non-zero, dump DIEs after they are read in. */
98 static unsigned int dwarf_die_debug = 0;
100 /* When non-zero, dump line number entries as they are read in. */
101 static unsigned int dwarf_line_debug = 0;
103 /* When non-zero, cross-check physname against demangler. */
104 static int check_physname = 0;
106 /* When non-zero, do not reject deprecated .gdb_index sections. */
107 static int use_deprecated_index_sections = 0;
109 static const struct objfile_data *dwarf2_objfile_data_key;
111 /* The "aclass" indices for various kinds of computed DWARF symbols. */
113 static int dwarf2_locexpr_index;
114 static int dwarf2_loclist_index;
115 static int dwarf2_locexpr_block_index;
116 static int dwarf2_loclist_block_index;
118 /* A descriptor for dwarf sections.
120 S.ASECTION, SIZE are typically initialized when the objfile is first
121 scanned. BUFFER, READIN are filled in later when the section is read.
122 If the section contained compressed data then SIZE is updated to record
123 the uncompressed size of the section.
125 DWP file format V2 introduces a wrinkle that is easiest to handle by
126 creating the concept of virtual sections contained within a real section.
127 In DWP V2 the sections of the input DWO files are concatenated together
128 into one section, but section offsets are kept relative to the original
130 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
131 the real section this "virtual" section is contained in, and BUFFER,SIZE
132 describe the virtual section. */
134 struct dwarf2_section_info
138 /* If this is a real section, the bfd section. */
140 /* If this is a virtual section, pointer to the containing ("real")
142 struct dwarf2_section_info *containing_section;
144 /* Pointer to section data, only valid if readin. */
145 const gdb_byte *buffer;
146 /* The size of the section, real or virtual. */
148 /* If this is a virtual section, the offset in the real section.
149 Only valid if is_virtual. */
150 bfd_size_type virtual_offset;
151 /* True if we have tried to read this section. */
153 /* True if this is a virtual section, False otherwise.
154 This specifies which of s.section and s.containing_section to use. */
158 typedef struct dwarf2_section_info dwarf2_section_info_def;
159 DEF_VEC_O (dwarf2_section_info_def);
161 /* All offsets in the index are of this type. It must be
162 architecture-independent. */
163 typedef uint32_t offset_type;
165 DEF_VEC_I (offset_type);
167 /* Ensure only legit values are used. */
168 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
170 gdb_assert ((unsigned int) (value) <= 1); \
171 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
174 /* Ensure only legit values are used. */
175 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
177 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
178 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
179 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
182 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
183 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
185 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
186 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
191 /* Convert VALUE between big- and little-endian. */
194 byte_swap (offset_type value)
198 result = (value & 0xff) << 24;
199 result |= (value & 0xff00) << 8;
200 result |= (value & 0xff0000) >> 8;
201 result |= (value & 0xff000000) >> 24;
205 #define MAYBE_SWAP(V) byte_swap (V)
208 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
209 #endif /* WORDS_BIGENDIAN */
211 /* An index into a (C++) symbol name component in a symbol name as
212 recorded in the mapped_index's symbol table. For each C++ symbol
213 in the symbol table, we record one entry for the start of each
214 component in the symbol in a table of name components, and then
215 sort the table, in order to be able to binary search symbol names,
216 ignoring leading namespaces, both completion and regular look up.
217 For example, for symbol "A::B::C", we'll have an entry that points
218 to "A::B::C", another that points to "B::C", and another for "C".
219 Note that function symbols in GDB index have no parameter
220 information, just the function/method names. You can convert a
221 name_component to a "const char *" using the
222 'mapped_index::symbol_name_at(offset_type)' method. */
224 struct name_component
226 /* Offset in the symbol name where the component starts. Stored as
227 a (32-bit) offset instead of a pointer to save memory and improve
228 locality on 64-bit architectures. */
229 offset_type name_offset;
231 /* The symbol's index in the symbol and constant pool tables of a
236 /* A description of the mapped index. The file format is described in
237 a comment by the code that writes the index. */
240 /* Index data format version. */
243 /* The total length of the buffer. */
246 /* A pointer to the address table data. */
247 const gdb_byte *address_table;
249 /* Size of the address table data in bytes. */
250 offset_type address_table_size;
252 /* The symbol table, implemented as a hash table. */
253 const offset_type *symbol_table;
255 /* Size in slots, each slot is 2 offset_types. */
256 offset_type symbol_table_slots;
258 /* A pointer to the constant pool. */
259 const char *constant_pool;
261 /* The name_component table (a sorted vector). See name_component's
262 description above. */
263 std::vector<name_component> name_components;
265 /* How NAME_COMPONENTS is sorted. */
266 enum case_sensitivity name_components_casing;
268 /* Convenience method to get at the name of the symbol at IDX in the
270 const char *symbol_name_at (offset_type idx) const
271 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx]); }
273 /* Build the symbol name component sorted vector, if we haven't
275 void build_name_components ();
277 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
278 possible matches for LN_NO_PARAMS in the name component
280 std::pair<std::vector<name_component>::const_iterator,
281 std::vector<name_component>::const_iterator>
282 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
285 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
286 DEF_VEC_P (dwarf2_per_cu_ptr);
290 int nr_uniq_abbrev_tables;
292 int nr_symtab_sharers;
293 int nr_stmt_less_type_units;
294 int nr_all_type_units_reallocs;
297 /* Collection of data recorded per objfile.
298 This hangs off of dwarf2_objfile_data_key. */
300 struct dwarf2_per_objfile
302 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
303 dwarf2 section names, or is NULL if the standard ELF names are
305 dwarf2_per_objfile (struct objfile *objfile,
306 const dwarf2_debug_sections *names);
308 ~dwarf2_per_objfile ();
310 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
312 /* Free all cached compilation units. */
313 void free_cached_comp_units ();
315 /* This function is mapped across the sections and remembers the
316 offset and size of each of the debugging sections we are
318 void locate_sections (bfd *abfd, asection *sectp,
319 const dwarf2_debug_sections &names);
322 dwarf2_section_info info {};
323 dwarf2_section_info abbrev {};
324 dwarf2_section_info line {};
325 dwarf2_section_info loc {};
326 dwarf2_section_info loclists {};
327 dwarf2_section_info macinfo {};
328 dwarf2_section_info macro {};
329 dwarf2_section_info str {};
330 dwarf2_section_info line_str {};
331 dwarf2_section_info ranges {};
332 dwarf2_section_info rnglists {};
333 dwarf2_section_info addr {};
334 dwarf2_section_info frame {};
335 dwarf2_section_info eh_frame {};
336 dwarf2_section_info gdb_index {};
338 VEC (dwarf2_section_info_def) *types = NULL;
341 struct objfile *objfile = NULL;
343 /* Table of all the compilation units. This is used to locate
344 the target compilation unit of a particular reference. */
345 struct dwarf2_per_cu_data **all_comp_units = NULL;
347 /* The number of compilation units in ALL_COMP_UNITS. */
348 int n_comp_units = 0;
350 /* The number of .debug_types-related CUs. */
351 int n_type_units = 0;
353 /* The number of elements allocated in all_type_units.
354 If there are skeleton-less TUs, we add them to all_type_units lazily. */
355 int n_allocated_type_units = 0;
357 /* The .debug_types-related CUs (TUs).
358 This is stored in malloc space because we may realloc it. */
359 struct signatured_type **all_type_units = NULL;
361 /* Table of struct type_unit_group objects.
362 The hash key is the DW_AT_stmt_list value. */
363 htab_t type_unit_groups {};
365 /* A table mapping .debug_types signatures to its signatured_type entry.
366 This is NULL if the .debug_types section hasn't been read in yet. */
367 htab_t signatured_types {};
369 /* Type unit statistics, to see how well the scaling improvements
371 struct tu_stats tu_stats {};
373 /* A chain of compilation units that are currently read in, so that
374 they can be freed later. */
375 dwarf2_per_cu_data *read_in_chain = NULL;
377 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
378 This is NULL if the table hasn't been allocated yet. */
381 /* True if we've checked for whether there is a DWP file. */
382 bool dwp_checked = false;
384 /* The DWP file if there is one, or NULL. */
385 struct dwp_file *dwp_file = NULL;
387 /* The shared '.dwz' file, if one exists. This is used when the
388 original data was compressed using 'dwz -m'. */
389 struct dwz_file *dwz_file = NULL;
391 /* A flag indicating whether this objfile has a section loaded at a
393 bool has_section_at_zero = false;
395 /* True if we are using the mapped index,
396 or we are faking it for OBJF_READNOW's sake. */
397 bool using_index = false;
399 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
400 mapped_index *index_table = NULL;
402 /* When using index_table, this keeps track of all quick_file_names entries.
403 TUs typically share line table entries with a CU, so we maintain a
404 separate table of all line table entries to support the sharing.
405 Note that while there can be way more TUs than CUs, we've already
406 sorted all the TUs into "type unit groups", grouped by their
407 DW_AT_stmt_list value. Therefore the only sharing done here is with a
408 CU and its associated TU group if there is one. */
409 htab_t quick_file_names_table {};
411 /* Set during partial symbol reading, to prevent queueing of full
413 bool reading_partial_symbols = false;
415 /* Table mapping type DIEs to their struct type *.
416 This is NULL if not allocated yet.
417 The mapping is done via (CU/TU + DIE offset) -> type. */
418 htab_t die_type_hash {};
420 /* The CUs we recently read. */
421 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
423 /* Table containing line_header indexed by offset and offset_in_dwz. */
424 htab_t line_header_hash {};
426 /* Table containing all filenames. This is an optional because the
427 table is lazily constructed on first access. */
428 gdb::optional<filename_seen_cache> filenames_cache;
431 static struct dwarf2_per_objfile *dwarf2_per_objfile;
433 /* Default names of the debugging sections. */
435 /* Note that if the debugging section has been compressed, it might
436 have a name like .zdebug_info. */
438 static const struct dwarf2_debug_sections dwarf2_elf_names =
440 { ".debug_info", ".zdebug_info" },
441 { ".debug_abbrev", ".zdebug_abbrev" },
442 { ".debug_line", ".zdebug_line" },
443 { ".debug_loc", ".zdebug_loc" },
444 { ".debug_loclists", ".zdebug_loclists" },
445 { ".debug_macinfo", ".zdebug_macinfo" },
446 { ".debug_macro", ".zdebug_macro" },
447 { ".debug_str", ".zdebug_str" },
448 { ".debug_line_str", ".zdebug_line_str" },
449 { ".debug_ranges", ".zdebug_ranges" },
450 { ".debug_rnglists", ".zdebug_rnglists" },
451 { ".debug_types", ".zdebug_types" },
452 { ".debug_addr", ".zdebug_addr" },
453 { ".debug_frame", ".zdebug_frame" },
454 { ".eh_frame", NULL },
455 { ".gdb_index", ".zgdb_index" },
459 /* List of DWO/DWP sections. */
461 static const struct dwop_section_names
463 struct dwarf2_section_names abbrev_dwo;
464 struct dwarf2_section_names info_dwo;
465 struct dwarf2_section_names line_dwo;
466 struct dwarf2_section_names loc_dwo;
467 struct dwarf2_section_names loclists_dwo;
468 struct dwarf2_section_names macinfo_dwo;
469 struct dwarf2_section_names macro_dwo;
470 struct dwarf2_section_names str_dwo;
471 struct dwarf2_section_names str_offsets_dwo;
472 struct dwarf2_section_names types_dwo;
473 struct dwarf2_section_names cu_index;
474 struct dwarf2_section_names tu_index;
478 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
479 { ".debug_info.dwo", ".zdebug_info.dwo" },
480 { ".debug_line.dwo", ".zdebug_line.dwo" },
481 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
482 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
483 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
484 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
485 { ".debug_str.dwo", ".zdebug_str.dwo" },
486 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
487 { ".debug_types.dwo", ".zdebug_types.dwo" },
488 { ".debug_cu_index", ".zdebug_cu_index" },
489 { ".debug_tu_index", ".zdebug_tu_index" },
492 /* local data types */
494 /* The data in a compilation unit header, after target2host
495 translation, looks like this. */
496 struct comp_unit_head
500 unsigned char addr_size;
501 unsigned char signed_addr_p;
502 sect_offset abbrev_sect_off;
504 /* Size of file offsets; either 4 or 8. */
505 unsigned int offset_size;
507 /* Size of the length field; either 4 or 12. */
508 unsigned int initial_length_size;
510 enum dwarf_unit_type unit_type;
512 /* Offset to the first byte of this compilation unit header in the
513 .debug_info section, for resolving relative reference dies. */
514 sect_offset sect_off;
516 /* Offset to first die in this cu from the start of the cu.
517 This will be the first byte following the compilation unit header. */
518 cu_offset first_die_cu_offset;
520 /* 64-bit signature of this type unit - it is valid only for
521 UNIT_TYPE DW_UT_type. */
524 /* For types, offset in the type's DIE of the type defined by this TU. */
525 cu_offset type_cu_offset_in_tu;
528 /* Type used for delaying computation of method physnames.
529 See comments for compute_delayed_physnames. */
530 struct delayed_method_info
532 /* The type to which the method is attached, i.e., its parent class. */
535 /* The index of the method in the type's function fieldlists. */
538 /* The index of the method in the fieldlist. */
541 /* The name of the DIE. */
544 /* The DIE associated with this method. */
545 struct die_info *die;
548 typedef struct delayed_method_info delayed_method_info;
549 DEF_VEC_O (delayed_method_info);
551 /* Internal state when decoding a particular compilation unit. */
554 /* The objfile containing this compilation unit. */
555 struct objfile *objfile;
557 /* The header of the compilation unit. */
558 struct comp_unit_head header;
560 /* Base address of this compilation unit. */
561 CORE_ADDR base_address;
563 /* Non-zero if base_address has been set. */
566 /* The language we are debugging. */
567 enum language language;
568 const struct language_defn *language_defn;
570 const char *producer;
572 /* The generic symbol table building routines have separate lists for
573 file scope symbols and all all other scopes (local scopes). So
574 we need to select the right one to pass to add_symbol_to_list().
575 We do it by keeping a pointer to the correct list in list_in_scope.
577 FIXME: The original dwarf code just treated the file scope as the
578 first local scope, and all other local scopes as nested local
579 scopes, and worked fine. Check to see if we really need to
580 distinguish these in buildsym.c. */
581 struct pending **list_in_scope;
583 /* The abbrev table for this CU.
584 Normally this points to the abbrev table in the objfile.
585 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
586 struct abbrev_table *abbrev_table;
588 /* Hash table holding all the loaded partial DIEs
589 with partial_die->offset.SECT_OFF as hash. */
592 /* Storage for things with the same lifetime as this read-in compilation
593 unit, including partial DIEs. */
594 struct obstack comp_unit_obstack;
596 /* When multiple dwarf2_cu structures are living in memory, this field
597 chains them all together, so that they can be released efficiently.
598 We will probably also want a generation counter so that most-recently-used
599 compilation units are cached... */
600 struct dwarf2_per_cu_data *read_in_chain;
602 /* Backlink to our per_cu entry. */
603 struct dwarf2_per_cu_data *per_cu;
605 /* How many compilation units ago was this CU last referenced? */
608 /* A hash table of DIE cu_offset for following references with
609 die_info->offset.sect_off as hash. */
612 /* Full DIEs if read in. */
613 struct die_info *dies;
615 /* A set of pointers to dwarf2_per_cu_data objects for compilation
616 units referenced by this one. Only set during full symbol processing;
617 partial symbol tables do not have dependencies. */
620 /* Header data from the line table, during full symbol processing. */
621 struct line_header *line_header;
622 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
623 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
624 this is the DW_TAG_compile_unit die for this CU. We'll hold on
625 to the line header as long as this DIE is being processed. See
626 process_die_scope. */
627 die_info *line_header_die_owner;
629 /* A list of methods which need to have physnames computed
630 after all type information has been read. */
631 VEC (delayed_method_info) *method_list;
633 /* To be copied to symtab->call_site_htab. */
634 htab_t call_site_htab;
636 /* Non-NULL if this CU came from a DWO file.
637 There is an invariant here that is important to remember:
638 Except for attributes copied from the top level DIE in the "main"
639 (or "stub") file in preparation for reading the DWO file
640 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
641 Either there isn't a DWO file (in which case this is NULL and the point
642 is moot), or there is and either we're not going to read it (in which
643 case this is NULL) or there is and we are reading it (in which case this
645 struct dwo_unit *dwo_unit;
647 /* The DW_AT_addr_base attribute if present, zero otherwise
648 (zero is a valid value though).
649 Note this value comes from the Fission stub CU/TU's DIE. */
652 /* The DW_AT_ranges_base attribute if present, zero otherwise
653 (zero is a valid value though).
654 Note this value comes from the Fission stub CU/TU's DIE.
655 Also note that the value is zero in the non-DWO case so this value can
656 be used without needing to know whether DWO files are in use or not.
657 N.B. This does not apply to DW_AT_ranges appearing in
658 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
659 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
660 DW_AT_ranges_base *would* have to be applied, and we'd have to care
661 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
662 ULONGEST ranges_base;
664 /* Mark used when releasing cached dies. */
665 unsigned int mark : 1;
667 /* This CU references .debug_loc. See the symtab->locations_valid field.
668 This test is imperfect as there may exist optimized debug code not using
669 any location list and still facing inlining issues if handled as
670 unoptimized code. For a future better test see GCC PR other/32998. */
671 unsigned int has_loclist : 1;
673 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
674 if all the producer_is_* fields are valid. This information is cached
675 because profiling CU expansion showed excessive time spent in
676 producer_is_gxx_lt_4_6. */
677 unsigned int checked_producer : 1;
678 unsigned int producer_is_gxx_lt_4_6 : 1;
679 unsigned int producer_is_gcc_lt_4_3 : 1;
680 unsigned int producer_is_icc_lt_14 : 1;
682 /* When set, the file that we're processing is known to have
683 debugging info for C++ namespaces. GCC 3.3.x did not produce
684 this information, but later versions do. */
686 unsigned int processing_has_namespace_info : 1;
689 /* Persistent data held for a compilation unit, even when not
690 processing it. We put a pointer to this structure in the
691 read_symtab_private field of the psymtab. */
693 struct dwarf2_per_cu_data
695 /* The start offset and length of this compilation unit.
696 NOTE: Unlike comp_unit_head.length, this length includes
698 If the DIE refers to a DWO file, this is always of the original die,
700 sect_offset sect_off;
703 /* DWARF standard version this data has been read from (such as 4 or 5). */
706 /* Flag indicating this compilation unit will be read in before
707 any of the current compilation units are processed. */
708 unsigned int queued : 1;
710 /* This flag will be set when reading partial DIEs if we need to load
711 absolutely all DIEs for this compilation unit, instead of just the ones
712 we think are interesting. It gets set if we look for a DIE in the
713 hash table and don't find it. */
714 unsigned int load_all_dies : 1;
716 /* Non-zero if this CU is from .debug_types.
717 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
719 unsigned int is_debug_types : 1;
721 /* Non-zero if this CU is from the .dwz file. */
722 unsigned int is_dwz : 1;
724 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
725 This flag is only valid if is_debug_types is true.
726 We can't read a CU directly from a DWO file: There are required
727 attributes in the stub. */
728 unsigned int reading_dwo_directly : 1;
730 /* Non-zero if the TU has been read.
731 This is used to assist the "Stay in DWO Optimization" for Fission:
732 When reading a DWO, it's faster to read TUs from the DWO instead of
733 fetching them from random other DWOs (due to comdat folding).
734 If the TU has already been read, the optimization is unnecessary
735 (and unwise - we don't want to change where gdb thinks the TU lives
737 This flag is only valid if is_debug_types is true. */
738 unsigned int tu_read : 1;
740 /* The section this CU/TU lives in.
741 If the DIE refers to a DWO file, this is always the original die,
743 struct dwarf2_section_info *section;
745 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
746 of the CU cache it gets reset to NULL again. This is left as NULL for
747 dummy CUs (a CU header, but nothing else). */
748 struct dwarf2_cu *cu;
750 /* The corresponding objfile.
751 Normally we can get the objfile from dwarf2_per_objfile.
752 However we can enter this file with just a "per_cu" handle. */
753 struct objfile *objfile;
755 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
756 is active. Otherwise, the 'psymtab' field is active. */
759 /* The partial symbol table associated with this compilation unit,
760 or NULL for unread partial units. */
761 struct partial_symtab *psymtab;
763 /* Data needed by the "quick" functions. */
764 struct dwarf2_per_cu_quick_data *quick;
767 /* The CUs we import using DW_TAG_imported_unit. This is filled in
768 while reading psymtabs, used to compute the psymtab dependencies,
769 and then cleared. Then it is filled in again while reading full
770 symbols, and only deleted when the objfile is destroyed.
772 This is also used to work around a difference between the way gold
773 generates .gdb_index version <=7 and the way gdb does. Arguably this
774 is a gold bug. For symbols coming from TUs, gold records in the index
775 the CU that includes the TU instead of the TU itself. This breaks
776 dw2_lookup_symbol: It assumes that if the index says symbol X lives
777 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
778 will find X. Alas TUs live in their own symtab, so after expanding CU Y
779 we need to look in TU Z to find X. Fortunately, this is akin to
780 DW_TAG_imported_unit, so we just use the same mechanism: For
781 .gdb_index version <=7 this also records the TUs that the CU referred
782 to. Concurrently with this change gdb was modified to emit version 8
783 indices so we only pay a price for gold generated indices.
784 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
785 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
788 /* Entry in the signatured_types hash table. */
790 struct signatured_type
792 /* The "per_cu" object of this type.
793 This struct is used iff per_cu.is_debug_types.
794 N.B.: This is the first member so that it's easy to convert pointers
796 struct dwarf2_per_cu_data per_cu;
798 /* The type's signature. */
801 /* Offset in the TU of the type's DIE, as read from the TU header.
802 If this TU is a DWO stub and the definition lives in a DWO file
803 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
804 cu_offset type_offset_in_tu;
806 /* Offset in the section of the type's DIE.
807 If the definition lives in a DWO file, this is the offset in the
808 .debug_types.dwo section.
809 The value is zero until the actual value is known.
810 Zero is otherwise not a valid section offset. */
811 sect_offset type_offset_in_section;
813 /* Type units are grouped by their DW_AT_stmt_list entry so that they
814 can share them. This points to the containing symtab. */
815 struct type_unit_group *type_unit_group;
818 The first time we encounter this type we fully read it in and install it
819 in the symbol tables. Subsequent times we only need the type. */
822 /* Containing DWO unit.
823 This field is valid iff per_cu.reading_dwo_directly. */
824 struct dwo_unit *dwo_unit;
827 typedef struct signatured_type *sig_type_ptr;
828 DEF_VEC_P (sig_type_ptr);
830 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
831 This includes type_unit_group and quick_file_names. */
833 struct stmt_list_hash
835 /* The DWO unit this table is from or NULL if there is none. */
836 struct dwo_unit *dwo_unit;
838 /* Offset in .debug_line or .debug_line.dwo. */
839 sect_offset line_sect_off;
842 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
843 an object of this type. */
845 struct type_unit_group
847 /* dwarf2read.c's main "handle" on a TU symtab.
848 To simplify things we create an artificial CU that "includes" all the
849 type units using this stmt_list so that the rest of the code still has
850 a "per_cu" handle on the symtab.
851 This PER_CU is recognized by having no section. */
852 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
853 struct dwarf2_per_cu_data per_cu;
855 /* The TUs that share this DW_AT_stmt_list entry.
856 This is added to while parsing type units to build partial symtabs,
857 and is deleted afterwards and not used again. */
858 VEC (sig_type_ptr) *tus;
860 /* The compunit symtab.
861 Type units in a group needn't all be defined in the same source file,
862 so we create an essentially anonymous symtab as the compunit symtab. */
863 struct compunit_symtab *compunit_symtab;
865 /* The data used to construct the hash key. */
866 struct stmt_list_hash hash;
868 /* The number of symtabs from the line header.
869 The value here must match line_header.num_file_names. */
870 unsigned int num_symtabs;
872 /* The symbol tables for this TU (obtained from the files listed in
874 WARNING: The order of entries here must match the order of entries
875 in the line header. After the first TU using this type_unit_group, the
876 line header for the subsequent TUs is recreated from this. This is done
877 because we need to use the same symtabs for each TU using the same
878 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
879 there's no guarantee the line header doesn't have duplicate entries. */
880 struct symtab **symtabs;
883 /* These sections are what may appear in a (real or virtual) DWO file. */
887 struct dwarf2_section_info abbrev;
888 struct dwarf2_section_info line;
889 struct dwarf2_section_info loc;
890 struct dwarf2_section_info loclists;
891 struct dwarf2_section_info macinfo;
892 struct dwarf2_section_info macro;
893 struct dwarf2_section_info str;
894 struct dwarf2_section_info str_offsets;
895 /* In the case of a virtual DWO file, these two are unused. */
896 struct dwarf2_section_info info;
897 VEC (dwarf2_section_info_def) *types;
900 /* CUs/TUs in DWP/DWO files. */
904 /* Backlink to the containing struct dwo_file. */
905 struct dwo_file *dwo_file;
907 /* The "id" that distinguishes this CU/TU.
908 .debug_info calls this "dwo_id", .debug_types calls this "signature".
909 Since signatures came first, we stick with it for consistency. */
912 /* The section this CU/TU lives in, in the DWO file. */
913 struct dwarf2_section_info *section;
915 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
916 sect_offset sect_off;
919 /* For types, offset in the type's DIE of the type defined by this TU. */
920 cu_offset type_offset_in_tu;
923 /* include/dwarf2.h defines the DWP section codes.
924 It defines a max value but it doesn't define a min value, which we
925 use for error checking, so provide one. */
927 enum dwp_v2_section_ids
932 /* Data for one DWO file.
934 This includes virtual DWO files (a virtual DWO file is a DWO file as it
935 appears in a DWP file). DWP files don't really have DWO files per se -
936 comdat folding of types "loses" the DWO file they came from, and from
937 a high level view DWP files appear to contain a mass of random types.
938 However, to maintain consistency with the non-DWP case we pretend DWP
939 files contain virtual DWO files, and we assign each TU with one virtual
940 DWO file (generally based on the line and abbrev section offsets -
941 a heuristic that seems to work in practice). */
945 /* The DW_AT_GNU_dwo_name attribute.
946 For virtual DWO files the name is constructed from the section offsets
947 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
948 from related CU+TUs. */
949 const char *dwo_name;
951 /* The DW_AT_comp_dir attribute. */
952 const char *comp_dir;
954 /* The bfd, when the file is open. Otherwise this is NULL.
955 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
958 /* The sections that make up this DWO file.
959 Remember that for virtual DWO files in DWP V2, these are virtual
960 sections (for lack of a better name). */
961 struct dwo_sections sections;
963 /* The CUs in the file.
964 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
965 an extension to handle LLVM's Link Time Optimization output (where
966 multiple source files may be compiled into a single object/dwo pair). */
969 /* Table of TUs in the file.
970 Each element is a struct dwo_unit. */
974 /* These sections are what may appear in a DWP file. */
978 /* These are used by both DWP version 1 and 2. */
979 struct dwarf2_section_info str;
980 struct dwarf2_section_info cu_index;
981 struct dwarf2_section_info tu_index;
983 /* These are only used by DWP version 2 files.
984 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
985 sections are referenced by section number, and are not recorded here.
986 In DWP version 2 there is at most one copy of all these sections, each
987 section being (effectively) comprised of the concatenation of all of the
988 individual sections that exist in the version 1 format.
989 To keep the code simple we treat each of these concatenated pieces as a
990 section itself (a virtual section?). */
991 struct dwarf2_section_info abbrev;
992 struct dwarf2_section_info info;
993 struct dwarf2_section_info line;
994 struct dwarf2_section_info loc;
995 struct dwarf2_section_info macinfo;
996 struct dwarf2_section_info macro;
997 struct dwarf2_section_info str_offsets;
998 struct dwarf2_section_info types;
1001 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1002 A virtual DWO file is a DWO file as it appears in a DWP file. */
1004 struct virtual_v1_dwo_sections
1006 struct dwarf2_section_info abbrev;
1007 struct dwarf2_section_info line;
1008 struct dwarf2_section_info loc;
1009 struct dwarf2_section_info macinfo;
1010 struct dwarf2_section_info macro;
1011 struct dwarf2_section_info str_offsets;
1012 /* Each DWP hash table entry records one CU or one TU.
1013 That is recorded here, and copied to dwo_unit.section. */
1014 struct dwarf2_section_info info_or_types;
1017 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1018 In version 2, the sections of the DWO files are concatenated together
1019 and stored in one section of that name. Thus each ELF section contains
1020 several "virtual" sections. */
1022 struct virtual_v2_dwo_sections
1024 bfd_size_type abbrev_offset;
1025 bfd_size_type abbrev_size;
1027 bfd_size_type line_offset;
1028 bfd_size_type line_size;
1030 bfd_size_type loc_offset;
1031 bfd_size_type loc_size;
1033 bfd_size_type macinfo_offset;
1034 bfd_size_type macinfo_size;
1036 bfd_size_type macro_offset;
1037 bfd_size_type macro_size;
1039 bfd_size_type str_offsets_offset;
1040 bfd_size_type str_offsets_size;
1042 /* Each DWP hash table entry records one CU or one TU.
1043 That is recorded here, and copied to dwo_unit.section. */
1044 bfd_size_type info_or_types_offset;
1045 bfd_size_type info_or_types_size;
1048 /* Contents of DWP hash tables. */
1050 struct dwp_hash_table
1052 uint32_t version, nr_columns;
1053 uint32_t nr_units, nr_slots;
1054 const gdb_byte *hash_table, *unit_table;
1059 const gdb_byte *indices;
1063 /* This is indexed by column number and gives the id of the section
1065 #define MAX_NR_V2_DWO_SECTIONS \
1066 (1 /* .debug_info or .debug_types */ \
1067 + 1 /* .debug_abbrev */ \
1068 + 1 /* .debug_line */ \
1069 + 1 /* .debug_loc */ \
1070 + 1 /* .debug_str_offsets */ \
1071 + 1 /* .debug_macro or .debug_macinfo */)
1072 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1073 const gdb_byte *offsets;
1074 const gdb_byte *sizes;
1079 /* Data for one DWP file. */
1083 /* Name of the file. */
1086 /* File format version. */
1092 /* Section info for this file. */
1093 struct dwp_sections sections;
1095 /* Table of CUs in the file. */
1096 const struct dwp_hash_table *cus;
1098 /* Table of TUs in the file. */
1099 const struct dwp_hash_table *tus;
1101 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1105 /* Table to map ELF section numbers to their sections.
1106 This is only needed for the DWP V1 file format. */
1107 unsigned int num_sections;
1108 asection **elf_sections;
1111 /* This represents a '.dwz' file. */
1115 /* A dwz file can only contain a few sections. */
1116 struct dwarf2_section_info abbrev;
1117 struct dwarf2_section_info info;
1118 struct dwarf2_section_info str;
1119 struct dwarf2_section_info line;
1120 struct dwarf2_section_info macro;
1121 struct dwarf2_section_info gdb_index;
1123 /* The dwz's BFD. */
1127 /* Struct used to pass misc. parameters to read_die_and_children, et
1128 al. which are used for both .debug_info and .debug_types dies.
1129 All parameters here are unchanging for the life of the call. This
1130 struct exists to abstract away the constant parameters of die reading. */
1132 struct die_reader_specs
1134 /* The bfd of die_section. */
1137 /* The CU of the DIE we are parsing. */
1138 struct dwarf2_cu *cu;
1140 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1141 struct dwo_file *dwo_file;
1143 /* The section the die comes from.
1144 This is either .debug_info or .debug_types, or the .dwo variants. */
1145 struct dwarf2_section_info *die_section;
1147 /* die_section->buffer. */
1148 const gdb_byte *buffer;
1150 /* The end of the buffer. */
1151 const gdb_byte *buffer_end;
1153 /* The value of the DW_AT_comp_dir attribute. */
1154 const char *comp_dir;
1157 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1158 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1159 const gdb_byte *info_ptr,
1160 struct die_info *comp_unit_die,
1164 /* A 1-based directory index. This is a strong typedef to prevent
1165 accidentally using a directory index as a 0-based index into an
1167 enum class dir_index : unsigned int {};
1169 /* Likewise, a 1-based file name index. */
1170 enum class file_name_index : unsigned int {};
1174 file_entry () = default;
1176 file_entry (const char *name_, dir_index d_index_,
1177 unsigned int mod_time_, unsigned int length_)
1180 mod_time (mod_time_),
1184 /* Return the include directory at D_INDEX stored in LH. Returns
1185 NULL if D_INDEX is out of bounds. */
1186 const char *include_dir (const line_header *lh) const;
1188 /* The file name. Note this is an observing pointer. The memory is
1189 owned by debug_line_buffer. */
1190 const char *name {};
1192 /* The directory index (1-based). */
1193 dir_index d_index {};
1195 unsigned int mod_time {};
1197 unsigned int length {};
1199 /* True if referenced by the Line Number Program. */
1202 /* The associated symbol table, if any. */
1203 struct symtab *symtab {};
1206 /* The line number information for a compilation unit (found in the
1207 .debug_line section) begins with a "statement program header",
1208 which contains the following information. */
1215 /* Add an entry to the include directory table. */
1216 void add_include_dir (const char *include_dir);
1218 /* Add an entry to the file name table. */
1219 void add_file_name (const char *name, dir_index d_index,
1220 unsigned int mod_time, unsigned int length);
1222 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1223 is out of bounds. */
1224 const char *include_dir_at (dir_index index) const
1226 /* Convert directory index number (1-based) to vector index
1228 size_t vec_index = to_underlying (index) - 1;
1230 if (vec_index >= include_dirs.size ())
1232 return include_dirs[vec_index];
1235 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1236 is out of bounds. */
1237 file_entry *file_name_at (file_name_index index)
1239 /* Convert file name index number (1-based) to vector index
1241 size_t vec_index = to_underlying (index) - 1;
1243 if (vec_index >= file_names.size ())
1245 return &file_names[vec_index];
1248 /* Const version of the above. */
1249 const file_entry *file_name_at (unsigned int index) const
1251 if (index >= file_names.size ())
1253 return &file_names[index];
1256 /* Offset of line number information in .debug_line section. */
1257 sect_offset sect_off {};
1259 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1260 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1262 unsigned int total_length {};
1263 unsigned short version {};
1264 unsigned int header_length {};
1265 unsigned char minimum_instruction_length {};
1266 unsigned char maximum_ops_per_instruction {};
1267 unsigned char default_is_stmt {};
1269 unsigned char line_range {};
1270 unsigned char opcode_base {};
1272 /* standard_opcode_lengths[i] is the number of operands for the
1273 standard opcode whose value is i. This means that
1274 standard_opcode_lengths[0] is unused, and the last meaningful
1275 element is standard_opcode_lengths[opcode_base - 1]. */
1276 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1278 /* The include_directories table. Note these are observing
1279 pointers. The memory is owned by debug_line_buffer. */
1280 std::vector<const char *> include_dirs;
1282 /* The file_names table. */
1283 std::vector<file_entry> file_names;
1285 /* The start and end of the statement program following this
1286 header. These point into dwarf2_per_objfile->line_buffer. */
1287 const gdb_byte *statement_program_start {}, *statement_program_end {};
1290 typedef std::unique_ptr<line_header> line_header_up;
1293 file_entry::include_dir (const line_header *lh) const
1295 return lh->include_dir_at (d_index);
1298 /* When we construct a partial symbol table entry we only
1299 need this much information. */
1300 struct partial_die_info
1302 /* Offset of this DIE. */
1303 sect_offset sect_off;
1305 /* DWARF-2 tag for this DIE. */
1306 ENUM_BITFIELD(dwarf_tag) tag : 16;
1308 /* Assorted flags describing the data found in this DIE. */
1309 unsigned int has_children : 1;
1310 unsigned int is_external : 1;
1311 unsigned int is_declaration : 1;
1312 unsigned int has_type : 1;
1313 unsigned int has_specification : 1;
1314 unsigned int has_pc_info : 1;
1315 unsigned int may_be_inlined : 1;
1317 /* This DIE has been marked DW_AT_main_subprogram. */
1318 unsigned int main_subprogram : 1;
1320 /* Flag set if the SCOPE field of this structure has been
1322 unsigned int scope_set : 1;
1324 /* Flag set if the DIE has a byte_size attribute. */
1325 unsigned int has_byte_size : 1;
1327 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1328 unsigned int has_const_value : 1;
1330 /* Flag set if any of the DIE's children are template arguments. */
1331 unsigned int has_template_arguments : 1;
1333 /* Flag set if fixup_partial_die has been called on this die. */
1334 unsigned int fixup_called : 1;
1336 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1337 unsigned int is_dwz : 1;
1339 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1340 unsigned int spec_is_dwz : 1;
1342 /* The name of this DIE. Normally the value of DW_AT_name, but
1343 sometimes a default name for unnamed DIEs. */
1346 /* The linkage name, if present. */
1347 const char *linkage_name;
1349 /* The scope to prepend to our children. This is generally
1350 allocated on the comp_unit_obstack, so will disappear
1351 when this compilation unit leaves the cache. */
1354 /* Some data associated with the partial DIE. The tag determines
1355 which field is live. */
1358 /* The location description associated with this DIE, if any. */
1359 struct dwarf_block *locdesc;
1360 /* The offset of an import, for DW_TAG_imported_unit. */
1361 sect_offset sect_off;
1364 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1368 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1369 DW_AT_sibling, if any. */
1370 /* NOTE: This member isn't strictly necessary, read_partial_die could
1371 return DW_AT_sibling values to its caller load_partial_dies. */
1372 const gdb_byte *sibling;
1374 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1375 DW_AT_specification (or DW_AT_abstract_origin or
1376 DW_AT_extension). */
1377 sect_offset spec_offset;
1379 /* Pointers to this DIE's parent, first child, and next sibling,
1381 struct partial_die_info *die_parent, *die_child, *die_sibling;
1384 /* This data structure holds the information of an abbrev. */
1387 unsigned int number; /* number identifying abbrev */
1388 enum dwarf_tag tag; /* dwarf tag */
1389 unsigned short has_children; /* boolean */
1390 unsigned short num_attrs; /* number of attributes */
1391 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1392 struct abbrev_info *next; /* next in chain */
1397 ENUM_BITFIELD(dwarf_attribute) name : 16;
1398 ENUM_BITFIELD(dwarf_form) form : 16;
1400 /* It is valid only if FORM is DW_FORM_implicit_const. */
1401 LONGEST implicit_const;
1404 /* Size of abbrev_table.abbrev_hash_table. */
1405 #define ABBREV_HASH_SIZE 121
1407 /* Top level data structure to contain an abbreviation table. */
1411 /* Where the abbrev table came from.
1412 This is used as a sanity check when the table is used. */
1413 sect_offset sect_off;
1415 /* Storage for the abbrev table. */
1416 struct obstack abbrev_obstack;
1418 /* Hash table of abbrevs.
1419 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1420 It could be statically allocated, but the previous code didn't so we
1422 struct abbrev_info **abbrevs;
1425 /* Attributes have a name and a value. */
1428 ENUM_BITFIELD(dwarf_attribute) name : 16;
1429 ENUM_BITFIELD(dwarf_form) form : 15;
1431 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1432 field should be in u.str (existing only for DW_STRING) but it is kept
1433 here for better struct attribute alignment. */
1434 unsigned int string_is_canonical : 1;
1439 struct dwarf_block *blk;
1448 /* This data structure holds a complete die structure. */
1451 /* DWARF-2 tag for this DIE. */
1452 ENUM_BITFIELD(dwarf_tag) tag : 16;
1454 /* Number of attributes */
1455 unsigned char num_attrs;
1457 /* True if we're presently building the full type name for the
1458 type derived from this DIE. */
1459 unsigned char building_fullname : 1;
1461 /* True if this die is in process. PR 16581. */
1462 unsigned char in_process : 1;
1465 unsigned int abbrev;
1467 /* Offset in .debug_info or .debug_types section. */
1468 sect_offset sect_off;
1470 /* The dies in a compilation unit form an n-ary tree. PARENT
1471 points to this die's parent; CHILD points to the first child of
1472 this node; and all the children of a given node are chained
1473 together via their SIBLING fields. */
1474 struct die_info *child; /* Its first child, if any. */
1475 struct die_info *sibling; /* Its next sibling, if any. */
1476 struct die_info *parent; /* Its parent, if any. */
1478 /* An array of attributes, with NUM_ATTRS elements. There may be
1479 zero, but it's not common and zero-sized arrays are not
1480 sufficiently portable C. */
1481 struct attribute attrs[1];
1484 /* Get at parts of an attribute structure. */
1486 #define DW_STRING(attr) ((attr)->u.str)
1487 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1488 #define DW_UNSND(attr) ((attr)->u.unsnd)
1489 #define DW_BLOCK(attr) ((attr)->u.blk)
1490 #define DW_SND(attr) ((attr)->u.snd)
1491 #define DW_ADDR(attr) ((attr)->u.addr)
1492 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1494 /* Blocks are a bunch of untyped bytes. */
1499 /* Valid only if SIZE is not zero. */
1500 const gdb_byte *data;
1503 #ifndef ATTR_ALLOC_CHUNK
1504 #define ATTR_ALLOC_CHUNK 4
1507 /* Allocate fields for structs, unions and enums in this size. */
1508 #ifndef DW_FIELD_ALLOC_CHUNK
1509 #define DW_FIELD_ALLOC_CHUNK 4
1512 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1513 but this would require a corresponding change in unpack_field_as_long
1515 static int bits_per_byte = 8;
1519 struct nextfield *next;
1527 struct nextfnfield *next;
1528 struct fn_field fnfield;
1535 struct nextfnfield *head;
1538 struct decl_field_list
1540 struct decl_field field;
1541 struct decl_field_list *next;
1544 /* The routines that read and process dies for a C struct or C++ class
1545 pass lists of data member fields and lists of member function fields
1546 in an instance of a field_info structure, as defined below. */
1549 /* List of data member and baseclasses fields. */
1550 struct nextfield *fields, *baseclasses;
1552 /* Number of fields (including baseclasses). */
1555 /* Number of baseclasses. */
1558 /* Set if the accesibility of one of the fields is not public. */
1559 int non_public_fields;
1561 /* Member function fieldlist array, contains name of possibly overloaded
1562 member function, number of overloaded member functions and a pointer
1563 to the head of the member function field chain. */
1564 struct fnfieldlist *fnfieldlists;
1566 /* Number of entries in the fnfieldlists array. */
1569 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1570 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1571 struct decl_field_list *typedef_field_list;
1572 unsigned typedef_field_list_count;
1574 /* Nested types defined by this class and the number of elements in this
1576 struct decl_field_list *nested_types_list;
1577 unsigned nested_types_list_count;
1580 /* One item on the queue of compilation units to read in full symbols
1582 struct dwarf2_queue_item
1584 struct dwarf2_per_cu_data *per_cu;
1585 enum language pretend_language;
1586 struct dwarf2_queue_item *next;
1589 /* The current queue. */
1590 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1592 /* Loaded secondary compilation units are kept in memory until they
1593 have not been referenced for the processing of this many
1594 compilation units. Set this to zero to disable caching. Cache
1595 sizes of up to at least twenty will improve startup time for
1596 typical inter-CU-reference binaries, at an obvious memory cost. */
1597 static int dwarf_max_cache_age = 5;
1599 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1600 struct cmd_list_element *c, const char *value)
1602 fprintf_filtered (file, _("The upper bound on the age of cached "
1603 "DWARF compilation units is %s.\n"),
1607 /* local function prototypes */
1609 static const char *get_section_name (const struct dwarf2_section_info *);
1611 static const char *get_section_file_name (const struct dwarf2_section_info *);
1613 static void dwarf2_find_base_address (struct die_info *die,
1614 struct dwarf2_cu *cu);
1616 static struct partial_symtab *create_partial_symtab
1617 (struct dwarf2_per_cu_data *per_cu, const char *name);
1619 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1620 const gdb_byte *info_ptr,
1621 struct die_info *type_unit_die,
1622 int has_children, void *data);
1624 static void dwarf2_build_psymtabs_hard (struct objfile *);
1626 static void scan_partial_symbols (struct partial_die_info *,
1627 CORE_ADDR *, CORE_ADDR *,
1628 int, struct dwarf2_cu *);
1630 static void add_partial_symbol (struct partial_die_info *,
1631 struct dwarf2_cu *);
1633 static void add_partial_namespace (struct partial_die_info *pdi,
1634 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1635 int set_addrmap, struct dwarf2_cu *cu);
1637 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1638 CORE_ADDR *highpc, int set_addrmap,
1639 struct dwarf2_cu *cu);
1641 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1642 struct dwarf2_cu *cu);
1644 static void add_partial_subprogram (struct partial_die_info *pdi,
1645 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1646 int need_pc, struct dwarf2_cu *cu);
1648 static void dwarf2_read_symtab (struct partial_symtab *,
1651 static void psymtab_to_symtab_1 (struct partial_symtab *);
1653 static struct abbrev_info *abbrev_table_lookup_abbrev
1654 (const struct abbrev_table *, unsigned int);
1656 static struct abbrev_table *abbrev_table_read_table
1657 (struct dwarf2_section_info *, sect_offset);
1659 static void abbrev_table_free (struct abbrev_table *);
1661 static void abbrev_table_free_cleanup (void *);
1663 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1664 struct dwarf2_section_info *);
1666 static void dwarf2_free_abbrev_table (void *);
1668 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1670 static struct partial_die_info *load_partial_dies
1671 (const struct die_reader_specs *, const gdb_byte *, int);
1673 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1674 struct partial_die_info *,
1675 struct abbrev_info *,
1679 static struct partial_die_info *find_partial_die (sect_offset, int,
1680 struct dwarf2_cu *);
1682 static void fixup_partial_die (struct partial_die_info *,
1683 struct dwarf2_cu *);
1685 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1686 struct attribute *, struct attr_abbrev *,
1689 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1691 static int read_1_signed_byte (bfd *, const gdb_byte *);
1693 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1695 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1697 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1699 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1702 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1704 static LONGEST read_checked_initial_length_and_offset
1705 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1706 unsigned int *, unsigned int *);
1708 static LONGEST read_offset (bfd *, const gdb_byte *,
1709 const struct comp_unit_head *,
1712 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1714 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1717 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1719 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1721 static const char *read_indirect_string (bfd *, const gdb_byte *,
1722 const struct comp_unit_head *,
1725 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1726 const struct comp_unit_head *,
1729 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1731 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1733 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1737 static const char *read_str_index (const struct die_reader_specs *reader,
1738 ULONGEST str_index);
1740 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1742 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1743 struct dwarf2_cu *);
1745 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1748 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1749 struct dwarf2_cu *cu);
1751 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1752 struct dwarf2_cu *cu);
1754 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1756 static struct die_info *die_specification (struct die_info *die,
1757 struct dwarf2_cu **);
1759 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1760 struct dwarf2_cu *cu);
1762 static void dwarf_decode_lines (struct line_header *, const char *,
1763 struct dwarf2_cu *, struct partial_symtab *,
1764 CORE_ADDR, int decode_mapping);
1766 static void dwarf2_start_subfile (const char *, const char *);
1768 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1769 const char *, const char *,
1772 static struct symbol *new_symbol (struct die_info *, struct type *,
1773 struct dwarf2_cu *);
1775 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1776 struct dwarf2_cu *, struct symbol *);
1778 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1779 struct dwarf2_cu *);
1781 static void dwarf2_const_value_attr (const struct attribute *attr,
1784 struct obstack *obstack,
1785 struct dwarf2_cu *cu, LONGEST *value,
1786 const gdb_byte **bytes,
1787 struct dwarf2_locexpr_baton **baton);
1789 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1791 static int need_gnat_info (struct dwarf2_cu *);
1793 static struct type *die_descriptive_type (struct die_info *,
1794 struct dwarf2_cu *);
1796 static void set_descriptive_type (struct type *, struct die_info *,
1797 struct dwarf2_cu *);
1799 static struct type *die_containing_type (struct die_info *,
1800 struct dwarf2_cu *);
1802 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1803 struct dwarf2_cu *);
1805 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1807 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1809 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1811 static char *typename_concat (struct obstack *obs, const char *prefix,
1812 const char *suffix, int physname,
1813 struct dwarf2_cu *cu);
1815 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1817 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1819 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1821 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1823 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1825 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1827 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1828 struct dwarf2_cu *, struct partial_symtab *);
1830 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1831 values. Keep the items ordered with increasing constraints compliance. */
1834 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1835 PC_BOUNDS_NOT_PRESENT,
1837 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1838 were present but they do not form a valid range of PC addresses. */
1841 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1844 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1848 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1849 CORE_ADDR *, CORE_ADDR *,
1851 struct partial_symtab *);
1853 static void get_scope_pc_bounds (struct die_info *,
1854 CORE_ADDR *, CORE_ADDR *,
1855 struct dwarf2_cu *);
1857 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1858 CORE_ADDR, struct dwarf2_cu *);
1860 static void dwarf2_add_field (struct field_info *, struct die_info *,
1861 struct dwarf2_cu *);
1863 static void dwarf2_attach_fields_to_type (struct field_info *,
1864 struct type *, struct dwarf2_cu *);
1866 static void dwarf2_add_member_fn (struct field_info *,
1867 struct die_info *, struct type *,
1868 struct dwarf2_cu *);
1870 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1872 struct dwarf2_cu *);
1874 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1876 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1878 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1880 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1882 static struct using_direct **using_directives (enum language);
1884 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1886 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1888 static struct type *read_module_type (struct die_info *die,
1889 struct dwarf2_cu *cu);
1891 static const char *namespace_name (struct die_info *die,
1892 int *is_anonymous, struct dwarf2_cu *);
1894 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1896 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1898 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1899 struct dwarf2_cu *);
1901 static struct die_info *read_die_and_siblings_1
1902 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1905 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1906 const gdb_byte *info_ptr,
1907 const gdb_byte **new_info_ptr,
1908 struct die_info *parent);
1910 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1911 struct die_info **, const gdb_byte *,
1914 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1915 struct die_info **, const gdb_byte *,
1918 static void process_die (struct die_info *, struct dwarf2_cu *);
1920 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1923 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1925 static const char *dwarf2_full_name (const char *name,
1926 struct die_info *die,
1927 struct dwarf2_cu *cu);
1929 static const char *dwarf2_physname (const char *name, struct die_info *die,
1930 struct dwarf2_cu *cu);
1932 static struct die_info *dwarf2_extension (struct die_info *die,
1933 struct dwarf2_cu **);
1935 static const char *dwarf_tag_name (unsigned int);
1937 static const char *dwarf_attr_name (unsigned int);
1939 static const char *dwarf_form_name (unsigned int);
1941 static const char *dwarf_bool_name (unsigned int);
1943 static const char *dwarf_type_encoding_name (unsigned int);
1945 static struct die_info *sibling_die (struct die_info *);
1947 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1949 static void dump_die_for_error (struct die_info *);
1951 static void dump_die_1 (struct ui_file *, int level, int max_level,
1954 /*static*/ void dump_die (struct die_info *, int max_level);
1956 static void store_in_ref_table (struct die_info *,
1957 struct dwarf2_cu *);
1959 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1961 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1963 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1964 const struct attribute *,
1965 struct dwarf2_cu **);
1967 static struct die_info *follow_die_ref (struct die_info *,
1968 const struct attribute *,
1969 struct dwarf2_cu **);
1971 static struct die_info *follow_die_sig (struct die_info *,
1972 const struct attribute *,
1973 struct dwarf2_cu **);
1975 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1976 struct dwarf2_cu *);
1978 static struct type *get_DW_AT_signature_type (struct die_info *,
1979 const struct attribute *,
1980 struct dwarf2_cu *);
1982 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1984 static void read_signatured_type (struct signatured_type *);
1986 static int attr_to_dynamic_prop (const struct attribute *attr,
1987 struct die_info *die, struct dwarf2_cu *cu,
1988 struct dynamic_prop *prop);
1990 /* memory allocation interface */
1992 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1994 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1996 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1998 static int attr_form_is_block (const struct attribute *);
2000 static int attr_form_is_section_offset (const struct attribute *);
2002 static int attr_form_is_constant (const struct attribute *);
2004 static int attr_form_is_ref (const struct attribute *);
2006 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2007 struct dwarf2_loclist_baton *baton,
2008 const struct attribute *attr);
2010 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2012 struct dwarf2_cu *cu,
2015 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2016 const gdb_byte *info_ptr,
2017 struct abbrev_info *abbrev);
2019 static void free_stack_comp_unit (void *);
2021 static hashval_t partial_die_hash (const void *item);
2023 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2025 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2026 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2028 static void init_one_comp_unit (struct dwarf2_cu *cu,
2029 struct dwarf2_per_cu_data *per_cu);
2031 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2032 struct die_info *comp_unit_die,
2033 enum language pretend_language);
2035 static void free_heap_comp_unit (void *);
2037 static void free_cached_comp_units (void *);
2039 static void age_cached_comp_units (void);
2041 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2043 static struct type *set_die_type (struct die_info *, struct type *,
2044 struct dwarf2_cu *);
2046 static void create_all_comp_units (struct objfile *);
2048 static int create_all_type_units (struct objfile *);
2050 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2053 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2056 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2059 static void dwarf2_add_dependence (struct dwarf2_cu *,
2060 struct dwarf2_per_cu_data *);
2062 static void dwarf2_mark (struct dwarf2_cu *);
2064 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2066 static struct type *get_die_type_at_offset (sect_offset,
2067 struct dwarf2_per_cu_data *);
2069 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2071 static void dwarf2_release_queue (void *dummy);
2073 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2074 enum language pretend_language);
2076 static void process_queue (void);
2078 /* The return type of find_file_and_directory. Note, the enclosed
2079 string pointers are only valid while this object is valid. */
2081 struct file_and_directory
2083 /* The filename. This is never NULL. */
2086 /* The compilation directory. NULL if not known. If we needed to
2087 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2088 points directly to the DW_AT_comp_dir string attribute owned by
2089 the obstack that owns the DIE. */
2090 const char *comp_dir;
2092 /* If we needed to build a new string for comp_dir, this is what
2093 owns the storage. */
2094 std::string comp_dir_storage;
2097 static file_and_directory find_file_and_directory (struct die_info *die,
2098 struct dwarf2_cu *cu);
2100 static char *file_full_name (int file, struct line_header *lh,
2101 const char *comp_dir);
2103 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2104 enum class rcuh_kind { COMPILE, TYPE };
2106 static const gdb_byte *read_and_check_comp_unit_head
2107 (struct comp_unit_head *header,
2108 struct dwarf2_section_info *section,
2109 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2110 rcuh_kind section_kind);
2112 static void init_cutu_and_read_dies
2113 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2114 int use_existing_cu, int keep,
2115 die_reader_func_ftype *die_reader_func, void *data);
2117 static void init_cutu_and_read_dies_simple
2118 (struct dwarf2_per_cu_data *this_cu,
2119 die_reader_func_ftype *die_reader_func, void *data);
2121 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2123 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2125 static struct dwo_unit *lookup_dwo_unit_in_dwp
2126 (struct dwp_file *dwp_file, const char *comp_dir,
2127 ULONGEST signature, int is_debug_types);
2129 static struct dwp_file *get_dwp_file (void);
2131 static struct dwo_unit *lookup_dwo_comp_unit
2132 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2134 static struct dwo_unit *lookup_dwo_type_unit
2135 (struct signatured_type *, const char *, const char *);
2137 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2139 static void free_dwo_file_cleanup (void *);
2141 static void process_cu_includes (void);
2143 static void check_producer (struct dwarf2_cu *cu);
2145 static void free_line_header_voidp (void *arg);
2147 /* Various complaints about symbol reading that don't abort the process. */
2150 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2152 complaint (&symfile_complaints,
2153 _("statement list doesn't fit in .debug_line section"));
2157 dwarf2_debug_line_missing_file_complaint (void)
2159 complaint (&symfile_complaints,
2160 _(".debug_line section has line data without a file"));
2164 dwarf2_debug_line_missing_end_sequence_complaint (void)
2166 complaint (&symfile_complaints,
2167 _(".debug_line section has line "
2168 "program sequence without an end"));
2172 dwarf2_complex_location_expr_complaint (void)
2174 complaint (&symfile_complaints, _("location expression too complex"));
2178 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2181 complaint (&symfile_complaints,
2182 _("const value length mismatch for '%s', got %d, expected %d"),
2187 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2189 complaint (&symfile_complaints,
2190 _("debug info runs off end of %s section"
2192 get_section_name (section),
2193 get_section_file_name (section));
2197 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2199 complaint (&symfile_complaints,
2200 _("macro debug info contains a "
2201 "malformed macro definition:\n`%s'"),
2206 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2208 complaint (&symfile_complaints,
2209 _("invalid attribute class or form for '%s' in '%s'"),
2213 /* Hash function for line_header_hash. */
2216 line_header_hash (const struct line_header *ofs)
2218 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2221 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2224 line_header_hash_voidp (const void *item)
2226 const struct line_header *ofs = (const struct line_header *) item;
2228 return line_header_hash (ofs);
2231 /* Equality function for line_header_hash. */
2234 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2236 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2237 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2239 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2240 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2245 /* Read the given attribute value as an address, taking the attribute's
2246 form into account. */
2249 attr_value_as_address (struct attribute *attr)
2253 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2255 /* Aside from a few clearly defined exceptions, attributes that
2256 contain an address must always be in DW_FORM_addr form.
2257 Unfortunately, some compilers happen to be violating this
2258 requirement by encoding addresses using other forms, such
2259 as DW_FORM_data4 for example. For those broken compilers,
2260 we try to do our best, without any guarantee of success,
2261 to interpret the address correctly. It would also be nice
2262 to generate a complaint, but that would require us to maintain
2263 a list of legitimate cases where a non-address form is allowed,
2264 as well as update callers to pass in at least the CU's DWARF
2265 version. This is more overhead than what we're willing to
2266 expand for a pretty rare case. */
2267 addr = DW_UNSND (attr);
2270 addr = DW_ADDR (attr);
2275 /* The suffix for an index file. */
2276 #define INDEX4_SUFFIX ".gdb-index"
2277 #define INDEX5_SUFFIX ".debug_names"
2278 #define DEBUG_STR_SUFFIX ".debug_str"
2280 /* See declaration. */
2282 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2283 const dwarf2_debug_sections *names)
2284 : objfile (objfile_)
2287 names = &dwarf2_elf_names;
2289 bfd *obfd = objfile->obfd;
2291 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2292 locate_sections (obfd, sec, *names);
2295 dwarf2_per_objfile::~dwarf2_per_objfile ()
2297 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2298 free_cached_comp_units ();
2300 if (quick_file_names_table)
2301 htab_delete (quick_file_names_table);
2303 if (line_header_hash)
2304 htab_delete (line_header_hash);
2306 /* Everything else should be on the objfile obstack. */
2309 /* See declaration. */
2312 dwarf2_per_objfile::free_cached_comp_units ()
2314 dwarf2_per_cu_data *per_cu = read_in_chain;
2315 dwarf2_per_cu_data **last_chain = &read_in_chain;
2316 while (per_cu != NULL)
2318 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2320 free_heap_comp_unit (per_cu->cu);
2321 *last_chain = next_cu;
2326 /* Try to locate the sections we need for DWARF 2 debugging
2327 information and return true if we have enough to do something.
2328 NAMES points to the dwarf2 section names, or is NULL if the standard
2329 ELF names are used. */
2332 dwarf2_has_info (struct objfile *objfile,
2333 const struct dwarf2_debug_sections *names)
2335 if (objfile->flags & OBJF_READNEVER)
2338 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2339 objfile_data (objfile, dwarf2_objfile_data_key));
2340 if (!dwarf2_per_objfile)
2342 /* Initialize per-objfile state. */
2343 struct dwarf2_per_objfile *data
2344 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2346 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2347 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2349 return (!dwarf2_per_objfile->info.is_virtual
2350 && dwarf2_per_objfile->info.s.section != NULL
2351 && !dwarf2_per_objfile->abbrev.is_virtual
2352 && dwarf2_per_objfile->abbrev.s.section != NULL);
2355 /* Return the containing section of virtual section SECTION. */
2357 static struct dwarf2_section_info *
2358 get_containing_section (const struct dwarf2_section_info *section)
2360 gdb_assert (section->is_virtual);
2361 return section->s.containing_section;
2364 /* Return the bfd owner of SECTION. */
2367 get_section_bfd_owner (const struct dwarf2_section_info *section)
2369 if (section->is_virtual)
2371 section = get_containing_section (section);
2372 gdb_assert (!section->is_virtual);
2374 return section->s.section->owner;
2377 /* Return the bfd section of SECTION.
2378 Returns NULL if the section is not present. */
2381 get_section_bfd_section (const struct dwarf2_section_info *section)
2383 if (section->is_virtual)
2385 section = get_containing_section (section);
2386 gdb_assert (!section->is_virtual);
2388 return section->s.section;
2391 /* Return the name of SECTION. */
2394 get_section_name (const struct dwarf2_section_info *section)
2396 asection *sectp = get_section_bfd_section (section);
2398 gdb_assert (sectp != NULL);
2399 return bfd_section_name (get_section_bfd_owner (section), sectp);
2402 /* Return the name of the file SECTION is in. */
2405 get_section_file_name (const struct dwarf2_section_info *section)
2407 bfd *abfd = get_section_bfd_owner (section);
2409 return bfd_get_filename (abfd);
2412 /* Return the id of SECTION.
2413 Returns 0 if SECTION doesn't exist. */
2416 get_section_id (const struct dwarf2_section_info *section)
2418 asection *sectp = get_section_bfd_section (section);
2425 /* Return the flags of SECTION.
2426 SECTION (or containing section if this is a virtual section) must exist. */
2429 get_section_flags (const struct dwarf2_section_info *section)
2431 asection *sectp = get_section_bfd_section (section);
2433 gdb_assert (sectp != NULL);
2434 return bfd_get_section_flags (sectp->owner, sectp);
2437 /* When loading sections, we look either for uncompressed section or for
2438 compressed section names. */
2441 section_is_p (const char *section_name,
2442 const struct dwarf2_section_names *names)
2444 if (names->normal != NULL
2445 && strcmp (section_name, names->normal) == 0)
2447 if (names->compressed != NULL
2448 && strcmp (section_name, names->compressed) == 0)
2453 /* See declaration. */
2456 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2457 const dwarf2_debug_sections &names)
2459 flagword aflag = bfd_get_section_flags (abfd, sectp);
2461 if ((aflag & SEC_HAS_CONTENTS) == 0)
2464 else if (section_is_p (sectp->name, &names.info))
2466 this->info.s.section = sectp;
2467 this->info.size = bfd_get_section_size (sectp);
2469 else if (section_is_p (sectp->name, &names.abbrev))
2471 this->abbrev.s.section = sectp;
2472 this->abbrev.size = bfd_get_section_size (sectp);
2474 else if (section_is_p (sectp->name, &names.line))
2476 this->line.s.section = sectp;
2477 this->line.size = bfd_get_section_size (sectp);
2479 else if (section_is_p (sectp->name, &names.loc))
2481 this->loc.s.section = sectp;
2482 this->loc.size = bfd_get_section_size (sectp);
2484 else if (section_is_p (sectp->name, &names.loclists))
2486 this->loclists.s.section = sectp;
2487 this->loclists.size = bfd_get_section_size (sectp);
2489 else if (section_is_p (sectp->name, &names.macinfo))
2491 this->macinfo.s.section = sectp;
2492 this->macinfo.size = bfd_get_section_size (sectp);
2494 else if (section_is_p (sectp->name, &names.macro))
2496 this->macro.s.section = sectp;
2497 this->macro.size = bfd_get_section_size (sectp);
2499 else if (section_is_p (sectp->name, &names.str))
2501 this->str.s.section = sectp;
2502 this->str.size = bfd_get_section_size (sectp);
2504 else if (section_is_p (sectp->name, &names.line_str))
2506 this->line_str.s.section = sectp;
2507 this->line_str.size = bfd_get_section_size (sectp);
2509 else if (section_is_p (sectp->name, &names.addr))
2511 this->addr.s.section = sectp;
2512 this->addr.size = bfd_get_section_size (sectp);
2514 else if (section_is_p (sectp->name, &names.frame))
2516 this->frame.s.section = sectp;
2517 this->frame.size = bfd_get_section_size (sectp);
2519 else if (section_is_p (sectp->name, &names.eh_frame))
2521 this->eh_frame.s.section = sectp;
2522 this->eh_frame.size = bfd_get_section_size (sectp);
2524 else if (section_is_p (sectp->name, &names.ranges))
2526 this->ranges.s.section = sectp;
2527 this->ranges.size = bfd_get_section_size (sectp);
2529 else if (section_is_p (sectp->name, &names.rnglists))
2531 this->rnglists.s.section = sectp;
2532 this->rnglists.size = bfd_get_section_size (sectp);
2534 else if (section_is_p (sectp->name, &names.types))
2536 struct dwarf2_section_info type_section;
2538 memset (&type_section, 0, sizeof (type_section));
2539 type_section.s.section = sectp;
2540 type_section.size = bfd_get_section_size (sectp);
2542 VEC_safe_push (dwarf2_section_info_def, this->types,
2545 else if (section_is_p (sectp->name, &names.gdb_index))
2547 this->gdb_index.s.section = sectp;
2548 this->gdb_index.size = bfd_get_section_size (sectp);
2551 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2552 && bfd_section_vma (abfd, sectp) == 0)
2553 this->has_section_at_zero = true;
2556 /* A helper function that decides whether a section is empty,
2560 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2562 if (section->is_virtual)
2563 return section->size == 0;
2564 return section->s.section == NULL || section->size == 0;
2567 /* Read the contents of the section INFO.
2568 OBJFILE is the main object file, but not necessarily the file where
2569 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2571 If the section is compressed, uncompress it before returning. */
2574 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2578 gdb_byte *buf, *retbuf;
2582 info->buffer = NULL;
2585 if (dwarf2_section_empty_p (info))
2588 sectp = get_section_bfd_section (info);
2590 /* If this is a virtual section we need to read in the real one first. */
2591 if (info->is_virtual)
2593 struct dwarf2_section_info *containing_section =
2594 get_containing_section (info);
2596 gdb_assert (sectp != NULL);
2597 if ((sectp->flags & SEC_RELOC) != 0)
2599 error (_("Dwarf Error: DWP format V2 with relocations is not"
2600 " supported in section %s [in module %s]"),
2601 get_section_name (info), get_section_file_name (info));
2603 dwarf2_read_section (objfile, containing_section);
2604 /* Other code should have already caught virtual sections that don't
2606 gdb_assert (info->virtual_offset + info->size
2607 <= containing_section->size);
2608 /* If the real section is empty or there was a problem reading the
2609 section we shouldn't get here. */
2610 gdb_assert (containing_section->buffer != NULL);
2611 info->buffer = containing_section->buffer + info->virtual_offset;
2615 /* If the section has relocations, we must read it ourselves.
2616 Otherwise we attach it to the BFD. */
2617 if ((sectp->flags & SEC_RELOC) == 0)
2619 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2623 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2626 /* When debugging .o files, we may need to apply relocations; see
2627 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2628 We never compress sections in .o files, so we only need to
2629 try this when the section is not compressed. */
2630 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2633 info->buffer = retbuf;
2637 abfd = get_section_bfd_owner (info);
2638 gdb_assert (abfd != NULL);
2640 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2641 || bfd_bread (buf, info->size, abfd) != info->size)
2643 error (_("Dwarf Error: Can't read DWARF data"
2644 " in section %s [in module %s]"),
2645 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2649 /* A helper function that returns the size of a section in a safe way.
2650 If you are positive that the section has been read before using the
2651 size, then it is safe to refer to the dwarf2_section_info object's
2652 "size" field directly. In other cases, you must call this
2653 function, because for compressed sections the size field is not set
2654 correctly until the section has been read. */
2656 static bfd_size_type
2657 dwarf2_section_size (struct objfile *objfile,
2658 struct dwarf2_section_info *info)
2661 dwarf2_read_section (objfile, info);
2665 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2669 dwarf2_get_section_info (struct objfile *objfile,
2670 enum dwarf2_section_enum sect,
2671 asection **sectp, const gdb_byte **bufp,
2672 bfd_size_type *sizep)
2674 struct dwarf2_per_objfile *data
2675 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2676 dwarf2_objfile_data_key);
2677 struct dwarf2_section_info *info;
2679 /* We may see an objfile without any DWARF, in which case we just
2690 case DWARF2_DEBUG_FRAME:
2691 info = &data->frame;
2693 case DWARF2_EH_FRAME:
2694 info = &data->eh_frame;
2697 gdb_assert_not_reached ("unexpected section");
2700 dwarf2_read_section (objfile, info);
2702 *sectp = get_section_bfd_section (info);
2703 *bufp = info->buffer;
2704 *sizep = info->size;
2707 /* A helper function to find the sections for a .dwz file. */
2710 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2712 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2714 /* Note that we only support the standard ELF names, because .dwz
2715 is ELF-only (at the time of writing). */
2716 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2718 dwz_file->abbrev.s.section = sectp;
2719 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2721 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2723 dwz_file->info.s.section = sectp;
2724 dwz_file->info.size = bfd_get_section_size (sectp);
2726 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2728 dwz_file->str.s.section = sectp;
2729 dwz_file->str.size = bfd_get_section_size (sectp);
2731 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2733 dwz_file->line.s.section = sectp;
2734 dwz_file->line.size = bfd_get_section_size (sectp);
2736 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2738 dwz_file->macro.s.section = sectp;
2739 dwz_file->macro.size = bfd_get_section_size (sectp);
2741 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2743 dwz_file->gdb_index.s.section = sectp;
2744 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2748 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2749 there is no .gnu_debugaltlink section in the file. Error if there
2750 is such a section but the file cannot be found. */
2752 static struct dwz_file *
2753 dwarf2_get_dwz_file (void)
2755 const char *filename;
2756 struct dwz_file *result;
2757 bfd_size_type buildid_len_arg;
2761 if (dwarf2_per_objfile->dwz_file != NULL)
2762 return dwarf2_per_objfile->dwz_file;
2764 bfd_set_error (bfd_error_no_error);
2765 gdb::unique_xmalloc_ptr<char> data
2766 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2767 &buildid_len_arg, &buildid));
2770 if (bfd_get_error () == bfd_error_no_error)
2772 error (_("could not read '.gnu_debugaltlink' section: %s"),
2773 bfd_errmsg (bfd_get_error ()));
2776 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2778 buildid_len = (size_t) buildid_len_arg;
2780 filename = data.get ();
2782 std::string abs_storage;
2783 if (!IS_ABSOLUTE_PATH (filename))
2785 gdb::unique_xmalloc_ptr<char> abs
2786 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2788 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2789 filename = abs_storage.c_str ();
2792 /* First try the file name given in the section. If that doesn't
2793 work, try to use the build-id instead. */
2794 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2795 if (dwz_bfd != NULL)
2797 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2801 if (dwz_bfd == NULL)
2802 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2804 if (dwz_bfd == NULL)
2805 error (_("could not find '.gnu_debugaltlink' file for %s"),
2806 objfile_name (dwarf2_per_objfile->objfile));
2808 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2810 result->dwz_bfd = dwz_bfd.release ();
2812 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2814 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2815 dwarf2_per_objfile->dwz_file = result;
2819 /* DWARF quick_symbols_functions support. */
2821 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2822 unique line tables, so we maintain a separate table of all .debug_line
2823 derived entries to support the sharing.
2824 All the quick functions need is the list of file names. We discard the
2825 line_header when we're done and don't need to record it here. */
2826 struct quick_file_names
2828 /* The data used to construct the hash key. */
2829 struct stmt_list_hash hash;
2831 /* The number of entries in file_names, real_names. */
2832 unsigned int num_file_names;
2834 /* The file names from the line table, after being run through
2836 const char **file_names;
2838 /* The file names from the line table after being run through
2839 gdb_realpath. These are computed lazily. */
2840 const char **real_names;
2843 /* When using the index (and thus not using psymtabs), each CU has an
2844 object of this type. This is used to hold information needed by
2845 the various "quick" methods. */
2846 struct dwarf2_per_cu_quick_data
2848 /* The file table. This can be NULL if there was no file table
2849 or it's currently not read in.
2850 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2851 struct quick_file_names *file_names;
2853 /* The corresponding symbol table. This is NULL if symbols for this
2854 CU have not yet been read. */
2855 struct compunit_symtab *compunit_symtab;
2857 /* A temporary mark bit used when iterating over all CUs in
2858 expand_symtabs_matching. */
2859 unsigned int mark : 1;
2861 /* True if we've tried to read the file table and found there isn't one.
2862 There will be no point in trying to read it again next time. */
2863 unsigned int no_file_data : 1;
2866 /* Utility hash function for a stmt_list_hash. */
2869 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2873 if (stmt_list_hash->dwo_unit != NULL)
2874 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2875 v += to_underlying (stmt_list_hash->line_sect_off);
2879 /* Utility equality function for a stmt_list_hash. */
2882 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2883 const struct stmt_list_hash *rhs)
2885 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2887 if (lhs->dwo_unit != NULL
2888 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2891 return lhs->line_sect_off == rhs->line_sect_off;
2894 /* Hash function for a quick_file_names. */
2897 hash_file_name_entry (const void *e)
2899 const struct quick_file_names *file_data
2900 = (const struct quick_file_names *) e;
2902 return hash_stmt_list_entry (&file_data->hash);
2905 /* Equality function for a quick_file_names. */
2908 eq_file_name_entry (const void *a, const void *b)
2910 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2911 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2913 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2916 /* Delete function for a quick_file_names. */
2919 delete_file_name_entry (void *e)
2921 struct quick_file_names *file_data = (struct quick_file_names *) e;
2924 for (i = 0; i < file_data->num_file_names; ++i)
2926 xfree ((void*) file_data->file_names[i]);
2927 if (file_data->real_names)
2928 xfree ((void*) file_data->real_names[i]);
2931 /* The space for the struct itself lives on objfile_obstack,
2932 so we don't free it here. */
2935 /* Create a quick_file_names hash table. */
2938 create_quick_file_names_table (unsigned int nr_initial_entries)
2940 return htab_create_alloc (nr_initial_entries,
2941 hash_file_name_entry, eq_file_name_entry,
2942 delete_file_name_entry, xcalloc, xfree);
2945 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2946 have to be created afterwards. You should call age_cached_comp_units after
2947 processing PER_CU->CU. dw2_setup must have been already called. */
2950 load_cu (struct dwarf2_per_cu_data *per_cu)
2952 if (per_cu->is_debug_types)
2953 load_full_type_unit (per_cu);
2955 load_full_comp_unit (per_cu, language_minimal);
2957 if (per_cu->cu == NULL)
2958 return; /* Dummy CU. */
2960 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2963 /* Read in the symbols for PER_CU. */
2966 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2968 struct cleanup *back_to;
2970 /* Skip type_unit_groups, reading the type units they contain
2971 is handled elsewhere. */
2972 if (IS_TYPE_UNIT_GROUP (per_cu))
2975 back_to = make_cleanup (dwarf2_release_queue, NULL);
2977 if (dwarf2_per_objfile->using_index
2978 ? per_cu->v.quick->compunit_symtab == NULL
2979 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2981 queue_comp_unit (per_cu, language_minimal);
2984 /* If we just loaded a CU from a DWO, and we're working with an index
2985 that may badly handle TUs, load all the TUs in that DWO as well.
2986 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2987 if (!per_cu->is_debug_types
2988 && per_cu->cu != NULL
2989 && per_cu->cu->dwo_unit != NULL
2990 && dwarf2_per_objfile->index_table != NULL
2991 && dwarf2_per_objfile->index_table->version <= 7
2992 /* DWP files aren't supported yet. */
2993 && get_dwp_file () == NULL)
2994 queue_and_load_all_dwo_tus (per_cu);
2999 /* Age the cache, releasing compilation units that have not
3000 been used recently. */
3001 age_cached_comp_units ();
3003 do_cleanups (back_to);
3006 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3007 the objfile from which this CU came. Returns the resulting symbol
3010 static struct compunit_symtab *
3011 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3013 gdb_assert (dwarf2_per_objfile->using_index);
3014 if (!per_cu->v.quick->compunit_symtab)
3016 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
3017 scoped_restore decrementer = increment_reading_symtab ();
3018 dw2_do_instantiate_symtab (per_cu);
3019 process_cu_includes ();
3020 do_cleanups (back_to);
3023 return per_cu->v.quick->compunit_symtab;
3026 /* Return the CU/TU given its index.
3028 This is intended for loops like:
3030 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3031 + dwarf2_per_objfile->n_type_units); ++i)
3033 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3039 static struct dwarf2_per_cu_data *
3040 dw2_get_cutu (int index)
3042 if (index >= dwarf2_per_objfile->n_comp_units)
3044 index -= dwarf2_per_objfile->n_comp_units;
3045 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3046 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3049 return dwarf2_per_objfile->all_comp_units[index];
3052 /* Return the CU given its index.
3053 This differs from dw2_get_cutu in that it's for when you know INDEX
3056 static struct dwarf2_per_cu_data *
3057 dw2_get_cu (int index)
3059 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3061 return dwarf2_per_objfile->all_comp_units[index];
3064 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3065 objfile_obstack, and constructed with the specified field
3068 static dwarf2_per_cu_data *
3069 create_cu_from_index_list (struct objfile *objfile,
3070 struct dwarf2_section_info *section,
3072 sect_offset sect_off, ULONGEST length)
3074 dwarf2_per_cu_data *the_cu
3075 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3076 struct dwarf2_per_cu_data);
3077 the_cu->sect_off = sect_off;
3078 the_cu->length = length;
3079 the_cu->objfile = objfile;
3080 the_cu->section = section;
3081 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3082 struct dwarf2_per_cu_quick_data);
3083 the_cu->is_dwz = is_dwz;
3087 /* A helper for create_cus_from_index that handles a given list of
3091 create_cus_from_index_list (struct objfile *objfile,
3092 const gdb_byte *cu_list, offset_type n_elements,
3093 struct dwarf2_section_info *section,
3099 for (i = 0; i < n_elements; i += 2)
3101 gdb_static_assert (sizeof (ULONGEST) >= 8);
3103 sect_offset sect_off
3104 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3105 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3108 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3109 = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
3113 /* Read the CU list from the mapped index, and use it to create all
3114 the CU objects for this objfile. */
3117 create_cus_from_index (struct objfile *objfile,
3118 const gdb_byte *cu_list, offset_type cu_list_elements,
3119 const gdb_byte *dwz_list, offset_type dwz_elements)
3121 struct dwz_file *dwz;
3123 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3124 dwarf2_per_objfile->all_comp_units =
3125 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3126 dwarf2_per_objfile->n_comp_units);
3128 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3129 &dwarf2_per_objfile->info, 0, 0);
3131 if (dwz_elements == 0)
3134 dwz = dwarf2_get_dwz_file ();
3135 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3136 cu_list_elements / 2);
3139 /* Create the signatured type hash table from the index. */
3142 create_signatured_type_table_from_index (struct objfile *objfile,
3143 struct dwarf2_section_info *section,
3144 const gdb_byte *bytes,
3145 offset_type elements)
3148 htab_t sig_types_hash;
3150 dwarf2_per_objfile->n_type_units
3151 = dwarf2_per_objfile->n_allocated_type_units
3153 dwarf2_per_objfile->all_type_units =
3154 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3156 sig_types_hash = allocate_signatured_type_table (objfile);
3158 for (i = 0; i < elements; i += 3)
3160 struct signatured_type *sig_type;
3163 cu_offset type_offset_in_tu;
3165 gdb_static_assert (sizeof (ULONGEST) >= 8);
3166 sect_offset sect_off
3167 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3169 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3171 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3174 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3175 struct signatured_type);
3176 sig_type->signature = signature;
3177 sig_type->type_offset_in_tu = type_offset_in_tu;
3178 sig_type->per_cu.is_debug_types = 1;
3179 sig_type->per_cu.section = section;
3180 sig_type->per_cu.sect_off = sect_off;
3181 sig_type->per_cu.objfile = objfile;
3182 sig_type->per_cu.v.quick
3183 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3184 struct dwarf2_per_cu_quick_data);
3186 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3189 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3192 dwarf2_per_objfile->signatured_types = sig_types_hash;
3195 /* Read the address map data from the mapped index, and use it to
3196 populate the objfile's psymtabs_addrmap. */
3199 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3202 const gdb_byte *iter, *end;
3203 struct addrmap *mutable_map;
3206 auto_obstack temp_obstack;
3208 mutable_map = addrmap_create_mutable (&temp_obstack);
3210 iter = index->address_table;
3211 end = iter + index->address_table_size;
3213 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3217 ULONGEST hi, lo, cu_index;
3218 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3220 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3222 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3227 complaint (&symfile_complaints,
3228 _(".gdb_index address table has invalid range (%s - %s)"),
3229 hex_string (lo), hex_string (hi));
3233 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3235 complaint (&symfile_complaints,
3236 _(".gdb_index address table has invalid CU number %u"),
3237 (unsigned) cu_index);
3241 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3242 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3243 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3246 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3247 &objfile->objfile_obstack);
3250 /* The hash function for strings in the mapped index. This is the same as
3251 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3252 implementation. This is necessary because the hash function is tied to the
3253 format of the mapped index file. The hash values do not have to match with
3256 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3259 mapped_index_string_hash (int index_version, const void *p)
3261 const unsigned char *str = (const unsigned char *) p;
3265 while ((c = *str++) != 0)
3267 if (index_version >= 5)
3269 r = r * 67 + c - 113;
3275 /* Find a slot in the mapped index INDEX for the object named NAME.
3276 If NAME is found, set *VEC_OUT to point to the CU vector in the
3277 constant pool and return true. If NAME cannot be found, return
3281 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3282 offset_type **vec_out)
3285 offset_type slot, step;
3286 int (*cmp) (const char *, const char *);
3288 gdb::unique_xmalloc_ptr<char> without_params;
3289 if (current_language->la_language == language_cplus
3290 || current_language->la_language == language_fortran
3291 || current_language->la_language == language_d)
3293 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3296 if (strchr (name, '(') != NULL)
3298 without_params = cp_remove_params (name);
3300 if (without_params != NULL)
3301 name = without_params.get ();
3305 /* Index version 4 did not support case insensitive searches. But the
3306 indices for case insensitive languages are built in lowercase, therefore
3307 simulate our NAME being searched is also lowercased. */
3308 hash = mapped_index_string_hash ((index->version == 4
3309 && case_sensitivity == case_sensitive_off
3310 ? 5 : index->version),
3313 slot = hash & (index->symbol_table_slots - 1);
3314 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3315 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3319 /* Convert a slot number to an offset into the table. */
3320 offset_type i = 2 * slot;
3322 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3325 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3326 if (!cmp (name, str))
3328 *vec_out = (offset_type *) (index->constant_pool
3329 + MAYBE_SWAP (index->symbol_table[i + 1]));
3333 slot = (slot + step) & (index->symbol_table_slots - 1);
3337 /* A helper function that reads the .gdb_index from SECTION and fills
3338 in MAP. FILENAME is the name of the file containing the section;
3339 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3340 ok to use deprecated sections.
3342 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3343 out parameters that are filled in with information about the CU and
3344 TU lists in the section.
3346 Returns 1 if all went well, 0 otherwise. */
3349 read_index_from_section (struct objfile *objfile,
3350 const char *filename,
3352 struct dwarf2_section_info *section,
3353 struct mapped_index *map,
3354 const gdb_byte **cu_list,
3355 offset_type *cu_list_elements,
3356 const gdb_byte **types_list,
3357 offset_type *types_list_elements)
3359 const gdb_byte *addr;
3360 offset_type version;
3361 offset_type *metadata;
3364 if (dwarf2_section_empty_p (section))
3367 /* Older elfutils strip versions could keep the section in the main
3368 executable while splitting it for the separate debug info file. */
3369 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3372 dwarf2_read_section (objfile, section);
3374 addr = section->buffer;
3375 /* Version check. */
3376 version = MAYBE_SWAP (*(offset_type *) addr);
3377 /* Versions earlier than 3 emitted every copy of a psymbol. This
3378 causes the index to behave very poorly for certain requests. Version 3
3379 contained incomplete addrmap. So, it seems better to just ignore such
3383 static int warning_printed = 0;
3384 if (!warning_printed)
3386 warning (_("Skipping obsolete .gdb_index section in %s."),
3388 warning_printed = 1;
3392 /* Index version 4 uses a different hash function than index version
3395 Versions earlier than 6 did not emit psymbols for inlined
3396 functions. Using these files will cause GDB not to be able to
3397 set breakpoints on inlined functions by name, so we ignore these
3398 indices unless the user has done
3399 "set use-deprecated-index-sections on". */
3400 if (version < 6 && !deprecated_ok)
3402 static int warning_printed = 0;
3403 if (!warning_printed)
3406 Skipping deprecated .gdb_index section in %s.\n\
3407 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3408 to use the section anyway."),
3410 warning_printed = 1;
3414 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3415 of the TU (for symbols coming from TUs),
3416 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3417 Plus gold-generated indices can have duplicate entries for global symbols,
3418 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3419 These are just performance bugs, and we can't distinguish gdb-generated
3420 indices from gold-generated ones, so issue no warning here. */
3422 /* Indexes with higher version than the one supported by GDB may be no
3423 longer backward compatible. */
3427 map->version = version;
3428 map->total_size = section->size;
3430 metadata = (offset_type *) (addr + sizeof (offset_type));
3433 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3434 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3438 *types_list = addr + MAYBE_SWAP (metadata[i]);
3439 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3440 - MAYBE_SWAP (metadata[i]))
3444 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3445 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3446 - MAYBE_SWAP (metadata[i]));
3449 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3450 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3451 - MAYBE_SWAP (metadata[i]))
3452 / (2 * sizeof (offset_type)));
3455 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3461 /* Read the index file. If everything went ok, initialize the "quick"
3462 elements of all the CUs and return 1. Otherwise, return 0. */
3465 dwarf2_read_index (struct objfile *objfile)
3467 struct mapped_index local_map, *map;
3468 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3469 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3470 struct dwz_file *dwz;
3472 if (!read_index_from_section (objfile, objfile_name (objfile),
3473 use_deprecated_index_sections,
3474 &dwarf2_per_objfile->gdb_index, &local_map,
3475 &cu_list, &cu_list_elements,
3476 &types_list, &types_list_elements))
3479 /* Don't use the index if it's empty. */
3480 if (local_map.symbol_table_slots == 0)
3483 /* If there is a .dwz file, read it so we can get its CU list as
3485 dwz = dwarf2_get_dwz_file ();
3488 struct mapped_index dwz_map;
3489 const gdb_byte *dwz_types_ignore;
3490 offset_type dwz_types_elements_ignore;
3492 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3494 &dwz->gdb_index, &dwz_map,
3495 &dwz_list, &dwz_list_elements,
3497 &dwz_types_elements_ignore))
3499 warning (_("could not read '.gdb_index' section from %s; skipping"),
3500 bfd_get_filename (dwz->dwz_bfd));
3505 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3508 if (types_list_elements)
3510 struct dwarf2_section_info *section;
3512 /* We can only handle a single .debug_types when we have an
3514 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3517 section = VEC_index (dwarf2_section_info_def,
3518 dwarf2_per_objfile->types, 0);
3520 create_signatured_type_table_from_index (objfile, section, types_list,
3521 types_list_elements);
3524 create_addrmap_from_index (objfile, &local_map);
3526 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3527 map = new (map) mapped_index ();
3530 dwarf2_per_objfile->index_table = map;
3531 dwarf2_per_objfile->using_index = 1;
3532 dwarf2_per_objfile->quick_file_names_table =
3533 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3538 /* A helper for the "quick" functions which sets the global
3539 dwarf2_per_objfile according to OBJFILE. */
3542 dw2_setup (struct objfile *objfile)
3544 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3545 objfile_data (objfile, dwarf2_objfile_data_key));
3546 gdb_assert (dwarf2_per_objfile);
3549 /* die_reader_func for dw2_get_file_names. */
3552 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3553 const gdb_byte *info_ptr,
3554 struct die_info *comp_unit_die,
3558 struct dwarf2_cu *cu = reader->cu;
3559 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3560 struct objfile *objfile = dwarf2_per_objfile->objfile;
3561 struct dwarf2_per_cu_data *lh_cu;
3562 struct attribute *attr;
3565 struct quick_file_names *qfn;
3567 gdb_assert (! this_cu->is_debug_types);
3569 /* Our callers never want to match partial units -- instead they
3570 will match the enclosing full CU. */
3571 if (comp_unit_die->tag == DW_TAG_partial_unit)
3573 this_cu->v.quick->no_file_data = 1;
3581 sect_offset line_offset {};
3583 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3586 struct quick_file_names find_entry;
3588 line_offset = (sect_offset) DW_UNSND (attr);
3590 /* We may have already read in this line header (TU line header sharing).
3591 If we have we're done. */
3592 find_entry.hash.dwo_unit = cu->dwo_unit;
3593 find_entry.hash.line_sect_off = line_offset;
3594 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3595 &find_entry, INSERT);
3598 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3602 lh = dwarf_decode_line_header (line_offset, cu);
3606 lh_cu->v.quick->no_file_data = 1;
3610 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3611 qfn->hash.dwo_unit = cu->dwo_unit;
3612 qfn->hash.line_sect_off = line_offset;
3613 gdb_assert (slot != NULL);
3616 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3618 qfn->num_file_names = lh->file_names.size ();
3620 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3621 for (i = 0; i < lh->file_names.size (); ++i)
3622 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3623 qfn->real_names = NULL;
3625 lh_cu->v.quick->file_names = qfn;
3628 /* A helper for the "quick" functions which attempts to read the line
3629 table for THIS_CU. */
3631 static struct quick_file_names *
3632 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3634 /* This should never be called for TUs. */
3635 gdb_assert (! this_cu->is_debug_types);
3636 /* Nor type unit groups. */
3637 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3639 if (this_cu->v.quick->file_names != NULL)
3640 return this_cu->v.quick->file_names;
3641 /* If we know there is no line data, no point in looking again. */
3642 if (this_cu->v.quick->no_file_data)
3645 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3647 if (this_cu->v.quick->no_file_data)
3649 return this_cu->v.quick->file_names;
3652 /* A helper for the "quick" functions which computes and caches the
3653 real path for a given file name from the line table. */
3656 dw2_get_real_path (struct objfile *objfile,
3657 struct quick_file_names *qfn, int index)
3659 if (qfn->real_names == NULL)
3660 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3661 qfn->num_file_names, const char *);
3663 if (qfn->real_names[index] == NULL)
3664 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3666 return qfn->real_names[index];
3669 static struct symtab *
3670 dw2_find_last_source_symtab (struct objfile *objfile)
3672 struct compunit_symtab *cust;
3675 dw2_setup (objfile);
3676 index = dwarf2_per_objfile->n_comp_units - 1;
3677 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3680 return compunit_primary_filetab (cust);
3683 /* Traversal function for dw2_forget_cached_source_info. */
3686 dw2_free_cached_file_names (void **slot, void *info)
3688 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3690 if (file_data->real_names)
3694 for (i = 0; i < file_data->num_file_names; ++i)
3696 xfree ((void*) file_data->real_names[i]);
3697 file_data->real_names[i] = NULL;
3705 dw2_forget_cached_source_info (struct objfile *objfile)
3707 dw2_setup (objfile);
3709 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3710 dw2_free_cached_file_names, NULL);
3713 /* Helper function for dw2_map_symtabs_matching_filename that expands
3714 the symtabs and calls the iterator. */
3717 dw2_map_expand_apply (struct objfile *objfile,
3718 struct dwarf2_per_cu_data *per_cu,
3719 const char *name, const char *real_path,
3720 gdb::function_view<bool (symtab *)> callback)
3722 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3724 /* Don't visit already-expanded CUs. */
3725 if (per_cu->v.quick->compunit_symtab)
3728 /* This may expand more than one symtab, and we want to iterate over
3730 dw2_instantiate_symtab (per_cu);
3732 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3733 last_made, callback);
3736 /* Implementation of the map_symtabs_matching_filename method. */
3739 dw2_map_symtabs_matching_filename
3740 (struct objfile *objfile, const char *name, const char *real_path,
3741 gdb::function_view<bool (symtab *)> callback)
3744 const char *name_basename = lbasename (name);
3746 dw2_setup (objfile);
3748 /* The rule is CUs specify all the files, including those used by
3749 any TU, so there's no need to scan TUs here. */
3751 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3754 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3755 struct quick_file_names *file_data;
3757 /* We only need to look at symtabs not already expanded. */
3758 if (per_cu->v.quick->compunit_symtab)
3761 file_data = dw2_get_file_names (per_cu);
3762 if (file_data == NULL)
3765 for (j = 0; j < file_data->num_file_names; ++j)
3767 const char *this_name = file_data->file_names[j];
3768 const char *this_real_name;
3770 if (compare_filenames_for_search (this_name, name))
3772 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3778 /* Before we invoke realpath, which can get expensive when many
3779 files are involved, do a quick comparison of the basenames. */
3780 if (! basenames_may_differ
3781 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3784 this_real_name = dw2_get_real_path (objfile, file_data, j);
3785 if (compare_filenames_for_search (this_real_name, name))
3787 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3793 if (real_path != NULL)
3795 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3796 gdb_assert (IS_ABSOLUTE_PATH (name));
3797 if (this_real_name != NULL
3798 && FILENAME_CMP (real_path, this_real_name) == 0)
3800 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3812 /* Struct used to manage iterating over all CUs looking for a symbol. */
3814 struct dw2_symtab_iterator
3816 /* The internalized form of .gdb_index. */
3817 struct mapped_index *index;
3818 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3819 int want_specific_block;
3820 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3821 Unused if !WANT_SPECIFIC_BLOCK. */
3823 /* The kind of symbol we're looking for. */
3825 /* The list of CUs from the index entry of the symbol,
3826 or NULL if not found. */
3828 /* The next element in VEC to look at. */
3830 /* The number of elements in VEC, or zero if there is no match. */
3832 /* Have we seen a global version of the symbol?
3833 If so we can ignore all further global instances.
3834 This is to work around gold/15646, inefficient gold-generated
3839 /* Initialize the index symtab iterator ITER.
3840 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3841 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3844 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3845 struct mapped_index *index,
3846 int want_specific_block,
3851 iter->index = index;
3852 iter->want_specific_block = want_specific_block;
3853 iter->block_index = block_index;
3854 iter->domain = domain;
3856 iter->global_seen = 0;
3858 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3859 iter->length = MAYBE_SWAP (*iter->vec);
3867 /* Return the next matching CU or NULL if there are no more. */
3869 static struct dwarf2_per_cu_data *
3870 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3872 for ( ; iter->next < iter->length; ++iter->next)
3874 offset_type cu_index_and_attrs =
3875 MAYBE_SWAP (iter->vec[iter->next + 1]);
3876 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3877 struct dwarf2_per_cu_data *per_cu;
3878 int want_static = iter->block_index != GLOBAL_BLOCK;
3879 /* This value is only valid for index versions >= 7. */
3880 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3881 gdb_index_symbol_kind symbol_kind =
3882 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3883 /* Only check the symbol attributes if they're present.
3884 Indices prior to version 7 don't record them,
3885 and indices >= 7 may elide them for certain symbols
3886 (gold does this). */
3888 (iter->index->version >= 7
3889 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3891 /* Don't crash on bad data. */
3892 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3893 + dwarf2_per_objfile->n_type_units))
3895 complaint (&symfile_complaints,
3896 _(".gdb_index entry has bad CU index"
3898 objfile_name (dwarf2_per_objfile->objfile));
3902 per_cu = dw2_get_cutu (cu_index);
3904 /* Skip if already read in. */
3905 if (per_cu->v.quick->compunit_symtab)
3908 /* Check static vs global. */
3911 if (iter->want_specific_block
3912 && want_static != is_static)
3914 /* Work around gold/15646. */
3915 if (!is_static && iter->global_seen)
3918 iter->global_seen = 1;
3921 /* Only check the symbol's kind if it has one. */
3924 switch (iter->domain)
3927 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3928 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3929 /* Some types are also in VAR_DOMAIN. */
3930 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3934 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3938 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3953 static struct compunit_symtab *
3954 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3955 const char *name, domain_enum domain)
3957 struct compunit_symtab *stab_best = NULL;
3958 struct mapped_index *index;
3960 dw2_setup (objfile);
3962 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3964 index = dwarf2_per_objfile->index_table;
3966 /* index is NULL if OBJF_READNOW. */
3969 struct dw2_symtab_iterator iter;
3970 struct dwarf2_per_cu_data *per_cu;
3972 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3974 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3976 struct symbol *sym, *with_opaque = NULL;
3977 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3978 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3979 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3981 sym = block_find_symbol (block, name, domain,
3982 block_find_non_opaque_type_preferred,
3985 /* Some caution must be observed with overloaded functions
3986 and methods, since the index will not contain any overload
3987 information (but NAME might contain it). */
3990 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3992 if (with_opaque != NULL
3993 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3996 /* Keep looking through other CUs. */
4004 dw2_print_stats (struct objfile *objfile)
4006 int i, total, count;
4008 dw2_setup (objfile);
4009 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4011 for (i = 0; i < total; ++i)
4013 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4015 if (!per_cu->v.quick->compunit_symtab)
4018 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4019 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4022 /* This dumps minimal information about the index.
4023 It is called via "mt print objfiles".
4024 One use is to verify .gdb_index has been loaded by the
4025 gdb.dwarf2/gdb-index.exp testcase. */
4028 dw2_dump (struct objfile *objfile)
4030 dw2_setup (objfile);
4031 gdb_assert (dwarf2_per_objfile->using_index);
4032 printf_filtered (".gdb_index:");
4033 if (dwarf2_per_objfile->index_table != NULL)
4035 printf_filtered (" version %d\n",
4036 dwarf2_per_objfile->index_table->version);
4039 printf_filtered (" faked for \"readnow\"\n");
4040 printf_filtered ("\n");
4044 dw2_relocate (struct objfile *objfile,
4045 const struct section_offsets *new_offsets,
4046 const struct section_offsets *delta)
4048 /* There's nothing to relocate here. */
4052 dw2_expand_symtabs_for_function (struct objfile *objfile,
4053 const char *func_name)
4055 struct mapped_index *index;
4057 dw2_setup (objfile);
4059 index = dwarf2_per_objfile->index_table;
4061 /* index is NULL if OBJF_READNOW. */
4064 struct dw2_symtab_iterator iter;
4065 struct dwarf2_per_cu_data *per_cu;
4067 /* Note: It doesn't matter what we pass for block_index here. */
4068 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4071 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4072 dw2_instantiate_symtab (per_cu);
4077 dw2_expand_all_symtabs (struct objfile *objfile)
4081 dw2_setup (objfile);
4083 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4084 + dwarf2_per_objfile->n_type_units); ++i)
4086 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4088 dw2_instantiate_symtab (per_cu);
4093 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4094 const char *fullname)
4098 dw2_setup (objfile);
4100 /* We don't need to consider type units here.
4101 This is only called for examining code, e.g. expand_line_sal.
4102 There can be an order of magnitude (or more) more type units
4103 than comp units, and we avoid them if we can. */
4105 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4108 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4109 struct quick_file_names *file_data;
4111 /* We only need to look at symtabs not already expanded. */
4112 if (per_cu->v.quick->compunit_symtab)
4115 file_data = dw2_get_file_names (per_cu);
4116 if (file_data == NULL)
4119 for (j = 0; j < file_data->num_file_names; ++j)
4121 const char *this_fullname = file_data->file_names[j];
4123 if (filename_cmp (this_fullname, fullname) == 0)
4125 dw2_instantiate_symtab (per_cu);
4133 dw2_map_matching_symbols (struct objfile *objfile,
4134 const char * name, domain_enum domain,
4136 int (*callback) (struct block *,
4137 struct symbol *, void *),
4138 void *data, symbol_name_match_type match,
4139 symbol_compare_ftype *ordered_compare)
4141 /* Currently unimplemented; used for Ada. The function can be called if the
4142 current language is Ada for a non-Ada objfile using GNU index. As Ada
4143 does not look for non-Ada symbols this function should just return. */
4146 /* Symbol name matcher for .gdb_index names.
4148 Symbol names in .gdb_index have a few particularities:
4150 - There's no indication of which is the language of each symbol.
4152 Since each language has its own symbol name matching algorithm,
4153 and we don't know which language is the right one, we must match
4154 each symbol against all languages. This would be a potential
4155 performance problem if it were not mitigated by the
4156 mapped_index::name_components lookup table, which significantly
4157 reduces the number of times we need to call into this matcher,
4158 making it a non-issue.
4160 - Symbol names in the index have no overload (parameter)
4161 information. I.e., in C++, "foo(int)" and "foo(long)" both
4162 appear as "foo" in the index, for example.
4164 This means that the lookup names passed to the symbol name
4165 matcher functions must have no parameter information either
4166 because (e.g.) symbol search name "foo" does not match
4167 lookup-name "foo(int)" [while swapping search name for lookup
4170 class gdb_index_symbol_name_matcher
4173 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4174 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4176 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4177 Returns true if any matcher matches. */
4178 bool matches (const char *symbol_name);
4181 /* A reference to the lookup name we're matching against. */
4182 const lookup_name_info &m_lookup_name;
4184 /* A vector holding all the different symbol name matchers, for all
4186 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4189 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4190 (const lookup_name_info &lookup_name)
4191 : m_lookup_name (lookup_name)
4193 /* Prepare the vector of comparison functions upfront, to avoid
4194 doing the same work for each symbol. Care is taken to avoid
4195 matching with the same matcher more than once if/when multiple
4196 languages use the same matcher function. */
4197 auto &matchers = m_symbol_name_matcher_funcs;
4198 matchers.reserve (nr_languages);
4200 matchers.push_back (default_symbol_name_matcher);
4202 for (int i = 0; i < nr_languages; i++)
4204 const language_defn *lang = language_def ((enum language) i);
4205 if (lang->la_get_symbol_name_matcher != NULL)
4207 symbol_name_matcher_ftype *name_matcher
4208 = lang->la_get_symbol_name_matcher (m_lookup_name);
4210 /* Don't insert the same comparison routine more than once.
4211 Note that we do this linear walk instead of a cheaper
4212 sorted insert, or use a std::set or something like that,
4213 because relative order of function addresses is not
4214 stable. This is not a problem in practice because the
4215 number of supported languages is low, and the cost here
4216 is tiny compared to the number of searches we'll do
4217 afterwards using this object. */
4218 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4220 matchers.push_back (name_matcher);
4226 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4228 for (auto matches_name : m_symbol_name_matcher_funcs)
4229 if (matches_name (symbol_name, m_lookup_name, NULL))
4235 /* Starting from a search name, return the string that finds the upper
4236 bound of all strings that start with SEARCH_NAME in a sorted name
4237 list. Returns the empty string to indicate that the upper bound is
4238 the end of the list. */
4241 make_sort_after_prefix_name (const char *search_name)
4243 /* When looking to complete "func", we find the upper bound of all
4244 symbols that start with "func" by looking for where we'd insert
4245 the closest string that would follow "func" in lexicographical
4246 order. Usually, that's "func"-with-last-character-incremented,
4247 i.e. "fund". Mind non-ASCII characters, though. Usually those
4248 will be UTF-8 multi-byte sequences, but we can't be certain.
4249 Especially mind the 0xff character, which is a valid character in
4250 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4251 rule out compilers allowing it in identifiers. Note that
4252 conveniently, strcmp/strcasecmp are specified to compare
4253 characters interpreted as unsigned char. So what we do is treat
4254 the whole string as a base 256 number composed of a sequence of
4255 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4256 to 0, and carries 1 to the following more-significant position.
4257 If the very first character in SEARCH_NAME ends up incremented
4258 and carries/overflows, then the upper bound is the end of the
4259 list. The string after the empty string is also the empty
4262 Some examples of this operation:
4264 SEARCH_NAME => "+1" RESULT
4268 "\xff" "a" "\xff" => "\xff" "b"
4273 Then, with these symbols for example:
4279 completing "func" looks for symbols between "func" and
4280 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4281 which finds "func" and "func1", but not "fund".
4285 funcÿ (Latin1 'ÿ' [0xff])
4289 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4290 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4294 ÿÿ (Latin1 'ÿ' [0xff])
4297 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4298 the end of the list.
4300 std::string after = search_name;
4301 while (!after.empty () && (unsigned char) after.back () == 0xff)
4303 if (!after.empty ())
4304 after.back () = (unsigned char) after.back () + 1;
4308 /* See declaration. */
4310 std::pair<std::vector<name_component>::const_iterator,
4311 std::vector<name_component>::const_iterator>
4312 mapped_index::find_name_components_bounds
4313 (const lookup_name_info &lookup_name_without_params) const
4316 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4319 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4321 /* Comparison function object for lower_bound that matches against a
4322 given symbol name. */
4323 auto lookup_compare_lower = [&] (const name_component &elem,
4326 const char *elem_qualified = this->symbol_name_at (elem.idx);
4327 const char *elem_name = elem_qualified + elem.name_offset;
4328 return name_cmp (elem_name, name) < 0;
4331 /* Comparison function object for upper_bound that matches against a
4332 given symbol name. */
4333 auto lookup_compare_upper = [&] (const char *name,
4334 const name_component &elem)
4336 const char *elem_qualified = this->symbol_name_at (elem.idx);
4337 const char *elem_name = elem_qualified + elem.name_offset;
4338 return name_cmp (name, elem_name) < 0;
4341 auto begin = this->name_components.begin ();
4342 auto end = this->name_components.end ();
4344 /* Find the lower bound. */
4347 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4350 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4353 /* Find the upper bound. */
4356 if (lookup_name_without_params.completion_mode ())
4358 /* In completion mode, we want UPPER to point past all
4359 symbols names that have the same prefix. I.e., with
4360 these symbols, and completing "func":
4362 function << lower bound
4364 other_function << upper bound
4366 We find the upper bound by looking for the insertion
4367 point of "func"-with-last-character-incremented,
4369 std::string after = make_sort_after_prefix_name (cplus);
4372 return std::lower_bound (lower, end, after.c_str (),
4373 lookup_compare_lower);
4376 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4379 return {lower, upper};
4382 /* See declaration. */
4385 mapped_index::build_name_components ()
4387 if (!this->name_components.empty ())
4390 this->name_components_casing = case_sensitivity;
4392 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4394 /* The code below only knows how to break apart components of C++
4395 symbol names (and other languages that use '::' as
4396 namespace/module separator). If we add support for wild matching
4397 to some language that uses some other operator (E.g., Ada, Go and
4398 D use '.'), then we'll need to try splitting the symbol name
4399 according to that language too. Note that Ada does support wild
4400 matching, but doesn't currently support .gdb_index. */
4401 for (size_t iter = 0; iter < this->symbol_table_slots; ++iter)
4403 offset_type idx = 2 * iter;
4405 if (this->symbol_table[idx] == 0
4406 && this->symbol_table[idx + 1] == 0)
4409 const char *name = this->symbol_name_at (idx);
4411 /* Add each name component to the name component table. */
4412 unsigned int previous_len = 0;
4413 for (unsigned int current_len = cp_find_first_component (name);
4414 name[current_len] != '\0';
4415 current_len += cp_find_first_component (name + current_len))
4417 gdb_assert (name[current_len] == ':');
4418 this->name_components.push_back ({previous_len, idx});
4419 /* Skip the '::'. */
4421 previous_len = current_len;
4423 this->name_components.push_back ({previous_len, idx});
4426 /* Sort name_components elements by name. */
4427 auto name_comp_compare = [&] (const name_component &left,
4428 const name_component &right)
4430 const char *left_qualified = this->symbol_name_at (left.idx);
4431 const char *right_qualified = this->symbol_name_at (right.idx);
4433 const char *left_name = left_qualified + left.name_offset;
4434 const char *right_name = right_qualified + right.name_offset;
4436 return name_cmp (left_name, right_name) < 0;
4439 std::sort (this->name_components.begin (),
4440 this->name_components.end (),
4444 /* Helper for dw2_expand_symtabs_matching that works with a
4445 mapped_index instead of the containing objfile. This is split to a
4446 separate function in order to be able to unit test the
4447 name_components matching using a mock mapped_index. For each
4448 symbol name that matches, calls MATCH_CALLBACK, passing it the
4449 symbol's index in the mapped_index symbol table. */
4452 dw2_expand_symtabs_matching_symbol
4453 (mapped_index &index,
4454 const lookup_name_info &lookup_name_in,
4455 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4456 enum search_domain kind,
4457 gdb::function_view<void (offset_type)> match_callback)
4459 lookup_name_info lookup_name_without_params
4460 = lookup_name_in.make_ignore_params ();
4461 gdb_index_symbol_name_matcher lookup_name_matcher
4462 (lookup_name_without_params);
4464 /* Build the symbol name component sorted vector, if we haven't
4466 index.build_name_components ();
4468 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4470 /* Now for each symbol name in range, check to see if we have a name
4471 match, and if so, call the MATCH_CALLBACK callback. */
4473 /* The same symbol may appear more than once in the range though.
4474 E.g., if we're looking for symbols that complete "w", and we have
4475 a symbol named "w1::w2", we'll find the two name components for
4476 that same symbol in the range. To be sure we only call the
4477 callback once per symbol, we first collect the symbol name
4478 indexes that matched in a temporary vector and ignore
4480 std::vector<offset_type> matches;
4481 matches.reserve (std::distance (bounds.first, bounds.second));
4483 for (; bounds.first != bounds.second; ++bounds.first)
4485 const char *qualified = index.symbol_name_at (bounds.first->idx);
4487 if (!lookup_name_matcher.matches (qualified)
4488 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4491 matches.push_back (bounds.first->idx);
4494 std::sort (matches.begin (), matches.end ());
4496 /* Finally call the callback, once per match. */
4498 for (offset_type idx : matches)
4502 match_callback (idx);
4507 /* Above we use a type wider than idx's for 'prev', since 0 and
4508 (offset_type)-1 are both possible values. */
4509 static_assert (sizeof (prev) > sizeof (offset_type), "");
4514 namespace selftests { namespace dw2_expand_symtabs_matching {
4516 /* A wrapper around mapped_index that builds a mock mapped_index, from
4517 the symbol list passed as parameter to the constructor. */
4518 class mock_mapped_index
4522 mock_mapped_index (const char *(&symbols)[N])
4523 : mock_mapped_index (symbols, N)
4526 /* Access the built index. */
4527 mapped_index &index ()
4531 mock_mapped_index(const mock_mapped_index &) = delete;
4532 void operator= (const mock_mapped_index &) = delete;
4535 mock_mapped_index (const char **symbols, size_t symbols_size)
4537 /* No string can live at offset zero. Add a dummy entry. */
4538 obstack_grow_str0 (&m_constant_pool, "");
4540 for (size_t i = 0; i < symbols_size; i++)
4542 const char *sym = symbols[i];
4543 size_t offset = obstack_object_size (&m_constant_pool);
4544 obstack_grow_str0 (&m_constant_pool, sym);
4545 m_symbol_table.push_back (offset);
4546 m_symbol_table.push_back (0);
4549 m_index.constant_pool = (const char *) obstack_base (&m_constant_pool);
4550 m_index.symbol_table = m_symbol_table.data ();
4551 m_index.symbol_table_slots = m_symbol_table.size () / 2;
4555 /* The built mapped_index. */
4556 mapped_index m_index{};
4558 /* The storage that the built mapped_index uses for symbol and
4559 constant pool tables. */
4560 std::vector<offset_type> m_symbol_table;
4561 auto_obstack m_constant_pool;
4564 /* Convenience function that converts a NULL pointer to a "<null>"
4565 string, to pass to print routines. */
4568 string_or_null (const char *str)
4570 return str != NULL ? str : "<null>";
4573 /* Check if a lookup_name_info built from
4574 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4575 index. EXPECTED_LIST is the list of expected matches, in expected
4576 matching order. If no match expected, then an empty list is
4577 specified. Returns true on success. On failure prints a warning
4578 indicating the file:line that failed, and returns false. */
4581 check_match (const char *file, int line,
4582 mock_mapped_index &mock_index,
4583 const char *name, symbol_name_match_type match_type,
4584 bool completion_mode,
4585 std::initializer_list<const char *> expected_list)
4587 lookup_name_info lookup_name (name, match_type, completion_mode);
4589 bool matched = true;
4591 auto mismatch = [&] (const char *expected_str,
4594 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4595 "expected=\"%s\", got=\"%s\"\n"),
4597 (match_type == symbol_name_match_type::FULL
4599 name, string_or_null (expected_str), string_or_null (got));
4603 auto expected_it = expected_list.begin ();
4604 auto expected_end = expected_list.end ();
4606 dw2_expand_symtabs_matching_symbol (mock_index.index (), lookup_name,
4608 [&] (offset_type idx)
4610 const char *matched_name = mock_index.index ().symbol_name_at (idx);
4611 const char *expected_str
4612 = expected_it == expected_end ? NULL : *expected_it++;
4614 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4615 mismatch (expected_str, matched_name);
4618 const char *expected_str
4619 = expected_it == expected_end ? NULL : *expected_it++;
4620 if (expected_str != NULL)
4621 mismatch (expected_str, NULL);
4626 /* The symbols added to the mock mapped_index for testing (in
4628 static const char *test_symbols[] = {
4637 "ns2::tmpl<int>::foo2",
4638 "(anonymous namespace)::A::B::C",
4640 /* These are used to check that the increment-last-char in the
4641 matching algorithm for completion doesn't match "t1_fund" when
4642 completing "t1_func". */
4648 /* A UTF-8 name with multi-byte sequences to make sure that
4649 cp-name-parser understands this as a single identifier ("função"
4650 is "function" in PT). */
4653 /* \377 (0xff) is Latin1 'ÿ'. */
4656 /* \377 (0xff) is Latin1 'ÿ'. */
4660 /* A name with all sorts of complications. Starts with "z" to make
4661 it easier for the completion tests below. */
4662 #define Z_SYM_NAME \
4663 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4664 "::tuple<(anonymous namespace)::ui*, " \
4665 "std::default_delete<(anonymous namespace)::ui>, void>"
4670 /* Returns true if the mapped_index::find_name_component_bounds method
4671 finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME, in
4675 check_find_bounds_finds (mapped_index &index,
4676 const char *search_name,
4677 gdb::array_view<const char *> expected_syms)
4679 lookup_name_info lookup_name (search_name,
4680 symbol_name_match_type::FULL, true);
4682 auto bounds = index.find_name_components_bounds (lookup_name);
4684 size_t distance = std::distance (bounds.first, bounds.second);
4685 if (distance != expected_syms.size ())
4688 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4690 auto nc_elem = bounds.first + exp_elem;
4691 const char *qualified = index.symbol_name_at (nc_elem->idx);
4692 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4699 /* Test the lower-level mapped_index::find_name_component_bounds
4703 test_mapped_index_find_name_component_bounds ()
4705 mock_mapped_index mock_index (test_symbols);
4707 mock_index.index ().build_name_components ();
4709 /* Test the lower-level mapped_index::find_name_component_bounds
4710 method in completion mode. */
4712 static const char *expected_syms[] = {
4717 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4718 "t1_func", expected_syms));
4721 /* Check that the increment-last-char in the name matching algorithm
4722 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4724 static const char *expected_syms1[] = {
4728 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4729 "\377", expected_syms1));
4731 static const char *expected_syms2[] = {
4734 SELF_CHECK (check_find_bounds_finds (mock_index.index (),
4735 "\377\377", expected_syms2));
4739 /* Test dw2_expand_symtabs_matching_symbol. */
4742 test_dw2_expand_symtabs_matching_symbol ()
4744 mock_mapped_index mock_index (test_symbols);
4746 /* We let all tests run until the end even if some fails, for debug
4748 bool any_mismatch = false;
4750 /* Create the expected symbols list (an initializer_list). Needed
4751 because lists have commas, and we need to pass them to CHECK,
4752 which is a macro. */
4753 #define EXPECT(...) { __VA_ARGS__ }
4755 /* Wrapper for check_match that passes down the current
4756 __FILE__/__LINE__. */
4757 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4758 any_mismatch |= !check_match (__FILE__, __LINE__, \
4760 NAME, MATCH_TYPE, COMPLETION_MODE, \
4763 /* Identity checks. */
4764 for (const char *sym : test_symbols)
4766 /* Should be able to match all existing symbols. */
4767 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4770 /* Should be able to match all existing symbols with
4772 std::string with_params = std::string (sym) + "(int)";
4773 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4776 /* Should be able to match all existing symbols with
4777 parameters and qualifiers. */
4778 with_params = std::string (sym) + " ( int ) const";
4779 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4782 /* This should really find sym, but cp-name-parser.y doesn't
4783 know about lvalue/rvalue qualifiers yet. */
4784 with_params = std::string (sym) + " ( int ) &&";
4785 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4789 /* Check that the name matching algorithm for completion doesn't get
4790 confused with Latin1 'ÿ' / 0xff. */
4792 static const char str[] = "\377";
4793 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4794 EXPECT ("\377", "\377\377123"));
4797 /* Check that the increment-last-char in the matching algorithm for
4798 completion doesn't match "t1_fund" when completing "t1_func". */
4800 static const char str[] = "t1_func";
4801 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4802 EXPECT ("t1_func", "t1_func1"));
4805 /* Check that completion mode works at each prefix of the expected
4808 static const char str[] = "function(int)";
4809 size_t len = strlen (str);
4812 for (size_t i = 1; i < len; i++)
4814 lookup.assign (str, i);
4815 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4816 EXPECT ("function"));
4820 /* While "w" is a prefix of both components, the match function
4821 should still only be called once. */
4823 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4825 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4829 /* Same, with a "complicated" symbol. */
4831 static const char str[] = Z_SYM_NAME;
4832 size_t len = strlen (str);
4835 for (size_t i = 1; i < len; i++)
4837 lookup.assign (str, i);
4838 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4839 EXPECT (Z_SYM_NAME));
4843 /* In FULL mode, an incomplete symbol doesn't match. */
4845 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4849 /* A complete symbol with parameters matches any overload, since the
4850 index has no overload info. */
4852 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4853 EXPECT ("std::zfunction", "std::zfunction2"));
4854 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4855 EXPECT ("std::zfunction", "std::zfunction2"));
4856 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4857 EXPECT ("std::zfunction", "std::zfunction2"));
4860 /* Check that whitespace is ignored appropriately. A symbol with a
4861 template argument list. */
4863 static const char expected[] = "ns::foo<int>";
4864 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4866 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4870 /* Check that whitespace is ignored appropriately. A symbol with a
4871 template argument list that includes a pointer. */
4873 static const char expected[] = "ns::foo<char*>";
4874 /* Try both completion and non-completion modes. */
4875 static const bool completion_mode[2] = {false, true};
4876 for (size_t i = 0; i < 2; i++)
4878 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4879 completion_mode[i], EXPECT (expected));
4880 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4881 completion_mode[i], EXPECT (expected));
4883 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4884 completion_mode[i], EXPECT (expected));
4885 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4886 completion_mode[i], EXPECT (expected));
4891 /* Check method qualifiers are ignored. */
4892 static const char expected[] = "ns::foo<char*>";
4893 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4894 symbol_name_match_type::FULL, true, EXPECT (expected));
4895 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4896 symbol_name_match_type::FULL, true, EXPECT (expected));
4897 CHECK_MATCH ("foo < char * > ( int ) const",
4898 symbol_name_match_type::WILD, true, EXPECT (expected));
4899 CHECK_MATCH ("foo < char * > ( int ) &&",
4900 symbol_name_match_type::WILD, true, EXPECT (expected));
4903 /* Test lookup names that don't match anything. */
4905 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4908 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4912 /* Some wild matching tests, exercising "(anonymous namespace)",
4913 which should not be confused with a parameter list. */
4915 static const char *syms[] = {
4919 "A :: B :: C ( int )",
4924 for (const char *s : syms)
4926 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4927 EXPECT ("(anonymous namespace)::A::B::C"));
4932 static const char expected[] = "ns2::tmpl<int>::foo2";
4933 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4935 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4939 SELF_CHECK (!any_mismatch);
4948 test_mapped_index_find_name_component_bounds ();
4949 test_dw2_expand_symtabs_matching_symbol ();
4952 }} // namespace selftests::dw2_expand_symtabs_matching
4954 #endif /* GDB_SELF_TEST */
4956 /* If FILE_MATCHER is NULL or if PER_CU has
4957 dwarf2_per_cu_quick_data::MARK set (see
4958 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4959 EXPANSION_NOTIFY on it. */
4962 dw2_expand_symtabs_matching_one
4963 (struct dwarf2_per_cu_data *per_cu,
4964 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4965 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4967 if (file_matcher == NULL || per_cu->v.quick->mark)
4969 bool symtab_was_null
4970 = (per_cu->v.quick->compunit_symtab == NULL);
4972 dw2_instantiate_symtab (per_cu);
4974 if (expansion_notify != NULL
4976 && per_cu->v.quick->compunit_symtab != NULL)
4977 expansion_notify (per_cu->v.quick->compunit_symtab);
4981 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4982 matched, to expand corresponding CUs that were marked. IDX is the
4983 index of the symbol name that matched. */
4986 dw2_expand_marked_cus
4987 (mapped_index &index, offset_type idx,
4988 struct objfile *objfile,
4989 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4990 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4993 offset_type *vec, vec_len, vec_idx;
4994 bool global_seen = false;
4996 vec = (offset_type *) (index.constant_pool
4997 + MAYBE_SWAP (index.symbol_table[idx + 1]));
4998 vec_len = MAYBE_SWAP (vec[0]);
4999 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5001 struct dwarf2_per_cu_data *per_cu;
5002 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5003 /* This value is only valid for index versions >= 7. */
5004 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5005 gdb_index_symbol_kind symbol_kind =
5006 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5007 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5008 /* Only check the symbol attributes if they're present.
5009 Indices prior to version 7 don't record them,
5010 and indices >= 7 may elide them for certain symbols
5011 (gold does this). */
5014 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5016 /* Work around gold/15646. */
5019 if (!is_static && global_seen)
5025 /* Only check the symbol's kind if it has one. */
5030 case VARIABLES_DOMAIN:
5031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5034 case FUNCTIONS_DOMAIN:
5035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5047 /* Don't crash on bad data. */
5048 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5049 + dwarf2_per_objfile->n_type_units))
5051 complaint (&symfile_complaints,
5052 _(".gdb_index entry has bad CU index"
5053 " [in module %s]"), objfile_name (objfile));
5057 per_cu = dw2_get_cutu (cu_index);
5058 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5063 /* If FILE_MATCHER is non-NULL, set all the
5064 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5065 that match FILE_MATCHER. */
5068 dw_expand_symtabs_matching_file_matcher
5069 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5071 if (file_matcher == NULL)
5074 objfile *const objfile = dwarf2_per_objfile->objfile;
5076 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5078 NULL, xcalloc, xfree));
5079 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5081 NULL, xcalloc, xfree));
5083 /* The rule is CUs specify all the files, including those used by
5084 any TU, so there's no need to scan TUs here. */
5086 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5089 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5090 struct quick_file_names *file_data;
5095 per_cu->v.quick->mark = 0;
5097 /* We only need to look at symtabs not already expanded. */
5098 if (per_cu->v.quick->compunit_symtab)
5101 file_data = dw2_get_file_names (per_cu);
5102 if (file_data == NULL)
5105 if (htab_find (visited_not_found.get (), file_data) != NULL)
5107 else if (htab_find (visited_found.get (), file_data) != NULL)
5109 per_cu->v.quick->mark = 1;
5113 for (j = 0; j < file_data->num_file_names; ++j)
5115 const char *this_real_name;
5117 if (file_matcher (file_data->file_names[j], false))
5119 per_cu->v.quick->mark = 1;
5123 /* Before we invoke realpath, which can get expensive when many
5124 files are involved, do a quick comparison of the basenames. */
5125 if (!basenames_may_differ
5126 && !file_matcher (lbasename (file_data->file_names[j]),
5130 this_real_name = dw2_get_real_path (objfile, file_data, j);
5131 if (file_matcher (this_real_name, false))
5133 per_cu->v.quick->mark = 1;
5138 slot = htab_find_slot (per_cu->v.quick->mark
5139 ? visited_found.get ()
5140 : visited_not_found.get (),
5147 dw2_expand_symtabs_matching
5148 (struct objfile *objfile,
5149 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5150 const lookup_name_info &lookup_name,
5151 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5152 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5153 enum search_domain kind)
5157 dw2_setup (objfile);
5159 /* index_table is NULL if OBJF_READNOW. */
5160 if (!dwarf2_per_objfile->index_table)
5163 dw_expand_symtabs_matching_file_matcher (file_matcher);
5165 mapped_index &index = *dwarf2_per_objfile->index_table;
5167 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5169 kind, [&] (offset_type idx)
5171 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5172 expansion_notify, kind);
5176 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5179 static struct compunit_symtab *
5180 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5185 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5186 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5189 if (cust->includes == NULL)
5192 for (i = 0; cust->includes[i]; ++i)
5194 struct compunit_symtab *s = cust->includes[i];
5196 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5204 static struct compunit_symtab *
5205 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5206 struct bound_minimal_symbol msymbol,
5208 struct obj_section *section,
5211 struct dwarf2_per_cu_data *data;
5212 struct compunit_symtab *result;
5214 dw2_setup (objfile);
5216 if (!objfile->psymtabs_addrmap)
5219 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5224 if (warn_if_readin && data->v.quick->compunit_symtab)
5225 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5226 paddress (get_objfile_arch (objfile), pc));
5229 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5231 gdb_assert (result != NULL);
5236 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5237 void *data, int need_fullname)
5239 dw2_setup (objfile);
5241 if (!dwarf2_per_objfile->filenames_cache)
5243 dwarf2_per_objfile->filenames_cache.emplace ();
5245 htab_up visited (htab_create_alloc (10,
5246 htab_hash_pointer, htab_eq_pointer,
5247 NULL, xcalloc, xfree));
5249 /* The rule is CUs specify all the files, including those used
5250 by any TU, so there's no need to scan TUs here. We can
5251 ignore file names coming from already-expanded CUs. */
5253 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5255 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5257 if (per_cu->v.quick->compunit_symtab)
5259 void **slot = htab_find_slot (visited.get (),
5260 per_cu->v.quick->file_names,
5263 *slot = per_cu->v.quick->file_names;
5267 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5269 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5270 struct quick_file_names *file_data;
5273 /* We only need to look at symtabs not already expanded. */
5274 if (per_cu->v.quick->compunit_symtab)
5277 file_data = dw2_get_file_names (per_cu);
5278 if (file_data == NULL)
5281 slot = htab_find_slot (visited.get (), file_data, INSERT);
5284 /* Already visited. */
5289 for (int j = 0; j < file_data->num_file_names; ++j)
5291 const char *filename = file_data->file_names[j];
5292 dwarf2_per_objfile->filenames_cache->seen (filename);
5297 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5299 gdb::unique_xmalloc_ptr<char> this_real_name;
5302 this_real_name = gdb_realpath (filename);
5303 (*fun) (filename, this_real_name.get (), data);
5308 dw2_has_symbols (struct objfile *objfile)
5313 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5316 dw2_find_last_source_symtab,
5317 dw2_forget_cached_source_info,
5318 dw2_map_symtabs_matching_filename,
5323 dw2_expand_symtabs_for_function,
5324 dw2_expand_all_symtabs,
5325 dw2_expand_symtabs_with_fullname,
5326 dw2_map_matching_symbols,
5327 dw2_expand_symtabs_matching,
5328 dw2_find_pc_sect_compunit_symtab,
5330 dw2_map_symbol_filenames
5333 /* Initialize for reading DWARF for this objfile. Return 0 if this
5334 file will use psymtabs, or 1 if using the GNU index. */
5337 dwarf2_initialize_objfile (struct objfile *objfile)
5339 /* If we're about to read full symbols, don't bother with the
5340 indices. In this case we also don't care if some other debug
5341 format is making psymtabs, because they are all about to be
5343 if ((objfile->flags & OBJF_READNOW))
5347 dwarf2_per_objfile->using_index = 1;
5348 create_all_comp_units (objfile);
5349 create_all_type_units (objfile);
5350 dwarf2_per_objfile->quick_file_names_table =
5351 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5353 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
5354 + dwarf2_per_objfile->n_type_units); ++i)
5356 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5358 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5359 struct dwarf2_per_cu_quick_data);
5362 /* Return 1 so that gdb sees the "quick" functions. However,
5363 these functions will be no-ops because we will have expanded
5365 return elf_sym_fns_gdb_index;
5368 if (dwarf2_read_index (objfile))
5369 return elf_sym_fns_gdb_index;
5371 return elf_sym_fns_lazy_psyms;
5376 /* Build a partial symbol table. */
5379 dwarf2_build_psymtabs (struct objfile *objfile)
5382 if (objfile->global_psymbols.capacity () == 0
5383 && objfile->static_psymbols.capacity () == 0)
5384 init_psymbol_list (objfile, 1024);
5388 /* This isn't really ideal: all the data we allocate on the
5389 objfile's obstack is still uselessly kept around. However,
5390 freeing it seems unsafe. */
5391 psymtab_discarder psymtabs (objfile);
5392 dwarf2_build_psymtabs_hard (objfile);
5395 CATCH (except, RETURN_MASK_ERROR)
5397 exception_print (gdb_stderr, except);
5402 /* Return the total length of the CU described by HEADER. */
5405 get_cu_length (const struct comp_unit_head *header)
5407 return header->initial_length_size + header->length;
5410 /* Return TRUE if SECT_OFF is within CU_HEADER. */
5413 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
5415 sect_offset bottom = cu_header->sect_off;
5416 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
5418 return sect_off >= bottom && sect_off < top;
5421 /* Find the base address of the compilation unit for range lists and
5422 location lists. It will normally be specified by DW_AT_low_pc.
5423 In DWARF-3 draft 4, the base address could be overridden by
5424 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5425 compilation units with discontinuous ranges. */
5428 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5430 struct attribute *attr;
5433 cu->base_address = 0;
5435 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5438 cu->base_address = attr_value_as_address (attr);
5443 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5446 cu->base_address = attr_value_as_address (attr);
5452 /* Read in the comp unit header information from the debug_info at info_ptr.
5453 Use rcuh_kind::COMPILE as the default type if not known by the caller.
5454 NOTE: This leaves members offset, first_die_offset to be filled in
5457 static const gdb_byte *
5458 read_comp_unit_head (struct comp_unit_head *cu_header,
5459 const gdb_byte *info_ptr,
5460 struct dwarf2_section_info *section,
5461 rcuh_kind section_kind)
5464 unsigned int bytes_read;
5465 const char *filename = get_section_file_name (section);
5466 bfd *abfd = get_section_bfd_owner (section);
5468 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5469 cu_header->initial_length_size = bytes_read;
5470 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
5471 info_ptr += bytes_read;
5472 cu_header->version = read_2_bytes (abfd, info_ptr);
5474 if (cu_header->version < 5)
5475 switch (section_kind)
5477 case rcuh_kind::COMPILE:
5478 cu_header->unit_type = DW_UT_compile;
5480 case rcuh_kind::TYPE:
5481 cu_header->unit_type = DW_UT_type;
5484 internal_error (__FILE__, __LINE__,
5485 _("read_comp_unit_head: invalid section_kind"));
5489 cu_header->unit_type = static_cast<enum dwarf_unit_type>
5490 (read_1_byte (abfd, info_ptr));
5492 switch (cu_header->unit_type)
5495 if (section_kind != rcuh_kind::COMPILE)
5496 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5497 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
5501 section_kind = rcuh_kind::TYPE;
5504 error (_("Dwarf Error: wrong unit_type in compilation unit header "
5505 "(is %d, should be %d or %d) [in module %s]"),
5506 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
5509 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5512 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
5515 info_ptr += bytes_read;
5516 if (cu_header->version < 5)
5518 cu_header->addr_size = read_1_byte (abfd, info_ptr);
5521 signed_addr = bfd_get_sign_extend_vma (abfd);
5522 if (signed_addr < 0)
5523 internal_error (__FILE__, __LINE__,
5524 _("read_comp_unit_head: dwarf from non elf file"));
5525 cu_header->signed_addr_p = signed_addr;
5527 if (section_kind == rcuh_kind::TYPE)
5529 LONGEST type_offset;
5531 cu_header->signature = read_8_bytes (abfd, info_ptr);
5534 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
5535 info_ptr += bytes_read;
5536 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
5537 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
5538 error (_("Dwarf Error: Too big type_offset in compilation unit "
5539 "header (is %s) [in module %s]"), plongest (type_offset),
5546 /* Helper function that returns the proper abbrev section for
5549 static struct dwarf2_section_info *
5550 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5552 struct dwarf2_section_info *abbrev;
5554 if (this_cu->is_dwz)
5555 abbrev = &dwarf2_get_dwz_file ()->abbrev;
5557 abbrev = &dwarf2_per_objfile->abbrev;
5562 /* Subroutine of read_and_check_comp_unit_head and
5563 read_and_check_type_unit_head to simplify them.
5564 Perform various error checking on the header. */
5567 error_check_comp_unit_head (struct comp_unit_head *header,
5568 struct dwarf2_section_info *section,
5569 struct dwarf2_section_info *abbrev_section)
5571 const char *filename = get_section_file_name (section);
5573 if (header->version < 2 || header->version > 5)
5574 error (_("Dwarf Error: wrong version in compilation unit header "
5575 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
5578 if (to_underlying (header->abbrev_sect_off)
5579 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
5580 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
5581 "(offset 0x%x + 6) [in module %s]"),
5582 to_underlying (header->abbrev_sect_off),
5583 to_underlying (header->sect_off),
5586 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
5587 avoid potential 32-bit overflow. */
5588 if (((ULONGEST) header->sect_off + get_cu_length (header))
5590 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
5591 "(offset 0x%x + 0) [in module %s]"),
5592 header->length, to_underlying (header->sect_off),
5596 /* Read in a CU/TU header and perform some basic error checking.
5597 The contents of the header are stored in HEADER.
5598 The result is a pointer to the start of the first DIE. */
5600 static const gdb_byte *
5601 read_and_check_comp_unit_head (struct comp_unit_head *header,
5602 struct dwarf2_section_info *section,
5603 struct dwarf2_section_info *abbrev_section,
5604 const gdb_byte *info_ptr,
5605 rcuh_kind section_kind)
5607 const gdb_byte *beg_of_comp_unit = info_ptr;
5609 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
5611 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
5613 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
5615 error_check_comp_unit_head (header, section, abbrev_section);
5620 /* Fetch the abbreviation table offset from a comp or type unit header. */
5623 read_abbrev_offset (struct dwarf2_section_info *section,
5624 sect_offset sect_off)
5626 bfd *abfd = get_section_bfd_owner (section);
5627 const gdb_byte *info_ptr;
5628 unsigned int initial_length_size, offset_size;
5631 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
5632 info_ptr = section->buffer + to_underlying (sect_off);
5633 read_initial_length (abfd, info_ptr, &initial_length_size);
5634 offset_size = initial_length_size == 4 ? 4 : 8;
5635 info_ptr += initial_length_size;
5637 version = read_2_bytes (abfd, info_ptr);
5641 /* Skip unit type and address size. */
5645 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
5648 /* Allocate a new partial symtab for file named NAME and mark this new
5649 partial symtab as being an include of PST. */
5652 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
5653 struct objfile *objfile)
5655 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
5657 if (!IS_ABSOLUTE_PATH (subpst->filename))
5659 /* It shares objfile->objfile_obstack. */
5660 subpst->dirname = pst->dirname;
5663 subpst->textlow = 0;
5664 subpst->texthigh = 0;
5666 subpst->dependencies
5667 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
5668 subpst->dependencies[0] = pst;
5669 subpst->number_of_dependencies = 1;
5671 subpst->globals_offset = 0;
5672 subpst->n_global_syms = 0;
5673 subpst->statics_offset = 0;
5674 subpst->n_static_syms = 0;
5675 subpst->compunit_symtab = NULL;
5676 subpst->read_symtab = pst->read_symtab;
5679 /* No private part is necessary for include psymtabs. This property
5680 can be used to differentiate between such include psymtabs and
5681 the regular ones. */
5682 subpst->read_symtab_private = NULL;
5685 /* Read the Line Number Program data and extract the list of files
5686 included by the source file represented by PST. Build an include
5687 partial symtab for each of these included files. */
5690 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5691 struct die_info *die,
5692 struct partial_symtab *pst)
5695 struct attribute *attr;
5697 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5699 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5701 return; /* No linetable, so no includes. */
5703 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
5704 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
5708 hash_signatured_type (const void *item)
5710 const struct signatured_type *sig_type
5711 = (const struct signatured_type *) item;
5713 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5714 return sig_type->signature;
5718 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5720 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5721 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5723 return lhs->signature == rhs->signature;
5726 /* Allocate a hash table for signatured types. */
5729 allocate_signatured_type_table (struct objfile *objfile)
5731 return htab_create_alloc_ex (41,
5732 hash_signatured_type,
5735 &objfile->objfile_obstack,
5736 hashtab_obstack_allocate,
5737 dummy_obstack_deallocate);
5740 /* A helper function to add a signatured type CU to a table. */
5743 add_signatured_type_cu_to_table (void **slot, void *datum)
5745 struct signatured_type *sigt = (struct signatured_type *) *slot;
5746 struct signatured_type ***datap = (struct signatured_type ***) datum;
5754 /* A helper for create_debug_types_hash_table. Read types from SECTION
5755 and fill them into TYPES_HTAB. It will process only type units,
5756 therefore DW_UT_type. */
5759 create_debug_type_hash_table (struct dwo_file *dwo_file,
5760 dwarf2_section_info *section, htab_t &types_htab,
5761 rcuh_kind section_kind)
5763 struct objfile *objfile = dwarf2_per_objfile->objfile;
5764 struct dwarf2_section_info *abbrev_section;
5766 const gdb_byte *info_ptr, *end_ptr;
5768 abbrev_section = (dwo_file != NULL
5769 ? &dwo_file->sections.abbrev
5770 : &dwarf2_per_objfile->abbrev);
5772 if (dwarf_read_debug)
5773 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5774 get_section_name (section),
5775 get_section_file_name (abbrev_section));
5777 dwarf2_read_section (objfile, section);
5778 info_ptr = section->buffer;
5780 if (info_ptr == NULL)
5783 /* We can't set abfd until now because the section may be empty or
5784 not present, in which case the bfd is unknown. */
5785 abfd = get_section_bfd_owner (section);
5787 /* We don't use init_cutu_and_read_dies_simple, or some such, here
5788 because we don't need to read any dies: the signature is in the
5791 end_ptr = info_ptr + section->size;
5792 while (info_ptr < end_ptr)
5794 struct signatured_type *sig_type;
5795 struct dwo_unit *dwo_tu;
5797 const gdb_byte *ptr = info_ptr;
5798 struct comp_unit_head header;
5799 unsigned int length;
5801 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5803 /* Initialize it due to a false compiler warning. */
5804 header.signature = -1;
5805 header.type_cu_offset_in_tu = (cu_offset) -1;
5807 /* We need to read the type's signature in order to build the hash
5808 table, but we don't need anything else just yet. */
5810 ptr = read_and_check_comp_unit_head (&header, section,
5811 abbrev_section, ptr, section_kind);
5813 length = get_cu_length (&header);
5815 /* Skip dummy type units. */
5816 if (ptr >= info_ptr + length
5817 || peek_abbrev_code (abfd, ptr) == 0
5818 || header.unit_type != DW_UT_type)
5824 if (types_htab == NULL)
5827 types_htab = allocate_dwo_unit_table (objfile);
5829 types_htab = allocate_signatured_type_table (objfile);
5835 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5837 dwo_tu->dwo_file = dwo_file;
5838 dwo_tu->signature = header.signature;
5839 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5840 dwo_tu->section = section;
5841 dwo_tu->sect_off = sect_off;
5842 dwo_tu->length = length;
5846 /* N.B.: type_offset is not usable if this type uses a DWO file.
5847 The real type_offset is in the DWO file. */
5849 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5850 struct signatured_type);
5851 sig_type->signature = header.signature;
5852 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
5853 sig_type->per_cu.objfile = objfile;
5854 sig_type->per_cu.is_debug_types = 1;
5855 sig_type->per_cu.section = section;
5856 sig_type->per_cu.sect_off = sect_off;
5857 sig_type->per_cu.length = length;
5860 slot = htab_find_slot (types_htab,
5861 dwo_file ? (void*) dwo_tu : (void *) sig_type,
5863 gdb_assert (slot != NULL);
5866 sect_offset dup_sect_off;
5870 const struct dwo_unit *dup_tu
5871 = (const struct dwo_unit *) *slot;
5873 dup_sect_off = dup_tu->sect_off;
5877 const struct signatured_type *dup_tu
5878 = (const struct signatured_type *) *slot;
5880 dup_sect_off = dup_tu->per_cu.sect_off;
5883 complaint (&symfile_complaints,
5884 _("debug type entry at offset 0x%x is duplicate to"
5885 " the entry at offset 0x%x, signature %s"),
5886 to_underlying (sect_off), to_underlying (dup_sect_off),
5887 hex_string (header.signature));
5889 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
5891 if (dwarf_read_debug > 1)
5892 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
5893 to_underlying (sect_off),
5894 hex_string (header.signature));
5900 /* Create the hash table of all entries in the .debug_types
5901 (or .debug_types.dwo) section(s).
5902 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
5903 otherwise it is NULL.
5905 The result is a pointer to the hash table or NULL if there are no types.
5907 Note: This function processes DWO files only, not DWP files. */
5910 create_debug_types_hash_table (struct dwo_file *dwo_file,
5911 VEC (dwarf2_section_info_def) *types,
5915 struct dwarf2_section_info *section;
5917 if (VEC_empty (dwarf2_section_info_def, types))
5921 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5923 create_debug_type_hash_table (dwo_file, section, types_htab,
5927 /* Create the hash table of all entries in the .debug_types section,
5928 and initialize all_type_units.
5929 The result is zero if there is an error (e.g. missing .debug_types section),
5930 otherwise non-zero. */
5933 create_all_type_units (struct objfile *objfile)
5935 htab_t types_htab = NULL;
5936 struct signatured_type **iter;
5938 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5939 rcuh_kind::COMPILE);
5940 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5941 if (types_htab == NULL)
5943 dwarf2_per_objfile->signatured_types = NULL;
5947 dwarf2_per_objfile->signatured_types = types_htab;
5949 dwarf2_per_objfile->n_type_units
5950 = dwarf2_per_objfile->n_allocated_type_units
5951 = htab_elements (types_htab);
5952 dwarf2_per_objfile->all_type_units =
5953 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5954 iter = &dwarf2_per_objfile->all_type_units[0];
5955 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5956 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5957 == dwarf2_per_objfile->n_type_units);
5962 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5963 If SLOT is non-NULL, it is the entry to use in the hash table.
5964 Otherwise we find one. */
5966 static struct signatured_type *
5967 add_type_unit (ULONGEST sig, void **slot)
5969 struct objfile *objfile = dwarf2_per_objfile->objfile;
5970 int n_type_units = dwarf2_per_objfile->n_type_units;
5971 struct signatured_type *sig_type;
5973 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5975 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5977 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5978 dwarf2_per_objfile->n_allocated_type_units = 1;
5979 dwarf2_per_objfile->n_allocated_type_units *= 2;
5980 dwarf2_per_objfile->all_type_units
5981 = XRESIZEVEC (struct signatured_type *,
5982 dwarf2_per_objfile->all_type_units,
5983 dwarf2_per_objfile->n_allocated_type_units);
5984 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5986 dwarf2_per_objfile->n_type_units = n_type_units;
5988 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5989 struct signatured_type);
5990 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5991 sig_type->signature = sig;
5992 sig_type->per_cu.is_debug_types = 1;
5993 if (dwarf2_per_objfile->using_index)
5995 sig_type->per_cu.v.quick =
5996 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5997 struct dwarf2_per_cu_quick_data);
6002 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6005 gdb_assert (*slot == NULL);
6007 /* The rest of sig_type must be filled in by the caller. */
6011 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6012 Fill in SIG_ENTRY with DWO_ENTRY. */
6015 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
6016 struct signatured_type *sig_entry,
6017 struct dwo_unit *dwo_entry)
6019 /* Make sure we're not clobbering something we don't expect to. */
6020 gdb_assert (! sig_entry->per_cu.queued);
6021 gdb_assert (sig_entry->per_cu.cu == NULL);
6022 if (dwarf2_per_objfile->using_index)
6024 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6025 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6028 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6029 gdb_assert (sig_entry->signature == dwo_entry->signature);
6030 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6031 gdb_assert (sig_entry->type_unit_group == NULL);
6032 gdb_assert (sig_entry->dwo_unit == NULL);
6034 sig_entry->per_cu.section = dwo_entry->section;
6035 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6036 sig_entry->per_cu.length = dwo_entry->length;
6037 sig_entry->per_cu.reading_dwo_directly = 1;
6038 sig_entry->per_cu.objfile = objfile;
6039 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6040 sig_entry->dwo_unit = dwo_entry;
6043 /* Subroutine of lookup_signatured_type.
6044 If we haven't read the TU yet, create the signatured_type data structure
6045 for a TU to be read in directly from a DWO file, bypassing the stub.
6046 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6047 using .gdb_index, then when reading a CU we want to stay in the DWO file
6048 containing that CU. Otherwise we could end up reading several other DWO
6049 files (due to comdat folding) to process the transitive closure of all the
6050 mentioned TUs, and that can be slow. The current DWO file will have every
6051 type signature that it needs.
6052 We only do this for .gdb_index because in the psymtab case we already have
6053 to read all the DWOs to build the type unit groups. */
6055 static struct signatured_type *
6056 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6058 struct objfile *objfile = dwarf2_per_objfile->objfile;
6059 struct dwo_file *dwo_file;
6060 struct dwo_unit find_dwo_entry, *dwo_entry;
6061 struct signatured_type find_sig_entry, *sig_entry;
6064 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6066 /* If TU skeletons have been removed then we may not have read in any
6068 if (dwarf2_per_objfile->signatured_types == NULL)
6070 dwarf2_per_objfile->signatured_types
6071 = allocate_signatured_type_table (objfile);
6074 /* We only ever need to read in one copy of a signatured type.
6075 Use the global signatured_types array to do our own comdat-folding
6076 of types. If this is the first time we're reading this TU, and
6077 the TU has an entry in .gdb_index, replace the recorded data from
6078 .gdb_index with this TU. */
6080 find_sig_entry.signature = sig;
6081 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6082 &find_sig_entry, INSERT);
6083 sig_entry = (struct signatured_type *) *slot;
6085 /* We can get here with the TU already read, *or* in the process of being
6086 read. Don't reassign the global entry to point to this DWO if that's
6087 the case. Also note that if the TU is already being read, it may not
6088 have come from a DWO, the program may be a mix of Fission-compiled
6089 code and non-Fission-compiled code. */
6091 /* Have we already tried to read this TU?
6092 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6093 needn't exist in the global table yet). */
6094 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6097 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6098 dwo_unit of the TU itself. */
6099 dwo_file = cu->dwo_unit->dwo_file;
6101 /* Ok, this is the first time we're reading this TU. */
6102 if (dwo_file->tus == NULL)
6104 find_dwo_entry.signature = sig;
6105 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6106 if (dwo_entry == NULL)
6109 /* If the global table doesn't have an entry for this TU, add one. */
6110 if (sig_entry == NULL)
6111 sig_entry = add_type_unit (sig, slot);
6113 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6114 sig_entry->per_cu.tu_read = 1;
6118 /* Subroutine of lookup_signatured_type.
6119 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6120 then try the DWP file. If the TU stub (skeleton) has been removed then
6121 it won't be in .gdb_index. */
6123 static struct signatured_type *
6124 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6126 struct objfile *objfile = dwarf2_per_objfile->objfile;
6127 struct dwp_file *dwp_file = get_dwp_file ();
6128 struct dwo_unit *dwo_entry;
6129 struct signatured_type find_sig_entry, *sig_entry;
6132 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6133 gdb_assert (dwp_file != NULL);
6135 /* If TU skeletons have been removed then we may not have read in any
6137 if (dwarf2_per_objfile->signatured_types == NULL)
6139 dwarf2_per_objfile->signatured_types
6140 = allocate_signatured_type_table (objfile);
6143 find_sig_entry.signature = sig;
6144 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6145 &find_sig_entry, INSERT);
6146 sig_entry = (struct signatured_type *) *slot;
6148 /* Have we already tried to read this TU?
6149 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6150 needn't exist in the global table yet). */
6151 if (sig_entry != NULL)
6154 if (dwp_file->tus == NULL)
6156 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
6157 sig, 1 /* is_debug_types */);
6158 if (dwo_entry == NULL)
6161 sig_entry = add_type_unit (sig, slot);
6162 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
6167 /* Lookup a signature based type for DW_FORM_ref_sig8.
6168 Returns NULL if signature SIG is not present in the table.
6169 It is up to the caller to complain about this. */
6171 static struct signatured_type *
6172 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6175 && dwarf2_per_objfile->using_index)
6177 /* We're in a DWO/DWP file, and we're using .gdb_index.
6178 These cases require special processing. */
6179 if (get_dwp_file () == NULL)
6180 return lookup_dwo_signatured_type (cu, sig);
6182 return lookup_dwp_signatured_type (cu, sig);
6186 struct signatured_type find_entry, *entry;
6188 if (dwarf2_per_objfile->signatured_types == NULL)
6190 find_entry.signature = sig;
6191 entry = ((struct signatured_type *)
6192 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
6197 /* Low level DIE reading support. */
6199 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6202 init_cu_die_reader (struct die_reader_specs *reader,
6203 struct dwarf2_cu *cu,
6204 struct dwarf2_section_info *section,
6205 struct dwo_file *dwo_file)
6207 gdb_assert (section->readin && section->buffer != NULL);
6208 reader->abfd = get_section_bfd_owner (section);
6210 reader->dwo_file = dwo_file;
6211 reader->die_section = section;
6212 reader->buffer = section->buffer;
6213 reader->buffer_end = section->buffer + section->size;
6214 reader->comp_dir = NULL;
6217 /* Subroutine of init_cutu_and_read_dies to simplify it.
6218 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6219 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
6222 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6223 from it to the DIE in the DWO. If NULL we are skipping the stub.
6224 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6225 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6226 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6227 STUB_COMP_DIR may be non-NULL.
6228 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
6229 are filled in with the info of the DIE from the DWO file.
6230 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
6231 provided an abbrev table to use.
6232 The result is non-zero if a valid (non-dummy) DIE was found. */
6235 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6236 struct dwo_unit *dwo_unit,
6237 int abbrev_table_provided,
6238 struct die_info *stub_comp_unit_die,
6239 const char *stub_comp_dir,
6240 struct die_reader_specs *result_reader,
6241 const gdb_byte **result_info_ptr,
6242 struct die_info **result_comp_unit_die,
6243 int *result_has_children)
6245 struct objfile *objfile = dwarf2_per_objfile->objfile;
6246 struct dwarf2_cu *cu = this_cu->cu;
6247 struct dwarf2_section_info *section;
6249 const gdb_byte *begin_info_ptr, *info_ptr;
6250 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6251 int i,num_extra_attrs;
6252 struct dwarf2_section_info *dwo_abbrev_section;
6253 struct attribute *attr;
6254 struct die_info *comp_unit_die;
6256 /* At most one of these may be provided. */
6257 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6259 /* These attributes aren't processed until later:
6260 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6261 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6262 referenced later. However, these attributes are found in the stub
6263 which we won't have later. In order to not impose this complication
6264 on the rest of the code, we read them here and copy them to the
6273 if (stub_comp_unit_die != NULL)
6275 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6277 if (! this_cu->is_debug_types)
6278 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6279 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6280 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6281 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6282 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6284 /* There should be a DW_AT_addr_base attribute here (if needed).
6285 We need the value before we can process DW_FORM_GNU_addr_index. */
6287 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
6289 cu->addr_base = DW_UNSND (attr);
6291 /* There should be a DW_AT_ranges_base attribute here (if needed).
6292 We need the value before we can process DW_AT_ranges. */
6293 cu->ranges_base = 0;
6294 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
6296 cu->ranges_base = DW_UNSND (attr);
6298 else if (stub_comp_dir != NULL)
6300 /* Reconstruct the comp_dir attribute to simplify the code below. */
6301 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6302 comp_dir->name = DW_AT_comp_dir;
6303 comp_dir->form = DW_FORM_string;
6304 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6305 DW_STRING (comp_dir) = stub_comp_dir;
6308 /* Set up for reading the DWO CU/TU. */
6309 cu->dwo_unit = dwo_unit;
6310 section = dwo_unit->section;
6311 dwarf2_read_section (objfile, section);
6312 abfd = get_section_bfd_owner (section);
6313 begin_info_ptr = info_ptr = (section->buffer
6314 + to_underlying (dwo_unit->sect_off));
6315 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6316 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
6318 if (this_cu->is_debug_types)
6320 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6322 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6324 info_ptr, rcuh_kind::TYPE);
6325 /* This is not an assert because it can be caused by bad debug info. */
6326 if (sig_type->signature != cu->header.signature)
6328 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6329 " TU at offset 0x%x [in module %s]"),
6330 hex_string (sig_type->signature),
6331 hex_string (cu->header.signature),
6332 to_underlying (dwo_unit->sect_off),
6333 bfd_get_filename (abfd));
6335 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6336 /* For DWOs coming from DWP files, we don't know the CU length
6337 nor the type's offset in the TU until now. */
6338 dwo_unit->length = get_cu_length (&cu->header);
6339 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6341 /* Establish the type offset that can be used to lookup the type.
6342 For DWO files, we don't know it until now. */
6343 sig_type->type_offset_in_section
6344 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6348 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6350 info_ptr, rcuh_kind::COMPILE);
6351 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6352 /* For DWOs coming from DWP files, we don't know the CU length
6354 dwo_unit->length = get_cu_length (&cu->header);
6357 /* Replace the CU's original abbrev table with the DWO's.
6358 Reminder: We can't read the abbrev table until we've read the header. */
6359 if (abbrev_table_provided)
6361 /* Don't free the provided abbrev table, the caller of
6362 init_cutu_and_read_dies owns it. */
6363 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6364 /* Ensure the DWO abbrev table gets freed. */
6365 make_cleanup (dwarf2_free_abbrev_table, cu);
6369 dwarf2_free_abbrev_table (cu);
6370 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
6371 /* Leave any existing abbrev table cleanup as is. */
6374 /* Read in the die, but leave space to copy over the attributes
6375 from the stub. This has the benefit of simplifying the rest of
6376 the code - all the work to maintain the illusion of a single
6377 DW_TAG_{compile,type}_unit DIE is done here. */
6378 num_extra_attrs = ((stmt_list != NULL)
6382 + (comp_dir != NULL));
6383 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6384 result_has_children, num_extra_attrs);
6386 /* Copy over the attributes from the stub to the DIE we just read in. */
6387 comp_unit_die = *result_comp_unit_die;
6388 i = comp_unit_die->num_attrs;
6389 if (stmt_list != NULL)
6390 comp_unit_die->attrs[i++] = *stmt_list;
6392 comp_unit_die->attrs[i++] = *low_pc;
6393 if (high_pc != NULL)
6394 comp_unit_die->attrs[i++] = *high_pc;
6396 comp_unit_die->attrs[i++] = *ranges;
6397 if (comp_dir != NULL)
6398 comp_unit_die->attrs[i++] = *comp_dir;
6399 comp_unit_die->num_attrs += num_extra_attrs;
6401 if (dwarf_die_debug)
6403 fprintf_unfiltered (gdb_stdlog,
6404 "Read die from %s@0x%x of %s:\n",
6405 get_section_name (section),
6406 (unsigned) (begin_info_ptr - section->buffer),
6407 bfd_get_filename (abfd));
6408 dump_die (comp_unit_die, dwarf_die_debug);
6411 /* Save the comp_dir attribute. If there is no DWP file then we'll read
6412 TUs by skipping the stub and going directly to the entry in the DWO file.
6413 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
6414 to get it via circuitous means. Blech. */
6415 if (comp_dir != NULL)
6416 result_reader->comp_dir = DW_STRING (comp_dir);
6418 /* Skip dummy compilation units. */
6419 if (info_ptr >= begin_info_ptr + dwo_unit->length
6420 || peek_abbrev_code (abfd, info_ptr) == 0)
6423 *result_info_ptr = info_ptr;
6427 /* Subroutine of init_cutu_and_read_dies to simplify it.
6428 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6429 Returns NULL if the specified DWO unit cannot be found. */
6431 static struct dwo_unit *
6432 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6433 struct die_info *comp_unit_die)
6435 struct dwarf2_cu *cu = this_cu->cu;
6437 struct dwo_unit *dwo_unit;
6438 const char *comp_dir, *dwo_name;
6440 gdb_assert (cu != NULL);
6442 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6443 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6444 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6446 if (this_cu->is_debug_types)
6448 struct signatured_type *sig_type;
6450 /* Since this_cu is the first member of struct signatured_type,
6451 we can go from a pointer to one to a pointer to the other. */
6452 sig_type = (struct signatured_type *) this_cu;
6453 signature = sig_type->signature;
6454 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6458 struct attribute *attr;
6460 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6462 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6464 dwo_name, objfile_name (this_cu->objfile));
6465 signature = DW_UNSND (attr);
6466 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6473 /* Subroutine of init_cutu_and_read_dies to simplify it.
6474 See it for a description of the parameters.
6475 Read a TU directly from a DWO file, bypassing the stub.
6477 Note: This function could be a little bit simpler if we shared cleanups
6478 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
6479 to do, so we keep this function self-contained. Or we could move this
6480 into our caller, but it's complex enough already. */
6483 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6484 int use_existing_cu, int keep,
6485 die_reader_func_ftype *die_reader_func,
6488 struct dwarf2_cu *cu;
6489 struct signatured_type *sig_type;
6490 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6491 struct die_reader_specs reader;
6492 const gdb_byte *info_ptr;
6493 struct die_info *comp_unit_die;
6496 /* Verify we can do the following downcast, and that we have the
6498 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6499 sig_type = (struct signatured_type *) this_cu;
6500 gdb_assert (sig_type->dwo_unit != NULL);
6502 cleanups = make_cleanup (null_cleanup, NULL);
6504 if (use_existing_cu && this_cu->cu != NULL)
6506 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6508 /* There's no need to do the rereading_dwo_cu handling that
6509 init_cutu_and_read_dies does since we don't read the stub. */
6513 /* If !use_existing_cu, this_cu->cu must be NULL. */
6514 gdb_assert (this_cu->cu == NULL);
6515 cu = XNEW (struct dwarf2_cu);
6516 init_one_comp_unit (cu, this_cu);
6517 /* If an error occurs while loading, release our storage. */
6518 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6521 /* A future optimization, if needed, would be to use an existing
6522 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6523 could share abbrev tables. */
6525 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6526 0 /* abbrev_table_provided */,
6527 NULL /* stub_comp_unit_die */,
6528 sig_type->dwo_unit->dwo_file->comp_dir,
6530 &comp_unit_die, &has_children) == 0)
6533 do_cleanups (cleanups);
6537 /* All the "real" work is done here. */
6538 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6540 /* This duplicates the code in init_cutu_and_read_dies,
6541 but the alternative is making the latter more complex.
6542 This function is only for the special case of using DWO files directly:
6543 no point in overly complicating the general case just to handle this. */
6544 if (free_cu_cleanup != NULL)
6548 /* We've successfully allocated this compilation unit. Let our
6549 caller clean it up when finished with it. */
6550 discard_cleanups (free_cu_cleanup);
6552 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6553 So we have to manually free the abbrev table. */
6554 dwarf2_free_abbrev_table (cu);
6556 /* Link this CU into read_in_chain. */
6557 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6558 dwarf2_per_objfile->read_in_chain = this_cu;
6561 do_cleanups (free_cu_cleanup);
6564 do_cleanups (cleanups);
6567 /* Initialize a CU (or TU) and read its DIEs.
6568 If the CU defers to a DWO file, read the DWO file as well.
6570 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6571 Otherwise the table specified in the comp unit header is read in and used.
6572 This is an optimization for when we already have the abbrev table.
6574 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6575 Otherwise, a new CU is allocated with xmalloc.
6577 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6578 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
6580 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6581 linker) then DIE_READER_FUNC will not get called. */
6584 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
6585 struct abbrev_table *abbrev_table,
6586 int use_existing_cu, int keep,
6587 die_reader_func_ftype *die_reader_func,
6590 struct objfile *objfile = dwarf2_per_objfile->objfile;
6591 struct dwarf2_section_info *section = this_cu->section;
6592 bfd *abfd = get_section_bfd_owner (section);
6593 struct dwarf2_cu *cu;
6594 const gdb_byte *begin_info_ptr, *info_ptr;
6595 struct die_reader_specs reader;
6596 struct die_info *comp_unit_die;
6598 struct attribute *attr;
6599 struct cleanup *cleanups, *free_cu_cleanup = NULL;
6600 struct signatured_type *sig_type = NULL;
6601 struct dwarf2_section_info *abbrev_section;
6602 /* Non-zero if CU currently points to a DWO file and we need to
6603 reread it. When this happens we need to reread the skeleton die
6604 before we can reread the DWO file (this only applies to CUs, not TUs). */
6605 int rereading_dwo_cu = 0;
6607 if (dwarf_die_debug)
6608 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6609 this_cu->is_debug_types ? "type" : "comp",
6610 to_underlying (this_cu->sect_off));
6612 if (use_existing_cu)
6615 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6616 file (instead of going through the stub), short-circuit all of this. */
6617 if (this_cu->reading_dwo_directly)
6619 /* Narrow down the scope of possibilities to have to understand. */
6620 gdb_assert (this_cu->is_debug_types);
6621 gdb_assert (abbrev_table == NULL);
6622 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
6623 die_reader_func, data);
6627 cleanups = make_cleanup (null_cleanup, NULL);
6629 /* This is cheap if the section is already read in. */
6630 dwarf2_read_section (objfile, section);
6632 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6634 abbrev_section = get_abbrev_section_for_cu (this_cu);
6636 if (use_existing_cu && this_cu->cu != NULL)
6639 /* If this CU is from a DWO file we need to start over, we need to
6640 refetch the attributes from the skeleton CU.
6641 This could be optimized by retrieving those attributes from when we
6642 were here the first time: the previous comp_unit_die was stored in
6643 comp_unit_obstack. But there's no data yet that we need this
6645 if (cu->dwo_unit != NULL)
6646 rereading_dwo_cu = 1;
6650 /* If !use_existing_cu, this_cu->cu must be NULL. */
6651 gdb_assert (this_cu->cu == NULL);
6652 cu = XNEW (struct dwarf2_cu);
6653 init_one_comp_unit (cu, this_cu);
6654 /* If an error occurs while loading, release our storage. */
6655 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
6658 /* Get the header. */
6659 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6661 /* We already have the header, there's no need to read it in again. */
6662 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6666 if (this_cu->is_debug_types)
6668 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6669 abbrev_section, info_ptr,
6672 /* Since per_cu is the first member of struct signatured_type,
6673 we can go from a pointer to one to a pointer to the other. */
6674 sig_type = (struct signatured_type *) this_cu;
6675 gdb_assert (sig_type->signature == cu->header.signature);
6676 gdb_assert (sig_type->type_offset_in_tu
6677 == cu->header.type_cu_offset_in_tu);
6678 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6680 /* LENGTH has not been set yet for type units if we're
6681 using .gdb_index. */
6682 this_cu->length = get_cu_length (&cu->header);
6684 /* Establish the type offset that can be used to lookup the type. */
6685 sig_type->type_offset_in_section =
6686 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6688 this_cu->dwarf_version = cu->header.version;
6692 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
6695 rcuh_kind::COMPILE);
6697 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6698 gdb_assert (this_cu->length == get_cu_length (&cu->header));
6699 this_cu->dwarf_version = cu->header.version;
6703 /* Skip dummy compilation units. */
6704 if (info_ptr >= begin_info_ptr + this_cu->length
6705 || peek_abbrev_code (abfd, info_ptr) == 0)
6707 do_cleanups (cleanups);
6711 /* If we don't have them yet, read the abbrevs for this compilation unit.
6712 And if we need to read them now, make sure they're freed when we're
6713 done. Note that it's important that if the CU had an abbrev table
6714 on entry we don't free it when we're done: Somewhere up the call stack
6715 it may be in use. */
6716 if (abbrev_table != NULL)
6718 gdb_assert (cu->abbrev_table == NULL);
6719 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6720 cu->abbrev_table = abbrev_table;
6722 else if (cu->abbrev_table == NULL)
6724 dwarf2_read_abbrevs (cu, abbrev_section);
6725 make_cleanup (dwarf2_free_abbrev_table, cu);
6727 else if (rereading_dwo_cu)
6729 dwarf2_free_abbrev_table (cu);
6730 dwarf2_read_abbrevs (cu, abbrev_section);
6733 /* Read the top level CU/TU die. */
6734 init_cu_die_reader (&reader, cu, section, NULL);
6735 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6737 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6739 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6740 DWO CU, that this test will fail (the attribute will not be present). */
6741 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
6744 struct dwo_unit *dwo_unit;
6745 struct die_info *dwo_comp_unit_die;
6749 complaint (&symfile_complaints,
6750 _("compilation unit with DW_AT_GNU_dwo_name"
6751 " has children (offset 0x%x) [in module %s]"),
6752 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6754 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6755 if (dwo_unit != NULL)
6757 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6758 abbrev_table != NULL,
6759 comp_unit_die, NULL,
6761 &dwo_comp_unit_die, &has_children) == 0)
6764 do_cleanups (cleanups);
6767 comp_unit_die = dwo_comp_unit_die;
6771 /* Yikes, we couldn't find the rest of the DIE, we only have
6772 the stub. A complaint has already been logged. There's
6773 not much more we can do except pass on the stub DIE to
6774 die_reader_func. We don't want to throw an error on bad
6779 /* All of the above is setup for this call. Yikes. */
6780 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6782 /* Done, clean up. */
6783 if (free_cu_cleanup != NULL)
6787 /* We've successfully allocated this compilation unit. Let our
6788 caller clean it up when finished with it. */
6789 discard_cleanups (free_cu_cleanup);
6791 /* We can only discard free_cu_cleanup and all subsequent cleanups.
6792 So we have to manually free the abbrev table. */
6793 dwarf2_free_abbrev_table (cu);
6795 /* Link this CU into read_in_chain. */
6796 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6797 dwarf2_per_objfile->read_in_chain = this_cu;
6800 do_cleanups (free_cu_cleanup);
6803 do_cleanups (cleanups);
6806 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
6807 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
6808 to have already done the lookup to find the DWO file).
6810 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6811 THIS_CU->is_debug_types, but nothing else.
6813 We fill in THIS_CU->length.
6815 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
6816 linker) then DIE_READER_FUNC will not get called.
6818 THIS_CU->cu is always freed when done.
6819 This is done in order to not leave THIS_CU->cu in a state where we have
6820 to care whether it refers to the "main" CU or the DWO CU. */
6823 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
6824 struct dwo_file *dwo_file,
6825 die_reader_func_ftype *die_reader_func,
6828 struct objfile *objfile = dwarf2_per_objfile->objfile;
6829 struct dwarf2_section_info *section = this_cu->section;
6830 bfd *abfd = get_section_bfd_owner (section);
6831 struct dwarf2_section_info *abbrev_section;
6832 struct dwarf2_cu cu;
6833 const gdb_byte *begin_info_ptr, *info_ptr;
6834 struct die_reader_specs reader;
6835 struct cleanup *cleanups;
6836 struct die_info *comp_unit_die;
6839 if (dwarf_die_debug)
6840 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
6841 this_cu->is_debug_types ? "type" : "comp",
6842 to_underlying (this_cu->sect_off));
6844 gdb_assert (this_cu->cu == NULL);
6846 abbrev_section = (dwo_file != NULL
6847 ? &dwo_file->sections.abbrev
6848 : get_abbrev_section_for_cu (this_cu));
6850 /* This is cheap if the section is already read in. */
6851 dwarf2_read_section (objfile, section);
6853 init_one_comp_unit (&cu, this_cu);
6855 cleanups = make_cleanup (free_stack_comp_unit, &cu);
6857 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6858 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
6859 abbrev_section, info_ptr,
6860 (this_cu->is_debug_types
6862 : rcuh_kind::COMPILE));
6864 this_cu->length = get_cu_length (&cu.header);
6866 /* Skip dummy compilation units. */
6867 if (info_ptr >= begin_info_ptr + this_cu->length
6868 || peek_abbrev_code (abfd, info_ptr) == 0)
6870 do_cleanups (cleanups);
6874 dwarf2_read_abbrevs (&cu, abbrev_section);
6875 make_cleanup (dwarf2_free_abbrev_table, &cu);
6877 init_cu_die_reader (&reader, &cu, section, dwo_file);
6878 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
6880 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
6882 do_cleanups (cleanups);
6885 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
6886 does not lookup the specified DWO file.
6887 This cannot be used to read DWO files.
6889 THIS_CU->cu is always freed when done.
6890 This is done in order to not leave THIS_CU->cu in a state where we have
6891 to care whether it refers to the "main" CU or the DWO CU.
6892 We can revisit this if the data shows there's a performance issue. */
6895 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
6896 die_reader_func_ftype *die_reader_func,
6899 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
6902 /* Type Unit Groups.
6904 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6905 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6906 so that all types coming from the same compilation (.o file) are grouped
6907 together. A future step could be to put the types in the same symtab as
6908 the CU the types ultimately came from. */
6911 hash_type_unit_group (const void *item)
6913 const struct type_unit_group *tu_group
6914 = (const struct type_unit_group *) item;
6916 return hash_stmt_list_entry (&tu_group->hash);
6920 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6922 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6923 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6925 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6928 /* Allocate a hash table for type unit groups. */
6931 allocate_type_unit_groups_table (void)
6933 return htab_create_alloc_ex (3,
6934 hash_type_unit_group,
6937 &dwarf2_per_objfile->objfile->objfile_obstack,
6938 hashtab_obstack_allocate,
6939 dummy_obstack_deallocate);
6942 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6943 partial symtabs. We combine several TUs per psymtab to not let the size
6944 of any one psymtab grow too big. */
6945 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6946 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6948 /* Helper routine for get_type_unit_group.
6949 Create the type_unit_group object used to hold one or more TUs. */
6951 static struct type_unit_group *
6952 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6954 struct objfile *objfile = dwarf2_per_objfile->objfile;
6955 struct dwarf2_per_cu_data *per_cu;
6956 struct type_unit_group *tu_group;
6958 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6959 struct type_unit_group);
6960 per_cu = &tu_group->per_cu;
6961 per_cu->objfile = objfile;
6963 if (dwarf2_per_objfile->using_index)
6965 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6966 struct dwarf2_per_cu_quick_data);
6970 unsigned int line_offset = to_underlying (line_offset_struct);
6971 struct partial_symtab *pst;
6974 /* Give the symtab a useful name for debug purposes. */
6975 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6976 name = xstrprintf ("<type_units_%d>",
6977 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6979 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6981 pst = create_partial_symtab (per_cu, name);
6987 tu_group->hash.dwo_unit = cu->dwo_unit;
6988 tu_group->hash.line_sect_off = line_offset_struct;
6993 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6994 STMT_LIST is a DW_AT_stmt_list attribute. */
6996 static struct type_unit_group *
6997 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6999 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7000 struct type_unit_group *tu_group;
7002 unsigned int line_offset;
7003 struct type_unit_group type_unit_group_for_lookup;
7005 if (dwarf2_per_objfile->type_unit_groups == NULL)
7007 dwarf2_per_objfile->type_unit_groups =
7008 allocate_type_unit_groups_table ();
7011 /* Do we need to create a new group, or can we use an existing one? */
7015 line_offset = DW_UNSND (stmt_list);
7016 ++tu_stats->nr_symtab_sharers;
7020 /* Ugh, no stmt_list. Rare, but we have to handle it.
7021 We can do various things here like create one group per TU or
7022 spread them over multiple groups to split up the expansion work.
7023 To avoid worst case scenarios (too many groups or too large groups)
7024 we, umm, group them in bunches. */
7025 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7026 | (tu_stats->nr_stmt_less_type_units
7027 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7028 ++tu_stats->nr_stmt_less_type_units;
7031 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7032 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7033 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7034 &type_unit_group_for_lookup, INSERT);
7037 tu_group = (struct type_unit_group *) *slot;
7038 gdb_assert (tu_group != NULL);
7042 sect_offset line_offset_struct = (sect_offset) line_offset;
7043 tu_group = create_type_unit_group (cu, line_offset_struct);
7045 ++tu_stats->nr_symtabs;
7051 /* Partial symbol tables. */
7053 /* Create a psymtab named NAME and assign it to PER_CU.
7055 The caller must fill in the following details:
7056 dirname, textlow, texthigh. */
7058 static struct partial_symtab *
7059 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7061 struct objfile *objfile = per_cu->objfile;
7062 struct partial_symtab *pst;
7064 pst = start_psymtab_common (objfile, name, 0,
7065 objfile->global_psymbols,
7066 objfile->static_psymbols);
7068 pst->psymtabs_addrmap_supported = 1;
7070 /* This is the glue that links PST into GDB's symbol API. */
7071 pst->read_symtab_private = per_cu;
7072 pst->read_symtab = dwarf2_read_symtab;
7073 per_cu->v.psymtab = pst;
7078 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7081 struct process_psymtab_comp_unit_data
7083 /* True if we are reading a DW_TAG_partial_unit. */
7085 int want_partial_unit;
7087 /* The "pretend" language that is used if the CU doesn't declare a
7090 enum language pretend_language;
7093 /* die_reader_func for process_psymtab_comp_unit. */
7096 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7097 const gdb_byte *info_ptr,
7098 struct die_info *comp_unit_die,
7102 struct dwarf2_cu *cu = reader->cu;
7103 struct objfile *objfile = cu->objfile;
7104 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7105 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7107 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7108 struct partial_symtab *pst;
7109 enum pc_bounds_kind cu_bounds_kind;
7110 const char *filename;
7111 struct process_psymtab_comp_unit_data *info
7112 = (struct process_psymtab_comp_unit_data *) data;
7114 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7117 gdb_assert (! per_cu->is_debug_types);
7119 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7121 cu->list_in_scope = &file_symbols;
7123 /* Allocate a new partial symbol table structure. */
7124 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7125 if (filename == NULL)
7128 pst = create_partial_symtab (per_cu, filename);
7130 /* This must be done before calling dwarf2_build_include_psymtabs. */
7131 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7133 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7135 dwarf2_find_base_address (comp_unit_die, cu);
7137 /* Possibly set the default values of LOWPC and HIGHPC from
7139 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7140 &best_highpc, cu, pst);
7141 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7142 /* Store the contiguous range if it is not empty; it can be empty for
7143 CUs with no code. */
7144 addrmap_set_empty (objfile->psymtabs_addrmap,
7145 gdbarch_adjust_dwarf2_addr (gdbarch,
7146 best_lowpc + baseaddr),
7147 gdbarch_adjust_dwarf2_addr (gdbarch,
7148 best_highpc + baseaddr) - 1,
7151 /* Check if comp unit has_children.
7152 If so, read the rest of the partial symbols from this comp unit.
7153 If not, there's no more debug_info for this comp unit. */
7156 struct partial_die_info *first_die;
7157 CORE_ADDR lowpc, highpc;
7159 lowpc = ((CORE_ADDR) -1);
7160 highpc = ((CORE_ADDR) 0);
7162 first_die = load_partial_dies (reader, info_ptr, 1);
7164 scan_partial_symbols (first_die, &lowpc, &highpc,
7165 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7167 /* If we didn't find a lowpc, set it to highpc to avoid
7168 complaints from `maint check'. */
7169 if (lowpc == ((CORE_ADDR) -1))
7172 /* If the compilation unit didn't have an explicit address range,
7173 then use the information extracted from its child dies. */
7174 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7177 best_highpc = highpc;
7180 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7181 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
7183 end_psymtab_common (objfile, pst);
7185 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7188 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7189 struct dwarf2_per_cu_data *iter;
7191 /* Fill in 'dependencies' here; we fill in 'users' in a
7193 pst->number_of_dependencies = len;
7195 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7197 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7200 pst->dependencies[i] = iter->v.psymtab;
7202 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7205 /* Get the list of files included in the current compilation unit,
7206 and build a psymtab for each of them. */
7207 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7209 if (dwarf_read_debug)
7211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7213 fprintf_unfiltered (gdb_stdlog,
7214 "Psymtab for %s unit @0x%x: %s - %s"
7215 ", %d global, %d static syms\n",
7216 per_cu->is_debug_types ? "type" : "comp",
7217 to_underlying (per_cu->sect_off),
7218 paddress (gdbarch, pst->textlow),
7219 paddress (gdbarch, pst->texthigh),
7220 pst->n_global_syms, pst->n_static_syms);
7224 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7225 Process compilation unit THIS_CU for a psymtab. */
7228 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7229 int want_partial_unit,
7230 enum language pretend_language)
7232 /* If this compilation unit was already read in, free the
7233 cached copy in order to read it in again. This is
7234 necessary because we skipped some symbols when we first
7235 read in the compilation unit (see load_partial_dies).
7236 This problem could be avoided, but the benefit is unclear. */
7237 if (this_cu->cu != NULL)
7238 free_one_cached_comp_unit (this_cu);
7240 if (this_cu->is_debug_types)
7241 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
7245 process_psymtab_comp_unit_data info;
7246 info.want_partial_unit = want_partial_unit;
7247 info.pretend_language = pretend_language;
7248 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
7249 process_psymtab_comp_unit_reader, &info);
7252 /* Age out any secondary CUs. */
7253 age_cached_comp_units ();
7256 /* Reader function for build_type_psymtabs. */
7259 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7260 const gdb_byte *info_ptr,
7261 struct die_info *type_unit_die,
7265 struct objfile *objfile = dwarf2_per_objfile->objfile;
7266 struct dwarf2_cu *cu = reader->cu;
7267 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7268 struct signatured_type *sig_type;
7269 struct type_unit_group *tu_group;
7270 struct attribute *attr;
7271 struct partial_die_info *first_die;
7272 CORE_ADDR lowpc, highpc;
7273 struct partial_symtab *pst;
7275 gdb_assert (data == NULL);
7276 gdb_assert (per_cu->is_debug_types);
7277 sig_type = (struct signatured_type *) per_cu;
7282 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7283 tu_group = get_type_unit_group (cu, attr);
7285 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
7287 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7288 cu->list_in_scope = &file_symbols;
7289 pst = create_partial_symtab (per_cu, "");
7292 first_die = load_partial_dies (reader, info_ptr, 1);
7294 lowpc = (CORE_ADDR) -1;
7295 highpc = (CORE_ADDR) 0;
7296 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7298 end_psymtab_common (objfile, pst);
7301 /* Struct used to sort TUs by their abbreviation table offset. */
7303 struct tu_abbrev_offset
7305 struct signatured_type *sig_type;
7306 sect_offset abbrev_offset;
7309 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
7312 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
7314 const struct tu_abbrev_offset * const *a
7315 = (const struct tu_abbrev_offset * const*) ap;
7316 const struct tu_abbrev_offset * const *b
7317 = (const struct tu_abbrev_offset * const*) bp;
7318 sect_offset aoff = (*a)->abbrev_offset;
7319 sect_offset boff = (*b)->abbrev_offset;
7321 return (aoff > boff) - (aoff < boff);
7324 /* Efficiently read all the type units.
7325 This does the bulk of the work for build_type_psymtabs.
7327 The efficiency is because we sort TUs by the abbrev table they use and
7328 only read each abbrev table once. In one program there are 200K TUs
7329 sharing 8K abbrev tables.
7331 The main purpose of this function is to support building the
7332 dwarf2_per_objfile->type_unit_groups table.
7333 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7334 can collapse the search space by grouping them by stmt_list.
7335 The savings can be significant, in the same program from above the 200K TUs
7336 share 8K stmt_list tables.
7338 FUNC is expected to call get_type_unit_group, which will create the
7339 struct type_unit_group if necessary and add it to
7340 dwarf2_per_objfile->type_unit_groups. */
7343 build_type_psymtabs_1 (void)
7345 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7346 struct cleanup *cleanups;
7347 struct abbrev_table *abbrev_table;
7348 sect_offset abbrev_offset;
7349 struct tu_abbrev_offset *sorted_by_abbrev;
7352 /* It's up to the caller to not call us multiple times. */
7353 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7355 if (dwarf2_per_objfile->n_type_units == 0)
7358 /* TUs typically share abbrev tables, and there can be way more TUs than
7359 abbrev tables. Sort by abbrev table to reduce the number of times we
7360 read each abbrev table in.
7361 Alternatives are to punt or to maintain a cache of abbrev tables.
7362 This is simpler and efficient enough for now.
7364 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7365 symtab to use). Typically TUs with the same abbrev offset have the same
7366 stmt_list value too so in practice this should work well.
7368 The basic algorithm here is:
7370 sort TUs by abbrev table
7371 for each TU with same abbrev table:
7372 read abbrev table if first user
7373 read TU top level DIE
7374 [IWBN if DWO skeletons had DW_AT_stmt_list]
7377 if (dwarf_read_debug)
7378 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7380 /* Sort in a separate table to maintain the order of all_type_units
7381 for .gdb_index: TU indices directly index all_type_units. */
7382 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
7383 dwarf2_per_objfile->n_type_units);
7384 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7386 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
7388 sorted_by_abbrev[i].sig_type = sig_type;
7389 sorted_by_abbrev[i].abbrev_offset =
7390 read_abbrev_offset (sig_type->per_cu.section,
7391 sig_type->per_cu.sect_off);
7393 cleanups = make_cleanup (xfree, sorted_by_abbrev);
7394 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
7395 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
7397 abbrev_offset = (sect_offset) ~(unsigned) 0;
7398 abbrev_table = NULL;
7399 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
7401 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
7403 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
7405 /* Switch to the next abbrev table if necessary. */
7406 if (abbrev_table == NULL
7407 || tu->abbrev_offset != abbrev_offset)
7409 if (abbrev_table != NULL)
7411 abbrev_table_free (abbrev_table);
7412 /* Reset to NULL in case abbrev_table_read_table throws
7413 an error: abbrev_table_free_cleanup will get called. */
7414 abbrev_table = NULL;
7416 abbrev_offset = tu->abbrev_offset;
7418 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
7420 ++tu_stats->nr_uniq_abbrev_tables;
7423 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
7424 build_type_psymtabs_reader, NULL);
7427 do_cleanups (cleanups);
7430 /* Print collected type unit statistics. */
7433 print_tu_stats (void)
7435 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7437 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7438 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
7439 dwarf2_per_objfile->n_type_units);
7440 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7441 tu_stats->nr_uniq_abbrev_tables);
7442 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7443 tu_stats->nr_symtabs);
7444 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7445 tu_stats->nr_symtab_sharers);
7446 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7447 tu_stats->nr_stmt_less_type_units);
7448 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7449 tu_stats->nr_all_type_units_reallocs);
7452 /* Traversal function for build_type_psymtabs. */
7455 build_type_psymtab_dependencies (void **slot, void *info)
7457 struct objfile *objfile = dwarf2_per_objfile->objfile;
7458 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7459 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7460 struct partial_symtab *pst = per_cu->v.psymtab;
7461 int len = VEC_length (sig_type_ptr, tu_group->tus);
7462 struct signatured_type *iter;
7465 gdb_assert (len > 0);
7466 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7468 pst->number_of_dependencies = len;
7470 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
7472 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
7475 gdb_assert (iter->per_cu.is_debug_types);
7476 pst->dependencies[i] = iter->per_cu.v.psymtab;
7477 iter->type_unit_group = tu_group;
7480 VEC_free (sig_type_ptr, tu_group->tus);
7485 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7486 Build partial symbol tables for the .debug_types comp-units. */
7489 build_type_psymtabs (struct objfile *objfile)
7491 if (! create_all_type_units (objfile))
7494 build_type_psymtabs_1 ();
7497 /* Traversal function for process_skeletonless_type_unit.
7498 Read a TU in a DWO file and build partial symbols for it. */
7501 process_skeletonless_type_unit (void **slot, void *info)
7503 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7504 struct objfile *objfile = (struct objfile *) info;
7505 struct signatured_type find_entry, *entry;
7507 /* If this TU doesn't exist in the global table, add it and read it in. */
7509 if (dwarf2_per_objfile->signatured_types == NULL)
7511 dwarf2_per_objfile->signatured_types
7512 = allocate_signatured_type_table (objfile);
7515 find_entry.signature = dwo_unit->signature;
7516 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
7518 /* If we've already seen this type there's nothing to do. What's happening
7519 is we're doing our own version of comdat-folding here. */
7523 /* This does the job that create_all_type_units would have done for
7525 entry = add_type_unit (dwo_unit->signature, slot);
7526 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
7529 /* This does the job that build_type_psymtabs_1 would have done. */
7530 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
7531 build_type_psymtabs_reader, NULL);
7536 /* Traversal function for process_skeletonless_type_units. */
7539 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7541 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7543 if (dwo_file->tus != NULL)
7545 htab_traverse_noresize (dwo_file->tus,
7546 process_skeletonless_type_unit, info);
7552 /* Scan all TUs of DWO files, verifying we've processed them.
7553 This is needed in case a TU was emitted without its skeleton.
7554 Note: This can't be done until we know what all the DWO files are. */
7557 process_skeletonless_type_units (struct objfile *objfile)
7559 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7560 if (get_dwp_file () == NULL
7561 && dwarf2_per_objfile->dwo_files != NULL)
7563 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
7564 process_dwo_file_for_skeletonless_type_units,
7569 /* Compute the 'user' field for each psymtab in OBJFILE. */
7572 set_partial_user (struct objfile *objfile)
7576 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7578 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7579 struct partial_symtab *pst = per_cu->v.psymtab;
7585 for (j = 0; j < pst->number_of_dependencies; ++j)
7587 /* Set the 'user' field only if it is not already set. */
7588 if (pst->dependencies[j]->user == NULL)
7589 pst->dependencies[j]->user = pst;
7594 /* Build the partial symbol table by doing a quick pass through the
7595 .debug_info and .debug_abbrev sections. */
7598 dwarf2_build_psymtabs_hard (struct objfile *objfile)
7600 struct cleanup *back_to;
7603 if (dwarf_read_debug)
7605 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7606 objfile_name (objfile));
7609 dwarf2_per_objfile->reading_partial_symbols = 1;
7611 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
7613 /* Any cached compilation units will be linked by the per-objfile
7614 read_in_chain. Make sure to free them when we're done. */
7615 back_to = make_cleanup (free_cached_comp_units, NULL);
7617 build_type_psymtabs (objfile);
7619 create_all_comp_units (objfile);
7621 /* Create a temporary address map on a temporary obstack. We later
7622 copy this to the final obstack. */
7623 auto_obstack temp_obstack;
7625 scoped_restore save_psymtabs_addrmap
7626 = make_scoped_restore (&objfile->psymtabs_addrmap,
7627 addrmap_create_mutable (&temp_obstack));
7629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
7631 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7633 process_psymtab_comp_unit (per_cu, 0, language_minimal);
7636 /* This has to wait until we read the CUs, we need the list of DWOs. */
7637 process_skeletonless_type_units (objfile);
7639 /* Now that all TUs have been processed we can fill in the dependencies. */
7640 if (dwarf2_per_objfile->type_unit_groups != NULL)
7642 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
7643 build_type_psymtab_dependencies, NULL);
7646 if (dwarf_read_debug)
7649 set_partial_user (objfile);
7651 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
7652 &objfile->objfile_obstack);
7653 /* At this point we want to keep the address map. */
7654 save_psymtabs_addrmap.release ();
7656 do_cleanups (back_to);
7658 if (dwarf_read_debug)
7659 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7660 objfile_name (objfile));
7663 /* die_reader_func for load_partial_comp_unit. */
7666 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
7667 const gdb_byte *info_ptr,
7668 struct die_info *comp_unit_die,
7672 struct dwarf2_cu *cu = reader->cu;
7674 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
7676 /* Check if comp unit has_children.
7677 If so, read the rest of the partial symbols from this comp unit.
7678 If not, there's no more debug_info for this comp unit. */
7680 load_partial_dies (reader, info_ptr, 0);
7683 /* Load the partial DIEs for a secondary CU into memory.
7684 This is also used when rereading a primary CU with load_all_dies. */
7687 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7689 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7690 load_partial_comp_unit_reader, NULL);
7694 read_comp_units_from_section (struct objfile *objfile,
7695 struct dwarf2_section_info *section,
7696 struct dwarf2_section_info *abbrev_section,
7697 unsigned int is_dwz,
7700 struct dwarf2_per_cu_data ***all_comp_units)
7702 const gdb_byte *info_ptr;
7704 if (dwarf_read_debug)
7705 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7706 get_section_name (section),
7707 get_section_file_name (section));
7709 dwarf2_read_section (objfile, section);
7711 info_ptr = section->buffer;
7713 while (info_ptr < section->buffer + section->size)
7715 struct dwarf2_per_cu_data *this_cu;
7717 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7719 comp_unit_head cu_header;
7720 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
7721 info_ptr, rcuh_kind::COMPILE);
7723 /* Save the compilation unit for later lookup. */
7724 if (cu_header.unit_type != DW_UT_type)
7726 this_cu = XOBNEW (&objfile->objfile_obstack,
7727 struct dwarf2_per_cu_data);
7728 memset (this_cu, 0, sizeof (*this_cu));
7732 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7733 struct signatured_type);
7734 memset (sig_type, 0, sizeof (*sig_type));
7735 sig_type->signature = cu_header.signature;
7736 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7737 this_cu = &sig_type->per_cu;
7739 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7740 this_cu->sect_off = sect_off;
7741 this_cu->length = cu_header.length + cu_header.initial_length_size;
7742 this_cu->is_dwz = is_dwz;
7743 this_cu->objfile = objfile;
7744 this_cu->section = section;
7746 if (*n_comp_units == *n_allocated)
7749 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
7750 *all_comp_units, *n_allocated);
7752 (*all_comp_units)[*n_comp_units] = this_cu;
7755 info_ptr = info_ptr + this_cu->length;
7759 /* Create a list of all compilation units in OBJFILE.
7760 This is only done for -readnow and building partial symtabs. */
7763 create_all_comp_units (struct objfile *objfile)
7767 struct dwarf2_per_cu_data **all_comp_units;
7768 struct dwz_file *dwz;
7772 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
7774 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
7775 &dwarf2_per_objfile->abbrev, 0,
7776 &n_allocated, &n_comp_units, &all_comp_units);
7778 dwz = dwarf2_get_dwz_file ();
7780 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
7781 &n_allocated, &n_comp_units,
7784 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
7785 struct dwarf2_per_cu_data *,
7787 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
7788 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
7789 xfree (all_comp_units);
7790 dwarf2_per_objfile->n_comp_units = n_comp_units;
7793 /* Process all loaded DIEs for compilation unit CU, starting at
7794 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7795 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7796 DW_AT_ranges). See the comments of add_partial_subprogram on how
7797 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7800 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7801 CORE_ADDR *highpc, int set_addrmap,
7802 struct dwarf2_cu *cu)
7804 struct partial_die_info *pdi;
7806 /* Now, march along the PDI's, descending into ones which have
7807 interesting children but skipping the children of the other ones,
7808 until we reach the end of the compilation unit. */
7814 fixup_partial_die (pdi, cu);
7816 /* Anonymous namespaces or modules have no name but have interesting
7817 children, so we need to look at them. Ditto for anonymous
7820 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7821 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7822 || pdi->tag == DW_TAG_imported_unit)
7826 case DW_TAG_subprogram:
7827 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7829 case DW_TAG_constant:
7830 case DW_TAG_variable:
7831 case DW_TAG_typedef:
7832 case DW_TAG_union_type:
7833 if (!pdi->is_declaration)
7835 add_partial_symbol (pdi, cu);
7838 case DW_TAG_class_type:
7839 case DW_TAG_interface_type:
7840 case DW_TAG_structure_type:
7841 if (!pdi->is_declaration)
7843 add_partial_symbol (pdi, cu);
7845 if (cu->language == language_rust && pdi->has_children)
7846 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7849 case DW_TAG_enumeration_type:
7850 if (!pdi->is_declaration)
7851 add_partial_enumeration (pdi, cu);
7853 case DW_TAG_base_type:
7854 case DW_TAG_subrange_type:
7855 /* File scope base type definitions are added to the partial
7857 add_partial_symbol (pdi, cu);
7859 case DW_TAG_namespace:
7860 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7863 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7865 case DW_TAG_imported_unit:
7867 struct dwarf2_per_cu_data *per_cu;
7869 /* For now we don't handle imported units in type units. */
7870 if (cu->per_cu->is_debug_types)
7872 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7873 " supported in type units [in module %s]"),
7874 objfile_name (cu->objfile));
7877 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
7881 /* Go read the partial unit, if needed. */
7882 if (per_cu->v.psymtab == NULL)
7883 process_psymtab_comp_unit (per_cu, 1, cu->language);
7885 VEC_safe_push (dwarf2_per_cu_ptr,
7886 cu->per_cu->imported_symtabs, per_cu);
7889 case DW_TAG_imported_declaration:
7890 add_partial_symbol (pdi, cu);
7897 /* If the die has a sibling, skip to the sibling. */
7899 pdi = pdi->die_sibling;
7903 /* Functions used to compute the fully scoped name of a partial DIE.
7905 Normally, this is simple. For C++, the parent DIE's fully scoped
7906 name is concatenated with "::" and the partial DIE's name.
7907 Enumerators are an exception; they use the scope of their parent
7908 enumeration type, i.e. the name of the enumeration type is not
7909 prepended to the enumerator.
7911 There are two complexities. One is DW_AT_specification; in this
7912 case "parent" means the parent of the target of the specification,
7913 instead of the direct parent of the DIE. The other is compilers
7914 which do not emit DW_TAG_namespace; in this case we try to guess
7915 the fully qualified name of structure types from their members'
7916 linkage names. This must be done using the DIE's children rather
7917 than the children of any DW_AT_specification target. We only need
7918 to do this for structures at the top level, i.e. if the target of
7919 any DW_AT_specification (if any; otherwise the DIE itself) does not
7922 /* Compute the scope prefix associated with PDI's parent, in
7923 compilation unit CU. The result will be allocated on CU's
7924 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7925 field. NULL is returned if no prefix is necessary. */
7927 partial_die_parent_scope (struct partial_die_info *pdi,
7928 struct dwarf2_cu *cu)
7930 const char *grandparent_scope;
7931 struct partial_die_info *parent, *real_pdi;
7933 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7934 then this means the parent of the specification DIE. */
7937 while (real_pdi->has_specification)
7938 real_pdi = find_partial_die (real_pdi->spec_offset,
7939 real_pdi->spec_is_dwz, cu);
7941 parent = real_pdi->die_parent;
7945 if (parent->scope_set)
7946 return parent->scope;
7948 fixup_partial_die (parent, cu);
7950 grandparent_scope = partial_die_parent_scope (parent, cu);
7952 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7953 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7954 Work around this problem here. */
7955 if (cu->language == language_cplus
7956 && parent->tag == DW_TAG_namespace
7957 && strcmp (parent->name, "::") == 0
7958 && grandparent_scope == NULL)
7960 parent->scope = NULL;
7961 parent->scope_set = 1;
7965 if (pdi->tag == DW_TAG_enumerator)
7966 /* Enumerators should not get the name of the enumeration as a prefix. */
7967 parent->scope = grandparent_scope;
7968 else if (parent->tag == DW_TAG_namespace
7969 || parent->tag == DW_TAG_module
7970 || parent->tag == DW_TAG_structure_type
7971 || parent->tag == DW_TAG_class_type
7972 || parent->tag == DW_TAG_interface_type
7973 || parent->tag == DW_TAG_union_type
7974 || parent->tag == DW_TAG_enumeration_type)
7976 if (grandparent_scope == NULL)
7977 parent->scope = parent->name;
7979 parent->scope = typename_concat (&cu->comp_unit_obstack,
7981 parent->name, 0, cu);
7985 /* FIXME drow/2004-04-01: What should we be doing with
7986 function-local names? For partial symbols, we should probably be
7988 complaint (&symfile_complaints,
7989 _("unhandled containing DIE tag %d for DIE at %d"),
7990 parent->tag, to_underlying (pdi->sect_off));
7991 parent->scope = grandparent_scope;
7994 parent->scope_set = 1;
7995 return parent->scope;
7998 /* Return the fully scoped name associated with PDI, from compilation unit
7999 CU. The result will be allocated with malloc. */
8002 partial_die_full_name (struct partial_die_info *pdi,
8003 struct dwarf2_cu *cu)
8005 const char *parent_scope;
8007 /* If this is a template instantiation, we can not work out the
8008 template arguments from partial DIEs. So, unfortunately, we have
8009 to go through the full DIEs. At least any work we do building
8010 types here will be reused if full symbols are loaded later. */
8011 if (pdi->has_template_arguments)
8013 fixup_partial_die (pdi, cu);
8015 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8017 struct die_info *die;
8018 struct attribute attr;
8019 struct dwarf2_cu *ref_cu = cu;
8021 /* DW_FORM_ref_addr is using section offset. */
8022 attr.name = (enum dwarf_attribute) 0;
8023 attr.form = DW_FORM_ref_addr;
8024 attr.u.unsnd = to_underlying (pdi->sect_off);
8025 die = follow_die_ref (NULL, &attr, &ref_cu);
8027 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8031 parent_scope = partial_die_parent_scope (pdi, cu);
8032 if (parent_scope == NULL)
8035 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8039 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8041 struct objfile *objfile = cu->objfile;
8042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8044 const char *actual_name = NULL;
8046 char *built_actual_name;
8048 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8050 built_actual_name = partial_die_full_name (pdi, cu);
8051 if (built_actual_name != NULL)
8052 actual_name = built_actual_name;
8054 if (actual_name == NULL)
8055 actual_name = pdi->name;
8059 case DW_TAG_subprogram:
8060 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8061 if (pdi->is_external || cu->language == language_ada)
8063 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8064 of the global scope. But in Ada, we want to be able to access
8065 nested procedures globally. So all Ada subprograms are stored
8066 in the global scope. */
8067 add_psymbol_to_list (actual_name, strlen (actual_name),
8068 built_actual_name != NULL,
8069 VAR_DOMAIN, LOC_BLOCK,
8070 &objfile->global_psymbols,
8071 addr, cu->language, objfile);
8075 add_psymbol_to_list (actual_name, strlen (actual_name),
8076 built_actual_name != NULL,
8077 VAR_DOMAIN, LOC_BLOCK,
8078 &objfile->static_psymbols,
8079 addr, cu->language, objfile);
8082 if (pdi->main_subprogram && actual_name != NULL)
8083 set_objfile_main_name (objfile, actual_name, cu->language);
8085 case DW_TAG_constant:
8087 std::vector<partial_symbol *> *list;
8089 if (pdi->is_external)
8090 list = &objfile->global_psymbols;
8092 list = &objfile->static_psymbols;
8093 add_psymbol_to_list (actual_name, strlen (actual_name),
8094 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8095 list, 0, cu->language, objfile);
8098 case DW_TAG_variable:
8100 addr = decode_locdesc (pdi->d.locdesc, cu);
8104 && !dwarf2_per_objfile->has_section_at_zero)
8106 /* A global or static variable may also have been stripped
8107 out by the linker if unused, in which case its address
8108 will be nullified; do not add such variables into partial
8109 symbol table then. */
8111 else if (pdi->is_external)
8114 Don't enter into the minimal symbol tables as there is
8115 a minimal symbol table entry from the ELF symbols already.
8116 Enter into partial symbol table if it has a location
8117 descriptor or a type.
8118 If the location descriptor is missing, new_symbol will create
8119 a LOC_UNRESOLVED symbol, the address of the variable will then
8120 be determined from the minimal symbol table whenever the variable
8122 The address for the partial symbol table entry is not
8123 used by GDB, but it comes in handy for debugging partial symbol
8126 if (pdi->d.locdesc || pdi->has_type)
8127 add_psymbol_to_list (actual_name, strlen (actual_name),
8128 built_actual_name != NULL,
8129 VAR_DOMAIN, LOC_STATIC,
8130 &objfile->global_psymbols,
8132 cu->language, objfile);
8136 int has_loc = pdi->d.locdesc != NULL;
8138 /* Static Variable. Skip symbols whose value we cannot know (those
8139 without location descriptors or constant values). */
8140 if (!has_loc && !pdi->has_const_value)
8142 xfree (built_actual_name);
8146 add_psymbol_to_list (actual_name, strlen (actual_name),
8147 built_actual_name != NULL,
8148 VAR_DOMAIN, LOC_STATIC,
8149 &objfile->static_psymbols,
8150 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8151 cu->language, objfile);
8154 case DW_TAG_typedef:
8155 case DW_TAG_base_type:
8156 case DW_TAG_subrange_type:
8157 add_psymbol_to_list (actual_name, strlen (actual_name),
8158 built_actual_name != NULL,
8159 VAR_DOMAIN, LOC_TYPEDEF,
8160 &objfile->static_psymbols,
8161 0, cu->language, objfile);
8163 case DW_TAG_imported_declaration:
8164 case DW_TAG_namespace:
8165 add_psymbol_to_list (actual_name, strlen (actual_name),
8166 built_actual_name != NULL,
8167 VAR_DOMAIN, LOC_TYPEDEF,
8168 &objfile->global_psymbols,
8169 0, cu->language, objfile);
8172 add_psymbol_to_list (actual_name, strlen (actual_name),
8173 built_actual_name != NULL,
8174 MODULE_DOMAIN, LOC_TYPEDEF,
8175 &objfile->global_psymbols,
8176 0, cu->language, objfile);
8178 case DW_TAG_class_type:
8179 case DW_TAG_interface_type:
8180 case DW_TAG_structure_type:
8181 case DW_TAG_union_type:
8182 case DW_TAG_enumeration_type:
8183 /* Skip external references. The DWARF standard says in the section
8184 about "Structure, Union, and Class Type Entries": "An incomplete
8185 structure, union or class type is represented by a structure,
8186 union or class entry that does not have a byte size attribute
8187 and that has a DW_AT_declaration attribute." */
8188 if (!pdi->has_byte_size && pdi->is_declaration)
8190 xfree (built_actual_name);
8194 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8195 static vs. global. */
8196 add_psymbol_to_list (actual_name, strlen (actual_name),
8197 built_actual_name != NULL,
8198 STRUCT_DOMAIN, LOC_TYPEDEF,
8199 cu->language == language_cplus
8200 ? &objfile->global_psymbols
8201 : &objfile->static_psymbols,
8202 0, cu->language, objfile);
8205 case DW_TAG_enumerator:
8206 add_psymbol_to_list (actual_name, strlen (actual_name),
8207 built_actual_name != NULL,
8208 VAR_DOMAIN, LOC_CONST,
8209 cu->language == language_cplus
8210 ? &objfile->global_psymbols
8211 : &objfile->static_psymbols,
8212 0, cu->language, objfile);
8218 xfree (built_actual_name);
8221 /* Read a partial die corresponding to a namespace; also, add a symbol
8222 corresponding to that namespace to the symbol table. NAMESPACE is
8223 the name of the enclosing namespace. */
8226 add_partial_namespace (struct partial_die_info *pdi,
8227 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8228 int set_addrmap, struct dwarf2_cu *cu)
8230 /* Add a symbol for the namespace. */
8232 add_partial_symbol (pdi, cu);
8234 /* Now scan partial symbols in that namespace. */
8236 if (pdi->has_children)
8237 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8240 /* Read a partial die corresponding to a Fortran module. */
8243 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8244 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8246 /* Add a symbol for the namespace. */
8248 add_partial_symbol (pdi, cu);
8250 /* Now scan partial symbols in that module. */
8252 if (pdi->has_children)
8253 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8256 /* Read a partial die corresponding to a subprogram and create a partial
8257 symbol for that subprogram. When the CU language allows it, this
8258 routine also defines a partial symbol for each nested subprogram
8259 that this subprogram contains. If SET_ADDRMAP is true, record the
8260 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
8261 and highest PC values found in PDI.
8263 PDI may also be a lexical block, in which case we simply search
8264 recursively for subprograms defined inside that lexical block.
8265 Again, this is only performed when the CU language allows this
8266 type of definitions. */
8269 add_partial_subprogram (struct partial_die_info *pdi,
8270 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8271 int set_addrmap, struct dwarf2_cu *cu)
8273 if (pdi->tag == DW_TAG_subprogram)
8275 if (pdi->has_pc_info)
8277 if (pdi->lowpc < *lowpc)
8278 *lowpc = pdi->lowpc;
8279 if (pdi->highpc > *highpc)
8280 *highpc = pdi->highpc;
8283 struct objfile *objfile = cu->objfile;
8284 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8289 baseaddr = ANOFFSET (objfile->section_offsets,
8290 SECT_OFF_TEXT (objfile));
8291 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8292 pdi->lowpc + baseaddr);
8293 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
8294 pdi->highpc + baseaddr);
8295 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
8296 cu->per_cu->v.psymtab);
8300 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8302 if (!pdi->is_declaration)
8303 /* Ignore subprogram DIEs that do not have a name, they are
8304 illegal. Do not emit a complaint at this point, we will
8305 do so when we convert this psymtab into a symtab. */
8307 add_partial_symbol (pdi, cu);
8311 if (! pdi->has_children)
8314 if (cu->language == language_ada)
8316 pdi = pdi->die_child;
8319 fixup_partial_die (pdi, cu);
8320 if (pdi->tag == DW_TAG_subprogram
8321 || pdi->tag == DW_TAG_lexical_block)
8322 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8323 pdi = pdi->die_sibling;
8328 /* Read a partial die corresponding to an enumeration type. */
8331 add_partial_enumeration (struct partial_die_info *enum_pdi,
8332 struct dwarf2_cu *cu)
8334 struct partial_die_info *pdi;
8336 if (enum_pdi->name != NULL)
8337 add_partial_symbol (enum_pdi, cu);
8339 pdi = enum_pdi->die_child;
8342 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8343 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8345 add_partial_symbol (pdi, cu);
8346 pdi = pdi->die_sibling;
8350 /* Return the initial uleb128 in the die at INFO_PTR. */
8353 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8355 unsigned int bytes_read;
8357 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8360 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
8361 Return the corresponding abbrev, or NULL if the number is zero (indicating
8362 an empty DIE). In either case *BYTES_READ will be set to the length of
8363 the initial number. */
8365 static struct abbrev_info *
8366 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
8367 struct dwarf2_cu *cu)
8369 bfd *abfd = cu->objfile->obfd;
8370 unsigned int abbrev_number;
8371 struct abbrev_info *abbrev;
8373 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8375 if (abbrev_number == 0)
8378 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
8381 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8382 " at offset 0x%x [in module %s]"),
8383 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8384 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
8390 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8391 Returns a pointer to the end of a series of DIEs, terminated by an empty
8392 DIE. Any children of the skipped DIEs will also be skipped. */
8394 static const gdb_byte *
8395 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8397 struct dwarf2_cu *cu = reader->cu;
8398 struct abbrev_info *abbrev;
8399 unsigned int bytes_read;
8403 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8405 return info_ptr + bytes_read;
8407 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8411 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8412 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8413 abbrev corresponding to that skipped uleb128 should be passed in
8414 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8417 static const gdb_byte *
8418 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8419 struct abbrev_info *abbrev)
8421 unsigned int bytes_read;
8422 struct attribute attr;
8423 bfd *abfd = reader->abfd;
8424 struct dwarf2_cu *cu = reader->cu;
8425 const gdb_byte *buffer = reader->buffer;
8426 const gdb_byte *buffer_end = reader->buffer_end;
8427 unsigned int form, i;
8429 for (i = 0; i < abbrev->num_attrs; i++)
8431 /* The only abbrev we care about is DW_AT_sibling. */
8432 if (abbrev->attrs[i].name == DW_AT_sibling)
8434 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8435 if (attr.form == DW_FORM_ref_addr)
8436 complaint (&symfile_complaints,
8437 _("ignoring absolute DW_AT_sibling"));
8440 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8441 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8443 if (sibling_ptr < info_ptr)
8444 complaint (&symfile_complaints,
8445 _("DW_AT_sibling points backwards"));
8446 else if (sibling_ptr > reader->buffer_end)
8447 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8453 /* If it isn't DW_AT_sibling, skip this attribute. */
8454 form = abbrev->attrs[i].form;
8458 case DW_FORM_ref_addr:
8459 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8460 and later it is offset sized. */
8461 if (cu->header.version == 2)
8462 info_ptr += cu->header.addr_size;
8464 info_ptr += cu->header.offset_size;
8466 case DW_FORM_GNU_ref_alt:
8467 info_ptr += cu->header.offset_size;
8470 info_ptr += cu->header.addr_size;
8477 case DW_FORM_flag_present:
8478 case DW_FORM_implicit_const:
8490 case DW_FORM_ref_sig8:
8493 case DW_FORM_data16:
8496 case DW_FORM_string:
8497 read_direct_string (abfd, info_ptr, &bytes_read);
8498 info_ptr += bytes_read;
8500 case DW_FORM_sec_offset:
8502 case DW_FORM_GNU_strp_alt:
8503 info_ptr += cu->header.offset_size;
8505 case DW_FORM_exprloc:
8507 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8508 info_ptr += bytes_read;
8510 case DW_FORM_block1:
8511 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8513 case DW_FORM_block2:
8514 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8516 case DW_FORM_block4:
8517 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8521 case DW_FORM_ref_udata:
8522 case DW_FORM_GNU_addr_index:
8523 case DW_FORM_GNU_str_index:
8524 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8526 case DW_FORM_indirect:
8527 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8528 info_ptr += bytes_read;
8529 /* We need to continue parsing from here, so just go back to
8531 goto skip_attribute;
8534 error (_("Dwarf Error: Cannot handle %s "
8535 "in DWARF reader [in module %s]"),
8536 dwarf_form_name (form),
8537 bfd_get_filename (abfd));
8541 if (abbrev->has_children)
8542 return skip_children (reader, info_ptr);
8547 /* Locate ORIG_PDI's sibling.
8548 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8550 static const gdb_byte *
8551 locate_pdi_sibling (const struct die_reader_specs *reader,
8552 struct partial_die_info *orig_pdi,
8553 const gdb_byte *info_ptr)
8555 /* Do we know the sibling already? */
8557 if (orig_pdi->sibling)
8558 return orig_pdi->sibling;
8560 /* Are there any children to deal with? */
8562 if (!orig_pdi->has_children)
8565 /* Skip the children the long way. */
8567 return skip_children (reader, info_ptr);
8570 /* Expand this partial symbol table into a full symbol table. SELF is
8574 dwarf2_read_symtab (struct partial_symtab *self,
8575 struct objfile *objfile)
8579 warning (_("bug: psymtab for %s is already read in."),
8586 printf_filtered (_("Reading in symbols for %s..."),
8588 gdb_flush (gdb_stdout);
8591 /* Restore our global data. */
8593 = (struct dwarf2_per_objfile *) objfile_data (objfile,
8594 dwarf2_objfile_data_key);
8596 /* If this psymtab is constructed from a debug-only objfile, the
8597 has_section_at_zero flag will not necessarily be correct. We
8598 can get the correct value for this flag by looking at the data
8599 associated with the (presumably stripped) associated objfile. */
8600 if (objfile->separate_debug_objfile_backlink)
8602 struct dwarf2_per_objfile *dpo_backlink
8603 = ((struct dwarf2_per_objfile *)
8604 objfile_data (objfile->separate_debug_objfile_backlink,
8605 dwarf2_objfile_data_key));
8607 dwarf2_per_objfile->has_section_at_zero
8608 = dpo_backlink->has_section_at_zero;
8611 dwarf2_per_objfile->reading_partial_symbols = 0;
8613 psymtab_to_symtab_1 (self);
8615 /* Finish up the debug error message. */
8617 printf_filtered (_("done.\n"));
8620 process_cu_includes ();
8623 /* Reading in full CUs. */
8625 /* Add PER_CU to the queue. */
8628 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8629 enum language pretend_language)
8631 struct dwarf2_queue_item *item;
8634 item = XNEW (struct dwarf2_queue_item);
8635 item->per_cu = per_cu;
8636 item->pretend_language = pretend_language;
8639 if (dwarf2_queue == NULL)
8640 dwarf2_queue = item;
8642 dwarf2_queue_tail->next = item;
8644 dwarf2_queue_tail = item;
8647 /* If PER_CU is not yet queued, add it to the queue.
8648 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8650 The result is non-zero if PER_CU was queued, otherwise the result is zero
8651 meaning either PER_CU is already queued or it is already loaded.
8653 N.B. There is an invariant here that if a CU is queued then it is loaded.
8654 The caller is required to load PER_CU if we return non-zero. */
8657 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8658 struct dwarf2_per_cu_data *per_cu,
8659 enum language pretend_language)
8661 /* We may arrive here during partial symbol reading, if we need full
8662 DIEs to process an unusual case (e.g. template arguments). Do
8663 not queue PER_CU, just tell our caller to load its DIEs. */
8664 if (dwarf2_per_objfile->reading_partial_symbols)
8666 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8671 /* Mark the dependence relation so that we don't flush PER_CU
8673 if (dependent_cu != NULL)
8674 dwarf2_add_dependence (dependent_cu, per_cu);
8676 /* If it's already on the queue, we have nothing to do. */
8680 /* If the compilation unit is already loaded, just mark it as
8682 if (per_cu->cu != NULL)
8684 per_cu->cu->last_used = 0;
8688 /* Add it to the queue. */
8689 queue_comp_unit (per_cu, pretend_language);
8694 /* Process the queue. */
8697 process_queue (void)
8699 struct dwarf2_queue_item *item, *next_item;
8701 if (dwarf_read_debug)
8703 fprintf_unfiltered (gdb_stdlog,
8704 "Expanding one or more symtabs of objfile %s ...\n",
8705 objfile_name (dwarf2_per_objfile->objfile));
8708 /* The queue starts out with one item, but following a DIE reference
8709 may load a new CU, adding it to the end of the queue. */
8710 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
8712 if ((dwarf2_per_objfile->using_index
8713 ? !item->per_cu->v.quick->compunit_symtab
8714 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
8715 /* Skip dummy CUs. */
8716 && item->per_cu->cu != NULL)
8718 struct dwarf2_per_cu_data *per_cu = item->per_cu;
8719 unsigned int debug_print_threshold;
8722 if (per_cu->is_debug_types)
8724 struct signatured_type *sig_type =
8725 (struct signatured_type *) per_cu;
8727 sprintf (buf, "TU %s at offset 0x%x",
8728 hex_string (sig_type->signature),
8729 to_underlying (per_cu->sect_off));
8730 /* There can be 100s of TUs.
8731 Only print them in verbose mode. */
8732 debug_print_threshold = 2;
8736 sprintf (buf, "CU at offset 0x%x",
8737 to_underlying (per_cu->sect_off));
8738 debug_print_threshold = 1;
8741 if (dwarf_read_debug >= debug_print_threshold)
8742 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8744 if (per_cu->is_debug_types)
8745 process_full_type_unit (per_cu, item->pretend_language);
8747 process_full_comp_unit (per_cu, item->pretend_language);
8749 if (dwarf_read_debug >= debug_print_threshold)
8750 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8753 item->per_cu->queued = 0;
8754 next_item = item->next;
8758 dwarf2_queue_tail = NULL;
8760 if (dwarf_read_debug)
8762 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8763 objfile_name (dwarf2_per_objfile->objfile));
8767 /* Free all allocated queue entries. This function only releases anything if
8768 an error was thrown; if the queue was processed then it would have been
8769 freed as we went along. */
8772 dwarf2_release_queue (void *dummy)
8774 struct dwarf2_queue_item *item, *last;
8776 item = dwarf2_queue;
8779 /* Anything still marked queued is likely to be in an
8780 inconsistent state, so discard it. */
8781 if (item->per_cu->queued)
8783 if (item->per_cu->cu != NULL)
8784 free_one_cached_comp_unit (item->per_cu);
8785 item->per_cu->queued = 0;
8793 dwarf2_queue = dwarf2_queue_tail = NULL;
8796 /* Read in full symbols for PST, and anything it depends on. */
8799 psymtab_to_symtab_1 (struct partial_symtab *pst)
8801 struct dwarf2_per_cu_data *per_cu;
8807 for (i = 0; i < pst->number_of_dependencies; i++)
8808 if (!pst->dependencies[i]->readin
8809 && pst->dependencies[i]->user == NULL)
8811 /* Inform about additional files that need to be read in. */
8814 /* FIXME: i18n: Need to make this a single string. */
8815 fputs_filtered (" ", gdb_stdout);
8817 fputs_filtered ("and ", gdb_stdout);
8819 printf_filtered ("%s...", pst->dependencies[i]->filename);
8820 wrap_here (""); /* Flush output. */
8821 gdb_flush (gdb_stdout);
8823 psymtab_to_symtab_1 (pst->dependencies[i]);
8826 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
8830 /* It's an include file, no symbols to read for it.
8831 Everything is in the parent symtab. */
8836 dw2_do_instantiate_symtab (per_cu);
8839 /* Trivial hash function for die_info: the hash value of a DIE
8840 is its offset in .debug_info for this objfile. */
8843 die_hash (const void *item)
8845 const struct die_info *die = (const struct die_info *) item;
8847 return to_underlying (die->sect_off);
8850 /* Trivial comparison function for die_info structures: two DIEs
8851 are equal if they have the same offset. */
8854 die_eq (const void *item_lhs, const void *item_rhs)
8856 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8857 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8859 return die_lhs->sect_off == die_rhs->sect_off;
8862 /* die_reader_func for load_full_comp_unit.
8863 This is identical to read_signatured_type_reader,
8864 but is kept separate for now. */
8867 load_full_comp_unit_reader (const struct die_reader_specs *reader,
8868 const gdb_byte *info_ptr,
8869 struct die_info *comp_unit_die,
8873 struct dwarf2_cu *cu = reader->cu;
8874 enum language *language_ptr = (enum language *) data;
8876 gdb_assert (cu->die_hash == NULL);
8878 htab_create_alloc_ex (cu->header.length / 12,
8882 &cu->comp_unit_obstack,
8883 hashtab_obstack_allocate,
8884 dummy_obstack_deallocate);
8887 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
8888 &info_ptr, comp_unit_die);
8889 cu->dies = comp_unit_die;
8890 /* comp_unit_die is not stored in die_hash, no need. */
8892 /* We try not to read any attributes in this function, because not
8893 all CUs needed for references have been loaded yet, and symbol
8894 table processing isn't initialized. But we have to set the CU language,
8895 or we won't be able to build types correctly.
8896 Similarly, if we do not read the producer, we can not apply
8897 producer-specific interpretation. */
8898 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
8901 /* Load the DIEs associated with PER_CU into memory. */
8904 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8905 enum language pretend_language)
8907 gdb_assert (! this_cu->is_debug_types);
8909 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8910 load_full_comp_unit_reader, &pretend_language);
8913 /* Add a DIE to the delayed physname list. */
8916 add_to_method_list (struct type *type, int fnfield_index, int index,
8917 const char *name, struct die_info *die,
8918 struct dwarf2_cu *cu)
8920 struct delayed_method_info mi;
8922 mi.fnfield_index = fnfield_index;
8926 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8929 /* A cleanup for freeing the delayed method list. */
8932 free_delayed_list (void *ptr)
8934 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8935 if (cu->method_list != NULL)
8937 VEC_free (delayed_method_info, cu->method_list);
8938 cu->method_list = NULL;
8942 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8943 "const" / "volatile". If so, decrements LEN by the length of the
8944 modifier and return true. Otherwise return false. */
8948 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8950 size_t mod_len = sizeof (mod) - 1;
8951 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8959 /* Compute the physnames of any methods on the CU's method list.
8961 The computation of method physnames is delayed in order to avoid the
8962 (bad) condition that one of the method's formal parameters is of an as yet
8966 compute_delayed_physnames (struct dwarf2_cu *cu)
8969 struct delayed_method_info *mi;
8971 /* Only C++ delays computing physnames. */
8972 if (VEC_empty (delayed_method_info, cu->method_list))
8974 gdb_assert (cu->language == language_cplus);
8976 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8978 const char *physname;
8979 struct fn_fieldlist *fn_flp
8980 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8981 physname = dwarf2_physname (mi->name, mi->die, cu);
8982 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8983 = physname ? physname : "";
8985 /* Since there's no tag to indicate whether a method is a
8986 const/volatile overload, extract that information out of the
8988 if (physname != NULL)
8990 size_t len = strlen (physname);
8994 if (physname[len] == ')') /* shortcut */
8996 else if (check_modifier (physname, len, " const"))
8997 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8998 else if (check_modifier (physname, len, " volatile"))
8999 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
9007 /* Go objects should be embedded in a DW_TAG_module DIE,
9008 and it's not clear if/how imported objects will appear.
9009 To keep Go support simple until that's worked out,
9010 go back through what we've read and create something usable.
9011 We could do this while processing each DIE, and feels kinda cleaner,
9012 but that way is more invasive.
9013 This is to, for example, allow the user to type "p var" or "b main"
9014 without having to specify the package name, and allow lookups
9015 of module.object to work in contexts that use the expression
9019 fixup_go_packaging (struct dwarf2_cu *cu)
9021 char *package_name = NULL;
9022 struct pending *list;
9025 for (list = global_symbols; list != NULL; list = list->next)
9027 for (i = 0; i < list->nsyms; ++i)
9029 struct symbol *sym = list->symbol[i];
9031 if (SYMBOL_LANGUAGE (sym) == language_go
9032 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9034 char *this_package_name = go_symbol_package_name (sym);
9036 if (this_package_name == NULL)
9038 if (package_name == NULL)
9039 package_name = this_package_name;
9042 if (strcmp (package_name, this_package_name) != 0)
9043 complaint (&symfile_complaints,
9044 _("Symtab %s has objects from two different Go packages: %s and %s"),
9045 (symbol_symtab (sym) != NULL
9046 ? symtab_to_filename_for_display
9047 (symbol_symtab (sym))
9048 : objfile_name (cu->objfile)),
9049 this_package_name, package_name);
9050 xfree (this_package_name);
9056 if (package_name != NULL)
9058 struct objfile *objfile = cu->objfile;
9059 const char *saved_package_name
9060 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9062 strlen (package_name));
9063 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9064 saved_package_name);
9067 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9069 sym = allocate_symbol (objfile);
9070 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9071 SYMBOL_SET_NAMES (sym, saved_package_name,
9072 strlen (saved_package_name), 0, objfile);
9073 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9074 e.g., "main" finds the "main" module and not C's main(). */
9075 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9076 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9077 SYMBOL_TYPE (sym) = type;
9079 add_symbol_to_list (sym, &global_symbols);
9081 xfree (package_name);
9085 /* Return the symtab for PER_CU. This works properly regardless of
9086 whether we're using the index or psymtabs. */
9088 static struct compunit_symtab *
9089 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9091 return (dwarf2_per_objfile->using_index
9092 ? per_cu->v.quick->compunit_symtab
9093 : per_cu->v.psymtab->compunit_symtab);
9096 /* A helper function for computing the list of all symbol tables
9097 included by PER_CU. */
9100 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
9101 htab_t all_children, htab_t all_type_symtabs,
9102 struct dwarf2_per_cu_data *per_cu,
9103 struct compunit_symtab *immediate_parent)
9107 struct compunit_symtab *cust;
9108 struct dwarf2_per_cu_data *iter;
9110 slot = htab_find_slot (all_children, per_cu, INSERT);
9113 /* This inclusion and its children have been processed. */
9118 /* Only add a CU if it has a symbol table. */
9119 cust = get_compunit_symtab (per_cu);
9122 /* If this is a type unit only add its symbol table if we haven't
9123 seen it yet (type unit per_cu's can share symtabs). */
9124 if (per_cu->is_debug_types)
9126 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9130 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9131 if (cust->user == NULL)
9132 cust->user = immediate_parent;
9137 VEC_safe_push (compunit_symtab_ptr, *result, cust);
9138 if (cust->user == NULL)
9139 cust->user = immediate_parent;
9144 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
9147 recursively_compute_inclusions (result, all_children,
9148 all_type_symtabs, iter, cust);
9152 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9156 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9158 gdb_assert (! per_cu->is_debug_types);
9160 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
9163 struct dwarf2_per_cu_data *per_cu_iter;
9164 struct compunit_symtab *compunit_symtab_iter;
9165 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
9166 htab_t all_children, all_type_symtabs;
9167 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9169 /* If we don't have a symtab, we can just skip this case. */
9173 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9174 NULL, xcalloc, xfree);
9175 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9176 NULL, xcalloc, xfree);
9179 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
9183 recursively_compute_inclusions (&result_symtabs, all_children,
9184 all_type_symtabs, per_cu_iter,
9188 /* Now we have a transitive closure of all the included symtabs. */
9189 len = VEC_length (compunit_symtab_ptr, result_symtabs);
9191 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
9192 struct compunit_symtab *, len + 1);
9194 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
9195 compunit_symtab_iter);
9197 cust->includes[ix] = compunit_symtab_iter;
9198 cust->includes[len] = NULL;
9200 VEC_free (compunit_symtab_ptr, result_symtabs);
9201 htab_delete (all_children);
9202 htab_delete (all_type_symtabs);
9206 /* Compute the 'includes' field for the symtabs of all the CUs we just
9210 process_cu_includes (void)
9213 struct dwarf2_per_cu_data *iter;
9216 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
9220 if (! iter->is_debug_types)
9221 compute_compunit_symtab_includes (iter);
9224 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
9227 /* Generate full symbol information for PER_CU, whose DIEs have
9228 already been loaded into memory. */
9231 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9232 enum language pretend_language)
9234 struct dwarf2_cu *cu = per_cu->cu;
9235 struct objfile *objfile = per_cu->objfile;
9236 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9237 CORE_ADDR lowpc, highpc;
9238 struct compunit_symtab *cust;
9239 struct cleanup *delayed_list_cleanup;
9241 struct block *static_block;
9244 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9247 scoped_free_pendings free_pending;
9248 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9250 cu->list_in_scope = &file_symbols;
9252 cu->language = pretend_language;
9253 cu->language_defn = language_def (cu->language);
9255 /* Do line number decoding in read_file_scope () */
9256 process_die (cu->dies, cu);
9258 /* For now fudge the Go package. */
9259 if (cu->language == language_go)
9260 fixup_go_packaging (cu);
9262 /* Now that we have processed all the DIEs in the CU, all the types
9263 should be complete, and it should now be safe to compute all of the
9265 compute_delayed_physnames (cu);
9266 do_cleanups (delayed_list_cleanup);
9268 /* Some compilers don't define a DW_AT_high_pc attribute for the
9269 compilation unit. If the DW_AT_high_pc is missing, synthesize
9270 it, by scanning the DIE's below the compilation unit. */
9271 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9273 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9274 static_block = end_symtab_get_static_block (addr, 0, 1);
9276 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9277 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9278 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9279 addrmap to help ensure it has an accurate map of pc values belonging to
9281 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9283 cust = end_symtab_from_static_block (static_block,
9284 SECT_OFF_TEXT (objfile), 0);
9288 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9290 /* Set symtab language to language from DW_AT_language. If the
9291 compilation is from a C file generated by language preprocessors, do
9292 not set the language if it was already deduced by start_subfile. */
9293 if (!(cu->language == language_c
9294 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9295 COMPUNIT_FILETABS (cust)->language = cu->language;
9297 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9298 produce DW_AT_location with location lists but it can be possibly
9299 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9300 there were bugs in prologue debug info, fixed later in GCC-4.5
9301 by "unwind info for epilogues" patch (which is not directly related).
9303 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9304 needed, it would be wrong due to missing DW_AT_producer there.
9306 Still one can confuse GDB by using non-standard GCC compilation
9307 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9309 if (cu->has_loclist && gcc_4_minor >= 5)
9310 cust->locations_valid = 1;
9312 if (gcc_4_minor >= 5)
9313 cust->epilogue_unwind_valid = 1;
9315 cust->call_site_htab = cu->call_site_htab;
9318 if (dwarf2_per_objfile->using_index)
9319 per_cu->v.quick->compunit_symtab = cust;
9322 struct partial_symtab *pst = per_cu->v.psymtab;
9323 pst->compunit_symtab = cust;
9327 /* Push it for inclusion processing later. */
9328 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
9331 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9332 already been loaded into memory. */
9335 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9336 enum language pretend_language)
9338 struct dwarf2_cu *cu = per_cu->cu;
9339 struct objfile *objfile = per_cu->objfile;
9340 struct compunit_symtab *cust;
9341 struct cleanup *delayed_list_cleanup;
9342 struct signatured_type *sig_type;
9344 gdb_assert (per_cu->is_debug_types);
9345 sig_type = (struct signatured_type *) per_cu;
9348 scoped_free_pendings free_pending;
9349 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
9351 cu->list_in_scope = &file_symbols;
9353 cu->language = pretend_language;
9354 cu->language_defn = language_def (cu->language);
9356 /* The symbol tables are set up in read_type_unit_scope. */
9357 process_die (cu->dies, cu);
9359 /* For now fudge the Go package. */
9360 if (cu->language == language_go)
9361 fixup_go_packaging (cu);
9363 /* Now that we have processed all the DIEs in the CU, all the types
9364 should be complete, and it should now be safe to compute all of the
9366 compute_delayed_physnames (cu);
9367 do_cleanups (delayed_list_cleanup);
9369 /* TUs share symbol tables.
9370 If this is the first TU to use this symtab, complete the construction
9371 of it with end_expandable_symtab. Otherwise, complete the addition of
9372 this TU's symbols to the existing symtab. */
9373 if (sig_type->type_unit_group->compunit_symtab == NULL)
9375 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9376 sig_type->type_unit_group->compunit_symtab = cust;
9380 /* Set symtab language to language from DW_AT_language. If the
9381 compilation is from a C file generated by language preprocessors,
9382 do not set the language if it was already deduced by
9384 if (!(cu->language == language_c
9385 && COMPUNIT_FILETABS (cust)->language != language_c))
9386 COMPUNIT_FILETABS (cust)->language = cu->language;
9391 augment_type_symtab ();
9392 cust = sig_type->type_unit_group->compunit_symtab;
9395 if (dwarf2_per_objfile->using_index)
9396 per_cu->v.quick->compunit_symtab = cust;
9399 struct partial_symtab *pst = per_cu->v.psymtab;
9400 pst->compunit_symtab = cust;
9405 /* Process an imported unit DIE. */
9408 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9410 struct attribute *attr;
9412 /* For now we don't handle imported units in type units. */
9413 if (cu->per_cu->is_debug_types)
9415 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9416 " supported in type units [in module %s]"),
9417 objfile_name (cu->objfile));
9420 attr = dwarf2_attr (die, DW_AT_import, cu);
9423 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9424 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9425 dwarf2_per_cu_data *per_cu
9426 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
9428 /* If necessary, add it to the queue and load its DIEs. */
9429 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9430 load_full_comp_unit (per_cu, cu->language);
9432 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
9437 /* RAII object that represents a process_die scope: i.e.,
9438 starts/finishes processing a DIE. */
9439 class process_die_scope
9442 process_die_scope (die_info *die, dwarf2_cu *cu)
9443 : m_die (die), m_cu (cu)
9445 /* We should only be processing DIEs not already in process. */
9446 gdb_assert (!m_die->in_process);
9447 m_die->in_process = true;
9450 ~process_die_scope ()
9452 m_die->in_process = false;
9454 /* If we're done processing the DIE for the CU that owns the line
9455 header, we don't need the line header anymore. */
9456 if (m_cu->line_header_die_owner == m_die)
9458 delete m_cu->line_header;
9459 m_cu->line_header = NULL;
9460 m_cu->line_header_die_owner = NULL;
9469 /* Process a die and its children. */
9472 process_die (struct die_info *die, struct dwarf2_cu *cu)
9474 process_die_scope scope (die, cu);
9478 case DW_TAG_padding:
9480 case DW_TAG_compile_unit:
9481 case DW_TAG_partial_unit:
9482 read_file_scope (die, cu);
9484 case DW_TAG_type_unit:
9485 read_type_unit_scope (die, cu);
9487 case DW_TAG_subprogram:
9488 case DW_TAG_inlined_subroutine:
9489 read_func_scope (die, cu);
9491 case DW_TAG_lexical_block:
9492 case DW_TAG_try_block:
9493 case DW_TAG_catch_block:
9494 read_lexical_block_scope (die, cu);
9496 case DW_TAG_call_site:
9497 case DW_TAG_GNU_call_site:
9498 read_call_site_scope (die, cu);
9500 case DW_TAG_class_type:
9501 case DW_TAG_interface_type:
9502 case DW_TAG_structure_type:
9503 case DW_TAG_union_type:
9504 process_structure_scope (die, cu);
9506 case DW_TAG_enumeration_type:
9507 process_enumeration_scope (die, cu);
9510 /* These dies have a type, but processing them does not create
9511 a symbol or recurse to process the children. Therefore we can
9512 read them on-demand through read_type_die. */
9513 case DW_TAG_subroutine_type:
9514 case DW_TAG_set_type:
9515 case DW_TAG_array_type:
9516 case DW_TAG_pointer_type:
9517 case DW_TAG_ptr_to_member_type:
9518 case DW_TAG_reference_type:
9519 case DW_TAG_rvalue_reference_type:
9520 case DW_TAG_string_type:
9523 case DW_TAG_base_type:
9524 case DW_TAG_subrange_type:
9525 case DW_TAG_typedef:
9526 /* Add a typedef symbol for the type definition, if it has a
9528 new_symbol (die, read_type_die (die, cu), cu);
9530 case DW_TAG_common_block:
9531 read_common_block (die, cu);
9533 case DW_TAG_common_inclusion:
9535 case DW_TAG_namespace:
9536 cu->processing_has_namespace_info = 1;
9537 read_namespace (die, cu);
9540 cu->processing_has_namespace_info = 1;
9541 read_module (die, cu);
9543 case DW_TAG_imported_declaration:
9544 cu->processing_has_namespace_info = 1;
9545 if (read_namespace_alias (die, cu))
9547 /* The declaration is not a global namespace alias: fall through. */
9548 case DW_TAG_imported_module:
9549 cu->processing_has_namespace_info = 1;
9550 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9551 || cu->language != language_fortran))
9552 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
9553 dwarf_tag_name (die->tag));
9554 read_import_statement (die, cu);
9557 case DW_TAG_imported_unit:
9558 process_imported_unit_die (die, cu);
9561 case DW_TAG_variable:
9562 read_variable (die, cu);
9566 new_symbol (die, NULL, cu);
9571 /* DWARF name computation. */
9573 /* A helper function for dwarf2_compute_name which determines whether DIE
9574 needs to have the name of the scope prepended to the name listed in the
9578 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9580 struct attribute *attr;
9584 case DW_TAG_namespace:
9585 case DW_TAG_typedef:
9586 case DW_TAG_class_type:
9587 case DW_TAG_interface_type:
9588 case DW_TAG_structure_type:
9589 case DW_TAG_union_type:
9590 case DW_TAG_enumeration_type:
9591 case DW_TAG_enumerator:
9592 case DW_TAG_subprogram:
9593 case DW_TAG_inlined_subroutine:
9595 case DW_TAG_imported_declaration:
9598 case DW_TAG_variable:
9599 case DW_TAG_constant:
9600 /* We only need to prefix "globally" visible variables. These include
9601 any variable marked with DW_AT_external or any variable that
9602 lives in a namespace. [Variables in anonymous namespaces
9603 require prefixing, but they are not DW_AT_external.] */
9605 if (dwarf2_attr (die, DW_AT_specification, cu))
9607 struct dwarf2_cu *spec_cu = cu;
9609 return die_needs_namespace (die_specification (die, &spec_cu),
9613 attr = dwarf2_attr (die, DW_AT_external, cu);
9614 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9615 && die->parent->tag != DW_TAG_module)
9617 /* A variable in a lexical block of some kind does not need a
9618 namespace, even though in C++ such variables may be external
9619 and have a mangled name. */
9620 if (die->parent->tag == DW_TAG_lexical_block
9621 || die->parent->tag == DW_TAG_try_block
9622 || die->parent->tag == DW_TAG_catch_block
9623 || die->parent->tag == DW_TAG_subprogram)
9632 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9633 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9634 defined for the given DIE. */
9636 static struct attribute *
9637 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9639 struct attribute *attr;
9641 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9643 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9648 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9649 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9650 defined for the given DIE. */
9653 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9655 const char *linkage_name;
9657 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9658 if (linkage_name == NULL)
9659 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9661 return linkage_name;
9664 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9665 compute the physname for the object, which include a method's:
9666 - formal parameters (C++),
9667 - receiver type (Go),
9669 The term "physname" is a bit confusing.
9670 For C++, for example, it is the demangled name.
9671 For Go, for example, it's the mangled name.
9673 For Ada, return the DIE's linkage name rather than the fully qualified
9674 name. PHYSNAME is ignored..
9676 The result is allocated on the objfile_obstack and canonicalized. */
9679 dwarf2_compute_name (const char *name,
9680 struct die_info *die, struct dwarf2_cu *cu,
9683 struct objfile *objfile = cu->objfile;
9686 name = dwarf2_name (die, cu);
9688 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9689 but otherwise compute it by typename_concat inside GDB.
9690 FIXME: Actually this is not really true, or at least not always true.
9691 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
9692 Fortran names because there is no mangling standard. So new_symbol_full
9693 will set the demangled name to the result of dwarf2_full_name, and it is
9694 the demangled name that GDB uses if it exists. */
9695 if (cu->language == language_ada
9696 || (cu->language == language_fortran && physname))
9698 /* For Ada unit, we prefer the linkage name over the name, as
9699 the former contains the exported name, which the user expects
9700 to be able to reference. Ideally, we want the user to be able
9701 to reference this entity using either natural or linkage name,
9702 but we haven't started looking at this enhancement yet. */
9703 const char *linkage_name = dw2_linkage_name (die, cu);
9705 if (linkage_name != NULL)
9706 return linkage_name;
9709 /* These are the only languages we know how to qualify names in. */
9711 && (cu->language == language_cplus
9712 || cu->language == language_fortran || cu->language == language_d
9713 || cu->language == language_rust))
9715 if (die_needs_namespace (die, cu))
9718 const char *canonical_name = NULL;
9722 prefix = determine_prefix (die, cu);
9723 if (*prefix != '\0')
9725 char *prefixed_name = typename_concat (NULL, prefix, name,
9728 buf.puts (prefixed_name);
9729 xfree (prefixed_name);
9734 /* Template parameters may be specified in the DIE's DW_AT_name, or
9735 as children with DW_TAG_template_type_param or
9736 DW_TAG_value_type_param. If the latter, add them to the name
9737 here. If the name already has template parameters, then
9738 skip this step; some versions of GCC emit both, and
9739 it is more efficient to use the pre-computed name.
9741 Something to keep in mind about this process: it is very
9742 unlikely, or in some cases downright impossible, to produce
9743 something that will match the mangled name of a function.
9744 If the definition of the function has the same debug info,
9745 we should be able to match up with it anyway. But fallbacks
9746 using the minimal symbol, for instance to find a method
9747 implemented in a stripped copy of libstdc++, will not work.
9748 If we do not have debug info for the definition, we will have to
9749 match them up some other way.
9751 When we do name matching there is a related problem with function
9752 templates; two instantiated function templates are allowed to
9753 differ only by their return types, which we do not add here. */
9755 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9757 struct attribute *attr;
9758 struct die_info *child;
9761 die->building_fullname = 1;
9763 for (child = die->child; child != NULL; child = child->sibling)
9767 const gdb_byte *bytes;
9768 struct dwarf2_locexpr_baton *baton;
9771 if (child->tag != DW_TAG_template_type_param
9772 && child->tag != DW_TAG_template_value_param)
9783 attr = dwarf2_attr (child, DW_AT_type, cu);
9786 complaint (&symfile_complaints,
9787 _("template parameter missing DW_AT_type"));
9788 buf.puts ("UNKNOWN_TYPE");
9791 type = die_type (child, cu);
9793 if (child->tag == DW_TAG_template_type_param)
9795 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
9799 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9802 complaint (&symfile_complaints,
9803 _("template parameter missing "
9804 "DW_AT_const_value"));
9805 buf.puts ("UNKNOWN_VALUE");
9809 dwarf2_const_value_attr (attr, type, name,
9810 &cu->comp_unit_obstack, cu,
9811 &value, &bytes, &baton);
9813 if (TYPE_NOSIGN (type))
9814 /* GDB prints characters as NUMBER 'CHAR'. If that's
9815 changed, this can use value_print instead. */
9816 c_printchar (value, type, &buf);
9819 struct value_print_options opts;
9822 v = dwarf2_evaluate_loc_desc (type, NULL,
9826 else if (bytes != NULL)
9828 v = allocate_value (type);
9829 memcpy (value_contents_writeable (v), bytes,
9830 TYPE_LENGTH (type));
9833 v = value_from_longest (type, value);
9835 /* Specify decimal so that we do not depend on
9837 get_formatted_print_options (&opts, 'd');
9839 value_print (v, &buf, &opts);
9845 die->building_fullname = 0;
9849 /* Close the argument list, with a space if necessary
9850 (nested templates). */
9851 if (!buf.empty () && buf.string ().back () == '>')
9858 /* For C++ methods, append formal parameter type
9859 information, if PHYSNAME. */
9861 if (physname && die->tag == DW_TAG_subprogram
9862 && cu->language == language_cplus)
9864 struct type *type = read_type_die (die, cu);
9866 c_type_print_args (type, &buf, 1, cu->language,
9867 &type_print_raw_options);
9869 if (cu->language == language_cplus)
9871 /* Assume that an artificial first parameter is
9872 "this", but do not crash if it is not. RealView
9873 marks unnamed (and thus unused) parameters as
9874 artificial; there is no way to differentiate
9876 if (TYPE_NFIELDS (type) > 0
9877 && TYPE_FIELD_ARTIFICIAL (type, 0)
9878 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
9879 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
9881 buf.puts (" const");
9885 const std::string &intermediate_name = buf.string ();
9887 if (cu->language == language_cplus)
9889 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9890 &objfile->per_bfd->storage_obstack);
9892 /* If we only computed INTERMEDIATE_NAME, or if
9893 INTERMEDIATE_NAME is already canonical, then we need to
9894 copy it to the appropriate obstack. */
9895 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9896 name = ((const char *)
9897 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9898 intermediate_name.c_str (),
9899 intermediate_name.length ()));
9901 name = canonical_name;
9908 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9909 If scope qualifiers are appropriate they will be added. The result
9910 will be allocated on the storage_obstack, or NULL if the DIE does
9911 not have a name. NAME may either be from a previous call to
9912 dwarf2_name or NULL.
9914 The output string will be canonicalized (if C++). */
9917 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9919 return dwarf2_compute_name (name, die, cu, 0);
9922 /* Construct a physname for the given DIE in CU. NAME may either be
9923 from a previous call to dwarf2_name or NULL. The result will be
9924 allocated on the objfile_objstack or NULL if the DIE does not have a
9927 The output string will be canonicalized (if C++). */
9930 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9932 struct objfile *objfile = cu->objfile;
9933 const char *retval, *mangled = NULL, *canon = NULL;
9936 /* In this case dwarf2_compute_name is just a shortcut not building anything
9938 if (!die_needs_namespace (die, cu))
9939 return dwarf2_compute_name (name, die, cu, 1);
9941 mangled = dw2_linkage_name (die, cu);
9943 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9944 See https://github.com/rust-lang/rust/issues/32925. */
9945 if (cu->language == language_rust && mangled != NULL
9946 && strchr (mangled, '{') != NULL)
9949 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9951 gdb::unique_xmalloc_ptr<char> demangled;
9952 if (mangled != NULL)
9954 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9955 type. It is easier for GDB users to search for such functions as
9956 `name(params)' than `long name(params)'. In such case the minimal
9957 symbol names do not match the full symbol names but for template
9958 functions there is never a need to look up their definition from their
9959 declaration so the only disadvantage remains the minimal symbol
9960 variant `long name(params)' does not have the proper inferior type.
9963 if (cu->language == language_go)
9965 /* This is a lie, but we already lie to the caller new_symbol_full.
9966 new_symbol_full assumes we return the mangled name.
9967 This just undoes that lie until things are cleaned up. */
9971 demangled.reset (gdb_demangle (mangled,
9972 (DMGL_PARAMS | DMGL_ANSI
9976 canon = demangled.get ();
9984 if (canon == NULL || check_physname)
9986 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9988 if (canon != NULL && strcmp (physname, canon) != 0)
9990 /* It may not mean a bug in GDB. The compiler could also
9991 compute DW_AT_linkage_name incorrectly. But in such case
9992 GDB would need to be bug-to-bug compatible. */
9994 complaint (&symfile_complaints,
9995 _("Computed physname <%s> does not match demangled <%s> "
9996 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9997 physname, canon, mangled, to_underlying (die->sect_off),
9998 objfile_name (objfile));
10000 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10001 is available here - over computed PHYSNAME. It is safer
10002 against both buggy GDB and buggy compilers. */
10016 retval = ((const char *)
10017 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10018 retval, strlen (retval)));
10023 /* Inspect DIE in CU for a namespace alias. If one exists, record
10024 a new symbol for it.
10026 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10029 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10031 struct attribute *attr;
10033 /* If the die does not have a name, this is not a namespace
10035 attr = dwarf2_attr (die, DW_AT_name, cu);
10039 struct die_info *d = die;
10040 struct dwarf2_cu *imported_cu = cu;
10042 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10043 keep inspecting DIEs until we hit the underlying import. */
10044 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10045 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10047 attr = dwarf2_attr (d, DW_AT_import, cu);
10051 d = follow_die_ref (d, attr, &imported_cu);
10052 if (d->tag != DW_TAG_imported_declaration)
10056 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10058 complaint (&symfile_complaints,
10059 _("DIE at 0x%x has too many recursively imported "
10060 "declarations"), to_underlying (d->sect_off));
10067 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10069 type = get_die_type_at_offset (sect_off, cu->per_cu);
10070 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10072 /* This declaration is a global namespace alias. Add
10073 a symbol for it whose type is the aliased namespace. */
10074 new_symbol (die, type, cu);
10083 /* Return the using directives repository (global or local?) to use in the
10084 current context for LANGUAGE.
10086 For Ada, imported declarations can materialize renamings, which *may* be
10087 global. However it is impossible (for now?) in DWARF to distinguish
10088 "external" imported declarations and "static" ones. As all imported
10089 declarations seem to be static in all other languages, make them all CU-wide
10090 global only in Ada. */
10092 static struct using_direct **
10093 using_directives (enum language language)
10095 if (language == language_ada && context_stack_depth == 0)
10096 return &global_using_directives;
10098 return &local_using_directives;
10101 /* Read the import statement specified by the given die and record it. */
10104 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10106 struct objfile *objfile = cu->objfile;
10107 struct attribute *import_attr;
10108 struct die_info *imported_die, *child_die;
10109 struct dwarf2_cu *imported_cu;
10110 const char *imported_name;
10111 const char *imported_name_prefix;
10112 const char *canonical_name;
10113 const char *import_alias;
10114 const char *imported_declaration = NULL;
10115 const char *import_prefix;
10116 std::vector<const char *> excludes;
10118 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10119 if (import_attr == NULL)
10121 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10122 dwarf_tag_name (die->tag));
10127 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10128 imported_name = dwarf2_name (imported_die, imported_cu);
10129 if (imported_name == NULL)
10131 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10133 The import in the following code:
10147 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10148 <52> DW_AT_decl_file : 1
10149 <53> DW_AT_decl_line : 6
10150 <54> DW_AT_import : <0x75>
10151 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10152 <59> DW_AT_name : B
10153 <5b> DW_AT_decl_file : 1
10154 <5c> DW_AT_decl_line : 2
10155 <5d> DW_AT_type : <0x6e>
10157 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10158 <76> DW_AT_byte_size : 4
10159 <77> DW_AT_encoding : 5 (signed)
10161 imports the wrong die ( 0x75 instead of 0x58 ).
10162 This case will be ignored until the gcc bug is fixed. */
10166 /* Figure out the local name after import. */
10167 import_alias = dwarf2_name (die, cu);
10169 /* Figure out where the statement is being imported to. */
10170 import_prefix = determine_prefix (die, cu);
10172 /* Figure out what the scope of the imported die is and prepend it
10173 to the name of the imported die. */
10174 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10176 if (imported_die->tag != DW_TAG_namespace
10177 && imported_die->tag != DW_TAG_module)
10179 imported_declaration = imported_name;
10180 canonical_name = imported_name_prefix;
10182 else if (strlen (imported_name_prefix) > 0)
10183 canonical_name = obconcat (&objfile->objfile_obstack,
10184 imported_name_prefix,
10185 (cu->language == language_d ? "." : "::"),
10186 imported_name, (char *) NULL);
10188 canonical_name = imported_name;
10190 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10191 for (child_die = die->child; child_die && child_die->tag;
10192 child_die = sibling_die (child_die))
10194 /* DWARF-4: A Fortran use statement with a “rename list” may be
10195 represented by an imported module entry with an import attribute
10196 referring to the module and owned entries corresponding to those
10197 entities that are renamed as part of being imported. */
10199 if (child_die->tag != DW_TAG_imported_declaration)
10201 complaint (&symfile_complaints,
10202 _("child DW_TAG_imported_declaration expected "
10203 "- DIE at 0x%x [in module %s]"),
10204 to_underlying (child_die->sect_off), objfile_name (objfile));
10208 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10209 if (import_attr == NULL)
10211 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
10212 dwarf_tag_name (child_die->tag));
10217 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10219 imported_name = dwarf2_name (imported_die, imported_cu);
10220 if (imported_name == NULL)
10222 complaint (&symfile_complaints,
10223 _("child DW_TAG_imported_declaration has unknown "
10224 "imported name - DIE at 0x%x [in module %s]"),
10225 to_underlying (child_die->sect_off), objfile_name (objfile));
10229 excludes.push_back (imported_name);
10231 process_die (child_die, cu);
10234 add_using_directive (using_directives (cu->language),
10238 imported_declaration,
10241 &objfile->objfile_obstack);
10244 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10245 types, but gives them a size of zero. Starting with version 14,
10246 ICC is compatible with GCC. */
10249 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10251 if (!cu->checked_producer)
10252 check_producer (cu);
10254 return cu->producer_is_icc_lt_14;
10257 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10258 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10259 this, it was first present in GCC release 4.3.0. */
10262 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10264 if (!cu->checked_producer)
10265 check_producer (cu);
10267 return cu->producer_is_gcc_lt_4_3;
10270 static file_and_directory
10271 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10273 file_and_directory res;
10275 /* Find the filename. Do not use dwarf2_name here, since the filename
10276 is not a source language identifier. */
10277 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10278 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10280 if (res.comp_dir == NULL
10281 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10282 && IS_ABSOLUTE_PATH (res.name))
10284 res.comp_dir_storage = ldirname (res.name);
10285 if (!res.comp_dir_storage.empty ())
10286 res.comp_dir = res.comp_dir_storage.c_str ();
10288 if (res.comp_dir != NULL)
10290 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10291 directory, get rid of it. */
10292 const char *cp = strchr (res.comp_dir, ':');
10294 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10295 res.comp_dir = cp + 1;
10298 if (res.name == NULL)
10299 res.name = "<unknown>";
10304 /* Handle DW_AT_stmt_list for a compilation unit.
10305 DIE is the DW_TAG_compile_unit die for CU.
10306 COMP_DIR is the compilation directory. LOWPC is passed to
10307 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10310 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10311 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10313 struct objfile *objfile = dwarf2_per_objfile->objfile;
10314 struct attribute *attr;
10315 struct line_header line_header_local;
10316 hashval_t line_header_local_hash;
10318 int decode_mapping;
10320 gdb_assert (! cu->per_cu->is_debug_types);
10322 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10326 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10328 /* The line header hash table is only created if needed (it exists to
10329 prevent redundant reading of the line table for partial_units).
10330 If we're given a partial_unit, we'll need it. If we're given a
10331 compile_unit, then use the line header hash table if it's already
10332 created, but don't create one just yet. */
10334 if (dwarf2_per_objfile->line_header_hash == NULL
10335 && die->tag == DW_TAG_partial_unit)
10337 dwarf2_per_objfile->line_header_hash
10338 = htab_create_alloc_ex (127, line_header_hash_voidp,
10339 line_header_eq_voidp,
10340 free_line_header_voidp,
10341 &objfile->objfile_obstack,
10342 hashtab_obstack_allocate,
10343 dummy_obstack_deallocate);
10346 line_header_local.sect_off = line_offset;
10347 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10348 line_header_local_hash = line_header_hash (&line_header_local);
10349 if (dwarf2_per_objfile->line_header_hash != NULL)
10351 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10352 &line_header_local,
10353 line_header_local_hash, NO_INSERT);
10355 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10356 is not present in *SLOT (since if there is something in *SLOT then
10357 it will be for a partial_unit). */
10358 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10360 gdb_assert (*slot != NULL);
10361 cu->line_header = (struct line_header *) *slot;
10366 /* dwarf_decode_line_header does not yet provide sufficient information.
10367 We always have to call also dwarf_decode_lines for it. */
10368 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10372 cu->line_header = lh.release ();
10373 cu->line_header_die_owner = die;
10375 if (dwarf2_per_objfile->line_header_hash == NULL)
10379 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
10380 &line_header_local,
10381 line_header_local_hash, INSERT);
10382 gdb_assert (slot != NULL);
10384 if (slot != NULL && *slot == NULL)
10386 /* This newly decoded line number information unit will be owned
10387 by line_header_hash hash table. */
10388 *slot = cu->line_header;
10389 cu->line_header_die_owner = NULL;
10393 /* We cannot free any current entry in (*slot) as that struct line_header
10394 may be already used by multiple CUs. Create only temporary decoded
10395 line_header for this CU - it may happen at most once for each line
10396 number information unit. And if we're not using line_header_hash
10397 then this is what we want as well. */
10398 gdb_assert (die->tag != DW_TAG_partial_unit);
10400 decode_mapping = (die->tag != DW_TAG_partial_unit);
10401 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10406 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10409 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10411 struct objfile *objfile = dwarf2_per_objfile->objfile;
10412 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10413 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10414 CORE_ADDR highpc = ((CORE_ADDR) 0);
10415 struct attribute *attr;
10416 struct die_info *child_die;
10417 CORE_ADDR baseaddr;
10419 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10421 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10423 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10424 from finish_block. */
10425 if (lowpc == ((CORE_ADDR) -1))
10427 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10429 file_and_directory fnd = find_file_and_directory (die, cu);
10431 prepare_one_comp_unit (cu, die, cu->language);
10433 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10434 standardised yet. As a workaround for the language detection we fall
10435 back to the DW_AT_producer string. */
10436 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10437 cu->language = language_opencl;
10439 /* Similar hack for Go. */
10440 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10441 set_cu_language (DW_LANG_Go, cu);
10443 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
10445 /* Decode line number information if present. We do this before
10446 processing child DIEs, so that the line header table is available
10447 for DW_AT_decl_file. */
10448 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10450 /* Process all dies in compilation unit. */
10451 if (die->child != NULL)
10453 child_die = die->child;
10454 while (child_die && child_die->tag)
10456 process_die (child_die, cu);
10457 child_die = sibling_die (child_die);
10461 /* Decode macro information, if present. Dwarf 2 macro information
10462 refers to information in the line number info statement program
10463 header, so we can only read it if we've read the header
10465 attr = dwarf2_attr (die, DW_AT_macros, cu);
10467 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10468 if (attr && cu->line_header)
10470 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10471 complaint (&symfile_complaints,
10472 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10474 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10478 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10479 if (attr && cu->line_header)
10481 unsigned int macro_offset = DW_UNSND (attr);
10483 dwarf_decode_macros (cu, macro_offset, 0);
10488 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
10489 Create the set of symtabs used by this TU, or if this TU is sharing
10490 symtabs with another TU and the symtabs have already been created
10491 then restore those symtabs in the line header.
10492 We don't need the pc/line-number mapping for type units. */
10495 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
10497 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
10498 struct type_unit_group *tu_group;
10500 struct attribute *attr;
10502 struct signatured_type *sig_type;
10504 gdb_assert (per_cu->is_debug_types);
10505 sig_type = (struct signatured_type *) per_cu;
10507 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10509 /* If we're using .gdb_index (includes -readnow) then
10510 per_cu->type_unit_group may not have been set up yet. */
10511 if (sig_type->type_unit_group == NULL)
10512 sig_type->type_unit_group = get_type_unit_group (cu, attr);
10513 tu_group = sig_type->type_unit_group;
10515 /* If we've already processed this stmt_list there's no real need to
10516 do it again, we could fake it and just recreate the part we need
10517 (file name,index -> symtab mapping). If data shows this optimization
10518 is useful we can do it then. */
10519 first_time = tu_group->compunit_symtab == NULL;
10521 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10526 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10527 lh = dwarf_decode_line_header (line_offset, cu);
10532 dwarf2_start_symtab (cu, "", NULL, 0);
10535 gdb_assert (tu_group->symtabs == NULL);
10536 restart_symtab (tu_group->compunit_symtab, "", 0);
10541 cu->line_header = lh.release ();
10542 cu->line_header_die_owner = die;
10546 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
10548 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10549 still initializing it, and our caller (a few levels up)
10550 process_full_type_unit still needs to know if this is the first
10553 tu_group->num_symtabs = cu->line_header->file_names.size ();
10554 tu_group->symtabs = XNEWVEC (struct symtab *,
10555 cu->line_header->file_names.size ());
10557 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10559 file_entry &fe = cu->line_header->file_names[i];
10561 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
10563 if (current_subfile->symtab == NULL)
10565 /* NOTE: start_subfile will recognize when it's been
10566 passed a file it has already seen. So we can't
10567 assume there's a simple mapping from
10568 cu->line_header->file_names to subfiles, plus
10569 cu->line_header->file_names may contain dups. */
10570 current_subfile->symtab
10571 = allocate_symtab (cust, current_subfile->name);
10574 fe.symtab = current_subfile->symtab;
10575 tu_group->symtabs[i] = fe.symtab;
10580 restart_symtab (tu_group->compunit_symtab, "", 0);
10582 for (i = 0; i < cu->line_header->file_names.size (); ++i)
10584 file_entry &fe = cu->line_header->file_names[i];
10586 fe.symtab = tu_group->symtabs[i];
10590 /* The main symtab is allocated last. Type units don't have DW_AT_name
10591 so they don't have a "real" (so to speak) symtab anyway.
10592 There is later code that will assign the main symtab to all symbols
10593 that don't have one. We need to handle the case of a symbol with a
10594 missing symtab (DW_AT_decl_file) anyway. */
10597 /* Process DW_TAG_type_unit.
10598 For TUs we want to skip the first top level sibling if it's not the
10599 actual type being defined by this TU. In this case the first top
10600 level sibling is there to provide context only. */
10603 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10605 struct die_info *child_die;
10607 prepare_one_comp_unit (cu, die, language_minimal);
10609 /* Initialize (or reinitialize) the machinery for building symtabs.
10610 We do this before processing child DIEs, so that the line header table
10611 is available for DW_AT_decl_file. */
10612 setup_type_unit_groups (die, cu);
10614 if (die->child != NULL)
10616 child_die = die->child;
10617 while (child_die && child_die->tag)
10619 process_die (child_die, cu);
10620 child_die = sibling_die (child_die);
10627 http://gcc.gnu.org/wiki/DebugFission
10628 http://gcc.gnu.org/wiki/DebugFissionDWP
10630 To simplify handling of both DWO files ("object" files with the DWARF info)
10631 and DWP files (a file with the DWOs packaged up into one file), we treat
10632 DWP files as having a collection of virtual DWO files. */
10635 hash_dwo_file (const void *item)
10637 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10640 hash = htab_hash_string (dwo_file->dwo_name);
10641 if (dwo_file->comp_dir != NULL)
10642 hash += htab_hash_string (dwo_file->comp_dir);
10647 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10649 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10650 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10652 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10654 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10655 return lhs->comp_dir == rhs->comp_dir;
10656 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10659 /* Allocate a hash table for DWO files. */
10662 allocate_dwo_file_hash_table (void)
10664 struct objfile *objfile = dwarf2_per_objfile->objfile;
10666 return htab_create_alloc_ex (41,
10670 &objfile->objfile_obstack,
10671 hashtab_obstack_allocate,
10672 dummy_obstack_deallocate);
10675 /* Lookup DWO file DWO_NAME. */
10678 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
10680 struct dwo_file find_entry;
10683 if (dwarf2_per_objfile->dwo_files == NULL)
10684 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10686 memset (&find_entry, 0, sizeof (find_entry));
10687 find_entry.dwo_name = dwo_name;
10688 find_entry.comp_dir = comp_dir;
10689 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
10695 hash_dwo_unit (const void *item)
10697 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10699 /* This drops the top 32 bits of the id, but is ok for a hash. */
10700 return dwo_unit->signature;
10704 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10706 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10707 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10709 /* The signature is assumed to be unique within the DWO file.
10710 So while object file CU dwo_id's always have the value zero,
10711 that's OK, assuming each object file DWO file has only one CU,
10712 and that's the rule for now. */
10713 return lhs->signature == rhs->signature;
10716 /* Allocate a hash table for DWO CUs,TUs.
10717 There is one of these tables for each of CUs,TUs for each DWO file. */
10720 allocate_dwo_unit_table (struct objfile *objfile)
10722 /* Start out with a pretty small number.
10723 Generally DWO files contain only one CU and maybe some TUs. */
10724 return htab_create_alloc_ex (3,
10728 &objfile->objfile_obstack,
10729 hashtab_obstack_allocate,
10730 dummy_obstack_deallocate);
10733 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
10735 struct create_dwo_cu_data
10737 struct dwo_file *dwo_file;
10738 struct dwo_unit dwo_unit;
10741 /* die_reader_func for create_dwo_cu. */
10744 create_dwo_cu_reader (const struct die_reader_specs *reader,
10745 const gdb_byte *info_ptr,
10746 struct die_info *comp_unit_die,
10750 struct dwarf2_cu *cu = reader->cu;
10751 sect_offset sect_off = cu->per_cu->sect_off;
10752 struct dwarf2_section_info *section = cu->per_cu->section;
10753 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
10754 struct dwo_file *dwo_file = data->dwo_file;
10755 struct dwo_unit *dwo_unit = &data->dwo_unit;
10756 struct attribute *attr;
10758 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
10761 complaint (&symfile_complaints,
10762 _("Dwarf Error: debug entry at offset 0x%x is missing"
10763 " its dwo_id [in module %s]"),
10764 to_underlying (sect_off), dwo_file->dwo_name);
10768 dwo_unit->dwo_file = dwo_file;
10769 dwo_unit->signature = DW_UNSND (attr);
10770 dwo_unit->section = section;
10771 dwo_unit->sect_off = sect_off;
10772 dwo_unit->length = cu->per_cu->length;
10774 if (dwarf_read_debug)
10775 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
10776 to_underlying (sect_off),
10777 hex_string (dwo_unit->signature));
10780 /* Create the dwo_units for the CUs in a DWO_FILE.
10781 Note: This function processes DWO files only, not DWP files. */
10784 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info §ion,
10787 struct objfile *objfile = dwarf2_per_objfile->objfile;
10788 const gdb_byte *info_ptr, *end_ptr;
10790 dwarf2_read_section (objfile, §ion);
10791 info_ptr = section.buffer;
10793 if (info_ptr == NULL)
10796 if (dwarf_read_debug)
10798 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
10799 get_section_name (§ion),
10800 get_section_file_name (§ion));
10803 end_ptr = info_ptr + section.size;
10804 while (info_ptr < end_ptr)
10806 struct dwarf2_per_cu_data per_cu;
10807 struct create_dwo_cu_data create_dwo_cu_data;
10808 struct dwo_unit *dwo_unit;
10810 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10812 memset (&create_dwo_cu_data.dwo_unit, 0,
10813 sizeof (create_dwo_cu_data.dwo_unit));
10814 memset (&per_cu, 0, sizeof (per_cu));
10815 per_cu.objfile = objfile;
10816 per_cu.is_debug_types = 0;
10817 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10818 per_cu.section = §ion;
10819 create_dwo_cu_data.dwo_file = &dwo_file;
10821 init_cutu_and_read_dies_no_follow (
10822 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
10823 info_ptr += per_cu.length;
10825 // If the unit could not be parsed, skip it.
10826 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
10829 if (cus_htab == NULL)
10830 cus_htab = allocate_dwo_unit_table (objfile);
10832 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10833 *dwo_unit = create_dwo_cu_data.dwo_unit;
10834 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
10835 gdb_assert (slot != NULL);
10838 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10839 sect_offset dup_sect_off = dup_cu->sect_off;
10841 complaint (&symfile_complaints,
10842 _("debug cu entry at offset 0x%x is duplicate to"
10843 " the entry at offset 0x%x, signature %s"),
10844 to_underlying (sect_off), to_underlying (dup_sect_off),
10845 hex_string (dwo_unit->signature));
10847 *slot = (void *)dwo_unit;
10851 /* DWP file .debug_{cu,tu}_index section format:
10852 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10856 Both index sections have the same format, and serve to map a 64-bit
10857 signature to a set of section numbers. Each section begins with a header,
10858 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10859 indexes, and a pool of 32-bit section numbers. The index sections will be
10860 aligned at 8-byte boundaries in the file.
10862 The index section header consists of:
10864 V, 32 bit version number
10866 N, 32 bit number of compilation units or type units in the index
10867 M, 32 bit number of slots in the hash table
10869 Numbers are recorded using the byte order of the application binary.
10871 The hash table begins at offset 16 in the section, and consists of an array
10872 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10873 order of the application binary). Unused slots in the hash table are 0.
10874 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10876 The parallel table begins immediately after the hash table
10877 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10878 array of 32-bit indexes (using the byte order of the application binary),
10879 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10880 table contains a 32-bit index into the pool of section numbers. For unused
10881 hash table slots, the corresponding entry in the parallel table will be 0.
10883 The pool of section numbers begins immediately following the hash table
10884 (at offset 16 + 12 * M from the beginning of the section). The pool of
10885 section numbers consists of an array of 32-bit words (using the byte order
10886 of the application binary). Each item in the array is indexed starting
10887 from 0. The hash table entry provides the index of the first section
10888 number in the set. Additional section numbers in the set follow, and the
10889 set is terminated by a 0 entry (section number 0 is not used in ELF).
10891 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10892 section must be the first entry in the set, and the .debug_abbrev.dwo must
10893 be the second entry. Other members of the set may follow in any order.
10899 DWP Version 2 combines all the .debug_info, etc. sections into one,
10900 and the entries in the index tables are now offsets into these sections.
10901 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10904 Index Section Contents:
10906 Hash Table of Signatures dwp_hash_table.hash_table
10907 Parallel Table of Indices dwp_hash_table.unit_table
10908 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10909 Table of Section Sizes dwp_hash_table.v2.sizes
10911 The index section header consists of:
10913 V, 32 bit version number
10914 L, 32 bit number of columns in the table of section offsets
10915 N, 32 bit number of compilation units or type units in the index
10916 M, 32 bit number of slots in the hash table
10918 Numbers are recorded using the byte order of the application binary.
10920 The hash table has the same format as version 1.
10921 The parallel table of indices has the same format as version 1,
10922 except that the entries are origin-1 indices into the table of sections
10923 offsets and the table of section sizes.
10925 The table of offsets begins immediately following the parallel table
10926 (at offset 16 + 12 * M from the beginning of the section). The table is
10927 a two-dimensional array of 32-bit words (using the byte order of the
10928 application binary), with L columns and N+1 rows, in row-major order.
10929 Each row in the array is indexed starting from 0. The first row provides
10930 a key to the remaining rows: each column in this row provides an identifier
10931 for a debug section, and the offsets in the same column of subsequent rows
10932 refer to that section. The section identifiers are:
10934 DW_SECT_INFO 1 .debug_info.dwo
10935 DW_SECT_TYPES 2 .debug_types.dwo
10936 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10937 DW_SECT_LINE 4 .debug_line.dwo
10938 DW_SECT_LOC 5 .debug_loc.dwo
10939 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10940 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10941 DW_SECT_MACRO 8 .debug_macro.dwo
10943 The offsets provided by the CU and TU index sections are the base offsets
10944 for the contributions made by each CU or TU to the corresponding section
10945 in the package file. Each CU and TU header contains an abbrev_offset
10946 field, used to find the abbreviations table for that CU or TU within the
10947 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10948 be interpreted as relative to the base offset given in the index section.
10949 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10950 should be interpreted as relative to the base offset for .debug_line.dwo,
10951 and offsets into other debug sections obtained from DWARF attributes should
10952 also be interpreted as relative to the corresponding base offset.
10954 The table of sizes begins immediately following the table of offsets.
10955 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10956 with L columns and N rows, in row-major order. Each row in the array is
10957 indexed starting from 1 (row 0 is shared by the two tables).
10961 Hash table lookup is handled the same in version 1 and 2:
10963 We assume that N and M will not exceed 2^32 - 1.
10964 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10966 Given a 64-bit compilation unit signature or a type signature S, an entry
10967 in the hash table is located as follows:
10969 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10970 the low-order k bits all set to 1.
10972 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10974 3) If the hash table entry at index H matches the signature, use that
10975 entry. If the hash table entry at index H is unused (all zeroes),
10976 terminate the search: the signature is not present in the table.
10978 4) Let H = (H + H') modulo M. Repeat at Step 3.
10980 Because M > N and H' and M are relatively prime, the search is guaranteed
10981 to stop at an unused slot or find the match. */
10983 /* Create a hash table to map DWO IDs to their CU/TU entry in
10984 .debug_{info,types}.dwo in DWP_FILE.
10985 Returns NULL if there isn't one.
10986 Note: This function processes DWP files only, not DWO files. */
10988 static struct dwp_hash_table *
10989 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10991 struct objfile *objfile = dwarf2_per_objfile->objfile;
10992 bfd *dbfd = dwp_file->dbfd;
10993 const gdb_byte *index_ptr, *index_end;
10994 struct dwarf2_section_info *index;
10995 uint32_t version, nr_columns, nr_units, nr_slots;
10996 struct dwp_hash_table *htab;
10998 if (is_debug_types)
10999 index = &dwp_file->sections.tu_index;
11001 index = &dwp_file->sections.cu_index;
11003 if (dwarf2_section_empty_p (index))
11005 dwarf2_read_section (objfile, index);
11007 index_ptr = index->buffer;
11008 index_end = index_ptr + index->size;
11010 version = read_4_bytes (dbfd, index_ptr);
11013 nr_columns = read_4_bytes (dbfd, index_ptr);
11017 nr_units = read_4_bytes (dbfd, index_ptr);
11019 nr_slots = read_4_bytes (dbfd, index_ptr);
11022 if (version != 1 && version != 2)
11024 error (_("Dwarf Error: unsupported DWP file version (%s)"
11025 " [in module %s]"),
11026 pulongest (version), dwp_file->name);
11028 if (nr_slots != (nr_slots & -nr_slots))
11030 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11031 " is not power of 2 [in module %s]"),
11032 pulongest (nr_slots), dwp_file->name);
11035 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11036 htab->version = version;
11037 htab->nr_columns = nr_columns;
11038 htab->nr_units = nr_units;
11039 htab->nr_slots = nr_slots;
11040 htab->hash_table = index_ptr;
11041 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11043 /* Exit early if the table is empty. */
11044 if (nr_slots == 0 || nr_units == 0
11045 || (version == 2 && nr_columns == 0))
11047 /* All must be zero. */
11048 if (nr_slots != 0 || nr_units != 0
11049 || (version == 2 && nr_columns != 0))
11051 complaint (&symfile_complaints,
11052 _("Empty DWP but nr_slots,nr_units,nr_columns not"
11053 " all zero [in modules %s]"),
11061 htab->section_pool.v1.indices =
11062 htab->unit_table + sizeof (uint32_t) * nr_slots;
11063 /* It's harder to decide whether the section is too small in v1.
11064 V1 is deprecated anyway so we punt. */
11068 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11069 int *ids = htab->section_pool.v2.section_ids;
11070 /* Reverse map for error checking. */
11071 int ids_seen[DW_SECT_MAX + 1];
11074 if (nr_columns < 2)
11076 error (_("Dwarf Error: bad DWP hash table, too few columns"
11077 " in section table [in module %s]"),
11080 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11082 error (_("Dwarf Error: bad DWP hash table, too many columns"
11083 " in section table [in module %s]"),
11086 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11087 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
11088 for (i = 0; i < nr_columns; ++i)
11090 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11092 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11094 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11095 " in section table [in module %s]"),
11096 id, dwp_file->name);
11098 if (ids_seen[id] != -1)
11100 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11101 " id %d in section table [in module %s]"),
11102 id, dwp_file->name);
11107 /* Must have exactly one info or types section. */
11108 if (((ids_seen[DW_SECT_INFO] != -1)
11109 + (ids_seen[DW_SECT_TYPES] != -1))
11112 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11113 " DWO info/types section [in module %s]"),
11116 /* Must have an abbrev section. */
11117 if (ids_seen[DW_SECT_ABBREV] == -1)
11119 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11120 " section [in module %s]"),
11123 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11124 htab->section_pool.v2.sizes =
11125 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11126 * nr_units * nr_columns);
11127 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11128 * nr_units * nr_columns))
11131 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11132 " [in module %s]"),
11140 /* Update SECTIONS with the data from SECTP.
11142 This function is like the other "locate" section routines that are
11143 passed to bfd_map_over_sections, but in this context the sections to
11144 read comes from the DWP V1 hash table, not the full ELF section table.
11146 The result is non-zero for success, or zero if an error was found. */
11149 locate_v1_virtual_dwo_sections (asection *sectp,
11150 struct virtual_v1_dwo_sections *sections)
11152 const struct dwop_section_names *names = &dwop_section_names;
11154 if (section_is_p (sectp->name, &names->abbrev_dwo))
11156 /* There can be only one. */
11157 if (sections->abbrev.s.section != NULL)
11159 sections->abbrev.s.section = sectp;
11160 sections->abbrev.size = bfd_get_section_size (sectp);
11162 else if (section_is_p (sectp->name, &names->info_dwo)
11163 || section_is_p (sectp->name, &names->types_dwo))
11165 /* There can be only one. */
11166 if (sections->info_or_types.s.section != NULL)
11168 sections->info_or_types.s.section = sectp;
11169 sections->info_or_types.size = bfd_get_section_size (sectp);
11171 else if (section_is_p (sectp->name, &names->line_dwo))
11173 /* There can be only one. */
11174 if (sections->line.s.section != NULL)
11176 sections->line.s.section = sectp;
11177 sections->line.size = bfd_get_section_size (sectp);
11179 else if (section_is_p (sectp->name, &names->loc_dwo))
11181 /* There can be only one. */
11182 if (sections->loc.s.section != NULL)
11184 sections->loc.s.section = sectp;
11185 sections->loc.size = bfd_get_section_size (sectp);
11187 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11189 /* There can be only one. */
11190 if (sections->macinfo.s.section != NULL)
11192 sections->macinfo.s.section = sectp;
11193 sections->macinfo.size = bfd_get_section_size (sectp);
11195 else if (section_is_p (sectp->name, &names->macro_dwo))
11197 /* There can be only one. */
11198 if (sections->macro.s.section != NULL)
11200 sections->macro.s.section = sectp;
11201 sections->macro.size = bfd_get_section_size (sectp);
11203 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11205 /* There can be only one. */
11206 if (sections->str_offsets.s.section != NULL)
11208 sections->str_offsets.s.section = sectp;
11209 sections->str_offsets.size = bfd_get_section_size (sectp);
11213 /* No other kind of section is valid. */
11220 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11221 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11222 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11223 This is for DWP version 1 files. */
11225 static struct dwo_unit *
11226 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
11227 uint32_t unit_index,
11228 const char *comp_dir,
11229 ULONGEST signature, int is_debug_types)
11231 struct objfile *objfile = dwarf2_per_objfile->objfile;
11232 const struct dwp_hash_table *dwp_htab =
11233 is_debug_types ? dwp_file->tus : dwp_file->cus;
11234 bfd *dbfd = dwp_file->dbfd;
11235 const char *kind = is_debug_types ? "TU" : "CU";
11236 struct dwo_file *dwo_file;
11237 struct dwo_unit *dwo_unit;
11238 struct virtual_v1_dwo_sections sections;
11239 void **dwo_file_slot;
11242 gdb_assert (dwp_file->version == 1);
11244 if (dwarf_read_debug)
11246 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11248 pulongest (unit_index), hex_string (signature),
11252 /* Fetch the sections of this DWO unit.
11253 Put a limit on the number of sections we look for so that bad data
11254 doesn't cause us to loop forever. */
11256 #define MAX_NR_V1_DWO_SECTIONS \
11257 (1 /* .debug_info or .debug_types */ \
11258 + 1 /* .debug_abbrev */ \
11259 + 1 /* .debug_line */ \
11260 + 1 /* .debug_loc */ \
11261 + 1 /* .debug_str_offsets */ \
11262 + 1 /* .debug_macro or .debug_macinfo */ \
11263 + 1 /* trailing zero */)
11265 memset (§ions, 0, sizeof (sections));
11267 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11270 uint32_t section_nr =
11271 read_4_bytes (dbfd,
11272 dwp_htab->section_pool.v1.indices
11273 + (unit_index + i) * sizeof (uint32_t));
11275 if (section_nr == 0)
11277 if (section_nr >= dwp_file->num_sections)
11279 error (_("Dwarf Error: bad DWP hash table, section number too large"
11280 " [in module %s]"),
11284 sectp = dwp_file->elf_sections[section_nr];
11285 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
11287 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11288 " [in module %s]"),
11294 || dwarf2_section_empty_p (§ions.info_or_types)
11295 || dwarf2_section_empty_p (§ions.abbrev))
11297 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11298 " [in module %s]"),
11301 if (i == MAX_NR_V1_DWO_SECTIONS)
11303 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11304 " [in module %s]"),
11308 /* It's easier for the rest of the code if we fake a struct dwo_file and
11309 have dwo_unit "live" in that. At least for now.
11311 The DWP file can be made up of a random collection of CUs and TUs.
11312 However, for each CU + set of TUs that came from the same original DWO
11313 file, we can combine them back into a virtual DWO file to save space
11314 (fewer struct dwo_file objects to allocate). Remember that for really
11315 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11317 std::string virtual_dwo_name =
11318 string_printf ("virtual-dwo/%d-%d-%d-%d",
11319 get_section_id (§ions.abbrev),
11320 get_section_id (§ions.line),
11321 get_section_id (§ions.loc),
11322 get_section_id (§ions.str_offsets));
11323 /* Can we use an existing virtual DWO file? */
11324 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11325 /* Create one if necessary. */
11326 if (*dwo_file_slot == NULL)
11328 if (dwarf_read_debug)
11330 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11331 virtual_dwo_name.c_str ());
11333 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11335 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11336 virtual_dwo_name.c_str (),
11337 virtual_dwo_name.size ());
11338 dwo_file->comp_dir = comp_dir;
11339 dwo_file->sections.abbrev = sections.abbrev;
11340 dwo_file->sections.line = sections.line;
11341 dwo_file->sections.loc = sections.loc;
11342 dwo_file->sections.macinfo = sections.macinfo;
11343 dwo_file->sections.macro = sections.macro;
11344 dwo_file->sections.str_offsets = sections.str_offsets;
11345 /* The "str" section is global to the entire DWP file. */
11346 dwo_file->sections.str = dwp_file->sections.str;
11347 /* The info or types section is assigned below to dwo_unit,
11348 there's no need to record it in dwo_file.
11349 Also, we can't simply record type sections in dwo_file because
11350 we record a pointer into the vector in dwo_unit. As we collect more
11351 types we'll grow the vector and eventually have to reallocate space
11352 for it, invalidating all copies of pointers into the previous
11354 *dwo_file_slot = dwo_file;
11358 if (dwarf_read_debug)
11360 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11361 virtual_dwo_name.c_str ());
11363 dwo_file = (struct dwo_file *) *dwo_file_slot;
11366 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11367 dwo_unit->dwo_file = dwo_file;
11368 dwo_unit->signature = signature;
11369 dwo_unit->section =
11370 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11371 *dwo_unit->section = sections.info_or_types;
11372 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11377 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11378 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11379 piece within that section used by a TU/CU, return a virtual section
11380 of just that piece. */
11382 static struct dwarf2_section_info
11383 create_dwp_v2_section (struct dwarf2_section_info *section,
11384 bfd_size_type offset, bfd_size_type size)
11386 struct dwarf2_section_info result;
11389 gdb_assert (section != NULL);
11390 gdb_assert (!section->is_virtual);
11392 memset (&result, 0, sizeof (result));
11393 result.s.containing_section = section;
11394 result.is_virtual = 1;
11399 sectp = get_section_bfd_section (section);
11401 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11402 bounds of the real section. This is a pretty-rare event, so just
11403 flag an error (easier) instead of a warning and trying to cope. */
11405 || offset + size > bfd_get_section_size (sectp))
11407 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11408 " in section %s [in module %s]"),
11409 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
11410 objfile_name (dwarf2_per_objfile->objfile));
11413 result.virtual_offset = offset;
11414 result.size = size;
11418 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11419 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11420 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11421 This is for DWP version 2 files. */
11423 static struct dwo_unit *
11424 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
11425 uint32_t unit_index,
11426 const char *comp_dir,
11427 ULONGEST signature, int is_debug_types)
11429 struct objfile *objfile = dwarf2_per_objfile->objfile;
11430 const struct dwp_hash_table *dwp_htab =
11431 is_debug_types ? dwp_file->tus : dwp_file->cus;
11432 bfd *dbfd = dwp_file->dbfd;
11433 const char *kind = is_debug_types ? "TU" : "CU";
11434 struct dwo_file *dwo_file;
11435 struct dwo_unit *dwo_unit;
11436 struct virtual_v2_dwo_sections sections;
11437 void **dwo_file_slot;
11440 gdb_assert (dwp_file->version == 2);
11442 if (dwarf_read_debug)
11444 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11446 pulongest (unit_index), hex_string (signature),
11450 /* Fetch the section offsets of this DWO unit. */
11452 memset (§ions, 0, sizeof (sections));
11454 for (i = 0; i < dwp_htab->nr_columns; ++i)
11456 uint32_t offset = read_4_bytes (dbfd,
11457 dwp_htab->section_pool.v2.offsets
11458 + (((unit_index - 1) * dwp_htab->nr_columns
11460 * sizeof (uint32_t)));
11461 uint32_t size = read_4_bytes (dbfd,
11462 dwp_htab->section_pool.v2.sizes
11463 + (((unit_index - 1) * dwp_htab->nr_columns
11465 * sizeof (uint32_t)));
11467 switch (dwp_htab->section_pool.v2.section_ids[i])
11470 case DW_SECT_TYPES:
11471 sections.info_or_types_offset = offset;
11472 sections.info_or_types_size = size;
11474 case DW_SECT_ABBREV:
11475 sections.abbrev_offset = offset;
11476 sections.abbrev_size = size;
11479 sections.line_offset = offset;
11480 sections.line_size = size;
11483 sections.loc_offset = offset;
11484 sections.loc_size = size;
11486 case DW_SECT_STR_OFFSETS:
11487 sections.str_offsets_offset = offset;
11488 sections.str_offsets_size = size;
11490 case DW_SECT_MACINFO:
11491 sections.macinfo_offset = offset;
11492 sections.macinfo_size = size;
11494 case DW_SECT_MACRO:
11495 sections.macro_offset = offset;
11496 sections.macro_size = size;
11501 /* It's easier for the rest of the code if we fake a struct dwo_file and
11502 have dwo_unit "live" in that. At least for now.
11504 The DWP file can be made up of a random collection of CUs and TUs.
11505 However, for each CU + set of TUs that came from the same original DWO
11506 file, we can combine them back into a virtual DWO file to save space
11507 (fewer struct dwo_file objects to allocate). Remember that for really
11508 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11510 std::string virtual_dwo_name =
11511 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11512 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11513 (long) (sections.line_size ? sections.line_offset : 0),
11514 (long) (sections.loc_size ? sections.loc_offset : 0),
11515 (long) (sections.str_offsets_size
11516 ? sections.str_offsets_offset : 0));
11517 /* Can we use an existing virtual DWO file? */
11518 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
11519 /* Create one if necessary. */
11520 if (*dwo_file_slot == NULL)
11522 if (dwarf_read_debug)
11524 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11525 virtual_dwo_name.c_str ());
11527 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11529 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
11530 virtual_dwo_name.c_str (),
11531 virtual_dwo_name.size ());
11532 dwo_file->comp_dir = comp_dir;
11533 dwo_file->sections.abbrev =
11534 create_dwp_v2_section (&dwp_file->sections.abbrev,
11535 sections.abbrev_offset, sections.abbrev_size);
11536 dwo_file->sections.line =
11537 create_dwp_v2_section (&dwp_file->sections.line,
11538 sections.line_offset, sections.line_size);
11539 dwo_file->sections.loc =
11540 create_dwp_v2_section (&dwp_file->sections.loc,
11541 sections.loc_offset, sections.loc_size);
11542 dwo_file->sections.macinfo =
11543 create_dwp_v2_section (&dwp_file->sections.macinfo,
11544 sections.macinfo_offset, sections.macinfo_size);
11545 dwo_file->sections.macro =
11546 create_dwp_v2_section (&dwp_file->sections.macro,
11547 sections.macro_offset, sections.macro_size);
11548 dwo_file->sections.str_offsets =
11549 create_dwp_v2_section (&dwp_file->sections.str_offsets,
11550 sections.str_offsets_offset,
11551 sections.str_offsets_size);
11552 /* The "str" section is global to the entire DWP file. */
11553 dwo_file->sections.str = dwp_file->sections.str;
11554 /* The info or types section is assigned below to dwo_unit,
11555 there's no need to record it in dwo_file.
11556 Also, we can't simply record type sections in dwo_file because
11557 we record a pointer into the vector in dwo_unit. As we collect more
11558 types we'll grow the vector and eventually have to reallocate space
11559 for it, invalidating all copies of pointers into the previous
11561 *dwo_file_slot = dwo_file;
11565 if (dwarf_read_debug)
11567 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11568 virtual_dwo_name.c_str ());
11570 dwo_file = (struct dwo_file *) *dwo_file_slot;
11573 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11574 dwo_unit->dwo_file = dwo_file;
11575 dwo_unit->signature = signature;
11576 dwo_unit->section =
11577 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11578 *dwo_unit->section = create_dwp_v2_section (is_debug_types
11579 ? &dwp_file->sections.types
11580 : &dwp_file->sections.info,
11581 sections.info_or_types_offset,
11582 sections.info_or_types_size);
11583 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11588 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11589 Returns NULL if the signature isn't found. */
11591 static struct dwo_unit *
11592 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
11593 ULONGEST signature, int is_debug_types)
11595 const struct dwp_hash_table *dwp_htab =
11596 is_debug_types ? dwp_file->tus : dwp_file->cus;
11597 bfd *dbfd = dwp_file->dbfd;
11598 uint32_t mask = dwp_htab->nr_slots - 1;
11599 uint32_t hash = signature & mask;
11600 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11603 struct dwo_unit find_dwo_cu;
11605 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11606 find_dwo_cu.signature = signature;
11607 slot = htab_find_slot (is_debug_types
11608 ? dwp_file->loaded_tus
11609 : dwp_file->loaded_cus,
11610 &find_dwo_cu, INSERT);
11613 return (struct dwo_unit *) *slot;
11615 /* Use a for loop so that we don't loop forever on bad debug info. */
11616 for (i = 0; i < dwp_htab->nr_slots; ++i)
11618 ULONGEST signature_in_table;
11620 signature_in_table =
11621 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11622 if (signature_in_table == signature)
11624 uint32_t unit_index =
11625 read_4_bytes (dbfd,
11626 dwp_htab->unit_table + hash * sizeof (uint32_t));
11628 if (dwp_file->version == 1)
11630 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
11631 comp_dir, signature,
11636 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
11637 comp_dir, signature,
11640 return (struct dwo_unit *) *slot;
11642 if (signature_in_table == 0)
11644 hash = (hash + hash2) & mask;
11647 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11648 " [in module %s]"),
11652 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11653 Open the file specified by FILE_NAME and hand it off to BFD for
11654 preliminary analysis. Return a newly initialized bfd *, which
11655 includes a canonicalized copy of FILE_NAME.
11656 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11657 SEARCH_CWD is true if the current directory is to be searched.
11658 It will be searched before debug-file-directory.
11659 If successful, the file is added to the bfd include table of the
11660 objfile's bfd (see gdb_bfd_record_inclusion).
11661 If unable to find/open the file, return NULL.
11662 NOTE: This function is derived from symfile_bfd_open. */
11664 static gdb_bfd_ref_ptr
11665 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
11668 char *absolute_name;
11669 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11670 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11671 to debug_file_directory. */
11673 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11677 if (*debug_file_directory != '\0')
11678 search_path = concat (".", dirname_separator_string,
11679 debug_file_directory, (char *) NULL);
11681 search_path = xstrdup (".");
11684 search_path = xstrdup (debug_file_directory);
11686 flags = OPF_RETURN_REALPATH;
11688 flags |= OPF_SEARCH_IN_PATH;
11689 desc = openp (search_path, flags, file_name,
11690 O_RDONLY | O_BINARY, &absolute_name);
11691 xfree (search_path);
11695 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
11696 xfree (absolute_name);
11697 if (sym_bfd == NULL)
11699 bfd_set_cacheable (sym_bfd.get (), 1);
11701 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11704 /* Success. Record the bfd as having been included by the objfile's bfd.
11705 This is important because things like demangled_names_hash lives in the
11706 objfile's per_bfd space and may have references to things like symbol
11707 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11708 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11713 /* Try to open DWO file FILE_NAME.
11714 COMP_DIR is the DW_AT_comp_dir attribute.
11715 The result is the bfd handle of the file.
11716 If there is a problem finding or opening the file, return NULL.
11717 Upon success, the canonicalized path of the file is stored in the bfd,
11718 same as symfile_bfd_open. */
11720 static gdb_bfd_ref_ptr
11721 open_dwo_file (const char *file_name, const char *comp_dir)
11723 if (IS_ABSOLUTE_PATH (file_name))
11724 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
11726 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11728 if (comp_dir != NULL)
11730 char *path_to_try = concat (comp_dir, SLASH_STRING,
11731 file_name, (char *) NULL);
11733 /* NOTE: If comp_dir is a relative path, this will also try the
11734 search path, which seems useful. */
11735 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
11736 1 /*search_cwd*/));
11737 xfree (path_to_try);
11742 /* That didn't work, try debug-file-directory, which, despite its name,
11743 is a list of paths. */
11745 if (*debug_file_directory == '\0')
11748 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
11751 /* This function is mapped across the sections and remembers the offset and
11752 size of each of the DWO debugging sections we are interested in. */
11755 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
11757 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
11758 const struct dwop_section_names *names = &dwop_section_names;
11760 if (section_is_p (sectp->name, &names->abbrev_dwo))
11762 dwo_sections->abbrev.s.section = sectp;
11763 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
11765 else if (section_is_p (sectp->name, &names->info_dwo))
11767 dwo_sections->info.s.section = sectp;
11768 dwo_sections->info.size = bfd_get_section_size (sectp);
11770 else if (section_is_p (sectp->name, &names->line_dwo))
11772 dwo_sections->line.s.section = sectp;
11773 dwo_sections->line.size = bfd_get_section_size (sectp);
11775 else if (section_is_p (sectp->name, &names->loc_dwo))
11777 dwo_sections->loc.s.section = sectp;
11778 dwo_sections->loc.size = bfd_get_section_size (sectp);
11780 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11782 dwo_sections->macinfo.s.section = sectp;
11783 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
11785 else if (section_is_p (sectp->name, &names->macro_dwo))
11787 dwo_sections->macro.s.section = sectp;
11788 dwo_sections->macro.size = bfd_get_section_size (sectp);
11790 else if (section_is_p (sectp->name, &names->str_dwo))
11792 dwo_sections->str.s.section = sectp;
11793 dwo_sections->str.size = bfd_get_section_size (sectp);
11795 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11797 dwo_sections->str_offsets.s.section = sectp;
11798 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
11800 else if (section_is_p (sectp->name, &names->types_dwo))
11802 struct dwarf2_section_info type_section;
11804 memset (&type_section, 0, sizeof (type_section));
11805 type_section.s.section = sectp;
11806 type_section.size = bfd_get_section_size (sectp);
11807 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
11812 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11813 by PER_CU. This is for the non-DWP case.
11814 The result is NULL if DWO_NAME can't be found. */
11816 static struct dwo_file *
11817 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
11818 const char *dwo_name, const char *comp_dir)
11820 struct objfile *objfile = dwarf2_per_objfile->objfile;
11821 struct dwo_file *dwo_file;
11822 struct cleanup *cleanups;
11824 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
11827 if (dwarf_read_debug)
11828 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
11831 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
11832 dwo_file->dwo_name = dwo_name;
11833 dwo_file->comp_dir = comp_dir;
11834 dwo_file->dbfd = dbfd.release ();
11836 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
11838 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
11839 &dwo_file->sections);
11841 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
11843 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
11846 discard_cleanups (cleanups);
11848 if (dwarf_read_debug)
11849 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
11854 /* This function is mapped across the sections and remembers the offset and
11855 size of each of the DWP debugging sections common to version 1 and 2 that
11856 we are interested in. */
11859 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
11860 void *dwp_file_ptr)
11862 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11863 const struct dwop_section_names *names = &dwop_section_names;
11864 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11866 /* Record the ELF section number for later lookup: this is what the
11867 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11868 gdb_assert (elf_section_nr < dwp_file->num_sections);
11869 dwp_file->elf_sections[elf_section_nr] = sectp;
11871 /* Look for specific sections that we need. */
11872 if (section_is_p (sectp->name, &names->str_dwo))
11874 dwp_file->sections.str.s.section = sectp;
11875 dwp_file->sections.str.size = bfd_get_section_size (sectp);
11877 else if (section_is_p (sectp->name, &names->cu_index))
11879 dwp_file->sections.cu_index.s.section = sectp;
11880 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11882 else if (section_is_p (sectp->name, &names->tu_index))
11884 dwp_file->sections.tu_index.s.section = sectp;
11885 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11889 /* This function is mapped across the sections and remembers the offset and
11890 size of each of the DWP version 2 debugging sections that we are interested
11891 in. This is split into a separate function because we don't know if we
11892 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11895 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11897 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11898 const struct dwop_section_names *names = &dwop_section_names;
11899 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11901 /* Record the ELF section number for later lookup: this is what the
11902 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11903 gdb_assert (elf_section_nr < dwp_file->num_sections);
11904 dwp_file->elf_sections[elf_section_nr] = sectp;
11906 /* Look for specific sections that we need. */
11907 if (section_is_p (sectp->name, &names->abbrev_dwo))
11909 dwp_file->sections.abbrev.s.section = sectp;
11910 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11912 else if (section_is_p (sectp->name, &names->info_dwo))
11914 dwp_file->sections.info.s.section = sectp;
11915 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11917 else if (section_is_p (sectp->name, &names->line_dwo))
11919 dwp_file->sections.line.s.section = sectp;
11920 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11922 else if (section_is_p (sectp->name, &names->loc_dwo))
11924 dwp_file->sections.loc.s.section = sectp;
11925 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11927 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11929 dwp_file->sections.macinfo.s.section = sectp;
11930 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11932 else if (section_is_p (sectp->name, &names->macro_dwo))
11934 dwp_file->sections.macro.s.section = sectp;
11935 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11937 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11939 dwp_file->sections.str_offsets.s.section = sectp;
11940 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11942 else if (section_is_p (sectp->name, &names->types_dwo))
11944 dwp_file->sections.types.s.section = sectp;
11945 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11949 /* Hash function for dwp_file loaded CUs/TUs. */
11952 hash_dwp_loaded_cutus (const void *item)
11954 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11956 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11957 return dwo_unit->signature;
11960 /* Equality function for dwp_file loaded CUs/TUs. */
11963 eq_dwp_loaded_cutus (const void *a, const void *b)
11965 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11966 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11968 return dua->signature == dub->signature;
11971 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11974 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11976 return htab_create_alloc_ex (3,
11977 hash_dwp_loaded_cutus,
11978 eq_dwp_loaded_cutus,
11980 &objfile->objfile_obstack,
11981 hashtab_obstack_allocate,
11982 dummy_obstack_deallocate);
11985 /* Try to open DWP file FILE_NAME.
11986 The result is the bfd handle of the file.
11987 If there is a problem finding or opening the file, return NULL.
11988 Upon success, the canonicalized path of the file is stored in the bfd,
11989 same as symfile_bfd_open. */
11991 static gdb_bfd_ref_ptr
11992 open_dwp_file (const char *file_name)
11994 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11995 1 /*search_cwd*/));
11999 /* Work around upstream bug 15652.
12000 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12001 [Whether that's a "bug" is debatable, but it is getting in our way.]
12002 We have no real idea where the dwp file is, because gdb's realpath-ing
12003 of the executable's path may have discarded the needed info.
12004 [IWBN if the dwp file name was recorded in the executable, akin to
12005 .gnu_debuglink, but that doesn't exist yet.]
12006 Strip the directory from FILE_NAME and search again. */
12007 if (*debug_file_directory != '\0')
12009 /* Don't implicitly search the current directory here.
12010 If the user wants to search "." to handle this case,
12011 it must be added to debug-file-directory. */
12012 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
12019 /* Initialize the use of the DWP file for the current objfile.
12020 By convention the name of the DWP file is ${objfile}.dwp.
12021 The result is NULL if it can't be found. */
12023 static struct dwp_file *
12024 open_and_init_dwp_file (void)
12026 struct objfile *objfile = dwarf2_per_objfile->objfile;
12027 struct dwp_file *dwp_file;
12029 /* Try to find first .dwp for the binary file before any symbolic links
12032 /* If the objfile is a debug file, find the name of the real binary
12033 file and get the name of dwp file from there. */
12034 std::string dwp_name;
12035 if (objfile->separate_debug_objfile_backlink != NULL)
12037 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12038 const char *backlink_basename = lbasename (backlink->original_name);
12040 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12043 dwp_name = objfile->original_name;
12045 dwp_name += ".dwp";
12047 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
12049 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12051 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12052 dwp_name = objfile_name (objfile);
12053 dwp_name += ".dwp";
12054 dbfd = open_dwp_file (dwp_name.c_str ());
12059 if (dwarf_read_debug)
12060 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12063 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
12064 dwp_file->name = bfd_get_filename (dbfd.get ());
12065 dwp_file->dbfd = dbfd.release ();
12067 /* +1: section 0 is unused */
12068 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
12069 dwp_file->elf_sections =
12070 OBSTACK_CALLOC (&objfile->objfile_obstack,
12071 dwp_file->num_sections, asection *);
12073 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
12076 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
12078 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
12080 /* The DWP file version is stored in the hash table. Oh well. */
12081 if (dwp_file->cus && dwp_file->tus
12082 && dwp_file->cus->version != dwp_file->tus->version)
12084 /* Technically speaking, we should try to limp along, but this is
12085 pretty bizarre. We use pulongest here because that's the established
12086 portability solution (e.g, we cannot use %u for uint32_t). */
12087 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12088 " TU version %s [in DWP file %s]"),
12089 pulongest (dwp_file->cus->version),
12090 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12094 dwp_file->version = dwp_file->cus->version;
12095 else if (dwp_file->tus)
12096 dwp_file->version = dwp_file->tus->version;
12098 dwp_file->version = 2;
12100 if (dwp_file->version == 2)
12101 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
12104 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12105 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12107 if (dwarf_read_debug)
12109 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12110 fprintf_unfiltered (gdb_stdlog,
12111 " %s CUs, %s TUs\n",
12112 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12113 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12119 /* Wrapper around open_and_init_dwp_file, only open it once. */
12121 static struct dwp_file *
12122 get_dwp_file (void)
12124 if (! dwarf2_per_objfile->dwp_checked)
12126 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
12127 dwarf2_per_objfile->dwp_checked = 1;
12129 return dwarf2_per_objfile->dwp_file;
12132 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12133 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12134 or in the DWP file for the objfile, referenced by THIS_UNIT.
12135 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12136 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12138 This is called, for example, when wanting to read a variable with a
12139 complex location. Therefore we don't want to do file i/o for every call.
12140 Therefore we don't want to look for a DWO file on every call.
12141 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12142 then we check if we've already seen DWO_NAME, and only THEN do we check
12145 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12146 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12148 static struct dwo_unit *
12149 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12150 const char *dwo_name, const char *comp_dir,
12151 ULONGEST signature, int is_debug_types)
12153 struct objfile *objfile = dwarf2_per_objfile->objfile;
12154 const char *kind = is_debug_types ? "TU" : "CU";
12155 void **dwo_file_slot;
12156 struct dwo_file *dwo_file;
12157 struct dwp_file *dwp_file;
12159 /* First see if there's a DWP file.
12160 If we have a DWP file but didn't find the DWO inside it, don't
12161 look for the original DWO file. It makes gdb behave differently
12162 depending on whether one is debugging in the build tree. */
12164 dwp_file = get_dwp_file ();
12165 if (dwp_file != NULL)
12167 const struct dwp_hash_table *dwp_htab =
12168 is_debug_types ? dwp_file->tus : dwp_file->cus;
12170 if (dwp_htab != NULL)
12172 struct dwo_unit *dwo_cutu =
12173 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
12174 signature, is_debug_types);
12176 if (dwo_cutu != NULL)
12178 if (dwarf_read_debug)
12180 fprintf_unfiltered (gdb_stdlog,
12181 "Virtual DWO %s %s found: @%s\n",
12182 kind, hex_string (signature),
12183 host_address_to_string (dwo_cutu));
12191 /* No DWP file, look for the DWO file. */
12193 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
12194 if (*dwo_file_slot == NULL)
12196 /* Read in the file and build a table of the CUs/TUs it contains. */
12197 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12199 /* NOTE: This will be NULL if unable to open the file. */
12200 dwo_file = (struct dwo_file *) *dwo_file_slot;
12202 if (dwo_file != NULL)
12204 struct dwo_unit *dwo_cutu = NULL;
12206 if (is_debug_types && dwo_file->tus)
12208 struct dwo_unit find_dwo_cutu;
12210 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12211 find_dwo_cutu.signature = signature;
12213 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
12215 else if (!is_debug_types && dwo_file->cus)
12217 struct dwo_unit find_dwo_cutu;
12219 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12220 find_dwo_cutu.signature = signature;
12221 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
12225 if (dwo_cutu != NULL)
12227 if (dwarf_read_debug)
12229 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12230 kind, dwo_name, hex_string (signature),
12231 host_address_to_string (dwo_cutu));
12238 /* We didn't find it. This could mean a dwo_id mismatch, or
12239 someone deleted the DWO/DWP file, or the search path isn't set up
12240 correctly to find the file. */
12242 if (dwarf_read_debug)
12244 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12245 kind, dwo_name, hex_string (signature));
12248 /* This is a warning and not a complaint because it can be caused by
12249 pilot error (e.g., user accidentally deleting the DWO). */
12251 /* Print the name of the DWP file if we looked there, helps the user
12252 better diagnose the problem. */
12253 std::string dwp_text;
12255 if (dwp_file != NULL)
12256 dwp_text = string_printf (" [in DWP file %s]",
12257 lbasename (dwp_file->name));
12259 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
12260 " [in module %s]"),
12261 kind, dwo_name, hex_string (signature),
12263 this_unit->is_debug_types ? "TU" : "CU",
12264 to_underlying (this_unit->sect_off), objfile_name (objfile));
12269 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12270 See lookup_dwo_cutu_unit for details. */
12272 static struct dwo_unit *
12273 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12274 const char *dwo_name, const char *comp_dir,
12275 ULONGEST signature)
12277 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12280 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12281 See lookup_dwo_cutu_unit for details. */
12283 static struct dwo_unit *
12284 lookup_dwo_type_unit (struct signatured_type *this_tu,
12285 const char *dwo_name, const char *comp_dir)
12287 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12290 /* Traversal function for queue_and_load_all_dwo_tus. */
12293 queue_and_load_dwo_tu (void **slot, void *info)
12295 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12296 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12297 ULONGEST signature = dwo_unit->signature;
12298 struct signatured_type *sig_type =
12299 lookup_dwo_signatured_type (per_cu->cu, signature);
12301 if (sig_type != NULL)
12303 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12305 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12306 a real dependency of PER_CU on SIG_TYPE. That is detected later
12307 while processing PER_CU. */
12308 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12309 load_full_type_unit (sig_cu);
12310 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
12316 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12317 The DWO may have the only definition of the type, though it may not be
12318 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12319 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12322 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12324 struct dwo_unit *dwo_unit;
12325 struct dwo_file *dwo_file;
12327 gdb_assert (!per_cu->is_debug_types);
12328 gdb_assert (get_dwp_file () == NULL);
12329 gdb_assert (per_cu->cu != NULL);
12331 dwo_unit = per_cu->cu->dwo_unit;
12332 gdb_assert (dwo_unit != NULL);
12334 dwo_file = dwo_unit->dwo_file;
12335 if (dwo_file->tus != NULL)
12336 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
12339 /* Free all resources associated with DWO_FILE.
12340 Close the DWO file and munmap the sections.
12341 All memory should be on the objfile obstack. */
12344 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
12347 /* Note: dbfd is NULL for virtual DWO files. */
12348 gdb_bfd_unref (dwo_file->dbfd);
12350 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
12353 /* Wrapper for free_dwo_file for use in cleanups. */
12356 free_dwo_file_cleanup (void *arg)
12358 struct dwo_file *dwo_file = (struct dwo_file *) arg;
12359 struct objfile *objfile = dwarf2_per_objfile->objfile;
12361 free_dwo_file (dwo_file, objfile);
12364 /* Traversal function for free_dwo_files. */
12367 free_dwo_file_from_slot (void **slot, void *info)
12369 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
12370 struct objfile *objfile = (struct objfile *) info;
12372 free_dwo_file (dwo_file, objfile);
12377 /* Free all resources associated with DWO_FILES. */
12380 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
12382 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
12385 /* Read in various DIEs. */
12387 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12388 Inherit only the children of the DW_AT_abstract_origin DIE not being
12389 already referenced by DW_AT_abstract_origin from the children of the
12393 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12395 struct die_info *child_die;
12396 sect_offset *offsetp;
12397 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12398 struct die_info *origin_die;
12399 /* Iterator of the ORIGIN_DIE children. */
12400 struct die_info *origin_child_die;
12401 struct attribute *attr;
12402 struct dwarf2_cu *origin_cu;
12403 struct pending **origin_previous_list_in_scope;
12405 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12409 /* Note that following die references may follow to a die in a
12413 origin_die = follow_die_ref (die, attr, &origin_cu);
12415 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12417 origin_previous_list_in_scope = origin_cu->list_in_scope;
12418 origin_cu->list_in_scope = cu->list_in_scope;
12420 if (die->tag != origin_die->tag
12421 && !(die->tag == DW_TAG_inlined_subroutine
12422 && origin_die->tag == DW_TAG_subprogram))
12423 complaint (&symfile_complaints,
12424 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
12425 to_underlying (die->sect_off),
12426 to_underlying (origin_die->sect_off));
12428 std::vector<sect_offset> offsets;
12430 for (child_die = die->child;
12431 child_die && child_die->tag;
12432 child_die = sibling_die (child_die))
12434 struct die_info *child_origin_die;
12435 struct dwarf2_cu *child_origin_cu;
12437 /* We are trying to process concrete instance entries:
12438 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12439 it's not relevant to our analysis here. i.e. detecting DIEs that are
12440 present in the abstract instance but not referenced in the concrete
12442 if (child_die->tag == DW_TAG_call_site
12443 || child_die->tag == DW_TAG_GNU_call_site)
12446 /* For each CHILD_DIE, find the corresponding child of
12447 ORIGIN_DIE. If there is more than one layer of
12448 DW_AT_abstract_origin, follow them all; there shouldn't be,
12449 but GCC versions at least through 4.4 generate this (GCC PR
12451 child_origin_die = child_die;
12452 child_origin_cu = cu;
12455 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12459 child_origin_die = follow_die_ref (child_origin_die, attr,
12463 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12464 counterpart may exist. */
12465 if (child_origin_die != child_die)
12467 if (child_die->tag != child_origin_die->tag
12468 && !(child_die->tag == DW_TAG_inlined_subroutine
12469 && child_origin_die->tag == DW_TAG_subprogram))
12470 complaint (&symfile_complaints,
12471 _("Child DIE 0x%x and its abstract origin 0x%x have "
12473 to_underlying (child_die->sect_off),
12474 to_underlying (child_origin_die->sect_off));
12475 if (child_origin_die->parent != origin_die)
12476 complaint (&symfile_complaints,
12477 _("Child DIE 0x%x and its abstract origin 0x%x have "
12478 "different parents"),
12479 to_underlying (child_die->sect_off),
12480 to_underlying (child_origin_die->sect_off));
12482 offsets.push_back (child_origin_die->sect_off);
12485 std::sort (offsets.begin (), offsets.end ());
12486 sect_offset *offsets_end = offsets.data () + offsets.size ();
12487 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12488 if (offsetp[-1] == *offsetp)
12489 complaint (&symfile_complaints,
12490 _("Multiple children of DIE 0x%x refer "
12491 "to DIE 0x%x as their abstract origin"),
12492 to_underlying (die->sect_off), to_underlying (*offsetp));
12494 offsetp = offsets.data ();
12495 origin_child_die = origin_die->child;
12496 while (origin_child_die && origin_child_die->tag)
12498 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12499 while (offsetp < offsets_end
12500 && *offsetp < origin_child_die->sect_off)
12502 if (offsetp >= offsets_end
12503 || *offsetp > origin_child_die->sect_off)
12505 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12506 Check whether we're already processing ORIGIN_CHILD_DIE.
12507 This can happen with mutually referenced abstract_origins.
12509 if (!origin_child_die->in_process)
12510 process_die (origin_child_die, origin_cu);
12512 origin_child_die = sibling_die (origin_child_die);
12514 origin_cu->list_in_scope = origin_previous_list_in_scope;
12518 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12520 struct objfile *objfile = cu->objfile;
12521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12522 struct context_stack *newobj;
12525 struct die_info *child_die;
12526 struct attribute *attr, *call_line, *call_file;
12528 CORE_ADDR baseaddr;
12529 struct block *block;
12530 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12531 std::vector<struct symbol *> template_args;
12532 struct template_symbol *templ_func = NULL;
12536 /* If we do not have call site information, we can't show the
12537 caller of this inlined function. That's too confusing, so
12538 only use the scope for local variables. */
12539 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12540 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12541 if (call_line == NULL || call_file == NULL)
12543 read_lexical_block_scope (die, cu);
12548 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12550 name = dwarf2_name (die, cu);
12552 /* Ignore functions with missing or empty names. These are actually
12553 illegal according to the DWARF standard. */
12556 complaint (&symfile_complaints,
12557 _("missing name for subprogram DIE at %d"),
12558 to_underlying (die->sect_off));
12562 /* Ignore functions with missing or invalid low and high pc attributes. */
12563 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12564 <= PC_BOUNDS_INVALID)
12566 attr = dwarf2_attr (die, DW_AT_external, cu);
12567 if (!attr || !DW_UNSND (attr))
12568 complaint (&symfile_complaints,
12569 _("cannot get low and high bounds "
12570 "for subprogram DIE at %d"),
12571 to_underlying (die->sect_off));
12575 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12576 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12578 /* If we have any template arguments, then we must allocate a
12579 different sort of symbol. */
12580 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12582 if (child_die->tag == DW_TAG_template_type_param
12583 || child_die->tag == DW_TAG_template_value_param)
12585 templ_func = allocate_template_symbol (objfile);
12586 templ_func->subclass = SYMBOL_TEMPLATE;
12591 newobj = push_context (0, lowpc);
12592 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
12593 (struct symbol *) templ_func);
12595 /* If there is a location expression for DW_AT_frame_base, record
12597 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12599 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12601 /* If there is a location for the static link, record it. */
12602 newobj->static_link = NULL;
12603 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12606 newobj->static_link
12607 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12608 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
12611 cu->list_in_scope = &local_symbols;
12613 if (die->child != NULL)
12615 child_die = die->child;
12616 while (child_die && child_die->tag)
12618 if (child_die->tag == DW_TAG_template_type_param
12619 || child_die->tag == DW_TAG_template_value_param)
12621 struct symbol *arg = new_symbol (child_die, NULL, cu);
12624 template_args.push_back (arg);
12627 process_die (child_die, cu);
12628 child_die = sibling_die (child_die);
12632 inherit_abstract_dies (die, cu);
12634 /* If we have a DW_AT_specification, we might need to import using
12635 directives from the context of the specification DIE. See the
12636 comment in determine_prefix. */
12637 if (cu->language == language_cplus
12638 && dwarf2_attr (die, DW_AT_specification, cu))
12640 struct dwarf2_cu *spec_cu = cu;
12641 struct die_info *spec_die = die_specification (die, &spec_cu);
12645 child_die = spec_die->child;
12646 while (child_die && child_die->tag)
12648 if (child_die->tag == DW_TAG_imported_module)
12649 process_die (child_die, spec_cu);
12650 child_die = sibling_die (child_die);
12653 /* In some cases, GCC generates specification DIEs that
12654 themselves contain DW_AT_specification attributes. */
12655 spec_die = die_specification (spec_die, &spec_cu);
12659 newobj = pop_context ();
12660 /* Make a block for the local symbols within. */
12661 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
12662 newobj->static_link, lowpc, highpc);
12664 /* For C++, set the block's scope. */
12665 if ((cu->language == language_cplus
12666 || cu->language == language_fortran
12667 || cu->language == language_d
12668 || cu->language == language_rust)
12669 && cu->processing_has_namespace_info)
12670 block_set_scope (block, determine_prefix (die, cu),
12671 &objfile->objfile_obstack);
12673 /* If we have address ranges, record them. */
12674 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12676 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
12678 /* Attach template arguments to function. */
12679 if (!template_args.empty ())
12681 gdb_assert (templ_func != NULL);
12683 templ_func->n_template_arguments = template_args.size ();
12684 templ_func->template_arguments
12685 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12686 templ_func->n_template_arguments);
12687 memcpy (templ_func->template_arguments,
12688 template_args.data (),
12689 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12692 /* In C++, we can have functions nested inside functions (e.g., when
12693 a function declares a class that has methods). This means that
12694 when we finish processing a function scope, we may need to go
12695 back to building a containing block's symbol lists. */
12696 local_symbols = newobj->locals;
12697 local_using_directives = newobj->local_using_directives;
12699 /* If we've finished processing a top-level function, subsequent
12700 symbols go in the file symbol list. */
12701 if (outermost_context_p ())
12702 cu->list_in_scope = &file_symbols;
12705 /* Process all the DIES contained within a lexical block scope. Start
12706 a new scope, process the dies, and then close the scope. */
12709 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12711 struct objfile *objfile = cu->objfile;
12712 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12713 struct context_stack *newobj;
12714 CORE_ADDR lowpc, highpc;
12715 struct die_info *child_die;
12716 CORE_ADDR baseaddr;
12718 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12720 /* Ignore blocks with missing or invalid low and high pc attributes. */
12721 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12722 as multiple lexical blocks? Handling children in a sane way would
12723 be nasty. Might be easier to properly extend generic blocks to
12724 describe ranges. */
12725 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12727 case PC_BOUNDS_NOT_PRESENT:
12728 /* DW_TAG_lexical_block has no attributes, process its children as if
12729 there was no wrapping by that DW_TAG_lexical_block.
12730 GCC does no longer produces such DWARF since GCC r224161. */
12731 for (child_die = die->child;
12732 child_die != NULL && child_die->tag;
12733 child_die = sibling_die (child_die))
12734 process_die (child_die, cu);
12736 case PC_BOUNDS_INVALID:
12739 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12740 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12742 push_context (0, lowpc);
12743 if (die->child != NULL)
12745 child_die = die->child;
12746 while (child_die && child_die->tag)
12748 process_die (child_die, cu);
12749 child_die = sibling_die (child_die);
12752 inherit_abstract_dies (die, cu);
12753 newobj = pop_context ();
12755 if (local_symbols != NULL || local_using_directives != NULL)
12757 struct block *block
12758 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
12759 newobj->start_addr, highpc);
12761 /* Note that recording ranges after traversing children, as we
12762 do here, means that recording a parent's ranges entails
12763 walking across all its children's ranges as they appear in
12764 the address map, which is quadratic behavior.
12766 It would be nicer to record the parent's ranges before
12767 traversing its children, simply overriding whatever you find
12768 there. But since we don't even decide whether to create a
12769 block until after we've traversed its children, that's hard
12771 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12773 local_symbols = newobj->locals;
12774 local_using_directives = newobj->local_using_directives;
12777 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12780 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12782 struct objfile *objfile = cu->objfile;
12783 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12784 CORE_ADDR pc, baseaddr;
12785 struct attribute *attr;
12786 struct call_site *call_site, call_site_local;
12789 struct die_info *child_die;
12791 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12793 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12796 /* This was a pre-DWARF-5 GNU extension alias
12797 for DW_AT_call_return_pc. */
12798 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12802 complaint (&symfile_complaints,
12803 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
12804 "DIE 0x%x [in module %s]"),
12805 to_underlying (die->sect_off), objfile_name (objfile));
12808 pc = attr_value_as_address (attr) + baseaddr;
12809 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12811 if (cu->call_site_htab == NULL)
12812 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
12813 NULL, &objfile->objfile_obstack,
12814 hashtab_obstack_allocate, NULL);
12815 call_site_local.pc = pc;
12816 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12819 complaint (&symfile_complaints,
12820 _("Duplicate PC %s for DW_TAG_call_site "
12821 "DIE 0x%x [in module %s]"),
12822 paddress (gdbarch, pc), to_underlying (die->sect_off),
12823 objfile_name (objfile));
12827 /* Count parameters at the caller. */
12830 for (child_die = die->child; child_die && child_die->tag;
12831 child_die = sibling_die (child_die))
12833 if (child_die->tag != DW_TAG_call_site_parameter
12834 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12836 complaint (&symfile_complaints,
12837 _("Tag %d is not DW_TAG_call_site_parameter in "
12838 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12839 child_die->tag, to_underlying (child_die->sect_off),
12840 objfile_name (objfile));
12848 = ((struct call_site *)
12849 obstack_alloc (&objfile->objfile_obstack,
12850 sizeof (*call_site)
12851 + (sizeof (*call_site->parameter) * (nparams - 1))));
12853 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
12854 call_site->pc = pc;
12856 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12857 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12859 struct die_info *func_die;
12861 /* Skip also over DW_TAG_inlined_subroutine. */
12862 for (func_die = die->parent;
12863 func_die && func_die->tag != DW_TAG_subprogram
12864 && func_die->tag != DW_TAG_subroutine_type;
12865 func_die = func_die->parent);
12867 /* DW_AT_call_all_calls is a superset
12868 of DW_AT_call_all_tail_calls. */
12870 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12871 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12872 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12873 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12875 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12876 not complete. But keep CALL_SITE for look ups via call_site_htab,
12877 both the initial caller containing the real return address PC and
12878 the final callee containing the current PC of a chain of tail
12879 calls do not need to have the tail call list complete. But any
12880 function candidate for a virtual tail call frame searched via
12881 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12882 determined unambiguously. */
12886 struct type *func_type = NULL;
12889 func_type = get_die_type (func_die, cu);
12890 if (func_type != NULL)
12892 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12894 /* Enlist this call site to the function. */
12895 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12896 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12899 complaint (&symfile_complaints,
12900 _("Cannot find function owning DW_TAG_call_site "
12901 "DIE 0x%x [in module %s]"),
12902 to_underlying (die->sect_off), objfile_name (objfile));
12906 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12908 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12910 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12913 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12914 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12916 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12917 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12918 /* Keep NULL DWARF_BLOCK. */;
12919 else if (attr_form_is_block (attr))
12921 struct dwarf2_locexpr_baton *dlbaton;
12923 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12924 dlbaton->data = DW_BLOCK (attr)->data;
12925 dlbaton->size = DW_BLOCK (attr)->size;
12926 dlbaton->per_cu = cu->per_cu;
12928 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12930 else if (attr_form_is_ref (attr))
12932 struct dwarf2_cu *target_cu = cu;
12933 struct die_info *target_die;
12935 target_die = follow_die_ref (die, attr, &target_cu);
12936 gdb_assert (target_cu->objfile == objfile);
12937 if (die_is_declaration (target_die, target_cu))
12939 const char *target_physname;
12941 /* Prefer the mangled name; otherwise compute the demangled one. */
12942 target_physname = dw2_linkage_name (target_die, target_cu);
12943 if (target_physname == NULL)
12944 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12945 if (target_physname == NULL)
12946 complaint (&symfile_complaints,
12947 _("DW_AT_call_target target DIE has invalid "
12948 "physname, for referencing DIE 0x%x [in module %s]"),
12949 to_underlying (die->sect_off), objfile_name (objfile));
12951 SET_FIELD_PHYSNAME (call_site->target, target_physname);
12957 /* DW_AT_entry_pc should be preferred. */
12958 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
12959 <= PC_BOUNDS_INVALID)
12960 complaint (&symfile_complaints,
12961 _("DW_AT_call_target target DIE has invalid "
12962 "low pc, for referencing DIE 0x%x [in module %s]"),
12963 to_underlying (die->sect_off), objfile_name (objfile));
12966 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12967 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12972 complaint (&symfile_complaints,
12973 _("DW_TAG_call_site DW_AT_call_target is neither "
12974 "block nor reference, for DIE 0x%x [in module %s]"),
12975 to_underlying (die->sect_off), objfile_name (objfile));
12977 call_site->per_cu = cu->per_cu;
12979 for (child_die = die->child;
12980 child_die && child_die->tag;
12981 child_die = sibling_die (child_die))
12983 struct call_site_parameter *parameter;
12984 struct attribute *loc, *origin;
12986 if (child_die->tag != DW_TAG_call_site_parameter
12987 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12989 /* Already printed the complaint above. */
12993 gdb_assert (call_site->parameter_count < nparams);
12994 parameter = &call_site->parameter[call_site->parameter_count];
12996 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12997 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12998 register is contained in DW_AT_call_value. */
13000 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13001 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13002 if (origin == NULL)
13004 /* This was a pre-DWARF-5 GNU extension alias
13005 for DW_AT_call_parameter. */
13006 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13008 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
13010 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13012 sect_offset sect_off
13013 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13014 if (!offset_in_cu_p (&cu->header, sect_off))
13016 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13017 binding can be done only inside one CU. Such referenced DIE
13018 therefore cannot be even moved to DW_TAG_partial_unit. */
13019 complaint (&symfile_complaints,
13020 _("DW_AT_call_parameter offset is not in CU for "
13021 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13022 to_underlying (child_die->sect_off),
13023 objfile_name (objfile));
13026 parameter->u.param_cu_off
13027 = (cu_offset) (sect_off - cu->header.sect_off);
13029 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
13031 complaint (&symfile_complaints,
13032 _("No DW_FORM_block* DW_AT_location for "
13033 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13034 to_underlying (child_die->sect_off), objfile_name (objfile));
13039 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13040 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13041 if (parameter->u.dwarf_reg != -1)
13042 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13043 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13044 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13045 ¶meter->u.fb_offset))
13046 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13049 complaint (&symfile_complaints,
13050 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
13051 "for DW_FORM_block* DW_AT_location is supported for "
13052 "DW_TAG_call_site child DIE 0x%x "
13054 to_underlying (child_die->sect_off),
13055 objfile_name (objfile));
13060 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13062 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13063 if (!attr_form_is_block (attr))
13065 complaint (&symfile_complaints,
13066 _("No DW_FORM_block* DW_AT_call_value for "
13067 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13068 to_underlying (child_die->sect_off),
13069 objfile_name (objfile));
13072 parameter->value = DW_BLOCK (attr)->data;
13073 parameter->value_size = DW_BLOCK (attr)->size;
13075 /* Parameters are not pre-cleared by memset above. */
13076 parameter->data_value = NULL;
13077 parameter->data_value_size = 0;
13078 call_site->parameter_count++;
13080 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13082 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13085 if (!attr_form_is_block (attr))
13086 complaint (&symfile_complaints,
13087 _("No DW_FORM_block* DW_AT_call_data_value for "
13088 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13089 to_underlying (child_die->sect_off),
13090 objfile_name (objfile));
13093 parameter->data_value = DW_BLOCK (attr)->data;
13094 parameter->data_value_size = DW_BLOCK (attr)->size;
13100 /* Helper function for read_variable. If DIE represents a virtual
13101 table, then return the type of the concrete object that is
13102 associated with the virtual table. Otherwise, return NULL. */
13104 static struct type *
13105 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13107 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13111 /* Find the type DIE. */
13112 struct die_info *type_die = NULL;
13113 struct dwarf2_cu *type_cu = cu;
13115 if (attr_form_is_ref (attr))
13116 type_die = follow_die_ref (die, attr, &type_cu);
13117 if (type_die == NULL)
13120 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13122 return die_containing_type (type_die, type_cu);
13125 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13128 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13130 struct rust_vtable_symbol *storage = NULL;
13132 if (cu->language == language_rust)
13134 struct type *containing_type = rust_containing_type (die, cu);
13136 if (containing_type != NULL)
13138 struct objfile *objfile = cu->objfile;
13140 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
13141 struct rust_vtable_symbol);
13142 initialize_objfile_symbol (storage);
13143 storage->concrete_type = containing_type;
13144 storage->subclass = SYMBOL_RUST_VTABLE;
13148 new_symbol_full (die, NULL, cu, storage);
13151 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13152 reading .debug_rnglists.
13153 Callback's type should be:
13154 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13155 Return true if the attributes are present and valid, otherwise,
13158 template <typename Callback>
13160 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13161 Callback &&callback)
13163 struct objfile *objfile = cu->objfile;
13164 bfd *obfd = objfile->obfd;
13165 /* Base address selection entry. */
13168 const gdb_byte *buffer;
13169 CORE_ADDR baseaddr;
13170 bool overflow = false;
13172 found_base = cu->base_known;
13173 base = cu->base_address;
13175 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
13176 if (offset >= dwarf2_per_objfile->rnglists.size)
13178 complaint (&symfile_complaints,
13179 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13183 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13185 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13189 /* Initialize it due to a false compiler warning. */
13190 CORE_ADDR range_beginning = 0, range_end = 0;
13191 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13192 + dwarf2_per_objfile->rnglists.size);
13193 unsigned int bytes_read;
13195 if (buffer == buf_end)
13200 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13203 case DW_RLE_end_of_list:
13205 case DW_RLE_base_address:
13206 if (buffer + cu->header.addr_size > buf_end)
13211 base = read_address (obfd, buffer, cu, &bytes_read);
13213 buffer += bytes_read;
13215 case DW_RLE_start_length:
13216 if (buffer + cu->header.addr_size > buf_end)
13221 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13222 buffer += bytes_read;
13223 range_end = (range_beginning
13224 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13225 buffer += bytes_read;
13226 if (buffer > buf_end)
13232 case DW_RLE_offset_pair:
13233 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13234 buffer += bytes_read;
13235 if (buffer > buf_end)
13240 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13241 buffer += bytes_read;
13242 if (buffer > buf_end)
13248 case DW_RLE_start_end:
13249 if (buffer + 2 * cu->header.addr_size > buf_end)
13254 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13255 buffer += bytes_read;
13256 range_end = read_address (obfd, buffer, cu, &bytes_read);
13257 buffer += bytes_read;
13260 complaint (&symfile_complaints,
13261 _("Invalid .debug_rnglists data (no base address)"));
13264 if (rlet == DW_RLE_end_of_list || overflow)
13266 if (rlet == DW_RLE_base_address)
13271 /* We have no valid base address for the ranges
13273 complaint (&symfile_complaints,
13274 _("Invalid .debug_rnglists data (no base address)"));
13278 if (range_beginning > range_end)
13280 /* Inverted range entries are invalid. */
13281 complaint (&symfile_complaints,
13282 _("Invalid .debug_rnglists data (inverted range)"));
13286 /* Empty range entries have no effect. */
13287 if (range_beginning == range_end)
13290 range_beginning += base;
13293 /* A not-uncommon case of bad debug info.
13294 Don't pollute the addrmap with bad data. */
13295 if (range_beginning + baseaddr == 0
13296 && !dwarf2_per_objfile->has_section_at_zero)
13298 complaint (&symfile_complaints,
13299 _(".debug_rnglists entry has start address of zero"
13300 " [in module %s]"), objfile_name (objfile));
13304 callback (range_beginning, range_end);
13309 complaint (&symfile_complaints,
13310 _("Offset %d is not terminated "
13311 "for DW_AT_ranges attribute"),
13319 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13320 Callback's type should be:
13321 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13322 Return 1 if the attributes are present and valid, otherwise, return 0. */
13324 template <typename Callback>
13326 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13327 Callback &&callback)
13329 struct objfile *objfile = cu->objfile;
13330 struct comp_unit_head *cu_header = &cu->header;
13331 bfd *obfd = objfile->obfd;
13332 unsigned int addr_size = cu_header->addr_size;
13333 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13334 /* Base address selection entry. */
13337 unsigned int dummy;
13338 const gdb_byte *buffer;
13339 CORE_ADDR baseaddr;
13341 if (cu_header->version >= 5)
13342 return dwarf2_rnglists_process (offset, cu, callback);
13344 found_base = cu->base_known;
13345 base = cu->base_address;
13347 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
13348 if (offset >= dwarf2_per_objfile->ranges.size)
13350 complaint (&symfile_complaints,
13351 _("Offset %d out of bounds for DW_AT_ranges attribute"),
13355 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13357 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13361 CORE_ADDR range_beginning, range_end;
13363 range_beginning = read_address (obfd, buffer, cu, &dummy);
13364 buffer += addr_size;
13365 range_end = read_address (obfd, buffer, cu, &dummy);
13366 buffer += addr_size;
13367 offset += 2 * addr_size;
13369 /* An end of list marker is a pair of zero addresses. */
13370 if (range_beginning == 0 && range_end == 0)
13371 /* Found the end of list entry. */
13374 /* Each base address selection entry is a pair of 2 values.
13375 The first is the largest possible address, the second is
13376 the base address. Check for a base address here. */
13377 if ((range_beginning & mask) == mask)
13379 /* If we found the largest possible address, then we already
13380 have the base address in range_end. */
13388 /* We have no valid base address for the ranges
13390 complaint (&symfile_complaints,
13391 _("Invalid .debug_ranges data (no base address)"));
13395 if (range_beginning > range_end)
13397 /* Inverted range entries are invalid. */
13398 complaint (&symfile_complaints,
13399 _("Invalid .debug_ranges data (inverted range)"));
13403 /* Empty range entries have no effect. */
13404 if (range_beginning == range_end)
13407 range_beginning += base;
13410 /* A not-uncommon case of bad debug info.
13411 Don't pollute the addrmap with bad data. */
13412 if (range_beginning + baseaddr == 0
13413 && !dwarf2_per_objfile->has_section_at_zero)
13415 complaint (&symfile_complaints,
13416 _(".debug_ranges entry has start address of zero"
13417 " [in module %s]"), objfile_name (objfile));
13421 callback (range_beginning, range_end);
13427 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13428 Return 1 if the attributes are present and valid, otherwise, return 0.
13429 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13432 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13433 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13434 struct partial_symtab *ranges_pst)
13436 struct objfile *objfile = cu->objfile;
13437 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13438 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
13439 SECT_OFF_TEXT (objfile));
13442 CORE_ADDR high = 0;
13445 retval = dwarf2_ranges_process (offset, cu,
13446 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13448 if (ranges_pst != NULL)
13453 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13454 range_beginning + baseaddr);
13455 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
13456 range_end + baseaddr);
13457 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
13461 /* FIXME: This is recording everything as a low-high
13462 segment of consecutive addresses. We should have a
13463 data structure for discontiguous block ranges
13467 low = range_beginning;
13473 if (range_beginning < low)
13474 low = range_beginning;
13475 if (range_end > high)
13483 /* If the first entry is an end-of-list marker, the range
13484 describes an empty scope, i.e. no instructions. */
13490 *high_return = high;
13494 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13495 definition for the return value. *LOWPC and *HIGHPC are set iff
13496 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13498 static enum pc_bounds_kind
13499 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13500 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13501 struct partial_symtab *pst)
13503 struct attribute *attr;
13504 struct attribute *attr_high;
13506 CORE_ADDR high = 0;
13507 enum pc_bounds_kind ret;
13509 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13512 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13515 low = attr_value_as_address (attr);
13516 high = attr_value_as_address (attr_high);
13517 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13521 /* Found high w/o low attribute. */
13522 return PC_BOUNDS_INVALID;
13524 /* Found consecutive range of addresses. */
13525 ret = PC_BOUNDS_HIGH_LOW;
13529 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13532 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13533 We take advantage of the fact that DW_AT_ranges does not appear
13534 in DW_TAG_compile_unit of DWO files. */
13535 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13536 unsigned int ranges_offset = (DW_UNSND (attr)
13537 + (need_ranges_base
13541 /* Value of the DW_AT_ranges attribute is the offset in the
13542 .debug_ranges section. */
13543 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13544 return PC_BOUNDS_INVALID;
13545 /* Found discontinuous range of addresses. */
13546 ret = PC_BOUNDS_RANGES;
13549 return PC_BOUNDS_NOT_PRESENT;
13552 /* read_partial_die has also the strict LOW < HIGH requirement. */
13554 return PC_BOUNDS_INVALID;
13556 /* When using the GNU linker, .gnu.linkonce. sections are used to
13557 eliminate duplicate copies of functions and vtables and such.
13558 The linker will arbitrarily choose one and discard the others.
13559 The AT_*_pc values for such functions refer to local labels in
13560 these sections. If the section from that file was discarded, the
13561 labels are not in the output, so the relocs get a value of 0.
13562 If this is a discarded function, mark the pc bounds as invalid,
13563 so that GDB will ignore it. */
13564 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13565 return PC_BOUNDS_INVALID;
13573 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13574 its low and high PC addresses. Do nothing if these addresses could not
13575 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13576 and HIGHPC to the high address if greater than HIGHPC. */
13579 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13580 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13581 struct dwarf2_cu *cu)
13583 CORE_ADDR low, high;
13584 struct die_info *child = die->child;
13586 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13588 *lowpc = std::min (*lowpc, low);
13589 *highpc = std::max (*highpc, high);
13592 /* If the language does not allow nested subprograms (either inside
13593 subprograms or lexical blocks), we're done. */
13594 if (cu->language != language_ada)
13597 /* Check all the children of the given DIE. If it contains nested
13598 subprograms, then check their pc bounds. Likewise, we need to
13599 check lexical blocks as well, as they may also contain subprogram
13601 while (child && child->tag)
13603 if (child->tag == DW_TAG_subprogram
13604 || child->tag == DW_TAG_lexical_block)
13605 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13606 child = sibling_die (child);
13610 /* Get the low and high pc's represented by the scope DIE, and store
13611 them in *LOWPC and *HIGHPC. If the correct values can't be
13612 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13615 get_scope_pc_bounds (struct die_info *die,
13616 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13617 struct dwarf2_cu *cu)
13619 CORE_ADDR best_low = (CORE_ADDR) -1;
13620 CORE_ADDR best_high = (CORE_ADDR) 0;
13621 CORE_ADDR current_low, current_high;
13623 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
13624 >= PC_BOUNDS_RANGES)
13626 best_low = current_low;
13627 best_high = current_high;
13631 struct die_info *child = die->child;
13633 while (child && child->tag)
13635 switch (child->tag) {
13636 case DW_TAG_subprogram:
13637 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13639 case DW_TAG_namespace:
13640 case DW_TAG_module:
13641 /* FIXME: carlton/2004-01-16: Should we do this for
13642 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13643 that current GCC's always emit the DIEs corresponding
13644 to definitions of methods of classes as children of a
13645 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13646 the DIEs giving the declarations, which could be
13647 anywhere). But I don't see any reason why the
13648 standards says that they have to be there. */
13649 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
13651 if (current_low != ((CORE_ADDR) -1))
13653 best_low = std::min (best_low, current_low);
13654 best_high = std::max (best_high, current_high);
13662 child = sibling_die (child);
13667 *highpc = best_high;
13670 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13674 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13675 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13677 struct objfile *objfile = cu->objfile;
13678 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13679 struct attribute *attr;
13680 struct attribute *attr_high;
13682 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13685 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13688 CORE_ADDR low = attr_value_as_address (attr);
13689 CORE_ADDR high = attr_value_as_address (attr_high);
13691 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
13694 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13695 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13696 record_block_range (block, low, high - 1);
13700 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13703 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
13704 We take advantage of the fact that DW_AT_ranges does not appear
13705 in DW_TAG_compile_unit of DWO files. */
13706 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13708 /* The value of the DW_AT_ranges attribute is the offset of the
13709 address range list in the .debug_ranges section. */
13710 unsigned long offset = (DW_UNSND (attr)
13711 + (need_ranges_base ? cu->ranges_base : 0));
13712 const gdb_byte *buffer;
13714 /* For some target architectures, but not others, the
13715 read_address function sign-extends the addresses it returns.
13716 To recognize base address selection entries, we need a
13718 unsigned int addr_size = cu->header.addr_size;
13719 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13721 /* The base address, to which the next pair is relative. Note
13722 that this 'base' is a DWARF concept: most entries in a range
13723 list are relative, to reduce the number of relocs against the
13724 debugging information. This is separate from this function's
13725 'baseaddr' argument, which GDB uses to relocate debugging
13726 information from a shared library based on the address at
13727 which the library was loaded. */
13728 CORE_ADDR base = cu->base_address;
13729 int base_known = cu->base_known;
13731 dwarf2_ranges_process (offset, cu,
13732 [&] (CORE_ADDR start, CORE_ADDR end)
13736 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13737 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13738 record_block_range (block, start, end - 1);
13743 /* Check whether the producer field indicates either of GCC < 4.6, or the
13744 Intel C/C++ compiler, and cache the result in CU. */
13747 check_producer (struct dwarf2_cu *cu)
13751 if (cu->producer == NULL)
13753 /* For unknown compilers expect their behavior is DWARF version
13756 GCC started to support .debug_types sections by -gdwarf-4 since
13757 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13758 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13759 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13760 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13762 else if (producer_is_gcc (cu->producer, &major, &minor))
13764 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13765 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13767 else if (producer_is_icc (cu->producer, &major, &minor))
13768 cu->producer_is_icc_lt_14 = major < 14;
13771 /* For other non-GCC compilers, expect their behavior is DWARF version
13775 cu->checked_producer = 1;
13778 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13779 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13780 during 4.6.0 experimental. */
13783 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13785 if (!cu->checked_producer)
13786 check_producer (cu);
13788 return cu->producer_is_gxx_lt_4_6;
13791 /* Return the default accessibility type if it is not overriden by
13792 DW_AT_accessibility. */
13794 static enum dwarf_access_attribute
13795 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13797 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13799 /* The default DWARF 2 accessibility for members is public, the default
13800 accessibility for inheritance is private. */
13802 if (die->tag != DW_TAG_inheritance)
13803 return DW_ACCESS_public;
13805 return DW_ACCESS_private;
13809 /* DWARF 3+ defines the default accessibility a different way. The same
13810 rules apply now for DW_TAG_inheritance as for the members and it only
13811 depends on the container kind. */
13813 if (die->parent->tag == DW_TAG_class_type)
13814 return DW_ACCESS_private;
13816 return DW_ACCESS_public;
13820 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
13821 offset. If the attribute was not found return 0, otherwise return
13822 1. If it was found but could not properly be handled, set *OFFSET
13826 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
13829 struct attribute *attr;
13831 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13836 /* Note that we do not check for a section offset first here.
13837 This is because DW_AT_data_member_location is new in DWARF 4,
13838 so if we see it, we can assume that a constant form is really
13839 a constant and not a section offset. */
13840 if (attr_form_is_constant (attr))
13841 *offset = dwarf2_get_attr_constant_value (attr, 0);
13842 else if (attr_form_is_section_offset (attr))
13843 dwarf2_complex_location_expr_complaint ();
13844 else if (attr_form_is_block (attr))
13845 *offset = decode_locdesc (DW_BLOCK (attr), cu);
13847 dwarf2_complex_location_expr_complaint ();
13855 /* Add an aggregate field to the field list. */
13858 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13859 struct dwarf2_cu *cu)
13861 struct objfile *objfile = cu->objfile;
13862 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13863 struct nextfield *new_field;
13864 struct attribute *attr;
13866 const char *fieldname = "";
13868 /* Allocate a new field list entry and link it in. */
13869 new_field = XNEW (struct nextfield);
13870 make_cleanup (xfree, new_field);
13871 memset (new_field, 0, sizeof (struct nextfield));
13873 if (die->tag == DW_TAG_inheritance)
13875 new_field->next = fip->baseclasses;
13876 fip->baseclasses = new_field;
13880 new_field->next = fip->fields;
13881 fip->fields = new_field;
13885 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13887 new_field->accessibility = DW_UNSND (attr);
13889 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
13890 if (new_field->accessibility != DW_ACCESS_public)
13891 fip->non_public_fields = 1;
13893 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13895 new_field->virtuality = DW_UNSND (attr);
13897 new_field->virtuality = DW_VIRTUALITY_none;
13899 fp = &new_field->field;
13901 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
13905 /* Data member other than a C++ static data member. */
13907 /* Get type of field. */
13908 fp->type = die_type (die, cu);
13910 SET_FIELD_BITPOS (*fp, 0);
13912 /* Get bit size of field (zero if none). */
13913 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13916 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13920 FIELD_BITSIZE (*fp) = 0;
13923 /* Get bit offset of field. */
13924 if (handle_data_member_location (die, cu, &offset))
13925 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13926 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13929 if (gdbarch_bits_big_endian (gdbarch))
13931 /* For big endian bits, the DW_AT_bit_offset gives the
13932 additional bit offset from the MSB of the containing
13933 anonymous object to the MSB of the field. We don't
13934 have to do anything special since we don't need to
13935 know the size of the anonymous object. */
13936 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
13940 /* For little endian bits, compute the bit offset to the
13941 MSB of the anonymous object, subtract off the number of
13942 bits from the MSB of the field to the MSB of the
13943 object, and then subtract off the number of bits of
13944 the field itself. The result is the bit offset of
13945 the LSB of the field. */
13946 int anonymous_size;
13947 int bit_offset = DW_UNSND (attr);
13949 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13952 /* The size of the anonymous object containing
13953 the bit field is explicit, so use the
13954 indicated size (in bytes). */
13955 anonymous_size = DW_UNSND (attr);
13959 /* The size of the anonymous object containing
13960 the bit field must be inferred from the type
13961 attribute of the data member containing the
13963 anonymous_size = TYPE_LENGTH (fp->type);
13965 SET_FIELD_BITPOS (*fp,
13966 (FIELD_BITPOS (*fp)
13967 + anonymous_size * bits_per_byte
13968 - bit_offset - FIELD_BITSIZE (*fp)));
13971 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13973 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13974 + dwarf2_get_attr_constant_value (attr, 0)));
13976 /* Get name of field. */
13977 fieldname = dwarf2_name (die, cu);
13978 if (fieldname == NULL)
13981 /* The name is already allocated along with this objfile, so we don't
13982 need to duplicate it for the type. */
13983 fp->name = fieldname;
13985 /* Change accessibility for artificial fields (e.g. virtual table
13986 pointer or virtual base class pointer) to private. */
13987 if (dwarf2_attr (die, DW_AT_artificial, cu))
13989 FIELD_ARTIFICIAL (*fp) = 1;
13990 new_field->accessibility = DW_ACCESS_private;
13991 fip->non_public_fields = 1;
13994 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13996 /* C++ static member. */
13998 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13999 is a declaration, but all versions of G++ as of this writing
14000 (so through at least 3.2.1) incorrectly generate
14001 DW_TAG_variable tags. */
14003 const char *physname;
14005 /* Get name of field. */
14006 fieldname = dwarf2_name (die, cu);
14007 if (fieldname == NULL)
14010 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14012 /* Only create a symbol if this is an external value.
14013 new_symbol checks this and puts the value in the global symbol
14014 table, which we want. If it is not external, new_symbol
14015 will try to put the value in cu->list_in_scope which is wrong. */
14016 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14018 /* A static const member, not much different than an enum as far as
14019 we're concerned, except that we can support more types. */
14020 new_symbol (die, NULL, cu);
14023 /* Get physical name. */
14024 physname = dwarf2_physname (fieldname, die, cu);
14026 /* The name is already allocated along with this objfile, so we don't
14027 need to duplicate it for the type. */
14028 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14029 FIELD_TYPE (*fp) = die_type (die, cu);
14030 FIELD_NAME (*fp) = fieldname;
14032 else if (die->tag == DW_TAG_inheritance)
14036 /* C++ base class field. */
14037 if (handle_data_member_location (die, cu, &offset))
14038 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14039 FIELD_BITSIZE (*fp) = 0;
14040 FIELD_TYPE (*fp) = die_type (die, cu);
14041 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
14042 fip->nbaseclasses++;
14046 /* Can the type given by DIE define another type? */
14049 type_can_define_types (const struct die_info *die)
14053 case DW_TAG_typedef:
14054 case DW_TAG_class_type:
14055 case DW_TAG_structure_type:
14056 case DW_TAG_union_type:
14057 case DW_TAG_enumeration_type:
14065 /* Add a type definition defined in the scope of the FIP's class. */
14068 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14069 struct dwarf2_cu *cu)
14071 struct decl_field_list *new_field;
14072 struct decl_field *fp;
14074 /* Allocate a new field list entry and link it in. */
14075 new_field = XCNEW (struct decl_field_list);
14076 make_cleanup (xfree, new_field);
14078 gdb_assert (type_can_define_types (die));
14080 fp = &new_field->field;
14082 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14083 fp->name = dwarf2_name (die, cu);
14084 fp->type = read_type_die (die, cu);
14086 /* Save accessibility. */
14087 enum dwarf_access_attribute accessibility;
14088 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14090 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14092 accessibility = dwarf2_default_access_attribute (die, cu);
14093 switch (accessibility)
14095 case DW_ACCESS_public:
14096 /* The assumed value if neither private nor protected. */
14098 case DW_ACCESS_private:
14099 fp->is_private = 1;
14101 case DW_ACCESS_protected:
14102 fp->is_protected = 1;
14105 complaint (&symfile_complaints,
14106 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14109 if (die->tag == DW_TAG_typedef)
14111 new_field->next = fip->typedef_field_list;
14112 fip->typedef_field_list = new_field;
14113 fip->typedef_field_list_count++;
14117 new_field->next = fip->nested_types_list;
14118 fip->nested_types_list = new_field;
14119 fip->nested_types_list_count++;
14123 /* Create the vector of fields, and attach it to the type. */
14126 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14127 struct dwarf2_cu *cu)
14129 int nfields = fip->nfields;
14131 /* Record the field count, allocate space for the array of fields,
14132 and create blank accessibility bitfields if necessary. */
14133 TYPE_NFIELDS (type) = nfields;
14134 TYPE_FIELDS (type) = (struct field *)
14135 TYPE_ALLOC (type, sizeof (struct field) * nfields);
14136 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
14138 if (fip->non_public_fields && cu->language != language_ada)
14140 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14142 TYPE_FIELD_PRIVATE_BITS (type) =
14143 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14144 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14146 TYPE_FIELD_PROTECTED_BITS (type) =
14147 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14148 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14150 TYPE_FIELD_IGNORE_BITS (type) =
14151 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14152 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14155 /* If the type has baseclasses, allocate and clear a bit vector for
14156 TYPE_FIELD_VIRTUAL_BITS. */
14157 if (fip->nbaseclasses && cu->language != language_ada)
14159 int num_bytes = B_BYTES (fip->nbaseclasses);
14160 unsigned char *pointer;
14162 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14163 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14164 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14165 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
14166 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
14169 /* Copy the saved-up fields into the field vector. Start from the head of
14170 the list, adding to the tail of the field array, so that they end up in
14171 the same order in the array in which they were added to the list. */
14172 while (nfields-- > 0)
14174 struct nextfield *fieldp;
14178 fieldp = fip->fields;
14179 fip->fields = fieldp->next;
14183 fieldp = fip->baseclasses;
14184 fip->baseclasses = fieldp->next;
14187 TYPE_FIELD (type, nfields) = fieldp->field;
14188 switch (fieldp->accessibility)
14190 case DW_ACCESS_private:
14191 if (cu->language != language_ada)
14192 SET_TYPE_FIELD_PRIVATE (type, nfields);
14195 case DW_ACCESS_protected:
14196 if (cu->language != language_ada)
14197 SET_TYPE_FIELD_PROTECTED (type, nfields);
14200 case DW_ACCESS_public:
14204 /* Unknown accessibility. Complain and treat it as public. */
14206 complaint (&symfile_complaints, _("unsupported accessibility %d"),
14207 fieldp->accessibility);
14211 if (nfields < fip->nbaseclasses)
14213 switch (fieldp->virtuality)
14215 case DW_VIRTUALITY_virtual:
14216 case DW_VIRTUALITY_pure_virtual:
14217 if (cu->language == language_ada)
14218 error (_("unexpected virtuality in component of Ada type"));
14219 SET_TYPE_FIELD_VIRTUAL (type, nfields);
14226 /* Return true if this member function is a constructor, false
14230 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14232 const char *fieldname;
14233 const char *type_name;
14236 if (die->parent == NULL)
14239 if (die->parent->tag != DW_TAG_structure_type
14240 && die->parent->tag != DW_TAG_union_type
14241 && die->parent->tag != DW_TAG_class_type)
14244 fieldname = dwarf2_name (die, cu);
14245 type_name = dwarf2_name (die->parent, cu);
14246 if (fieldname == NULL || type_name == NULL)
14249 len = strlen (fieldname);
14250 return (strncmp (fieldname, type_name, len) == 0
14251 && (type_name[len] == '\0' || type_name[len] == '<'));
14254 /* Add a member function to the proper fieldlist. */
14257 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14258 struct type *type, struct dwarf2_cu *cu)
14260 struct objfile *objfile = cu->objfile;
14261 struct attribute *attr;
14262 struct fnfieldlist *flp;
14264 struct fn_field *fnp;
14265 const char *fieldname;
14266 struct nextfnfield *new_fnfield;
14267 struct type *this_type;
14268 enum dwarf_access_attribute accessibility;
14270 if (cu->language == language_ada)
14271 error (_("unexpected member function in Ada type"));
14273 /* Get name of member function. */
14274 fieldname = dwarf2_name (die, cu);
14275 if (fieldname == NULL)
14278 /* Look up member function name in fieldlist. */
14279 for (i = 0; i < fip->nfnfields; i++)
14281 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14285 /* Create new list element if necessary. */
14286 if (i < fip->nfnfields)
14287 flp = &fip->fnfieldlists[i];
14290 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
14292 fip->fnfieldlists = (struct fnfieldlist *)
14293 xrealloc (fip->fnfieldlists,
14294 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
14295 * sizeof (struct fnfieldlist));
14296 if (fip->nfnfields == 0)
14297 make_cleanup (free_current_contents, &fip->fnfieldlists);
14299 flp = &fip->fnfieldlists[fip->nfnfields];
14300 flp->name = fieldname;
14303 i = fip->nfnfields++;
14306 /* Create a new member function field and chain it to the field list
14308 new_fnfield = XNEW (struct nextfnfield);
14309 make_cleanup (xfree, new_fnfield);
14310 memset (new_fnfield, 0, sizeof (struct nextfnfield));
14311 new_fnfield->next = flp->head;
14312 flp->head = new_fnfield;
14315 /* Fill in the member function field info. */
14316 fnp = &new_fnfield->fnfield;
14318 /* Delay processing of the physname until later. */
14319 if (cu->language == language_cplus)
14321 add_to_method_list (type, i, flp->length - 1, fieldname,
14326 const char *physname = dwarf2_physname (fieldname, die, cu);
14327 fnp->physname = physname ? physname : "";
14330 fnp->type = alloc_type (objfile);
14331 this_type = read_type_die (die, cu);
14332 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14334 int nparams = TYPE_NFIELDS (this_type);
14336 /* TYPE is the domain of this method, and THIS_TYPE is the type
14337 of the method itself (TYPE_CODE_METHOD). */
14338 smash_to_method_type (fnp->type, type,
14339 TYPE_TARGET_TYPE (this_type),
14340 TYPE_FIELDS (this_type),
14341 TYPE_NFIELDS (this_type),
14342 TYPE_VARARGS (this_type));
14344 /* Handle static member functions.
14345 Dwarf2 has no clean way to discern C++ static and non-static
14346 member functions. G++ helps GDB by marking the first
14347 parameter for non-static member functions (which is the this
14348 pointer) as artificial. We obtain this information from
14349 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14350 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14351 fnp->voffset = VOFFSET_STATIC;
14354 complaint (&symfile_complaints, _("member function type missing for '%s'"),
14355 dwarf2_full_name (fieldname, die, cu));
14357 /* Get fcontext from DW_AT_containing_type if present. */
14358 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14359 fnp->fcontext = die_containing_type (die, cu);
14361 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14362 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14364 /* Get accessibility. */
14365 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14367 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14369 accessibility = dwarf2_default_access_attribute (die, cu);
14370 switch (accessibility)
14372 case DW_ACCESS_private:
14373 fnp->is_private = 1;
14375 case DW_ACCESS_protected:
14376 fnp->is_protected = 1;
14380 /* Check for artificial methods. */
14381 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14382 if (attr && DW_UNSND (attr) != 0)
14383 fnp->is_artificial = 1;
14385 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14387 /* Get index in virtual function table if it is a virtual member
14388 function. For older versions of GCC, this is an offset in the
14389 appropriate virtual table, as specified by DW_AT_containing_type.
14390 For everyone else, it is an expression to be evaluated relative
14391 to the object address. */
14393 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14396 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
14398 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14400 /* Old-style GCC. */
14401 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14403 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14404 || (DW_BLOCK (attr)->size > 1
14405 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14406 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14408 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14409 if ((fnp->voffset % cu->header.addr_size) != 0)
14410 dwarf2_complex_location_expr_complaint ();
14412 fnp->voffset /= cu->header.addr_size;
14416 dwarf2_complex_location_expr_complaint ();
14418 if (!fnp->fcontext)
14420 /* If there is no `this' field and no DW_AT_containing_type,
14421 we cannot actually find a base class context for the
14423 if (TYPE_NFIELDS (this_type) == 0
14424 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14426 complaint (&symfile_complaints,
14427 _("cannot determine context for virtual member "
14428 "function \"%s\" (offset %d)"),
14429 fieldname, to_underlying (die->sect_off));
14434 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14438 else if (attr_form_is_section_offset (attr))
14440 dwarf2_complex_location_expr_complaint ();
14444 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14450 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14451 if (attr && DW_UNSND (attr))
14453 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14454 complaint (&symfile_complaints,
14455 _("Member function \"%s\" (offset %d) is virtual "
14456 "but the vtable offset is not specified"),
14457 fieldname, to_underlying (die->sect_off));
14458 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14459 TYPE_CPLUS_DYNAMIC (type) = 1;
14464 /* Create the vector of member function fields, and attach it to the type. */
14467 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14468 struct dwarf2_cu *cu)
14470 struct fnfieldlist *flp;
14473 if (cu->language == language_ada)
14474 error (_("unexpected member functions in Ada type"));
14476 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14477 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14478 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
14480 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
14482 struct nextfnfield *nfp = flp->head;
14483 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14486 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
14487 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
14488 fn_flp->fn_fields = (struct fn_field *)
14489 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
14490 for (k = flp->length; (k--, nfp); nfp = nfp->next)
14491 fn_flp->fn_fields[k] = nfp->fnfield;
14494 TYPE_NFN_FIELDS (type) = fip->nfnfields;
14497 /* Returns non-zero if NAME is the name of a vtable member in CU's
14498 language, zero otherwise. */
14500 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14502 static const char vptr[] = "_vptr";
14504 /* Look for the C++ form of the vtable. */
14505 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14511 /* GCC outputs unnamed structures that are really pointers to member
14512 functions, with the ABI-specified layout. If TYPE describes
14513 such a structure, smash it into a member function type.
14515 GCC shouldn't do this; it should just output pointer to member DIEs.
14516 This is GCC PR debug/28767. */
14519 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14521 struct type *pfn_type, *self_type, *new_type;
14523 /* Check for a structure with no name and two children. */
14524 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14527 /* Check for __pfn and __delta members. */
14528 if (TYPE_FIELD_NAME (type, 0) == NULL
14529 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14530 || TYPE_FIELD_NAME (type, 1) == NULL
14531 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14534 /* Find the type of the method. */
14535 pfn_type = TYPE_FIELD_TYPE (type, 0);
14536 if (pfn_type == NULL
14537 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14538 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14541 /* Look for the "this" argument. */
14542 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14543 if (TYPE_NFIELDS (pfn_type) == 0
14544 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14545 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14548 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14549 new_type = alloc_type (objfile);
14550 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14551 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14552 TYPE_VARARGS (pfn_type));
14553 smash_to_methodptr_type (type, new_type);
14557 /* Called when we find the DIE that starts a structure or union scope
14558 (definition) to create a type for the structure or union. Fill in
14559 the type's name and general properties; the members will not be
14560 processed until process_structure_scope. A symbol table entry for
14561 the type will also not be done until process_structure_scope (assuming
14562 the type has a name).
14564 NOTE: we need to call these functions regardless of whether or not the
14565 DIE has a DW_AT_name attribute, since it might be an anonymous
14566 structure or union. This gets the type entered into our set of
14567 user defined types. */
14569 static struct type *
14570 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14572 struct objfile *objfile = cu->objfile;
14574 struct attribute *attr;
14577 /* If the definition of this type lives in .debug_types, read that type.
14578 Don't follow DW_AT_specification though, that will take us back up
14579 the chain and we want to go down. */
14580 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14583 type = get_DW_AT_signature_type (die, attr, cu);
14585 /* The type's CU may not be the same as CU.
14586 Ensure TYPE is recorded with CU in die_type_hash. */
14587 return set_die_type (die, type, cu);
14590 type = alloc_type (objfile);
14591 INIT_CPLUS_SPECIFIC (type);
14593 name = dwarf2_name (die, cu);
14596 if (cu->language == language_cplus
14597 || cu->language == language_d
14598 || cu->language == language_rust)
14600 const char *full_name = dwarf2_full_name (name, die, cu);
14602 /* dwarf2_full_name might have already finished building the DIE's
14603 type. If so, there is no need to continue. */
14604 if (get_die_type (die, cu) != NULL)
14605 return get_die_type (die, cu);
14607 TYPE_TAG_NAME (type) = full_name;
14608 if (die->tag == DW_TAG_structure_type
14609 || die->tag == DW_TAG_class_type)
14610 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14614 /* The name is already allocated along with this objfile, so
14615 we don't need to duplicate it for the type. */
14616 TYPE_TAG_NAME (type) = name;
14617 if (die->tag == DW_TAG_class_type)
14618 TYPE_NAME (type) = TYPE_TAG_NAME (type);
14622 if (die->tag == DW_TAG_structure_type)
14624 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14626 else if (die->tag == DW_TAG_union_type)
14628 TYPE_CODE (type) = TYPE_CODE_UNION;
14632 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14635 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14636 TYPE_DECLARED_CLASS (type) = 1;
14638 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14641 if (attr_form_is_constant (attr))
14642 TYPE_LENGTH (type) = DW_UNSND (attr);
14645 /* For the moment, dynamic type sizes are not supported
14646 by GDB's struct type. The actual size is determined
14647 on-demand when resolving the type of a given object,
14648 so set the type's length to zero for now. Otherwise,
14649 we record an expression as the length, and that expression
14650 could lead to a very large value, which could eventually
14651 lead to us trying to allocate that much memory when creating
14652 a value of that type. */
14653 TYPE_LENGTH (type) = 0;
14658 TYPE_LENGTH (type) = 0;
14661 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14663 /* ICC<14 does not output the required DW_AT_declaration on
14664 incomplete types, but gives them a size of zero. */
14665 TYPE_STUB (type) = 1;
14668 TYPE_STUB_SUPPORTED (type) = 1;
14670 if (die_is_declaration (die, cu))
14671 TYPE_STUB (type) = 1;
14672 else if (attr == NULL && die->child == NULL
14673 && producer_is_realview (cu->producer))
14674 /* RealView does not output the required DW_AT_declaration
14675 on incomplete types. */
14676 TYPE_STUB (type) = 1;
14678 /* We need to add the type field to the die immediately so we don't
14679 infinitely recurse when dealing with pointers to the structure
14680 type within the structure itself. */
14681 set_die_type (die, type, cu);
14683 /* set_die_type should be already done. */
14684 set_descriptive_type (type, die, cu);
14689 /* Finish creating a structure or union type, including filling in
14690 its members and creating a symbol for it. */
14693 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14695 struct objfile *objfile = cu->objfile;
14696 struct die_info *child_die;
14699 type = get_die_type (die, cu);
14701 type = read_structure_type (die, cu);
14703 if (die->child != NULL && ! die_is_declaration (die, cu))
14705 struct field_info fi;
14706 std::vector<struct symbol *> template_args;
14707 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
14709 memset (&fi, 0, sizeof (struct field_info));
14711 child_die = die->child;
14713 while (child_die && child_die->tag)
14715 if (child_die->tag == DW_TAG_member
14716 || child_die->tag == DW_TAG_variable)
14718 /* NOTE: carlton/2002-11-05: A C++ static data member
14719 should be a DW_TAG_member that is a declaration, but
14720 all versions of G++ as of this writing (so through at
14721 least 3.2.1) incorrectly generate DW_TAG_variable
14722 tags for them instead. */
14723 dwarf2_add_field (&fi, child_die, cu);
14725 else if (child_die->tag == DW_TAG_subprogram)
14727 /* Rust doesn't have member functions in the C++ sense.
14728 However, it does emit ordinary functions as children
14729 of a struct DIE. */
14730 if (cu->language == language_rust)
14731 read_func_scope (child_die, cu);
14734 /* C++ member function. */
14735 dwarf2_add_member_fn (&fi, child_die, type, cu);
14738 else if (child_die->tag == DW_TAG_inheritance)
14740 /* C++ base class field. */
14741 dwarf2_add_field (&fi, child_die, cu);
14743 else if (type_can_define_types (child_die))
14744 dwarf2_add_type_defn (&fi, child_die, cu);
14745 else if (child_die->tag == DW_TAG_template_type_param
14746 || child_die->tag == DW_TAG_template_value_param)
14748 struct symbol *arg = new_symbol (child_die, NULL, cu);
14751 template_args.push_back (arg);
14754 child_die = sibling_die (child_die);
14757 /* Attach template arguments to type. */
14758 if (!template_args.empty ())
14760 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14761 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14762 TYPE_TEMPLATE_ARGUMENTS (type)
14763 = XOBNEWVEC (&objfile->objfile_obstack,
14765 TYPE_N_TEMPLATE_ARGUMENTS (type));
14766 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14767 template_args.data (),
14768 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14769 * sizeof (struct symbol *)));
14772 /* Attach fields and member functions to the type. */
14774 dwarf2_attach_fields_to_type (&fi, type, cu);
14777 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14779 /* Get the type which refers to the base class (possibly this
14780 class itself) which contains the vtable pointer for the current
14781 class from the DW_AT_containing_type attribute. This use of
14782 DW_AT_containing_type is a GNU extension. */
14784 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14786 struct type *t = die_containing_type (die, cu);
14788 set_type_vptr_basetype (type, t);
14793 /* Our own class provides vtbl ptr. */
14794 for (i = TYPE_NFIELDS (t) - 1;
14795 i >= TYPE_N_BASECLASSES (t);
14798 const char *fieldname = TYPE_FIELD_NAME (t, i);
14800 if (is_vtable_name (fieldname, cu))
14802 set_type_vptr_fieldno (type, i);
14807 /* Complain if virtual function table field not found. */
14808 if (i < TYPE_N_BASECLASSES (t))
14809 complaint (&symfile_complaints,
14810 _("virtual function table pointer "
14811 "not found when defining class '%s'"),
14812 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
14817 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14820 else if (cu->producer
14821 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14823 /* The IBM XLC compiler does not provide direct indication
14824 of the containing type, but the vtable pointer is
14825 always named __vfp. */
14829 for (i = TYPE_NFIELDS (type) - 1;
14830 i >= TYPE_N_BASECLASSES (type);
14833 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
14835 set_type_vptr_fieldno (type, i);
14836 set_type_vptr_basetype (type, type);
14843 /* Copy fi.typedef_field_list linked list elements content into the
14844 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14845 if (fi.typedef_field_list)
14847 int i = fi.typedef_field_list_count;
14849 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14850 TYPE_TYPEDEF_FIELD_ARRAY (type)
14851 = ((struct decl_field *)
14852 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
14853 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
14855 /* Reverse the list order to keep the debug info elements order. */
14858 struct decl_field *dest, *src;
14860 dest = &TYPE_TYPEDEF_FIELD (type, i);
14861 src = &fi.typedef_field_list->field;
14862 fi.typedef_field_list = fi.typedef_field_list->next;
14867 /* Copy fi.nested_types_list linked list elements content into the
14868 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14869 if (fi.nested_types_list != NULL && cu->language != language_ada)
14871 int i = fi.nested_types_list_count;
14873 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14874 TYPE_NESTED_TYPES_ARRAY (type)
14875 = ((struct decl_field *)
14876 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
14877 TYPE_NESTED_TYPES_COUNT (type) = i;
14879 /* Reverse the list order to keep the debug info elements order. */
14882 struct decl_field *dest, *src;
14884 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
14885 src = &fi.nested_types_list->field;
14886 fi.nested_types_list = fi.nested_types_list->next;
14891 do_cleanups (back_to);
14894 quirk_gcc_member_function_pointer (type, objfile);
14896 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14897 snapshots) has been known to create a die giving a declaration
14898 for a class that has, as a child, a die giving a definition for a
14899 nested class. So we have to process our children even if the
14900 current die is a declaration. Normally, of course, a declaration
14901 won't have any children at all. */
14903 child_die = die->child;
14905 while (child_die != NULL && child_die->tag)
14907 if (child_die->tag == DW_TAG_member
14908 || child_die->tag == DW_TAG_variable
14909 || child_die->tag == DW_TAG_inheritance
14910 || child_die->tag == DW_TAG_template_value_param
14911 || child_die->tag == DW_TAG_template_type_param)
14916 process_die (child_die, cu);
14918 child_die = sibling_die (child_die);
14921 /* Do not consider external references. According to the DWARF standard,
14922 these DIEs are identified by the fact that they have no byte_size
14923 attribute, and a declaration attribute. */
14924 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14925 || !die_is_declaration (die, cu))
14926 new_symbol (die, type, cu);
14929 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
14930 update TYPE using some information only available in DIE's children. */
14933 update_enumeration_type_from_children (struct die_info *die,
14935 struct dwarf2_cu *cu)
14937 struct die_info *child_die;
14938 int unsigned_enum = 1;
14942 auto_obstack obstack;
14944 for (child_die = die->child;
14945 child_die != NULL && child_die->tag;
14946 child_die = sibling_die (child_die))
14948 struct attribute *attr;
14950 const gdb_byte *bytes;
14951 struct dwarf2_locexpr_baton *baton;
14954 if (child_die->tag != DW_TAG_enumerator)
14957 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
14961 name = dwarf2_name (child_die, cu);
14963 name = "<anonymous enumerator>";
14965 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
14966 &value, &bytes, &baton);
14972 else if ((mask & value) != 0)
14977 /* If we already know that the enum type is neither unsigned, nor
14978 a flag type, no need to look at the rest of the enumerates. */
14979 if (!unsigned_enum && !flag_enum)
14984 TYPE_UNSIGNED (type) = 1;
14986 TYPE_FLAG_ENUM (type) = 1;
14989 /* Given a DW_AT_enumeration_type die, set its type. We do not
14990 complete the type's fields yet, or create any symbols. */
14992 static struct type *
14993 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
14995 struct objfile *objfile = cu->objfile;
14997 struct attribute *attr;
15000 /* If the definition of this type lives in .debug_types, read that type.
15001 Don't follow DW_AT_specification though, that will take us back up
15002 the chain and we want to go down. */
15003 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15006 type = get_DW_AT_signature_type (die, attr, cu);
15008 /* The type's CU may not be the same as CU.
15009 Ensure TYPE is recorded with CU in die_type_hash. */
15010 return set_die_type (die, type, cu);
15013 type = alloc_type (objfile);
15015 TYPE_CODE (type) = TYPE_CODE_ENUM;
15016 name = dwarf2_full_name (NULL, die, cu);
15018 TYPE_TAG_NAME (type) = name;
15020 attr = dwarf2_attr (die, DW_AT_type, cu);
15023 struct type *underlying_type = die_type (die, cu);
15025 TYPE_TARGET_TYPE (type) = underlying_type;
15028 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15031 TYPE_LENGTH (type) = DW_UNSND (attr);
15035 TYPE_LENGTH (type) = 0;
15038 /* The enumeration DIE can be incomplete. In Ada, any type can be
15039 declared as private in the package spec, and then defined only
15040 inside the package body. Such types are known as Taft Amendment
15041 Types. When another package uses such a type, an incomplete DIE
15042 may be generated by the compiler. */
15043 if (die_is_declaration (die, cu))
15044 TYPE_STUB (type) = 1;
15046 /* Finish the creation of this type by using the enum's children.
15047 We must call this even when the underlying type has been provided
15048 so that we can determine if we're looking at a "flag" enum. */
15049 update_enumeration_type_from_children (die, type, cu);
15051 /* If this type has an underlying type that is not a stub, then we
15052 may use its attributes. We always use the "unsigned" attribute
15053 in this situation, because ordinarily we guess whether the type
15054 is unsigned -- but the guess can be wrong and the underlying type
15055 can tell us the reality. However, we defer to a local size
15056 attribute if one exists, because this lets the compiler override
15057 the underlying type if needed. */
15058 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15060 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15061 if (TYPE_LENGTH (type) == 0)
15062 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15065 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15067 return set_die_type (die, type, cu);
15070 /* Given a pointer to a die which begins an enumeration, process all
15071 the dies that define the members of the enumeration, and create the
15072 symbol for the enumeration type.
15074 NOTE: We reverse the order of the element list. */
15077 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15079 struct type *this_type;
15081 this_type = get_die_type (die, cu);
15082 if (this_type == NULL)
15083 this_type = read_enumeration_type (die, cu);
15085 if (die->child != NULL)
15087 struct die_info *child_die;
15088 struct symbol *sym;
15089 struct field *fields = NULL;
15090 int num_fields = 0;
15093 child_die = die->child;
15094 while (child_die && child_die->tag)
15096 if (child_die->tag != DW_TAG_enumerator)
15098 process_die (child_die, cu);
15102 name = dwarf2_name (child_die, cu);
15105 sym = new_symbol (child_die, this_type, cu);
15107 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
15109 fields = (struct field *)
15111 (num_fields + DW_FIELD_ALLOC_CHUNK)
15112 * sizeof (struct field));
15115 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
15116 FIELD_TYPE (fields[num_fields]) = NULL;
15117 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
15118 FIELD_BITSIZE (fields[num_fields]) = 0;
15124 child_die = sibling_die (child_die);
15129 TYPE_NFIELDS (this_type) = num_fields;
15130 TYPE_FIELDS (this_type) = (struct field *)
15131 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
15132 memcpy (TYPE_FIELDS (this_type), fields,
15133 sizeof (struct field) * num_fields);
15138 /* If we are reading an enum from a .debug_types unit, and the enum
15139 is a declaration, and the enum is not the signatured type in the
15140 unit, then we do not want to add a symbol for it. Adding a
15141 symbol would in some cases obscure the true definition of the
15142 enum, giving users an incomplete type when the definition is
15143 actually available. Note that we do not want to do this for all
15144 enums which are just declarations, because C++0x allows forward
15145 enum declarations. */
15146 if (cu->per_cu->is_debug_types
15147 && die_is_declaration (die, cu))
15149 struct signatured_type *sig_type;
15151 sig_type = (struct signatured_type *) cu->per_cu;
15152 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15153 if (sig_type->type_offset_in_section != die->sect_off)
15157 new_symbol (die, this_type, cu);
15160 /* Extract all information from a DW_TAG_array_type DIE and put it in
15161 the DIE's type field. For now, this only handles one dimensional
15164 static struct type *
15165 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15167 struct objfile *objfile = cu->objfile;
15168 struct die_info *child_die;
15170 struct type *element_type, *range_type, *index_type;
15171 struct attribute *attr;
15173 unsigned int bit_stride = 0;
15175 element_type = die_type (die, cu);
15177 /* The die_type call above may have already set the type for this DIE. */
15178 type = get_die_type (die, cu);
15182 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15184 bit_stride = DW_UNSND (attr) * 8;
15186 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15188 bit_stride = DW_UNSND (attr);
15190 /* Irix 6.2 native cc creates array types without children for
15191 arrays with unspecified length. */
15192 if (die->child == NULL)
15194 index_type = objfile_type (objfile)->builtin_int;
15195 range_type = create_static_range_type (NULL, index_type, 0, -1);
15196 type = create_array_type_with_stride (NULL, element_type, range_type,
15198 return set_die_type (die, type, cu);
15201 std::vector<struct type *> range_types;
15202 child_die = die->child;
15203 while (child_die && child_die->tag)
15205 if (child_die->tag == DW_TAG_subrange_type)
15207 struct type *child_type = read_type_die (child_die, cu);
15209 if (child_type != NULL)
15211 /* The range type was succesfully read. Save it for the
15212 array type creation. */
15213 range_types.push_back (child_type);
15216 child_die = sibling_die (child_die);
15219 /* Dwarf2 dimensions are output from left to right, create the
15220 necessary array types in backwards order. */
15222 type = element_type;
15224 if (read_array_order (die, cu) == DW_ORD_col_major)
15228 while (i < range_types.size ())
15229 type = create_array_type_with_stride (NULL, type, range_types[i++],
15234 size_t ndim = range_types.size ();
15236 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15240 /* Understand Dwarf2 support for vector types (like they occur on
15241 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15242 array type. This is not part of the Dwarf2/3 standard yet, but a
15243 custom vendor extension. The main difference between a regular
15244 array and the vector variant is that vectors are passed by value
15246 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15248 make_vector_type (type);
15250 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15251 implementation may choose to implement triple vectors using this
15253 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15256 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15257 TYPE_LENGTH (type) = DW_UNSND (attr);
15259 complaint (&symfile_complaints,
15260 _("DW_AT_byte_size for array type smaller "
15261 "than the total size of elements"));
15264 name = dwarf2_name (die, cu);
15266 TYPE_NAME (type) = name;
15268 /* Install the type in the die. */
15269 set_die_type (die, type, cu);
15271 /* set_die_type should be already done. */
15272 set_descriptive_type (type, die, cu);
15277 static enum dwarf_array_dim_ordering
15278 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15280 struct attribute *attr;
15282 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15285 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15287 /* GNU F77 is a special case, as at 08/2004 array type info is the
15288 opposite order to the dwarf2 specification, but data is still
15289 laid out as per normal fortran.
15291 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15292 version checking. */
15294 if (cu->language == language_fortran
15295 && cu->producer && strstr (cu->producer, "GNU F77"))
15297 return DW_ORD_row_major;
15300 switch (cu->language_defn->la_array_ordering)
15302 case array_column_major:
15303 return DW_ORD_col_major;
15304 case array_row_major:
15306 return DW_ORD_row_major;
15310 /* Extract all information from a DW_TAG_set_type DIE and put it in
15311 the DIE's type field. */
15313 static struct type *
15314 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15316 struct type *domain_type, *set_type;
15317 struct attribute *attr;
15319 domain_type = die_type (die, cu);
15321 /* The die_type call above may have already set the type for this DIE. */
15322 set_type = get_die_type (die, cu);
15326 set_type = create_set_type (NULL, domain_type);
15328 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15330 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15332 return set_die_type (die, set_type, cu);
15335 /* A helper for read_common_block that creates a locexpr baton.
15336 SYM is the symbol which we are marking as computed.
15337 COMMON_DIE is the DIE for the common block.
15338 COMMON_LOC is the location expression attribute for the common
15340 MEMBER_LOC is the location expression attribute for the particular
15341 member of the common block that we are processing.
15342 CU is the CU from which the above come. */
15345 mark_common_block_symbol_computed (struct symbol *sym,
15346 struct die_info *common_die,
15347 struct attribute *common_loc,
15348 struct attribute *member_loc,
15349 struct dwarf2_cu *cu)
15351 struct objfile *objfile = dwarf2_per_objfile->objfile;
15352 struct dwarf2_locexpr_baton *baton;
15354 unsigned int cu_off;
15355 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15356 LONGEST offset = 0;
15358 gdb_assert (common_loc && member_loc);
15359 gdb_assert (attr_form_is_block (common_loc));
15360 gdb_assert (attr_form_is_block (member_loc)
15361 || attr_form_is_constant (member_loc));
15363 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15364 baton->per_cu = cu->per_cu;
15365 gdb_assert (baton->per_cu);
15367 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15369 if (attr_form_is_constant (member_loc))
15371 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15372 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15375 baton->size += DW_BLOCK (member_loc)->size;
15377 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15380 *ptr++ = DW_OP_call4;
15381 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15382 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15385 if (attr_form_is_constant (member_loc))
15387 *ptr++ = DW_OP_addr;
15388 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15389 ptr += cu->header.addr_size;
15393 /* We have to copy the data here, because DW_OP_call4 will only
15394 use a DW_AT_location attribute. */
15395 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15396 ptr += DW_BLOCK (member_loc)->size;
15399 *ptr++ = DW_OP_plus;
15400 gdb_assert (ptr - baton->data == baton->size);
15402 SYMBOL_LOCATION_BATON (sym) = baton;
15403 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15406 /* Create appropriate locally-scoped variables for all the
15407 DW_TAG_common_block entries. Also create a struct common_block
15408 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15409 is used to sepate the common blocks name namespace from regular
15413 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15415 struct attribute *attr;
15417 attr = dwarf2_attr (die, DW_AT_location, cu);
15420 /* Support the .debug_loc offsets. */
15421 if (attr_form_is_block (attr))
15425 else if (attr_form_is_section_offset (attr))
15427 dwarf2_complex_location_expr_complaint ();
15432 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15433 "common block member");
15438 if (die->child != NULL)
15440 struct objfile *objfile = cu->objfile;
15441 struct die_info *child_die;
15442 size_t n_entries = 0, size;
15443 struct common_block *common_block;
15444 struct symbol *sym;
15446 for (child_die = die->child;
15447 child_die && child_die->tag;
15448 child_die = sibling_die (child_die))
15451 size = (sizeof (struct common_block)
15452 + (n_entries - 1) * sizeof (struct symbol *));
15454 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15456 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15457 common_block->n_entries = 0;
15459 for (child_die = die->child;
15460 child_die && child_die->tag;
15461 child_die = sibling_die (child_die))
15463 /* Create the symbol in the DW_TAG_common_block block in the current
15465 sym = new_symbol (child_die, NULL, cu);
15468 struct attribute *member_loc;
15470 common_block->contents[common_block->n_entries++] = sym;
15472 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15476 /* GDB has handled this for a long time, but it is
15477 not specified by DWARF. It seems to have been
15478 emitted by gfortran at least as recently as:
15479 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15480 complaint (&symfile_complaints,
15481 _("Variable in common block has "
15482 "DW_AT_data_member_location "
15483 "- DIE at 0x%x [in module %s]"),
15484 to_underlying (child_die->sect_off),
15485 objfile_name (cu->objfile));
15487 if (attr_form_is_section_offset (member_loc))
15488 dwarf2_complex_location_expr_complaint ();
15489 else if (attr_form_is_constant (member_loc)
15490 || attr_form_is_block (member_loc))
15493 mark_common_block_symbol_computed (sym, die, attr,
15497 dwarf2_complex_location_expr_complaint ();
15502 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15503 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15507 /* Create a type for a C++ namespace. */
15509 static struct type *
15510 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15512 struct objfile *objfile = cu->objfile;
15513 const char *previous_prefix, *name;
15517 /* For extensions, reuse the type of the original namespace. */
15518 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15520 struct die_info *ext_die;
15521 struct dwarf2_cu *ext_cu = cu;
15523 ext_die = dwarf2_extension (die, &ext_cu);
15524 type = read_type_die (ext_die, ext_cu);
15526 /* EXT_CU may not be the same as CU.
15527 Ensure TYPE is recorded with CU in die_type_hash. */
15528 return set_die_type (die, type, cu);
15531 name = namespace_name (die, &is_anonymous, cu);
15533 /* Now build the name of the current namespace. */
15535 previous_prefix = determine_prefix (die, cu);
15536 if (previous_prefix[0] != '\0')
15537 name = typename_concat (&objfile->objfile_obstack,
15538 previous_prefix, name, 0, cu);
15540 /* Create the type. */
15541 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15542 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15544 return set_die_type (die, type, cu);
15547 /* Read a namespace scope. */
15550 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15552 struct objfile *objfile = cu->objfile;
15555 /* Add a symbol associated to this if we haven't seen the namespace
15556 before. Also, add a using directive if it's an anonymous
15559 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15563 type = read_type_die (die, cu);
15564 new_symbol (die, type, cu);
15566 namespace_name (die, &is_anonymous, cu);
15569 const char *previous_prefix = determine_prefix (die, cu);
15571 std::vector<const char *> excludes;
15572 add_using_directive (using_directives (cu->language),
15573 previous_prefix, TYPE_NAME (type), NULL,
15574 NULL, excludes, 0, &objfile->objfile_obstack);
15578 if (die->child != NULL)
15580 struct die_info *child_die = die->child;
15582 while (child_die && child_die->tag)
15584 process_die (child_die, cu);
15585 child_die = sibling_die (child_die);
15590 /* Read a Fortran module as type. This DIE can be only a declaration used for
15591 imported module. Still we need that type as local Fortran "use ... only"
15592 declaration imports depend on the created type in determine_prefix. */
15594 static struct type *
15595 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15597 struct objfile *objfile = cu->objfile;
15598 const char *module_name;
15601 module_name = dwarf2_name (die, cu);
15603 complaint (&symfile_complaints,
15604 _("DW_TAG_module has no name, offset 0x%x"),
15605 to_underlying (die->sect_off));
15606 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
15608 /* determine_prefix uses TYPE_TAG_NAME. */
15609 TYPE_TAG_NAME (type) = TYPE_NAME (type);
15611 return set_die_type (die, type, cu);
15614 /* Read a Fortran module. */
15617 read_module (struct die_info *die, struct dwarf2_cu *cu)
15619 struct die_info *child_die = die->child;
15622 type = read_type_die (die, cu);
15623 new_symbol (die, type, cu);
15625 while (child_die && child_die->tag)
15627 process_die (child_die, cu);
15628 child_die = sibling_die (child_die);
15632 /* Return the name of the namespace represented by DIE. Set
15633 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
15636 static const char *
15637 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
15639 struct die_info *current_die;
15640 const char *name = NULL;
15642 /* Loop through the extensions until we find a name. */
15644 for (current_die = die;
15645 current_die != NULL;
15646 current_die = dwarf2_extension (die, &cu))
15648 /* We don't use dwarf2_name here so that we can detect the absence
15649 of a name -> anonymous namespace. */
15650 name = dwarf2_string_attr (die, DW_AT_name, cu);
15656 /* Is it an anonymous namespace? */
15658 *is_anonymous = (name == NULL);
15660 name = CP_ANONYMOUS_NAMESPACE_STR;
15665 /* Extract all information from a DW_TAG_pointer_type DIE and add to
15666 the user defined type vector. */
15668 static struct type *
15669 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
15671 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
15672 struct comp_unit_head *cu_header = &cu->header;
15674 struct attribute *attr_byte_size;
15675 struct attribute *attr_address_class;
15676 int byte_size, addr_class;
15677 struct type *target_type;
15679 target_type = die_type (die, cu);
15681 /* The die_type call above may have already set the type for this DIE. */
15682 type = get_die_type (die, cu);
15686 type = lookup_pointer_type (target_type);
15688 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
15689 if (attr_byte_size)
15690 byte_size = DW_UNSND (attr_byte_size);
15692 byte_size = cu_header->addr_size;
15694 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
15695 if (attr_address_class)
15696 addr_class = DW_UNSND (attr_address_class);
15698 addr_class = DW_ADDR_none;
15700 /* If the pointer size or address class is different than the
15701 default, create a type variant marked as such and set the
15702 length accordingly. */
15703 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
15705 if (gdbarch_address_class_type_flags_p (gdbarch))
15709 type_flags = gdbarch_address_class_type_flags
15710 (gdbarch, byte_size, addr_class);
15711 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
15713 type = make_type_with_address_space (type, type_flags);
15715 else if (TYPE_LENGTH (type) != byte_size)
15717 complaint (&symfile_complaints,
15718 _("invalid pointer size %d"), byte_size);
15722 /* Should we also complain about unhandled address classes? */
15726 TYPE_LENGTH (type) = byte_size;
15727 return set_die_type (die, type, cu);
15730 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
15731 the user defined type vector. */
15733 static struct type *
15734 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
15737 struct type *to_type;
15738 struct type *domain;
15740 to_type = die_type (die, cu);
15741 domain = die_containing_type (die, cu);
15743 /* The calls above may have already set the type for this DIE. */
15744 type = get_die_type (die, cu);
15748 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
15749 type = lookup_methodptr_type (to_type);
15750 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
15752 struct type *new_type = alloc_type (cu->objfile);
15754 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
15755 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
15756 TYPE_VARARGS (to_type));
15757 type = lookup_methodptr_type (new_type);
15760 type = lookup_memberptr_type (to_type, domain);
15762 return set_die_type (die, type, cu);
15765 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
15766 the user defined type vector. */
15768 static struct type *
15769 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
15770 enum type_code refcode)
15772 struct comp_unit_head *cu_header = &cu->header;
15773 struct type *type, *target_type;
15774 struct attribute *attr;
15776 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
15778 target_type = die_type (die, cu);
15780 /* The die_type call above may have already set the type for this DIE. */
15781 type = get_die_type (die, cu);
15785 type = lookup_reference_type (target_type, refcode);
15786 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15789 TYPE_LENGTH (type) = DW_UNSND (attr);
15793 TYPE_LENGTH (type) = cu_header->addr_size;
15795 return set_die_type (die, type, cu);
15798 /* Add the given cv-qualifiers to the element type of the array. GCC
15799 outputs DWARF type qualifiers that apply to an array, not the
15800 element type. But GDB relies on the array element type to carry
15801 the cv-qualifiers. This mimics section 6.7.3 of the C99
15804 static struct type *
15805 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
15806 struct type *base_type, int cnst, int voltl)
15808 struct type *el_type, *inner_array;
15810 base_type = copy_type (base_type);
15811 inner_array = base_type;
15813 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
15815 TYPE_TARGET_TYPE (inner_array) =
15816 copy_type (TYPE_TARGET_TYPE (inner_array));
15817 inner_array = TYPE_TARGET_TYPE (inner_array);
15820 el_type = TYPE_TARGET_TYPE (inner_array);
15821 cnst |= TYPE_CONST (el_type);
15822 voltl |= TYPE_VOLATILE (el_type);
15823 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
15825 return set_die_type (die, base_type, cu);
15828 static struct type *
15829 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
15831 struct type *base_type, *cv_type;
15833 base_type = die_type (die, cu);
15835 /* The die_type call above may have already set the type for this DIE. */
15836 cv_type = get_die_type (die, cu);
15840 /* In case the const qualifier is applied to an array type, the element type
15841 is so qualified, not the array type (section 6.7.3 of C99). */
15842 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15843 return add_array_cv_type (die, cu, base_type, 1, 0);
15845 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
15846 return set_die_type (die, cv_type, cu);
15849 static struct type *
15850 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
15852 struct type *base_type, *cv_type;
15854 base_type = die_type (die, cu);
15856 /* The die_type call above may have already set the type for this DIE. */
15857 cv_type = get_die_type (die, cu);
15861 /* In case the volatile qualifier is applied to an array type, the
15862 element type is so qualified, not the array type (section 6.7.3
15864 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
15865 return add_array_cv_type (die, cu, base_type, 0, 1);
15867 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
15868 return set_die_type (die, cv_type, cu);
15871 /* Handle DW_TAG_restrict_type. */
15873 static struct type *
15874 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
15876 struct type *base_type, *cv_type;
15878 base_type = die_type (die, cu);
15880 /* The die_type call above may have already set the type for this DIE. */
15881 cv_type = get_die_type (die, cu);
15885 cv_type = make_restrict_type (base_type);
15886 return set_die_type (die, cv_type, cu);
15889 /* Handle DW_TAG_atomic_type. */
15891 static struct type *
15892 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
15894 struct type *base_type, *cv_type;
15896 base_type = die_type (die, cu);
15898 /* The die_type call above may have already set the type for this DIE. */
15899 cv_type = get_die_type (die, cu);
15903 cv_type = make_atomic_type (base_type);
15904 return set_die_type (die, cv_type, cu);
15907 /* Extract all information from a DW_TAG_string_type DIE and add to
15908 the user defined type vector. It isn't really a user defined type,
15909 but it behaves like one, with other DIE's using an AT_user_def_type
15910 attribute to reference it. */
15912 static struct type *
15913 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
15915 struct objfile *objfile = cu->objfile;
15916 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15917 struct type *type, *range_type, *index_type, *char_type;
15918 struct attribute *attr;
15919 unsigned int length;
15921 attr = dwarf2_attr (die, DW_AT_string_length, cu);
15924 length = DW_UNSND (attr);
15928 /* Check for the DW_AT_byte_size attribute. */
15929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15932 length = DW_UNSND (attr);
15940 index_type = objfile_type (objfile)->builtin_int;
15941 range_type = create_static_range_type (NULL, index_type, 1, length);
15942 char_type = language_string_char_type (cu->language_defn, gdbarch);
15943 type = create_string_type (NULL, char_type, range_type);
15945 return set_die_type (die, type, cu);
15948 /* Assuming that DIE corresponds to a function, returns nonzero
15949 if the function is prototyped. */
15952 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
15954 struct attribute *attr;
15956 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15957 if (attr && (DW_UNSND (attr) != 0))
15960 /* The DWARF standard implies that the DW_AT_prototyped attribute
15961 is only meaninful for C, but the concept also extends to other
15962 languages that allow unprototyped functions (Eg: Objective C).
15963 For all other languages, assume that functions are always
15965 if (cu->language != language_c
15966 && cu->language != language_objc
15967 && cu->language != language_opencl)
15970 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15971 prototyped and unprototyped functions; default to prototyped,
15972 since that is more common in modern code (and RealView warns
15973 about unprototyped functions). */
15974 if (producer_is_realview (cu->producer))
15980 /* Handle DIES due to C code like:
15984 int (*funcp)(int a, long l);
15988 ('funcp' generates a DW_TAG_subroutine_type DIE). */
15990 static struct type *
15991 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
15993 struct objfile *objfile = cu->objfile;
15994 struct type *type; /* Type that this function returns. */
15995 struct type *ftype; /* Function that returns above type. */
15996 struct attribute *attr;
15998 type = die_type (die, cu);
16000 /* The die_type call above may have already set the type for this DIE. */
16001 ftype = get_die_type (die, cu);
16005 ftype = lookup_function_type (type);
16007 if (prototyped_function_p (die, cu))
16008 TYPE_PROTOTYPED (ftype) = 1;
16010 /* Store the calling convention in the type if it's available in
16011 the subroutine die. Otherwise set the calling convention to
16012 the default value DW_CC_normal. */
16013 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16015 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
16016 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16017 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16019 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16021 /* Record whether the function returns normally to its caller or not
16022 if the DWARF producer set that information. */
16023 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16024 if (attr && (DW_UNSND (attr) != 0))
16025 TYPE_NO_RETURN (ftype) = 1;
16027 /* We need to add the subroutine type to the die immediately so
16028 we don't infinitely recurse when dealing with parameters
16029 declared as the same subroutine type. */
16030 set_die_type (die, ftype, cu);
16032 if (die->child != NULL)
16034 struct type *void_type = objfile_type (objfile)->builtin_void;
16035 struct die_info *child_die;
16036 int nparams, iparams;
16038 /* Count the number of parameters.
16039 FIXME: GDB currently ignores vararg functions, but knows about
16040 vararg member functions. */
16042 child_die = die->child;
16043 while (child_die && child_die->tag)
16045 if (child_die->tag == DW_TAG_formal_parameter)
16047 else if (child_die->tag == DW_TAG_unspecified_parameters)
16048 TYPE_VARARGS (ftype) = 1;
16049 child_die = sibling_die (child_die);
16052 /* Allocate storage for parameters and fill them in. */
16053 TYPE_NFIELDS (ftype) = nparams;
16054 TYPE_FIELDS (ftype) = (struct field *)
16055 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16057 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16058 even if we error out during the parameters reading below. */
16059 for (iparams = 0; iparams < nparams; iparams++)
16060 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16063 child_die = die->child;
16064 while (child_die && child_die->tag)
16066 if (child_die->tag == DW_TAG_formal_parameter)
16068 struct type *arg_type;
16070 /* DWARF version 2 has no clean way to discern C++
16071 static and non-static member functions. G++ helps
16072 GDB by marking the first parameter for non-static
16073 member functions (which is the this pointer) as
16074 artificial. We pass this information to
16075 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16077 DWARF version 3 added DW_AT_object_pointer, which GCC
16078 4.5 does not yet generate. */
16079 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16081 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16083 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16084 arg_type = die_type (child_die, cu);
16086 /* RealView does not mark THIS as const, which the testsuite
16087 expects. GCC marks THIS as const in method definitions,
16088 but not in the class specifications (GCC PR 43053). */
16089 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16090 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16093 struct dwarf2_cu *arg_cu = cu;
16094 const char *name = dwarf2_name (child_die, cu);
16096 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16099 /* If the compiler emits this, use it. */
16100 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16103 else if (name && strcmp (name, "this") == 0)
16104 /* Function definitions will have the argument names. */
16106 else if (name == NULL && iparams == 0)
16107 /* Declarations may not have the names, so like
16108 elsewhere in GDB, assume an artificial first
16109 argument is "this". */
16113 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16117 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16120 child_die = sibling_die (child_die);
16127 static struct type *
16128 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16130 struct objfile *objfile = cu->objfile;
16131 const char *name = NULL;
16132 struct type *this_type, *target_type;
16134 name = dwarf2_full_name (NULL, die, cu);
16135 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16136 TYPE_TARGET_STUB (this_type) = 1;
16137 set_die_type (die, this_type, cu);
16138 target_type = die_type (die, cu);
16139 if (target_type != this_type)
16140 TYPE_TARGET_TYPE (this_type) = target_type;
16143 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16144 spec and cause infinite loops in GDB. */
16145 complaint (&symfile_complaints,
16146 _("Self-referential DW_TAG_typedef "
16147 "- DIE at 0x%x [in module %s]"),
16148 to_underlying (die->sect_off), objfile_name (objfile));
16149 TYPE_TARGET_TYPE (this_type) = NULL;
16154 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16155 (which may be different from NAME) to the architecture back-end to allow
16156 it to guess the correct format if necessary. */
16158 static struct type *
16159 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16160 const char *name_hint)
16162 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16163 const struct floatformat **format;
16166 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16168 type = init_float_type (objfile, bits, name, format);
16170 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16175 /* Find a representation of a given base type and install
16176 it in the TYPE field of the die. */
16178 static struct type *
16179 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16181 struct objfile *objfile = cu->objfile;
16183 struct attribute *attr;
16184 int encoding = 0, bits = 0;
16187 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16190 encoding = DW_UNSND (attr);
16192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16195 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16197 name = dwarf2_name (die, cu);
16200 complaint (&symfile_complaints,
16201 _("DW_AT_name missing from DW_TAG_base_type"));
16206 case DW_ATE_address:
16207 /* Turn DW_ATE_address into a void * pointer. */
16208 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16209 type = init_pointer_type (objfile, bits, name, type);
16211 case DW_ATE_boolean:
16212 type = init_boolean_type (objfile, bits, 1, name);
16214 case DW_ATE_complex_float:
16215 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
16216 type = init_complex_type (objfile, name, type);
16218 case DW_ATE_decimal_float:
16219 type = init_decfloat_type (objfile, bits, name);
16222 type = dwarf2_init_float_type (objfile, bits, name, name);
16224 case DW_ATE_signed:
16225 type = init_integer_type (objfile, bits, 0, name);
16227 case DW_ATE_unsigned:
16228 if (cu->language == language_fortran
16230 && startswith (name, "character("))
16231 type = init_character_type (objfile, bits, 1, name);
16233 type = init_integer_type (objfile, bits, 1, name);
16235 case DW_ATE_signed_char:
16236 if (cu->language == language_ada || cu->language == language_m2
16237 || cu->language == language_pascal
16238 || cu->language == language_fortran)
16239 type = init_character_type (objfile, bits, 0, name);
16241 type = init_integer_type (objfile, bits, 0, name);
16243 case DW_ATE_unsigned_char:
16244 if (cu->language == language_ada || cu->language == language_m2
16245 || cu->language == language_pascal
16246 || cu->language == language_fortran
16247 || cu->language == language_rust)
16248 type = init_character_type (objfile, bits, 1, name);
16250 type = init_integer_type (objfile, bits, 1, name);
16254 gdbarch *arch = get_objfile_arch (objfile);
16257 type = builtin_type (arch)->builtin_char16;
16258 else if (bits == 32)
16259 type = builtin_type (arch)->builtin_char32;
16262 complaint (&symfile_complaints,
16263 _("unsupported DW_ATE_UTF bit size: '%d'"),
16265 type = init_integer_type (objfile, bits, 1, name);
16267 return set_die_type (die, type, cu);
16272 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
16273 dwarf_type_encoding_name (encoding));
16274 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16278 if (name && strcmp (name, "char") == 0)
16279 TYPE_NOSIGN (type) = 1;
16281 return set_die_type (die, type, cu);
16284 /* Parse dwarf attribute if it's a block, reference or constant and put the
16285 resulting value of the attribute into struct bound_prop.
16286 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16289 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16290 struct dwarf2_cu *cu, struct dynamic_prop *prop)
16292 struct dwarf2_property_baton *baton;
16293 struct obstack *obstack = &cu->objfile->objfile_obstack;
16295 if (attr == NULL || prop == NULL)
16298 if (attr_form_is_block (attr))
16300 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16301 baton->referenced_type = NULL;
16302 baton->locexpr.per_cu = cu->per_cu;
16303 baton->locexpr.size = DW_BLOCK (attr)->size;
16304 baton->locexpr.data = DW_BLOCK (attr)->data;
16305 prop->data.baton = baton;
16306 prop->kind = PROP_LOCEXPR;
16307 gdb_assert (prop->data.baton != NULL);
16309 else if (attr_form_is_ref (attr))
16311 struct dwarf2_cu *target_cu = cu;
16312 struct die_info *target_die;
16313 struct attribute *target_attr;
16315 target_die = follow_die_ref (die, attr, &target_cu);
16316 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
16317 if (target_attr == NULL)
16318 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
16320 if (target_attr == NULL)
16323 switch (target_attr->name)
16325 case DW_AT_location:
16326 if (attr_form_is_section_offset (target_attr))
16328 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16329 baton->referenced_type = die_type (target_die, target_cu);
16330 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
16331 prop->data.baton = baton;
16332 prop->kind = PROP_LOCLIST;
16333 gdb_assert (prop->data.baton != NULL);
16335 else if (attr_form_is_block (target_attr))
16337 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16338 baton->referenced_type = die_type (target_die, target_cu);
16339 baton->locexpr.per_cu = cu->per_cu;
16340 baton->locexpr.size = DW_BLOCK (target_attr)->size;
16341 baton->locexpr.data = DW_BLOCK (target_attr)->data;
16342 prop->data.baton = baton;
16343 prop->kind = PROP_LOCEXPR;
16344 gdb_assert (prop->data.baton != NULL);
16348 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16349 "dynamic property");
16353 case DW_AT_data_member_location:
16357 if (!handle_data_member_location (target_die, target_cu,
16361 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16362 baton->referenced_type = read_type_die (target_die->parent,
16364 baton->offset_info.offset = offset;
16365 baton->offset_info.type = die_type (target_die, target_cu);
16366 prop->data.baton = baton;
16367 prop->kind = PROP_ADDR_OFFSET;
16372 else if (attr_form_is_constant (attr))
16374 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
16375 prop->kind = PROP_CONST;
16379 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
16380 dwarf2_name (die, cu));
16387 /* Read the given DW_AT_subrange DIE. */
16389 static struct type *
16390 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
16392 struct type *base_type, *orig_base_type;
16393 struct type *range_type;
16394 struct attribute *attr;
16395 struct dynamic_prop low, high;
16396 int low_default_is_valid;
16397 int high_bound_is_count = 0;
16399 LONGEST negative_mask;
16401 orig_base_type = die_type (die, cu);
16402 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
16403 whereas the real type might be. So, we use ORIG_BASE_TYPE when
16404 creating the range type, but we use the result of check_typedef
16405 when examining properties of the type. */
16406 base_type = check_typedef (orig_base_type);
16408 /* The die_type call above may have already set the type for this DIE. */
16409 range_type = get_die_type (die, cu);
16413 low.kind = PROP_CONST;
16414 high.kind = PROP_CONST;
16415 high.data.const_val = 0;
16417 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
16418 omitting DW_AT_lower_bound. */
16419 switch (cu->language)
16422 case language_cplus:
16423 low.data.const_val = 0;
16424 low_default_is_valid = 1;
16426 case language_fortran:
16427 low.data.const_val = 1;
16428 low_default_is_valid = 1;
16431 case language_objc:
16432 case language_rust:
16433 low.data.const_val = 0;
16434 low_default_is_valid = (cu->header.version >= 4);
16438 case language_pascal:
16439 low.data.const_val = 1;
16440 low_default_is_valid = (cu->header.version >= 4);
16443 low.data.const_val = 0;
16444 low_default_is_valid = 0;
16448 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
16450 attr_to_dynamic_prop (attr, die, cu, &low);
16451 else if (!low_default_is_valid)
16452 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
16453 "- DIE at 0x%x [in module %s]"),
16454 to_underlying (die->sect_off), objfile_name (cu->objfile));
16456 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
16457 if (!attr_to_dynamic_prop (attr, die, cu, &high))
16459 attr = dwarf2_attr (die, DW_AT_count, cu);
16460 if (attr_to_dynamic_prop (attr, die, cu, &high))
16462 /* If bounds are constant do the final calculation here. */
16463 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
16464 high.data.const_val = low.data.const_val + high.data.const_val - 1;
16466 high_bound_is_count = 1;
16470 /* Dwarf-2 specifications explicitly allows to create subrange types
16471 without specifying a base type.
16472 In that case, the base type must be set to the type of
16473 the lower bound, upper bound or count, in that order, if any of these
16474 three attributes references an object that has a type.
16475 If no base type is found, the Dwarf-2 specifications say that
16476 a signed integer type of size equal to the size of an address should
16478 For the following C code: `extern char gdb_int [];'
16479 GCC produces an empty range DIE.
16480 FIXME: muller/2010-05-28: Possible references to object for low bound,
16481 high bound or count are not yet handled by this code. */
16482 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
16484 struct objfile *objfile = cu->objfile;
16485 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16486 int addr_size = gdbarch_addr_bit (gdbarch) /8;
16487 struct type *int_type = objfile_type (objfile)->builtin_int;
16489 /* Test "int", "long int", and "long long int" objfile types,
16490 and select the first one having a size above or equal to the
16491 architecture address size. */
16492 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16493 base_type = int_type;
16496 int_type = objfile_type (objfile)->builtin_long;
16497 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16498 base_type = int_type;
16501 int_type = objfile_type (objfile)->builtin_long_long;
16502 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
16503 base_type = int_type;
16508 /* Normally, the DWARF producers are expected to use a signed
16509 constant form (Eg. DW_FORM_sdata) to express negative bounds.
16510 But this is unfortunately not always the case, as witnessed
16511 with GCC, for instance, where the ambiguous DW_FORM_dataN form
16512 is used instead. To work around that ambiguity, we treat
16513 the bounds as signed, and thus sign-extend their values, when
16514 the base type is signed. */
16516 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
16517 if (low.kind == PROP_CONST
16518 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
16519 low.data.const_val |= negative_mask;
16520 if (high.kind == PROP_CONST
16521 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
16522 high.data.const_val |= negative_mask;
16524 range_type = create_range_type (NULL, orig_base_type, &low, &high);
16526 if (high_bound_is_count)
16527 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
16529 /* Ada expects an empty array on no boundary attributes. */
16530 if (attr == NULL && cu->language != language_ada)
16531 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
16533 name = dwarf2_name (die, cu);
16535 TYPE_NAME (range_type) = name;
16537 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16539 TYPE_LENGTH (range_type) = DW_UNSND (attr);
16541 set_die_type (die, range_type, cu);
16543 /* set_die_type should be already done. */
16544 set_descriptive_type (range_type, die, cu);
16549 static struct type *
16550 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
16554 /* For now, we only support the C meaning of an unspecified type: void. */
16556 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
16557 TYPE_NAME (type) = dwarf2_name (die, cu);
16559 return set_die_type (die, type, cu);
16562 /* Read a single die and all its descendents. Set the die's sibling
16563 field to NULL; set other fields in the die correctly, and set all
16564 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
16565 location of the info_ptr after reading all of those dies. PARENT
16566 is the parent of the die in question. */
16568 static struct die_info *
16569 read_die_and_children (const struct die_reader_specs *reader,
16570 const gdb_byte *info_ptr,
16571 const gdb_byte **new_info_ptr,
16572 struct die_info *parent)
16574 struct die_info *die;
16575 const gdb_byte *cur_ptr;
16578 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
16581 *new_info_ptr = cur_ptr;
16584 store_in_ref_table (die, reader->cu);
16587 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
16591 *new_info_ptr = cur_ptr;
16594 die->sibling = NULL;
16595 die->parent = parent;
16599 /* Read a die, all of its descendents, and all of its siblings; set
16600 all of the fields of all of the dies correctly. Arguments are as
16601 in read_die_and_children. */
16603 static struct die_info *
16604 read_die_and_siblings_1 (const struct die_reader_specs *reader,
16605 const gdb_byte *info_ptr,
16606 const gdb_byte **new_info_ptr,
16607 struct die_info *parent)
16609 struct die_info *first_die, *last_sibling;
16610 const gdb_byte *cur_ptr;
16612 cur_ptr = info_ptr;
16613 first_die = last_sibling = NULL;
16617 struct die_info *die
16618 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
16622 *new_info_ptr = cur_ptr;
16629 last_sibling->sibling = die;
16631 last_sibling = die;
16635 /* Read a die, all of its descendents, and all of its siblings; set
16636 all of the fields of all of the dies correctly. Arguments are as
16637 in read_die_and_children.
16638 This the main entry point for reading a DIE and all its children. */
16640 static struct die_info *
16641 read_die_and_siblings (const struct die_reader_specs *reader,
16642 const gdb_byte *info_ptr,
16643 const gdb_byte **new_info_ptr,
16644 struct die_info *parent)
16646 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
16647 new_info_ptr, parent);
16649 if (dwarf_die_debug)
16651 fprintf_unfiltered (gdb_stdlog,
16652 "Read die from %s@0x%x of %s:\n",
16653 get_section_name (reader->die_section),
16654 (unsigned) (info_ptr - reader->die_section->buffer),
16655 bfd_get_filename (reader->abfd));
16656 dump_die (die, dwarf_die_debug);
16662 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
16664 The caller is responsible for filling in the extra attributes
16665 and updating (*DIEP)->num_attrs.
16666 Set DIEP to point to a newly allocated die with its information,
16667 except for its child, sibling, and parent fields.
16668 Set HAS_CHILDREN to tell whether the die has children or not. */
16670 static const gdb_byte *
16671 read_full_die_1 (const struct die_reader_specs *reader,
16672 struct die_info **diep, const gdb_byte *info_ptr,
16673 int *has_children, int num_extra_attrs)
16675 unsigned int abbrev_number, bytes_read, i;
16676 struct abbrev_info *abbrev;
16677 struct die_info *die;
16678 struct dwarf2_cu *cu = reader->cu;
16679 bfd *abfd = reader->abfd;
16681 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
16682 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16683 info_ptr += bytes_read;
16684 if (!abbrev_number)
16691 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
16693 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
16695 bfd_get_filename (abfd));
16697 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
16698 die->sect_off = sect_off;
16699 die->tag = abbrev->tag;
16700 die->abbrev = abbrev_number;
16702 /* Make the result usable.
16703 The caller needs to update num_attrs after adding the extra
16705 die->num_attrs = abbrev->num_attrs;
16707 for (i = 0; i < abbrev->num_attrs; ++i)
16708 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
16712 *has_children = abbrev->has_children;
16716 /* Read a die and all its attributes.
16717 Set DIEP to point to a newly allocated die with its information,
16718 except for its child, sibling, and parent fields.
16719 Set HAS_CHILDREN to tell whether the die has children or not. */
16721 static const gdb_byte *
16722 read_full_die (const struct die_reader_specs *reader,
16723 struct die_info **diep, const gdb_byte *info_ptr,
16726 const gdb_byte *result;
16728 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
16730 if (dwarf_die_debug)
16732 fprintf_unfiltered (gdb_stdlog,
16733 "Read die from %s@0x%x of %s:\n",
16734 get_section_name (reader->die_section),
16735 (unsigned) (info_ptr - reader->die_section->buffer),
16736 bfd_get_filename (reader->abfd));
16737 dump_die (*diep, dwarf_die_debug);
16743 /* Abbreviation tables.
16745 In DWARF version 2, the description of the debugging information is
16746 stored in a separate .debug_abbrev section. Before we read any
16747 dies from a section we read in all abbreviations and install them
16748 in a hash table. */
16750 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
16752 static struct abbrev_info *
16753 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
16755 struct abbrev_info *abbrev;
16757 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
16758 memset (abbrev, 0, sizeof (struct abbrev_info));
16763 /* Add an abbreviation to the table. */
16766 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
16767 unsigned int abbrev_number,
16768 struct abbrev_info *abbrev)
16770 unsigned int hash_number;
16772 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16773 abbrev->next = abbrev_table->abbrevs[hash_number];
16774 abbrev_table->abbrevs[hash_number] = abbrev;
16777 /* Look up an abbrev in the table.
16778 Returns NULL if the abbrev is not found. */
16780 static struct abbrev_info *
16781 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
16782 unsigned int abbrev_number)
16784 unsigned int hash_number;
16785 struct abbrev_info *abbrev;
16787 hash_number = abbrev_number % ABBREV_HASH_SIZE;
16788 abbrev = abbrev_table->abbrevs[hash_number];
16792 if (abbrev->number == abbrev_number)
16794 abbrev = abbrev->next;
16799 /* Read in an abbrev table. */
16801 static struct abbrev_table *
16802 abbrev_table_read_table (struct dwarf2_section_info *section,
16803 sect_offset sect_off)
16805 struct objfile *objfile = dwarf2_per_objfile->objfile;
16806 bfd *abfd = get_section_bfd_owner (section);
16807 struct abbrev_table *abbrev_table;
16808 const gdb_byte *abbrev_ptr;
16809 struct abbrev_info *cur_abbrev;
16810 unsigned int abbrev_number, bytes_read, abbrev_name;
16811 unsigned int abbrev_form;
16812 struct attr_abbrev *cur_attrs;
16813 unsigned int allocated_attrs;
16815 abbrev_table = XNEW (struct abbrev_table);
16816 abbrev_table->sect_off = sect_off;
16817 obstack_init (&abbrev_table->abbrev_obstack);
16818 abbrev_table->abbrevs =
16819 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
16821 memset (abbrev_table->abbrevs, 0,
16822 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
16824 dwarf2_read_section (objfile, section);
16825 abbrev_ptr = section->buffer + to_underlying (sect_off);
16826 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16827 abbrev_ptr += bytes_read;
16829 allocated_attrs = ATTR_ALLOC_CHUNK;
16830 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
16832 /* Loop until we reach an abbrev number of 0. */
16833 while (abbrev_number)
16835 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
16837 /* read in abbrev header */
16838 cur_abbrev->number = abbrev_number;
16840 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16841 abbrev_ptr += bytes_read;
16842 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
16845 /* now read in declarations */
16848 LONGEST implicit_const;
16850 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16851 abbrev_ptr += bytes_read;
16852 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16853 abbrev_ptr += bytes_read;
16854 if (abbrev_form == DW_FORM_implicit_const)
16856 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
16858 abbrev_ptr += bytes_read;
16862 /* Initialize it due to a false compiler warning. */
16863 implicit_const = -1;
16866 if (abbrev_name == 0)
16869 if (cur_abbrev->num_attrs == allocated_attrs)
16871 allocated_attrs += ATTR_ALLOC_CHUNK;
16873 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
16876 cur_attrs[cur_abbrev->num_attrs].name
16877 = (enum dwarf_attribute) abbrev_name;
16878 cur_attrs[cur_abbrev->num_attrs].form
16879 = (enum dwarf_form) abbrev_form;
16880 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
16881 ++cur_abbrev->num_attrs;
16884 cur_abbrev->attrs =
16885 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
16886 cur_abbrev->num_attrs);
16887 memcpy (cur_abbrev->attrs, cur_attrs,
16888 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
16890 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
16892 /* Get next abbreviation.
16893 Under Irix6 the abbreviations for a compilation unit are not
16894 always properly terminated with an abbrev number of 0.
16895 Exit loop if we encounter an abbreviation which we have
16896 already read (which means we are about to read the abbreviations
16897 for the next compile unit) or if the end of the abbreviation
16898 table is reached. */
16899 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
16901 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
16902 abbrev_ptr += bytes_read;
16903 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
16908 return abbrev_table;
16911 /* Free the resources held by ABBREV_TABLE. */
16914 abbrev_table_free (struct abbrev_table *abbrev_table)
16916 obstack_free (&abbrev_table->abbrev_obstack, NULL);
16917 xfree (abbrev_table);
16920 /* Same as abbrev_table_free but as a cleanup.
16921 We pass in a pointer to the pointer to the table so that we can
16922 set the pointer to NULL when we're done. It also simplifies
16923 build_type_psymtabs_1. */
16926 abbrev_table_free_cleanup (void *table_ptr)
16928 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
16930 if (*abbrev_table_ptr != NULL)
16931 abbrev_table_free (*abbrev_table_ptr);
16932 *abbrev_table_ptr = NULL;
16935 /* Read the abbrev table for CU from ABBREV_SECTION. */
16938 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
16939 struct dwarf2_section_info *abbrev_section)
16942 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
16945 /* Release the memory used by the abbrev table for a compilation unit. */
16948 dwarf2_free_abbrev_table (void *ptr_to_cu)
16950 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
16952 if (cu->abbrev_table != NULL)
16953 abbrev_table_free (cu->abbrev_table);
16954 /* Set this to NULL so that we SEGV if we try to read it later,
16955 and also because free_comp_unit verifies this is NULL. */
16956 cu->abbrev_table = NULL;
16959 /* Returns nonzero if TAG represents a type that we might generate a partial
16963 is_type_tag_for_partial (int tag)
16968 /* Some types that would be reasonable to generate partial symbols for,
16969 that we don't at present. */
16970 case DW_TAG_array_type:
16971 case DW_TAG_file_type:
16972 case DW_TAG_ptr_to_member_type:
16973 case DW_TAG_set_type:
16974 case DW_TAG_string_type:
16975 case DW_TAG_subroutine_type:
16977 case DW_TAG_base_type:
16978 case DW_TAG_class_type:
16979 case DW_TAG_interface_type:
16980 case DW_TAG_enumeration_type:
16981 case DW_TAG_structure_type:
16982 case DW_TAG_subrange_type:
16983 case DW_TAG_typedef:
16984 case DW_TAG_union_type:
16991 /* Load all DIEs that are interesting for partial symbols into memory. */
16993 static struct partial_die_info *
16994 load_partial_dies (const struct die_reader_specs *reader,
16995 const gdb_byte *info_ptr, int building_psymtab)
16997 struct dwarf2_cu *cu = reader->cu;
16998 struct objfile *objfile = cu->objfile;
16999 struct partial_die_info *part_die;
17000 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17001 struct abbrev_info *abbrev;
17002 unsigned int bytes_read;
17003 unsigned int load_all = 0;
17004 int nesting_level = 1;
17009 gdb_assert (cu->per_cu != NULL);
17010 if (cu->per_cu->load_all_dies)
17014 = htab_create_alloc_ex (cu->header.length / 12,
17018 &cu->comp_unit_obstack,
17019 hashtab_obstack_allocate,
17020 dummy_obstack_deallocate);
17022 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
17026 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
17028 /* A NULL abbrev means the end of a series of children. */
17029 if (abbrev == NULL)
17031 if (--nesting_level == 0)
17033 /* PART_DIE was probably the last thing allocated on the
17034 comp_unit_obstack, so we could call obstack_free
17035 here. We don't do that because the waste is small,
17036 and will be cleaned up when we're done with this
17037 compilation unit. This way, we're also more robust
17038 against other users of the comp_unit_obstack. */
17041 info_ptr += bytes_read;
17042 last_die = parent_die;
17043 parent_die = parent_die->die_parent;
17047 /* Check for template arguments. We never save these; if
17048 they're seen, we just mark the parent, and go on our way. */
17049 if (parent_die != NULL
17050 && cu->language == language_cplus
17051 && (abbrev->tag == DW_TAG_template_type_param
17052 || abbrev->tag == DW_TAG_template_value_param))
17054 parent_die->has_template_arguments = 1;
17058 /* We don't need a partial DIE for the template argument. */
17059 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17064 /* We only recurse into c++ subprograms looking for template arguments.
17065 Skip their other children. */
17067 && cu->language == language_cplus
17068 && parent_die != NULL
17069 && parent_die->tag == DW_TAG_subprogram)
17071 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17075 /* Check whether this DIE is interesting enough to save. Normally
17076 we would not be interested in members here, but there may be
17077 later variables referencing them via DW_AT_specification (for
17078 static members). */
17080 && !is_type_tag_for_partial (abbrev->tag)
17081 && abbrev->tag != DW_TAG_constant
17082 && abbrev->tag != DW_TAG_enumerator
17083 && abbrev->tag != DW_TAG_subprogram
17084 && abbrev->tag != DW_TAG_lexical_block
17085 && abbrev->tag != DW_TAG_variable
17086 && abbrev->tag != DW_TAG_namespace
17087 && abbrev->tag != DW_TAG_module
17088 && abbrev->tag != DW_TAG_member
17089 && abbrev->tag != DW_TAG_imported_unit
17090 && abbrev->tag != DW_TAG_imported_declaration)
17092 /* Otherwise we skip to the next sibling, if any. */
17093 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17097 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
17100 /* This two-pass algorithm for processing partial symbols has a
17101 high cost in cache pressure. Thus, handle some simple cases
17102 here which cover the majority of C partial symbols. DIEs
17103 which neither have specification tags in them, nor could have
17104 specification tags elsewhere pointing at them, can simply be
17105 processed and discarded.
17107 This segment is also optional; scan_partial_symbols and
17108 add_partial_symbol will handle these DIEs if we chain
17109 them in normally. When compilers which do not emit large
17110 quantities of duplicate debug information are more common,
17111 this code can probably be removed. */
17113 /* Any complete simple types at the top level (pretty much all
17114 of them, for a language without namespaces), can be processed
17116 if (parent_die == NULL
17117 && part_die->has_specification == 0
17118 && part_die->is_declaration == 0
17119 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
17120 || part_die->tag == DW_TAG_base_type
17121 || part_die->tag == DW_TAG_subrange_type))
17123 if (building_psymtab && part_die->name != NULL)
17124 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
17125 VAR_DOMAIN, LOC_TYPEDEF,
17126 &objfile->static_psymbols,
17127 0, cu->language, objfile);
17128 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
17132 /* The exception for DW_TAG_typedef with has_children above is
17133 a workaround of GCC PR debug/47510. In the case of this complaint
17134 type_name_no_tag_or_error will error on such types later.
17136 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17137 it could not find the child DIEs referenced later, this is checked
17138 above. In correct DWARF DW_TAG_typedef should have no children. */
17140 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
17141 complaint (&symfile_complaints,
17142 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17143 "- DIE at 0x%x [in module %s]"),
17144 to_underlying (part_die->sect_off), objfile_name (objfile));
17146 /* If we're at the second level, and we're an enumerator, and
17147 our parent has no specification (meaning possibly lives in a
17148 namespace elsewhere), then we can add the partial symbol now
17149 instead of queueing it. */
17150 if (part_die->tag == DW_TAG_enumerator
17151 && parent_die != NULL
17152 && parent_die->die_parent == NULL
17153 && parent_die->tag == DW_TAG_enumeration_type
17154 && parent_die->has_specification == 0)
17156 if (part_die->name == NULL)
17157 complaint (&symfile_complaints,
17158 _("malformed enumerator DIE ignored"));
17159 else if (building_psymtab)
17160 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
17161 VAR_DOMAIN, LOC_CONST,
17162 cu->language == language_cplus
17163 ? &objfile->global_psymbols
17164 : &objfile->static_psymbols,
17165 0, cu->language, objfile);
17167 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
17171 /* We'll save this DIE so link it in. */
17172 part_die->die_parent = parent_die;
17173 part_die->die_sibling = NULL;
17174 part_die->die_child = NULL;
17176 if (last_die && last_die == parent_die)
17177 last_die->die_child = part_die;
17179 last_die->die_sibling = part_die;
17181 last_die = part_die;
17183 if (first_die == NULL)
17184 first_die = part_die;
17186 /* Maybe add the DIE to the hash table. Not all DIEs that we
17187 find interesting need to be in the hash table, because we
17188 also have the parent/sibling/child chains; only those that we
17189 might refer to by offset later during partial symbol reading.
17191 For now this means things that might have be the target of a
17192 DW_AT_specification, DW_AT_abstract_origin, or
17193 DW_AT_extension. DW_AT_extension will refer only to
17194 namespaces; DW_AT_abstract_origin refers to functions (and
17195 many things under the function DIE, but we do not recurse
17196 into function DIEs during partial symbol reading) and
17197 possibly variables as well; DW_AT_specification refers to
17198 declarations. Declarations ought to have the DW_AT_declaration
17199 flag. It happens that GCC forgets to put it in sometimes, but
17200 only for functions, not for types.
17202 Adding more things than necessary to the hash table is harmless
17203 except for the performance cost. Adding too few will result in
17204 wasted time in find_partial_die, when we reread the compilation
17205 unit with load_all_dies set. */
17208 || abbrev->tag == DW_TAG_constant
17209 || abbrev->tag == DW_TAG_subprogram
17210 || abbrev->tag == DW_TAG_variable
17211 || abbrev->tag == DW_TAG_namespace
17212 || part_die->is_declaration)
17216 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17217 to_underlying (part_die->sect_off),
17222 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
17224 /* For some DIEs we want to follow their children (if any). For C
17225 we have no reason to follow the children of structures; for other
17226 languages we have to, so that we can get at method physnames
17227 to infer fully qualified class names, for DW_AT_specification,
17228 and for C++ template arguments. For C++, we also look one level
17229 inside functions to find template arguments (if the name of the
17230 function does not already contain the template arguments).
17232 For Ada, we need to scan the children of subprograms and lexical
17233 blocks as well because Ada allows the definition of nested
17234 entities that could be interesting for the debugger, such as
17235 nested subprograms for instance. */
17236 if (last_die->has_children
17238 || last_die->tag == DW_TAG_namespace
17239 || last_die->tag == DW_TAG_module
17240 || last_die->tag == DW_TAG_enumeration_type
17241 || (cu->language == language_cplus
17242 && last_die->tag == DW_TAG_subprogram
17243 && (last_die->name == NULL
17244 || strchr (last_die->name, '<') == NULL))
17245 || (cu->language != language_c
17246 && (last_die->tag == DW_TAG_class_type
17247 || last_die->tag == DW_TAG_interface_type
17248 || last_die->tag == DW_TAG_structure_type
17249 || last_die->tag == DW_TAG_union_type))
17250 || (cu->language == language_ada
17251 && (last_die->tag == DW_TAG_subprogram
17252 || last_die->tag == DW_TAG_lexical_block))))
17255 parent_die = last_die;
17259 /* Otherwise we skip to the next sibling, if any. */
17260 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17262 /* Back to the top, do it again. */
17266 /* Read a minimal amount of information into the minimal die structure. */
17268 static const gdb_byte *
17269 read_partial_die (const struct die_reader_specs *reader,
17270 struct partial_die_info *part_die,
17271 struct abbrev_info *abbrev, unsigned int abbrev_len,
17272 const gdb_byte *info_ptr)
17274 struct dwarf2_cu *cu = reader->cu;
17275 struct objfile *objfile = cu->objfile;
17276 const gdb_byte *buffer = reader->buffer;
17278 struct attribute attr;
17279 int has_low_pc_attr = 0;
17280 int has_high_pc_attr = 0;
17281 int high_pc_relative = 0;
17283 memset (part_die, 0, sizeof (struct partial_die_info));
17285 part_die->sect_off = (sect_offset) (info_ptr - buffer);
17287 info_ptr += abbrev_len;
17289 if (abbrev == NULL)
17292 part_die->tag = abbrev->tag;
17293 part_die->has_children = abbrev->has_children;
17295 for (i = 0; i < abbrev->num_attrs; ++i)
17297 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
17299 /* Store the data if it is of an attribute we want to keep in a
17300 partial symbol table. */
17304 switch (part_die->tag)
17306 case DW_TAG_compile_unit:
17307 case DW_TAG_partial_unit:
17308 case DW_TAG_type_unit:
17309 /* Compilation units have a DW_AT_name that is a filename, not
17310 a source language identifier. */
17311 case DW_TAG_enumeration_type:
17312 case DW_TAG_enumerator:
17313 /* These tags always have simple identifiers already; no need
17314 to canonicalize them. */
17315 part_die->name = DW_STRING (&attr);
17319 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17320 &objfile->per_bfd->storage_obstack);
17324 case DW_AT_linkage_name:
17325 case DW_AT_MIPS_linkage_name:
17326 /* Note that both forms of linkage name might appear. We
17327 assume they will be the same, and we only store the last
17329 if (cu->language == language_ada)
17330 part_die->name = DW_STRING (&attr);
17331 part_die->linkage_name = DW_STRING (&attr);
17334 has_low_pc_attr = 1;
17335 part_die->lowpc = attr_value_as_address (&attr);
17337 case DW_AT_high_pc:
17338 has_high_pc_attr = 1;
17339 part_die->highpc = attr_value_as_address (&attr);
17340 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
17341 high_pc_relative = 1;
17343 case DW_AT_location:
17344 /* Support the .debug_loc offsets. */
17345 if (attr_form_is_block (&attr))
17347 part_die->d.locdesc = DW_BLOCK (&attr);
17349 else if (attr_form_is_section_offset (&attr))
17351 dwarf2_complex_location_expr_complaint ();
17355 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17356 "partial symbol information");
17359 case DW_AT_external:
17360 part_die->is_external = DW_UNSND (&attr);
17362 case DW_AT_declaration:
17363 part_die->is_declaration = DW_UNSND (&attr);
17366 part_die->has_type = 1;
17368 case DW_AT_abstract_origin:
17369 case DW_AT_specification:
17370 case DW_AT_extension:
17371 part_die->has_specification = 1;
17372 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
17373 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17374 || cu->per_cu->is_dwz);
17376 case DW_AT_sibling:
17377 /* Ignore absolute siblings, they might point outside of
17378 the current compile unit. */
17379 if (attr.form == DW_FORM_ref_addr)
17380 complaint (&symfile_complaints,
17381 _("ignoring absolute DW_AT_sibling"));
17384 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17385 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17387 if (sibling_ptr < info_ptr)
17388 complaint (&symfile_complaints,
17389 _("DW_AT_sibling points backwards"));
17390 else if (sibling_ptr > reader->buffer_end)
17391 dwarf2_section_buffer_overflow_complaint (reader->die_section);
17393 part_die->sibling = sibling_ptr;
17396 case DW_AT_byte_size:
17397 part_die->has_byte_size = 1;
17399 case DW_AT_const_value:
17400 part_die->has_const_value = 1;
17402 case DW_AT_calling_convention:
17403 /* DWARF doesn't provide a way to identify a program's source-level
17404 entry point. DW_AT_calling_convention attributes are only meant
17405 to describe functions' calling conventions.
17407 However, because it's a necessary piece of information in
17408 Fortran, and before DWARF 4 DW_CC_program was the only
17409 piece of debugging information whose definition refers to
17410 a 'main program' at all, several compilers marked Fortran
17411 main programs with DW_CC_program --- even when those
17412 functions use the standard calling conventions.
17414 Although DWARF now specifies a way to provide this
17415 information, we support this practice for backward
17417 if (DW_UNSND (&attr) == DW_CC_program
17418 && cu->language == language_fortran)
17419 part_die->main_subprogram = 1;
17422 if (DW_UNSND (&attr) == DW_INL_inlined
17423 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17424 part_die->may_be_inlined = 1;
17428 if (part_die->tag == DW_TAG_imported_unit)
17430 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
17431 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17432 || cu->per_cu->is_dwz);
17436 case DW_AT_main_subprogram:
17437 part_die->main_subprogram = DW_UNSND (&attr);
17445 if (high_pc_relative)
17446 part_die->highpc += part_die->lowpc;
17448 if (has_low_pc_attr && has_high_pc_attr)
17450 /* When using the GNU linker, .gnu.linkonce. sections are used to
17451 eliminate duplicate copies of functions and vtables and such.
17452 The linker will arbitrarily choose one and discard the others.
17453 The AT_*_pc values for such functions refer to local labels in
17454 these sections. If the section from that file was discarded, the
17455 labels are not in the output, so the relocs get a value of 0.
17456 If this is a discarded function, mark the pc bounds as invalid,
17457 so that GDB will ignore it. */
17458 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
17460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17462 complaint (&symfile_complaints,
17463 _("DW_AT_low_pc %s is zero "
17464 "for DIE at 0x%x [in module %s]"),
17465 paddress (gdbarch, part_die->lowpc),
17466 to_underlying (part_die->sect_off), objfile_name (objfile));
17468 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
17469 else if (part_die->lowpc >= part_die->highpc)
17471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17473 complaint (&symfile_complaints,
17474 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
17475 "for DIE at 0x%x [in module %s]"),
17476 paddress (gdbarch, part_die->lowpc),
17477 paddress (gdbarch, part_die->highpc),
17478 to_underlying (part_die->sect_off),
17479 objfile_name (objfile));
17482 part_die->has_pc_info = 1;
17488 /* Find a cached partial DIE at OFFSET in CU. */
17490 static struct partial_die_info *
17491 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
17493 struct partial_die_info *lookup_die = NULL;
17494 struct partial_die_info part_die;
17496 part_die.sect_off = sect_off;
17497 lookup_die = ((struct partial_die_info *)
17498 htab_find_with_hash (cu->partial_dies, &part_die,
17499 to_underlying (sect_off)));
17504 /* Find a partial DIE at OFFSET, which may or may not be in CU,
17505 except in the case of .debug_types DIEs which do not reference
17506 outside their CU (they do however referencing other types via
17507 DW_FORM_ref_sig8). */
17509 static struct partial_die_info *
17510 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
17512 struct objfile *objfile = cu->objfile;
17513 struct dwarf2_per_cu_data *per_cu = NULL;
17514 struct partial_die_info *pd = NULL;
17516 if (offset_in_dwz == cu->per_cu->is_dwz
17517 && offset_in_cu_p (&cu->header, sect_off))
17519 pd = find_partial_die_in_comp_unit (sect_off, cu);
17522 /* We missed recording what we needed.
17523 Load all dies and try again. */
17524 per_cu = cu->per_cu;
17528 /* TUs don't reference other CUs/TUs (except via type signatures). */
17529 if (cu->per_cu->is_debug_types)
17531 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
17532 " external reference to offset 0x%x [in module %s].\n"),
17533 to_underlying (cu->header.sect_off), to_underlying (sect_off),
17534 bfd_get_filename (objfile->obfd));
17536 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
17539 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
17540 load_partial_comp_unit (per_cu);
17542 per_cu->cu->last_used = 0;
17543 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17546 /* If we didn't find it, and not all dies have been loaded,
17547 load them all and try again. */
17549 if (pd == NULL && per_cu->load_all_dies == 0)
17551 per_cu->load_all_dies = 1;
17553 /* This is nasty. When we reread the DIEs, somewhere up the call chain
17554 THIS_CU->cu may already be in use. So we can't just free it and
17555 replace its DIEs with the ones we read in. Instead, we leave those
17556 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
17557 and clobber THIS_CU->cu->partial_dies with the hash table for the new
17559 load_partial_comp_unit (per_cu);
17561 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
17565 internal_error (__FILE__, __LINE__,
17566 _("could not find partial DIE 0x%x "
17567 "in cache [from module %s]\n"),
17568 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
17572 /* See if we can figure out if the class lives in a namespace. We do
17573 this by looking for a member function; its demangled name will
17574 contain namespace info, if there is any. */
17577 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
17578 struct dwarf2_cu *cu)
17580 /* NOTE: carlton/2003-10-07: Getting the info this way changes
17581 what template types look like, because the demangler
17582 frequently doesn't give the same name as the debug info. We
17583 could fix this by only using the demangled name to get the
17584 prefix (but see comment in read_structure_type). */
17586 struct partial_die_info *real_pdi;
17587 struct partial_die_info *child_pdi;
17589 /* If this DIE (this DIE's specification, if any) has a parent, then
17590 we should not do this. We'll prepend the parent's fully qualified
17591 name when we create the partial symbol. */
17593 real_pdi = struct_pdi;
17594 while (real_pdi->has_specification)
17595 real_pdi = find_partial_die (real_pdi->spec_offset,
17596 real_pdi->spec_is_dwz, cu);
17598 if (real_pdi->die_parent != NULL)
17601 for (child_pdi = struct_pdi->die_child;
17603 child_pdi = child_pdi->die_sibling)
17605 if (child_pdi->tag == DW_TAG_subprogram
17606 && child_pdi->linkage_name != NULL)
17608 char *actual_class_name
17609 = language_class_name_from_physname (cu->language_defn,
17610 child_pdi->linkage_name);
17611 if (actual_class_name != NULL)
17615 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17617 strlen (actual_class_name)));
17618 xfree (actual_class_name);
17625 /* Adjust PART_DIE before generating a symbol for it. This function
17626 may set the is_external flag or change the DIE's name. */
17629 fixup_partial_die (struct partial_die_info *part_die,
17630 struct dwarf2_cu *cu)
17632 /* Once we've fixed up a die, there's no point in doing so again.
17633 This also avoids a memory leak if we were to call
17634 guess_partial_die_structure_name multiple times. */
17635 if (part_die->fixup_called)
17638 /* If we found a reference attribute and the DIE has no name, try
17639 to find a name in the referred to DIE. */
17641 if (part_die->name == NULL && part_die->has_specification)
17643 struct partial_die_info *spec_die;
17645 spec_die = find_partial_die (part_die->spec_offset,
17646 part_die->spec_is_dwz, cu);
17648 fixup_partial_die (spec_die, cu);
17650 if (spec_die->name)
17652 part_die->name = spec_die->name;
17654 /* Copy DW_AT_external attribute if it is set. */
17655 if (spec_die->is_external)
17656 part_die->is_external = spec_die->is_external;
17660 /* Set default names for some unnamed DIEs. */
17662 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
17663 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
17665 /* If there is no parent die to provide a namespace, and there are
17666 children, see if we can determine the namespace from their linkage
17668 if (cu->language == language_cplus
17669 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17670 && part_die->die_parent == NULL
17671 && part_die->has_children
17672 && (part_die->tag == DW_TAG_class_type
17673 || part_die->tag == DW_TAG_structure_type
17674 || part_die->tag == DW_TAG_union_type))
17675 guess_partial_die_structure_name (part_die, cu);
17677 /* GCC might emit a nameless struct or union that has a linkage
17678 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17679 if (part_die->name == NULL
17680 && (part_die->tag == DW_TAG_class_type
17681 || part_die->tag == DW_TAG_interface_type
17682 || part_die->tag == DW_TAG_structure_type
17683 || part_die->tag == DW_TAG_union_type)
17684 && part_die->linkage_name != NULL)
17688 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
17693 /* Strip any leading namespaces/classes, keep only the base name.
17694 DW_AT_name for named DIEs does not contain the prefixes. */
17695 base = strrchr (demangled, ':');
17696 if (base && base > demangled && base[-1] == ':')
17703 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
17704 base, strlen (base)));
17709 part_die->fixup_called = 1;
17712 /* Read an attribute value described by an attribute form. */
17714 static const gdb_byte *
17715 read_attribute_value (const struct die_reader_specs *reader,
17716 struct attribute *attr, unsigned form,
17717 LONGEST implicit_const, const gdb_byte *info_ptr)
17719 struct dwarf2_cu *cu = reader->cu;
17720 struct objfile *objfile = cu->objfile;
17721 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17722 bfd *abfd = reader->abfd;
17723 struct comp_unit_head *cu_header = &cu->header;
17724 unsigned int bytes_read;
17725 struct dwarf_block *blk;
17727 attr->form = (enum dwarf_form) form;
17730 case DW_FORM_ref_addr:
17731 if (cu->header.version == 2)
17732 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17734 DW_UNSND (attr) = read_offset (abfd, info_ptr,
17735 &cu->header, &bytes_read);
17736 info_ptr += bytes_read;
17738 case DW_FORM_GNU_ref_alt:
17739 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17740 info_ptr += bytes_read;
17743 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
17744 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
17745 info_ptr += bytes_read;
17747 case DW_FORM_block2:
17748 blk = dwarf_alloc_block (cu);
17749 blk->size = read_2_bytes (abfd, info_ptr);
17751 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17752 info_ptr += blk->size;
17753 DW_BLOCK (attr) = blk;
17755 case DW_FORM_block4:
17756 blk = dwarf_alloc_block (cu);
17757 blk->size = read_4_bytes (abfd, info_ptr);
17759 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17760 info_ptr += blk->size;
17761 DW_BLOCK (attr) = blk;
17763 case DW_FORM_data2:
17764 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
17767 case DW_FORM_data4:
17768 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
17771 case DW_FORM_data8:
17772 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
17775 case DW_FORM_data16:
17776 blk = dwarf_alloc_block (cu);
17778 blk->data = read_n_bytes (abfd, info_ptr, 16);
17780 DW_BLOCK (attr) = blk;
17782 case DW_FORM_sec_offset:
17783 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
17784 info_ptr += bytes_read;
17786 case DW_FORM_string:
17787 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
17788 DW_STRING_IS_CANONICAL (attr) = 0;
17789 info_ptr += bytes_read;
17792 if (!cu->per_cu->is_dwz)
17794 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
17796 DW_STRING_IS_CANONICAL (attr) = 0;
17797 info_ptr += bytes_read;
17801 case DW_FORM_line_strp:
17802 if (!cu->per_cu->is_dwz)
17804 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
17805 cu_header, &bytes_read);
17806 DW_STRING_IS_CANONICAL (attr) = 0;
17807 info_ptr += bytes_read;
17811 case DW_FORM_GNU_strp_alt:
17813 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17814 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
17817 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
17818 DW_STRING_IS_CANONICAL (attr) = 0;
17819 info_ptr += bytes_read;
17822 case DW_FORM_exprloc:
17823 case DW_FORM_block:
17824 blk = dwarf_alloc_block (cu);
17825 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17826 info_ptr += bytes_read;
17827 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17828 info_ptr += blk->size;
17829 DW_BLOCK (attr) = blk;
17831 case DW_FORM_block1:
17832 blk = dwarf_alloc_block (cu);
17833 blk->size = read_1_byte (abfd, info_ptr);
17835 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17836 info_ptr += blk->size;
17837 DW_BLOCK (attr) = blk;
17839 case DW_FORM_data1:
17840 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17844 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
17847 case DW_FORM_flag_present:
17848 DW_UNSND (attr) = 1;
17850 case DW_FORM_sdata:
17851 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17852 info_ptr += bytes_read;
17854 case DW_FORM_udata:
17855 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17856 info_ptr += bytes_read;
17859 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17860 + read_1_byte (abfd, info_ptr));
17864 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17865 + read_2_bytes (abfd, info_ptr));
17869 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17870 + read_4_bytes (abfd, info_ptr));
17874 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17875 + read_8_bytes (abfd, info_ptr));
17878 case DW_FORM_ref_sig8:
17879 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
17882 case DW_FORM_ref_udata:
17883 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
17884 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17885 info_ptr += bytes_read;
17887 case DW_FORM_indirect:
17888 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17889 info_ptr += bytes_read;
17890 if (form == DW_FORM_implicit_const)
17892 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17893 info_ptr += bytes_read;
17895 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17898 case DW_FORM_implicit_const:
17899 DW_SND (attr) = implicit_const;
17901 case DW_FORM_GNU_addr_index:
17902 if (reader->dwo_file == NULL)
17904 /* For now flag a hard error.
17905 Later we can turn this into a complaint. */
17906 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17907 dwarf_form_name (form),
17908 bfd_get_filename (abfd));
17910 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
17911 info_ptr += bytes_read;
17913 case DW_FORM_GNU_str_index:
17914 if (reader->dwo_file == NULL)
17916 /* For now flag a hard error.
17917 Later we can turn this into a complaint if warranted. */
17918 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
17919 dwarf_form_name (form),
17920 bfd_get_filename (abfd));
17923 ULONGEST str_index =
17924 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17926 DW_STRING (attr) = read_str_index (reader, str_index);
17927 DW_STRING_IS_CANONICAL (attr) = 0;
17928 info_ptr += bytes_read;
17932 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17933 dwarf_form_name (form),
17934 bfd_get_filename (abfd));
17938 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
17939 attr->form = DW_FORM_GNU_ref_alt;
17941 /* We have seen instances where the compiler tried to emit a byte
17942 size attribute of -1 which ended up being encoded as an unsigned
17943 0xffffffff. Although 0xffffffff is technically a valid size value,
17944 an object of this size seems pretty unlikely so we can relatively
17945 safely treat these cases as if the size attribute was invalid and
17946 treat them as zero by default. */
17947 if (attr->name == DW_AT_byte_size
17948 && form == DW_FORM_data4
17949 && DW_UNSND (attr) >= 0xffffffff)
17952 (&symfile_complaints,
17953 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17954 hex_string (DW_UNSND (attr)));
17955 DW_UNSND (attr) = 0;
17961 /* Read an attribute described by an abbreviated attribute. */
17963 static const gdb_byte *
17964 read_attribute (const struct die_reader_specs *reader,
17965 struct attribute *attr, struct attr_abbrev *abbrev,
17966 const gdb_byte *info_ptr)
17968 attr->name = abbrev->name;
17969 return read_attribute_value (reader, attr, abbrev->form,
17970 abbrev->implicit_const, info_ptr);
17973 /* Read dwarf information from a buffer. */
17975 static unsigned int
17976 read_1_byte (bfd *abfd, const gdb_byte *buf)
17978 return bfd_get_8 (abfd, buf);
17982 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
17984 return bfd_get_signed_8 (abfd, buf);
17987 static unsigned int
17988 read_2_bytes (bfd *abfd, const gdb_byte *buf)
17990 return bfd_get_16 (abfd, buf);
17994 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
17996 return bfd_get_signed_16 (abfd, buf);
17999 static unsigned int
18000 read_4_bytes (bfd *abfd, const gdb_byte *buf)
18002 return bfd_get_32 (abfd, buf);
18006 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
18008 return bfd_get_signed_32 (abfd, buf);
18012 read_8_bytes (bfd *abfd, const gdb_byte *buf)
18014 return bfd_get_64 (abfd, buf);
18018 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
18019 unsigned int *bytes_read)
18021 struct comp_unit_head *cu_header = &cu->header;
18022 CORE_ADDR retval = 0;
18024 if (cu_header->signed_addr_p)
18026 switch (cu_header->addr_size)
18029 retval = bfd_get_signed_16 (abfd, buf);
18032 retval = bfd_get_signed_32 (abfd, buf);
18035 retval = bfd_get_signed_64 (abfd, buf);
18038 internal_error (__FILE__, __LINE__,
18039 _("read_address: bad switch, signed [in module %s]"),
18040 bfd_get_filename (abfd));
18045 switch (cu_header->addr_size)
18048 retval = bfd_get_16 (abfd, buf);
18051 retval = bfd_get_32 (abfd, buf);
18054 retval = bfd_get_64 (abfd, buf);
18057 internal_error (__FILE__, __LINE__,
18058 _("read_address: bad switch, "
18059 "unsigned [in module %s]"),
18060 bfd_get_filename (abfd));
18064 *bytes_read = cu_header->addr_size;
18068 /* Read the initial length from a section. The (draft) DWARF 3
18069 specification allows the initial length to take up either 4 bytes
18070 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
18071 bytes describe the length and all offsets will be 8 bytes in length
18074 An older, non-standard 64-bit format is also handled by this
18075 function. The older format in question stores the initial length
18076 as an 8-byte quantity without an escape value. Lengths greater
18077 than 2^32 aren't very common which means that the initial 4 bytes
18078 is almost always zero. Since a length value of zero doesn't make
18079 sense for the 32-bit format, this initial zero can be considered to
18080 be an escape value which indicates the presence of the older 64-bit
18081 format. As written, the code can't detect (old format) lengths
18082 greater than 4GB. If it becomes necessary to handle lengths
18083 somewhat larger than 4GB, we could allow other small values (such
18084 as the non-sensical values of 1, 2, and 3) to also be used as
18085 escape values indicating the presence of the old format.
18087 The value returned via bytes_read should be used to increment the
18088 relevant pointer after calling read_initial_length().
18090 [ Note: read_initial_length() and read_offset() are based on the
18091 document entitled "DWARF Debugging Information Format", revision
18092 3, draft 8, dated November 19, 2001. This document was obtained
18095 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
18097 This document is only a draft and is subject to change. (So beware.)
18099 Details regarding the older, non-standard 64-bit format were
18100 determined empirically by examining 64-bit ELF files produced by
18101 the SGI toolchain on an IRIX 6.5 machine.
18103 - Kevin, July 16, 2002
18107 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
18109 LONGEST length = bfd_get_32 (abfd, buf);
18111 if (length == 0xffffffff)
18113 length = bfd_get_64 (abfd, buf + 4);
18116 else if (length == 0)
18118 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
18119 length = bfd_get_64 (abfd, buf);
18130 /* Cover function for read_initial_length.
18131 Returns the length of the object at BUF, and stores the size of the
18132 initial length in *BYTES_READ and stores the size that offsets will be in
18134 If the initial length size is not equivalent to that specified in
18135 CU_HEADER then issue a complaint.
18136 This is useful when reading non-comp-unit headers. */
18139 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18140 const struct comp_unit_head *cu_header,
18141 unsigned int *bytes_read,
18142 unsigned int *offset_size)
18144 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18146 gdb_assert (cu_header->initial_length_size == 4
18147 || cu_header->initial_length_size == 8
18148 || cu_header->initial_length_size == 12);
18150 if (cu_header->initial_length_size != *bytes_read)
18151 complaint (&symfile_complaints,
18152 _("intermixed 32-bit and 64-bit DWARF sections"));
18154 *offset_size = (*bytes_read == 4) ? 4 : 8;
18158 /* Read an offset from the data stream. The size of the offset is
18159 given by cu_header->offset_size. */
18162 read_offset (bfd *abfd, const gdb_byte *buf,
18163 const struct comp_unit_head *cu_header,
18164 unsigned int *bytes_read)
18166 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
18168 *bytes_read = cu_header->offset_size;
18172 /* Read an offset from the data stream. */
18175 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
18177 LONGEST retval = 0;
18179 switch (offset_size)
18182 retval = bfd_get_32 (abfd, buf);
18185 retval = bfd_get_64 (abfd, buf);
18188 internal_error (__FILE__, __LINE__,
18189 _("read_offset_1: bad switch [in module %s]"),
18190 bfd_get_filename (abfd));
18196 static const gdb_byte *
18197 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
18199 /* If the size of a host char is 8 bits, we can return a pointer
18200 to the buffer, otherwise we have to copy the data to a buffer
18201 allocated on the temporary obstack. */
18202 gdb_assert (HOST_CHAR_BIT == 8);
18206 static const char *
18207 read_direct_string (bfd *abfd, const gdb_byte *buf,
18208 unsigned int *bytes_read_ptr)
18210 /* If the size of a host char is 8 bits, we can return a pointer
18211 to the string, otherwise we have to copy the string to a buffer
18212 allocated on the temporary obstack. */
18213 gdb_assert (HOST_CHAR_BIT == 8);
18216 *bytes_read_ptr = 1;
18219 *bytes_read_ptr = strlen ((const char *) buf) + 1;
18220 return (const char *) buf;
18223 /* Return pointer to string at section SECT offset STR_OFFSET with error
18224 reporting strings FORM_NAME and SECT_NAME. */
18226 static const char *
18227 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
18228 struct dwarf2_section_info *sect,
18229 const char *form_name,
18230 const char *sect_name)
18232 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
18233 if (sect->buffer == NULL)
18234 error (_("%s used without %s section [in module %s]"),
18235 form_name, sect_name, bfd_get_filename (abfd));
18236 if (str_offset >= sect->size)
18237 error (_("%s pointing outside of %s section [in module %s]"),
18238 form_name, sect_name, bfd_get_filename (abfd));
18239 gdb_assert (HOST_CHAR_BIT == 8);
18240 if (sect->buffer[str_offset] == '\0')
18242 return (const char *) (sect->buffer + str_offset);
18245 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18247 static const char *
18248 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
18250 return read_indirect_string_at_offset_from (abfd, str_offset,
18251 &dwarf2_per_objfile->str,
18252 "DW_FORM_strp", ".debug_str");
18255 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18257 static const char *
18258 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
18260 return read_indirect_string_at_offset_from (abfd, str_offset,
18261 &dwarf2_per_objfile->line_str,
18262 "DW_FORM_line_strp",
18263 ".debug_line_str");
18266 /* Read a string at offset STR_OFFSET in the .debug_str section from
18267 the .dwz file DWZ. Throw an error if the offset is too large. If
18268 the string consists of a single NUL byte, return NULL; otherwise
18269 return a pointer to the string. */
18271 static const char *
18272 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
18274 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
18276 if (dwz->str.buffer == NULL)
18277 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18278 "section [in module %s]"),
18279 bfd_get_filename (dwz->dwz_bfd));
18280 if (str_offset >= dwz->str.size)
18281 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18282 ".debug_str section [in module %s]"),
18283 bfd_get_filename (dwz->dwz_bfd));
18284 gdb_assert (HOST_CHAR_BIT == 8);
18285 if (dwz->str.buffer[str_offset] == '\0')
18287 return (const char *) (dwz->str.buffer + str_offset);
18290 /* Return pointer to string at .debug_str offset as read from BUF.
18291 BUF is assumed to be in a compilation unit described by CU_HEADER.
18292 Return *BYTES_READ_PTR count of bytes read from BUF. */
18294 static const char *
18295 read_indirect_string (bfd *abfd, const gdb_byte *buf,
18296 const struct comp_unit_head *cu_header,
18297 unsigned int *bytes_read_ptr)
18299 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18301 return read_indirect_string_at_offset (abfd, str_offset);
18304 /* Return pointer to string at .debug_line_str offset as read from BUF.
18305 BUF is assumed to be in a compilation unit described by CU_HEADER.
18306 Return *BYTES_READ_PTR count of bytes read from BUF. */
18308 static const char *
18309 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
18310 const struct comp_unit_head *cu_header,
18311 unsigned int *bytes_read_ptr)
18313 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
18315 return read_indirect_line_string_at_offset (abfd, str_offset);
18319 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
18320 unsigned int *bytes_read_ptr)
18323 unsigned int num_read;
18325 unsigned char byte;
18332 byte = bfd_get_8 (abfd, buf);
18335 result |= ((ULONGEST) (byte & 127) << shift);
18336 if ((byte & 128) == 0)
18342 *bytes_read_ptr = num_read;
18347 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
18348 unsigned int *bytes_read_ptr)
18351 int shift, num_read;
18352 unsigned char byte;
18359 byte = bfd_get_8 (abfd, buf);
18362 result |= ((LONGEST) (byte & 127) << shift);
18364 if ((byte & 128) == 0)
18369 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
18370 result |= -(((LONGEST) 1) << shift);
18371 *bytes_read_ptr = num_read;
18375 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18376 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
18377 ADDR_SIZE is the size of addresses from the CU header. */
18380 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
18382 struct objfile *objfile = dwarf2_per_objfile->objfile;
18383 bfd *abfd = objfile->obfd;
18384 const gdb_byte *info_ptr;
18386 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
18387 if (dwarf2_per_objfile->addr.buffer == NULL)
18388 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18389 objfile_name (objfile));
18390 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
18391 error (_("DW_FORM_addr_index pointing outside of "
18392 ".debug_addr section [in module %s]"),
18393 objfile_name (objfile));
18394 info_ptr = (dwarf2_per_objfile->addr.buffer
18395 + addr_base + addr_index * addr_size);
18396 if (addr_size == 4)
18397 return bfd_get_32 (abfd, info_ptr);
18399 return bfd_get_64 (abfd, info_ptr);
18402 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18405 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18407 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
18410 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18413 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18414 unsigned int *bytes_read)
18416 bfd *abfd = cu->objfile->obfd;
18417 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18419 return read_addr_index (cu, addr_index);
18422 /* Data structure to pass results from dwarf2_read_addr_index_reader
18423 back to dwarf2_read_addr_index. */
18425 struct dwarf2_read_addr_index_data
18427 ULONGEST addr_base;
18431 /* die_reader_func for dwarf2_read_addr_index. */
18434 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
18435 const gdb_byte *info_ptr,
18436 struct die_info *comp_unit_die,
18440 struct dwarf2_cu *cu = reader->cu;
18441 struct dwarf2_read_addr_index_data *aidata =
18442 (struct dwarf2_read_addr_index_data *) data;
18444 aidata->addr_base = cu->addr_base;
18445 aidata->addr_size = cu->header.addr_size;
18448 /* Given an index in .debug_addr, fetch the value.
18449 NOTE: This can be called during dwarf expression evaluation,
18450 long after the debug information has been read, and thus per_cu->cu
18451 may no longer exist. */
18454 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18455 unsigned int addr_index)
18457 struct objfile *objfile = per_cu->objfile;
18458 struct dwarf2_cu *cu = per_cu->cu;
18459 ULONGEST addr_base;
18462 /* This is intended to be called from outside this file. */
18463 dw2_setup (objfile);
18465 /* We need addr_base and addr_size.
18466 If we don't have PER_CU->cu, we have to get it.
18467 Nasty, but the alternative is storing the needed info in PER_CU,
18468 which at this point doesn't seem justified: it's not clear how frequently
18469 it would get used and it would increase the size of every PER_CU.
18470 Entry points like dwarf2_per_cu_addr_size do a similar thing
18471 so we're not in uncharted territory here.
18472 Alas we need to be a bit more complicated as addr_base is contained
18475 We don't need to read the entire CU(/TU).
18476 We just need the header and top level die.
18478 IWBN to use the aging mechanism to let us lazily later discard the CU.
18479 For now we skip this optimization. */
18483 addr_base = cu->addr_base;
18484 addr_size = cu->header.addr_size;
18488 struct dwarf2_read_addr_index_data aidata;
18490 /* Note: We can't use init_cutu_and_read_dies_simple here,
18491 we need addr_base. */
18492 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
18493 dwarf2_read_addr_index_reader, &aidata);
18494 addr_base = aidata.addr_base;
18495 addr_size = aidata.addr_size;
18498 return read_addr_index_1 (addr_index, addr_base, addr_size);
18501 /* Given a DW_FORM_GNU_str_index, fetch the string.
18502 This is only used by the Fission support. */
18504 static const char *
18505 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18507 struct objfile *objfile = dwarf2_per_objfile->objfile;
18508 const char *objf_name = objfile_name (objfile);
18509 bfd *abfd = objfile->obfd;
18510 struct dwarf2_cu *cu = reader->cu;
18511 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
18512 struct dwarf2_section_info *str_offsets_section =
18513 &reader->dwo_file->sections.str_offsets;
18514 const gdb_byte *info_ptr;
18515 ULONGEST str_offset;
18516 static const char form_name[] = "DW_FORM_GNU_str_index";
18518 dwarf2_read_section (objfile, str_section);
18519 dwarf2_read_section (objfile, str_offsets_section);
18520 if (str_section->buffer == NULL)
18521 error (_("%s used without .debug_str.dwo section"
18522 " in CU at offset 0x%x [in module %s]"),
18523 form_name, to_underlying (cu->header.sect_off), objf_name);
18524 if (str_offsets_section->buffer == NULL)
18525 error (_("%s used without .debug_str_offsets.dwo section"
18526 " in CU at offset 0x%x [in module %s]"),
18527 form_name, to_underlying (cu->header.sect_off), objf_name);
18528 if (str_index * cu->header.offset_size >= str_offsets_section->size)
18529 error (_("%s pointing outside of .debug_str_offsets.dwo"
18530 " section in CU at offset 0x%x [in module %s]"),
18531 form_name, to_underlying (cu->header.sect_off), objf_name);
18532 info_ptr = (str_offsets_section->buffer
18533 + str_index * cu->header.offset_size);
18534 if (cu->header.offset_size == 4)
18535 str_offset = bfd_get_32 (abfd, info_ptr);
18537 str_offset = bfd_get_64 (abfd, info_ptr);
18538 if (str_offset >= str_section->size)
18539 error (_("Offset from %s pointing outside of"
18540 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
18541 form_name, to_underlying (cu->header.sect_off), objf_name);
18542 return (const char *) (str_section->buffer + str_offset);
18545 /* Return the length of an LEB128 number in BUF. */
18548 leb128_size (const gdb_byte *buf)
18550 const gdb_byte *begin = buf;
18556 if ((byte & 128) == 0)
18557 return buf - begin;
18562 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18571 cu->language = language_c;
18574 case DW_LANG_C_plus_plus:
18575 case DW_LANG_C_plus_plus_11:
18576 case DW_LANG_C_plus_plus_14:
18577 cu->language = language_cplus;
18580 cu->language = language_d;
18582 case DW_LANG_Fortran77:
18583 case DW_LANG_Fortran90:
18584 case DW_LANG_Fortran95:
18585 case DW_LANG_Fortran03:
18586 case DW_LANG_Fortran08:
18587 cu->language = language_fortran;
18590 cu->language = language_go;
18592 case DW_LANG_Mips_Assembler:
18593 cu->language = language_asm;
18595 case DW_LANG_Ada83:
18596 case DW_LANG_Ada95:
18597 cu->language = language_ada;
18599 case DW_LANG_Modula2:
18600 cu->language = language_m2;
18602 case DW_LANG_Pascal83:
18603 cu->language = language_pascal;
18606 cu->language = language_objc;
18609 case DW_LANG_Rust_old:
18610 cu->language = language_rust;
18612 case DW_LANG_Cobol74:
18613 case DW_LANG_Cobol85:
18615 cu->language = language_minimal;
18618 cu->language_defn = language_def (cu->language);
18621 /* Return the named attribute or NULL if not there. */
18623 static struct attribute *
18624 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18629 struct attribute *spec = NULL;
18631 for (i = 0; i < die->num_attrs; ++i)
18633 if (die->attrs[i].name == name)
18634 return &die->attrs[i];
18635 if (die->attrs[i].name == DW_AT_specification
18636 || die->attrs[i].name == DW_AT_abstract_origin)
18637 spec = &die->attrs[i];
18643 die = follow_die_ref (die, spec, &cu);
18649 /* Return the named attribute or NULL if not there,
18650 but do not follow DW_AT_specification, etc.
18651 This is for use in contexts where we're reading .debug_types dies.
18652 Following DW_AT_specification, DW_AT_abstract_origin will take us
18653 back up the chain, and we want to go down. */
18655 static struct attribute *
18656 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
18660 for (i = 0; i < die->num_attrs; ++i)
18661 if (die->attrs[i].name == name)
18662 return &die->attrs[i];
18667 /* Return the string associated with a string-typed attribute, or NULL if it
18668 is either not found or is of an incorrect type. */
18670 static const char *
18671 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18673 struct attribute *attr;
18674 const char *str = NULL;
18676 attr = dwarf2_attr (die, name, cu);
18680 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18681 || attr->form == DW_FORM_string
18682 || attr->form == DW_FORM_GNU_str_index
18683 || attr->form == DW_FORM_GNU_strp_alt)
18684 str = DW_STRING (attr);
18686 complaint (&symfile_complaints,
18687 _("string type expected for attribute %s for "
18688 "DIE at 0x%x in module %s"),
18689 dwarf_attr_name (name), to_underlying (die->sect_off),
18690 objfile_name (cu->objfile));
18696 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18697 and holds a non-zero value. This function should only be used for
18698 DW_FORM_flag or DW_FORM_flag_present attributes. */
18701 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18703 struct attribute *attr = dwarf2_attr (die, name, cu);
18705 return (attr && DW_UNSND (attr));
18709 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
18711 /* A DIE is a declaration if it has a DW_AT_declaration attribute
18712 which value is non-zero. However, we have to be careful with
18713 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
18714 (via dwarf2_flag_true_p) follows this attribute. So we may
18715 end up accidently finding a declaration attribute that belongs
18716 to a different DIE referenced by the specification attribute,
18717 even though the given DIE does not have a declaration attribute. */
18718 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
18719 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
18722 /* Return the die giving the specification for DIE, if there is
18723 one. *SPEC_CU is the CU containing DIE on input, and the CU
18724 containing the return value on output. If there is no
18725 specification, but there is an abstract origin, that is
18728 static struct die_info *
18729 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
18731 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
18734 if (spec_attr == NULL)
18735 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
18737 if (spec_attr == NULL)
18740 return follow_die_ref (die, spec_attr, spec_cu);
18743 /* Stub for free_line_header to match void * callback types. */
18746 free_line_header_voidp (void *arg)
18748 struct line_header *lh = (struct line_header *) arg;
18754 line_header::add_include_dir (const char *include_dir)
18756 if (dwarf_line_debug >= 2)
18757 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
18758 include_dirs.size () + 1, include_dir);
18760 include_dirs.push_back (include_dir);
18764 line_header::add_file_name (const char *name,
18766 unsigned int mod_time,
18767 unsigned int length)
18769 if (dwarf_line_debug >= 2)
18770 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
18771 (unsigned) file_names.size () + 1, name);
18773 file_names.emplace_back (name, d_index, mod_time, length);
18776 /* A convenience function to find the proper .debug_line section for a CU. */
18778 static struct dwarf2_section_info *
18779 get_debug_line_section (struct dwarf2_cu *cu)
18781 struct dwarf2_section_info *section;
18783 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
18785 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18786 section = &cu->dwo_unit->dwo_file->sections.line;
18787 else if (cu->per_cu->is_dwz)
18789 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18791 section = &dwz->line;
18794 section = &dwarf2_per_objfile->line;
18799 /* Read directory or file name entry format, starting with byte of
18800 format count entries, ULEB128 pairs of entry formats, ULEB128 of
18801 entries count and the entries themselves in the described entry
18805 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
18806 struct line_header *lh,
18807 const struct comp_unit_head *cu_header,
18808 void (*callback) (struct line_header *lh,
18811 unsigned int mod_time,
18812 unsigned int length))
18814 gdb_byte format_count, formati;
18815 ULONGEST data_count, datai;
18816 const gdb_byte *buf = *bufp;
18817 const gdb_byte *format_header_data;
18818 unsigned int bytes_read;
18820 format_count = read_1_byte (abfd, buf);
18822 format_header_data = buf;
18823 for (formati = 0; formati < format_count; formati++)
18825 read_unsigned_leb128 (abfd, buf, &bytes_read);
18827 read_unsigned_leb128 (abfd, buf, &bytes_read);
18831 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
18833 for (datai = 0; datai < data_count; datai++)
18835 const gdb_byte *format = format_header_data;
18836 struct file_entry fe;
18838 for (formati = 0; formati < format_count; formati++)
18840 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
18841 format += bytes_read;
18843 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
18844 format += bytes_read;
18846 gdb::optional<const char *> string;
18847 gdb::optional<unsigned int> uint;
18851 case DW_FORM_string:
18852 string.emplace (read_direct_string (abfd, buf, &bytes_read));
18856 case DW_FORM_line_strp:
18857 string.emplace (read_indirect_line_string (abfd, buf,
18863 case DW_FORM_data1:
18864 uint.emplace (read_1_byte (abfd, buf));
18868 case DW_FORM_data2:
18869 uint.emplace (read_2_bytes (abfd, buf));
18873 case DW_FORM_data4:
18874 uint.emplace (read_4_bytes (abfd, buf));
18878 case DW_FORM_data8:
18879 uint.emplace (read_8_bytes (abfd, buf));
18883 case DW_FORM_udata:
18884 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
18888 case DW_FORM_block:
18889 /* It is valid only for DW_LNCT_timestamp which is ignored by
18894 switch (content_type)
18897 if (string.has_value ())
18900 case DW_LNCT_directory_index:
18901 if (uint.has_value ())
18902 fe.d_index = (dir_index) *uint;
18904 case DW_LNCT_timestamp:
18905 if (uint.has_value ())
18906 fe.mod_time = *uint;
18909 if (uint.has_value ())
18915 complaint (&symfile_complaints,
18916 _("Unknown format content type %s"),
18917 pulongest (content_type));
18921 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
18927 /* Read the statement program header starting at OFFSET in
18928 .debug_line, or .debug_line.dwo. Return a pointer
18929 to a struct line_header, allocated using xmalloc.
18930 Returns NULL if there is a problem reading the header, e.g., if it
18931 has a version we don't understand.
18933 NOTE: the strings in the include directory and file name tables of
18934 the returned object point into the dwarf line section buffer,
18935 and must not be freed. */
18937 static line_header_up
18938 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
18940 const gdb_byte *line_ptr;
18941 unsigned int bytes_read, offset_size;
18943 const char *cur_dir, *cur_file;
18944 struct dwarf2_section_info *section;
18947 section = get_debug_line_section (cu);
18948 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18949 if (section->buffer == NULL)
18951 if (cu->dwo_unit && cu->per_cu->is_debug_types)
18952 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18954 complaint (&symfile_complaints, _("missing .debug_line section"));
18958 /* We can't do this until we know the section is non-empty.
18959 Only then do we know we have such a section. */
18960 abfd = get_section_bfd_owner (section);
18962 /* Make sure that at least there's room for the total_length field.
18963 That could be 12 bytes long, but we're just going to fudge that. */
18964 if (to_underlying (sect_off) + 4 >= section->size)
18966 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18970 line_header_up lh (new line_header ());
18972 lh->sect_off = sect_off;
18973 lh->offset_in_dwz = cu->per_cu->is_dwz;
18975 line_ptr = section->buffer + to_underlying (sect_off);
18977 /* Read in the header. */
18979 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18980 &bytes_read, &offset_size);
18981 line_ptr += bytes_read;
18982 if (line_ptr + lh->total_length > (section->buffer + section->size))
18984 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18987 lh->statement_program_end = line_ptr + lh->total_length;
18988 lh->version = read_2_bytes (abfd, line_ptr);
18990 if (lh->version > 5)
18992 /* This is a version we don't understand. The format could have
18993 changed in ways we don't handle properly so just punt. */
18994 complaint (&symfile_complaints,
18995 _("unsupported version in .debug_line section"));
18998 if (lh->version >= 5)
19000 gdb_byte segment_selector_size;
19002 /* Skip address size. */
19003 read_1_byte (abfd, line_ptr);
19006 segment_selector_size = read_1_byte (abfd, line_ptr);
19008 if (segment_selector_size != 0)
19010 complaint (&symfile_complaints,
19011 _("unsupported segment selector size %u "
19012 "in .debug_line section"),
19013 segment_selector_size);
19017 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
19018 line_ptr += offset_size;
19019 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19021 if (lh->version >= 4)
19023 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19027 lh->maximum_ops_per_instruction = 1;
19029 if (lh->maximum_ops_per_instruction == 0)
19031 lh->maximum_ops_per_instruction = 1;
19032 complaint (&symfile_complaints,
19033 _("invalid maximum_ops_per_instruction "
19034 "in `.debug_line' section"));
19037 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19039 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19041 lh->line_range = read_1_byte (abfd, line_ptr);
19043 lh->opcode_base = read_1_byte (abfd, line_ptr);
19045 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19047 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19048 for (i = 1; i < lh->opcode_base; ++i)
19050 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19054 if (lh->version >= 5)
19056 /* Read directory table. */
19057 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
19058 [] (struct line_header *lh, const char *name,
19059 dir_index d_index, unsigned int mod_time,
19060 unsigned int length)
19062 lh->add_include_dir (name);
19065 /* Read file name table. */
19066 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
19067 [] (struct line_header *lh, const char *name,
19068 dir_index d_index, unsigned int mod_time,
19069 unsigned int length)
19071 lh->add_file_name (name, d_index, mod_time, length);
19076 /* Read directory table. */
19077 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19079 line_ptr += bytes_read;
19080 lh->add_include_dir (cur_dir);
19082 line_ptr += bytes_read;
19084 /* Read file name table. */
19085 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19087 unsigned int mod_time, length;
19090 line_ptr += bytes_read;
19091 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19092 line_ptr += bytes_read;
19093 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19094 line_ptr += bytes_read;
19095 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19096 line_ptr += bytes_read;
19098 lh->add_file_name (cur_file, d_index, mod_time, length);
19100 line_ptr += bytes_read;
19102 lh->statement_program_start = line_ptr;
19104 if (line_ptr > (section->buffer + section->size))
19105 complaint (&symfile_complaints,
19106 _("line number info header doesn't "
19107 "fit in `.debug_line' section"));
19112 /* Subroutine of dwarf_decode_lines to simplify it.
19113 Return the file name of the psymtab for included file FILE_INDEX
19114 in line header LH of PST.
19115 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19116 If space for the result is malloc'd, it will be freed by a cleanup.
19117 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
19119 The function creates dangling cleanup registration. */
19121 static const char *
19122 psymtab_include_file_name (const struct line_header *lh, int file_index,
19123 const struct partial_symtab *pst,
19124 const char *comp_dir)
19126 const file_entry &fe = lh->file_names[file_index];
19127 const char *include_name = fe.name;
19128 const char *include_name_to_compare = include_name;
19129 const char *pst_filename;
19130 char *copied_name = NULL;
19133 const char *dir_name = fe.include_dir (lh);
19135 if (!IS_ABSOLUTE_PATH (include_name)
19136 && (dir_name != NULL || comp_dir != NULL))
19138 /* Avoid creating a duplicate psymtab for PST.
19139 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19140 Before we do the comparison, however, we need to account
19141 for DIR_NAME and COMP_DIR.
19142 First prepend dir_name (if non-NULL). If we still don't
19143 have an absolute path prepend comp_dir (if non-NULL).
19144 However, the directory we record in the include-file's
19145 psymtab does not contain COMP_DIR (to match the
19146 corresponding symtab(s)).
19151 bash$ gcc -g ./hello.c
19152 include_name = "hello.c"
19154 DW_AT_comp_dir = comp_dir = "/tmp"
19155 DW_AT_name = "./hello.c"
19159 if (dir_name != NULL)
19161 char *tem = concat (dir_name, SLASH_STRING,
19162 include_name, (char *)NULL);
19164 make_cleanup (xfree, tem);
19165 include_name = tem;
19166 include_name_to_compare = include_name;
19168 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19170 char *tem = concat (comp_dir, SLASH_STRING,
19171 include_name, (char *)NULL);
19173 make_cleanup (xfree, tem);
19174 include_name_to_compare = tem;
19178 pst_filename = pst->filename;
19179 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19181 copied_name = concat (pst->dirname, SLASH_STRING,
19182 pst_filename, (char *)NULL);
19183 pst_filename = copied_name;
19186 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19188 if (copied_name != NULL)
19189 xfree (copied_name);
19193 return include_name;
19196 /* State machine to track the state of the line number program. */
19198 class lnp_state_machine
19201 /* Initialize a machine state for the start of a line number
19203 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
19205 file_entry *current_file ()
19207 /* lh->file_names is 0-based, but the file name numbers in the
19208 statement program are 1-based. */
19209 return m_line_header->file_name_at (m_file);
19212 /* Record the line in the state machine. END_SEQUENCE is true if
19213 we're processing the end of a sequence. */
19214 void record_line (bool end_sequence);
19216 /* Check address and if invalid nop-out the rest of the lines in this
19218 void check_line_address (struct dwarf2_cu *cu,
19219 const gdb_byte *line_ptr,
19220 CORE_ADDR lowpc, CORE_ADDR address);
19222 void handle_set_discriminator (unsigned int discriminator)
19224 m_discriminator = discriminator;
19225 m_line_has_non_zero_discriminator |= discriminator != 0;
19228 /* Handle DW_LNE_set_address. */
19229 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19232 address += baseaddr;
19233 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19236 /* Handle DW_LNS_advance_pc. */
19237 void handle_advance_pc (CORE_ADDR adjust);
19239 /* Handle a special opcode. */
19240 void handle_special_opcode (unsigned char op_code);
19242 /* Handle DW_LNS_advance_line. */
19243 void handle_advance_line (int line_delta)
19245 advance_line (line_delta);
19248 /* Handle DW_LNS_set_file. */
19249 void handle_set_file (file_name_index file);
19251 /* Handle DW_LNS_negate_stmt. */
19252 void handle_negate_stmt ()
19254 m_is_stmt = !m_is_stmt;
19257 /* Handle DW_LNS_const_add_pc. */
19258 void handle_const_add_pc ();
19260 /* Handle DW_LNS_fixed_advance_pc. */
19261 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19263 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19267 /* Handle DW_LNS_copy. */
19268 void handle_copy ()
19270 record_line (false);
19271 m_discriminator = 0;
19274 /* Handle DW_LNE_end_sequence. */
19275 void handle_end_sequence ()
19277 m_record_line_callback = ::record_line;
19281 /* Advance the line by LINE_DELTA. */
19282 void advance_line (int line_delta)
19284 m_line += line_delta;
19286 if (line_delta != 0)
19287 m_line_has_non_zero_discriminator = m_discriminator != 0;
19290 gdbarch *m_gdbarch;
19292 /* True if we're recording lines.
19293 Otherwise we're building partial symtabs and are just interested in
19294 finding include files mentioned by the line number program. */
19295 bool m_record_lines_p;
19297 /* The line number header. */
19298 line_header *m_line_header;
19300 /* These are part of the standard DWARF line number state machine,
19301 and initialized according to the DWARF spec. */
19303 unsigned char m_op_index = 0;
19304 /* The line table index (1-based) of the current file. */
19305 file_name_index m_file = (file_name_index) 1;
19306 unsigned int m_line = 1;
19308 /* These are initialized in the constructor. */
19310 CORE_ADDR m_address;
19312 unsigned int m_discriminator;
19314 /* Additional bits of state we need to track. */
19316 /* The last file that we called dwarf2_start_subfile for.
19317 This is only used for TLLs. */
19318 unsigned int m_last_file = 0;
19319 /* The last file a line number was recorded for. */
19320 struct subfile *m_last_subfile = NULL;
19322 /* The function to call to record a line. */
19323 record_line_ftype *m_record_line_callback = NULL;
19325 /* The last line number that was recorded, used to coalesce
19326 consecutive entries for the same line. This can happen, for
19327 example, when discriminators are present. PR 17276. */
19328 unsigned int m_last_line = 0;
19329 bool m_line_has_non_zero_discriminator = false;
19333 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19335 CORE_ADDR addr_adj = (((m_op_index + adjust)
19336 / m_line_header->maximum_ops_per_instruction)
19337 * m_line_header->minimum_instruction_length);
19338 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19339 m_op_index = ((m_op_index + adjust)
19340 % m_line_header->maximum_ops_per_instruction);
19344 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19346 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19347 CORE_ADDR addr_adj = (((m_op_index
19348 + (adj_opcode / m_line_header->line_range))
19349 / m_line_header->maximum_ops_per_instruction)
19350 * m_line_header->minimum_instruction_length);
19351 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19352 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19353 % m_line_header->maximum_ops_per_instruction);
19355 int line_delta = (m_line_header->line_base
19356 + (adj_opcode % m_line_header->line_range));
19357 advance_line (line_delta);
19358 record_line (false);
19359 m_discriminator = 0;
19363 lnp_state_machine::handle_set_file (file_name_index file)
19367 const file_entry *fe = current_file ();
19369 dwarf2_debug_line_missing_file_complaint ();
19370 else if (m_record_lines_p)
19372 const char *dir = fe->include_dir (m_line_header);
19374 m_last_subfile = current_subfile;
19375 m_line_has_non_zero_discriminator = m_discriminator != 0;
19376 dwarf2_start_subfile (fe->name, dir);
19381 lnp_state_machine::handle_const_add_pc ()
19384 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19387 = (((m_op_index + adjust)
19388 / m_line_header->maximum_ops_per_instruction)
19389 * m_line_header->minimum_instruction_length);
19391 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19392 m_op_index = ((m_op_index + adjust)
19393 % m_line_header->maximum_ops_per_instruction);
19396 /* Ignore this record_line request. */
19399 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
19404 /* Return non-zero if we should add LINE to the line number table.
19405 LINE is the line to add, LAST_LINE is the last line that was added,
19406 LAST_SUBFILE is the subfile for LAST_LINE.
19407 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19408 had a non-zero discriminator.
19410 We have to be careful in the presence of discriminators.
19411 E.g., for this line:
19413 for (i = 0; i < 100000; i++);
19415 clang can emit four line number entries for that one line,
19416 each with a different discriminator.
19417 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19419 However, we want gdb to coalesce all four entries into one.
19420 Otherwise the user could stepi into the middle of the line and
19421 gdb would get confused about whether the pc really was in the
19422 middle of the line.
19424 Things are further complicated by the fact that two consecutive
19425 line number entries for the same line is a heuristic used by gcc
19426 to denote the end of the prologue. So we can't just discard duplicate
19427 entries, we have to be selective about it. The heuristic we use is
19428 that we only collapse consecutive entries for the same line if at least
19429 one of those entries has a non-zero discriminator. PR 17276.
19431 Note: Addresses in the line number state machine can never go backwards
19432 within one sequence, thus this coalescing is ok. */
19435 dwarf_record_line_p (unsigned int line, unsigned int last_line,
19436 int line_has_non_zero_discriminator,
19437 struct subfile *last_subfile)
19439 if (current_subfile != last_subfile)
19441 if (line != last_line)
19443 /* Same line for the same file that we've seen already.
19444 As a last check, for pr 17276, only record the line if the line
19445 has never had a non-zero discriminator. */
19446 if (!line_has_non_zero_discriminator)
19451 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
19452 in the line table of subfile SUBFILE. */
19455 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19456 unsigned int line, CORE_ADDR address,
19457 record_line_ftype p_record_line)
19459 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19461 if (dwarf_line_debug)
19463 fprintf_unfiltered (gdb_stdlog,
19464 "Recording line %u, file %s, address %s\n",
19465 line, lbasename (subfile->name),
19466 paddress (gdbarch, address));
19469 (*p_record_line) (subfile, line, addr);
19472 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19473 Mark the end of a set of line number records.
19474 The arguments are the same as for dwarf_record_line_1.
19475 If SUBFILE is NULL the request is ignored. */
19478 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19479 CORE_ADDR address, record_line_ftype p_record_line)
19481 if (subfile == NULL)
19484 if (dwarf_line_debug)
19486 fprintf_unfiltered (gdb_stdlog,
19487 "Finishing current line, file %s, address %s\n",
19488 lbasename (subfile->name),
19489 paddress (gdbarch, address));
19492 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
19496 lnp_state_machine::record_line (bool end_sequence)
19498 if (dwarf_line_debug)
19500 fprintf_unfiltered (gdb_stdlog,
19501 "Processing actual line %u: file %u,"
19502 " address %s, is_stmt %u, discrim %u\n",
19503 m_line, to_underlying (m_file),
19504 paddress (m_gdbarch, m_address),
19505 m_is_stmt, m_discriminator);
19508 file_entry *fe = current_file ();
19511 dwarf2_debug_line_missing_file_complaint ();
19512 /* For now we ignore lines not starting on an instruction boundary.
19513 But not when processing end_sequence for compatibility with the
19514 previous version of the code. */
19515 else if (m_op_index == 0 || end_sequence)
19517 fe->included_p = 1;
19518 if (m_record_lines_p && m_is_stmt)
19520 if (m_last_subfile != current_subfile || end_sequence)
19522 dwarf_finish_line (m_gdbarch, m_last_subfile,
19523 m_address, m_record_line_callback);
19528 if (dwarf_record_line_p (m_line, m_last_line,
19529 m_line_has_non_zero_discriminator,
19532 dwarf_record_line_1 (m_gdbarch, current_subfile,
19534 m_record_line_callback);
19536 m_last_subfile = current_subfile;
19537 m_last_line = m_line;
19543 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
19544 bool record_lines_p)
19547 m_record_lines_p = record_lines_p;
19548 m_line_header = lh;
19550 m_record_line_callback = ::record_line;
19552 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19553 was a line entry for it so that the backend has a chance to adjust it
19554 and also record it in case it needs it. This is currently used by MIPS
19555 code, cf. `mips_adjust_dwarf2_line'. */
19556 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19557 m_is_stmt = lh->default_is_stmt;
19558 m_discriminator = 0;
19562 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19563 const gdb_byte *line_ptr,
19564 CORE_ADDR lowpc, CORE_ADDR address)
19566 /* If address < lowpc then it's not a usable value, it's outside the
19567 pc range of the CU. However, we restrict the test to only address
19568 values of zero to preserve GDB's previous behaviour which is to
19569 handle the specific case of a function being GC'd by the linker. */
19571 if (address == 0 && address < lowpc)
19573 /* This line table is for a function which has been
19574 GCd by the linker. Ignore it. PR gdb/12528 */
19576 struct objfile *objfile = cu->objfile;
19577 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19579 complaint (&symfile_complaints,
19580 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19581 line_offset, objfile_name (objfile));
19582 m_record_line_callback = noop_record_line;
19583 /* Note: record_line_callback is left as noop_record_line until
19584 we see DW_LNE_end_sequence. */
19588 /* Subroutine of dwarf_decode_lines to simplify it.
19589 Process the line number information in LH.
19590 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19591 program in order to set included_p for every referenced header. */
19594 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19595 const int decode_for_pst_p, CORE_ADDR lowpc)
19597 const gdb_byte *line_ptr, *extended_end;
19598 const gdb_byte *line_end;
19599 unsigned int bytes_read, extended_len;
19600 unsigned char op_code, extended_op;
19601 CORE_ADDR baseaddr;
19602 struct objfile *objfile = cu->objfile;
19603 bfd *abfd = objfile->obfd;
19604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19605 /* True if we're recording line info (as opposed to building partial
19606 symtabs and just interested in finding include files mentioned by
19607 the line number program). */
19608 bool record_lines_p = !decode_for_pst_p;
19610 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19612 line_ptr = lh->statement_program_start;
19613 line_end = lh->statement_program_end;
19615 /* Read the statement sequences until there's nothing left. */
19616 while (line_ptr < line_end)
19618 /* The DWARF line number program state machine. Reset the state
19619 machine at the start of each sequence. */
19620 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
19621 bool end_sequence = false;
19623 if (record_lines_p)
19625 /* Start a subfile for the current file of the state
19627 const file_entry *fe = state_machine.current_file ();
19630 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
19633 /* Decode the table. */
19634 while (line_ptr < line_end && !end_sequence)
19636 op_code = read_1_byte (abfd, line_ptr);
19639 if (op_code >= lh->opcode_base)
19641 /* Special opcode. */
19642 state_machine.handle_special_opcode (op_code);
19644 else switch (op_code)
19646 case DW_LNS_extended_op:
19647 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19649 line_ptr += bytes_read;
19650 extended_end = line_ptr + extended_len;
19651 extended_op = read_1_byte (abfd, line_ptr);
19653 switch (extended_op)
19655 case DW_LNE_end_sequence:
19656 state_machine.handle_end_sequence ();
19657 end_sequence = true;
19659 case DW_LNE_set_address:
19662 = read_address (abfd, line_ptr, cu, &bytes_read);
19663 line_ptr += bytes_read;
19665 state_machine.check_line_address (cu, line_ptr,
19667 state_machine.handle_set_address (baseaddr, address);
19670 case DW_LNE_define_file:
19672 const char *cur_file;
19673 unsigned int mod_time, length;
19676 cur_file = read_direct_string (abfd, line_ptr,
19678 line_ptr += bytes_read;
19679 dindex = (dir_index)
19680 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19681 line_ptr += bytes_read;
19683 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19684 line_ptr += bytes_read;
19686 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19687 line_ptr += bytes_read;
19688 lh->add_file_name (cur_file, dindex, mod_time, length);
19691 case DW_LNE_set_discriminator:
19693 /* The discriminator is not interesting to the
19694 debugger; just ignore it. We still need to
19695 check its value though:
19696 if there are consecutive entries for the same
19697 (non-prologue) line we want to coalesce them.
19700 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19701 line_ptr += bytes_read;
19703 state_machine.handle_set_discriminator (discr);
19707 complaint (&symfile_complaints,
19708 _("mangled .debug_line section"));
19711 /* Make sure that we parsed the extended op correctly. If e.g.
19712 we expected a different address size than the producer used,
19713 we may have read the wrong number of bytes. */
19714 if (line_ptr != extended_end)
19716 complaint (&symfile_complaints,
19717 _("mangled .debug_line section"));
19722 state_machine.handle_copy ();
19724 case DW_LNS_advance_pc:
19727 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19728 line_ptr += bytes_read;
19730 state_machine.handle_advance_pc (adjust);
19733 case DW_LNS_advance_line:
19736 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19737 line_ptr += bytes_read;
19739 state_machine.handle_advance_line (line_delta);
19742 case DW_LNS_set_file:
19744 file_name_index file
19745 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19747 line_ptr += bytes_read;
19749 state_machine.handle_set_file (file);
19752 case DW_LNS_set_column:
19753 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19754 line_ptr += bytes_read;
19756 case DW_LNS_negate_stmt:
19757 state_machine.handle_negate_stmt ();
19759 case DW_LNS_set_basic_block:
19761 /* Add to the address register of the state machine the
19762 address increment value corresponding to special opcode
19763 255. I.e., this value is scaled by the minimum
19764 instruction length since special opcode 255 would have
19765 scaled the increment. */
19766 case DW_LNS_const_add_pc:
19767 state_machine.handle_const_add_pc ();
19769 case DW_LNS_fixed_advance_pc:
19771 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19774 state_machine.handle_fixed_advance_pc (addr_adj);
19779 /* Unknown standard opcode, ignore it. */
19782 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19784 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19785 line_ptr += bytes_read;
19792 dwarf2_debug_line_missing_end_sequence_complaint ();
19794 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19795 in which case we still finish recording the last line). */
19796 state_machine.record_line (true);
19800 /* Decode the Line Number Program (LNP) for the given line_header
19801 structure and CU. The actual information extracted and the type
19802 of structures created from the LNP depends on the value of PST.
19804 1. If PST is NULL, then this procedure uses the data from the program
19805 to create all necessary symbol tables, and their linetables.
19807 2. If PST is not NULL, this procedure reads the program to determine
19808 the list of files included by the unit represented by PST, and
19809 builds all the associated partial symbol tables.
19811 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19812 It is used for relative paths in the line table.
19813 NOTE: When processing partial symtabs (pst != NULL),
19814 comp_dir == pst->dirname.
19816 NOTE: It is important that psymtabs have the same file name (via strcmp)
19817 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19818 symtab we don't use it in the name of the psymtabs we create.
19819 E.g. expand_line_sal requires this when finding psymtabs to expand.
19820 A good testcase for this is mb-inline.exp.
19822 LOWPC is the lowest address in CU (or 0 if not known).
19824 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19825 for its PC<->lines mapping information. Otherwise only the filename
19826 table is read in. */
19829 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19830 struct dwarf2_cu *cu, struct partial_symtab *pst,
19831 CORE_ADDR lowpc, int decode_mapping)
19833 struct objfile *objfile = cu->objfile;
19834 const int decode_for_pst_p = (pst != NULL);
19836 if (decode_mapping)
19837 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19839 if (decode_for_pst_p)
19843 /* Now that we're done scanning the Line Header Program, we can
19844 create the psymtab of each included file. */
19845 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
19846 if (lh->file_names[file_index].included_p == 1)
19848 const char *include_name =
19849 psymtab_include_file_name (lh, file_index, pst, comp_dir);
19850 if (include_name != NULL)
19851 dwarf2_create_include_psymtab (include_name, pst, objfile);
19856 /* Make sure a symtab is created for every file, even files
19857 which contain only variables (i.e. no code with associated
19859 struct compunit_symtab *cust = buildsym_compunit_symtab ();
19862 for (i = 0; i < lh->file_names.size (); i++)
19864 file_entry &fe = lh->file_names[i];
19866 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
19868 if (current_subfile->symtab == NULL)
19870 current_subfile->symtab
19871 = allocate_symtab (cust, current_subfile->name);
19873 fe.symtab = current_subfile->symtab;
19878 /* Start a subfile for DWARF. FILENAME is the name of the file and
19879 DIRNAME the name of the source directory which contains FILENAME
19880 or NULL if not known.
19881 This routine tries to keep line numbers from identical absolute and
19882 relative file names in a common subfile.
19884 Using the `list' example from the GDB testsuite, which resides in
19885 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19886 of /srcdir/list0.c yields the following debugging information for list0.c:
19888 DW_AT_name: /srcdir/list0.c
19889 DW_AT_comp_dir: /compdir
19890 files.files[0].name: list0.h
19891 files.files[0].dir: /srcdir
19892 files.files[1].name: list0.c
19893 files.files[1].dir: /srcdir
19895 The line number information for list0.c has to end up in a single
19896 subfile, so that `break /srcdir/list0.c:1' works as expected.
19897 start_subfile will ensure that this happens provided that we pass the
19898 concatenation of files.files[1].dir and files.files[1].name as the
19902 dwarf2_start_subfile (const char *filename, const char *dirname)
19906 /* In order not to lose the line information directory,
19907 we concatenate it to the filename when it makes sense.
19908 Note that the Dwarf3 standard says (speaking of filenames in line
19909 information): ``The directory index is ignored for file names
19910 that represent full path names''. Thus ignoring dirname in the
19911 `else' branch below isn't an issue. */
19913 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19915 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
19919 start_subfile (filename);
19925 /* Start a symtab for DWARF.
19926 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
19928 static struct compunit_symtab *
19929 dwarf2_start_symtab (struct dwarf2_cu *cu,
19930 const char *name, const char *comp_dir, CORE_ADDR low_pc)
19932 struct compunit_symtab *cust
19933 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
19935 record_debugformat ("DWARF 2");
19936 record_producer (cu->producer);
19938 /* We assume that we're processing GCC output. */
19939 processing_gcc_compilation = 2;
19941 cu->processing_has_namespace_info = 0;
19947 var_decode_location (struct attribute *attr, struct symbol *sym,
19948 struct dwarf2_cu *cu)
19950 struct objfile *objfile = cu->objfile;
19951 struct comp_unit_head *cu_header = &cu->header;
19953 /* NOTE drow/2003-01-30: There used to be a comment and some special
19954 code here to turn a symbol with DW_AT_external and a
19955 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19956 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19957 with some versions of binutils) where shared libraries could have
19958 relocations against symbols in their debug information - the
19959 minimal symbol would have the right address, but the debug info
19960 would not. It's no longer necessary, because we will explicitly
19961 apply relocations when we read in the debug information now. */
19963 /* A DW_AT_location attribute with no contents indicates that a
19964 variable has been optimized away. */
19965 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19967 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19971 /* Handle one degenerate form of location expression specially, to
19972 preserve GDB's previous behavior when section offsets are
19973 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19974 then mark this symbol as LOC_STATIC. */
19976 if (attr_form_is_block (attr)
19977 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19978 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19979 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19980 && (DW_BLOCK (attr)->size
19981 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19983 unsigned int dummy;
19985 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19986 SYMBOL_VALUE_ADDRESS (sym) =
19987 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19989 SYMBOL_VALUE_ADDRESS (sym) =
19990 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
19991 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19992 fixup_symbol_section (sym, objfile);
19993 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19994 SYMBOL_SECTION (sym));
19998 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19999 expression evaluator, and use LOC_COMPUTED only when necessary
20000 (i.e. when the value of a register or memory location is
20001 referenced, or a thread-local block, etc.). Then again, it might
20002 not be worthwhile. I'm assuming that it isn't unless performance
20003 or memory numbers show me otherwise. */
20005 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20007 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20008 cu->has_loclist = 1;
20011 /* Given a pointer to a DWARF information entry, figure out if we need
20012 to make a symbol table entry for it, and if so, create a new entry
20013 and return a pointer to it.
20014 If TYPE is NULL, determine symbol type from the die, otherwise
20015 used the passed type.
20016 If SPACE is not NULL, use it to hold the new symbol. If it is
20017 NULL, allocate a new symbol on the objfile's obstack. */
20019 static struct symbol *
20020 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20021 struct symbol *space)
20023 struct objfile *objfile = cu->objfile;
20024 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20025 struct symbol *sym = NULL;
20027 struct attribute *attr = NULL;
20028 struct attribute *attr2 = NULL;
20029 CORE_ADDR baseaddr;
20030 struct pending **list_to_add = NULL;
20032 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20036 name = dwarf2_name (die, cu);
20039 const char *linkagename;
20040 int suppress_add = 0;
20045 sym = allocate_symbol (objfile);
20046 OBJSTAT (objfile, n_syms++);
20048 /* Cache this symbol's name and the name's demangled form (if any). */
20049 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
20050 linkagename = dwarf2_physname (name, die, cu);
20051 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
20053 /* Fortran does not have mangling standard and the mangling does differ
20054 between gfortran, iFort etc. */
20055 if (cu->language == language_fortran
20056 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
20057 symbol_set_demangled_name (&(sym->ginfo),
20058 dwarf2_full_name (name, die, cu),
20061 /* Default assumptions.
20062 Use the passed type or decode it from the die. */
20063 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20064 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20066 SYMBOL_TYPE (sym) = type;
20068 SYMBOL_TYPE (sym) = die_type (die, cu);
20069 attr = dwarf2_attr (die,
20070 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20074 SYMBOL_LINE (sym) = DW_UNSND (attr);
20077 attr = dwarf2_attr (die,
20078 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20082 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20083 struct file_entry *fe;
20085 if (cu->line_header != NULL)
20086 fe = cu->line_header->file_name_at (file_index);
20091 complaint (&symfile_complaints,
20092 _("file index out of range"));
20094 symbol_set_symtab (sym, fe->symtab);
20100 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20105 addr = attr_value_as_address (attr);
20106 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20107 SYMBOL_VALUE_ADDRESS (sym) = addr;
20109 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20110 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20111 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20112 add_symbol_to_list (sym, cu->list_in_scope);
20114 case DW_TAG_subprogram:
20115 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20117 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20118 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20119 if ((attr2 && (DW_UNSND (attr2) != 0))
20120 || cu->language == language_ada)
20122 /* Subprograms marked external are stored as a global symbol.
20123 Ada subprograms, whether marked external or not, are always
20124 stored as a global symbol, because we want to be able to
20125 access them globally. For instance, we want to be able
20126 to break on a nested subprogram without having to
20127 specify the context. */
20128 list_to_add = &global_symbols;
20132 list_to_add = cu->list_in_scope;
20135 case DW_TAG_inlined_subroutine:
20136 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20138 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20139 SYMBOL_INLINED (sym) = 1;
20140 list_to_add = cu->list_in_scope;
20142 case DW_TAG_template_value_param:
20144 /* Fall through. */
20145 case DW_TAG_constant:
20146 case DW_TAG_variable:
20147 case DW_TAG_member:
20148 /* Compilation with minimal debug info may result in
20149 variables with missing type entries. Change the
20150 misleading `void' type to something sensible. */
20151 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20152 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20154 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20155 /* In the case of DW_TAG_member, we should only be called for
20156 static const members. */
20157 if (die->tag == DW_TAG_member)
20159 /* dwarf2_add_field uses die_is_declaration,
20160 so we do the same. */
20161 gdb_assert (die_is_declaration (die, cu));
20166 dwarf2_const_value (attr, sym, cu);
20167 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20170 if (attr2 && (DW_UNSND (attr2) != 0))
20171 list_to_add = &global_symbols;
20173 list_to_add = cu->list_in_scope;
20177 attr = dwarf2_attr (die, DW_AT_location, cu);
20180 var_decode_location (attr, sym, cu);
20181 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20183 /* Fortran explicitly imports any global symbols to the local
20184 scope by DW_TAG_common_block. */
20185 if (cu->language == language_fortran && die->parent
20186 && die->parent->tag == DW_TAG_common_block)
20189 if (SYMBOL_CLASS (sym) == LOC_STATIC
20190 && SYMBOL_VALUE_ADDRESS (sym) == 0
20191 && !dwarf2_per_objfile->has_section_at_zero)
20193 /* When a static variable is eliminated by the linker,
20194 the corresponding debug information is not stripped
20195 out, but the variable address is set to null;
20196 do not add such variables into symbol table. */
20198 else if (attr2 && (DW_UNSND (attr2) != 0))
20200 /* Workaround gfortran PR debug/40040 - it uses
20201 DW_AT_location for variables in -fPIC libraries which may
20202 get overriden by other libraries/executable and get
20203 a different address. Resolve it by the minimal symbol
20204 which may come from inferior's executable using copy
20205 relocation. Make this workaround only for gfortran as for
20206 other compilers GDB cannot guess the minimal symbol
20207 Fortran mangling kind. */
20208 if (cu->language == language_fortran && die->parent
20209 && die->parent->tag == DW_TAG_module
20211 && startswith (cu->producer, "GNU Fortran"))
20212 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20214 /* A variable with DW_AT_external is never static,
20215 but it may be block-scoped. */
20216 list_to_add = (cu->list_in_scope == &file_symbols
20217 ? &global_symbols : cu->list_in_scope);
20220 list_to_add = cu->list_in_scope;
20224 /* We do not know the address of this symbol.
20225 If it is an external symbol and we have type information
20226 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20227 The address of the variable will then be determined from
20228 the minimal symbol table whenever the variable is
20230 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20232 /* Fortran explicitly imports any global symbols to the local
20233 scope by DW_TAG_common_block. */
20234 if (cu->language == language_fortran && die->parent
20235 && die->parent->tag == DW_TAG_common_block)
20237 /* SYMBOL_CLASS doesn't matter here because
20238 read_common_block is going to reset it. */
20240 list_to_add = cu->list_in_scope;
20242 else if (attr2 && (DW_UNSND (attr2) != 0)
20243 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20245 /* A variable with DW_AT_external is never static, but it
20246 may be block-scoped. */
20247 list_to_add = (cu->list_in_scope == &file_symbols
20248 ? &global_symbols : cu->list_in_scope);
20250 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20252 else if (!die_is_declaration (die, cu))
20254 /* Use the default LOC_OPTIMIZED_OUT class. */
20255 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20257 list_to_add = cu->list_in_scope;
20261 case DW_TAG_formal_parameter:
20262 /* If we are inside a function, mark this as an argument. If
20263 not, we might be looking at an argument to an inlined function
20264 when we do not have enough information to show inlined frames;
20265 pretend it's a local variable in that case so that the user can
20267 if (context_stack_depth > 0
20268 && context_stack[context_stack_depth - 1].name != NULL)
20269 SYMBOL_IS_ARGUMENT (sym) = 1;
20270 attr = dwarf2_attr (die, DW_AT_location, cu);
20273 var_decode_location (attr, sym, cu);
20275 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20278 dwarf2_const_value (attr, sym, cu);
20281 list_to_add = cu->list_in_scope;
20283 case DW_TAG_unspecified_parameters:
20284 /* From varargs functions; gdb doesn't seem to have any
20285 interest in this information, so just ignore it for now.
20288 case DW_TAG_template_type_param:
20290 /* Fall through. */
20291 case DW_TAG_class_type:
20292 case DW_TAG_interface_type:
20293 case DW_TAG_structure_type:
20294 case DW_TAG_union_type:
20295 case DW_TAG_set_type:
20296 case DW_TAG_enumeration_type:
20297 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20298 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20301 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20302 really ever be static objects: otherwise, if you try
20303 to, say, break of a class's method and you're in a file
20304 which doesn't mention that class, it won't work unless
20305 the check for all static symbols in lookup_symbol_aux
20306 saves you. See the OtherFileClass tests in
20307 gdb.c++/namespace.exp. */
20311 list_to_add = (cu->list_in_scope == &file_symbols
20312 && cu->language == language_cplus
20313 ? &global_symbols : cu->list_in_scope);
20315 /* The semantics of C++ state that "struct foo {
20316 ... }" also defines a typedef for "foo". */
20317 if (cu->language == language_cplus
20318 || cu->language == language_ada
20319 || cu->language == language_d
20320 || cu->language == language_rust)
20322 /* The symbol's name is already allocated along
20323 with this objfile, so we don't need to
20324 duplicate it for the type. */
20325 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20326 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
20331 case DW_TAG_typedef:
20332 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20333 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20334 list_to_add = cu->list_in_scope;
20336 case DW_TAG_base_type:
20337 case DW_TAG_subrange_type:
20338 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20339 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20340 list_to_add = cu->list_in_scope;
20342 case DW_TAG_enumerator:
20343 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20346 dwarf2_const_value (attr, sym, cu);
20349 /* NOTE: carlton/2003-11-10: See comment above in the
20350 DW_TAG_class_type, etc. block. */
20352 list_to_add = (cu->list_in_scope == &file_symbols
20353 && cu->language == language_cplus
20354 ? &global_symbols : cu->list_in_scope);
20357 case DW_TAG_imported_declaration:
20358 case DW_TAG_namespace:
20359 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20360 list_to_add = &global_symbols;
20362 case DW_TAG_module:
20363 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20364 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20365 list_to_add = &global_symbols;
20367 case DW_TAG_common_block:
20368 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20369 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20370 add_symbol_to_list (sym, cu->list_in_scope);
20373 /* Not a tag we recognize. Hopefully we aren't processing
20374 trash data, but since we must specifically ignore things
20375 we don't recognize, there is nothing else we should do at
20377 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
20378 dwarf_tag_name (die->tag));
20384 sym->hash_next = objfile->template_symbols;
20385 objfile->template_symbols = sym;
20386 list_to_add = NULL;
20389 if (list_to_add != NULL)
20390 add_symbol_to_list (sym, list_to_add);
20392 /* For the benefit of old versions of GCC, check for anonymous
20393 namespaces based on the demangled name. */
20394 if (!cu->processing_has_namespace_info
20395 && cu->language == language_cplus)
20396 cp_scan_for_anonymous_namespaces (sym, objfile);
20401 /* A wrapper for new_symbol_full that always allocates a new symbol. */
20403 static struct symbol *
20404 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20406 return new_symbol_full (die, type, cu, NULL);
20409 /* Given an attr with a DW_FORM_dataN value in host byte order,
20410 zero-extend it as appropriate for the symbol's type. The DWARF
20411 standard (v4) is not entirely clear about the meaning of using
20412 DW_FORM_dataN for a constant with a signed type, where the type is
20413 wider than the data. The conclusion of a discussion on the DWARF
20414 list was that this is unspecified. We choose to always zero-extend
20415 because that is the interpretation long in use by GCC. */
20418 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20419 struct dwarf2_cu *cu, LONGEST *value, int bits)
20421 struct objfile *objfile = cu->objfile;
20422 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20423 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20424 LONGEST l = DW_UNSND (attr);
20426 if (bits < sizeof (*value) * 8)
20428 l &= ((LONGEST) 1 << bits) - 1;
20431 else if (bits == sizeof (*value) * 8)
20435 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20436 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20443 /* Read a constant value from an attribute. Either set *VALUE, or if
20444 the value does not fit in *VALUE, set *BYTES - either already
20445 allocated on the objfile obstack, or newly allocated on OBSTACK,
20446 or, set *BATON, if we translated the constant to a location
20450 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20451 const char *name, struct obstack *obstack,
20452 struct dwarf2_cu *cu,
20453 LONGEST *value, const gdb_byte **bytes,
20454 struct dwarf2_locexpr_baton **baton)
20456 struct objfile *objfile = cu->objfile;
20457 struct comp_unit_head *cu_header = &cu->header;
20458 struct dwarf_block *blk;
20459 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20460 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20466 switch (attr->form)
20469 case DW_FORM_GNU_addr_index:
20473 if (TYPE_LENGTH (type) != cu_header->addr_size)
20474 dwarf2_const_value_length_mismatch_complaint (name,
20475 cu_header->addr_size,
20476 TYPE_LENGTH (type));
20477 /* Symbols of this form are reasonably rare, so we just
20478 piggyback on the existing location code rather than writing
20479 a new implementation of symbol_computed_ops. */
20480 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20481 (*baton)->per_cu = cu->per_cu;
20482 gdb_assert ((*baton)->per_cu);
20484 (*baton)->size = 2 + cu_header->addr_size;
20485 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20486 (*baton)->data = data;
20488 data[0] = DW_OP_addr;
20489 store_unsigned_integer (&data[1], cu_header->addr_size,
20490 byte_order, DW_ADDR (attr));
20491 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20494 case DW_FORM_string:
20496 case DW_FORM_GNU_str_index:
20497 case DW_FORM_GNU_strp_alt:
20498 /* DW_STRING is already allocated on the objfile obstack, point
20500 *bytes = (const gdb_byte *) DW_STRING (attr);
20502 case DW_FORM_block1:
20503 case DW_FORM_block2:
20504 case DW_FORM_block4:
20505 case DW_FORM_block:
20506 case DW_FORM_exprloc:
20507 case DW_FORM_data16:
20508 blk = DW_BLOCK (attr);
20509 if (TYPE_LENGTH (type) != blk->size)
20510 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20511 TYPE_LENGTH (type));
20512 *bytes = blk->data;
20515 /* The DW_AT_const_value attributes are supposed to carry the
20516 symbol's value "represented as it would be on the target
20517 architecture." By the time we get here, it's already been
20518 converted to host endianness, so we just need to sign- or
20519 zero-extend it as appropriate. */
20520 case DW_FORM_data1:
20521 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20523 case DW_FORM_data2:
20524 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20526 case DW_FORM_data4:
20527 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20529 case DW_FORM_data8:
20530 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20533 case DW_FORM_sdata:
20534 case DW_FORM_implicit_const:
20535 *value = DW_SND (attr);
20538 case DW_FORM_udata:
20539 *value = DW_UNSND (attr);
20543 complaint (&symfile_complaints,
20544 _("unsupported const value attribute form: '%s'"),
20545 dwarf_form_name (attr->form));
20552 /* Copy constant value from an attribute to a symbol. */
20555 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20556 struct dwarf2_cu *cu)
20558 struct objfile *objfile = cu->objfile;
20560 const gdb_byte *bytes;
20561 struct dwarf2_locexpr_baton *baton;
20563 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20564 SYMBOL_PRINT_NAME (sym),
20565 &objfile->objfile_obstack, cu,
20566 &value, &bytes, &baton);
20570 SYMBOL_LOCATION_BATON (sym) = baton;
20571 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20573 else if (bytes != NULL)
20575 SYMBOL_VALUE_BYTES (sym) = bytes;
20576 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20580 SYMBOL_VALUE (sym) = value;
20581 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20585 /* Return the type of the die in question using its DW_AT_type attribute. */
20587 static struct type *
20588 die_type (struct die_info *die, struct dwarf2_cu *cu)
20590 struct attribute *type_attr;
20592 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20595 /* A missing DW_AT_type represents a void type. */
20596 return objfile_type (cu->objfile)->builtin_void;
20599 return lookup_die_type (die, type_attr, cu);
20602 /* True iff CU's producer generates GNAT Ada auxiliary information
20603 that allows to find parallel types through that information instead
20604 of having to do expensive parallel lookups by type name. */
20607 need_gnat_info (struct dwarf2_cu *cu)
20609 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
20610 of GNAT produces this auxiliary information, without any indication
20611 that it is produced. Part of enhancing the FSF version of GNAT
20612 to produce that information will be to put in place an indicator
20613 that we can use in order to determine whether the descriptive type
20614 info is available or not. One suggestion that has been made is
20615 to use a new attribute, attached to the CU die. For now, assume
20616 that the descriptive type info is not available. */
20620 /* Return the auxiliary type of the die in question using its
20621 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20622 attribute is not present. */
20624 static struct type *
20625 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20627 struct attribute *type_attr;
20629 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20633 return lookup_die_type (die, type_attr, cu);
20636 /* If DIE has a descriptive_type attribute, then set the TYPE's
20637 descriptive type accordingly. */
20640 set_descriptive_type (struct type *type, struct die_info *die,
20641 struct dwarf2_cu *cu)
20643 struct type *descriptive_type = die_descriptive_type (die, cu);
20645 if (descriptive_type)
20647 ALLOCATE_GNAT_AUX_TYPE (type);
20648 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20652 /* Return the containing type of the die in question using its
20653 DW_AT_containing_type attribute. */
20655 static struct type *
20656 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20658 struct attribute *type_attr;
20660 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20662 error (_("Dwarf Error: Problem turning containing type into gdb type "
20663 "[in module %s]"), objfile_name (cu->objfile));
20665 return lookup_die_type (die, type_attr, cu);
20668 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20670 static struct type *
20671 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20673 struct objfile *objfile = dwarf2_per_objfile->objfile;
20674 char *message, *saved;
20676 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
20677 objfile_name (objfile),
20678 to_underlying (cu->header.sect_off),
20679 to_underlying (die->sect_off));
20680 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
20681 message, strlen (message));
20684 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20687 /* Look up the type of DIE in CU using its type attribute ATTR.
20688 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20689 DW_AT_containing_type.
20690 If there is no type substitute an error marker. */
20692 static struct type *
20693 lookup_die_type (struct die_info *die, const struct attribute *attr,
20694 struct dwarf2_cu *cu)
20696 struct objfile *objfile = cu->objfile;
20697 struct type *this_type;
20699 gdb_assert (attr->name == DW_AT_type
20700 || attr->name == DW_AT_GNAT_descriptive_type
20701 || attr->name == DW_AT_containing_type);
20703 /* First see if we have it cached. */
20705 if (attr->form == DW_FORM_GNU_ref_alt)
20707 struct dwarf2_per_cu_data *per_cu;
20708 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20710 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
20711 this_type = get_die_type_at_offset (sect_off, per_cu);
20713 else if (attr_form_is_ref (attr))
20715 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20717 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20719 else if (attr->form == DW_FORM_ref_sig8)
20721 ULONGEST signature = DW_SIGNATURE (attr);
20723 return get_signatured_type (die, signature, cu);
20727 complaint (&symfile_complaints,
20728 _("Dwarf Error: Bad type attribute %s in DIE"
20729 " at 0x%x [in module %s]"),
20730 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
20731 objfile_name (objfile));
20732 return build_error_marker_type (cu, die);
20735 /* If not cached we need to read it in. */
20737 if (this_type == NULL)
20739 struct die_info *type_die = NULL;
20740 struct dwarf2_cu *type_cu = cu;
20742 if (attr_form_is_ref (attr))
20743 type_die = follow_die_ref (die, attr, &type_cu);
20744 if (type_die == NULL)
20745 return build_error_marker_type (cu, die);
20746 /* If we find the type now, it's probably because the type came
20747 from an inter-CU reference and the type's CU got expanded before
20749 this_type = read_type_die (type_die, type_cu);
20752 /* If we still don't have a type use an error marker. */
20754 if (this_type == NULL)
20755 return build_error_marker_type (cu, die);
20760 /* Return the type in DIE, CU.
20761 Returns NULL for invalid types.
20763 This first does a lookup in die_type_hash,
20764 and only reads the die in if necessary.
20766 NOTE: This can be called when reading in partial or full symbols. */
20768 static struct type *
20769 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20771 struct type *this_type;
20773 this_type = get_die_type (die, cu);
20777 return read_type_die_1 (die, cu);
20780 /* Read the type in DIE, CU.
20781 Returns NULL for invalid types. */
20783 static struct type *
20784 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20786 struct type *this_type = NULL;
20790 case DW_TAG_class_type:
20791 case DW_TAG_interface_type:
20792 case DW_TAG_structure_type:
20793 case DW_TAG_union_type:
20794 this_type = read_structure_type (die, cu);
20796 case DW_TAG_enumeration_type:
20797 this_type = read_enumeration_type (die, cu);
20799 case DW_TAG_subprogram:
20800 case DW_TAG_subroutine_type:
20801 case DW_TAG_inlined_subroutine:
20802 this_type = read_subroutine_type (die, cu);
20804 case DW_TAG_array_type:
20805 this_type = read_array_type (die, cu);
20807 case DW_TAG_set_type:
20808 this_type = read_set_type (die, cu);
20810 case DW_TAG_pointer_type:
20811 this_type = read_tag_pointer_type (die, cu);
20813 case DW_TAG_ptr_to_member_type:
20814 this_type = read_tag_ptr_to_member_type (die, cu);
20816 case DW_TAG_reference_type:
20817 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20819 case DW_TAG_rvalue_reference_type:
20820 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20822 case DW_TAG_const_type:
20823 this_type = read_tag_const_type (die, cu);
20825 case DW_TAG_volatile_type:
20826 this_type = read_tag_volatile_type (die, cu);
20828 case DW_TAG_restrict_type:
20829 this_type = read_tag_restrict_type (die, cu);
20831 case DW_TAG_string_type:
20832 this_type = read_tag_string_type (die, cu);
20834 case DW_TAG_typedef:
20835 this_type = read_typedef (die, cu);
20837 case DW_TAG_subrange_type:
20838 this_type = read_subrange_type (die, cu);
20840 case DW_TAG_base_type:
20841 this_type = read_base_type (die, cu);
20843 case DW_TAG_unspecified_type:
20844 this_type = read_unspecified_type (die, cu);
20846 case DW_TAG_namespace:
20847 this_type = read_namespace_type (die, cu);
20849 case DW_TAG_module:
20850 this_type = read_module_type (die, cu);
20852 case DW_TAG_atomic_type:
20853 this_type = read_tag_atomic_type (die, cu);
20856 complaint (&symfile_complaints,
20857 _("unexpected tag in read_type_die: '%s'"),
20858 dwarf_tag_name (die->tag));
20865 /* See if we can figure out if the class lives in a namespace. We do
20866 this by looking for a member function; its demangled name will
20867 contain namespace info, if there is any.
20868 Return the computed name or NULL.
20869 Space for the result is allocated on the objfile's obstack.
20870 This is the full-die version of guess_partial_die_structure_name.
20871 In this case we know DIE has no useful parent. */
20874 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20876 struct die_info *spec_die;
20877 struct dwarf2_cu *spec_cu;
20878 struct die_info *child;
20881 spec_die = die_specification (die, &spec_cu);
20882 if (spec_die != NULL)
20888 for (child = die->child;
20890 child = child->sibling)
20892 if (child->tag == DW_TAG_subprogram)
20894 const char *linkage_name = dw2_linkage_name (child, cu);
20896 if (linkage_name != NULL)
20899 = language_class_name_from_physname (cu->language_defn,
20903 if (actual_name != NULL)
20905 const char *die_name = dwarf2_name (die, cu);
20907 if (die_name != NULL
20908 && strcmp (die_name, actual_name) != 0)
20910 /* Strip off the class name from the full name.
20911 We want the prefix. */
20912 int die_name_len = strlen (die_name);
20913 int actual_name_len = strlen (actual_name);
20915 /* Test for '::' as a sanity check. */
20916 if (actual_name_len > die_name_len + 2
20917 && actual_name[actual_name_len
20918 - die_name_len - 1] == ':')
20919 name = (char *) obstack_copy0 (
20920 &cu->objfile->per_bfd->storage_obstack,
20921 actual_name, actual_name_len - die_name_len - 2);
20924 xfree (actual_name);
20933 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20934 prefix part in such case. See
20935 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20937 static const char *
20938 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20940 struct attribute *attr;
20943 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20944 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20947 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20950 attr = dw2_linkage_name_attr (die, cu);
20951 if (attr == NULL || DW_STRING (attr) == NULL)
20954 /* dwarf2_name had to be already called. */
20955 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20957 /* Strip the base name, keep any leading namespaces/classes. */
20958 base = strrchr (DW_STRING (attr), ':');
20959 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20962 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20964 &base[-1] - DW_STRING (attr));
20967 /* Return the name of the namespace/class that DIE is defined within,
20968 or "" if we can't tell. The caller should not xfree the result.
20970 For example, if we're within the method foo() in the following
20980 then determine_prefix on foo's die will return "N::C". */
20982 static const char *
20983 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20985 struct die_info *parent, *spec_die;
20986 struct dwarf2_cu *spec_cu;
20987 struct type *parent_type;
20988 const char *retval;
20990 if (cu->language != language_cplus
20991 && cu->language != language_fortran && cu->language != language_d
20992 && cu->language != language_rust)
20995 retval = anonymous_struct_prefix (die, cu);
20999 /* We have to be careful in the presence of DW_AT_specification.
21000 For example, with GCC 3.4, given the code
21004 // Definition of N::foo.
21008 then we'll have a tree of DIEs like this:
21010 1: DW_TAG_compile_unit
21011 2: DW_TAG_namespace // N
21012 3: DW_TAG_subprogram // declaration of N::foo
21013 4: DW_TAG_subprogram // definition of N::foo
21014 DW_AT_specification // refers to die #3
21016 Thus, when processing die #4, we have to pretend that we're in
21017 the context of its DW_AT_specification, namely the contex of die
21020 spec_die = die_specification (die, &spec_cu);
21021 if (spec_die == NULL)
21022 parent = die->parent;
21025 parent = spec_die->parent;
21029 if (parent == NULL)
21031 else if (parent->building_fullname)
21034 const char *parent_name;
21036 /* It has been seen on RealView 2.2 built binaries,
21037 DW_TAG_template_type_param types actually _defined_ as
21038 children of the parent class:
21041 template class <class Enum> Class{};
21042 Class<enum E> class_e;
21044 1: DW_TAG_class_type (Class)
21045 2: DW_TAG_enumeration_type (E)
21046 3: DW_TAG_enumerator (enum1:0)
21047 3: DW_TAG_enumerator (enum2:1)
21049 2: DW_TAG_template_type_param
21050 DW_AT_type DW_FORM_ref_udata (E)
21052 Besides being broken debug info, it can put GDB into an
21053 infinite loop. Consider:
21055 When we're building the full name for Class<E>, we'll start
21056 at Class, and go look over its template type parameters,
21057 finding E. We'll then try to build the full name of E, and
21058 reach here. We're now trying to build the full name of E,
21059 and look over the parent DIE for containing scope. In the
21060 broken case, if we followed the parent DIE of E, we'd again
21061 find Class, and once again go look at its template type
21062 arguments, etc., etc. Simply don't consider such parent die
21063 as source-level parent of this die (it can't be, the language
21064 doesn't allow it), and break the loop here. */
21065 name = dwarf2_name (die, cu);
21066 parent_name = dwarf2_name (parent, cu);
21067 complaint (&symfile_complaints,
21068 _("template param type '%s' defined within parent '%s'"),
21069 name ? name : "<unknown>",
21070 parent_name ? parent_name : "<unknown>");
21074 switch (parent->tag)
21076 case DW_TAG_namespace:
21077 parent_type = read_type_die (parent, cu);
21078 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21079 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21080 Work around this problem here. */
21081 if (cu->language == language_cplus
21082 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
21084 /* We give a name to even anonymous namespaces. */
21085 return TYPE_TAG_NAME (parent_type);
21086 case DW_TAG_class_type:
21087 case DW_TAG_interface_type:
21088 case DW_TAG_structure_type:
21089 case DW_TAG_union_type:
21090 case DW_TAG_module:
21091 parent_type = read_type_die (parent, cu);
21092 if (TYPE_TAG_NAME (parent_type) != NULL)
21093 return TYPE_TAG_NAME (parent_type);
21095 /* An anonymous structure is only allowed non-static data
21096 members; no typedefs, no member functions, et cetera.
21097 So it does not need a prefix. */
21099 case DW_TAG_compile_unit:
21100 case DW_TAG_partial_unit:
21101 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21102 if (cu->language == language_cplus
21103 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
21104 && die->child != NULL
21105 && (die->tag == DW_TAG_class_type
21106 || die->tag == DW_TAG_structure_type
21107 || die->tag == DW_TAG_union_type))
21109 char *name = guess_full_die_structure_name (die, cu);
21114 case DW_TAG_enumeration_type:
21115 parent_type = read_type_die (parent, cu);
21116 if (TYPE_DECLARED_CLASS (parent_type))
21118 if (TYPE_TAG_NAME (parent_type) != NULL)
21119 return TYPE_TAG_NAME (parent_type);
21122 /* Fall through. */
21124 return determine_prefix (parent, cu);
21128 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21129 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21130 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21131 an obconcat, otherwise allocate storage for the result. The CU argument is
21132 used to determine the language and hence, the appropriate separator. */
21134 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21137 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21138 int physname, struct dwarf2_cu *cu)
21140 const char *lead = "";
21143 if (suffix == NULL || suffix[0] == '\0'
21144 || prefix == NULL || prefix[0] == '\0')
21146 else if (cu->language == language_d)
21148 /* For D, the 'main' function could be defined in any module, but it
21149 should never be prefixed. */
21150 if (strcmp (suffix, "D main") == 0)
21158 else if (cu->language == language_fortran && physname)
21160 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21161 DW_AT_MIPS_linkage_name is preferred and used instead. */
21169 if (prefix == NULL)
21171 if (suffix == NULL)
21178 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21180 strcpy (retval, lead);
21181 strcat (retval, prefix);
21182 strcat (retval, sep);
21183 strcat (retval, suffix);
21188 /* We have an obstack. */
21189 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21193 /* Return sibling of die, NULL if no sibling. */
21195 static struct die_info *
21196 sibling_die (struct die_info *die)
21198 return die->sibling;
21201 /* Get name of a die, return NULL if not found. */
21203 static const char *
21204 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21205 struct obstack *obstack)
21207 if (name && cu->language == language_cplus)
21209 std::string canon_name = cp_canonicalize_string (name);
21211 if (!canon_name.empty ())
21213 if (canon_name != name)
21214 name = (const char *) obstack_copy0 (obstack,
21215 canon_name.c_str (),
21216 canon_name.length ());
21223 /* Get name of a die, return NULL if not found.
21224 Anonymous namespaces are converted to their magic string. */
21226 static const char *
21227 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21229 struct attribute *attr;
21231 attr = dwarf2_attr (die, DW_AT_name, cu);
21232 if ((!attr || !DW_STRING (attr))
21233 && die->tag != DW_TAG_namespace
21234 && die->tag != DW_TAG_class_type
21235 && die->tag != DW_TAG_interface_type
21236 && die->tag != DW_TAG_structure_type
21237 && die->tag != DW_TAG_union_type)
21242 case DW_TAG_compile_unit:
21243 case DW_TAG_partial_unit:
21244 /* Compilation units have a DW_AT_name that is a filename, not
21245 a source language identifier. */
21246 case DW_TAG_enumeration_type:
21247 case DW_TAG_enumerator:
21248 /* These tags always have simple identifiers already; no need
21249 to canonicalize them. */
21250 return DW_STRING (attr);
21252 case DW_TAG_namespace:
21253 if (attr != NULL && DW_STRING (attr) != NULL)
21254 return DW_STRING (attr);
21255 return CP_ANONYMOUS_NAMESPACE_STR;
21257 case DW_TAG_class_type:
21258 case DW_TAG_interface_type:
21259 case DW_TAG_structure_type:
21260 case DW_TAG_union_type:
21261 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21262 structures or unions. These were of the form "._%d" in GCC 4.1,
21263 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21264 and GCC 4.4. We work around this problem by ignoring these. */
21265 if (attr && DW_STRING (attr)
21266 && (startswith (DW_STRING (attr), "._")
21267 || startswith (DW_STRING (attr), "<anonymous")))
21270 /* GCC might emit a nameless typedef that has a linkage name. See
21271 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21272 if (!attr || DW_STRING (attr) == NULL)
21274 char *demangled = NULL;
21276 attr = dw2_linkage_name_attr (die, cu);
21277 if (attr == NULL || DW_STRING (attr) == NULL)
21280 /* Avoid demangling DW_STRING (attr) the second time on a second
21281 call for the same DIE. */
21282 if (!DW_STRING_IS_CANONICAL (attr))
21283 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
21289 /* FIXME: we already did this for the partial symbol... */
21292 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
21293 demangled, strlen (demangled)));
21294 DW_STRING_IS_CANONICAL (attr) = 1;
21297 /* Strip any leading namespaces/classes, keep only the base name.
21298 DW_AT_name for named DIEs does not contain the prefixes. */
21299 base = strrchr (DW_STRING (attr), ':');
21300 if (base && base > DW_STRING (attr) && base[-1] == ':')
21303 return DW_STRING (attr);
21312 if (!DW_STRING_IS_CANONICAL (attr))
21315 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21316 &cu->objfile->per_bfd->storage_obstack);
21317 DW_STRING_IS_CANONICAL (attr) = 1;
21319 return DW_STRING (attr);
21322 /* Return the die that this die in an extension of, or NULL if there
21323 is none. *EXT_CU is the CU containing DIE on input, and the CU
21324 containing the return value on output. */
21326 static struct die_info *
21327 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21329 struct attribute *attr;
21331 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21335 return follow_die_ref (die, attr, ext_cu);
21338 /* Convert a DIE tag into its string name. */
21340 static const char *
21341 dwarf_tag_name (unsigned tag)
21343 const char *name = get_DW_TAG_name (tag);
21346 return "DW_TAG_<unknown>";
21351 /* Convert a DWARF attribute code into its string name. */
21353 static const char *
21354 dwarf_attr_name (unsigned attr)
21358 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21359 if (attr == DW_AT_MIPS_fde)
21360 return "DW_AT_MIPS_fde";
21362 if (attr == DW_AT_HP_block_index)
21363 return "DW_AT_HP_block_index";
21366 name = get_DW_AT_name (attr);
21369 return "DW_AT_<unknown>";
21374 /* Convert a DWARF value form code into its string name. */
21376 static const char *
21377 dwarf_form_name (unsigned form)
21379 const char *name = get_DW_FORM_name (form);
21382 return "DW_FORM_<unknown>";
21387 static const char *
21388 dwarf_bool_name (unsigned mybool)
21396 /* Convert a DWARF type code into its string name. */
21398 static const char *
21399 dwarf_type_encoding_name (unsigned enc)
21401 const char *name = get_DW_ATE_name (enc);
21404 return "DW_ATE_<unknown>";
21410 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21414 print_spaces (indent, f);
21415 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
21416 dwarf_tag_name (die->tag), die->abbrev,
21417 to_underlying (die->sect_off));
21419 if (die->parent != NULL)
21421 print_spaces (indent, f);
21422 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
21423 to_underlying (die->parent->sect_off));
21426 print_spaces (indent, f);
21427 fprintf_unfiltered (f, " has children: %s\n",
21428 dwarf_bool_name (die->child != NULL));
21430 print_spaces (indent, f);
21431 fprintf_unfiltered (f, " attributes:\n");
21433 for (i = 0; i < die->num_attrs; ++i)
21435 print_spaces (indent, f);
21436 fprintf_unfiltered (f, " %s (%s) ",
21437 dwarf_attr_name (die->attrs[i].name),
21438 dwarf_form_name (die->attrs[i].form));
21440 switch (die->attrs[i].form)
21443 case DW_FORM_GNU_addr_index:
21444 fprintf_unfiltered (f, "address: ");
21445 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21447 case DW_FORM_block2:
21448 case DW_FORM_block4:
21449 case DW_FORM_block:
21450 case DW_FORM_block1:
21451 fprintf_unfiltered (f, "block: size %s",
21452 pulongest (DW_BLOCK (&die->attrs[i])->size));
21454 case DW_FORM_exprloc:
21455 fprintf_unfiltered (f, "expression: size %s",
21456 pulongest (DW_BLOCK (&die->attrs[i])->size));
21458 case DW_FORM_data16:
21459 fprintf_unfiltered (f, "constant of 16 bytes");
21461 case DW_FORM_ref_addr:
21462 fprintf_unfiltered (f, "ref address: ");
21463 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21465 case DW_FORM_GNU_ref_alt:
21466 fprintf_unfiltered (f, "alt ref address: ");
21467 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21473 case DW_FORM_ref_udata:
21474 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21475 (long) (DW_UNSND (&die->attrs[i])));
21477 case DW_FORM_data1:
21478 case DW_FORM_data2:
21479 case DW_FORM_data4:
21480 case DW_FORM_data8:
21481 case DW_FORM_udata:
21482 case DW_FORM_sdata:
21483 fprintf_unfiltered (f, "constant: %s",
21484 pulongest (DW_UNSND (&die->attrs[i])));
21486 case DW_FORM_sec_offset:
21487 fprintf_unfiltered (f, "section offset: %s",
21488 pulongest (DW_UNSND (&die->attrs[i])));
21490 case DW_FORM_ref_sig8:
21491 fprintf_unfiltered (f, "signature: %s",
21492 hex_string (DW_SIGNATURE (&die->attrs[i])));
21494 case DW_FORM_string:
21496 case DW_FORM_line_strp:
21497 case DW_FORM_GNU_str_index:
21498 case DW_FORM_GNU_strp_alt:
21499 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21500 DW_STRING (&die->attrs[i])
21501 ? DW_STRING (&die->attrs[i]) : "",
21502 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21505 if (DW_UNSND (&die->attrs[i]))
21506 fprintf_unfiltered (f, "flag: TRUE");
21508 fprintf_unfiltered (f, "flag: FALSE");
21510 case DW_FORM_flag_present:
21511 fprintf_unfiltered (f, "flag: TRUE");
21513 case DW_FORM_indirect:
21514 /* The reader will have reduced the indirect form to
21515 the "base form" so this form should not occur. */
21516 fprintf_unfiltered (f,
21517 "unexpected attribute form: DW_FORM_indirect");
21519 case DW_FORM_implicit_const:
21520 fprintf_unfiltered (f, "constant: %s",
21521 plongest (DW_SND (&die->attrs[i])));
21524 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21525 die->attrs[i].form);
21528 fprintf_unfiltered (f, "\n");
21533 dump_die_for_error (struct die_info *die)
21535 dump_die_shallow (gdb_stderr, 0, die);
21539 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21541 int indent = level * 4;
21543 gdb_assert (die != NULL);
21545 if (level >= max_level)
21548 dump_die_shallow (f, indent, die);
21550 if (die->child != NULL)
21552 print_spaces (indent, f);
21553 fprintf_unfiltered (f, " Children:");
21554 if (level + 1 < max_level)
21556 fprintf_unfiltered (f, "\n");
21557 dump_die_1 (f, level + 1, max_level, die->child);
21561 fprintf_unfiltered (f,
21562 " [not printed, max nesting level reached]\n");
21566 if (die->sibling != NULL && level > 0)
21568 dump_die_1 (f, level, max_level, die->sibling);
21572 /* This is called from the pdie macro in gdbinit.in.
21573 It's not static so gcc will keep a copy callable from gdb. */
21576 dump_die (struct die_info *die, int max_level)
21578 dump_die_1 (gdb_stdlog, 0, max_level, die);
21582 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21586 slot = htab_find_slot_with_hash (cu->die_hash, die,
21587 to_underlying (die->sect_off),
21593 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21597 dwarf2_get_ref_die_offset (const struct attribute *attr)
21599 if (attr_form_is_ref (attr))
21600 return (sect_offset) DW_UNSND (attr);
21602 complaint (&symfile_complaints,
21603 _("unsupported die ref attribute form: '%s'"),
21604 dwarf_form_name (attr->form));
21608 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21609 * the value held by the attribute is not constant. */
21612 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21614 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21615 return DW_SND (attr);
21616 else if (attr->form == DW_FORM_udata
21617 || attr->form == DW_FORM_data1
21618 || attr->form == DW_FORM_data2
21619 || attr->form == DW_FORM_data4
21620 || attr->form == DW_FORM_data8)
21621 return DW_UNSND (attr);
21624 /* For DW_FORM_data16 see attr_form_is_constant. */
21625 complaint (&symfile_complaints,
21626 _("Attribute value is not a constant (%s)"),
21627 dwarf_form_name (attr->form));
21628 return default_value;
21632 /* Follow reference or signature attribute ATTR of SRC_DIE.
21633 On entry *REF_CU is the CU of SRC_DIE.
21634 On exit *REF_CU is the CU of the result. */
21636 static struct die_info *
21637 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21638 struct dwarf2_cu **ref_cu)
21640 struct die_info *die;
21642 if (attr_form_is_ref (attr))
21643 die = follow_die_ref (src_die, attr, ref_cu);
21644 else if (attr->form == DW_FORM_ref_sig8)
21645 die = follow_die_sig (src_die, attr, ref_cu);
21648 dump_die_for_error (src_die);
21649 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21650 objfile_name ((*ref_cu)->objfile));
21656 /* Follow reference OFFSET.
21657 On entry *REF_CU is the CU of the source die referencing OFFSET.
21658 On exit *REF_CU is the CU of the result.
21659 Returns NULL if OFFSET is invalid. */
21661 static struct die_info *
21662 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21663 struct dwarf2_cu **ref_cu)
21665 struct die_info temp_die;
21666 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21668 gdb_assert (cu->per_cu != NULL);
21672 if (cu->per_cu->is_debug_types)
21674 /* .debug_types CUs cannot reference anything outside their CU.
21675 If they need to, they have to reference a signatured type via
21676 DW_FORM_ref_sig8. */
21677 if (!offset_in_cu_p (&cu->header, sect_off))
21680 else if (offset_in_dwz != cu->per_cu->is_dwz
21681 || !offset_in_cu_p (&cu->header, sect_off))
21683 struct dwarf2_per_cu_data *per_cu;
21685 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21688 /* If necessary, add it to the queue and load its DIEs. */
21689 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21690 load_full_comp_unit (per_cu, cu->language);
21692 target_cu = per_cu->cu;
21694 else if (cu->dies == NULL)
21696 /* We're loading full DIEs during partial symbol reading. */
21697 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21698 load_full_comp_unit (cu->per_cu, language_minimal);
21701 *ref_cu = target_cu;
21702 temp_die.sect_off = sect_off;
21703 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21705 to_underlying (sect_off));
21708 /* Follow reference attribute ATTR of SRC_DIE.
21709 On entry *REF_CU is the CU of SRC_DIE.
21710 On exit *REF_CU is the CU of the result. */
21712 static struct die_info *
21713 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21714 struct dwarf2_cu **ref_cu)
21716 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21717 struct dwarf2_cu *cu = *ref_cu;
21718 struct die_info *die;
21720 die = follow_die_offset (sect_off,
21721 (attr->form == DW_FORM_GNU_ref_alt
21722 || cu->per_cu->is_dwz),
21725 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
21726 "at 0x%x [in module %s]"),
21727 to_underlying (sect_off), to_underlying (src_die->sect_off),
21728 objfile_name (cu->objfile));
21733 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
21734 Returned value is intended for DW_OP_call*. Returned
21735 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
21737 struct dwarf2_locexpr_baton
21738 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21739 struct dwarf2_per_cu_data *per_cu,
21740 CORE_ADDR (*get_frame_pc) (void *baton),
21743 struct dwarf2_cu *cu;
21744 struct die_info *die;
21745 struct attribute *attr;
21746 struct dwarf2_locexpr_baton retval;
21748 dw2_setup (per_cu->objfile);
21750 if (per_cu->cu == NULL)
21755 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21756 Instead just throw an error, not much else we can do. */
21757 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21758 to_underlying (sect_off), objfile_name (per_cu->objfile));
21761 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21763 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21764 to_underlying (sect_off), objfile_name (per_cu->objfile));
21766 attr = dwarf2_attr (die, DW_AT_location, cu);
21769 /* DWARF: "If there is no such attribute, then there is no effect.".
21770 DATA is ignored if SIZE is 0. */
21772 retval.data = NULL;
21775 else if (attr_form_is_section_offset (attr))
21777 struct dwarf2_loclist_baton loclist_baton;
21778 CORE_ADDR pc = (*get_frame_pc) (baton);
21781 fill_in_loclist_baton (cu, &loclist_baton, attr);
21783 retval.data = dwarf2_find_location_expression (&loclist_baton,
21785 retval.size = size;
21789 if (!attr_form_is_block (attr))
21790 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
21791 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21792 to_underlying (sect_off), objfile_name (per_cu->objfile));
21794 retval.data = DW_BLOCK (attr)->data;
21795 retval.size = DW_BLOCK (attr)->size;
21797 retval.per_cu = cu->per_cu;
21799 age_cached_comp_units ();
21804 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
21807 struct dwarf2_locexpr_baton
21808 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21809 struct dwarf2_per_cu_data *per_cu,
21810 CORE_ADDR (*get_frame_pc) (void *baton),
21813 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21815 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21818 /* Write a constant of a given type as target-ordered bytes into
21821 static const gdb_byte *
21822 write_constant_as_bytes (struct obstack *obstack,
21823 enum bfd_endian byte_order,
21830 *len = TYPE_LENGTH (type);
21831 result = (gdb_byte *) obstack_alloc (obstack, *len);
21832 store_unsigned_integer (result, *len, byte_order, value);
21837 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
21838 pointer to the constant bytes and set LEN to the length of the
21839 data. If memory is needed, allocate it on OBSTACK. If the DIE
21840 does not have a DW_AT_const_value, return NULL. */
21843 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21844 struct dwarf2_per_cu_data *per_cu,
21845 struct obstack *obstack,
21848 struct dwarf2_cu *cu;
21849 struct die_info *die;
21850 struct attribute *attr;
21851 const gdb_byte *result = NULL;
21854 enum bfd_endian byte_order;
21856 dw2_setup (per_cu->objfile);
21858 if (per_cu->cu == NULL)
21863 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21864 Instead just throw an error, not much else we can do. */
21865 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
21866 to_underlying (sect_off), objfile_name (per_cu->objfile));
21869 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21871 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
21872 to_underlying (sect_off), objfile_name (per_cu->objfile));
21875 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21879 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
21880 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21882 switch (attr->form)
21885 case DW_FORM_GNU_addr_index:
21889 *len = cu->header.addr_size;
21890 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21891 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21895 case DW_FORM_string:
21897 case DW_FORM_GNU_str_index:
21898 case DW_FORM_GNU_strp_alt:
21899 /* DW_STRING is already allocated on the objfile obstack, point
21901 result = (const gdb_byte *) DW_STRING (attr);
21902 *len = strlen (DW_STRING (attr));
21904 case DW_FORM_block1:
21905 case DW_FORM_block2:
21906 case DW_FORM_block4:
21907 case DW_FORM_block:
21908 case DW_FORM_exprloc:
21909 case DW_FORM_data16:
21910 result = DW_BLOCK (attr)->data;
21911 *len = DW_BLOCK (attr)->size;
21914 /* The DW_AT_const_value attributes are supposed to carry the
21915 symbol's value "represented as it would be on the target
21916 architecture." By the time we get here, it's already been
21917 converted to host endianness, so we just need to sign- or
21918 zero-extend it as appropriate. */
21919 case DW_FORM_data1:
21920 type = die_type (die, cu);
21921 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21922 if (result == NULL)
21923 result = write_constant_as_bytes (obstack, byte_order,
21926 case DW_FORM_data2:
21927 type = die_type (die, cu);
21928 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21929 if (result == NULL)
21930 result = write_constant_as_bytes (obstack, byte_order,
21933 case DW_FORM_data4:
21934 type = die_type (die, cu);
21935 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21936 if (result == NULL)
21937 result = write_constant_as_bytes (obstack, byte_order,
21940 case DW_FORM_data8:
21941 type = die_type (die, cu);
21942 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21943 if (result == NULL)
21944 result = write_constant_as_bytes (obstack, byte_order,
21948 case DW_FORM_sdata:
21949 case DW_FORM_implicit_const:
21950 type = die_type (die, cu);
21951 result = write_constant_as_bytes (obstack, byte_order,
21952 type, DW_SND (attr), len);
21955 case DW_FORM_udata:
21956 type = die_type (die, cu);
21957 result = write_constant_as_bytes (obstack, byte_order,
21958 type, DW_UNSND (attr), len);
21962 complaint (&symfile_complaints,
21963 _("unsupported const value attribute form: '%s'"),
21964 dwarf_form_name (attr->form));
21971 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21972 valid type for this die is found. */
21975 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21976 struct dwarf2_per_cu_data *per_cu)
21978 struct dwarf2_cu *cu;
21979 struct die_info *die;
21981 dw2_setup (per_cu->objfile);
21983 if (per_cu->cu == NULL)
21989 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21993 return die_type (die, cu);
21996 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22000 dwarf2_get_die_type (cu_offset die_offset,
22001 struct dwarf2_per_cu_data *per_cu)
22003 dw2_setup (per_cu->objfile);
22005 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22006 return get_die_type_at_offset (die_offset_sect, per_cu);
22009 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22010 On entry *REF_CU is the CU of SRC_DIE.
22011 On exit *REF_CU is the CU of the result.
22012 Returns NULL if the referenced DIE isn't found. */
22014 static struct die_info *
22015 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22016 struct dwarf2_cu **ref_cu)
22018 struct die_info temp_die;
22019 struct dwarf2_cu *sig_cu;
22020 struct die_info *die;
22022 /* While it might be nice to assert sig_type->type == NULL here,
22023 we can get here for DW_AT_imported_declaration where we need
22024 the DIE not the type. */
22026 /* If necessary, add it to the queue and load its DIEs. */
22028 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22029 read_signatured_type (sig_type);
22031 sig_cu = sig_type->per_cu.cu;
22032 gdb_assert (sig_cu != NULL);
22033 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22034 temp_die.sect_off = sig_type->type_offset_in_section;
22035 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22036 to_underlying (temp_die.sect_off));
22039 /* For .gdb_index version 7 keep track of included TUs.
22040 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22041 if (dwarf2_per_objfile->index_table != NULL
22042 && dwarf2_per_objfile->index_table->version <= 7)
22044 VEC_safe_push (dwarf2_per_cu_ptr,
22045 (*ref_cu)->per_cu->imported_symtabs,
22056 /* Follow signatured type referenced by ATTR in SRC_DIE.
22057 On entry *REF_CU is the CU of SRC_DIE.
22058 On exit *REF_CU is the CU of the result.
22059 The result is the DIE of the type.
22060 If the referenced type cannot be found an error is thrown. */
22062 static struct die_info *
22063 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22064 struct dwarf2_cu **ref_cu)
22066 ULONGEST signature = DW_SIGNATURE (attr);
22067 struct signatured_type *sig_type;
22068 struct die_info *die;
22070 gdb_assert (attr->form == DW_FORM_ref_sig8);
22072 sig_type = lookup_signatured_type (*ref_cu, signature);
22073 /* sig_type will be NULL if the signatured type is missing from
22075 if (sig_type == NULL)
22077 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22078 " from DIE at 0x%x [in module %s]"),
22079 hex_string (signature), to_underlying (src_die->sect_off),
22080 objfile_name ((*ref_cu)->objfile));
22083 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22086 dump_die_for_error (src_die);
22087 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22088 " from DIE at 0x%x [in module %s]"),
22089 hex_string (signature), to_underlying (src_die->sect_off),
22090 objfile_name ((*ref_cu)->objfile));
22096 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22097 reading in and processing the type unit if necessary. */
22099 static struct type *
22100 get_signatured_type (struct die_info *die, ULONGEST signature,
22101 struct dwarf2_cu *cu)
22103 struct signatured_type *sig_type;
22104 struct dwarf2_cu *type_cu;
22105 struct die_info *type_die;
22108 sig_type = lookup_signatured_type (cu, signature);
22109 /* sig_type will be NULL if the signatured type is missing from
22111 if (sig_type == NULL)
22113 complaint (&symfile_complaints,
22114 _("Dwarf Error: Cannot find signatured DIE %s referenced"
22115 " from DIE at 0x%x [in module %s]"),
22116 hex_string (signature), to_underlying (die->sect_off),
22117 objfile_name (dwarf2_per_objfile->objfile));
22118 return build_error_marker_type (cu, die);
22121 /* If we already know the type we're done. */
22122 if (sig_type->type != NULL)
22123 return sig_type->type;
22126 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22127 if (type_die != NULL)
22129 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22130 is created. This is important, for example, because for c++ classes
22131 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22132 type = read_type_die (type_die, type_cu);
22135 complaint (&symfile_complaints,
22136 _("Dwarf Error: Cannot build signatured type %s"
22137 " referenced from DIE at 0x%x [in module %s]"),
22138 hex_string (signature), to_underlying (die->sect_off),
22139 objfile_name (dwarf2_per_objfile->objfile));
22140 type = build_error_marker_type (cu, die);
22145 complaint (&symfile_complaints,
22146 _("Dwarf Error: Problem reading signatured DIE %s referenced"
22147 " from DIE at 0x%x [in module %s]"),
22148 hex_string (signature), to_underlying (die->sect_off),
22149 objfile_name (dwarf2_per_objfile->objfile));
22150 type = build_error_marker_type (cu, die);
22152 sig_type->type = type;
22157 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22158 reading in and processing the type unit if necessary. */
22160 static struct type *
22161 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22162 struct dwarf2_cu *cu) /* ARI: editCase function */
22164 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22165 if (attr_form_is_ref (attr))
22167 struct dwarf2_cu *type_cu = cu;
22168 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22170 return read_type_die (type_die, type_cu);
22172 else if (attr->form == DW_FORM_ref_sig8)
22174 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22178 complaint (&symfile_complaints,
22179 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22180 " at 0x%x [in module %s]"),
22181 dwarf_form_name (attr->form), to_underlying (die->sect_off),
22182 objfile_name (dwarf2_per_objfile->objfile));
22183 return build_error_marker_type (cu, die);
22187 /* Load the DIEs associated with type unit PER_CU into memory. */
22190 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22192 struct signatured_type *sig_type;
22194 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22195 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22197 /* We have the per_cu, but we need the signatured_type.
22198 Fortunately this is an easy translation. */
22199 gdb_assert (per_cu->is_debug_types);
22200 sig_type = (struct signatured_type *) per_cu;
22202 gdb_assert (per_cu->cu == NULL);
22204 read_signatured_type (sig_type);
22206 gdb_assert (per_cu->cu != NULL);
22209 /* die_reader_func for read_signatured_type.
22210 This is identical to load_full_comp_unit_reader,
22211 but is kept separate for now. */
22214 read_signatured_type_reader (const struct die_reader_specs *reader,
22215 const gdb_byte *info_ptr,
22216 struct die_info *comp_unit_die,
22220 struct dwarf2_cu *cu = reader->cu;
22222 gdb_assert (cu->die_hash == NULL);
22224 htab_create_alloc_ex (cu->header.length / 12,
22228 &cu->comp_unit_obstack,
22229 hashtab_obstack_allocate,
22230 dummy_obstack_deallocate);
22233 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
22234 &info_ptr, comp_unit_die);
22235 cu->dies = comp_unit_die;
22236 /* comp_unit_die is not stored in die_hash, no need. */
22238 /* We try not to read any attributes in this function, because not
22239 all CUs needed for references have been loaded yet, and symbol
22240 table processing isn't initialized. But we have to set the CU language,
22241 or we won't be able to build types correctly.
22242 Similarly, if we do not read the producer, we can not apply
22243 producer-specific interpretation. */
22244 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22247 /* Read in a signatured type and build its CU and DIEs.
22248 If the type is a stub for the real type in a DWO file,
22249 read in the real type from the DWO file as well. */
22252 read_signatured_type (struct signatured_type *sig_type)
22254 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22256 gdb_assert (per_cu->is_debug_types);
22257 gdb_assert (per_cu->cu == NULL);
22259 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
22260 read_signatured_type_reader, NULL);
22261 sig_type->per_cu.tu_read = 1;
22264 /* Decode simple location descriptions.
22265 Given a pointer to a dwarf block that defines a location, compute
22266 the location and return the value.
22268 NOTE drow/2003-11-18: This function is called in two situations
22269 now: for the address of static or global variables (partial symbols
22270 only) and for offsets into structures which are expected to be
22271 (more or less) constant. The partial symbol case should go away,
22272 and only the constant case should remain. That will let this
22273 function complain more accurately. A few special modes are allowed
22274 without complaint for global variables (for instance, global
22275 register values and thread-local values).
22277 A location description containing no operations indicates that the
22278 object is optimized out. The return value is 0 for that case.
22279 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22280 callers will only want a very basic result and this can become a
22283 Note that stack[0] is unused except as a default error return. */
22286 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22288 struct objfile *objfile = cu->objfile;
22290 size_t size = blk->size;
22291 const gdb_byte *data = blk->data;
22292 CORE_ADDR stack[64];
22294 unsigned int bytes_read, unsnd;
22300 stack[++stacki] = 0;
22339 stack[++stacki] = op - DW_OP_lit0;
22374 stack[++stacki] = op - DW_OP_reg0;
22376 dwarf2_complex_location_expr_complaint ();
22380 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22382 stack[++stacki] = unsnd;
22384 dwarf2_complex_location_expr_complaint ();
22388 stack[++stacki] = read_address (objfile->obfd, &data[i],
22393 case DW_OP_const1u:
22394 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22398 case DW_OP_const1s:
22399 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22403 case DW_OP_const2u:
22404 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22408 case DW_OP_const2s:
22409 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22413 case DW_OP_const4u:
22414 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22418 case DW_OP_const4s:
22419 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22423 case DW_OP_const8u:
22424 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22429 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22435 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22440 stack[stacki + 1] = stack[stacki];
22445 stack[stacki - 1] += stack[stacki];
22449 case DW_OP_plus_uconst:
22450 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22456 stack[stacki - 1] -= stack[stacki];
22461 /* If we're not the last op, then we definitely can't encode
22462 this using GDB's address_class enum. This is valid for partial
22463 global symbols, although the variable's address will be bogus
22466 dwarf2_complex_location_expr_complaint ();
22469 case DW_OP_GNU_push_tls_address:
22470 case DW_OP_form_tls_address:
22471 /* The top of the stack has the offset from the beginning
22472 of the thread control block at which the variable is located. */
22473 /* Nothing should follow this operator, so the top of stack would
22475 /* This is valid for partial global symbols, but the variable's
22476 address will be bogus in the psymtab. Make it always at least
22477 non-zero to not look as a variable garbage collected by linker
22478 which have DW_OP_addr 0. */
22480 dwarf2_complex_location_expr_complaint ();
22484 case DW_OP_GNU_uninit:
22487 case DW_OP_GNU_addr_index:
22488 case DW_OP_GNU_const_index:
22489 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22496 const char *name = get_DW_OP_name (op);
22499 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
22502 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
22506 return (stack[stacki]);
22509 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22510 outside of the allocated space. Also enforce minimum>0. */
22511 if (stacki >= ARRAY_SIZE (stack) - 1)
22513 complaint (&symfile_complaints,
22514 _("location description stack overflow"));
22520 complaint (&symfile_complaints,
22521 _("location description stack underflow"));
22525 return (stack[stacki]);
22528 /* memory allocation interface */
22530 static struct dwarf_block *
22531 dwarf_alloc_block (struct dwarf2_cu *cu)
22533 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22536 static struct die_info *
22537 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22539 struct die_info *die;
22540 size_t size = sizeof (struct die_info);
22543 size += (num_attrs - 1) * sizeof (struct attribute);
22545 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22546 memset (die, 0, sizeof (struct die_info));
22551 /* Macro support. */
22553 /* Return file name relative to the compilation directory of file number I in
22554 *LH's file name table. The result is allocated using xmalloc; the caller is
22555 responsible for freeing it. */
22558 file_file_name (int file, struct line_header *lh)
22560 /* Is the file number a valid index into the line header's file name
22561 table? Remember that file numbers start with one, not zero. */
22562 if (1 <= file && file <= lh->file_names.size ())
22564 const file_entry &fe = lh->file_names[file - 1];
22566 if (!IS_ABSOLUTE_PATH (fe.name))
22568 const char *dir = fe.include_dir (lh);
22570 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
22572 return xstrdup (fe.name);
22576 /* The compiler produced a bogus file number. We can at least
22577 record the macro definitions made in the file, even if we
22578 won't be able to find the file by name. */
22579 char fake_name[80];
22581 xsnprintf (fake_name, sizeof (fake_name),
22582 "<bad macro file number %d>", file);
22584 complaint (&symfile_complaints,
22585 _("bad file number in macro information (%d)"),
22588 return xstrdup (fake_name);
22592 /* Return the full name of file number I in *LH's file name table.
22593 Use COMP_DIR as the name of the current directory of the
22594 compilation. The result is allocated using xmalloc; the caller is
22595 responsible for freeing it. */
22597 file_full_name (int file, struct line_header *lh, const char *comp_dir)
22599 /* Is the file number a valid index into the line header's file name
22600 table? Remember that file numbers start with one, not zero. */
22601 if (1 <= file && file <= lh->file_names.size ())
22603 char *relative = file_file_name (file, lh);
22605 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
22607 return reconcat (relative, comp_dir, SLASH_STRING,
22608 relative, (char *) NULL);
22611 return file_file_name (file, lh);
22615 static struct macro_source_file *
22616 macro_start_file (int file, int line,
22617 struct macro_source_file *current_file,
22618 struct line_header *lh)
22620 /* File name relative to the compilation directory of this source file. */
22621 char *file_name = file_file_name (file, lh);
22623 if (! current_file)
22625 /* Note: We don't create a macro table for this compilation unit
22626 at all until we actually get a filename. */
22627 struct macro_table *macro_table = get_macro_table ();
22629 /* If we have no current file, then this must be the start_file
22630 directive for the compilation unit's main source file. */
22631 current_file = macro_set_main (macro_table, file_name);
22632 macro_define_special (macro_table);
22635 current_file = macro_include (current_file, line, file_name);
22639 return current_file;
22642 static const char *
22643 consume_improper_spaces (const char *p, const char *body)
22647 complaint (&symfile_complaints,
22648 _("macro definition contains spaces "
22649 "in formal argument list:\n`%s'"),
22661 parse_macro_definition (struct macro_source_file *file, int line,
22666 /* The body string takes one of two forms. For object-like macro
22667 definitions, it should be:
22669 <macro name> " " <definition>
22671 For function-like macro definitions, it should be:
22673 <macro name> "() " <definition>
22675 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
22677 Spaces may appear only where explicitly indicated, and in the
22680 The Dwarf 2 spec says that an object-like macro's name is always
22681 followed by a space, but versions of GCC around March 2002 omit
22682 the space when the macro's definition is the empty string.
22684 The Dwarf 2 spec says that there should be no spaces between the
22685 formal arguments in a function-like macro's formal argument list,
22686 but versions of GCC around March 2002 include spaces after the
22690 /* Find the extent of the macro name. The macro name is terminated
22691 by either a space or null character (for an object-like macro) or
22692 an opening paren (for a function-like macro). */
22693 for (p = body; *p; p++)
22694 if (*p == ' ' || *p == '(')
22697 if (*p == ' ' || *p == '\0')
22699 /* It's an object-like macro. */
22700 int name_len = p - body;
22701 char *name = savestring (body, name_len);
22702 const char *replacement;
22705 replacement = body + name_len + 1;
22708 dwarf2_macro_malformed_definition_complaint (body);
22709 replacement = body + name_len;
22712 macro_define_object (file, line, name, replacement);
22716 else if (*p == '(')
22718 /* It's a function-like macro. */
22719 char *name = savestring (body, p - body);
22722 char **argv = XNEWVEC (char *, argv_size);
22726 p = consume_improper_spaces (p, body);
22728 /* Parse the formal argument list. */
22729 while (*p && *p != ')')
22731 /* Find the extent of the current argument name. */
22732 const char *arg_start = p;
22734 while (*p && *p != ',' && *p != ')' && *p != ' ')
22737 if (! *p || p == arg_start)
22738 dwarf2_macro_malformed_definition_complaint (body);
22741 /* Make sure argv has room for the new argument. */
22742 if (argc >= argv_size)
22745 argv = XRESIZEVEC (char *, argv, argv_size);
22748 argv[argc++] = savestring (arg_start, p - arg_start);
22751 p = consume_improper_spaces (p, body);
22753 /* Consume the comma, if present. */
22758 p = consume_improper_spaces (p, body);
22767 /* Perfectly formed definition, no complaints. */
22768 macro_define_function (file, line, name,
22769 argc, (const char **) argv,
22771 else if (*p == '\0')
22773 /* Complain, but do define it. */
22774 dwarf2_macro_malformed_definition_complaint (body);
22775 macro_define_function (file, line, name,
22776 argc, (const char **) argv,
22780 /* Just complain. */
22781 dwarf2_macro_malformed_definition_complaint (body);
22784 /* Just complain. */
22785 dwarf2_macro_malformed_definition_complaint (body);
22791 for (i = 0; i < argc; i++)
22797 dwarf2_macro_malformed_definition_complaint (body);
22800 /* Skip some bytes from BYTES according to the form given in FORM.
22801 Returns the new pointer. */
22803 static const gdb_byte *
22804 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
22805 enum dwarf_form form,
22806 unsigned int offset_size,
22807 struct dwarf2_section_info *section)
22809 unsigned int bytes_read;
22813 case DW_FORM_data1:
22818 case DW_FORM_data2:
22822 case DW_FORM_data4:
22826 case DW_FORM_data8:
22830 case DW_FORM_data16:
22834 case DW_FORM_string:
22835 read_direct_string (abfd, bytes, &bytes_read);
22836 bytes += bytes_read;
22839 case DW_FORM_sec_offset:
22841 case DW_FORM_GNU_strp_alt:
22842 bytes += offset_size;
22845 case DW_FORM_block:
22846 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
22847 bytes += bytes_read;
22850 case DW_FORM_block1:
22851 bytes += 1 + read_1_byte (abfd, bytes);
22853 case DW_FORM_block2:
22854 bytes += 2 + read_2_bytes (abfd, bytes);
22856 case DW_FORM_block4:
22857 bytes += 4 + read_4_bytes (abfd, bytes);
22860 case DW_FORM_sdata:
22861 case DW_FORM_udata:
22862 case DW_FORM_GNU_addr_index:
22863 case DW_FORM_GNU_str_index:
22864 bytes = gdb_skip_leb128 (bytes, buffer_end);
22867 dwarf2_section_buffer_overflow_complaint (section);
22872 case DW_FORM_implicit_const:
22877 complaint (&symfile_complaints,
22878 _("invalid form 0x%x in `%s'"),
22879 form, get_section_name (section));
22887 /* A helper for dwarf_decode_macros that handles skipping an unknown
22888 opcode. Returns an updated pointer to the macro data buffer; or,
22889 on error, issues a complaint and returns NULL. */
22891 static const gdb_byte *
22892 skip_unknown_opcode (unsigned int opcode,
22893 const gdb_byte **opcode_definitions,
22894 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22896 unsigned int offset_size,
22897 struct dwarf2_section_info *section)
22899 unsigned int bytes_read, i;
22901 const gdb_byte *defn;
22903 if (opcode_definitions[opcode] == NULL)
22905 complaint (&symfile_complaints,
22906 _("unrecognized DW_MACFINO opcode 0x%x"),
22911 defn = opcode_definitions[opcode];
22912 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
22913 defn += bytes_read;
22915 for (i = 0; i < arg; ++i)
22917 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
22918 (enum dwarf_form) defn[i], offset_size,
22920 if (mac_ptr == NULL)
22922 /* skip_form_bytes already issued the complaint. */
22930 /* A helper function which parses the header of a macro section.
22931 If the macro section is the extended (for now called "GNU") type,
22932 then this updates *OFFSET_SIZE. Returns a pointer to just after
22933 the header, or issues a complaint and returns NULL on error. */
22935 static const gdb_byte *
22936 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
22938 const gdb_byte *mac_ptr,
22939 unsigned int *offset_size,
22940 int section_is_gnu)
22942 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
22944 if (section_is_gnu)
22946 unsigned int version, flags;
22948 version = read_2_bytes (abfd, mac_ptr);
22949 if (version != 4 && version != 5)
22951 complaint (&symfile_complaints,
22952 _("unrecognized version `%d' in .debug_macro section"),
22958 flags = read_1_byte (abfd, mac_ptr);
22960 *offset_size = (flags & 1) ? 8 : 4;
22962 if ((flags & 2) != 0)
22963 /* We don't need the line table offset. */
22964 mac_ptr += *offset_size;
22966 /* Vendor opcode descriptions. */
22967 if ((flags & 4) != 0)
22969 unsigned int i, count;
22971 count = read_1_byte (abfd, mac_ptr);
22973 for (i = 0; i < count; ++i)
22975 unsigned int opcode, bytes_read;
22978 opcode = read_1_byte (abfd, mac_ptr);
22980 opcode_definitions[opcode] = mac_ptr;
22981 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22982 mac_ptr += bytes_read;
22991 /* A helper for dwarf_decode_macros that handles the GNU extensions,
22992 including DW_MACRO_import. */
22995 dwarf_decode_macro_bytes (bfd *abfd,
22996 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22997 struct macro_source_file *current_file,
22998 struct line_header *lh,
22999 struct dwarf2_section_info *section,
23000 int section_is_gnu, int section_is_dwz,
23001 unsigned int offset_size,
23002 htab_t include_hash)
23004 struct objfile *objfile = dwarf2_per_objfile->objfile;
23005 enum dwarf_macro_record_type macinfo_type;
23006 int at_commandline;
23007 const gdb_byte *opcode_definitions[256];
23009 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23010 &offset_size, section_is_gnu);
23011 if (mac_ptr == NULL)
23013 /* We already issued a complaint. */
23017 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23018 GDB is still reading the definitions from command line. First
23019 DW_MACINFO_start_file will need to be ignored as it was already executed
23020 to create CURRENT_FILE for the main source holding also the command line
23021 definitions. On first met DW_MACINFO_start_file this flag is reset to
23022 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23024 at_commandline = 1;
23028 /* Do we at least have room for a macinfo type byte? */
23029 if (mac_ptr >= mac_end)
23031 dwarf2_section_buffer_overflow_complaint (section);
23035 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23038 /* Note that we rely on the fact that the corresponding GNU and
23039 DWARF constants are the same. */
23040 switch (macinfo_type)
23042 /* A zero macinfo type indicates the end of the macro
23047 case DW_MACRO_define:
23048 case DW_MACRO_undef:
23049 case DW_MACRO_define_strp:
23050 case DW_MACRO_undef_strp:
23051 case DW_MACRO_define_sup:
23052 case DW_MACRO_undef_sup:
23054 unsigned int bytes_read;
23059 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23060 mac_ptr += bytes_read;
23062 if (macinfo_type == DW_MACRO_define
23063 || macinfo_type == DW_MACRO_undef)
23065 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23066 mac_ptr += bytes_read;
23070 LONGEST str_offset;
23072 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
23073 mac_ptr += offset_size;
23075 if (macinfo_type == DW_MACRO_define_sup
23076 || macinfo_type == DW_MACRO_undef_sup
23079 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23081 body = read_indirect_string_from_dwz (dwz, str_offset);
23084 body = read_indirect_string_at_offset (abfd, str_offset);
23087 is_define = (macinfo_type == DW_MACRO_define
23088 || macinfo_type == DW_MACRO_define_strp
23089 || macinfo_type == DW_MACRO_define_sup);
23090 if (! current_file)
23092 /* DWARF violation as no main source is present. */
23093 complaint (&symfile_complaints,
23094 _("debug info with no main source gives macro %s "
23096 is_define ? _("definition") : _("undefinition"),
23100 if ((line == 0 && !at_commandline)
23101 || (line != 0 && at_commandline))
23102 complaint (&symfile_complaints,
23103 _("debug info gives %s macro %s with %s line %d: %s"),
23104 at_commandline ? _("command-line") : _("in-file"),
23105 is_define ? _("definition") : _("undefinition"),
23106 line == 0 ? _("zero") : _("non-zero"), line, body);
23109 parse_macro_definition (current_file, line, body);
23112 gdb_assert (macinfo_type == DW_MACRO_undef
23113 || macinfo_type == DW_MACRO_undef_strp
23114 || macinfo_type == DW_MACRO_undef_sup);
23115 macro_undef (current_file, line, body);
23120 case DW_MACRO_start_file:
23122 unsigned int bytes_read;
23125 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23126 mac_ptr += bytes_read;
23127 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23128 mac_ptr += bytes_read;
23130 if ((line == 0 && !at_commandline)
23131 || (line != 0 && at_commandline))
23132 complaint (&symfile_complaints,
23133 _("debug info gives source %d included "
23134 "from %s at %s line %d"),
23135 file, at_commandline ? _("command-line") : _("file"),
23136 line == 0 ? _("zero") : _("non-zero"), line);
23138 if (at_commandline)
23140 /* This DW_MACRO_start_file was executed in the
23142 at_commandline = 0;
23145 current_file = macro_start_file (file, line, current_file, lh);
23149 case DW_MACRO_end_file:
23150 if (! current_file)
23151 complaint (&symfile_complaints,
23152 _("macro debug info has an unmatched "
23153 "`close_file' directive"));
23156 current_file = current_file->included_by;
23157 if (! current_file)
23159 enum dwarf_macro_record_type next_type;
23161 /* GCC circa March 2002 doesn't produce the zero
23162 type byte marking the end of the compilation
23163 unit. Complain if it's not there, but exit no
23166 /* Do we at least have room for a macinfo type byte? */
23167 if (mac_ptr >= mac_end)
23169 dwarf2_section_buffer_overflow_complaint (section);
23173 /* We don't increment mac_ptr here, so this is just
23176 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23178 if (next_type != 0)
23179 complaint (&symfile_complaints,
23180 _("no terminating 0-type entry for "
23181 "macros in `.debug_macinfo' section"));
23188 case DW_MACRO_import:
23189 case DW_MACRO_import_sup:
23193 bfd *include_bfd = abfd;
23194 struct dwarf2_section_info *include_section = section;
23195 const gdb_byte *include_mac_end = mac_end;
23196 int is_dwz = section_is_dwz;
23197 const gdb_byte *new_mac_ptr;
23199 offset = read_offset_1 (abfd, mac_ptr, offset_size);
23200 mac_ptr += offset_size;
23202 if (macinfo_type == DW_MACRO_import_sup)
23204 struct dwz_file *dwz = dwarf2_get_dwz_file ();
23206 dwarf2_read_section (objfile, &dwz->macro);
23208 include_section = &dwz->macro;
23209 include_bfd = get_section_bfd_owner (include_section);
23210 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23214 new_mac_ptr = include_section->buffer + offset;
23215 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23219 /* This has actually happened; see
23220 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23221 complaint (&symfile_complaints,
23222 _("recursive DW_MACRO_import in "
23223 ".debug_macro section"));
23227 *slot = (void *) new_mac_ptr;
23229 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
23230 include_mac_end, current_file, lh,
23231 section, section_is_gnu, is_dwz,
23232 offset_size, include_hash);
23234 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23239 case DW_MACINFO_vendor_ext:
23240 if (!section_is_gnu)
23242 unsigned int bytes_read;
23244 /* This reads the constant, but since we don't recognize
23245 any vendor extensions, we ignore it. */
23246 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23247 mac_ptr += bytes_read;
23248 read_direct_string (abfd, mac_ptr, &bytes_read);
23249 mac_ptr += bytes_read;
23251 /* We don't recognize any vendor extensions. */
23257 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23258 mac_ptr, mac_end, abfd, offset_size,
23260 if (mac_ptr == NULL)
23264 } while (macinfo_type != 0);
23268 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23269 int section_is_gnu)
23271 struct objfile *objfile = dwarf2_per_objfile->objfile;
23272 struct line_header *lh = cu->line_header;
23274 const gdb_byte *mac_ptr, *mac_end;
23275 struct macro_source_file *current_file = 0;
23276 enum dwarf_macro_record_type macinfo_type;
23277 unsigned int offset_size = cu->header.offset_size;
23278 const gdb_byte *opcode_definitions[256];
23280 struct dwarf2_section_info *section;
23281 const char *section_name;
23283 if (cu->dwo_unit != NULL)
23285 if (section_is_gnu)
23287 section = &cu->dwo_unit->dwo_file->sections.macro;
23288 section_name = ".debug_macro.dwo";
23292 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23293 section_name = ".debug_macinfo.dwo";
23298 if (section_is_gnu)
23300 section = &dwarf2_per_objfile->macro;
23301 section_name = ".debug_macro";
23305 section = &dwarf2_per_objfile->macinfo;
23306 section_name = ".debug_macinfo";
23310 dwarf2_read_section (objfile, section);
23311 if (section->buffer == NULL)
23313 complaint (&symfile_complaints, _("missing %s section"), section_name);
23316 abfd = get_section_bfd_owner (section);
23318 /* First pass: Find the name of the base filename.
23319 This filename is needed in order to process all macros whose definition
23320 (or undefinition) comes from the command line. These macros are defined
23321 before the first DW_MACINFO_start_file entry, and yet still need to be
23322 associated to the base file.
23324 To determine the base file name, we scan the macro definitions until we
23325 reach the first DW_MACINFO_start_file entry. We then initialize
23326 CURRENT_FILE accordingly so that any macro definition found before the
23327 first DW_MACINFO_start_file can still be associated to the base file. */
23329 mac_ptr = section->buffer + offset;
23330 mac_end = section->buffer + section->size;
23332 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23333 &offset_size, section_is_gnu);
23334 if (mac_ptr == NULL)
23336 /* We already issued a complaint. */
23342 /* Do we at least have room for a macinfo type byte? */
23343 if (mac_ptr >= mac_end)
23345 /* Complaint is printed during the second pass as GDB will probably
23346 stop the first pass earlier upon finding
23347 DW_MACINFO_start_file. */
23351 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23354 /* Note that we rely on the fact that the corresponding GNU and
23355 DWARF constants are the same. */
23356 switch (macinfo_type)
23358 /* A zero macinfo type indicates the end of the macro
23363 case DW_MACRO_define:
23364 case DW_MACRO_undef:
23365 /* Only skip the data by MAC_PTR. */
23367 unsigned int bytes_read;
23369 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23370 mac_ptr += bytes_read;
23371 read_direct_string (abfd, mac_ptr, &bytes_read);
23372 mac_ptr += bytes_read;
23376 case DW_MACRO_start_file:
23378 unsigned int bytes_read;
23381 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23382 mac_ptr += bytes_read;
23383 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23384 mac_ptr += bytes_read;
23386 current_file = macro_start_file (file, line, current_file, lh);
23390 case DW_MACRO_end_file:
23391 /* No data to skip by MAC_PTR. */
23394 case DW_MACRO_define_strp:
23395 case DW_MACRO_undef_strp:
23396 case DW_MACRO_define_sup:
23397 case DW_MACRO_undef_sup:
23399 unsigned int bytes_read;
23401 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23402 mac_ptr += bytes_read;
23403 mac_ptr += offset_size;
23407 case DW_MACRO_import:
23408 case DW_MACRO_import_sup:
23409 /* Note that, according to the spec, a transparent include
23410 chain cannot call DW_MACRO_start_file. So, we can just
23411 skip this opcode. */
23412 mac_ptr += offset_size;
23415 case DW_MACINFO_vendor_ext:
23416 /* Only skip the data by MAC_PTR. */
23417 if (!section_is_gnu)
23419 unsigned int bytes_read;
23421 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23422 mac_ptr += bytes_read;
23423 read_direct_string (abfd, mac_ptr, &bytes_read);
23424 mac_ptr += bytes_read;
23429 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23430 mac_ptr, mac_end, abfd, offset_size,
23432 if (mac_ptr == NULL)
23436 } while (macinfo_type != 0 && current_file == NULL);
23438 /* Second pass: Process all entries.
23440 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23441 command-line macro definitions/undefinitions. This flag is unset when we
23442 reach the first DW_MACINFO_start_file entry. */
23444 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23446 NULL, xcalloc, xfree));
23447 mac_ptr = section->buffer + offset;
23448 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23449 *slot = (void *) mac_ptr;
23450 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
23451 current_file, lh, section,
23452 section_is_gnu, 0, offset_size,
23453 include_hash.get ());
23456 /* Check if the attribute's form is a DW_FORM_block*
23457 if so return true else false. */
23460 attr_form_is_block (const struct attribute *attr)
23462 return (attr == NULL ? 0 :
23463 attr->form == DW_FORM_block1
23464 || attr->form == DW_FORM_block2
23465 || attr->form == DW_FORM_block4
23466 || attr->form == DW_FORM_block
23467 || attr->form == DW_FORM_exprloc);
23470 /* Return non-zero if ATTR's value is a section offset --- classes
23471 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
23472 You may use DW_UNSND (attr) to retrieve such offsets.
23474 Section 7.5.4, "Attribute Encodings", explains that no attribute
23475 may have a value that belongs to more than one of these classes; it
23476 would be ambiguous if we did, because we use the same forms for all
23480 attr_form_is_section_offset (const struct attribute *attr)
23482 return (attr->form == DW_FORM_data4
23483 || attr->form == DW_FORM_data8
23484 || attr->form == DW_FORM_sec_offset);
23487 /* Return non-zero if ATTR's value falls in the 'constant' class, or
23488 zero otherwise. When this function returns true, you can apply
23489 dwarf2_get_attr_constant_value to it.
23491 However, note that for some attributes you must check
23492 attr_form_is_section_offset before using this test. DW_FORM_data4
23493 and DW_FORM_data8 are members of both the constant class, and of
23494 the classes that contain offsets into other debug sections
23495 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
23496 that, if an attribute's can be either a constant or one of the
23497 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
23498 taken as section offsets, not constants.
23500 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
23501 cannot handle that. */
23504 attr_form_is_constant (const struct attribute *attr)
23506 switch (attr->form)
23508 case DW_FORM_sdata:
23509 case DW_FORM_udata:
23510 case DW_FORM_data1:
23511 case DW_FORM_data2:
23512 case DW_FORM_data4:
23513 case DW_FORM_data8:
23514 case DW_FORM_implicit_const:
23522 /* DW_ADDR is always stored already as sect_offset; despite for the forms
23523 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
23526 attr_form_is_ref (const struct attribute *attr)
23528 switch (attr->form)
23530 case DW_FORM_ref_addr:
23535 case DW_FORM_ref_udata:
23536 case DW_FORM_GNU_ref_alt:
23543 /* Return the .debug_loc section to use for CU.
23544 For DWO files use .debug_loc.dwo. */
23546 static struct dwarf2_section_info *
23547 cu_debug_loc_section (struct dwarf2_cu *cu)
23551 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23553 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
23555 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23556 : &dwarf2_per_objfile->loc);
23559 /* A helper function that fills in a dwarf2_loclist_baton. */
23562 fill_in_loclist_baton (struct dwarf2_cu *cu,
23563 struct dwarf2_loclist_baton *baton,
23564 const struct attribute *attr)
23566 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23568 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
23570 baton->per_cu = cu->per_cu;
23571 gdb_assert (baton->per_cu);
23572 /* We don't know how long the location list is, but make sure we
23573 don't run off the edge of the section. */
23574 baton->size = section->size - DW_UNSND (attr);
23575 baton->data = section->buffer + DW_UNSND (attr);
23576 baton->base_address = cu->base_address;
23577 baton->from_dwo = cu->dwo_unit != NULL;
23581 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23582 struct dwarf2_cu *cu, int is_block)
23584 struct objfile *objfile = dwarf2_per_objfile->objfile;
23585 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23587 if (attr_form_is_section_offset (attr)
23588 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23589 the section. If so, fall through to the complaint in the
23591 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
23593 struct dwarf2_loclist_baton *baton;
23595 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23597 fill_in_loclist_baton (cu, baton, attr);
23599 if (cu->base_known == 0)
23600 complaint (&symfile_complaints,
23601 _("Location list used without "
23602 "specifying the CU base address."));
23604 SYMBOL_ACLASS_INDEX (sym) = (is_block
23605 ? dwarf2_loclist_block_index
23606 : dwarf2_loclist_index);
23607 SYMBOL_LOCATION_BATON (sym) = baton;
23611 struct dwarf2_locexpr_baton *baton;
23613 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23614 baton->per_cu = cu->per_cu;
23615 gdb_assert (baton->per_cu);
23617 if (attr_form_is_block (attr))
23619 /* Note that we're just copying the block's data pointer
23620 here, not the actual data. We're still pointing into the
23621 info_buffer for SYM's objfile; right now we never release
23622 that buffer, but when we do clean up properly this may
23624 baton->size = DW_BLOCK (attr)->size;
23625 baton->data = DW_BLOCK (attr)->data;
23629 dwarf2_invalid_attrib_class_complaint ("location description",
23630 SYMBOL_NATURAL_NAME (sym));
23634 SYMBOL_ACLASS_INDEX (sym) = (is_block
23635 ? dwarf2_locexpr_block_index
23636 : dwarf2_locexpr_index);
23637 SYMBOL_LOCATION_BATON (sym) = baton;
23641 /* Return the OBJFILE associated with the compilation unit CU. If CU
23642 came from a separate debuginfo file, then the master objfile is
23646 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
23648 struct objfile *objfile = per_cu->objfile;
23650 /* Return the master objfile, so that we can report and look up the
23651 correct file containing this variable. */
23652 if (objfile->separate_debug_objfile_backlink)
23653 objfile = objfile->separate_debug_objfile_backlink;
23658 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23659 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23660 CU_HEADERP first. */
23662 static const struct comp_unit_head *
23663 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23664 struct dwarf2_per_cu_data *per_cu)
23666 const gdb_byte *info_ptr;
23669 return &per_cu->cu->header;
23671 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23673 memset (cu_headerp, 0, sizeof (*cu_headerp));
23674 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23675 rcuh_kind::COMPILE);
23680 /* Return the address size given in the compilation unit header for CU. */
23683 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
23685 struct comp_unit_head cu_header_local;
23686 const struct comp_unit_head *cu_headerp;
23688 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23690 return cu_headerp->addr_size;
23693 /* Return the offset size given in the compilation unit header for CU. */
23696 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
23698 struct comp_unit_head cu_header_local;
23699 const struct comp_unit_head *cu_headerp;
23701 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23703 return cu_headerp->offset_size;
23706 /* See its dwarf2loc.h declaration. */
23709 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
23711 struct comp_unit_head cu_header_local;
23712 const struct comp_unit_head *cu_headerp;
23714 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
23716 if (cu_headerp->version == 2)
23717 return cu_headerp->addr_size;
23719 return cu_headerp->offset_size;
23722 /* Return the text offset of the CU. The returned offset comes from
23723 this CU's objfile. If this objfile came from a separate debuginfo
23724 file, then the offset may be different from the corresponding
23725 offset in the parent objfile. */
23728 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
23730 struct objfile *objfile = per_cu->objfile;
23732 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23735 /* Return DWARF version number of PER_CU. */
23738 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
23740 return per_cu->dwarf_version;
23743 /* Locate the .debug_info compilation unit from CU's objfile which contains
23744 the DIE at OFFSET. Raises an error on failure. */
23746 static struct dwarf2_per_cu_data *
23747 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23748 unsigned int offset_in_dwz,
23749 struct objfile *objfile)
23751 struct dwarf2_per_cu_data *this_cu;
23753 const sect_offset *cu_off;
23756 high = dwarf2_per_objfile->n_comp_units - 1;
23759 struct dwarf2_per_cu_data *mid_cu;
23760 int mid = low + (high - low) / 2;
23762 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
23763 cu_off = &mid_cu->sect_off;
23764 if (mid_cu->is_dwz > offset_in_dwz
23765 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
23770 gdb_assert (low == high);
23771 this_cu = dwarf2_per_objfile->all_comp_units[low];
23772 cu_off = &this_cu->sect_off;
23773 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
23775 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23776 error (_("Dwarf Error: could not find partial DIE containing "
23777 "offset 0x%x [in module %s]"),
23778 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
23780 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23782 return dwarf2_per_objfile->all_comp_units[low-1];
23786 this_cu = dwarf2_per_objfile->all_comp_units[low];
23787 if (low == dwarf2_per_objfile->n_comp_units - 1
23788 && sect_off >= this_cu->sect_off + this_cu->length)
23789 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
23790 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23795 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23798 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
23800 memset (cu, 0, sizeof (*cu));
23802 cu->per_cu = per_cu;
23803 cu->objfile = per_cu->objfile;
23804 obstack_init (&cu->comp_unit_obstack);
23807 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23810 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23811 enum language pretend_language)
23813 struct attribute *attr;
23815 /* Set the language we're debugging. */
23816 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23818 set_cu_language (DW_UNSND (attr), cu);
23821 cu->language = pretend_language;
23822 cu->language_defn = language_def (cu->language);
23825 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23828 /* Release one cached compilation unit, CU. We unlink it from the tree
23829 of compilation units, but we don't remove it from the read_in_chain;
23830 the caller is responsible for that.
23831 NOTE: DATA is a void * because this function is also used as a
23832 cleanup routine. */
23835 free_heap_comp_unit (void *data)
23837 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23839 gdb_assert (cu->per_cu != NULL);
23840 cu->per_cu->cu = NULL;
23843 obstack_free (&cu->comp_unit_obstack, NULL);
23848 /* This cleanup function is passed the address of a dwarf2_cu on the stack
23849 when we're finished with it. We can't free the pointer itself, but be
23850 sure to unlink it from the cache. Also release any associated storage. */
23853 free_stack_comp_unit (void *data)
23855 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
23857 gdb_assert (cu->per_cu != NULL);
23858 cu->per_cu->cu = NULL;
23861 obstack_free (&cu->comp_unit_obstack, NULL);
23862 cu->partial_dies = NULL;
23865 /* Free all cached compilation units. */
23868 free_cached_comp_units (void *data)
23870 dwarf2_per_objfile->free_cached_comp_units ();
23873 /* Increase the age counter on each cached compilation unit, and free
23874 any that are too old. */
23877 age_cached_comp_units (void)
23879 struct dwarf2_per_cu_data *per_cu, **last_chain;
23881 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23882 per_cu = dwarf2_per_objfile->read_in_chain;
23883 while (per_cu != NULL)
23885 per_cu->cu->last_used ++;
23886 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23887 dwarf2_mark (per_cu->cu);
23888 per_cu = per_cu->cu->read_in_chain;
23891 per_cu = dwarf2_per_objfile->read_in_chain;
23892 last_chain = &dwarf2_per_objfile->read_in_chain;
23893 while (per_cu != NULL)
23895 struct dwarf2_per_cu_data *next_cu;
23897 next_cu = per_cu->cu->read_in_chain;
23899 if (!per_cu->cu->mark)
23901 free_heap_comp_unit (per_cu->cu);
23902 *last_chain = next_cu;
23905 last_chain = &per_cu->cu->read_in_chain;
23911 /* Remove a single compilation unit from the cache. */
23914 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23916 struct dwarf2_per_cu_data *per_cu, **last_chain;
23918 per_cu = dwarf2_per_objfile->read_in_chain;
23919 last_chain = &dwarf2_per_objfile->read_in_chain;
23920 while (per_cu != NULL)
23922 struct dwarf2_per_cu_data *next_cu;
23924 next_cu = per_cu->cu->read_in_chain;
23926 if (per_cu == target_per_cu)
23928 free_heap_comp_unit (per_cu->cu);
23930 *last_chain = next_cu;
23934 last_chain = &per_cu->cu->read_in_chain;
23940 /* Release all extra memory associated with OBJFILE. */
23943 dwarf2_free_objfile (struct objfile *objfile)
23946 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23947 dwarf2_objfile_data_key);
23949 if (dwarf2_per_objfile == NULL)
23952 dwarf2_per_objfile->~dwarf2_per_objfile ();
23955 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23956 We store these in a hash table separate from the DIEs, and preserve them
23957 when the DIEs are flushed out of cache.
23959 The CU "per_cu" pointer is needed because offset alone is not enough to
23960 uniquely identify the type. A file may have multiple .debug_types sections,
23961 or the type may come from a DWO file. Furthermore, while it's more logical
23962 to use per_cu->section+offset, with Fission the section with the data is in
23963 the DWO file but we don't know that section at the point we need it.
23964 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23965 because we can enter the lookup routine, get_die_type_at_offset, from
23966 outside this file, and thus won't necessarily have PER_CU->cu.
23967 Fortunately, PER_CU is stable for the life of the objfile. */
23969 struct dwarf2_per_cu_offset_and_type
23971 const struct dwarf2_per_cu_data *per_cu;
23972 sect_offset sect_off;
23976 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23979 per_cu_offset_and_type_hash (const void *item)
23981 const struct dwarf2_per_cu_offset_and_type *ofs
23982 = (const struct dwarf2_per_cu_offset_and_type *) item;
23984 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23987 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23990 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23992 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23993 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23994 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23995 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23997 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23998 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24001 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24002 table if necessary. For convenience, return TYPE.
24004 The DIEs reading must have careful ordering to:
24005 * Not cause infite loops trying to read in DIEs as a prerequisite for
24006 reading current DIE.
24007 * Not trying to dereference contents of still incompletely read in types
24008 while reading in other DIEs.
24009 * Enable referencing still incompletely read in types just by a pointer to
24010 the type without accessing its fields.
24012 Therefore caller should follow these rules:
24013 * Try to fetch any prerequisite types we may need to build this DIE type
24014 before building the type and calling set_die_type.
24015 * After building type call set_die_type for current DIE as soon as
24016 possible before fetching more types to complete the current type.
24017 * Make the type as complete as possible before fetching more types. */
24019 static struct type *
24020 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24022 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24023 struct objfile *objfile = cu->objfile;
24024 struct attribute *attr;
24025 struct dynamic_prop prop;
24027 /* For Ada types, make sure that the gnat-specific data is always
24028 initialized (if not already set). There are a few types where
24029 we should not be doing so, because the type-specific area is
24030 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24031 where the type-specific area is used to store the floatformat).
24032 But this is not a problem, because the gnat-specific information
24033 is actually not needed for these types. */
24034 if (need_gnat_info (cu)
24035 && TYPE_CODE (type) != TYPE_CODE_FUNC
24036 && TYPE_CODE (type) != TYPE_CODE_FLT
24037 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24038 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24039 && TYPE_CODE (type) != TYPE_CODE_METHOD
24040 && !HAVE_GNAT_AUX_INFO (type))
24041 INIT_GNAT_SPECIFIC (type);
24043 /* Read DW_AT_allocated and set in type. */
24044 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24045 if (attr_form_is_block (attr))
24047 if (attr_to_dynamic_prop (attr, die, cu, &prop))
24048 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
24050 else if (attr != NULL)
24052 complaint (&symfile_complaints,
24053 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
24054 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24055 to_underlying (die->sect_off));
24058 /* Read DW_AT_associated and set in type. */
24059 attr = dwarf2_attr (die, DW_AT_associated, cu);
24060 if (attr_form_is_block (attr))
24062 if (attr_to_dynamic_prop (attr, die, cu, &prop))
24063 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
24065 else if (attr != NULL)
24067 complaint (&symfile_complaints,
24068 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
24069 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24070 to_underlying (die->sect_off));
24073 /* Read DW_AT_data_location and set in type. */
24074 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24075 if (attr_to_dynamic_prop (attr, die, cu, &prop))
24076 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
24078 if (dwarf2_per_objfile->die_type_hash == NULL)
24080 dwarf2_per_objfile->die_type_hash =
24081 htab_create_alloc_ex (127,
24082 per_cu_offset_and_type_hash,
24083 per_cu_offset_and_type_eq,
24085 &objfile->objfile_obstack,
24086 hashtab_obstack_allocate,
24087 dummy_obstack_deallocate);
24090 ofs.per_cu = cu->per_cu;
24091 ofs.sect_off = die->sect_off;
24093 slot = (struct dwarf2_per_cu_offset_and_type **)
24094 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
24096 complaint (&symfile_complaints,
24097 _("A problem internal to GDB: DIE 0x%x has type already set"),
24098 to_underlying (die->sect_off));
24099 *slot = XOBNEW (&objfile->objfile_obstack,
24100 struct dwarf2_per_cu_offset_and_type);
24105 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24106 or return NULL if the die does not have a saved type. */
24108 static struct type *
24109 get_die_type_at_offset (sect_offset sect_off,
24110 struct dwarf2_per_cu_data *per_cu)
24112 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24114 if (dwarf2_per_objfile->die_type_hash == NULL)
24117 ofs.per_cu = per_cu;
24118 ofs.sect_off = sect_off;
24119 slot = ((struct dwarf2_per_cu_offset_and_type *)
24120 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
24127 /* Look up the type for DIE in CU in die_type_hash,
24128 or return NULL if DIE does not have a saved type. */
24130 static struct type *
24131 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24133 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24136 /* Add a dependence relationship from CU to REF_PER_CU. */
24139 dwarf2_add_dependence (struct dwarf2_cu *cu,
24140 struct dwarf2_per_cu_data *ref_per_cu)
24144 if (cu->dependencies == NULL)
24146 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24147 NULL, &cu->comp_unit_obstack,
24148 hashtab_obstack_allocate,
24149 dummy_obstack_deallocate);
24151 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24153 *slot = ref_per_cu;
24156 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24157 Set the mark field in every compilation unit in the
24158 cache that we must keep because we are keeping CU. */
24161 dwarf2_mark_helper (void **slot, void *data)
24163 struct dwarf2_per_cu_data *per_cu;
24165 per_cu = (struct dwarf2_per_cu_data *) *slot;
24167 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24168 reading of the chain. As such dependencies remain valid it is not much
24169 useful to track and undo them during QUIT cleanups. */
24170 if (per_cu->cu == NULL)
24173 if (per_cu->cu->mark)
24175 per_cu->cu->mark = 1;
24177 if (per_cu->cu->dependencies != NULL)
24178 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24183 /* Set the mark field in CU and in every other compilation unit in the
24184 cache that we must keep because we are keeping CU. */
24187 dwarf2_mark (struct dwarf2_cu *cu)
24192 if (cu->dependencies != NULL)
24193 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24197 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24201 per_cu->cu->mark = 0;
24202 per_cu = per_cu->cu->read_in_chain;
24206 /* Trivial hash function for partial_die_info: the hash value of a DIE
24207 is its offset in .debug_info for this objfile. */
24210 partial_die_hash (const void *item)
24212 const struct partial_die_info *part_die
24213 = (const struct partial_die_info *) item;
24215 return to_underlying (part_die->sect_off);
24218 /* Trivial comparison function for partial_die_info structures: two DIEs
24219 are equal if they have the same offset. */
24222 partial_die_eq (const void *item_lhs, const void *item_rhs)
24224 const struct partial_die_info *part_die_lhs
24225 = (const struct partial_die_info *) item_lhs;
24226 const struct partial_die_info *part_die_rhs
24227 = (const struct partial_die_info *) item_rhs;
24229 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24232 static struct cmd_list_element *set_dwarf_cmdlist;
24233 static struct cmd_list_element *show_dwarf_cmdlist;
24236 set_dwarf_cmd (const char *args, int from_tty)
24238 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24243 show_dwarf_cmd (const char *args, int from_tty)
24245 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24248 /* Free data associated with OBJFILE, if necessary. */
24251 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
24253 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
24256 /* Make sure we don't accidentally use dwarf2_per_objfile while
24258 dwarf2_per_objfile = NULL;
24260 for (ix = 0; ix < data->n_comp_units; ++ix)
24261 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
24263 for (ix = 0; ix < data->n_type_units; ++ix)
24264 VEC_free (dwarf2_per_cu_ptr,
24265 data->all_type_units[ix]->per_cu.imported_symtabs);
24266 xfree (data->all_type_units);
24268 VEC_free (dwarf2_section_info_def, data->types);
24270 if (data->dwo_files)
24271 free_dwo_files (data->dwo_files, objfile);
24272 if (data->dwp_file)
24273 gdb_bfd_unref (data->dwp_file->dbfd);
24275 if (data->dwz_file && data->dwz_file->dwz_bfd)
24276 gdb_bfd_unref (data->dwz_file->dwz_bfd);
24278 if (data->index_table != NULL)
24279 data->index_table->~mapped_index ();
24283 /* The "save gdb-index" command. */
24285 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
24289 file_write (FILE *file, const void *data, size_t size)
24291 if (fwrite (data, 1, size, file) != size)
24292 error (_("couldn't data write to file"));
24295 /* Write the contents of VEC to FILE, with error checking. */
24297 template<typename Elem, typename Alloc>
24299 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
24301 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
24304 /* In-memory buffer to prepare data to be written later to a file. */
24308 /* Copy DATA to the end of the buffer. */
24309 template<typename T>
24310 void append_data (const T &data)
24312 std::copy (reinterpret_cast<const gdb_byte *> (&data),
24313 reinterpret_cast<const gdb_byte *> (&data + 1),
24314 grow (sizeof (data)));
24317 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
24318 terminating zero is appended too. */
24319 void append_cstr0 (const char *cstr)
24321 const size_t size = strlen (cstr) + 1;
24322 std::copy (cstr, cstr + size, grow (size));
24325 /* Store INPUT as ULEB128 to the end of buffer. */
24326 void append_unsigned_leb128 (ULONGEST input)
24330 gdb_byte output = input & 0x7f;
24334 append_data (output);
24340 /* Accept a host-format integer in VAL and append it to the buffer
24341 as a target-format integer which is LEN bytes long. */
24342 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
24344 ::store_unsigned_integer (grow (len), len, byte_order, val);
24347 /* Return the size of the buffer. */
24348 size_t size () const
24350 return m_vec.size ();
24353 /* Return true iff the buffer is empty. */
24354 bool empty () const
24356 return m_vec.empty ();
24359 /* Write the buffer to FILE. */
24360 void file_write (FILE *file) const
24362 ::file_write (file, m_vec);
24366 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
24367 the start of the new block. */
24368 gdb_byte *grow (size_t size)
24370 m_vec.resize (m_vec.size () + size);
24371 return &*m_vec.end () - size;
24374 gdb::byte_vector m_vec;
24377 /* An entry in the symbol table. */
24378 struct symtab_index_entry
24380 /* The name of the symbol. */
24382 /* The offset of the name in the constant pool. */
24383 offset_type index_offset;
24384 /* A sorted vector of the indices of all the CUs that hold an object
24386 std::vector<offset_type> cu_indices;
24389 /* The symbol table. This is a power-of-2-sized hash table. */
24390 struct mapped_symtab
24394 data.resize (1024);
24397 offset_type n_elements = 0;
24398 std::vector<symtab_index_entry> data;
24401 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
24404 Function is used only during write_hash_table so no index format backward
24405 compatibility is needed. */
24407 static symtab_index_entry &
24408 find_slot (struct mapped_symtab *symtab, const char *name)
24410 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
24412 index = hash & (symtab->data.size () - 1);
24413 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
24417 if (symtab->data[index].name == NULL
24418 || strcmp (name, symtab->data[index].name) == 0)
24419 return symtab->data[index];
24420 index = (index + step) & (symtab->data.size () - 1);
24424 /* Expand SYMTAB's hash table. */
24427 hash_expand (struct mapped_symtab *symtab)
24429 auto old_entries = std::move (symtab->data);
24431 symtab->data.clear ();
24432 symtab->data.resize (old_entries.size () * 2);
24434 for (auto &it : old_entries)
24435 if (it.name != NULL)
24437 auto &ref = find_slot (symtab, it.name);
24438 ref = std::move (it);
24442 /* Add an entry to SYMTAB. NAME is the name of the symbol.
24443 CU_INDEX is the index of the CU in which the symbol appears.
24444 IS_STATIC is one if the symbol is static, otherwise zero (global). */
24447 add_index_entry (struct mapped_symtab *symtab, const char *name,
24448 int is_static, gdb_index_symbol_kind kind,
24449 offset_type cu_index)
24451 offset_type cu_index_and_attrs;
24453 ++symtab->n_elements;
24454 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
24455 hash_expand (symtab);
24457 symtab_index_entry &slot = find_slot (symtab, name);
24458 if (slot.name == NULL)
24461 /* index_offset is set later. */
24464 cu_index_and_attrs = 0;
24465 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
24466 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
24467 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
24469 /* We don't want to record an index value twice as we want to avoid the
24471 We process all global symbols and then all static symbols
24472 (which would allow us to avoid the duplication by only having to check
24473 the last entry pushed), but a symbol could have multiple kinds in one CU.
24474 To keep things simple we don't worry about the duplication here and
24475 sort and uniqufy the list after we've processed all symbols. */
24476 slot.cu_indices.push_back (cu_index_and_attrs);
24479 /* Sort and remove duplicates of all symbols' cu_indices lists. */
24482 uniquify_cu_indices (struct mapped_symtab *symtab)
24484 for (auto &entry : symtab->data)
24486 if (entry.name != NULL && !entry.cu_indices.empty ())
24488 auto &cu_indices = entry.cu_indices;
24489 std::sort (cu_indices.begin (), cu_indices.end ());
24490 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
24491 cu_indices.erase (from, cu_indices.end ());
24496 /* A form of 'const char *' suitable for container keys. Only the
24497 pointer is stored. The strings themselves are compared, not the
24502 c_str_view (const char *cstr)
24506 bool operator== (const c_str_view &other) const
24508 return strcmp (m_cstr, other.m_cstr) == 0;
24511 /* Return the underlying C string. Note, the returned string is
24512 only a reference with lifetime of this object. */
24513 const char *c_str () const
24519 friend class c_str_view_hasher;
24520 const char *const m_cstr;
24523 /* A std::unordered_map::hasher for c_str_view that uses the right
24524 hash function for strings in a mapped index. */
24525 class c_str_view_hasher
24528 size_t operator () (const c_str_view &x) const
24530 return mapped_index_string_hash (INT_MAX, x.m_cstr);
24534 /* A std::unordered_map::hasher for std::vector<>. */
24535 template<typename T>
24536 class vector_hasher
24539 size_t operator () (const std::vector<T> &key) const
24541 return iterative_hash (key.data (),
24542 sizeof (key.front ()) * key.size (), 0);
24546 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
24547 constant pool entries going into the data buffer CPOOL. */
24550 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
24553 /* Elements are sorted vectors of the indices of all the CUs that
24554 hold an object of this name. */
24555 std::unordered_map<std::vector<offset_type>, offset_type,
24556 vector_hasher<offset_type>>
24559 /* We add all the index vectors to the constant pool first, to
24560 ensure alignment is ok. */
24561 for (symtab_index_entry &entry : symtab->data)
24563 if (entry.name == NULL)
24565 gdb_assert (entry.index_offset == 0);
24567 /* Finding before inserting is faster than always trying to
24568 insert, because inserting always allocates a node, does the
24569 lookup, and then destroys the new node if another node
24570 already had the same key. C++17 try_emplace will avoid
24573 = symbol_hash_table.find (entry.cu_indices);
24574 if (found != symbol_hash_table.end ())
24576 entry.index_offset = found->second;
24580 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
24581 entry.index_offset = cpool.size ();
24582 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
24583 for (const auto index : entry.cu_indices)
24584 cpool.append_data (MAYBE_SWAP (index));
24588 /* Now write out the hash table. */
24589 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
24590 for (const auto &entry : symtab->data)
24592 offset_type str_off, vec_off;
24594 if (entry.name != NULL)
24596 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
24597 if (insertpair.second)
24598 cpool.append_cstr0 (entry.name);
24599 str_off = insertpair.first->second;
24600 vec_off = entry.index_offset;
24604 /* While 0 is a valid constant pool index, it is not valid
24605 to have 0 for both offsets. */
24610 output.append_data (MAYBE_SWAP (str_off));
24611 output.append_data (MAYBE_SWAP (vec_off));
24615 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
24617 /* Helper struct for building the address table. */
24618 struct addrmap_index_data
24620 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
24621 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
24624 struct objfile *objfile;
24625 data_buf &addr_vec;
24626 psym_index_map &cu_index_htab;
24628 /* Non-zero if the previous_* fields are valid.
24629 We can't write an entry until we see the next entry (since it is only then
24630 that we know the end of the entry). */
24631 int previous_valid;
24632 /* Index of the CU in the table of all CUs in the index file. */
24633 unsigned int previous_cu_index;
24634 /* Start address of the CU. */
24635 CORE_ADDR previous_cu_start;
24638 /* Write an address entry to ADDR_VEC. */
24641 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
24642 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
24644 CORE_ADDR baseaddr;
24646 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24648 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
24649 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
24650 addr_vec.append_data (MAYBE_SWAP (cu_index));
24653 /* Worker function for traversing an addrmap to build the address table. */
24656 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
24658 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
24659 struct partial_symtab *pst = (struct partial_symtab *) obj;
24661 if (data->previous_valid)
24662 add_address_entry (data->objfile, data->addr_vec,
24663 data->previous_cu_start, start_addr,
24664 data->previous_cu_index);
24666 data->previous_cu_start = start_addr;
24669 const auto it = data->cu_index_htab.find (pst);
24670 gdb_assert (it != data->cu_index_htab.cend ());
24671 data->previous_cu_index = it->second;
24672 data->previous_valid = 1;
24675 data->previous_valid = 0;
24680 /* Write OBJFILE's address map to ADDR_VEC.
24681 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
24682 in the index file. */
24685 write_address_map (struct objfile *objfile, data_buf &addr_vec,
24686 psym_index_map &cu_index_htab)
24688 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
24690 /* When writing the address table, we have to cope with the fact that
24691 the addrmap iterator only provides the start of a region; we have to
24692 wait until the next invocation to get the start of the next region. */
24694 addrmap_index_data.objfile = objfile;
24695 addrmap_index_data.previous_valid = 0;
24697 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
24698 &addrmap_index_data);
24700 /* It's highly unlikely the last entry (end address = 0xff...ff)
24701 is valid, but we should still handle it.
24702 The end address is recorded as the start of the next region, but that
24703 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
24705 if (addrmap_index_data.previous_valid)
24706 add_address_entry (objfile, addr_vec,
24707 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
24708 addrmap_index_data.previous_cu_index);
24711 /* Return the symbol kind of PSYM. */
24713 static gdb_index_symbol_kind
24714 symbol_kind (struct partial_symbol *psym)
24716 domain_enum domain = PSYMBOL_DOMAIN (psym);
24717 enum address_class aclass = PSYMBOL_CLASS (psym);
24725 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
24727 return GDB_INDEX_SYMBOL_KIND_TYPE;
24729 case LOC_CONST_BYTES:
24730 case LOC_OPTIMIZED_OUT:
24732 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24734 /* Note: It's currently impossible to recognize psyms as enum values
24735 short of reading the type info. For now punt. */
24736 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
24738 /* There are other LOC_FOO values that one might want to classify
24739 as variables, but dwarf2read.c doesn't currently use them. */
24740 return GDB_INDEX_SYMBOL_KIND_OTHER;
24742 case STRUCT_DOMAIN:
24743 return GDB_INDEX_SYMBOL_KIND_TYPE;
24745 return GDB_INDEX_SYMBOL_KIND_OTHER;
24749 /* Add a list of partial symbols to SYMTAB. */
24752 write_psymbols (struct mapped_symtab *symtab,
24753 std::unordered_set<partial_symbol *> &psyms_seen,
24754 struct partial_symbol **psymp,
24756 offset_type cu_index,
24759 for (; count-- > 0; ++psymp)
24761 struct partial_symbol *psym = *psymp;
24763 if (SYMBOL_LANGUAGE (psym) == language_ada)
24764 error (_("Ada is not currently supported by the index"));
24766 /* Only add a given psymbol once. */
24767 if (psyms_seen.insert (psym).second)
24769 gdb_index_symbol_kind kind = symbol_kind (psym);
24771 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
24772 is_static, kind, cu_index);
24777 /* A helper struct used when iterating over debug_types. */
24778 struct signatured_type_index_data
24780 signatured_type_index_data (data_buf &types_list_,
24781 std::unordered_set<partial_symbol *> &psyms_seen_)
24782 : types_list (types_list_), psyms_seen (psyms_seen_)
24785 struct objfile *objfile;
24786 struct mapped_symtab *symtab;
24787 data_buf &types_list;
24788 std::unordered_set<partial_symbol *> &psyms_seen;
24792 /* A helper function that writes a single signatured_type to an
24796 write_one_signatured_type (void **slot, void *d)
24798 struct signatured_type_index_data *info
24799 = (struct signatured_type_index_data *) d;
24800 struct signatured_type *entry = (struct signatured_type *) *slot;
24801 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
24803 write_psymbols (info->symtab,
24805 &info->objfile->global_psymbols[psymtab->globals_offset],
24806 psymtab->n_global_syms, info->cu_index,
24808 write_psymbols (info->symtab,
24810 &info->objfile->static_psymbols[psymtab->statics_offset],
24811 psymtab->n_static_syms, info->cu_index,
24814 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24815 to_underlying (entry->per_cu.sect_off));
24816 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
24817 to_underlying (entry->type_offset_in_tu));
24818 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
24825 /* Recurse into all "included" dependencies and count their symbols as
24826 if they appeared in this psymtab. */
24829 recursively_count_psymbols (struct partial_symtab *psymtab,
24830 size_t &psyms_seen)
24832 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
24833 if (psymtab->dependencies[i]->user != NULL)
24834 recursively_count_psymbols (psymtab->dependencies[i],
24837 psyms_seen += psymtab->n_global_syms;
24838 psyms_seen += psymtab->n_static_syms;
24841 /* Recurse into all "included" dependencies and write their symbols as
24842 if they appeared in this psymtab. */
24845 recursively_write_psymbols (struct objfile *objfile,
24846 struct partial_symtab *psymtab,
24847 struct mapped_symtab *symtab,
24848 std::unordered_set<partial_symbol *> &psyms_seen,
24849 offset_type cu_index)
24853 for (i = 0; i < psymtab->number_of_dependencies; ++i)
24854 if (psymtab->dependencies[i]->user != NULL)
24855 recursively_write_psymbols (objfile, psymtab->dependencies[i],
24856 symtab, psyms_seen, cu_index);
24858 write_psymbols (symtab,
24860 &objfile->global_psymbols[psymtab->globals_offset],
24861 psymtab->n_global_syms, cu_index,
24863 write_psymbols (symtab,
24865 &objfile->static_psymbols[psymtab->statics_offset],
24866 psymtab->n_static_syms, cu_index,
24870 /* Symbol name hashing function as specified by DWARF-5. */
24873 dwarf5_djb_hash (const char *str_)
24875 const unsigned char *str = (const unsigned char *) str_;
24877 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
24878 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
24880 uint32_t hash = 5381;
24881 while (int c = *str++)
24882 hash = hash * 33 + tolower (c);
24886 /* DWARF-5 .debug_names builder. */
24890 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
24891 : m_dwarf5_byte_order (dwarf5_byte_order),
24892 m_dwarf32 (dwarf5_byte_order),
24893 m_dwarf64 (dwarf5_byte_order),
24894 m_dwarf (is_dwarf64
24895 ? static_cast<dwarf &> (m_dwarf64)
24896 : static_cast<dwarf &> (m_dwarf32)),
24897 m_name_table_string_offs (m_dwarf.name_table_string_offs),
24898 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
24901 /* Insert one symbol. */
24902 void insert (const partial_symbol *psym, int cu_index, bool is_static)
24904 const int dwarf_tag = psymbol_tag (psym);
24905 if (dwarf_tag == 0)
24907 const char *const name = SYMBOL_SEARCH_NAME (psym);
24908 const auto insertpair
24909 = m_name_to_value_set.emplace (c_str_view (name),
24910 std::set<symbol_value> ());
24911 std::set<symbol_value> &value_set = insertpair.first->second;
24912 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static));
24915 /* Build all the tables. All symbols must be already inserted.
24916 This function does not call file_write, caller has to do it
24920 /* Verify the build method has not be called twice. */
24921 gdb_assert (m_abbrev_table.empty ());
24922 const size_t name_count = m_name_to_value_set.size ();
24923 m_bucket_table.resize
24924 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
24925 m_hash_table.reserve (name_count);
24926 m_name_table_string_offs.reserve (name_count);
24927 m_name_table_entry_offs.reserve (name_count);
24929 /* Map each hash of symbol to its name and value. */
24930 struct hash_it_pair
24933 decltype (m_name_to_value_set)::const_iterator it;
24935 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
24936 bucket_hash.resize (m_bucket_table.size ());
24937 for (decltype (m_name_to_value_set)::const_iterator it
24938 = m_name_to_value_set.cbegin ();
24939 it != m_name_to_value_set.cend ();
24942 const char *const name = it->first.c_str ();
24943 const uint32_t hash = dwarf5_djb_hash (name);
24944 hash_it_pair hashitpair;
24945 hashitpair.hash = hash;
24946 hashitpair.it = it;
24947 auto &slot = bucket_hash[hash % bucket_hash.size()];
24948 slot.push_front (std::move (hashitpair));
24950 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
24952 const std::forward_list<hash_it_pair> &hashitlist
24953 = bucket_hash[bucket_ix];
24954 if (hashitlist.empty ())
24956 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
24957 /* The hashes array is indexed starting at 1. */
24958 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
24959 sizeof (bucket_slot), m_dwarf5_byte_order,
24960 m_hash_table.size () + 1);
24961 for (const hash_it_pair &hashitpair : hashitlist)
24963 m_hash_table.push_back (0);
24964 store_unsigned_integer (reinterpret_cast<gdb_byte *>
24965 (&m_hash_table.back ()),
24966 sizeof (m_hash_table.back ()),
24967 m_dwarf5_byte_order, hashitpair.hash);
24968 const c_str_view &name = hashitpair.it->first;
24969 const std::set<symbol_value> &value_set = hashitpair.it->second;
24970 m_name_table_string_offs.push_back_reorder
24971 (m_debugstrlookup.lookup (name.c_str ()));
24972 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
24973 gdb_assert (!value_set.empty ());
24974 for (const symbol_value &value : value_set)
24976 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
24980 idx = m_idx_next++;
24981 m_abbrev_table.append_unsigned_leb128 (idx);
24982 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
24983 m_abbrev_table.append_unsigned_leb128 (DW_IDX_compile_unit);
24984 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
24985 m_abbrev_table.append_unsigned_leb128 (value.is_static
24986 ? DW_IDX_GNU_internal
24987 : DW_IDX_GNU_external);
24988 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
24990 /* Terminate attributes list. */
24991 m_abbrev_table.append_unsigned_leb128 (0);
24992 m_abbrev_table.append_unsigned_leb128 (0);
24995 m_entry_pool.append_unsigned_leb128 (idx);
24996 m_entry_pool.append_unsigned_leb128 (value.cu_index);
24999 /* Terminate the list of CUs. */
25000 m_entry_pool.append_unsigned_leb128 (0);
25003 gdb_assert (m_hash_table.size () == name_count);
25005 /* Terminate tags list. */
25006 m_abbrev_table.append_unsigned_leb128 (0);
25009 /* Return .debug_names bucket count. This must be called only after
25010 calling the build method. */
25011 uint32_t bucket_count () const
25013 /* Verify the build method has been already called. */
25014 gdb_assert (!m_abbrev_table.empty ());
25015 const uint32_t retval = m_bucket_table.size ();
25017 /* Check for overflow. */
25018 gdb_assert (retval == m_bucket_table.size ());
25022 /* Return .debug_names names count. This must be called only after
25023 calling the build method. */
25024 uint32_t name_count () const
25026 /* Verify the build method has been already called. */
25027 gdb_assert (!m_abbrev_table.empty ());
25028 const uint32_t retval = m_hash_table.size ();
25030 /* Check for overflow. */
25031 gdb_assert (retval == m_hash_table.size ());
25035 /* Return number of bytes of .debug_names abbreviation table. This
25036 must be called only after calling the build method. */
25037 uint32_t abbrev_table_bytes () const
25039 gdb_assert (!m_abbrev_table.empty ());
25040 return m_abbrev_table.size ();
25043 /* Recurse into all "included" dependencies and store their symbols
25044 as if they appeared in this psymtab. */
25045 void recursively_write_psymbols
25046 (struct objfile *objfile,
25047 struct partial_symtab *psymtab,
25048 std::unordered_set<partial_symbol *> &psyms_seen,
25051 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
25052 if (psymtab->dependencies[i]->user != NULL)
25053 recursively_write_psymbols (objfile, psymtab->dependencies[i],
25054 psyms_seen, cu_index);
25056 write_psymbols (psyms_seen,
25057 &objfile->global_psymbols[psymtab->globals_offset],
25058 psymtab->n_global_syms, cu_index, false);
25059 write_psymbols (psyms_seen,
25060 &objfile->static_psymbols[psymtab->statics_offset],
25061 psymtab->n_static_syms, cu_index, true);
25064 /* Return number of bytes the .debug_names section will have. This
25065 must be called only after calling the build method. */
25066 size_t bytes () const
25068 /* Verify the build method has been already called. */
25069 gdb_assert (!m_abbrev_table.empty ());
25070 size_t expected_bytes = 0;
25071 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
25072 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
25073 expected_bytes += m_name_table_string_offs.bytes ();
25074 expected_bytes += m_name_table_entry_offs.bytes ();
25075 expected_bytes += m_abbrev_table.size ();
25076 expected_bytes += m_entry_pool.size ();
25077 return expected_bytes;
25080 /* Write .debug_names to FILE_NAMES and .debug_str addition to
25081 FILE_STR. This must be called only after calling the build
25083 void file_write (FILE *file_names, FILE *file_str) const
25085 /* Verify the build method has been already called. */
25086 gdb_assert (!m_abbrev_table.empty ());
25087 ::file_write (file_names, m_bucket_table);
25088 ::file_write (file_names, m_hash_table);
25089 m_name_table_string_offs.file_write (file_names);
25090 m_name_table_entry_offs.file_write (file_names);
25091 m_abbrev_table.file_write (file_names);
25092 m_entry_pool.file_write (file_names);
25093 m_debugstrlookup.file_write (file_str);
25098 /* Storage for symbol names mapping them to their .debug_str section
25100 class debug_str_lookup
25104 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
25105 All .debug_str section strings are automatically stored. */
25106 debug_str_lookup ()
25107 : m_abfd (dwarf2_per_objfile->objfile->obfd)
25109 dwarf2_read_section (dwarf2_per_objfile->objfile,
25110 &dwarf2_per_objfile->str);
25111 if (dwarf2_per_objfile->str.buffer == NULL)
25113 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
25114 data < (dwarf2_per_objfile->str.buffer
25115 + dwarf2_per_objfile->str.size);)
25117 const char *const s = reinterpret_cast<const char *> (data);
25118 const auto insertpair
25119 = m_str_table.emplace (c_str_view (s),
25120 data - dwarf2_per_objfile->str.buffer);
25121 if (!insertpair.second)
25122 complaint (&symfile_complaints,
25123 _("Duplicate string \"%s\" in "
25124 ".debug_str section [in module %s]"),
25125 s, bfd_get_filename (m_abfd));
25126 data += strlen (s) + 1;
25130 /* Return offset of symbol name S in the .debug_str section. Add
25131 such symbol to the section's end if it does not exist there
25133 size_t lookup (const char *s)
25135 const auto it = m_str_table.find (c_str_view (s));
25136 if (it != m_str_table.end ())
25138 const size_t offset = (dwarf2_per_objfile->str.size
25139 + m_str_add_buf.size ());
25140 m_str_table.emplace (c_str_view (s), offset);
25141 m_str_add_buf.append_cstr0 (s);
25145 /* Append the end of the .debug_str section to FILE. */
25146 void file_write (FILE *file) const
25148 m_str_add_buf.file_write (file);
25152 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
25155 /* Data to add at the end of .debug_str for new needed symbol names. */
25156 data_buf m_str_add_buf;
25159 /* Container to map used DWARF tags to their .debug_names abbreviation
25164 index_key (int dwarf_tag_, bool is_static_)
25165 : dwarf_tag (dwarf_tag_), is_static (is_static_)
25170 operator== (const index_key &other) const
25172 return dwarf_tag == other.dwarf_tag && is_static == other.is_static;
25175 const int dwarf_tag;
25176 const bool is_static;
25179 /* Provide std::unordered_map::hasher for index_key. */
25180 class index_key_hasher
25184 operator () (const index_key &key) const
25186 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
25190 /* Parameters of one symbol entry. */
25194 const int dwarf_tag, cu_index;
25195 const bool is_static;
25197 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_)
25198 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_)
25202 operator< (const symbol_value &other) const
25221 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
25226 const bfd_endian dwarf5_byte_order;
25228 explicit offset_vec (bfd_endian dwarf5_byte_order_)
25229 : dwarf5_byte_order (dwarf5_byte_order_)
25232 /* Call std::vector::reserve for NELEM elements. */
25233 virtual void reserve (size_t nelem) = 0;
25235 /* Call std::vector::push_back with store_unsigned_integer byte
25236 reordering for ELEM. */
25237 virtual void push_back_reorder (size_t elem) = 0;
25239 /* Return expected output size in bytes. */
25240 virtual size_t bytes () const = 0;
25242 /* Write name table to FILE. */
25243 virtual void file_write (FILE *file) const = 0;
25246 /* Template to unify DWARF-32 and DWARF-64 output. */
25247 template<typename OffsetSize>
25248 class offset_vec_tmpl : public offset_vec
25251 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
25252 : offset_vec (dwarf5_byte_order_)
25255 /* Implement offset_vec::reserve. */
25256 void reserve (size_t nelem) override
25258 m_vec.reserve (nelem);
25261 /* Implement offset_vec::push_back_reorder. */
25262 void push_back_reorder (size_t elem) override
25264 m_vec.push_back (elem);
25265 /* Check for overflow. */
25266 gdb_assert (m_vec.back () == elem);
25267 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
25268 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
25271 /* Implement offset_vec::bytes. */
25272 size_t bytes () const override
25274 return m_vec.size () * sizeof (m_vec[0]);
25277 /* Implement offset_vec::file_write. */
25278 void file_write (FILE *file) const override
25280 ::file_write (file, m_vec);
25284 std::vector<OffsetSize> m_vec;
25287 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
25288 respecting name table width. */
25292 offset_vec &name_table_string_offs, &name_table_entry_offs;
25294 dwarf (offset_vec &name_table_string_offs_,
25295 offset_vec &name_table_entry_offs_)
25296 : name_table_string_offs (name_table_string_offs_),
25297 name_table_entry_offs (name_table_entry_offs_)
25302 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
25303 respecting name table width. */
25304 template<typename OffsetSize>
25305 class dwarf_tmpl : public dwarf
25308 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
25309 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
25310 m_name_table_string_offs (dwarf5_byte_order_),
25311 m_name_table_entry_offs (dwarf5_byte_order_)
25315 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
25316 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
25319 /* Try to reconstruct original DWARF tag for given partial_symbol.
25320 This function is not DWARF-5 compliant but it is sufficient for
25321 GDB as a DWARF-5 index consumer. */
25322 static int psymbol_tag (const struct partial_symbol *psym)
25324 domain_enum domain = PSYMBOL_DOMAIN (psym);
25325 enum address_class aclass = PSYMBOL_CLASS (psym);
25333 return DW_TAG_subprogram;
25335 return DW_TAG_typedef;
25337 case LOC_CONST_BYTES:
25338 case LOC_OPTIMIZED_OUT:
25340 return DW_TAG_variable;
25342 /* Note: It's currently impossible to recognize psyms as enum values
25343 short of reading the type info. For now punt. */
25344 return DW_TAG_variable;
25346 /* There are other LOC_FOO values that one might want to classify
25347 as variables, but dwarf2read.c doesn't currently use them. */
25348 return DW_TAG_variable;
25350 case STRUCT_DOMAIN:
25351 return DW_TAG_structure_type;
25357 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
25358 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
25359 struct partial_symbol **psymp, int count, int cu_index,
25362 for (; count-- > 0; ++psymp)
25364 struct partial_symbol *psym = *psymp;
25366 if (SYMBOL_LANGUAGE (psym) == language_ada)
25367 error (_("Ada is not currently supported by the index"));
25369 /* Only add a given psymbol once. */
25370 if (psyms_seen.insert (psym).second)
25371 insert (psym, cu_index, is_static);
25375 /* Store value of each symbol. */
25376 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
25377 m_name_to_value_set;
25379 /* Tables of DWARF-5 .debug_names. They are in object file byte
25381 std::vector<uint32_t> m_bucket_table;
25382 std::vector<uint32_t> m_hash_table;
25384 const bfd_endian m_dwarf5_byte_order;
25385 dwarf_tmpl<uint32_t> m_dwarf32;
25386 dwarf_tmpl<uint64_t> m_dwarf64;
25388 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
25389 debug_str_lookup m_debugstrlookup;
25391 /* Map each used .debug_names abbreviation tag parameter to its
25393 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
25395 /* Next unused .debug_names abbreviation tag for
25396 m_indexkey_to_idx. */
25397 int m_idx_next = 1;
25399 /* .debug_names abbreviation table. */
25400 data_buf m_abbrev_table;
25402 /* .debug_names entry pool. */
25403 data_buf m_entry_pool;
25406 /* Return iff any of the needed offsets does not fit into 32-bit
25407 .debug_names section. */
25410 check_dwarf64_offsets ()
25412 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
25414 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
25416 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
25419 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
25421 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
25422 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
25424 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
25430 /* The psyms_seen set is potentially going to be largish (~40k
25431 elements when indexing a -g3 build of GDB itself). Estimate the
25432 number of elements in order to avoid too many rehashes, which
25433 require rebuilding buckets and thus many trips to
25439 size_t psyms_count = 0;
25440 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
25442 struct dwarf2_per_cu_data *per_cu
25443 = dwarf2_per_objfile->all_comp_units[i];
25444 struct partial_symtab *psymtab = per_cu->v.psymtab;
25446 if (psymtab != NULL && psymtab->user == NULL)
25447 recursively_count_psymbols (psymtab, psyms_count);
25449 /* Generating an index for gdb itself shows a ratio of
25450 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
25451 return psyms_count / 4;
25454 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
25455 Return how many bytes were expected to be written into OUT_FILE. */
25458 write_gdbindex (struct objfile *objfile, FILE *out_file)
25460 mapped_symtab symtab;
25463 /* While we're scanning CU's create a table that maps a psymtab pointer
25464 (which is what addrmap records) to its index (which is what is recorded
25465 in the index file). This will later be needed to write the address
25467 psym_index_map cu_index_htab;
25468 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
25470 /* The CU list is already sorted, so we don't need to do additional
25471 work here. Also, the debug_types entries do not appear in
25472 all_comp_units, but only in their own hash table. */
25474 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
25475 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
25477 struct dwarf2_per_cu_data *per_cu
25478 = dwarf2_per_objfile->all_comp_units[i];
25479 struct partial_symtab *psymtab = per_cu->v.psymtab;
25481 /* CU of a shared file from 'dwz -m' may be unused by this main file.
25482 It may be referenced from a local scope but in such case it does not
25483 need to be present in .gdb_index. */
25484 if (psymtab == NULL)
25487 if (psymtab->user == NULL)
25488 recursively_write_psymbols (objfile, psymtab, &symtab,
25491 const auto insertpair = cu_index_htab.emplace (psymtab, i);
25492 gdb_assert (insertpair.second);
25494 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
25495 to_underlying (per_cu->sect_off));
25496 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
25499 /* Dump the address map. */
25501 write_address_map (objfile, addr_vec, cu_index_htab);
25503 /* Write out the .debug_type entries, if any. */
25504 data_buf types_cu_list;
25505 if (dwarf2_per_objfile->signatured_types)
25507 signatured_type_index_data sig_data (types_cu_list,
25510 sig_data.objfile = objfile;
25511 sig_data.symtab = &symtab;
25512 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
25513 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
25514 write_one_signatured_type, &sig_data);
25517 /* Now that we've processed all symbols we can shrink their cu_indices
25519 uniquify_cu_indices (&symtab);
25521 data_buf symtab_vec, constant_pool;
25522 write_hash_table (&symtab, symtab_vec, constant_pool);
25525 const offset_type size_of_contents = 6 * sizeof (offset_type);
25526 offset_type total_len = size_of_contents;
25528 /* The version number. */
25529 contents.append_data (MAYBE_SWAP (8));
25531 /* The offset of the CU list from the start of the file. */
25532 contents.append_data (MAYBE_SWAP (total_len));
25533 total_len += cu_list.size ();
25535 /* The offset of the types CU list from the start of the file. */
25536 contents.append_data (MAYBE_SWAP (total_len));
25537 total_len += types_cu_list.size ();
25539 /* The offset of the address table from the start of the file. */
25540 contents.append_data (MAYBE_SWAP (total_len));
25541 total_len += addr_vec.size ();
25543 /* The offset of the symbol table from the start of the file. */
25544 contents.append_data (MAYBE_SWAP (total_len));
25545 total_len += symtab_vec.size ();
25547 /* The offset of the constant pool from the start of the file. */
25548 contents.append_data (MAYBE_SWAP (total_len));
25549 total_len += constant_pool.size ();
25551 gdb_assert (contents.size () == size_of_contents);
25553 contents.file_write (out_file);
25554 cu_list.file_write (out_file);
25555 types_cu_list.file_write (out_file);
25556 addr_vec.file_write (out_file);
25557 symtab_vec.file_write (out_file);
25558 constant_pool.file_write (out_file);
25563 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
25564 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
25566 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
25567 needed addition to .debug_str section to OUT_FILE_STR. Return how
25568 many bytes were expected to be written into OUT_FILE. */
25571 write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
25573 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
25574 const int dwarf5_offset_size = dwarf5_is_dwarf64 ? 8 : 4;
25575 const enum bfd_endian dwarf5_byte_order
25576 = gdbarch_byte_order (get_objfile_arch (objfile));
25578 /* The CU list is already sorted, so we don't need to do additional
25579 work here. Also, the debug_types entries do not appear in
25580 all_comp_units, but only in their own hash table. */
25582 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
25583 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
25584 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
25586 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
25587 partial_symtab *psymtab = per_cu->v.psymtab;
25589 /* CU of a shared file from 'dwz -m' may be unused by this main
25590 file. It may be referenced from a local scope but in such
25591 case it does not need to be present in .debug_names. */
25592 if (psymtab == NULL)
25595 if (psymtab->user == NULL)
25596 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
25598 cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
25599 to_underlying (per_cu->sect_off));
25601 nametable.build ();
25603 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
25605 data_buf types_cu_list;
25606 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
25608 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
25609 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
25611 types_cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
25612 to_underlying (per_cu.sect_off));
25615 const offset_type bytes_of_header
25616 = ((dwarf5_is_dwarf64 ? 12 : 4)
25618 + sizeof (dwarf5_gdb_augmentation));
25619 size_t expected_bytes = 0;
25620 expected_bytes += bytes_of_header;
25621 expected_bytes += cu_list.size ();
25622 expected_bytes += types_cu_list.size ();
25623 expected_bytes += nametable.bytes ();
25626 if (!dwarf5_is_dwarf64)
25628 const uint64_t size64 = expected_bytes - 4;
25629 gdb_assert (size64 < 0xfffffff0);
25630 header.append_uint (4, dwarf5_byte_order, size64);
25634 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
25635 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
25638 /* The version number. */
25639 header.append_uint (2, dwarf5_byte_order, 5);
25642 header.append_uint (2, dwarf5_byte_order, 0);
25644 /* comp_unit_count - The number of CUs in the CU list. */
25645 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
25647 /* local_type_unit_count - The number of TUs in the local TU
25649 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
25651 /* foreign_type_unit_count - The number of TUs in the foreign TU
25653 header.append_uint (4, dwarf5_byte_order, 0);
25655 /* bucket_count - The number of hash buckets in the hash lookup
25657 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
25659 /* name_count - The number of unique names in the index. */
25660 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
25662 /* abbrev_table_size - The size in bytes of the abbreviations
25664 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
25666 /* augmentation_string_size - The size in bytes of the augmentation
25667 string. This value is rounded up to a multiple of 4. */
25668 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
25669 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
25670 header.append_data (dwarf5_gdb_augmentation);
25672 gdb_assert (header.size () == bytes_of_header);
25674 header.file_write (out_file);
25675 cu_list.file_write (out_file);
25676 types_cu_list.file_write (out_file);
25677 nametable.file_write (out_file, out_file_str);
25679 return expected_bytes;
25682 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
25683 position is at the end of the file. */
25686 assert_file_size (FILE *file, const char *filename, size_t expected_size)
25688 const auto file_size = ftell (file);
25689 if (file_size == -1)
25690 error (_("Can't get `%s' size"), filename);
25691 gdb_assert (file_size == expected_size);
25694 /* An index variant. */
25697 /* GDB's own .gdb_index format. */
25700 /* DWARF5 .debug_names. */
25704 /* Create an index file for OBJFILE in the directory DIR. */
25707 write_psymtabs_to_index (struct objfile *objfile, const char *dir,
25708 dw_index_kind index_kind)
25710 if (dwarf2_per_objfile->using_index)
25711 error (_("Cannot use an index to create the index"));
25713 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
25714 error (_("Cannot make an index when the file has multiple .debug_types sections"));
25716 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
25720 if (stat (objfile_name (objfile), &st) < 0)
25721 perror_with_name (objfile_name (objfile));
25723 std::string filename (std::string (dir) + SLASH_STRING
25724 + lbasename (objfile_name (objfile))
25725 + (index_kind == dw_index_kind::DEBUG_NAMES
25726 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
25728 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
25730 error (_("Can't open `%s' for writing"), filename.c_str ());
25732 /* Order matters here; we want FILE to be closed before FILENAME is
25733 unlinked, because on MS-Windows one cannot delete a file that is
25734 still open. (Don't call anything here that might throw until
25735 file_closer is created.) */
25736 gdb::unlinker unlink_file (filename.c_str ());
25737 gdb_file_up close_out_file (out_file);
25739 if (index_kind == dw_index_kind::DEBUG_NAMES)
25741 std::string filename_str (std::string (dir) + SLASH_STRING
25742 + lbasename (objfile_name (objfile))
25743 + DEBUG_STR_SUFFIX);
25745 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
25747 error (_("Can't open `%s' for writing"), filename_str.c_str ());
25748 gdb::unlinker unlink_file_str (filename_str.c_str ());
25749 gdb_file_up close_out_file_str (out_file_str);
25751 const size_t total_len
25752 = write_debug_names (objfile, out_file, out_file_str);
25753 assert_file_size (out_file, filename.c_str (), total_len);
25755 /* We want to keep the file .debug_str file too. */
25756 unlink_file_str.keep ();
25760 const size_t total_len
25761 = write_gdbindex (objfile, out_file);
25762 assert_file_size (out_file, filename.c_str (), total_len);
25765 /* We want to keep the file. */
25766 unlink_file.keep ();
25769 /* Implementation of the `save gdb-index' command.
25771 Note that the .gdb_index file format used by this command is
25772 documented in the GDB manual. Any changes here must be documented
25776 save_gdb_index_command (const char *arg, int from_tty)
25778 struct objfile *objfile;
25779 const char dwarf5space[] = "-dwarf-5 ";
25780 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
25785 arg = skip_spaces (arg);
25786 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
25788 index_kind = dw_index_kind::DEBUG_NAMES;
25789 arg += strlen (dwarf5space);
25790 arg = skip_spaces (arg);
25794 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
25796 ALL_OBJFILES (objfile)
25800 /* If the objfile does not correspond to an actual file, skip it. */
25801 if (stat (objfile_name (objfile), &st) < 0)
25805 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25806 dwarf2_objfile_data_key);
25807 if (dwarf2_per_objfile)
25812 write_psymtabs_to_index (objfile, arg, index_kind);
25814 CATCH (except, RETURN_MASK_ERROR)
25816 exception_fprintf (gdb_stderr, except,
25817 _("Error while writing index for `%s': "),
25818 objfile_name (objfile));
25827 int dwarf_always_disassemble;
25830 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25831 struct cmd_list_element *c, const char *value)
25833 fprintf_filtered (file,
25834 _("Whether to always disassemble "
25835 "DWARF expressions is %s.\n"),
25840 show_check_physname (struct ui_file *file, int from_tty,
25841 struct cmd_list_element *c, const char *value)
25843 fprintf_filtered (file,
25844 _("Whether to check \"physname\" is %s.\n"),
25849 _initialize_dwarf2_read (void)
25851 struct cmd_list_element *c;
25853 dwarf2_objfile_data_key
25854 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
25856 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25857 Set DWARF specific variables.\n\
25858 Configure DWARF variables such as the cache size"),
25859 &set_dwarf_cmdlist, "maintenance set dwarf ",
25860 0/*allow-unknown*/, &maintenance_set_cmdlist);
25862 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25863 Show DWARF specific variables\n\
25864 Show DWARF variables such as the cache size"),
25865 &show_dwarf_cmdlist, "maintenance show dwarf ",
25866 0/*allow-unknown*/, &maintenance_show_cmdlist);
25868 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25869 &dwarf_max_cache_age, _("\
25870 Set the upper bound on the age of cached DWARF compilation units."), _("\
25871 Show the upper bound on the age of cached DWARF compilation units."), _("\
25872 A higher limit means that cached compilation units will be stored\n\
25873 in memory longer, and more total memory will be used. Zero disables\n\
25874 caching, which can slow down startup."),
25876 show_dwarf_max_cache_age,
25877 &set_dwarf_cmdlist,
25878 &show_dwarf_cmdlist);
25880 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25881 &dwarf_always_disassemble, _("\
25882 Set whether `info address' always disassembles DWARF expressions."), _("\
25883 Show whether `info address' always disassembles DWARF expressions."), _("\
25884 When enabled, DWARF expressions are always printed in an assembly-like\n\
25885 syntax. When disabled, expressions will be printed in a more\n\
25886 conversational style, when possible."),
25888 show_dwarf_always_disassemble,
25889 &set_dwarf_cmdlist,
25890 &show_dwarf_cmdlist);
25892 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25893 Set debugging of the DWARF reader."), _("\
25894 Show debugging of the DWARF reader."), _("\
25895 When enabled (non-zero), debugging messages are printed during DWARF\n\
25896 reading and symtab expansion. A value of 1 (one) provides basic\n\
25897 information. A value greater than 1 provides more verbose information."),
25900 &setdebuglist, &showdebuglist);
25902 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25903 Set debugging of the DWARF DIE reader."), _("\
25904 Show debugging of the DWARF DIE reader."), _("\
25905 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25906 The value is the maximum depth to print."),
25909 &setdebuglist, &showdebuglist);
25911 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25912 Set debugging of the dwarf line reader."), _("\
25913 Show debugging of the dwarf line reader."), _("\
25914 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25915 A value of 1 (one) provides basic information.\n\
25916 A value greater than 1 provides more verbose information."),
25919 &setdebuglist, &showdebuglist);
25921 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25922 Set cross-checking of \"physname\" code against demangler."), _("\
25923 Show cross-checking of \"physname\" code against demangler."), _("\
25924 When enabled, GDB's internal \"physname\" code is checked against\n\
25926 NULL, show_check_physname,
25927 &setdebuglist, &showdebuglist);
25929 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25930 no_class, &use_deprecated_index_sections, _("\
25931 Set whether to use deprecated gdb_index sections."), _("\
25932 Show whether to use deprecated gdb_index sections."), _("\
25933 When enabled, deprecated .gdb_index sections are used anyway.\n\
25934 Normally they are ignored either because of a missing feature or\n\
25935 performance issue.\n\
25936 Warning: This option must be enabled before gdb reads the file."),
25939 &setlist, &showlist);
25941 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
25943 Save a gdb-index file.\n\
25944 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
25946 No options create one file with .gdb-index extension for pre-DWARF-5\n\
25947 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
25948 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
25950 set_cmd_completer (c, filename_completer);
25952 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25953 &dwarf2_locexpr_funcs);
25954 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25955 &dwarf2_loclist_funcs);
25957 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25958 &dwarf2_block_frame_base_locexpr_funcs);
25959 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25960 &dwarf2_block_frame_base_loclist_funcs);
25963 selftests::register_test ("dw2_expand_symtabs_matching",
25964 selftests::dw2_expand_symtabs_matching::run_test);