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"
78 #include <sys/types.h>
80 #include <unordered_set>
81 #include <unordered_map>
83 typedef struct symbol *symbolp;
86 /* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89 static unsigned int dwarf_read_debug = 0;
91 /* When non-zero, dump DIEs after they are read in. */
92 static unsigned int dwarf_die_debug = 0;
94 /* When non-zero, dump line number entries as they are read in. */
95 static unsigned int dwarf_line_debug = 0;
97 /* When non-zero, cross-check physname against demangler. */
98 static int check_physname = 0;
100 /* When non-zero, do not reject deprecated .gdb_index sections. */
101 static int use_deprecated_index_sections = 0;
103 static const struct objfile_data *dwarf2_objfile_data_key;
105 /* The "aclass" indices for various kinds of computed DWARF symbols. */
107 static int dwarf2_locexpr_index;
108 static int dwarf2_loclist_index;
109 static int dwarf2_locexpr_block_index;
110 static int dwarf2_loclist_block_index;
112 /* A descriptor for dwarf sections.
114 S.ASECTION, SIZE are typically initialized when the objfile is first
115 scanned. BUFFER, READIN are filled in later when the section is read.
116 If the section contained compressed data then SIZE is updated to record
117 the uncompressed size of the section.
119 DWP file format V2 introduces a wrinkle that is easiest to handle by
120 creating the concept of virtual sections contained within a real section.
121 In DWP V2 the sections of the input DWO files are concatenated together
122 into one section, but section offsets are kept relative to the original
124 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
125 the real section this "virtual" section is contained in, and BUFFER,SIZE
126 describe the virtual section. */
128 struct dwarf2_section_info
132 /* If this is a real section, the bfd section. */
134 /* If this is a virtual section, pointer to the containing ("real")
136 struct dwarf2_section_info *containing_section;
138 /* Pointer to section data, only valid if readin. */
139 const gdb_byte *buffer;
140 /* The size of the section, real or virtual. */
142 /* If this is a virtual section, the offset in the real section.
143 Only valid if is_virtual. */
144 bfd_size_type virtual_offset;
145 /* True if we have tried to read this section. */
147 /* True if this is a virtual section, False otherwise.
148 This specifies which of s.section and s.containing_section to use. */
152 typedef struct dwarf2_section_info dwarf2_section_info_def;
153 DEF_VEC_O (dwarf2_section_info_def);
155 /* All offsets in the index are of this type. It must be
156 architecture-independent. */
157 typedef uint32_t offset_type;
159 DEF_VEC_I (offset_type);
161 /* Ensure only legit values are used. */
162 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
164 gdb_assert ((unsigned int) (value) <= 1); \
165 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
168 /* Ensure only legit values are used. */
169 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
171 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
172 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
173 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
176 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
177 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
179 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
180 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
183 /* A description of the mapped index. The file format is described in
184 a comment by the code that writes the index. */
187 /* Index data format version. */
190 /* The total length of the buffer. */
193 /* A pointer to the address table data. */
194 const gdb_byte *address_table;
196 /* Size of the address table data in bytes. */
197 offset_type address_table_size;
199 /* The symbol table, implemented as a hash table. */
200 const offset_type *symbol_table;
202 /* Size in slots, each slot is 2 offset_types. */
203 offset_type symbol_table_slots;
205 /* A pointer to the constant pool. */
206 const char *constant_pool;
209 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
210 DEF_VEC_P (dwarf2_per_cu_ptr);
214 int nr_uniq_abbrev_tables;
216 int nr_symtab_sharers;
217 int nr_stmt_less_type_units;
218 int nr_all_type_units_reallocs;
221 /* Collection of data recorded per objfile.
222 This hangs off of dwarf2_objfile_data_key. */
224 struct dwarf2_per_objfile
226 struct dwarf2_section_info info;
227 struct dwarf2_section_info abbrev;
228 struct dwarf2_section_info line;
229 struct dwarf2_section_info loc;
230 struct dwarf2_section_info loclists;
231 struct dwarf2_section_info macinfo;
232 struct dwarf2_section_info macro;
233 struct dwarf2_section_info str;
234 struct dwarf2_section_info line_str;
235 struct dwarf2_section_info ranges;
236 struct dwarf2_section_info rnglists;
237 struct dwarf2_section_info addr;
238 struct dwarf2_section_info frame;
239 struct dwarf2_section_info eh_frame;
240 struct dwarf2_section_info gdb_index;
242 VEC (dwarf2_section_info_def) *types;
245 struct objfile *objfile;
247 /* Table of all the compilation units. This is used to locate
248 the target compilation unit of a particular reference. */
249 struct dwarf2_per_cu_data **all_comp_units;
251 /* The number of compilation units in ALL_COMP_UNITS. */
254 /* The number of .debug_types-related CUs. */
257 /* The number of elements allocated in all_type_units.
258 If there are skeleton-less TUs, we add them to all_type_units lazily. */
259 int n_allocated_type_units;
261 /* The .debug_types-related CUs (TUs).
262 This is stored in malloc space because we may realloc it. */
263 struct signatured_type **all_type_units;
265 /* Table of struct type_unit_group objects.
266 The hash key is the DW_AT_stmt_list value. */
267 htab_t type_unit_groups;
269 /* A table mapping .debug_types signatures to its signatured_type entry.
270 This is NULL if the .debug_types section hasn't been read in yet. */
271 htab_t signatured_types;
273 /* Type unit statistics, to see how well the scaling improvements
275 struct tu_stats tu_stats;
277 /* A chain of compilation units that are currently read in, so that
278 they can be freed later. */
279 struct dwarf2_per_cu_data *read_in_chain;
281 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
282 This is NULL if the table hasn't been allocated yet. */
285 /* Non-zero if we've check for whether there is a DWP file. */
288 /* The DWP file if there is one, or NULL. */
289 struct dwp_file *dwp_file;
291 /* The shared '.dwz' file, if one exists. This is used when the
292 original data was compressed using 'dwz -m'. */
293 struct dwz_file *dwz_file;
295 /* A flag indicating wether this objfile has a section loaded at a
297 int has_section_at_zero;
299 /* True if we are using the mapped index,
300 or we are faking it for OBJF_READNOW's sake. */
301 unsigned char using_index;
303 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
304 struct mapped_index *index_table;
306 /* When using index_table, this keeps track of all quick_file_names entries.
307 TUs typically share line table entries with a CU, so we maintain a
308 separate table of all line table entries to support the sharing.
309 Note that while there can be way more TUs than CUs, we've already
310 sorted all the TUs into "type unit groups", grouped by their
311 DW_AT_stmt_list value. Therefore the only sharing done here is with a
312 CU and its associated TU group if there is one. */
313 htab_t quick_file_names_table;
315 /* Set during partial symbol reading, to prevent queueing of full
317 int reading_partial_symbols;
319 /* Table mapping type DIEs to their struct type *.
320 This is NULL if not allocated yet.
321 The mapping is done via (CU/TU + DIE offset) -> type. */
322 htab_t die_type_hash;
324 /* The CUs we recently read. */
325 VEC (dwarf2_per_cu_ptr) *just_read_cus;
327 /* Table containing line_header indexed by offset and offset_in_dwz. */
328 htab_t line_header_hash;
331 static struct dwarf2_per_objfile *dwarf2_per_objfile;
333 /* Default names of the debugging sections. */
335 /* Note that if the debugging section has been compressed, it might
336 have a name like .zdebug_info. */
338 static const struct dwarf2_debug_sections dwarf2_elf_names =
340 { ".debug_info", ".zdebug_info" },
341 { ".debug_abbrev", ".zdebug_abbrev" },
342 { ".debug_line", ".zdebug_line" },
343 { ".debug_loc", ".zdebug_loc" },
344 { ".debug_loclists", ".zdebug_loclists" },
345 { ".debug_macinfo", ".zdebug_macinfo" },
346 { ".debug_macro", ".zdebug_macro" },
347 { ".debug_str", ".zdebug_str" },
348 { ".debug_line_str", ".zdebug_line_str" },
349 { ".debug_ranges", ".zdebug_ranges" },
350 { ".debug_rnglists", ".zdebug_rnglists" },
351 { ".debug_types", ".zdebug_types" },
352 { ".debug_addr", ".zdebug_addr" },
353 { ".debug_frame", ".zdebug_frame" },
354 { ".eh_frame", NULL },
355 { ".gdb_index", ".zgdb_index" },
359 /* List of DWO/DWP sections. */
361 static const struct dwop_section_names
363 struct dwarf2_section_names abbrev_dwo;
364 struct dwarf2_section_names info_dwo;
365 struct dwarf2_section_names line_dwo;
366 struct dwarf2_section_names loc_dwo;
367 struct dwarf2_section_names loclists_dwo;
368 struct dwarf2_section_names macinfo_dwo;
369 struct dwarf2_section_names macro_dwo;
370 struct dwarf2_section_names str_dwo;
371 struct dwarf2_section_names str_offsets_dwo;
372 struct dwarf2_section_names types_dwo;
373 struct dwarf2_section_names cu_index;
374 struct dwarf2_section_names tu_index;
378 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
379 { ".debug_info.dwo", ".zdebug_info.dwo" },
380 { ".debug_line.dwo", ".zdebug_line.dwo" },
381 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
382 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
383 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
384 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
385 { ".debug_str.dwo", ".zdebug_str.dwo" },
386 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
387 { ".debug_types.dwo", ".zdebug_types.dwo" },
388 { ".debug_cu_index", ".zdebug_cu_index" },
389 { ".debug_tu_index", ".zdebug_tu_index" },
392 /* local data types */
394 /* The data in a compilation unit header, after target2host
395 translation, looks like this. */
396 struct comp_unit_head
400 unsigned char addr_size;
401 unsigned char signed_addr_p;
402 sect_offset abbrev_sect_off;
404 /* Size of file offsets; either 4 or 8. */
405 unsigned int offset_size;
407 /* Size of the length field; either 4 or 12. */
408 unsigned int initial_length_size;
410 enum dwarf_unit_type unit_type;
412 /* Offset to the first byte of this compilation unit header in the
413 .debug_info section, for resolving relative reference dies. */
414 sect_offset sect_off;
416 /* Offset to first die in this cu from the start of the cu.
417 This will be the first byte following the compilation unit header. */
418 cu_offset first_die_cu_offset;
420 /* 64-bit signature of this type unit - it is valid only for
421 UNIT_TYPE DW_UT_type. */
424 /* For types, offset in the type's DIE of the type defined by this TU. */
425 cu_offset type_cu_offset_in_tu;
428 /* Type used for delaying computation of method physnames.
429 See comments for compute_delayed_physnames. */
430 struct delayed_method_info
432 /* The type to which the method is attached, i.e., its parent class. */
435 /* The index of the method in the type's function fieldlists. */
438 /* The index of the method in the fieldlist. */
441 /* The name of the DIE. */
444 /* The DIE associated with this method. */
445 struct die_info *die;
448 typedef struct delayed_method_info delayed_method_info;
449 DEF_VEC_O (delayed_method_info);
451 /* Internal state when decoding a particular compilation unit. */
454 /* The objfile containing this compilation unit. */
455 struct objfile *objfile;
457 /* The header of the compilation unit. */
458 struct comp_unit_head header;
460 /* Base address of this compilation unit. */
461 CORE_ADDR base_address;
463 /* Non-zero if base_address has been set. */
466 /* The language we are debugging. */
467 enum language language;
468 const struct language_defn *language_defn;
470 const char *producer;
472 /* The generic symbol table building routines have separate lists for
473 file scope symbols and all all other scopes (local scopes). So
474 we need to select the right one to pass to add_symbol_to_list().
475 We do it by keeping a pointer to the correct list in list_in_scope.
477 FIXME: The original dwarf code just treated the file scope as the
478 first local scope, and all other local scopes as nested local
479 scopes, and worked fine. Check to see if we really need to
480 distinguish these in buildsym.c. */
481 struct pending **list_in_scope;
483 /* The abbrev table for this CU.
484 Normally this points to the abbrev table in the objfile.
485 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
486 struct abbrev_table *abbrev_table;
488 /* Hash table holding all the loaded partial DIEs
489 with partial_die->offset.SECT_OFF as hash. */
492 /* Storage for things with the same lifetime as this read-in compilation
493 unit, including partial DIEs. */
494 struct obstack comp_unit_obstack;
496 /* When multiple dwarf2_cu structures are living in memory, this field
497 chains them all together, so that they can be released efficiently.
498 We will probably also want a generation counter so that most-recently-used
499 compilation units are cached... */
500 struct dwarf2_per_cu_data *read_in_chain;
502 /* Backlink to our per_cu entry. */
503 struct dwarf2_per_cu_data *per_cu;
505 /* How many compilation units ago was this CU last referenced? */
508 /* A hash table of DIE cu_offset for following references with
509 die_info->offset.sect_off as hash. */
512 /* Full DIEs if read in. */
513 struct die_info *dies;
515 /* A set of pointers to dwarf2_per_cu_data objects for compilation
516 units referenced by this one. Only set during full symbol processing;
517 partial symbol tables do not have dependencies. */
520 /* Header data from the line table, during full symbol processing. */
521 struct line_header *line_header;
523 /* A list of methods which need to have physnames computed
524 after all type information has been read. */
525 VEC (delayed_method_info) *method_list;
527 /* To be copied to symtab->call_site_htab. */
528 htab_t call_site_htab;
530 /* Non-NULL if this CU came from a DWO file.
531 There is an invariant here that is important to remember:
532 Except for attributes copied from the top level DIE in the "main"
533 (or "stub") file in preparation for reading the DWO file
534 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
535 Either there isn't a DWO file (in which case this is NULL and the point
536 is moot), or there is and either we're not going to read it (in which
537 case this is NULL) or there is and we are reading it (in which case this
539 struct dwo_unit *dwo_unit;
541 /* The DW_AT_addr_base attribute if present, zero otherwise
542 (zero is a valid value though).
543 Note this value comes from the Fission stub CU/TU's DIE. */
546 /* The DW_AT_ranges_base attribute if present, zero otherwise
547 (zero is a valid value though).
548 Note this value comes from the Fission stub CU/TU's DIE.
549 Also note that the value is zero in the non-DWO case so this value can
550 be used without needing to know whether DWO files are in use or not.
551 N.B. This does not apply to DW_AT_ranges appearing in
552 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
553 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
554 DW_AT_ranges_base *would* have to be applied, and we'd have to care
555 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
556 ULONGEST ranges_base;
558 /* Mark used when releasing cached dies. */
559 unsigned int mark : 1;
561 /* This CU references .debug_loc. See the symtab->locations_valid field.
562 This test is imperfect as there may exist optimized debug code not using
563 any location list and still facing inlining issues if handled as
564 unoptimized code. For a future better test see GCC PR other/32998. */
565 unsigned int has_loclist : 1;
567 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
568 if all the producer_is_* fields are valid. This information is cached
569 because profiling CU expansion showed excessive time spent in
570 producer_is_gxx_lt_4_6. */
571 unsigned int checked_producer : 1;
572 unsigned int producer_is_gxx_lt_4_6 : 1;
573 unsigned int producer_is_gcc_lt_4_3 : 1;
574 unsigned int producer_is_icc : 1;
576 /* When set, the file that we're processing is known to have
577 debugging info for C++ namespaces. GCC 3.3.x did not produce
578 this information, but later versions do. */
580 unsigned int processing_has_namespace_info : 1;
583 /* Persistent data held for a compilation unit, even when not
584 processing it. We put a pointer to this structure in the
585 read_symtab_private field of the psymtab. */
587 struct dwarf2_per_cu_data
589 /* The start offset and length of this compilation unit.
590 NOTE: Unlike comp_unit_head.length, this length includes
592 If the DIE refers to a DWO file, this is always of the original die,
594 sect_offset sect_off;
597 /* DWARF standard version this data has been read from (such as 4 or 5). */
600 /* Flag indicating this compilation unit will be read in before
601 any of the current compilation units are processed. */
602 unsigned int queued : 1;
604 /* This flag will be set when reading partial DIEs if we need to load
605 absolutely all DIEs for this compilation unit, instead of just the ones
606 we think are interesting. It gets set if we look for a DIE in the
607 hash table and don't find it. */
608 unsigned int load_all_dies : 1;
610 /* Non-zero if this CU is from .debug_types.
611 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
613 unsigned int is_debug_types : 1;
615 /* Non-zero if this CU is from the .dwz file. */
616 unsigned int is_dwz : 1;
618 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
619 This flag is only valid if is_debug_types is true.
620 We can't read a CU directly from a DWO file: There are required
621 attributes in the stub. */
622 unsigned int reading_dwo_directly : 1;
624 /* Non-zero if the TU has been read.
625 This is used to assist the "Stay in DWO Optimization" for Fission:
626 When reading a DWO, it's faster to read TUs from the DWO instead of
627 fetching them from random other DWOs (due to comdat folding).
628 If the TU has already been read, the optimization is unnecessary
629 (and unwise - we don't want to change where gdb thinks the TU lives
631 This flag is only valid if is_debug_types is true. */
632 unsigned int tu_read : 1;
634 /* The section this CU/TU lives in.
635 If the DIE refers to a DWO file, this is always the original die,
637 struct dwarf2_section_info *section;
639 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
640 of the CU cache it gets reset to NULL again. This is left as NULL for
641 dummy CUs (a CU header, but nothing else). */
642 struct dwarf2_cu *cu;
644 /* The corresponding objfile.
645 Normally we can get the objfile from dwarf2_per_objfile.
646 However we can enter this file with just a "per_cu" handle. */
647 struct objfile *objfile;
649 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
650 is active. Otherwise, the 'psymtab' field is active. */
653 /* The partial symbol table associated with this compilation unit,
654 or NULL for unread partial units. */
655 struct partial_symtab *psymtab;
657 /* Data needed by the "quick" functions. */
658 struct dwarf2_per_cu_quick_data *quick;
661 /* The CUs we import using DW_TAG_imported_unit. This is filled in
662 while reading psymtabs, used to compute the psymtab dependencies,
663 and then cleared. Then it is filled in again while reading full
664 symbols, and only deleted when the objfile is destroyed.
666 This is also used to work around a difference between the way gold
667 generates .gdb_index version <=7 and the way gdb does. Arguably this
668 is a gold bug. For symbols coming from TUs, gold records in the index
669 the CU that includes the TU instead of the TU itself. This breaks
670 dw2_lookup_symbol: It assumes that if the index says symbol X lives
671 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
672 will find X. Alas TUs live in their own symtab, so after expanding CU Y
673 we need to look in TU Z to find X. Fortunately, this is akin to
674 DW_TAG_imported_unit, so we just use the same mechanism: For
675 .gdb_index version <=7 this also records the TUs that the CU referred
676 to. Concurrently with this change gdb was modified to emit version 8
677 indices so we only pay a price for gold generated indices.
678 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
679 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
682 /* Entry in the signatured_types hash table. */
684 struct signatured_type
686 /* The "per_cu" object of this type.
687 This struct is used iff per_cu.is_debug_types.
688 N.B.: This is the first member so that it's easy to convert pointers
690 struct dwarf2_per_cu_data per_cu;
692 /* The type's signature. */
695 /* Offset in the TU of the type's DIE, as read from the TU header.
696 If this TU is a DWO stub and the definition lives in a DWO file
697 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
698 cu_offset type_offset_in_tu;
700 /* Offset in the section of the type's DIE.
701 If the definition lives in a DWO file, this is the offset in the
702 .debug_types.dwo section.
703 The value is zero until the actual value is known.
704 Zero is otherwise not a valid section offset. */
705 sect_offset type_offset_in_section;
707 /* Type units are grouped by their DW_AT_stmt_list entry so that they
708 can share them. This points to the containing symtab. */
709 struct type_unit_group *type_unit_group;
712 The first time we encounter this type we fully read it in and install it
713 in the symbol tables. Subsequent times we only need the type. */
716 /* Containing DWO unit.
717 This field is valid iff per_cu.reading_dwo_directly. */
718 struct dwo_unit *dwo_unit;
721 typedef struct signatured_type *sig_type_ptr;
722 DEF_VEC_P (sig_type_ptr);
724 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
725 This includes type_unit_group and quick_file_names. */
727 struct stmt_list_hash
729 /* The DWO unit this table is from or NULL if there is none. */
730 struct dwo_unit *dwo_unit;
732 /* Offset in .debug_line or .debug_line.dwo. */
733 sect_offset line_sect_off;
736 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
737 an object of this type. */
739 struct type_unit_group
741 /* dwarf2read.c's main "handle" on a TU symtab.
742 To simplify things we create an artificial CU that "includes" all the
743 type units using this stmt_list so that the rest of the code still has
744 a "per_cu" handle on the symtab.
745 This PER_CU is recognized by having no section. */
746 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
747 struct dwarf2_per_cu_data per_cu;
749 /* The TUs that share this DW_AT_stmt_list entry.
750 This is added to while parsing type units to build partial symtabs,
751 and is deleted afterwards and not used again. */
752 VEC (sig_type_ptr) *tus;
754 /* The compunit symtab.
755 Type units in a group needn't all be defined in the same source file,
756 so we create an essentially anonymous symtab as the compunit symtab. */
757 struct compunit_symtab *compunit_symtab;
759 /* The data used to construct the hash key. */
760 struct stmt_list_hash hash;
762 /* The number of symtabs from the line header.
763 The value here must match line_header.num_file_names. */
764 unsigned int num_symtabs;
766 /* The symbol tables for this TU (obtained from the files listed in
768 WARNING: The order of entries here must match the order of entries
769 in the line header. After the first TU using this type_unit_group, the
770 line header for the subsequent TUs is recreated from this. This is done
771 because we need to use the same symtabs for each TU using the same
772 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
773 there's no guarantee the line header doesn't have duplicate entries. */
774 struct symtab **symtabs;
777 /* These sections are what may appear in a (real or virtual) DWO file. */
781 struct dwarf2_section_info abbrev;
782 struct dwarf2_section_info line;
783 struct dwarf2_section_info loc;
784 struct dwarf2_section_info loclists;
785 struct dwarf2_section_info macinfo;
786 struct dwarf2_section_info macro;
787 struct dwarf2_section_info str;
788 struct dwarf2_section_info str_offsets;
789 /* In the case of a virtual DWO file, these two are unused. */
790 struct dwarf2_section_info info;
791 VEC (dwarf2_section_info_def) *types;
794 /* CUs/TUs in DWP/DWO files. */
798 /* Backlink to the containing struct dwo_file. */
799 struct dwo_file *dwo_file;
801 /* The "id" that distinguishes this CU/TU.
802 .debug_info calls this "dwo_id", .debug_types calls this "signature".
803 Since signatures came first, we stick with it for consistency. */
806 /* The section this CU/TU lives in, in the DWO file. */
807 struct dwarf2_section_info *section;
809 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
810 sect_offset sect_off;
813 /* For types, offset in the type's DIE of the type defined by this TU. */
814 cu_offset type_offset_in_tu;
817 /* include/dwarf2.h defines the DWP section codes.
818 It defines a max value but it doesn't define a min value, which we
819 use for error checking, so provide one. */
821 enum dwp_v2_section_ids
826 /* Data for one DWO file.
828 This includes virtual DWO files (a virtual DWO file is a DWO file as it
829 appears in a DWP file). DWP files don't really have DWO files per se -
830 comdat folding of types "loses" the DWO file they came from, and from
831 a high level view DWP files appear to contain a mass of random types.
832 However, to maintain consistency with the non-DWP case we pretend DWP
833 files contain virtual DWO files, and we assign each TU with one virtual
834 DWO file (generally based on the line and abbrev section offsets -
835 a heuristic that seems to work in practice). */
839 /* The DW_AT_GNU_dwo_name attribute.
840 For virtual DWO files the name is constructed from the section offsets
841 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
842 from related CU+TUs. */
843 const char *dwo_name;
845 /* The DW_AT_comp_dir attribute. */
846 const char *comp_dir;
848 /* The bfd, when the file is open. Otherwise this is NULL.
849 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
852 /* The sections that make up this DWO file.
853 Remember that for virtual DWO files in DWP V2, these are virtual
854 sections (for lack of a better name). */
855 struct dwo_sections sections;
857 /* The CU in the file.
858 We only support one because having more than one requires hacking the
859 dwo_name of each to match, which is highly unlikely to happen.
860 Doing this means all TUs can share comp_dir: We also assume that
861 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
864 /* Table of TUs in the file.
865 Each element is a struct dwo_unit. */
869 /* These sections are what may appear in a DWP file. */
873 /* These are used by both DWP version 1 and 2. */
874 struct dwarf2_section_info str;
875 struct dwarf2_section_info cu_index;
876 struct dwarf2_section_info tu_index;
878 /* These are only used by DWP version 2 files.
879 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
880 sections are referenced by section number, and are not recorded here.
881 In DWP version 2 there is at most one copy of all these sections, each
882 section being (effectively) comprised of the concatenation of all of the
883 individual sections that exist in the version 1 format.
884 To keep the code simple we treat each of these concatenated pieces as a
885 section itself (a virtual section?). */
886 struct dwarf2_section_info abbrev;
887 struct dwarf2_section_info info;
888 struct dwarf2_section_info line;
889 struct dwarf2_section_info loc;
890 struct dwarf2_section_info macinfo;
891 struct dwarf2_section_info macro;
892 struct dwarf2_section_info str_offsets;
893 struct dwarf2_section_info types;
896 /* These sections are what may appear in a virtual DWO file in DWP version 1.
897 A virtual DWO file is a DWO file as it appears in a DWP file. */
899 struct virtual_v1_dwo_sections
901 struct dwarf2_section_info abbrev;
902 struct dwarf2_section_info line;
903 struct dwarf2_section_info loc;
904 struct dwarf2_section_info macinfo;
905 struct dwarf2_section_info macro;
906 struct dwarf2_section_info str_offsets;
907 /* Each DWP hash table entry records one CU or one TU.
908 That is recorded here, and copied to dwo_unit.section. */
909 struct dwarf2_section_info info_or_types;
912 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
913 In version 2, the sections of the DWO files are concatenated together
914 and stored in one section of that name. Thus each ELF section contains
915 several "virtual" sections. */
917 struct virtual_v2_dwo_sections
919 bfd_size_type abbrev_offset;
920 bfd_size_type abbrev_size;
922 bfd_size_type line_offset;
923 bfd_size_type line_size;
925 bfd_size_type loc_offset;
926 bfd_size_type loc_size;
928 bfd_size_type macinfo_offset;
929 bfd_size_type macinfo_size;
931 bfd_size_type macro_offset;
932 bfd_size_type macro_size;
934 bfd_size_type str_offsets_offset;
935 bfd_size_type str_offsets_size;
937 /* Each DWP hash table entry records one CU or one TU.
938 That is recorded here, and copied to dwo_unit.section. */
939 bfd_size_type info_or_types_offset;
940 bfd_size_type info_or_types_size;
943 /* Contents of DWP hash tables. */
945 struct dwp_hash_table
947 uint32_t version, nr_columns;
948 uint32_t nr_units, nr_slots;
949 const gdb_byte *hash_table, *unit_table;
954 const gdb_byte *indices;
958 /* This is indexed by column number and gives the id of the section
960 #define MAX_NR_V2_DWO_SECTIONS \
961 (1 /* .debug_info or .debug_types */ \
962 + 1 /* .debug_abbrev */ \
963 + 1 /* .debug_line */ \
964 + 1 /* .debug_loc */ \
965 + 1 /* .debug_str_offsets */ \
966 + 1 /* .debug_macro or .debug_macinfo */)
967 int section_ids[MAX_NR_V2_DWO_SECTIONS];
968 const gdb_byte *offsets;
969 const gdb_byte *sizes;
974 /* Data for one DWP file. */
978 /* Name of the file. */
981 /* File format version. */
987 /* Section info for this file. */
988 struct dwp_sections sections;
990 /* Table of CUs in the file. */
991 const struct dwp_hash_table *cus;
993 /* Table of TUs in the file. */
994 const struct dwp_hash_table *tus;
996 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1000 /* Table to map ELF section numbers to their sections.
1001 This is only needed for the DWP V1 file format. */
1002 unsigned int num_sections;
1003 asection **elf_sections;
1006 /* This represents a '.dwz' file. */
1010 /* A dwz file can only contain a few sections. */
1011 struct dwarf2_section_info abbrev;
1012 struct dwarf2_section_info info;
1013 struct dwarf2_section_info str;
1014 struct dwarf2_section_info line;
1015 struct dwarf2_section_info macro;
1016 struct dwarf2_section_info gdb_index;
1018 /* The dwz's BFD. */
1022 /* Struct used to pass misc. parameters to read_die_and_children, et
1023 al. which are used for both .debug_info and .debug_types dies.
1024 All parameters here are unchanging for the life of the call. This
1025 struct exists to abstract away the constant parameters of die reading. */
1027 struct die_reader_specs
1029 /* The bfd of die_section. */
1032 /* The CU of the DIE we are parsing. */
1033 struct dwarf2_cu *cu;
1035 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1036 struct dwo_file *dwo_file;
1038 /* The section the die comes from.
1039 This is either .debug_info or .debug_types, or the .dwo variants. */
1040 struct dwarf2_section_info *die_section;
1042 /* die_section->buffer. */
1043 const gdb_byte *buffer;
1045 /* The end of the buffer. */
1046 const gdb_byte *buffer_end;
1048 /* The value of the DW_AT_comp_dir attribute. */
1049 const char *comp_dir;
1052 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1053 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1054 const gdb_byte *info_ptr,
1055 struct die_info *comp_unit_die,
1059 /* A 1-based directory index. This is a strong typedef to prevent
1060 accidentally using a directory index as a 0-based index into an
1062 enum class dir_index : unsigned int {};
1064 /* Likewise, a 1-based file name index. */
1065 enum class file_name_index : unsigned int {};
1069 file_entry () = default;
1071 file_entry (const char *name_, dir_index d_index_,
1072 unsigned int mod_time_, unsigned int length_)
1075 mod_time (mod_time_),
1079 /* Return the include directory at D_INDEX stored in LH. Returns
1080 NULL if D_INDEX is out of bounds. */
1081 const char *include_dir (const line_header *lh) const;
1083 /* The file name. Note this is an observing pointer. The memory is
1084 owned by debug_line_buffer. */
1085 const char *name {};
1087 /* The directory index (1-based). */
1088 dir_index d_index {};
1090 unsigned int mod_time {};
1092 unsigned int length {};
1094 /* True if referenced by the Line Number Program. */
1097 /* The associated symbol table, if any. */
1098 struct symtab *symtab {};
1101 /* The line number information for a compilation unit (found in the
1102 .debug_line section) begins with a "statement program header",
1103 which contains the following information. */
1110 /* Add an entry to the include directory table. */
1111 void add_include_dir (const char *include_dir);
1113 /* Add an entry to the file name table. */
1114 void add_file_name (const char *name, dir_index d_index,
1115 unsigned int mod_time, unsigned int length);
1117 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1118 is out of bounds. */
1119 const char *include_dir_at (dir_index index) const
1121 /* Convert directory index number (1-based) to vector index
1123 size_t vec_index = to_underlying (index) - 1;
1125 if (vec_index >= include_dirs.size ())
1127 return include_dirs[vec_index];
1130 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1131 is out of bounds. */
1132 file_entry *file_name_at (file_name_index index)
1134 /* Convert file name index number (1-based) to vector index
1136 size_t vec_index = to_underlying (index) - 1;
1138 if (vec_index >= file_names.size ())
1140 return &file_names[vec_index];
1143 /* Const version of the above. */
1144 const file_entry *file_name_at (unsigned int index) const
1146 if (index >= file_names.size ())
1148 return &file_names[index];
1151 /* Offset of line number information in .debug_line section. */
1152 sect_offset sect_off {};
1154 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1155 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1157 unsigned int total_length {};
1158 unsigned short version {};
1159 unsigned int header_length {};
1160 unsigned char minimum_instruction_length {};
1161 unsigned char maximum_ops_per_instruction {};
1162 unsigned char default_is_stmt {};
1164 unsigned char line_range {};
1165 unsigned char opcode_base {};
1167 /* standard_opcode_lengths[i] is the number of operands for the
1168 standard opcode whose value is i. This means that
1169 standard_opcode_lengths[0] is unused, and the last meaningful
1170 element is standard_opcode_lengths[opcode_base - 1]. */
1171 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1173 /* The include_directories table. Note these are observing
1174 pointers. The memory is owned by debug_line_buffer. */
1175 std::vector<const char *> include_dirs;
1177 /* The file_names table. */
1178 std::vector<file_entry> file_names;
1180 /* The start and end of the statement program following this
1181 header. These point into dwarf2_per_objfile->line_buffer. */
1182 const gdb_byte *statement_program_start {}, *statement_program_end {};
1185 typedef std::unique_ptr<line_header> line_header_up;
1188 file_entry::include_dir (const line_header *lh) const
1190 return lh->include_dir_at (d_index);
1193 /* When we construct a partial symbol table entry we only
1194 need this much information. */
1195 struct partial_die_info
1197 /* Offset of this DIE. */
1198 sect_offset sect_off;
1200 /* DWARF-2 tag for this DIE. */
1201 ENUM_BITFIELD(dwarf_tag) tag : 16;
1203 /* Assorted flags describing the data found in this DIE. */
1204 unsigned int has_children : 1;
1205 unsigned int is_external : 1;
1206 unsigned int is_declaration : 1;
1207 unsigned int has_type : 1;
1208 unsigned int has_specification : 1;
1209 unsigned int has_pc_info : 1;
1210 unsigned int may_be_inlined : 1;
1212 /* This DIE has been marked DW_AT_main_subprogram. */
1213 unsigned int main_subprogram : 1;
1215 /* Flag set if the SCOPE field of this structure has been
1217 unsigned int scope_set : 1;
1219 /* Flag set if the DIE has a byte_size attribute. */
1220 unsigned int has_byte_size : 1;
1222 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1223 unsigned int has_const_value : 1;
1225 /* Flag set if any of the DIE's children are template arguments. */
1226 unsigned int has_template_arguments : 1;
1228 /* Flag set if fixup_partial_die has been called on this die. */
1229 unsigned int fixup_called : 1;
1231 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1232 unsigned int is_dwz : 1;
1234 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1235 unsigned int spec_is_dwz : 1;
1237 /* The name of this DIE. Normally the value of DW_AT_name, but
1238 sometimes a default name for unnamed DIEs. */
1241 /* The linkage name, if present. */
1242 const char *linkage_name;
1244 /* The scope to prepend to our children. This is generally
1245 allocated on the comp_unit_obstack, so will disappear
1246 when this compilation unit leaves the cache. */
1249 /* Some data associated with the partial DIE. The tag determines
1250 which field is live. */
1253 /* The location description associated with this DIE, if any. */
1254 struct dwarf_block *locdesc;
1255 /* The offset of an import, for DW_TAG_imported_unit. */
1256 sect_offset sect_off;
1259 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1263 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1264 DW_AT_sibling, if any. */
1265 /* NOTE: This member isn't strictly necessary, read_partial_die could
1266 return DW_AT_sibling values to its caller load_partial_dies. */
1267 const gdb_byte *sibling;
1269 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1270 DW_AT_specification (or DW_AT_abstract_origin or
1271 DW_AT_extension). */
1272 sect_offset spec_offset;
1274 /* Pointers to this DIE's parent, first child, and next sibling,
1276 struct partial_die_info *die_parent, *die_child, *die_sibling;
1279 /* This data structure holds the information of an abbrev. */
1282 unsigned int number; /* number identifying abbrev */
1283 enum dwarf_tag tag; /* dwarf tag */
1284 unsigned short has_children; /* boolean */
1285 unsigned short num_attrs; /* number of attributes */
1286 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1287 struct abbrev_info *next; /* next in chain */
1292 ENUM_BITFIELD(dwarf_attribute) name : 16;
1293 ENUM_BITFIELD(dwarf_form) form : 16;
1295 /* It is valid only if FORM is DW_FORM_implicit_const. */
1296 LONGEST implicit_const;
1299 /* Size of abbrev_table.abbrev_hash_table. */
1300 #define ABBREV_HASH_SIZE 121
1302 /* Top level data structure to contain an abbreviation table. */
1306 /* Where the abbrev table came from.
1307 This is used as a sanity check when the table is used. */
1308 sect_offset sect_off;
1310 /* Storage for the abbrev table. */
1311 struct obstack abbrev_obstack;
1313 /* Hash table of abbrevs.
1314 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1315 It could be statically allocated, but the previous code didn't so we
1317 struct abbrev_info **abbrevs;
1320 /* Attributes have a name and a value. */
1323 ENUM_BITFIELD(dwarf_attribute) name : 16;
1324 ENUM_BITFIELD(dwarf_form) form : 15;
1326 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1327 field should be in u.str (existing only for DW_STRING) but it is kept
1328 here for better struct attribute alignment. */
1329 unsigned int string_is_canonical : 1;
1334 struct dwarf_block *blk;
1343 /* This data structure holds a complete die structure. */
1346 /* DWARF-2 tag for this DIE. */
1347 ENUM_BITFIELD(dwarf_tag) tag : 16;
1349 /* Number of attributes */
1350 unsigned char num_attrs;
1352 /* True if we're presently building the full type name for the
1353 type derived from this DIE. */
1354 unsigned char building_fullname : 1;
1356 /* True if this die is in process. PR 16581. */
1357 unsigned char in_process : 1;
1360 unsigned int abbrev;
1362 /* Offset in .debug_info or .debug_types section. */
1363 sect_offset sect_off;
1365 /* The dies in a compilation unit form an n-ary tree. PARENT
1366 points to this die's parent; CHILD points to the first child of
1367 this node; and all the children of a given node are chained
1368 together via their SIBLING fields. */
1369 struct die_info *child; /* Its first child, if any. */
1370 struct die_info *sibling; /* Its next sibling, if any. */
1371 struct die_info *parent; /* Its parent, if any. */
1373 /* An array of attributes, with NUM_ATTRS elements. There may be
1374 zero, but it's not common and zero-sized arrays are not
1375 sufficiently portable C. */
1376 struct attribute attrs[1];
1379 /* Get at parts of an attribute structure. */
1381 #define DW_STRING(attr) ((attr)->u.str)
1382 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1383 #define DW_UNSND(attr) ((attr)->u.unsnd)
1384 #define DW_BLOCK(attr) ((attr)->u.blk)
1385 #define DW_SND(attr) ((attr)->u.snd)
1386 #define DW_ADDR(attr) ((attr)->u.addr)
1387 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1389 /* Blocks are a bunch of untyped bytes. */
1394 /* Valid only if SIZE is not zero. */
1395 const gdb_byte *data;
1398 #ifndef ATTR_ALLOC_CHUNK
1399 #define ATTR_ALLOC_CHUNK 4
1402 /* Allocate fields for structs, unions and enums in this size. */
1403 #ifndef DW_FIELD_ALLOC_CHUNK
1404 #define DW_FIELD_ALLOC_CHUNK 4
1407 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1408 but this would require a corresponding change in unpack_field_as_long
1410 static int bits_per_byte = 8;
1414 struct nextfield *next;
1422 struct nextfnfield *next;
1423 struct fn_field fnfield;
1430 struct nextfnfield *head;
1433 struct typedef_field_list
1435 struct typedef_field field;
1436 struct typedef_field_list *next;
1439 /* The routines that read and process dies for a C struct or C++ class
1440 pass lists of data member fields and lists of member function fields
1441 in an instance of a field_info structure, as defined below. */
1444 /* List of data member and baseclasses fields. */
1445 struct nextfield *fields, *baseclasses;
1447 /* Number of fields (including baseclasses). */
1450 /* Number of baseclasses. */
1453 /* Set if the accesibility of one of the fields is not public. */
1454 int non_public_fields;
1456 /* Member function fields array, entries are allocated in the order they
1457 are encountered in the object file. */
1458 struct nextfnfield *fnfields;
1460 /* Member function fieldlist array, contains name of possibly overloaded
1461 member function, number of overloaded member functions and a pointer
1462 to the head of the member function field chain. */
1463 struct fnfieldlist *fnfieldlists;
1465 /* Number of entries in the fnfieldlists array. */
1468 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1469 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1470 struct typedef_field_list *typedef_field_list;
1471 unsigned typedef_field_list_count;
1474 /* One item on the queue of compilation units to read in full symbols
1476 struct dwarf2_queue_item
1478 struct dwarf2_per_cu_data *per_cu;
1479 enum language pretend_language;
1480 struct dwarf2_queue_item *next;
1483 /* The current queue. */
1484 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1486 /* Loaded secondary compilation units are kept in memory until they
1487 have not been referenced for the processing of this many
1488 compilation units. Set this to zero to disable caching. Cache
1489 sizes of up to at least twenty will improve startup time for
1490 typical inter-CU-reference binaries, at an obvious memory cost. */
1491 static int dwarf_max_cache_age = 5;
1493 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1494 struct cmd_list_element *c, const char *value)
1496 fprintf_filtered (file, _("The upper bound on the age of cached "
1497 "DWARF compilation units is %s.\n"),
1501 /* local function prototypes */
1503 static const char *get_section_name (const struct dwarf2_section_info *);
1505 static const char *get_section_file_name (const struct dwarf2_section_info *);
1507 static void dwarf2_locate_sections (bfd *, asection *, void *);
1509 static void dwarf2_find_base_address (struct die_info *die,
1510 struct dwarf2_cu *cu);
1512 static struct partial_symtab *create_partial_symtab
1513 (struct dwarf2_per_cu_data *per_cu, const char *name);
1515 static void dwarf2_build_psymtabs_hard (struct objfile *);
1517 static void scan_partial_symbols (struct partial_die_info *,
1518 CORE_ADDR *, CORE_ADDR *,
1519 int, struct dwarf2_cu *);
1521 static void add_partial_symbol (struct partial_die_info *,
1522 struct dwarf2_cu *);
1524 static void add_partial_namespace (struct partial_die_info *pdi,
1525 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1526 int set_addrmap, struct dwarf2_cu *cu);
1528 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1529 CORE_ADDR *highpc, int set_addrmap,
1530 struct dwarf2_cu *cu);
1532 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1533 struct dwarf2_cu *cu);
1535 static void add_partial_subprogram (struct partial_die_info *pdi,
1536 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1537 int need_pc, struct dwarf2_cu *cu);
1539 static void dwarf2_read_symtab (struct partial_symtab *,
1542 static void psymtab_to_symtab_1 (struct partial_symtab *);
1544 static struct abbrev_info *abbrev_table_lookup_abbrev
1545 (const struct abbrev_table *, unsigned int);
1547 static struct abbrev_table *abbrev_table_read_table
1548 (struct dwarf2_section_info *, sect_offset);
1550 static void abbrev_table_free (struct abbrev_table *);
1552 static void abbrev_table_free_cleanup (void *);
1554 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1555 struct dwarf2_section_info *);
1557 static void dwarf2_free_abbrev_table (void *);
1559 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1561 static struct partial_die_info *load_partial_dies
1562 (const struct die_reader_specs *, const gdb_byte *, int);
1564 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1565 struct partial_die_info *,
1566 struct abbrev_info *,
1570 static struct partial_die_info *find_partial_die (sect_offset, int,
1571 struct dwarf2_cu *);
1573 static void fixup_partial_die (struct partial_die_info *,
1574 struct dwarf2_cu *);
1576 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1577 struct attribute *, struct attr_abbrev *,
1580 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1582 static int read_1_signed_byte (bfd *, const gdb_byte *);
1584 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1586 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1588 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1590 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1593 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1595 static LONGEST read_checked_initial_length_and_offset
1596 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1597 unsigned int *, unsigned int *);
1599 static LONGEST read_offset (bfd *, const gdb_byte *,
1600 const struct comp_unit_head *,
1603 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1605 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1608 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1610 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1612 static const char *read_indirect_string (bfd *, const gdb_byte *,
1613 const struct comp_unit_head *,
1616 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1617 const struct comp_unit_head *,
1620 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1622 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1624 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1628 static const char *read_str_index (const struct die_reader_specs *reader,
1629 ULONGEST str_index);
1631 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1633 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1634 struct dwarf2_cu *);
1636 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1639 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1640 struct dwarf2_cu *cu);
1642 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1643 struct dwarf2_cu *cu);
1645 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1647 static struct die_info *die_specification (struct die_info *die,
1648 struct dwarf2_cu **);
1650 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1651 struct dwarf2_cu *cu);
1653 static void dwarf_decode_lines (struct line_header *, const char *,
1654 struct dwarf2_cu *, struct partial_symtab *,
1655 CORE_ADDR, int decode_mapping);
1657 static void dwarf2_start_subfile (const char *, const char *);
1659 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1660 const char *, const char *,
1663 static struct symbol *new_symbol (struct die_info *, struct type *,
1664 struct dwarf2_cu *);
1666 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1667 struct dwarf2_cu *, struct symbol *);
1669 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1670 struct dwarf2_cu *);
1672 static void dwarf2_const_value_attr (const struct attribute *attr,
1675 struct obstack *obstack,
1676 struct dwarf2_cu *cu, LONGEST *value,
1677 const gdb_byte **bytes,
1678 struct dwarf2_locexpr_baton **baton);
1680 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1682 static int need_gnat_info (struct dwarf2_cu *);
1684 static struct type *die_descriptive_type (struct die_info *,
1685 struct dwarf2_cu *);
1687 static void set_descriptive_type (struct type *, struct die_info *,
1688 struct dwarf2_cu *);
1690 static struct type *die_containing_type (struct die_info *,
1691 struct dwarf2_cu *);
1693 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1694 struct dwarf2_cu *);
1696 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1698 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1700 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1702 static char *typename_concat (struct obstack *obs, const char *prefix,
1703 const char *suffix, int physname,
1704 struct dwarf2_cu *cu);
1706 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1708 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1710 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1712 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1714 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1716 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1717 struct dwarf2_cu *, struct partial_symtab *);
1719 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1720 values. Keep the items ordered with increasing constraints compliance. */
1723 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1724 PC_BOUNDS_NOT_PRESENT,
1726 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1727 were present but they do not form a valid range of PC addresses. */
1730 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1733 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1737 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1738 CORE_ADDR *, CORE_ADDR *,
1740 struct partial_symtab *);
1742 static void get_scope_pc_bounds (struct die_info *,
1743 CORE_ADDR *, CORE_ADDR *,
1744 struct dwarf2_cu *);
1746 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1747 CORE_ADDR, struct dwarf2_cu *);
1749 static void dwarf2_add_field (struct field_info *, struct die_info *,
1750 struct dwarf2_cu *);
1752 static void dwarf2_attach_fields_to_type (struct field_info *,
1753 struct type *, struct dwarf2_cu *);
1755 static void dwarf2_add_member_fn (struct field_info *,
1756 struct die_info *, struct type *,
1757 struct dwarf2_cu *);
1759 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1761 struct dwarf2_cu *);
1763 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1765 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1767 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1769 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1771 static struct using_direct **using_directives (enum language);
1773 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1775 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1777 static struct type *read_module_type (struct die_info *die,
1778 struct dwarf2_cu *cu);
1780 static const char *namespace_name (struct die_info *die,
1781 int *is_anonymous, struct dwarf2_cu *);
1783 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1785 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1787 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1788 struct dwarf2_cu *);
1790 static struct die_info *read_die_and_siblings_1
1791 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1794 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1795 const gdb_byte *info_ptr,
1796 const gdb_byte **new_info_ptr,
1797 struct die_info *parent);
1799 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1800 struct die_info **, const gdb_byte *,
1803 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1804 struct die_info **, const gdb_byte *,
1807 static void process_die (struct die_info *, struct dwarf2_cu *);
1809 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1812 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1814 static const char *dwarf2_full_name (const char *name,
1815 struct die_info *die,
1816 struct dwarf2_cu *cu);
1818 static const char *dwarf2_physname (const char *name, struct die_info *die,
1819 struct dwarf2_cu *cu);
1821 static struct die_info *dwarf2_extension (struct die_info *die,
1822 struct dwarf2_cu **);
1824 static const char *dwarf_tag_name (unsigned int);
1826 static const char *dwarf_attr_name (unsigned int);
1828 static const char *dwarf_form_name (unsigned int);
1830 static const char *dwarf_bool_name (unsigned int);
1832 static const char *dwarf_type_encoding_name (unsigned int);
1834 static struct die_info *sibling_die (struct die_info *);
1836 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1838 static void dump_die_for_error (struct die_info *);
1840 static void dump_die_1 (struct ui_file *, int level, int max_level,
1843 /*static*/ void dump_die (struct die_info *, int max_level);
1845 static void store_in_ref_table (struct die_info *,
1846 struct dwarf2_cu *);
1848 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1850 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1852 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1853 const struct attribute *,
1854 struct dwarf2_cu **);
1856 static struct die_info *follow_die_ref (struct die_info *,
1857 const struct attribute *,
1858 struct dwarf2_cu **);
1860 static struct die_info *follow_die_sig (struct die_info *,
1861 const struct attribute *,
1862 struct dwarf2_cu **);
1864 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1865 struct dwarf2_cu *);
1867 static struct type *get_DW_AT_signature_type (struct die_info *,
1868 const struct attribute *,
1869 struct dwarf2_cu *);
1871 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1873 static void read_signatured_type (struct signatured_type *);
1875 static int attr_to_dynamic_prop (const struct attribute *attr,
1876 struct die_info *die, struct dwarf2_cu *cu,
1877 struct dynamic_prop *prop);
1879 /* memory allocation interface */
1881 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1883 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1885 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1887 static int attr_form_is_block (const struct attribute *);
1889 static int attr_form_is_section_offset (const struct attribute *);
1891 static int attr_form_is_constant (const struct attribute *);
1893 static int attr_form_is_ref (const struct attribute *);
1895 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1896 struct dwarf2_loclist_baton *baton,
1897 const struct attribute *attr);
1899 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1901 struct dwarf2_cu *cu,
1904 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1905 const gdb_byte *info_ptr,
1906 struct abbrev_info *abbrev);
1908 static void free_stack_comp_unit (void *);
1910 static hashval_t partial_die_hash (const void *item);
1912 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1914 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1915 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
1917 static void init_one_comp_unit (struct dwarf2_cu *cu,
1918 struct dwarf2_per_cu_data *per_cu);
1920 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1921 struct die_info *comp_unit_die,
1922 enum language pretend_language);
1924 static void free_heap_comp_unit (void *);
1926 static void free_cached_comp_units (void *);
1928 static void age_cached_comp_units (void);
1930 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1932 static struct type *set_die_type (struct die_info *, struct type *,
1933 struct dwarf2_cu *);
1935 static void create_all_comp_units (struct objfile *);
1937 static int create_all_type_units (struct objfile *);
1939 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1942 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1945 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1948 static void dwarf2_add_dependence (struct dwarf2_cu *,
1949 struct dwarf2_per_cu_data *);
1951 static void dwarf2_mark (struct dwarf2_cu *);
1953 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1955 static struct type *get_die_type_at_offset (sect_offset,
1956 struct dwarf2_per_cu_data *);
1958 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1960 static void dwarf2_release_queue (void *dummy);
1962 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1963 enum language pretend_language);
1965 static void process_queue (void);
1967 /* The return type of find_file_and_directory. Note, the enclosed
1968 string pointers are only valid while this object is valid. */
1970 struct file_and_directory
1972 /* The filename. This is never NULL. */
1975 /* The compilation directory. NULL if not known. If we needed to
1976 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1977 points directly to the DW_AT_comp_dir string attribute owned by
1978 the obstack that owns the DIE. */
1979 const char *comp_dir;
1981 /* If we needed to build a new string for comp_dir, this is what
1982 owns the storage. */
1983 std::string comp_dir_storage;
1986 static file_and_directory find_file_and_directory (struct die_info *die,
1987 struct dwarf2_cu *cu);
1989 static char *file_full_name (int file, struct line_header *lh,
1990 const char *comp_dir);
1992 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1993 enum class rcuh_kind { COMPILE, TYPE };
1995 static const gdb_byte *read_and_check_comp_unit_head
1996 (struct comp_unit_head *header,
1997 struct dwarf2_section_info *section,
1998 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1999 rcuh_kind section_kind);
2001 static void init_cutu_and_read_dies
2002 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2003 int use_existing_cu, int keep,
2004 die_reader_func_ftype *die_reader_func, void *data);
2006 static void init_cutu_and_read_dies_simple
2007 (struct dwarf2_per_cu_data *this_cu,
2008 die_reader_func_ftype *die_reader_func, void *data);
2010 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2012 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2014 static struct dwo_unit *lookup_dwo_unit_in_dwp
2015 (struct dwp_file *dwp_file, const char *comp_dir,
2016 ULONGEST signature, int is_debug_types);
2018 static struct dwp_file *get_dwp_file (void);
2020 static struct dwo_unit *lookup_dwo_comp_unit
2021 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2023 static struct dwo_unit *lookup_dwo_type_unit
2024 (struct signatured_type *, const char *, const char *);
2026 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2028 static void free_dwo_file_cleanup (void *);
2030 static void process_cu_includes (void);
2032 static void check_producer (struct dwarf2_cu *cu);
2034 static void free_line_header_voidp (void *arg);
2036 /* Various complaints about symbol reading that don't abort the process. */
2039 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2041 complaint (&symfile_complaints,
2042 _("statement list doesn't fit in .debug_line section"));
2046 dwarf2_debug_line_missing_file_complaint (void)
2048 complaint (&symfile_complaints,
2049 _(".debug_line section has line data without a file"));
2053 dwarf2_debug_line_missing_end_sequence_complaint (void)
2055 complaint (&symfile_complaints,
2056 _(".debug_line section has line "
2057 "program sequence without an end"));
2061 dwarf2_complex_location_expr_complaint (void)
2063 complaint (&symfile_complaints, _("location expression too complex"));
2067 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2070 complaint (&symfile_complaints,
2071 _("const value length mismatch for '%s', got %d, expected %d"),
2076 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2078 complaint (&symfile_complaints,
2079 _("debug info runs off end of %s section"
2081 get_section_name (section),
2082 get_section_file_name (section));
2086 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2088 complaint (&symfile_complaints,
2089 _("macro debug info contains a "
2090 "malformed macro definition:\n`%s'"),
2095 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2097 complaint (&symfile_complaints,
2098 _("invalid attribute class or form for '%s' in '%s'"),
2102 /* Hash function for line_header_hash. */
2105 line_header_hash (const struct line_header *ofs)
2107 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2110 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2113 line_header_hash_voidp (const void *item)
2115 const struct line_header *ofs = (const struct line_header *) item;
2117 return line_header_hash (ofs);
2120 /* Equality function for line_header_hash. */
2123 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2125 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2126 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2128 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2129 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2135 /* Convert VALUE between big- and little-endian. */
2137 byte_swap (offset_type value)
2141 result = (value & 0xff) << 24;
2142 result |= (value & 0xff00) << 8;
2143 result |= (value & 0xff0000) >> 8;
2144 result |= (value & 0xff000000) >> 24;
2148 #define MAYBE_SWAP(V) byte_swap (V)
2151 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
2152 #endif /* WORDS_BIGENDIAN */
2154 /* Read the given attribute value as an address, taking the attribute's
2155 form into account. */
2158 attr_value_as_address (struct attribute *attr)
2162 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2164 /* Aside from a few clearly defined exceptions, attributes that
2165 contain an address must always be in DW_FORM_addr form.
2166 Unfortunately, some compilers happen to be violating this
2167 requirement by encoding addresses using other forms, such
2168 as DW_FORM_data4 for example. For those broken compilers,
2169 we try to do our best, without any guarantee of success,
2170 to interpret the address correctly. It would also be nice
2171 to generate a complaint, but that would require us to maintain
2172 a list of legitimate cases where a non-address form is allowed,
2173 as well as update callers to pass in at least the CU's DWARF
2174 version. This is more overhead than what we're willing to
2175 expand for a pretty rare case. */
2176 addr = DW_UNSND (attr);
2179 addr = DW_ADDR (attr);
2184 /* The suffix for an index file. */
2185 #define INDEX_SUFFIX ".gdb-index"
2187 /* Try to locate the sections we need for DWARF 2 debugging
2188 information and return true if we have enough to do something.
2189 NAMES points to the dwarf2 section names, or is NULL if the standard
2190 ELF names are used. */
2193 dwarf2_has_info (struct objfile *objfile,
2194 const struct dwarf2_debug_sections *names)
2196 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2197 objfile_data (objfile, dwarf2_objfile_data_key));
2198 if (!dwarf2_per_objfile)
2200 /* Initialize per-objfile state. */
2201 struct dwarf2_per_objfile *data
2202 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2204 memset (data, 0, sizeof (*data));
2205 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2206 dwarf2_per_objfile = data;
2208 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2210 dwarf2_per_objfile->objfile = objfile;
2212 return (!dwarf2_per_objfile->info.is_virtual
2213 && dwarf2_per_objfile->info.s.section != NULL
2214 && !dwarf2_per_objfile->abbrev.is_virtual
2215 && dwarf2_per_objfile->abbrev.s.section != NULL);
2218 /* Return the containing section of virtual section SECTION. */
2220 static struct dwarf2_section_info *
2221 get_containing_section (const struct dwarf2_section_info *section)
2223 gdb_assert (section->is_virtual);
2224 return section->s.containing_section;
2227 /* Return the bfd owner of SECTION. */
2230 get_section_bfd_owner (const struct dwarf2_section_info *section)
2232 if (section->is_virtual)
2234 section = get_containing_section (section);
2235 gdb_assert (!section->is_virtual);
2237 return section->s.section->owner;
2240 /* Return the bfd section of SECTION.
2241 Returns NULL if the section is not present. */
2244 get_section_bfd_section (const struct dwarf2_section_info *section)
2246 if (section->is_virtual)
2248 section = get_containing_section (section);
2249 gdb_assert (!section->is_virtual);
2251 return section->s.section;
2254 /* Return the name of SECTION. */
2257 get_section_name (const struct dwarf2_section_info *section)
2259 asection *sectp = get_section_bfd_section (section);
2261 gdb_assert (sectp != NULL);
2262 return bfd_section_name (get_section_bfd_owner (section), sectp);
2265 /* Return the name of the file SECTION is in. */
2268 get_section_file_name (const struct dwarf2_section_info *section)
2270 bfd *abfd = get_section_bfd_owner (section);
2272 return bfd_get_filename (abfd);
2275 /* Return the id of SECTION.
2276 Returns 0 if SECTION doesn't exist. */
2279 get_section_id (const struct dwarf2_section_info *section)
2281 asection *sectp = get_section_bfd_section (section);
2288 /* Return the flags of SECTION.
2289 SECTION (or containing section if this is a virtual section) must exist. */
2292 get_section_flags (const struct dwarf2_section_info *section)
2294 asection *sectp = get_section_bfd_section (section);
2296 gdb_assert (sectp != NULL);
2297 return bfd_get_section_flags (sectp->owner, sectp);
2300 /* When loading sections, we look either for uncompressed section or for
2301 compressed section names. */
2304 section_is_p (const char *section_name,
2305 const struct dwarf2_section_names *names)
2307 if (names->normal != NULL
2308 && strcmp (section_name, names->normal) == 0)
2310 if (names->compressed != NULL
2311 && strcmp (section_name, names->compressed) == 0)
2316 /* This function is mapped across the sections and remembers the
2317 offset and size of each of the debugging sections we are interested
2321 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2323 const struct dwarf2_debug_sections *names;
2324 flagword aflag = bfd_get_section_flags (abfd, sectp);
2327 names = &dwarf2_elf_names;
2329 names = (const struct dwarf2_debug_sections *) vnames;
2331 if ((aflag & SEC_HAS_CONTENTS) == 0)
2334 else if (section_is_p (sectp->name, &names->info))
2336 dwarf2_per_objfile->info.s.section = sectp;
2337 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2339 else if (section_is_p (sectp->name, &names->abbrev))
2341 dwarf2_per_objfile->abbrev.s.section = sectp;
2342 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2344 else if (section_is_p (sectp->name, &names->line))
2346 dwarf2_per_objfile->line.s.section = sectp;
2347 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2349 else if (section_is_p (sectp->name, &names->loc))
2351 dwarf2_per_objfile->loc.s.section = sectp;
2352 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2354 else if (section_is_p (sectp->name, &names->loclists))
2356 dwarf2_per_objfile->loclists.s.section = sectp;
2357 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2359 else if (section_is_p (sectp->name, &names->macinfo))
2361 dwarf2_per_objfile->macinfo.s.section = sectp;
2362 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2364 else if (section_is_p (sectp->name, &names->macro))
2366 dwarf2_per_objfile->macro.s.section = sectp;
2367 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2369 else if (section_is_p (sectp->name, &names->str))
2371 dwarf2_per_objfile->str.s.section = sectp;
2372 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2374 else if (section_is_p (sectp->name, &names->line_str))
2376 dwarf2_per_objfile->line_str.s.section = sectp;
2377 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2379 else if (section_is_p (sectp->name, &names->addr))
2381 dwarf2_per_objfile->addr.s.section = sectp;
2382 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2384 else if (section_is_p (sectp->name, &names->frame))
2386 dwarf2_per_objfile->frame.s.section = sectp;
2387 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2389 else if (section_is_p (sectp->name, &names->eh_frame))
2391 dwarf2_per_objfile->eh_frame.s.section = sectp;
2392 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2394 else if (section_is_p (sectp->name, &names->ranges))
2396 dwarf2_per_objfile->ranges.s.section = sectp;
2397 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2399 else if (section_is_p (sectp->name, &names->rnglists))
2401 dwarf2_per_objfile->rnglists.s.section = sectp;
2402 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2404 else if (section_is_p (sectp->name, &names->types))
2406 struct dwarf2_section_info type_section;
2408 memset (&type_section, 0, sizeof (type_section));
2409 type_section.s.section = sectp;
2410 type_section.size = bfd_get_section_size (sectp);
2412 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2415 else if (section_is_p (sectp->name, &names->gdb_index))
2417 dwarf2_per_objfile->gdb_index.s.section = sectp;
2418 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2421 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2422 && bfd_section_vma (abfd, sectp) == 0)
2423 dwarf2_per_objfile->has_section_at_zero = 1;
2426 /* A helper function that decides whether a section is empty,
2430 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2432 if (section->is_virtual)
2433 return section->size == 0;
2434 return section->s.section == NULL || section->size == 0;
2437 /* Read the contents of the section INFO.
2438 OBJFILE is the main object file, but not necessarily the file where
2439 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2441 If the section is compressed, uncompress it before returning. */
2444 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2448 gdb_byte *buf, *retbuf;
2452 info->buffer = NULL;
2455 if (dwarf2_section_empty_p (info))
2458 sectp = get_section_bfd_section (info);
2460 /* If this is a virtual section we need to read in the real one first. */
2461 if (info->is_virtual)
2463 struct dwarf2_section_info *containing_section =
2464 get_containing_section (info);
2466 gdb_assert (sectp != NULL);
2467 if ((sectp->flags & SEC_RELOC) != 0)
2469 error (_("Dwarf Error: DWP format V2 with relocations is not"
2470 " supported in section %s [in module %s]"),
2471 get_section_name (info), get_section_file_name (info));
2473 dwarf2_read_section (objfile, containing_section);
2474 /* Other code should have already caught virtual sections that don't
2476 gdb_assert (info->virtual_offset + info->size
2477 <= containing_section->size);
2478 /* If the real section is empty or there was a problem reading the
2479 section we shouldn't get here. */
2480 gdb_assert (containing_section->buffer != NULL);
2481 info->buffer = containing_section->buffer + info->virtual_offset;
2485 /* If the section has relocations, we must read it ourselves.
2486 Otherwise we attach it to the BFD. */
2487 if ((sectp->flags & SEC_RELOC) == 0)
2489 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2493 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2496 /* When debugging .o files, we may need to apply relocations; see
2497 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2498 We never compress sections in .o files, so we only need to
2499 try this when the section is not compressed. */
2500 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2503 info->buffer = retbuf;
2507 abfd = get_section_bfd_owner (info);
2508 gdb_assert (abfd != NULL);
2510 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2511 || bfd_bread (buf, info->size, abfd) != info->size)
2513 error (_("Dwarf Error: Can't read DWARF data"
2514 " in section %s [in module %s]"),
2515 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2519 /* A helper function that returns the size of a section in a safe way.
2520 If you are positive that the section has been read before using the
2521 size, then it is safe to refer to the dwarf2_section_info object's
2522 "size" field directly. In other cases, you must call this
2523 function, because for compressed sections the size field is not set
2524 correctly until the section has been read. */
2526 static bfd_size_type
2527 dwarf2_section_size (struct objfile *objfile,
2528 struct dwarf2_section_info *info)
2531 dwarf2_read_section (objfile, info);
2535 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2539 dwarf2_get_section_info (struct objfile *objfile,
2540 enum dwarf2_section_enum sect,
2541 asection **sectp, const gdb_byte **bufp,
2542 bfd_size_type *sizep)
2544 struct dwarf2_per_objfile *data
2545 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2546 dwarf2_objfile_data_key);
2547 struct dwarf2_section_info *info;
2549 /* We may see an objfile without any DWARF, in which case we just
2560 case DWARF2_DEBUG_FRAME:
2561 info = &data->frame;
2563 case DWARF2_EH_FRAME:
2564 info = &data->eh_frame;
2567 gdb_assert_not_reached ("unexpected section");
2570 dwarf2_read_section (objfile, info);
2572 *sectp = get_section_bfd_section (info);
2573 *bufp = info->buffer;
2574 *sizep = info->size;
2577 /* A helper function to find the sections for a .dwz file. */
2580 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2582 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2584 /* Note that we only support the standard ELF names, because .dwz
2585 is ELF-only (at the time of writing). */
2586 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2588 dwz_file->abbrev.s.section = sectp;
2589 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2591 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2593 dwz_file->info.s.section = sectp;
2594 dwz_file->info.size = bfd_get_section_size (sectp);
2596 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2598 dwz_file->str.s.section = sectp;
2599 dwz_file->str.size = bfd_get_section_size (sectp);
2601 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2603 dwz_file->line.s.section = sectp;
2604 dwz_file->line.size = bfd_get_section_size (sectp);
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2608 dwz_file->macro.s.section = sectp;
2609 dwz_file->macro.size = bfd_get_section_size (sectp);
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2613 dwz_file->gdb_index.s.section = sectp;
2614 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2618 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2619 there is no .gnu_debugaltlink section in the file. Error if there
2620 is such a section but the file cannot be found. */
2622 static struct dwz_file *
2623 dwarf2_get_dwz_file (void)
2626 struct cleanup *cleanup;
2627 const char *filename;
2628 struct dwz_file *result;
2629 bfd_size_type buildid_len_arg;
2633 if (dwarf2_per_objfile->dwz_file != NULL)
2634 return dwarf2_per_objfile->dwz_file;
2636 bfd_set_error (bfd_error_no_error);
2637 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2638 &buildid_len_arg, &buildid);
2641 if (bfd_get_error () == bfd_error_no_error)
2643 error (_("could not read '.gnu_debugaltlink' section: %s"),
2644 bfd_errmsg (bfd_get_error ()));
2646 cleanup = make_cleanup (xfree, data);
2647 make_cleanup (xfree, buildid);
2649 buildid_len = (size_t) buildid_len_arg;
2651 filename = (const char *) data;
2653 std::string abs_storage;
2654 if (!IS_ABSOLUTE_PATH (filename))
2656 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2658 make_cleanup (xfree, abs);
2659 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2660 filename = abs_storage.c_str ();
2663 /* First try the file name given in the section. If that doesn't
2664 work, try to use the build-id instead. */
2665 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2666 if (dwz_bfd != NULL)
2668 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2672 if (dwz_bfd == NULL)
2673 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2675 if (dwz_bfd == NULL)
2676 error (_("could not find '.gnu_debugaltlink' file for %s"),
2677 objfile_name (dwarf2_per_objfile->objfile));
2679 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2681 result->dwz_bfd = dwz_bfd.release ();
2683 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2685 do_cleanups (cleanup);
2687 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2688 dwarf2_per_objfile->dwz_file = result;
2692 /* DWARF quick_symbols_functions support. */
2694 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2695 unique line tables, so we maintain a separate table of all .debug_line
2696 derived entries to support the sharing.
2697 All the quick functions need is the list of file names. We discard the
2698 line_header when we're done and don't need to record it here. */
2699 struct quick_file_names
2701 /* The data used to construct the hash key. */
2702 struct stmt_list_hash hash;
2704 /* The number of entries in file_names, real_names. */
2705 unsigned int num_file_names;
2707 /* The file names from the line table, after being run through
2709 const char **file_names;
2711 /* The file names from the line table after being run through
2712 gdb_realpath. These are computed lazily. */
2713 const char **real_names;
2716 /* When using the index (and thus not using psymtabs), each CU has an
2717 object of this type. This is used to hold information needed by
2718 the various "quick" methods. */
2719 struct dwarf2_per_cu_quick_data
2721 /* The file table. This can be NULL if there was no file table
2722 or it's currently not read in.
2723 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2724 struct quick_file_names *file_names;
2726 /* The corresponding symbol table. This is NULL if symbols for this
2727 CU have not yet been read. */
2728 struct compunit_symtab *compunit_symtab;
2730 /* A temporary mark bit used when iterating over all CUs in
2731 expand_symtabs_matching. */
2732 unsigned int mark : 1;
2734 /* True if we've tried to read the file table and found there isn't one.
2735 There will be no point in trying to read it again next time. */
2736 unsigned int no_file_data : 1;
2739 /* Utility hash function for a stmt_list_hash. */
2742 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2746 if (stmt_list_hash->dwo_unit != NULL)
2747 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2748 v += to_underlying (stmt_list_hash->line_sect_off);
2752 /* Utility equality function for a stmt_list_hash. */
2755 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2756 const struct stmt_list_hash *rhs)
2758 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2760 if (lhs->dwo_unit != NULL
2761 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2764 return lhs->line_sect_off == rhs->line_sect_off;
2767 /* Hash function for a quick_file_names. */
2770 hash_file_name_entry (const void *e)
2772 const struct quick_file_names *file_data
2773 = (const struct quick_file_names *) e;
2775 return hash_stmt_list_entry (&file_data->hash);
2778 /* Equality function for a quick_file_names. */
2781 eq_file_name_entry (const void *a, const void *b)
2783 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2784 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2786 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2789 /* Delete function for a quick_file_names. */
2792 delete_file_name_entry (void *e)
2794 struct quick_file_names *file_data = (struct quick_file_names *) e;
2797 for (i = 0; i < file_data->num_file_names; ++i)
2799 xfree ((void*) file_data->file_names[i]);
2800 if (file_data->real_names)
2801 xfree ((void*) file_data->real_names[i]);
2804 /* The space for the struct itself lives on objfile_obstack,
2805 so we don't free it here. */
2808 /* Create a quick_file_names hash table. */
2811 create_quick_file_names_table (unsigned int nr_initial_entries)
2813 return htab_create_alloc (nr_initial_entries,
2814 hash_file_name_entry, eq_file_name_entry,
2815 delete_file_name_entry, xcalloc, xfree);
2818 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2819 have to be created afterwards. You should call age_cached_comp_units after
2820 processing PER_CU->CU. dw2_setup must have been already called. */
2823 load_cu (struct dwarf2_per_cu_data *per_cu)
2825 if (per_cu->is_debug_types)
2826 load_full_type_unit (per_cu);
2828 load_full_comp_unit (per_cu, language_minimal);
2830 if (per_cu->cu == NULL)
2831 return; /* Dummy CU. */
2833 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2836 /* Read in the symbols for PER_CU. */
2839 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2841 struct cleanup *back_to;
2843 /* Skip type_unit_groups, reading the type units they contain
2844 is handled elsewhere. */
2845 if (IS_TYPE_UNIT_GROUP (per_cu))
2848 back_to = make_cleanup (dwarf2_release_queue, NULL);
2850 if (dwarf2_per_objfile->using_index
2851 ? per_cu->v.quick->compunit_symtab == NULL
2852 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2854 queue_comp_unit (per_cu, language_minimal);
2857 /* If we just loaded a CU from a DWO, and we're working with an index
2858 that may badly handle TUs, load all the TUs in that DWO as well.
2859 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2860 if (!per_cu->is_debug_types
2861 && per_cu->cu != NULL
2862 && per_cu->cu->dwo_unit != NULL
2863 && dwarf2_per_objfile->index_table != NULL
2864 && dwarf2_per_objfile->index_table->version <= 7
2865 /* DWP files aren't supported yet. */
2866 && get_dwp_file () == NULL)
2867 queue_and_load_all_dwo_tus (per_cu);
2872 /* Age the cache, releasing compilation units that have not
2873 been used recently. */
2874 age_cached_comp_units ();
2876 do_cleanups (back_to);
2879 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2880 the objfile from which this CU came. Returns the resulting symbol
2883 static struct compunit_symtab *
2884 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2886 gdb_assert (dwarf2_per_objfile->using_index);
2887 if (!per_cu->v.quick->compunit_symtab)
2889 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2890 scoped_restore decrementer = increment_reading_symtab ();
2891 dw2_do_instantiate_symtab (per_cu);
2892 process_cu_includes ();
2893 do_cleanups (back_to);
2896 return per_cu->v.quick->compunit_symtab;
2899 /* Return the CU/TU given its index.
2901 This is intended for loops like:
2903 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2904 + dwarf2_per_objfile->n_type_units); ++i)
2906 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2912 static struct dwarf2_per_cu_data *
2913 dw2_get_cutu (int index)
2915 if (index >= dwarf2_per_objfile->n_comp_units)
2917 index -= dwarf2_per_objfile->n_comp_units;
2918 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2919 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2922 return dwarf2_per_objfile->all_comp_units[index];
2925 /* Return the CU given its index.
2926 This differs from dw2_get_cutu in that it's for when you know INDEX
2929 static struct dwarf2_per_cu_data *
2930 dw2_get_cu (int index)
2932 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2934 return dwarf2_per_objfile->all_comp_units[index];
2937 /* A helper for create_cus_from_index that handles a given list of
2941 create_cus_from_index_list (struct objfile *objfile,
2942 const gdb_byte *cu_list, offset_type n_elements,
2943 struct dwarf2_section_info *section,
2949 for (i = 0; i < n_elements; i += 2)
2951 gdb_static_assert (sizeof (ULONGEST) >= 8);
2953 sect_offset sect_off
2954 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2955 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2958 dwarf2_per_cu_data *the_cu
2959 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2960 struct dwarf2_per_cu_data);
2961 the_cu->sect_off = sect_off;
2962 the_cu->length = length;
2963 the_cu->objfile = objfile;
2964 the_cu->section = section;
2965 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_quick_data);
2967 the_cu->is_dwz = is_dwz;
2968 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2972 /* Read the CU list from the mapped index, and use it to create all
2973 the CU objects for this objfile. */
2976 create_cus_from_index (struct objfile *objfile,
2977 const gdb_byte *cu_list, offset_type cu_list_elements,
2978 const gdb_byte *dwz_list, offset_type dwz_elements)
2980 struct dwz_file *dwz;
2982 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2983 dwarf2_per_objfile->all_comp_units =
2984 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2985 dwarf2_per_objfile->n_comp_units);
2987 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2988 &dwarf2_per_objfile->info, 0, 0);
2990 if (dwz_elements == 0)
2993 dwz = dwarf2_get_dwz_file ();
2994 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2995 cu_list_elements / 2);
2998 /* Create the signatured type hash table from the index. */
3001 create_signatured_type_table_from_index (struct objfile *objfile,
3002 struct dwarf2_section_info *section,
3003 const gdb_byte *bytes,
3004 offset_type elements)
3007 htab_t sig_types_hash;
3009 dwarf2_per_objfile->n_type_units
3010 = dwarf2_per_objfile->n_allocated_type_units
3012 dwarf2_per_objfile->all_type_units =
3013 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3015 sig_types_hash = allocate_signatured_type_table (objfile);
3017 for (i = 0; i < elements; i += 3)
3019 struct signatured_type *sig_type;
3022 cu_offset type_offset_in_tu;
3024 gdb_static_assert (sizeof (ULONGEST) >= 8);
3025 sect_offset sect_off
3026 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3028 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3030 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3033 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3034 struct signatured_type);
3035 sig_type->signature = signature;
3036 sig_type->type_offset_in_tu = type_offset_in_tu;
3037 sig_type->per_cu.is_debug_types = 1;
3038 sig_type->per_cu.section = section;
3039 sig_type->per_cu.sect_off = sect_off;
3040 sig_type->per_cu.objfile = objfile;
3041 sig_type->per_cu.v.quick
3042 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3043 struct dwarf2_per_cu_quick_data);
3045 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3048 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3051 dwarf2_per_objfile->signatured_types = sig_types_hash;
3054 /* Read the address map data from the mapped index, and use it to
3055 populate the objfile's psymtabs_addrmap. */
3058 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3060 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3061 const gdb_byte *iter, *end;
3062 struct obstack temp_obstack;
3063 struct addrmap *mutable_map;
3064 struct cleanup *cleanup;
3067 obstack_init (&temp_obstack);
3068 cleanup = make_cleanup_obstack_free (&temp_obstack);
3069 mutable_map = addrmap_create_mutable (&temp_obstack);
3071 iter = index->address_table;
3072 end = iter + index->address_table_size;
3074 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3078 ULONGEST hi, lo, cu_index;
3079 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3081 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3083 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3088 complaint (&symfile_complaints,
3089 _(".gdb_index address table has invalid range (%s - %s)"),
3090 hex_string (lo), hex_string (hi));
3094 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3096 complaint (&symfile_complaints,
3097 _(".gdb_index address table has invalid CU number %u"),
3098 (unsigned) cu_index);
3102 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3103 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3104 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3107 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3108 &objfile->objfile_obstack);
3109 do_cleanups (cleanup);
3112 /* The hash function for strings in the mapped index. This is the same as
3113 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3114 implementation. This is necessary because the hash function is tied to the
3115 format of the mapped index file. The hash values do not have to match with
3118 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3121 mapped_index_string_hash (int index_version, const void *p)
3123 const unsigned char *str = (const unsigned char *) p;
3127 while ((c = *str++) != 0)
3129 if (index_version >= 5)
3131 r = r * 67 + c - 113;
3137 /* Find a slot in the mapped index INDEX for the object named NAME.
3138 If NAME is found, set *VEC_OUT to point to the CU vector in the
3139 constant pool and return 1. If NAME cannot be found, return 0. */
3142 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3143 offset_type **vec_out)
3145 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3147 offset_type slot, step;
3148 int (*cmp) (const char *, const char *);
3150 if (current_language->la_language == language_cplus
3151 || current_language->la_language == language_fortran
3152 || current_language->la_language == language_d)
3154 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3157 if (strchr (name, '(') != NULL)
3159 char *without_params = cp_remove_params (name);
3161 if (without_params != NULL)
3163 make_cleanup (xfree, without_params);
3164 name = without_params;
3169 /* Index version 4 did not support case insensitive searches. But the
3170 indices for case insensitive languages are built in lowercase, therefore
3171 simulate our NAME being searched is also lowercased. */
3172 hash = mapped_index_string_hash ((index->version == 4
3173 && case_sensitivity == case_sensitive_off
3174 ? 5 : index->version),
3177 slot = hash & (index->symbol_table_slots - 1);
3178 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3179 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3183 /* Convert a slot number to an offset into the table. */
3184 offset_type i = 2 * slot;
3186 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3188 do_cleanups (back_to);
3192 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3193 if (!cmp (name, str))
3195 *vec_out = (offset_type *) (index->constant_pool
3196 + MAYBE_SWAP (index->symbol_table[i + 1]));
3197 do_cleanups (back_to);
3201 slot = (slot + step) & (index->symbol_table_slots - 1);
3205 /* A helper function that reads the .gdb_index from SECTION and fills
3206 in MAP. FILENAME is the name of the file containing the section;
3207 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3208 ok to use deprecated sections.
3210 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3211 out parameters that are filled in with information about the CU and
3212 TU lists in the section.
3214 Returns 1 if all went well, 0 otherwise. */
3217 read_index_from_section (struct objfile *objfile,
3218 const char *filename,
3220 struct dwarf2_section_info *section,
3221 struct mapped_index *map,
3222 const gdb_byte **cu_list,
3223 offset_type *cu_list_elements,
3224 const gdb_byte **types_list,
3225 offset_type *types_list_elements)
3227 const gdb_byte *addr;
3228 offset_type version;
3229 offset_type *metadata;
3232 if (dwarf2_section_empty_p (section))
3235 /* Older elfutils strip versions could keep the section in the main
3236 executable while splitting it for the separate debug info file. */
3237 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3240 dwarf2_read_section (objfile, section);
3242 addr = section->buffer;
3243 /* Version check. */
3244 version = MAYBE_SWAP (*(offset_type *) addr);
3245 /* Versions earlier than 3 emitted every copy of a psymbol. This
3246 causes the index to behave very poorly for certain requests. Version 3
3247 contained incomplete addrmap. So, it seems better to just ignore such
3251 static int warning_printed = 0;
3252 if (!warning_printed)
3254 warning (_("Skipping obsolete .gdb_index section in %s."),
3256 warning_printed = 1;
3260 /* Index version 4 uses a different hash function than index version
3263 Versions earlier than 6 did not emit psymbols for inlined
3264 functions. Using these files will cause GDB not to be able to
3265 set breakpoints on inlined functions by name, so we ignore these
3266 indices unless the user has done
3267 "set use-deprecated-index-sections on". */
3268 if (version < 6 && !deprecated_ok)
3270 static int warning_printed = 0;
3271 if (!warning_printed)
3274 Skipping deprecated .gdb_index section in %s.\n\
3275 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3276 to use the section anyway."),
3278 warning_printed = 1;
3282 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3283 of the TU (for symbols coming from TUs),
3284 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3285 Plus gold-generated indices can have duplicate entries for global symbols,
3286 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3287 These are just performance bugs, and we can't distinguish gdb-generated
3288 indices from gold-generated ones, so issue no warning here. */
3290 /* Indexes with higher version than the one supported by GDB may be no
3291 longer backward compatible. */
3295 map->version = version;
3296 map->total_size = section->size;
3298 metadata = (offset_type *) (addr + sizeof (offset_type));
3301 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3302 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3306 *types_list = addr + MAYBE_SWAP (metadata[i]);
3307 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3308 - MAYBE_SWAP (metadata[i]))
3312 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3313 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3314 - MAYBE_SWAP (metadata[i]));
3317 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3318 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3319 - MAYBE_SWAP (metadata[i]))
3320 / (2 * sizeof (offset_type)));
3323 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3329 /* Read the index file. If everything went ok, initialize the "quick"
3330 elements of all the CUs and return 1. Otherwise, return 0. */
3333 dwarf2_read_index (struct objfile *objfile)
3335 struct mapped_index local_map, *map;
3336 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3337 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3338 struct dwz_file *dwz;
3340 if (!read_index_from_section (objfile, objfile_name (objfile),
3341 use_deprecated_index_sections,
3342 &dwarf2_per_objfile->gdb_index, &local_map,
3343 &cu_list, &cu_list_elements,
3344 &types_list, &types_list_elements))
3347 /* Don't use the index if it's empty. */
3348 if (local_map.symbol_table_slots == 0)
3351 /* If there is a .dwz file, read it so we can get its CU list as
3353 dwz = dwarf2_get_dwz_file ();
3356 struct mapped_index dwz_map;
3357 const gdb_byte *dwz_types_ignore;
3358 offset_type dwz_types_elements_ignore;
3360 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3362 &dwz->gdb_index, &dwz_map,
3363 &dwz_list, &dwz_list_elements,
3365 &dwz_types_elements_ignore))
3367 warning (_("could not read '.gdb_index' section from %s; skipping"),
3368 bfd_get_filename (dwz->dwz_bfd));
3373 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3376 if (types_list_elements)
3378 struct dwarf2_section_info *section;
3380 /* We can only handle a single .debug_types when we have an
3382 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3385 section = VEC_index (dwarf2_section_info_def,
3386 dwarf2_per_objfile->types, 0);
3388 create_signatured_type_table_from_index (objfile, section, types_list,
3389 types_list_elements);
3392 create_addrmap_from_index (objfile, &local_map);
3394 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3397 dwarf2_per_objfile->index_table = map;
3398 dwarf2_per_objfile->using_index = 1;
3399 dwarf2_per_objfile->quick_file_names_table =
3400 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3405 /* A helper for the "quick" functions which sets the global
3406 dwarf2_per_objfile according to OBJFILE. */
3409 dw2_setup (struct objfile *objfile)
3411 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3412 objfile_data (objfile, dwarf2_objfile_data_key));
3413 gdb_assert (dwarf2_per_objfile);
3416 /* die_reader_func for dw2_get_file_names. */
3419 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3420 const gdb_byte *info_ptr,
3421 struct die_info *comp_unit_die,
3425 struct dwarf2_cu *cu = reader->cu;
3426 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3427 struct objfile *objfile = dwarf2_per_objfile->objfile;
3428 struct dwarf2_per_cu_data *lh_cu;
3429 struct attribute *attr;
3432 struct quick_file_names *qfn;
3434 gdb_assert (! this_cu->is_debug_types);
3436 /* Our callers never want to match partial units -- instead they
3437 will match the enclosing full CU. */
3438 if (comp_unit_die->tag == DW_TAG_partial_unit)
3440 this_cu->v.quick->no_file_data = 1;
3448 sect_offset line_offset {};
3450 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3453 struct quick_file_names find_entry;
3455 line_offset = (sect_offset) DW_UNSND (attr);
3457 /* We may have already read in this line header (TU line header sharing).
3458 If we have we're done. */
3459 find_entry.hash.dwo_unit = cu->dwo_unit;
3460 find_entry.hash.line_sect_off = line_offset;
3461 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3462 &find_entry, INSERT);
3465 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3469 lh = dwarf_decode_line_header (line_offset, cu);
3473 lh_cu->v.quick->no_file_data = 1;
3477 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3478 qfn->hash.dwo_unit = cu->dwo_unit;
3479 qfn->hash.line_sect_off = line_offset;
3480 gdb_assert (slot != NULL);
3483 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3485 qfn->num_file_names = lh->file_names.size ();
3487 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3488 for (i = 0; i < lh->file_names.size (); ++i)
3489 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3490 qfn->real_names = NULL;
3492 lh_cu->v.quick->file_names = qfn;
3495 /* A helper for the "quick" functions which attempts to read the line
3496 table for THIS_CU. */
3498 static struct quick_file_names *
3499 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3501 /* This should never be called for TUs. */
3502 gdb_assert (! this_cu->is_debug_types);
3503 /* Nor type unit groups. */
3504 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3506 if (this_cu->v.quick->file_names != NULL)
3507 return this_cu->v.quick->file_names;
3508 /* If we know there is no line data, no point in looking again. */
3509 if (this_cu->v.quick->no_file_data)
3512 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3514 if (this_cu->v.quick->no_file_data)
3516 return this_cu->v.quick->file_names;
3519 /* A helper for the "quick" functions which computes and caches the
3520 real path for a given file name from the line table. */
3523 dw2_get_real_path (struct objfile *objfile,
3524 struct quick_file_names *qfn, int index)
3526 if (qfn->real_names == NULL)
3527 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3528 qfn->num_file_names, const char *);
3530 if (qfn->real_names[index] == NULL)
3531 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3533 return qfn->real_names[index];
3536 static struct symtab *
3537 dw2_find_last_source_symtab (struct objfile *objfile)
3539 struct compunit_symtab *cust;
3542 dw2_setup (objfile);
3543 index = dwarf2_per_objfile->n_comp_units - 1;
3544 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3547 return compunit_primary_filetab (cust);
3550 /* Traversal function for dw2_forget_cached_source_info. */
3553 dw2_free_cached_file_names (void **slot, void *info)
3555 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3557 if (file_data->real_names)
3561 for (i = 0; i < file_data->num_file_names; ++i)
3563 xfree ((void*) file_data->real_names[i]);
3564 file_data->real_names[i] = NULL;
3572 dw2_forget_cached_source_info (struct objfile *objfile)
3574 dw2_setup (objfile);
3576 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3577 dw2_free_cached_file_names, NULL);
3580 /* Helper function for dw2_map_symtabs_matching_filename that expands
3581 the symtabs and calls the iterator. */
3584 dw2_map_expand_apply (struct objfile *objfile,
3585 struct dwarf2_per_cu_data *per_cu,
3586 const char *name, const char *real_path,
3587 gdb::function_view<bool (symtab *)> callback)
3589 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3591 /* Don't visit already-expanded CUs. */
3592 if (per_cu->v.quick->compunit_symtab)
3595 /* This may expand more than one symtab, and we want to iterate over
3597 dw2_instantiate_symtab (per_cu);
3599 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3600 last_made, callback);
3603 /* Implementation of the map_symtabs_matching_filename method. */
3606 dw2_map_symtabs_matching_filename
3607 (struct objfile *objfile, const char *name, const char *real_path,
3608 gdb::function_view<bool (symtab *)> callback)
3611 const char *name_basename = lbasename (name);
3613 dw2_setup (objfile);
3615 /* The rule is CUs specify all the files, including those used by
3616 any TU, so there's no need to scan TUs here. */
3618 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3621 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3622 struct quick_file_names *file_data;
3624 /* We only need to look at symtabs not already expanded. */
3625 if (per_cu->v.quick->compunit_symtab)
3628 file_data = dw2_get_file_names (per_cu);
3629 if (file_data == NULL)
3632 for (j = 0; j < file_data->num_file_names; ++j)
3634 const char *this_name = file_data->file_names[j];
3635 const char *this_real_name;
3637 if (compare_filenames_for_search (this_name, name))
3639 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3645 /* Before we invoke realpath, which can get expensive when many
3646 files are involved, do a quick comparison of the basenames. */
3647 if (! basenames_may_differ
3648 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3651 this_real_name = dw2_get_real_path (objfile, file_data, j);
3652 if (compare_filenames_for_search (this_real_name, name))
3654 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3660 if (real_path != NULL)
3662 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3663 gdb_assert (IS_ABSOLUTE_PATH (name));
3664 if (this_real_name != NULL
3665 && FILENAME_CMP (real_path, this_real_name) == 0)
3667 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3679 /* Struct used to manage iterating over all CUs looking for a symbol. */
3681 struct dw2_symtab_iterator
3683 /* The internalized form of .gdb_index. */
3684 struct mapped_index *index;
3685 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3686 int want_specific_block;
3687 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3688 Unused if !WANT_SPECIFIC_BLOCK. */
3690 /* The kind of symbol we're looking for. */
3692 /* The list of CUs from the index entry of the symbol,
3693 or NULL if not found. */
3695 /* The next element in VEC to look at. */
3697 /* The number of elements in VEC, or zero if there is no match. */
3699 /* Have we seen a global version of the symbol?
3700 If so we can ignore all further global instances.
3701 This is to work around gold/15646, inefficient gold-generated
3706 /* Initialize the index symtab iterator ITER.
3707 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3708 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3711 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3712 struct mapped_index *index,
3713 int want_specific_block,
3718 iter->index = index;
3719 iter->want_specific_block = want_specific_block;
3720 iter->block_index = block_index;
3721 iter->domain = domain;
3723 iter->global_seen = 0;
3725 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3726 iter->length = MAYBE_SWAP (*iter->vec);
3734 /* Return the next matching CU or NULL if there are no more. */
3736 static struct dwarf2_per_cu_data *
3737 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3739 for ( ; iter->next < iter->length; ++iter->next)
3741 offset_type cu_index_and_attrs =
3742 MAYBE_SWAP (iter->vec[iter->next + 1]);
3743 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3744 struct dwarf2_per_cu_data *per_cu;
3745 int want_static = iter->block_index != GLOBAL_BLOCK;
3746 /* This value is only valid for index versions >= 7. */
3747 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3748 gdb_index_symbol_kind symbol_kind =
3749 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3750 /* Only check the symbol attributes if they're present.
3751 Indices prior to version 7 don't record them,
3752 and indices >= 7 may elide them for certain symbols
3753 (gold does this). */
3755 (iter->index->version >= 7
3756 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3758 /* Don't crash on bad data. */
3759 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3760 + dwarf2_per_objfile->n_type_units))
3762 complaint (&symfile_complaints,
3763 _(".gdb_index entry has bad CU index"
3765 objfile_name (dwarf2_per_objfile->objfile));
3769 per_cu = dw2_get_cutu (cu_index);
3771 /* Skip if already read in. */
3772 if (per_cu->v.quick->compunit_symtab)
3775 /* Check static vs global. */
3778 if (iter->want_specific_block
3779 && want_static != is_static)
3781 /* Work around gold/15646. */
3782 if (!is_static && iter->global_seen)
3785 iter->global_seen = 1;
3788 /* Only check the symbol's kind if it has one. */
3791 switch (iter->domain)
3794 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3795 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3796 /* Some types are also in VAR_DOMAIN. */
3797 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3801 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3805 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3820 static struct compunit_symtab *
3821 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3822 const char *name, domain_enum domain)
3824 struct compunit_symtab *stab_best = NULL;
3825 struct mapped_index *index;
3827 dw2_setup (objfile);
3829 index = dwarf2_per_objfile->index_table;
3831 /* index is NULL if OBJF_READNOW. */
3834 struct dw2_symtab_iterator iter;
3835 struct dwarf2_per_cu_data *per_cu;
3837 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3839 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3841 struct symbol *sym, *with_opaque = NULL;
3842 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3843 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3844 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3846 sym = block_find_symbol (block, name, domain,
3847 block_find_non_opaque_type_preferred,
3850 /* Some caution must be observed with overloaded functions
3851 and methods, since the index will not contain any overload
3852 information (but NAME might contain it). */
3855 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3857 if (with_opaque != NULL
3858 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3861 /* Keep looking through other CUs. */
3869 dw2_print_stats (struct objfile *objfile)
3871 int i, total, count;
3873 dw2_setup (objfile);
3874 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3876 for (i = 0; i < total; ++i)
3878 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3880 if (!per_cu->v.quick->compunit_symtab)
3883 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3884 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3887 /* This dumps minimal information about the index.
3888 It is called via "mt print objfiles".
3889 One use is to verify .gdb_index has been loaded by the
3890 gdb.dwarf2/gdb-index.exp testcase. */
3893 dw2_dump (struct objfile *objfile)
3895 dw2_setup (objfile);
3896 gdb_assert (dwarf2_per_objfile->using_index);
3897 printf_filtered (".gdb_index:");
3898 if (dwarf2_per_objfile->index_table != NULL)
3900 printf_filtered (" version %d\n",
3901 dwarf2_per_objfile->index_table->version);
3904 printf_filtered (" faked for \"readnow\"\n");
3905 printf_filtered ("\n");
3909 dw2_relocate (struct objfile *objfile,
3910 const struct section_offsets *new_offsets,
3911 const struct section_offsets *delta)
3913 /* There's nothing to relocate here. */
3917 dw2_expand_symtabs_for_function (struct objfile *objfile,
3918 const char *func_name)
3920 struct mapped_index *index;
3922 dw2_setup (objfile);
3924 index = dwarf2_per_objfile->index_table;
3926 /* index is NULL if OBJF_READNOW. */
3929 struct dw2_symtab_iterator iter;
3930 struct dwarf2_per_cu_data *per_cu;
3932 /* Note: It doesn't matter what we pass for block_index here. */
3933 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3936 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3937 dw2_instantiate_symtab (per_cu);
3942 dw2_expand_all_symtabs (struct objfile *objfile)
3946 dw2_setup (objfile);
3948 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3949 + dwarf2_per_objfile->n_type_units); ++i)
3951 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3953 dw2_instantiate_symtab (per_cu);
3958 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3959 const char *fullname)
3963 dw2_setup (objfile);
3965 /* We don't need to consider type units here.
3966 This is only called for examining code, e.g. expand_line_sal.
3967 There can be an order of magnitude (or more) more type units
3968 than comp units, and we avoid them if we can. */
3970 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3973 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3974 struct quick_file_names *file_data;
3976 /* We only need to look at symtabs not already expanded. */
3977 if (per_cu->v.quick->compunit_symtab)
3980 file_data = dw2_get_file_names (per_cu);
3981 if (file_data == NULL)
3984 for (j = 0; j < file_data->num_file_names; ++j)
3986 const char *this_fullname = file_data->file_names[j];
3988 if (filename_cmp (this_fullname, fullname) == 0)
3990 dw2_instantiate_symtab (per_cu);
3998 dw2_map_matching_symbols (struct objfile *objfile,
3999 const char * name, domain_enum domain,
4001 int (*callback) (struct block *,
4002 struct symbol *, void *),
4003 void *data, symbol_compare_ftype *match,
4004 symbol_compare_ftype *ordered_compare)
4006 /* Currently unimplemented; used for Ada. The function can be called if the
4007 current language is Ada for a non-Ada objfile using GNU index. As Ada
4008 does not look for non-Ada symbols this function should just return. */
4012 dw2_expand_symtabs_matching
4013 (struct objfile *objfile,
4014 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4015 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4016 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4017 enum search_domain kind)
4021 struct mapped_index *index;
4023 dw2_setup (objfile);
4025 /* index_table is NULL if OBJF_READNOW. */
4026 if (!dwarf2_per_objfile->index_table)
4028 index = dwarf2_per_objfile->index_table;
4030 if (file_matcher != NULL)
4032 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4034 NULL, xcalloc, xfree));
4035 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4037 NULL, xcalloc, xfree));
4039 /* The rule is CUs specify all the files, including those used by
4040 any TU, so there's no need to scan TUs here. */
4042 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4045 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4046 struct quick_file_names *file_data;
4051 per_cu->v.quick->mark = 0;
4053 /* We only need to look at symtabs not already expanded. */
4054 if (per_cu->v.quick->compunit_symtab)
4057 file_data = dw2_get_file_names (per_cu);
4058 if (file_data == NULL)
4061 if (htab_find (visited_not_found.get (), file_data) != NULL)
4063 else if (htab_find (visited_found.get (), file_data) != NULL)
4065 per_cu->v.quick->mark = 1;
4069 for (j = 0; j < file_data->num_file_names; ++j)
4071 const char *this_real_name;
4073 if (file_matcher (file_data->file_names[j], false))
4075 per_cu->v.quick->mark = 1;
4079 /* Before we invoke realpath, which can get expensive when many
4080 files are involved, do a quick comparison of the basenames. */
4081 if (!basenames_may_differ
4082 && !file_matcher (lbasename (file_data->file_names[j]),
4086 this_real_name = dw2_get_real_path (objfile, file_data, j);
4087 if (file_matcher (this_real_name, false))
4089 per_cu->v.quick->mark = 1;
4094 slot = htab_find_slot (per_cu->v.quick->mark
4095 ? visited_found.get ()
4096 : visited_not_found.get (),
4102 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4104 offset_type idx = 2 * iter;
4106 offset_type *vec, vec_len, vec_idx;
4107 int global_seen = 0;
4111 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4114 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4116 if (!symbol_matcher (name))
4119 /* The name was matched, now expand corresponding CUs that were
4121 vec = (offset_type *) (index->constant_pool
4122 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4123 vec_len = MAYBE_SWAP (vec[0]);
4124 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4126 struct dwarf2_per_cu_data *per_cu;
4127 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4128 /* This value is only valid for index versions >= 7. */
4129 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4130 gdb_index_symbol_kind symbol_kind =
4131 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4132 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4133 /* Only check the symbol attributes if they're present.
4134 Indices prior to version 7 don't record them,
4135 and indices >= 7 may elide them for certain symbols
4136 (gold does this). */
4138 (index->version >= 7
4139 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4141 /* Work around gold/15646. */
4144 if (!is_static && global_seen)
4150 /* Only check the symbol's kind if it has one. */
4155 case VARIABLES_DOMAIN:
4156 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4159 case FUNCTIONS_DOMAIN:
4160 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4164 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4172 /* Don't crash on bad data. */
4173 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4174 + dwarf2_per_objfile->n_type_units))
4176 complaint (&symfile_complaints,
4177 _(".gdb_index entry has bad CU index"
4178 " [in module %s]"), objfile_name (objfile));
4182 per_cu = dw2_get_cutu (cu_index);
4183 if (file_matcher == NULL || per_cu->v.quick->mark)
4185 int symtab_was_null =
4186 (per_cu->v.quick->compunit_symtab == NULL);
4188 dw2_instantiate_symtab (per_cu);
4190 if (expansion_notify != NULL
4192 && per_cu->v.quick->compunit_symtab != NULL)
4194 expansion_notify (per_cu->v.quick->compunit_symtab);
4201 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4204 static struct compunit_symtab *
4205 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4210 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4211 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4214 if (cust->includes == NULL)
4217 for (i = 0; cust->includes[i]; ++i)
4219 struct compunit_symtab *s = cust->includes[i];
4221 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4229 static struct compunit_symtab *
4230 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4231 struct bound_minimal_symbol msymbol,
4233 struct obj_section *section,
4236 struct dwarf2_per_cu_data *data;
4237 struct compunit_symtab *result;
4239 dw2_setup (objfile);
4241 if (!objfile->psymtabs_addrmap)
4244 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4249 if (warn_if_readin && data->v.quick->compunit_symtab)
4250 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4251 paddress (get_objfile_arch (objfile), pc));
4254 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4256 gdb_assert (result != NULL);
4261 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4262 void *data, int need_fullname)
4265 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4266 NULL, xcalloc, xfree));
4268 dw2_setup (objfile);
4270 /* The rule is CUs specify all the files, including those used by
4271 any TU, so there's no need to scan TUs here.
4272 We can ignore file names coming from already-expanded CUs. */
4274 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4276 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4278 if (per_cu->v.quick->compunit_symtab)
4280 void **slot = htab_find_slot (visited.get (),
4281 per_cu->v.quick->file_names,
4284 *slot = per_cu->v.quick->file_names;
4288 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4291 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4292 struct quick_file_names *file_data;
4295 /* We only need to look at symtabs not already expanded. */
4296 if (per_cu->v.quick->compunit_symtab)
4299 file_data = dw2_get_file_names (per_cu);
4300 if (file_data == NULL)
4303 slot = htab_find_slot (visited.get (), file_data, INSERT);
4306 /* Already visited. */
4311 for (j = 0; j < file_data->num_file_names; ++j)
4313 const char *this_real_name;
4316 this_real_name = dw2_get_real_path (objfile, file_data, j);
4318 this_real_name = NULL;
4319 (*fun) (file_data->file_names[j], this_real_name, data);
4325 dw2_has_symbols (struct objfile *objfile)
4330 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4333 dw2_find_last_source_symtab,
4334 dw2_forget_cached_source_info,
4335 dw2_map_symtabs_matching_filename,
4340 dw2_expand_symtabs_for_function,
4341 dw2_expand_all_symtabs,
4342 dw2_expand_symtabs_with_fullname,
4343 dw2_map_matching_symbols,
4344 dw2_expand_symtabs_matching,
4345 dw2_find_pc_sect_compunit_symtab,
4346 dw2_map_symbol_filenames
4349 /* Initialize for reading DWARF for this objfile. Return 0 if this
4350 file will use psymtabs, or 1 if using the GNU index. */
4353 dwarf2_initialize_objfile (struct objfile *objfile)
4355 /* If we're about to read full symbols, don't bother with the
4356 indices. In this case we also don't care if some other debug
4357 format is making psymtabs, because they are all about to be
4359 if ((objfile->flags & OBJF_READNOW))
4363 dwarf2_per_objfile->using_index = 1;
4364 create_all_comp_units (objfile);
4365 create_all_type_units (objfile);
4366 dwarf2_per_objfile->quick_file_names_table =
4367 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4369 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4370 + dwarf2_per_objfile->n_type_units); ++i)
4372 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4374 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4375 struct dwarf2_per_cu_quick_data);
4378 /* Return 1 so that gdb sees the "quick" functions. However,
4379 these functions will be no-ops because we will have expanded
4384 if (dwarf2_read_index (objfile))
4392 /* Build a partial symbol table. */
4395 dwarf2_build_psymtabs (struct objfile *objfile)
4398 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4400 init_psymbol_list (objfile, 1024);
4405 /* This isn't really ideal: all the data we allocate on the
4406 objfile's obstack is still uselessly kept around. However,
4407 freeing it seems unsafe. */
4408 psymtab_discarder psymtabs (objfile);
4409 dwarf2_build_psymtabs_hard (objfile);
4412 CATCH (except, RETURN_MASK_ERROR)
4414 exception_print (gdb_stderr, except);
4419 /* Return the total length of the CU described by HEADER. */
4422 get_cu_length (const struct comp_unit_head *header)
4424 return header->initial_length_size + header->length;
4427 /* Return TRUE if SECT_OFF is within CU_HEADER. */
4430 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
4432 sect_offset bottom = cu_header->sect_off;
4433 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
4435 return sect_off >= bottom && sect_off < top;
4438 /* Find the base address of the compilation unit for range lists and
4439 location lists. It will normally be specified by DW_AT_low_pc.
4440 In DWARF-3 draft 4, the base address could be overridden by
4441 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4442 compilation units with discontinuous ranges. */
4445 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4447 struct attribute *attr;
4450 cu->base_address = 0;
4452 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4455 cu->base_address = attr_value_as_address (attr);
4460 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4463 cu->base_address = attr_value_as_address (attr);
4469 /* Read in the comp unit header information from the debug_info at info_ptr.
4470 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4471 NOTE: This leaves members offset, first_die_offset to be filled in
4474 static const gdb_byte *
4475 read_comp_unit_head (struct comp_unit_head *cu_header,
4476 const gdb_byte *info_ptr,
4477 struct dwarf2_section_info *section,
4478 rcuh_kind section_kind)
4481 unsigned int bytes_read;
4482 const char *filename = get_section_file_name (section);
4483 bfd *abfd = get_section_bfd_owner (section);
4485 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4486 cu_header->initial_length_size = bytes_read;
4487 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4488 info_ptr += bytes_read;
4489 cu_header->version = read_2_bytes (abfd, info_ptr);
4491 if (cu_header->version < 5)
4492 switch (section_kind)
4494 case rcuh_kind::COMPILE:
4495 cu_header->unit_type = DW_UT_compile;
4497 case rcuh_kind::TYPE:
4498 cu_header->unit_type = DW_UT_type;
4501 internal_error (__FILE__, __LINE__,
4502 _("read_comp_unit_head: invalid section_kind"));
4506 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4507 (read_1_byte (abfd, info_ptr));
4509 switch (cu_header->unit_type)
4512 if (section_kind != rcuh_kind::COMPILE)
4513 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4514 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4518 section_kind = rcuh_kind::TYPE;
4521 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4522 "(is %d, should be %d or %d) [in module %s]"),
4523 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4526 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4529 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4532 info_ptr += bytes_read;
4533 if (cu_header->version < 5)
4535 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4538 signed_addr = bfd_get_sign_extend_vma (abfd);
4539 if (signed_addr < 0)
4540 internal_error (__FILE__, __LINE__,
4541 _("read_comp_unit_head: dwarf from non elf file"));
4542 cu_header->signed_addr_p = signed_addr;
4544 if (section_kind == rcuh_kind::TYPE)
4546 LONGEST type_offset;
4548 cu_header->signature = read_8_bytes (abfd, info_ptr);
4551 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4552 info_ptr += bytes_read;
4553 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4554 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
4555 error (_("Dwarf Error: Too big type_offset in compilation unit "
4556 "header (is %s) [in module %s]"), plongest (type_offset),
4563 /* Helper function that returns the proper abbrev section for
4566 static struct dwarf2_section_info *
4567 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4569 struct dwarf2_section_info *abbrev;
4571 if (this_cu->is_dwz)
4572 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4574 abbrev = &dwarf2_per_objfile->abbrev;
4579 /* Subroutine of read_and_check_comp_unit_head and
4580 read_and_check_type_unit_head to simplify them.
4581 Perform various error checking on the header. */
4584 error_check_comp_unit_head (struct comp_unit_head *header,
4585 struct dwarf2_section_info *section,
4586 struct dwarf2_section_info *abbrev_section)
4588 const char *filename = get_section_file_name (section);
4590 if (header->version < 2 || header->version > 5)
4591 error (_("Dwarf Error: wrong version in compilation unit header "
4592 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4595 if (to_underlying (header->abbrev_sect_off)
4596 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4597 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4598 "(offset 0x%x + 6) [in module %s]"),
4599 to_underlying (header->abbrev_sect_off),
4600 to_underlying (header->sect_off),
4603 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
4604 avoid potential 32-bit overflow. */
4605 if (((ULONGEST) header->sect_off + get_cu_length (header))
4607 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4608 "(offset 0x%x + 0) [in module %s]"),
4609 header->length, to_underlying (header->sect_off),
4613 /* Read in a CU/TU header and perform some basic error checking.
4614 The contents of the header are stored in HEADER.
4615 The result is a pointer to the start of the first DIE. */
4617 static const gdb_byte *
4618 read_and_check_comp_unit_head (struct comp_unit_head *header,
4619 struct dwarf2_section_info *section,
4620 struct dwarf2_section_info *abbrev_section,
4621 const gdb_byte *info_ptr,
4622 rcuh_kind section_kind)
4624 const gdb_byte *beg_of_comp_unit = info_ptr;
4625 bfd *abfd = get_section_bfd_owner (section);
4627 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
4629 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4631 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
4633 error_check_comp_unit_head (header, section, abbrev_section);
4638 /* Fetch the abbreviation table offset from a comp or type unit header. */
4641 read_abbrev_offset (struct dwarf2_section_info *section,
4642 sect_offset sect_off)
4644 bfd *abfd = get_section_bfd_owner (section);
4645 const gdb_byte *info_ptr;
4646 unsigned int initial_length_size, offset_size;
4649 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4650 info_ptr = section->buffer + to_underlying (sect_off);
4651 read_initial_length (abfd, info_ptr, &initial_length_size);
4652 offset_size = initial_length_size == 4 ? 4 : 8;
4653 info_ptr += initial_length_size;
4655 version = read_2_bytes (abfd, info_ptr);
4659 /* Skip unit type and address size. */
4663 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
4666 /* Allocate a new partial symtab for file named NAME and mark this new
4667 partial symtab as being an include of PST. */
4670 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4671 struct objfile *objfile)
4673 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4675 if (!IS_ABSOLUTE_PATH (subpst->filename))
4677 /* It shares objfile->objfile_obstack. */
4678 subpst->dirname = pst->dirname;
4681 subpst->textlow = 0;
4682 subpst->texthigh = 0;
4684 subpst->dependencies
4685 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4686 subpst->dependencies[0] = pst;
4687 subpst->number_of_dependencies = 1;
4689 subpst->globals_offset = 0;
4690 subpst->n_global_syms = 0;
4691 subpst->statics_offset = 0;
4692 subpst->n_static_syms = 0;
4693 subpst->compunit_symtab = NULL;
4694 subpst->read_symtab = pst->read_symtab;
4697 /* No private part is necessary for include psymtabs. This property
4698 can be used to differentiate between such include psymtabs and
4699 the regular ones. */
4700 subpst->read_symtab_private = NULL;
4703 /* Read the Line Number Program data and extract the list of files
4704 included by the source file represented by PST. Build an include
4705 partial symtab for each of these included files. */
4708 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4709 struct die_info *die,
4710 struct partial_symtab *pst)
4713 struct attribute *attr;
4715 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4717 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
4719 return; /* No linetable, so no includes. */
4721 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4722 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
4726 hash_signatured_type (const void *item)
4728 const struct signatured_type *sig_type
4729 = (const struct signatured_type *) item;
4731 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4732 return sig_type->signature;
4736 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4738 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4739 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4741 return lhs->signature == rhs->signature;
4744 /* Allocate a hash table for signatured types. */
4747 allocate_signatured_type_table (struct objfile *objfile)
4749 return htab_create_alloc_ex (41,
4750 hash_signatured_type,
4753 &objfile->objfile_obstack,
4754 hashtab_obstack_allocate,
4755 dummy_obstack_deallocate);
4758 /* A helper function to add a signatured type CU to a table. */
4761 add_signatured_type_cu_to_table (void **slot, void *datum)
4763 struct signatured_type *sigt = (struct signatured_type *) *slot;
4764 struct signatured_type ***datap = (struct signatured_type ***) datum;
4772 /* A helper for create_debug_types_hash_table. Read types from SECTION
4773 and fill them into TYPES_HTAB. It will process only type units,
4774 therefore DW_UT_type. */
4777 create_debug_type_hash_table (struct dwo_file *dwo_file,
4778 dwarf2_section_info *section, htab_t &types_htab,
4779 rcuh_kind section_kind)
4781 struct objfile *objfile = dwarf2_per_objfile->objfile;
4782 struct dwarf2_section_info *abbrev_section;
4784 const gdb_byte *info_ptr, *end_ptr;
4786 abbrev_section = (dwo_file != NULL
4787 ? &dwo_file->sections.abbrev
4788 : &dwarf2_per_objfile->abbrev);
4790 if (dwarf_read_debug)
4791 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4792 get_section_name (section),
4793 get_section_file_name (abbrev_section));
4795 dwarf2_read_section (objfile, section);
4796 info_ptr = section->buffer;
4798 if (info_ptr == NULL)
4801 /* We can't set abfd until now because the section may be empty or
4802 not present, in which case the bfd is unknown. */
4803 abfd = get_section_bfd_owner (section);
4805 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4806 because we don't need to read any dies: the signature is in the
4809 end_ptr = info_ptr + section->size;
4810 while (info_ptr < end_ptr)
4812 struct signatured_type *sig_type;
4813 struct dwo_unit *dwo_tu;
4815 const gdb_byte *ptr = info_ptr;
4816 struct comp_unit_head header;
4817 unsigned int length;
4819 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
4821 /* Initialize it due to a false compiler warning. */
4822 header.signature = -1;
4823 header.type_cu_offset_in_tu = (cu_offset) -1;
4825 /* We need to read the type's signature in order to build the hash
4826 table, but we don't need anything else just yet. */
4828 ptr = read_and_check_comp_unit_head (&header, section,
4829 abbrev_section, ptr, section_kind);
4831 length = get_cu_length (&header);
4833 /* Skip dummy type units. */
4834 if (ptr >= info_ptr + length
4835 || peek_abbrev_code (abfd, ptr) == 0
4836 || header.unit_type != DW_UT_type)
4842 if (types_htab == NULL)
4845 types_htab = allocate_dwo_unit_table (objfile);
4847 types_htab = allocate_signatured_type_table (objfile);
4853 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4855 dwo_tu->dwo_file = dwo_file;
4856 dwo_tu->signature = header.signature;
4857 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
4858 dwo_tu->section = section;
4859 dwo_tu->sect_off = sect_off;
4860 dwo_tu->length = length;
4864 /* N.B.: type_offset is not usable if this type uses a DWO file.
4865 The real type_offset is in the DWO file. */
4867 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4868 struct signatured_type);
4869 sig_type->signature = header.signature;
4870 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
4871 sig_type->per_cu.objfile = objfile;
4872 sig_type->per_cu.is_debug_types = 1;
4873 sig_type->per_cu.section = section;
4874 sig_type->per_cu.sect_off = sect_off;
4875 sig_type->per_cu.length = length;
4878 slot = htab_find_slot (types_htab,
4879 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4881 gdb_assert (slot != NULL);
4884 sect_offset dup_sect_off;
4888 const struct dwo_unit *dup_tu
4889 = (const struct dwo_unit *) *slot;
4891 dup_sect_off = dup_tu->sect_off;
4895 const struct signatured_type *dup_tu
4896 = (const struct signatured_type *) *slot;
4898 dup_sect_off = dup_tu->per_cu.sect_off;
4901 complaint (&symfile_complaints,
4902 _("debug type entry at offset 0x%x is duplicate to"
4903 " the entry at offset 0x%x, signature %s"),
4904 to_underlying (sect_off), to_underlying (dup_sect_off),
4905 hex_string (header.signature));
4907 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4909 if (dwarf_read_debug > 1)
4910 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4911 to_underlying (sect_off),
4912 hex_string (header.signature));
4918 /* Create the hash table of all entries in the .debug_types
4919 (or .debug_types.dwo) section(s).
4920 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4921 otherwise it is NULL.
4923 The result is a pointer to the hash table or NULL if there are no types.
4925 Note: This function processes DWO files only, not DWP files. */
4928 create_debug_types_hash_table (struct dwo_file *dwo_file,
4929 VEC (dwarf2_section_info_def) *types,
4933 struct dwarf2_section_info *section;
4935 if (VEC_empty (dwarf2_section_info_def, types))
4939 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4941 create_debug_type_hash_table (dwo_file, section, types_htab,
4945 /* Create the hash table of all entries in the .debug_types section,
4946 and initialize all_type_units.
4947 The result is zero if there is an error (e.g. missing .debug_types section),
4948 otherwise non-zero. */
4951 create_all_type_units (struct objfile *objfile)
4953 htab_t types_htab = NULL;
4954 struct signatured_type **iter;
4956 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4957 rcuh_kind::COMPILE);
4958 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
4959 if (types_htab == NULL)
4961 dwarf2_per_objfile->signatured_types = NULL;
4965 dwarf2_per_objfile->signatured_types = types_htab;
4967 dwarf2_per_objfile->n_type_units
4968 = dwarf2_per_objfile->n_allocated_type_units
4969 = htab_elements (types_htab);
4970 dwarf2_per_objfile->all_type_units =
4971 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4972 iter = &dwarf2_per_objfile->all_type_units[0];
4973 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4974 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4975 == dwarf2_per_objfile->n_type_units);
4980 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4981 If SLOT is non-NULL, it is the entry to use in the hash table.
4982 Otherwise we find one. */
4984 static struct signatured_type *
4985 add_type_unit (ULONGEST sig, void **slot)
4987 struct objfile *objfile = dwarf2_per_objfile->objfile;
4988 int n_type_units = dwarf2_per_objfile->n_type_units;
4989 struct signatured_type *sig_type;
4991 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4993 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4995 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4996 dwarf2_per_objfile->n_allocated_type_units = 1;
4997 dwarf2_per_objfile->n_allocated_type_units *= 2;
4998 dwarf2_per_objfile->all_type_units
4999 = XRESIZEVEC (struct signatured_type *,
5000 dwarf2_per_objfile->all_type_units,
5001 dwarf2_per_objfile->n_allocated_type_units);
5002 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5004 dwarf2_per_objfile->n_type_units = n_type_units;
5006 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5007 struct signatured_type);
5008 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5009 sig_type->signature = sig;
5010 sig_type->per_cu.is_debug_types = 1;
5011 if (dwarf2_per_objfile->using_index)
5013 sig_type->per_cu.v.quick =
5014 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5015 struct dwarf2_per_cu_quick_data);
5020 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5023 gdb_assert (*slot == NULL);
5025 /* The rest of sig_type must be filled in by the caller. */
5029 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5030 Fill in SIG_ENTRY with DWO_ENTRY. */
5033 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5034 struct signatured_type *sig_entry,
5035 struct dwo_unit *dwo_entry)
5037 /* Make sure we're not clobbering something we don't expect to. */
5038 gdb_assert (! sig_entry->per_cu.queued);
5039 gdb_assert (sig_entry->per_cu.cu == NULL);
5040 if (dwarf2_per_objfile->using_index)
5042 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5043 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5046 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5047 gdb_assert (sig_entry->signature == dwo_entry->signature);
5048 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5049 gdb_assert (sig_entry->type_unit_group == NULL);
5050 gdb_assert (sig_entry->dwo_unit == NULL);
5052 sig_entry->per_cu.section = dwo_entry->section;
5053 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5054 sig_entry->per_cu.length = dwo_entry->length;
5055 sig_entry->per_cu.reading_dwo_directly = 1;
5056 sig_entry->per_cu.objfile = objfile;
5057 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5058 sig_entry->dwo_unit = dwo_entry;
5061 /* Subroutine of lookup_signatured_type.
5062 If we haven't read the TU yet, create the signatured_type data structure
5063 for a TU to be read in directly from a DWO file, bypassing the stub.
5064 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5065 using .gdb_index, then when reading a CU we want to stay in the DWO file
5066 containing that CU. Otherwise we could end up reading several other DWO
5067 files (due to comdat folding) to process the transitive closure of all the
5068 mentioned TUs, and that can be slow. The current DWO file will have every
5069 type signature that it needs.
5070 We only do this for .gdb_index because in the psymtab case we already have
5071 to read all the DWOs to build the type unit groups. */
5073 static struct signatured_type *
5074 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5076 struct objfile *objfile = dwarf2_per_objfile->objfile;
5077 struct dwo_file *dwo_file;
5078 struct dwo_unit find_dwo_entry, *dwo_entry;
5079 struct signatured_type find_sig_entry, *sig_entry;
5082 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5084 /* If TU skeletons have been removed then we may not have read in any
5086 if (dwarf2_per_objfile->signatured_types == NULL)
5088 dwarf2_per_objfile->signatured_types
5089 = allocate_signatured_type_table (objfile);
5092 /* We only ever need to read in one copy of a signatured type.
5093 Use the global signatured_types array to do our own comdat-folding
5094 of types. If this is the first time we're reading this TU, and
5095 the TU has an entry in .gdb_index, replace the recorded data from
5096 .gdb_index with this TU. */
5098 find_sig_entry.signature = sig;
5099 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5100 &find_sig_entry, INSERT);
5101 sig_entry = (struct signatured_type *) *slot;
5103 /* We can get here with the TU already read, *or* in the process of being
5104 read. Don't reassign the global entry to point to this DWO if that's
5105 the case. Also note that if the TU is already being read, it may not
5106 have come from a DWO, the program may be a mix of Fission-compiled
5107 code and non-Fission-compiled code. */
5109 /* Have we already tried to read this TU?
5110 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5111 needn't exist in the global table yet). */
5112 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5115 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5116 dwo_unit of the TU itself. */
5117 dwo_file = cu->dwo_unit->dwo_file;
5119 /* Ok, this is the first time we're reading this TU. */
5120 if (dwo_file->tus == NULL)
5122 find_dwo_entry.signature = sig;
5123 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5124 if (dwo_entry == NULL)
5127 /* If the global table doesn't have an entry for this TU, add one. */
5128 if (sig_entry == NULL)
5129 sig_entry = add_type_unit (sig, slot);
5131 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5132 sig_entry->per_cu.tu_read = 1;
5136 /* Subroutine of lookup_signatured_type.
5137 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5138 then try the DWP file. If the TU stub (skeleton) has been removed then
5139 it won't be in .gdb_index. */
5141 static struct signatured_type *
5142 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5144 struct objfile *objfile = dwarf2_per_objfile->objfile;
5145 struct dwp_file *dwp_file = get_dwp_file ();
5146 struct dwo_unit *dwo_entry;
5147 struct signatured_type find_sig_entry, *sig_entry;
5150 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5151 gdb_assert (dwp_file != NULL);
5153 /* If TU skeletons have been removed then we may not have read in any
5155 if (dwarf2_per_objfile->signatured_types == NULL)
5157 dwarf2_per_objfile->signatured_types
5158 = allocate_signatured_type_table (objfile);
5161 find_sig_entry.signature = sig;
5162 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5163 &find_sig_entry, INSERT);
5164 sig_entry = (struct signatured_type *) *slot;
5166 /* Have we already tried to read this TU?
5167 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5168 needn't exist in the global table yet). */
5169 if (sig_entry != NULL)
5172 if (dwp_file->tus == NULL)
5174 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5175 sig, 1 /* is_debug_types */);
5176 if (dwo_entry == NULL)
5179 sig_entry = add_type_unit (sig, slot);
5180 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5185 /* Lookup a signature based type for DW_FORM_ref_sig8.
5186 Returns NULL if signature SIG is not present in the table.
5187 It is up to the caller to complain about this. */
5189 static struct signatured_type *
5190 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5193 && dwarf2_per_objfile->using_index)
5195 /* We're in a DWO/DWP file, and we're using .gdb_index.
5196 These cases require special processing. */
5197 if (get_dwp_file () == NULL)
5198 return lookup_dwo_signatured_type (cu, sig);
5200 return lookup_dwp_signatured_type (cu, sig);
5204 struct signatured_type find_entry, *entry;
5206 if (dwarf2_per_objfile->signatured_types == NULL)
5208 find_entry.signature = sig;
5209 entry = ((struct signatured_type *)
5210 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5215 /* Low level DIE reading support. */
5217 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5220 init_cu_die_reader (struct die_reader_specs *reader,
5221 struct dwarf2_cu *cu,
5222 struct dwarf2_section_info *section,
5223 struct dwo_file *dwo_file)
5225 gdb_assert (section->readin && section->buffer != NULL);
5226 reader->abfd = get_section_bfd_owner (section);
5228 reader->dwo_file = dwo_file;
5229 reader->die_section = section;
5230 reader->buffer = section->buffer;
5231 reader->buffer_end = section->buffer + section->size;
5232 reader->comp_dir = NULL;
5235 /* Subroutine of init_cutu_and_read_dies to simplify it.
5236 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5237 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5240 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5241 from it to the DIE in the DWO. If NULL we are skipping the stub.
5242 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5243 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5244 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5245 STUB_COMP_DIR may be non-NULL.
5246 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5247 are filled in with the info of the DIE from the DWO file.
5248 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5249 provided an abbrev table to use.
5250 The result is non-zero if a valid (non-dummy) DIE was found. */
5253 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5254 struct dwo_unit *dwo_unit,
5255 int abbrev_table_provided,
5256 struct die_info *stub_comp_unit_die,
5257 const char *stub_comp_dir,
5258 struct die_reader_specs *result_reader,
5259 const gdb_byte **result_info_ptr,
5260 struct die_info **result_comp_unit_die,
5261 int *result_has_children)
5263 struct objfile *objfile = dwarf2_per_objfile->objfile;
5264 struct dwarf2_cu *cu = this_cu->cu;
5265 struct dwarf2_section_info *section;
5267 const gdb_byte *begin_info_ptr, *info_ptr;
5268 ULONGEST signature; /* Or dwo_id. */
5269 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5270 int i,num_extra_attrs;
5271 struct dwarf2_section_info *dwo_abbrev_section;
5272 struct attribute *attr;
5273 struct die_info *comp_unit_die;
5275 /* At most one of these may be provided. */
5276 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5278 /* These attributes aren't processed until later:
5279 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5280 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5281 referenced later. However, these attributes are found in the stub
5282 which we won't have later. In order to not impose this complication
5283 on the rest of the code, we read them here and copy them to the
5292 if (stub_comp_unit_die != NULL)
5294 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5296 if (! this_cu->is_debug_types)
5297 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5298 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5299 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5300 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5301 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5303 /* There should be a DW_AT_addr_base attribute here (if needed).
5304 We need the value before we can process DW_FORM_GNU_addr_index. */
5306 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5308 cu->addr_base = DW_UNSND (attr);
5310 /* There should be a DW_AT_ranges_base attribute here (if needed).
5311 We need the value before we can process DW_AT_ranges. */
5312 cu->ranges_base = 0;
5313 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5315 cu->ranges_base = DW_UNSND (attr);
5317 else if (stub_comp_dir != NULL)
5319 /* Reconstruct the comp_dir attribute to simplify the code below. */
5320 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5321 comp_dir->name = DW_AT_comp_dir;
5322 comp_dir->form = DW_FORM_string;
5323 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5324 DW_STRING (comp_dir) = stub_comp_dir;
5327 /* Set up for reading the DWO CU/TU. */
5328 cu->dwo_unit = dwo_unit;
5329 section = dwo_unit->section;
5330 dwarf2_read_section (objfile, section);
5331 abfd = get_section_bfd_owner (section);
5332 begin_info_ptr = info_ptr = (section->buffer
5333 + to_underlying (dwo_unit->sect_off));
5334 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5335 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5337 if (this_cu->is_debug_types)
5339 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5341 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5343 info_ptr, rcuh_kind::TYPE);
5344 /* This is not an assert because it can be caused by bad debug info. */
5345 if (sig_type->signature != cu->header.signature)
5347 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5348 " TU at offset 0x%x [in module %s]"),
5349 hex_string (sig_type->signature),
5350 hex_string (cu->header.signature),
5351 to_underlying (dwo_unit->sect_off),
5352 bfd_get_filename (abfd));
5354 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5355 /* For DWOs coming from DWP files, we don't know the CU length
5356 nor the type's offset in the TU until now. */
5357 dwo_unit->length = get_cu_length (&cu->header);
5358 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5360 /* Establish the type offset that can be used to lookup the type.
5361 For DWO files, we don't know it until now. */
5362 sig_type->type_offset_in_section
5363 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5367 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5369 info_ptr, rcuh_kind::COMPILE);
5370 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5371 /* For DWOs coming from DWP files, we don't know the CU length
5373 dwo_unit->length = get_cu_length (&cu->header);
5376 /* Replace the CU's original abbrev table with the DWO's.
5377 Reminder: We can't read the abbrev table until we've read the header. */
5378 if (abbrev_table_provided)
5380 /* Don't free the provided abbrev table, the caller of
5381 init_cutu_and_read_dies owns it. */
5382 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5383 /* Ensure the DWO abbrev table gets freed. */
5384 make_cleanup (dwarf2_free_abbrev_table, cu);
5388 dwarf2_free_abbrev_table (cu);
5389 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5390 /* Leave any existing abbrev table cleanup as is. */
5393 /* Read in the die, but leave space to copy over the attributes
5394 from the stub. This has the benefit of simplifying the rest of
5395 the code - all the work to maintain the illusion of a single
5396 DW_TAG_{compile,type}_unit DIE is done here. */
5397 num_extra_attrs = ((stmt_list != NULL)
5401 + (comp_dir != NULL));
5402 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5403 result_has_children, num_extra_attrs);
5405 /* Copy over the attributes from the stub to the DIE we just read in. */
5406 comp_unit_die = *result_comp_unit_die;
5407 i = comp_unit_die->num_attrs;
5408 if (stmt_list != NULL)
5409 comp_unit_die->attrs[i++] = *stmt_list;
5411 comp_unit_die->attrs[i++] = *low_pc;
5412 if (high_pc != NULL)
5413 comp_unit_die->attrs[i++] = *high_pc;
5415 comp_unit_die->attrs[i++] = *ranges;
5416 if (comp_dir != NULL)
5417 comp_unit_die->attrs[i++] = *comp_dir;
5418 comp_unit_die->num_attrs += num_extra_attrs;
5420 if (dwarf_die_debug)
5422 fprintf_unfiltered (gdb_stdlog,
5423 "Read die from %s@0x%x of %s:\n",
5424 get_section_name (section),
5425 (unsigned) (begin_info_ptr - section->buffer),
5426 bfd_get_filename (abfd));
5427 dump_die (comp_unit_die, dwarf_die_debug);
5430 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5431 TUs by skipping the stub and going directly to the entry in the DWO file.
5432 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5433 to get it via circuitous means. Blech. */
5434 if (comp_dir != NULL)
5435 result_reader->comp_dir = DW_STRING (comp_dir);
5437 /* Skip dummy compilation units. */
5438 if (info_ptr >= begin_info_ptr + dwo_unit->length
5439 || peek_abbrev_code (abfd, info_ptr) == 0)
5442 *result_info_ptr = info_ptr;
5446 /* Subroutine of init_cutu_and_read_dies to simplify it.
5447 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5448 Returns NULL if the specified DWO unit cannot be found. */
5450 static struct dwo_unit *
5451 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5452 struct die_info *comp_unit_die)
5454 struct dwarf2_cu *cu = this_cu->cu;
5455 struct attribute *attr;
5457 struct dwo_unit *dwo_unit;
5458 const char *comp_dir, *dwo_name;
5460 gdb_assert (cu != NULL);
5462 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5463 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5464 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5466 if (this_cu->is_debug_types)
5468 struct signatured_type *sig_type;
5470 /* Since this_cu is the first member of struct signatured_type,
5471 we can go from a pointer to one to a pointer to the other. */
5472 sig_type = (struct signatured_type *) this_cu;
5473 signature = sig_type->signature;
5474 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5478 struct attribute *attr;
5480 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5482 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5484 dwo_name, objfile_name (this_cu->objfile));
5485 signature = DW_UNSND (attr);
5486 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5493 /* Subroutine of init_cutu_and_read_dies to simplify it.
5494 See it for a description of the parameters.
5495 Read a TU directly from a DWO file, bypassing the stub.
5497 Note: This function could be a little bit simpler if we shared cleanups
5498 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5499 to do, so we keep this function self-contained. Or we could move this
5500 into our caller, but it's complex enough already. */
5503 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5504 int use_existing_cu, int keep,
5505 die_reader_func_ftype *die_reader_func,
5508 struct dwarf2_cu *cu;
5509 struct signatured_type *sig_type;
5510 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5511 struct die_reader_specs reader;
5512 const gdb_byte *info_ptr;
5513 struct die_info *comp_unit_die;
5516 /* Verify we can do the following downcast, and that we have the
5518 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5519 sig_type = (struct signatured_type *) this_cu;
5520 gdb_assert (sig_type->dwo_unit != NULL);
5522 cleanups = make_cleanup (null_cleanup, NULL);
5524 if (use_existing_cu && this_cu->cu != NULL)
5526 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5528 /* There's no need to do the rereading_dwo_cu handling that
5529 init_cutu_and_read_dies does since we don't read the stub. */
5533 /* If !use_existing_cu, this_cu->cu must be NULL. */
5534 gdb_assert (this_cu->cu == NULL);
5535 cu = XNEW (struct dwarf2_cu);
5536 init_one_comp_unit (cu, this_cu);
5537 /* If an error occurs while loading, release our storage. */
5538 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5541 /* A future optimization, if needed, would be to use an existing
5542 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5543 could share abbrev tables. */
5545 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5546 0 /* abbrev_table_provided */,
5547 NULL /* stub_comp_unit_die */,
5548 sig_type->dwo_unit->dwo_file->comp_dir,
5550 &comp_unit_die, &has_children) == 0)
5553 do_cleanups (cleanups);
5557 /* All the "real" work is done here. */
5558 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5560 /* This duplicates the code in init_cutu_and_read_dies,
5561 but the alternative is making the latter more complex.
5562 This function is only for the special case of using DWO files directly:
5563 no point in overly complicating the general case just to handle this. */
5564 if (free_cu_cleanup != NULL)
5568 /* We've successfully allocated this compilation unit. Let our
5569 caller clean it up when finished with it. */
5570 discard_cleanups (free_cu_cleanup);
5572 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5573 So we have to manually free the abbrev table. */
5574 dwarf2_free_abbrev_table (cu);
5576 /* Link this CU into read_in_chain. */
5577 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5578 dwarf2_per_objfile->read_in_chain = this_cu;
5581 do_cleanups (free_cu_cleanup);
5584 do_cleanups (cleanups);
5587 /* Initialize a CU (or TU) and read its DIEs.
5588 If the CU defers to a DWO file, read the DWO file as well.
5590 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5591 Otherwise the table specified in the comp unit header is read in and used.
5592 This is an optimization for when we already have the abbrev table.
5594 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5595 Otherwise, a new CU is allocated with xmalloc.
5597 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5598 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5600 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5601 linker) then DIE_READER_FUNC will not get called. */
5604 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5605 struct abbrev_table *abbrev_table,
5606 int use_existing_cu, int keep,
5607 die_reader_func_ftype *die_reader_func,
5610 struct objfile *objfile = dwarf2_per_objfile->objfile;
5611 struct dwarf2_section_info *section = this_cu->section;
5612 bfd *abfd = get_section_bfd_owner (section);
5613 struct dwarf2_cu *cu;
5614 const gdb_byte *begin_info_ptr, *info_ptr;
5615 struct die_reader_specs reader;
5616 struct die_info *comp_unit_die;
5618 struct attribute *attr;
5619 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5620 struct signatured_type *sig_type = NULL;
5621 struct dwarf2_section_info *abbrev_section;
5622 /* Non-zero if CU currently points to a DWO file and we need to
5623 reread it. When this happens we need to reread the skeleton die
5624 before we can reread the DWO file (this only applies to CUs, not TUs). */
5625 int rereading_dwo_cu = 0;
5627 if (dwarf_die_debug)
5628 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5629 this_cu->is_debug_types ? "type" : "comp",
5630 to_underlying (this_cu->sect_off));
5632 if (use_existing_cu)
5635 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5636 file (instead of going through the stub), short-circuit all of this. */
5637 if (this_cu->reading_dwo_directly)
5639 /* Narrow down the scope of possibilities to have to understand. */
5640 gdb_assert (this_cu->is_debug_types);
5641 gdb_assert (abbrev_table == NULL);
5642 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5643 die_reader_func, data);
5647 cleanups = make_cleanup (null_cleanup, NULL);
5649 /* This is cheap if the section is already read in. */
5650 dwarf2_read_section (objfile, section);
5652 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5654 abbrev_section = get_abbrev_section_for_cu (this_cu);
5656 if (use_existing_cu && this_cu->cu != NULL)
5659 /* If this CU is from a DWO file we need to start over, we need to
5660 refetch the attributes from the skeleton CU.
5661 This could be optimized by retrieving those attributes from when we
5662 were here the first time: the previous comp_unit_die was stored in
5663 comp_unit_obstack. But there's no data yet that we need this
5665 if (cu->dwo_unit != NULL)
5666 rereading_dwo_cu = 1;
5670 /* If !use_existing_cu, this_cu->cu must be NULL. */
5671 gdb_assert (this_cu->cu == NULL);
5672 cu = XNEW (struct dwarf2_cu);
5673 init_one_comp_unit (cu, this_cu);
5674 /* If an error occurs while loading, release our storage. */
5675 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5678 /* Get the header. */
5679 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
5681 /* We already have the header, there's no need to read it in again. */
5682 info_ptr += to_underlying (cu->header.first_die_cu_offset);
5686 if (this_cu->is_debug_types)
5688 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5689 abbrev_section, info_ptr,
5692 /* Since per_cu is the first member of struct signatured_type,
5693 we can go from a pointer to one to a pointer to the other. */
5694 sig_type = (struct signatured_type *) this_cu;
5695 gdb_assert (sig_type->signature == cu->header.signature);
5696 gdb_assert (sig_type->type_offset_in_tu
5697 == cu->header.type_cu_offset_in_tu);
5698 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5700 /* LENGTH has not been set yet for type units if we're
5701 using .gdb_index. */
5702 this_cu->length = get_cu_length (&cu->header);
5704 /* Establish the type offset that can be used to lookup the type. */
5705 sig_type->type_offset_in_section =
5706 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
5708 this_cu->dwarf_version = cu->header.version;
5712 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5715 rcuh_kind::COMPILE);
5717 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5718 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5719 this_cu->dwarf_version = cu->header.version;
5723 /* Skip dummy compilation units. */
5724 if (info_ptr >= begin_info_ptr + this_cu->length
5725 || peek_abbrev_code (abfd, info_ptr) == 0)
5727 do_cleanups (cleanups);
5731 /* If we don't have them yet, read the abbrevs for this compilation unit.
5732 And if we need to read them now, make sure they're freed when we're
5733 done. Note that it's important that if the CU had an abbrev table
5734 on entry we don't free it when we're done: Somewhere up the call stack
5735 it may be in use. */
5736 if (abbrev_table != NULL)
5738 gdb_assert (cu->abbrev_table == NULL);
5739 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
5740 cu->abbrev_table = abbrev_table;
5742 else if (cu->abbrev_table == NULL)
5744 dwarf2_read_abbrevs (cu, abbrev_section);
5745 make_cleanup (dwarf2_free_abbrev_table, cu);
5747 else if (rereading_dwo_cu)
5749 dwarf2_free_abbrev_table (cu);
5750 dwarf2_read_abbrevs (cu, abbrev_section);
5753 /* Read the top level CU/TU die. */
5754 init_cu_die_reader (&reader, cu, section, NULL);
5755 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5757 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5759 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5760 DWO CU, that this test will fail (the attribute will not be present). */
5761 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5764 struct dwo_unit *dwo_unit;
5765 struct die_info *dwo_comp_unit_die;
5769 complaint (&symfile_complaints,
5770 _("compilation unit with DW_AT_GNU_dwo_name"
5771 " has children (offset 0x%x) [in module %s]"),
5772 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
5774 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5775 if (dwo_unit != NULL)
5777 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5778 abbrev_table != NULL,
5779 comp_unit_die, NULL,
5781 &dwo_comp_unit_die, &has_children) == 0)
5784 do_cleanups (cleanups);
5787 comp_unit_die = dwo_comp_unit_die;
5791 /* Yikes, we couldn't find the rest of the DIE, we only have
5792 the stub. A complaint has already been logged. There's
5793 not much more we can do except pass on the stub DIE to
5794 die_reader_func. We don't want to throw an error on bad
5799 /* All of the above is setup for this call. Yikes. */
5800 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5802 /* Done, clean up. */
5803 if (free_cu_cleanup != NULL)
5807 /* We've successfully allocated this compilation unit. Let our
5808 caller clean it up when finished with it. */
5809 discard_cleanups (free_cu_cleanup);
5811 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5812 So we have to manually free the abbrev table. */
5813 dwarf2_free_abbrev_table (cu);
5815 /* Link this CU into read_in_chain. */
5816 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5817 dwarf2_per_objfile->read_in_chain = this_cu;
5820 do_cleanups (free_cu_cleanup);
5823 do_cleanups (cleanups);
5826 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5827 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5828 to have already done the lookup to find the DWO file).
5830 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5831 THIS_CU->is_debug_types, but nothing else.
5833 We fill in THIS_CU->length.
5835 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5836 linker) then DIE_READER_FUNC will not get called.
5838 THIS_CU->cu is always freed when done.
5839 This is done in order to not leave THIS_CU->cu in a state where we have
5840 to care whether it refers to the "main" CU or the DWO CU. */
5843 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5844 struct dwo_file *dwo_file,
5845 die_reader_func_ftype *die_reader_func,
5848 struct objfile *objfile = dwarf2_per_objfile->objfile;
5849 struct dwarf2_section_info *section = this_cu->section;
5850 bfd *abfd = get_section_bfd_owner (section);
5851 struct dwarf2_section_info *abbrev_section;
5852 struct dwarf2_cu cu;
5853 const gdb_byte *begin_info_ptr, *info_ptr;
5854 struct die_reader_specs reader;
5855 struct cleanup *cleanups;
5856 struct die_info *comp_unit_die;
5859 if (dwarf_die_debug)
5860 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5861 this_cu->is_debug_types ? "type" : "comp",
5862 to_underlying (this_cu->sect_off));
5864 gdb_assert (this_cu->cu == NULL);
5866 abbrev_section = (dwo_file != NULL
5867 ? &dwo_file->sections.abbrev
5868 : get_abbrev_section_for_cu (this_cu));
5870 /* This is cheap if the section is already read in. */
5871 dwarf2_read_section (objfile, section);
5873 init_one_comp_unit (&cu, this_cu);
5875 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5877 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5878 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5879 abbrev_section, info_ptr,
5880 (this_cu->is_debug_types
5882 : rcuh_kind::COMPILE));
5884 this_cu->length = get_cu_length (&cu.header);
5886 /* Skip dummy compilation units. */
5887 if (info_ptr >= begin_info_ptr + this_cu->length
5888 || peek_abbrev_code (abfd, info_ptr) == 0)
5890 do_cleanups (cleanups);
5894 dwarf2_read_abbrevs (&cu, abbrev_section);
5895 make_cleanup (dwarf2_free_abbrev_table, &cu);
5897 init_cu_die_reader (&reader, &cu, section, dwo_file);
5898 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5900 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5902 do_cleanups (cleanups);
5905 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5906 does not lookup the specified DWO file.
5907 This cannot be used to read DWO files.
5909 THIS_CU->cu is always freed when done.
5910 This is done in order to not leave THIS_CU->cu in a state where we have
5911 to care whether it refers to the "main" CU or the DWO CU.
5912 We can revisit this if the data shows there's a performance issue. */
5915 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5916 die_reader_func_ftype *die_reader_func,
5919 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5922 /* Type Unit Groups.
5924 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5925 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5926 so that all types coming from the same compilation (.o file) are grouped
5927 together. A future step could be to put the types in the same symtab as
5928 the CU the types ultimately came from. */
5931 hash_type_unit_group (const void *item)
5933 const struct type_unit_group *tu_group
5934 = (const struct type_unit_group *) item;
5936 return hash_stmt_list_entry (&tu_group->hash);
5940 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5942 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5943 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5945 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5948 /* Allocate a hash table for type unit groups. */
5951 allocate_type_unit_groups_table (void)
5953 return htab_create_alloc_ex (3,
5954 hash_type_unit_group,
5957 &dwarf2_per_objfile->objfile->objfile_obstack,
5958 hashtab_obstack_allocate,
5959 dummy_obstack_deallocate);
5962 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5963 partial symtabs. We combine several TUs per psymtab to not let the size
5964 of any one psymtab grow too big. */
5965 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5966 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5968 /* Helper routine for get_type_unit_group.
5969 Create the type_unit_group object used to hold one or more TUs. */
5971 static struct type_unit_group *
5972 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5974 struct objfile *objfile = dwarf2_per_objfile->objfile;
5975 struct dwarf2_per_cu_data *per_cu;
5976 struct type_unit_group *tu_group;
5978 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5979 struct type_unit_group);
5980 per_cu = &tu_group->per_cu;
5981 per_cu->objfile = objfile;
5983 if (dwarf2_per_objfile->using_index)
5985 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5986 struct dwarf2_per_cu_quick_data);
5990 unsigned int line_offset = to_underlying (line_offset_struct);
5991 struct partial_symtab *pst;
5994 /* Give the symtab a useful name for debug purposes. */
5995 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5996 name = xstrprintf ("<type_units_%d>",
5997 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5999 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6001 pst = create_partial_symtab (per_cu, name);
6007 tu_group->hash.dwo_unit = cu->dwo_unit;
6008 tu_group->hash.line_sect_off = line_offset_struct;
6013 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6014 STMT_LIST is a DW_AT_stmt_list attribute. */
6016 static struct type_unit_group *
6017 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6019 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6020 struct type_unit_group *tu_group;
6022 unsigned int line_offset;
6023 struct type_unit_group type_unit_group_for_lookup;
6025 if (dwarf2_per_objfile->type_unit_groups == NULL)
6027 dwarf2_per_objfile->type_unit_groups =
6028 allocate_type_unit_groups_table ();
6031 /* Do we need to create a new group, or can we use an existing one? */
6035 line_offset = DW_UNSND (stmt_list);
6036 ++tu_stats->nr_symtab_sharers;
6040 /* Ugh, no stmt_list. Rare, but we have to handle it.
6041 We can do various things here like create one group per TU or
6042 spread them over multiple groups to split up the expansion work.
6043 To avoid worst case scenarios (too many groups or too large groups)
6044 we, umm, group them in bunches. */
6045 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6046 | (tu_stats->nr_stmt_less_type_units
6047 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6048 ++tu_stats->nr_stmt_less_type_units;
6051 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6052 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6053 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6054 &type_unit_group_for_lookup, INSERT);
6057 tu_group = (struct type_unit_group *) *slot;
6058 gdb_assert (tu_group != NULL);
6062 sect_offset line_offset_struct = (sect_offset) line_offset;
6063 tu_group = create_type_unit_group (cu, line_offset_struct);
6065 ++tu_stats->nr_symtabs;
6071 /* Partial symbol tables. */
6073 /* Create a psymtab named NAME and assign it to PER_CU.
6075 The caller must fill in the following details:
6076 dirname, textlow, texthigh. */
6078 static struct partial_symtab *
6079 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6081 struct objfile *objfile = per_cu->objfile;
6082 struct partial_symtab *pst;
6084 pst = start_psymtab_common (objfile, name, 0,
6085 objfile->global_psymbols.next,
6086 objfile->static_psymbols.next);
6088 pst->psymtabs_addrmap_supported = 1;
6090 /* This is the glue that links PST into GDB's symbol API. */
6091 pst->read_symtab_private = per_cu;
6092 pst->read_symtab = dwarf2_read_symtab;
6093 per_cu->v.psymtab = pst;
6098 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6101 struct process_psymtab_comp_unit_data
6103 /* True if we are reading a DW_TAG_partial_unit. */
6105 int want_partial_unit;
6107 /* The "pretend" language that is used if the CU doesn't declare a
6110 enum language pretend_language;
6113 /* die_reader_func for process_psymtab_comp_unit. */
6116 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6117 const gdb_byte *info_ptr,
6118 struct die_info *comp_unit_die,
6122 struct dwarf2_cu *cu = reader->cu;
6123 struct objfile *objfile = cu->objfile;
6124 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6125 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6127 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6128 struct partial_symtab *pst;
6129 enum pc_bounds_kind cu_bounds_kind;
6130 const char *filename;
6131 struct process_psymtab_comp_unit_data *info
6132 = (struct process_psymtab_comp_unit_data *) data;
6134 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6137 gdb_assert (! per_cu->is_debug_types);
6139 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6141 cu->list_in_scope = &file_symbols;
6143 /* Allocate a new partial symbol table structure. */
6144 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6145 if (filename == NULL)
6148 pst = create_partial_symtab (per_cu, filename);
6150 /* This must be done before calling dwarf2_build_include_psymtabs. */
6151 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6153 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6155 dwarf2_find_base_address (comp_unit_die, cu);
6157 /* Possibly set the default values of LOWPC and HIGHPC from
6159 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6160 &best_highpc, cu, pst);
6161 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6162 /* Store the contiguous range if it is not empty; it can be empty for
6163 CUs with no code. */
6164 addrmap_set_empty (objfile->psymtabs_addrmap,
6165 gdbarch_adjust_dwarf2_addr (gdbarch,
6166 best_lowpc + baseaddr),
6167 gdbarch_adjust_dwarf2_addr (gdbarch,
6168 best_highpc + baseaddr) - 1,
6171 /* Check if comp unit has_children.
6172 If so, read the rest of the partial symbols from this comp unit.
6173 If not, there's no more debug_info for this comp unit. */
6176 struct partial_die_info *first_die;
6177 CORE_ADDR lowpc, highpc;
6179 lowpc = ((CORE_ADDR) -1);
6180 highpc = ((CORE_ADDR) 0);
6182 first_die = load_partial_dies (reader, info_ptr, 1);
6184 scan_partial_symbols (first_die, &lowpc, &highpc,
6185 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6187 /* If we didn't find a lowpc, set it to highpc to avoid
6188 complaints from `maint check'. */
6189 if (lowpc == ((CORE_ADDR) -1))
6192 /* If the compilation unit didn't have an explicit address range,
6193 then use the information extracted from its child dies. */
6194 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6197 best_highpc = highpc;
6200 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6201 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6203 end_psymtab_common (objfile, pst);
6205 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6208 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6209 struct dwarf2_per_cu_data *iter;
6211 /* Fill in 'dependencies' here; we fill in 'users' in a
6213 pst->number_of_dependencies = len;
6215 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6217 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6220 pst->dependencies[i] = iter->v.psymtab;
6222 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6225 /* Get the list of files included in the current compilation unit,
6226 and build a psymtab for each of them. */
6227 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6229 if (dwarf_read_debug)
6231 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6233 fprintf_unfiltered (gdb_stdlog,
6234 "Psymtab for %s unit @0x%x: %s - %s"
6235 ", %d global, %d static syms\n",
6236 per_cu->is_debug_types ? "type" : "comp",
6237 to_underlying (per_cu->sect_off),
6238 paddress (gdbarch, pst->textlow),
6239 paddress (gdbarch, pst->texthigh),
6240 pst->n_global_syms, pst->n_static_syms);
6244 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6245 Process compilation unit THIS_CU for a psymtab. */
6248 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6249 int want_partial_unit,
6250 enum language pretend_language)
6252 struct process_psymtab_comp_unit_data info;
6254 /* If this compilation unit was already read in, free the
6255 cached copy in order to read it in again. This is
6256 necessary because we skipped some symbols when we first
6257 read in the compilation unit (see load_partial_dies).
6258 This problem could be avoided, but the benefit is unclear. */
6259 if (this_cu->cu != NULL)
6260 free_one_cached_comp_unit (this_cu);
6262 gdb_assert (! this_cu->is_debug_types);
6263 info.want_partial_unit = want_partial_unit;
6264 info.pretend_language = pretend_language;
6265 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6266 process_psymtab_comp_unit_reader,
6269 /* Age out any secondary CUs. */
6270 age_cached_comp_units ();
6273 /* Reader function for build_type_psymtabs. */
6276 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6277 const gdb_byte *info_ptr,
6278 struct die_info *type_unit_die,
6282 struct objfile *objfile = dwarf2_per_objfile->objfile;
6283 struct dwarf2_cu *cu = reader->cu;
6284 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6285 struct signatured_type *sig_type;
6286 struct type_unit_group *tu_group;
6287 struct attribute *attr;
6288 struct partial_die_info *first_die;
6289 CORE_ADDR lowpc, highpc;
6290 struct partial_symtab *pst;
6292 gdb_assert (data == NULL);
6293 gdb_assert (per_cu->is_debug_types);
6294 sig_type = (struct signatured_type *) per_cu;
6299 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6300 tu_group = get_type_unit_group (cu, attr);
6302 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6304 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6305 cu->list_in_scope = &file_symbols;
6306 pst = create_partial_symtab (per_cu, "");
6309 first_die = load_partial_dies (reader, info_ptr, 1);
6311 lowpc = (CORE_ADDR) -1;
6312 highpc = (CORE_ADDR) 0;
6313 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6315 end_psymtab_common (objfile, pst);
6318 /* Struct used to sort TUs by their abbreviation table offset. */
6320 struct tu_abbrev_offset
6322 struct signatured_type *sig_type;
6323 sect_offset abbrev_offset;
6326 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6329 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6331 const struct tu_abbrev_offset * const *a
6332 = (const struct tu_abbrev_offset * const*) ap;
6333 const struct tu_abbrev_offset * const *b
6334 = (const struct tu_abbrev_offset * const*) bp;
6335 sect_offset aoff = (*a)->abbrev_offset;
6336 sect_offset boff = (*b)->abbrev_offset;
6338 return (aoff > boff) - (aoff < boff);
6341 /* Efficiently read all the type units.
6342 This does the bulk of the work for build_type_psymtabs.
6344 The efficiency is because we sort TUs by the abbrev table they use and
6345 only read each abbrev table once. In one program there are 200K TUs
6346 sharing 8K abbrev tables.
6348 The main purpose of this function is to support building the
6349 dwarf2_per_objfile->type_unit_groups table.
6350 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6351 can collapse the search space by grouping them by stmt_list.
6352 The savings can be significant, in the same program from above the 200K TUs
6353 share 8K stmt_list tables.
6355 FUNC is expected to call get_type_unit_group, which will create the
6356 struct type_unit_group if necessary and add it to
6357 dwarf2_per_objfile->type_unit_groups. */
6360 build_type_psymtabs_1 (void)
6362 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6363 struct cleanup *cleanups;
6364 struct abbrev_table *abbrev_table;
6365 sect_offset abbrev_offset;
6366 struct tu_abbrev_offset *sorted_by_abbrev;
6369 /* It's up to the caller to not call us multiple times. */
6370 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6372 if (dwarf2_per_objfile->n_type_units == 0)
6375 /* TUs typically share abbrev tables, and there can be way more TUs than
6376 abbrev tables. Sort by abbrev table to reduce the number of times we
6377 read each abbrev table in.
6378 Alternatives are to punt or to maintain a cache of abbrev tables.
6379 This is simpler and efficient enough for now.
6381 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6382 symtab to use). Typically TUs with the same abbrev offset have the same
6383 stmt_list value too so in practice this should work well.
6385 The basic algorithm here is:
6387 sort TUs by abbrev table
6388 for each TU with same abbrev table:
6389 read abbrev table if first user
6390 read TU top level DIE
6391 [IWBN if DWO skeletons had DW_AT_stmt_list]
6394 if (dwarf_read_debug)
6395 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6397 /* Sort in a separate table to maintain the order of all_type_units
6398 for .gdb_index: TU indices directly index all_type_units. */
6399 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6400 dwarf2_per_objfile->n_type_units);
6401 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6403 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6405 sorted_by_abbrev[i].sig_type = sig_type;
6406 sorted_by_abbrev[i].abbrev_offset =
6407 read_abbrev_offset (sig_type->per_cu.section,
6408 sig_type->per_cu.sect_off);
6410 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6411 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6412 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6414 abbrev_offset = (sect_offset) ~(unsigned) 0;
6415 abbrev_table = NULL;
6416 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6418 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6420 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6422 /* Switch to the next abbrev table if necessary. */
6423 if (abbrev_table == NULL
6424 || tu->abbrev_offset != abbrev_offset)
6426 if (abbrev_table != NULL)
6428 abbrev_table_free (abbrev_table);
6429 /* Reset to NULL in case abbrev_table_read_table throws
6430 an error: abbrev_table_free_cleanup will get called. */
6431 abbrev_table = NULL;
6433 abbrev_offset = tu->abbrev_offset;
6435 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6437 ++tu_stats->nr_uniq_abbrev_tables;
6440 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6441 build_type_psymtabs_reader, NULL);
6444 do_cleanups (cleanups);
6447 /* Print collected type unit statistics. */
6450 print_tu_stats (void)
6452 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6454 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6455 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6456 dwarf2_per_objfile->n_type_units);
6457 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6458 tu_stats->nr_uniq_abbrev_tables);
6459 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6460 tu_stats->nr_symtabs);
6461 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6462 tu_stats->nr_symtab_sharers);
6463 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6464 tu_stats->nr_stmt_less_type_units);
6465 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6466 tu_stats->nr_all_type_units_reallocs);
6469 /* Traversal function for build_type_psymtabs. */
6472 build_type_psymtab_dependencies (void **slot, void *info)
6474 struct objfile *objfile = dwarf2_per_objfile->objfile;
6475 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6476 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6477 struct partial_symtab *pst = per_cu->v.psymtab;
6478 int len = VEC_length (sig_type_ptr, tu_group->tus);
6479 struct signatured_type *iter;
6482 gdb_assert (len > 0);
6483 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6485 pst->number_of_dependencies = len;
6487 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6489 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6492 gdb_assert (iter->per_cu.is_debug_types);
6493 pst->dependencies[i] = iter->per_cu.v.psymtab;
6494 iter->type_unit_group = tu_group;
6497 VEC_free (sig_type_ptr, tu_group->tus);
6502 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6503 Build partial symbol tables for the .debug_types comp-units. */
6506 build_type_psymtabs (struct objfile *objfile)
6508 if (! create_all_type_units (objfile))
6511 build_type_psymtabs_1 ();
6514 /* Traversal function for process_skeletonless_type_unit.
6515 Read a TU in a DWO file and build partial symbols for it. */
6518 process_skeletonless_type_unit (void **slot, void *info)
6520 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6521 struct objfile *objfile = (struct objfile *) info;
6522 struct signatured_type find_entry, *entry;
6524 /* If this TU doesn't exist in the global table, add it and read it in. */
6526 if (dwarf2_per_objfile->signatured_types == NULL)
6528 dwarf2_per_objfile->signatured_types
6529 = allocate_signatured_type_table (objfile);
6532 find_entry.signature = dwo_unit->signature;
6533 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6535 /* If we've already seen this type there's nothing to do. What's happening
6536 is we're doing our own version of comdat-folding here. */
6540 /* This does the job that create_all_type_units would have done for
6542 entry = add_type_unit (dwo_unit->signature, slot);
6543 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6546 /* This does the job that build_type_psymtabs_1 would have done. */
6547 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6548 build_type_psymtabs_reader, NULL);
6553 /* Traversal function for process_skeletonless_type_units. */
6556 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6558 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6560 if (dwo_file->tus != NULL)
6562 htab_traverse_noresize (dwo_file->tus,
6563 process_skeletonless_type_unit, info);
6569 /* Scan all TUs of DWO files, verifying we've processed them.
6570 This is needed in case a TU was emitted without its skeleton.
6571 Note: This can't be done until we know what all the DWO files are. */
6574 process_skeletonless_type_units (struct objfile *objfile)
6576 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6577 if (get_dwp_file () == NULL
6578 && dwarf2_per_objfile->dwo_files != NULL)
6580 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6581 process_dwo_file_for_skeletonless_type_units,
6586 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6589 psymtabs_addrmap_cleanup (void *o)
6591 struct objfile *objfile = (struct objfile *) o;
6593 objfile->psymtabs_addrmap = NULL;
6596 /* Compute the 'user' field for each psymtab in OBJFILE. */
6599 set_partial_user (struct objfile *objfile)
6603 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6605 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6606 struct partial_symtab *pst = per_cu->v.psymtab;
6612 for (j = 0; j < pst->number_of_dependencies; ++j)
6614 /* Set the 'user' field only if it is not already set. */
6615 if (pst->dependencies[j]->user == NULL)
6616 pst->dependencies[j]->user = pst;
6621 /* Build the partial symbol table by doing a quick pass through the
6622 .debug_info and .debug_abbrev sections. */
6625 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6627 struct cleanup *back_to, *addrmap_cleanup;
6628 struct obstack temp_obstack;
6631 if (dwarf_read_debug)
6633 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6634 objfile_name (objfile));
6637 dwarf2_per_objfile->reading_partial_symbols = 1;
6639 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6641 /* Any cached compilation units will be linked by the per-objfile
6642 read_in_chain. Make sure to free them when we're done. */
6643 back_to = make_cleanup (free_cached_comp_units, NULL);
6645 build_type_psymtabs (objfile);
6647 create_all_comp_units (objfile);
6649 /* Create a temporary address map on a temporary obstack. We later
6650 copy this to the final obstack. */
6651 obstack_init (&temp_obstack);
6652 make_cleanup_obstack_free (&temp_obstack);
6653 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6654 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6656 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6658 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6660 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6663 /* This has to wait until we read the CUs, we need the list of DWOs. */
6664 process_skeletonless_type_units (objfile);
6666 /* Now that all TUs have been processed we can fill in the dependencies. */
6667 if (dwarf2_per_objfile->type_unit_groups != NULL)
6669 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6670 build_type_psymtab_dependencies, NULL);
6673 if (dwarf_read_debug)
6676 set_partial_user (objfile);
6678 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6679 &objfile->objfile_obstack);
6680 discard_cleanups (addrmap_cleanup);
6682 do_cleanups (back_to);
6684 if (dwarf_read_debug)
6685 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6686 objfile_name (objfile));
6689 /* die_reader_func for load_partial_comp_unit. */
6692 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6693 const gdb_byte *info_ptr,
6694 struct die_info *comp_unit_die,
6698 struct dwarf2_cu *cu = reader->cu;
6700 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6702 /* Check if comp unit has_children.
6703 If so, read the rest of the partial symbols from this comp unit.
6704 If not, there's no more debug_info for this comp unit. */
6706 load_partial_dies (reader, info_ptr, 0);
6709 /* Load the partial DIEs for a secondary CU into memory.
6710 This is also used when rereading a primary CU with load_all_dies. */
6713 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6715 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6716 load_partial_comp_unit_reader, NULL);
6720 read_comp_units_from_section (struct objfile *objfile,
6721 struct dwarf2_section_info *section,
6722 unsigned int is_dwz,
6725 struct dwarf2_per_cu_data ***all_comp_units)
6727 const gdb_byte *info_ptr;
6728 bfd *abfd = get_section_bfd_owner (section);
6730 if (dwarf_read_debug)
6731 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6732 get_section_name (section),
6733 get_section_file_name (section));
6735 dwarf2_read_section (objfile, section);
6737 info_ptr = section->buffer;
6739 while (info_ptr < section->buffer + section->size)
6741 unsigned int length, initial_length_size;
6742 struct dwarf2_per_cu_data *this_cu;
6744 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
6746 /* Read just enough information to find out where the next
6747 compilation unit is. */
6748 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6750 /* Save the compilation unit for later lookup. */
6751 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6752 memset (this_cu, 0, sizeof (*this_cu));
6753 this_cu->sect_off = sect_off;
6754 this_cu->length = length + initial_length_size;
6755 this_cu->is_dwz = is_dwz;
6756 this_cu->objfile = objfile;
6757 this_cu->section = section;
6759 if (*n_comp_units == *n_allocated)
6762 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6763 *all_comp_units, *n_allocated);
6765 (*all_comp_units)[*n_comp_units] = this_cu;
6768 info_ptr = info_ptr + this_cu->length;
6772 /* Create a list of all compilation units in OBJFILE.
6773 This is only done for -readnow and building partial symtabs. */
6776 create_all_comp_units (struct objfile *objfile)
6780 struct dwarf2_per_cu_data **all_comp_units;
6781 struct dwz_file *dwz;
6785 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6787 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6788 &n_allocated, &n_comp_units, &all_comp_units);
6790 dwz = dwarf2_get_dwz_file ();
6792 read_comp_units_from_section (objfile, &dwz->info, 1,
6793 &n_allocated, &n_comp_units,
6796 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6797 struct dwarf2_per_cu_data *,
6799 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6800 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6801 xfree (all_comp_units);
6802 dwarf2_per_objfile->n_comp_units = n_comp_units;
6805 /* Process all loaded DIEs for compilation unit CU, starting at
6806 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6807 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6808 DW_AT_ranges). See the comments of add_partial_subprogram on how
6809 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6812 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6813 CORE_ADDR *highpc, int set_addrmap,
6814 struct dwarf2_cu *cu)
6816 struct partial_die_info *pdi;
6818 /* Now, march along the PDI's, descending into ones which have
6819 interesting children but skipping the children of the other ones,
6820 until we reach the end of the compilation unit. */
6826 fixup_partial_die (pdi, cu);
6828 /* Anonymous namespaces or modules have no name but have interesting
6829 children, so we need to look at them. Ditto for anonymous
6832 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6833 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6834 || pdi->tag == DW_TAG_imported_unit)
6838 case DW_TAG_subprogram:
6839 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6841 case DW_TAG_constant:
6842 case DW_TAG_variable:
6843 case DW_TAG_typedef:
6844 case DW_TAG_union_type:
6845 if (!pdi->is_declaration)
6847 add_partial_symbol (pdi, cu);
6850 case DW_TAG_class_type:
6851 case DW_TAG_interface_type:
6852 case DW_TAG_structure_type:
6853 if (!pdi->is_declaration)
6855 add_partial_symbol (pdi, cu);
6857 if (cu->language == language_rust && pdi->has_children)
6858 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6861 case DW_TAG_enumeration_type:
6862 if (!pdi->is_declaration)
6863 add_partial_enumeration (pdi, cu);
6865 case DW_TAG_base_type:
6866 case DW_TAG_subrange_type:
6867 /* File scope base type definitions are added to the partial
6869 add_partial_symbol (pdi, cu);
6871 case DW_TAG_namespace:
6872 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6875 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6877 case DW_TAG_imported_unit:
6879 struct dwarf2_per_cu_data *per_cu;
6881 /* For now we don't handle imported units in type units. */
6882 if (cu->per_cu->is_debug_types)
6884 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6885 " supported in type units [in module %s]"),
6886 objfile_name (cu->objfile));
6889 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
6893 /* Go read the partial unit, if needed. */
6894 if (per_cu->v.psymtab == NULL)
6895 process_psymtab_comp_unit (per_cu, 1, cu->language);
6897 VEC_safe_push (dwarf2_per_cu_ptr,
6898 cu->per_cu->imported_symtabs, per_cu);
6901 case DW_TAG_imported_declaration:
6902 add_partial_symbol (pdi, cu);
6909 /* If the die has a sibling, skip to the sibling. */
6911 pdi = pdi->die_sibling;
6915 /* Functions used to compute the fully scoped name of a partial DIE.
6917 Normally, this is simple. For C++, the parent DIE's fully scoped
6918 name is concatenated with "::" and the partial DIE's name.
6919 Enumerators are an exception; they use the scope of their parent
6920 enumeration type, i.e. the name of the enumeration type is not
6921 prepended to the enumerator.
6923 There are two complexities. One is DW_AT_specification; in this
6924 case "parent" means the parent of the target of the specification,
6925 instead of the direct parent of the DIE. The other is compilers
6926 which do not emit DW_TAG_namespace; in this case we try to guess
6927 the fully qualified name of structure types from their members'
6928 linkage names. This must be done using the DIE's children rather
6929 than the children of any DW_AT_specification target. We only need
6930 to do this for structures at the top level, i.e. if the target of
6931 any DW_AT_specification (if any; otherwise the DIE itself) does not
6934 /* Compute the scope prefix associated with PDI's parent, in
6935 compilation unit CU. The result will be allocated on CU's
6936 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6937 field. NULL is returned if no prefix is necessary. */
6939 partial_die_parent_scope (struct partial_die_info *pdi,
6940 struct dwarf2_cu *cu)
6942 const char *grandparent_scope;
6943 struct partial_die_info *parent, *real_pdi;
6945 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6946 then this means the parent of the specification DIE. */
6949 while (real_pdi->has_specification)
6950 real_pdi = find_partial_die (real_pdi->spec_offset,
6951 real_pdi->spec_is_dwz, cu);
6953 parent = real_pdi->die_parent;
6957 if (parent->scope_set)
6958 return parent->scope;
6960 fixup_partial_die (parent, cu);
6962 grandparent_scope = partial_die_parent_scope (parent, cu);
6964 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6965 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6966 Work around this problem here. */
6967 if (cu->language == language_cplus
6968 && parent->tag == DW_TAG_namespace
6969 && strcmp (parent->name, "::") == 0
6970 && grandparent_scope == NULL)
6972 parent->scope = NULL;
6973 parent->scope_set = 1;
6977 if (pdi->tag == DW_TAG_enumerator)
6978 /* Enumerators should not get the name of the enumeration as a prefix. */
6979 parent->scope = grandparent_scope;
6980 else if (parent->tag == DW_TAG_namespace
6981 || parent->tag == DW_TAG_module
6982 || parent->tag == DW_TAG_structure_type
6983 || parent->tag == DW_TAG_class_type
6984 || parent->tag == DW_TAG_interface_type
6985 || parent->tag == DW_TAG_union_type
6986 || parent->tag == DW_TAG_enumeration_type)
6988 if (grandparent_scope == NULL)
6989 parent->scope = parent->name;
6991 parent->scope = typename_concat (&cu->comp_unit_obstack,
6993 parent->name, 0, cu);
6997 /* FIXME drow/2004-04-01: What should we be doing with
6998 function-local names? For partial symbols, we should probably be
7000 complaint (&symfile_complaints,
7001 _("unhandled containing DIE tag %d for DIE at %d"),
7002 parent->tag, to_underlying (pdi->sect_off));
7003 parent->scope = grandparent_scope;
7006 parent->scope_set = 1;
7007 return parent->scope;
7010 /* Return the fully scoped name associated with PDI, from compilation unit
7011 CU. The result will be allocated with malloc. */
7014 partial_die_full_name (struct partial_die_info *pdi,
7015 struct dwarf2_cu *cu)
7017 const char *parent_scope;
7019 /* If this is a template instantiation, we can not work out the
7020 template arguments from partial DIEs. So, unfortunately, we have
7021 to go through the full DIEs. At least any work we do building
7022 types here will be reused if full symbols are loaded later. */
7023 if (pdi->has_template_arguments)
7025 fixup_partial_die (pdi, cu);
7027 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7029 struct die_info *die;
7030 struct attribute attr;
7031 struct dwarf2_cu *ref_cu = cu;
7033 /* DW_FORM_ref_addr is using section offset. */
7034 attr.name = (enum dwarf_attribute) 0;
7035 attr.form = DW_FORM_ref_addr;
7036 attr.u.unsnd = to_underlying (pdi->sect_off);
7037 die = follow_die_ref (NULL, &attr, &ref_cu);
7039 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7043 parent_scope = partial_die_parent_scope (pdi, cu);
7044 if (parent_scope == NULL)
7047 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7051 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7053 struct objfile *objfile = cu->objfile;
7054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7056 const char *actual_name = NULL;
7058 char *built_actual_name;
7060 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7062 built_actual_name = partial_die_full_name (pdi, cu);
7063 if (built_actual_name != NULL)
7064 actual_name = built_actual_name;
7066 if (actual_name == NULL)
7067 actual_name = pdi->name;
7071 case DW_TAG_subprogram:
7072 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7073 if (pdi->is_external || cu->language == language_ada)
7075 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7076 of the global scope. But in Ada, we want to be able to access
7077 nested procedures globally. So all Ada subprograms are stored
7078 in the global scope. */
7079 add_psymbol_to_list (actual_name, strlen (actual_name),
7080 built_actual_name != NULL,
7081 VAR_DOMAIN, LOC_BLOCK,
7082 &objfile->global_psymbols,
7083 addr, cu->language, objfile);
7087 add_psymbol_to_list (actual_name, strlen (actual_name),
7088 built_actual_name != NULL,
7089 VAR_DOMAIN, LOC_BLOCK,
7090 &objfile->static_psymbols,
7091 addr, cu->language, objfile);
7094 if (pdi->main_subprogram && actual_name != NULL)
7095 set_objfile_main_name (objfile, actual_name, cu->language);
7097 case DW_TAG_constant:
7099 struct psymbol_allocation_list *list;
7101 if (pdi->is_external)
7102 list = &objfile->global_psymbols;
7104 list = &objfile->static_psymbols;
7105 add_psymbol_to_list (actual_name, strlen (actual_name),
7106 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7107 list, 0, cu->language, objfile);
7110 case DW_TAG_variable:
7112 addr = decode_locdesc (pdi->d.locdesc, cu);
7116 && !dwarf2_per_objfile->has_section_at_zero)
7118 /* A global or static variable may also have been stripped
7119 out by the linker if unused, in which case its address
7120 will be nullified; do not add such variables into partial
7121 symbol table then. */
7123 else if (pdi->is_external)
7126 Don't enter into the minimal symbol tables as there is
7127 a minimal symbol table entry from the ELF symbols already.
7128 Enter into partial symbol table if it has a location
7129 descriptor or a type.
7130 If the location descriptor is missing, new_symbol will create
7131 a LOC_UNRESOLVED symbol, the address of the variable will then
7132 be determined from the minimal symbol table whenever the variable
7134 The address for the partial symbol table entry is not
7135 used by GDB, but it comes in handy for debugging partial symbol
7138 if (pdi->d.locdesc || pdi->has_type)
7139 add_psymbol_to_list (actual_name, strlen (actual_name),
7140 built_actual_name != NULL,
7141 VAR_DOMAIN, LOC_STATIC,
7142 &objfile->global_psymbols,
7144 cu->language, objfile);
7148 int has_loc = pdi->d.locdesc != NULL;
7150 /* Static Variable. Skip symbols whose value we cannot know (those
7151 without location descriptors or constant values). */
7152 if (!has_loc && !pdi->has_const_value)
7154 xfree (built_actual_name);
7158 add_psymbol_to_list (actual_name, strlen (actual_name),
7159 built_actual_name != NULL,
7160 VAR_DOMAIN, LOC_STATIC,
7161 &objfile->static_psymbols,
7162 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7163 cu->language, objfile);
7166 case DW_TAG_typedef:
7167 case DW_TAG_base_type:
7168 case DW_TAG_subrange_type:
7169 add_psymbol_to_list (actual_name, strlen (actual_name),
7170 built_actual_name != NULL,
7171 VAR_DOMAIN, LOC_TYPEDEF,
7172 &objfile->static_psymbols,
7173 0, cu->language, objfile);
7175 case DW_TAG_imported_declaration:
7176 case DW_TAG_namespace:
7177 add_psymbol_to_list (actual_name, strlen (actual_name),
7178 built_actual_name != NULL,
7179 VAR_DOMAIN, LOC_TYPEDEF,
7180 &objfile->global_psymbols,
7181 0, cu->language, objfile);
7184 add_psymbol_to_list (actual_name, strlen (actual_name),
7185 built_actual_name != NULL,
7186 MODULE_DOMAIN, LOC_TYPEDEF,
7187 &objfile->global_psymbols,
7188 0, cu->language, objfile);
7190 case DW_TAG_class_type:
7191 case DW_TAG_interface_type:
7192 case DW_TAG_structure_type:
7193 case DW_TAG_union_type:
7194 case DW_TAG_enumeration_type:
7195 /* Skip external references. The DWARF standard says in the section
7196 about "Structure, Union, and Class Type Entries": "An incomplete
7197 structure, union or class type is represented by a structure,
7198 union or class entry that does not have a byte size attribute
7199 and that has a DW_AT_declaration attribute." */
7200 if (!pdi->has_byte_size && pdi->is_declaration)
7202 xfree (built_actual_name);
7206 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7207 static vs. global. */
7208 add_psymbol_to_list (actual_name, strlen (actual_name),
7209 built_actual_name != NULL,
7210 STRUCT_DOMAIN, LOC_TYPEDEF,
7211 cu->language == language_cplus
7212 ? &objfile->global_psymbols
7213 : &objfile->static_psymbols,
7214 0, cu->language, objfile);
7217 case DW_TAG_enumerator:
7218 add_psymbol_to_list (actual_name, strlen (actual_name),
7219 built_actual_name != NULL,
7220 VAR_DOMAIN, LOC_CONST,
7221 cu->language == language_cplus
7222 ? &objfile->global_psymbols
7223 : &objfile->static_psymbols,
7224 0, cu->language, objfile);
7230 xfree (built_actual_name);
7233 /* Read a partial die corresponding to a namespace; also, add a symbol
7234 corresponding to that namespace to the symbol table. NAMESPACE is
7235 the name of the enclosing namespace. */
7238 add_partial_namespace (struct partial_die_info *pdi,
7239 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7240 int set_addrmap, struct dwarf2_cu *cu)
7242 /* Add a symbol for the namespace. */
7244 add_partial_symbol (pdi, cu);
7246 /* Now scan partial symbols in that namespace. */
7248 if (pdi->has_children)
7249 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7252 /* Read a partial die corresponding to a Fortran module. */
7255 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7256 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7258 /* Add a symbol for the namespace. */
7260 add_partial_symbol (pdi, cu);
7262 /* Now scan partial symbols in that module. */
7264 if (pdi->has_children)
7265 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7268 /* Read a partial die corresponding to a subprogram and create a partial
7269 symbol for that subprogram. When the CU language allows it, this
7270 routine also defines a partial symbol for each nested subprogram
7271 that this subprogram contains. If SET_ADDRMAP is true, record the
7272 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7273 and highest PC values found in PDI.
7275 PDI may also be a lexical block, in which case we simply search
7276 recursively for subprograms defined inside that lexical block.
7277 Again, this is only performed when the CU language allows this
7278 type of definitions. */
7281 add_partial_subprogram (struct partial_die_info *pdi,
7282 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7283 int set_addrmap, struct dwarf2_cu *cu)
7285 if (pdi->tag == DW_TAG_subprogram)
7287 if (pdi->has_pc_info)
7289 if (pdi->lowpc < *lowpc)
7290 *lowpc = pdi->lowpc;
7291 if (pdi->highpc > *highpc)
7292 *highpc = pdi->highpc;
7295 struct objfile *objfile = cu->objfile;
7296 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7301 baseaddr = ANOFFSET (objfile->section_offsets,
7302 SECT_OFF_TEXT (objfile));
7303 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7304 pdi->lowpc + baseaddr);
7305 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7306 pdi->highpc + baseaddr);
7307 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7308 cu->per_cu->v.psymtab);
7312 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7314 if (!pdi->is_declaration)
7315 /* Ignore subprogram DIEs that do not have a name, they are
7316 illegal. Do not emit a complaint at this point, we will
7317 do so when we convert this psymtab into a symtab. */
7319 add_partial_symbol (pdi, cu);
7323 if (! pdi->has_children)
7326 if (cu->language == language_ada)
7328 pdi = pdi->die_child;
7331 fixup_partial_die (pdi, cu);
7332 if (pdi->tag == DW_TAG_subprogram
7333 || pdi->tag == DW_TAG_lexical_block)
7334 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7335 pdi = pdi->die_sibling;
7340 /* Read a partial die corresponding to an enumeration type. */
7343 add_partial_enumeration (struct partial_die_info *enum_pdi,
7344 struct dwarf2_cu *cu)
7346 struct partial_die_info *pdi;
7348 if (enum_pdi->name != NULL)
7349 add_partial_symbol (enum_pdi, cu);
7351 pdi = enum_pdi->die_child;
7354 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7355 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7357 add_partial_symbol (pdi, cu);
7358 pdi = pdi->die_sibling;
7362 /* Return the initial uleb128 in the die at INFO_PTR. */
7365 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7367 unsigned int bytes_read;
7369 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7372 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7373 Return the corresponding abbrev, or NULL if the number is zero (indicating
7374 an empty DIE). In either case *BYTES_READ will be set to the length of
7375 the initial number. */
7377 static struct abbrev_info *
7378 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7379 struct dwarf2_cu *cu)
7381 bfd *abfd = cu->objfile->obfd;
7382 unsigned int abbrev_number;
7383 struct abbrev_info *abbrev;
7385 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7387 if (abbrev_number == 0)
7390 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7393 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7394 " at offset 0x%x [in module %s]"),
7395 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7396 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
7402 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7403 Returns a pointer to the end of a series of DIEs, terminated by an empty
7404 DIE. Any children of the skipped DIEs will also be skipped. */
7406 static const gdb_byte *
7407 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7409 struct dwarf2_cu *cu = reader->cu;
7410 struct abbrev_info *abbrev;
7411 unsigned int bytes_read;
7415 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7417 return info_ptr + bytes_read;
7419 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7423 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7424 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7425 abbrev corresponding to that skipped uleb128 should be passed in
7426 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7429 static const gdb_byte *
7430 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7431 struct abbrev_info *abbrev)
7433 unsigned int bytes_read;
7434 struct attribute attr;
7435 bfd *abfd = reader->abfd;
7436 struct dwarf2_cu *cu = reader->cu;
7437 const gdb_byte *buffer = reader->buffer;
7438 const gdb_byte *buffer_end = reader->buffer_end;
7439 unsigned int form, i;
7441 for (i = 0; i < abbrev->num_attrs; i++)
7443 /* The only abbrev we care about is DW_AT_sibling. */
7444 if (abbrev->attrs[i].name == DW_AT_sibling)
7446 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7447 if (attr.form == DW_FORM_ref_addr)
7448 complaint (&symfile_complaints,
7449 _("ignoring absolute DW_AT_sibling"));
7452 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7453 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7455 if (sibling_ptr < info_ptr)
7456 complaint (&symfile_complaints,
7457 _("DW_AT_sibling points backwards"));
7458 else if (sibling_ptr > reader->buffer_end)
7459 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7465 /* If it isn't DW_AT_sibling, skip this attribute. */
7466 form = abbrev->attrs[i].form;
7470 case DW_FORM_ref_addr:
7471 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7472 and later it is offset sized. */
7473 if (cu->header.version == 2)
7474 info_ptr += cu->header.addr_size;
7476 info_ptr += cu->header.offset_size;
7478 case DW_FORM_GNU_ref_alt:
7479 info_ptr += cu->header.offset_size;
7482 info_ptr += cu->header.addr_size;
7489 case DW_FORM_flag_present:
7490 case DW_FORM_implicit_const:
7502 case DW_FORM_ref_sig8:
7505 case DW_FORM_data16:
7508 case DW_FORM_string:
7509 read_direct_string (abfd, info_ptr, &bytes_read);
7510 info_ptr += bytes_read;
7512 case DW_FORM_sec_offset:
7514 case DW_FORM_GNU_strp_alt:
7515 info_ptr += cu->header.offset_size;
7517 case DW_FORM_exprloc:
7519 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7520 info_ptr += bytes_read;
7522 case DW_FORM_block1:
7523 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7525 case DW_FORM_block2:
7526 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7528 case DW_FORM_block4:
7529 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7533 case DW_FORM_ref_udata:
7534 case DW_FORM_GNU_addr_index:
7535 case DW_FORM_GNU_str_index:
7536 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7538 case DW_FORM_indirect:
7539 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7540 info_ptr += bytes_read;
7541 /* We need to continue parsing from here, so just go back to
7543 goto skip_attribute;
7546 error (_("Dwarf Error: Cannot handle %s "
7547 "in DWARF reader [in module %s]"),
7548 dwarf_form_name (form),
7549 bfd_get_filename (abfd));
7553 if (abbrev->has_children)
7554 return skip_children (reader, info_ptr);
7559 /* Locate ORIG_PDI's sibling.
7560 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7562 static const gdb_byte *
7563 locate_pdi_sibling (const struct die_reader_specs *reader,
7564 struct partial_die_info *orig_pdi,
7565 const gdb_byte *info_ptr)
7567 /* Do we know the sibling already? */
7569 if (orig_pdi->sibling)
7570 return orig_pdi->sibling;
7572 /* Are there any children to deal with? */
7574 if (!orig_pdi->has_children)
7577 /* Skip the children the long way. */
7579 return skip_children (reader, info_ptr);
7582 /* Expand this partial symbol table into a full symbol table. SELF is
7586 dwarf2_read_symtab (struct partial_symtab *self,
7587 struct objfile *objfile)
7591 warning (_("bug: psymtab for %s is already read in."),
7598 printf_filtered (_("Reading in symbols for %s..."),
7600 gdb_flush (gdb_stdout);
7603 /* Restore our global data. */
7605 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7606 dwarf2_objfile_data_key);
7608 /* If this psymtab is constructed from a debug-only objfile, the
7609 has_section_at_zero flag will not necessarily be correct. We
7610 can get the correct value for this flag by looking at the data
7611 associated with the (presumably stripped) associated objfile. */
7612 if (objfile->separate_debug_objfile_backlink)
7614 struct dwarf2_per_objfile *dpo_backlink
7615 = ((struct dwarf2_per_objfile *)
7616 objfile_data (objfile->separate_debug_objfile_backlink,
7617 dwarf2_objfile_data_key));
7619 dwarf2_per_objfile->has_section_at_zero
7620 = dpo_backlink->has_section_at_zero;
7623 dwarf2_per_objfile->reading_partial_symbols = 0;
7625 psymtab_to_symtab_1 (self);
7627 /* Finish up the debug error message. */
7629 printf_filtered (_("done.\n"));
7632 process_cu_includes ();
7635 /* Reading in full CUs. */
7637 /* Add PER_CU to the queue. */
7640 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7641 enum language pretend_language)
7643 struct dwarf2_queue_item *item;
7646 item = XNEW (struct dwarf2_queue_item);
7647 item->per_cu = per_cu;
7648 item->pretend_language = pretend_language;
7651 if (dwarf2_queue == NULL)
7652 dwarf2_queue = item;
7654 dwarf2_queue_tail->next = item;
7656 dwarf2_queue_tail = item;
7659 /* If PER_CU is not yet queued, add it to the queue.
7660 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7662 The result is non-zero if PER_CU was queued, otherwise the result is zero
7663 meaning either PER_CU is already queued or it is already loaded.
7665 N.B. There is an invariant here that if a CU is queued then it is loaded.
7666 The caller is required to load PER_CU if we return non-zero. */
7669 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7670 struct dwarf2_per_cu_data *per_cu,
7671 enum language pretend_language)
7673 /* We may arrive here during partial symbol reading, if we need full
7674 DIEs to process an unusual case (e.g. template arguments). Do
7675 not queue PER_CU, just tell our caller to load its DIEs. */
7676 if (dwarf2_per_objfile->reading_partial_symbols)
7678 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7683 /* Mark the dependence relation so that we don't flush PER_CU
7685 if (dependent_cu != NULL)
7686 dwarf2_add_dependence (dependent_cu, per_cu);
7688 /* If it's already on the queue, we have nothing to do. */
7692 /* If the compilation unit is already loaded, just mark it as
7694 if (per_cu->cu != NULL)
7696 per_cu->cu->last_used = 0;
7700 /* Add it to the queue. */
7701 queue_comp_unit (per_cu, pretend_language);
7706 /* Process the queue. */
7709 process_queue (void)
7711 struct dwarf2_queue_item *item, *next_item;
7713 if (dwarf_read_debug)
7715 fprintf_unfiltered (gdb_stdlog,
7716 "Expanding one or more symtabs of objfile %s ...\n",
7717 objfile_name (dwarf2_per_objfile->objfile));
7720 /* The queue starts out with one item, but following a DIE reference
7721 may load a new CU, adding it to the end of the queue. */
7722 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7724 if ((dwarf2_per_objfile->using_index
7725 ? !item->per_cu->v.quick->compunit_symtab
7726 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7727 /* Skip dummy CUs. */
7728 && item->per_cu->cu != NULL)
7730 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7731 unsigned int debug_print_threshold;
7734 if (per_cu->is_debug_types)
7736 struct signatured_type *sig_type =
7737 (struct signatured_type *) per_cu;
7739 sprintf (buf, "TU %s at offset 0x%x",
7740 hex_string (sig_type->signature),
7741 to_underlying (per_cu->sect_off));
7742 /* There can be 100s of TUs.
7743 Only print them in verbose mode. */
7744 debug_print_threshold = 2;
7748 sprintf (buf, "CU at offset 0x%x",
7749 to_underlying (per_cu->sect_off));
7750 debug_print_threshold = 1;
7753 if (dwarf_read_debug >= debug_print_threshold)
7754 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7756 if (per_cu->is_debug_types)
7757 process_full_type_unit (per_cu, item->pretend_language);
7759 process_full_comp_unit (per_cu, item->pretend_language);
7761 if (dwarf_read_debug >= debug_print_threshold)
7762 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7765 item->per_cu->queued = 0;
7766 next_item = item->next;
7770 dwarf2_queue_tail = NULL;
7772 if (dwarf_read_debug)
7774 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7775 objfile_name (dwarf2_per_objfile->objfile));
7779 /* Free all allocated queue entries. This function only releases anything if
7780 an error was thrown; if the queue was processed then it would have been
7781 freed as we went along. */
7784 dwarf2_release_queue (void *dummy)
7786 struct dwarf2_queue_item *item, *last;
7788 item = dwarf2_queue;
7791 /* Anything still marked queued is likely to be in an
7792 inconsistent state, so discard it. */
7793 if (item->per_cu->queued)
7795 if (item->per_cu->cu != NULL)
7796 free_one_cached_comp_unit (item->per_cu);
7797 item->per_cu->queued = 0;
7805 dwarf2_queue = dwarf2_queue_tail = NULL;
7808 /* Read in full symbols for PST, and anything it depends on. */
7811 psymtab_to_symtab_1 (struct partial_symtab *pst)
7813 struct dwarf2_per_cu_data *per_cu;
7819 for (i = 0; i < pst->number_of_dependencies; i++)
7820 if (!pst->dependencies[i]->readin
7821 && pst->dependencies[i]->user == NULL)
7823 /* Inform about additional files that need to be read in. */
7826 /* FIXME: i18n: Need to make this a single string. */
7827 fputs_filtered (" ", gdb_stdout);
7829 fputs_filtered ("and ", gdb_stdout);
7831 printf_filtered ("%s...", pst->dependencies[i]->filename);
7832 wrap_here (""); /* Flush output. */
7833 gdb_flush (gdb_stdout);
7835 psymtab_to_symtab_1 (pst->dependencies[i]);
7838 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7842 /* It's an include file, no symbols to read for it.
7843 Everything is in the parent symtab. */
7848 dw2_do_instantiate_symtab (per_cu);
7851 /* Trivial hash function for die_info: the hash value of a DIE
7852 is its offset in .debug_info for this objfile. */
7855 die_hash (const void *item)
7857 const struct die_info *die = (const struct die_info *) item;
7859 return to_underlying (die->sect_off);
7862 /* Trivial comparison function for die_info structures: two DIEs
7863 are equal if they have the same offset. */
7866 die_eq (const void *item_lhs, const void *item_rhs)
7868 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7869 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7871 return die_lhs->sect_off == die_rhs->sect_off;
7874 /* die_reader_func for load_full_comp_unit.
7875 This is identical to read_signatured_type_reader,
7876 but is kept separate for now. */
7879 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7880 const gdb_byte *info_ptr,
7881 struct die_info *comp_unit_die,
7885 struct dwarf2_cu *cu = reader->cu;
7886 enum language *language_ptr = (enum language *) data;
7888 gdb_assert (cu->die_hash == NULL);
7890 htab_create_alloc_ex (cu->header.length / 12,
7894 &cu->comp_unit_obstack,
7895 hashtab_obstack_allocate,
7896 dummy_obstack_deallocate);
7899 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7900 &info_ptr, comp_unit_die);
7901 cu->dies = comp_unit_die;
7902 /* comp_unit_die is not stored in die_hash, no need. */
7904 /* We try not to read any attributes in this function, because not
7905 all CUs needed for references have been loaded yet, and symbol
7906 table processing isn't initialized. But we have to set the CU language,
7907 or we won't be able to build types correctly.
7908 Similarly, if we do not read the producer, we can not apply
7909 producer-specific interpretation. */
7910 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7913 /* Load the DIEs associated with PER_CU into memory. */
7916 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7917 enum language pretend_language)
7919 gdb_assert (! this_cu->is_debug_types);
7921 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7922 load_full_comp_unit_reader, &pretend_language);
7925 /* Add a DIE to the delayed physname list. */
7928 add_to_method_list (struct type *type, int fnfield_index, int index,
7929 const char *name, struct die_info *die,
7930 struct dwarf2_cu *cu)
7932 struct delayed_method_info mi;
7934 mi.fnfield_index = fnfield_index;
7938 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7941 /* A cleanup for freeing the delayed method list. */
7944 free_delayed_list (void *ptr)
7946 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7947 if (cu->method_list != NULL)
7949 VEC_free (delayed_method_info, cu->method_list);
7950 cu->method_list = NULL;
7954 /* Compute the physnames of any methods on the CU's method list.
7956 The computation of method physnames is delayed in order to avoid the
7957 (bad) condition that one of the method's formal parameters is of an as yet
7961 compute_delayed_physnames (struct dwarf2_cu *cu)
7964 struct delayed_method_info *mi;
7965 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7967 const char *physname;
7968 struct fn_fieldlist *fn_flp
7969 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7970 physname = dwarf2_physname (mi->name, mi->die, cu);
7971 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7972 = physname ? physname : "";
7976 /* Go objects should be embedded in a DW_TAG_module DIE,
7977 and it's not clear if/how imported objects will appear.
7978 To keep Go support simple until that's worked out,
7979 go back through what we've read and create something usable.
7980 We could do this while processing each DIE, and feels kinda cleaner,
7981 but that way is more invasive.
7982 This is to, for example, allow the user to type "p var" or "b main"
7983 without having to specify the package name, and allow lookups
7984 of module.object to work in contexts that use the expression
7988 fixup_go_packaging (struct dwarf2_cu *cu)
7990 char *package_name = NULL;
7991 struct pending *list;
7994 for (list = global_symbols; list != NULL; list = list->next)
7996 for (i = 0; i < list->nsyms; ++i)
7998 struct symbol *sym = list->symbol[i];
8000 if (SYMBOL_LANGUAGE (sym) == language_go
8001 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8003 char *this_package_name = go_symbol_package_name (sym);
8005 if (this_package_name == NULL)
8007 if (package_name == NULL)
8008 package_name = this_package_name;
8011 if (strcmp (package_name, this_package_name) != 0)
8012 complaint (&symfile_complaints,
8013 _("Symtab %s has objects from two different Go packages: %s and %s"),
8014 (symbol_symtab (sym) != NULL
8015 ? symtab_to_filename_for_display
8016 (symbol_symtab (sym))
8017 : objfile_name (cu->objfile)),
8018 this_package_name, package_name);
8019 xfree (this_package_name);
8025 if (package_name != NULL)
8027 struct objfile *objfile = cu->objfile;
8028 const char *saved_package_name
8029 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8031 strlen (package_name));
8032 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8033 saved_package_name);
8036 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8038 sym = allocate_symbol (objfile);
8039 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8040 SYMBOL_SET_NAMES (sym, saved_package_name,
8041 strlen (saved_package_name), 0, objfile);
8042 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8043 e.g., "main" finds the "main" module and not C's main(). */
8044 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8046 SYMBOL_TYPE (sym) = type;
8048 add_symbol_to_list (sym, &global_symbols);
8050 xfree (package_name);
8054 /* Return the symtab for PER_CU. This works properly regardless of
8055 whether we're using the index or psymtabs. */
8057 static struct compunit_symtab *
8058 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8060 return (dwarf2_per_objfile->using_index
8061 ? per_cu->v.quick->compunit_symtab
8062 : per_cu->v.psymtab->compunit_symtab);
8065 /* A helper function for computing the list of all symbol tables
8066 included by PER_CU. */
8069 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8070 htab_t all_children, htab_t all_type_symtabs,
8071 struct dwarf2_per_cu_data *per_cu,
8072 struct compunit_symtab *immediate_parent)
8076 struct compunit_symtab *cust;
8077 struct dwarf2_per_cu_data *iter;
8079 slot = htab_find_slot (all_children, per_cu, INSERT);
8082 /* This inclusion and its children have been processed. */
8087 /* Only add a CU if it has a symbol table. */
8088 cust = get_compunit_symtab (per_cu);
8091 /* If this is a type unit only add its symbol table if we haven't
8092 seen it yet (type unit per_cu's can share symtabs). */
8093 if (per_cu->is_debug_types)
8095 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8099 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8100 if (cust->user == NULL)
8101 cust->user = immediate_parent;
8106 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8107 if (cust->user == NULL)
8108 cust->user = immediate_parent;
8113 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8116 recursively_compute_inclusions (result, all_children,
8117 all_type_symtabs, iter, cust);
8121 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8125 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8127 gdb_assert (! per_cu->is_debug_types);
8129 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8132 struct dwarf2_per_cu_data *per_cu_iter;
8133 struct compunit_symtab *compunit_symtab_iter;
8134 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8135 htab_t all_children, all_type_symtabs;
8136 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8138 /* If we don't have a symtab, we can just skip this case. */
8142 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8143 NULL, xcalloc, xfree);
8144 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8145 NULL, xcalloc, xfree);
8148 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8152 recursively_compute_inclusions (&result_symtabs, all_children,
8153 all_type_symtabs, per_cu_iter,
8157 /* Now we have a transitive closure of all the included symtabs. */
8158 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8160 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8161 struct compunit_symtab *, len + 1);
8163 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8164 compunit_symtab_iter);
8166 cust->includes[ix] = compunit_symtab_iter;
8167 cust->includes[len] = NULL;
8169 VEC_free (compunit_symtab_ptr, result_symtabs);
8170 htab_delete (all_children);
8171 htab_delete (all_type_symtabs);
8175 /* Compute the 'includes' field for the symtabs of all the CUs we just
8179 process_cu_includes (void)
8182 struct dwarf2_per_cu_data *iter;
8185 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8189 if (! iter->is_debug_types)
8190 compute_compunit_symtab_includes (iter);
8193 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8196 /* Generate full symbol information for PER_CU, whose DIEs have
8197 already been loaded into memory. */
8200 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8201 enum language pretend_language)
8203 struct dwarf2_cu *cu = per_cu->cu;
8204 struct objfile *objfile = per_cu->objfile;
8205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8206 CORE_ADDR lowpc, highpc;
8207 struct compunit_symtab *cust;
8208 struct cleanup *back_to, *delayed_list_cleanup;
8210 struct block *static_block;
8213 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8216 back_to = make_cleanup (really_free_pendings, NULL);
8217 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8219 cu->list_in_scope = &file_symbols;
8221 cu->language = pretend_language;
8222 cu->language_defn = language_def (cu->language);
8224 /* Do line number decoding in read_file_scope () */
8225 process_die (cu->dies, cu);
8227 /* For now fudge the Go package. */
8228 if (cu->language == language_go)
8229 fixup_go_packaging (cu);
8231 /* Now that we have processed all the DIEs in the CU, all the types
8232 should be complete, and it should now be safe to compute all of the
8234 compute_delayed_physnames (cu);
8235 do_cleanups (delayed_list_cleanup);
8237 /* Some compilers don't define a DW_AT_high_pc attribute for the
8238 compilation unit. If the DW_AT_high_pc is missing, synthesize
8239 it, by scanning the DIE's below the compilation unit. */
8240 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8242 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8243 static_block = end_symtab_get_static_block (addr, 0, 1);
8245 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8246 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8247 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8248 addrmap to help ensure it has an accurate map of pc values belonging to
8250 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8252 cust = end_symtab_from_static_block (static_block,
8253 SECT_OFF_TEXT (objfile), 0);
8257 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8259 /* Set symtab language to language from DW_AT_language. If the
8260 compilation is from a C file generated by language preprocessors, do
8261 not set the language if it was already deduced by start_subfile. */
8262 if (!(cu->language == language_c
8263 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8264 COMPUNIT_FILETABS (cust)->language = cu->language;
8266 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8267 produce DW_AT_location with location lists but it can be possibly
8268 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8269 there were bugs in prologue debug info, fixed later in GCC-4.5
8270 by "unwind info for epilogues" patch (which is not directly related).
8272 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8273 needed, it would be wrong due to missing DW_AT_producer there.
8275 Still one can confuse GDB by using non-standard GCC compilation
8276 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8278 if (cu->has_loclist && gcc_4_minor >= 5)
8279 cust->locations_valid = 1;
8281 if (gcc_4_minor >= 5)
8282 cust->epilogue_unwind_valid = 1;
8284 cust->call_site_htab = cu->call_site_htab;
8287 if (dwarf2_per_objfile->using_index)
8288 per_cu->v.quick->compunit_symtab = cust;
8291 struct partial_symtab *pst = per_cu->v.psymtab;
8292 pst->compunit_symtab = cust;
8296 /* Push it for inclusion processing later. */
8297 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8299 do_cleanups (back_to);
8302 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8303 already been loaded into memory. */
8306 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8307 enum language pretend_language)
8309 struct dwarf2_cu *cu = per_cu->cu;
8310 struct objfile *objfile = per_cu->objfile;
8311 struct compunit_symtab *cust;
8312 struct cleanup *back_to, *delayed_list_cleanup;
8313 struct signatured_type *sig_type;
8315 gdb_assert (per_cu->is_debug_types);
8316 sig_type = (struct signatured_type *) per_cu;
8319 back_to = make_cleanup (really_free_pendings, NULL);
8320 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8322 cu->list_in_scope = &file_symbols;
8324 cu->language = pretend_language;
8325 cu->language_defn = language_def (cu->language);
8327 /* The symbol tables are set up in read_type_unit_scope. */
8328 process_die (cu->dies, cu);
8330 /* For now fudge the Go package. */
8331 if (cu->language == language_go)
8332 fixup_go_packaging (cu);
8334 /* Now that we have processed all the DIEs in the CU, all the types
8335 should be complete, and it should now be safe to compute all of the
8337 compute_delayed_physnames (cu);
8338 do_cleanups (delayed_list_cleanup);
8340 /* TUs share symbol tables.
8341 If this is the first TU to use this symtab, complete the construction
8342 of it with end_expandable_symtab. Otherwise, complete the addition of
8343 this TU's symbols to the existing symtab. */
8344 if (sig_type->type_unit_group->compunit_symtab == NULL)
8346 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8347 sig_type->type_unit_group->compunit_symtab = cust;
8351 /* Set symtab language to language from DW_AT_language. If the
8352 compilation is from a C file generated by language preprocessors,
8353 do not set the language if it was already deduced by
8355 if (!(cu->language == language_c
8356 && COMPUNIT_FILETABS (cust)->language != language_c))
8357 COMPUNIT_FILETABS (cust)->language = cu->language;
8362 augment_type_symtab ();
8363 cust = sig_type->type_unit_group->compunit_symtab;
8366 if (dwarf2_per_objfile->using_index)
8367 per_cu->v.quick->compunit_symtab = cust;
8370 struct partial_symtab *pst = per_cu->v.psymtab;
8371 pst->compunit_symtab = cust;
8375 do_cleanups (back_to);
8378 /* Process an imported unit DIE. */
8381 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8383 struct attribute *attr;
8385 /* For now we don't handle imported units in type units. */
8386 if (cu->per_cu->is_debug_types)
8388 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8389 " supported in type units [in module %s]"),
8390 objfile_name (cu->objfile));
8393 attr = dwarf2_attr (die, DW_AT_import, cu);
8396 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8397 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8398 dwarf2_per_cu_data *per_cu
8399 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
8401 /* If necessary, add it to the queue and load its DIEs. */
8402 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8403 load_full_comp_unit (per_cu, cu->language);
8405 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8410 /* Reset the in_process bit of a die. */
8413 reset_die_in_process (void *arg)
8415 struct die_info *die = (struct die_info *) arg;
8417 die->in_process = 0;
8420 /* Process a die and its children. */
8423 process_die (struct die_info *die, struct dwarf2_cu *cu)
8425 struct cleanup *in_process;
8427 /* We should only be processing those not already in process. */
8428 gdb_assert (!die->in_process);
8430 die->in_process = 1;
8431 in_process = make_cleanup (reset_die_in_process,die);
8435 case DW_TAG_padding:
8437 case DW_TAG_compile_unit:
8438 case DW_TAG_partial_unit:
8439 read_file_scope (die, cu);
8441 case DW_TAG_type_unit:
8442 read_type_unit_scope (die, cu);
8444 case DW_TAG_subprogram:
8445 case DW_TAG_inlined_subroutine:
8446 read_func_scope (die, cu);
8448 case DW_TAG_lexical_block:
8449 case DW_TAG_try_block:
8450 case DW_TAG_catch_block:
8451 read_lexical_block_scope (die, cu);
8453 case DW_TAG_call_site:
8454 case DW_TAG_GNU_call_site:
8455 read_call_site_scope (die, cu);
8457 case DW_TAG_class_type:
8458 case DW_TAG_interface_type:
8459 case DW_TAG_structure_type:
8460 case DW_TAG_union_type:
8461 process_structure_scope (die, cu);
8463 case DW_TAG_enumeration_type:
8464 process_enumeration_scope (die, cu);
8467 /* These dies have a type, but processing them does not create
8468 a symbol or recurse to process the children. Therefore we can
8469 read them on-demand through read_type_die. */
8470 case DW_TAG_subroutine_type:
8471 case DW_TAG_set_type:
8472 case DW_TAG_array_type:
8473 case DW_TAG_pointer_type:
8474 case DW_TAG_ptr_to_member_type:
8475 case DW_TAG_reference_type:
8476 case DW_TAG_rvalue_reference_type:
8477 case DW_TAG_string_type:
8480 case DW_TAG_base_type:
8481 case DW_TAG_subrange_type:
8482 case DW_TAG_typedef:
8483 /* Add a typedef symbol for the type definition, if it has a
8485 new_symbol (die, read_type_die (die, cu), cu);
8487 case DW_TAG_common_block:
8488 read_common_block (die, cu);
8490 case DW_TAG_common_inclusion:
8492 case DW_TAG_namespace:
8493 cu->processing_has_namespace_info = 1;
8494 read_namespace (die, cu);
8497 cu->processing_has_namespace_info = 1;
8498 read_module (die, cu);
8500 case DW_TAG_imported_declaration:
8501 cu->processing_has_namespace_info = 1;
8502 if (read_namespace_alias (die, cu))
8504 /* The declaration is not a global namespace alias: fall through. */
8505 case DW_TAG_imported_module:
8506 cu->processing_has_namespace_info = 1;
8507 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8508 || cu->language != language_fortran))
8509 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8510 dwarf_tag_name (die->tag));
8511 read_import_statement (die, cu);
8514 case DW_TAG_imported_unit:
8515 process_imported_unit_die (die, cu);
8519 new_symbol (die, NULL, cu);
8523 do_cleanups (in_process);
8526 /* DWARF name computation. */
8528 /* A helper function for dwarf2_compute_name which determines whether DIE
8529 needs to have the name of the scope prepended to the name listed in the
8533 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8535 struct attribute *attr;
8539 case DW_TAG_namespace:
8540 case DW_TAG_typedef:
8541 case DW_TAG_class_type:
8542 case DW_TAG_interface_type:
8543 case DW_TAG_structure_type:
8544 case DW_TAG_union_type:
8545 case DW_TAG_enumeration_type:
8546 case DW_TAG_enumerator:
8547 case DW_TAG_subprogram:
8548 case DW_TAG_inlined_subroutine:
8550 case DW_TAG_imported_declaration:
8553 case DW_TAG_variable:
8554 case DW_TAG_constant:
8555 /* We only need to prefix "globally" visible variables. These include
8556 any variable marked with DW_AT_external or any variable that
8557 lives in a namespace. [Variables in anonymous namespaces
8558 require prefixing, but they are not DW_AT_external.] */
8560 if (dwarf2_attr (die, DW_AT_specification, cu))
8562 struct dwarf2_cu *spec_cu = cu;
8564 return die_needs_namespace (die_specification (die, &spec_cu),
8568 attr = dwarf2_attr (die, DW_AT_external, cu);
8569 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8570 && die->parent->tag != DW_TAG_module)
8572 /* A variable in a lexical block of some kind does not need a
8573 namespace, even though in C++ such variables may be external
8574 and have a mangled name. */
8575 if (die->parent->tag == DW_TAG_lexical_block
8576 || die->parent->tag == DW_TAG_try_block
8577 || die->parent->tag == DW_TAG_catch_block
8578 || die->parent->tag == DW_TAG_subprogram)
8587 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8588 compute the physname for the object, which include a method's:
8589 - formal parameters (C++),
8590 - receiver type (Go),
8592 The term "physname" is a bit confusing.
8593 For C++, for example, it is the demangled name.
8594 For Go, for example, it's the mangled name.
8596 For Ada, return the DIE's linkage name rather than the fully qualified
8597 name. PHYSNAME is ignored..
8599 The result is allocated on the objfile_obstack and canonicalized. */
8602 dwarf2_compute_name (const char *name,
8603 struct die_info *die, struct dwarf2_cu *cu,
8606 struct objfile *objfile = cu->objfile;
8609 name = dwarf2_name (die, cu);
8611 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8612 but otherwise compute it by typename_concat inside GDB.
8613 FIXME: Actually this is not really true, or at least not always true.
8614 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8615 Fortran names because there is no mangling standard. So new_symbol_full
8616 will set the demangled name to the result of dwarf2_full_name, and it is
8617 the demangled name that GDB uses if it exists. */
8618 if (cu->language == language_ada
8619 || (cu->language == language_fortran && physname))
8621 /* For Ada unit, we prefer the linkage name over the name, as
8622 the former contains the exported name, which the user expects
8623 to be able to reference. Ideally, we want the user to be able
8624 to reference this entity using either natural or linkage name,
8625 but we haven't started looking at this enhancement yet. */
8626 const char *linkage_name;
8628 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8629 if (linkage_name == NULL)
8630 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8631 if (linkage_name != NULL)
8632 return linkage_name;
8635 /* These are the only languages we know how to qualify names in. */
8637 && (cu->language == language_cplus
8638 || cu->language == language_fortran || cu->language == language_d
8639 || cu->language == language_rust))
8641 if (die_needs_namespace (die, cu))
8645 const char *canonical_name = NULL;
8649 prefix = determine_prefix (die, cu);
8650 if (*prefix != '\0')
8652 char *prefixed_name = typename_concat (NULL, prefix, name,
8655 buf.puts (prefixed_name);
8656 xfree (prefixed_name);
8661 /* Template parameters may be specified in the DIE's DW_AT_name, or
8662 as children with DW_TAG_template_type_param or
8663 DW_TAG_value_type_param. If the latter, add them to the name
8664 here. If the name already has template parameters, then
8665 skip this step; some versions of GCC emit both, and
8666 it is more efficient to use the pre-computed name.
8668 Something to keep in mind about this process: it is very
8669 unlikely, or in some cases downright impossible, to produce
8670 something that will match the mangled name of a function.
8671 If the definition of the function has the same debug info,
8672 we should be able to match up with it anyway. But fallbacks
8673 using the minimal symbol, for instance to find a method
8674 implemented in a stripped copy of libstdc++, will not work.
8675 If we do not have debug info for the definition, we will have to
8676 match them up some other way.
8678 When we do name matching there is a related problem with function
8679 templates; two instantiated function templates are allowed to
8680 differ only by their return types, which we do not add here. */
8682 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8684 struct attribute *attr;
8685 struct die_info *child;
8688 die->building_fullname = 1;
8690 for (child = die->child; child != NULL; child = child->sibling)
8694 const gdb_byte *bytes;
8695 struct dwarf2_locexpr_baton *baton;
8698 if (child->tag != DW_TAG_template_type_param
8699 && child->tag != DW_TAG_template_value_param)
8710 attr = dwarf2_attr (child, DW_AT_type, cu);
8713 complaint (&symfile_complaints,
8714 _("template parameter missing DW_AT_type"));
8715 buf.puts ("UNKNOWN_TYPE");
8718 type = die_type (child, cu);
8720 if (child->tag == DW_TAG_template_type_param)
8722 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8726 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8729 complaint (&symfile_complaints,
8730 _("template parameter missing "
8731 "DW_AT_const_value"));
8732 buf.puts ("UNKNOWN_VALUE");
8736 dwarf2_const_value_attr (attr, type, name,
8737 &cu->comp_unit_obstack, cu,
8738 &value, &bytes, &baton);
8740 if (TYPE_NOSIGN (type))
8741 /* GDB prints characters as NUMBER 'CHAR'. If that's
8742 changed, this can use value_print instead. */
8743 c_printchar (value, type, &buf);
8746 struct value_print_options opts;
8749 v = dwarf2_evaluate_loc_desc (type, NULL,
8753 else if (bytes != NULL)
8755 v = allocate_value (type);
8756 memcpy (value_contents_writeable (v), bytes,
8757 TYPE_LENGTH (type));
8760 v = value_from_longest (type, value);
8762 /* Specify decimal so that we do not depend on
8764 get_formatted_print_options (&opts, 'd');
8766 value_print (v, &buf, &opts);
8772 die->building_fullname = 0;
8776 /* Close the argument list, with a space if necessary
8777 (nested templates). */
8778 if (!buf.empty () && buf.string ().back () == '>')
8785 /* For C++ methods, append formal parameter type
8786 information, if PHYSNAME. */
8788 if (physname && die->tag == DW_TAG_subprogram
8789 && cu->language == language_cplus)
8791 struct type *type = read_type_die (die, cu);
8793 c_type_print_args (type, &buf, 1, cu->language,
8794 &type_print_raw_options);
8796 if (cu->language == language_cplus)
8798 /* Assume that an artificial first parameter is
8799 "this", but do not crash if it is not. RealView
8800 marks unnamed (and thus unused) parameters as
8801 artificial; there is no way to differentiate
8803 if (TYPE_NFIELDS (type) > 0
8804 && TYPE_FIELD_ARTIFICIAL (type, 0)
8805 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8806 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8808 buf.puts (" const");
8812 const std::string &intermediate_name = buf.string ();
8814 if (cu->language == language_cplus)
8816 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8817 &objfile->per_bfd->storage_obstack);
8819 /* If we only computed INTERMEDIATE_NAME, or if
8820 INTERMEDIATE_NAME is already canonical, then we need to
8821 copy it to the appropriate obstack. */
8822 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8823 name = ((const char *)
8824 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8825 intermediate_name.c_str (),
8826 intermediate_name.length ()));
8828 name = canonical_name;
8835 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8836 If scope qualifiers are appropriate they will be added. The result
8837 will be allocated on the storage_obstack, or NULL if the DIE does
8838 not have a name. NAME may either be from a previous call to
8839 dwarf2_name or NULL.
8841 The output string will be canonicalized (if C++). */
8844 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8846 return dwarf2_compute_name (name, die, cu, 0);
8849 /* Construct a physname for the given DIE in CU. NAME may either be
8850 from a previous call to dwarf2_name or NULL. The result will be
8851 allocated on the objfile_objstack or NULL if the DIE does not have a
8854 The output string will be canonicalized (if C++). */
8857 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8859 struct objfile *objfile = cu->objfile;
8860 const char *retval, *mangled = NULL, *canon = NULL;
8861 struct cleanup *back_to;
8864 /* In this case dwarf2_compute_name is just a shortcut not building anything
8866 if (!die_needs_namespace (die, cu))
8867 return dwarf2_compute_name (name, die, cu, 1);
8869 back_to = make_cleanup (null_cleanup, NULL);
8871 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8872 if (mangled == NULL)
8873 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8875 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8876 See https://github.com/rust-lang/rust/issues/32925. */
8877 if (cu->language == language_rust && mangled != NULL
8878 && strchr (mangled, '{') != NULL)
8881 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8883 if (mangled != NULL)
8887 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8888 type. It is easier for GDB users to search for such functions as
8889 `name(params)' than `long name(params)'. In such case the minimal
8890 symbol names do not match the full symbol names but for template
8891 functions there is never a need to look up their definition from their
8892 declaration so the only disadvantage remains the minimal symbol
8893 variant `long name(params)' does not have the proper inferior type.
8896 if (cu->language == language_go)
8898 /* This is a lie, but we already lie to the caller new_symbol_full.
8899 new_symbol_full assumes we return the mangled name.
8900 This just undoes that lie until things are cleaned up. */
8905 demangled = gdb_demangle (mangled,
8906 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8910 make_cleanup (xfree, demangled);
8920 if (canon == NULL || check_physname)
8922 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8924 if (canon != NULL && strcmp (physname, canon) != 0)
8926 /* It may not mean a bug in GDB. The compiler could also
8927 compute DW_AT_linkage_name incorrectly. But in such case
8928 GDB would need to be bug-to-bug compatible. */
8930 complaint (&symfile_complaints,
8931 _("Computed physname <%s> does not match demangled <%s> "
8932 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8933 physname, canon, mangled, to_underlying (die->sect_off),
8934 objfile_name (objfile));
8936 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8937 is available here - over computed PHYSNAME. It is safer
8938 against both buggy GDB and buggy compilers. */
8952 retval = ((const char *)
8953 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8954 retval, strlen (retval)));
8956 do_cleanups (back_to);
8960 /* Inspect DIE in CU for a namespace alias. If one exists, record
8961 a new symbol for it.
8963 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8966 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8968 struct attribute *attr;
8970 /* If the die does not have a name, this is not a namespace
8972 attr = dwarf2_attr (die, DW_AT_name, cu);
8976 struct die_info *d = die;
8977 struct dwarf2_cu *imported_cu = cu;
8979 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8980 keep inspecting DIEs until we hit the underlying import. */
8981 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8982 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8984 attr = dwarf2_attr (d, DW_AT_import, cu);
8988 d = follow_die_ref (d, attr, &imported_cu);
8989 if (d->tag != DW_TAG_imported_declaration)
8993 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8995 complaint (&symfile_complaints,
8996 _("DIE at 0x%x has too many recursively imported "
8997 "declarations"), to_underlying (d->sect_off));
9004 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9006 type = get_die_type_at_offset (sect_off, cu->per_cu);
9007 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9009 /* This declaration is a global namespace alias. Add
9010 a symbol for it whose type is the aliased namespace. */
9011 new_symbol (die, type, cu);
9020 /* Return the using directives repository (global or local?) to use in the
9021 current context for LANGUAGE.
9023 For Ada, imported declarations can materialize renamings, which *may* be
9024 global. However it is impossible (for now?) in DWARF to distinguish
9025 "external" imported declarations and "static" ones. As all imported
9026 declarations seem to be static in all other languages, make them all CU-wide
9027 global only in Ada. */
9029 static struct using_direct **
9030 using_directives (enum language language)
9032 if (language == language_ada && context_stack_depth == 0)
9033 return &global_using_directives;
9035 return &local_using_directives;
9038 /* Read the import statement specified by the given die and record it. */
9041 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9043 struct objfile *objfile = cu->objfile;
9044 struct attribute *import_attr;
9045 struct die_info *imported_die, *child_die;
9046 struct dwarf2_cu *imported_cu;
9047 const char *imported_name;
9048 const char *imported_name_prefix;
9049 const char *canonical_name;
9050 const char *import_alias;
9051 const char *imported_declaration = NULL;
9052 const char *import_prefix;
9053 VEC (const_char_ptr) *excludes = NULL;
9054 struct cleanup *cleanups;
9056 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9057 if (import_attr == NULL)
9059 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9060 dwarf_tag_name (die->tag));
9065 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9066 imported_name = dwarf2_name (imported_die, imported_cu);
9067 if (imported_name == NULL)
9069 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9071 The import in the following code:
9085 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9086 <52> DW_AT_decl_file : 1
9087 <53> DW_AT_decl_line : 6
9088 <54> DW_AT_import : <0x75>
9089 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9091 <5b> DW_AT_decl_file : 1
9092 <5c> DW_AT_decl_line : 2
9093 <5d> DW_AT_type : <0x6e>
9095 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9096 <76> DW_AT_byte_size : 4
9097 <77> DW_AT_encoding : 5 (signed)
9099 imports the wrong die ( 0x75 instead of 0x58 ).
9100 This case will be ignored until the gcc bug is fixed. */
9104 /* Figure out the local name after import. */
9105 import_alias = dwarf2_name (die, cu);
9107 /* Figure out where the statement is being imported to. */
9108 import_prefix = determine_prefix (die, cu);
9110 /* Figure out what the scope of the imported die is and prepend it
9111 to the name of the imported die. */
9112 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9114 if (imported_die->tag != DW_TAG_namespace
9115 && imported_die->tag != DW_TAG_module)
9117 imported_declaration = imported_name;
9118 canonical_name = imported_name_prefix;
9120 else if (strlen (imported_name_prefix) > 0)
9121 canonical_name = obconcat (&objfile->objfile_obstack,
9122 imported_name_prefix,
9123 (cu->language == language_d ? "." : "::"),
9124 imported_name, (char *) NULL);
9126 canonical_name = imported_name;
9128 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9130 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9131 for (child_die = die->child; child_die && child_die->tag;
9132 child_die = sibling_die (child_die))
9134 /* DWARF-4: A Fortran use statement with a “rename list” may be
9135 represented by an imported module entry with an import attribute
9136 referring to the module and owned entries corresponding to those
9137 entities that are renamed as part of being imported. */
9139 if (child_die->tag != DW_TAG_imported_declaration)
9141 complaint (&symfile_complaints,
9142 _("child DW_TAG_imported_declaration expected "
9143 "- DIE at 0x%x [in module %s]"),
9144 to_underlying (child_die->sect_off), objfile_name (objfile));
9148 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9149 if (import_attr == NULL)
9151 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9152 dwarf_tag_name (child_die->tag));
9157 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9159 imported_name = dwarf2_name (imported_die, imported_cu);
9160 if (imported_name == NULL)
9162 complaint (&symfile_complaints,
9163 _("child DW_TAG_imported_declaration has unknown "
9164 "imported name - DIE at 0x%x [in module %s]"),
9165 to_underlying (child_die->sect_off), objfile_name (objfile));
9169 VEC_safe_push (const_char_ptr, excludes, imported_name);
9171 process_die (child_die, cu);
9174 add_using_directive (using_directives (cu->language),
9178 imported_declaration,
9181 &objfile->objfile_obstack);
9183 do_cleanups (cleanups);
9186 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9187 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9188 this, it was first present in GCC release 4.3.0. */
9191 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9193 if (!cu->checked_producer)
9194 check_producer (cu);
9196 return cu->producer_is_gcc_lt_4_3;
9199 static file_and_directory
9200 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9202 file_and_directory res;
9204 /* Find the filename. Do not use dwarf2_name here, since the filename
9205 is not a source language identifier. */
9206 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9207 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9209 if (res.comp_dir == NULL
9210 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9211 && IS_ABSOLUTE_PATH (res.name))
9213 res.comp_dir_storage = ldirname (res.name);
9214 if (!res.comp_dir_storage.empty ())
9215 res.comp_dir = res.comp_dir_storage.c_str ();
9217 if (res.comp_dir != NULL)
9219 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9220 directory, get rid of it. */
9221 const char *cp = strchr (res.comp_dir, ':');
9223 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9224 res.comp_dir = cp + 1;
9227 if (res.name == NULL)
9228 res.name = "<unknown>";
9233 /* Handle DW_AT_stmt_list for a compilation unit.
9234 DIE is the DW_TAG_compile_unit die for CU.
9235 COMP_DIR is the compilation directory. LOWPC is passed to
9236 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9239 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9240 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9242 struct objfile *objfile = dwarf2_per_objfile->objfile;
9243 struct attribute *attr;
9244 struct line_header line_header_local;
9245 hashval_t line_header_local_hash;
9250 gdb_assert (! cu->per_cu->is_debug_types);
9252 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9256 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9258 /* The line header hash table is only created if needed (it exists to
9259 prevent redundant reading of the line table for partial_units).
9260 If we're given a partial_unit, we'll need it. If we're given a
9261 compile_unit, then use the line header hash table if it's already
9262 created, but don't create one just yet. */
9264 if (dwarf2_per_objfile->line_header_hash == NULL
9265 && die->tag == DW_TAG_partial_unit)
9267 dwarf2_per_objfile->line_header_hash
9268 = htab_create_alloc_ex (127, line_header_hash_voidp,
9269 line_header_eq_voidp,
9270 free_line_header_voidp,
9271 &objfile->objfile_obstack,
9272 hashtab_obstack_allocate,
9273 dummy_obstack_deallocate);
9276 line_header_local.sect_off = line_offset;
9277 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9278 line_header_local_hash = line_header_hash (&line_header_local);
9279 if (dwarf2_per_objfile->line_header_hash != NULL)
9281 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9283 line_header_local_hash, NO_INSERT);
9285 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9286 is not present in *SLOT (since if there is something in *SLOT then
9287 it will be for a partial_unit). */
9288 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9290 gdb_assert (*slot != NULL);
9291 cu->line_header = (struct line_header *) *slot;
9296 /* dwarf_decode_line_header does not yet provide sufficient information.
9297 We always have to call also dwarf_decode_lines for it. */
9298 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9301 cu->line_header = lh.get ();
9303 if (dwarf2_per_objfile->line_header_hash == NULL)
9307 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9309 line_header_local_hash, INSERT);
9310 gdb_assert (slot != NULL);
9312 if (slot != NULL && *slot == NULL)
9314 /* This newly decoded line number information unit will be owned
9315 by line_header_hash hash table. */
9316 *slot = cu->line_header;
9320 /* We cannot free any current entry in (*slot) as that struct line_header
9321 may be already used by multiple CUs. Create only temporary decoded
9322 line_header for this CU - it may happen at most once for each line
9323 number information unit. And if we're not using line_header_hash
9324 then this is what we want as well. */
9325 gdb_assert (die->tag != DW_TAG_partial_unit);
9327 decode_mapping = (die->tag != DW_TAG_partial_unit);
9328 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9334 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9337 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9339 struct objfile *objfile = dwarf2_per_objfile->objfile;
9340 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9341 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9342 CORE_ADDR highpc = ((CORE_ADDR) 0);
9343 struct attribute *attr;
9344 struct die_info *child_die;
9347 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9349 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9351 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9352 from finish_block. */
9353 if (lowpc == ((CORE_ADDR) -1))
9355 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9357 file_and_directory fnd = find_file_and_directory (die, cu);
9359 prepare_one_comp_unit (cu, die, cu->language);
9361 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9362 standardised yet. As a workaround for the language detection we fall
9363 back to the DW_AT_producer string. */
9364 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9365 cu->language = language_opencl;
9367 /* Similar hack for Go. */
9368 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9369 set_cu_language (DW_LANG_Go, cu);
9371 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
9373 /* Decode line number information if present. We do this before
9374 processing child DIEs, so that the line header table is available
9375 for DW_AT_decl_file. */
9376 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
9378 /* Process all dies in compilation unit. */
9379 if (die->child != NULL)
9381 child_die = die->child;
9382 while (child_die && child_die->tag)
9384 process_die (child_die, cu);
9385 child_die = sibling_die (child_die);
9389 /* Decode macro information, if present. Dwarf 2 macro information
9390 refers to information in the line number info statement program
9391 header, so we can only read it if we've read the header
9393 attr = dwarf2_attr (die, DW_AT_macros, cu);
9395 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9396 if (attr && cu->line_header)
9398 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9399 complaint (&symfile_complaints,
9400 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9402 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9406 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9407 if (attr && cu->line_header)
9409 unsigned int macro_offset = DW_UNSND (attr);
9411 dwarf_decode_macros (cu, macro_offset, 0);
9416 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9417 Create the set of symtabs used by this TU, or if this TU is sharing
9418 symtabs with another TU and the symtabs have already been created
9419 then restore those symtabs in the line header.
9420 We don't need the pc/line-number mapping for type units. */
9423 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9425 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9426 struct type_unit_group *tu_group;
9428 struct attribute *attr;
9430 struct signatured_type *sig_type;
9432 gdb_assert (per_cu->is_debug_types);
9433 sig_type = (struct signatured_type *) per_cu;
9435 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9437 /* If we're using .gdb_index (includes -readnow) then
9438 per_cu->type_unit_group may not have been set up yet. */
9439 if (sig_type->type_unit_group == NULL)
9440 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9441 tu_group = sig_type->type_unit_group;
9443 /* If we've already processed this stmt_list there's no real need to
9444 do it again, we could fake it and just recreate the part we need
9445 (file name,index -> symtab mapping). If data shows this optimization
9446 is useful we can do it then. */
9447 first_time = tu_group->compunit_symtab == NULL;
9449 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9454 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9455 lh = dwarf_decode_line_header (line_offset, cu);
9460 dwarf2_start_symtab (cu, "", NULL, 0);
9463 gdb_assert (tu_group->symtabs == NULL);
9464 restart_symtab (tu_group->compunit_symtab, "", 0);
9469 cu->line_header = lh.get ();
9473 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9475 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9476 still initializing it, and our caller (a few levels up)
9477 process_full_type_unit still needs to know if this is the first
9480 tu_group->num_symtabs = lh->file_names.size ();
9481 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
9483 for (i = 0; i < lh->file_names.size (); ++i)
9485 file_entry &fe = lh->file_names[i];
9487 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
9489 if (current_subfile->symtab == NULL)
9491 /* NOTE: start_subfile will recognize when it's been passed
9492 a file it has already seen. So we can't assume there's a
9493 simple mapping from lh->file_names to subfiles, plus
9494 lh->file_names may contain dups. */
9495 current_subfile->symtab
9496 = allocate_symtab (cust, current_subfile->name);
9499 fe.symtab = current_subfile->symtab;
9500 tu_group->symtabs[i] = fe.symtab;
9505 restart_symtab (tu_group->compunit_symtab, "", 0);
9507 for (i = 0; i < lh->file_names.size (); ++i)
9509 struct file_entry *fe = &lh->file_names[i];
9511 fe->symtab = tu_group->symtabs[i];
9517 /* The main symtab is allocated last. Type units don't have DW_AT_name
9518 so they don't have a "real" (so to speak) symtab anyway.
9519 There is later code that will assign the main symtab to all symbols
9520 that don't have one. We need to handle the case of a symbol with a
9521 missing symtab (DW_AT_decl_file) anyway. */
9524 /* Process DW_TAG_type_unit.
9525 For TUs we want to skip the first top level sibling if it's not the
9526 actual type being defined by this TU. In this case the first top
9527 level sibling is there to provide context only. */
9530 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9532 struct die_info *child_die;
9534 prepare_one_comp_unit (cu, die, language_minimal);
9536 /* Initialize (or reinitialize) the machinery for building symtabs.
9537 We do this before processing child DIEs, so that the line header table
9538 is available for DW_AT_decl_file. */
9539 setup_type_unit_groups (die, cu);
9541 if (die->child != NULL)
9543 child_die = die->child;
9544 while (child_die && child_die->tag)
9546 process_die (child_die, cu);
9547 child_die = sibling_die (child_die);
9554 http://gcc.gnu.org/wiki/DebugFission
9555 http://gcc.gnu.org/wiki/DebugFissionDWP
9557 To simplify handling of both DWO files ("object" files with the DWARF info)
9558 and DWP files (a file with the DWOs packaged up into one file), we treat
9559 DWP files as having a collection of virtual DWO files. */
9562 hash_dwo_file (const void *item)
9564 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9567 hash = htab_hash_string (dwo_file->dwo_name);
9568 if (dwo_file->comp_dir != NULL)
9569 hash += htab_hash_string (dwo_file->comp_dir);
9574 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9576 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9577 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9579 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9581 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9582 return lhs->comp_dir == rhs->comp_dir;
9583 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9586 /* Allocate a hash table for DWO files. */
9589 allocate_dwo_file_hash_table (void)
9591 struct objfile *objfile = dwarf2_per_objfile->objfile;
9593 return htab_create_alloc_ex (41,
9597 &objfile->objfile_obstack,
9598 hashtab_obstack_allocate,
9599 dummy_obstack_deallocate);
9602 /* Lookup DWO file DWO_NAME. */
9605 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9607 struct dwo_file find_entry;
9610 if (dwarf2_per_objfile->dwo_files == NULL)
9611 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9613 memset (&find_entry, 0, sizeof (find_entry));
9614 find_entry.dwo_name = dwo_name;
9615 find_entry.comp_dir = comp_dir;
9616 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9622 hash_dwo_unit (const void *item)
9624 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9626 /* This drops the top 32 bits of the id, but is ok for a hash. */
9627 return dwo_unit->signature;
9631 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9633 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9634 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9636 /* The signature is assumed to be unique within the DWO file.
9637 So while object file CU dwo_id's always have the value zero,
9638 that's OK, assuming each object file DWO file has only one CU,
9639 and that's the rule for now. */
9640 return lhs->signature == rhs->signature;
9643 /* Allocate a hash table for DWO CUs,TUs.
9644 There is one of these tables for each of CUs,TUs for each DWO file. */
9647 allocate_dwo_unit_table (struct objfile *objfile)
9649 /* Start out with a pretty small number.
9650 Generally DWO files contain only one CU and maybe some TUs. */
9651 return htab_create_alloc_ex (3,
9655 &objfile->objfile_obstack,
9656 hashtab_obstack_allocate,
9657 dummy_obstack_deallocate);
9660 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9662 struct create_dwo_cu_data
9664 struct dwo_file *dwo_file;
9665 struct dwo_unit dwo_unit;
9668 /* die_reader_func for create_dwo_cu. */
9671 create_dwo_cu_reader (const struct die_reader_specs *reader,
9672 const gdb_byte *info_ptr,
9673 struct die_info *comp_unit_die,
9677 struct dwarf2_cu *cu = reader->cu;
9678 sect_offset sect_off = cu->per_cu->sect_off;
9679 struct dwarf2_section_info *section = cu->per_cu->section;
9680 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9681 struct dwo_file *dwo_file = data->dwo_file;
9682 struct dwo_unit *dwo_unit = &data->dwo_unit;
9683 struct attribute *attr;
9685 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9688 complaint (&symfile_complaints,
9689 _("Dwarf Error: debug entry at offset 0x%x is missing"
9690 " its dwo_id [in module %s]"),
9691 to_underlying (sect_off), dwo_file->dwo_name);
9695 dwo_unit->dwo_file = dwo_file;
9696 dwo_unit->signature = DW_UNSND (attr);
9697 dwo_unit->section = section;
9698 dwo_unit->sect_off = sect_off;
9699 dwo_unit->length = cu->per_cu->length;
9701 if (dwarf_read_debug)
9702 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9703 to_underlying (sect_off),
9704 hex_string (dwo_unit->signature));
9707 /* Create the dwo_unit for the lone CU in DWO_FILE.
9708 Note: This function processes DWO files only, not DWP files. */
9710 static struct dwo_unit *
9711 create_dwo_cu (struct dwo_file *dwo_file)
9713 struct objfile *objfile = dwarf2_per_objfile->objfile;
9714 struct dwarf2_section_info *section = &dwo_file->sections.info;
9715 const gdb_byte *info_ptr, *end_ptr;
9716 struct create_dwo_cu_data create_dwo_cu_data;
9717 struct dwo_unit *dwo_unit;
9719 dwarf2_read_section (objfile, section);
9720 info_ptr = section->buffer;
9722 if (info_ptr == NULL)
9725 if (dwarf_read_debug)
9727 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9728 get_section_name (section),
9729 get_section_file_name (section));
9732 create_dwo_cu_data.dwo_file = dwo_file;
9735 end_ptr = info_ptr + section->size;
9736 while (info_ptr < end_ptr)
9738 struct dwarf2_per_cu_data per_cu;
9740 memset (&create_dwo_cu_data.dwo_unit, 0,
9741 sizeof (create_dwo_cu_data.dwo_unit));
9742 memset (&per_cu, 0, sizeof (per_cu));
9743 per_cu.objfile = objfile;
9744 per_cu.is_debug_types = 0;
9745 per_cu.sect_off = sect_offset (info_ptr - section->buffer);
9746 per_cu.section = section;
9748 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9749 create_dwo_cu_reader,
9750 &create_dwo_cu_data);
9752 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9754 /* If we've already found one, complain. We only support one
9755 because having more than one requires hacking the dwo_name of
9756 each to match, which is highly unlikely to happen. */
9757 if (dwo_unit != NULL)
9759 complaint (&symfile_complaints,
9760 _("Multiple CUs in DWO file %s [in module %s]"),
9761 dwo_file->dwo_name, objfile_name (objfile));
9765 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9766 *dwo_unit = create_dwo_cu_data.dwo_unit;
9769 info_ptr += per_cu.length;
9775 /* DWP file .debug_{cu,tu}_index section format:
9776 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9780 Both index sections have the same format, and serve to map a 64-bit
9781 signature to a set of section numbers. Each section begins with a header,
9782 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9783 indexes, and a pool of 32-bit section numbers. The index sections will be
9784 aligned at 8-byte boundaries in the file.
9786 The index section header consists of:
9788 V, 32 bit version number
9790 N, 32 bit number of compilation units or type units in the index
9791 M, 32 bit number of slots in the hash table
9793 Numbers are recorded using the byte order of the application binary.
9795 The hash table begins at offset 16 in the section, and consists of an array
9796 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9797 order of the application binary). Unused slots in the hash table are 0.
9798 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9800 The parallel table begins immediately after the hash table
9801 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9802 array of 32-bit indexes (using the byte order of the application binary),
9803 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9804 table contains a 32-bit index into the pool of section numbers. For unused
9805 hash table slots, the corresponding entry in the parallel table will be 0.
9807 The pool of section numbers begins immediately following the hash table
9808 (at offset 16 + 12 * M from the beginning of the section). The pool of
9809 section numbers consists of an array of 32-bit words (using the byte order
9810 of the application binary). Each item in the array is indexed starting
9811 from 0. The hash table entry provides the index of the first section
9812 number in the set. Additional section numbers in the set follow, and the
9813 set is terminated by a 0 entry (section number 0 is not used in ELF).
9815 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9816 section must be the first entry in the set, and the .debug_abbrev.dwo must
9817 be the second entry. Other members of the set may follow in any order.
9823 DWP Version 2 combines all the .debug_info, etc. sections into one,
9824 and the entries in the index tables are now offsets into these sections.
9825 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9828 Index Section Contents:
9830 Hash Table of Signatures dwp_hash_table.hash_table
9831 Parallel Table of Indices dwp_hash_table.unit_table
9832 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9833 Table of Section Sizes dwp_hash_table.v2.sizes
9835 The index section header consists of:
9837 V, 32 bit version number
9838 L, 32 bit number of columns in the table of section offsets
9839 N, 32 bit number of compilation units or type units in the index
9840 M, 32 bit number of slots in the hash table
9842 Numbers are recorded using the byte order of the application binary.
9844 The hash table has the same format as version 1.
9845 The parallel table of indices has the same format as version 1,
9846 except that the entries are origin-1 indices into the table of sections
9847 offsets and the table of section sizes.
9849 The table of offsets begins immediately following the parallel table
9850 (at offset 16 + 12 * M from the beginning of the section). The table is
9851 a two-dimensional array of 32-bit words (using the byte order of the
9852 application binary), with L columns and N+1 rows, in row-major order.
9853 Each row in the array is indexed starting from 0. The first row provides
9854 a key to the remaining rows: each column in this row provides an identifier
9855 for a debug section, and the offsets in the same column of subsequent rows
9856 refer to that section. The section identifiers are:
9858 DW_SECT_INFO 1 .debug_info.dwo
9859 DW_SECT_TYPES 2 .debug_types.dwo
9860 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9861 DW_SECT_LINE 4 .debug_line.dwo
9862 DW_SECT_LOC 5 .debug_loc.dwo
9863 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9864 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9865 DW_SECT_MACRO 8 .debug_macro.dwo
9867 The offsets provided by the CU and TU index sections are the base offsets
9868 for the contributions made by each CU or TU to the corresponding section
9869 in the package file. Each CU and TU header contains an abbrev_offset
9870 field, used to find the abbreviations table for that CU or TU within the
9871 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9872 be interpreted as relative to the base offset given in the index section.
9873 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9874 should be interpreted as relative to the base offset for .debug_line.dwo,
9875 and offsets into other debug sections obtained from DWARF attributes should
9876 also be interpreted as relative to the corresponding base offset.
9878 The table of sizes begins immediately following the table of offsets.
9879 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9880 with L columns and N rows, in row-major order. Each row in the array is
9881 indexed starting from 1 (row 0 is shared by the two tables).
9885 Hash table lookup is handled the same in version 1 and 2:
9887 We assume that N and M will not exceed 2^32 - 1.
9888 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9890 Given a 64-bit compilation unit signature or a type signature S, an entry
9891 in the hash table is located as follows:
9893 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9894 the low-order k bits all set to 1.
9896 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9898 3) If the hash table entry at index H matches the signature, use that
9899 entry. If the hash table entry at index H is unused (all zeroes),
9900 terminate the search: the signature is not present in the table.
9902 4) Let H = (H + H') modulo M. Repeat at Step 3.
9904 Because M > N and H' and M are relatively prime, the search is guaranteed
9905 to stop at an unused slot or find the match. */
9907 /* Create a hash table to map DWO IDs to their CU/TU entry in
9908 .debug_{info,types}.dwo in DWP_FILE.
9909 Returns NULL if there isn't one.
9910 Note: This function processes DWP files only, not DWO files. */
9912 static struct dwp_hash_table *
9913 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9915 struct objfile *objfile = dwarf2_per_objfile->objfile;
9916 bfd *dbfd = dwp_file->dbfd;
9917 const gdb_byte *index_ptr, *index_end;
9918 struct dwarf2_section_info *index;
9919 uint32_t version, nr_columns, nr_units, nr_slots;
9920 struct dwp_hash_table *htab;
9923 index = &dwp_file->sections.tu_index;
9925 index = &dwp_file->sections.cu_index;
9927 if (dwarf2_section_empty_p (index))
9929 dwarf2_read_section (objfile, index);
9931 index_ptr = index->buffer;
9932 index_end = index_ptr + index->size;
9934 version = read_4_bytes (dbfd, index_ptr);
9937 nr_columns = read_4_bytes (dbfd, index_ptr);
9941 nr_units = read_4_bytes (dbfd, index_ptr);
9943 nr_slots = read_4_bytes (dbfd, index_ptr);
9946 if (version != 1 && version != 2)
9948 error (_("Dwarf Error: unsupported DWP file version (%s)"
9950 pulongest (version), dwp_file->name);
9952 if (nr_slots != (nr_slots & -nr_slots))
9954 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9955 " is not power of 2 [in module %s]"),
9956 pulongest (nr_slots), dwp_file->name);
9959 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9960 htab->version = version;
9961 htab->nr_columns = nr_columns;
9962 htab->nr_units = nr_units;
9963 htab->nr_slots = nr_slots;
9964 htab->hash_table = index_ptr;
9965 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9967 /* Exit early if the table is empty. */
9968 if (nr_slots == 0 || nr_units == 0
9969 || (version == 2 && nr_columns == 0))
9971 /* All must be zero. */
9972 if (nr_slots != 0 || nr_units != 0
9973 || (version == 2 && nr_columns != 0))
9975 complaint (&symfile_complaints,
9976 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9977 " all zero [in modules %s]"),
9985 htab->section_pool.v1.indices =
9986 htab->unit_table + sizeof (uint32_t) * nr_slots;
9987 /* It's harder to decide whether the section is too small in v1.
9988 V1 is deprecated anyway so we punt. */
9992 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9993 int *ids = htab->section_pool.v2.section_ids;
9994 /* Reverse map for error checking. */
9995 int ids_seen[DW_SECT_MAX + 1];
10000 error (_("Dwarf Error: bad DWP hash table, too few columns"
10001 " in section table [in module %s]"),
10004 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10006 error (_("Dwarf Error: bad DWP hash table, too many columns"
10007 " in section table [in module %s]"),
10010 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10011 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10012 for (i = 0; i < nr_columns; ++i)
10014 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10016 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10018 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10019 " in section table [in module %s]"),
10020 id, dwp_file->name);
10022 if (ids_seen[id] != -1)
10024 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10025 " id %d in section table [in module %s]"),
10026 id, dwp_file->name);
10031 /* Must have exactly one info or types section. */
10032 if (((ids_seen[DW_SECT_INFO] != -1)
10033 + (ids_seen[DW_SECT_TYPES] != -1))
10036 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10037 " DWO info/types section [in module %s]"),
10040 /* Must have an abbrev section. */
10041 if (ids_seen[DW_SECT_ABBREV] == -1)
10043 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10044 " section [in module %s]"),
10047 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10048 htab->section_pool.v2.sizes =
10049 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10050 * nr_units * nr_columns);
10051 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10052 * nr_units * nr_columns))
10055 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10056 " [in module %s]"),
10064 /* Update SECTIONS with the data from SECTP.
10066 This function is like the other "locate" section routines that are
10067 passed to bfd_map_over_sections, but in this context the sections to
10068 read comes from the DWP V1 hash table, not the full ELF section table.
10070 The result is non-zero for success, or zero if an error was found. */
10073 locate_v1_virtual_dwo_sections (asection *sectp,
10074 struct virtual_v1_dwo_sections *sections)
10076 const struct dwop_section_names *names = &dwop_section_names;
10078 if (section_is_p (sectp->name, &names->abbrev_dwo))
10080 /* There can be only one. */
10081 if (sections->abbrev.s.section != NULL)
10083 sections->abbrev.s.section = sectp;
10084 sections->abbrev.size = bfd_get_section_size (sectp);
10086 else if (section_is_p (sectp->name, &names->info_dwo)
10087 || section_is_p (sectp->name, &names->types_dwo))
10089 /* There can be only one. */
10090 if (sections->info_or_types.s.section != NULL)
10092 sections->info_or_types.s.section = sectp;
10093 sections->info_or_types.size = bfd_get_section_size (sectp);
10095 else if (section_is_p (sectp->name, &names->line_dwo))
10097 /* There can be only one. */
10098 if (sections->line.s.section != NULL)
10100 sections->line.s.section = sectp;
10101 sections->line.size = bfd_get_section_size (sectp);
10103 else if (section_is_p (sectp->name, &names->loc_dwo))
10105 /* There can be only one. */
10106 if (sections->loc.s.section != NULL)
10108 sections->loc.s.section = sectp;
10109 sections->loc.size = bfd_get_section_size (sectp);
10111 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10113 /* There can be only one. */
10114 if (sections->macinfo.s.section != NULL)
10116 sections->macinfo.s.section = sectp;
10117 sections->macinfo.size = bfd_get_section_size (sectp);
10119 else if (section_is_p (sectp->name, &names->macro_dwo))
10121 /* There can be only one. */
10122 if (sections->macro.s.section != NULL)
10124 sections->macro.s.section = sectp;
10125 sections->macro.size = bfd_get_section_size (sectp);
10127 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10129 /* There can be only one. */
10130 if (sections->str_offsets.s.section != NULL)
10132 sections->str_offsets.s.section = sectp;
10133 sections->str_offsets.size = bfd_get_section_size (sectp);
10137 /* No other kind of section is valid. */
10144 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10145 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10146 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10147 This is for DWP version 1 files. */
10149 static struct dwo_unit *
10150 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10151 uint32_t unit_index,
10152 const char *comp_dir,
10153 ULONGEST signature, int is_debug_types)
10155 struct objfile *objfile = dwarf2_per_objfile->objfile;
10156 const struct dwp_hash_table *dwp_htab =
10157 is_debug_types ? dwp_file->tus : dwp_file->cus;
10158 bfd *dbfd = dwp_file->dbfd;
10159 const char *kind = is_debug_types ? "TU" : "CU";
10160 struct dwo_file *dwo_file;
10161 struct dwo_unit *dwo_unit;
10162 struct virtual_v1_dwo_sections sections;
10163 void **dwo_file_slot;
10164 char *virtual_dwo_name;
10165 struct cleanup *cleanups;
10168 gdb_assert (dwp_file->version == 1);
10170 if (dwarf_read_debug)
10172 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10174 pulongest (unit_index), hex_string (signature),
10178 /* Fetch the sections of this DWO unit.
10179 Put a limit on the number of sections we look for so that bad data
10180 doesn't cause us to loop forever. */
10182 #define MAX_NR_V1_DWO_SECTIONS \
10183 (1 /* .debug_info or .debug_types */ \
10184 + 1 /* .debug_abbrev */ \
10185 + 1 /* .debug_line */ \
10186 + 1 /* .debug_loc */ \
10187 + 1 /* .debug_str_offsets */ \
10188 + 1 /* .debug_macro or .debug_macinfo */ \
10189 + 1 /* trailing zero */)
10191 memset (§ions, 0, sizeof (sections));
10192 cleanups = make_cleanup (null_cleanup, 0);
10194 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10197 uint32_t section_nr =
10198 read_4_bytes (dbfd,
10199 dwp_htab->section_pool.v1.indices
10200 + (unit_index + i) * sizeof (uint32_t));
10202 if (section_nr == 0)
10204 if (section_nr >= dwp_file->num_sections)
10206 error (_("Dwarf Error: bad DWP hash table, section number too large"
10207 " [in module %s]"),
10211 sectp = dwp_file->elf_sections[section_nr];
10212 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
10214 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10215 " [in module %s]"),
10221 || dwarf2_section_empty_p (§ions.info_or_types)
10222 || dwarf2_section_empty_p (§ions.abbrev))
10224 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10225 " [in module %s]"),
10228 if (i == MAX_NR_V1_DWO_SECTIONS)
10230 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10231 " [in module %s]"),
10235 /* It's easier for the rest of the code if we fake a struct dwo_file and
10236 have dwo_unit "live" in that. At least for now.
10238 The DWP file can be made up of a random collection of CUs and TUs.
10239 However, for each CU + set of TUs that came from the same original DWO
10240 file, we can combine them back into a virtual DWO file to save space
10241 (fewer struct dwo_file objects to allocate). Remember that for really
10242 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10245 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10246 get_section_id (§ions.abbrev),
10247 get_section_id (§ions.line),
10248 get_section_id (§ions.loc),
10249 get_section_id (§ions.str_offsets));
10250 make_cleanup (xfree, virtual_dwo_name);
10251 /* Can we use an existing virtual DWO file? */
10252 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10253 /* Create one if necessary. */
10254 if (*dwo_file_slot == NULL)
10256 if (dwarf_read_debug)
10258 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10261 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10263 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10265 strlen (virtual_dwo_name));
10266 dwo_file->comp_dir = comp_dir;
10267 dwo_file->sections.abbrev = sections.abbrev;
10268 dwo_file->sections.line = sections.line;
10269 dwo_file->sections.loc = sections.loc;
10270 dwo_file->sections.macinfo = sections.macinfo;
10271 dwo_file->sections.macro = sections.macro;
10272 dwo_file->sections.str_offsets = sections.str_offsets;
10273 /* The "str" section is global to the entire DWP file. */
10274 dwo_file->sections.str = dwp_file->sections.str;
10275 /* The info or types section is assigned below to dwo_unit,
10276 there's no need to record it in dwo_file.
10277 Also, we can't simply record type sections in dwo_file because
10278 we record a pointer into the vector in dwo_unit. As we collect more
10279 types we'll grow the vector and eventually have to reallocate space
10280 for it, invalidating all copies of pointers into the previous
10282 *dwo_file_slot = dwo_file;
10286 if (dwarf_read_debug)
10288 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10291 dwo_file = (struct dwo_file *) *dwo_file_slot;
10293 do_cleanups (cleanups);
10295 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10296 dwo_unit->dwo_file = dwo_file;
10297 dwo_unit->signature = signature;
10298 dwo_unit->section =
10299 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10300 *dwo_unit->section = sections.info_or_types;
10301 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10306 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10307 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10308 piece within that section used by a TU/CU, return a virtual section
10309 of just that piece. */
10311 static struct dwarf2_section_info
10312 create_dwp_v2_section (struct dwarf2_section_info *section,
10313 bfd_size_type offset, bfd_size_type size)
10315 struct dwarf2_section_info result;
10318 gdb_assert (section != NULL);
10319 gdb_assert (!section->is_virtual);
10321 memset (&result, 0, sizeof (result));
10322 result.s.containing_section = section;
10323 result.is_virtual = 1;
10328 sectp = get_section_bfd_section (section);
10330 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10331 bounds of the real section. This is a pretty-rare event, so just
10332 flag an error (easier) instead of a warning and trying to cope. */
10334 || offset + size > bfd_get_section_size (sectp))
10336 bfd *abfd = sectp->owner;
10338 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10339 " in section %s [in module %s]"),
10340 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10341 objfile_name (dwarf2_per_objfile->objfile));
10344 result.virtual_offset = offset;
10345 result.size = size;
10349 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10350 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10351 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10352 This is for DWP version 2 files. */
10354 static struct dwo_unit *
10355 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10356 uint32_t unit_index,
10357 const char *comp_dir,
10358 ULONGEST signature, int is_debug_types)
10360 struct objfile *objfile = dwarf2_per_objfile->objfile;
10361 const struct dwp_hash_table *dwp_htab =
10362 is_debug_types ? dwp_file->tus : dwp_file->cus;
10363 bfd *dbfd = dwp_file->dbfd;
10364 const char *kind = is_debug_types ? "TU" : "CU";
10365 struct dwo_file *dwo_file;
10366 struct dwo_unit *dwo_unit;
10367 struct virtual_v2_dwo_sections sections;
10368 void **dwo_file_slot;
10369 char *virtual_dwo_name;
10370 struct cleanup *cleanups;
10373 gdb_assert (dwp_file->version == 2);
10375 if (dwarf_read_debug)
10377 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10379 pulongest (unit_index), hex_string (signature),
10383 /* Fetch the section offsets of this DWO unit. */
10385 memset (§ions, 0, sizeof (sections));
10386 cleanups = make_cleanup (null_cleanup, 0);
10388 for (i = 0; i < dwp_htab->nr_columns; ++i)
10390 uint32_t offset = read_4_bytes (dbfd,
10391 dwp_htab->section_pool.v2.offsets
10392 + (((unit_index - 1) * dwp_htab->nr_columns
10394 * sizeof (uint32_t)));
10395 uint32_t size = read_4_bytes (dbfd,
10396 dwp_htab->section_pool.v2.sizes
10397 + (((unit_index - 1) * dwp_htab->nr_columns
10399 * sizeof (uint32_t)));
10401 switch (dwp_htab->section_pool.v2.section_ids[i])
10404 case DW_SECT_TYPES:
10405 sections.info_or_types_offset = offset;
10406 sections.info_or_types_size = size;
10408 case DW_SECT_ABBREV:
10409 sections.abbrev_offset = offset;
10410 sections.abbrev_size = size;
10413 sections.line_offset = offset;
10414 sections.line_size = size;
10417 sections.loc_offset = offset;
10418 sections.loc_size = size;
10420 case DW_SECT_STR_OFFSETS:
10421 sections.str_offsets_offset = offset;
10422 sections.str_offsets_size = size;
10424 case DW_SECT_MACINFO:
10425 sections.macinfo_offset = offset;
10426 sections.macinfo_size = size;
10428 case DW_SECT_MACRO:
10429 sections.macro_offset = offset;
10430 sections.macro_size = size;
10435 /* It's easier for the rest of the code if we fake a struct dwo_file and
10436 have dwo_unit "live" in that. At least for now.
10438 The DWP file can be made up of a random collection of CUs and TUs.
10439 However, for each CU + set of TUs that came from the same original DWO
10440 file, we can combine them back into a virtual DWO file to save space
10441 (fewer struct dwo_file objects to allocate). Remember that for really
10442 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10445 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10446 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10447 (long) (sections.line_size ? sections.line_offset : 0),
10448 (long) (sections.loc_size ? sections.loc_offset : 0),
10449 (long) (sections.str_offsets_size
10450 ? sections.str_offsets_offset : 0));
10451 make_cleanup (xfree, virtual_dwo_name);
10452 /* Can we use an existing virtual DWO file? */
10453 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10454 /* Create one if necessary. */
10455 if (*dwo_file_slot == NULL)
10457 if (dwarf_read_debug)
10459 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10462 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10464 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10466 strlen (virtual_dwo_name));
10467 dwo_file->comp_dir = comp_dir;
10468 dwo_file->sections.abbrev =
10469 create_dwp_v2_section (&dwp_file->sections.abbrev,
10470 sections.abbrev_offset, sections.abbrev_size);
10471 dwo_file->sections.line =
10472 create_dwp_v2_section (&dwp_file->sections.line,
10473 sections.line_offset, sections.line_size);
10474 dwo_file->sections.loc =
10475 create_dwp_v2_section (&dwp_file->sections.loc,
10476 sections.loc_offset, sections.loc_size);
10477 dwo_file->sections.macinfo =
10478 create_dwp_v2_section (&dwp_file->sections.macinfo,
10479 sections.macinfo_offset, sections.macinfo_size);
10480 dwo_file->sections.macro =
10481 create_dwp_v2_section (&dwp_file->sections.macro,
10482 sections.macro_offset, sections.macro_size);
10483 dwo_file->sections.str_offsets =
10484 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10485 sections.str_offsets_offset,
10486 sections.str_offsets_size);
10487 /* The "str" section is global to the entire DWP file. */
10488 dwo_file->sections.str = dwp_file->sections.str;
10489 /* The info or types section is assigned below to dwo_unit,
10490 there's no need to record it in dwo_file.
10491 Also, we can't simply record type sections in dwo_file because
10492 we record a pointer into the vector in dwo_unit. As we collect more
10493 types we'll grow the vector and eventually have to reallocate space
10494 for it, invalidating all copies of pointers into the previous
10496 *dwo_file_slot = dwo_file;
10500 if (dwarf_read_debug)
10502 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10505 dwo_file = (struct dwo_file *) *dwo_file_slot;
10507 do_cleanups (cleanups);
10509 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10510 dwo_unit->dwo_file = dwo_file;
10511 dwo_unit->signature = signature;
10512 dwo_unit->section =
10513 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10514 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10515 ? &dwp_file->sections.types
10516 : &dwp_file->sections.info,
10517 sections.info_or_types_offset,
10518 sections.info_or_types_size);
10519 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10524 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10525 Returns NULL if the signature isn't found. */
10527 static struct dwo_unit *
10528 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10529 ULONGEST signature, int is_debug_types)
10531 const struct dwp_hash_table *dwp_htab =
10532 is_debug_types ? dwp_file->tus : dwp_file->cus;
10533 bfd *dbfd = dwp_file->dbfd;
10534 uint32_t mask = dwp_htab->nr_slots - 1;
10535 uint32_t hash = signature & mask;
10536 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10539 struct dwo_unit find_dwo_cu;
10541 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10542 find_dwo_cu.signature = signature;
10543 slot = htab_find_slot (is_debug_types
10544 ? dwp_file->loaded_tus
10545 : dwp_file->loaded_cus,
10546 &find_dwo_cu, INSERT);
10549 return (struct dwo_unit *) *slot;
10551 /* Use a for loop so that we don't loop forever on bad debug info. */
10552 for (i = 0; i < dwp_htab->nr_slots; ++i)
10554 ULONGEST signature_in_table;
10556 signature_in_table =
10557 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10558 if (signature_in_table == signature)
10560 uint32_t unit_index =
10561 read_4_bytes (dbfd,
10562 dwp_htab->unit_table + hash * sizeof (uint32_t));
10564 if (dwp_file->version == 1)
10566 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10567 comp_dir, signature,
10572 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10573 comp_dir, signature,
10576 return (struct dwo_unit *) *slot;
10578 if (signature_in_table == 0)
10580 hash = (hash + hash2) & mask;
10583 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10584 " [in module %s]"),
10588 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10589 Open the file specified by FILE_NAME and hand it off to BFD for
10590 preliminary analysis. Return a newly initialized bfd *, which
10591 includes a canonicalized copy of FILE_NAME.
10592 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10593 SEARCH_CWD is true if the current directory is to be searched.
10594 It will be searched before debug-file-directory.
10595 If successful, the file is added to the bfd include table of the
10596 objfile's bfd (see gdb_bfd_record_inclusion).
10597 If unable to find/open the file, return NULL.
10598 NOTE: This function is derived from symfile_bfd_open. */
10600 static gdb_bfd_ref_ptr
10601 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10604 char *absolute_name;
10605 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10606 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10607 to debug_file_directory. */
10609 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10613 if (*debug_file_directory != '\0')
10614 search_path = concat (".", dirname_separator_string,
10615 debug_file_directory, (char *) NULL);
10617 search_path = xstrdup (".");
10620 search_path = xstrdup (debug_file_directory);
10622 flags = OPF_RETURN_REALPATH;
10624 flags |= OPF_SEARCH_IN_PATH;
10625 desc = openp (search_path, flags, file_name,
10626 O_RDONLY | O_BINARY, &absolute_name);
10627 xfree (search_path);
10631 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10632 xfree (absolute_name);
10633 if (sym_bfd == NULL)
10635 bfd_set_cacheable (sym_bfd.get (), 1);
10637 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10640 /* Success. Record the bfd as having been included by the objfile's bfd.
10641 This is important because things like demangled_names_hash lives in the
10642 objfile's per_bfd space and may have references to things like symbol
10643 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10644 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10649 /* Try to open DWO file FILE_NAME.
10650 COMP_DIR is the DW_AT_comp_dir attribute.
10651 The result is the bfd handle of the file.
10652 If there is a problem finding or opening the file, return NULL.
10653 Upon success, the canonicalized path of the file is stored in the bfd,
10654 same as symfile_bfd_open. */
10656 static gdb_bfd_ref_ptr
10657 open_dwo_file (const char *file_name, const char *comp_dir)
10659 if (IS_ABSOLUTE_PATH (file_name))
10660 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10662 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10664 if (comp_dir != NULL)
10666 char *path_to_try = concat (comp_dir, SLASH_STRING,
10667 file_name, (char *) NULL);
10669 /* NOTE: If comp_dir is a relative path, this will also try the
10670 search path, which seems useful. */
10671 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10672 1 /*search_cwd*/));
10673 xfree (path_to_try);
10678 /* That didn't work, try debug-file-directory, which, despite its name,
10679 is a list of paths. */
10681 if (*debug_file_directory == '\0')
10684 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10687 /* This function is mapped across the sections and remembers the offset and
10688 size of each of the DWO debugging sections we are interested in. */
10691 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10693 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10694 const struct dwop_section_names *names = &dwop_section_names;
10696 if (section_is_p (sectp->name, &names->abbrev_dwo))
10698 dwo_sections->abbrev.s.section = sectp;
10699 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10701 else if (section_is_p (sectp->name, &names->info_dwo))
10703 dwo_sections->info.s.section = sectp;
10704 dwo_sections->info.size = bfd_get_section_size (sectp);
10706 else if (section_is_p (sectp->name, &names->line_dwo))
10708 dwo_sections->line.s.section = sectp;
10709 dwo_sections->line.size = bfd_get_section_size (sectp);
10711 else if (section_is_p (sectp->name, &names->loc_dwo))
10713 dwo_sections->loc.s.section = sectp;
10714 dwo_sections->loc.size = bfd_get_section_size (sectp);
10716 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10718 dwo_sections->macinfo.s.section = sectp;
10719 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10721 else if (section_is_p (sectp->name, &names->macro_dwo))
10723 dwo_sections->macro.s.section = sectp;
10724 dwo_sections->macro.size = bfd_get_section_size (sectp);
10726 else if (section_is_p (sectp->name, &names->str_dwo))
10728 dwo_sections->str.s.section = sectp;
10729 dwo_sections->str.size = bfd_get_section_size (sectp);
10731 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10733 dwo_sections->str_offsets.s.section = sectp;
10734 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10736 else if (section_is_p (sectp->name, &names->types_dwo))
10738 struct dwarf2_section_info type_section;
10740 memset (&type_section, 0, sizeof (type_section));
10741 type_section.s.section = sectp;
10742 type_section.size = bfd_get_section_size (sectp);
10743 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10748 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10749 by PER_CU. This is for the non-DWP case.
10750 The result is NULL if DWO_NAME can't be found. */
10752 static struct dwo_file *
10753 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10754 const char *dwo_name, const char *comp_dir)
10756 struct objfile *objfile = dwarf2_per_objfile->objfile;
10757 struct dwo_file *dwo_file;
10758 struct cleanup *cleanups;
10760 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10763 if (dwarf_read_debug)
10764 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10767 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10768 dwo_file->dwo_name = dwo_name;
10769 dwo_file->comp_dir = comp_dir;
10770 dwo_file->dbfd = dbfd.release ();
10772 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10774 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10775 &dwo_file->sections);
10777 dwo_file->cu = create_dwo_cu (dwo_file);
10779 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10782 discard_cleanups (cleanups);
10784 if (dwarf_read_debug)
10785 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10790 /* This function is mapped across the sections and remembers the offset and
10791 size of each of the DWP debugging sections common to version 1 and 2 that
10792 we are interested in. */
10795 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10796 void *dwp_file_ptr)
10798 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10799 const struct dwop_section_names *names = &dwop_section_names;
10800 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10802 /* Record the ELF section number for later lookup: this is what the
10803 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10804 gdb_assert (elf_section_nr < dwp_file->num_sections);
10805 dwp_file->elf_sections[elf_section_nr] = sectp;
10807 /* Look for specific sections that we need. */
10808 if (section_is_p (sectp->name, &names->str_dwo))
10810 dwp_file->sections.str.s.section = sectp;
10811 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10813 else if (section_is_p (sectp->name, &names->cu_index))
10815 dwp_file->sections.cu_index.s.section = sectp;
10816 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10818 else if (section_is_p (sectp->name, &names->tu_index))
10820 dwp_file->sections.tu_index.s.section = sectp;
10821 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10825 /* This function is mapped across the sections and remembers the offset and
10826 size of each of the DWP version 2 debugging sections that we are interested
10827 in. This is split into a separate function because we don't know if we
10828 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10831 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10833 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10834 const struct dwop_section_names *names = &dwop_section_names;
10835 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10837 /* Record the ELF section number for later lookup: this is what the
10838 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10839 gdb_assert (elf_section_nr < dwp_file->num_sections);
10840 dwp_file->elf_sections[elf_section_nr] = sectp;
10842 /* Look for specific sections that we need. */
10843 if (section_is_p (sectp->name, &names->abbrev_dwo))
10845 dwp_file->sections.abbrev.s.section = sectp;
10846 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10848 else if (section_is_p (sectp->name, &names->info_dwo))
10850 dwp_file->sections.info.s.section = sectp;
10851 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10853 else if (section_is_p (sectp->name, &names->line_dwo))
10855 dwp_file->sections.line.s.section = sectp;
10856 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10858 else if (section_is_p (sectp->name, &names->loc_dwo))
10860 dwp_file->sections.loc.s.section = sectp;
10861 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10863 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10865 dwp_file->sections.macinfo.s.section = sectp;
10866 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10868 else if (section_is_p (sectp->name, &names->macro_dwo))
10870 dwp_file->sections.macro.s.section = sectp;
10871 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10873 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10875 dwp_file->sections.str_offsets.s.section = sectp;
10876 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10878 else if (section_is_p (sectp->name, &names->types_dwo))
10880 dwp_file->sections.types.s.section = sectp;
10881 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10885 /* Hash function for dwp_file loaded CUs/TUs. */
10888 hash_dwp_loaded_cutus (const void *item)
10890 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10892 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10893 return dwo_unit->signature;
10896 /* Equality function for dwp_file loaded CUs/TUs. */
10899 eq_dwp_loaded_cutus (const void *a, const void *b)
10901 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10902 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10904 return dua->signature == dub->signature;
10907 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10910 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10912 return htab_create_alloc_ex (3,
10913 hash_dwp_loaded_cutus,
10914 eq_dwp_loaded_cutus,
10916 &objfile->objfile_obstack,
10917 hashtab_obstack_allocate,
10918 dummy_obstack_deallocate);
10921 /* Try to open DWP file FILE_NAME.
10922 The result is the bfd handle of the file.
10923 If there is a problem finding or opening the file, return NULL.
10924 Upon success, the canonicalized path of the file is stored in the bfd,
10925 same as symfile_bfd_open. */
10927 static gdb_bfd_ref_ptr
10928 open_dwp_file (const char *file_name)
10930 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10931 1 /*search_cwd*/));
10935 /* Work around upstream bug 15652.
10936 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10937 [Whether that's a "bug" is debatable, but it is getting in our way.]
10938 We have no real idea where the dwp file is, because gdb's realpath-ing
10939 of the executable's path may have discarded the needed info.
10940 [IWBN if the dwp file name was recorded in the executable, akin to
10941 .gnu_debuglink, but that doesn't exist yet.]
10942 Strip the directory from FILE_NAME and search again. */
10943 if (*debug_file_directory != '\0')
10945 /* Don't implicitly search the current directory here.
10946 If the user wants to search "." to handle this case,
10947 it must be added to debug-file-directory. */
10948 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10955 /* Initialize the use of the DWP file for the current objfile.
10956 By convention the name of the DWP file is ${objfile}.dwp.
10957 The result is NULL if it can't be found. */
10959 static struct dwp_file *
10960 open_and_init_dwp_file (void)
10962 struct objfile *objfile = dwarf2_per_objfile->objfile;
10963 struct dwp_file *dwp_file;
10965 /* Try to find first .dwp for the binary file before any symbolic links
10968 /* If the objfile is a debug file, find the name of the real binary
10969 file and get the name of dwp file from there. */
10970 std::string dwp_name;
10971 if (objfile->separate_debug_objfile_backlink != NULL)
10973 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10974 const char *backlink_basename = lbasename (backlink->original_name);
10976 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
10979 dwp_name = objfile->original_name;
10981 dwp_name += ".dwp";
10983 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
10985 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10987 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10988 dwp_name = objfile_name (objfile);
10989 dwp_name += ".dwp";
10990 dbfd = open_dwp_file (dwp_name.c_str ());
10995 if (dwarf_read_debug)
10996 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
10999 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11000 dwp_file->name = bfd_get_filename (dbfd.get ());
11001 dwp_file->dbfd = dbfd.release ();
11003 /* +1: section 0 is unused */
11004 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11005 dwp_file->elf_sections =
11006 OBSTACK_CALLOC (&objfile->objfile_obstack,
11007 dwp_file->num_sections, asection *);
11009 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11012 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11014 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11016 /* The DWP file version is stored in the hash table. Oh well. */
11017 if (dwp_file->cus->version != dwp_file->tus->version)
11019 /* Technically speaking, we should try to limp along, but this is
11020 pretty bizarre. We use pulongest here because that's the established
11021 portability solution (e.g, we cannot use %u for uint32_t). */
11022 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11023 " TU version %s [in DWP file %s]"),
11024 pulongest (dwp_file->cus->version),
11025 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11027 dwp_file->version = dwp_file->cus->version;
11029 if (dwp_file->version == 2)
11030 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11033 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11034 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11036 if (dwarf_read_debug)
11038 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11039 fprintf_unfiltered (gdb_stdlog,
11040 " %s CUs, %s TUs\n",
11041 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11042 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11048 /* Wrapper around open_and_init_dwp_file, only open it once. */
11050 static struct dwp_file *
11051 get_dwp_file (void)
11053 if (! dwarf2_per_objfile->dwp_checked)
11055 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11056 dwarf2_per_objfile->dwp_checked = 1;
11058 return dwarf2_per_objfile->dwp_file;
11061 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11062 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11063 or in the DWP file for the objfile, referenced by THIS_UNIT.
11064 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11065 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11067 This is called, for example, when wanting to read a variable with a
11068 complex location. Therefore we don't want to do file i/o for every call.
11069 Therefore we don't want to look for a DWO file on every call.
11070 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11071 then we check if we've already seen DWO_NAME, and only THEN do we check
11074 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11075 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11077 static struct dwo_unit *
11078 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11079 const char *dwo_name, const char *comp_dir,
11080 ULONGEST signature, int is_debug_types)
11082 struct objfile *objfile = dwarf2_per_objfile->objfile;
11083 const char *kind = is_debug_types ? "TU" : "CU";
11084 void **dwo_file_slot;
11085 struct dwo_file *dwo_file;
11086 struct dwp_file *dwp_file;
11088 /* First see if there's a DWP file.
11089 If we have a DWP file but didn't find the DWO inside it, don't
11090 look for the original DWO file. It makes gdb behave differently
11091 depending on whether one is debugging in the build tree. */
11093 dwp_file = get_dwp_file ();
11094 if (dwp_file != NULL)
11096 const struct dwp_hash_table *dwp_htab =
11097 is_debug_types ? dwp_file->tus : dwp_file->cus;
11099 if (dwp_htab != NULL)
11101 struct dwo_unit *dwo_cutu =
11102 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11103 signature, is_debug_types);
11105 if (dwo_cutu != NULL)
11107 if (dwarf_read_debug)
11109 fprintf_unfiltered (gdb_stdlog,
11110 "Virtual DWO %s %s found: @%s\n",
11111 kind, hex_string (signature),
11112 host_address_to_string (dwo_cutu));
11120 /* No DWP file, look for the DWO file. */
11122 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11123 if (*dwo_file_slot == NULL)
11125 /* Read in the file and build a table of the CUs/TUs it contains. */
11126 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11128 /* NOTE: This will be NULL if unable to open the file. */
11129 dwo_file = (struct dwo_file *) *dwo_file_slot;
11131 if (dwo_file != NULL)
11133 struct dwo_unit *dwo_cutu = NULL;
11135 if (is_debug_types && dwo_file->tus)
11137 struct dwo_unit find_dwo_cutu;
11139 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11140 find_dwo_cutu.signature = signature;
11142 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11144 else if (!is_debug_types && dwo_file->cu)
11146 if (signature == dwo_file->cu->signature)
11147 dwo_cutu = dwo_file->cu;
11150 if (dwo_cutu != NULL)
11152 if (dwarf_read_debug)
11154 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11155 kind, dwo_name, hex_string (signature),
11156 host_address_to_string (dwo_cutu));
11163 /* We didn't find it. This could mean a dwo_id mismatch, or
11164 someone deleted the DWO/DWP file, or the search path isn't set up
11165 correctly to find the file. */
11167 if (dwarf_read_debug)
11169 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11170 kind, dwo_name, hex_string (signature));
11173 /* This is a warning and not a complaint because it can be caused by
11174 pilot error (e.g., user accidentally deleting the DWO). */
11176 /* Print the name of the DWP file if we looked there, helps the user
11177 better diagnose the problem. */
11178 char *dwp_text = NULL;
11179 struct cleanup *cleanups;
11181 if (dwp_file != NULL)
11182 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11183 cleanups = make_cleanup (xfree, dwp_text);
11185 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11186 " [in module %s]"),
11187 kind, dwo_name, hex_string (signature),
11188 dwp_text != NULL ? dwp_text : "",
11189 this_unit->is_debug_types ? "TU" : "CU",
11190 to_underlying (this_unit->sect_off), objfile_name (objfile));
11192 do_cleanups (cleanups);
11197 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11198 See lookup_dwo_cutu_unit for details. */
11200 static struct dwo_unit *
11201 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11202 const char *dwo_name, const char *comp_dir,
11203 ULONGEST signature)
11205 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11208 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11209 See lookup_dwo_cutu_unit for details. */
11211 static struct dwo_unit *
11212 lookup_dwo_type_unit (struct signatured_type *this_tu,
11213 const char *dwo_name, const char *comp_dir)
11215 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11218 /* Traversal function for queue_and_load_all_dwo_tus. */
11221 queue_and_load_dwo_tu (void **slot, void *info)
11223 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11224 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11225 ULONGEST signature = dwo_unit->signature;
11226 struct signatured_type *sig_type =
11227 lookup_dwo_signatured_type (per_cu->cu, signature);
11229 if (sig_type != NULL)
11231 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11233 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11234 a real dependency of PER_CU on SIG_TYPE. That is detected later
11235 while processing PER_CU. */
11236 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11237 load_full_type_unit (sig_cu);
11238 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11244 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11245 The DWO may have the only definition of the type, though it may not be
11246 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11247 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11250 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11252 struct dwo_unit *dwo_unit;
11253 struct dwo_file *dwo_file;
11255 gdb_assert (!per_cu->is_debug_types);
11256 gdb_assert (get_dwp_file () == NULL);
11257 gdb_assert (per_cu->cu != NULL);
11259 dwo_unit = per_cu->cu->dwo_unit;
11260 gdb_assert (dwo_unit != NULL);
11262 dwo_file = dwo_unit->dwo_file;
11263 if (dwo_file->tus != NULL)
11264 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11267 /* Free all resources associated with DWO_FILE.
11268 Close the DWO file and munmap the sections.
11269 All memory should be on the objfile obstack. */
11272 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11275 /* Note: dbfd is NULL for virtual DWO files. */
11276 gdb_bfd_unref (dwo_file->dbfd);
11278 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11281 /* Wrapper for free_dwo_file for use in cleanups. */
11284 free_dwo_file_cleanup (void *arg)
11286 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11287 struct objfile *objfile = dwarf2_per_objfile->objfile;
11289 free_dwo_file (dwo_file, objfile);
11292 /* Traversal function for free_dwo_files. */
11295 free_dwo_file_from_slot (void **slot, void *info)
11297 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11298 struct objfile *objfile = (struct objfile *) info;
11300 free_dwo_file (dwo_file, objfile);
11305 /* Free all resources associated with DWO_FILES. */
11308 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11310 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11313 /* Read in various DIEs. */
11315 /* qsort helper for inherit_abstract_dies. */
11318 unsigned_int_compar (const void *ap, const void *bp)
11320 unsigned int a = *(unsigned int *) ap;
11321 unsigned int b = *(unsigned int *) bp;
11323 return (a > b) - (b > a);
11326 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11327 Inherit only the children of the DW_AT_abstract_origin DIE not being
11328 already referenced by DW_AT_abstract_origin from the children of the
11332 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11334 struct die_info *child_die;
11335 unsigned die_children_count;
11336 /* CU offsets which were referenced by children of the current DIE. */
11337 sect_offset *offsets;
11338 sect_offset *offsets_end, *offsetp;
11339 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11340 struct die_info *origin_die;
11341 /* Iterator of the ORIGIN_DIE children. */
11342 struct die_info *origin_child_die;
11343 struct cleanup *cleanups;
11344 struct attribute *attr;
11345 struct dwarf2_cu *origin_cu;
11346 struct pending **origin_previous_list_in_scope;
11348 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11352 /* Note that following die references may follow to a die in a
11356 origin_die = follow_die_ref (die, attr, &origin_cu);
11358 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11360 origin_previous_list_in_scope = origin_cu->list_in_scope;
11361 origin_cu->list_in_scope = cu->list_in_scope;
11363 if (die->tag != origin_die->tag
11364 && !(die->tag == DW_TAG_inlined_subroutine
11365 && origin_die->tag == DW_TAG_subprogram))
11366 complaint (&symfile_complaints,
11367 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11368 to_underlying (die->sect_off),
11369 to_underlying (origin_die->sect_off));
11371 child_die = die->child;
11372 die_children_count = 0;
11373 while (child_die && child_die->tag)
11375 child_die = sibling_die (child_die);
11376 die_children_count++;
11378 offsets = XNEWVEC (sect_offset, die_children_count);
11379 cleanups = make_cleanup (xfree, offsets);
11381 offsets_end = offsets;
11382 for (child_die = die->child;
11383 child_die && child_die->tag;
11384 child_die = sibling_die (child_die))
11386 struct die_info *child_origin_die;
11387 struct dwarf2_cu *child_origin_cu;
11389 /* We are trying to process concrete instance entries:
11390 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11391 it's not relevant to our analysis here. i.e. detecting DIEs that are
11392 present in the abstract instance but not referenced in the concrete
11394 if (child_die->tag == DW_TAG_call_site
11395 || child_die->tag == DW_TAG_GNU_call_site)
11398 /* For each CHILD_DIE, find the corresponding child of
11399 ORIGIN_DIE. If there is more than one layer of
11400 DW_AT_abstract_origin, follow them all; there shouldn't be,
11401 but GCC versions at least through 4.4 generate this (GCC PR
11403 child_origin_die = child_die;
11404 child_origin_cu = cu;
11407 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11411 child_origin_die = follow_die_ref (child_origin_die, attr,
11415 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11416 counterpart may exist. */
11417 if (child_origin_die != child_die)
11419 if (child_die->tag != child_origin_die->tag
11420 && !(child_die->tag == DW_TAG_inlined_subroutine
11421 && child_origin_die->tag == DW_TAG_subprogram))
11422 complaint (&symfile_complaints,
11423 _("Child DIE 0x%x and its abstract origin 0x%x have "
11425 to_underlying (child_die->sect_off),
11426 to_underlying (child_origin_die->sect_off));
11427 if (child_origin_die->parent != origin_die)
11428 complaint (&symfile_complaints,
11429 _("Child DIE 0x%x and its abstract origin 0x%x have "
11430 "different parents"),
11431 to_underlying (child_die->sect_off),
11432 to_underlying (child_origin_die->sect_off));
11434 *offsets_end++ = child_origin_die->sect_off;
11437 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11438 unsigned_int_compar);
11439 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11440 if (offsetp[-1] == *offsetp)
11441 complaint (&symfile_complaints,
11442 _("Multiple children of DIE 0x%x refer "
11443 "to DIE 0x%x as their abstract origin"),
11444 to_underlying (die->sect_off), to_underlying (*offsetp));
11447 origin_child_die = origin_die->child;
11448 while (origin_child_die && origin_child_die->tag)
11450 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11451 while (offsetp < offsets_end
11452 && *offsetp < origin_child_die->sect_off)
11454 if (offsetp >= offsets_end
11455 || *offsetp > origin_child_die->sect_off)
11457 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11458 Check whether we're already processing ORIGIN_CHILD_DIE.
11459 This can happen with mutually referenced abstract_origins.
11461 if (!origin_child_die->in_process)
11462 process_die (origin_child_die, origin_cu);
11464 origin_child_die = sibling_die (origin_child_die);
11466 origin_cu->list_in_scope = origin_previous_list_in_scope;
11468 do_cleanups (cleanups);
11472 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11474 struct objfile *objfile = cu->objfile;
11475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11476 struct context_stack *newobj;
11479 struct die_info *child_die;
11480 struct attribute *attr, *call_line, *call_file;
11482 CORE_ADDR baseaddr;
11483 struct block *block;
11484 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11485 VEC (symbolp) *template_args = NULL;
11486 struct template_symbol *templ_func = NULL;
11490 /* If we do not have call site information, we can't show the
11491 caller of this inlined function. That's too confusing, so
11492 only use the scope for local variables. */
11493 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11494 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11495 if (call_line == NULL || call_file == NULL)
11497 read_lexical_block_scope (die, cu);
11502 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11504 name = dwarf2_name (die, cu);
11506 /* Ignore functions with missing or empty names. These are actually
11507 illegal according to the DWARF standard. */
11510 complaint (&symfile_complaints,
11511 _("missing name for subprogram DIE at %d"),
11512 to_underlying (die->sect_off));
11516 /* Ignore functions with missing or invalid low and high pc attributes. */
11517 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11518 <= PC_BOUNDS_INVALID)
11520 attr = dwarf2_attr (die, DW_AT_external, cu);
11521 if (!attr || !DW_UNSND (attr))
11522 complaint (&symfile_complaints,
11523 _("cannot get low and high bounds "
11524 "for subprogram DIE at %d"),
11525 to_underlying (die->sect_off));
11529 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11530 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11532 /* If we have any template arguments, then we must allocate a
11533 different sort of symbol. */
11534 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11536 if (child_die->tag == DW_TAG_template_type_param
11537 || child_die->tag == DW_TAG_template_value_param)
11539 templ_func = allocate_template_symbol (objfile);
11540 templ_func->base.is_cplus_template_function = 1;
11545 newobj = push_context (0, lowpc);
11546 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11547 (struct symbol *) templ_func);
11549 /* If there is a location expression for DW_AT_frame_base, record
11551 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11553 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11555 /* If there is a location for the static link, record it. */
11556 newobj->static_link = NULL;
11557 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11560 newobj->static_link
11561 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11562 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11565 cu->list_in_scope = &local_symbols;
11567 if (die->child != NULL)
11569 child_die = die->child;
11570 while (child_die && child_die->tag)
11572 if (child_die->tag == DW_TAG_template_type_param
11573 || child_die->tag == DW_TAG_template_value_param)
11575 struct symbol *arg = new_symbol (child_die, NULL, cu);
11578 VEC_safe_push (symbolp, template_args, arg);
11581 process_die (child_die, cu);
11582 child_die = sibling_die (child_die);
11586 inherit_abstract_dies (die, cu);
11588 /* If we have a DW_AT_specification, we might need to import using
11589 directives from the context of the specification DIE. See the
11590 comment in determine_prefix. */
11591 if (cu->language == language_cplus
11592 && dwarf2_attr (die, DW_AT_specification, cu))
11594 struct dwarf2_cu *spec_cu = cu;
11595 struct die_info *spec_die = die_specification (die, &spec_cu);
11599 child_die = spec_die->child;
11600 while (child_die && child_die->tag)
11602 if (child_die->tag == DW_TAG_imported_module)
11603 process_die (child_die, spec_cu);
11604 child_die = sibling_die (child_die);
11607 /* In some cases, GCC generates specification DIEs that
11608 themselves contain DW_AT_specification attributes. */
11609 spec_die = die_specification (spec_die, &spec_cu);
11613 newobj = pop_context ();
11614 /* Make a block for the local symbols within. */
11615 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11616 newobj->static_link, lowpc, highpc);
11618 /* For C++, set the block's scope. */
11619 if ((cu->language == language_cplus
11620 || cu->language == language_fortran
11621 || cu->language == language_d
11622 || cu->language == language_rust)
11623 && cu->processing_has_namespace_info)
11624 block_set_scope (block, determine_prefix (die, cu),
11625 &objfile->objfile_obstack);
11627 /* If we have address ranges, record them. */
11628 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11630 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11632 /* Attach template arguments to function. */
11633 if (! VEC_empty (symbolp, template_args))
11635 gdb_assert (templ_func != NULL);
11637 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11638 templ_func->template_arguments
11639 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11640 templ_func->n_template_arguments);
11641 memcpy (templ_func->template_arguments,
11642 VEC_address (symbolp, template_args),
11643 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11644 VEC_free (symbolp, template_args);
11647 /* In C++, we can have functions nested inside functions (e.g., when
11648 a function declares a class that has methods). This means that
11649 when we finish processing a function scope, we may need to go
11650 back to building a containing block's symbol lists. */
11651 local_symbols = newobj->locals;
11652 local_using_directives = newobj->local_using_directives;
11654 /* If we've finished processing a top-level function, subsequent
11655 symbols go in the file symbol list. */
11656 if (outermost_context_p ())
11657 cu->list_in_scope = &file_symbols;
11660 /* Process all the DIES contained within a lexical block scope. Start
11661 a new scope, process the dies, and then close the scope. */
11664 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11666 struct objfile *objfile = cu->objfile;
11667 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11668 struct context_stack *newobj;
11669 CORE_ADDR lowpc, highpc;
11670 struct die_info *child_die;
11671 CORE_ADDR baseaddr;
11673 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11675 /* Ignore blocks with missing or invalid low and high pc attributes. */
11676 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11677 as multiple lexical blocks? Handling children in a sane way would
11678 be nasty. Might be easier to properly extend generic blocks to
11679 describe ranges. */
11680 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11682 case PC_BOUNDS_NOT_PRESENT:
11683 /* DW_TAG_lexical_block has no attributes, process its children as if
11684 there was no wrapping by that DW_TAG_lexical_block.
11685 GCC does no longer produces such DWARF since GCC r224161. */
11686 for (child_die = die->child;
11687 child_die != NULL && child_die->tag;
11688 child_die = sibling_die (child_die))
11689 process_die (child_die, cu);
11691 case PC_BOUNDS_INVALID:
11694 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11695 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11697 push_context (0, lowpc);
11698 if (die->child != NULL)
11700 child_die = die->child;
11701 while (child_die && child_die->tag)
11703 process_die (child_die, cu);
11704 child_die = sibling_die (child_die);
11707 inherit_abstract_dies (die, cu);
11708 newobj = pop_context ();
11710 if (local_symbols != NULL || local_using_directives != NULL)
11712 struct block *block
11713 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11714 newobj->start_addr, highpc);
11716 /* Note that recording ranges after traversing children, as we
11717 do here, means that recording a parent's ranges entails
11718 walking across all its children's ranges as they appear in
11719 the address map, which is quadratic behavior.
11721 It would be nicer to record the parent's ranges before
11722 traversing its children, simply overriding whatever you find
11723 there. But since we don't even decide whether to create a
11724 block until after we've traversed its children, that's hard
11726 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11728 local_symbols = newobj->locals;
11729 local_using_directives = newobj->local_using_directives;
11732 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11735 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11737 struct objfile *objfile = cu->objfile;
11738 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11739 CORE_ADDR pc, baseaddr;
11740 struct attribute *attr;
11741 struct call_site *call_site, call_site_local;
11744 struct die_info *child_die;
11746 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11748 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11751 /* This was a pre-DWARF-5 GNU extension alias
11752 for DW_AT_call_return_pc. */
11753 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11757 complaint (&symfile_complaints,
11758 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11759 "DIE 0x%x [in module %s]"),
11760 to_underlying (die->sect_off), objfile_name (objfile));
11763 pc = attr_value_as_address (attr) + baseaddr;
11764 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11766 if (cu->call_site_htab == NULL)
11767 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11768 NULL, &objfile->objfile_obstack,
11769 hashtab_obstack_allocate, NULL);
11770 call_site_local.pc = pc;
11771 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11774 complaint (&symfile_complaints,
11775 _("Duplicate PC %s for DW_TAG_call_site "
11776 "DIE 0x%x [in module %s]"),
11777 paddress (gdbarch, pc), to_underlying (die->sect_off),
11778 objfile_name (objfile));
11782 /* Count parameters at the caller. */
11785 for (child_die = die->child; child_die && child_die->tag;
11786 child_die = sibling_die (child_die))
11788 if (child_die->tag != DW_TAG_call_site_parameter
11789 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11791 complaint (&symfile_complaints,
11792 _("Tag %d is not DW_TAG_call_site_parameter in "
11793 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11794 child_die->tag, to_underlying (child_die->sect_off),
11795 objfile_name (objfile));
11803 = ((struct call_site *)
11804 obstack_alloc (&objfile->objfile_obstack,
11805 sizeof (*call_site)
11806 + (sizeof (*call_site->parameter) * (nparams - 1))));
11808 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11809 call_site->pc = pc;
11811 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11812 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11814 struct die_info *func_die;
11816 /* Skip also over DW_TAG_inlined_subroutine. */
11817 for (func_die = die->parent;
11818 func_die && func_die->tag != DW_TAG_subprogram
11819 && func_die->tag != DW_TAG_subroutine_type;
11820 func_die = func_die->parent);
11822 /* DW_AT_call_all_calls is a superset
11823 of DW_AT_call_all_tail_calls. */
11825 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11826 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11827 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11828 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11830 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11831 not complete. But keep CALL_SITE for look ups via call_site_htab,
11832 both the initial caller containing the real return address PC and
11833 the final callee containing the current PC of a chain of tail
11834 calls do not need to have the tail call list complete. But any
11835 function candidate for a virtual tail call frame searched via
11836 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11837 determined unambiguously. */
11841 struct type *func_type = NULL;
11844 func_type = get_die_type (func_die, cu);
11845 if (func_type != NULL)
11847 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11849 /* Enlist this call site to the function. */
11850 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11851 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11854 complaint (&symfile_complaints,
11855 _("Cannot find function owning DW_TAG_call_site "
11856 "DIE 0x%x [in module %s]"),
11857 to_underlying (die->sect_off), objfile_name (objfile));
11861 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11863 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11865 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11868 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11869 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11871 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11872 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11873 /* Keep NULL DWARF_BLOCK. */;
11874 else if (attr_form_is_block (attr))
11876 struct dwarf2_locexpr_baton *dlbaton;
11878 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11879 dlbaton->data = DW_BLOCK (attr)->data;
11880 dlbaton->size = DW_BLOCK (attr)->size;
11881 dlbaton->per_cu = cu->per_cu;
11883 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11885 else if (attr_form_is_ref (attr))
11887 struct dwarf2_cu *target_cu = cu;
11888 struct die_info *target_die;
11890 target_die = follow_die_ref (die, attr, &target_cu);
11891 gdb_assert (target_cu->objfile == objfile);
11892 if (die_is_declaration (target_die, target_cu))
11894 const char *target_physname;
11896 /* Prefer the mangled name; otherwise compute the demangled one. */
11897 target_physname = dwarf2_string_attr (target_die,
11898 DW_AT_linkage_name,
11900 if (target_physname == NULL)
11901 target_physname = dwarf2_string_attr (target_die,
11902 DW_AT_MIPS_linkage_name,
11904 if (target_physname == NULL)
11905 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11906 if (target_physname == NULL)
11907 complaint (&symfile_complaints,
11908 _("DW_AT_call_target target DIE has invalid "
11909 "physname, for referencing DIE 0x%x [in module %s]"),
11910 to_underlying (die->sect_off), objfile_name (objfile));
11912 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11918 /* DW_AT_entry_pc should be preferred. */
11919 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11920 <= PC_BOUNDS_INVALID)
11921 complaint (&symfile_complaints,
11922 _("DW_AT_call_target target DIE has invalid "
11923 "low pc, for referencing DIE 0x%x [in module %s]"),
11924 to_underlying (die->sect_off), objfile_name (objfile));
11927 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11928 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11933 complaint (&symfile_complaints,
11934 _("DW_TAG_call_site DW_AT_call_target is neither "
11935 "block nor reference, for DIE 0x%x [in module %s]"),
11936 to_underlying (die->sect_off), objfile_name (objfile));
11938 call_site->per_cu = cu->per_cu;
11940 for (child_die = die->child;
11941 child_die && child_die->tag;
11942 child_die = sibling_die (child_die))
11944 struct call_site_parameter *parameter;
11945 struct attribute *loc, *origin;
11947 if (child_die->tag != DW_TAG_call_site_parameter
11948 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11950 /* Already printed the complaint above. */
11954 gdb_assert (call_site->parameter_count < nparams);
11955 parameter = &call_site->parameter[call_site->parameter_count];
11957 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11958 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11959 register is contained in DW_AT_call_value. */
11961 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11962 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11963 if (origin == NULL)
11965 /* This was a pre-DWARF-5 GNU extension alias
11966 for DW_AT_call_parameter. */
11967 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11969 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11971 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11973 sect_offset sect_off
11974 = (sect_offset) dwarf2_get_ref_die_offset (origin);
11975 if (!offset_in_cu_p (&cu->header, sect_off))
11977 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11978 binding can be done only inside one CU. Such referenced DIE
11979 therefore cannot be even moved to DW_TAG_partial_unit. */
11980 complaint (&symfile_complaints,
11981 _("DW_AT_call_parameter offset is not in CU for "
11982 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11983 to_underlying (child_die->sect_off),
11984 objfile_name (objfile));
11987 parameter->u.param_cu_off
11988 = (cu_offset) (sect_off - cu->header.sect_off);
11990 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11992 complaint (&symfile_complaints,
11993 _("No DW_FORM_block* DW_AT_location for "
11994 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11995 to_underlying (child_die->sect_off), objfile_name (objfile));
12000 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12001 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12002 if (parameter->u.dwarf_reg != -1)
12003 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12004 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12005 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12006 ¶meter->u.fb_offset))
12007 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12010 complaint (&symfile_complaints,
12011 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12012 "for DW_FORM_block* DW_AT_location is supported for "
12013 "DW_TAG_call_site child DIE 0x%x "
12015 to_underlying (child_die->sect_off),
12016 objfile_name (objfile));
12021 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12023 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12024 if (!attr_form_is_block (attr))
12026 complaint (&symfile_complaints,
12027 _("No DW_FORM_block* DW_AT_call_value for "
12028 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12029 to_underlying (child_die->sect_off),
12030 objfile_name (objfile));
12033 parameter->value = DW_BLOCK (attr)->data;
12034 parameter->value_size = DW_BLOCK (attr)->size;
12036 /* Parameters are not pre-cleared by memset above. */
12037 parameter->data_value = NULL;
12038 parameter->data_value_size = 0;
12039 call_site->parameter_count++;
12041 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12043 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12046 if (!attr_form_is_block (attr))
12047 complaint (&symfile_complaints,
12048 _("No DW_FORM_block* DW_AT_call_data_value for "
12049 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12050 to_underlying (child_die->sect_off),
12051 objfile_name (objfile));
12054 parameter->data_value = DW_BLOCK (attr)->data;
12055 parameter->data_value_size = DW_BLOCK (attr)->size;
12061 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12062 reading .debug_rnglists.
12063 Callback's type should be:
12064 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12065 Return true if the attributes are present and valid, otherwise,
12068 template <typename Callback>
12070 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12071 Callback &&callback)
12073 struct objfile *objfile = cu->objfile;
12074 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12075 struct comp_unit_head *cu_header = &cu->header;
12076 bfd *obfd = objfile->obfd;
12077 unsigned int addr_size = cu_header->addr_size;
12078 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12079 /* Base address selection entry. */
12082 unsigned int dummy;
12083 const gdb_byte *buffer;
12085 CORE_ADDR high = 0;
12086 CORE_ADDR baseaddr;
12087 bool overflow = false;
12089 found_base = cu->base_known;
12090 base = cu->base_address;
12092 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12093 if (offset >= dwarf2_per_objfile->rnglists.size)
12095 complaint (&symfile_complaints,
12096 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12100 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12102 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12106 /* Initialize it due to a false compiler warning. */
12107 CORE_ADDR range_beginning = 0, range_end = 0;
12108 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12109 + dwarf2_per_objfile->rnglists.size);
12110 unsigned int bytes_read;
12112 if (buffer == buf_end)
12117 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12120 case DW_RLE_end_of_list:
12122 case DW_RLE_base_address:
12123 if (buffer + cu->header.addr_size > buf_end)
12128 base = read_address (obfd, buffer, cu, &bytes_read);
12130 buffer += bytes_read;
12132 case DW_RLE_start_length:
12133 if (buffer + cu->header.addr_size > buf_end)
12138 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12139 buffer += bytes_read;
12140 range_end = (range_beginning
12141 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12142 buffer += bytes_read;
12143 if (buffer > buf_end)
12149 case DW_RLE_offset_pair:
12150 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12151 buffer += bytes_read;
12152 if (buffer > buf_end)
12157 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12158 buffer += bytes_read;
12159 if (buffer > buf_end)
12165 case DW_RLE_start_end:
12166 if (buffer + 2 * cu->header.addr_size > buf_end)
12171 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12172 buffer += bytes_read;
12173 range_end = read_address (obfd, buffer, cu, &bytes_read);
12174 buffer += bytes_read;
12177 complaint (&symfile_complaints,
12178 _("Invalid .debug_rnglists data (no base address)"));
12181 if (rlet == DW_RLE_end_of_list || overflow)
12183 if (rlet == DW_RLE_base_address)
12188 /* We have no valid base address for the ranges
12190 complaint (&symfile_complaints,
12191 _("Invalid .debug_rnglists data (no base address)"));
12195 if (range_beginning > range_end)
12197 /* Inverted range entries are invalid. */
12198 complaint (&symfile_complaints,
12199 _("Invalid .debug_rnglists data (inverted range)"));
12203 /* Empty range entries have no effect. */
12204 if (range_beginning == range_end)
12207 range_beginning += base;
12210 /* A not-uncommon case of bad debug info.
12211 Don't pollute the addrmap with bad data. */
12212 if (range_beginning + baseaddr == 0
12213 && !dwarf2_per_objfile->has_section_at_zero)
12215 complaint (&symfile_complaints,
12216 _(".debug_rnglists entry has start address of zero"
12217 " [in module %s]"), objfile_name (objfile));
12221 callback (range_beginning, range_end);
12226 complaint (&symfile_complaints,
12227 _("Offset %d is not terminated "
12228 "for DW_AT_ranges attribute"),
12236 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12237 Callback's type should be:
12238 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12239 Return 1 if the attributes are present and valid, otherwise, return 0. */
12241 template <typename Callback>
12243 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12244 Callback &&callback)
12246 struct objfile *objfile = cu->objfile;
12247 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12248 struct comp_unit_head *cu_header = &cu->header;
12249 bfd *obfd = objfile->obfd;
12250 unsigned int addr_size = cu_header->addr_size;
12251 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12252 /* Base address selection entry. */
12255 unsigned int dummy;
12256 const gdb_byte *buffer;
12257 CORE_ADDR baseaddr;
12259 if (cu_header->version >= 5)
12260 return dwarf2_rnglists_process (offset, cu, callback);
12262 found_base = cu->base_known;
12263 base = cu->base_address;
12265 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12266 if (offset >= dwarf2_per_objfile->ranges.size)
12268 complaint (&symfile_complaints,
12269 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12273 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12279 CORE_ADDR range_beginning, range_end;
12281 range_beginning = read_address (obfd, buffer, cu, &dummy);
12282 buffer += addr_size;
12283 range_end = read_address (obfd, buffer, cu, &dummy);
12284 buffer += addr_size;
12285 offset += 2 * addr_size;
12287 /* An end of list marker is a pair of zero addresses. */
12288 if (range_beginning == 0 && range_end == 0)
12289 /* Found the end of list entry. */
12292 /* Each base address selection entry is a pair of 2 values.
12293 The first is the largest possible address, the second is
12294 the base address. Check for a base address here. */
12295 if ((range_beginning & mask) == mask)
12297 /* If we found the largest possible address, then we already
12298 have the base address in range_end. */
12306 /* We have no valid base address for the ranges
12308 complaint (&symfile_complaints,
12309 _("Invalid .debug_ranges data (no base address)"));
12313 if (range_beginning > range_end)
12315 /* Inverted range entries are invalid. */
12316 complaint (&symfile_complaints,
12317 _("Invalid .debug_ranges data (inverted range)"));
12321 /* Empty range entries have no effect. */
12322 if (range_beginning == range_end)
12325 range_beginning += base;
12328 /* A not-uncommon case of bad debug info.
12329 Don't pollute the addrmap with bad data. */
12330 if (range_beginning + baseaddr == 0
12331 && !dwarf2_per_objfile->has_section_at_zero)
12333 complaint (&symfile_complaints,
12334 _(".debug_ranges entry has start address of zero"
12335 " [in module %s]"), objfile_name (objfile));
12339 callback (range_beginning, range_end);
12345 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12346 Return 1 if the attributes are present and valid, otherwise, return 0.
12347 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12350 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12351 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12352 struct partial_symtab *ranges_pst)
12354 struct objfile *objfile = cu->objfile;
12355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12356 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12357 SECT_OFF_TEXT (objfile));
12360 CORE_ADDR high = 0;
12363 retval = dwarf2_ranges_process (offset, cu,
12364 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12366 if (ranges_pst != NULL)
12371 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12372 range_beginning + baseaddr);
12373 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12374 range_end + baseaddr);
12375 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12379 /* FIXME: This is recording everything as a low-high
12380 segment of consecutive addresses. We should have a
12381 data structure for discontiguous block ranges
12385 low = range_beginning;
12391 if (range_beginning < low)
12392 low = range_beginning;
12393 if (range_end > high)
12401 /* If the first entry is an end-of-list marker, the range
12402 describes an empty scope, i.e. no instructions. */
12408 *high_return = high;
12412 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12413 definition for the return value. *LOWPC and *HIGHPC are set iff
12414 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12416 static enum pc_bounds_kind
12417 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12418 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12419 struct partial_symtab *pst)
12421 struct attribute *attr;
12422 struct attribute *attr_high;
12424 CORE_ADDR high = 0;
12425 enum pc_bounds_kind ret;
12427 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12430 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12433 low = attr_value_as_address (attr);
12434 high = attr_value_as_address (attr_high);
12435 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12439 /* Found high w/o low attribute. */
12440 return PC_BOUNDS_INVALID;
12442 /* Found consecutive range of addresses. */
12443 ret = PC_BOUNDS_HIGH_LOW;
12447 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12450 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12451 We take advantage of the fact that DW_AT_ranges does not appear
12452 in DW_TAG_compile_unit of DWO files. */
12453 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12454 unsigned int ranges_offset = (DW_UNSND (attr)
12455 + (need_ranges_base
12459 /* Value of the DW_AT_ranges attribute is the offset in the
12460 .debug_ranges section. */
12461 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12462 return PC_BOUNDS_INVALID;
12463 /* Found discontinuous range of addresses. */
12464 ret = PC_BOUNDS_RANGES;
12467 return PC_BOUNDS_NOT_PRESENT;
12470 /* read_partial_die has also the strict LOW < HIGH requirement. */
12472 return PC_BOUNDS_INVALID;
12474 /* When using the GNU linker, .gnu.linkonce. sections are used to
12475 eliminate duplicate copies of functions and vtables and such.
12476 The linker will arbitrarily choose one and discard the others.
12477 The AT_*_pc values for such functions refer to local labels in
12478 these sections. If the section from that file was discarded, the
12479 labels are not in the output, so the relocs get a value of 0.
12480 If this is a discarded function, mark the pc bounds as invalid,
12481 so that GDB will ignore it. */
12482 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12483 return PC_BOUNDS_INVALID;
12491 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12492 its low and high PC addresses. Do nothing if these addresses could not
12493 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12494 and HIGHPC to the high address if greater than HIGHPC. */
12497 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12498 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12499 struct dwarf2_cu *cu)
12501 CORE_ADDR low, high;
12502 struct die_info *child = die->child;
12504 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12506 *lowpc = std::min (*lowpc, low);
12507 *highpc = std::max (*highpc, high);
12510 /* If the language does not allow nested subprograms (either inside
12511 subprograms or lexical blocks), we're done. */
12512 if (cu->language != language_ada)
12515 /* Check all the children of the given DIE. If it contains nested
12516 subprograms, then check their pc bounds. Likewise, we need to
12517 check lexical blocks as well, as they may also contain subprogram
12519 while (child && child->tag)
12521 if (child->tag == DW_TAG_subprogram
12522 || child->tag == DW_TAG_lexical_block)
12523 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12524 child = sibling_die (child);
12528 /* Get the low and high pc's represented by the scope DIE, and store
12529 them in *LOWPC and *HIGHPC. If the correct values can't be
12530 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12533 get_scope_pc_bounds (struct die_info *die,
12534 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12535 struct dwarf2_cu *cu)
12537 CORE_ADDR best_low = (CORE_ADDR) -1;
12538 CORE_ADDR best_high = (CORE_ADDR) 0;
12539 CORE_ADDR current_low, current_high;
12541 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
12542 >= PC_BOUNDS_RANGES)
12544 best_low = current_low;
12545 best_high = current_high;
12549 struct die_info *child = die->child;
12551 while (child && child->tag)
12553 switch (child->tag) {
12554 case DW_TAG_subprogram:
12555 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12557 case DW_TAG_namespace:
12558 case DW_TAG_module:
12559 /* FIXME: carlton/2004-01-16: Should we do this for
12560 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12561 that current GCC's always emit the DIEs corresponding
12562 to definitions of methods of classes as children of a
12563 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12564 the DIEs giving the declarations, which could be
12565 anywhere). But I don't see any reason why the
12566 standards says that they have to be there. */
12567 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
12569 if (current_low != ((CORE_ADDR) -1))
12571 best_low = std::min (best_low, current_low);
12572 best_high = std::max (best_high, current_high);
12580 child = sibling_die (child);
12585 *highpc = best_high;
12588 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12592 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12593 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12595 struct objfile *objfile = cu->objfile;
12596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12597 struct attribute *attr;
12598 struct attribute *attr_high;
12600 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12603 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12606 CORE_ADDR low = attr_value_as_address (attr);
12607 CORE_ADDR high = attr_value_as_address (attr_high);
12609 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12612 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12613 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12614 record_block_range (block, low, high - 1);
12618 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12621 bfd *obfd = objfile->obfd;
12622 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12623 We take advantage of the fact that DW_AT_ranges does not appear
12624 in DW_TAG_compile_unit of DWO files. */
12625 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12627 /* The value of the DW_AT_ranges attribute is the offset of the
12628 address range list in the .debug_ranges section. */
12629 unsigned long offset = (DW_UNSND (attr)
12630 + (need_ranges_base ? cu->ranges_base : 0));
12631 const gdb_byte *buffer;
12633 /* For some target architectures, but not others, the
12634 read_address function sign-extends the addresses it returns.
12635 To recognize base address selection entries, we need a
12637 unsigned int addr_size = cu->header.addr_size;
12638 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12640 /* The base address, to which the next pair is relative. Note
12641 that this 'base' is a DWARF concept: most entries in a range
12642 list are relative, to reduce the number of relocs against the
12643 debugging information. This is separate from this function's
12644 'baseaddr' argument, which GDB uses to relocate debugging
12645 information from a shared library based on the address at
12646 which the library was loaded. */
12647 CORE_ADDR base = cu->base_address;
12648 int base_known = cu->base_known;
12650 dwarf2_ranges_process (offset, cu,
12651 [&] (CORE_ADDR start, CORE_ADDR end)
12655 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12656 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12657 record_block_range (block, start, end - 1);
12662 /* Check whether the producer field indicates either of GCC < 4.6, or the
12663 Intel C/C++ compiler, and cache the result in CU. */
12666 check_producer (struct dwarf2_cu *cu)
12670 if (cu->producer == NULL)
12672 /* For unknown compilers expect their behavior is DWARF version
12675 GCC started to support .debug_types sections by -gdwarf-4 since
12676 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12677 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12678 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12679 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12681 else if (producer_is_gcc (cu->producer, &major, &minor))
12683 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12684 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12686 else if (startswith (cu->producer, "Intel(R) C"))
12687 cu->producer_is_icc = 1;
12690 /* For other non-GCC compilers, expect their behavior is DWARF version
12694 cu->checked_producer = 1;
12697 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12698 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12699 during 4.6.0 experimental. */
12702 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12704 if (!cu->checked_producer)
12705 check_producer (cu);
12707 return cu->producer_is_gxx_lt_4_6;
12710 /* Return the default accessibility type if it is not overriden by
12711 DW_AT_accessibility. */
12713 static enum dwarf_access_attribute
12714 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12716 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12718 /* The default DWARF 2 accessibility for members is public, the default
12719 accessibility for inheritance is private. */
12721 if (die->tag != DW_TAG_inheritance)
12722 return DW_ACCESS_public;
12724 return DW_ACCESS_private;
12728 /* DWARF 3+ defines the default accessibility a different way. The same
12729 rules apply now for DW_TAG_inheritance as for the members and it only
12730 depends on the container kind. */
12732 if (die->parent->tag == DW_TAG_class_type)
12733 return DW_ACCESS_private;
12735 return DW_ACCESS_public;
12739 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12740 offset. If the attribute was not found return 0, otherwise return
12741 1. If it was found but could not properly be handled, set *OFFSET
12745 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12748 struct attribute *attr;
12750 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12755 /* Note that we do not check for a section offset first here.
12756 This is because DW_AT_data_member_location is new in DWARF 4,
12757 so if we see it, we can assume that a constant form is really
12758 a constant and not a section offset. */
12759 if (attr_form_is_constant (attr))
12760 *offset = dwarf2_get_attr_constant_value (attr, 0);
12761 else if (attr_form_is_section_offset (attr))
12762 dwarf2_complex_location_expr_complaint ();
12763 else if (attr_form_is_block (attr))
12764 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12766 dwarf2_complex_location_expr_complaint ();
12774 /* Add an aggregate field to the field list. */
12777 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12778 struct dwarf2_cu *cu)
12780 struct objfile *objfile = cu->objfile;
12781 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12782 struct nextfield *new_field;
12783 struct attribute *attr;
12785 const char *fieldname = "";
12787 /* Allocate a new field list entry and link it in. */
12788 new_field = XNEW (struct nextfield);
12789 make_cleanup (xfree, new_field);
12790 memset (new_field, 0, sizeof (struct nextfield));
12792 if (die->tag == DW_TAG_inheritance)
12794 new_field->next = fip->baseclasses;
12795 fip->baseclasses = new_field;
12799 new_field->next = fip->fields;
12800 fip->fields = new_field;
12804 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12806 new_field->accessibility = DW_UNSND (attr);
12808 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12809 if (new_field->accessibility != DW_ACCESS_public)
12810 fip->non_public_fields = 1;
12812 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12814 new_field->virtuality = DW_UNSND (attr);
12816 new_field->virtuality = DW_VIRTUALITY_none;
12818 fp = &new_field->field;
12820 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12824 /* Data member other than a C++ static data member. */
12826 /* Get type of field. */
12827 fp->type = die_type (die, cu);
12829 SET_FIELD_BITPOS (*fp, 0);
12831 /* Get bit size of field (zero if none). */
12832 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12835 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12839 FIELD_BITSIZE (*fp) = 0;
12842 /* Get bit offset of field. */
12843 if (handle_data_member_location (die, cu, &offset))
12844 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12845 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12848 if (gdbarch_bits_big_endian (gdbarch))
12850 /* For big endian bits, the DW_AT_bit_offset gives the
12851 additional bit offset from the MSB of the containing
12852 anonymous object to the MSB of the field. We don't
12853 have to do anything special since we don't need to
12854 know the size of the anonymous object. */
12855 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12859 /* For little endian bits, compute the bit offset to the
12860 MSB of the anonymous object, subtract off the number of
12861 bits from the MSB of the field to the MSB of the
12862 object, and then subtract off the number of bits of
12863 the field itself. The result is the bit offset of
12864 the LSB of the field. */
12865 int anonymous_size;
12866 int bit_offset = DW_UNSND (attr);
12868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12871 /* The size of the anonymous object containing
12872 the bit field is explicit, so use the
12873 indicated size (in bytes). */
12874 anonymous_size = DW_UNSND (attr);
12878 /* The size of the anonymous object containing
12879 the bit field must be inferred from the type
12880 attribute of the data member containing the
12882 anonymous_size = TYPE_LENGTH (fp->type);
12884 SET_FIELD_BITPOS (*fp,
12885 (FIELD_BITPOS (*fp)
12886 + anonymous_size * bits_per_byte
12887 - bit_offset - FIELD_BITSIZE (*fp)));
12890 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12892 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12893 + dwarf2_get_attr_constant_value (attr, 0)));
12895 /* Get name of field. */
12896 fieldname = dwarf2_name (die, cu);
12897 if (fieldname == NULL)
12900 /* The name is already allocated along with this objfile, so we don't
12901 need to duplicate it for the type. */
12902 fp->name = fieldname;
12904 /* Change accessibility for artificial fields (e.g. virtual table
12905 pointer or virtual base class pointer) to private. */
12906 if (dwarf2_attr (die, DW_AT_artificial, cu))
12908 FIELD_ARTIFICIAL (*fp) = 1;
12909 new_field->accessibility = DW_ACCESS_private;
12910 fip->non_public_fields = 1;
12913 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12915 /* C++ static member. */
12917 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12918 is a declaration, but all versions of G++ as of this writing
12919 (so through at least 3.2.1) incorrectly generate
12920 DW_TAG_variable tags. */
12922 const char *physname;
12924 /* Get name of field. */
12925 fieldname = dwarf2_name (die, cu);
12926 if (fieldname == NULL)
12929 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12931 /* Only create a symbol if this is an external value.
12932 new_symbol checks this and puts the value in the global symbol
12933 table, which we want. If it is not external, new_symbol
12934 will try to put the value in cu->list_in_scope which is wrong. */
12935 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12937 /* A static const member, not much different than an enum as far as
12938 we're concerned, except that we can support more types. */
12939 new_symbol (die, NULL, cu);
12942 /* Get physical name. */
12943 physname = dwarf2_physname (fieldname, die, cu);
12945 /* The name is already allocated along with this objfile, so we don't
12946 need to duplicate it for the type. */
12947 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12948 FIELD_TYPE (*fp) = die_type (die, cu);
12949 FIELD_NAME (*fp) = fieldname;
12951 else if (die->tag == DW_TAG_inheritance)
12955 /* C++ base class field. */
12956 if (handle_data_member_location (die, cu, &offset))
12957 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12958 FIELD_BITSIZE (*fp) = 0;
12959 FIELD_TYPE (*fp) = die_type (die, cu);
12960 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12961 fip->nbaseclasses++;
12965 /* Add a typedef defined in the scope of the FIP's class. */
12968 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12969 struct dwarf2_cu *cu)
12971 struct typedef_field_list *new_field;
12972 struct typedef_field *fp;
12974 /* Allocate a new field list entry and link it in. */
12975 new_field = XCNEW (struct typedef_field_list);
12976 make_cleanup (xfree, new_field);
12978 gdb_assert (die->tag == DW_TAG_typedef);
12980 fp = &new_field->field;
12982 /* Get name of field. */
12983 fp->name = dwarf2_name (die, cu);
12984 if (fp->name == NULL)
12987 fp->type = read_type_die (die, cu);
12989 new_field->next = fip->typedef_field_list;
12990 fip->typedef_field_list = new_field;
12991 fip->typedef_field_list_count++;
12994 /* Create the vector of fields, and attach it to the type. */
12997 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12998 struct dwarf2_cu *cu)
13000 int nfields = fip->nfields;
13002 /* Record the field count, allocate space for the array of fields,
13003 and create blank accessibility bitfields if necessary. */
13004 TYPE_NFIELDS (type) = nfields;
13005 TYPE_FIELDS (type) = (struct field *)
13006 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13007 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13009 if (fip->non_public_fields && cu->language != language_ada)
13011 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13013 TYPE_FIELD_PRIVATE_BITS (type) =
13014 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13015 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13017 TYPE_FIELD_PROTECTED_BITS (type) =
13018 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13019 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13021 TYPE_FIELD_IGNORE_BITS (type) =
13022 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13023 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13026 /* If the type has baseclasses, allocate and clear a bit vector for
13027 TYPE_FIELD_VIRTUAL_BITS. */
13028 if (fip->nbaseclasses && cu->language != language_ada)
13030 int num_bytes = B_BYTES (fip->nbaseclasses);
13031 unsigned char *pointer;
13033 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13034 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13035 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13036 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13037 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13040 /* Copy the saved-up fields into the field vector. Start from the head of
13041 the list, adding to the tail of the field array, so that they end up in
13042 the same order in the array in which they were added to the list. */
13043 while (nfields-- > 0)
13045 struct nextfield *fieldp;
13049 fieldp = fip->fields;
13050 fip->fields = fieldp->next;
13054 fieldp = fip->baseclasses;
13055 fip->baseclasses = fieldp->next;
13058 TYPE_FIELD (type, nfields) = fieldp->field;
13059 switch (fieldp->accessibility)
13061 case DW_ACCESS_private:
13062 if (cu->language != language_ada)
13063 SET_TYPE_FIELD_PRIVATE (type, nfields);
13066 case DW_ACCESS_protected:
13067 if (cu->language != language_ada)
13068 SET_TYPE_FIELD_PROTECTED (type, nfields);
13071 case DW_ACCESS_public:
13075 /* Unknown accessibility. Complain and treat it as public. */
13077 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13078 fieldp->accessibility);
13082 if (nfields < fip->nbaseclasses)
13084 switch (fieldp->virtuality)
13086 case DW_VIRTUALITY_virtual:
13087 case DW_VIRTUALITY_pure_virtual:
13088 if (cu->language == language_ada)
13089 error (_("unexpected virtuality in component of Ada type"));
13090 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13097 /* Return true if this member function is a constructor, false
13101 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13103 const char *fieldname;
13104 const char *type_name;
13107 if (die->parent == NULL)
13110 if (die->parent->tag != DW_TAG_structure_type
13111 && die->parent->tag != DW_TAG_union_type
13112 && die->parent->tag != DW_TAG_class_type)
13115 fieldname = dwarf2_name (die, cu);
13116 type_name = dwarf2_name (die->parent, cu);
13117 if (fieldname == NULL || type_name == NULL)
13120 len = strlen (fieldname);
13121 return (strncmp (fieldname, type_name, len) == 0
13122 && (type_name[len] == '\0' || type_name[len] == '<'));
13125 /* Add a member function to the proper fieldlist. */
13128 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13129 struct type *type, struct dwarf2_cu *cu)
13131 struct objfile *objfile = cu->objfile;
13132 struct attribute *attr;
13133 struct fnfieldlist *flp;
13135 struct fn_field *fnp;
13136 const char *fieldname;
13137 struct nextfnfield *new_fnfield;
13138 struct type *this_type;
13139 enum dwarf_access_attribute accessibility;
13141 if (cu->language == language_ada)
13142 error (_("unexpected member function in Ada type"));
13144 /* Get name of member function. */
13145 fieldname = dwarf2_name (die, cu);
13146 if (fieldname == NULL)
13149 /* Look up member function name in fieldlist. */
13150 for (i = 0; i < fip->nfnfields; i++)
13152 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13156 /* Create new list element if necessary. */
13157 if (i < fip->nfnfields)
13158 flp = &fip->fnfieldlists[i];
13161 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13163 fip->fnfieldlists = (struct fnfieldlist *)
13164 xrealloc (fip->fnfieldlists,
13165 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13166 * sizeof (struct fnfieldlist));
13167 if (fip->nfnfields == 0)
13168 make_cleanup (free_current_contents, &fip->fnfieldlists);
13170 flp = &fip->fnfieldlists[fip->nfnfields];
13171 flp->name = fieldname;
13174 i = fip->nfnfields++;
13177 /* Create a new member function field and chain it to the field list
13179 new_fnfield = XNEW (struct nextfnfield);
13180 make_cleanup (xfree, new_fnfield);
13181 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13182 new_fnfield->next = flp->head;
13183 flp->head = new_fnfield;
13186 /* Fill in the member function field info. */
13187 fnp = &new_fnfield->fnfield;
13189 /* Delay processing of the physname until later. */
13190 if (cu->language == language_cplus)
13192 add_to_method_list (type, i, flp->length - 1, fieldname,
13197 const char *physname = dwarf2_physname (fieldname, die, cu);
13198 fnp->physname = physname ? physname : "";
13201 fnp->type = alloc_type (objfile);
13202 this_type = read_type_die (die, cu);
13203 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13205 int nparams = TYPE_NFIELDS (this_type);
13207 /* TYPE is the domain of this method, and THIS_TYPE is the type
13208 of the method itself (TYPE_CODE_METHOD). */
13209 smash_to_method_type (fnp->type, type,
13210 TYPE_TARGET_TYPE (this_type),
13211 TYPE_FIELDS (this_type),
13212 TYPE_NFIELDS (this_type),
13213 TYPE_VARARGS (this_type));
13215 /* Handle static member functions.
13216 Dwarf2 has no clean way to discern C++ static and non-static
13217 member functions. G++ helps GDB by marking the first
13218 parameter for non-static member functions (which is the this
13219 pointer) as artificial. We obtain this information from
13220 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13221 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13222 fnp->voffset = VOFFSET_STATIC;
13225 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13226 dwarf2_full_name (fieldname, die, cu));
13228 /* Get fcontext from DW_AT_containing_type if present. */
13229 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13230 fnp->fcontext = die_containing_type (die, cu);
13232 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13233 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13235 /* Get accessibility. */
13236 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13238 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13240 accessibility = dwarf2_default_access_attribute (die, cu);
13241 switch (accessibility)
13243 case DW_ACCESS_private:
13244 fnp->is_private = 1;
13246 case DW_ACCESS_protected:
13247 fnp->is_protected = 1;
13251 /* Check for artificial methods. */
13252 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13253 if (attr && DW_UNSND (attr) != 0)
13254 fnp->is_artificial = 1;
13256 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13258 /* Get index in virtual function table if it is a virtual member
13259 function. For older versions of GCC, this is an offset in the
13260 appropriate virtual table, as specified by DW_AT_containing_type.
13261 For everyone else, it is an expression to be evaluated relative
13262 to the object address. */
13264 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13267 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13269 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13271 /* Old-style GCC. */
13272 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13274 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13275 || (DW_BLOCK (attr)->size > 1
13276 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13277 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13279 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13280 if ((fnp->voffset % cu->header.addr_size) != 0)
13281 dwarf2_complex_location_expr_complaint ();
13283 fnp->voffset /= cu->header.addr_size;
13287 dwarf2_complex_location_expr_complaint ();
13289 if (!fnp->fcontext)
13291 /* If there is no `this' field and no DW_AT_containing_type,
13292 we cannot actually find a base class context for the
13294 if (TYPE_NFIELDS (this_type) == 0
13295 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13297 complaint (&symfile_complaints,
13298 _("cannot determine context for virtual member "
13299 "function \"%s\" (offset %d)"),
13300 fieldname, to_underlying (die->sect_off));
13305 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13309 else if (attr_form_is_section_offset (attr))
13311 dwarf2_complex_location_expr_complaint ();
13315 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13321 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13322 if (attr && DW_UNSND (attr))
13324 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13325 complaint (&symfile_complaints,
13326 _("Member function \"%s\" (offset %d) is virtual "
13327 "but the vtable offset is not specified"),
13328 fieldname, to_underlying (die->sect_off));
13329 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13330 TYPE_CPLUS_DYNAMIC (type) = 1;
13335 /* Create the vector of member function fields, and attach it to the type. */
13338 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13339 struct dwarf2_cu *cu)
13341 struct fnfieldlist *flp;
13344 if (cu->language == language_ada)
13345 error (_("unexpected member functions in Ada type"));
13347 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13348 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13349 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13351 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13353 struct nextfnfield *nfp = flp->head;
13354 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13357 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13358 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13359 fn_flp->fn_fields = (struct fn_field *)
13360 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13361 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13362 fn_flp->fn_fields[k] = nfp->fnfield;
13365 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13368 /* Returns non-zero if NAME is the name of a vtable member in CU's
13369 language, zero otherwise. */
13371 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13373 static const char vptr[] = "_vptr";
13374 static const char vtable[] = "vtable";
13376 /* Look for the C++ form of the vtable. */
13377 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13383 /* GCC outputs unnamed structures that are really pointers to member
13384 functions, with the ABI-specified layout. If TYPE describes
13385 such a structure, smash it into a member function type.
13387 GCC shouldn't do this; it should just output pointer to member DIEs.
13388 This is GCC PR debug/28767. */
13391 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13393 struct type *pfn_type, *self_type, *new_type;
13395 /* Check for a structure with no name and two children. */
13396 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13399 /* Check for __pfn and __delta members. */
13400 if (TYPE_FIELD_NAME (type, 0) == NULL
13401 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13402 || TYPE_FIELD_NAME (type, 1) == NULL
13403 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13406 /* Find the type of the method. */
13407 pfn_type = TYPE_FIELD_TYPE (type, 0);
13408 if (pfn_type == NULL
13409 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13410 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13413 /* Look for the "this" argument. */
13414 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13415 if (TYPE_NFIELDS (pfn_type) == 0
13416 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13417 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13420 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13421 new_type = alloc_type (objfile);
13422 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13423 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13424 TYPE_VARARGS (pfn_type));
13425 smash_to_methodptr_type (type, new_type);
13428 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13432 producer_is_icc (struct dwarf2_cu *cu)
13434 if (!cu->checked_producer)
13435 check_producer (cu);
13437 return cu->producer_is_icc;
13440 /* Called when we find the DIE that starts a structure or union scope
13441 (definition) to create a type for the structure or union. Fill in
13442 the type's name and general properties; the members will not be
13443 processed until process_structure_scope. A symbol table entry for
13444 the type will also not be done until process_structure_scope (assuming
13445 the type has a name).
13447 NOTE: we need to call these functions regardless of whether or not the
13448 DIE has a DW_AT_name attribute, since it might be an anonymous
13449 structure or union. This gets the type entered into our set of
13450 user defined types. */
13452 static struct type *
13453 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13455 struct objfile *objfile = cu->objfile;
13457 struct attribute *attr;
13460 /* If the definition of this type lives in .debug_types, read that type.
13461 Don't follow DW_AT_specification though, that will take us back up
13462 the chain and we want to go down. */
13463 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13466 type = get_DW_AT_signature_type (die, attr, cu);
13468 /* The type's CU may not be the same as CU.
13469 Ensure TYPE is recorded with CU in die_type_hash. */
13470 return set_die_type (die, type, cu);
13473 type = alloc_type (objfile);
13474 INIT_CPLUS_SPECIFIC (type);
13476 name = dwarf2_name (die, cu);
13479 if (cu->language == language_cplus
13480 || cu->language == language_d
13481 || cu->language == language_rust)
13483 const char *full_name = dwarf2_full_name (name, die, cu);
13485 /* dwarf2_full_name might have already finished building the DIE's
13486 type. If so, there is no need to continue. */
13487 if (get_die_type (die, cu) != NULL)
13488 return get_die_type (die, cu);
13490 TYPE_TAG_NAME (type) = full_name;
13491 if (die->tag == DW_TAG_structure_type
13492 || die->tag == DW_TAG_class_type)
13493 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13497 /* The name is already allocated along with this objfile, so
13498 we don't need to duplicate it for the type. */
13499 TYPE_TAG_NAME (type) = name;
13500 if (die->tag == DW_TAG_class_type)
13501 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13505 if (die->tag == DW_TAG_structure_type)
13507 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13509 else if (die->tag == DW_TAG_union_type)
13511 TYPE_CODE (type) = TYPE_CODE_UNION;
13515 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13518 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13519 TYPE_DECLARED_CLASS (type) = 1;
13521 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13524 if (attr_form_is_constant (attr))
13525 TYPE_LENGTH (type) = DW_UNSND (attr);
13528 /* For the moment, dynamic type sizes are not supported
13529 by GDB's struct type. The actual size is determined
13530 on-demand when resolving the type of a given object,
13531 so set the type's length to zero for now. Otherwise,
13532 we record an expression as the length, and that expression
13533 could lead to a very large value, which could eventually
13534 lead to us trying to allocate that much memory when creating
13535 a value of that type. */
13536 TYPE_LENGTH (type) = 0;
13541 TYPE_LENGTH (type) = 0;
13544 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13546 /* ICC does not output the required DW_AT_declaration
13547 on incomplete types, but gives them a size of zero. */
13548 TYPE_STUB (type) = 1;
13551 TYPE_STUB_SUPPORTED (type) = 1;
13553 if (die_is_declaration (die, cu))
13554 TYPE_STUB (type) = 1;
13555 else if (attr == NULL && die->child == NULL
13556 && producer_is_realview (cu->producer))
13557 /* RealView does not output the required DW_AT_declaration
13558 on incomplete types. */
13559 TYPE_STUB (type) = 1;
13561 /* We need to add the type field to the die immediately so we don't
13562 infinitely recurse when dealing with pointers to the structure
13563 type within the structure itself. */
13564 set_die_type (die, type, cu);
13566 /* set_die_type should be already done. */
13567 set_descriptive_type (type, die, cu);
13572 /* Finish creating a structure or union type, including filling in
13573 its members and creating a symbol for it. */
13576 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13578 struct objfile *objfile = cu->objfile;
13579 struct die_info *child_die;
13582 type = get_die_type (die, cu);
13584 type = read_structure_type (die, cu);
13586 if (die->child != NULL && ! die_is_declaration (die, cu))
13588 struct field_info fi;
13589 VEC (symbolp) *template_args = NULL;
13590 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13592 memset (&fi, 0, sizeof (struct field_info));
13594 child_die = die->child;
13596 while (child_die && child_die->tag)
13598 if (child_die->tag == DW_TAG_member
13599 || child_die->tag == DW_TAG_variable)
13601 /* NOTE: carlton/2002-11-05: A C++ static data member
13602 should be a DW_TAG_member that is a declaration, but
13603 all versions of G++ as of this writing (so through at
13604 least 3.2.1) incorrectly generate DW_TAG_variable
13605 tags for them instead. */
13606 dwarf2_add_field (&fi, child_die, cu);
13608 else if (child_die->tag == DW_TAG_subprogram)
13610 /* Rust doesn't have member functions in the C++ sense.
13611 However, it does emit ordinary functions as children
13612 of a struct DIE. */
13613 if (cu->language == language_rust)
13614 read_func_scope (child_die, cu);
13617 /* C++ member function. */
13618 dwarf2_add_member_fn (&fi, child_die, type, cu);
13621 else if (child_die->tag == DW_TAG_inheritance)
13623 /* C++ base class field. */
13624 dwarf2_add_field (&fi, child_die, cu);
13626 else if (child_die->tag == DW_TAG_typedef)
13627 dwarf2_add_typedef (&fi, child_die, cu);
13628 else if (child_die->tag == DW_TAG_template_type_param
13629 || child_die->tag == DW_TAG_template_value_param)
13631 struct symbol *arg = new_symbol (child_die, NULL, cu);
13634 VEC_safe_push (symbolp, template_args, arg);
13637 child_die = sibling_die (child_die);
13640 /* Attach template arguments to type. */
13641 if (! VEC_empty (symbolp, template_args))
13643 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13644 TYPE_N_TEMPLATE_ARGUMENTS (type)
13645 = VEC_length (symbolp, template_args);
13646 TYPE_TEMPLATE_ARGUMENTS (type)
13647 = XOBNEWVEC (&objfile->objfile_obstack,
13649 TYPE_N_TEMPLATE_ARGUMENTS (type));
13650 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13651 VEC_address (symbolp, template_args),
13652 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13653 * sizeof (struct symbol *)));
13654 VEC_free (symbolp, template_args);
13657 /* Attach fields and member functions to the type. */
13659 dwarf2_attach_fields_to_type (&fi, type, cu);
13662 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13664 /* Get the type which refers to the base class (possibly this
13665 class itself) which contains the vtable pointer for the current
13666 class from the DW_AT_containing_type attribute. This use of
13667 DW_AT_containing_type is a GNU extension. */
13669 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13671 struct type *t = die_containing_type (die, cu);
13673 set_type_vptr_basetype (type, t);
13678 /* Our own class provides vtbl ptr. */
13679 for (i = TYPE_NFIELDS (t) - 1;
13680 i >= TYPE_N_BASECLASSES (t);
13683 const char *fieldname = TYPE_FIELD_NAME (t, i);
13685 if (is_vtable_name (fieldname, cu))
13687 set_type_vptr_fieldno (type, i);
13692 /* Complain if virtual function table field not found. */
13693 if (i < TYPE_N_BASECLASSES (t))
13694 complaint (&symfile_complaints,
13695 _("virtual function table pointer "
13696 "not found when defining class '%s'"),
13697 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13702 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13705 else if (cu->producer
13706 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13708 /* The IBM XLC compiler does not provide direct indication
13709 of the containing type, but the vtable pointer is
13710 always named __vfp. */
13714 for (i = TYPE_NFIELDS (type) - 1;
13715 i >= TYPE_N_BASECLASSES (type);
13718 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13720 set_type_vptr_fieldno (type, i);
13721 set_type_vptr_basetype (type, type);
13728 /* Copy fi.typedef_field_list linked list elements content into the
13729 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13730 if (fi.typedef_field_list)
13732 int i = fi.typedef_field_list_count;
13734 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13735 TYPE_TYPEDEF_FIELD_ARRAY (type)
13736 = ((struct typedef_field *)
13737 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13738 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13740 /* Reverse the list order to keep the debug info elements order. */
13743 struct typedef_field *dest, *src;
13745 dest = &TYPE_TYPEDEF_FIELD (type, i);
13746 src = &fi.typedef_field_list->field;
13747 fi.typedef_field_list = fi.typedef_field_list->next;
13752 do_cleanups (back_to);
13755 quirk_gcc_member_function_pointer (type, objfile);
13757 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13758 snapshots) has been known to create a die giving a declaration
13759 for a class that has, as a child, a die giving a definition for a
13760 nested class. So we have to process our children even if the
13761 current die is a declaration. Normally, of course, a declaration
13762 won't have any children at all. */
13764 child_die = die->child;
13766 while (child_die != NULL && child_die->tag)
13768 if (child_die->tag == DW_TAG_member
13769 || child_die->tag == DW_TAG_variable
13770 || child_die->tag == DW_TAG_inheritance
13771 || child_die->tag == DW_TAG_template_value_param
13772 || child_die->tag == DW_TAG_template_type_param)
13777 process_die (child_die, cu);
13779 child_die = sibling_die (child_die);
13782 /* Do not consider external references. According to the DWARF standard,
13783 these DIEs are identified by the fact that they have no byte_size
13784 attribute, and a declaration attribute. */
13785 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13786 || !die_is_declaration (die, cu))
13787 new_symbol (die, type, cu);
13790 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13791 update TYPE using some information only available in DIE's children. */
13794 update_enumeration_type_from_children (struct die_info *die,
13796 struct dwarf2_cu *cu)
13798 struct obstack obstack;
13799 struct die_info *child_die;
13800 int unsigned_enum = 1;
13803 struct cleanup *old_chain;
13805 obstack_init (&obstack);
13806 old_chain = make_cleanup_obstack_free (&obstack);
13808 for (child_die = die->child;
13809 child_die != NULL && child_die->tag;
13810 child_die = sibling_die (child_die))
13812 struct attribute *attr;
13814 const gdb_byte *bytes;
13815 struct dwarf2_locexpr_baton *baton;
13818 if (child_die->tag != DW_TAG_enumerator)
13821 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13825 name = dwarf2_name (child_die, cu);
13827 name = "<anonymous enumerator>";
13829 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13830 &value, &bytes, &baton);
13836 else if ((mask & value) != 0)
13841 /* If we already know that the enum type is neither unsigned, nor
13842 a flag type, no need to look at the rest of the enumerates. */
13843 if (!unsigned_enum && !flag_enum)
13848 TYPE_UNSIGNED (type) = 1;
13850 TYPE_FLAG_ENUM (type) = 1;
13852 do_cleanups (old_chain);
13855 /* Given a DW_AT_enumeration_type die, set its type. We do not
13856 complete the type's fields yet, or create any symbols. */
13858 static struct type *
13859 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13861 struct objfile *objfile = cu->objfile;
13863 struct attribute *attr;
13866 /* If the definition of this type lives in .debug_types, read that type.
13867 Don't follow DW_AT_specification though, that will take us back up
13868 the chain and we want to go down. */
13869 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13872 type = get_DW_AT_signature_type (die, attr, cu);
13874 /* The type's CU may not be the same as CU.
13875 Ensure TYPE is recorded with CU in die_type_hash. */
13876 return set_die_type (die, type, cu);
13879 type = alloc_type (objfile);
13881 TYPE_CODE (type) = TYPE_CODE_ENUM;
13882 name = dwarf2_full_name (NULL, die, cu);
13884 TYPE_TAG_NAME (type) = name;
13886 attr = dwarf2_attr (die, DW_AT_type, cu);
13889 struct type *underlying_type = die_type (die, cu);
13891 TYPE_TARGET_TYPE (type) = underlying_type;
13894 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13897 TYPE_LENGTH (type) = DW_UNSND (attr);
13901 TYPE_LENGTH (type) = 0;
13904 /* The enumeration DIE can be incomplete. In Ada, any type can be
13905 declared as private in the package spec, and then defined only
13906 inside the package body. Such types are known as Taft Amendment
13907 Types. When another package uses such a type, an incomplete DIE
13908 may be generated by the compiler. */
13909 if (die_is_declaration (die, cu))
13910 TYPE_STUB (type) = 1;
13912 /* Finish the creation of this type by using the enum's children.
13913 We must call this even when the underlying type has been provided
13914 so that we can determine if we're looking at a "flag" enum. */
13915 update_enumeration_type_from_children (die, type, cu);
13917 /* If this type has an underlying type that is not a stub, then we
13918 may use its attributes. We always use the "unsigned" attribute
13919 in this situation, because ordinarily we guess whether the type
13920 is unsigned -- but the guess can be wrong and the underlying type
13921 can tell us the reality. However, we defer to a local size
13922 attribute if one exists, because this lets the compiler override
13923 the underlying type if needed. */
13924 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13926 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13927 if (TYPE_LENGTH (type) == 0)
13928 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13931 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13933 return set_die_type (die, type, cu);
13936 /* Given a pointer to a die which begins an enumeration, process all
13937 the dies that define the members of the enumeration, and create the
13938 symbol for the enumeration type.
13940 NOTE: We reverse the order of the element list. */
13943 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13945 struct type *this_type;
13947 this_type = get_die_type (die, cu);
13948 if (this_type == NULL)
13949 this_type = read_enumeration_type (die, cu);
13951 if (die->child != NULL)
13953 struct die_info *child_die;
13954 struct symbol *sym;
13955 struct field *fields = NULL;
13956 int num_fields = 0;
13959 child_die = die->child;
13960 while (child_die && child_die->tag)
13962 if (child_die->tag != DW_TAG_enumerator)
13964 process_die (child_die, cu);
13968 name = dwarf2_name (child_die, cu);
13971 sym = new_symbol (child_die, this_type, cu);
13973 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13975 fields = (struct field *)
13977 (num_fields + DW_FIELD_ALLOC_CHUNK)
13978 * sizeof (struct field));
13981 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13982 FIELD_TYPE (fields[num_fields]) = NULL;
13983 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13984 FIELD_BITSIZE (fields[num_fields]) = 0;
13990 child_die = sibling_die (child_die);
13995 TYPE_NFIELDS (this_type) = num_fields;
13996 TYPE_FIELDS (this_type) = (struct field *)
13997 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13998 memcpy (TYPE_FIELDS (this_type), fields,
13999 sizeof (struct field) * num_fields);
14004 /* If we are reading an enum from a .debug_types unit, and the enum
14005 is a declaration, and the enum is not the signatured type in the
14006 unit, then we do not want to add a symbol for it. Adding a
14007 symbol would in some cases obscure the true definition of the
14008 enum, giving users an incomplete type when the definition is
14009 actually available. Note that we do not want to do this for all
14010 enums which are just declarations, because C++0x allows forward
14011 enum declarations. */
14012 if (cu->per_cu->is_debug_types
14013 && die_is_declaration (die, cu))
14015 struct signatured_type *sig_type;
14017 sig_type = (struct signatured_type *) cu->per_cu;
14018 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14019 if (sig_type->type_offset_in_section != die->sect_off)
14023 new_symbol (die, this_type, cu);
14026 /* Extract all information from a DW_TAG_array_type DIE and put it in
14027 the DIE's type field. For now, this only handles one dimensional
14030 static struct type *
14031 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14033 struct objfile *objfile = cu->objfile;
14034 struct die_info *child_die;
14036 struct type *element_type, *range_type, *index_type;
14037 struct type **range_types = NULL;
14038 struct attribute *attr;
14040 struct cleanup *back_to;
14042 unsigned int bit_stride = 0;
14044 element_type = die_type (die, cu);
14046 /* The die_type call above may have already set the type for this DIE. */
14047 type = get_die_type (die, cu);
14051 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14053 bit_stride = DW_UNSND (attr) * 8;
14055 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14057 bit_stride = DW_UNSND (attr);
14059 /* Irix 6.2 native cc creates array types without children for
14060 arrays with unspecified length. */
14061 if (die->child == NULL)
14063 index_type = objfile_type (objfile)->builtin_int;
14064 range_type = create_static_range_type (NULL, index_type, 0, -1);
14065 type = create_array_type_with_stride (NULL, element_type, range_type,
14067 return set_die_type (die, type, cu);
14070 back_to = make_cleanup (null_cleanup, NULL);
14071 child_die = die->child;
14072 while (child_die && child_die->tag)
14074 if (child_die->tag == DW_TAG_subrange_type)
14076 struct type *child_type = read_type_die (child_die, cu);
14078 if (child_type != NULL)
14080 /* The range type was succesfully read. Save it for the
14081 array type creation. */
14082 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14084 range_types = (struct type **)
14085 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14086 * sizeof (struct type *));
14088 make_cleanup (free_current_contents, &range_types);
14090 range_types[ndim++] = child_type;
14093 child_die = sibling_die (child_die);
14096 /* Dwarf2 dimensions are output from left to right, create the
14097 necessary array types in backwards order. */
14099 type = element_type;
14101 if (read_array_order (die, cu) == DW_ORD_col_major)
14106 type = create_array_type_with_stride (NULL, type, range_types[i++],
14112 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14116 /* Understand Dwarf2 support for vector types (like they occur on
14117 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14118 array type. This is not part of the Dwarf2/3 standard yet, but a
14119 custom vendor extension. The main difference between a regular
14120 array and the vector variant is that vectors are passed by value
14122 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14124 make_vector_type (type);
14126 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14127 implementation may choose to implement triple vectors using this
14129 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14132 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14133 TYPE_LENGTH (type) = DW_UNSND (attr);
14135 complaint (&symfile_complaints,
14136 _("DW_AT_byte_size for array type smaller "
14137 "than the total size of elements"));
14140 name = dwarf2_name (die, cu);
14142 TYPE_NAME (type) = name;
14144 /* Install the type in the die. */
14145 set_die_type (die, type, cu);
14147 /* set_die_type should be already done. */
14148 set_descriptive_type (type, die, cu);
14150 do_cleanups (back_to);
14155 static enum dwarf_array_dim_ordering
14156 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14158 struct attribute *attr;
14160 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14163 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14165 /* GNU F77 is a special case, as at 08/2004 array type info is the
14166 opposite order to the dwarf2 specification, but data is still
14167 laid out as per normal fortran.
14169 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14170 version checking. */
14172 if (cu->language == language_fortran
14173 && cu->producer && strstr (cu->producer, "GNU F77"))
14175 return DW_ORD_row_major;
14178 switch (cu->language_defn->la_array_ordering)
14180 case array_column_major:
14181 return DW_ORD_col_major;
14182 case array_row_major:
14184 return DW_ORD_row_major;
14188 /* Extract all information from a DW_TAG_set_type DIE and put it in
14189 the DIE's type field. */
14191 static struct type *
14192 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14194 struct type *domain_type, *set_type;
14195 struct attribute *attr;
14197 domain_type = die_type (die, cu);
14199 /* The die_type call above may have already set the type for this DIE. */
14200 set_type = get_die_type (die, cu);
14204 set_type = create_set_type (NULL, domain_type);
14206 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14208 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14210 return set_die_type (die, set_type, cu);
14213 /* A helper for read_common_block that creates a locexpr baton.
14214 SYM is the symbol which we are marking as computed.
14215 COMMON_DIE is the DIE for the common block.
14216 COMMON_LOC is the location expression attribute for the common
14218 MEMBER_LOC is the location expression attribute for the particular
14219 member of the common block that we are processing.
14220 CU is the CU from which the above come. */
14223 mark_common_block_symbol_computed (struct symbol *sym,
14224 struct die_info *common_die,
14225 struct attribute *common_loc,
14226 struct attribute *member_loc,
14227 struct dwarf2_cu *cu)
14229 struct objfile *objfile = dwarf2_per_objfile->objfile;
14230 struct dwarf2_locexpr_baton *baton;
14232 unsigned int cu_off;
14233 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14234 LONGEST offset = 0;
14236 gdb_assert (common_loc && member_loc);
14237 gdb_assert (attr_form_is_block (common_loc));
14238 gdb_assert (attr_form_is_block (member_loc)
14239 || attr_form_is_constant (member_loc));
14241 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14242 baton->per_cu = cu->per_cu;
14243 gdb_assert (baton->per_cu);
14245 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14247 if (attr_form_is_constant (member_loc))
14249 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14250 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14253 baton->size += DW_BLOCK (member_loc)->size;
14255 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14258 *ptr++ = DW_OP_call4;
14259 cu_off = common_die->sect_off - cu->per_cu->sect_off;
14260 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14263 if (attr_form_is_constant (member_loc))
14265 *ptr++ = DW_OP_addr;
14266 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14267 ptr += cu->header.addr_size;
14271 /* We have to copy the data here, because DW_OP_call4 will only
14272 use a DW_AT_location attribute. */
14273 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14274 ptr += DW_BLOCK (member_loc)->size;
14277 *ptr++ = DW_OP_plus;
14278 gdb_assert (ptr - baton->data == baton->size);
14280 SYMBOL_LOCATION_BATON (sym) = baton;
14281 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14284 /* Create appropriate locally-scoped variables for all the
14285 DW_TAG_common_block entries. Also create a struct common_block
14286 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14287 is used to sepate the common blocks name namespace from regular
14291 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14293 struct attribute *attr;
14295 attr = dwarf2_attr (die, DW_AT_location, cu);
14298 /* Support the .debug_loc offsets. */
14299 if (attr_form_is_block (attr))
14303 else if (attr_form_is_section_offset (attr))
14305 dwarf2_complex_location_expr_complaint ();
14310 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14311 "common block member");
14316 if (die->child != NULL)
14318 struct objfile *objfile = cu->objfile;
14319 struct die_info *child_die;
14320 size_t n_entries = 0, size;
14321 struct common_block *common_block;
14322 struct symbol *sym;
14324 for (child_die = die->child;
14325 child_die && child_die->tag;
14326 child_die = sibling_die (child_die))
14329 size = (sizeof (struct common_block)
14330 + (n_entries - 1) * sizeof (struct symbol *));
14332 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14334 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14335 common_block->n_entries = 0;
14337 for (child_die = die->child;
14338 child_die && child_die->tag;
14339 child_die = sibling_die (child_die))
14341 /* Create the symbol in the DW_TAG_common_block block in the current
14343 sym = new_symbol (child_die, NULL, cu);
14346 struct attribute *member_loc;
14348 common_block->contents[common_block->n_entries++] = sym;
14350 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14354 /* GDB has handled this for a long time, but it is
14355 not specified by DWARF. It seems to have been
14356 emitted by gfortran at least as recently as:
14357 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14358 complaint (&symfile_complaints,
14359 _("Variable in common block has "
14360 "DW_AT_data_member_location "
14361 "- DIE at 0x%x [in module %s]"),
14362 to_underlying (child_die->sect_off),
14363 objfile_name (cu->objfile));
14365 if (attr_form_is_section_offset (member_loc))
14366 dwarf2_complex_location_expr_complaint ();
14367 else if (attr_form_is_constant (member_loc)
14368 || attr_form_is_block (member_loc))
14371 mark_common_block_symbol_computed (sym, die, attr,
14375 dwarf2_complex_location_expr_complaint ();
14380 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14381 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14385 /* Create a type for a C++ namespace. */
14387 static struct type *
14388 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14390 struct objfile *objfile = cu->objfile;
14391 const char *previous_prefix, *name;
14395 /* For extensions, reuse the type of the original namespace. */
14396 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14398 struct die_info *ext_die;
14399 struct dwarf2_cu *ext_cu = cu;
14401 ext_die = dwarf2_extension (die, &ext_cu);
14402 type = read_type_die (ext_die, ext_cu);
14404 /* EXT_CU may not be the same as CU.
14405 Ensure TYPE is recorded with CU in die_type_hash. */
14406 return set_die_type (die, type, cu);
14409 name = namespace_name (die, &is_anonymous, cu);
14411 /* Now build the name of the current namespace. */
14413 previous_prefix = determine_prefix (die, cu);
14414 if (previous_prefix[0] != '\0')
14415 name = typename_concat (&objfile->objfile_obstack,
14416 previous_prefix, name, 0, cu);
14418 /* Create the type. */
14419 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14420 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14422 return set_die_type (die, type, cu);
14425 /* Read a namespace scope. */
14428 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14430 struct objfile *objfile = cu->objfile;
14433 /* Add a symbol associated to this if we haven't seen the namespace
14434 before. Also, add a using directive if it's an anonymous
14437 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14441 type = read_type_die (die, cu);
14442 new_symbol (die, type, cu);
14444 namespace_name (die, &is_anonymous, cu);
14447 const char *previous_prefix = determine_prefix (die, cu);
14449 add_using_directive (using_directives (cu->language),
14450 previous_prefix, TYPE_NAME (type), NULL,
14451 NULL, NULL, 0, &objfile->objfile_obstack);
14455 if (die->child != NULL)
14457 struct die_info *child_die = die->child;
14459 while (child_die && child_die->tag)
14461 process_die (child_die, cu);
14462 child_die = sibling_die (child_die);
14467 /* Read a Fortran module as type. This DIE can be only a declaration used for
14468 imported module. Still we need that type as local Fortran "use ... only"
14469 declaration imports depend on the created type in determine_prefix. */
14471 static struct type *
14472 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14474 struct objfile *objfile = cu->objfile;
14475 const char *module_name;
14478 module_name = dwarf2_name (die, cu);
14480 complaint (&symfile_complaints,
14481 _("DW_TAG_module has no name, offset 0x%x"),
14482 to_underlying (die->sect_off));
14483 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14485 /* determine_prefix uses TYPE_TAG_NAME. */
14486 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14488 return set_die_type (die, type, cu);
14491 /* Read a Fortran module. */
14494 read_module (struct die_info *die, struct dwarf2_cu *cu)
14496 struct die_info *child_die = die->child;
14499 type = read_type_die (die, cu);
14500 new_symbol (die, type, cu);
14502 while (child_die && child_die->tag)
14504 process_die (child_die, cu);
14505 child_die = sibling_die (child_die);
14509 /* Return the name of the namespace represented by DIE. Set
14510 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14513 static const char *
14514 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14516 struct die_info *current_die;
14517 const char *name = NULL;
14519 /* Loop through the extensions until we find a name. */
14521 for (current_die = die;
14522 current_die != NULL;
14523 current_die = dwarf2_extension (die, &cu))
14525 /* We don't use dwarf2_name here so that we can detect the absence
14526 of a name -> anonymous namespace. */
14527 name = dwarf2_string_attr (die, DW_AT_name, cu);
14533 /* Is it an anonymous namespace? */
14535 *is_anonymous = (name == NULL);
14537 name = CP_ANONYMOUS_NAMESPACE_STR;
14542 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14543 the user defined type vector. */
14545 static struct type *
14546 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14548 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14549 struct comp_unit_head *cu_header = &cu->header;
14551 struct attribute *attr_byte_size;
14552 struct attribute *attr_address_class;
14553 int byte_size, addr_class;
14554 struct type *target_type;
14556 target_type = die_type (die, cu);
14558 /* The die_type call above may have already set the type for this DIE. */
14559 type = get_die_type (die, cu);
14563 type = lookup_pointer_type (target_type);
14565 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14566 if (attr_byte_size)
14567 byte_size = DW_UNSND (attr_byte_size);
14569 byte_size = cu_header->addr_size;
14571 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14572 if (attr_address_class)
14573 addr_class = DW_UNSND (attr_address_class);
14575 addr_class = DW_ADDR_none;
14577 /* If the pointer size or address class is different than the
14578 default, create a type variant marked as such and set the
14579 length accordingly. */
14580 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14582 if (gdbarch_address_class_type_flags_p (gdbarch))
14586 type_flags = gdbarch_address_class_type_flags
14587 (gdbarch, byte_size, addr_class);
14588 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14590 type = make_type_with_address_space (type, type_flags);
14592 else if (TYPE_LENGTH (type) != byte_size)
14594 complaint (&symfile_complaints,
14595 _("invalid pointer size %d"), byte_size);
14599 /* Should we also complain about unhandled address classes? */
14603 TYPE_LENGTH (type) = byte_size;
14604 return set_die_type (die, type, cu);
14607 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14608 the user defined type vector. */
14610 static struct type *
14611 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14614 struct type *to_type;
14615 struct type *domain;
14617 to_type = die_type (die, cu);
14618 domain = die_containing_type (die, cu);
14620 /* The calls above may have already set the type for this DIE. */
14621 type = get_die_type (die, cu);
14625 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14626 type = lookup_methodptr_type (to_type);
14627 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14629 struct type *new_type = alloc_type (cu->objfile);
14631 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14632 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14633 TYPE_VARARGS (to_type));
14634 type = lookup_methodptr_type (new_type);
14637 type = lookup_memberptr_type (to_type, domain);
14639 return set_die_type (die, type, cu);
14642 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14643 the user defined type vector. */
14645 static struct type *
14646 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14647 enum type_code refcode)
14649 struct comp_unit_head *cu_header = &cu->header;
14650 struct type *type, *target_type;
14651 struct attribute *attr;
14653 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14655 target_type = die_type (die, cu);
14657 /* The die_type call above may have already set the type for this DIE. */
14658 type = get_die_type (die, cu);
14662 type = lookup_reference_type (target_type, refcode);
14663 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14666 TYPE_LENGTH (type) = DW_UNSND (attr);
14670 TYPE_LENGTH (type) = cu_header->addr_size;
14672 return set_die_type (die, type, cu);
14675 /* Add the given cv-qualifiers to the element type of the array. GCC
14676 outputs DWARF type qualifiers that apply to an array, not the
14677 element type. But GDB relies on the array element type to carry
14678 the cv-qualifiers. This mimics section 6.7.3 of the C99
14681 static struct type *
14682 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14683 struct type *base_type, int cnst, int voltl)
14685 struct type *el_type, *inner_array;
14687 base_type = copy_type (base_type);
14688 inner_array = base_type;
14690 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14692 TYPE_TARGET_TYPE (inner_array) =
14693 copy_type (TYPE_TARGET_TYPE (inner_array));
14694 inner_array = TYPE_TARGET_TYPE (inner_array);
14697 el_type = TYPE_TARGET_TYPE (inner_array);
14698 cnst |= TYPE_CONST (el_type);
14699 voltl |= TYPE_VOLATILE (el_type);
14700 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14702 return set_die_type (die, base_type, cu);
14705 static struct type *
14706 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14708 struct type *base_type, *cv_type;
14710 base_type = die_type (die, cu);
14712 /* The die_type call above may have already set the type for this DIE. */
14713 cv_type = get_die_type (die, cu);
14717 /* In case the const qualifier is applied to an array type, the element type
14718 is so qualified, not the array type (section 6.7.3 of C99). */
14719 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14720 return add_array_cv_type (die, cu, base_type, 1, 0);
14722 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14723 return set_die_type (die, cv_type, cu);
14726 static struct type *
14727 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14729 struct type *base_type, *cv_type;
14731 base_type = die_type (die, cu);
14733 /* The die_type call above may have already set the type for this DIE. */
14734 cv_type = get_die_type (die, cu);
14738 /* In case the volatile qualifier is applied to an array type, the
14739 element type is so qualified, not the array type (section 6.7.3
14741 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14742 return add_array_cv_type (die, cu, base_type, 0, 1);
14744 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14745 return set_die_type (die, cv_type, cu);
14748 /* Handle DW_TAG_restrict_type. */
14750 static struct type *
14751 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14753 struct type *base_type, *cv_type;
14755 base_type = die_type (die, cu);
14757 /* The die_type call above may have already set the type for this DIE. */
14758 cv_type = get_die_type (die, cu);
14762 cv_type = make_restrict_type (base_type);
14763 return set_die_type (die, cv_type, cu);
14766 /* Handle DW_TAG_atomic_type. */
14768 static struct type *
14769 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14771 struct type *base_type, *cv_type;
14773 base_type = die_type (die, cu);
14775 /* The die_type call above may have already set the type for this DIE. */
14776 cv_type = get_die_type (die, cu);
14780 cv_type = make_atomic_type (base_type);
14781 return set_die_type (die, cv_type, cu);
14784 /* Extract all information from a DW_TAG_string_type DIE and add to
14785 the user defined type vector. It isn't really a user defined type,
14786 but it behaves like one, with other DIE's using an AT_user_def_type
14787 attribute to reference it. */
14789 static struct type *
14790 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14792 struct objfile *objfile = cu->objfile;
14793 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14794 struct type *type, *range_type, *index_type, *char_type;
14795 struct attribute *attr;
14796 unsigned int length;
14798 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14801 length = DW_UNSND (attr);
14805 /* Check for the DW_AT_byte_size attribute. */
14806 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14809 length = DW_UNSND (attr);
14817 index_type = objfile_type (objfile)->builtin_int;
14818 range_type = create_static_range_type (NULL, index_type, 1, length);
14819 char_type = language_string_char_type (cu->language_defn, gdbarch);
14820 type = create_string_type (NULL, char_type, range_type);
14822 return set_die_type (die, type, cu);
14825 /* Assuming that DIE corresponds to a function, returns nonzero
14826 if the function is prototyped. */
14829 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14831 struct attribute *attr;
14833 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14834 if (attr && (DW_UNSND (attr) != 0))
14837 /* The DWARF standard implies that the DW_AT_prototyped attribute
14838 is only meaninful for C, but the concept also extends to other
14839 languages that allow unprototyped functions (Eg: Objective C).
14840 For all other languages, assume that functions are always
14842 if (cu->language != language_c
14843 && cu->language != language_objc
14844 && cu->language != language_opencl)
14847 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14848 prototyped and unprototyped functions; default to prototyped,
14849 since that is more common in modern code (and RealView warns
14850 about unprototyped functions). */
14851 if (producer_is_realview (cu->producer))
14857 /* Handle DIES due to C code like:
14861 int (*funcp)(int a, long l);
14865 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14867 static struct type *
14868 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14870 struct objfile *objfile = cu->objfile;
14871 struct type *type; /* Type that this function returns. */
14872 struct type *ftype; /* Function that returns above type. */
14873 struct attribute *attr;
14875 type = die_type (die, cu);
14877 /* The die_type call above may have already set the type for this DIE. */
14878 ftype = get_die_type (die, cu);
14882 ftype = lookup_function_type (type);
14884 if (prototyped_function_p (die, cu))
14885 TYPE_PROTOTYPED (ftype) = 1;
14887 /* Store the calling convention in the type if it's available in
14888 the subroutine die. Otherwise set the calling convention to
14889 the default value DW_CC_normal. */
14890 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14892 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14893 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14894 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14896 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14898 /* Record whether the function returns normally to its caller or not
14899 if the DWARF producer set that information. */
14900 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14901 if (attr && (DW_UNSND (attr) != 0))
14902 TYPE_NO_RETURN (ftype) = 1;
14904 /* We need to add the subroutine type to the die immediately so
14905 we don't infinitely recurse when dealing with parameters
14906 declared as the same subroutine type. */
14907 set_die_type (die, ftype, cu);
14909 if (die->child != NULL)
14911 struct type *void_type = objfile_type (objfile)->builtin_void;
14912 struct die_info *child_die;
14913 int nparams, iparams;
14915 /* Count the number of parameters.
14916 FIXME: GDB currently ignores vararg functions, but knows about
14917 vararg member functions. */
14919 child_die = die->child;
14920 while (child_die && child_die->tag)
14922 if (child_die->tag == DW_TAG_formal_parameter)
14924 else if (child_die->tag == DW_TAG_unspecified_parameters)
14925 TYPE_VARARGS (ftype) = 1;
14926 child_die = sibling_die (child_die);
14929 /* Allocate storage for parameters and fill them in. */
14930 TYPE_NFIELDS (ftype) = nparams;
14931 TYPE_FIELDS (ftype) = (struct field *)
14932 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14934 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14935 even if we error out during the parameters reading below. */
14936 for (iparams = 0; iparams < nparams; iparams++)
14937 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14940 child_die = die->child;
14941 while (child_die && child_die->tag)
14943 if (child_die->tag == DW_TAG_formal_parameter)
14945 struct type *arg_type;
14947 /* DWARF version 2 has no clean way to discern C++
14948 static and non-static member functions. G++ helps
14949 GDB by marking the first parameter for non-static
14950 member functions (which is the this pointer) as
14951 artificial. We pass this information to
14952 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14954 DWARF version 3 added DW_AT_object_pointer, which GCC
14955 4.5 does not yet generate. */
14956 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14958 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14960 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14961 arg_type = die_type (child_die, cu);
14963 /* RealView does not mark THIS as const, which the testsuite
14964 expects. GCC marks THIS as const in method definitions,
14965 but not in the class specifications (GCC PR 43053). */
14966 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14967 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14970 struct dwarf2_cu *arg_cu = cu;
14971 const char *name = dwarf2_name (child_die, cu);
14973 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14976 /* If the compiler emits this, use it. */
14977 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14980 else if (name && strcmp (name, "this") == 0)
14981 /* Function definitions will have the argument names. */
14983 else if (name == NULL && iparams == 0)
14984 /* Declarations may not have the names, so like
14985 elsewhere in GDB, assume an artificial first
14986 argument is "this". */
14990 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14994 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14997 child_die = sibling_die (child_die);
15004 static struct type *
15005 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15007 struct objfile *objfile = cu->objfile;
15008 const char *name = NULL;
15009 struct type *this_type, *target_type;
15011 name = dwarf2_full_name (NULL, die, cu);
15012 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15013 TYPE_TARGET_STUB (this_type) = 1;
15014 set_die_type (die, this_type, cu);
15015 target_type = die_type (die, cu);
15016 if (target_type != this_type)
15017 TYPE_TARGET_TYPE (this_type) = target_type;
15020 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15021 spec and cause infinite loops in GDB. */
15022 complaint (&symfile_complaints,
15023 _("Self-referential DW_TAG_typedef "
15024 "- DIE at 0x%x [in module %s]"),
15025 to_underlying (die->sect_off), objfile_name (objfile));
15026 TYPE_TARGET_TYPE (this_type) = NULL;
15031 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15032 (which may be different from NAME) to the architecture back-end to allow
15033 it to guess the correct format if necessary. */
15035 static struct type *
15036 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15037 const char *name_hint)
15039 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15040 const struct floatformat **format;
15043 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15045 type = init_float_type (objfile, bits, name, format);
15047 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15052 /* Find a representation of a given base type and install
15053 it in the TYPE field of the die. */
15055 static struct type *
15056 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15058 struct objfile *objfile = cu->objfile;
15060 struct attribute *attr;
15061 int encoding = 0, bits = 0;
15064 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15067 encoding = DW_UNSND (attr);
15069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15072 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15074 name = dwarf2_name (die, cu);
15077 complaint (&symfile_complaints,
15078 _("DW_AT_name missing from DW_TAG_base_type"));
15083 case DW_ATE_address:
15084 /* Turn DW_ATE_address into a void * pointer. */
15085 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15086 type = init_pointer_type (objfile, bits, name, type);
15088 case DW_ATE_boolean:
15089 type = init_boolean_type (objfile, bits, 1, name);
15091 case DW_ATE_complex_float:
15092 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15093 type = init_complex_type (objfile, name, type);
15095 case DW_ATE_decimal_float:
15096 type = init_decfloat_type (objfile, bits, name);
15099 type = dwarf2_init_float_type (objfile, bits, name, name);
15101 case DW_ATE_signed:
15102 type = init_integer_type (objfile, bits, 0, name);
15104 case DW_ATE_unsigned:
15105 if (cu->language == language_fortran
15107 && startswith (name, "character("))
15108 type = init_character_type (objfile, bits, 1, name);
15110 type = init_integer_type (objfile, bits, 1, name);
15112 case DW_ATE_signed_char:
15113 if (cu->language == language_ada || cu->language == language_m2
15114 || cu->language == language_pascal
15115 || cu->language == language_fortran)
15116 type = init_character_type (objfile, bits, 0, name);
15118 type = init_integer_type (objfile, bits, 0, name);
15120 case DW_ATE_unsigned_char:
15121 if (cu->language == language_ada || cu->language == language_m2
15122 || cu->language == language_pascal
15123 || cu->language == language_fortran
15124 || cu->language == language_rust)
15125 type = init_character_type (objfile, bits, 1, name);
15127 type = init_integer_type (objfile, bits, 1, name);
15131 gdbarch *arch = get_objfile_arch (objfile);
15134 type = builtin_type (arch)->builtin_char16;
15135 else if (bits == 32)
15136 type = builtin_type (arch)->builtin_char32;
15139 complaint (&symfile_complaints,
15140 _("unsupported DW_ATE_UTF bit size: '%d'"),
15142 type = init_integer_type (objfile, bits, 1, name);
15144 return set_die_type (die, type, cu);
15149 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15150 dwarf_type_encoding_name (encoding));
15151 type = init_type (objfile, TYPE_CODE_ERROR,
15152 bits / TARGET_CHAR_BIT, name);
15156 if (name && strcmp (name, "char") == 0)
15157 TYPE_NOSIGN (type) = 1;
15159 return set_die_type (die, type, cu);
15162 /* Parse dwarf attribute if it's a block, reference or constant and put the
15163 resulting value of the attribute into struct bound_prop.
15164 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15167 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15168 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15170 struct dwarf2_property_baton *baton;
15171 struct obstack *obstack = &cu->objfile->objfile_obstack;
15173 if (attr == NULL || prop == NULL)
15176 if (attr_form_is_block (attr))
15178 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15179 baton->referenced_type = NULL;
15180 baton->locexpr.per_cu = cu->per_cu;
15181 baton->locexpr.size = DW_BLOCK (attr)->size;
15182 baton->locexpr.data = DW_BLOCK (attr)->data;
15183 prop->data.baton = baton;
15184 prop->kind = PROP_LOCEXPR;
15185 gdb_assert (prop->data.baton != NULL);
15187 else if (attr_form_is_ref (attr))
15189 struct dwarf2_cu *target_cu = cu;
15190 struct die_info *target_die;
15191 struct attribute *target_attr;
15193 target_die = follow_die_ref (die, attr, &target_cu);
15194 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15195 if (target_attr == NULL)
15196 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15198 if (target_attr == NULL)
15201 switch (target_attr->name)
15203 case DW_AT_location:
15204 if (attr_form_is_section_offset (target_attr))
15206 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15207 baton->referenced_type = die_type (target_die, target_cu);
15208 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15209 prop->data.baton = baton;
15210 prop->kind = PROP_LOCLIST;
15211 gdb_assert (prop->data.baton != NULL);
15213 else if (attr_form_is_block (target_attr))
15215 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15216 baton->referenced_type = die_type (target_die, target_cu);
15217 baton->locexpr.per_cu = cu->per_cu;
15218 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15219 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15220 prop->data.baton = baton;
15221 prop->kind = PROP_LOCEXPR;
15222 gdb_assert (prop->data.baton != NULL);
15226 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15227 "dynamic property");
15231 case DW_AT_data_member_location:
15235 if (!handle_data_member_location (target_die, target_cu,
15239 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15240 baton->referenced_type = read_type_die (target_die->parent,
15242 baton->offset_info.offset = offset;
15243 baton->offset_info.type = die_type (target_die, target_cu);
15244 prop->data.baton = baton;
15245 prop->kind = PROP_ADDR_OFFSET;
15250 else if (attr_form_is_constant (attr))
15252 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15253 prop->kind = PROP_CONST;
15257 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15258 dwarf2_name (die, cu));
15265 /* Read the given DW_AT_subrange DIE. */
15267 static struct type *
15268 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15270 struct type *base_type, *orig_base_type;
15271 struct type *range_type;
15272 struct attribute *attr;
15273 struct dynamic_prop low, high;
15274 int low_default_is_valid;
15275 int high_bound_is_count = 0;
15277 LONGEST negative_mask;
15279 orig_base_type = die_type (die, cu);
15280 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15281 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15282 creating the range type, but we use the result of check_typedef
15283 when examining properties of the type. */
15284 base_type = check_typedef (orig_base_type);
15286 /* The die_type call above may have already set the type for this DIE. */
15287 range_type = get_die_type (die, cu);
15291 low.kind = PROP_CONST;
15292 high.kind = PROP_CONST;
15293 high.data.const_val = 0;
15295 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15296 omitting DW_AT_lower_bound. */
15297 switch (cu->language)
15300 case language_cplus:
15301 low.data.const_val = 0;
15302 low_default_is_valid = 1;
15304 case language_fortran:
15305 low.data.const_val = 1;
15306 low_default_is_valid = 1;
15309 case language_objc:
15310 case language_rust:
15311 low.data.const_val = 0;
15312 low_default_is_valid = (cu->header.version >= 4);
15316 case language_pascal:
15317 low.data.const_val = 1;
15318 low_default_is_valid = (cu->header.version >= 4);
15321 low.data.const_val = 0;
15322 low_default_is_valid = 0;
15326 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15328 attr_to_dynamic_prop (attr, die, cu, &low);
15329 else if (!low_default_is_valid)
15330 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15331 "- DIE at 0x%x [in module %s]"),
15332 to_underlying (die->sect_off), objfile_name (cu->objfile));
15334 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15335 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15337 attr = dwarf2_attr (die, DW_AT_count, cu);
15338 if (attr_to_dynamic_prop (attr, die, cu, &high))
15340 /* If bounds are constant do the final calculation here. */
15341 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15342 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15344 high_bound_is_count = 1;
15348 /* Dwarf-2 specifications explicitly allows to create subrange types
15349 without specifying a base type.
15350 In that case, the base type must be set to the type of
15351 the lower bound, upper bound or count, in that order, if any of these
15352 three attributes references an object that has a type.
15353 If no base type is found, the Dwarf-2 specifications say that
15354 a signed integer type of size equal to the size of an address should
15356 For the following C code: `extern char gdb_int [];'
15357 GCC produces an empty range DIE.
15358 FIXME: muller/2010-05-28: Possible references to object for low bound,
15359 high bound or count are not yet handled by this code. */
15360 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15362 struct objfile *objfile = cu->objfile;
15363 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15364 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15365 struct type *int_type = objfile_type (objfile)->builtin_int;
15367 /* Test "int", "long int", and "long long int" objfile types,
15368 and select the first one having a size above or equal to the
15369 architecture address size. */
15370 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15371 base_type = int_type;
15374 int_type = objfile_type (objfile)->builtin_long;
15375 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15376 base_type = int_type;
15379 int_type = objfile_type (objfile)->builtin_long_long;
15380 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15381 base_type = int_type;
15386 /* Normally, the DWARF producers are expected to use a signed
15387 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15388 But this is unfortunately not always the case, as witnessed
15389 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15390 is used instead. To work around that ambiguity, we treat
15391 the bounds as signed, and thus sign-extend their values, when
15392 the base type is signed. */
15394 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15395 if (low.kind == PROP_CONST
15396 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15397 low.data.const_val |= negative_mask;
15398 if (high.kind == PROP_CONST
15399 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15400 high.data.const_val |= negative_mask;
15402 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15404 if (high_bound_is_count)
15405 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15407 /* Ada expects an empty array on no boundary attributes. */
15408 if (attr == NULL && cu->language != language_ada)
15409 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15411 name = dwarf2_name (die, cu);
15413 TYPE_NAME (range_type) = name;
15415 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15417 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15419 set_die_type (die, range_type, cu);
15421 /* set_die_type should be already done. */
15422 set_descriptive_type (range_type, die, cu);
15427 static struct type *
15428 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15432 /* For now, we only support the C meaning of an unspecified type: void. */
15434 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15435 TYPE_NAME (type) = dwarf2_name (die, cu);
15437 return set_die_type (die, type, cu);
15440 /* Read a single die and all its descendents. Set the die's sibling
15441 field to NULL; set other fields in the die correctly, and set all
15442 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15443 location of the info_ptr after reading all of those dies. PARENT
15444 is the parent of the die in question. */
15446 static struct die_info *
15447 read_die_and_children (const struct die_reader_specs *reader,
15448 const gdb_byte *info_ptr,
15449 const gdb_byte **new_info_ptr,
15450 struct die_info *parent)
15452 struct die_info *die;
15453 const gdb_byte *cur_ptr;
15456 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15459 *new_info_ptr = cur_ptr;
15462 store_in_ref_table (die, reader->cu);
15465 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15469 *new_info_ptr = cur_ptr;
15472 die->sibling = NULL;
15473 die->parent = parent;
15477 /* Read a die, all of its descendents, and all of its siblings; set
15478 all of the fields of all of the dies correctly. Arguments are as
15479 in read_die_and_children. */
15481 static struct die_info *
15482 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15483 const gdb_byte *info_ptr,
15484 const gdb_byte **new_info_ptr,
15485 struct die_info *parent)
15487 struct die_info *first_die, *last_sibling;
15488 const gdb_byte *cur_ptr;
15490 cur_ptr = info_ptr;
15491 first_die = last_sibling = NULL;
15495 struct die_info *die
15496 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15500 *new_info_ptr = cur_ptr;
15507 last_sibling->sibling = die;
15509 last_sibling = die;
15513 /* Read a die, all of its descendents, and all of its siblings; set
15514 all of the fields of all of the dies correctly. Arguments are as
15515 in read_die_and_children.
15516 This the main entry point for reading a DIE and all its children. */
15518 static struct die_info *
15519 read_die_and_siblings (const struct die_reader_specs *reader,
15520 const gdb_byte *info_ptr,
15521 const gdb_byte **new_info_ptr,
15522 struct die_info *parent)
15524 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15525 new_info_ptr, parent);
15527 if (dwarf_die_debug)
15529 fprintf_unfiltered (gdb_stdlog,
15530 "Read die from %s@0x%x of %s:\n",
15531 get_section_name (reader->die_section),
15532 (unsigned) (info_ptr - reader->die_section->buffer),
15533 bfd_get_filename (reader->abfd));
15534 dump_die (die, dwarf_die_debug);
15540 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15542 The caller is responsible for filling in the extra attributes
15543 and updating (*DIEP)->num_attrs.
15544 Set DIEP to point to a newly allocated die with its information,
15545 except for its child, sibling, and parent fields.
15546 Set HAS_CHILDREN to tell whether the die has children or not. */
15548 static const gdb_byte *
15549 read_full_die_1 (const struct die_reader_specs *reader,
15550 struct die_info **diep, const gdb_byte *info_ptr,
15551 int *has_children, int num_extra_attrs)
15553 unsigned int abbrev_number, bytes_read, i;
15554 struct abbrev_info *abbrev;
15555 struct die_info *die;
15556 struct dwarf2_cu *cu = reader->cu;
15557 bfd *abfd = reader->abfd;
15559 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15560 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15561 info_ptr += bytes_read;
15562 if (!abbrev_number)
15569 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15571 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15573 bfd_get_filename (abfd));
15575 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15576 die->sect_off = sect_off;
15577 die->tag = abbrev->tag;
15578 die->abbrev = abbrev_number;
15580 /* Make the result usable.
15581 The caller needs to update num_attrs after adding the extra
15583 die->num_attrs = abbrev->num_attrs;
15585 for (i = 0; i < abbrev->num_attrs; ++i)
15586 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15590 *has_children = abbrev->has_children;
15594 /* Read a die and all its attributes.
15595 Set DIEP to point to a newly allocated die with its information,
15596 except for its child, sibling, and parent fields.
15597 Set HAS_CHILDREN to tell whether the die has children or not. */
15599 static const gdb_byte *
15600 read_full_die (const struct die_reader_specs *reader,
15601 struct die_info **diep, const gdb_byte *info_ptr,
15604 const gdb_byte *result;
15606 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15608 if (dwarf_die_debug)
15610 fprintf_unfiltered (gdb_stdlog,
15611 "Read die from %s@0x%x of %s:\n",
15612 get_section_name (reader->die_section),
15613 (unsigned) (info_ptr - reader->die_section->buffer),
15614 bfd_get_filename (reader->abfd));
15615 dump_die (*diep, dwarf_die_debug);
15621 /* Abbreviation tables.
15623 In DWARF version 2, the description of the debugging information is
15624 stored in a separate .debug_abbrev section. Before we read any
15625 dies from a section we read in all abbreviations and install them
15626 in a hash table. */
15628 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15630 static struct abbrev_info *
15631 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15633 struct abbrev_info *abbrev;
15635 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15636 memset (abbrev, 0, sizeof (struct abbrev_info));
15641 /* Add an abbreviation to the table. */
15644 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15645 unsigned int abbrev_number,
15646 struct abbrev_info *abbrev)
15648 unsigned int hash_number;
15650 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15651 abbrev->next = abbrev_table->abbrevs[hash_number];
15652 abbrev_table->abbrevs[hash_number] = abbrev;
15655 /* Look up an abbrev in the table.
15656 Returns NULL if the abbrev is not found. */
15658 static struct abbrev_info *
15659 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15660 unsigned int abbrev_number)
15662 unsigned int hash_number;
15663 struct abbrev_info *abbrev;
15665 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15666 abbrev = abbrev_table->abbrevs[hash_number];
15670 if (abbrev->number == abbrev_number)
15672 abbrev = abbrev->next;
15677 /* Read in an abbrev table. */
15679 static struct abbrev_table *
15680 abbrev_table_read_table (struct dwarf2_section_info *section,
15681 sect_offset sect_off)
15683 struct objfile *objfile = dwarf2_per_objfile->objfile;
15684 bfd *abfd = get_section_bfd_owner (section);
15685 struct abbrev_table *abbrev_table;
15686 const gdb_byte *abbrev_ptr;
15687 struct abbrev_info *cur_abbrev;
15688 unsigned int abbrev_number, bytes_read, abbrev_name;
15689 unsigned int abbrev_form;
15690 struct attr_abbrev *cur_attrs;
15691 unsigned int allocated_attrs;
15693 abbrev_table = XNEW (struct abbrev_table);
15694 abbrev_table->sect_off = sect_off;
15695 obstack_init (&abbrev_table->abbrev_obstack);
15696 abbrev_table->abbrevs =
15697 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15699 memset (abbrev_table->abbrevs, 0,
15700 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15702 dwarf2_read_section (objfile, section);
15703 abbrev_ptr = section->buffer + to_underlying (sect_off);
15704 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15705 abbrev_ptr += bytes_read;
15707 allocated_attrs = ATTR_ALLOC_CHUNK;
15708 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15710 /* Loop until we reach an abbrev number of 0. */
15711 while (abbrev_number)
15713 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15715 /* read in abbrev header */
15716 cur_abbrev->number = abbrev_number;
15718 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15719 abbrev_ptr += bytes_read;
15720 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15723 /* now read in declarations */
15726 LONGEST implicit_const;
15728 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15729 abbrev_ptr += bytes_read;
15730 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15731 abbrev_ptr += bytes_read;
15732 if (abbrev_form == DW_FORM_implicit_const)
15734 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15736 abbrev_ptr += bytes_read;
15740 /* Initialize it due to a false compiler warning. */
15741 implicit_const = -1;
15744 if (abbrev_name == 0)
15747 if (cur_abbrev->num_attrs == allocated_attrs)
15749 allocated_attrs += ATTR_ALLOC_CHUNK;
15751 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15754 cur_attrs[cur_abbrev->num_attrs].name
15755 = (enum dwarf_attribute) abbrev_name;
15756 cur_attrs[cur_abbrev->num_attrs].form
15757 = (enum dwarf_form) abbrev_form;
15758 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15759 ++cur_abbrev->num_attrs;
15762 cur_abbrev->attrs =
15763 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15764 cur_abbrev->num_attrs);
15765 memcpy (cur_abbrev->attrs, cur_attrs,
15766 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15768 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15770 /* Get next abbreviation.
15771 Under Irix6 the abbreviations for a compilation unit are not
15772 always properly terminated with an abbrev number of 0.
15773 Exit loop if we encounter an abbreviation which we have
15774 already read (which means we are about to read the abbreviations
15775 for the next compile unit) or if the end of the abbreviation
15776 table is reached. */
15777 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15779 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15780 abbrev_ptr += bytes_read;
15781 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15786 return abbrev_table;
15789 /* Free the resources held by ABBREV_TABLE. */
15792 abbrev_table_free (struct abbrev_table *abbrev_table)
15794 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15795 xfree (abbrev_table);
15798 /* Same as abbrev_table_free but as a cleanup.
15799 We pass in a pointer to the pointer to the table so that we can
15800 set the pointer to NULL when we're done. It also simplifies
15801 build_type_psymtabs_1. */
15804 abbrev_table_free_cleanup (void *table_ptr)
15806 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15808 if (*abbrev_table_ptr != NULL)
15809 abbrev_table_free (*abbrev_table_ptr);
15810 *abbrev_table_ptr = NULL;
15813 /* Read the abbrev table for CU from ABBREV_SECTION. */
15816 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15817 struct dwarf2_section_info *abbrev_section)
15820 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
15823 /* Release the memory used by the abbrev table for a compilation unit. */
15826 dwarf2_free_abbrev_table (void *ptr_to_cu)
15828 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15830 if (cu->abbrev_table != NULL)
15831 abbrev_table_free (cu->abbrev_table);
15832 /* Set this to NULL so that we SEGV if we try to read it later,
15833 and also because free_comp_unit verifies this is NULL. */
15834 cu->abbrev_table = NULL;
15837 /* Returns nonzero if TAG represents a type that we might generate a partial
15841 is_type_tag_for_partial (int tag)
15846 /* Some types that would be reasonable to generate partial symbols for,
15847 that we don't at present. */
15848 case DW_TAG_array_type:
15849 case DW_TAG_file_type:
15850 case DW_TAG_ptr_to_member_type:
15851 case DW_TAG_set_type:
15852 case DW_TAG_string_type:
15853 case DW_TAG_subroutine_type:
15855 case DW_TAG_base_type:
15856 case DW_TAG_class_type:
15857 case DW_TAG_interface_type:
15858 case DW_TAG_enumeration_type:
15859 case DW_TAG_structure_type:
15860 case DW_TAG_subrange_type:
15861 case DW_TAG_typedef:
15862 case DW_TAG_union_type:
15869 /* Load all DIEs that are interesting for partial symbols into memory. */
15871 static struct partial_die_info *
15872 load_partial_dies (const struct die_reader_specs *reader,
15873 const gdb_byte *info_ptr, int building_psymtab)
15875 struct dwarf2_cu *cu = reader->cu;
15876 struct objfile *objfile = cu->objfile;
15877 struct partial_die_info *part_die;
15878 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15879 struct abbrev_info *abbrev;
15880 unsigned int bytes_read;
15881 unsigned int load_all = 0;
15882 int nesting_level = 1;
15887 gdb_assert (cu->per_cu != NULL);
15888 if (cu->per_cu->load_all_dies)
15892 = htab_create_alloc_ex (cu->header.length / 12,
15896 &cu->comp_unit_obstack,
15897 hashtab_obstack_allocate,
15898 dummy_obstack_deallocate);
15900 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15904 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15906 /* A NULL abbrev means the end of a series of children. */
15907 if (abbrev == NULL)
15909 if (--nesting_level == 0)
15911 /* PART_DIE was probably the last thing allocated on the
15912 comp_unit_obstack, so we could call obstack_free
15913 here. We don't do that because the waste is small,
15914 and will be cleaned up when we're done with this
15915 compilation unit. This way, we're also more robust
15916 against other users of the comp_unit_obstack. */
15919 info_ptr += bytes_read;
15920 last_die = parent_die;
15921 parent_die = parent_die->die_parent;
15925 /* Check for template arguments. We never save these; if
15926 they're seen, we just mark the parent, and go on our way. */
15927 if (parent_die != NULL
15928 && cu->language == language_cplus
15929 && (abbrev->tag == DW_TAG_template_type_param
15930 || abbrev->tag == DW_TAG_template_value_param))
15932 parent_die->has_template_arguments = 1;
15936 /* We don't need a partial DIE for the template argument. */
15937 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15942 /* We only recurse into c++ subprograms looking for template arguments.
15943 Skip their other children. */
15945 && cu->language == language_cplus
15946 && parent_die != NULL
15947 && parent_die->tag == DW_TAG_subprogram)
15949 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15953 /* Check whether this DIE is interesting enough to save. Normally
15954 we would not be interested in members here, but there may be
15955 later variables referencing them via DW_AT_specification (for
15956 static members). */
15958 && !is_type_tag_for_partial (abbrev->tag)
15959 && abbrev->tag != DW_TAG_constant
15960 && abbrev->tag != DW_TAG_enumerator
15961 && abbrev->tag != DW_TAG_subprogram
15962 && abbrev->tag != DW_TAG_lexical_block
15963 && abbrev->tag != DW_TAG_variable
15964 && abbrev->tag != DW_TAG_namespace
15965 && abbrev->tag != DW_TAG_module
15966 && abbrev->tag != DW_TAG_member
15967 && abbrev->tag != DW_TAG_imported_unit
15968 && abbrev->tag != DW_TAG_imported_declaration)
15970 /* Otherwise we skip to the next sibling, if any. */
15971 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15975 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15978 /* This two-pass algorithm for processing partial symbols has a
15979 high cost in cache pressure. Thus, handle some simple cases
15980 here which cover the majority of C partial symbols. DIEs
15981 which neither have specification tags in them, nor could have
15982 specification tags elsewhere pointing at them, can simply be
15983 processed and discarded.
15985 This segment is also optional; scan_partial_symbols and
15986 add_partial_symbol will handle these DIEs if we chain
15987 them in normally. When compilers which do not emit large
15988 quantities of duplicate debug information are more common,
15989 this code can probably be removed. */
15991 /* Any complete simple types at the top level (pretty much all
15992 of them, for a language without namespaces), can be processed
15994 if (parent_die == NULL
15995 && part_die->has_specification == 0
15996 && part_die->is_declaration == 0
15997 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15998 || part_die->tag == DW_TAG_base_type
15999 || part_die->tag == DW_TAG_subrange_type))
16001 if (building_psymtab && part_die->name != NULL)
16002 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16003 VAR_DOMAIN, LOC_TYPEDEF,
16004 &objfile->static_psymbols,
16005 0, cu->language, objfile);
16006 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16010 /* The exception for DW_TAG_typedef with has_children above is
16011 a workaround of GCC PR debug/47510. In the case of this complaint
16012 type_name_no_tag_or_error will error on such types later.
16014 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16015 it could not find the child DIEs referenced later, this is checked
16016 above. In correct DWARF DW_TAG_typedef should have no children. */
16018 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16019 complaint (&symfile_complaints,
16020 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16021 "- DIE at 0x%x [in module %s]"),
16022 to_underlying (part_die->sect_off), objfile_name (objfile));
16024 /* If we're at the second level, and we're an enumerator, and
16025 our parent has no specification (meaning possibly lives in a
16026 namespace elsewhere), then we can add the partial symbol now
16027 instead of queueing it. */
16028 if (part_die->tag == DW_TAG_enumerator
16029 && parent_die != NULL
16030 && parent_die->die_parent == NULL
16031 && parent_die->tag == DW_TAG_enumeration_type
16032 && parent_die->has_specification == 0)
16034 if (part_die->name == NULL)
16035 complaint (&symfile_complaints,
16036 _("malformed enumerator DIE ignored"));
16037 else if (building_psymtab)
16038 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16039 VAR_DOMAIN, LOC_CONST,
16040 cu->language == language_cplus
16041 ? &objfile->global_psymbols
16042 : &objfile->static_psymbols,
16043 0, cu->language, objfile);
16045 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16049 /* We'll save this DIE so link it in. */
16050 part_die->die_parent = parent_die;
16051 part_die->die_sibling = NULL;
16052 part_die->die_child = NULL;
16054 if (last_die && last_die == parent_die)
16055 last_die->die_child = part_die;
16057 last_die->die_sibling = part_die;
16059 last_die = part_die;
16061 if (first_die == NULL)
16062 first_die = part_die;
16064 /* Maybe add the DIE to the hash table. Not all DIEs that we
16065 find interesting need to be in the hash table, because we
16066 also have the parent/sibling/child chains; only those that we
16067 might refer to by offset later during partial symbol reading.
16069 For now this means things that might have be the target of a
16070 DW_AT_specification, DW_AT_abstract_origin, or
16071 DW_AT_extension. DW_AT_extension will refer only to
16072 namespaces; DW_AT_abstract_origin refers to functions (and
16073 many things under the function DIE, but we do not recurse
16074 into function DIEs during partial symbol reading) and
16075 possibly variables as well; DW_AT_specification refers to
16076 declarations. Declarations ought to have the DW_AT_declaration
16077 flag. It happens that GCC forgets to put it in sometimes, but
16078 only for functions, not for types.
16080 Adding more things than necessary to the hash table is harmless
16081 except for the performance cost. Adding too few will result in
16082 wasted time in find_partial_die, when we reread the compilation
16083 unit with load_all_dies set. */
16086 || abbrev->tag == DW_TAG_constant
16087 || abbrev->tag == DW_TAG_subprogram
16088 || abbrev->tag == DW_TAG_variable
16089 || abbrev->tag == DW_TAG_namespace
16090 || part_die->is_declaration)
16094 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16095 to_underlying (part_die->sect_off),
16100 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16102 /* For some DIEs we want to follow their children (if any). For C
16103 we have no reason to follow the children of structures; for other
16104 languages we have to, so that we can get at method physnames
16105 to infer fully qualified class names, for DW_AT_specification,
16106 and for C++ template arguments. For C++, we also look one level
16107 inside functions to find template arguments (if the name of the
16108 function does not already contain the template arguments).
16110 For Ada, we need to scan the children of subprograms and lexical
16111 blocks as well because Ada allows the definition of nested
16112 entities that could be interesting for the debugger, such as
16113 nested subprograms for instance. */
16114 if (last_die->has_children
16116 || last_die->tag == DW_TAG_namespace
16117 || last_die->tag == DW_TAG_module
16118 || last_die->tag == DW_TAG_enumeration_type
16119 || (cu->language == language_cplus
16120 && last_die->tag == DW_TAG_subprogram
16121 && (last_die->name == NULL
16122 || strchr (last_die->name, '<') == NULL))
16123 || (cu->language != language_c
16124 && (last_die->tag == DW_TAG_class_type
16125 || last_die->tag == DW_TAG_interface_type
16126 || last_die->tag == DW_TAG_structure_type
16127 || last_die->tag == DW_TAG_union_type))
16128 || (cu->language == language_ada
16129 && (last_die->tag == DW_TAG_subprogram
16130 || last_die->tag == DW_TAG_lexical_block))))
16133 parent_die = last_die;
16137 /* Otherwise we skip to the next sibling, if any. */
16138 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16140 /* Back to the top, do it again. */
16144 /* Read a minimal amount of information into the minimal die structure. */
16146 static const gdb_byte *
16147 read_partial_die (const struct die_reader_specs *reader,
16148 struct partial_die_info *part_die,
16149 struct abbrev_info *abbrev, unsigned int abbrev_len,
16150 const gdb_byte *info_ptr)
16152 struct dwarf2_cu *cu = reader->cu;
16153 struct objfile *objfile = cu->objfile;
16154 const gdb_byte *buffer = reader->buffer;
16156 struct attribute attr;
16157 int has_low_pc_attr = 0;
16158 int has_high_pc_attr = 0;
16159 int high_pc_relative = 0;
16161 memset (part_die, 0, sizeof (struct partial_die_info));
16163 part_die->sect_off = (sect_offset) (info_ptr - buffer);
16165 info_ptr += abbrev_len;
16167 if (abbrev == NULL)
16170 part_die->tag = abbrev->tag;
16171 part_die->has_children = abbrev->has_children;
16173 for (i = 0; i < abbrev->num_attrs; ++i)
16175 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16177 /* Store the data if it is of an attribute we want to keep in a
16178 partial symbol table. */
16182 switch (part_die->tag)
16184 case DW_TAG_compile_unit:
16185 case DW_TAG_partial_unit:
16186 case DW_TAG_type_unit:
16187 /* Compilation units have a DW_AT_name that is a filename, not
16188 a source language identifier. */
16189 case DW_TAG_enumeration_type:
16190 case DW_TAG_enumerator:
16191 /* These tags always have simple identifiers already; no need
16192 to canonicalize them. */
16193 part_die->name = DW_STRING (&attr);
16197 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16198 &objfile->per_bfd->storage_obstack);
16202 case DW_AT_linkage_name:
16203 case DW_AT_MIPS_linkage_name:
16204 /* Note that both forms of linkage name might appear. We
16205 assume they will be the same, and we only store the last
16207 if (cu->language == language_ada)
16208 part_die->name = DW_STRING (&attr);
16209 part_die->linkage_name = DW_STRING (&attr);
16212 has_low_pc_attr = 1;
16213 part_die->lowpc = attr_value_as_address (&attr);
16215 case DW_AT_high_pc:
16216 has_high_pc_attr = 1;
16217 part_die->highpc = attr_value_as_address (&attr);
16218 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16219 high_pc_relative = 1;
16221 case DW_AT_location:
16222 /* Support the .debug_loc offsets. */
16223 if (attr_form_is_block (&attr))
16225 part_die->d.locdesc = DW_BLOCK (&attr);
16227 else if (attr_form_is_section_offset (&attr))
16229 dwarf2_complex_location_expr_complaint ();
16233 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16234 "partial symbol information");
16237 case DW_AT_external:
16238 part_die->is_external = DW_UNSND (&attr);
16240 case DW_AT_declaration:
16241 part_die->is_declaration = DW_UNSND (&attr);
16244 part_die->has_type = 1;
16246 case DW_AT_abstract_origin:
16247 case DW_AT_specification:
16248 case DW_AT_extension:
16249 part_die->has_specification = 1;
16250 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16251 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16252 || cu->per_cu->is_dwz);
16254 case DW_AT_sibling:
16255 /* Ignore absolute siblings, they might point outside of
16256 the current compile unit. */
16257 if (attr.form == DW_FORM_ref_addr)
16258 complaint (&symfile_complaints,
16259 _("ignoring absolute DW_AT_sibling"));
16262 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16263 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
16265 if (sibling_ptr < info_ptr)
16266 complaint (&symfile_complaints,
16267 _("DW_AT_sibling points backwards"));
16268 else if (sibling_ptr > reader->buffer_end)
16269 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16271 part_die->sibling = sibling_ptr;
16274 case DW_AT_byte_size:
16275 part_die->has_byte_size = 1;
16277 case DW_AT_const_value:
16278 part_die->has_const_value = 1;
16280 case DW_AT_calling_convention:
16281 /* DWARF doesn't provide a way to identify a program's source-level
16282 entry point. DW_AT_calling_convention attributes are only meant
16283 to describe functions' calling conventions.
16285 However, because it's a necessary piece of information in
16286 Fortran, and before DWARF 4 DW_CC_program was the only
16287 piece of debugging information whose definition refers to
16288 a 'main program' at all, several compilers marked Fortran
16289 main programs with DW_CC_program --- even when those
16290 functions use the standard calling conventions.
16292 Although DWARF now specifies a way to provide this
16293 information, we support this practice for backward
16295 if (DW_UNSND (&attr) == DW_CC_program
16296 && cu->language == language_fortran)
16297 part_die->main_subprogram = 1;
16300 if (DW_UNSND (&attr) == DW_INL_inlined
16301 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16302 part_die->may_be_inlined = 1;
16306 if (part_die->tag == DW_TAG_imported_unit)
16308 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
16309 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16310 || cu->per_cu->is_dwz);
16314 case DW_AT_main_subprogram:
16315 part_die->main_subprogram = DW_UNSND (&attr);
16323 if (high_pc_relative)
16324 part_die->highpc += part_die->lowpc;
16326 if (has_low_pc_attr && has_high_pc_attr)
16328 /* When using the GNU linker, .gnu.linkonce. sections are used to
16329 eliminate duplicate copies of functions and vtables and such.
16330 The linker will arbitrarily choose one and discard the others.
16331 The AT_*_pc values for such functions refer to local labels in
16332 these sections. If the section from that file was discarded, the
16333 labels are not in the output, so the relocs get a value of 0.
16334 If this is a discarded function, mark the pc bounds as invalid,
16335 so that GDB will ignore it. */
16336 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16338 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16340 complaint (&symfile_complaints,
16341 _("DW_AT_low_pc %s is zero "
16342 "for DIE at 0x%x [in module %s]"),
16343 paddress (gdbarch, part_die->lowpc),
16344 to_underlying (part_die->sect_off), objfile_name (objfile));
16346 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16347 else if (part_die->lowpc >= part_die->highpc)
16349 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16351 complaint (&symfile_complaints,
16352 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16353 "for DIE at 0x%x [in module %s]"),
16354 paddress (gdbarch, part_die->lowpc),
16355 paddress (gdbarch, part_die->highpc),
16356 to_underlying (part_die->sect_off),
16357 objfile_name (objfile));
16360 part_die->has_pc_info = 1;
16366 /* Find a cached partial DIE at OFFSET in CU. */
16368 static struct partial_die_info *
16369 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
16371 struct partial_die_info *lookup_die = NULL;
16372 struct partial_die_info part_die;
16374 part_die.sect_off = sect_off;
16375 lookup_die = ((struct partial_die_info *)
16376 htab_find_with_hash (cu->partial_dies, &part_die,
16377 to_underlying (sect_off)));
16382 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16383 except in the case of .debug_types DIEs which do not reference
16384 outside their CU (they do however referencing other types via
16385 DW_FORM_ref_sig8). */
16387 static struct partial_die_info *
16388 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
16390 struct objfile *objfile = cu->objfile;
16391 struct dwarf2_per_cu_data *per_cu = NULL;
16392 struct partial_die_info *pd = NULL;
16394 if (offset_in_dwz == cu->per_cu->is_dwz
16395 && offset_in_cu_p (&cu->header, sect_off))
16397 pd = find_partial_die_in_comp_unit (sect_off, cu);
16400 /* We missed recording what we needed.
16401 Load all dies and try again. */
16402 per_cu = cu->per_cu;
16406 /* TUs don't reference other CUs/TUs (except via type signatures). */
16407 if (cu->per_cu->is_debug_types)
16409 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16410 " external reference to offset 0x%x [in module %s].\n"),
16411 to_underlying (cu->header.sect_off), to_underlying (sect_off),
16412 bfd_get_filename (objfile->obfd));
16414 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
16417 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16418 load_partial_comp_unit (per_cu);
16420 per_cu->cu->last_used = 0;
16421 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16424 /* If we didn't find it, and not all dies have been loaded,
16425 load them all and try again. */
16427 if (pd == NULL && per_cu->load_all_dies == 0)
16429 per_cu->load_all_dies = 1;
16431 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16432 THIS_CU->cu may already be in use. So we can't just free it and
16433 replace its DIEs with the ones we read in. Instead, we leave those
16434 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16435 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16437 load_partial_comp_unit (per_cu);
16439 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16443 internal_error (__FILE__, __LINE__,
16444 _("could not find partial DIE 0x%x "
16445 "in cache [from module %s]\n"),
16446 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
16450 /* See if we can figure out if the class lives in a namespace. We do
16451 this by looking for a member function; its demangled name will
16452 contain namespace info, if there is any. */
16455 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16456 struct dwarf2_cu *cu)
16458 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16459 what template types look like, because the demangler
16460 frequently doesn't give the same name as the debug info. We
16461 could fix this by only using the demangled name to get the
16462 prefix (but see comment in read_structure_type). */
16464 struct partial_die_info *real_pdi;
16465 struct partial_die_info *child_pdi;
16467 /* If this DIE (this DIE's specification, if any) has a parent, then
16468 we should not do this. We'll prepend the parent's fully qualified
16469 name when we create the partial symbol. */
16471 real_pdi = struct_pdi;
16472 while (real_pdi->has_specification)
16473 real_pdi = find_partial_die (real_pdi->spec_offset,
16474 real_pdi->spec_is_dwz, cu);
16476 if (real_pdi->die_parent != NULL)
16479 for (child_pdi = struct_pdi->die_child;
16481 child_pdi = child_pdi->die_sibling)
16483 if (child_pdi->tag == DW_TAG_subprogram
16484 && child_pdi->linkage_name != NULL)
16486 char *actual_class_name
16487 = language_class_name_from_physname (cu->language_defn,
16488 child_pdi->linkage_name);
16489 if (actual_class_name != NULL)
16493 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16495 strlen (actual_class_name)));
16496 xfree (actual_class_name);
16503 /* Adjust PART_DIE before generating a symbol for it. This function
16504 may set the is_external flag or change the DIE's name. */
16507 fixup_partial_die (struct partial_die_info *part_die,
16508 struct dwarf2_cu *cu)
16510 /* Once we've fixed up a die, there's no point in doing so again.
16511 This also avoids a memory leak if we were to call
16512 guess_partial_die_structure_name multiple times. */
16513 if (part_die->fixup_called)
16516 /* If we found a reference attribute and the DIE has no name, try
16517 to find a name in the referred to DIE. */
16519 if (part_die->name == NULL && part_die->has_specification)
16521 struct partial_die_info *spec_die;
16523 spec_die = find_partial_die (part_die->spec_offset,
16524 part_die->spec_is_dwz, cu);
16526 fixup_partial_die (spec_die, cu);
16528 if (spec_die->name)
16530 part_die->name = spec_die->name;
16532 /* Copy DW_AT_external attribute if it is set. */
16533 if (spec_die->is_external)
16534 part_die->is_external = spec_die->is_external;
16538 /* Set default names for some unnamed DIEs. */
16540 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16541 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16543 /* If there is no parent die to provide a namespace, and there are
16544 children, see if we can determine the namespace from their linkage
16546 if (cu->language == language_cplus
16547 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16548 && part_die->die_parent == NULL
16549 && part_die->has_children
16550 && (part_die->tag == DW_TAG_class_type
16551 || part_die->tag == DW_TAG_structure_type
16552 || part_die->tag == DW_TAG_union_type))
16553 guess_partial_die_structure_name (part_die, cu);
16555 /* GCC might emit a nameless struct or union that has a linkage
16556 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16557 if (part_die->name == NULL
16558 && (part_die->tag == DW_TAG_class_type
16559 || part_die->tag == DW_TAG_interface_type
16560 || part_die->tag == DW_TAG_structure_type
16561 || part_die->tag == DW_TAG_union_type)
16562 && part_die->linkage_name != NULL)
16566 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16571 /* Strip any leading namespaces/classes, keep only the base name.
16572 DW_AT_name for named DIEs does not contain the prefixes. */
16573 base = strrchr (demangled, ':');
16574 if (base && base > demangled && base[-1] == ':')
16581 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16582 base, strlen (base)));
16587 part_die->fixup_called = 1;
16590 /* Read an attribute value described by an attribute form. */
16592 static const gdb_byte *
16593 read_attribute_value (const struct die_reader_specs *reader,
16594 struct attribute *attr, unsigned form,
16595 LONGEST implicit_const, const gdb_byte *info_ptr)
16597 struct dwarf2_cu *cu = reader->cu;
16598 struct objfile *objfile = cu->objfile;
16599 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16600 bfd *abfd = reader->abfd;
16601 struct comp_unit_head *cu_header = &cu->header;
16602 unsigned int bytes_read;
16603 struct dwarf_block *blk;
16605 attr->form = (enum dwarf_form) form;
16608 case DW_FORM_ref_addr:
16609 if (cu->header.version == 2)
16610 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16612 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16613 &cu->header, &bytes_read);
16614 info_ptr += bytes_read;
16616 case DW_FORM_GNU_ref_alt:
16617 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16618 info_ptr += bytes_read;
16621 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16622 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16623 info_ptr += bytes_read;
16625 case DW_FORM_block2:
16626 blk = dwarf_alloc_block (cu);
16627 blk->size = read_2_bytes (abfd, info_ptr);
16629 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16630 info_ptr += blk->size;
16631 DW_BLOCK (attr) = blk;
16633 case DW_FORM_block4:
16634 blk = dwarf_alloc_block (cu);
16635 blk->size = read_4_bytes (abfd, info_ptr);
16637 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16638 info_ptr += blk->size;
16639 DW_BLOCK (attr) = blk;
16641 case DW_FORM_data2:
16642 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16645 case DW_FORM_data4:
16646 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16649 case DW_FORM_data8:
16650 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16653 case DW_FORM_data16:
16654 blk = dwarf_alloc_block (cu);
16656 blk->data = read_n_bytes (abfd, info_ptr, 16);
16658 DW_BLOCK (attr) = blk;
16660 case DW_FORM_sec_offset:
16661 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16662 info_ptr += bytes_read;
16664 case DW_FORM_string:
16665 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16666 DW_STRING_IS_CANONICAL (attr) = 0;
16667 info_ptr += bytes_read;
16670 if (!cu->per_cu->is_dwz)
16672 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16674 DW_STRING_IS_CANONICAL (attr) = 0;
16675 info_ptr += bytes_read;
16679 case DW_FORM_line_strp:
16680 if (!cu->per_cu->is_dwz)
16682 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16683 cu_header, &bytes_read);
16684 DW_STRING_IS_CANONICAL (attr) = 0;
16685 info_ptr += bytes_read;
16689 case DW_FORM_GNU_strp_alt:
16691 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16692 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16695 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16696 DW_STRING_IS_CANONICAL (attr) = 0;
16697 info_ptr += bytes_read;
16700 case DW_FORM_exprloc:
16701 case DW_FORM_block:
16702 blk = dwarf_alloc_block (cu);
16703 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16704 info_ptr += bytes_read;
16705 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16706 info_ptr += blk->size;
16707 DW_BLOCK (attr) = blk;
16709 case DW_FORM_block1:
16710 blk = dwarf_alloc_block (cu);
16711 blk->size = read_1_byte (abfd, info_ptr);
16713 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16714 info_ptr += blk->size;
16715 DW_BLOCK (attr) = blk;
16717 case DW_FORM_data1:
16718 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16722 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16725 case DW_FORM_flag_present:
16726 DW_UNSND (attr) = 1;
16728 case DW_FORM_sdata:
16729 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16730 info_ptr += bytes_read;
16732 case DW_FORM_udata:
16733 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16734 info_ptr += bytes_read;
16737 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16738 + read_1_byte (abfd, info_ptr));
16742 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16743 + read_2_bytes (abfd, info_ptr));
16747 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16748 + read_4_bytes (abfd, info_ptr));
16752 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16753 + read_8_bytes (abfd, info_ptr));
16756 case DW_FORM_ref_sig8:
16757 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16760 case DW_FORM_ref_udata:
16761 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16762 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16763 info_ptr += bytes_read;
16765 case DW_FORM_indirect:
16766 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16767 info_ptr += bytes_read;
16768 if (form == DW_FORM_implicit_const)
16770 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16771 info_ptr += bytes_read;
16773 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16776 case DW_FORM_implicit_const:
16777 DW_SND (attr) = implicit_const;
16779 case DW_FORM_GNU_addr_index:
16780 if (reader->dwo_file == NULL)
16782 /* For now flag a hard error.
16783 Later we can turn this into a complaint. */
16784 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16785 dwarf_form_name (form),
16786 bfd_get_filename (abfd));
16788 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16789 info_ptr += bytes_read;
16791 case DW_FORM_GNU_str_index:
16792 if (reader->dwo_file == NULL)
16794 /* For now flag a hard error.
16795 Later we can turn this into a complaint if warranted. */
16796 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16797 dwarf_form_name (form),
16798 bfd_get_filename (abfd));
16801 ULONGEST str_index =
16802 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16804 DW_STRING (attr) = read_str_index (reader, str_index);
16805 DW_STRING_IS_CANONICAL (attr) = 0;
16806 info_ptr += bytes_read;
16810 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16811 dwarf_form_name (form),
16812 bfd_get_filename (abfd));
16816 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16817 attr->form = DW_FORM_GNU_ref_alt;
16819 /* We have seen instances where the compiler tried to emit a byte
16820 size attribute of -1 which ended up being encoded as an unsigned
16821 0xffffffff. Although 0xffffffff is technically a valid size value,
16822 an object of this size seems pretty unlikely so we can relatively
16823 safely treat these cases as if the size attribute was invalid and
16824 treat them as zero by default. */
16825 if (attr->name == DW_AT_byte_size
16826 && form == DW_FORM_data4
16827 && DW_UNSND (attr) >= 0xffffffff)
16830 (&symfile_complaints,
16831 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16832 hex_string (DW_UNSND (attr)));
16833 DW_UNSND (attr) = 0;
16839 /* Read an attribute described by an abbreviated attribute. */
16841 static const gdb_byte *
16842 read_attribute (const struct die_reader_specs *reader,
16843 struct attribute *attr, struct attr_abbrev *abbrev,
16844 const gdb_byte *info_ptr)
16846 attr->name = abbrev->name;
16847 return read_attribute_value (reader, attr, abbrev->form,
16848 abbrev->implicit_const, info_ptr);
16851 /* Read dwarf information from a buffer. */
16853 static unsigned int
16854 read_1_byte (bfd *abfd, const gdb_byte *buf)
16856 return bfd_get_8 (abfd, buf);
16860 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16862 return bfd_get_signed_8 (abfd, buf);
16865 static unsigned int
16866 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16868 return bfd_get_16 (abfd, buf);
16872 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16874 return bfd_get_signed_16 (abfd, buf);
16877 static unsigned int
16878 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16880 return bfd_get_32 (abfd, buf);
16884 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16886 return bfd_get_signed_32 (abfd, buf);
16890 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16892 return bfd_get_64 (abfd, buf);
16896 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16897 unsigned int *bytes_read)
16899 struct comp_unit_head *cu_header = &cu->header;
16900 CORE_ADDR retval = 0;
16902 if (cu_header->signed_addr_p)
16904 switch (cu_header->addr_size)
16907 retval = bfd_get_signed_16 (abfd, buf);
16910 retval = bfd_get_signed_32 (abfd, buf);
16913 retval = bfd_get_signed_64 (abfd, buf);
16916 internal_error (__FILE__, __LINE__,
16917 _("read_address: bad switch, signed [in module %s]"),
16918 bfd_get_filename (abfd));
16923 switch (cu_header->addr_size)
16926 retval = bfd_get_16 (abfd, buf);
16929 retval = bfd_get_32 (abfd, buf);
16932 retval = bfd_get_64 (abfd, buf);
16935 internal_error (__FILE__, __LINE__,
16936 _("read_address: bad switch, "
16937 "unsigned [in module %s]"),
16938 bfd_get_filename (abfd));
16942 *bytes_read = cu_header->addr_size;
16946 /* Read the initial length from a section. The (draft) DWARF 3
16947 specification allows the initial length to take up either 4 bytes
16948 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16949 bytes describe the length and all offsets will be 8 bytes in length
16952 An older, non-standard 64-bit format is also handled by this
16953 function. The older format in question stores the initial length
16954 as an 8-byte quantity without an escape value. Lengths greater
16955 than 2^32 aren't very common which means that the initial 4 bytes
16956 is almost always zero. Since a length value of zero doesn't make
16957 sense for the 32-bit format, this initial zero can be considered to
16958 be an escape value which indicates the presence of the older 64-bit
16959 format. As written, the code can't detect (old format) lengths
16960 greater than 4GB. If it becomes necessary to handle lengths
16961 somewhat larger than 4GB, we could allow other small values (such
16962 as the non-sensical values of 1, 2, and 3) to also be used as
16963 escape values indicating the presence of the old format.
16965 The value returned via bytes_read should be used to increment the
16966 relevant pointer after calling read_initial_length().
16968 [ Note: read_initial_length() and read_offset() are based on the
16969 document entitled "DWARF Debugging Information Format", revision
16970 3, draft 8, dated November 19, 2001. This document was obtained
16973 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16975 This document is only a draft and is subject to change. (So beware.)
16977 Details regarding the older, non-standard 64-bit format were
16978 determined empirically by examining 64-bit ELF files produced by
16979 the SGI toolchain on an IRIX 6.5 machine.
16981 - Kevin, July 16, 2002
16985 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16987 LONGEST length = bfd_get_32 (abfd, buf);
16989 if (length == 0xffffffff)
16991 length = bfd_get_64 (abfd, buf + 4);
16994 else if (length == 0)
16996 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16997 length = bfd_get_64 (abfd, buf);
17008 /* Cover function for read_initial_length.
17009 Returns the length of the object at BUF, and stores the size of the
17010 initial length in *BYTES_READ and stores the size that offsets will be in
17012 If the initial length size is not equivalent to that specified in
17013 CU_HEADER then issue a complaint.
17014 This is useful when reading non-comp-unit headers. */
17017 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17018 const struct comp_unit_head *cu_header,
17019 unsigned int *bytes_read,
17020 unsigned int *offset_size)
17022 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17024 gdb_assert (cu_header->initial_length_size == 4
17025 || cu_header->initial_length_size == 8
17026 || cu_header->initial_length_size == 12);
17028 if (cu_header->initial_length_size != *bytes_read)
17029 complaint (&symfile_complaints,
17030 _("intermixed 32-bit and 64-bit DWARF sections"));
17032 *offset_size = (*bytes_read == 4) ? 4 : 8;
17036 /* Read an offset from the data stream. The size of the offset is
17037 given by cu_header->offset_size. */
17040 read_offset (bfd *abfd, const gdb_byte *buf,
17041 const struct comp_unit_head *cu_header,
17042 unsigned int *bytes_read)
17044 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17046 *bytes_read = cu_header->offset_size;
17050 /* Read an offset from the data stream. */
17053 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17055 LONGEST retval = 0;
17057 switch (offset_size)
17060 retval = bfd_get_32 (abfd, buf);
17063 retval = bfd_get_64 (abfd, buf);
17066 internal_error (__FILE__, __LINE__,
17067 _("read_offset_1: bad switch [in module %s]"),
17068 bfd_get_filename (abfd));
17074 static const gdb_byte *
17075 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17077 /* If the size of a host char is 8 bits, we can return a pointer
17078 to the buffer, otherwise we have to copy the data to a buffer
17079 allocated on the temporary obstack. */
17080 gdb_assert (HOST_CHAR_BIT == 8);
17084 static const char *
17085 read_direct_string (bfd *abfd, const gdb_byte *buf,
17086 unsigned int *bytes_read_ptr)
17088 /* If the size of a host char is 8 bits, we can return a pointer
17089 to the string, otherwise we have to copy the string to a buffer
17090 allocated on the temporary obstack. */
17091 gdb_assert (HOST_CHAR_BIT == 8);
17094 *bytes_read_ptr = 1;
17097 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17098 return (const char *) buf;
17101 /* Return pointer to string at section SECT offset STR_OFFSET with error
17102 reporting strings FORM_NAME and SECT_NAME. */
17104 static const char *
17105 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17106 struct dwarf2_section_info *sect,
17107 const char *form_name,
17108 const char *sect_name)
17110 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17111 if (sect->buffer == NULL)
17112 error (_("%s used without %s section [in module %s]"),
17113 form_name, sect_name, bfd_get_filename (abfd));
17114 if (str_offset >= sect->size)
17115 error (_("%s pointing outside of %s section [in module %s]"),
17116 form_name, sect_name, bfd_get_filename (abfd));
17117 gdb_assert (HOST_CHAR_BIT == 8);
17118 if (sect->buffer[str_offset] == '\0')
17120 return (const char *) (sect->buffer + str_offset);
17123 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17125 static const char *
17126 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17128 return read_indirect_string_at_offset_from (abfd, str_offset,
17129 &dwarf2_per_objfile->str,
17130 "DW_FORM_strp", ".debug_str");
17133 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17135 static const char *
17136 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17138 return read_indirect_string_at_offset_from (abfd, str_offset,
17139 &dwarf2_per_objfile->line_str,
17140 "DW_FORM_line_strp",
17141 ".debug_line_str");
17144 /* Read a string at offset STR_OFFSET in the .debug_str section from
17145 the .dwz file DWZ. Throw an error if the offset is too large. If
17146 the string consists of a single NUL byte, return NULL; otherwise
17147 return a pointer to the string. */
17149 static const char *
17150 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17152 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17154 if (dwz->str.buffer == NULL)
17155 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17156 "section [in module %s]"),
17157 bfd_get_filename (dwz->dwz_bfd));
17158 if (str_offset >= dwz->str.size)
17159 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17160 ".debug_str section [in module %s]"),
17161 bfd_get_filename (dwz->dwz_bfd));
17162 gdb_assert (HOST_CHAR_BIT == 8);
17163 if (dwz->str.buffer[str_offset] == '\0')
17165 return (const char *) (dwz->str.buffer + str_offset);
17168 /* Return pointer to string at .debug_str offset as read from BUF.
17169 BUF is assumed to be in a compilation unit described by CU_HEADER.
17170 Return *BYTES_READ_PTR count of bytes read from BUF. */
17172 static const char *
17173 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17174 const struct comp_unit_head *cu_header,
17175 unsigned int *bytes_read_ptr)
17177 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17179 return read_indirect_string_at_offset (abfd, str_offset);
17182 /* Return pointer to string at .debug_line_str offset as read from BUF.
17183 BUF is assumed to be in a compilation unit described by CU_HEADER.
17184 Return *BYTES_READ_PTR count of bytes read from BUF. */
17186 static const char *
17187 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17188 const struct comp_unit_head *cu_header,
17189 unsigned int *bytes_read_ptr)
17191 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17193 return read_indirect_line_string_at_offset (abfd, str_offset);
17197 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17198 unsigned int *bytes_read_ptr)
17201 unsigned int num_read;
17203 unsigned char byte;
17210 byte = bfd_get_8 (abfd, buf);
17213 result |= ((ULONGEST) (byte & 127) << shift);
17214 if ((byte & 128) == 0)
17220 *bytes_read_ptr = num_read;
17225 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17226 unsigned int *bytes_read_ptr)
17229 int shift, num_read;
17230 unsigned char byte;
17237 byte = bfd_get_8 (abfd, buf);
17240 result |= ((LONGEST) (byte & 127) << shift);
17242 if ((byte & 128) == 0)
17247 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17248 result |= -(((LONGEST) 1) << shift);
17249 *bytes_read_ptr = num_read;
17253 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17254 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17255 ADDR_SIZE is the size of addresses from the CU header. */
17258 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17260 struct objfile *objfile = dwarf2_per_objfile->objfile;
17261 bfd *abfd = objfile->obfd;
17262 const gdb_byte *info_ptr;
17264 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17265 if (dwarf2_per_objfile->addr.buffer == NULL)
17266 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17267 objfile_name (objfile));
17268 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17269 error (_("DW_FORM_addr_index pointing outside of "
17270 ".debug_addr section [in module %s]"),
17271 objfile_name (objfile));
17272 info_ptr = (dwarf2_per_objfile->addr.buffer
17273 + addr_base + addr_index * addr_size);
17274 if (addr_size == 4)
17275 return bfd_get_32 (abfd, info_ptr);
17277 return bfd_get_64 (abfd, info_ptr);
17280 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17283 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17285 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17288 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17291 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17292 unsigned int *bytes_read)
17294 bfd *abfd = cu->objfile->obfd;
17295 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17297 return read_addr_index (cu, addr_index);
17300 /* Data structure to pass results from dwarf2_read_addr_index_reader
17301 back to dwarf2_read_addr_index. */
17303 struct dwarf2_read_addr_index_data
17305 ULONGEST addr_base;
17309 /* die_reader_func for dwarf2_read_addr_index. */
17312 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17313 const gdb_byte *info_ptr,
17314 struct die_info *comp_unit_die,
17318 struct dwarf2_cu *cu = reader->cu;
17319 struct dwarf2_read_addr_index_data *aidata =
17320 (struct dwarf2_read_addr_index_data *) data;
17322 aidata->addr_base = cu->addr_base;
17323 aidata->addr_size = cu->header.addr_size;
17326 /* Given an index in .debug_addr, fetch the value.
17327 NOTE: This can be called during dwarf expression evaluation,
17328 long after the debug information has been read, and thus per_cu->cu
17329 may no longer exist. */
17332 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17333 unsigned int addr_index)
17335 struct objfile *objfile = per_cu->objfile;
17336 struct dwarf2_cu *cu = per_cu->cu;
17337 ULONGEST addr_base;
17340 /* This is intended to be called from outside this file. */
17341 dw2_setup (objfile);
17343 /* We need addr_base and addr_size.
17344 If we don't have PER_CU->cu, we have to get it.
17345 Nasty, but the alternative is storing the needed info in PER_CU,
17346 which at this point doesn't seem justified: it's not clear how frequently
17347 it would get used and it would increase the size of every PER_CU.
17348 Entry points like dwarf2_per_cu_addr_size do a similar thing
17349 so we're not in uncharted territory here.
17350 Alas we need to be a bit more complicated as addr_base is contained
17353 We don't need to read the entire CU(/TU).
17354 We just need the header and top level die.
17356 IWBN to use the aging mechanism to let us lazily later discard the CU.
17357 For now we skip this optimization. */
17361 addr_base = cu->addr_base;
17362 addr_size = cu->header.addr_size;
17366 struct dwarf2_read_addr_index_data aidata;
17368 /* Note: We can't use init_cutu_and_read_dies_simple here,
17369 we need addr_base. */
17370 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17371 dwarf2_read_addr_index_reader, &aidata);
17372 addr_base = aidata.addr_base;
17373 addr_size = aidata.addr_size;
17376 return read_addr_index_1 (addr_index, addr_base, addr_size);
17379 /* Given a DW_FORM_GNU_str_index, fetch the string.
17380 This is only used by the Fission support. */
17382 static const char *
17383 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17385 struct objfile *objfile = dwarf2_per_objfile->objfile;
17386 const char *objf_name = objfile_name (objfile);
17387 bfd *abfd = objfile->obfd;
17388 struct dwarf2_cu *cu = reader->cu;
17389 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17390 struct dwarf2_section_info *str_offsets_section =
17391 &reader->dwo_file->sections.str_offsets;
17392 const gdb_byte *info_ptr;
17393 ULONGEST str_offset;
17394 static const char form_name[] = "DW_FORM_GNU_str_index";
17396 dwarf2_read_section (objfile, str_section);
17397 dwarf2_read_section (objfile, str_offsets_section);
17398 if (str_section->buffer == NULL)
17399 error (_("%s used without .debug_str.dwo section"
17400 " in CU at offset 0x%x [in module %s]"),
17401 form_name, to_underlying (cu->header.sect_off), objf_name);
17402 if (str_offsets_section->buffer == NULL)
17403 error (_("%s used without .debug_str_offsets.dwo section"
17404 " in CU at offset 0x%x [in module %s]"),
17405 form_name, to_underlying (cu->header.sect_off), objf_name);
17406 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17407 error (_("%s pointing outside of .debug_str_offsets.dwo"
17408 " section in CU at offset 0x%x [in module %s]"),
17409 form_name, to_underlying (cu->header.sect_off), objf_name);
17410 info_ptr = (str_offsets_section->buffer
17411 + str_index * cu->header.offset_size);
17412 if (cu->header.offset_size == 4)
17413 str_offset = bfd_get_32 (abfd, info_ptr);
17415 str_offset = bfd_get_64 (abfd, info_ptr);
17416 if (str_offset >= str_section->size)
17417 error (_("Offset from %s pointing outside of"
17418 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17419 form_name, to_underlying (cu->header.sect_off), objf_name);
17420 return (const char *) (str_section->buffer + str_offset);
17423 /* Return the length of an LEB128 number in BUF. */
17426 leb128_size (const gdb_byte *buf)
17428 const gdb_byte *begin = buf;
17434 if ((byte & 128) == 0)
17435 return buf - begin;
17440 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17449 cu->language = language_c;
17452 case DW_LANG_C_plus_plus:
17453 case DW_LANG_C_plus_plus_11:
17454 case DW_LANG_C_plus_plus_14:
17455 cu->language = language_cplus;
17458 cu->language = language_d;
17460 case DW_LANG_Fortran77:
17461 case DW_LANG_Fortran90:
17462 case DW_LANG_Fortran95:
17463 case DW_LANG_Fortran03:
17464 case DW_LANG_Fortran08:
17465 cu->language = language_fortran;
17468 cu->language = language_go;
17470 case DW_LANG_Mips_Assembler:
17471 cu->language = language_asm;
17473 case DW_LANG_Ada83:
17474 case DW_LANG_Ada95:
17475 cu->language = language_ada;
17477 case DW_LANG_Modula2:
17478 cu->language = language_m2;
17480 case DW_LANG_Pascal83:
17481 cu->language = language_pascal;
17484 cu->language = language_objc;
17487 case DW_LANG_Rust_old:
17488 cu->language = language_rust;
17490 case DW_LANG_Cobol74:
17491 case DW_LANG_Cobol85:
17493 cu->language = language_minimal;
17496 cu->language_defn = language_def (cu->language);
17499 /* Return the named attribute or NULL if not there. */
17501 static struct attribute *
17502 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17507 struct attribute *spec = NULL;
17509 for (i = 0; i < die->num_attrs; ++i)
17511 if (die->attrs[i].name == name)
17512 return &die->attrs[i];
17513 if (die->attrs[i].name == DW_AT_specification
17514 || die->attrs[i].name == DW_AT_abstract_origin)
17515 spec = &die->attrs[i];
17521 die = follow_die_ref (die, spec, &cu);
17527 /* Return the named attribute or NULL if not there,
17528 but do not follow DW_AT_specification, etc.
17529 This is for use in contexts where we're reading .debug_types dies.
17530 Following DW_AT_specification, DW_AT_abstract_origin will take us
17531 back up the chain, and we want to go down. */
17533 static struct attribute *
17534 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17538 for (i = 0; i < die->num_attrs; ++i)
17539 if (die->attrs[i].name == name)
17540 return &die->attrs[i];
17545 /* Return the string associated with a string-typed attribute, or NULL if it
17546 is either not found or is of an incorrect type. */
17548 static const char *
17549 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17551 struct attribute *attr;
17552 const char *str = NULL;
17554 attr = dwarf2_attr (die, name, cu);
17558 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17559 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17560 str = DW_STRING (attr);
17562 complaint (&symfile_complaints,
17563 _("string type expected for attribute %s for "
17564 "DIE at 0x%x in module %s"),
17565 dwarf_attr_name (name), to_underlying (die->sect_off),
17566 objfile_name (cu->objfile));
17572 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17573 and holds a non-zero value. This function should only be used for
17574 DW_FORM_flag or DW_FORM_flag_present attributes. */
17577 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17579 struct attribute *attr = dwarf2_attr (die, name, cu);
17581 return (attr && DW_UNSND (attr));
17585 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17587 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17588 which value is non-zero. However, we have to be careful with
17589 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17590 (via dwarf2_flag_true_p) follows this attribute. So we may
17591 end up accidently finding a declaration attribute that belongs
17592 to a different DIE referenced by the specification attribute,
17593 even though the given DIE does not have a declaration attribute. */
17594 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17595 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17598 /* Return the die giving the specification for DIE, if there is
17599 one. *SPEC_CU is the CU containing DIE on input, and the CU
17600 containing the return value on output. If there is no
17601 specification, but there is an abstract origin, that is
17604 static struct die_info *
17605 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17607 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17610 if (spec_attr == NULL)
17611 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17613 if (spec_attr == NULL)
17616 return follow_die_ref (die, spec_attr, spec_cu);
17619 /* Stub for free_line_header to match void * callback types. */
17622 free_line_header_voidp (void *arg)
17624 struct line_header *lh = (struct line_header *) arg;
17630 line_header::add_include_dir (const char *include_dir)
17632 if (dwarf_line_debug >= 2)
17633 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17634 include_dirs.size () + 1, include_dir);
17636 include_dirs.push_back (include_dir);
17640 line_header::add_file_name (const char *name,
17642 unsigned int mod_time,
17643 unsigned int length)
17645 if (dwarf_line_debug >= 2)
17646 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17647 (unsigned) file_names.size () + 1, name);
17649 file_names.emplace_back (name, d_index, mod_time, length);
17652 /* A convenience function to find the proper .debug_line section for a CU. */
17654 static struct dwarf2_section_info *
17655 get_debug_line_section (struct dwarf2_cu *cu)
17657 struct dwarf2_section_info *section;
17659 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17661 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17662 section = &cu->dwo_unit->dwo_file->sections.line;
17663 else if (cu->per_cu->is_dwz)
17665 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17667 section = &dwz->line;
17670 section = &dwarf2_per_objfile->line;
17675 /* Read directory or file name entry format, starting with byte of
17676 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17677 entries count and the entries themselves in the described entry
17681 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17682 struct line_header *lh,
17683 const struct comp_unit_head *cu_header,
17684 void (*callback) (struct line_header *lh,
17687 unsigned int mod_time,
17688 unsigned int length))
17690 gdb_byte format_count, formati;
17691 ULONGEST data_count, datai;
17692 const gdb_byte *buf = *bufp;
17693 const gdb_byte *format_header_data;
17695 unsigned int bytes_read;
17697 format_count = read_1_byte (abfd, buf);
17699 format_header_data = buf;
17700 for (formati = 0; formati < format_count; formati++)
17702 read_unsigned_leb128 (abfd, buf, &bytes_read);
17704 read_unsigned_leb128 (abfd, buf, &bytes_read);
17708 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17710 for (datai = 0; datai < data_count; datai++)
17712 const gdb_byte *format = format_header_data;
17713 struct file_entry fe;
17715 for (formati = 0; formati < format_count; formati++)
17717 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17718 format += bytes_read;
17720 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
17721 format += bytes_read;
17723 gdb::optional<const char *> string;
17724 gdb::optional<unsigned int> uint;
17728 case DW_FORM_string:
17729 string.emplace (read_direct_string (abfd, buf, &bytes_read));
17733 case DW_FORM_line_strp:
17734 string.emplace (read_indirect_line_string (abfd, buf,
17740 case DW_FORM_data1:
17741 uint.emplace (read_1_byte (abfd, buf));
17745 case DW_FORM_data2:
17746 uint.emplace (read_2_bytes (abfd, buf));
17750 case DW_FORM_data4:
17751 uint.emplace (read_4_bytes (abfd, buf));
17755 case DW_FORM_data8:
17756 uint.emplace (read_8_bytes (abfd, buf));
17760 case DW_FORM_udata:
17761 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
17765 case DW_FORM_block:
17766 /* It is valid only for DW_LNCT_timestamp which is ignored by
17771 switch (content_type)
17774 if (string.has_value ())
17777 case DW_LNCT_directory_index:
17778 if (uint.has_value ())
17779 fe.d_index = (dir_index) *uint;
17781 case DW_LNCT_timestamp:
17782 if (uint.has_value ())
17783 fe.mod_time = *uint;
17786 if (uint.has_value ())
17792 complaint (&symfile_complaints,
17793 _("Unknown format content type %s"),
17794 pulongest (content_type));
17798 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
17804 /* Read the statement program header starting at OFFSET in
17805 .debug_line, or .debug_line.dwo. Return a pointer
17806 to a struct line_header, allocated using xmalloc.
17807 Returns NULL if there is a problem reading the header, e.g., if it
17808 has a version we don't understand.
17810 NOTE: the strings in the include directory and file name tables of
17811 the returned object point into the dwarf line section buffer,
17812 and must not be freed. */
17814 static line_header_up
17815 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
17817 const gdb_byte *line_ptr;
17818 unsigned int bytes_read, offset_size;
17820 const char *cur_dir, *cur_file;
17821 struct dwarf2_section_info *section;
17824 section = get_debug_line_section (cu);
17825 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17826 if (section->buffer == NULL)
17828 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17829 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17831 complaint (&symfile_complaints, _("missing .debug_line section"));
17835 /* We can't do this until we know the section is non-empty.
17836 Only then do we know we have such a section. */
17837 abfd = get_section_bfd_owner (section);
17839 /* Make sure that at least there's room for the total_length field.
17840 That could be 12 bytes long, but we're just going to fudge that. */
17841 if (to_underlying (sect_off) + 4 >= section->size)
17843 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17847 line_header_up lh (new line_header ());
17849 lh->sect_off = sect_off;
17850 lh->offset_in_dwz = cu->per_cu->is_dwz;
17852 line_ptr = section->buffer + to_underlying (sect_off);
17854 /* Read in the header. */
17856 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17857 &bytes_read, &offset_size);
17858 line_ptr += bytes_read;
17859 if (line_ptr + lh->total_length > (section->buffer + section->size))
17861 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17864 lh->statement_program_end = line_ptr + lh->total_length;
17865 lh->version = read_2_bytes (abfd, line_ptr);
17867 if (lh->version > 5)
17869 /* This is a version we don't understand. The format could have
17870 changed in ways we don't handle properly so just punt. */
17871 complaint (&symfile_complaints,
17872 _("unsupported version in .debug_line section"));
17875 if (lh->version >= 5)
17877 gdb_byte segment_selector_size;
17879 /* Skip address size. */
17880 read_1_byte (abfd, line_ptr);
17883 segment_selector_size = read_1_byte (abfd, line_ptr);
17885 if (segment_selector_size != 0)
17887 complaint (&symfile_complaints,
17888 _("unsupported segment selector size %u "
17889 "in .debug_line section"),
17890 segment_selector_size);
17894 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17895 line_ptr += offset_size;
17896 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17898 if (lh->version >= 4)
17900 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17904 lh->maximum_ops_per_instruction = 1;
17906 if (lh->maximum_ops_per_instruction == 0)
17908 lh->maximum_ops_per_instruction = 1;
17909 complaint (&symfile_complaints,
17910 _("invalid maximum_ops_per_instruction "
17911 "in `.debug_line' section"));
17914 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17916 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17918 lh->line_range = read_1_byte (abfd, line_ptr);
17920 lh->opcode_base = read_1_byte (abfd, line_ptr);
17922 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
17924 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17925 for (i = 1; i < lh->opcode_base; ++i)
17927 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17931 if (lh->version >= 5)
17933 /* Read directory table. */
17934 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17935 [] (struct line_header *lh, const char *name,
17936 dir_index d_index, unsigned int mod_time,
17937 unsigned int length)
17939 lh->add_include_dir (name);
17942 /* Read file name table. */
17943 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17944 [] (struct line_header *lh, const char *name,
17945 dir_index d_index, unsigned int mod_time,
17946 unsigned int length)
17948 lh->add_file_name (name, d_index, mod_time, length);
17953 /* Read directory table. */
17954 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17956 line_ptr += bytes_read;
17957 lh->add_include_dir (cur_dir);
17959 line_ptr += bytes_read;
17961 /* Read file name table. */
17962 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17964 unsigned int mod_time, length;
17967 line_ptr += bytes_read;
17968 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17969 line_ptr += bytes_read;
17970 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17971 line_ptr += bytes_read;
17972 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17973 line_ptr += bytes_read;
17975 lh->add_file_name (cur_file, d_index, mod_time, length);
17977 line_ptr += bytes_read;
17979 lh->statement_program_start = line_ptr;
17981 if (line_ptr > (section->buffer + section->size))
17982 complaint (&symfile_complaints,
17983 _("line number info header doesn't "
17984 "fit in `.debug_line' section"));
17989 /* Subroutine of dwarf_decode_lines to simplify it.
17990 Return the file name of the psymtab for included file FILE_INDEX
17991 in line header LH of PST.
17992 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17993 If space for the result is malloc'd, it will be freed by a cleanup.
17994 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17996 The function creates dangling cleanup registration. */
17998 static const char *
17999 psymtab_include_file_name (const struct line_header *lh, int file_index,
18000 const struct partial_symtab *pst,
18001 const char *comp_dir)
18003 const file_entry &fe = lh->file_names[file_index];
18004 const char *include_name = fe.name;
18005 const char *include_name_to_compare = include_name;
18006 const char *pst_filename;
18007 char *copied_name = NULL;
18010 const char *dir_name = fe.include_dir (lh);
18012 if (!IS_ABSOLUTE_PATH (include_name)
18013 && (dir_name != NULL || comp_dir != NULL))
18015 /* Avoid creating a duplicate psymtab for PST.
18016 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18017 Before we do the comparison, however, we need to account
18018 for DIR_NAME and COMP_DIR.
18019 First prepend dir_name (if non-NULL). If we still don't
18020 have an absolute path prepend comp_dir (if non-NULL).
18021 However, the directory we record in the include-file's
18022 psymtab does not contain COMP_DIR (to match the
18023 corresponding symtab(s)).
18028 bash$ gcc -g ./hello.c
18029 include_name = "hello.c"
18031 DW_AT_comp_dir = comp_dir = "/tmp"
18032 DW_AT_name = "./hello.c"
18036 if (dir_name != NULL)
18038 char *tem = concat (dir_name, SLASH_STRING,
18039 include_name, (char *)NULL);
18041 make_cleanup (xfree, tem);
18042 include_name = tem;
18043 include_name_to_compare = include_name;
18045 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18047 char *tem = concat (comp_dir, SLASH_STRING,
18048 include_name, (char *)NULL);
18050 make_cleanup (xfree, tem);
18051 include_name_to_compare = tem;
18055 pst_filename = pst->filename;
18056 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18058 copied_name = concat (pst->dirname, SLASH_STRING,
18059 pst_filename, (char *)NULL);
18060 pst_filename = copied_name;
18063 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18065 if (copied_name != NULL)
18066 xfree (copied_name);
18070 return include_name;
18073 /* State machine to track the state of the line number program. */
18075 class lnp_state_machine
18078 /* Initialize a machine state for the start of a line number
18080 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18082 file_entry *current_file ()
18084 /* lh->file_names is 0-based, but the file name numbers in the
18085 statement program are 1-based. */
18086 return m_line_header->file_name_at (m_file);
18089 /* Record the line in the state machine. END_SEQUENCE is true if
18090 we're processing the end of a sequence. */
18091 void record_line (bool end_sequence);
18093 /* Check address and if invalid nop-out the rest of the lines in this
18095 void check_line_address (struct dwarf2_cu *cu,
18096 const gdb_byte *line_ptr,
18097 CORE_ADDR lowpc, CORE_ADDR address);
18099 void handle_set_discriminator (unsigned int discriminator)
18101 m_discriminator = discriminator;
18102 m_line_has_non_zero_discriminator |= discriminator != 0;
18105 /* Handle DW_LNE_set_address. */
18106 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18109 address += baseaddr;
18110 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18113 /* Handle DW_LNS_advance_pc. */
18114 void handle_advance_pc (CORE_ADDR adjust);
18116 /* Handle a special opcode. */
18117 void handle_special_opcode (unsigned char op_code);
18119 /* Handle DW_LNS_advance_line. */
18120 void handle_advance_line (int line_delta)
18122 advance_line (line_delta);
18125 /* Handle DW_LNS_set_file. */
18126 void handle_set_file (file_name_index file);
18128 /* Handle DW_LNS_negate_stmt. */
18129 void handle_negate_stmt ()
18131 m_is_stmt = !m_is_stmt;
18134 /* Handle DW_LNS_const_add_pc. */
18135 void handle_const_add_pc ();
18137 /* Handle DW_LNS_fixed_advance_pc. */
18138 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18140 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18144 /* Handle DW_LNS_copy. */
18145 void handle_copy ()
18147 record_line (false);
18148 m_discriminator = 0;
18151 /* Handle DW_LNE_end_sequence. */
18152 void handle_end_sequence ()
18154 m_record_line_callback = ::record_line;
18158 /* Advance the line by LINE_DELTA. */
18159 void advance_line (int line_delta)
18161 m_line += line_delta;
18163 if (line_delta != 0)
18164 m_line_has_non_zero_discriminator = m_discriminator != 0;
18167 gdbarch *m_gdbarch;
18169 /* True if we're recording lines.
18170 Otherwise we're building partial symtabs and are just interested in
18171 finding include files mentioned by the line number program. */
18172 bool m_record_lines_p;
18174 /* The line number header. */
18175 line_header *m_line_header;
18177 /* These are part of the standard DWARF line number state machine,
18178 and initialized according to the DWARF spec. */
18180 unsigned char m_op_index = 0;
18181 /* The line table index (1-based) of the current file. */
18182 file_name_index m_file = (file_name_index) 1;
18183 unsigned int m_line = 1;
18185 /* These are initialized in the constructor. */
18187 CORE_ADDR m_address;
18189 unsigned int m_discriminator;
18191 /* Additional bits of state we need to track. */
18193 /* The last file that we called dwarf2_start_subfile for.
18194 This is only used for TLLs. */
18195 unsigned int m_last_file = 0;
18196 /* The last file a line number was recorded for. */
18197 struct subfile *m_last_subfile = NULL;
18199 /* The function to call to record a line. */
18200 record_line_ftype *m_record_line_callback = NULL;
18202 /* The last line number that was recorded, used to coalesce
18203 consecutive entries for the same line. This can happen, for
18204 example, when discriminators are present. PR 17276. */
18205 unsigned int m_last_line = 0;
18206 bool m_line_has_non_zero_discriminator = false;
18210 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18212 CORE_ADDR addr_adj = (((m_op_index + adjust)
18213 / m_line_header->maximum_ops_per_instruction)
18214 * m_line_header->minimum_instruction_length);
18215 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18216 m_op_index = ((m_op_index + adjust)
18217 % m_line_header->maximum_ops_per_instruction);
18221 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18223 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18224 CORE_ADDR addr_adj = (((m_op_index
18225 + (adj_opcode / m_line_header->line_range))
18226 / m_line_header->maximum_ops_per_instruction)
18227 * m_line_header->minimum_instruction_length);
18228 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18229 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18230 % m_line_header->maximum_ops_per_instruction);
18232 int line_delta = (m_line_header->line_base
18233 + (adj_opcode % m_line_header->line_range));
18234 advance_line (line_delta);
18235 record_line (false);
18236 m_discriminator = 0;
18240 lnp_state_machine::handle_set_file (file_name_index file)
18244 const file_entry *fe = current_file ();
18246 dwarf2_debug_line_missing_file_complaint ();
18247 else if (m_record_lines_p)
18249 const char *dir = fe->include_dir (m_line_header);
18251 m_last_subfile = current_subfile;
18252 m_line_has_non_zero_discriminator = m_discriminator != 0;
18253 dwarf2_start_subfile (fe->name, dir);
18258 lnp_state_machine::handle_const_add_pc ()
18261 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18264 = (((m_op_index + adjust)
18265 / m_line_header->maximum_ops_per_instruction)
18266 * m_line_header->minimum_instruction_length);
18268 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18269 m_op_index = ((m_op_index + adjust)
18270 % m_line_header->maximum_ops_per_instruction);
18273 /* Ignore this record_line request. */
18276 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18281 /* Return non-zero if we should add LINE to the line number table.
18282 LINE is the line to add, LAST_LINE is the last line that was added,
18283 LAST_SUBFILE is the subfile for LAST_LINE.
18284 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18285 had a non-zero discriminator.
18287 We have to be careful in the presence of discriminators.
18288 E.g., for this line:
18290 for (i = 0; i < 100000; i++);
18292 clang can emit four line number entries for that one line,
18293 each with a different discriminator.
18294 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18296 However, we want gdb to coalesce all four entries into one.
18297 Otherwise the user could stepi into the middle of the line and
18298 gdb would get confused about whether the pc really was in the
18299 middle of the line.
18301 Things are further complicated by the fact that two consecutive
18302 line number entries for the same line is a heuristic used by gcc
18303 to denote the end of the prologue. So we can't just discard duplicate
18304 entries, we have to be selective about it. The heuristic we use is
18305 that we only collapse consecutive entries for the same line if at least
18306 one of those entries has a non-zero discriminator. PR 17276.
18308 Note: Addresses in the line number state machine can never go backwards
18309 within one sequence, thus this coalescing is ok. */
18312 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18313 int line_has_non_zero_discriminator,
18314 struct subfile *last_subfile)
18316 if (current_subfile != last_subfile)
18318 if (line != last_line)
18320 /* Same line for the same file that we've seen already.
18321 As a last check, for pr 17276, only record the line if the line
18322 has never had a non-zero discriminator. */
18323 if (!line_has_non_zero_discriminator)
18328 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18329 in the line table of subfile SUBFILE. */
18332 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18333 unsigned int line, CORE_ADDR address,
18334 record_line_ftype p_record_line)
18336 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18338 if (dwarf_line_debug)
18340 fprintf_unfiltered (gdb_stdlog,
18341 "Recording line %u, file %s, address %s\n",
18342 line, lbasename (subfile->name),
18343 paddress (gdbarch, address));
18346 (*p_record_line) (subfile, line, addr);
18349 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18350 Mark the end of a set of line number records.
18351 The arguments are the same as for dwarf_record_line_1.
18352 If SUBFILE is NULL the request is ignored. */
18355 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18356 CORE_ADDR address, record_line_ftype p_record_line)
18358 if (subfile == NULL)
18361 if (dwarf_line_debug)
18363 fprintf_unfiltered (gdb_stdlog,
18364 "Finishing current line, file %s, address %s\n",
18365 lbasename (subfile->name),
18366 paddress (gdbarch, address));
18369 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18373 lnp_state_machine::record_line (bool end_sequence)
18375 if (dwarf_line_debug)
18377 fprintf_unfiltered (gdb_stdlog,
18378 "Processing actual line %u: file %u,"
18379 " address %s, is_stmt %u, discrim %u\n",
18380 m_line, to_underlying (m_file),
18381 paddress (m_gdbarch, m_address),
18382 m_is_stmt, m_discriminator);
18385 file_entry *fe = current_file ();
18388 dwarf2_debug_line_missing_file_complaint ();
18389 /* For now we ignore lines not starting on an instruction boundary.
18390 But not when processing end_sequence for compatibility with the
18391 previous version of the code. */
18392 else if (m_op_index == 0 || end_sequence)
18394 fe->included_p = 1;
18395 if (m_record_lines_p && m_is_stmt)
18397 if (m_last_subfile != current_subfile || end_sequence)
18399 dwarf_finish_line (m_gdbarch, m_last_subfile,
18400 m_address, m_record_line_callback);
18405 if (dwarf_record_line_p (m_line, m_last_line,
18406 m_line_has_non_zero_discriminator,
18409 dwarf_record_line_1 (m_gdbarch, current_subfile,
18411 m_record_line_callback);
18413 m_last_subfile = current_subfile;
18414 m_last_line = m_line;
18420 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18421 bool record_lines_p)
18424 m_record_lines_p = record_lines_p;
18425 m_line_header = lh;
18427 m_record_line_callback = ::record_line;
18429 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18430 was a line entry for it so that the backend has a chance to adjust it
18431 and also record it in case it needs it. This is currently used by MIPS
18432 code, cf. `mips_adjust_dwarf2_line'. */
18433 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18434 m_is_stmt = lh->default_is_stmt;
18435 m_discriminator = 0;
18439 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18440 const gdb_byte *line_ptr,
18441 CORE_ADDR lowpc, CORE_ADDR address)
18443 /* If address < lowpc then it's not a usable value, it's outside the
18444 pc range of the CU. However, we restrict the test to only address
18445 values of zero to preserve GDB's previous behaviour which is to
18446 handle the specific case of a function being GC'd by the linker. */
18448 if (address == 0 && address < lowpc)
18450 /* This line table is for a function which has been
18451 GCd by the linker. Ignore it. PR gdb/12528 */
18453 struct objfile *objfile = cu->objfile;
18454 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18456 complaint (&symfile_complaints,
18457 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18458 line_offset, objfile_name (objfile));
18459 m_record_line_callback = noop_record_line;
18460 /* Note: record_line_callback is left as noop_record_line until
18461 we see DW_LNE_end_sequence. */
18465 /* Subroutine of dwarf_decode_lines to simplify it.
18466 Process the line number information in LH.
18467 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18468 program in order to set included_p for every referenced header. */
18471 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18472 const int decode_for_pst_p, CORE_ADDR lowpc)
18474 const gdb_byte *line_ptr, *extended_end;
18475 const gdb_byte *line_end;
18476 unsigned int bytes_read, extended_len;
18477 unsigned char op_code, extended_op;
18478 CORE_ADDR baseaddr;
18479 struct objfile *objfile = cu->objfile;
18480 bfd *abfd = objfile->obfd;
18481 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18482 /* True if we're recording line info (as opposed to building partial
18483 symtabs and just interested in finding include files mentioned by
18484 the line number program). */
18485 bool record_lines_p = !decode_for_pst_p;
18487 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18489 line_ptr = lh->statement_program_start;
18490 line_end = lh->statement_program_end;
18492 /* Read the statement sequences until there's nothing left. */
18493 while (line_ptr < line_end)
18495 /* The DWARF line number program state machine. Reset the state
18496 machine at the start of each sequence. */
18497 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18498 bool end_sequence = false;
18500 if (record_lines_p)
18502 /* Start a subfile for the current file of the state
18504 const file_entry *fe = state_machine.current_file ();
18507 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
18510 /* Decode the table. */
18511 while (line_ptr < line_end && !end_sequence)
18513 op_code = read_1_byte (abfd, line_ptr);
18516 if (op_code >= lh->opcode_base)
18518 /* Special opcode. */
18519 state_machine.handle_special_opcode (op_code);
18521 else switch (op_code)
18523 case DW_LNS_extended_op:
18524 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18526 line_ptr += bytes_read;
18527 extended_end = line_ptr + extended_len;
18528 extended_op = read_1_byte (abfd, line_ptr);
18530 switch (extended_op)
18532 case DW_LNE_end_sequence:
18533 state_machine.handle_end_sequence ();
18534 end_sequence = true;
18536 case DW_LNE_set_address:
18539 = read_address (abfd, line_ptr, cu, &bytes_read);
18540 line_ptr += bytes_read;
18542 state_machine.check_line_address (cu, line_ptr,
18544 state_machine.handle_set_address (baseaddr, address);
18547 case DW_LNE_define_file:
18549 const char *cur_file;
18550 unsigned int mod_time, length;
18553 cur_file = read_direct_string (abfd, line_ptr,
18555 line_ptr += bytes_read;
18556 dindex = (dir_index)
18557 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18558 line_ptr += bytes_read;
18560 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18561 line_ptr += bytes_read;
18563 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18564 line_ptr += bytes_read;
18565 lh->add_file_name (cur_file, dindex, mod_time, length);
18568 case DW_LNE_set_discriminator:
18570 /* The discriminator is not interesting to the
18571 debugger; just ignore it. We still need to
18572 check its value though:
18573 if there are consecutive entries for the same
18574 (non-prologue) line we want to coalesce them.
18577 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18578 line_ptr += bytes_read;
18580 state_machine.handle_set_discriminator (discr);
18584 complaint (&symfile_complaints,
18585 _("mangled .debug_line section"));
18588 /* Make sure that we parsed the extended op correctly. If e.g.
18589 we expected a different address size than the producer used,
18590 we may have read the wrong number of bytes. */
18591 if (line_ptr != extended_end)
18593 complaint (&symfile_complaints,
18594 _("mangled .debug_line section"));
18599 state_machine.handle_copy ();
18601 case DW_LNS_advance_pc:
18604 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18605 line_ptr += bytes_read;
18607 state_machine.handle_advance_pc (adjust);
18610 case DW_LNS_advance_line:
18613 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18614 line_ptr += bytes_read;
18616 state_machine.handle_advance_line (line_delta);
18619 case DW_LNS_set_file:
18621 file_name_index file
18622 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18624 line_ptr += bytes_read;
18626 state_machine.handle_set_file (file);
18629 case DW_LNS_set_column:
18630 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18631 line_ptr += bytes_read;
18633 case DW_LNS_negate_stmt:
18634 state_machine.handle_negate_stmt ();
18636 case DW_LNS_set_basic_block:
18638 /* Add to the address register of the state machine the
18639 address increment value corresponding to special opcode
18640 255. I.e., this value is scaled by the minimum
18641 instruction length since special opcode 255 would have
18642 scaled the increment. */
18643 case DW_LNS_const_add_pc:
18644 state_machine.handle_const_add_pc ();
18646 case DW_LNS_fixed_advance_pc:
18648 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18651 state_machine.handle_fixed_advance_pc (addr_adj);
18656 /* Unknown standard opcode, ignore it. */
18659 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18661 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18662 line_ptr += bytes_read;
18669 dwarf2_debug_line_missing_end_sequence_complaint ();
18671 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18672 in which case we still finish recording the last line). */
18673 state_machine.record_line (true);
18677 /* Decode the Line Number Program (LNP) for the given line_header
18678 structure and CU. The actual information extracted and the type
18679 of structures created from the LNP depends on the value of PST.
18681 1. If PST is NULL, then this procedure uses the data from the program
18682 to create all necessary symbol tables, and their linetables.
18684 2. If PST is not NULL, this procedure reads the program to determine
18685 the list of files included by the unit represented by PST, and
18686 builds all the associated partial symbol tables.
18688 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18689 It is used for relative paths in the line table.
18690 NOTE: When processing partial symtabs (pst != NULL),
18691 comp_dir == pst->dirname.
18693 NOTE: It is important that psymtabs have the same file name (via strcmp)
18694 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18695 symtab we don't use it in the name of the psymtabs we create.
18696 E.g. expand_line_sal requires this when finding psymtabs to expand.
18697 A good testcase for this is mb-inline.exp.
18699 LOWPC is the lowest address in CU (or 0 if not known).
18701 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18702 for its PC<->lines mapping information. Otherwise only the filename
18703 table is read in. */
18706 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18707 struct dwarf2_cu *cu, struct partial_symtab *pst,
18708 CORE_ADDR lowpc, int decode_mapping)
18710 struct objfile *objfile = cu->objfile;
18711 const int decode_for_pst_p = (pst != NULL);
18713 if (decode_mapping)
18714 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18716 if (decode_for_pst_p)
18720 /* Now that we're done scanning the Line Header Program, we can
18721 create the psymtab of each included file. */
18722 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
18723 if (lh->file_names[file_index].included_p == 1)
18725 const char *include_name =
18726 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18727 if (include_name != NULL)
18728 dwarf2_create_include_psymtab (include_name, pst, objfile);
18733 /* Make sure a symtab is created for every file, even files
18734 which contain only variables (i.e. no code with associated
18736 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18739 for (i = 0; i < lh->file_names.size (); i++)
18741 file_entry &fe = lh->file_names[i];
18743 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
18745 if (current_subfile->symtab == NULL)
18747 current_subfile->symtab
18748 = allocate_symtab (cust, current_subfile->name);
18750 fe.symtab = current_subfile->symtab;
18755 /* Start a subfile for DWARF. FILENAME is the name of the file and
18756 DIRNAME the name of the source directory which contains FILENAME
18757 or NULL if not known.
18758 This routine tries to keep line numbers from identical absolute and
18759 relative file names in a common subfile.
18761 Using the `list' example from the GDB testsuite, which resides in
18762 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18763 of /srcdir/list0.c yields the following debugging information for list0.c:
18765 DW_AT_name: /srcdir/list0.c
18766 DW_AT_comp_dir: /compdir
18767 files.files[0].name: list0.h
18768 files.files[0].dir: /srcdir
18769 files.files[1].name: list0.c
18770 files.files[1].dir: /srcdir
18772 The line number information for list0.c has to end up in a single
18773 subfile, so that `break /srcdir/list0.c:1' works as expected.
18774 start_subfile will ensure that this happens provided that we pass the
18775 concatenation of files.files[1].dir and files.files[1].name as the
18779 dwarf2_start_subfile (const char *filename, const char *dirname)
18783 /* In order not to lose the line information directory,
18784 we concatenate it to the filename when it makes sense.
18785 Note that the Dwarf3 standard says (speaking of filenames in line
18786 information): ``The directory index is ignored for file names
18787 that represent full path names''. Thus ignoring dirname in the
18788 `else' branch below isn't an issue. */
18790 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18792 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18796 start_subfile (filename);
18802 /* Start a symtab for DWARF.
18803 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18805 static struct compunit_symtab *
18806 dwarf2_start_symtab (struct dwarf2_cu *cu,
18807 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18809 struct compunit_symtab *cust
18810 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18812 record_debugformat ("DWARF 2");
18813 record_producer (cu->producer);
18815 /* We assume that we're processing GCC output. */
18816 processing_gcc_compilation = 2;
18818 cu->processing_has_namespace_info = 0;
18824 var_decode_location (struct attribute *attr, struct symbol *sym,
18825 struct dwarf2_cu *cu)
18827 struct objfile *objfile = cu->objfile;
18828 struct comp_unit_head *cu_header = &cu->header;
18830 /* NOTE drow/2003-01-30: There used to be a comment and some special
18831 code here to turn a symbol with DW_AT_external and a
18832 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18833 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18834 with some versions of binutils) where shared libraries could have
18835 relocations against symbols in their debug information - the
18836 minimal symbol would have the right address, but the debug info
18837 would not. It's no longer necessary, because we will explicitly
18838 apply relocations when we read in the debug information now. */
18840 /* A DW_AT_location attribute with no contents indicates that a
18841 variable has been optimized away. */
18842 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18844 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18848 /* Handle one degenerate form of location expression specially, to
18849 preserve GDB's previous behavior when section offsets are
18850 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18851 then mark this symbol as LOC_STATIC. */
18853 if (attr_form_is_block (attr)
18854 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18855 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18856 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18857 && (DW_BLOCK (attr)->size
18858 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18860 unsigned int dummy;
18862 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18863 SYMBOL_VALUE_ADDRESS (sym) =
18864 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18866 SYMBOL_VALUE_ADDRESS (sym) =
18867 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18868 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18869 fixup_symbol_section (sym, objfile);
18870 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18871 SYMBOL_SECTION (sym));
18875 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18876 expression evaluator, and use LOC_COMPUTED only when necessary
18877 (i.e. when the value of a register or memory location is
18878 referenced, or a thread-local block, etc.). Then again, it might
18879 not be worthwhile. I'm assuming that it isn't unless performance
18880 or memory numbers show me otherwise. */
18882 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18884 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18885 cu->has_loclist = 1;
18888 /* Given a pointer to a DWARF information entry, figure out if we need
18889 to make a symbol table entry for it, and if so, create a new entry
18890 and return a pointer to it.
18891 If TYPE is NULL, determine symbol type from the die, otherwise
18892 used the passed type.
18893 If SPACE is not NULL, use it to hold the new symbol. If it is
18894 NULL, allocate a new symbol on the objfile's obstack. */
18896 static struct symbol *
18897 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18898 struct symbol *space)
18900 struct objfile *objfile = cu->objfile;
18901 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18902 struct symbol *sym = NULL;
18904 struct attribute *attr = NULL;
18905 struct attribute *attr2 = NULL;
18906 CORE_ADDR baseaddr;
18907 struct pending **list_to_add = NULL;
18909 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18911 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18913 name = dwarf2_name (die, cu);
18916 const char *linkagename;
18917 int suppress_add = 0;
18922 sym = allocate_symbol (objfile);
18923 OBJSTAT (objfile, n_syms++);
18925 /* Cache this symbol's name and the name's demangled form (if any). */
18926 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18927 linkagename = dwarf2_physname (name, die, cu);
18928 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18930 /* Fortran does not have mangling standard and the mangling does differ
18931 between gfortran, iFort etc. */
18932 if (cu->language == language_fortran
18933 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18934 symbol_set_demangled_name (&(sym->ginfo),
18935 dwarf2_full_name (name, die, cu),
18938 /* Default assumptions.
18939 Use the passed type or decode it from the die. */
18940 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18941 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18943 SYMBOL_TYPE (sym) = type;
18945 SYMBOL_TYPE (sym) = die_type (die, cu);
18946 attr = dwarf2_attr (die,
18947 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18951 SYMBOL_LINE (sym) = DW_UNSND (attr);
18954 attr = dwarf2_attr (die,
18955 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18959 file_name_index file_index = (file_name_index) DW_UNSND (attr);
18960 struct file_entry *fe;
18962 if (cu->line_header != NULL)
18963 fe = cu->line_header->file_name_at (file_index);
18968 complaint (&symfile_complaints,
18969 _("file index out of range"));
18971 symbol_set_symtab (sym, fe->symtab);
18977 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18982 addr = attr_value_as_address (attr);
18983 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18984 SYMBOL_VALUE_ADDRESS (sym) = addr;
18986 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18987 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18988 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18989 add_symbol_to_list (sym, cu->list_in_scope);
18991 case DW_TAG_subprogram:
18992 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18994 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18995 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18996 if ((attr2 && (DW_UNSND (attr2) != 0))
18997 || cu->language == language_ada)
18999 /* Subprograms marked external are stored as a global symbol.
19000 Ada subprograms, whether marked external or not, are always
19001 stored as a global symbol, because we want to be able to
19002 access them globally. For instance, we want to be able
19003 to break on a nested subprogram without having to
19004 specify the context. */
19005 list_to_add = &global_symbols;
19009 list_to_add = cu->list_in_scope;
19012 case DW_TAG_inlined_subroutine:
19013 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19015 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19016 SYMBOL_INLINED (sym) = 1;
19017 list_to_add = cu->list_in_scope;
19019 case DW_TAG_template_value_param:
19021 /* Fall through. */
19022 case DW_TAG_constant:
19023 case DW_TAG_variable:
19024 case DW_TAG_member:
19025 /* Compilation with minimal debug info may result in
19026 variables with missing type entries. Change the
19027 misleading `void' type to something sensible. */
19028 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19030 = objfile_type (objfile)->nodebug_data_symbol;
19032 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19033 /* In the case of DW_TAG_member, we should only be called for
19034 static const members. */
19035 if (die->tag == DW_TAG_member)
19037 /* dwarf2_add_field uses die_is_declaration,
19038 so we do the same. */
19039 gdb_assert (die_is_declaration (die, cu));
19044 dwarf2_const_value (attr, sym, cu);
19045 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19048 if (attr2 && (DW_UNSND (attr2) != 0))
19049 list_to_add = &global_symbols;
19051 list_to_add = cu->list_in_scope;
19055 attr = dwarf2_attr (die, DW_AT_location, cu);
19058 var_decode_location (attr, sym, cu);
19059 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19061 /* Fortran explicitly imports any global symbols to the local
19062 scope by DW_TAG_common_block. */
19063 if (cu->language == language_fortran && die->parent
19064 && die->parent->tag == DW_TAG_common_block)
19067 if (SYMBOL_CLASS (sym) == LOC_STATIC
19068 && SYMBOL_VALUE_ADDRESS (sym) == 0
19069 && !dwarf2_per_objfile->has_section_at_zero)
19071 /* When a static variable is eliminated by the linker,
19072 the corresponding debug information is not stripped
19073 out, but the variable address is set to null;
19074 do not add such variables into symbol table. */
19076 else if (attr2 && (DW_UNSND (attr2) != 0))
19078 /* Workaround gfortran PR debug/40040 - it uses
19079 DW_AT_location for variables in -fPIC libraries which may
19080 get overriden by other libraries/executable and get
19081 a different address. Resolve it by the minimal symbol
19082 which may come from inferior's executable using copy
19083 relocation. Make this workaround only for gfortran as for
19084 other compilers GDB cannot guess the minimal symbol
19085 Fortran mangling kind. */
19086 if (cu->language == language_fortran && die->parent
19087 && die->parent->tag == DW_TAG_module
19089 && startswith (cu->producer, "GNU Fortran"))
19090 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19092 /* A variable with DW_AT_external is never static,
19093 but it may be block-scoped. */
19094 list_to_add = (cu->list_in_scope == &file_symbols
19095 ? &global_symbols : cu->list_in_scope);
19098 list_to_add = cu->list_in_scope;
19102 /* We do not know the address of this symbol.
19103 If it is an external symbol and we have type information
19104 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19105 The address of the variable will then be determined from
19106 the minimal symbol table whenever the variable is
19108 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19110 /* Fortran explicitly imports any global symbols to the local
19111 scope by DW_TAG_common_block. */
19112 if (cu->language == language_fortran && die->parent
19113 && die->parent->tag == DW_TAG_common_block)
19115 /* SYMBOL_CLASS doesn't matter here because
19116 read_common_block is going to reset it. */
19118 list_to_add = cu->list_in_scope;
19120 else if (attr2 && (DW_UNSND (attr2) != 0)
19121 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19123 /* A variable with DW_AT_external is never static, but it
19124 may be block-scoped. */
19125 list_to_add = (cu->list_in_scope == &file_symbols
19126 ? &global_symbols : cu->list_in_scope);
19128 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19130 else if (!die_is_declaration (die, cu))
19132 /* Use the default LOC_OPTIMIZED_OUT class. */
19133 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19135 list_to_add = cu->list_in_scope;
19139 case DW_TAG_formal_parameter:
19140 /* If we are inside a function, mark this as an argument. If
19141 not, we might be looking at an argument to an inlined function
19142 when we do not have enough information to show inlined frames;
19143 pretend it's a local variable in that case so that the user can
19145 if (context_stack_depth > 0
19146 && context_stack[context_stack_depth - 1].name != NULL)
19147 SYMBOL_IS_ARGUMENT (sym) = 1;
19148 attr = dwarf2_attr (die, DW_AT_location, cu);
19151 var_decode_location (attr, sym, cu);
19153 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19156 dwarf2_const_value (attr, sym, cu);
19159 list_to_add = cu->list_in_scope;
19161 case DW_TAG_unspecified_parameters:
19162 /* From varargs functions; gdb doesn't seem to have any
19163 interest in this information, so just ignore it for now.
19166 case DW_TAG_template_type_param:
19168 /* Fall through. */
19169 case DW_TAG_class_type:
19170 case DW_TAG_interface_type:
19171 case DW_TAG_structure_type:
19172 case DW_TAG_union_type:
19173 case DW_TAG_set_type:
19174 case DW_TAG_enumeration_type:
19175 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19176 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19179 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19180 really ever be static objects: otherwise, if you try
19181 to, say, break of a class's method and you're in a file
19182 which doesn't mention that class, it won't work unless
19183 the check for all static symbols in lookup_symbol_aux
19184 saves you. See the OtherFileClass tests in
19185 gdb.c++/namespace.exp. */
19189 list_to_add = (cu->list_in_scope == &file_symbols
19190 && cu->language == language_cplus
19191 ? &global_symbols : cu->list_in_scope);
19193 /* The semantics of C++ state that "struct foo {
19194 ... }" also defines a typedef for "foo". */
19195 if (cu->language == language_cplus
19196 || cu->language == language_ada
19197 || cu->language == language_d
19198 || cu->language == language_rust)
19200 /* The symbol's name is already allocated along
19201 with this objfile, so we don't need to
19202 duplicate it for the type. */
19203 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19204 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19209 case DW_TAG_typedef:
19210 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19211 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19212 list_to_add = cu->list_in_scope;
19214 case DW_TAG_base_type:
19215 case DW_TAG_subrange_type:
19216 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19217 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19218 list_to_add = cu->list_in_scope;
19220 case DW_TAG_enumerator:
19221 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19224 dwarf2_const_value (attr, sym, cu);
19227 /* NOTE: carlton/2003-11-10: See comment above in the
19228 DW_TAG_class_type, etc. block. */
19230 list_to_add = (cu->list_in_scope == &file_symbols
19231 && cu->language == language_cplus
19232 ? &global_symbols : cu->list_in_scope);
19235 case DW_TAG_imported_declaration:
19236 case DW_TAG_namespace:
19237 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19238 list_to_add = &global_symbols;
19240 case DW_TAG_module:
19241 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19242 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19243 list_to_add = &global_symbols;
19245 case DW_TAG_common_block:
19246 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19247 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19248 add_symbol_to_list (sym, cu->list_in_scope);
19251 /* Not a tag we recognize. Hopefully we aren't processing
19252 trash data, but since we must specifically ignore things
19253 we don't recognize, there is nothing else we should do at
19255 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19256 dwarf_tag_name (die->tag));
19262 sym->hash_next = objfile->template_symbols;
19263 objfile->template_symbols = sym;
19264 list_to_add = NULL;
19267 if (list_to_add != NULL)
19268 add_symbol_to_list (sym, list_to_add);
19270 /* For the benefit of old versions of GCC, check for anonymous
19271 namespaces based on the demangled name. */
19272 if (!cu->processing_has_namespace_info
19273 && cu->language == language_cplus)
19274 cp_scan_for_anonymous_namespaces (sym, objfile);
19279 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19281 static struct symbol *
19282 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19284 return new_symbol_full (die, type, cu, NULL);
19287 /* Given an attr with a DW_FORM_dataN value in host byte order,
19288 zero-extend it as appropriate for the symbol's type. The DWARF
19289 standard (v4) is not entirely clear about the meaning of using
19290 DW_FORM_dataN for a constant with a signed type, where the type is
19291 wider than the data. The conclusion of a discussion on the DWARF
19292 list was that this is unspecified. We choose to always zero-extend
19293 because that is the interpretation long in use by GCC. */
19296 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19297 struct dwarf2_cu *cu, LONGEST *value, int bits)
19299 struct objfile *objfile = cu->objfile;
19300 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19301 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19302 LONGEST l = DW_UNSND (attr);
19304 if (bits < sizeof (*value) * 8)
19306 l &= ((LONGEST) 1 << bits) - 1;
19309 else if (bits == sizeof (*value) * 8)
19313 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19314 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19321 /* Read a constant value from an attribute. Either set *VALUE, or if
19322 the value does not fit in *VALUE, set *BYTES - either already
19323 allocated on the objfile obstack, or newly allocated on OBSTACK,
19324 or, set *BATON, if we translated the constant to a location
19328 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19329 const char *name, struct obstack *obstack,
19330 struct dwarf2_cu *cu,
19331 LONGEST *value, const gdb_byte **bytes,
19332 struct dwarf2_locexpr_baton **baton)
19334 struct objfile *objfile = cu->objfile;
19335 struct comp_unit_head *cu_header = &cu->header;
19336 struct dwarf_block *blk;
19337 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19338 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19344 switch (attr->form)
19347 case DW_FORM_GNU_addr_index:
19351 if (TYPE_LENGTH (type) != cu_header->addr_size)
19352 dwarf2_const_value_length_mismatch_complaint (name,
19353 cu_header->addr_size,
19354 TYPE_LENGTH (type));
19355 /* Symbols of this form are reasonably rare, so we just
19356 piggyback on the existing location code rather than writing
19357 a new implementation of symbol_computed_ops. */
19358 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19359 (*baton)->per_cu = cu->per_cu;
19360 gdb_assert ((*baton)->per_cu);
19362 (*baton)->size = 2 + cu_header->addr_size;
19363 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19364 (*baton)->data = data;
19366 data[0] = DW_OP_addr;
19367 store_unsigned_integer (&data[1], cu_header->addr_size,
19368 byte_order, DW_ADDR (attr));
19369 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19372 case DW_FORM_string:
19374 case DW_FORM_GNU_str_index:
19375 case DW_FORM_GNU_strp_alt:
19376 /* DW_STRING is already allocated on the objfile obstack, point
19378 *bytes = (const gdb_byte *) DW_STRING (attr);
19380 case DW_FORM_block1:
19381 case DW_FORM_block2:
19382 case DW_FORM_block4:
19383 case DW_FORM_block:
19384 case DW_FORM_exprloc:
19385 case DW_FORM_data16:
19386 blk = DW_BLOCK (attr);
19387 if (TYPE_LENGTH (type) != blk->size)
19388 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19389 TYPE_LENGTH (type));
19390 *bytes = blk->data;
19393 /* The DW_AT_const_value attributes are supposed to carry the
19394 symbol's value "represented as it would be on the target
19395 architecture." By the time we get here, it's already been
19396 converted to host endianness, so we just need to sign- or
19397 zero-extend it as appropriate. */
19398 case DW_FORM_data1:
19399 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19401 case DW_FORM_data2:
19402 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19404 case DW_FORM_data4:
19405 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19407 case DW_FORM_data8:
19408 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19411 case DW_FORM_sdata:
19412 *value = DW_SND (attr);
19415 case DW_FORM_udata:
19416 *value = DW_UNSND (attr);
19420 complaint (&symfile_complaints,
19421 _("unsupported const value attribute form: '%s'"),
19422 dwarf_form_name (attr->form));
19429 /* Copy constant value from an attribute to a symbol. */
19432 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19433 struct dwarf2_cu *cu)
19435 struct objfile *objfile = cu->objfile;
19437 const gdb_byte *bytes;
19438 struct dwarf2_locexpr_baton *baton;
19440 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19441 SYMBOL_PRINT_NAME (sym),
19442 &objfile->objfile_obstack, cu,
19443 &value, &bytes, &baton);
19447 SYMBOL_LOCATION_BATON (sym) = baton;
19448 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19450 else if (bytes != NULL)
19452 SYMBOL_VALUE_BYTES (sym) = bytes;
19453 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19457 SYMBOL_VALUE (sym) = value;
19458 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19462 /* Return the type of the die in question using its DW_AT_type attribute. */
19464 static struct type *
19465 die_type (struct die_info *die, struct dwarf2_cu *cu)
19467 struct attribute *type_attr;
19469 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19472 /* A missing DW_AT_type represents a void type. */
19473 return objfile_type (cu->objfile)->builtin_void;
19476 return lookup_die_type (die, type_attr, cu);
19479 /* True iff CU's producer generates GNAT Ada auxiliary information
19480 that allows to find parallel types through that information instead
19481 of having to do expensive parallel lookups by type name. */
19484 need_gnat_info (struct dwarf2_cu *cu)
19486 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19487 of GNAT produces this auxiliary information, without any indication
19488 that it is produced. Part of enhancing the FSF version of GNAT
19489 to produce that information will be to put in place an indicator
19490 that we can use in order to determine whether the descriptive type
19491 info is available or not. One suggestion that has been made is
19492 to use a new attribute, attached to the CU die. For now, assume
19493 that the descriptive type info is not available. */
19497 /* Return the auxiliary type of the die in question using its
19498 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19499 attribute is not present. */
19501 static struct type *
19502 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19504 struct attribute *type_attr;
19506 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19510 return lookup_die_type (die, type_attr, cu);
19513 /* If DIE has a descriptive_type attribute, then set the TYPE's
19514 descriptive type accordingly. */
19517 set_descriptive_type (struct type *type, struct die_info *die,
19518 struct dwarf2_cu *cu)
19520 struct type *descriptive_type = die_descriptive_type (die, cu);
19522 if (descriptive_type)
19524 ALLOCATE_GNAT_AUX_TYPE (type);
19525 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19529 /* Return the containing type of the die in question using its
19530 DW_AT_containing_type attribute. */
19532 static struct type *
19533 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19535 struct attribute *type_attr;
19537 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19539 error (_("Dwarf Error: Problem turning containing type into gdb type "
19540 "[in module %s]"), objfile_name (cu->objfile));
19542 return lookup_die_type (die, type_attr, cu);
19545 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19547 static struct type *
19548 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19550 struct objfile *objfile = dwarf2_per_objfile->objfile;
19551 char *message, *saved;
19553 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19554 objfile_name (objfile),
19555 to_underlying (cu->header.sect_off),
19556 to_underlying (die->sect_off));
19557 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19558 message, strlen (message));
19561 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19564 /* Look up the type of DIE in CU using its type attribute ATTR.
19565 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19566 DW_AT_containing_type.
19567 If there is no type substitute an error marker. */
19569 static struct type *
19570 lookup_die_type (struct die_info *die, const struct attribute *attr,
19571 struct dwarf2_cu *cu)
19573 struct objfile *objfile = cu->objfile;
19574 struct type *this_type;
19576 gdb_assert (attr->name == DW_AT_type
19577 || attr->name == DW_AT_GNAT_descriptive_type
19578 || attr->name == DW_AT_containing_type);
19580 /* First see if we have it cached. */
19582 if (attr->form == DW_FORM_GNU_ref_alt)
19584 struct dwarf2_per_cu_data *per_cu;
19585 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19587 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19588 this_type = get_die_type_at_offset (sect_off, per_cu);
19590 else if (attr_form_is_ref (attr))
19592 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19594 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
19596 else if (attr->form == DW_FORM_ref_sig8)
19598 ULONGEST signature = DW_SIGNATURE (attr);
19600 return get_signatured_type (die, signature, cu);
19604 complaint (&symfile_complaints,
19605 _("Dwarf Error: Bad type attribute %s in DIE"
19606 " at 0x%x [in module %s]"),
19607 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
19608 objfile_name (objfile));
19609 return build_error_marker_type (cu, die);
19612 /* If not cached we need to read it in. */
19614 if (this_type == NULL)
19616 struct die_info *type_die = NULL;
19617 struct dwarf2_cu *type_cu = cu;
19619 if (attr_form_is_ref (attr))
19620 type_die = follow_die_ref (die, attr, &type_cu);
19621 if (type_die == NULL)
19622 return build_error_marker_type (cu, die);
19623 /* If we find the type now, it's probably because the type came
19624 from an inter-CU reference and the type's CU got expanded before
19626 this_type = read_type_die (type_die, type_cu);
19629 /* If we still don't have a type use an error marker. */
19631 if (this_type == NULL)
19632 return build_error_marker_type (cu, die);
19637 /* Return the type in DIE, CU.
19638 Returns NULL for invalid types.
19640 This first does a lookup in die_type_hash,
19641 and only reads the die in if necessary.
19643 NOTE: This can be called when reading in partial or full symbols. */
19645 static struct type *
19646 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19648 struct type *this_type;
19650 this_type = get_die_type (die, cu);
19654 return read_type_die_1 (die, cu);
19657 /* Read the type in DIE, CU.
19658 Returns NULL for invalid types. */
19660 static struct type *
19661 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19663 struct type *this_type = NULL;
19667 case DW_TAG_class_type:
19668 case DW_TAG_interface_type:
19669 case DW_TAG_structure_type:
19670 case DW_TAG_union_type:
19671 this_type = read_structure_type (die, cu);
19673 case DW_TAG_enumeration_type:
19674 this_type = read_enumeration_type (die, cu);
19676 case DW_TAG_subprogram:
19677 case DW_TAG_subroutine_type:
19678 case DW_TAG_inlined_subroutine:
19679 this_type = read_subroutine_type (die, cu);
19681 case DW_TAG_array_type:
19682 this_type = read_array_type (die, cu);
19684 case DW_TAG_set_type:
19685 this_type = read_set_type (die, cu);
19687 case DW_TAG_pointer_type:
19688 this_type = read_tag_pointer_type (die, cu);
19690 case DW_TAG_ptr_to_member_type:
19691 this_type = read_tag_ptr_to_member_type (die, cu);
19693 case DW_TAG_reference_type:
19694 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19696 case DW_TAG_rvalue_reference_type:
19697 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19699 case DW_TAG_const_type:
19700 this_type = read_tag_const_type (die, cu);
19702 case DW_TAG_volatile_type:
19703 this_type = read_tag_volatile_type (die, cu);
19705 case DW_TAG_restrict_type:
19706 this_type = read_tag_restrict_type (die, cu);
19708 case DW_TAG_string_type:
19709 this_type = read_tag_string_type (die, cu);
19711 case DW_TAG_typedef:
19712 this_type = read_typedef (die, cu);
19714 case DW_TAG_subrange_type:
19715 this_type = read_subrange_type (die, cu);
19717 case DW_TAG_base_type:
19718 this_type = read_base_type (die, cu);
19720 case DW_TAG_unspecified_type:
19721 this_type = read_unspecified_type (die, cu);
19723 case DW_TAG_namespace:
19724 this_type = read_namespace_type (die, cu);
19726 case DW_TAG_module:
19727 this_type = read_module_type (die, cu);
19729 case DW_TAG_atomic_type:
19730 this_type = read_tag_atomic_type (die, cu);
19733 complaint (&symfile_complaints,
19734 _("unexpected tag in read_type_die: '%s'"),
19735 dwarf_tag_name (die->tag));
19742 /* See if we can figure out if the class lives in a namespace. We do
19743 this by looking for a member function; its demangled name will
19744 contain namespace info, if there is any.
19745 Return the computed name or NULL.
19746 Space for the result is allocated on the objfile's obstack.
19747 This is the full-die version of guess_partial_die_structure_name.
19748 In this case we know DIE has no useful parent. */
19751 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19753 struct die_info *spec_die;
19754 struct dwarf2_cu *spec_cu;
19755 struct die_info *child;
19758 spec_die = die_specification (die, &spec_cu);
19759 if (spec_die != NULL)
19765 for (child = die->child;
19767 child = child->sibling)
19769 if (child->tag == DW_TAG_subprogram)
19771 const char *linkage_name;
19773 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19774 if (linkage_name == NULL)
19775 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19777 if (linkage_name != NULL)
19780 = language_class_name_from_physname (cu->language_defn,
19784 if (actual_name != NULL)
19786 const char *die_name = dwarf2_name (die, cu);
19788 if (die_name != NULL
19789 && strcmp (die_name, actual_name) != 0)
19791 /* Strip off the class name from the full name.
19792 We want the prefix. */
19793 int die_name_len = strlen (die_name);
19794 int actual_name_len = strlen (actual_name);
19796 /* Test for '::' as a sanity check. */
19797 if (actual_name_len > die_name_len + 2
19798 && actual_name[actual_name_len
19799 - die_name_len - 1] == ':')
19800 name = (char *) obstack_copy0 (
19801 &cu->objfile->per_bfd->storage_obstack,
19802 actual_name, actual_name_len - die_name_len - 2);
19805 xfree (actual_name);
19814 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19815 prefix part in such case. See
19816 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19818 static const char *
19819 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19821 struct attribute *attr;
19824 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19825 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19828 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19831 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19833 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19834 if (attr == NULL || DW_STRING (attr) == NULL)
19837 /* dwarf2_name had to be already called. */
19838 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19840 /* Strip the base name, keep any leading namespaces/classes. */
19841 base = strrchr (DW_STRING (attr), ':');
19842 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19845 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19847 &base[-1] - DW_STRING (attr));
19850 /* Return the name of the namespace/class that DIE is defined within,
19851 or "" if we can't tell. The caller should not xfree the result.
19853 For example, if we're within the method foo() in the following
19863 then determine_prefix on foo's die will return "N::C". */
19865 static const char *
19866 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19868 struct die_info *parent, *spec_die;
19869 struct dwarf2_cu *spec_cu;
19870 struct type *parent_type;
19871 const char *retval;
19873 if (cu->language != language_cplus
19874 && cu->language != language_fortran && cu->language != language_d
19875 && cu->language != language_rust)
19878 retval = anonymous_struct_prefix (die, cu);
19882 /* We have to be careful in the presence of DW_AT_specification.
19883 For example, with GCC 3.4, given the code
19887 // Definition of N::foo.
19891 then we'll have a tree of DIEs like this:
19893 1: DW_TAG_compile_unit
19894 2: DW_TAG_namespace // N
19895 3: DW_TAG_subprogram // declaration of N::foo
19896 4: DW_TAG_subprogram // definition of N::foo
19897 DW_AT_specification // refers to die #3
19899 Thus, when processing die #4, we have to pretend that we're in
19900 the context of its DW_AT_specification, namely the contex of die
19903 spec_die = die_specification (die, &spec_cu);
19904 if (spec_die == NULL)
19905 parent = die->parent;
19908 parent = spec_die->parent;
19912 if (parent == NULL)
19914 else if (parent->building_fullname)
19917 const char *parent_name;
19919 /* It has been seen on RealView 2.2 built binaries,
19920 DW_TAG_template_type_param types actually _defined_ as
19921 children of the parent class:
19924 template class <class Enum> Class{};
19925 Class<enum E> class_e;
19927 1: DW_TAG_class_type (Class)
19928 2: DW_TAG_enumeration_type (E)
19929 3: DW_TAG_enumerator (enum1:0)
19930 3: DW_TAG_enumerator (enum2:1)
19932 2: DW_TAG_template_type_param
19933 DW_AT_type DW_FORM_ref_udata (E)
19935 Besides being broken debug info, it can put GDB into an
19936 infinite loop. Consider:
19938 When we're building the full name for Class<E>, we'll start
19939 at Class, and go look over its template type parameters,
19940 finding E. We'll then try to build the full name of E, and
19941 reach here. We're now trying to build the full name of E,
19942 and look over the parent DIE for containing scope. In the
19943 broken case, if we followed the parent DIE of E, we'd again
19944 find Class, and once again go look at its template type
19945 arguments, etc., etc. Simply don't consider such parent die
19946 as source-level parent of this die (it can't be, the language
19947 doesn't allow it), and break the loop here. */
19948 name = dwarf2_name (die, cu);
19949 parent_name = dwarf2_name (parent, cu);
19950 complaint (&symfile_complaints,
19951 _("template param type '%s' defined within parent '%s'"),
19952 name ? name : "<unknown>",
19953 parent_name ? parent_name : "<unknown>");
19957 switch (parent->tag)
19959 case DW_TAG_namespace:
19960 parent_type = read_type_die (parent, cu);
19961 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19962 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19963 Work around this problem here. */
19964 if (cu->language == language_cplus
19965 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19967 /* We give a name to even anonymous namespaces. */
19968 return TYPE_TAG_NAME (parent_type);
19969 case DW_TAG_class_type:
19970 case DW_TAG_interface_type:
19971 case DW_TAG_structure_type:
19972 case DW_TAG_union_type:
19973 case DW_TAG_module:
19974 parent_type = read_type_die (parent, cu);
19975 if (TYPE_TAG_NAME (parent_type) != NULL)
19976 return TYPE_TAG_NAME (parent_type);
19978 /* An anonymous structure is only allowed non-static data
19979 members; no typedefs, no member functions, et cetera.
19980 So it does not need a prefix. */
19982 case DW_TAG_compile_unit:
19983 case DW_TAG_partial_unit:
19984 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19985 if (cu->language == language_cplus
19986 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19987 && die->child != NULL
19988 && (die->tag == DW_TAG_class_type
19989 || die->tag == DW_TAG_structure_type
19990 || die->tag == DW_TAG_union_type))
19992 char *name = guess_full_die_structure_name (die, cu);
19997 case DW_TAG_enumeration_type:
19998 parent_type = read_type_die (parent, cu);
19999 if (TYPE_DECLARED_CLASS (parent_type))
20001 if (TYPE_TAG_NAME (parent_type) != NULL)
20002 return TYPE_TAG_NAME (parent_type);
20005 /* Fall through. */
20007 return determine_prefix (parent, cu);
20011 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20012 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20013 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20014 an obconcat, otherwise allocate storage for the result. The CU argument is
20015 used to determine the language and hence, the appropriate separator. */
20017 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20020 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20021 int physname, struct dwarf2_cu *cu)
20023 const char *lead = "";
20026 if (suffix == NULL || suffix[0] == '\0'
20027 || prefix == NULL || prefix[0] == '\0')
20029 else if (cu->language == language_d)
20031 /* For D, the 'main' function could be defined in any module, but it
20032 should never be prefixed. */
20033 if (strcmp (suffix, "D main") == 0)
20041 else if (cu->language == language_fortran && physname)
20043 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20044 DW_AT_MIPS_linkage_name is preferred and used instead. */
20052 if (prefix == NULL)
20054 if (suffix == NULL)
20061 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20063 strcpy (retval, lead);
20064 strcat (retval, prefix);
20065 strcat (retval, sep);
20066 strcat (retval, suffix);
20071 /* We have an obstack. */
20072 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20076 /* Return sibling of die, NULL if no sibling. */
20078 static struct die_info *
20079 sibling_die (struct die_info *die)
20081 return die->sibling;
20084 /* Get name of a die, return NULL if not found. */
20086 static const char *
20087 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20088 struct obstack *obstack)
20090 if (name && cu->language == language_cplus)
20092 std::string canon_name = cp_canonicalize_string (name);
20094 if (!canon_name.empty ())
20096 if (canon_name != name)
20097 name = (const char *) obstack_copy0 (obstack,
20098 canon_name.c_str (),
20099 canon_name.length ());
20106 /* Get name of a die, return NULL if not found.
20107 Anonymous namespaces are converted to their magic string. */
20109 static const char *
20110 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20112 struct attribute *attr;
20114 attr = dwarf2_attr (die, DW_AT_name, cu);
20115 if ((!attr || !DW_STRING (attr))
20116 && die->tag != DW_TAG_namespace
20117 && die->tag != DW_TAG_class_type
20118 && die->tag != DW_TAG_interface_type
20119 && die->tag != DW_TAG_structure_type
20120 && die->tag != DW_TAG_union_type)
20125 case DW_TAG_compile_unit:
20126 case DW_TAG_partial_unit:
20127 /* Compilation units have a DW_AT_name that is a filename, not
20128 a source language identifier. */
20129 case DW_TAG_enumeration_type:
20130 case DW_TAG_enumerator:
20131 /* These tags always have simple identifiers already; no need
20132 to canonicalize them. */
20133 return DW_STRING (attr);
20135 case DW_TAG_namespace:
20136 if (attr != NULL && DW_STRING (attr) != NULL)
20137 return DW_STRING (attr);
20138 return CP_ANONYMOUS_NAMESPACE_STR;
20140 case DW_TAG_class_type:
20141 case DW_TAG_interface_type:
20142 case DW_TAG_structure_type:
20143 case DW_TAG_union_type:
20144 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20145 structures or unions. These were of the form "._%d" in GCC 4.1,
20146 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20147 and GCC 4.4. We work around this problem by ignoring these. */
20148 if (attr && DW_STRING (attr)
20149 && (startswith (DW_STRING (attr), "._")
20150 || startswith (DW_STRING (attr), "<anonymous")))
20153 /* GCC might emit a nameless typedef that has a linkage name. See
20154 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20155 if (!attr || DW_STRING (attr) == NULL)
20157 char *demangled = NULL;
20159 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20161 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20163 if (attr == NULL || DW_STRING (attr) == NULL)
20166 /* Avoid demangling DW_STRING (attr) the second time on a second
20167 call for the same DIE. */
20168 if (!DW_STRING_IS_CANONICAL (attr))
20169 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20175 /* FIXME: we already did this for the partial symbol... */
20178 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20179 demangled, strlen (demangled)));
20180 DW_STRING_IS_CANONICAL (attr) = 1;
20183 /* Strip any leading namespaces/classes, keep only the base name.
20184 DW_AT_name for named DIEs does not contain the prefixes. */
20185 base = strrchr (DW_STRING (attr), ':');
20186 if (base && base > DW_STRING (attr) && base[-1] == ':')
20189 return DW_STRING (attr);
20198 if (!DW_STRING_IS_CANONICAL (attr))
20201 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20202 &cu->objfile->per_bfd->storage_obstack);
20203 DW_STRING_IS_CANONICAL (attr) = 1;
20205 return DW_STRING (attr);
20208 /* Return the die that this die in an extension of, or NULL if there
20209 is none. *EXT_CU is the CU containing DIE on input, and the CU
20210 containing the return value on output. */
20212 static struct die_info *
20213 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20215 struct attribute *attr;
20217 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20221 return follow_die_ref (die, attr, ext_cu);
20224 /* Convert a DIE tag into its string name. */
20226 static const char *
20227 dwarf_tag_name (unsigned tag)
20229 const char *name = get_DW_TAG_name (tag);
20232 return "DW_TAG_<unknown>";
20237 /* Convert a DWARF attribute code into its string name. */
20239 static const char *
20240 dwarf_attr_name (unsigned attr)
20244 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20245 if (attr == DW_AT_MIPS_fde)
20246 return "DW_AT_MIPS_fde";
20248 if (attr == DW_AT_HP_block_index)
20249 return "DW_AT_HP_block_index";
20252 name = get_DW_AT_name (attr);
20255 return "DW_AT_<unknown>";
20260 /* Convert a DWARF value form code into its string name. */
20262 static const char *
20263 dwarf_form_name (unsigned form)
20265 const char *name = get_DW_FORM_name (form);
20268 return "DW_FORM_<unknown>";
20273 static const char *
20274 dwarf_bool_name (unsigned mybool)
20282 /* Convert a DWARF type code into its string name. */
20284 static const char *
20285 dwarf_type_encoding_name (unsigned enc)
20287 const char *name = get_DW_ATE_name (enc);
20290 return "DW_ATE_<unknown>";
20296 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20300 print_spaces (indent, f);
20301 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20302 dwarf_tag_name (die->tag), die->abbrev,
20303 to_underlying (die->sect_off));
20305 if (die->parent != NULL)
20307 print_spaces (indent, f);
20308 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20309 to_underlying (die->parent->sect_off));
20312 print_spaces (indent, f);
20313 fprintf_unfiltered (f, " has children: %s\n",
20314 dwarf_bool_name (die->child != NULL));
20316 print_spaces (indent, f);
20317 fprintf_unfiltered (f, " attributes:\n");
20319 for (i = 0; i < die->num_attrs; ++i)
20321 print_spaces (indent, f);
20322 fprintf_unfiltered (f, " %s (%s) ",
20323 dwarf_attr_name (die->attrs[i].name),
20324 dwarf_form_name (die->attrs[i].form));
20326 switch (die->attrs[i].form)
20329 case DW_FORM_GNU_addr_index:
20330 fprintf_unfiltered (f, "address: ");
20331 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20333 case DW_FORM_block2:
20334 case DW_FORM_block4:
20335 case DW_FORM_block:
20336 case DW_FORM_block1:
20337 fprintf_unfiltered (f, "block: size %s",
20338 pulongest (DW_BLOCK (&die->attrs[i])->size));
20340 case DW_FORM_exprloc:
20341 fprintf_unfiltered (f, "expression: size %s",
20342 pulongest (DW_BLOCK (&die->attrs[i])->size));
20344 case DW_FORM_data16:
20345 fprintf_unfiltered (f, "constant of 16 bytes");
20347 case DW_FORM_ref_addr:
20348 fprintf_unfiltered (f, "ref address: ");
20349 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20351 case DW_FORM_GNU_ref_alt:
20352 fprintf_unfiltered (f, "alt ref address: ");
20353 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20359 case DW_FORM_ref_udata:
20360 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20361 (long) (DW_UNSND (&die->attrs[i])));
20363 case DW_FORM_data1:
20364 case DW_FORM_data2:
20365 case DW_FORM_data4:
20366 case DW_FORM_data8:
20367 case DW_FORM_udata:
20368 case DW_FORM_sdata:
20369 fprintf_unfiltered (f, "constant: %s",
20370 pulongest (DW_UNSND (&die->attrs[i])));
20372 case DW_FORM_sec_offset:
20373 fprintf_unfiltered (f, "section offset: %s",
20374 pulongest (DW_UNSND (&die->attrs[i])));
20376 case DW_FORM_ref_sig8:
20377 fprintf_unfiltered (f, "signature: %s",
20378 hex_string (DW_SIGNATURE (&die->attrs[i])));
20380 case DW_FORM_string:
20382 case DW_FORM_line_strp:
20383 case DW_FORM_GNU_str_index:
20384 case DW_FORM_GNU_strp_alt:
20385 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20386 DW_STRING (&die->attrs[i])
20387 ? DW_STRING (&die->attrs[i]) : "",
20388 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20391 if (DW_UNSND (&die->attrs[i]))
20392 fprintf_unfiltered (f, "flag: TRUE");
20394 fprintf_unfiltered (f, "flag: FALSE");
20396 case DW_FORM_flag_present:
20397 fprintf_unfiltered (f, "flag: TRUE");
20399 case DW_FORM_indirect:
20400 /* The reader will have reduced the indirect form to
20401 the "base form" so this form should not occur. */
20402 fprintf_unfiltered (f,
20403 "unexpected attribute form: DW_FORM_indirect");
20406 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20407 die->attrs[i].form);
20410 fprintf_unfiltered (f, "\n");
20415 dump_die_for_error (struct die_info *die)
20417 dump_die_shallow (gdb_stderr, 0, die);
20421 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20423 int indent = level * 4;
20425 gdb_assert (die != NULL);
20427 if (level >= max_level)
20430 dump_die_shallow (f, indent, die);
20432 if (die->child != NULL)
20434 print_spaces (indent, f);
20435 fprintf_unfiltered (f, " Children:");
20436 if (level + 1 < max_level)
20438 fprintf_unfiltered (f, "\n");
20439 dump_die_1 (f, level + 1, max_level, die->child);
20443 fprintf_unfiltered (f,
20444 " [not printed, max nesting level reached]\n");
20448 if (die->sibling != NULL && level > 0)
20450 dump_die_1 (f, level, max_level, die->sibling);
20454 /* This is called from the pdie macro in gdbinit.in.
20455 It's not static so gcc will keep a copy callable from gdb. */
20458 dump_die (struct die_info *die, int max_level)
20460 dump_die_1 (gdb_stdlog, 0, max_level, die);
20464 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20468 slot = htab_find_slot_with_hash (cu->die_hash, die,
20469 to_underlying (die->sect_off),
20475 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20479 dwarf2_get_ref_die_offset (const struct attribute *attr)
20481 if (attr_form_is_ref (attr))
20482 return (sect_offset) DW_UNSND (attr);
20484 complaint (&symfile_complaints,
20485 _("unsupported die ref attribute form: '%s'"),
20486 dwarf_form_name (attr->form));
20490 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20491 * the value held by the attribute is not constant. */
20494 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20496 if (attr->form == DW_FORM_sdata)
20497 return DW_SND (attr);
20498 else if (attr->form == DW_FORM_udata
20499 || attr->form == DW_FORM_data1
20500 || attr->form == DW_FORM_data2
20501 || attr->form == DW_FORM_data4
20502 || attr->form == DW_FORM_data8)
20503 return DW_UNSND (attr);
20506 /* For DW_FORM_data16 see attr_form_is_constant. */
20507 complaint (&symfile_complaints,
20508 _("Attribute value is not a constant (%s)"),
20509 dwarf_form_name (attr->form));
20510 return default_value;
20514 /* Follow reference or signature attribute ATTR of SRC_DIE.
20515 On entry *REF_CU is the CU of SRC_DIE.
20516 On exit *REF_CU is the CU of the result. */
20518 static struct die_info *
20519 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20520 struct dwarf2_cu **ref_cu)
20522 struct die_info *die;
20524 if (attr_form_is_ref (attr))
20525 die = follow_die_ref (src_die, attr, ref_cu);
20526 else if (attr->form == DW_FORM_ref_sig8)
20527 die = follow_die_sig (src_die, attr, ref_cu);
20530 dump_die_for_error (src_die);
20531 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20532 objfile_name ((*ref_cu)->objfile));
20538 /* Follow reference OFFSET.
20539 On entry *REF_CU is the CU of the source die referencing OFFSET.
20540 On exit *REF_CU is the CU of the result.
20541 Returns NULL if OFFSET is invalid. */
20543 static struct die_info *
20544 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20545 struct dwarf2_cu **ref_cu)
20547 struct die_info temp_die;
20548 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20550 gdb_assert (cu->per_cu != NULL);
20554 if (cu->per_cu->is_debug_types)
20556 /* .debug_types CUs cannot reference anything outside their CU.
20557 If they need to, they have to reference a signatured type via
20558 DW_FORM_ref_sig8. */
20559 if (!offset_in_cu_p (&cu->header, sect_off))
20562 else if (offset_in_dwz != cu->per_cu->is_dwz
20563 || !offset_in_cu_p (&cu->header, sect_off))
20565 struct dwarf2_per_cu_data *per_cu;
20567 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20570 /* If necessary, add it to the queue and load its DIEs. */
20571 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20572 load_full_comp_unit (per_cu, cu->language);
20574 target_cu = per_cu->cu;
20576 else if (cu->dies == NULL)
20578 /* We're loading full DIEs during partial symbol reading. */
20579 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20580 load_full_comp_unit (cu->per_cu, language_minimal);
20583 *ref_cu = target_cu;
20584 temp_die.sect_off = sect_off;
20585 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20587 to_underlying (sect_off));
20590 /* Follow reference attribute ATTR of SRC_DIE.
20591 On entry *REF_CU is the CU of SRC_DIE.
20592 On exit *REF_CU is the CU of the result. */
20594 static struct die_info *
20595 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20596 struct dwarf2_cu **ref_cu)
20598 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20599 struct dwarf2_cu *cu = *ref_cu;
20600 struct die_info *die;
20602 die = follow_die_offset (sect_off,
20603 (attr->form == DW_FORM_GNU_ref_alt
20604 || cu->per_cu->is_dwz),
20607 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20608 "at 0x%x [in module %s]"),
20609 to_underlying (sect_off), to_underlying (src_die->sect_off),
20610 objfile_name (cu->objfile));
20615 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
20616 Returned value is intended for DW_OP_call*. Returned
20617 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20619 struct dwarf2_locexpr_baton
20620 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20621 struct dwarf2_per_cu_data *per_cu,
20622 CORE_ADDR (*get_frame_pc) (void *baton),
20625 struct dwarf2_cu *cu;
20626 struct die_info *die;
20627 struct attribute *attr;
20628 struct dwarf2_locexpr_baton retval;
20630 dw2_setup (per_cu->objfile);
20632 if (per_cu->cu == NULL)
20637 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20638 Instead just throw an error, not much else we can do. */
20639 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20640 to_underlying (sect_off), objfile_name (per_cu->objfile));
20643 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20645 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20646 to_underlying (sect_off), objfile_name (per_cu->objfile));
20648 attr = dwarf2_attr (die, DW_AT_location, cu);
20651 /* DWARF: "If there is no such attribute, then there is no effect.".
20652 DATA is ignored if SIZE is 0. */
20654 retval.data = NULL;
20657 else if (attr_form_is_section_offset (attr))
20659 struct dwarf2_loclist_baton loclist_baton;
20660 CORE_ADDR pc = (*get_frame_pc) (baton);
20663 fill_in_loclist_baton (cu, &loclist_baton, attr);
20665 retval.data = dwarf2_find_location_expression (&loclist_baton,
20667 retval.size = size;
20671 if (!attr_form_is_block (attr))
20672 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20673 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20674 to_underlying (sect_off), objfile_name (per_cu->objfile));
20676 retval.data = DW_BLOCK (attr)->data;
20677 retval.size = DW_BLOCK (attr)->size;
20679 retval.per_cu = cu->per_cu;
20681 age_cached_comp_units ();
20686 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20689 struct dwarf2_locexpr_baton
20690 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20691 struct dwarf2_per_cu_data *per_cu,
20692 CORE_ADDR (*get_frame_pc) (void *baton),
20695 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20697 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
20700 /* Write a constant of a given type as target-ordered bytes into
20703 static const gdb_byte *
20704 write_constant_as_bytes (struct obstack *obstack,
20705 enum bfd_endian byte_order,
20712 *len = TYPE_LENGTH (type);
20713 result = (gdb_byte *) obstack_alloc (obstack, *len);
20714 store_unsigned_integer (result, *len, byte_order, value);
20719 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20720 pointer to the constant bytes and set LEN to the length of the
20721 data. If memory is needed, allocate it on OBSTACK. If the DIE
20722 does not have a DW_AT_const_value, return NULL. */
20725 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20726 struct dwarf2_per_cu_data *per_cu,
20727 struct obstack *obstack,
20730 struct dwarf2_cu *cu;
20731 struct die_info *die;
20732 struct attribute *attr;
20733 const gdb_byte *result = NULL;
20736 enum bfd_endian byte_order;
20738 dw2_setup (per_cu->objfile);
20740 if (per_cu->cu == NULL)
20745 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20746 Instead just throw an error, not much else we can do. */
20747 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20748 to_underlying (sect_off), objfile_name (per_cu->objfile));
20751 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20753 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20754 to_underlying (sect_off), objfile_name (per_cu->objfile));
20757 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20761 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20762 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20764 switch (attr->form)
20767 case DW_FORM_GNU_addr_index:
20771 *len = cu->header.addr_size;
20772 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20773 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20777 case DW_FORM_string:
20779 case DW_FORM_GNU_str_index:
20780 case DW_FORM_GNU_strp_alt:
20781 /* DW_STRING is already allocated on the objfile obstack, point
20783 result = (const gdb_byte *) DW_STRING (attr);
20784 *len = strlen (DW_STRING (attr));
20786 case DW_FORM_block1:
20787 case DW_FORM_block2:
20788 case DW_FORM_block4:
20789 case DW_FORM_block:
20790 case DW_FORM_exprloc:
20791 case DW_FORM_data16:
20792 result = DW_BLOCK (attr)->data;
20793 *len = DW_BLOCK (attr)->size;
20796 /* The DW_AT_const_value attributes are supposed to carry the
20797 symbol's value "represented as it would be on the target
20798 architecture." By the time we get here, it's already been
20799 converted to host endianness, so we just need to sign- or
20800 zero-extend it as appropriate. */
20801 case DW_FORM_data1:
20802 type = die_type (die, cu);
20803 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20804 if (result == NULL)
20805 result = write_constant_as_bytes (obstack, byte_order,
20808 case DW_FORM_data2:
20809 type = die_type (die, cu);
20810 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20811 if (result == NULL)
20812 result = write_constant_as_bytes (obstack, byte_order,
20815 case DW_FORM_data4:
20816 type = die_type (die, cu);
20817 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20818 if (result == NULL)
20819 result = write_constant_as_bytes (obstack, byte_order,
20822 case DW_FORM_data8:
20823 type = die_type (die, cu);
20824 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20825 if (result == NULL)
20826 result = write_constant_as_bytes (obstack, byte_order,
20830 case DW_FORM_sdata:
20831 type = die_type (die, cu);
20832 result = write_constant_as_bytes (obstack, byte_order,
20833 type, DW_SND (attr), len);
20836 case DW_FORM_udata:
20837 type = die_type (die, cu);
20838 result = write_constant_as_bytes (obstack, byte_order,
20839 type, DW_UNSND (attr), len);
20843 complaint (&symfile_complaints,
20844 _("unsupported const value attribute form: '%s'"),
20845 dwarf_form_name (attr->form));
20852 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20853 valid type for this die is found. */
20856 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20857 struct dwarf2_per_cu_data *per_cu)
20859 struct dwarf2_cu *cu;
20860 struct die_info *die;
20862 dw2_setup (per_cu->objfile);
20864 if (per_cu->cu == NULL)
20870 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20874 return die_type (die, cu);
20877 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20881 dwarf2_get_die_type (cu_offset die_offset,
20882 struct dwarf2_per_cu_data *per_cu)
20884 dw2_setup (per_cu->objfile);
20886 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20887 return get_die_type_at_offset (die_offset_sect, per_cu);
20890 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20891 On entry *REF_CU is the CU of SRC_DIE.
20892 On exit *REF_CU is the CU of the result.
20893 Returns NULL if the referenced DIE isn't found. */
20895 static struct die_info *
20896 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20897 struct dwarf2_cu **ref_cu)
20899 struct die_info temp_die;
20900 struct dwarf2_cu *sig_cu;
20901 struct die_info *die;
20903 /* While it might be nice to assert sig_type->type == NULL here,
20904 we can get here for DW_AT_imported_declaration where we need
20905 the DIE not the type. */
20907 /* If necessary, add it to the queue and load its DIEs. */
20909 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20910 read_signatured_type (sig_type);
20912 sig_cu = sig_type->per_cu.cu;
20913 gdb_assert (sig_cu != NULL);
20914 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20915 temp_die.sect_off = sig_type->type_offset_in_section;
20916 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20917 to_underlying (temp_die.sect_off));
20920 /* For .gdb_index version 7 keep track of included TUs.
20921 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20922 if (dwarf2_per_objfile->index_table != NULL
20923 && dwarf2_per_objfile->index_table->version <= 7)
20925 VEC_safe_push (dwarf2_per_cu_ptr,
20926 (*ref_cu)->per_cu->imported_symtabs,
20937 /* Follow signatured type referenced by ATTR in SRC_DIE.
20938 On entry *REF_CU is the CU of SRC_DIE.
20939 On exit *REF_CU is the CU of the result.
20940 The result is the DIE of the type.
20941 If the referenced type cannot be found an error is thrown. */
20943 static struct die_info *
20944 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20945 struct dwarf2_cu **ref_cu)
20947 ULONGEST signature = DW_SIGNATURE (attr);
20948 struct signatured_type *sig_type;
20949 struct die_info *die;
20951 gdb_assert (attr->form == DW_FORM_ref_sig8);
20953 sig_type = lookup_signatured_type (*ref_cu, signature);
20954 /* sig_type will be NULL if the signatured type is missing from
20956 if (sig_type == NULL)
20958 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20959 " from DIE at 0x%x [in module %s]"),
20960 hex_string (signature), to_underlying (src_die->sect_off),
20961 objfile_name ((*ref_cu)->objfile));
20964 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20967 dump_die_for_error (src_die);
20968 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20969 " from DIE at 0x%x [in module %s]"),
20970 hex_string (signature), to_underlying (src_die->sect_off),
20971 objfile_name ((*ref_cu)->objfile));
20977 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20978 reading in and processing the type unit if necessary. */
20980 static struct type *
20981 get_signatured_type (struct die_info *die, ULONGEST signature,
20982 struct dwarf2_cu *cu)
20984 struct signatured_type *sig_type;
20985 struct dwarf2_cu *type_cu;
20986 struct die_info *type_die;
20989 sig_type = lookup_signatured_type (cu, signature);
20990 /* sig_type will be NULL if the signatured type is missing from
20992 if (sig_type == NULL)
20994 complaint (&symfile_complaints,
20995 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20996 " from DIE at 0x%x [in module %s]"),
20997 hex_string (signature), to_underlying (die->sect_off),
20998 objfile_name (dwarf2_per_objfile->objfile));
20999 return build_error_marker_type (cu, die);
21002 /* If we already know the type we're done. */
21003 if (sig_type->type != NULL)
21004 return sig_type->type;
21007 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21008 if (type_die != NULL)
21010 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21011 is created. This is important, for example, because for c++ classes
21012 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21013 type = read_type_die (type_die, type_cu);
21016 complaint (&symfile_complaints,
21017 _("Dwarf Error: Cannot build signatured type %s"
21018 " referenced from DIE at 0x%x [in module %s]"),
21019 hex_string (signature), to_underlying (die->sect_off),
21020 objfile_name (dwarf2_per_objfile->objfile));
21021 type = build_error_marker_type (cu, die);
21026 complaint (&symfile_complaints,
21027 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21028 " from DIE at 0x%x [in module %s]"),
21029 hex_string (signature), to_underlying (die->sect_off),
21030 objfile_name (dwarf2_per_objfile->objfile));
21031 type = build_error_marker_type (cu, die);
21033 sig_type->type = type;
21038 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21039 reading in and processing the type unit if necessary. */
21041 static struct type *
21042 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21043 struct dwarf2_cu *cu) /* ARI: editCase function */
21045 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21046 if (attr_form_is_ref (attr))
21048 struct dwarf2_cu *type_cu = cu;
21049 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21051 return read_type_die (type_die, type_cu);
21053 else if (attr->form == DW_FORM_ref_sig8)
21055 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21059 complaint (&symfile_complaints,
21060 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21061 " at 0x%x [in module %s]"),
21062 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21063 objfile_name (dwarf2_per_objfile->objfile));
21064 return build_error_marker_type (cu, die);
21068 /* Load the DIEs associated with type unit PER_CU into memory. */
21071 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21073 struct signatured_type *sig_type;
21075 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21076 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21078 /* We have the per_cu, but we need the signatured_type.
21079 Fortunately this is an easy translation. */
21080 gdb_assert (per_cu->is_debug_types);
21081 sig_type = (struct signatured_type *) per_cu;
21083 gdb_assert (per_cu->cu == NULL);
21085 read_signatured_type (sig_type);
21087 gdb_assert (per_cu->cu != NULL);
21090 /* die_reader_func for read_signatured_type.
21091 This is identical to load_full_comp_unit_reader,
21092 but is kept separate for now. */
21095 read_signatured_type_reader (const struct die_reader_specs *reader,
21096 const gdb_byte *info_ptr,
21097 struct die_info *comp_unit_die,
21101 struct dwarf2_cu *cu = reader->cu;
21103 gdb_assert (cu->die_hash == NULL);
21105 htab_create_alloc_ex (cu->header.length / 12,
21109 &cu->comp_unit_obstack,
21110 hashtab_obstack_allocate,
21111 dummy_obstack_deallocate);
21114 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21115 &info_ptr, comp_unit_die);
21116 cu->dies = comp_unit_die;
21117 /* comp_unit_die is not stored in die_hash, no need. */
21119 /* We try not to read any attributes in this function, because not
21120 all CUs needed for references have been loaded yet, and symbol
21121 table processing isn't initialized. But we have to set the CU language,
21122 or we won't be able to build types correctly.
21123 Similarly, if we do not read the producer, we can not apply
21124 producer-specific interpretation. */
21125 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21128 /* Read in a signatured type and build its CU and DIEs.
21129 If the type is a stub for the real type in a DWO file,
21130 read in the real type from the DWO file as well. */
21133 read_signatured_type (struct signatured_type *sig_type)
21135 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21137 gdb_assert (per_cu->is_debug_types);
21138 gdb_assert (per_cu->cu == NULL);
21140 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21141 read_signatured_type_reader, NULL);
21142 sig_type->per_cu.tu_read = 1;
21145 /* Decode simple location descriptions.
21146 Given a pointer to a dwarf block that defines a location, compute
21147 the location and return the value.
21149 NOTE drow/2003-11-18: This function is called in two situations
21150 now: for the address of static or global variables (partial symbols
21151 only) and for offsets into structures which are expected to be
21152 (more or less) constant. The partial symbol case should go away,
21153 and only the constant case should remain. That will let this
21154 function complain more accurately. A few special modes are allowed
21155 without complaint for global variables (for instance, global
21156 register values and thread-local values).
21158 A location description containing no operations indicates that the
21159 object is optimized out. The return value is 0 for that case.
21160 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21161 callers will only want a very basic result and this can become a
21164 Note that stack[0] is unused except as a default error return. */
21167 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21169 struct objfile *objfile = cu->objfile;
21171 size_t size = blk->size;
21172 const gdb_byte *data = blk->data;
21173 CORE_ADDR stack[64];
21175 unsigned int bytes_read, unsnd;
21181 stack[++stacki] = 0;
21220 stack[++stacki] = op - DW_OP_lit0;
21255 stack[++stacki] = op - DW_OP_reg0;
21257 dwarf2_complex_location_expr_complaint ();
21261 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21263 stack[++stacki] = unsnd;
21265 dwarf2_complex_location_expr_complaint ();
21269 stack[++stacki] = read_address (objfile->obfd, &data[i],
21274 case DW_OP_const1u:
21275 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21279 case DW_OP_const1s:
21280 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21284 case DW_OP_const2u:
21285 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21289 case DW_OP_const2s:
21290 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21294 case DW_OP_const4u:
21295 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21299 case DW_OP_const4s:
21300 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21304 case DW_OP_const8u:
21305 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21310 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21316 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21321 stack[stacki + 1] = stack[stacki];
21326 stack[stacki - 1] += stack[stacki];
21330 case DW_OP_plus_uconst:
21331 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21337 stack[stacki - 1] -= stack[stacki];
21342 /* If we're not the last op, then we definitely can't encode
21343 this using GDB's address_class enum. This is valid for partial
21344 global symbols, although the variable's address will be bogus
21347 dwarf2_complex_location_expr_complaint ();
21350 case DW_OP_GNU_push_tls_address:
21351 case DW_OP_form_tls_address:
21352 /* The top of the stack has the offset from the beginning
21353 of the thread control block at which the variable is located. */
21354 /* Nothing should follow this operator, so the top of stack would
21356 /* This is valid for partial global symbols, but the variable's
21357 address will be bogus in the psymtab. Make it always at least
21358 non-zero to not look as a variable garbage collected by linker
21359 which have DW_OP_addr 0. */
21361 dwarf2_complex_location_expr_complaint ();
21365 case DW_OP_GNU_uninit:
21368 case DW_OP_GNU_addr_index:
21369 case DW_OP_GNU_const_index:
21370 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21377 const char *name = get_DW_OP_name (op);
21380 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21383 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21387 return (stack[stacki]);
21390 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21391 outside of the allocated space. Also enforce minimum>0. */
21392 if (stacki >= ARRAY_SIZE (stack) - 1)
21394 complaint (&symfile_complaints,
21395 _("location description stack overflow"));
21401 complaint (&symfile_complaints,
21402 _("location description stack underflow"));
21406 return (stack[stacki]);
21409 /* memory allocation interface */
21411 static struct dwarf_block *
21412 dwarf_alloc_block (struct dwarf2_cu *cu)
21414 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21417 static struct die_info *
21418 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21420 struct die_info *die;
21421 size_t size = sizeof (struct die_info);
21424 size += (num_attrs - 1) * sizeof (struct attribute);
21426 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21427 memset (die, 0, sizeof (struct die_info));
21432 /* Macro support. */
21434 /* Return file name relative to the compilation directory of file number I in
21435 *LH's file name table. The result is allocated using xmalloc; the caller is
21436 responsible for freeing it. */
21439 file_file_name (int file, struct line_header *lh)
21441 /* Is the file number a valid index into the line header's file name
21442 table? Remember that file numbers start with one, not zero. */
21443 if (1 <= file && file <= lh->file_names.size ())
21445 const file_entry &fe = lh->file_names[file - 1];
21447 if (!IS_ABSOLUTE_PATH (fe.name))
21449 const char *dir = fe.include_dir (lh);
21451 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21453 return xstrdup (fe.name);
21457 /* The compiler produced a bogus file number. We can at least
21458 record the macro definitions made in the file, even if we
21459 won't be able to find the file by name. */
21460 char fake_name[80];
21462 xsnprintf (fake_name, sizeof (fake_name),
21463 "<bad macro file number %d>", file);
21465 complaint (&symfile_complaints,
21466 _("bad file number in macro information (%d)"),
21469 return xstrdup (fake_name);
21473 /* Return the full name of file number I in *LH's file name table.
21474 Use COMP_DIR as the name of the current directory of the
21475 compilation. The result is allocated using xmalloc; the caller is
21476 responsible for freeing it. */
21478 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21480 /* Is the file number a valid index into the line header's file name
21481 table? Remember that file numbers start with one, not zero. */
21482 if (1 <= file && file <= lh->file_names.size ())
21484 char *relative = file_file_name (file, lh);
21486 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21488 return reconcat (relative, comp_dir, SLASH_STRING,
21489 relative, (char *) NULL);
21492 return file_file_name (file, lh);
21496 static struct macro_source_file *
21497 macro_start_file (int file, int line,
21498 struct macro_source_file *current_file,
21499 struct line_header *lh)
21501 /* File name relative to the compilation directory of this source file. */
21502 char *file_name = file_file_name (file, lh);
21504 if (! current_file)
21506 /* Note: We don't create a macro table for this compilation unit
21507 at all until we actually get a filename. */
21508 struct macro_table *macro_table = get_macro_table ();
21510 /* If we have no current file, then this must be the start_file
21511 directive for the compilation unit's main source file. */
21512 current_file = macro_set_main (macro_table, file_name);
21513 macro_define_special (macro_table);
21516 current_file = macro_include (current_file, line, file_name);
21520 return current_file;
21524 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21525 followed by a null byte. */
21527 copy_string (const char *buf, int len)
21529 char *s = (char *) xmalloc (len + 1);
21531 memcpy (s, buf, len);
21537 static const char *
21538 consume_improper_spaces (const char *p, const char *body)
21542 complaint (&symfile_complaints,
21543 _("macro definition contains spaces "
21544 "in formal argument list:\n`%s'"),
21556 parse_macro_definition (struct macro_source_file *file, int line,
21561 /* The body string takes one of two forms. For object-like macro
21562 definitions, it should be:
21564 <macro name> " " <definition>
21566 For function-like macro definitions, it should be:
21568 <macro name> "() " <definition>
21570 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21572 Spaces may appear only where explicitly indicated, and in the
21575 The Dwarf 2 spec says that an object-like macro's name is always
21576 followed by a space, but versions of GCC around March 2002 omit
21577 the space when the macro's definition is the empty string.
21579 The Dwarf 2 spec says that there should be no spaces between the
21580 formal arguments in a function-like macro's formal argument list,
21581 but versions of GCC around March 2002 include spaces after the
21585 /* Find the extent of the macro name. The macro name is terminated
21586 by either a space or null character (for an object-like macro) or
21587 an opening paren (for a function-like macro). */
21588 for (p = body; *p; p++)
21589 if (*p == ' ' || *p == '(')
21592 if (*p == ' ' || *p == '\0')
21594 /* It's an object-like macro. */
21595 int name_len = p - body;
21596 char *name = copy_string (body, name_len);
21597 const char *replacement;
21600 replacement = body + name_len + 1;
21603 dwarf2_macro_malformed_definition_complaint (body);
21604 replacement = body + name_len;
21607 macro_define_object (file, line, name, replacement);
21611 else if (*p == '(')
21613 /* It's a function-like macro. */
21614 char *name = copy_string (body, p - body);
21617 char **argv = XNEWVEC (char *, argv_size);
21621 p = consume_improper_spaces (p, body);
21623 /* Parse the formal argument list. */
21624 while (*p && *p != ')')
21626 /* Find the extent of the current argument name. */
21627 const char *arg_start = p;
21629 while (*p && *p != ',' && *p != ')' && *p != ' ')
21632 if (! *p || p == arg_start)
21633 dwarf2_macro_malformed_definition_complaint (body);
21636 /* Make sure argv has room for the new argument. */
21637 if (argc >= argv_size)
21640 argv = XRESIZEVEC (char *, argv, argv_size);
21643 argv[argc++] = copy_string (arg_start, p - arg_start);
21646 p = consume_improper_spaces (p, body);
21648 /* Consume the comma, if present. */
21653 p = consume_improper_spaces (p, body);
21662 /* Perfectly formed definition, no complaints. */
21663 macro_define_function (file, line, name,
21664 argc, (const char **) argv,
21666 else if (*p == '\0')
21668 /* Complain, but do define it. */
21669 dwarf2_macro_malformed_definition_complaint (body);
21670 macro_define_function (file, line, name,
21671 argc, (const char **) argv,
21675 /* Just complain. */
21676 dwarf2_macro_malformed_definition_complaint (body);
21679 /* Just complain. */
21680 dwarf2_macro_malformed_definition_complaint (body);
21686 for (i = 0; i < argc; i++)
21692 dwarf2_macro_malformed_definition_complaint (body);
21695 /* Skip some bytes from BYTES according to the form given in FORM.
21696 Returns the new pointer. */
21698 static const gdb_byte *
21699 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21700 enum dwarf_form form,
21701 unsigned int offset_size,
21702 struct dwarf2_section_info *section)
21704 unsigned int bytes_read;
21708 case DW_FORM_data1:
21713 case DW_FORM_data2:
21717 case DW_FORM_data4:
21721 case DW_FORM_data8:
21725 case DW_FORM_data16:
21729 case DW_FORM_string:
21730 read_direct_string (abfd, bytes, &bytes_read);
21731 bytes += bytes_read;
21734 case DW_FORM_sec_offset:
21736 case DW_FORM_GNU_strp_alt:
21737 bytes += offset_size;
21740 case DW_FORM_block:
21741 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21742 bytes += bytes_read;
21745 case DW_FORM_block1:
21746 bytes += 1 + read_1_byte (abfd, bytes);
21748 case DW_FORM_block2:
21749 bytes += 2 + read_2_bytes (abfd, bytes);
21751 case DW_FORM_block4:
21752 bytes += 4 + read_4_bytes (abfd, bytes);
21755 case DW_FORM_sdata:
21756 case DW_FORM_udata:
21757 case DW_FORM_GNU_addr_index:
21758 case DW_FORM_GNU_str_index:
21759 bytes = gdb_skip_leb128 (bytes, buffer_end);
21762 dwarf2_section_buffer_overflow_complaint (section);
21770 complaint (&symfile_complaints,
21771 _("invalid form 0x%x in `%s'"),
21772 form, get_section_name (section));
21780 /* A helper for dwarf_decode_macros that handles skipping an unknown
21781 opcode. Returns an updated pointer to the macro data buffer; or,
21782 on error, issues a complaint and returns NULL. */
21784 static const gdb_byte *
21785 skip_unknown_opcode (unsigned int opcode,
21786 const gdb_byte **opcode_definitions,
21787 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21789 unsigned int offset_size,
21790 struct dwarf2_section_info *section)
21792 unsigned int bytes_read, i;
21794 const gdb_byte *defn;
21796 if (opcode_definitions[opcode] == NULL)
21798 complaint (&symfile_complaints,
21799 _("unrecognized DW_MACFINO opcode 0x%x"),
21804 defn = opcode_definitions[opcode];
21805 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21806 defn += bytes_read;
21808 for (i = 0; i < arg; ++i)
21810 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21811 (enum dwarf_form) defn[i], offset_size,
21813 if (mac_ptr == NULL)
21815 /* skip_form_bytes already issued the complaint. */
21823 /* A helper function which parses the header of a macro section.
21824 If the macro section is the extended (for now called "GNU") type,
21825 then this updates *OFFSET_SIZE. Returns a pointer to just after
21826 the header, or issues a complaint and returns NULL on error. */
21828 static const gdb_byte *
21829 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21831 const gdb_byte *mac_ptr,
21832 unsigned int *offset_size,
21833 int section_is_gnu)
21835 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21837 if (section_is_gnu)
21839 unsigned int version, flags;
21841 version = read_2_bytes (abfd, mac_ptr);
21842 if (version != 4 && version != 5)
21844 complaint (&symfile_complaints,
21845 _("unrecognized version `%d' in .debug_macro section"),
21851 flags = read_1_byte (abfd, mac_ptr);
21853 *offset_size = (flags & 1) ? 8 : 4;
21855 if ((flags & 2) != 0)
21856 /* We don't need the line table offset. */
21857 mac_ptr += *offset_size;
21859 /* Vendor opcode descriptions. */
21860 if ((flags & 4) != 0)
21862 unsigned int i, count;
21864 count = read_1_byte (abfd, mac_ptr);
21866 for (i = 0; i < count; ++i)
21868 unsigned int opcode, bytes_read;
21871 opcode = read_1_byte (abfd, mac_ptr);
21873 opcode_definitions[opcode] = mac_ptr;
21874 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21875 mac_ptr += bytes_read;
21884 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21885 including DW_MACRO_import. */
21888 dwarf_decode_macro_bytes (bfd *abfd,
21889 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21890 struct macro_source_file *current_file,
21891 struct line_header *lh,
21892 struct dwarf2_section_info *section,
21893 int section_is_gnu, int section_is_dwz,
21894 unsigned int offset_size,
21895 htab_t include_hash)
21897 struct objfile *objfile = dwarf2_per_objfile->objfile;
21898 enum dwarf_macro_record_type macinfo_type;
21899 int at_commandline;
21900 const gdb_byte *opcode_definitions[256];
21902 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21903 &offset_size, section_is_gnu);
21904 if (mac_ptr == NULL)
21906 /* We already issued a complaint. */
21910 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21911 GDB is still reading the definitions from command line. First
21912 DW_MACINFO_start_file will need to be ignored as it was already executed
21913 to create CURRENT_FILE for the main source holding also the command line
21914 definitions. On first met DW_MACINFO_start_file this flag is reset to
21915 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21917 at_commandline = 1;
21921 /* Do we at least have room for a macinfo type byte? */
21922 if (mac_ptr >= mac_end)
21924 dwarf2_section_buffer_overflow_complaint (section);
21928 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21931 /* Note that we rely on the fact that the corresponding GNU and
21932 DWARF constants are the same. */
21933 switch (macinfo_type)
21935 /* A zero macinfo type indicates the end of the macro
21940 case DW_MACRO_define:
21941 case DW_MACRO_undef:
21942 case DW_MACRO_define_strp:
21943 case DW_MACRO_undef_strp:
21944 case DW_MACRO_define_sup:
21945 case DW_MACRO_undef_sup:
21947 unsigned int bytes_read;
21952 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21953 mac_ptr += bytes_read;
21955 if (macinfo_type == DW_MACRO_define
21956 || macinfo_type == DW_MACRO_undef)
21958 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21959 mac_ptr += bytes_read;
21963 LONGEST str_offset;
21965 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21966 mac_ptr += offset_size;
21968 if (macinfo_type == DW_MACRO_define_sup
21969 || macinfo_type == DW_MACRO_undef_sup
21972 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21974 body = read_indirect_string_from_dwz (dwz, str_offset);
21977 body = read_indirect_string_at_offset (abfd, str_offset);
21980 is_define = (macinfo_type == DW_MACRO_define
21981 || macinfo_type == DW_MACRO_define_strp
21982 || macinfo_type == DW_MACRO_define_sup);
21983 if (! current_file)
21985 /* DWARF violation as no main source is present. */
21986 complaint (&symfile_complaints,
21987 _("debug info with no main source gives macro %s "
21989 is_define ? _("definition") : _("undefinition"),
21993 if ((line == 0 && !at_commandline)
21994 || (line != 0 && at_commandline))
21995 complaint (&symfile_complaints,
21996 _("debug info gives %s macro %s with %s line %d: %s"),
21997 at_commandline ? _("command-line") : _("in-file"),
21998 is_define ? _("definition") : _("undefinition"),
21999 line == 0 ? _("zero") : _("non-zero"), line, body);
22002 parse_macro_definition (current_file, line, body);
22005 gdb_assert (macinfo_type == DW_MACRO_undef
22006 || macinfo_type == DW_MACRO_undef_strp
22007 || macinfo_type == DW_MACRO_undef_sup);
22008 macro_undef (current_file, line, body);
22013 case DW_MACRO_start_file:
22015 unsigned int bytes_read;
22018 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22019 mac_ptr += bytes_read;
22020 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22021 mac_ptr += bytes_read;
22023 if ((line == 0 && !at_commandline)
22024 || (line != 0 && at_commandline))
22025 complaint (&symfile_complaints,
22026 _("debug info gives source %d included "
22027 "from %s at %s line %d"),
22028 file, at_commandline ? _("command-line") : _("file"),
22029 line == 0 ? _("zero") : _("non-zero"), line);
22031 if (at_commandline)
22033 /* This DW_MACRO_start_file was executed in the
22035 at_commandline = 0;
22038 current_file = macro_start_file (file, line, current_file, lh);
22042 case DW_MACRO_end_file:
22043 if (! current_file)
22044 complaint (&symfile_complaints,
22045 _("macro debug info has an unmatched "
22046 "`close_file' directive"));
22049 current_file = current_file->included_by;
22050 if (! current_file)
22052 enum dwarf_macro_record_type next_type;
22054 /* GCC circa March 2002 doesn't produce the zero
22055 type byte marking the end of the compilation
22056 unit. Complain if it's not there, but exit no
22059 /* Do we at least have room for a macinfo type byte? */
22060 if (mac_ptr >= mac_end)
22062 dwarf2_section_buffer_overflow_complaint (section);
22066 /* We don't increment mac_ptr here, so this is just
22069 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22071 if (next_type != 0)
22072 complaint (&symfile_complaints,
22073 _("no terminating 0-type entry for "
22074 "macros in `.debug_macinfo' section"));
22081 case DW_MACRO_import:
22082 case DW_MACRO_import_sup:
22086 bfd *include_bfd = abfd;
22087 struct dwarf2_section_info *include_section = section;
22088 const gdb_byte *include_mac_end = mac_end;
22089 int is_dwz = section_is_dwz;
22090 const gdb_byte *new_mac_ptr;
22092 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22093 mac_ptr += offset_size;
22095 if (macinfo_type == DW_MACRO_import_sup)
22097 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22099 dwarf2_read_section (objfile, &dwz->macro);
22101 include_section = &dwz->macro;
22102 include_bfd = get_section_bfd_owner (include_section);
22103 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22107 new_mac_ptr = include_section->buffer + offset;
22108 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22112 /* This has actually happened; see
22113 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22114 complaint (&symfile_complaints,
22115 _("recursive DW_MACRO_import in "
22116 ".debug_macro section"));
22120 *slot = (void *) new_mac_ptr;
22122 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22123 include_mac_end, current_file, lh,
22124 section, section_is_gnu, is_dwz,
22125 offset_size, include_hash);
22127 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22132 case DW_MACINFO_vendor_ext:
22133 if (!section_is_gnu)
22135 unsigned int bytes_read;
22137 /* This reads the constant, but since we don't recognize
22138 any vendor extensions, we ignore it. */
22139 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22140 mac_ptr += bytes_read;
22141 read_direct_string (abfd, mac_ptr, &bytes_read);
22142 mac_ptr += bytes_read;
22144 /* We don't recognize any vendor extensions. */
22150 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22151 mac_ptr, mac_end, abfd, offset_size,
22153 if (mac_ptr == NULL)
22157 } while (macinfo_type != 0);
22161 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22162 int section_is_gnu)
22164 struct objfile *objfile = dwarf2_per_objfile->objfile;
22165 struct line_header *lh = cu->line_header;
22167 const gdb_byte *mac_ptr, *mac_end;
22168 struct macro_source_file *current_file = 0;
22169 enum dwarf_macro_record_type macinfo_type;
22170 unsigned int offset_size = cu->header.offset_size;
22171 const gdb_byte *opcode_definitions[256];
22172 struct cleanup *cleanup;
22174 struct dwarf2_section_info *section;
22175 const char *section_name;
22177 if (cu->dwo_unit != NULL)
22179 if (section_is_gnu)
22181 section = &cu->dwo_unit->dwo_file->sections.macro;
22182 section_name = ".debug_macro.dwo";
22186 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22187 section_name = ".debug_macinfo.dwo";
22192 if (section_is_gnu)
22194 section = &dwarf2_per_objfile->macro;
22195 section_name = ".debug_macro";
22199 section = &dwarf2_per_objfile->macinfo;
22200 section_name = ".debug_macinfo";
22204 dwarf2_read_section (objfile, section);
22205 if (section->buffer == NULL)
22207 complaint (&symfile_complaints, _("missing %s section"), section_name);
22210 abfd = get_section_bfd_owner (section);
22212 /* First pass: Find the name of the base filename.
22213 This filename is needed in order to process all macros whose definition
22214 (or undefinition) comes from the command line. These macros are defined
22215 before the first DW_MACINFO_start_file entry, and yet still need to be
22216 associated to the base file.
22218 To determine the base file name, we scan the macro definitions until we
22219 reach the first DW_MACINFO_start_file entry. We then initialize
22220 CURRENT_FILE accordingly so that any macro definition found before the
22221 first DW_MACINFO_start_file can still be associated to the base file. */
22223 mac_ptr = section->buffer + offset;
22224 mac_end = section->buffer + section->size;
22226 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22227 &offset_size, section_is_gnu);
22228 if (mac_ptr == NULL)
22230 /* We already issued a complaint. */
22236 /* Do we at least have room for a macinfo type byte? */
22237 if (mac_ptr >= mac_end)
22239 /* Complaint is printed during the second pass as GDB will probably
22240 stop the first pass earlier upon finding
22241 DW_MACINFO_start_file. */
22245 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22248 /* Note that we rely on the fact that the corresponding GNU and
22249 DWARF constants are the same. */
22250 switch (macinfo_type)
22252 /* A zero macinfo type indicates the end of the macro
22257 case DW_MACRO_define:
22258 case DW_MACRO_undef:
22259 /* Only skip the data by MAC_PTR. */
22261 unsigned int bytes_read;
22263 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22264 mac_ptr += bytes_read;
22265 read_direct_string (abfd, mac_ptr, &bytes_read);
22266 mac_ptr += bytes_read;
22270 case DW_MACRO_start_file:
22272 unsigned int bytes_read;
22275 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22276 mac_ptr += bytes_read;
22277 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22278 mac_ptr += bytes_read;
22280 current_file = macro_start_file (file, line, current_file, lh);
22284 case DW_MACRO_end_file:
22285 /* No data to skip by MAC_PTR. */
22288 case DW_MACRO_define_strp:
22289 case DW_MACRO_undef_strp:
22290 case DW_MACRO_define_sup:
22291 case DW_MACRO_undef_sup:
22293 unsigned int bytes_read;
22295 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22296 mac_ptr += bytes_read;
22297 mac_ptr += offset_size;
22301 case DW_MACRO_import:
22302 case DW_MACRO_import_sup:
22303 /* Note that, according to the spec, a transparent include
22304 chain cannot call DW_MACRO_start_file. So, we can just
22305 skip this opcode. */
22306 mac_ptr += offset_size;
22309 case DW_MACINFO_vendor_ext:
22310 /* Only skip the data by MAC_PTR. */
22311 if (!section_is_gnu)
22313 unsigned int bytes_read;
22315 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22316 mac_ptr += bytes_read;
22317 read_direct_string (abfd, mac_ptr, &bytes_read);
22318 mac_ptr += bytes_read;
22323 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22324 mac_ptr, mac_end, abfd, offset_size,
22326 if (mac_ptr == NULL)
22330 } while (macinfo_type != 0 && current_file == NULL);
22332 /* Second pass: Process all entries.
22334 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22335 command-line macro definitions/undefinitions. This flag is unset when we
22336 reach the first DW_MACINFO_start_file entry. */
22338 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22340 NULL, xcalloc, xfree));
22341 mac_ptr = section->buffer + offset;
22342 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22343 *slot = (void *) mac_ptr;
22344 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22345 current_file, lh, section,
22346 section_is_gnu, 0, offset_size,
22347 include_hash.get ());
22350 /* Check if the attribute's form is a DW_FORM_block*
22351 if so return true else false. */
22354 attr_form_is_block (const struct attribute *attr)
22356 return (attr == NULL ? 0 :
22357 attr->form == DW_FORM_block1
22358 || attr->form == DW_FORM_block2
22359 || attr->form == DW_FORM_block4
22360 || attr->form == DW_FORM_block
22361 || attr->form == DW_FORM_exprloc);
22364 /* Return non-zero if ATTR's value is a section offset --- classes
22365 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22366 You may use DW_UNSND (attr) to retrieve such offsets.
22368 Section 7.5.4, "Attribute Encodings", explains that no attribute
22369 may have a value that belongs to more than one of these classes; it
22370 would be ambiguous if we did, because we use the same forms for all
22374 attr_form_is_section_offset (const struct attribute *attr)
22376 return (attr->form == DW_FORM_data4
22377 || attr->form == DW_FORM_data8
22378 || attr->form == DW_FORM_sec_offset);
22381 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22382 zero otherwise. When this function returns true, you can apply
22383 dwarf2_get_attr_constant_value to it.
22385 However, note that for some attributes you must check
22386 attr_form_is_section_offset before using this test. DW_FORM_data4
22387 and DW_FORM_data8 are members of both the constant class, and of
22388 the classes that contain offsets into other debug sections
22389 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22390 that, if an attribute's can be either a constant or one of the
22391 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22392 taken as section offsets, not constants.
22394 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22395 cannot handle that. */
22398 attr_form_is_constant (const struct attribute *attr)
22400 switch (attr->form)
22402 case DW_FORM_sdata:
22403 case DW_FORM_udata:
22404 case DW_FORM_data1:
22405 case DW_FORM_data2:
22406 case DW_FORM_data4:
22407 case DW_FORM_data8:
22415 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22416 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22419 attr_form_is_ref (const struct attribute *attr)
22421 switch (attr->form)
22423 case DW_FORM_ref_addr:
22428 case DW_FORM_ref_udata:
22429 case DW_FORM_GNU_ref_alt:
22436 /* Return the .debug_loc section to use for CU.
22437 For DWO files use .debug_loc.dwo. */
22439 static struct dwarf2_section_info *
22440 cu_debug_loc_section (struct dwarf2_cu *cu)
22444 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22446 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
22448 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22449 : &dwarf2_per_objfile->loc);
22452 /* A helper function that fills in a dwarf2_loclist_baton. */
22455 fill_in_loclist_baton (struct dwarf2_cu *cu,
22456 struct dwarf2_loclist_baton *baton,
22457 const struct attribute *attr)
22459 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22461 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22463 baton->per_cu = cu->per_cu;
22464 gdb_assert (baton->per_cu);
22465 /* We don't know how long the location list is, but make sure we
22466 don't run off the edge of the section. */
22467 baton->size = section->size - DW_UNSND (attr);
22468 baton->data = section->buffer + DW_UNSND (attr);
22469 baton->base_address = cu->base_address;
22470 baton->from_dwo = cu->dwo_unit != NULL;
22474 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22475 struct dwarf2_cu *cu, int is_block)
22477 struct objfile *objfile = dwarf2_per_objfile->objfile;
22478 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22480 if (attr_form_is_section_offset (attr)
22481 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22482 the section. If so, fall through to the complaint in the
22484 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22486 struct dwarf2_loclist_baton *baton;
22488 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22490 fill_in_loclist_baton (cu, baton, attr);
22492 if (cu->base_known == 0)
22493 complaint (&symfile_complaints,
22494 _("Location list used without "
22495 "specifying the CU base address."));
22497 SYMBOL_ACLASS_INDEX (sym) = (is_block
22498 ? dwarf2_loclist_block_index
22499 : dwarf2_loclist_index);
22500 SYMBOL_LOCATION_BATON (sym) = baton;
22504 struct dwarf2_locexpr_baton *baton;
22506 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22507 baton->per_cu = cu->per_cu;
22508 gdb_assert (baton->per_cu);
22510 if (attr_form_is_block (attr))
22512 /* Note that we're just copying the block's data pointer
22513 here, not the actual data. We're still pointing into the
22514 info_buffer for SYM's objfile; right now we never release
22515 that buffer, but when we do clean up properly this may
22517 baton->size = DW_BLOCK (attr)->size;
22518 baton->data = DW_BLOCK (attr)->data;
22522 dwarf2_invalid_attrib_class_complaint ("location description",
22523 SYMBOL_NATURAL_NAME (sym));
22527 SYMBOL_ACLASS_INDEX (sym) = (is_block
22528 ? dwarf2_locexpr_block_index
22529 : dwarf2_locexpr_index);
22530 SYMBOL_LOCATION_BATON (sym) = baton;
22534 /* Return the OBJFILE associated with the compilation unit CU. If CU
22535 came from a separate debuginfo file, then the master objfile is
22539 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22541 struct objfile *objfile = per_cu->objfile;
22543 /* Return the master objfile, so that we can report and look up the
22544 correct file containing this variable. */
22545 if (objfile->separate_debug_objfile_backlink)
22546 objfile = objfile->separate_debug_objfile_backlink;
22551 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22552 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22553 CU_HEADERP first. */
22555 static const struct comp_unit_head *
22556 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22557 struct dwarf2_per_cu_data *per_cu)
22559 const gdb_byte *info_ptr;
22562 return &per_cu->cu->header;
22564 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22566 memset (cu_headerp, 0, sizeof (*cu_headerp));
22567 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22568 rcuh_kind::COMPILE);
22573 /* Return the address size given in the compilation unit header for CU. */
22576 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22578 struct comp_unit_head cu_header_local;
22579 const struct comp_unit_head *cu_headerp;
22581 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22583 return cu_headerp->addr_size;
22586 /* Return the offset size given in the compilation unit header for CU. */
22589 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22591 struct comp_unit_head cu_header_local;
22592 const struct comp_unit_head *cu_headerp;
22594 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22596 return cu_headerp->offset_size;
22599 /* See its dwarf2loc.h declaration. */
22602 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22604 struct comp_unit_head cu_header_local;
22605 const struct comp_unit_head *cu_headerp;
22607 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22609 if (cu_headerp->version == 2)
22610 return cu_headerp->addr_size;
22612 return cu_headerp->offset_size;
22615 /* Return the text offset of the CU. The returned offset comes from
22616 this CU's objfile. If this objfile came from a separate debuginfo
22617 file, then the offset may be different from the corresponding
22618 offset in the parent objfile. */
22621 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22623 struct objfile *objfile = per_cu->objfile;
22625 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22628 /* Return DWARF version number of PER_CU. */
22631 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22633 return per_cu->dwarf_version;
22636 /* Locate the .debug_info compilation unit from CU's objfile which contains
22637 the DIE at OFFSET. Raises an error on failure. */
22639 static struct dwarf2_per_cu_data *
22640 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22641 unsigned int offset_in_dwz,
22642 struct objfile *objfile)
22644 struct dwarf2_per_cu_data *this_cu;
22646 const sect_offset *cu_off;
22649 high = dwarf2_per_objfile->n_comp_units - 1;
22652 struct dwarf2_per_cu_data *mid_cu;
22653 int mid = low + (high - low) / 2;
22655 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22656 cu_off = &mid_cu->sect_off;
22657 if (mid_cu->is_dwz > offset_in_dwz
22658 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
22663 gdb_assert (low == high);
22664 this_cu = dwarf2_per_objfile->all_comp_units[low];
22665 cu_off = &this_cu->sect_off;
22666 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
22668 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22669 error (_("Dwarf Error: could not find partial DIE containing "
22670 "offset 0x%x [in module %s]"),
22671 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
22673 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22675 return dwarf2_per_objfile->all_comp_units[low-1];
22679 this_cu = dwarf2_per_objfile->all_comp_units[low];
22680 if (low == dwarf2_per_objfile->n_comp_units - 1
22681 && sect_off >= this_cu->sect_off + this_cu->length)
22682 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22683 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22688 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22691 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22693 memset (cu, 0, sizeof (*cu));
22695 cu->per_cu = per_cu;
22696 cu->objfile = per_cu->objfile;
22697 obstack_init (&cu->comp_unit_obstack);
22700 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22703 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22704 enum language pretend_language)
22706 struct attribute *attr;
22708 /* Set the language we're debugging. */
22709 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22711 set_cu_language (DW_UNSND (attr), cu);
22714 cu->language = pretend_language;
22715 cu->language_defn = language_def (cu->language);
22718 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22721 /* Release one cached compilation unit, CU. We unlink it from the tree
22722 of compilation units, but we don't remove it from the read_in_chain;
22723 the caller is responsible for that.
22724 NOTE: DATA is a void * because this function is also used as a
22725 cleanup routine. */
22728 free_heap_comp_unit (void *data)
22730 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22732 gdb_assert (cu->per_cu != NULL);
22733 cu->per_cu->cu = NULL;
22736 obstack_free (&cu->comp_unit_obstack, NULL);
22741 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22742 when we're finished with it. We can't free the pointer itself, but be
22743 sure to unlink it from the cache. Also release any associated storage. */
22746 free_stack_comp_unit (void *data)
22748 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22750 gdb_assert (cu->per_cu != NULL);
22751 cu->per_cu->cu = NULL;
22754 obstack_free (&cu->comp_unit_obstack, NULL);
22755 cu->partial_dies = NULL;
22758 /* Free all cached compilation units. */
22761 free_cached_comp_units (void *data)
22763 struct dwarf2_per_cu_data *per_cu, **last_chain;
22765 per_cu = dwarf2_per_objfile->read_in_chain;
22766 last_chain = &dwarf2_per_objfile->read_in_chain;
22767 while (per_cu != NULL)
22769 struct dwarf2_per_cu_data *next_cu;
22771 next_cu = per_cu->cu->read_in_chain;
22773 free_heap_comp_unit (per_cu->cu);
22774 *last_chain = next_cu;
22780 /* Increase the age counter on each cached compilation unit, and free
22781 any that are too old. */
22784 age_cached_comp_units (void)
22786 struct dwarf2_per_cu_data *per_cu, **last_chain;
22788 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22789 per_cu = dwarf2_per_objfile->read_in_chain;
22790 while (per_cu != NULL)
22792 per_cu->cu->last_used ++;
22793 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22794 dwarf2_mark (per_cu->cu);
22795 per_cu = per_cu->cu->read_in_chain;
22798 per_cu = dwarf2_per_objfile->read_in_chain;
22799 last_chain = &dwarf2_per_objfile->read_in_chain;
22800 while (per_cu != NULL)
22802 struct dwarf2_per_cu_data *next_cu;
22804 next_cu = per_cu->cu->read_in_chain;
22806 if (!per_cu->cu->mark)
22808 free_heap_comp_unit (per_cu->cu);
22809 *last_chain = next_cu;
22812 last_chain = &per_cu->cu->read_in_chain;
22818 /* Remove a single compilation unit from the cache. */
22821 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22823 struct dwarf2_per_cu_data *per_cu, **last_chain;
22825 per_cu = dwarf2_per_objfile->read_in_chain;
22826 last_chain = &dwarf2_per_objfile->read_in_chain;
22827 while (per_cu != NULL)
22829 struct dwarf2_per_cu_data *next_cu;
22831 next_cu = per_cu->cu->read_in_chain;
22833 if (per_cu == target_per_cu)
22835 free_heap_comp_unit (per_cu->cu);
22837 *last_chain = next_cu;
22841 last_chain = &per_cu->cu->read_in_chain;
22847 /* Release all extra memory associated with OBJFILE. */
22850 dwarf2_free_objfile (struct objfile *objfile)
22853 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22854 dwarf2_objfile_data_key);
22856 if (dwarf2_per_objfile == NULL)
22859 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22860 free_cached_comp_units (NULL);
22862 if (dwarf2_per_objfile->quick_file_names_table)
22863 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22865 if (dwarf2_per_objfile->line_header_hash)
22866 htab_delete (dwarf2_per_objfile->line_header_hash);
22868 /* Everything else should be on the objfile obstack. */
22871 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22872 We store these in a hash table separate from the DIEs, and preserve them
22873 when the DIEs are flushed out of cache.
22875 The CU "per_cu" pointer is needed because offset alone is not enough to
22876 uniquely identify the type. A file may have multiple .debug_types sections,
22877 or the type may come from a DWO file. Furthermore, while it's more logical
22878 to use per_cu->section+offset, with Fission the section with the data is in
22879 the DWO file but we don't know that section at the point we need it.
22880 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22881 because we can enter the lookup routine, get_die_type_at_offset, from
22882 outside this file, and thus won't necessarily have PER_CU->cu.
22883 Fortunately, PER_CU is stable for the life of the objfile. */
22885 struct dwarf2_per_cu_offset_and_type
22887 const struct dwarf2_per_cu_data *per_cu;
22888 sect_offset sect_off;
22892 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22895 per_cu_offset_and_type_hash (const void *item)
22897 const struct dwarf2_per_cu_offset_and_type *ofs
22898 = (const struct dwarf2_per_cu_offset_and_type *) item;
22900 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
22903 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22906 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22908 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22909 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22910 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22911 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22913 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22914 && ofs_lhs->sect_off == ofs_rhs->sect_off);
22917 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22918 table if necessary. For convenience, return TYPE.
22920 The DIEs reading must have careful ordering to:
22921 * Not cause infite loops trying to read in DIEs as a prerequisite for
22922 reading current DIE.
22923 * Not trying to dereference contents of still incompletely read in types
22924 while reading in other DIEs.
22925 * Enable referencing still incompletely read in types just by a pointer to
22926 the type without accessing its fields.
22928 Therefore caller should follow these rules:
22929 * Try to fetch any prerequisite types we may need to build this DIE type
22930 before building the type and calling set_die_type.
22931 * After building type call set_die_type for current DIE as soon as
22932 possible before fetching more types to complete the current type.
22933 * Make the type as complete as possible before fetching more types. */
22935 static struct type *
22936 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22938 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22939 struct objfile *objfile = cu->objfile;
22940 struct attribute *attr;
22941 struct dynamic_prop prop;
22943 /* For Ada types, make sure that the gnat-specific data is always
22944 initialized (if not already set). There are a few types where
22945 we should not be doing so, because the type-specific area is
22946 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22947 where the type-specific area is used to store the floatformat).
22948 But this is not a problem, because the gnat-specific information
22949 is actually not needed for these types. */
22950 if (need_gnat_info (cu)
22951 && TYPE_CODE (type) != TYPE_CODE_FUNC
22952 && TYPE_CODE (type) != TYPE_CODE_FLT
22953 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22954 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22955 && TYPE_CODE (type) != TYPE_CODE_METHOD
22956 && !HAVE_GNAT_AUX_INFO (type))
22957 INIT_GNAT_SPECIFIC (type);
22959 /* Read DW_AT_allocated and set in type. */
22960 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22961 if (attr_form_is_block (attr))
22963 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22964 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22966 else if (attr != NULL)
22968 complaint (&symfile_complaints,
22969 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22970 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22971 to_underlying (die->sect_off));
22974 /* Read DW_AT_associated and set in type. */
22975 attr = dwarf2_attr (die, DW_AT_associated, cu);
22976 if (attr_form_is_block (attr))
22978 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22979 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22981 else if (attr != NULL)
22983 complaint (&symfile_complaints,
22984 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22985 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22986 to_underlying (die->sect_off));
22989 /* Read DW_AT_data_location and set in type. */
22990 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22991 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22992 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22994 if (dwarf2_per_objfile->die_type_hash == NULL)
22996 dwarf2_per_objfile->die_type_hash =
22997 htab_create_alloc_ex (127,
22998 per_cu_offset_and_type_hash,
22999 per_cu_offset_and_type_eq,
23001 &objfile->objfile_obstack,
23002 hashtab_obstack_allocate,
23003 dummy_obstack_deallocate);
23006 ofs.per_cu = cu->per_cu;
23007 ofs.sect_off = die->sect_off;
23009 slot = (struct dwarf2_per_cu_offset_and_type **)
23010 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23012 complaint (&symfile_complaints,
23013 _("A problem internal to GDB: DIE 0x%x has type already set"),
23014 to_underlying (die->sect_off));
23015 *slot = XOBNEW (&objfile->objfile_obstack,
23016 struct dwarf2_per_cu_offset_and_type);
23021 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23022 or return NULL if the die does not have a saved type. */
23024 static struct type *
23025 get_die_type_at_offset (sect_offset sect_off,
23026 struct dwarf2_per_cu_data *per_cu)
23028 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23030 if (dwarf2_per_objfile->die_type_hash == NULL)
23033 ofs.per_cu = per_cu;
23034 ofs.sect_off = sect_off;
23035 slot = ((struct dwarf2_per_cu_offset_and_type *)
23036 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23043 /* Look up the type for DIE in CU in die_type_hash,
23044 or return NULL if DIE does not have a saved type. */
23046 static struct type *
23047 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23049 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23052 /* Add a dependence relationship from CU to REF_PER_CU. */
23055 dwarf2_add_dependence (struct dwarf2_cu *cu,
23056 struct dwarf2_per_cu_data *ref_per_cu)
23060 if (cu->dependencies == NULL)
23062 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23063 NULL, &cu->comp_unit_obstack,
23064 hashtab_obstack_allocate,
23065 dummy_obstack_deallocate);
23067 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23069 *slot = ref_per_cu;
23072 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23073 Set the mark field in every compilation unit in the
23074 cache that we must keep because we are keeping CU. */
23077 dwarf2_mark_helper (void **slot, void *data)
23079 struct dwarf2_per_cu_data *per_cu;
23081 per_cu = (struct dwarf2_per_cu_data *) *slot;
23083 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23084 reading of the chain. As such dependencies remain valid it is not much
23085 useful to track and undo them during QUIT cleanups. */
23086 if (per_cu->cu == NULL)
23089 if (per_cu->cu->mark)
23091 per_cu->cu->mark = 1;
23093 if (per_cu->cu->dependencies != NULL)
23094 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23099 /* Set the mark field in CU and in every other compilation unit in the
23100 cache that we must keep because we are keeping CU. */
23103 dwarf2_mark (struct dwarf2_cu *cu)
23108 if (cu->dependencies != NULL)
23109 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23113 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23117 per_cu->cu->mark = 0;
23118 per_cu = per_cu->cu->read_in_chain;
23122 /* Trivial hash function for partial_die_info: the hash value of a DIE
23123 is its offset in .debug_info for this objfile. */
23126 partial_die_hash (const void *item)
23128 const struct partial_die_info *part_die
23129 = (const struct partial_die_info *) item;
23131 return to_underlying (part_die->sect_off);
23134 /* Trivial comparison function for partial_die_info structures: two DIEs
23135 are equal if they have the same offset. */
23138 partial_die_eq (const void *item_lhs, const void *item_rhs)
23140 const struct partial_die_info *part_die_lhs
23141 = (const struct partial_die_info *) item_lhs;
23142 const struct partial_die_info *part_die_rhs
23143 = (const struct partial_die_info *) item_rhs;
23145 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23148 static struct cmd_list_element *set_dwarf_cmdlist;
23149 static struct cmd_list_element *show_dwarf_cmdlist;
23152 set_dwarf_cmd (char *args, int from_tty)
23154 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23159 show_dwarf_cmd (char *args, int from_tty)
23161 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23164 /* Free data associated with OBJFILE, if necessary. */
23167 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23169 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23172 /* Make sure we don't accidentally use dwarf2_per_objfile while
23174 dwarf2_per_objfile = NULL;
23176 for (ix = 0; ix < data->n_comp_units; ++ix)
23177 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23179 for (ix = 0; ix < data->n_type_units; ++ix)
23180 VEC_free (dwarf2_per_cu_ptr,
23181 data->all_type_units[ix]->per_cu.imported_symtabs);
23182 xfree (data->all_type_units);
23184 VEC_free (dwarf2_section_info_def, data->types);
23186 if (data->dwo_files)
23187 free_dwo_files (data->dwo_files, objfile);
23188 if (data->dwp_file)
23189 gdb_bfd_unref (data->dwp_file->dbfd);
23191 if (data->dwz_file && data->dwz_file->dwz_bfd)
23192 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23196 /* The "save gdb-index" command. */
23198 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
23202 file_write (FILE *file, const void *data, size_t size)
23204 if (fwrite (data, 1, size, file) != size)
23205 error (_("couldn't data write to file"));
23208 /* Write the contents of VEC to FILE, with error checking. */
23210 template<class Elem>
23212 file_write (FILE *file, const std::vector<Elem> &vec)
23214 file_write (file, vec.data (), vec.size() * sizeof (vec[0]));
23217 /* In-memory buffer to prepare data to be written later to a file. */
23221 /* Add SIZE bytes at the end of the buffer. Returns a pointer to
23222 the start of the new block. */
23223 gdb_byte *append_space (size_t size)
23225 m_vec.resize (m_vec.size () + size);
23226 return &*m_vec.end () - size;
23229 /* Copy DATA to the end of the buffer. */
23230 template<typename T>
23231 void append_data (const T &data)
23233 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23234 reinterpret_cast<const gdb_byte *> (&data + 1),
23235 append_space (sizeof (data)));
23238 /* Copy CSTR (a null-terminated string) to the end of the buffer.
23239 The terminating null is appended too. */
23240 void append_cstr0 (const char *cstr)
23242 const size_t size = strlen (cstr) + 1;
23243 std::copy (cstr, cstr + size, append_space (size));
23246 /* Return the size of the buffer. */
23247 size_t size () const
23249 return m_vec.size ();
23252 /* Write the buffer to FILE. */
23253 void file_write (FILE *file) const
23255 ::file_write (file, m_vec);
23259 std::vector<gdb_byte> m_vec;
23262 /* An entry in the symbol table. */
23263 struct symtab_index_entry
23265 /* The name of the symbol. */
23267 /* The offset of the name in the constant pool. */
23268 offset_type index_offset;
23269 /* A sorted vector of the indices of all the CUs that hold an object
23271 std::vector<offset_type> cu_indices;
23274 /* The symbol table. This is a power-of-2-sized hash table. */
23275 struct mapped_symtab
23279 data.resize (1024);
23282 offset_type n_elements = 0;
23283 std::vector<std::unique_ptr<symtab_index_entry>> data;
23286 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
23289 Function is used only during write_hash_table so no index format backward
23290 compatibility is needed. */
23292 static std::unique_ptr<symtab_index_entry> &
23293 find_slot (struct mapped_symtab *symtab, const char *name)
23295 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23297 index = hash & (symtab->data.size () - 1);
23298 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
23302 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23303 return symtab->data[index];
23304 index = (index + step) & (symtab->data.size () - 1);
23308 /* Expand SYMTAB's hash table. */
23311 hash_expand (struct mapped_symtab *symtab)
23313 auto old_entries = std::move (symtab->data);
23315 symtab->data.clear ();
23316 symtab->data.resize (old_entries.size () * 2);
23318 for (auto &it : old_entries)
23321 auto &ref = find_slot (symtab, it->name);
23322 ref = std::move (it);
23326 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23327 CU_INDEX is the index of the CU in which the symbol appears.
23328 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23331 add_index_entry (struct mapped_symtab *symtab, const char *name,
23332 int is_static, gdb_index_symbol_kind kind,
23333 offset_type cu_index)
23335 offset_type cu_index_and_attrs;
23337 ++symtab->n_elements;
23338 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
23339 hash_expand (symtab);
23341 std::unique_ptr<symtab_index_entry> &slot = find_slot (symtab, name);
23344 slot.reset (new symtab_index_entry ());
23346 /* index_offset is set later. */
23349 cu_index_and_attrs = 0;
23350 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23351 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23352 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23354 /* We don't want to record an index value twice as we want to avoid the
23356 We process all global symbols and then all static symbols
23357 (which would allow us to avoid the duplication by only having to check
23358 the last entry pushed), but a symbol could have multiple kinds in one CU.
23359 To keep things simple we don't worry about the duplication here and
23360 sort and uniqufy the list after we've processed all symbols. */
23361 slot->cu_indices.push_back (cu_index_and_attrs);
23364 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23367 uniquify_cu_indices (struct mapped_symtab *symtab)
23369 for (const auto &entry : symtab->data)
23371 if (entry && !entry->cu_indices.empty ())
23373 auto &cu_indices = entry->cu_indices;
23374 std::sort (cu_indices.begin (), cu_indices.end ());
23375 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23376 cu_indices.erase (from, cu_indices.end ());
23381 /* A form of 'const char *' suitable for container keys. Only the
23382 pointer is stored. The strings themselves are compared, not the
23387 c_str_view (const char *cstr)
23391 bool operator== (const c_str_view &other) const
23393 return strcmp (m_cstr, other.m_cstr) == 0;
23397 friend class c_str_view_hasher;
23398 const char *const m_cstr;
23401 /* A std::unordered_map::hasher for c_str_view that uses the right
23402 hash function for strings in a mapped index. */
23403 class c_str_view_hasher
23406 size_t operator () (const c_str_view &x) const
23408 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23412 /* A std::unordered_map::hasher for std::vector<>. */
23413 template<typename T>
23414 class vector_hasher
23417 size_t operator () (const std::vector<T> &key) const
23419 return iterative_hash (key.data (),
23420 sizeof (key.front ()) * key.size (), 0);
23424 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23425 constant pool entries going into the data buffer CPOOL. */
23428 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23431 /* Elements are sorted vectors of the indices of all the CUs that
23432 hold an object of this name. */
23433 std::unordered_map<std::vector<offset_type>, offset_type,
23434 vector_hasher<offset_type>>
23437 /* We add all the index vectors to the constant pool first, to
23438 ensure alignment is ok. */
23439 for (const std::unique_ptr<symtab_index_entry> &it : symtab->data)
23443 gdb_assert (it->index_offset == 0);
23444 const auto insertpair
23445 = symbol_hash_table.emplace (it->cu_indices, cpool.size ());
23446 it->index_offset = insertpair.first->second;
23447 if (!insertpair.second)
23449 cpool.append_data (MAYBE_SWAP (it->cu_indices.size ()));
23450 for (const auto iter : it->cu_indices)
23451 cpool.append_data (MAYBE_SWAP (iter));
23455 /* Now write out the hash table. */
23456 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
23457 for (const auto &it : symtab->data)
23459 offset_type str_off, vec_off;
23463 const auto insertpair = str_table.emplace (it->name, cpool.size ());
23464 if (insertpair.second)
23465 cpool.append_cstr0 (it->name);
23466 str_off = insertpair.first->second;
23467 vec_off = it->index_offset;
23471 /* While 0 is a valid constant pool index, it is not valid
23472 to have 0 for both offsets. */
23477 output.append_data (MAYBE_SWAP (str_off));
23478 output.append_data (MAYBE_SWAP (vec_off));
23482 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
23484 /* Helper struct for building the address table. */
23485 struct addrmap_index_data
23487 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23488 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23491 struct objfile *objfile;
23492 data_buf &addr_vec;
23493 psym_index_map &cu_index_htab;
23495 /* Non-zero if the previous_* fields are valid.
23496 We can't write an entry until we see the next entry (since it is only then
23497 that we know the end of the entry). */
23498 int previous_valid;
23499 /* Index of the CU in the table of all CUs in the index file. */
23500 unsigned int previous_cu_index;
23501 /* Start address of the CU. */
23502 CORE_ADDR previous_cu_start;
23505 /* Write an address entry to ADDR_VEC. */
23508 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
23509 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23511 CORE_ADDR baseaddr;
23513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23515 store_unsigned_integer (addr_vec.append_space (8), 8, BFD_ENDIAN_LITTLE,
23517 store_unsigned_integer (addr_vec.append_space (8), 8, BFD_ENDIAN_LITTLE,
23519 addr_vec.append_data (MAYBE_SWAP (cu_index));
23522 /* Worker function for traversing an addrmap to build the address table. */
23525 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23527 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23528 struct partial_symtab *pst = (struct partial_symtab *) obj;
23530 if (data->previous_valid)
23531 add_address_entry (data->objfile, data->addr_vec,
23532 data->previous_cu_start, start_addr,
23533 data->previous_cu_index);
23535 data->previous_cu_start = start_addr;
23538 const auto it = data->cu_index_htab.find (pst);
23539 gdb_assert (it != data->cu_index_htab.cend ());
23540 data->previous_cu_index = it->second;
23541 data->previous_valid = 1;
23544 data->previous_valid = 0;
23549 /* Write OBJFILE's address map to ADDR_VEC.
23550 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23551 in the index file. */
23554 write_address_map (struct objfile *objfile, data_buf &addr_vec,
23555 psym_index_map &cu_index_htab)
23557 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
23559 /* When writing the address table, we have to cope with the fact that
23560 the addrmap iterator only provides the start of a region; we have to
23561 wait until the next invocation to get the start of the next region. */
23563 addrmap_index_data.objfile = objfile;
23564 addrmap_index_data.previous_valid = 0;
23566 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23567 &addrmap_index_data);
23569 /* It's highly unlikely the last entry (end address = 0xff...ff)
23570 is valid, but we should still handle it.
23571 The end address is recorded as the start of the next region, but that
23572 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23574 if (addrmap_index_data.previous_valid)
23575 add_address_entry (objfile, addr_vec,
23576 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23577 addrmap_index_data.previous_cu_index);
23580 /* Return the symbol kind of PSYM. */
23582 static gdb_index_symbol_kind
23583 symbol_kind (struct partial_symbol *psym)
23585 domain_enum domain = PSYMBOL_DOMAIN (psym);
23586 enum address_class aclass = PSYMBOL_CLASS (psym);
23594 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23596 return GDB_INDEX_SYMBOL_KIND_TYPE;
23598 case LOC_CONST_BYTES:
23599 case LOC_OPTIMIZED_OUT:
23601 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23603 /* Note: It's currently impossible to recognize psyms as enum values
23604 short of reading the type info. For now punt. */
23605 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23607 /* There are other LOC_FOO values that one might want to classify
23608 as variables, but dwarf2read.c doesn't currently use them. */
23609 return GDB_INDEX_SYMBOL_KIND_OTHER;
23611 case STRUCT_DOMAIN:
23612 return GDB_INDEX_SYMBOL_KIND_TYPE;
23614 return GDB_INDEX_SYMBOL_KIND_OTHER;
23618 /* Add a list of partial symbols to SYMTAB. */
23621 write_psymbols (struct mapped_symtab *symtab,
23622 std::unordered_set<partial_symbol *> &psyms_seen,
23623 struct partial_symbol **psymp,
23625 offset_type cu_index,
23628 for (; count-- > 0; ++psymp)
23630 struct partial_symbol *psym = *psymp;
23632 if (SYMBOL_LANGUAGE (psym) == language_ada)
23633 error (_("Ada is not currently supported by the index"));
23635 /* Only add a given psymbol once. */
23636 if (psyms_seen.insert (psym).second)
23638 gdb_index_symbol_kind kind = symbol_kind (psym);
23640 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23641 is_static, kind, cu_index);
23646 /* A helper struct used when iterating over debug_types. */
23647 struct signatured_type_index_data
23649 signatured_type_index_data (data_buf &types_list_,
23650 std::unordered_set<partial_symbol *> &psyms_seen_)
23651 : types_list (types_list_), psyms_seen (psyms_seen_)
23654 struct objfile *objfile;
23655 struct mapped_symtab *symtab;
23656 data_buf &types_list;
23657 std::unordered_set<partial_symbol *> &psyms_seen;
23661 /* A helper function that writes a single signatured_type to an
23665 write_one_signatured_type (void **slot, void *d)
23667 struct signatured_type_index_data *info
23668 = (struct signatured_type_index_data *) d;
23669 struct signatured_type *entry = (struct signatured_type *) *slot;
23670 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23672 write_psymbols (info->symtab,
23674 info->objfile->global_psymbols.list
23675 + psymtab->globals_offset,
23676 psymtab->n_global_syms, info->cu_index,
23678 write_psymbols (info->symtab,
23680 info->objfile->static_psymbols.list
23681 + psymtab->statics_offset,
23682 psymtab->n_static_syms, info->cu_index,
23685 store_unsigned_integer (info->types_list.append_space (8), 8,
23687 to_underlying (entry->per_cu.sect_off));
23688 store_unsigned_integer (info->types_list.append_space (8), 8,
23690 to_underlying (entry->type_offset_in_tu));
23691 store_unsigned_integer (info->types_list.append_space (8), 8,
23692 BFD_ENDIAN_LITTLE, entry->signature);
23699 /* Recurse into all "included" dependencies and write their symbols as
23700 if they appeared in this psymtab. */
23703 recursively_write_psymbols (struct objfile *objfile,
23704 struct partial_symtab *psymtab,
23705 struct mapped_symtab *symtab,
23706 std::unordered_set<partial_symbol *> &psyms_seen,
23707 offset_type cu_index)
23711 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23712 if (psymtab->dependencies[i]->user != NULL)
23713 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23714 symtab, psyms_seen, cu_index);
23716 write_psymbols (symtab,
23718 objfile->global_psymbols.list + psymtab->globals_offset,
23719 psymtab->n_global_syms, cu_index,
23721 write_psymbols (symtab,
23723 objfile->static_psymbols.list + psymtab->statics_offset,
23724 psymtab->n_static_syms, cu_index,
23728 /* Closes FILE on scope exit. */
23731 explicit file_closer (FILE *file)
23736 { fclose (m_file); }
23742 /* Create an index file for OBJFILE in the directory DIR. */
23745 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23747 if (dwarf2_per_objfile->using_index)
23748 error (_("Cannot use an index to create the index"));
23750 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23751 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23753 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23757 if (stat (objfile_name (objfile), &st) < 0)
23758 perror_with_name (objfile_name (objfile));
23760 std::string filename (std::string (dir) + SLASH_STRING
23761 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
23763 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb");
23765 error (_("Can't open `%s' for writing"), filename.c_str ());
23767 file_closer close_out_file (out_file);
23768 gdb::unlinker unlink_file (filename.c_str ());
23770 mapped_symtab symtab;
23772 std::unordered_set<partial_symbol *> psyms_seen;
23774 /* While we're scanning CU's create a table that maps a psymtab pointer
23775 (which is what addrmap records) to its index (which is what is recorded
23776 in the index file). This will later be needed to write the address
23778 psym_index_map cu_index_htab;
23779 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
23781 /* The CU list is already sorted, so we don't need to do additional
23782 work here. Also, the debug_types entries do not appear in
23783 all_comp_units, but only in their own hash table. */
23784 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23786 struct dwarf2_per_cu_data *per_cu
23787 = dwarf2_per_objfile->all_comp_units[i];
23788 struct partial_symtab *psymtab = per_cu->v.psymtab;
23790 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23791 It may be referenced from a local scope but in such case it does not
23792 need to be present in .gdb_index. */
23793 if (psymtab == NULL)
23796 if (psymtab->user == NULL)
23797 recursively_write_psymbols (objfile, psymtab, &symtab,
23800 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23801 gdb_assert (insertpair.second);
23803 store_unsigned_integer (cu_list.append_space (8), 8,
23805 to_underlying (per_cu->sect_off));
23806 store_unsigned_integer (cu_list.append_space (8), 8,
23807 BFD_ENDIAN_LITTLE, per_cu->length);
23810 /* Dump the address map. */
23812 write_address_map (objfile, addr_vec, cu_index_htab);
23814 /* Write out the .debug_type entries, if any. */
23815 data_buf types_cu_list;
23816 if (dwarf2_per_objfile->signatured_types)
23818 signatured_type_index_data sig_data (types_cu_list,
23821 sig_data.objfile = objfile;
23822 sig_data.symtab = &symtab;
23823 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23824 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23825 write_one_signatured_type, &sig_data);
23828 /* Now that we've processed all symbols we can shrink their cu_indices
23830 uniquify_cu_indices (&symtab);
23832 data_buf symtab_vec, constant_pool;
23833 write_hash_table (&symtab, symtab_vec, constant_pool);
23836 const offset_type size_of_contents = 6 * sizeof (offset_type);
23837 offset_type total_len = size_of_contents;
23839 /* The version number. */
23840 contents.append_data (MAYBE_SWAP (8));
23842 /* The offset of the CU list from the start of the file. */
23843 contents.append_data (MAYBE_SWAP (total_len));
23844 total_len += cu_list.size ();
23846 /* The offset of the types CU list from the start of the file. */
23847 contents.append_data (MAYBE_SWAP (total_len));
23848 total_len += types_cu_list.size ();
23850 /* The offset of the address table from the start of the file. */
23851 contents.append_data (MAYBE_SWAP (total_len));
23852 total_len += addr_vec.size ();
23854 /* The offset of the symbol table from the start of the file. */
23855 contents.append_data (MAYBE_SWAP (total_len));
23856 total_len += symtab_vec.size ();
23858 /* The offset of the constant pool from the start of the file. */
23859 contents.append_data (MAYBE_SWAP (total_len));
23860 total_len += constant_pool.size ();
23862 gdb_assert (contents.size () == size_of_contents);
23864 contents.file_write (out_file);
23865 cu_list.file_write (out_file);
23866 types_cu_list.file_write (out_file);
23867 addr_vec.file_write (out_file);
23868 symtab_vec.file_write (out_file);
23869 constant_pool.file_write (out_file);
23871 /* We want to keep the file. */
23872 unlink_file.keep ();
23875 /* Implementation of the `save gdb-index' command.
23877 Note that the file format used by this command is documented in the
23878 GDB manual. Any changes here must be documented there. */
23881 save_gdb_index_command (char *arg, int from_tty)
23883 struct objfile *objfile;
23886 error (_("usage: save gdb-index DIRECTORY"));
23888 ALL_OBJFILES (objfile)
23892 /* If the objfile does not correspond to an actual file, skip it. */
23893 if (stat (objfile_name (objfile), &st) < 0)
23897 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23898 dwarf2_objfile_data_key);
23899 if (dwarf2_per_objfile)
23904 write_psymtabs_to_index (objfile, arg);
23906 CATCH (except, RETURN_MASK_ERROR)
23908 exception_fprintf (gdb_stderr, except,
23909 _("Error while writing index for `%s': "),
23910 objfile_name (objfile));
23919 int dwarf_always_disassemble;
23922 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23923 struct cmd_list_element *c, const char *value)
23925 fprintf_filtered (file,
23926 _("Whether to always disassemble "
23927 "DWARF expressions is %s.\n"),
23932 show_check_physname (struct ui_file *file, int from_tty,
23933 struct cmd_list_element *c, const char *value)
23935 fprintf_filtered (file,
23936 _("Whether to check \"physname\" is %s.\n"),
23940 void _initialize_dwarf2_read (void);
23943 _initialize_dwarf2_read (void)
23945 struct cmd_list_element *c;
23947 dwarf2_objfile_data_key
23948 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
23950 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23951 Set DWARF specific variables.\n\
23952 Configure DWARF variables such as the cache size"),
23953 &set_dwarf_cmdlist, "maintenance set dwarf ",
23954 0/*allow-unknown*/, &maintenance_set_cmdlist);
23956 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23957 Show DWARF specific variables\n\
23958 Show DWARF variables such as the cache size"),
23959 &show_dwarf_cmdlist, "maintenance show dwarf ",
23960 0/*allow-unknown*/, &maintenance_show_cmdlist);
23962 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23963 &dwarf_max_cache_age, _("\
23964 Set the upper bound on the age of cached DWARF compilation units."), _("\
23965 Show the upper bound on the age of cached DWARF compilation units."), _("\
23966 A higher limit means that cached compilation units will be stored\n\
23967 in memory longer, and more total memory will be used. Zero disables\n\
23968 caching, which can slow down startup."),
23970 show_dwarf_max_cache_age,
23971 &set_dwarf_cmdlist,
23972 &show_dwarf_cmdlist);
23974 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
23975 &dwarf_always_disassemble, _("\
23976 Set whether `info address' always disassembles DWARF expressions."), _("\
23977 Show whether `info address' always disassembles DWARF expressions."), _("\
23978 When enabled, DWARF expressions are always printed in an assembly-like\n\
23979 syntax. When disabled, expressions will be printed in a more\n\
23980 conversational style, when possible."),
23982 show_dwarf_always_disassemble,
23983 &set_dwarf_cmdlist,
23984 &show_dwarf_cmdlist);
23986 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23987 Set debugging of the DWARF reader."), _("\
23988 Show debugging of the DWARF reader."), _("\
23989 When enabled (non-zero), debugging messages are printed during DWARF\n\
23990 reading and symtab expansion. A value of 1 (one) provides basic\n\
23991 information. A value greater than 1 provides more verbose information."),
23994 &setdebuglist, &showdebuglist);
23996 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23997 Set debugging of the DWARF DIE reader."), _("\
23998 Show debugging of the DWARF DIE reader."), _("\
23999 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24000 The value is the maximum depth to print."),
24003 &setdebuglist, &showdebuglist);
24005 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24006 Set debugging of the dwarf line reader."), _("\
24007 Show debugging of the dwarf line reader."), _("\
24008 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24009 A value of 1 (one) provides basic information.\n\
24010 A value greater than 1 provides more verbose information."),
24013 &setdebuglist, &showdebuglist);
24015 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24016 Set cross-checking of \"physname\" code against demangler."), _("\
24017 Show cross-checking of \"physname\" code against demangler."), _("\
24018 When enabled, GDB's internal \"physname\" code is checked against\n\
24020 NULL, show_check_physname,
24021 &setdebuglist, &showdebuglist);
24023 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24024 no_class, &use_deprecated_index_sections, _("\
24025 Set whether to use deprecated gdb_index sections."), _("\
24026 Show whether to use deprecated gdb_index sections."), _("\
24027 When enabled, deprecated .gdb_index sections are used anyway.\n\
24028 Normally they are ignored either because of a missing feature or\n\
24029 performance issue.\n\
24030 Warning: This option must be enabled before gdb reads the file."),
24033 &setlist, &showlist);
24035 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24037 Save a gdb-index file.\n\
24038 Usage: save gdb-index DIRECTORY"),
24040 set_cmd_completer (c, filename_completer);
24042 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24043 &dwarf2_locexpr_funcs);
24044 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24045 &dwarf2_loclist_funcs);
24047 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24048 &dwarf2_block_frame_base_locexpr_funcs);
24049 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24050 &dwarf2_block_frame_base_loclist_funcs);