1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
45 #include "complaints.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
55 #include "typeprint.h"
58 #include "completer.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
69 #include "filestuff.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "filename-seen-cache.h"
79 #include <sys/types.h>
81 #include <unordered_set>
82 #include <unordered_map>
84 typedef struct symbol *symbolp;
87 /* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90 static unsigned int dwarf_read_debug = 0;
92 /* When non-zero, dump DIEs after they are read in. */
93 static unsigned int dwarf_die_debug = 0;
95 /* When non-zero, dump line number entries as they are read in. */
96 static unsigned int dwarf_line_debug = 0;
98 /* When non-zero, cross-check physname against demangler. */
99 static int check_physname = 0;
101 /* When non-zero, do not reject deprecated .gdb_index sections. */
102 static int use_deprecated_index_sections = 0;
104 static const struct objfile_data *dwarf2_objfile_data_key;
106 /* The "aclass" indices for various kinds of computed DWARF symbols. */
108 static int dwarf2_locexpr_index;
109 static int dwarf2_loclist_index;
110 static int dwarf2_locexpr_block_index;
111 static int dwarf2_loclist_block_index;
113 /* A descriptor for dwarf sections.
115 S.ASECTION, SIZE are typically initialized when the objfile is first
116 scanned. BUFFER, READIN are filled in later when the section is read.
117 If the section contained compressed data then SIZE is updated to record
118 the uncompressed size of the section.
120 DWP file format V2 introduces a wrinkle that is easiest to handle by
121 creating the concept of virtual sections contained within a real section.
122 In DWP V2 the sections of the input DWO files are concatenated together
123 into one section, but section offsets are kept relative to the original
125 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
126 the real section this "virtual" section is contained in, and BUFFER,SIZE
127 describe the virtual section. */
129 struct dwarf2_section_info
133 /* If this is a real section, the bfd section. */
135 /* If this is a virtual section, pointer to the containing ("real")
137 struct dwarf2_section_info *containing_section;
139 /* Pointer to section data, only valid if readin. */
140 const gdb_byte *buffer;
141 /* The size of the section, real or virtual. */
143 /* If this is a virtual section, the offset in the real section.
144 Only valid if is_virtual. */
145 bfd_size_type virtual_offset;
146 /* True if we have tried to read this section. */
148 /* True if this is a virtual section, False otherwise.
149 This specifies which of s.section and s.containing_section to use. */
153 typedef struct dwarf2_section_info dwarf2_section_info_def;
154 DEF_VEC_O (dwarf2_section_info_def);
156 /* All offsets in the index are of this type. It must be
157 architecture-independent. */
158 typedef uint32_t offset_type;
160 DEF_VEC_I (offset_type);
162 /* Ensure only legit values are used. */
163 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
165 gdb_assert ((unsigned int) (value) <= 1); \
166 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
169 /* Ensure only legit values are used. */
170 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
172 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
173 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
174 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
177 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
178 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
180 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
181 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
184 /* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
188 /* Index data format version. */
191 /* The total length of the buffer. */
194 /* A pointer to the address table data. */
195 const gdb_byte *address_table;
197 /* Size of the address table data in bytes. */
198 offset_type address_table_size;
200 /* The symbol table, implemented as a hash table. */
201 const offset_type *symbol_table;
203 /* Size in slots, each slot is 2 offset_types. */
204 offset_type symbol_table_slots;
206 /* A pointer to the constant pool. */
207 const char *constant_pool;
210 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
211 DEF_VEC_P (dwarf2_per_cu_ptr);
215 int nr_uniq_abbrev_tables;
217 int nr_symtab_sharers;
218 int nr_stmt_less_type_units;
219 int nr_all_type_units_reallocs;
222 /* Collection of data recorded per objfile.
223 This hangs off of dwarf2_objfile_data_key. */
225 struct dwarf2_per_objfile
227 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
228 dwarf2 section names, or is NULL if the standard ELF names are
230 dwarf2_per_objfile (struct objfile *objfile,
231 const dwarf2_debug_sections *names);
233 ~dwarf2_per_objfile ();
236 dwarf2_per_objfile (const dwarf2_per_objfile &) = delete;
237 void operator= (const dwarf2_per_objfile &) = delete;
239 /* Free all cached compilation units. */
240 void free_cached_comp_units ();
242 /* This function is mapped across the sections and remembers the
243 offset and size of each of the debugging sections we are
245 void locate_sections (bfd *abfd, asection *sectp,
246 const dwarf2_debug_sections &names);
249 dwarf2_section_info info {};
250 dwarf2_section_info abbrev {};
251 dwarf2_section_info line {};
252 dwarf2_section_info loc {};
253 dwarf2_section_info loclists {};
254 dwarf2_section_info macinfo {};
255 dwarf2_section_info macro {};
256 dwarf2_section_info str {};
257 dwarf2_section_info line_str {};
258 dwarf2_section_info ranges {};
259 dwarf2_section_info rnglists {};
260 dwarf2_section_info addr {};
261 dwarf2_section_info frame {};
262 dwarf2_section_info eh_frame {};
263 dwarf2_section_info gdb_index {};
265 VEC (dwarf2_section_info_def) *types = NULL;
268 struct objfile *objfile = NULL;
270 /* Table of all the compilation units. This is used to locate
271 the target compilation unit of a particular reference. */
272 struct dwarf2_per_cu_data **all_comp_units = NULL;
274 /* The number of compilation units in ALL_COMP_UNITS. */
275 int n_comp_units = 0;
277 /* The number of .debug_types-related CUs. */
278 int n_type_units = 0;
280 /* The number of elements allocated in all_type_units.
281 If there are skeleton-less TUs, we add them to all_type_units lazily. */
282 int n_allocated_type_units = 0;
284 /* The .debug_types-related CUs (TUs).
285 This is stored in malloc space because we may realloc it. */
286 struct signatured_type **all_type_units = NULL;
288 /* Table of struct type_unit_group objects.
289 The hash key is the DW_AT_stmt_list value. */
290 htab_t type_unit_groups {};
292 /* A table mapping .debug_types signatures to its signatured_type entry.
293 This is NULL if the .debug_types section hasn't been read in yet. */
294 htab_t signatured_types {};
296 /* Type unit statistics, to see how well the scaling improvements
298 struct tu_stats tu_stats {};
300 /* A chain of compilation units that are currently read in, so that
301 they can be freed later. */
302 dwarf2_per_cu_data *read_in_chain = NULL;
304 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
305 This is NULL if the table hasn't been allocated yet. */
308 /* True if we've checked for whether there is a DWP file. */
309 bool dwp_checked = false;
311 /* The DWP file if there is one, or NULL. */
312 struct dwp_file *dwp_file = NULL;
314 /* The shared '.dwz' file, if one exists. This is used when the
315 original data was compressed using 'dwz -m'. */
316 struct dwz_file *dwz_file = NULL;
318 /* A flag indicating whether this objfile has a section loaded at a
320 bool has_section_at_zero = false;
322 /* True if we are using the mapped index,
323 or we are faking it for OBJF_READNOW's sake. */
324 bool using_index = false;
326 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
327 mapped_index *index_table = NULL;
329 /* When using index_table, this keeps track of all quick_file_names entries.
330 TUs typically share line table entries with a CU, so we maintain a
331 separate table of all line table entries to support the sharing.
332 Note that while there can be way more TUs than CUs, we've already
333 sorted all the TUs into "type unit groups", grouped by their
334 DW_AT_stmt_list value. Therefore the only sharing done here is with a
335 CU and its associated TU group if there is one. */
336 htab_t quick_file_names_table {};
338 /* Set during partial symbol reading, to prevent queueing of full
340 bool reading_partial_symbols = false;
342 /* Table mapping type DIEs to their struct type *.
343 This is NULL if not allocated yet.
344 The mapping is done via (CU/TU + DIE offset) -> type. */
345 htab_t die_type_hash {};
347 /* The CUs we recently read. */
348 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
350 /* Table containing line_header indexed by offset and offset_in_dwz. */
351 htab_t line_header_hash {};
353 /* Table containing all filenames. This is an optional because the
354 table is lazily constructed on first access. */
355 gdb::optional<filename_seen_cache> filenames_cache;
358 static struct dwarf2_per_objfile *dwarf2_per_objfile;
360 /* Default names of the debugging sections. */
362 /* Note that if the debugging section has been compressed, it might
363 have a name like .zdebug_info. */
365 static const struct dwarf2_debug_sections dwarf2_elf_names =
367 { ".debug_info", ".zdebug_info" },
368 { ".debug_abbrev", ".zdebug_abbrev" },
369 { ".debug_line", ".zdebug_line" },
370 { ".debug_loc", ".zdebug_loc" },
371 { ".debug_loclists", ".zdebug_loclists" },
372 { ".debug_macinfo", ".zdebug_macinfo" },
373 { ".debug_macro", ".zdebug_macro" },
374 { ".debug_str", ".zdebug_str" },
375 { ".debug_line_str", ".zdebug_line_str" },
376 { ".debug_ranges", ".zdebug_ranges" },
377 { ".debug_rnglists", ".zdebug_rnglists" },
378 { ".debug_types", ".zdebug_types" },
379 { ".debug_addr", ".zdebug_addr" },
380 { ".debug_frame", ".zdebug_frame" },
381 { ".eh_frame", NULL },
382 { ".gdb_index", ".zgdb_index" },
386 /* List of DWO/DWP sections. */
388 static const struct dwop_section_names
390 struct dwarf2_section_names abbrev_dwo;
391 struct dwarf2_section_names info_dwo;
392 struct dwarf2_section_names line_dwo;
393 struct dwarf2_section_names loc_dwo;
394 struct dwarf2_section_names loclists_dwo;
395 struct dwarf2_section_names macinfo_dwo;
396 struct dwarf2_section_names macro_dwo;
397 struct dwarf2_section_names str_dwo;
398 struct dwarf2_section_names str_offsets_dwo;
399 struct dwarf2_section_names types_dwo;
400 struct dwarf2_section_names cu_index;
401 struct dwarf2_section_names tu_index;
405 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
406 { ".debug_info.dwo", ".zdebug_info.dwo" },
407 { ".debug_line.dwo", ".zdebug_line.dwo" },
408 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
409 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
410 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
411 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
412 { ".debug_str.dwo", ".zdebug_str.dwo" },
413 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
414 { ".debug_types.dwo", ".zdebug_types.dwo" },
415 { ".debug_cu_index", ".zdebug_cu_index" },
416 { ".debug_tu_index", ".zdebug_tu_index" },
419 /* local data types */
421 /* The data in a compilation unit header, after target2host
422 translation, looks like this. */
423 struct comp_unit_head
427 unsigned char addr_size;
428 unsigned char signed_addr_p;
429 sect_offset abbrev_sect_off;
431 /* Size of file offsets; either 4 or 8. */
432 unsigned int offset_size;
434 /* Size of the length field; either 4 or 12. */
435 unsigned int initial_length_size;
437 enum dwarf_unit_type unit_type;
439 /* Offset to the first byte of this compilation unit header in the
440 .debug_info section, for resolving relative reference dies. */
441 sect_offset sect_off;
443 /* Offset to first die in this cu from the start of the cu.
444 This will be the first byte following the compilation unit header. */
445 cu_offset first_die_cu_offset;
447 /* 64-bit signature of this type unit - it is valid only for
448 UNIT_TYPE DW_UT_type. */
451 /* For types, offset in the type's DIE of the type defined by this TU. */
452 cu_offset type_cu_offset_in_tu;
455 /* Type used for delaying computation of method physnames.
456 See comments for compute_delayed_physnames. */
457 struct delayed_method_info
459 /* The type to which the method is attached, i.e., its parent class. */
462 /* The index of the method in the type's function fieldlists. */
465 /* The index of the method in the fieldlist. */
468 /* The name of the DIE. */
471 /* The DIE associated with this method. */
472 struct die_info *die;
475 typedef struct delayed_method_info delayed_method_info;
476 DEF_VEC_O (delayed_method_info);
478 /* Internal state when decoding a particular compilation unit. */
481 /* The objfile containing this compilation unit. */
482 struct objfile *objfile;
484 /* The header of the compilation unit. */
485 struct comp_unit_head header;
487 /* Base address of this compilation unit. */
488 CORE_ADDR base_address;
490 /* Non-zero if base_address has been set. */
493 /* The language we are debugging. */
494 enum language language;
495 const struct language_defn *language_defn;
497 const char *producer;
499 /* The generic symbol table building routines have separate lists for
500 file scope symbols and all all other scopes (local scopes). So
501 we need to select the right one to pass to add_symbol_to_list().
502 We do it by keeping a pointer to the correct list in list_in_scope.
504 FIXME: The original dwarf code just treated the file scope as the
505 first local scope, and all other local scopes as nested local
506 scopes, and worked fine. Check to see if we really need to
507 distinguish these in buildsym.c. */
508 struct pending **list_in_scope;
510 /* The abbrev table for this CU.
511 Normally this points to the abbrev table in the objfile.
512 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
513 struct abbrev_table *abbrev_table;
515 /* Hash table holding all the loaded partial DIEs
516 with partial_die->offset.SECT_OFF as hash. */
519 /* Storage for things with the same lifetime as this read-in compilation
520 unit, including partial DIEs. */
521 struct obstack comp_unit_obstack;
523 /* When multiple dwarf2_cu structures are living in memory, this field
524 chains them all together, so that they can be released efficiently.
525 We will probably also want a generation counter so that most-recently-used
526 compilation units are cached... */
527 struct dwarf2_per_cu_data *read_in_chain;
529 /* Backlink to our per_cu entry. */
530 struct dwarf2_per_cu_data *per_cu;
532 /* How many compilation units ago was this CU last referenced? */
535 /* A hash table of DIE cu_offset for following references with
536 die_info->offset.sect_off as hash. */
539 /* Full DIEs if read in. */
540 struct die_info *dies;
542 /* A set of pointers to dwarf2_per_cu_data objects for compilation
543 units referenced by this one. Only set during full symbol processing;
544 partial symbol tables do not have dependencies. */
547 /* Header data from the line table, during full symbol processing. */
548 struct line_header *line_header;
550 /* A list of methods which need to have physnames computed
551 after all type information has been read. */
552 VEC (delayed_method_info) *method_list;
554 /* To be copied to symtab->call_site_htab. */
555 htab_t call_site_htab;
557 /* Non-NULL if this CU came from a DWO file.
558 There is an invariant here that is important to remember:
559 Except for attributes copied from the top level DIE in the "main"
560 (or "stub") file in preparation for reading the DWO file
561 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
562 Either there isn't a DWO file (in which case this is NULL and the point
563 is moot), or there is and either we're not going to read it (in which
564 case this is NULL) or there is and we are reading it (in which case this
566 struct dwo_unit *dwo_unit;
568 /* The DW_AT_addr_base attribute if present, zero otherwise
569 (zero is a valid value though).
570 Note this value comes from the Fission stub CU/TU's DIE. */
573 /* The DW_AT_ranges_base attribute if present, zero otherwise
574 (zero is a valid value though).
575 Note this value comes from the Fission stub CU/TU's DIE.
576 Also note that the value is zero in the non-DWO case so this value can
577 be used without needing to know whether DWO files are in use or not.
578 N.B. This does not apply to DW_AT_ranges appearing in
579 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
580 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
581 DW_AT_ranges_base *would* have to be applied, and we'd have to care
582 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
583 ULONGEST ranges_base;
585 /* Mark used when releasing cached dies. */
586 unsigned int mark : 1;
588 /* This CU references .debug_loc. See the symtab->locations_valid field.
589 This test is imperfect as there may exist optimized debug code not using
590 any location list and still facing inlining issues if handled as
591 unoptimized code. For a future better test see GCC PR other/32998. */
592 unsigned int has_loclist : 1;
594 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
595 if all the producer_is_* fields are valid. This information is cached
596 because profiling CU expansion showed excessive time spent in
597 producer_is_gxx_lt_4_6. */
598 unsigned int checked_producer : 1;
599 unsigned int producer_is_gxx_lt_4_6 : 1;
600 unsigned int producer_is_gcc_lt_4_3 : 1;
601 unsigned int producer_is_icc : 1;
603 /* When set, the file that we're processing is known to have
604 debugging info for C++ namespaces. GCC 3.3.x did not produce
605 this information, but later versions do. */
607 unsigned int processing_has_namespace_info : 1;
610 /* Persistent data held for a compilation unit, even when not
611 processing it. We put a pointer to this structure in the
612 read_symtab_private field of the psymtab. */
614 struct dwarf2_per_cu_data
616 /* The start offset and length of this compilation unit.
617 NOTE: Unlike comp_unit_head.length, this length includes
619 If the DIE refers to a DWO file, this is always of the original die,
621 sect_offset sect_off;
624 /* DWARF standard version this data has been read from (such as 4 or 5). */
627 /* Flag indicating this compilation unit will be read in before
628 any of the current compilation units are processed. */
629 unsigned int queued : 1;
631 /* This flag will be set when reading partial DIEs if we need to load
632 absolutely all DIEs for this compilation unit, instead of just the ones
633 we think are interesting. It gets set if we look for a DIE in the
634 hash table and don't find it. */
635 unsigned int load_all_dies : 1;
637 /* Non-zero if this CU is from .debug_types.
638 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
640 unsigned int is_debug_types : 1;
642 /* Non-zero if this CU is from the .dwz file. */
643 unsigned int is_dwz : 1;
645 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
646 This flag is only valid if is_debug_types is true.
647 We can't read a CU directly from a DWO file: There are required
648 attributes in the stub. */
649 unsigned int reading_dwo_directly : 1;
651 /* Non-zero if the TU has been read.
652 This is used to assist the "Stay in DWO Optimization" for Fission:
653 When reading a DWO, it's faster to read TUs from the DWO instead of
654 fetching them from random other DWOs (due to comdat folding).
655 If the TU has already been read, the optimization is unnecessary
656 (and unwise - we don't want to change where gdb thinks the TU lives
658 This flag is only valid if is_debug_types is true. */
659 unsigned int tu_read : 1;
661 /* The section this CU/TU lives in.
662 If the DIE refers to a DWO file, this is always the original die,
664 struct dwarf2_section_info *section;
666 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
667 of the CU cache it gets reset to NULL again. This is left as NULL for
668 dummy CUs (a CU header, but nothing else). */
669 struct dwarf2_cu *cu;
671 /* The corresponding objfile.
672 Normally we can get the objfile from dwarf2_per_objfile.
673 However we can enter this file with just a "per_cu" handle. */
674 struct objfile *objfile;
676 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
677 is active. Otherwise, the 'psymtab' field is active. */
680 /* The partial symbol table associated with this compilation unit,
681 or NULL for unread partial units. */
682 struct partial_symtab *psymtab;
684 /* Data needed by the "quick" functions. */
685 struct dwarf2_per_cu_quick_data *quick;
688 /* The CUs we import using DW_TAG_imported_unit. This is filled in
689 while reading psymtabs, used to compute the psymtab dependencies,
690 and then cleared. Then it is filled in again while reading full
691 symbols, and only deleted when the objfile is destroyed.
693 This is also used to work around a difference between the way gold
694 generates .gdb_index version <=7 and the way gdb does. Arguably this
695 is a gold bug. For symbols coming from TUs, gold records in the index
696 the CU that includes the TU instead of the TU itself. This breaks
697 dw2_lookup_symbol: It assumes that if the index says symbol X lives
698 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
699 will find X. Alas TUs live in their own symtab, so after expanding CU Y
700 we need to look in TU Z to find X. Fortunately, this is akin to
701 DW_TAG_imported_unit, so we just use the same mechanism: For
702 .gdb_index version <=7 this also records the TUs that the CU referred
703 to. Concurrently with this change gdb was modified to emit version 8
704 indices so we only pay a price for gold generated indices.
705 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
706 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
709 /* Entry in the signatured_types hash table. */
711 struct signatured_type
713 /* The "per_cu" object of this type.
714 This struct is used iff per_cu.is_debug_types.
715 N.B.: This is the first member so that it's easy to convert pointers
717 struct dwarf2_per_cu_data per_cu;
719 /* The type's signature. */
722 /* Offset in the TU of the type's DIE, as read from the TU header.
723 If this TU is a DWO stub and the definition lives in a DWO file
724 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
725 cu_offset type_offset_in_tu;
727 /* Offset in the section of the type's DIE.
728 If the definition lives in a DWO file, this is the offset in the
729 .debug_types.dwo section.
730 The value is zero until the actual value is known.
731 Zero is otherwise not a valid section offset. */
732 sect_offset type_offset_in_section;
734 /* Type units are grouped by their DW_AT_stmt_list entry so that they
735 can share them. This points to the containing symtab. */
736 struct type_unit_group *type_unit_group;
739 The first time we encounter this type we fully read it in and install it
740 in the symbol tables. Subsequent times we only need the type. */
743 /* Containing DWO unit.
744 This field is valid iff per_cu.reading_dwo_directly. */
745 struct dwo_unit *dwo_unit;
748 typedef struct signatured_type *sig_type_ptr;
749 DEF_VEC_P (sig_type_ptr);
751 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
752 This includes type_unit_group and quick_file_names. */
754 struct stmt_list_hash
756 /* The DWO unit this table is from or NULL if there is none. */
757 struct dwo_unit *dwo_unit;
759 /* Offset in .debug_line or .debug_line.dwo. */
760 sect_offset line_sect_off;
763 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
764 an object of this type. */
766 struct type_unit_group
768 /* dwarf2read.c's main "handle" on a TU symtab.
769 To simplify things we create an artificial CU that "includes" all the
770 type units using this stmt_list so that the rest of the code still has
771 a "per_cu" handle on the symtab.
772 This PER_CU is recognized by having no section. */
773 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
774 struct dwarf2_per_cu_data per_cu;
776 /* The TUs that share this DW_AT_stmt_list entry.
777 This is added to while parsing type units to build partial symtabs,
778 and is deleted afterwards and not used again. */
779 VEC (sig_type_ptr) *tus;
781 /* The compunit symtab.
782 Type units in a group needn't all be defined in the same source file,
783 so we create an essentially anonymous symtab as the compunit symtab. */
784 struct compunit_symtab *compunit_symtab;
786 /* The data used to construct the hash key. */
787 struct stmt_list_hash hash;
789 /* The number of symtabs from the line header.
790 The value here must match line_header.num_file_names. */
791 unsigned int num_symtabs;
793 /* The symbol tables for this TU (obtained from the files listed in
795 WARNING: The order of entries here must match the order of entries
796 in the line header. After the first TU using this type_unit_group, the
797 line header for the subsequent TUs is recreated from this. This is done
798 because we need to use the same symtabs for each TU using the same
799 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
800 there's no guarantee the line header doesn't have duplicate entries. */
801 struct symtab **symtabs;
804 /* These sections are what may appear in a (real or virtual) DWO file. */
808 struct dwarf2_section_info abbrev;
809 struct dwarf2_section_info line;
810 struct dwarf2_section_info loc;
811 struct dwarf2_section_info loclists;
812 struct dwarf2_section_info macinfo;
813 struct dwarf2_section_info macro;
814 struct dwarf2_section_info str;
815 struct dwarf2_section_info str_offsets;
816 /* In the case of a virtual DWO file, these two are unused. */
817 struct dwarf2_section_info info;
818 VEC (dwarf2_section_info_def) *types;
821 /* CUs/TUs in DWP/DWO files. */
825 /* Backlink to the containing struct dwo_file. */
826 struct dwo_file *dwo_file;
828 /* The "id" that distinguishes this CU/TU.
829 .debug_info calls this "dwo_id", .debug_types calls this "signature".
830 Since signatures came first, we stick with it for consistency. */
833 /* The section this CU/TU lives in, in the DWO file. */
834 struct dwarf2_section_info *section;
836 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
837 sect_offset sect_off;
840 /* For types, offset in the type's DIE of the type defined by this TU. */
841 cu_offset type_offset_in_tu;
844 /* include/dwarf2.h defines the DWP section codes.
845 It defines a max value but it doesn't define a min value, which we
846 use for error checking, so provide one. */
848 enum dwp_v2_section_ids
853 /* Data for one DWO file.
855 This includes virtual DWO files (a virtual DWO file is a DWO file as it
856 appears in a DWP file). DWP files don't really have DWO files per se -
857 comdat folding of types "loses" the DWO file they came from, and from
858 a high level view DWP files appear to contain a mass of random types.
859 However, to maintain consistency with the non-DWP case we pretend DWP
860 files contain virtual DWO files, and we assign each TU with one virtual
861 DWO file (generally based on the line and abbrev section offsets -
862 a heuristic that seems to work in practice). */
866 /* The DW_AT_GNU_dwo_name attribute.
867 For virtual DWO files the name is constructed from the section offsets
868 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
869 from related CU+TUs. */
870 const char *dwo_name;
872 /* The DW_AT_comp_dir attribute. */
873 const char *comp_dir;
875 /* The bfd, when the file is open. Otherwise this is NULL.
876 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
879 /* The sections that make up this DWO file.
880 Remember that for virtual DWO files in DWP V2, these are virtual
881 sections (for lack of a better name). */
882 struct dwo_sections sections;
884 /* The CUs in the file.
885 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
886 an extension to handle LLVM's Link Time Optimization output (where
887 multiple source files may be compiled into a single object/dwo pair). */
890 /* Table of TUs in the file.
891 Each element is a struct dwo_unit. */
895 /* These sections are what may appear in a DWP file. */
899 /* These are used by both DWP version 1 and 2. */
900 struct dwarf2_section_info str;
901 struct dwarf2_section_info cu_index;
902 struct dwarf2_section_info tu_index;
904 /* These are only used by DWP version 2 files.
905 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
906 sections are referenced by section number, and are not recorded here.
907 In DWP version 2 there is at most one copy of all these sections, each
908 section being (effectively) comprised of the concatenation of all of the
909 individual sections that exist in the version 1 format.
910 To keep the code simple we treat each of these concatenated pieces as a
911 section itself (a virtual section?). */
912 struct dwarf2_section_info abbrev;
913 struct dwarf2_section_info info;
914 struct dwarf2_section_info line;
915 struct dwarf2_section_info loc;
916 struct dwarf2_section_info macinfo;
917 struct dwarf2_section_info macro;
918 struct dwarf2_section_info str_offsets;
919 struct dwarf2_section_info types;
922 /* These sections are what may appear in a virtual DWO file in DWP version 1.
923 A virtual DWO file is a DWO file as it appears in a DWP file. */
925 struct virtual_v1_dwo_sections
927 struct dwarf2_section_info abbrev;
928 struct dwarf2_section_info line;
929 struct dwarf2_section_info loc;
930 struct dwarf2_section_info macinfo;
931 struct dwarf2_section_info macro;
932 struct dwarf2_section_info str_offsets;
933 /* Each DWP hash table entry records one CU or one TU.
934 That is recorded here, and copied to dwo_unit.section. */
935 struct dwarf2_section_info info_or_types;
938 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
939 In version 2, the sections of the DWO files are concatenated together
940 and stored in one section of that name. Thus each ELF section contains
941 several "virtual" sections. */
943 struct virtual_v2_dwo_sections
945 bfd_size_type abbrev_offset;
946 bfd_size_type abbrev_size;
948 bfd_size_type line_offset;
949 bfd_size_type line_size;
951 bfd_size_type loc_offset;
952 bfd_size_type loc_size;
954 bfd_size_type macinfo_offset;
955 bfd_size_type macinfo_size;
957 bfd_size_type macro_offset;
958 bfd_size_type macro_size;
960 bfd_size_type str_offsets_offset;
961 bfd_size_type str_offsets_size;
963 /* Each DWP hash table entry records one CU or one TU.
964 That is recorded here, and copied to dwo_unit.section. */
965 bfd_size_type info_or_types_offset;
966 bfd_size_type info_or_types_size;
969 /* Contents of DWP hash tables. */
971 struct dwp_hash_table
973 uint32_t version, nr_columns;
974 uint32_t nr_units, nr_slots;
975 const gdb_byte *hash_table, *unit_table;
980 const gdb_byte *indices;
984 /* This is indexed by column number and gives the id of the section
986 #define MAX_NR_V2_DWO_SECTIONS \
987 (1 /* .debug_info or .debug_types */ \
988 + 1 /* .debug_abbrev */ \
989 + 1 /* .debug_line */ \
990 + 1 /* .debug_loc */ \
991 + 1 /* .debug_str_offsets */ \
992 + 1 /* .debug_macro or .debug_macinfo */)
993 int section_ids[MAX_NR_V2_DWO_SECTIONS];
994 const gdb_byte *offsets;
995 const gdb_byte *sizes;
1000 /* Data for one DWP file. */
1004 /* Name of the file. */
1007 /* File format version. */
1013 /* Section info for this file. */
1014 struct dwp_sections sections;
1016 /* Table of CUs in the file. */
1017 const struct dwp_hash_table *cus;
1019 /* Table of TUs in the file. */
1020 const struct dwp_hash_table *tus;
1022 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1026 /* Table to map ELF section numbers to their sections.
1027 This is only needed for the DWP V1 file format. */
1028 unsigned int num_sections;
1029 asection **elf_sections;
1032 /* This represents a '.dwz' file. */
1036 /* A dwz file can only contain a few sections. */
1037 struct dwarf2_section_info abbrev;
1038 struct dwarf2_section_info info;
1039 struct dwarf2_section_info str;
1040 struct dwarf2_section_info line;
1041 struct dwarf2_section_info macro;
1042 struct dwarf2_section_info gdb_index;
1044 /* The dwz's BFD. */
1048 /* Struct used to pass misc. parameters to read_die_and_children, et
1049 al. which are used for both .debug_info and .debug_types dies.
1050 All parameters here are unchanging for the life of the call. This
1051 struct exists to abstract away the constant parameters of die reading. */
1053 struct die_reader_specs
1055 /* The bfd of die_section. */
1058 /* The CU of the DIE we are parsing. */
1059 struct dwarf2_cu *cu;
1061 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1062 struct dwo_file *dwo_file;
1064 /* The section the die comes from.
1065 This is either .debug_info or .debug_types, or the .dwo variants. */
1066 struct dwarf2_section_info *die_section;
1068 /* die_section->buffer. */
1069 const gdb_byte *buffer;
1071 /* The end of the buffer. */
1072 const gdb_byte *buffer_end;
1074 /* The value of the DW_AT_comp_dir attribute. */
1075 const char *comp_dir;
1078 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1079 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1080 const gdb_byte *info_ptr,
1081 struct die_info *comp_unit_die,
1085 /* A 1-based directory index. This is a strong typedef to prevent
1086 accidentally using a directory index as a 0-based index into an
1088 enum class dir_index : unsigned int {};
1090 /* Likewise, a 1-based file name index. */
1091 enum class file_name_index : unsigned int {};
1095 file_entry () = default;
1097 file_entry (const char *name_, dir_index d_index_,
1098 unsigned int mod_time_, unsigned int length_)
1101 mod_time (mod_time_),
1105 /* Return the include directory at D_INDEX stored in LH. Returns
1106 NULL if D_INDEX is out of bounds. */
1107 const char *include_dir (const line_header *lh) const;
1109 /* The file name. Note this is an observing pointer. The memory is
1110 owned by debug_line_buffer. */
1111 const char *name {};
1113 /* The directory index (1-based). */
1114 dir_index d_index {};
1116 unsigned int mod_time {};
1118 unsigned int length {};
1120 /* True if referenced by the Line Number Program. */
1123 /* The associated symbol table, if any. */
1124 struct symtab *symtab {};
1127 /* The line number information for a compilation unit (found in the
1128 .debug_line section) begins with a "statement program header",
1129 which contains the following information. */
1136 /* Add an entry to the include directory table. */
1137 void add_include_dir (const char *include_dir);
1139 /* Add an entry to the file name table. */
1140 void add_file_name (const char *name, dir_index d_index,
1141 unsigned int mod_time, unsigned int length);
1143 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1144 is out of bounds. */
1145 const char *include_dir_at (dir_index index) const
1147 /* Convert directory index number (1-based) to vector index
1149 size_t vec_index = to_underlying (index) - 1;
1151 if (vec_index >= include_dirs.size ())
1153 return include_dirs[vec_index];
1156 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1157 is out of bounds. */
1158 file_entry *file_name_at (file_name_index index)
1160 /* Convert file name index number (1-based) to vector index
1162 size_t vec_index = to_underlying (index) - 1;
1164 if (vec_index >= file_names.size ())
1166 return &file_names[vec_index];
1169 /* Const version of the above. */
1170 const file_entry *file_name_at (unsigned int index) const
1172 if (index >= file_names.size ())
1174 return &file_names[index];
1177 /* Offset of line number information in .debug_line section. */
1178 sect_offset sect_off {};
1180 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1181 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1183 unsigned int total_length {};
1184 unsigned short version {};
1185 unsigned int header_length {};
1186 unsigned char minimum_instruction_length {};
1187 unsigned char maximum_ops_per_instruction {};
1188 unsigned char default_is_stmt {};
1190 unsigned char line_range {};
1191 unsigned char opcode_base {};
1193 /* standard_opcode_lengths[i] is the number of operands for the
1194 standard opcode whose value is i. This means that
1195 standard_opcode_lengths[0] is unused, and the last meaningful
1196 element is standard_opcode_lengths[opcode_base - 1]. */
1197 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1199 /* The include_directories table. Note these are observing
1200 pointers. The memory is owned by debug_line_buffer. */
1201 std::vector<const char *> include_dirs;
1203 /* The file_names table. */
1204 std::vector<file_entry> file_names;
1206 /* The start and end of the statement program following this
1207 header. These point into dwarf2_per_objfile->line_buffer. */
1208 const gdb_byte *statement_program_start {}, *statement_program_end {};
1211 typedef std::unique_ptr<line_header> line_header_up;
1214 file_entry::include_dir (const line_header *lh) const
1216 return lh->include_dir_at (d_index);
1219 /* When we construct a partial symbol table entry we only
1220 need this much information. */
1221 struct partial_die_info
1223 /* Offset of this DIE. */
1224 sect_offset sect_off;
1226 /* DWARF-2 tag for this DIE. */
1227 ENUM_BITFIELD(dwarf_tag) tag : 16;
1229 /* Assorted flags describing the data found in this DIE. */
1230 unsigned int has_children : 1;
1231 unsigned int is_external : 1;
1232 unsigned int is_declaration : 1;
1233 unsigned int has_type : 1;
1234 unsigned int has_specification : 1;
1235 unsigned int has_pc_info : 1;
1236 unsigned int may_be_inlined : 1;
1238 /* This DIE has been marked DW_AT_main_subprogram. */
1239 unsigned int main_subprogram : 1;
1241 /* Flag set if the SCOPE field of this structure has been
1243 unsigned int scope_set : 1;
1245 /* Flag set if the DIE has a byte_size attribute. */
1246 unsigned int has_byte_size : 1;
1248 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1249 unsigned int has_const_value : 1;
1251 /* Flag set if any of the DIE's children are template arguments. */
1252 unsigned int has_template_arguments : 1;
1254 /* Flag set if fixup_partial_die has been called on this die. */
1255 unsigned int fixup_called : 1;
1257 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1258 unsigned int is_dwz : 1;
1260 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1261 unsigned int spec_is_dwz : 1;
1263 /* The name of this DIE. Normally the value of DW_AT_name, but
1264 sometimes a default name for unnamed DIEs. */
1267 /* The linkage name, if present. */
1268 const char *linkage_name;
1270 /* The scope to prepend to our children. This is generally
1271 allocated on the comp_unit_obstack, so will disappear
1272 when this compilation unit leaves the cache. */
1275 /* Some data associated with the partial DIE. The tag determines
1276 which field is live. */
1279 /* The location description associated with this DIE, if any. */
1280 struct dwarf_block *locdesc;
1281 /* The offset of an import, for DW_TAG_imported_unit. */
1282 sect_offset sect_off;
1285 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1289 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1290 DW_AT_sibling, if any. */
1291 /* NOTE: This member isn't strictly necessary, read_partial_die could
1292 return DW_AT_sibling values to its caller load_partial_dies. */
1293 const gdb_byte *sibling;
1295 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1296 DW_AT_specification (or DW_AT_abstract_origin or
1297 DW_AT_extension). */
1298 sect_offset spec_offset;
1300 /* Pointers to this DIE's parent, first child, and next sibling,
1302 struct partial_die_info *die_parent, *die_child, *die_sibling;
1305 /* This data structure holds the information of an abbrev. */
1308 unsigned int number; /* number identifying abbrev */
1309 enum dwarf_tag tag; /* dwarf tag */
1310 unsigned short has_children; /* boolean */
1311 unsigned short num_attrs; /* number of attributes */
1312 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1313 struct abbrev_info *next; /* next in chain */
1318 ENUM_BITFIELD(dwarf_attribute) name : 16;
1319 ENUM_BITFIELD(dwarf_form) form : 16;
1321 /* It is valid only if FORM is DW_FORM_implicit_const. */
1322 LONGEST implicit_const;
1325 /* Size of abbrev_table.abbrev_hash_table. */
1326 #define ABBREV_HASH_SIZE 121
1328 /* Top level data structure to contain an abbreviation table. */
1332 /* Where the abbrev table came from.
1333 This is used as a sanity check when the table is used. */
1334 sect_offset sect_off;
1336 /* Storage for the abbrev table. */
1337 struct obstack abbrev_obstack;
1339 /* Hash table of abbrevs.
1340 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1341 It could be statically allocated, but the previous code didn't so we
1343 struct abbrev_info **abbrevs;
1346 /* Attributes have a name and a value. */
1349 ENUM_BITFIELD(dwarf_attribute) name : 16;
1350 ENUM_BITFIELD(dwarf_form) form : 15;
1352 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1353 field should be in u.str (existing only for DW_STRING) but it is kept
1354 here for better struct attribute alignment. */
1355 unsigned int string_is_canonical : 1;
1360 struct dwarf_block *blk;
1369 /* This data structure holds a complete die structure. */
1372 /* DWARF-2 tag for this DIE. */
1373 ENUM_BITFIELD(dwarf_tag) tag : 16;
1375 /* Number of attributes */
1376 unsigned char num_attrs;
1378 /* True if we're presently building the full type name for the
1379 type derived from this DIE. */
1380 unsigned char building_fullname : 1;
1382 /* True if this die is in process. PR 16581. */
1383 unsigned char in_process : 1;
1386 unsigned int abbrev;
1388 /* Offset in .debug_info or .debug_types section. */
1389 sect_offset sect_off;
1391 /* The dies in a compilation unit form an n-ary tree. PARENT
1392 points to this die's parent; CHILD points to the first child of
1393 this node; and all the children of a given node are chained
1394 together via their SIBLING fields. */
1395 struct die_info *child; /* Its first child, if any. */
1396 struct die_info *sibling; /* Its next sibling, if any. */
1397 struct die_info *parent; /* Its parent, if any. */
1399 /* An array of attributes, with NUM_ATTRS elements. There may be
1400 zero, but it's not common and zero-sized arrays are not
1401 sufficiently portable C. */
1402 struct attribute attrs[1];
1405 /* Get at parts of an attribute structure. */
1407 #define DW_STRING(attr) ((attr)->u.str)
1408 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1409 #define DW_UNSND(attr) ((attr)->u.unsnd)
1410 #define DW_BLOCK(attr) ((attr)->u.blk)
1411 #define DW_SND(attr) ((attr)->u.snd)
1412 #define DW_ADDR(attr) ((attr)->u.addr)
1413 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1415 /* Blocks are a bunch of untyped bytes. */
1420 /* Valid only if SIZE is not zero. */
1421 const gdb_byte *data;
1424 #ifndef ATTR_ALLOC_CHUNK
1425 #define ATTR_ALLOC_CHUNK 4
1428 /* Allocate fields for structs, unions and enums in this size. */
1429 #ifndef DW_FIELD_ALLOC_CHUNK
1430 #define DW_FIELD_ALLOC_CHUNK 4
1433 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1434 but this would require a corresponding change in unpack_field_as_long
1436 static int bits_per_byte = 8;
1440 struct nextfield *next;
1448 struct nextfnfield *next;
1449 struct fn_field fnfield;
1456 struct nextfnfield *head;
1459 struct typedef_field_list
1461 struct typedef_field field;
1462 struct typedef_field_list *next;
1465 /* The routines that read and process dies for a C struct or C++ class
1466 pass lists of data member fields and lists of member function fields
1467 in an instance of a field_info structure, as defined below. */
1470 /* List of data member and baseclasses fields. */
1471 struct nextfield *fields, *baseclasses;
1473 /* Number of fields (including baseclasses). */
1476 /* Number of baseclasses. */
1479 /* Set if the accesibility of one of the fields is not public. */
1480 int non_public_fields;
1482 /* Member function fields array, entries are allocated in the order they
1483 are encountered in the object file. */
1484 struct nextfnfield *fnfields;
1486 /* Member function fieldlist array, contains name of possibly overloaded
1487 member function, number of overloaded member functions and a pointer
1488 to the head of the member function field chain. */
1489 struct fnfieldlist *fnfieldlists;
1491 /* Number of entries in the fnfieldlists array. */
1494 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1495 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1496 struct typedef_field_list *typedef_field_list;
1497 unsigned typedef_field_list_count;
1500 /* One item on the queue of compilation units to read in full symbols
1502 struct dwarf2_queue_item
1504 struct dwarf2_per_cu_data *per_cu;
1505 enum language pretend_language;
1506 struct dwarf2_queue_item *next;
1509 /* The current queue. */
1510 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1512 /* Loaded secondary compilation units are kept in memory until they
1513 have not been referenced for the processing of this many
1514 compilation units. Set this to zero to disable caching. Cache
1515 sizes of up to at least twenty will improve startup time for
1516 typical inter-CU-reference binaries, at an obvious memory cost. */
1517 static int dwarf_max_cache_age = 5;
1519 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1520 struct cmd_list_element *c, const char *value)
1522 fprintf_filtered (file, _("The upper bound on the age of cached "
1523 "DWARF compilation units is %s.\n"),
1527 /* local function prototypes */
1529 static const char *get_section_name (const struct dwarf2_section_info *);
1531 static const char *get_section_file_name (const struct dwarf2_section_info *);
1533 static void dwarf2_find_base_address (struct die_info *die,
1534 struct dwarf2_cu *cu);
1536 static struct partial_symtab *create_partial_symtab
1537 (struct dwarf2_per_cu_data *per_cu, const char *name);
1539 static void dwarf2_build_psymtabs_hard (struct objfile *);
1541 static void scan_partial_symbols (struct partial_die_info *,
1542 CORE_ADDR *, CORE_ADDR *,
1543 int, struct dwarf2_cu *);
1545 static void add_partial_symbol (struct partial_die_info *,
1546 struct dwarf2_cu *);
1548 static void add_partial_namespace (struct partial_die_info *pdi,
1549 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1550 int set_addrmap, struct dwarf2_cu *cu);
1552 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1553 CORE_ADDR *highpc, int set_addrmap,
1554 struct dwarf2_cu *cu);
1556 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1557 struct dwarf2_cu *cu);
1559 static void add_partial_subprogram (struct partial_die_info *pdi,
1560 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1561 int need_pc, struct dwarf2_cu *cu);
1563 static void dwarf2_read_symtab (struct partial_symtab *,
1566 static void psymtab_to_symtab_1 (struct partial_symtab *);
1568 static struct abbrev_info *abbrev_table_lookup_abbrev
1569 (const struct abbrev_table *, unsigned int);
1571 static struct abbrev_table *abbrev_table_read_table
1572 (struct dwarf2_section_info *, sect_offset);
1574 static void abbrev_table_free (struct abbrev_table *);
1576 static void abbrev_table_free_cleanup (void *);
1578 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1579 struct dwarf2_section_info *);
1581 static void dwarf2_free_abbrev_table (void *);
1583 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1585 static struct partial_die_info *load_partial_dies
1586 (const struct die_reader_specs *, const gdb_byte *, int);
1588 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1589 struct partial_die_info *,
1590 struct abbrev_info *,
1594 static struct partial_die_info *find_partial_die (sect_offset, int,
1595 struct dwarf2_cu *);
1597 static void fixup_partial_die (struct partial_die_info *,
1598 struct dwarf2_cu *);
1600 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1601 struct attribute *, struct attr_abbrev *,
1604 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1606 static int read_1_signed_byte (bfd *, const gdb_byte *);
1608 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1610 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1612 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1614 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1617 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1619 static LONGEST read_checked_initial_length_and_offset
1620 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1621 unsigned int *, unsigned int *);
1623 static LONGEST read_offset (bfd *, const gdb_byte *,
1624 const struct comp_unit_head *,
1627 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1629 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1632 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1634 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1636 static const char *read_indirect_string (bfd *, const gdb_byte *,
1637 const struct comp_unit_head *,
1640 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1641 const struct comp_unit_head *,
1644 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1646 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1648 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1652 static const char *read_str_index (const struct die_reader_specs *reader,
1653 ULONGEST str_index);
1655 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1657 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1658 struct dwarf2_cu *);
1660 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1663 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1664 struct dwarf2_cu *cu);
1666 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1667 struct dwarf2_cu *cu);
1669 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1671 static struct die_info *die_specification (struct die_info *die,
1672 struct dwarf2_cu **);
1674 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1675 struct dwarf2_cu *cu);
1677 static void dwarf_decode_lines (struct line_header *, const char *,
1678 struct dwarf2_cu *, struct partial_symtab *,
1679 CORE_ADDR, int decode_mapping);
1681 static void dwarf2_start_subfile (const char *, const char *);
1683 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1684 const char *, const char *,
1687 static struct symbol *new_symbol (struct die_info *, struct type *,
1688 struct dwarf2_cu *);
1690 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1691 struct dwarf2_cu *, struct symbol *);
1693 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1694 struct dwarf2_cu *);
1696 static void dwarf2_const_value_attr (const struct attribute *attr,
1699 struct obstack *obstack,
1700 struct dwarf2_cu *cu, LONGEST *value,
1701 const gdb_byte **bytes,
1702 struct dwarf2_locexpr_baton **baton);
1704 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1706 static int need_gnat_info (struct dwarf2_cu *);
1708 static struct type *die_descriptive_type (struct die_info *,
1709 struct dwarf2_cu *);
1711 static void set_descriptive_type (struct type *, struct die_info *,
1712 struct dwarf2_cu *);
1714 static struct type *die_containing_type (struct die_info *,
1715 struct dwarf2_cu *);
1717 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1718 struct dwarf2_cu *);
1720 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1722 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1724 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1726 static char *typename_concat (struct obstack *obs, const char *prefix,
1727 const char *suffix, int physname,
1728 struct dwarf2_cu *cu);
1730 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1732 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1734 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1736 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1738 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1740 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1741 struct dwarf2_cu *, struct partial_symtab *);
1743 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1744 values. Keep the items ordered with increasing constraints compliance. */
1747 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1748 PC_BOUNDS_NOT_PRESENT,
1750 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1751 were present but they do not form a valid range of PC addresses. */
1754 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1757 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1761 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1762 CORE_ADDR *, CORE_ADDR *,
1764 struct partial_symtab *);
1766 static void get_scope_pc_bounds (struct die_info *,
1767 CORE_ADDR *, CORE_ADDR *,
1768 struct dwarf2_cu *);
1770 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1771 CORE_ADDR, struct dwarf2_cu *);
1773 static void dwarf2_add_field (struct field_info *, struct die_info *,
1774 struct dwarf2_cu *);
1776 static void dwarf2_attach_fields_to_type (struct field_info *,
1777 struct type *, struct dwarf2_cu *);
1779 static void dwarf2_add_member_fn (struct field_info *,
1780 struct die_info *, struct type *,
1781 struct dwarf2_cu *);
1783 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1785 struct dwarf2_cu *);
1787 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1789 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1791 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1793 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1795 static struct using_direct **using_directives (enum language);
1797 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1799 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1801 static struct type *read_module_type (struct die_info *die,
1802 struct dwarf2_cu *cu);
1804 static const char *namespace_name (struct die_info *die,
1805 int *is_anonymous, struct dwarf2_cu *);
1807 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1809 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1811 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1812 struct dwarf2_cu *);
1814 static struct die_info *read_die_and_siblings_1
1815 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1818 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1819 const gdb_byte *info_ptr,
1820 const gdb_byte **new_info_ptr,
1821 struct die_info *parent);
1823 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1824 struct die_info **, const gdb_byte *,
1827 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1828 struct die_info **, const gdb_byte *,
1831 static void process_die (struct die_info *, struct dwarf2_cu *);
1833 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1836 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1838 static const char *dwarf2_full_name (const char *name,
1839 struct die_info *die,
1840 struct dwarf2_cu *cu);
1842 static const char *dwarf2_physname (const char *name, struct die_info *die,
1843 struct dwarf2_cu *cu);
1845 static struct die_info *dwarf2_extension (struct die_info *die,
1846 struct dwarf2_cu **);
1848 static const char *dwarf_tag_name (unsigned int);
1850 static const char *dwarf_attr_name (unsigned int);
1852 static const char *dwarf_form_name (unsigned int);
1854 static const char *dwarf_bool_name (unsigned int);
1856 static const char *dwarf_type_encoding_name (unsigned int);
1858 static struct die_info *sibling_die (struct die_info *);
1860 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1862 static void dump_die_for_error (struct die_info *);
1864 static void dump_die_1 (struct ui_file *, int level, int max_level,
1867 /*static*/ void dump_die (struct die_info *, int max_level);
1869 static void store_in_ref_table (struct die_info *,
1870 struct dwarf2_cu *);
1872 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1874 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1876 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1877 const struct attribute *,
1878 struct dwarf2_cu **);
1880 static struct die_info *follow_die_ref (struct die_info *,
1881 const struct attribute *,
1882 struct dwarf2_cu **);
1884 static struct die_info *follow_die_sig (struct die_info *,
1885 const struct attribute *,
1886 struct dwarf2_cu **);
1888 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1889 struct dwarf2_cu *);
1891 static struct type *get_DW_AT_signature_type (struct die_info *,
1892 const struct attribute *,
1893 struct dwarf2_cu *);
1895 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1897 static void read_signatured_type (struct signatured_type *);
1899 static int attr_to_dynamic_prop (const struct attribute *attr,
1900 struct die_info *die, struct dwarf2_cu *cu,
1901 struct dynamic_prop *prop);
1903 /* memory allocation interface */
1905 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1907 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1909 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1911 static int attr_form_is_block (const struct attribute *);
1913 static int attr_form_is_section_offset (const struct attribute *);
1915 static int attr_form_is_constant (const struct attribute *);
1917 static int attr_form_is_ref (const struct attribute *);
1919 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1920 struct dwarf2_loclist_baton *baton,
1921 const struct attribute *attr);
1923 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1925 struct dwarf2_cu *cu,
1928 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1929 const gdb_byte *info_ptr,
1930 struct abbrev_info *abbrev);
1932 static void free_stack_comp_unit (void *);
1934 static hashval_t partial_die_hash (const void *item);
1936 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1938 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1939 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
1941 static void init_one_comp_unit (struct dwarf2_cu *cu,
1942 struct dwarf2_per_cu_data *per_cu);
1944 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1945 struct die_info *comp_unit_die,
1946 enum language pretend_language);
1948 static void free_heap_comp_unit (void *);
1950 static void free_cached_comp_units (void *);
1952 static void age_cached_comp_units (void);
1954 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1956 static struct type *set_die_type (struct die_info *, struct type *,
1957 struct dwarf2_cu *);
1959 static void create_all_comp_units (struct objfile *);
1961 static int create_all_type_units (struct objfile *);
1963 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1966 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1969 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1972 static void dwarf2_add_dependence (struct dwarf2_cu *,
1973 struct dwarf2_per_cu_data *);
1975 static void dwarf2_mark (struct dwarf2_cu *);
1977 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1979 static struct type *get_die_type_at_offset (sect_offset,
1980 struct dwarf2_per_cu_data *);
1982 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1984 static void dwarf2_release_queue (void *dummy);
1986 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1987 enum language pretend_language);
1989 static void process_queue (void);
1991 /* The return type of find_file_and_directory. Note, the enclosed
1992 string pointers are only valid while this object is valid. */
1994 struct file_and_directory
1996 /* The filename. This is never NULL. */
1999 /* The compilation directory. NULL if not known. If we needed to
2000 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2001 points directly to the DW_AT_comp_dir string attribute owned by
2002 the obstack that owns the DIE. */
2003 const char *comp_dir;
2005 /* If we needed to build a new string for comp_dir, this is what
2006 owns the storage. */
2007 std::string comp_dir_storage;
2010 static file_and_directory find_file_and_directory (struct die_info *die,
2011 struct dwarf2_cu *cu);
2013 static char *file_full_name (int file, struct line_header *lh,
2014 const char *comp_dir);
2016 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2017 enum class rcuh_kind { COMPILE, TYPE };
2019 static const gdb_byte *read_and_check_comp_unit_head
2020 (struct comp_unit_head *header,
2021 struct dwarf2_section_info *section,
2022 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2023 rcuh_kind section_kind);
2025 static void init_cutu_and_read_dies
2026 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2027 int use_existing_cu, int keep,
2028 die_reader_func_ftype *die_reader_func, void *data);
2030 static void init_cutu_and_read_dies_simple
2031 (struct dwarf2_per_cu_data *this_cu,
2032 die_reader_func_ftype *die_reader_func, void *data);
2034 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2036 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2038 static struct dwo_unit *lookup_dwo_unit_in_dwp
2039 (struct dwp_file *dwp_file, const char *comp_dir,
2040 ULONGEST signature, int is_debug_types);
2042 static struct dwp_file *get_dwp_file (void);
2044 static struct dwo_unit *lookup_dwo_comp_unit
2045 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2047 static struct dwo_unit *lookup_dwo_type_unit
2048 (struct signatured_type *, const char *, const char *);
2050 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2052 static void free_dwo_file_cleanup (void *);
2054 static void process_cu_includes (void);
2056 static void check_producer (struct dwarf2_cu *cu);
2058 static void free_line_header_voidp (void *arg);
2060 /* Various complaints about symbol reading that don't abort the process. */
2063 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2065 complaint (&symfile_complaints,
2066 _("statement list doesn't fit in .debug_line section"));
2070 dwarf2_debug_line_missing_file_complaint (void)
2072 complaint (&symfile_complaints,
2073 _(".debug_line section has line data without a file"));
2077 dwarf2_debug_line_missing_end_sequence_complaint (void)
2079 complaint (&symfile_complaints,
2080 _(".debug_line section has line "
2081 "program sequence without an end"));
2085 dwarf2_complex_location_expr_complaint (void)
2087 complaint (&symfile_complaints, _("location expression too complex"));
2091 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2094 complaint (&symfile_complaints,
2095 _("const value length mismatch for '%s', got %d, expected %d"),
2100 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2102 complaint (&symfile_complaints,
2103 _("debug info runs off end of %s section"
2105 get_section_name (section),
2106 get_section_file_name (section));
2110 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2112 complaint (&symfile_complaints,
2113 _("macro debug info contains a "
2114 "malformed macro definition:\n`%s'"),
2119 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2121 complaint (&symfile_complaints,
2122 _("invalid attribute class or form for '%s' in '%s'"),
2126 /* Hash function for line_header_hash. */
2129 line_header_hash (const struct line_header *ofs)
2131 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2134 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2137 line_header_hash_voidp (const void *item)
2139 const struct line_header *ofs = (const struct line_header *) item;
2141 return line_header_hash (ofs);
2144 /* Equality function for line_header_hash. */
2147 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2149 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2150 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2152 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2153 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2159 /* Convert VALUE between big- and little-endian. */
2161 byte_swap (offset_type value)
2165 result = (value & 0xff) << 24;
2166 result |= (value & 0xff00) << 8;
2167 result |= (value & 0xff0000) >> 8;
2168 result |= (value & 0xff000000) >> 24;
2172 #define MAYBE_SWAP(V) byte_swap (V)
2175 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
2176 #endif /* WORDS_BIGENDIAN */
2178 /* Read the given attribute value as an address, taking the attribute's
2179 form into account. */
2182 attr_value_as_address (struct attribute *attr)
2186 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2188 /* Aside from a few clearly defined exceptions, attributes that
2189 contain an address must always be in DW_FORM_addr form.
2190 Unfortunately, some compilers happen to be violating this
2191 requirement by encoding addresses using other forms, such
2192 as DW_FORM_data4 for example. For those broken compilers,
2193 we try to do our best, without any guarantee of success,
2194 to interpret the address correctly. It would also be nice
2195 to generate a complaint, but that would require us to maintain
2196 a list of legitimate cases where a non-address form is allowed,
2197 as well as update callers to pass in at least the CU's DWARF
2198 version. This is more overhead than what we're willing to
2199 expand for a pretty rare case. */
2200 addr = DW_UNSND (attr);
2203 addr = DW_ADDR (attr);
2208 /* The suffix for an index file. */
2209 #define INDEX_SUFFIX ".gdb-index"
2211 /* See declaration. */
2213 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2214 const dwarf2_debug_sections *names)
2215 : objfile (objfile_)
2218 names = &dwarf2_elf_names;
2220 bfd *obfd = objfile->obfd;
2222 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2223 locate_sections (obfd, sec, *names);
2226 dwarf2_per_objfile::~dwarf2_per_objfile ()
2228 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2229 free_cached_comp_units ();
2231 if (quick_file_names_table)
2232 htab_delete (quick_file_names_table);
2234 if (line_header_hash)
2235 htab_delete (line_header_hash);
2237 /* Everything else should be on the objfile obstack. */
2240 /* See declaration. */
2243 dwarf2_per_objfile::free_cached_comp_units ()
2245 dwarf2_per_cu_data *per_cu = read_in_chain;
2246 dwarf2_per_cu_data **last_chain = &read_in_chain;
2247 while (per_cu != NULL)
2249 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2251 free_heap_comp_unit (per_cu->cu);
2252 *last_chain = next_cu;
2257 /* Try to locate the sections we need for DWARF 2 debugging
2258 information and return true if we have enough to do something.
2259 NAMES points to the dwarf2 section names, or is NULL if the standard
2260 ELF names are used. */
2263 dwarf2_has_info (struct objfile *objfile,
2264 const struct dwarf2_debug_sections *names)
2266 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2267 objfile_data (objfile, dwarf2_objfile_data_key));
2268 if (!dwarf2_per_objfile)
2270 /* Initialize per-objfile state. */
2271 struct dwarf2_per_objfile *data
2272 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2274 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2275 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2277 return (!dwarf2_per_objfile->info.is_virtual
2278 && dwarf2_per_objfile->info.s.section != NULL
2279 && !dwarf2_per_objfile->abbrev.is_virtual
2280 && dwarf2_per_objfile->abbrev.s.section != NULL);
2283 /* Return the containing section of virtual section SECTION. */
2285 static struct dwarf2_section_info *
2286 get_containing_section (const struct dwarf2_section_info *section)
2288 gdb_assert (section->is_virtual);
2289 return section->s.containing_section;
2292 /* Return the bfd owner of SECTION. */
2295 get_section_bfd_owner (const struct dwarf2_section_info *section)
2297 if (section->is_virtual)
2299 section = get_containing_section (section);
2300 gdb_assert (!section->is_virtual);
2302 return section->s.section->owner;
2305 /* Return the bfd section of SECTION.
2306 Returns NULL if the section is not present. */
2309 get_section_bfd_section (const struct dwarf2_section_info *section)
2311 if (section->is_virtual)
2313 section = get_containing_section (section);
2314 gdb_assert (!section->is_virtual);
2316 return section->s.section;
2319 /* Return the name of SECTION. */
2322 get_section_name (const struct dwarf2_section_info *section)
2324 asection *sectp = get_section_bfd_section (section);
2326 gdb_assert (sectp != NULL);
2327 return bfd_section_name (get_section_bfd_owner (section), sectp);
2330 /* Return the name of the file SECTION is in. */
2333 get_section_file_name (const struct dwarf2_section_info *section)
2335 bfd *abfd = get_section_bfd_owner (section);
2337 return bfd_get_filename (abfd);
2340 /* Return the id of SECTION.
2341 Returns 0 if SECTION doesn't exist. */
2344 get_section_id (const struct dwarf2_section_info *section)
2346 asection *sectp = get_section_bfd_section (section);
2353 /* Return the flags of SECTION.
2354 SECTION (or containing section if this is a virtual section) must exist. */
2357 get_section_flags (const struct dwarf2_section_info *section)
2359 asection *sectp = get_section_bfd_section (section);
2361 gdb_assert (sectp != NULL);
2362 return bfd_get_section_flags (sectp->owner, sectp);
2365 /* When loading sections, we look either for uncompressed section or for
2366 compressed section names. */
2369 section_is_p (const char *section_name,
2370 const struct dwarf2_section_names *names)
2372 if (names->normal != NULL
2373 && strcmp (section_name, names->normal) == 0)
2375 if (names->compressed != NULL
2376 && strcmp (section_name, names->compressed) == 0)
2381 /* See declaration. */
2384 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2385 const dwarf2_debug_sections &names)
2387 flagword aflag = bfd_get_section_flags (abfd, sectp);
2389 if ((aflag & SEC_HAS_CONTENTS) == 0)
2392 else if (section_is_p (sectp->name, &names.info))
2394 this->info.s.section = sectp;
2395 this->info.size = bfd_get_section_size (sectp);
2397 else if (section_is_p (sectp->name, &names.abbrev))
2399 this->abbrev.s.section = sectp;
2400 this->abbrev.size = bfd_get_section_size (sectp);
2402 else if (section_is_p (sectp->name, &names.line))
2404 this->line.s.section = sectp;
2405 this->line.size = bfd_get_section_size (sectp);
2407 else if (section_is_p (sectp->name, &names.loc))
2409 this->loc.s.section = sectp;
2410 this->loc.size = bfd_get_section_size (sectp);
2412 else if (section_is_p (sectp->name, &names.loclists))
2414 this->loclists.s.section = sectp;
2415 this->loclists.size = bfd_get_section_size (sectp);
2417 else if (section_is_p (sectp->name, &names.macinfo))
2419 this->macinfo.s.section = sectp;
2420 this->macinfo.size = bfd_get_section_size (sectp);
2422 else if (section_is_p (sectp->name, &names.macro))
2424 this->macro.s.section = sectp;
2425 this->macro.size = bfd_get_section_size (sectp);
2427 else if (section_is_p (sectp->name, &names.str))
2429 this->str.s.section = sectp;
2430 this->str.size = bfd_get_section_size (sectp);
2432 else if (section_is_p (sectp->name, &names.line_str))
2434 this->line_str.s.section = sectp;
2435 this->line_str.size = bfd_get_section_size (sectp);
2437 else if (section_is_p (sectp->name, &names.addr))
2439 this->addr.s.section = sectp;
2440 this->addr.size = bfd_get_section_size (sectp);
2442 else if (section_is_p (sectp->name, &names.frame))
2444 this->frame.s.section = sectp;
2445 this->frame.size = bfd_get_section_size (sectp);
2447 else if (section_is_p (sectp->name, &names.eh_frame))
2449 this->eh_frame.s.section = sectp;
2450 this->eh_frame.size = bfd_get_section_size (sectp);
2452 else if (section_is_p (sectp->name, &names.ranges))
2454 this->ranges.s.section = sectp;
2455 this->ranges.size = bfd_get_section_size (sectp);
2457 else if (section_is_p (sectp->name, &names.rnglists))
2459 this->rnglists.s.section = sectp;
2460 this->rnglists.size = bfd_get_section_size (sectp);
2462 else if (section_is_p (sectp->name, &names.types))
2464 struct dwarf2_section_info type_section;
2466 memset (&type_section, 0, sizeof (type_section));
2467 type_section.s.section = sectp;
2468 type_section.size = bfd_get_section_size (sectp);
2470 VEC_safe_push (dwarf2_section_info_def, this->types,
2473 else if (section_is_p (sectp->name, &names.gdb_index))
2475 this->gdb_index.s.section = sectp;
2476 this->gdb_index.size = bfd_get_section_size (sectp);
2479 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2480 && bfd_section_vma (abfd, sectp) == 0)
2481 this->has_section_at_zero = true;
2484 /* A helper function that decides whether a section is empty,
2488 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2490 if (section->is_virtual)
2491 return section->size == 0;
2492 return section->s.section == NULL || section->size == 0;
2495 /* Read the contents of the section INFO.
2496 OBJFILE is the main object file, but not necessarily the file where
2497 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2499 If the section is compressed, uncompress it before returning. */
2502 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2506 gdb_byte *buf, *retbuf;
2510 info->buffer = NULL;
2513 if (dwarf2_section_empty_p (info))
2516 sectp = get_section_bfd_section (info);
2518 /* If this is a virtual section we need to read in the real one first. */
2519 if (info->is_virtual)
2521 struct dwarf2_section_info *containing_section =
2522 get_containing_section (info);
2524 gdb_assert (sectp != NULL);
2525 if ((sectp->flags & SEC_RELOC) != 0)
2527 error (_("Dwarf Error: DWP format V2 with relocations is not"
2528 " supported in section %s [in module %s]"),
2529 get_section_name (info), get_section_file_name (info));
2531 dwarf2_read_section (objfile, containing_section);
2532 /* Other code should have already caught virtual sections that don't
2534 gdb_assert (info->virtual_offset + info->size
2535 <= containing_section->size);
2536 /* If the real section is empty or there was a problem reading the
2537 section we shouldn't get here. */
2538 gdb_assert (containing_section->buffer != NULL);
2539 info->buffer = containing_section->buffer + info->virtual_offset;
2543 /* If the section has relocations, we must read it ourselves.
2544 Otherwise we attach it to the BFD. */
2545 if ((sectp->flags & SEC_RELOC) == 0)
2547 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2551 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2554 /* When debugging .o files, we may need to apply relocations; see
2555 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2556 We never compress sections in .o files, so we only need to
2557 try this when the section is not compressed. */
2558 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2561 info->buffer = retbuf;
2565 abfd = get_section_bfd_owner (info);
2566 gdb_assert (abfd != NULL);
2568 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2569 || bfd_bread (buf, info->size, abfd) != info->size)
2571 error (_("Dwarf Error: Can't read DWARF data"
2572 " in section %s [in module %s]"),
2573 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2577 /* A helper function that returns the size of a section in a safe way.
2578 If you are positive that the section has been read before using the
2579 size, then it is safe to refer to the dwarf2_section_info object's
2580 "size" field directly. In other cases, you must call this
2581 function, because for compressed sections the size field is not set
2582 correctly until the section has been read. */
2584 static bfd_size_type
2585 dwarf2_section_size (struct objfile *objfile,
2586 struct dwarf2_section_info *info)
2589 dwarf2_read_section (objfile, info);
2593 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2597 dwarf2_get_section_info (struct objfile *objfile,
2598 enum dwarf2_section_enum sect,
2599 asection **sectp, const gdb_byte **bufp,
2600 bfd_size_type *sizep)
2602 struct dwarf2_per_objfile *data
2603 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2604 dwarf2_objfile_data_key);
2605 struct dwarf2_section_info *info;
2607 /* We may see an objfile without any DWARF, in which case we just
2618 case DWARF2_DEBUG_FRAME:
2619 info = &data->frame;
2621 case DWARF2_EH_FRAME:
2622 info = &data->eh_frame;
2625 gdb_assert_not_reached ("unexpected section");
2628 dwarf2_read_section (objfile, info);
2630 *sectp = get_section_bfd_section (info);
2631 *bufp = info->buffer;
2632 *sizep = info->size;
2635 /* A helper function to find the sections for a .dwz file. */
2638 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2640 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2642 /* Note that we only support the standard ELF names, because .dwz
2643 is ELF-only (at the time of writing). */
2644 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2646 dwz_file->abbrev.s.section = sectp;
2647 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2649 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2651 dwz_file->info.s.section = sectp;
2652 dwz_file->info.size = bfd_get_section_size (sectp);
2654 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2656 dwz_file->str.s.section = sectp;
2657 dwz_file->str.size = bfd_get_section_size (sectp);
2659 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2661 dwz_file->line.s.section = sectp;
2662 dwz_file->line.size = bfd_get_section_size (sectp);
2664 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2666 dwz_file->macro.s.section = sectp;
2667 dwz_file->macro.size = bfd_get_section_size (sectp);
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2671 dwz_file->gdb_index.s.section = sectp;
2672 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2676 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2677 there is no .gnu_debugaltlink section in the file. Error if there
2678 is such a section but the file cannot be found. */
2680 static struct dwz_file *
2681 dwarf2_get_dwz_file (void)
2684 struct cleanup *cleanup;
2685 const char *filename;
2686 struct dwz_file *result;
2687 bfd_size_type buildid_len_arg;
2691 if (dwarf2_per_objfile->dwz_file != NULL)
2692 return dwarf2_per_objfile->dwz_file;
2694 bfd_set_error (bfd_error_no_error);
2695 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2696 &buildid_len_arg, &buildid);
2699 if (bfd_get_error () == bfd_error_no_error)
2701 error (_("could not read '.gnu_debugaltlink' section: %s"),
2702 bfd_errmsg (bfd_get_error ()));
2704 cleanup = make_cleanup (xfree, data);
2705 make_cleanup (xfree, buildid);
2707 buildid_len = (size_t) buildid_len_arg;
2709 filename = (const char *) data;
2711 std::string abs_storage;
2712 if (!IS_ABSOLUTE_PATH (filename))
2714 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2716 make_cleanup (xfree, abs);
2717 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2718 filename = abs_storage.c_str ();
2721 /* First try the file name given in the section. If that doesn't
2722 work, try to use the build-id instead. */
2723 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2724 if (dwz_bfd != NULL)
2726 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2730 if (dwz_bfd == NULL)
2731 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2733 if (dwz_bfd == NULL)
2734 error (_("could not find '.gnu_debugaltlink' file for %s"),
2735 objfile_name (dwarf2_per_objfile->objfile));
2737 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2739 result->dwz_bfd = dwz_bfd.release ();
2741 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2743 do_cleanups (cleanup);
2745 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2746 dwarf2_per_objfile->dwz_file = result;
2750 /* DWARF quick_symbols_functions support. */
2752 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2753 unique line tables, so we maintain a separate table of all .debug_line
2754 derived entries to support the sharing.
2755 All the quick functions need is the list of file names. We discard the
2756 line_header when we're done and don't need to record it here. */
2757 struct quick_file_names
2759 /* The data used to construct the hash key. */
2760 struct stmt_list_hash hash;
2762 /* The number of entries in file_names, real_names. */
2763 unsigned int num_file_names;
2765 /* The file names from the line table, after being run through
2767 const char **file_names;
2769 /* The file names from the line table after being run through
2770 gdb_realpath. These are computed lazily. */
2771 const char **real_names;
2774 /* When using the index (and thus not using psymtabs), each CU has an
2775 object of this type. This is used to hold information needed by
2776 the various "quick" methods. */
2777 struct dwarf2_per_cu_quick_data
2779 /* The file table. This can be NULL if there was no file table
2780 or it's currently not read in.
2781 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2782 struct quick_file_names *file_names;
2784 /* The corresponding symbol table. This is NULL if symbols for this
2785 CU have not yet been read. */
2786 struct compunit_symtab *compunit_symtab;
2788 /* A temporary mark bit used when iterating over all CUs in
2789 expand_symtabs_matching. */
2790 unsigned int mark : 1;
2792 /* True if we've tried to read the file table and found there isn't one.
2793 There will be no point in trying to read it again next time. */
2794 unsigned int no_file_data : 1;
2797 /* Utility hash function for a stmt_list_hash. */
2800 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2804 if (stmt_list_hash->dwo_unit != NULL)
2805 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2806 v += to_underlying (stmt_list_hash->line_sect_off);
2810 /* Utility equality function for a stmt_list_hash. */
2813 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2814 const struct stmt_list_hash *rhs)
2816 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2818 if (lhs->dwo_unit != NULL
2819 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2822 return lhs->line_sect_off == rhs->line_sect_off;
2825 /* Hash function for a quick_file_names. */
2828 hash_file_name_entry (const void *e)
2830 const struct quick_file_names *file_data
2831 = (const struct quick_file_names *) e;
2833 return hash_stmt_list_entry (&file_data->hash);
2836 /* Equality function for a quick_file_names. */
2839 eq_file_name_entry (const void *a, const void *b)
2841 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2842 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2844 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2847 /* Delete function for a quick_file_names. */
2850 delete_file_name_entry (void *e)
2852 struct quick_file_names *file_data = (struct quick_file_names *) e;
2855 for (i = 0; i < file_data->num_file_names; ++i)
2857 xfree ((void*) file_data->file_names[i]);
2858 if (file_data->real_names)
2859 xfree ((void*) file_data->real_names[i]);
2862 /* The space for the struct itself lives on objfile_obstack,
2863 so we don't free it here. */
2866 /* Create a quick_file_names hash table. */
2869 create_quick_file_names_table (unsigned int nr_initial_entries)
2871 return htab_create_alloc (nr_initial_entries,
2872 hash_file_name_entry, eq_file_name_entry,
2873 delete_file_name_entry, xcalloc, xfree);
2876 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2877 have to be created afterwards. You should call age_cached_comp_units after
2878 processing PER_CU->CU. dw2_setup must have been already called. */
2881 load_cu (struct dwarf2_per_cu_data *per_cu)
2883 if (per_cu->is_debug_types)
2884 load_full_type_unit (per_cu);
2886 load_full_comp_unit (per_cu, language_minimal);
2888 if (per_cu->cu == NULL)
2889 return; /* Dummy CU. */
2891 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2894 /* Read in the symbols for PER_CU. */
2897 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2899 struct cleanup *back_to;
2901 /* Skip type_unit_groups, reading the type units they contain
2902 is handled elsewhere. */
2903 if (IS_TYPE_UNIT_GROUP (per_cu))
2906 back_to = make_cleanup (dwarf2_release_queue, NULL);
2908 if (dwarf2_per_objfile->using_index
2909 ? per_cu->v.quick->compunit_symtab == NULL
2910 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2912 queue_comp_unit (per_cu, language_minimal);
2915 /* If we just loaded a CU from a DWO, and we're working with an index
2916 that may badly handle TUs, load all the TUs in that DWO as well.
2917 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2918 if (!per_cu->is_debug_types
2919 && per_cu->cu != NULL
2920 && per_cu->cu->dwo_unit != NULL
2921 && dwarf2_per_objfile->index_table != NULL
2922 && dwarf2_per_objfile->index_table->version <= 7
2923 /* DWP files aren't supported yet. */
2924 && get_dwp_file () == NULL)
2925 queue_and_load_all_dwo_tus (per_cu);
2930 /* Age the cache, releasing compilation units that have not
2931 been used recently. */
2932 age_cached_comp_units ();
2934 do_cleanups (back_to);
2937 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2938 the objfile from which this CU came. Returns the resulting symbol
2941 static struct compunit_symtab *
2942 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2944 gdb_assert (dwarf2_per_objfile->using_index);
2945 if (!per_cu->v.quick->compunit_symtab)
2947 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2948 scoped_restore decrementer = increment_reading_symtab ();
2949 dw2_do_instantiate_symtab (per_cu);
2950 process_cu_includes ();
2951 do_cleanups (back_to);
2954 return per_cu->v.quick->compunit_symtab;
2957 /* Return the CU/TU given its index.
2959 This is intended for loops like:
2961 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2962 + dwarf2_per_objfile->n_type_units); ++i)
2964 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2970 static struct dwarf2_per_cu_data *
2971 dw2_get_cutu (int index)
2973 if (index >= dwarf2_per_objfile->n_comp_units)
2975 index -= dwarf2_per_objfile->n_comp_units;
2976 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2977 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2980 return dwarf2_per_objfile->all_comp_units[index];
2983 /* Return the CU given its index.
2984 This differs from dw2_get_cutu in that it's for when you know INDEX
2987 static struct dwarf2_per_cu_data *
2988 dw2_get_cu (int index)
2990 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2992 return dwarf2_per_objfile->all_comp_units[index];
2995 /* A helper for create_cus_from_index that handles a given list of
2999 create_cus_from_index_list (struct objfile *objfile,
3000 const gdb_byte *cu_list, offset_type n_elements,
3001 struct dwarf2_section_info *section,
3007 for (i = 0; i < n_elements; i += 2)
3009 gdb_static_assert (sizeof (ULONGEST) >= 8);
3011 sect_offset sect_off
3012 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3013 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3016 dwarf2_per_cu_data *the_cu
3017 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3018 struct dwarf2_per_cu_data);
3019 the_cu->sect_off = sect_off;
3020 the_cu->length = length;
3021 the_cu->objfile = objfile;
3022 the_cu->section = section;
3023 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3024 struct dwarf2_per_cu_quick_data);
3025 the_cu->is_dwz = is_dwz;
3026 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
3030 /* Read the CU list from the mapped index, and use it to create all
3031 the CU objects for this objfile. */
3034 create_cus_from_index (struct objfile *objfile,
3035 const gdb_byte *cu_list, offset_type cu_list_elements,
3036 const gdb_byte *dwz_list, offset_type dwz_elements)
3038 struct dwz_file *dwz;
3040 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3041 dwarf2_per_objfile->all_comp_units =
3042 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3043 dwarf2_per_objfile->n_comp_units);
3045 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3046 &dwarf2_per_objfile->info, 0, 0);
3048 if (dwz_elements == 0)
3051 dwz = dwarf2_get_dwz_file ();
3052 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3053 cu_list_elements / 2);
3056 /* Create the signatured type hash table from the index. */
3059 create_signatured_type_table_from_index (struct objfile *objfile,
3060 struct dwarf2_section_info *section,
3061 const gdb_byte *bytes,
3062 offset_type elements)
3065 htab_t sig_types_hash;
3067 dwarf2_per_objfile->n_type_units
3068 = dwarf2_per_objfile->n_allocated_type_units
3070 dwarf2_per_objfile->all_type_units =
3071 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3073 sig_types_hash = allocate_signatured_type_table (objfile);
3075 for (i = 0; i < elements; i += 3)
3077 struct signatured_type *sig_type;
3080 cu_offset type_offset_in_tu;
3082 gdb_static_assert (sizeof (ULONGEST) >= 8);
3083 sect_offset sect_off
3084 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3086 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3088 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3091 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3092 struct signatured_type);
3093 sig_type->signature = signature;
3094 sig_type->type_offset_in_tu = type_offset_in_tu;
3095 sig_type->per_cu.is_debug_types = 1;
3096 sig_type->per_cu.section = section;
3097 sig_type->per_cu.sect_off = sect_off;
3098 sig_type->per_cu.objfile = objfile;
3099 sig_type->per_cu.v.quick
3100 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3101 struct dwarf2_per_cu_quick_data);
3103 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3106 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3109 dwarf2_per_objfile->signatured_types = sig_types_hash;
3112 /* Read the address map data from the mapped index, and use it to
3113 populate the objfile's psymtabs_addrmap. */
3116 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3118 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3119 const gdb_byte *iter, *end;
3120 struct addrmap *mutable_map;
3123 auto_obstack temp_obstack;
3125 mutable_map = addrmap_create_mutable (&temp_obstack);
3127 iter = index->address_table;
3128 end = iter + index->address_table_size;
3130 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3134 ULONGEST hi, lo, cu_index;
3135 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3137 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3139 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3144 complaint (&symfile_complaints,
3145 _(".gdb_index address table has invalid range (%s - %s)"),
3146 hex_string (lo), hex_string (hi));
3150 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3152 complaint (&symfile_complaints,
3153 _(".gdb_index address table has invalid CU number %u"),
3154 (unsigned) cu_index);
3158 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3159 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3160 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3163 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3164 &objfile->objfile_obstack);
3167 /* The hash function for strings in the mapped index. This is the same as
3168 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3169 implementation. This is necessary because the hash function is tied to the
3170 format of the mapped index file. The hash values do not have to match with
3173 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3176 mapped_index_string_hash (int index_version, const void *p)
3178 const unsigned char *str = (const unsigned char *) p;
3182 while ((c = *str++) != 0)
3184 if (index_version >= 5)
3186 r = r * 67 + c - 113;
3192 /* Find a slot in the mapped index INDEX for the object named NAME.
3193 If NAME is found, set *VEC_OUT to point to the CU vector in the
3194 constant pool and return 1. If NAME cannot be found, return 0. */
3197 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3198 offset_type **vec_out)
3200 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3202 offset_type slot, step;
3203 int (*cmp) (const char *, const char *);
3205 if (current_language->la_language == language_cplus
3206 || current_language->la_language == language_fortran
3207 || current_language->la_language == language_d)
3209 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3212 if (strchr (name, '(') != NULL)
3214 char *without_params = cp_remove_params (name);
3216 if (without_params != NULL)
3218 make_cleanup (xfree, without_params);
3219 name = without_params;
3224 /* Index version 4 did not support case insensitive searches. But the
3225 indices for case insensitive languages are built in lowercase, therefore
3226 simulate our NAME being searched is also lowercased. */
3227 hash = mapped_index_string_hash ((index->version == 4
3228 && case_sensitivity == case_sensitive_off
3229 ? 5 : index->version),
3232 slot = hash & (index->symbol_table_slots - 1);
3233 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3234 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3238 /* Convert a slot number to an offset into the table. */
3239 offset_type i = 2 * slot;
3241 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3243 do_cleanups (back_to);
3247 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3248 if (!cmp (name, str))
3250 *vec_out = (offset_type *) (index->constant_pool
3251 + MAYBE_SWAP (index->symbol_table[i + 1]));
3252 do_cleanups (back_to);
3256 slot = (slot + step) & (index->symbol_table_slots - 1);
3260 /* A helper function that reads the .gdb_index from SECTION and fills
3261 in MAP. FILENAME is the name of the file containing the section;
3262 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3263 ok to use deprecated sections.
3265 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3266 out parameters that are filled in with information about the CU and
3267 TU lists in the section.
3269 Returns 1 if all went well, 0 otherwise. */
3272 read_index_from_section (struct objfile *objfile,
3273 const char *filename,
3275 struct dwarf2_section_info *section,
3276 struct mapped_index *map,
3277 const gdb_byte **cu_list,
3278 offset_type *cu_list_elements,
3279 const gdb_byte **types_list,
3280 offset_type *types_list_elements)
3282 const gdb_byte *addr;
3283 offset_type version;
3284 offset_type *metadata;
3287 if (dwarf2_section_empty_p (section))
3290 /* Older elfutils strip versions could keep the section in the main
3291 executable while splitting it for the separate debug info file. */
3292 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3295 dwarf2_read_section (objfile, section);
3297 addr = section->buffer;
3298 /* Version check. */
3299 version = MAYBE_SWAP (*(offset_type *) addr);
3300 /* Versions earlier than 3 emitted every copy of a psymbol. This
3301 causes the index to behave very poorly for certain requests. Version 3
3302 contained incomplete addrmap. So, it seems better to just ignore such
3306 static int warning_printed = 0;
3307 if (!warning_printed)
3309 warning (_("Skipping obsolete .gdb_index section in %s."),
3311 warning_printed = 1;
3315 /* Index version 4 uses a different hash function than index version
3318 Versions earlier than 6 did not emit psymbols for inlined
3319 functions. Using these files will cause GDB not to be able to
3320 set breakpoints on inlined functions by name, so we ignore these
3321 indices unless the user has done
3322 "set use-deprecated-index-sections on". */
3323 if (version < 6 && !deprecated_ok)
3325 static int warning_printed = 0;
3326 if (!warning_printed)
3329 Skipping deprecated .gdb_index section in %s.\n\
3330 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3331 to use the section anyway."),
3333 warning_printed = 1;
3337 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3338 of the TU (for symbols coming from TUs),
3339 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3340 Plus gold-generated indices can have duplicate entries for global symbols,
3341 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3342 These are just performance bugs, and we can't distinguish gdb-generated
3343 indices from gold-generated ones, so issue no warning here. */
3345 /* Indexes with higher version than the one supported by GDB may be no
3346 longer backward compatible. */
3350 map->version = version;
3351 map->total_size = section->size;
3353 metadata = (offset_type *) (addr + sizeof (offset_type));
3356 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3357 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3361 *types_list = addr + MAYBE_SWAP (metadata[i]);
3362 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3363 - MAYBE_SWAP (metadata[i]))
3367 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3368 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3369 - MAYBE_SWAP (metadata[i]));
3372 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3373 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3374 - MAYBE_SWAP (metadata[i]))
3375 / (2 * sizeof (offset_type)));
3378 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3384 /* Read the index file. If everything went ok, initialize the "quick"
3385 elements of all the CUs and return 1. Otherwise, return 0. */
3388 dwarf2_read_index (struct objfile *objfile)
3390 struct mapped_index local_map, *map;
3391 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3392 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3393 struct dwz_file *dwz;
3395 if (!read_index_from_section (objfile, objfile_name (objfile),
3396 use_deprecated_index_sections,
3397 &dwarf2_per_objfile->gdb_index, &local_map,
3398 &cu_list, &cu_list_elements,
3399 &types_list, &types_list_elements))
3402 /* Don't use the index if it's empty. */
3403 if (local_map.symbol_table_slots == 0)
3406 /* If there is a .dwz file, read it so we can get its CU list as
3408 dwz = dwarf2_get_dwz_file ();
3411 struct mapped_index dwz_map;
3412 const gdb_byte *dwz_types_ignore;
3413 offset_type dwz_types_elements_ignore;
3415 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3417 &dwz->gdb_index, &dwz_map,
3418 &dwz_list, &dwz_list_elements,
3420 &dwz_types_elements_ignore))
3422 warning (_("could not read '.gdb_index' section from %s; skipping"),
3423 bfd_get_filename (dwz->dwz_bfd));
3428 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3431 if (types_list_elements)
3433 struct dwarf2_section_info *section;
3435 /* We can only handle a single .debug_types when we have an
3437 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3440 section = VEC_index (dwarf2_section_info_def,
3441 dwarf2_per_objfile->types, 0);
3443 create_signatured_type_table_from_index (objfile, section, types_list,
3444 types_list_elements);
3447 create_addrmap_from_index (objfile, &local_map);
3449 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3452 dwarf2_per_objfile->index_table = map;
3453 dwarf2_per_objfile->using_index = 1;
3454 dwarf2_per_objfile->quick_file_names_table =
3455 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3460 /* A helper for the "quick" functions which sets the global
3461 dwarf2_per_objfile according to OBJFILE. */
3464 dw2_setup (struct objfile *objfile)
3466 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3467 objfile_data (objfile, dwarf2_objfile_data_key));
3468 gdb_assert (dwarf2_per_objfile);
3471 /* die_reader_func for dw2_get_file_names. */
3474 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3475 const gdb_byte *info_ptr,
3476 struct die_info *comp_unit_die,
3480 struct dwarf2_cu *cu = reader->cu;
3481 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3482 struct objfile *objfile = dwarf2_per_objfile->objfile;
3483 struct dwarf2_per_cu_data *lh_cu;
3484 struct attribute *attr;
3487 struct quick_file_names *qfn;
3489 gdb_assert (! this_cu->is_debug_types);
3491 /* Our callers never want to match partial units -- instead they
3492 will match the enclosing full CU. */
3493 if (comp_unit_die->tag == DW_TAG_partial_unit)
3495 this_cu->v.quick->no_file_data = 1;
3503 sect_offset line_offset {};
3505 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3508 struct quick_file_names find_entry;
3510 line_offset = (sect_offset) DW_UNSND (attr);
3512 /* We may have already read in this line header (TU line header sharing).
3513 If we have we're done. */
3514 find_entry.hash.dwo_unit = cu->dwo_unit;
3515 find_entry.hash.line_sect_off = line_offset;
3516 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3517 &find_entry, INSERT);
3520 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3524 lh = dwarf_decode_line_header (line_offset, cu);
3528 lh_cu->v.quick->no_file_data = 1;
3532 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3533 qfn->hash.dwo_unit = cu->dwo_unit;
3534 qfn->hash.line_sect_off = line_offset;
3535 gdb_assert (slot != NULL);
3538 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3540 qfn->num_file_names = lh->file_names.size ();
3542 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3543 for (i = 0; i < lh->file_names.size (); ++i)
3544 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3545 qfn->real_names = NULL;
3547 lh_cu->v.quick->file_names = qfn;
3550 /* A helper for the "quick" functions which attempts to read the line
3551 table for THIS_CU. */
3553 static struct quick_file_names *
3554 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3556 /* This should never be called for TUs. */
3557 gdb_assert (! this_cu->is_debug_types);
3558 /* Nor type unit groups. */
3559 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3561 if (this_cu->v.quick->file_names != NULL)
3562 return this_cu->v.quick->file_names;
3563 /* If we know there is no line data, no point in looking again. */
3564 if (this_cu->v.quick->no_file_data)
3567 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3569 if (this_cu->v.quick->no_file_data)
3571 return this_cu->v.quick->file_names;
3574 /* A helper for the "quick" functions which computes and caches the
3575 real path for a given file name from the line table. */
3578 dw2_get_real_path (struct objfile *objfile,
3579 struct quick_file_names *qfn, int index)
3581 if (qfn->real_names == NULL)
3582 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3583 qfn->num_file_names, const char *);
3585 if (qfn->real_names[index] == NULL)
3586 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3588 return qfn->real_names[index];
3591 static struct symtab *
3592 dw2_find_last_source_symtab (struct objfile *objfile)
3594 struct compunit_symtab *cust;
3597 dw2_setup (objfile);
3598 index = dwarf2_per_objfile->n_comp_units - 1;
3599 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3602 return compunit_primary_filetab (cust);
3605 /* Traversal function for dw2_forget_cached_source_info. */
3608 dw2_free_cached_file_names (void **slot, void *info)
3610 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3612 if (file_data->real_names)
3616 for (i = 0; i < file_data->num_file_names; ++i)
3618 xfree ((void*) file_data->real_names[i]);
3619 file_data->real_names[i] = NULL;
3627 dw2_forget_cached_source_info (struct objfile *objfile)
3629 dw2_setup (objfile);
3631 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3632 dw2_free_cached_file_names, NULL);
3635 /* Helper function for dw2_map_symtabs_matching_filename that expands
3636 the symtabs and calls the iterator. */
3639 dw2_map_expand_apply (struct objfile *objfile,
3640 struct dwarf2_per_cu_data *per_cu,
3641 const char *name, const char *real_path,
3642 gdb::function_view<bool (symtab *)> callback)
3644 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3646 /* Don't visit already-expanded CUs. */
3647 if (per_cu->v.quick->compunit_symtab)
3650 /* This may expand more than one symtab, and we want to iterate over
3652 dw2_instantiate_symtab (per_cu);
3654 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3655 last_made, callback);
3658 /* Implementation of the map_symtabs_matching_filename method. */
3661 dw2_map_symtabs_matching_filename
3662 (struct objfile *objfile, const char *name, const char *real_path,
3663 gdb::function_view<bool (symtab *)> callback)
3666 const char *name_basename = lbasename (name);
3668 dw2_setup (objfile);
3670 /* The rule is CUs specify all the files, including those used by
3671 any TU, so there's no need to scan TUs here. */
3673 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3676 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3677 struct quick_file_names *file_data;
3679 /* We only need to look at symtabs not already expanded. */
3680 if (per_cu->v.quick->compunit_symtab)
3683 file_data = dw2_get_file_names (per_cu);
3684 if (file_data == NULL)
3687 for (j = 0; j < file_data->num_file_names; ++j)
3689 const char *this_name = file_data->file_names[j];
3690 const char *this_real_name;
3692 if (compare_filenames_for_search (this_name, name))
3694 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3700 /* Before we invoke realpath, which can get expensive when many
3701 files are involved, do a quick comparison of the basenames. */
3702 if (! basenames_may_differ
3703 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3706 this_real_name = dw2_get_real_path (objfile, file_data, j);
3707 if (compare_filenames_for_search (this_real_name, name))
3709 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3715 if (real_path != NULL)
3717 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3718 gdb_assert (IS_ABSOLUTE_PATH (name));
3719 if (this_real_name != NULL
3720 && FILENAME_CMP (real_path, this_real_name) == 0)
3722 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3734 /* Struct used to manage iterating over all CUs looking for a symbol. */
3736 struct dw2_symtab_iterator
3738 /* The internalized form of .gdb_index. */
3739 struct mapped_index *index;
3740 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3741 int want_specific_block;
3742 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3743 Unused if !WANT_SPECIFIC_BLOCK. */
3745 /* The kind of symbol we're looking for. */
3747 /* The list of CUs from the index entry of the symbol,
3748 or NULL if not found. */
3750 /* The next element in VEC to look at. */
3752 /* The number of elements in VEC, or zero if there is no match. */
3754 /* Have we seen a global version of the symbol?
3755 If so we can ignore all further global instances.
3756 This is to work around gold/15646, inefficient gold-generated
3761 /* Initialize the index symtab iterator ITER.
3762 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3763 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3766 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3767 struct mapped_index *index,
3768 int want_specific_block,
3773 iter->index = index;
3774 iter->want_specific_block = want_specific_block;
3775 iter->block_index = block_index;
3776 iter->domain = domain;
3778 iter->global_seen = 0;
3780 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3781 iter->length = MAYBE_SWAP (*iter->vec);
3789 /* Return the next matching CU or NULL if there are no more. */
3791 static struct dwarf2_per_cu_data *
3792 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3794 for ( ; iter->next < iter->length; ++iter->next)
3796 offset_type cu_index_and_attrs =
3797 MAYBE_SWAP (iter->vec[iter->next + 1]);
3798 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3799 struct dwarf2_per_cu_data *per_cu;
3800 int want_static = iter->block_index != GLOBAL_BLOCK;
3801 /* This value is only valid for index versions >= 7. */
3802 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3803 gdb_index_symbol_kind symbol_kind =
3804 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3805 /* Only check the symbol attributes if they're present.
3806 Indices prior to version 7 don't record them,
3807 and indices >= 7 may elide them for certain symbols
3808 (gold does this). */
3810 (iter->index->version >= 7
3811 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3813 /* Don't crash on bad data. */
3814 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3815 + dwarf2_per_objfile->n_type_units))
3817 complaint (&symfile_complaints,
3818 _(".gdb_index entry has bad CU index"
3820 objfile_name (dwarf2_per_objfile->objfile));
3824 per_cu = dw2_get_cutu (cu_index);
3826 /* Skip if already read in. */
3827 if (per_cu->v.quick->compunit_symtab)
3830 /* Check static vs global. */
3833 if (iter->want_specific_block
3834 && want_static != is_static)
3836 /* Work around gold/15646. */
3837 if (!is_static && iter->global_seen)
3840 iter->global_seen = 1;
3843 /* Only check the symbol's kind if it has one. */
3846 switch (iter->domain)
3849 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3850 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3851 /* Some types are also in VAR_DOMAIN. */
3852 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3856 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3860 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3875 static struct compunit_symtab *
3876 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3877 const char *name, domain_enum domain)
3879 struct compunit_symtab *stab_best = NULL;
3880 struct mapped_index *index;
3882 dw2_setup (objfile);
3884 index = dwarf2_per_objfile->index_table;
3886 /* index is NULL if OBJF_READNOW. */
3889 struct dw2_symtab_iterator iter;
3890 struct dwarf2_per_cu_data *per_cu;
3892 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3894 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3896 struct symbol *sym, *with_opaque = NULL;
3897 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3898 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3899 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3901 sym = block_find_symbol (block, name, domain,
3902 block_find_non_opaque_type_preferred,
3905 /* Some caution must be observed with overloaded functions
3906 and methods, since the index will not contain any overload
3907 information (but NAME might contain it). */
3910 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3912 if (with_opaque != NULL
3913 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3916 /* Keep looking through other CUs. */
3924 dw2_print_stats (struct objfile *objfile)
3926 int i, total, count;
3928 dw2_setup (objfile);
3929 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3931 for (i = 0; i < total; ++i)
3933 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3935 if (!per_cu->v.quick->compunit_symtab)
3938 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3939 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3942 /* This dumps minimal information about the index.
3943 It is called via "mt print objfiles".
3944 One use is to verify .gdb_index has been loaded by the
3945 gdb.dwarf2/gdb-index.exp testcase. */
3948 dw2_dump (struct objfile *objfile)
3950 dw2_setup (objfile);
3951 gdb_assert (dwarf2_per_objfile->using_index);
3952 printf_filtered (".gdb_index:");
3953 if (dwarf2_per_objfile->index_table != NULL)
3955 printf_filtered (" version %d\n",
3956 dwarf2_per_objfile->index_table->version);
3959 printf_filtered (" faked for \"readnow\"\n");
3960 printf_filtered ("\n");
3964 dw2_relocate (struct objfile *objfile,
3965 const struct section_offsets *new_offsets,
3966 const struct section_offsets *delta)
3968 /* There's nothing to relocate here. */
3972 dw2_expand_symtabs_for_function (struct objfile *objfile,
3973 const char *func_name)
3975 struct mapped_index *index;
3977 dw2_setup (objfile);
3979 index = dwarf2_per_objfile->index_table;
3981 /* index is NULL if OBJF_READNOW. */
3984 struct dw2_symtab_iterator iter;
3985 struct dwarf2_per_cu_data *per_cu;
3987 /* Note: It doesn't matter what we pass for block_index here. */
3988 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3991 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3992 dw2_instantiate_symtab (per_cu);
3997 dw2_expand_all_symtabs (struct objfile *objfile)
4001 dw2_setup (objfile);
4003 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4004 + dwarf2_per_objfile->n_type_units); ++i)
4006 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4008 dw2_instantiate_symtab (per_cu);
4013 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4014 const char *fullname)
4018 dw2_setup (objfile);
4020 /* We don't need to consider type units here.
4021 This is only called for examining code, e.g. expand_line_sal.
4022 There can be an order of magnitude (or more) more type units
4023 than comp units, and we avoid them if we can. */
4025 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4028 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4029 struct quick_file_names *file_data;
4031 /* We only need to look at symtabs not already expanded. */
4032 if (per_cu->v.quick->compunit_symtab)
4035 file_data = dw2_get_file_names (per_cu);
4036 if (file_data == NULL)
4039 for (j = 0; j < file_data->num_file_names; ++j)
4041 const char *this_fullname = file_data->file_names[j];
4043 if (filename_cmp (this_fullname, fullname) == 0)
4045 dw2_instantiate_symtab (per_cu);
4053 dw2_map_matching_symbols (struct objfile *objfile,
4054 const char * name, domain_enum domain,
4056 int (*callback) (struct block *,
4057 struct symbol *, void *),
4058 void *data, symbol_compare_ftype *match,
4059 symbol_compare_ftype *ordered_compare)
4061 /* Currently unimplemented; used for Ada. The function can be called if the
4062 current language is Ada for a non-Ada objfile using GNU index. As Ada
4063 does not look for non-Ada symbols this function should just return. */
4067 dw2_expand_symtabs_matching
4068 (struct objfile *objfile,
4069 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4070 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4071 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4072 enum search_domain kind)
4076 struct mapped_index *index;
4078 dw2_setup (objfile);
4080 /* index_table is NULL if OBJF_READNOW. */
4081 if (!dwarf2_per_objfile->index_table)
4083 index = dwarf2_per_objfile->index_table;
4085 if (file_matcher != NULL)
4087 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4089 NULL, xcalloc, xfree));
4090 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4092 NULL, xcalloc, xfree));
4094 /* The rule is CUs specify all the files, including those used by
4095 any TU, so there's no need to scan TUs here. */
4097 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4100 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4101 struct quick_file_names *file_data;
4106 per_cu->v.quick->mark = 0;
4108 /* We only need to look at symtabs not already expanded. */
4109 if (per_cu->v.quick->compunit_symtab)
4112 file_data = dw2_get_file_names (per_cu);
4113 if (file_data == NULL)
4116 if (htab_find (visited_not_found.get (), file_data) != NULL)
4118 else if (htab_find (visited_found.get (), file_data) != NULL)
4120 per_cu->v.quick->mark = 1;
4124 for (j = 0; j < file_data->num_file_names; ++j)
4126 const char *this_real_name;
4128 if (file_matcher (file_data->file_names[j], false))
4130 per_cu->v.quick->mark = 1;
4134 /* Before we invoke realpath, which can get expensive when many
4135 files are involved, do a quick comparison of the basenames. */
4136 if (!basenames_may_differ
4137 && !file_matcher (lbasename (file_data->file_names[j]),
4141 this_real_name = dw2_get_real_path (objfile, file_data, j);
4142 if (file_matcher (this_real_name, false))
4144 per_cu->v.quick->mark = 1;
4149 slot = htab_find_slot (per_cu->v.quick->mark
4150 ? visited_found.get ()
4151 : visited_not_found.get (),
4157 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4159 offset_type idx = 2 * iter;
4161 offset_type *vec, vec_len, vec_idx;
4162 int global_seen = 0;
4166 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4169 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4171 if (!symbol_matcher (name))
4174 /* The name was matched, now expand corresponding CUs that were
4176 vec = (offset_type *) (index->constant_pool
4177 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4178 vec_len = MAYBE_SWAP (vec[0]);
4179 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4181 struct dwarf2_per_cu_data *per_cu;
4182 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4183 /* This value is only valid for index versions >= 7. */
4184 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4185 gdb_index_symbol_kind symbol_kind =
4186 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4187 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4188 /* Only check the symbol attributes if they're present.
4189 Indices prior to version 7 don't record them,
4190 and indices >= 7 may elide them for certain symbols
4191 (gold does this). */
4193 (index->version >= 7
4194 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4196 /* Work around gold/15646. */
4199 if (!is_static && global_seen)
4205 /* Only check the symbol's kind if it has one. */
4210 case VARIABLES_DOMAIN:
4211 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4214 case FUNCTIONS_DOMAIN:
4215 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4219 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4227 /* Don't crash on bad data. */
4228 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4229 + dwarf2_per_objfile->n_type_units))
4231 complaint (&symfile_complaints,
4232 _(".gdb_index entry has bad CU index"
4233 " [in module %s]"), objfile_name (objfile));
4237 per_cu = dw2_get_cutu (cu_index);
4238 if (file_matcher == NULL || per_cu->v.quick->mark)
4240 int symtab_was_null =
4241 (per_cu->v.quick->compunit_symtab == NULL);
4243 dw2_instantiate_symtab (per_cu);
4245 if (expansion_notify != NULL
4247 && per_cu->v.quick->compunit_symtab != NULL)
4249 expansion_notify (per_cu->v.quick->compunit_symtab);
4256 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4259 static struct compunit_symtab *
4260 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4265 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4266 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4269 if (cust->includes == NULL)
4272 for (i = 0; cust->includes[i]; ++i)
4274 struct compunit_symtab *s = cust->includes[i];
4276 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4284 static struct compunit_symtab *
4285 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4286 struct bound_minimal_symbol msymbol,
4288 struct obj_section *section,
4291 struct dwarf2_per_cu_data *data;
4292 struct compunit_symtab *result;
4294 dw2_setup (objfile);
4296 if (!objfile->psymtabs_addrmap)
4299 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4304 if (warn_if_readin && data->v.quick->compunit_symtab)
4305 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4306 paddress (get_objfile_arch (objfile), pc));
4309 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4311 gdb_assert (result != NULL);
4316 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4317 void *data, int need_fullname)
4319 dw2_setup (objfile);
4321 if (!dwarf2_per_objfile->filenames_cache)
4323 dwarf2_per_objfile->filenames_cache.emplace ();
4325 htab_up visited (htab_create_alloc (10,
4326 htab_hash_pointer, htab_eq_pointer,
4327 NULL, xcalloc, xfree));
4329 /* The rule is CUs specify all the files, including those used
4330 by any TU, so there's no need to scan TUs here. We can
4331 ignore file names coming from already-expanded CUs. */
4333 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4335 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4337 if (per_cu->v.quick->compunit_symtab)
4339 void **slot = htab_find_slot (visited.get (),
4340 per_cu->v.quick->file_names,
4343 *slot = per_cu->v.quick->file_names;
4347 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4350 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4351 struct quick_file_names *file_data;
4354 /* We only need to look at symtabs not already expanded. */
4355 if (per_cu->v.quick->compunit_symtab)
4358 file_data = dw2_get_file_names (per_cu);
4359 if (file_data == NULL)
4362 slot = htab_find_slot (visited.get (), file_data, INSERT);
4365 /* Already visited. */
4370 for (int j = 0; j < file_data->num_file_names; ++j)
4372 const char *filename = file_data->file_names[j];
4373 dwarf2_per_objfile->filenames_cache->seen (filename);
4378 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4380 const char *this_real_name;
4383 this_real_name = gdb_realpath (filename);
4385 this_real_name = NULL;
4386 (*fun) (filename, this_real_name, data);
4391 dw2_has_symbols (struct objfile *objfile)
4396 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4399 dw2_find_last_source_symtab,
4400 dw2_forget_cached_source_info,
4401 dw2_map_symtabs_matching_filename,
4406 dw2_expand_symtabs_for_function,
4407 dw2_expand_all_symtabs,
4408 dw2_expand_symtabs_with_fullname,
4409 dw2_map_matching_symbols,
4410 dw2_expand_symtabs_matching,
4411 dw2_find_pc_sect_compunit_symtab,
4412 dw2_map_symbol_filenames
4415 /* Initialize for reading DWARF for this objfile. Return 0 if this
4416 file will use psymtabs, or 1 if using the GNU index. */
4419 dwarf2_initialize_objfile (struct objfile *objfile)
4421 /* If we're about to read full symbols, don't bother with the
4422 indices. In this case we also don't care if some other debug
4423 format is making psymtabs, because they are all about to be
4425 if ((objfile->flags & OBJF_READNOW))
4429 dwarf2_per_objfile->using_index = 1;
4430 create_all_comp_units (objfile);
4431 create_all_type_units (objfile);
4432 dwarf2_per_objfile->quick_file_names_table =
4433 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4435 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4436 + dwarf2_per_objfile->n_type_units); ++i)
4438 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4440 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4441 struct dwarf2_per_cu_quick_data);
4444 /* Return 1 so that gdb sees the "quick" functions. However,
4445 these functions will be no-ops because we will have expanded
4450 if (dwarf2_read_index (objfile))
4458 /* Build a partial symbol table. */
4461 dwarf2_build_psymtabs (struct objfile *objfile)
4464 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4466 init_psymbol_list (objfile, 1024);
4471 /* This isn't really ideal: all the data we allocate on the
4472 objfile's obstack is still uselessly kept around. However,
4473 freeing it seems unsafe. */
4474 psymtab_discarder psymtabs (objfile);
4475 dwarf2_build_psymtabs_hard (objfile);
4478 CATCH (except, RETURN_MASK_ERROR)
4480 exception_print (gdb_stderr, except);
4485 /* Return the total length of the CU described by HEADER. */
4488 get_cu_length (const struct comp_unit_head *header)
4490 return header->initial_length_size + header->length;
4493 /* Return TRUE if SECT_OFF is within CU_HEADER. */
4496 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
4498 sect_offset bottom = cu_header->sect_off;
4499 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
4501 return sect_off >= bottom && sect_off < top;
4504 /* Find the base address of the compilation unit for range lists and
4505 location lists. It will normally be specified by DW_AT_low_pc.
4506 In DWARF-3 draft 4, the base address could be overridden by
4507 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4508 compilation units with discontinuous ranges. */
4511 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4513 struct attribute *attr;
4516 cu->base_address = 0;
4518 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4521 cu->base_address = attr_value_as_address (attr);
4526 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4529 cu->base_address = attr_value_as_address (attr);
4535 /* Read in the comp unit header information from the debug_info at info_ptr.
4536 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4537 NOTE: This leaves members offset, first_die_offset to be filled in
4540 static const gdb_byte *
4541 read_comp_unit_head (struct comp_unit_head *cu_header,
4542 const gdb_byte *info_ptr,
4543 struct dwarf2_section_info *section,
4544 rcuh_kind section_kind)
4547 unsigned int bytes_read;
4548 const char *filename = get_section_file_name (section);
4549 bfd *abfd = get_section_bfd_owner (section);
4551 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4552 cu_header->initial_length_size = bytes_read;
4553 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4554 info_ptr += bytes_read;
4555 cu_header->version = read_2_bytes (abfd, info_ptr);
4557 if (cu_header->version < 5)
4558 switch (section_kind)
4560 case rcuh_kind::COMPILE:
4561 cu_header->unit_type = DW_UT_compile;
4563 case rcuh_kind::TYPE:
4564 cu_header->unit_type = DW_UT_type;
4567 internal_error (__FILE__, __LINE__,
4568 _("read_comp_unit_head: invalid section_kind"));
4572 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4573 (read_1_byte (abfd, info_ptr));
4575 switch (cu_header->unit_type)
4578 if (section_kind != rcuh_kind::COMPILE)
4579 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4580 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4584 section_kind = rcuh_kind::TYPE;
4587 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4588 "(is %d, should be %d or %d) [in module %s]"),
4589 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4592 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4595 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4598 info_ptr += bytes_read;
4599 if (cu_header->version < 5)
4601 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4604 signed_addr = bfd_get_sign_extend_vma (abfd);
4605 if (signed_addr < 0)
4606 internal_error (__FILE__, __LINE__,
4607 _("read_comp_unit_head: dwarf from non elf file"));
4608 cu_header->signed_addr_p = signed_addr;
4610 if (section_kind == rcuh_kind::TYPE)
4612 LONGEST type_offset;
4614 cu_header->signature = read_8_bytes (abfd, info_ptr);
4617 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4618 info_ptr += bytes_read;
4619 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4620 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
4621 error (_("Dwarf Error: Too big type_offset in compilation unit "
4622 "header (is %s) [in module %s]"), plongest (type_offset),
4629 /* Helper function that returns the proper abbrev section for
4632 static struct dwarf2_section_info *
4633 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4635 struct dwarf2_section_info *abbrev;
4637 if (this_cu->is_dwz)
4638 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4640 abbrev = &dwarf2_per_objfile->abbrev;
4645 /* Subroutine of read_and_check_comp_unit_head and
4646 read_and_check_type_unit_head to simplify them.
4647 Perform various error checking on the header. */
4650 error_check_comp_unit_head (struct comp_unit_head *header,
4651 struct dwarf2_section_info *section,
4652 struct dwarf2_section_info *abbrev_section)
4654 const char *filename = get_section_file_name (section);
4656 if (header->version < 2 || header->version > 5)
4657 error (_("Dwarf Error: wrong version in compilation unit header "
4658 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4661 if (to_underlying (header->abbrev_sect_off)
4662 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4663 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4664 "(offset 0x%x + 6) [in module %s]"),
4665 to_underlying (header->abbrev_sect_off),
4666 to_underlying (header->sect_off),
4669 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
4670 avoid potential 32-bit overflow. */
4671 if (((ULONGEST) header->sect_off + get_cu_length (header))
4673 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4674 "(offset 0x%x + 0) [in module %s]"),
4675 header->length, to_underlying (header->sect_off),
4679 /* Read in a CU/TU header and perform some basic error checking.
4680 The contents of the header are stored in HEADER.
4681 The result is a pointer to the start of the first DIE. */
4683 static const gdb_byte *
4684 read_and_check_comp_unit_head (struct comp_unit_head *header,
4685 struct dwarf2_section_info *section,
4686 struct dwarf2_section_info *abbrev_section,
4687 const gdb_byte *info_ptr,
4688 rcuh_kind section_kind)
4690 const gdb_byte *beg_of_comp_unit = info_ptr;
4691 bfd *abfd = get_section_bfd_owner (section);
4693 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
4695 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4697 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
4699 error_check_comp_unit_head (header, section, abbrev_section);
4704 /* Fetch the abbreviation table offset from a comp or type unit header. */
4707 read_abbrev_offset (struct dwarf2_section_info *section,
4708 sect_offset sect_off)
4710 bfd *abfd = get_section_bfd_owner (section);
4711 const gdb_byte *info_ptr;
4712 unsigned int initial_length_size, offset_size;
4715 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4716 info_ptr = section->buffer + to_underlying (sect_off);
4717 read_initial_length (abfd, info_ptr, &initial_length_size);
4718 offset_size = initial_length_size == 4 ? 4 : 8;
4719 info_ptr += initial_length_size;
4721 version = read_2_bytes (abfd, info_ptr);
4725 /* Skip unit type and address size. */
4729 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
4732 /* Allocate a new partial symtab for file named NAME and mark this new
4733 partial symtab as being an include of PST. */
4736 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4737 struct objfile *objfile)
4739 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4741 if (!IS_ABSOLUTE_PATH (subpst->filename))
4743 /* It shares objfile->objfile_obstack. */
4744 subpst->dirname = pst->dirname;
4747 subpst->textlow = 0;
4748 subpst->texthigh = 0;
4750 subpst->dependencies
4751 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4752 subpst->dependencies[0] = pst;
4753 subpst->number_of_dependencies = 1;
4755 subpst->globals_offset = 0;
4756 subpst->n_global_syms = 0;
4757 subpst->statics_offset = 0;
4758 subpst->n_static_syms = 0;
4759 subpst->compunit_symtab = NULL;
4760 subpst->read_symtab = pst->read_symtab;
4763 /* No private part is necessary for include psymtabs. This property
4764 can be used to differentiate between such include psymtabs and
4765 the regular ones. */
4766 subpst->read_symtab_private = NULL;
4769 /* Read the Line Number Program data and extract the list of files
4770 included by the source file represented by PST. Build an include
4771 partial symtab for each of these included files. */
4774 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4775 struct die_info *die,
4776 struct partial_symtab *pst)
4779 struct attribute *attr;
4781 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4783 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
4785 return; /* No linetable, so no includes. */
4787 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4788 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
4792 hash_signatured_type (const void *item)
4794 const struct signatured_type *sig_type
4795 = (const struct signatured_type *) item;
4797 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4798 return sig_type->signature;
4802 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4804 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4805 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4807 return lhs->signature == rhs->signature;
4810 /* Allocate a hash table for signatured types. */
4813 allocate_signatured_type_table (struct objfile *objfile)
4815 return htab_create_alloc_ex (41,
4816 hash_signatured_type,
4819 &objfile->objfile_obstack,
4820 hashtab_obstack_allocate,
4821 dummy_obstack_deallocate);
4824 /* A helper function to add a signatured type CU to a table. */
4827 add_signatured_type_cu_to_table (void **slot, void *datum)
4829 struct signatured_type *sigt = (struct signatured_type *) *slot;
4830 struct signatured_type ***datap = (struct signatured_type ***) datum;
4838 /* A helper for create_debug_types_hash_table. Read types from SECTION
4839 and fill them into TYPES_HTAB. It will process only type units,
4840 therefore DW_UT_type. */
4843 create_debug_type_hash_table (struct dwo_file *dwo_file,
4844 dwarf2_section_info *section, htab_t &types_htab,
4845 rcuh_kind section_kind)
4847 struct objfile *objfile = dwarf2_per_objfile->objfile;
4848 struct dwarf2_section_info *abbrev_section;
4850 const gdb_byte *info_ptr, *end_ptr;
4852 abbrev_section = (dwo_file != NULL
4853 ? &dwo_file->sections.abbrev
4854 : &dwarf2_per_objfile->abbrev);
4856 if (dwarf_read_debug)
4857 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4858 get_section_name (section),
4859 get_section_file_name (abbrev_section));
4861 dwarf2_read_section (objfile, section);
4862 info_ptr = section->buffer;
4864 if (info_ptr == NULL)
4867 /* We can't set abfd until now because the section may be empty or
4868 not present, in which case the bfd is unknown. */
4869 abfd = get_section_bfd_owner (section);
4871 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4872 because we don't need to read any dies: the signature is in the
4875 end_ptr = info_ptr + section->size;
4876 while (info_ptr < end_ptr)
4878 struct signatured_type *sig_type;
4879 struct dwo_unit *dwo_tu;
4881 const gdb_byte *ptr = info_ptr;
4882 struct comp_unit_head header;
4883 unsigned int length;
4885 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
4887 /* Initialize it due to a false compiler warning. */
4888 header.signature = -1;
4889 header.type_cu_offset_in_tu = (cu_offset) -1;
4891 /* We need to read the type's signature in order to build the hash
4892 table, but we don't need anything else just yet. */
4894 ptr = read_and_check_comp_unit_head (&header, section,
4895 abbrev_section, ptr, section_kind);
4897 length = get_cu_length (&header);
4899 /* Skip dummy type units. */
4900 if (ptr >= info_ptr + length
4901 || peek_abbrev_code (abfd, ptr) == 0
4902 || header.unit_type != DW_UT_type)
4908 if (types_htab == NULL)
4911 types_htab = allocate_dwo_unit_table (objfile);
4913 types_htab = allocate_signatured_type_table (objfile);
4919 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4921 dwo_tu->dwo_file = dwo_file;
4922 dwo_tu->signature = header.signature;
4923 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
4924 dwo_tu->section = section;
4925 dwo_tu->sect_off = sect_off;
4926 dwo_tu->length = length;
4930 /* N.B.: type_offset is not usable if this type uses a DWO file.
4931 The real type_offset is in the DWO file. */
4933 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4934 struct signatured_type);
4935 sig_type->signature = header.signature;
4936 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
4937 sig_type->per_cu.objfile = objfile;
4938 sig_type->per_cu.is_debug_types = 1;
4939 sig_type->per_cu.section = section;
4940 sig_type->per_cu.sect_off = sect_off;
4941 sig_type->per_cu.length = length;
4944 slot = htab_find_slot (types_htab,
4945 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4947 gdb_assert (slot != NULL);
4950 sect_offset dup_sect_off;
4954 const struct dwo_unit *dup_tu
4955 = (const struct dwo_unit *) *slot;
4957 dup_sect_off = dup_tu->sect_off;
4961 const struct signatured_type *dup_tu
4962 = (const struct signatured_type *) *slot;
4964 dup_sect_off = dup_tu->per_cu.sect_off;
4967 complaint (&symfile_complaints,
4968 _("debug type entry at offset 0x%x is duplicate to"
4969 " the entry at offset 0x%x, signature %s"),
4970 to_underlying (sect_off), to_underlying (dup_sect_off),
4971 hex_string (header.signature));
4973 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4975 if (dwarf_read_debug > 1)
4976 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4977 to_underlying (sect_off),
4978 hex_string (header.signature));
4984 /* Create the hash table of all entries in the .debug_types
4985 (or .debug_types.dwo) section(s).
4986 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4987 otherwise it is NULL.
4989 The result is a pointer to the hash table or NULL if there are no types.
4991 Note: This function processes DWO files only, not DWP files. */
4994 create_debug_types_hash_table (struct dwo_file *dwo_file,
4995 VEC (dwarf2_section_info_def) *types,
4999 struct dwarf2_section_info *section;
5001 if (VEC_empty (dwarf2_section_info_def, types))
5005 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5007 create_debug_type_hash_table (dwo_file, section, types_htab,
5011 /* Create the hash table of all entries in the .debug_types section,
5012 and initialize all_type_units.
5013 The result is zero if there is an error (e.g. missing .debug_types section),
5014 otherwise non-zero. */
5017 create_all_type_units (struct objfile *objfile)
5019 htab_t types_htab = NULL;
5020 struct signatured_type **iter;
5022 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5023 rcuh_kind::COMPILE);
5024 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5025 if (types_htab == NULL)
5027 dwarf2_per_objfile->signatured_types = NULL;
5031 dwarf2_per_objfile->signatured_types = types_htab;
5033 dwarf2_per_objfile->n_type_units
5034 = dwarf2_per_objfile->n_allocated_type_units
5035 = htab_elements (types_htab);
5036 dwarf2_per_objfile->all_type_units =
5037 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5038 iter = &dwarf2_per_objfile->all_type_units[0];
5039 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5040 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5041 == dwarf2_per_objfile->n_type_units);
5046 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5047 If SLOT is non-NULL, it is the entry to use in the hash table.
5048 Otherwise we find one. */
5050 static struct signatured_type *
5051 add_type_unit (ULONGEST sig, void **slot)
5053 struct objfile *objfile = dwarf2_per_objfile->objfile;
5054 int n_type_units = dwarf2_per_objfile->n_type_units;
5055 struct signatured_type *sig_type;
5057 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5059 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5061 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5062 dwarf2_per_objfile->n_allocated_type_units = 1;
5063 dwarf2_per_objfile->n_allocated_type_units *= 2;
5064 dwarf2_per_objfile->all_type_units
5065 = XRESIZEVEC (struct signatured_type *,
5066 dwarf2_per_objfile->all_type_units,
5067 dwarf2_per_objfile->n_allocated_type_units);
5068 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5070 dwarf2_per_objfile->n_type_units = n_type_units;
5072 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5073 struct signatured_type);
5074 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5075 sig_type->signature = sig;
5076 sig_type->per_cu.is_debug_types = 1;
5077 if (dwarf2_per_objfile->using_index)
5079 sig_type->per_cu.v.quick =
5080 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5081 struct dwarf2_per_cu_quick_data);
5086 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5089 gdb_assert (*slot == NULL);
5091 /* The rest of sig_type must be filled in by the caller. */
5095 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5096 Fill in SIG_ENTRY with DWO_ENTRY. */
5099 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5100 struct signatured_type *sig_entry,
5101 struct dwo_unit *dwo_entry)
5103 /* Make sure we're not clobbering something we don't expect to. */
5104 gdb_assert (! sig_entry->per_cu.queued);
5105 gdb_assert (sig_entry->per_cu.cu == NULL);
5106 if (dwarf2_per_objfile->using_index)
5108 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5109 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5112 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5113 gdb_assert (sig_entry->signature == dwo_entry->signature);
5114 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5115 gdb_assert (sig_entry->type_unit_group == NULL);
5116 gdb_assert (sig_entry->dwo_unit == NULL);
5118 sig_entry->per_cu.section = dwo_entry->section;
5119 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5120 sig_entry->per_cu.length = dwo_entry->length;
5121 sig_entry->per_cu.reading_dwo_directly = 1;
5122 sig_entry->per_cu.objfile = objfile;
5123 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5124 sig_entry->dwo_unit = dwo_entry;
5127 /* Subroutine of lookup_signatured_type.
5128 If we haven't read the TU yet, create the signatured_type data structure
5129 for a TU to be read in directly from a DWO file, bypassing the stub.
5130 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5131 using .gdb_index, then when reading a CU we want to stay in the DWO file
5132 containing that CU. Otherwise we could end up reading several other DWO
5133 files (due to comdat folding) to process the transitive closure of all the
5134 mentioned TUs, and that can be slow. The current DWO file will have every
5135 type signature that it needs.
5136 We only do this for .gdb_index because in the psymtab case we already have
5137 to read all the DWOs to build the type unit groups. */
5139 static struct signatured_type *
5140 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5142 struct objfile *objfile = dwarf2_per_objfile->objfile;
5143 struct dwo_file *dwo_file;
5144 struct dwo_unit find_dwo_entry, *dwo_entry;
5145 struct signatured_type find_sig_entry, *sig_entry;
5148 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5150 /* If TU skeletons have been removed then we may not have read in any
5152 if (dwarf2_per_objfile->signatured_types == NULL)
5154 dwarf2_per_objfile->signatured_types
5155 = allocate_signatured_type_table (objfile);
5158 /* We only ever need to read in one copy of a signatured type.
5159 Use the global signatured_types array to do our own comdat-folding
5160 of types. If this is the first time we're reading this TU, and
5161 the TU has an entry in .gdb_index, replace the recorded data from
5162 .gdb_index with this TU. */
5164 find_sig_entry.signature = sig;
5165 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5166 &find_sig_entry, INSERT);
5167 sig_entry = (struct signatured_type *) *slot;
5169 /* We can get here with the TU already read, *or* in the process of being
5170 read. Don't reassign the global entry to point to this DWO if that's
5171 the case. Also note that if the TU is already being read, it may not
5172 have come from a DWO, the program may be a mix of Fission-compiled
5173 code and non-Fission-compiled code. */
5175 /* Have we already tried to read this TU?
5176 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5177 needn't exist in the global table yet). */
5178 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5181 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5182 dwo_unit of the TU itself. */
5183 dwo_file = cu->dwo_unit->dwo_file;
5185 /* Ok, this is the first time we're reading this TU. */
5186 if (dwo_file->tus == NULL)
5188 find_dwo_entry.signature = sig;
5189 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5190 if (dwo_entry == NULL)
5193 /* If the global table doesn't have an entry for this TU, add one. */
5194 if (sig_entry == NULL)
5195 sig_entry = add_type_unit (sig, slot);
5197 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5198 sig_entry->per_cu.tu_read = 1;
5202 /* Subroutine of lookup_signatured_type.
5203 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5204 then try the DWP file. If the TU stub (skeleton) has been removed then
5205 it won't be in .gdb_index. */
5207 static struct signatured_type *
5208 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5210 struct objfile *objfile = dwarf2_per_objfile->objfile;
5211 struct dwp_file *dwp_file = get_dwp_file ();
5212 struct dwo_unit *dwo_entry;
5213 struct signatured_type find_sig_entry, *sig_entry;
5216 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5217 gdb_assert (dwp_file != NULL);
5219 /* If TU skeletons have been removed then we may not have read in any
5221 if (dwarf2_per_objfile->signatured_types == NULL)
5223 dwarf2_per_objfile->signatured_types
5224 = allocate_signatured_type_table (objfile);
5227 find_sig_entry.signature = sig;
5228 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5229 &find_sig_entry, INSERT);
5230 sig_entry = (struct signatured_type *) *slot;
5232 /* Have we already tried to read this TU?
5233 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5234 needn't exist in the global table yet). */
5235 if (sig_entry != NULL)
5238 if (dwp_file->tus == NULL)
5240 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5241 sig, 1 /* is_debug_types */);
5242 if (dwo_entry == NULL)
5245 sig_entry = add_type_unit (sig, slot);
5246 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5251 /* Lookup a signature based type for DW_FORM_ref_sig8.
5252 Returns NULL if signature SIG is not present in the table.
5253 It is up to the caller to complain about this. */
5255 static struct signatured_type *
5256 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5259 && dwarf2_per_objfile->using_index)
5261 /* We're in a DWO/DWP file, and we're using .gdb_index.
5262 These cases require special processing. */
5263 if (get_dwp_file () == NULL)
5264 return lookup_dwo_signatured_type (cu, sig);
5266 return lookup_dwp_signatured_type (cu, sig);
5270 struct signatured_type find_entry, *entry;
5272 if (dwarf2_per_objfile->signatured_types == NULL)
5274 find_entry.signature = sig;
5275 entry = ((struct signatured_type *)
5276 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5281 /* Low level DIE reading support. */
5283 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5286 init_cu_die_reader (struct die_reader_specs *reader,
5287 struct dwarf2_cu *cu,
5288 struct dwarf2_section_info *section,
5289 struct dwo_file *dwo_file)
5291 gdb_assert (section->readin && section->buffer != NULL);
5292 reader->abfd = get_section_bfd_owner (section);
5294 reader->dwo_file = dwo_file;
5295 reader->die_section = section;
5296 reader->buffer = section->buffer;
5297 reader->buffer_end = section->buffer + section->size;
5298 reader->comp_dir = NULL;
5301 /* Subroutine of init_cutu_and_read_dies to simplify it.
5302 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5303 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5306 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5307 from it to the DIE in the DWO. If NULL we are skipping the stub.
5308 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5309 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5310 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5311 STUB_COMP_DIR may be non-NULL.
5312 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5313 are filled in with the info of the DIE from the DWO file.
5314 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5315 provided an abbrev table to use.
5316 The result is non-zero if a valid (non-dummy) DIE was found. */
5319 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5320 struct dwo_unit *dwo_unit,
5321 int abbrev_table_provided,
5322 struct die_info *stub_comp_unit_die,
5323 const char *stub_comp_dir,
5324 struct die_reader_specs *result_reader,
5325 const gdb_byte **result_info_ptr,
5326 struct die_info **result_comp_unit_die,
5327 int *result_has_children)
5329 struct objfile *objfile = dwarf2_per_objfile->objfile;
5330 struct dwarf2_cu *cu = this_cu->cu;
5331 struct dwarf2_section_info *section;
5333 const gdb_byte *begin_info_ptr, *info_ptr;
5334 ULONGEST signature; /* Or dwo_id. */
5335 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5336 int i,num_extra_attrs;
5337 struct dwarf2_section_info *dwo_abbrev_section;
5338 struct attribute *attr;
5339 struct die_info *comp_unit_die;
5341 /* At most one of these may be provided. */
5342 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5344 /* These attributes aren't processed until later:
5345 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5346 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5347 referenced later. However, these attributes are found in the stub
5348 which we won't have later. In order to not impose this complication
5349 on the rest of the code, we read them here and copy them to the
5358 if (stub_comp_unit_die != NULL)
5360 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5362 if (! this_cu->is_debug_types)
5363 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5364 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5365 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5366 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5367 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5369 /* There should be a DW_AT_addr_base attribute here (if needed).
5370 We need the value before we can process DW_FORM_GNU_addr_index. */
5372 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5374 cu->addr_base = DW_UNSND (attr);
5376 /* There should be a DW_AT_ranges_base attribute here (if needed).
5377 We need the value before we can process DW_AT_ranges. */
5378 cu->ranges_base = 0;
5379 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5381 cu->ranges_base = DW_UNSND (attr);
5383 else if (stub_comp_dir != NULL)
5385 /* Reconstruct the comp_dir attribute to simplify the code below. */
5386 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5387 comp_dir->name = DW_AT_comp_dir;
5388 comp_dir->form = DW_FORM_string;
5389 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5390 DW_STRING (comp_dir) = stub_comp_dir;
5393 /* Set up for reading the DWO CU/TU. */
5394 cu->dwo_unit = dwo_unit;
5395 section = dwo_unit->section;
5396 dwarf2_read_section (objfile, section);
5397 abfd = get_section_bfd_owner (section);
5398 begin_info_ptr = info_ptr = (section->buffer
5399 + to_underlying (dwo_unit->sect_off));
5400 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5401 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5403 if (this_cu->is_debug_types)
5405 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5407 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5409 info_ptr, rcuh_kind::TYPE);
5410 /* This is not an assert because it can be caused by bad debug info. */
5411 if (sig_type->signature != cu->header.signature)
5413 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5414 " TU at offset 0x%x [in module %s]"),
5415 hex_string (sig_type->signature),
5416 hex_string (cu->header.signature),
5417 to_underlying (dwo_unit->sect_off),
5418 bfd_get_filename (abfd));
5420 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5421 /* For DWOs coming from DWP files, we don't know the CU length
5422 nor the type's offset in the TU until now. */
5423 dwo_unit->length = get_cu_length (&cu->header);
5424 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5426 /* Establish the type offset that can be used to lookup the type.
5427 For DWO files, we don't know it until now. */
5428 sig_type->type_offset_in_section
5429 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5433 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5435 info_ptr, rcuh_kind::COMPILE);
5436 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5437 /* For DWOs coming from DWP files, we don't know the CU length
5439 dwo_unit->length = get_cu_length (&cu->header);
5442 /* Replace the CU's original abbrev table with the DWO's.
5443 Reminder: We can't read the abbrev table until we've read the header. */
5444 if (abbrev_table_provided)
5446 /* Don't free the provided abbrev table, the caller of
5447 init_cutu_and_read_dies owns it. */
5448 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5449 /* Ensure the DWO abbrev table gets freed. */
5450 make_cleanup (dwarf2_free_abbrev_table, cu);
5454 dwarf2_free_abbrev_table (cu);
5455 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5456 /* Leave any existing abbrev table cleanup as is. */
5459 /* Read in the die, but leave space to copy over the attributes
5460 from the stub. This has the benefit of simplifying the rest of
5461 the code - all the work to maintain the illusion of a single
5462 DW_TAG_{compile,type}_unit DIE is done here. */
5463 num_extra_attrs = ((stmt_list != NULL)
5467 + (comp_dir != NULL));
5468 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5469 result_has_children, num_extra_attrs);
5471 /* Copy over the attributes from the stub to the DIE we just read in. */
5472 comp_unit_die = *result_comp_unit_die;
5473 i = comp_unit_die->num_attrs;
5474 if (stmt_list != NULL)
5475 comp_unit_die->attrs[i++] = *stmt_list;
5477 comp_unit_die->attrs[i++] = *low_pc;
5478 if (high_pc != NULL)
5479 comp_unit_die->attrs[i++] = *high_pc;
5481 comp_unit_die->attrs[i++] = *ranges;
5482 if (comp_dir != NULL)
5483 comp_unit_die->attrs[i++] = *comp_dir;
5484 comp_unit_die->num_attrs += num_extra_attrs;
5486 if (dwarf_die_debug)
5488 fprintf_unfiltered (gdb_stdlog,
5489 "Read die from %s@0x%x of %s:\n",
5490 get_section_name (section),
5491 (unsigned) (begin_info_ptr - section->buffer),
5492 bfd_get_filename (abfd));
5493 dump_die (comp_unit_die, dwarf_die_debug);
5496 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5497 TUs by skipping the stub and going directly to the entry in the DWO file.
5498 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5499 to get it via circuitous means. Blech. */
5500 if (comp_dir != NULL)
5501 result_reader->comp_dir = DW_STRING (comp_dir);
5503 /* Skip dummy compilation units. */
5504 if (info_ptr >= begin_info_ptr + dwo_unit->length
5505 || peek_abbrev_code (abfd, info_ptr) == 0)
5508 *result_info_ptr = info_ptr;
5512 /* Subroutine of init_cutu_and_read_dies to simplify it.
5513 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5514 Returns NULL if the specified DWO unit cannot be found. */
5516 static struct dwo_unit *
5517 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5518 struct die_info *comp_unit_die)
5520 struct dwarf2_cu *cu = this_cu->cu;
5521 struct attribute *attr;
5523 struct dwo_unit *dwo_unit;
5524 const char *comp_dir, *dwo_name;
5526 gdb_assert (cu != NULL);
5528 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5529 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5530 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5532 if (this_cu->is_debug_types)
5534 struct signatured_type *sig_type;
5536 /* Since this_cu is the first member of struct signatured_type,
5537 we can go from a pointer to one to a pointer to the other. */
5538 sig_type = (struct signatured_type *) this_cu;
5539 signature = sig_type->signature;
5540 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5544 struct attribute *attr;
5546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5548 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5550 dwo_name, objfile_name (this_cu->objfile));
5551 signature = DW_UNSND (attr);
5552 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5559 /* Subroutine of init_cutu_and_read_dies to simplify it.
5560 See it for a description of the parameters.
5561 Read a TU directly from a DWO file, bypassing the stub.
5563 Note: This function could be a little bit simpler if we shared cleanups
5564 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5565 to do, so we keep this function self-contained. Or we could move this
5566 into our caller, but it's complex enough already. */
5569 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5570 int use_existing_cu, int keep,
5571 die_reader_func_ftype *die_reader_func,
5574 struct dwarf2_cu *cu;
5575 struct signatured_type *sig_type;
5576 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5577 struct die_reader_specs reader;
5578 const gdb_byte *info_ptr;
5579 struct die_info *comp_unit_die;
5582 /* Verify we can do the following downcast, and that we have the
5584 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5585 sig_type = (struct signatured_type *) this_cu;
5586 gdb_assert (sig_type->dwo_unit != NULL);
5588 cleanups = make_cleanup (null_cleanup, NULL);
5590 if (use_existing_cu && this_cu->cu != NULL)
5592 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5594 /* There's no need to do the rereading_dwo_cu handling that
5595 init_cutu_and_read_dies does since we don't read the stub. */
5599 /* If !use_existing_cu, this_cu->cu must be NULL. */
5600 gdb_assert (this_cu->cu == NULL);
5601 cu = XNEW (struct dwarf2_cu);
5602 init_one_comp_unit (cu, this_cu);
5603 /* If an error occurs while loading, release our storage. */
5604 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5607 /* A future optimization, if needed, would be to use an existing
5608 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5609 could share abbrev tables. */
5611 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5612 0 /* abbrev_table_provided */,
5613 NULL /* stub_comp_unit_die */,
5614 sig_type->dwo_unit->dwo_file->comp_dir,
5616 &comp_unit_die, &has_children) == 0)
5619 do_cleanups (cleanups);
5623 /* All the "real" work is done here. */
5624 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5626 /* This duplicates the code in init_cutu_and_read_dies,
5627 but the alternative is making the latter more complex.
5628 This function is only for the special case of using DWO files directly:
5629 no point in overly complicating the general case just to handle this. */
5630 if (free_cu_cleanup != NULL)
5634 /* We've successfully allocated this compilation unit. Let our
5635 caller clean it up when finished with it. */
5636 discard_cleanups (free_cu_cleanup);
5638 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5639 So we have to manually free the abbrev table. */
5640 dwarf2_free_abbrev_table (cu);
5642 /* Link this CU into read_in_chain. */
5643 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5644 dwarf2_per_objfile->read_in_chain = this_cu;
5647 do_cleanups (free_cu_cleanup);
5650 do_cleanups (cleanups);
5653 /* Initialize a CU (or TU) and read its DIEs.
5654 If the CU defers to a DWO file, read the DWO file as well.
5656 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5657 Otherwise the table specified in the comp unit header is read in and used.
5658 This is an optimization for when we already have the abbrev table.
5660 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5661 Otherwise, a new CU is allocated with xmalloc.
5663 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5664 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5666 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5667 linker) then DIE_READER_FUNC will not get called. */
5670 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5671 struct abbrev_table *abbrev_table,
5672 int use_existing_cu, int keep,
5673 die_reader_func_ftype *die_reader_func,
5676 struct objfile *objfile = dwarf2_per_objfile->objfile;
5677 struct dwarf2_section_info *section = this_cu->section;
5678 bfd *abfd = get_section_bfd_owner (section);
5679 struct dwarf2_cu *cu;
5680 const gdb_byte *begin_info_ptr, *info_ptr;
5681 struct die_reader_specs reader;
5682 struct die_info *comp_unit_die;
5684 struct attribute *attr;
5685 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5686 struct signatured_type *sig_type = NULL;
5687 struct dwarf2_section_info *abbrev_section;
5688 /* Non-zero if CU currently points to a DWO file and we need to
5689 reread it. When this happens we need to reread the skeleton die
5690 before we can reread the DWO file (this only applies to CUs, not TUs). */
5691 int rereading_dwo_cu = 0;
5693 if (dwarf_die_debug)
5694 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5695 this_cu->is_debug_types ? "type" : "comp",
5696 to_underlying (this_cu->sect_off));
5698 if (use_existing_cu)
5701 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5702 file (instead of going through the stub), short-circuit all of this. */
5703 if (this_cu->reading_dwo_directly)
5705 /* Narrow down the scope of possibilities to have to understand. */
5706 gdb_assert (this_cu->is_debug_types);
5707 gdb_assert (abbrev_table == NULL);
5708 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5709 die_reader_func, data);
5713 cleanups = make_cleanup (null_cleanup, NULL);
5715 /* This is cheap if the section is already read in. */
5716 dwarf2_read_section (objfile, section);
5718 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5720 abbrev_section = get_abbrev_section_for_cu (this_cu);
5722 if (use_existing_cu && this_cu->cu != NULL)
5725 /* If this CU is from a DWO file we need to start over, we need to
5726 refetch the attributes from the skeleton CU.
5727 This could be optimized by retrieving those attributes from when we
5728 were here the first time: the previous comp_unit_die was stored in
5729 comp_unit_obstack. But there's no data yet that we need this
5731 if (cu->dwo_unit != NULL)
5732 rereading_dwo_cu = 1;
5736 /* If !use_existing_cu, this_cu->cu must be NULL. */
5737 gdb_assert (this_cu->cu == NULL);
5738 cu = XNEW (struct dwarf2_cu);
5739 init_one_comp_unit (cu, this_cu);
5740 /* If an error occurs while loading, release our storage. */
5741 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5744 /* Get the header. */
5745 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
5747 /* We already have the header, there's no need to read it in again. */
5748 info_ptr += to_underlying (cu->header.first_die_cu_offset);
5752 if (this_cu->is_debug_types)
5754 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5755 abbrev_section, info_ptr,
5758 /* Since per_cu is the first member of struct signatured_type,
5759 we can go from a pointer to one to a pointer to the other. */
5760 sig_type = (struct signatured_type *) this_cu;
5761 gdb_assert (sig_type->signature == cu->header.signature);
5762 gdb_assert (sig_type->type_offset_in_tu
5763 == cu->header.type_cu_offset_in_tu);
5764 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5766 /* LENGTH has not been set yet for type units if we're
5767 using .gdb_index. */
5768 this_cu->length = get_cu_length (&cu->header);
5770 /* Establish the type offset that can be used to lookup the type. */
5771 sig_type->type_offset_in_section =
5772 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
5774 this_cu->dwarf_version = cu->header.version;
5778 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5781 rcuh_kind::COMPILE);
5783 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5784 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5785 this_cu->dwarf_version = cu->header.version;
5789 /* Skip dummy compilation units. */
5790 if (info_ptr >= begin_info_ptr + this_cu->length
5791 || peek_abbrev_code (abfd, info_ptr) == 0)
5793 do_cleanups (cleanups);
5797 /* If we don't have them yet, read the abbrevs for this compilation unit.
5798 And if we need to read them now, make sure they're freed when we're
5799 done. Note that it's important that if the CU had an abbrev table
5800 on entry we don't free it when we're done: Somewhere up the call stack
5801 it may be in use. */
5802 if (abbrev_table != NULL)
5804 gdb_assert (cu->abbrev_table == NULL);
5805 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
5806 cu->abbrev_table = abbrev_table;
5808 else if (cu->abbrev_table == NULL)
5810 dwarf2_read_abbrevs (cu, abbrev_section);
5811 make_cleanup (dwarf2_free_abbrev_table, cu);
5813 else if (rereading_dwo_cu)
5815 dwarf2_free_abbrev_table (cu);
5816 dwarf2_read_abbrevs (cu, abbrev_section);
5819 /* Read the top level CU/TU die. */
5820 init_cu_die_reader (&reader, cu, section, NULL);
5821 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5823 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5825 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5826 DWO CU, that this test will fail (the attribute will not be present). */
5827 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5830 struct dwo_unit *dwo_unit;
5831 struct die_info *dwo_comp_unit_die;
5835 complaint (&symfile_complaints,
5836 _("compilation unit with DW_AT_GNU_dwo_name"
5837 " has children (offset 0x%x) [in module %s]"),
5838 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
5840 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5841 if (dwo_unit != NULL)
5843 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5844 abbrev_table != NULL,
5845 comp_unit_die, NULL,
5847 &dwo_comp_unit_die, &has_children) == 0)
5850 do_cleanups (cleanups);
5853 comp_unit_die = dwo_comp_unit_die;
5857 /* Yikes, we couldn't find the rest of the DIE, we only have
5858 the stub. A complaint has already been logged. There's
5859 not much more we can do except pass on the stub DIE to
5860 die_reader_func. We don't want to throw an error on bad
5865 /* All of the above is setup for this call. Yikes. */
5866 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5868 /* Done, clean up. */
5869 if (free_cu_cleanup != NULL)
5873 /* We've successfully allocated this compilation unit. Let our
5874 caller clean it up when finished with it. */
5875 discard_cleanups (free_cu_cleanup);
5877 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5878 So we have to manually free the abbrev table. */
5879 dwarf2_free_abbrev_table (cu);
5881 /* Link this CU into read_in_chain. */
5882 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5883 dwarf2_per_objfile->read_in_chain = this_cu;
5886 do_cleanups (free_cu_cleanup);
5889 do_cleanups (cleanups);
5892 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5893 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5894 to have already done the lookup to find the DWO file).
5896 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5897 THIS_CU->is_debug_types, but nothing else.
5899 We fill in THIS_CU->length.
5901 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5902 linker) then DIE_READER_FUNC will not get called.
5904 THIS_CU->cu is always freed when done.
5905 This is done in order to not leave THIS_CU->cu in a state where we have
5906 to care whether it refers to the "main" CU or the DWO CU. */
5909 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5910 struct dwo_file *dwo_file,
5911 die_reader_func_ftype *die_reader_func,
5914 struct objfile *objfile = dwarf2_per_objfile->objfile;
5915 struct dwarf2_section_info *section = this_cu->section;
5916 bfd *abfd = get_section_bfd_owner (section);
5917 struct dwarf2_section_info *abbrev_section;
5918 struct dwarf2_cu cu;
5919 const gdb_byte *begin_info_ptr, *info_ptr;
5920 struct die_reader_specs reader;
5921 struct cleanup *cleanups;
5922 struct die_info *comp_unit_die;
5925 if (dwarf_die_debug)
5926 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5927 this_cu->is_debug_types ? "type" : "comp",
5928 to_underlying (this_cu->sect_off));
5930 gdb_assert (this_cu->cu == NULL);
5932 abbrev_section = (dwo_file != NULL
5933 ? &dwo_file->sections.abbrev
5934 : get_abbrev_section_for_cu (this_cu));
5936 /* This is cheap if the section is already read in. */
5937 dwarf2_read_section (objfile, section);
5939 init_one_comp_unit (&cu, this_cu);
5941 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5943 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5944 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5945 abbrev_section, info_ptr,
5946 (this_cu->is_debug_types
5948 : rcuh_kind::COMPILE));
5950 this_cu->length = get_cu_length (&cu.header);
5952 /* Skip dummy compilation units. */
5953 if (info_ptr >= begin_info_ptr + this_cu->length
5954 || peek_abbrev_code (abfd, info_ptr) == 0)
5956 do_cleanups (cleanups);
5960 dwarf2_read_abbrevs (&cu, abbrev_section);
5961 make_cleanup (dwarf2_free_abbrev_table, &cu);
5963 init_cu_die_reader (&reader, &cu, section, dwo_file);
5964 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5966 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5968 do_cleanups (cleanups);
5971 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5972 does not lookup the specified DWO file.
5973 This cannot be used to read DWO files.
5975 THIS_CU->cu is always freed when done.
5976 This is done in order to not leave THIS_CU->cu in a state where we have
5977 to care whether it refers to the "main" CU or the DWO CU.
5978 We can revisit this if the data shows there's a performance issue. */
5981 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5982 die_reader_func_ftype *die_reader_func,
5985 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5988 /* Type Unit Groups.
5990 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5991 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5992 so that all types coming from the same compilation (.o file) are grouped
5993 together. A future step could be to put the types in the same symtab as
5994 the CU the types ultimately came from. */
5997 hash_type_unit_group (const void *item)
5999 const struct type_unit_group *tu_group
6000 = (const struct type_unit_group *) item;
6002 return hash_stmt_list_entry (&tu_group->hash);
6006 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6008 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6009 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6011 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6014 /* Allocate a hash table for type unit groups. */
6017 allocate_type_unit_groups_table (void)
6019 return htab_create_alloc_ex (3,
6020 hash_type_unit_group,
6023 &dwarf2_per_objfile->objfile->objfile_obstack,
6024 hashtab_obstack_allocate,
6025 dummy_obstack_deallocate);
6028 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6029 partial symtabs. We combine several TUs per psymtab to not let the size
6030 of any one psymtab grow too big. */
6031 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6032 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6034 /* Helper routine for get_type_unit_group.
6035 Create the type_unit_group object used to hold one or more TUs. */
6037 static struct type_unit_group *
6038 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6040 struct objfile *objfile = dwarf2_per_objfile->objfile;
6041 struct dwarf2_per_cu_data *per_cu;
6042 struct type_unit_group *tu_group;
6044 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6045 struct type_unit_group);
6046 per_cu = &tu_group->per_cu;
6047 per_cu->objfile = objfile;
6049 if (dwarf2_per_objfile->using_index)
6051 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6052 struct dwarf2_per_cu_quick_data);
6056 unsigned int line_offset = to_underlying (line_offset_struct);
6057 struct partial_symtab *pst;
6060 /* Give the symtab a useful name for debug purposes. */
6061 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6062 name = xstrprintf ("<type_units_%d>",
6063 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6065 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6067 pst = create_partial_symtab (per_cu, name);
6073 tu_group->hash.dwo_unit = cu->dwo_unit;
6074 tu_group->hash.line_sect_off = line_offset_struct;
6079 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6080 STMT_LIST is a DW_AT_stmt_list attribute. */
6082 static struct type_unit_group *
6083 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6085 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6086 struct type_unit_group *tu_group;
6088 unsigned int line_offset;
6089 struct type_unit_group type_unit_group_for_lookup;
6091 if (dwarf2_per_objfile->type_unit_groups == NULL)
6093 dwarf2_per_objfile->type_unit_groups =
6094 allocate_type_unit_groups_table ();
6097 /* Do we need to create a new group, or can we use an existing one? */
6101 line_offset = DW_UNSND (stmt_list);
6102 ++tu_stats->nr_symtab_sharers;
6106 /* Ugh, no stmt_list. Rare, but we have to handle it.
6107 We can do various things here like create one group per TU or
6108 spread them over multiple groups to split up the expansion work.
6109 To avoid worst case scenarios (too many groups or too large groups)
6110 we, umm, group them in bunches. */
6111 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6112 | (tu_stats->nr_stmt_less_type_units
6113 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6114 ++tu_stats->nr_stmt_less_type_units;
6117 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6118 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6119 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6120 &type_unit_group_for_lookup, INSERT);
6123 tu_group = (struct type_unit_group *) *slot;
6124 gdb_assert (tu_group != NULL);
6128 sect_offset line_offset_struct = (sect_offset) line_offset;
6129 tu_group = create_type_unit_group (cu, line_offset_struct);
6131 ++tu_stats->nr_symtabs;
6137 /* Partial symbol tables. */
6139 /* Create a psymtab named NAME and assign it to PER_CU.
6141 The caller must fill in the following details:
6142 dirname, textlow, texthigh. */
6144 static struct partial_symtab *
6145 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6147 struct objfile *objfile = per_cu->objfile;
6148 struct partial_symtab *pst;
6150 pst = start_psymtab_common (objfile, name, 0,
6151 objfile->global_psymbols.next,
6152 objfile->static_psymbols.next);
6154 pst->psymtabs_addrmap_supported = 1;
6156 /* This is the glue that links PST into GDB's symbol API. */
6157 pst->read_symtab_private = per_cu;
6158 pst->read_symtab = dwarf2_read_symtab;
6159 per_cu->v.psymtab = pst;
6164 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6167 struct process_psymtab_comp_unit_data
6169 /* True if we are reading a DW_TAG_partial_unit. */
6171 int want_partial_unit;
6173 /* The "pretend" language that is used if the CU doesn't declare a
6176 enum language pretend_language;
6179 /* die_reader_func for process_psymtab_comp_unit. */
6182 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6183 const gdb_byte *info_ptr,
6184 struct die_info *comp_unit_die,
6188 struct dwarf2_cu *cu = reader->cu;
6189 struct objfile *objfile = cu->objfile;
6190 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6191 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6193 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6194 struct partial_symtab *pst;
6195 enum pc_bounds_kind cu_bounds_kind;
6196 const char *filename;
6197 struct process_psymtab_comp_unit_data *info
6198 = (struct process_psymtab_comp_unit_data *) data;
6200 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6203 gdb_assert (! per_cu->is_debug_types);
6205 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6207 cu->list_in_scope = &file_symbols;
6209 /* Allocate a new partial symbol table structure. */
6210 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6211 if (filename == NULL)
6214 pst = create_partial_symtab (per_cu, filename);
6216 /* This must be done before calling dwarf2_build_include_psymtabs. */
6217 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6219 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6221 dwarf2_find_base_address (comp_unit_die, cu);
6223 /* Possibly set the default values of LOWPC and HIGHPC from
6225 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6226 &best_highpc, cu, pst);
6227 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6228 /* Store the contiguous range if it is not empty; it can be empty for
6229 CUs with no code. */
6230 addrmap_set_empty (objfile->psymtabs_addrmap,
6231 gdbarch_adjust_dwarf2_addr (gdbarch,
6232 best_lowpc + baseaddr),
6233 gdbarch_adjust_dwarf2_addr (gdbarch,
6234 best_highpc + baseaddr) - 1,
6237 /* Check if comp unit has_children.
6238 If so, read the rest of the partial symbols from this comp unit.
6239 If not, there's no more debug_info for this comp unit. */
6242 struct partial_die_info *first_die;
6243 CORE_ADDR lowpc, highpc;
6245 lowpc = ((CORE_ADDR) -1);
6246 highpc = ((CORE_ADDR) 0);
6248 first_die = load_partial_dies (reader, info_ptr, 1);
6250 scan_partial_symbols (first_die, &lowpc, &highpc,
6251 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6253 /* If we didn't find a lowpc, set it to highpc to avoid
6254 complaints from `maint check'. */
6255 if (lowpc == ((CORE_ADDR) -1))
6258 /* If the compilation unit didn't have an explicit address range,
6259 then use the information extracted from its child dies. */
6260 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6263 best_highpc = highpc;
6266 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6267 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6269 end_psymtab_common (objfile, pst);
6271 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6274 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6275 struct dwarf2_per_cu_data *iter;
6277 /* Fill in 'dependencies' here; we fill in 'users' in a
6279 pst->number_of_dependencies = len;
6281 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6283 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6286 pst->dependencies[i] = iter->v.psymtab;
6288 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6291 /* Get the list of files included in the current compilation unit,
6292 and build a psymtab for each of them. */
6293 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6295 if (dwarf_read_debug)
6297 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6299 fprintf_unfiltered (gdb_stdlog,
6300 "Psymtab for %s unit @0x%x: %s - %s"
6301 ", %d global, %d static syms\n",
6302 per_cu->is_debug_types ? "type" : "comp",
6303 to_underlying (per_cu->sect_off),
6304 paddress (gdbarch, pst->textlow),
6305 paddress (gdbarch, pst->texthigh),
6306 pst->n_global_syms, pst->n_static_syms);
6310 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6311 Process compilation unit THIS_CU for a psymtab. */
6314 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6315 int want_partial_unit,
6316 enum language pretend_language)
6318 struct process_psymtab_comp_unit_data info;
6320 /* If this compilation unit was already read in, free the
6321 cached copy in order to read it in again. This is
6322 necessary because we skipped some symbols when we first
6323 read in the compilation unit (see load_partial_dies).
6324 This problem could be avoided, but the benefit is unclear. */
6325 if (this_cu->cu != NULL)
6326 free_one_cached_comp_unit (this_cu);
6328 gdb_assert (! this_cu->is_debug_types);
6329 info.want_partial_unit = want_partial_unit;
6330 info.pretend_language = pretend_language;
6331 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6332 process_psymtab_comp_unit_reader,
6335 /* Age out any secondary CUs. */
6336 age_cached_comp_units ();
6339 /* Reader function for build_type_psymtabs. */
6342 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6343 const gdb_byte *info_ptr,
6344 struct die_info *type_unit_die,
6348 struct objfile *objfile = dwarf2_per_objfile->objfile;
6349 struct dwarf2_cu *cu = reader->cu;
6350 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6351 struct signatured_type *sig_type;
6352 struct type_unit_group *tu_group;
6353 struct attribute *attr;
6354 struct partial_die_info *first_die;
6355 CORE_ADDR lowpc, highpc;
6356 struct partial_symtab *pst;
6358 gdb_assert (data == NULL);
6359 gdb_assert (per_cu->is_debug_types);
6360 sig_type = (struct signatured_type *) per_cu;
6365 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6366 tu_group = get_type_unit_group (cu, attr);
6368 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6370 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6371 cu->list_in_scope = &file_symbols;
6372 pst = create_partial_symtab (per_cu, "");
6375 first_die = load_partial_dies (reader, info_ptr, 1);
6377 lowpc = (CORE_ADDR) -1;
6378 highpc = (CORE_ADDR) 0;
6379 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6381 end_psymtab_common (objfile, pst);
6384 /* Struct used to sort TUs by their abbreviation table offset. */
6386 struct tu_abbrev_offset
6388 struct signatured_type *sig_type;
6389 sect_offset abbrev_offset;
6392 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6395 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6397 const struct tu_abbrev_offset * const *a
6398 = (const struct tu_abbrev_offset * const*) ap;
6399 const struct tu_abbrev_offset * const *b
6400 = (const struct tu_abbrev_offset * const*) bp;
6401 sect_offset aoff = (*a)->abbrev_offset;
6402 sect_offset boff = (*b)->abbrev_offset;
6404 return (aoff > boff) - (aoff < boff);
6407 /* Efficiently read all the type units.
6408 This does the bulk of the work for build_type_psymtabs.
6410 The efficiency is because we sort TUs by the abbrev table they use and
6411 only read each abbrev table once. In one program there are 200K TUs
6412 sharing 8K abbrev tables.
6414 The main purpose of this function is to support building the
6415 dwarf2_per_objfile->type_unit_groups table.
6416 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6417 can collapse the search space by grouping them by stmt_list.
6418 The savings can be significant, in the same program from above the 200K TUs
6419 share 8K stmt_list tables.
6421 FUNC is expected to call get_type_unit_group, which will create the
6422 struct type_unit_group if necessary and add it to
6423 dwarf2_per_objfile->type_unit_groups. */
6426 build_type_psymtabs_1 (void)
6428 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6429 struct cleanup *cleanups;
6430 struct abbrev_table *abbrev_table;
6431 sect_offset abbrev_offset;
6432 struct tu_abbrev_offset *sorted_by_abbrev;
6435 /* It's up to the caller to not call us multiple times. */
6436 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6438 if (dwarf2_per_objfile->n_type_units == 0)
6441 /* TUs typically share abbrev tables, and there can be way more TUs than
6442 abbrev tables. Sort by abbrev table to reduce the number of times we
6443 read each abbrev table in.
6444 Alternatives are to punt or to maintain a cache of abbrev tables.
6445 This is simpler and efficient enough for now.
6447 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6448 symtab to use). Typically TUs with the same abbrev offset have the same
6449 stmt_list value too so in practice this should work well.
6451 The basic algorithm here is:
6453 sort TUs by abbrev table
6454 for each TU with same abbrev table:
6455 read abbrev table if first user
6456 read TU top level DIE
6457 [IWBN if DWO skeletons had DW_AT_stmt_list]
6460 if (dwarf_read_debug)
6461 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6463 /* Sort in a separate table to maintain the order of all_type_units
6464 for .gdb_index: TU indices directly index all_type_units. */
6465 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6466 dwarf2_per_objfile->n_type_units);
6467 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6469 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6471 sorted_by_abbrev[i].sig_type = sig_type;
6472 sorted_by_abbrev[i].abbrev_offset =
6473 read_abbrev_offset (sig_type->per_cu.section,
6474 sig_type->per_cu.sect_off);
6476 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6477 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6478 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6480 abbrev_offset = (sect_offset) ~(unsigned) 0;
6481 abbrev_table = NULL;
6482 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6484 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6486 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6488 /* Switch to the next abbrev table if necessary. */
6489 if (abbrev_table == NULL
6490 || tu->abbrev_offset != abbrev_offset)
6492 if (abbrev_table != NULL)
6494 abbrev_table_free (abbrev_table);
6495 /* Reset to NULL in case abbrev_table_read_table throws
6496 an error: abbrev_table_free_cleanup will get called. */
6497 abbrev_table = NULL;
6499 abbrev_offset = tu->abbrev_offset;
6501 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6503 ++tu_stats->nr_uniq_abbrev_tables;
6506 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6507 build_type_psymtabs_reader, NULL);
6510 do_cleanups (cleanups);
6513 /* Print collected type unit statistics. */
6516 print_tu_stats (void)
6518 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6520 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6521 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6522 dwarf2_per_objfile->n_type_units);
6523 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6524 tu_stats->nr_uniq_abbrev_tables);
6525 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6526 tu_stats->nr_symtabs);
6527 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6528 tu_stats->nr_symtab_sharers);
6529 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6530 tu_stats->nr_stmt_less_type_units);
6531 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6532 tu_stats->nr_all_type_units_reallocs);
6535 /* Traversal function for build_type_psymtabs. */
6538 build_type_psymtab_dependencies (void **slot, void *info)
6540 struct objfile *objfile = dwarf2_per_objfile->objfile;
6541 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6542 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6543 struct partial_symtab *pst = per_cu->v.psymtab;
6544 int len = VEC_length (sig_type_ptr, tu_group->tus);
6545 struct signatured_type *iter;
6548 gdb_assert (len > 0);
6549 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6551 pst->number_of_dependencies = len;
6553 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6555 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6558 gdb_assert (iter->per_cu.is_debug_types);
6559 pst->dependencies[i] = iter->per_cu.v.psymtab;
6560 iter->type_unit_group = tu_group;
6563 VEC_free (sig_type_ptr, tu_group->tus);
6568 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6569 Build partial symbol tables for the .debug_types comp-units. */
6572 build_type_psymtabs (struct objfile *objfile)
6574 if (! create_all_type_units (objfile))
6577 build_type_psymtabs_1 ();
6580 /* Traversal function for process_skeletonless_type_unit.
6581 Read a TU in a DWO file and build partial symbols for it. */
6584 process_skeletonless_type_unit (void **slot, void *info)
6586 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6587 struct objfile *objfile = (struct objfile *) info;
6588 struct signatured_type find_entry, *entry;
6590 /* If this TU doesn't exist in the global table, add it and read it in. */
6592 if (dwarf2_per_objfile->signatured_types == NULL)
6594 dwarf2_per_objfile->signatured_types
6595 = allocate_signatured_type_table (objfile);
6598 find_entry.signature = dwo_unit->signature;
6599 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6601 /* If we've already seen this type there's nothing to do. What's happening
6602 is we're doing our own version of comdat-folding here. */
6606 /* This does the job that create_all_type_units would have done for
6608 entry = add_type_unit (dwo_unit->signature, slot);
6609 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6612 /* This does the job that build_type_psymtabs_1 would have done. */
6613 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6614 build_type_psymtabs_reader, NULL);
6619 /* Traversal function for process_skeletonless_type_units. */
6622 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6624 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6626 if (dwo_file->tus != NULL)
6628 htab_traverse_noresize (dwo_file->tus,
6629 process_skeletonless_type_unit, info);
6635 /* Scan all TUs of DWO files, verifying we've processed them.
6636 This is needed in case a TU was emitted without its skeleton.
6637 Note: This can't be done until we know what all the DWO files are. */
6640 process_skeletonless_type_units (struct objfile *objfile)
6642 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6643 if (get_dwp_file () == NULL
6644 && dwarf2_per_objfile->dwo_files != NULL)
6646 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6647 process_dwo_file_for_skeletonless_type_units,
6652 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6655 psymtabs_addrmap_cleanup (void *o)
6657 struct objfile *objfile = (struct objfile *) o;
6659 objfile->psymtabs_addrmap = NULL;
6662 /* Compute the 'user' field for each psymtab in OBJFILE. */
6665 set_partial_user (struct objfile *objfile)
6669 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6671 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6672 struct partial_symtab *pst = per_cu->v.psymtab;
6678 for (j = 0; j < pst->number_of_dependencies; ++j)
6680 /* Set the 'user' field only if it is not already set. */
6681 if (pst->dependencies[j]->user == NULL)
6682 pst->dependencies[j]->user = pst;
6687 /* Build the partial symbol table by doing a quick pass through the
6688 .debug_info and .debug_abbrev sections. */
6691 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6693 struct cleanup *back_to, *addrmap_cleanup;
6696 if (dwarf_read_debug)
6698 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6699 objfile_name (objfile));
6702 dwarf2_per_objfile->reading_partial_symbols = 1;
6704 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6706 /* Any cached compilation units will be linked by the per-objfile
6707 read_in_chain. Make sure to free them when we're done. */
6708 back_to = make_cleanup (free_cached_comp_units, NULL);
6710 build_type_psymtabs (objfile);
6712 create_all_comp_units (objfile);
6714 /* Create a temporary address map on a temporary obstack. We later
6715 copy this to the final obstack. */
6716 auto_obstack temp_obstack;
6717 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6718 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6720 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6722 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6724 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6727 /* This has to wait until we read the CUs, we need the list of DWOs. */
6728 process_skeletonless_type_units (objfile);
6730 /* Now that all TUs have been processed we can fill in the dependencies. */
6731 if (dwarf2_per_objfile->type_unit_groups != NULL)
6733 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6734 build_type_psymtab_dependencies, NULL);
6737 if (dwarf_read_debug)
6740 set_partial_user (objfile);
6742 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6743 &objfile->objfile_obstack);
6744 discard_cleanups (addrmap_cleanup);
6746 do_cleanups (back_to);
6748 if (dwarf_read_debug)
6749 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6750 objfile_name (objfile));
6753 /* die_reader_func for load_partial_comp_unit. */
6756 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6757 const gdb_byte *info_ptr,
6758 struct die_info *comp_unit_die,
6762 struct dwarf2_cu *cu = reader->cu;
6764 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6766 /* Check if comp unit has_children.
6767 If so, read the rest of the partial symbols from this comp unit.
6768 If not, there's no more debug_info for this comp unit. */
6770 load_partial_dies (reader, info_ptr, 0);
6773 /* Load the partial DIEs for a secondary CU into memory.
6774 This is also used when rereading a primary CU with load_all_dies. */
6777 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6779 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6780 load_partial_comp_unit_reader, NULL);
6784 read_comp_units_from_section (struct objfile *objfile,
6785 struct dwarf2_section_info *section,
6786 unsigned int is_dwz,
6789 struct dwarf2_per_cu_data ***all_comp_units)
6791 const gdb_byte *info_ptr;
6792 bfd *abfd = get_section_bfd_owner (section);
6794 if (dwarf_read_debug)
6795 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6796 get_section_name (section),
6797 get_section_file_name (section));
6799 dwarf2_read_section (objfile, section);
6801 info_ptr = section->buffer;
6803 while (info_ptr < section->buffer + section->size)
6805 unsigned int length, initial_length_size;
6806 struct dwarf2_per_cu_data *this_cu;
6808 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
6810 /* Read just enough information to find out where the next
6811 compilation unit is. */
6812 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6814 /* Save the compilation unit for later lookup. */
6815 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6816 memset (this_cu, 0, sizeof (*this_cu));
6817 this_cu->sect_off = sect_off;
6818 this_cu->length = length + initial_length_size;
6819 this_cu->is_dwz = is_dwz;
6820 this_cu->objfile = objfile;
6821 this_cu->section = section;
6823 if (*n_comp_units == *n_allocated)
6826 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6827 *all_comp_units, *n_allocated);
6829 (*all_comp_units)[*n_comp_units] = this_cu;
6832 info_ptr = info_ptr + this_cu->length;
6836 /* Create a list of all compilation units in OBJFILE.
6837 This is only done for -readnow and building partial symtabs. */
6840 create_all_comp_units (struct objfile *objfile)
6844 struct dwarf2_per_cu_data **all_comp_units;
6845 struct dwz_file *dwz;
6849 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6851 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6852 &n_allocated, &n_comp_units, &all_comp_units);
6854 dwz = dwarf2_get_dwz_file ();
6856 read_comp_units_from_section (objfile, &dwz->info, 1,
6857 &n_allocated, &n_comp_units,
6860 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6861 struct dwarf2_per_cu_data *,
6863 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6864 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6865 xfree (all_comp_units);
6866 dwarf2_per_objfile->n_comp_units = n_comp_units;
6869 /* Process all loaded DIEs for compilation unit CU, starting at
6870 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6871 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6872 DW_AT_ranges). See the comments of add_partial_subprogram on how
6873 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6876 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6877 CORE_ADDR *highpc, int set_addrmap,
6878 struct dwarf2_cu *cu)
6880 struct partial_die_info *pdi;
6882 /* Now, march along the PDI's, descending into ones which have
6883 interesting children but skipping the children of the other ones,
6884 until we reach the end of the compilation unit. */
6890 fixup_partial_die (pdi, cu);
6892 /* Anonymous namespaces or modules have no name but have interesting
6893 children, so we need to look at them. Ditto for anonymous
6896 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6897 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6898 || pdi->tag == DW_TAG_imported_unit)
6902 case DW_TAG_subprogram:
6903 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6905 case DW_TAG_constant:
6906 case DW_TAG_variable:
6907 case DW_TAG_typedef:
6908 case DW_TAG_union_type:
6909 if (!pdi->is_declaration)
6911 add_partial_symbol (pdi, cu);
6914 case DW_TAG_class_type:
6915 case DW_TAG_interface_type:
6916 case DW_TAG_structure_type:
6917 if (!pdi->is_declaration)
6919 add_partial_symbol (pdi, cu);
6921 if (cu->language == language_rust && pdi->has_children)
6922 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6925 case DW_TAG_enumeration_type:
6926 if (!pdi->is_declaration)
6927 add_partial_enumeration (pdi, cu);
6929 case DW_TAG_base_type:
6930 case DW_TAG_subrange_type:
6931 /* File scope base type definitions are added to the partial
6933 add_partial_symbol (pdi, cu);
6935 case DW_TAG_namespace:
6936 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6939 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6941 case DW_TAG_imported_unit:
6943 struct dwarf2_per_cu_data *per_cu;
6945 /* For now we don't handle imported units in type units. */
6946 if (cu->per_cu->is_debug_types)
6948 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6949 " supported in type units [in module %s]"),
6950 objfile_name (cu->objfile));
6953 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
6957 /* Go read the partial unit, if needed. */
6958 if (per_cu->v.psymtab == NULL)
6959 process_psymtab_comp_unit (per_cu, 1, cu->language);
6961 VEC_safe_push (dwarf2_per_cu_ptr,
6962 cu->per_cu->imported_symtabs, per_cu);
6965 case DW_TAG_imported_declaration:
6966 add_partial_symbol (pdi, cu);
6973 /* If the die has a sibling, skip to the sibling. */
6975 pdi = pdi->die_sibling;
6979 /* Functions used to compute the fully scoped name of a partial DIE.
6981 Normally, this is simple. For C++, the parent DIE's fully scoped
6982 name is concatenated with "::" and the partial DIE's name.
6983 Enumerators are an exception; they use the scope of their parent
6984 enumeration type, i.e. the name of the enumeration type is not
6985 prepended to the enumerator.
6987 There are two complexities. One is DW_AT_specification; in this
6988 case "parent" means the parent of the target of the specification,
6989 instead of the direct parent of the DIE. The other is compilers
6990 which do not emit DW_TAG_namespace; in this case we try to guess
6991 the fully qualified name of structure types from their members'
6992 linkage names. This must be done using the DIE's children rather
6993 than the children of any DW_AT_specification target. We only need
6994 to do this for structures at the top level, i.e. if the target of
6995 any DW_AT_specification (if any; otherwise the DIE itself) does not
6998 /* Compute the scope prefix associated with PDI's parent, in
6999 compilation unit CU. The result will be allocated on CU's
7000 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7001 field. NULL is returned if no prefix is necessary. */
7003 partial_die_parent_scope (struct partial_die_info *pdi,
7004 struct dwarf2_cu *cu)
7006 const char *grandparent_scope;
7007 struct partial_die_info *parent, *real_pdi;
7009 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7010 then this means the parent of the specification DIE. */
7013 while (real_pdi->has_specification)
7014 real_pdi = find_partial_die (real_pdi->spec_offset,
7015 real_pdi->spec_is_dwz, cu);
7017 parent = real_pdi->die_parent;
7021 if (parent->scope_set)
7022 return parent->scope;
7024 fixup_partial_die (parent, cu);
7026 grandparent_scope = partial_die_parent_scope (parent, cu);
7028 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7029 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7030 Work around this problem here. */
7031 if (cu->language == language_cplus
7032 && parent->tag == DW_TAG_namespace
7033 && strcmp (parent->name, "::") == 0
7034 && grandparent_scope == NULL)
7036 parent->scope = NULL;
7037 parent->scope_set = 1;
7041 if (pdi->tag == DW_TAG_enumerator)
7042 /* Enumerators should not get the name of the enumeration as a prefix. */
7043 parent->scope = grandparent_scope;
7044 else if (parent->tag == DW_TAG_namespace
7045 || parent->tag == DW_TAG_module
7046 || parent->tag == DW_TAG_structure_type
7047 || parent->tag == DW_TAG_class_type
7048 || parent->tag == DW_TAG_interface_type
7049 || parent->tag == DW_TAG_union_type
7050 || parent->tag == DW_TAG_enumeration_type)
7052 if (grandparent_scope == NULL)
7053 parent->scope = parent->name;
7055 parent->scope = typename_concat (&cu->comp_unit_obstack,
7057 parent->name, 0, cu);
7061 /* FIXME drow/2004-04-01: What should we be doing with
7062 function-local names? For partial symbols, we should probably be
7064 complaint (&symfile_complaints,
7065 _("unhandled containing DIE tag %d for DIE at %d"),
7066 parent->tag, to_underlying (pdi->sect_off));
7067 parent->scope = grandparent_scope;
7070 parent->scope_set = 1;
7071 return parent->scope;
7074 /* Return the fully scoped name associated with PDI, from compilation unit
7075 CU. The result will be allocated with malloc. */
7078 partial_die_full_name (struct partial_die_info *pdi,
7079 struct dwarf2_cu *cu)
7081 const char *parent_scope;
7083 /* If this is a template instantiation, we can not work out the
7084 template arguments from partial DIEs. So, unfortunately, we have
7085 to go through the full DIEs. At least any work we do building
7086 types here will be reused if full symbols are loaded later. */
7087 if (pdi->has_template_arguments)
7089 fixup_partial_die (pdi, cu);
7091 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7093 struct die_info *die;
7094 struct attribute attr;
7095 struct dwarf2_cu *ref_cu = cu;
7097 /* DW_FORM_ref_addr is using section offset. */
7098 attr.name = (enum dwarf_attribute) 0;
7099 attr.form = DW_FORM_ref_addr;
7100 attr.u.unsnd = to_underlying (pdi->sect_off);
7101 die = follow_die_ref (NULL, &attr, &ref_cu);
7103 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7107 parent_scope = partial_die_parent_scope (pdi, cu);
7108 if (parent_scope == NULL)
7111 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7115 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7117 struct objfile *objfile = cu->objfile;
7118 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7120 const char *actual_name = NULL;
7122 char *built_actual_name;
7124 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7126 built_actual_name = partial_die_full_name (pdi, cu);
7127 if (built_actual_name != NULL)
7128 actual_name = built_actual_name;
7130 if (actual_name == NULL)
7131 actual_name = pdi->name;
7135 case DW_TAG_subprogram:
7136 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7137 if (pdi->is_external || cu->language == language_ada)
7139 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7140 of the global scope. But in Ada, we want to be able to access
7141 nested procedures globally. So all Ada subprograms are stored
7142 in the global scope. */
7143 add_psymbol_to_list (actual_name, strlen (actual_name),
7144 built_actual_name != NULL,
7145 VAR_DOMAIN, LOC_BLOCK,
7146 &objfile->global_psymbols,
7147 addr, cu->language, objfile);
7151 add_psymbol_to_list (actual_name, strlen (actual_name),
7152 built_actual_name != NULL,
7153 VAR_DOMAIN, LOC_BLOCK,
7154 &objfile->static_psymbols,
7155 addr, cu->language, objfile);
7158 if (pdi->main_subprogram && actual_name != NULL)
7159 set_objfile_main_name (objfile, actual_name, cu->language);
7161 case DW_TAG_constant:
7163 struct psymbol_allocation_list *list;
7165 if (pdi->is_external)
7166 list = &objfile->global_psymbols;
7168 list = &objfile->static_psymbols;
7169 add_psymbol_to_list (actual_name, strlen (actual_name),
7170 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7171 list, 0, cu->language, objfile);
7174 case DW_TAG_variable:
7176 addr = decode_locdesc (pdi->d.locdesc, cu);
7180 && !dwarf2_per_objfile->has_section_at_zero)
7182 /* A global or static variable may also have been stripped
7183 out by the linker if unused, in which case its address
7184 will be nullified; do not add such variables into partial
7185 symbol table then. */
7187 else if (pdi->is_external)
7190 Don't enter into the minimal symbol tables as there is
7191 a minimal symbol table entry from the ELF symbols already.
7192 Enter into partial symbol table if it has a location
7193 descriptor or a type.
7194 If the location descriptor is missing, new_symbol will create
7195 a LOC_UNRESOLVED symbol, the address of the variable will then
7196 be determined from the minimal symbol table whenever the variable
7198 The address for the partial symbol table entry is not
7199 used by GDB, but it comes in handy for debugging partial symbol
7202 if (pdi->d.locdesc || pdi->has_type)
7203 add_psymbol_to_list (actual_name, strlen (actual_name),
7204 built_actual_name != NULL,
7205 VAR_DOMAIN, LOC_STATIC,
7206 &objfile->global_psymbols,
7208 cu->language, objfile);
7212 int has_loc = pdi->d.locdesc != NULL;
7214 /* Static Variable. Skip symbols whose value we cannot know (those
7215 without location descriptors or constant values). */
7216 if (!has_loc && !pdi->has_const_value)
7218 xfree (built_actual_name);
7222 add_psymbol_to_list (actual_name, strlen (actual_name),
7223 built_actual_name != NULL,
7224 VAR_DOMAIN, LOC_STATIC,
7225 &objfile->static_psymbols,
7226 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7227 cu->language, objfile);
7230 case DW_TAG_typedef:
7231 case DW_TAG_base_type:
7232 case DW_TAG_subrange_type:
7233 add_psymbol_to_list (actual_name, strlen (actual_name),
7234 built_actual_name != NULL,
7235 VAR_DOMAIN, LOC_TYPEDEF,
7236 &objfile->static_psymbols,
7237 0, cu->language, objfile);
7239 case DW_TAG_imported_declaration:
7240 case DW_TAG_namespace:
7241 add_psymbol_to_list (actual_name, strlen (actual_name),
7242 built_actual_name != NULL,
7243 VAR_DOMAIN, LOC_TYPEDEF,
7244 &objfile->global_psymbols,
7245 0, cu->language, objfile);
7248 add_psymbol_to_list (actual_name, strlen (actual_name),
7249 built_actual_name != NULL,
7250 MODULE_DOMAIN, LOC_TYPEDEF,
7251 &objfile->global_psymbols,
7252 0, cu->language, objfile);
7254 case DW_TAG_class_type:
7255 case DW_TAG_interface_type:
7256 case DW_TAG_structure_type:
7257 case DW_TAG_union_type:
7258 case DW_TAG_enumeration_type:
7259 /* Skip external references. The DWARF standard says in the section
7260 about "Structure, Union, and Class Type Entries": "An incomplete
7261 structure, union or class type is represented by a structure,
7262 union or class entry that does not have a byte size attribute
7263 and that has a DW_AT_declaration attribute." */
7264 if (!pdi->has_byte_size && pdi->is_declaration)
7266 xfree (built_actual_name);
7270 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7271 static vs. global. */
7272 add_psymbol_to_list (actual_name, strlen (actual_name),
7273 built_actual_name != NULL,
7274 STRUCT_DOMAIN, LOC_TYPEDEF,
7275 cu->language == language_cplus
7276 ? &objfile->global_psymbols
7277 : &objfile->static_psymbols,
7278 0, cu->language, objfile);
7281 case DW_TAG_enumerator:
7282 add_psymbol_to_list (actual_name, strlen (actual_name),
7283 built_actual_name != NULL,
7284 VAR_DOMAIN, LOC_CONST,
7285 cu->language == language_cplus
7286 ? &objfile->global_psymbols
7287 : &objfile->static_psymbols,
7288 0, cu->language, objfile);
7294 xfree (built_actual_name);
7297 /* Read a partial die corresponding to a namespace; also, add a symbol
7298 corresponding to that namespace to the symbol table. NAMESPACE is
7299 the name of the enclosing namespace. */
7302 add_partial_namespace (struct partial_die_info *pdi,
7303 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7304 int set_addrmap, struct dwarf2_cu *cu)
7306 /* Add a symbol for the namespace. */
7308 add_partial_symbol (pdi, cu);
7310 /* Now scan partial symbols in that namespace. */
7312 if (pdi->has_children)
7313 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7316 /* Read a partial die corresponding to a Fortran module. */
7319 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7320 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7322 /* Add a symbol for the namespace. */
7324 add_partial_symbol (pdi, cu);
7326 /* Now scan partial symbols in that module. */
7328 if (pdi->has_children)
7329 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7332 /* Read a partial die corresponding to a subprogram and create a partial
7333 symbol for that subprogram. When the CU language allows it, this
7334 routine also defines a partial symbol for each nested subprogram
7335 that this subprogram contains. If SET_ADDRMAP is true, record the
7336 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7337 and highest PC values found in PDI.
7339 PDI may also be a lexical block, in which case we simply search
7340 recursively for subprograms defined inside that lexical block.
7341 Again, this is only performed when the CU language allows this
7342 type of definitions. */
7345 add_partial_subprogram (struct partial_die_info *pdi,
7346 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7347 int set_addrmap, struct dwarf2_cu *cu)
7349 if (pdi->tag == DW_TAG_subprogram)
7351 if (pdi->has_pc_info)
7353 if (pdi->lowpc < *lowpc)
7354 *lowpc = pdi->lowpc;
7355 if (pdi->highpc > *highpc)
7356 *highpc = pdi->highpc;
7359 struct objfile *objfile = cu->objfile;
7360 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7365 baseaddr = ANOFFSET (objfile->section_offsets,
7366 SECT_OFF_TEXT (objfile));
7367 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7368 pdi->lowpc + baseaddr);
7369 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7370 pdi->highpc + baseaddr);
7371 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7372 cu->per_cu->v.psymtab);
7376 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7378 if (!pdi->is_declaration)
7379 /* Ignore subprogram DIEs that do not have a name, they are
7380 illegal. Do not emit a complaint at this point, we will
7381 do so when we convert this psymtab into a symtab. */
7383 add_partial_symbol (pdi, cu);
7387 if (! pdi->has_children)
7390 if (cu->language == language_ada)
7392 pdi = pdi->die_child;
7395 fixup_partial_die (pdi, cu);
7396 if (pdi->tag == DW_TAG_subprogram
7397 || pdi->tag == DW_TAG_lexical_block)
7398 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7399 pdi = pdi->die_sibling;
7404 /* Read a partial die corresponding to an enumeration type. */
7407 add_partial_enumeration (struct partial_die_info *enum_pdi,
7408 struct dwarf2_cu *cu)
7410 struct partial_die_info *pdi;
7412 if (enum_pdi->name != NULL)
7413 add_partial_symbol (enum_pdi, cu);
7415 pdi = enum_pdi->die_child;
7418 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7419 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7421 add_partial_symbol (pdi, cu);
7422 pdi = pdi->die_sibling;
7426 /* Return the initial uleb128 in the die at INFO_PTR. */
7429 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7431 unsigned int bytes_read;
7433 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7436 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7437 Return the corresponding abbrev, or NULL if the number is zero (indicating
7438 an empty DIE). In either case *BYTES_READ will be set to the length of
7439 the initial number. */
7441 static struct abbrev_info *
7442 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7443 struct dwarf2_cu *cu)
7445 bfd *abfd = cu->objfile->obfd;
7446 unsigned int abbrev_number;
7447 struct abbrev_info *abbrev;
7449 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7451 if (abbrev_number == 0)
7454 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7457 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7458 " at offset 0x%x [in module %s]"),
7459 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7460 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
7466 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7467 Returns a pointer to the end of a series of DIEs, terminated by an empty
7468 DIE. Any children of the skipped DIEs will also be skipped. */
7470 static const gdb_byte *
7471 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7473 struct dwarf2_cu *cu = reader->cu;
7474 struct abbrev_info *abbrev;
7475 unsigned int bytes_read;
7479 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7481 return info_ptr + bytes_read;
7483 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7487 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7488 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7489 abbrev corresponding to that skipped uleb128 should be passed in
7490 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7493 static const gdb_byte *
7494 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7495 struct abbrev_info *abbrev)
7497 unsigned int bytes_read;
7498 struct attribute attr;
7499 bfd *abfd = reader->abfd;
7500 struct dwarf2_cu *cu = reader->cu;
7501 const gdb_byte *buffer = reader->buffer;
7502 const gdb_byte *buffer_end = reader->buffer_end;
7503 unsigned int form, i;
7505 for (i = 0; i < abbrev->num_attrs; i++)
7507 /* The only abbrev we care about is DW_AT_sibling. */
7508 if (abbrev->attrs[i].name == DW_AT_sibling)
7510 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7511 if (attr.form == DW_FORM_ref_addr)
7512 complaint (&symfile_complaints,
7513 _("ignoring absolute DW_AT_sibling"));
7516 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7517 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7519 if (sibling_ptr < info_ptr)
7520 complaint (&symfile_complaints,
7521 _("DW_AT_sibling points backwards"));
7522 else if (sibling_ptr > reader->buffer_end)
7523 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7529 /* If it isn't DW_AT_sibling, skip this attribute. */
7530 form = abbrev->attrs[i].form;
7534 case DW_FORM_ref_addr:
7535 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7536 and later it is offset sized. */
7537 if (cu->header.version == 2)
7538 info_ptr += cu->header.addr_size;
7540 info_ptr += cu->header.offset_size;
7542 case DW_FORM_GNU_ref_alt:
7543 info_ptr += cu->header.offset_size;
7546 info_ptr += cu->header.addr_size;
7553 case DW_FORM_flag_present:
7554 case DW_FORM_implicit_const:
7566 case DW_FORM_ref_sig8:
7569 case DW_FORM_data16:
7572 case DW_FORM_string:
7573 read_direct_string (abfd, info_ptr, &bytes_read);
7574 info_ptr += bytes_read;
7576 case DW_FORM_sec_offset:
7578 case DW_FORM_GNU_strp_alt:
7579 info_ptr += cu->header.offset_size;
7581 case DW_FORM_exprloc:
7583 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7584 info_ptr += bytes_read;
7586 case DW_FORM_block1:
7587 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7589 case DW_FORM_block2:
7590 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7592 case DW_FORM_block4:
7593 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7597 case DW_FORM_ref_udata:
7598 case DW_FORM_GNU_addr_index:
7599 case DW_FORM_GNU_str_index:
7600 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7602 case DW_FORM_indirect:
7603 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7604 info_ptr += bytes_read;
7605 /* We need to continue parsing from here, so just go back to
7607 goto skip_attribute;
7610 error (_("Dwarf Error: Cannot handle %s "
7611 "in DWARF reader [in module %s]"),
7612 dwarf_form_name (form),
7613 bfd_get_filename (abfd));
7617 if (abbrev->has_children)
7618 return skip_children (reader, info_ptr);
7623 /* Locate ORIG_PDI's sibling.
7624 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7626 static const gdb_byte *
7627 locate_pdi_sibling (const struct die_reader_specs *reader,
7628 struct partial_die_info *orig_pdi,
7629 const gdb_byte *info_ptr)
7631 /* Do we know the sibling already? */
7633 if (orig_pdi->sibling)
7634 return orig_pdi->sibling;
7636 /* Are there any children to deal with? */
7638 if (!orig_pdi->has_children)
7641 /* Skip the children the long way. */
7643 return skip_children (reader, info_ptr);
7646 /* Expand this partial symbol table into a full symbol table. SELF is
7650 dwarf2_read_symtab (struct partial_symtab *self,
7651 struct objfile *objfile)
7655 warning (_("bug: psymtab for %s is already read in."),
7662 printf_filtered (_("Reading in symbols for %s..."),
7664 gdb_flush (gdb_stdout);
7667 /* Restore our global data. */
7669 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7670 dwarf2_objfile_data_key);
7672 /* If this psymtab is constructed from a debug-only objfile, the
7673 has_section_at_zero flag will not necessarily be correct. We
7674 can get the correct value for this flag by looking at the data
7675 associated with the (presumably stripped) associated objfile. */
7676 if (objfile->separate_debug_objfile_backlink)
7678 struct dwarf2_per_objfile *dpo_backlink
7679 = ((struct dwarf2_per_objfile *)
7680 objfile_data (objfile->separate_debug_objfile_backlink,
7681 dwarf2_objfile_data_key));
7683 dwarf2_per_objfile->has_section_at_zero
7684 = dpo_backlink->has_section_at_zero;
7687 dwarf2_per_objfile->reading_partial_symbols = 0;
7689 psymtab_to_symtab_1 (self);
7691 /* Finish up the debug error message. */
7693 printf_filtered (_("done.\n"));
7696 process_cu_includes ();
7699 /* Reading in full CUs. */
7701 /* Add PER_CU to the queue. */
7704 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7705 enum language pretend_language)
7707 struct dwarf2_queue_item *item;
7710 item = XNEW (struct dwarf2_queue_item);
7711 item->per_cu = per_cu;
7712 item->pretend_language = pretend_language;
7715 if (dwarf2_queue == NULL)
7716 dwarf2_queue = item;
7718 dwarf2_queue_tail->next = item;
7720 dwarf2_queue_tail = item;
7723 /* If PER_CU is not yet queued, add it to the queue.
7724 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7726 The result is non-zero if PER_CU was queued, otherwise the result is zero
7727 meaning either PER_CU is already queued or it is already loaded.
7729 N.B. There is an invariant here that if a CU is queued then it is loaded.
7730 The caller is required to load PER_CU if we return non-zero. */
7733 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7734 struct dwarf2_per_cu_data *per_cu,
7735 enum language pretend_language)
7737 /* We may arrive here during partial symbol reading, if we need full
7738 DIEs to process an unusual case (e.g. template arguments). Do
7739 not queue PER_CU, just tell our caller to load its DIEs. */
7740 if (dwarf2_per_objfile->reading_partial_symbols)
7742 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7747 /* Mark the dependence relation so that we don't flush PER_CU
7749 if (dependent_cu != NULL)
7750 dwarf2_add_dependence (dependent_cu, per_cu);
7752 /* If it's already on the queue, we have nothing to do. */
7756 /* If the compilation unit is already loaded, just mark it as
7758 if (per_cu->cu != NULL)
7760 per_cu->cu->last_used = 0;
7764 /* Add it to the queue. */
7765 queue_comp_unit (per_cu, pretend_language);
7770 /* Process the queue. */
7773 process_queue (void)
7775 struct dwarf2_queue_item *item, *next_item;
7777 if (dwarf_read_debug)
7779 fprintf_unfiltered (gdb_stdlog,
7780 "Expanding one or more symtabs of objfile %s ...\n",
7781 objfile_name (dwarf2_per_objfile->objfile));
7784 /* The queue starts out with one item, but following a DIE reference
7785 may load a new CU, adding it to the end of the queue. */
7786 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7788 if ((dwarf2_per_objfile->using_index
7789 ? !item->per_cu->v.quick->compunit_symtab
7790 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7791 /* Skip dummy CUs. */
7792 && item->per_cu->cu != NULL)
7794 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7795 unsigned int debug_print_threshold;
7798 if (per_cu->is_debug_types)
7800 struct signatured_type *sig_type =
7801 (struct signatured_type *) per_cu;
7803 sprintf (buf, "TU %s at offset 0x%x",
7804 hex_string (sig_type->signature),
7805 to_underlying (per_cu->sect_off));
7806 /* There can be 100s of TUs.
7807 Only print them in verbose mode. */
7808 debug_print_threshold = 2;
7812 sprintf (buf, "CU at offset 0x%x",
7813 to_underlying (per_cu->sect_off));
7814 debug_print_threshold = 1;
7817 if (dwarf_read_debug >= debug_print_threshold)
7818 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7820 if (per_cu->is_debug_types)
7821 process_full_type_unit (per_cu, item->pretend_language);
7823 process_full_comp_unit (per_cu, item->pretend_language);
7825 if (dwarf_read_debug >= debug_print_threshold)
7826 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7829 item->per_cu->queued = 0;
7830 next_item = item->next;
7834 dwarf2_queue_tail = NULL;
7836 if (dwarf_read_debug)
7838 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7839 objfile_name (dwarf2_per_objfile->objfile));
7843 /* Free all allocated queue entries. This function only releases anything if
7844 an error was thrown; if the queue was processed then it would have been
7845 freed as we went along. */
7848 dwarf2_release_queue (void *dummy)
7850 struct dwarf2_queue_item *item, *last;
7852 item = dwarf2_queue;
7855 /* Anything still marked queued is likely to be in an
7856 inconsistent state, so discard it. */
7857 if (item->per_cu->queued)
7859 if (item->per_cu->cu != NULL)
7860 free_one_cached_comp_unit (item->per_cu);
7861 item->per_cu->queued = 0;
7869 dwarf2_queue = dwarf2_queue_tail = NULL;
7872 /* Read in full symbols for PST, and anything it depends on. */
7875 psymtab_to_symtab_1 (struct partial_symtab *pst)
7877 struct dwarf2_per_cu_data *per_cu;
7883 for (i = 0; i < pst->number_of_dependencies; i++)
7884 if (!pst->dependencies[i]->readin
7885 && pst->dependencies[i]->user == NULL)
7887 /* Inform about additional files that need to be read in. */
7890 /* FIXME: i18n: Need to make this a single string. */
7891 fputs_filtered (" ", gdb_stdout);
7893 fputs_filtered ("and ", gdb_stdout);
7895 printf_filtered ("%s...", pst->dependencies[i]->filename);
7896 wrap_here (""); /* Flush output. */
7897 gdb_flush (gdb_stdout);
7899 psymtab_to_symtab_1 (pst->dependencies[i]);
7902 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7906 /* It's an include file, no symbols to read for it.
7907 Everything is in the parent symtab. */
7912 dw2_do_instantiate_symtab (per_cu);
7915 /* Trivial hash function for die_info: the hash value of a DIE
7916 is its offset in .debug_info for this objfile. */
7919 die_hash (const void *item)
7921 const struct die_info *die = (const struct die_info *) item;
7923 return to_underlying (die->sect_off);
7926 /* Trivial comparison function for die_info structures: two DIEs
7927 are equal if they have the same offset. */
7930 die_eq (const void *item_lhs, const void *item_rhs)
7932 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7933 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7935 return die_lhs->sect_off == die_rhs->sect_off;
7938 /* die_reader_func for load_full_comp_unit.
7939 This is identical to read_signatured_type_reader,
7940 but is kept separate for now. */
7943 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7944 const gdb_byte *info_ptr,
7945 struct die_info *comp_unit_die,
7949 struct dwarf2_cu *cu = reader->cu;
7950 enum language *language_ptr = (enum language *) data;
7952 gdb_assert (cu->die_hash == NULL);
7954 htab_create_alloc_ex (cu->header.length / 12,
7958 &cu->comp_unit_obstack,
7959 hashtab_obstack_allocate,
7960 dummy_obstack_deallocate);
7963 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7964 &info_ptr, comp_unit_die);
7965 cu->dies = comp_unit_die;
7966 /* comp_unit_die is not stored in die_hash, no need. */
7968 /* We try not to read any attributes in this function, because not
7969 all CUs needed for references have been loaded yet, and symbol
7970 table processing isn't initialized. But we have to set the CU language,
7971 or we won't be able to build types correctly.
7972 Similarly, if we do not read the producer, we can not apply
7973 producer-specific interpretation. */
7974 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7977 /* Load the DIEs associated with PER_CU into memory. */
7980 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7981 enum language pretend_language)
7983 gdb_assert (! this_cu->is_debug_types);
7985 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7986 load_full_comp_unit_reader, &pretend_language);
7989 /* Add a DIE to the delayed physname list. */
7992 add_to_method_list (struct type *type, int fnfield_index, int index,
7993 const char *name, struct die_info *die,
7994 struct dwarf2_cu *cu)
7996 struct delayed_method_info mi;
7998 mi.fnfield_index = fnfield_index;
8002 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8005 /* A cleanup for freeing the delayed method list. */
8008 free_delayed_list (void *ptr)
8010 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8011 if (cu->method_list != NULL)
8013 VEC_free (delayed_method_info, cu->method_list);
8014 cu->method_list = NULL;
8018 /* Compute the physnames of any methods on the CU's method list.
8020 The computation of method physnames is delayed in order to avoid the
8021 (bad) condition that one of the method's formal parameters is of an as yet
8025 compute_delayed_physnames (struct dwarf2_cu *cu)
8028 struct delayed_method_info *mi;
8029 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8031 const char *physname;
8032 struct fn_fieldlist *fn_flp
8033 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8034 physname = dwarf2_physname (mi->name, mi->die, cu);
8035 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8036 = physname ? physname : "";
8040 /* Go objects should be embedded in a DW_TAG_module DIE,
8041 and it's not clear if/how imported objects will appear.
8042 To keep Go support simple until that's worked out,
8043 go back through what we've read and create something usable.
8044 We could do this while processing each DIE, and feels kinda cleaner,
8045 but that way is more invasive.
8046 This is to, for example, allow the user to type "p var" or "b main"
8047 without having to specify the package name, and allow lookups
8048 of module.object to work in contexts that use the expression
8052 fixup_go_packaging (struct dwarf2_cu *cu)
8054 char *package_name = NULL;
8055 struct pending *list;
8058 for (list = global_symbols; list != NULL; list = list->next)
8060 for (i = 0; i < list->nsyms; ++i)
8062 struct symbol *sym = list->symbol[i];
8064 if (SYMBOL_LANGUAGE (sym) == language_go
8065 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8067 char *this_package_name = go_symbol_package_name (sym);
8069 if (this_package_name == NULL)
8071 if (package_name == NULL)
8072 package_name = this_package_name;
8075 if (strcmp (package_name, this_package_name) != 0)
8076 complaint (&symfile_complaints,
8077 _("Symtab %s has objects from two different Go packages: %s and %s"),
8078 (symbol_symtab (sym) != NULL
8079 ? symtab_to_filename_for_display
8080 (symbol_symtab (sym))
8081 : objfile_name (cu->objfile)),
8082 this_package_name, package_name);
8083 xfree (this_package_name);
8089 if (package_name != NULL)
8091 struct objfile *objfile = cu->objfile;
8092 const char *saved_package_name
8093 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8095 strlen (package_name));
8096 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8097 saved_package_name);
8100 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8102 sym = allocate_symbol (objfile);
8103 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8104 SYMBOL_SET_NAMES (sym, saved_package_name,
8105 strlen (saved_package_name), 0, objfile);
8106 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8107 e.g., "main" finds the "main" module and not C's main(). */
8108 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8109 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8110 SYMBOL_TYPE (sym) = type;
8112 add_symbol_to_list (sym, &global_symbols);
8114 xfree (package_name);
8118 /* Return the symtab for PER_CU. This works properly regardless of
8119 whether we're using the index or psymtabs. */
8121 static struct compunit_symtab *
8122 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8124 return (dwarf2_per_objfile->using_index
8125 ? per_cu->v.quick->compunit_symtab
8126 : per_cu->v.psymtab->compunit_symtab);
8129 /* A helper function for computing the list of all symbol tables
8130 included by PER_CU. */
8133 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8134 htab_t all_children, htab_t all_type_symtabs,
8135 struct dwarf2_per_cu_data *per_cu,
8136 struct compunit_symtab *immediate_parent)
8140 struct compunit_symtab *cust;
8141 struct dwarf2_per_cu_data *iter;
8143 slot = htab_find_slot (all_children, per_cu, INSERT);
8146 /* This inclusion and its children have been processed. */
8151 /* Only add a CU if it has a symbol table. */
8152 cust = get_compunit_symtab (per_cu);
8155 /* If this is a type unit only add its symbol table if we haven't
8156 seen it yet (type unit per_cu's can share symtabs). */
8157 if (per_cu->is_debug_types)
8159 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8163 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8164 if (cust->user == NULL)
8165 cust->user = immediate_parent;
8170 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8171 if (cust->user == NULL)
8172 cust->user = immediate_parent;
8177 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8180 recursively_compute_inclusions (result, all_children,
8181 all_type_symtabs, iter, cust);
8185 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8189 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8191 gdb_assert (! per_cu->is_debug_types);
8193 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8196 struct dwarf2_per_cu_data *per_cu_iter;
8197 struct compunit_symtab *compunit_symtab_iter;
8198 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8199 htab_t all_children, all_type_symtabs;
8200 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8202 /* If we don't have a symtab, we can just skip this case. */
8206 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8207 NULL, xcalloc, xfree);
8208 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8209 NULL, xcalloc, xfree);
8212 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8216 recursively_compute_inclusions (&result_symtabs, all_children,
8217 all_type_symtabs, per_cu_iter,
8221 /* Now we have a transitive closure of all the included symtabs. */
8222 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8224 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8225 struct compunit_symtab *, len + 1);
8227 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8228 compunit_symtab_iter);
8230 cust->includes[ix] = compunit_symtab_iter;
8231 cust->includes[len] = NULL;
8233 VEC_free (compunit_symtab_ptr, result_symtabs);
8234 htab_delete (all_children);
8235 htab_delete (all_type_symtabs);
8239 /* Compute the 'includes' field for the symtabs of all the CUs we just
8243 process_cu_includes (void)
8246 struct dwarf2_per_cu_data *iter;
8249 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8253 if (! iter->is_debug_types)
8254 compute_compunit_symtab_includes (iter);
8257 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8260 /* Generate full symbol information for PER_CU, whose DIEs have
8261 already been loaded into memory. */
8264 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8265 enum language pretend_language)
8267 struct dwarf2_cu *cu = per_cu->cu;
8268 struct objfile *objfile = per_cu->objfile;
8269 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8270 CORE_ADDR lowpc, highpc;
8271 struct compunit_symtab *cust;
8272 struct cleanup *back_to, *delayed_list_cleanup;
8274 struct block *static_block;
8277 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8280 back_to = make_cleanup (really_free_pendings, NULL);
8281 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8283 cu->list_in_scope = &file_symbols;
8285 cu->language = pretend_language;
8286 cu->language_defn = language_def (cu->language);
8288 /* Do line number decoding in read_file_scope () */
8289 process_die (cu->dies, cu);
8291 /* For now fudge the Go package. */
8292 if (cu->language == language_go)
8293 fixup_go_packaging (cu);
8295 /* Now that we have processed all the DIEs in the CU, all the types
8296 should be complete, and it should now be safe to compute all of the
8298 compute_delayed_physnames (cu);
8299 do_cleanups (delayed_list_cleanup);
8301 /* Some compilers don't define a DW_AT_high_pc attribute for the
8302 compilation unit. If the DW_AT_high_pc is missing, synthesize
8303 it, by scanning the DIE's below the compilation unit. */
8304 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8306 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8307 static_block = end_symtab_get_static_block (addr, 0, 1);
8309 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8310 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8311 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8312 addrmap to help ensure it has an accurate map of pc values belonging to
8314 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8316 cust = end_symtab_from_static_block (static_block,
8317 SECT_OFF_TEXT (objfile), 0);
8321 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8323 /* Set symtab language to language from DW_AT_language. If the
8324 compilation is from a C file generated by language preprocessors, do
8325 not set the language if it was already deduced by start_subfile. */
8326 if (!(cu->language == language_c
8327 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8328 COMPUNIT_FILETABS (cust)->language = cu->language;
8330 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8331 produce DW_AT_location with location lists but it can be possibly
8332 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8333 there were bugs in prologue debug info, fixed later in GCC-4.5
8334 by "unwind info for epilogues" patch (which is not directly related).
8336 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8337 needed, it would be wrong due to missing DW_AT_producer there.
8339 Still one can confuse GDB by using non-standard GCC compilation
8340 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8342 if (cu->has_loclist && gcc_4_minor >= 5)
8343 cust->locations_valid = 1;
8345 if (gcc_4_minor >= 5)
8346 cust->epilogue_unwind_valid = 1;
8348 cust->call_site_htab = cu->call_site_htab;
8351 if (dwarf2_per_objfile->using_index)
8352 per_cu->v.quick->compunit_symtab = cust;
8355 struct partial_symtab *pst = per_cu->v.psymtab;
8356 pst->compunit_symtab = cust;
8360 /* Push it for inclusion processing later. */
8361 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8363 do_cleanups (back_to);
8366 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8367 already been loaded into memory. */
8370 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8371 enum language pretend_language)
8373 struct dwarf2_cu *cu = per_cu->cu;
8374 struct objfile *objfile = per_cu->objfile;
8375 struct compunit_symtab *cust;
8376 struct cleanup *back_to, *delayed_list_cleanup;
8377 struct signatured_type *sig_type;
8379 gdb_assert (per_cu->is_debug_types);
8380 sig_type = (struct signatured_type *) per_cu;
8383 back_to = make_cleanup (really_free_pendings, NULL);
8384 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8386 cu->list_in_scope = &file_symbols;
8388 cu->language = pretend_language;
8389 cu->language_defn = language_def (cu->language);
8391 /* The symbol tables are set up in read_type_unit_scope. */
8392 process_die (cu->dies, cu);
8394 /* For now fudge the Go package. */
8395 if (cu->language == language_go)
8396 fixup_go_packaging (cu);
8398 /* Now that we have processed all the DIEs in the CU, all the types
8399 should be complete, and it should now be safe to compute all of the
8401 compute_delayed_physnames (cu);
8402 do_cleanups (delayed_list_cleanup);
8404 /* TUs share symbol tables.
8405 If this is the first TU to use this symtab, complete the construction
8406 of it with end_expandable_symtab. Otherwise, complete the addition of
8407 this TU's symbols to the existing symtab. */
8408 if (sig_type->type_unit_group->compunit_symtab == NULL)
8410 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8411 sig_type->type_unit_group->compunit_symtab = cust;
8415 /* Set symtab language to language from DW_AT_language. If the
8416 compilation is from a C file generated by language preprocessors,
8417 do not set the language if it was already deduced by
8419 if (!(cu->language == language_c
8420 && COMPUNIT_FILETABS (cust)->language != language_c))
8421 COMPUNIT_FILETABS (cust)->language = cu->language;
8426 augment_type_symtab ();
8427 cust = sig_type->type_unit_group->compunit_symtab;
8430 if (dwarf2_per_objfile->using_index)
8431 per_cu->v.quick->compunit_symtab = cust;
8434 struct partial_symtab *pst = per_cu->v.psymtab;
8435 pst->compunit_symtab = cust;
8439 do_cleanups (back_to);
8442 /* Process an imported unit DIE. */
8445 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8447 struct attribute *attr;
8449 /* For now we don't handle imported units in type units. */
8450 if (cu->per_cu->is_debug_types)
8452 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8453 " supported in type units [in module %s]"),
8454 objfile_name (cu->objfile));
8457 attr = dwarf2_attr (die, DW_AT_import, cu);
8460 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8461 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8462 dwarf2_per_cu_data *per_cu
8463 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
8465 /* If necessary, add it to the queue and load its DIEs. */
8466 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8467 load_full_comp_unit (per_cu, cu->language);
8469 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8474 /* Reset the in_process bit of a die. */
8477 reset_die_in_process (void *arg)
8479 struct die_info *die = (struct die_info *) arg;
8481 die->in_process = 0;
8484 /* Process a die and its children. */
8487 process_die (struct die_info *die, struct dwarf2_cu *cu)
8489 struct cleanup *in_process;
8491 /* We should only be processing those not already in process. */
8492 gdb_assert (!die->in_process);
8494 die->in_process = 1;
8495 in_process = make_cleanup (reset_die_in_process,die);
8499 case DW_TAG_padding:
8501 case DW_TAG_compile_unit:
8502 case DW_TAG_partial_unit:
8503 read_file_scope (die, cu);
8505 case DW_TAG_type_unit:
8506 read_type_unit_scope (die, cu);
8508 case DW_TAG_subprogram:
8509 case DW_TAG_inlined_subroutine:
8510 read_func_scope (die, cu);
8512 case DW_TAG_lexical_block:
8513 case DW_TAG_try_block:
8514 case DW_TAG_catch_block:
8515 read_lexical_block_scope (die, cu);
8517 case DW_TAG_call_site:
8518 case DW_TAG_GNU_call_site:
8519 read_call_site_scope (die, cu);
8521 case DW_TAG_class_type:
8522 case DW_TAG_interface_type:
8523 case DW_TAG_structure_type:
8524 case DW_TAG_union_type:
8525 process_structure_scope (die, cu);
8527 case DW_TAG_enumeration_type:
8528 process_enumeration_scope (die, cu);
8531 /* These dies have a type, but processing them does not create
8532 a symbol or recurse to process the children. Therefore we can
8533 read them on-demand through read_type_die. */
8534 case DW_TAG_subroutine_type:
8535 case DW_TAG_set_type:
8536 case DW_TAG_array_type:
8537 case DW_TAG_pointer_type:
8538 case DW_TAG_ptr_to_member_type:
8539 case DW_TAG_reference_type:
8540 case DW_TAG_rvalue_reference_type:
8541 case DW_TAG_string_type:
8544 case DW_TAG_base_type:
8545 case DW_TAG_subrange_type:
8546 case DW_TAG_typedef:
8547 /* Add a typedef symbol for the type definition, if it has a
8549 new_symbol (die, read_type_die (die, cu), cu);
8551 case DW_TAG_common_block:
8552 read_common_block (die, cu);
8554 case DW_TAG_common_inclusion:
8556 case DW_TAG_namespace:
8557 cu->processing_has_namespace_info = 1;
8558 read_namespace (die, cu);
8561 cu->processing_has_namespace_info = 1;
8562 read_module (die, cu);
8564 case DW_TAG_imported_declaration:
8565 cu->processing_has_namespace_info = 1;
8566 if (read_namespace_alias (die, cu))
8568 /* The declaration is not a global namespace alias: fall through. */
8569 case DW_TAG_imported_module:
8570 cu->processing_has_namespace_info = 1;
8571 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8572 || cu->language != language_fortran))
8573 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8574 dwarf_tag_name (die->tag));
8575 read_import_statement (die, cu);
8578 case DW_TAG_imported_unit:
8579 process_imported_unit_die (die, cu);
8583 new_symbol (die, NULL, cu);
8587 do_cleanups (in_process);
8590 /* DWARF name computation. */
8592 /* A helper function for dwarf2_compute_name which determines whether DIE
8593 needs to have the name of the scope prepended to the name listed in the
8597 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8599 struct attribute *attr;
8603 case DW_TAG_namespace:
8604 case DW_TAG_typedef:
8605 case DW_TAG_class_type:
8606 case DW_TAG_interface_type:
8607 case DW_TAG_structure_type:
8608 case DW_TAG_union_type:
8609 case DW_TAG_enumeration_type:
8610 case DW_TAG_enumerator:
8611 case DW_TAG_subprogram:
8612 case DW_TAG_inlined_subroutine:
8614 case DW_TAG_imported_declaration:
8617 case DW_TAG_variable:
8618 case DW_TAG_constant:
8619 /* We only need to prefix "globally" visible variables. These include
8620 any variable marked with DW_AT_external or any variable that
8621 lives in a namespace. [Variables in anonymous namespaces
8622 require prefixing, but they are not DW_AT_external.] */
8624 if (dwarf2_attr (die, DW_AT_specification, cu))
8626 struct dwarf2_cu *spec_cu = cu;
8628 return die_needs_namespace (die_specification (die, &spec_cu),
8632 attr = dwarf2_attr (die, DW_AT_external, cu);
8633 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8634 && die->parent->tag != DW_TAG_module)
8636 /* A variable in a lexical block of some kind does not need a
8637 namespace, even though in C++ such variables may be external
8638 and have a mangled name. */
8639 if (die->parent->tag == DW_TAG_lexical_block
8640 || die->parent->tag == DW_TAG_try_block
8641 || die->parent->tag == DW_TAG_catch_block
8642 || die->parent->tag == DW_TAG_subprogram)
8651 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8652 compute the physname for the object, which include a method's:
8653 - formal parameters (C++),
8654 - receiver type (Go),
8656 The term "physname" is a bit confusing.
8657 For C++, for example, it is the demangled name.
8658 For Go, for example, it's the mangled name.
8660 For Ada, return the DIE's linkage name rather than the fully qualified
8661 name. PHYSNAME is ignored..
8663 The result is allocated on the objfile_obstack and canonicalized. */
8666 dwarf2_compute_name (const char *name,
8667 struct die_info *die, struct dwarf2_cu *cu,
8670 struct objfile *objfile = cu->objfile;
8673 name = dwarf2_name (die, cu);
8675 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8676 but otherwise compute it by typename_concat inside GDB.
8677 FIXME: Actually this is not really true, or at least not always true.
8678 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8679 Fortran names because there is no mangling standard. So new_symbol_full
8680 will set the demangled name to the result of dwarf2_full_name, and it is
8681 the demangled name that GDB uses if it exists. */
8682 if (cu->language == language_ada
8683 || (cu->language == language_fortran && physname))
8685 /* For Ada unit, we prefer the linkage name over the name, as
8686 the former contains the exported name, which the user expects
8687 to be able to reference. Ideally, we want the user to be able
8688 to reference this entity using either natural or linkage name,
8689 but we haven't started looking at this enhancement yet. */
8690 const char *linkage_name;
8692 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8693 if (linkage_name == NULL)
8694 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8695 if (linkage_name != NULL)
8696 return linkage_name;
8699 /* These are the only languages we know how to qualify names in. */
8701 && (cu->language == language_cplus
8702 || cu->language == language_fortran || cu->language == language_d
8703 || cu->language == language_rust))
8705 if (die_needs_namespace (die, cu))
8709 const char *canonical_name = NULL;
8713 prefix = determine_prefix (die, cu);
8714 if (*prefix != '\0')
8716 char *prefixed_name = typename_concat (NULL, prefix, name,
8719 buf.puts (prefixed_name);
8720 xfree (prefixed_name);
8725 /* Template parameters may be specified in the DIE's DW_AT_name, or
8726 as children with DW_TAG_template_type_param or
8727 DW_TAG_value_type_param. If the latter, add them to the name
8728 here. If the name already has template parameters, then
8729 skip this step; some versions of GCC emit both, and
8730 it is more efficient to use the pre-computed name.
8732 Something to keep in mind about this process: it is very
8733 unlikely, or in some cases downright impossible, to produce
8734 something that will match the mangled name of a function.
8735 If the definition of the function has the same debug info,
8736 we should be able to match up with it anyway. But fallbacks
8737 using the minimal symbol, for instance to find a method
8738 implemented in a stripped copy of libstdc++, will not work.
8739 If we do not have debug info for the definition, we will have to
8740 match them up some other way.
8742 When we do name matching there is a related problem with function
8743 templates; two instantiated function templates are allowed to
8744 differ only by their return types, which we do not add here. */
8746 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8748 struct attribute *attr;
8749 struct die_info *child;
8752 die->building_fullname = 1;
8754 for (child = die->child; child != NULL; child = child->sibling)
8758 const gdb_byte *bytes;
8759 struct dwarf2_locexpr_baton *baton;
8762 if (child->tag != DW_TAG_template_type_param
8763 && child->tag != DW_TAG_template_value_param)
8774 attr = dwarf2_attr (child, DW_AT_type, cu);
8777 complaint (&symfile_complaints,
8778 _("template parameter missing DW_AT_type"));
8779 buf.puts ("UNKNOWN_TYPE");
8782 type = die_type (child, cu);
8784 if (child->tag == DW_TAG_template_type_param)
8786 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8790 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8793 complaint (&symfile_complaints,
8794 _("template parameter missing "
8795 "DW_AT_const_value"));
8796 buf.puts ("UNKNOWN_VALUE");
8800 dwarf2_const_value_attr (attr, type, name,
8801 &cu->comp_unit_obstack, cu,
8802 &value, &bytes, &baton);
8804 if (TYPE_NOSIGN (type))
8805 /* GDB prints characters as NUMBER 'CHAR'. If that's
8806 changed, this can use value_print instead. */
8807 c_printchar (value, type, &buf);
8810 struct value_print_options opts;
8813 v = dwarf2_evaluate_loc_desc (type, NULL,
8817 else if (bytes != NULL)
8819 v = allocate_value (type);
8820 memcpy (value_contents_writeable (v), bytes,
8821 TYPE_LENGTH (type));
8824 v = value_from_longest (type, value);
8826 /* Specify decimal so that we do not depend on
8828 get_formatted_print_options (&opts, 'd');
8830 value_print (v, &buf, &opts);
8836 die->building_fullname = 0;
8840 /* Close the argument list, with a space if necessary
8841 (nested templates). */
8842 if (!buf.empty () && buf.string ().back () == '>')
8849 /* For C++ methods, append formal parameter type
8850 information, if PHYSNAME. */
8852 if (physname && die->tag == DW_TAG_subprogram
8853 && cu->language == language_cplus)
8855 struct type *type = read_type_die (die, cu);
8857 c_type_print_args (type, &buf, 1, cu->language,
8858 &type_print_raw_options);
8860 if (cu->language == language_cplus)
8862 /* Assume that an artificial first parameter is
8863 "this", but do not crash if it is not. RealView
8864 marks unnamed (and thus unused) parameters as
8865 artificial; there is no way to differentiate
8867 if (TYPE_NFIELDS (type) > 0
8868 && TYPE_FIELD_ARTIFICIAL (type, 0)
8869 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8870 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8872 buf.puts (" const");
8876 const std::string &intermediate_name = buf.string ();
8878 if (cu->language == language_cplus)
8880 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8881 &objfile->per_bfd->storage_obstack);
8883 /* If we only computed INTERMEDIATE_NAME, or if
8884 INTERMEDIATE_NAME is already canonical, then we need to
8885 copy it to the appropriate obstack. */
8886 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8887 name = ((const char *)
8888 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8889 intermediate_name.c_str (),
8890 intermediate_name.length ()));
8892 name = canonical_name;
8899 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8900 If scope qualifiers are appropriate they will be added. The result
8901 will be allocated on the storage_obstack, or NULL if the DIE does
8902 not have a name. NAME may either be from a previous call to
8903 dwarf2_name or NULL.
8905 The output string will be canonicalized (if C++). */
8908 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8910 return dwarf2_compute_name (name, die, cu, 0);
8913 /* Construct a physname for the given DIE in CU. NAME may either be
8914 from a previous call to dwarf2_name or NULL. The result will be
8915 allocated on the objfile_objstack or NULL if the DIE does not have a
8918 The output string will be canonicalized (if C++). */
8921 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8923 struct objfile *objfile = cu->objfile;
8924 const char *retval, *mangled = NULL, *canon = NULL;
8925 struct cleanup *back_to;
8928 /* In this case dwarf2_compute_name is just a shortcut not building anything
8930 if (!die_needs_namespace (die, cu))
8931 return dwarf2_compute_name (name, die, cu, 1);
8933 back_to = make_cleanup (null_cleanup, NULL);
8935 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8936 if (mangled == NULL)
8937 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8939 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8940 See https://github.com/rust-lang/rust/issues/32925. */
8941 if (cu->language == language_rust && mangled != NULL
8942 && strchr (mangled, '{') != NULL)
8945 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8947 if (mangled != NULL)
8951 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8952 type. It is easier for GDB users to search for such functions as
8953 `name(params)' than `long name(params)'. In such case the minimal
8954 symbol names do not match the full symbol names but for template
8955 functions there is never a need to look up their definition from their
8956 declaration so the only disadvantage remains the minimal symbol
8957 variant `long name(params)' does not have the proper inferior type.
8960 if (cu->language == language_go)
8962 /* This is a lie, but we already lie to the caller new_symbol_full.
8963 new_symbol_full assumes we return the mangled name.
8964 This just undoes that lie until things are cleaned up. */
8969 demangled = gdb_demangle (mangled,
8970 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8974 make_cleanup (xfree, demangled);
8984 if (canon == NULL || check_physname)
8986 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8988 if (canon != NULL && strcmp (physname, canon) != 0)
8990 /* It may not mean a bug in GDB. The compiler could also
8991 compute DW_AT_linkage_name incorrectly. But in such case
8992 GDB would need to be bug-to-bug compatible. */
8994 complaint (&symfile_complaints,
8995 _("Computed physname <%s> does not match demangled <%s> "
8996 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8997 physname, canon, mangled, to_underlying (die->sect_off),
8998 objfile_name (objfile));
9000 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9001 is available here - over computed PHYSNAME. It is safer
9002 against both buggy GDB and buggy compilers. */
9016 retval = ((const char *)
9017 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9018 retval, strlen (retval)));
9020 do_cleanups (back_to);
9024 /* Inspect DIE in CU for a namespace alias. If one exists, record
9025 a new symbol for it.
9027 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9030 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9032 struct attribute *attr;
9034 /* If the die does not have a name, this is not a namespace
9036 attr = dwarf2_attr (die, DW_AT_name, cu);
9040 struct die_info *d = die;
9041 struct dwarf2_cu *imported_cu = cu;
9043 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9044 keep inspecting DIEs until we hit the underlying import. */
9045 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9046 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9048 attr = dwarf2_attr (d, DW_AT_import, cu);
9052 d = follow_die_ref (d, attr, &imported_cu);
9053 if (d->tag != DW_TAG_imported_declaration)
9057 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9059 complaint (&symfile_complaints,
9060 _("DIE at 0x%x has too many recursively imported "
9061 "declarations"), to_underlying (d->sect_off));
9068 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9070 type = get_die_type_at_offset (sect_off, cu->per_cu);
9071 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9073 /* This declaration is a global namespace alias. Add
9074 a symbol for it whose type is the aliased namespace. */
9075 new_symbol (die, type, cu);
9084 /* Return the using directives repository (global or local?) to use in the
9085 current context for LANGUAGE.
9087 For Ada, imported declarations can materialize renamings, which *may* be
9088 global. However it is impossible (for now?) in DWARF to distinguish
9089 "external" imported declarations and "static" ones. As all imported
9090 declarations seem to be static in all other languages, make them all CU-wide
9091 global only in Ada. */
9093 static struct using_direct **
9094 using_directives (enum language language)
9096 if (language == language_ada && context_stack_depth == 0)
9097 return &global_using_directives;
9099 return &local_using_directives;
9102 /* Read the import statement specified by the given die and record it. */
9105 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9107 struct objfile *objfile = cu->objfile;
9108 struct attribute *import_attr;
9109 struct die_info *imported_die, *child_die;
9110 struct dwarf2_cu *imported_cu;
9111 const char *imported_name;
9112 const char *imported_name_prefix;
9113 const char *canonical_name;
9114 const char *import_alias;
9115 const char *imported_declaration = NULL;
9116 const char *import_prefix;
9117 VEC (const_char_ptr) *excludes = NULL;
9118 struct cleanup *cleanups;
9120 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9121 if (import_attr == NULL)
9123 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9124 dwarf_tag_name (die->tag));
9129 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9130 imported_name = dwarf2_name (imported_die, imported_cu);
9131 if (imported_name == NULL)
9133 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9135 The import in the following code:
9149 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9150 <52> DW_AT_decl_file : 1
9151 <53> DW_AT_decl_line : 6
9152 <54> DW_AT_import : <0x75>
9153 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9155 <5b> DW_AT_decl_file : 1
9156 <5c> DW_AT_decl_line : 2
9157 <5d> DW_AT_type : <0x6e>
9159 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9160 <76> DW_AT_byte_size : 4
9161 <77> DW_AT_encoding : 5 (signed)
9163 imports the wrong die ( 0x75 instead of 0x58 ).
9164 This case will be ignored until the gcc bug is fixed. */
9168 /* Figure out the local name after import. */
9169 import_alias = dwarf2_name (die, cu);
9171 /* Figure out where the statement is being imported to. */
9172 import_prefix = determine_prefix (die, cu);
9174 /* Figure out what the scope of the imported die is and prepend it
9175 to the name of the imported die. */
9176 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9178 if (imported_die->tag != DW_TAG_namespace
9179 && imported_die->tag != DW_TAG_module)
9181 imported_declaration = imported_name;
9182 canonical_name = imported_name_prefix;
9184 else if (strlen (imported_name_prefix) > 0)
9185 canonical_name = obconcat (&objfile->objfile_obstack,
9186 imported_name_prefix,
9187 (cu->language == language_d ? "." : "::"),
9188 imported_name, (char *) NULL);
9190 canonical_name = imported_name;
9192 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9194 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9195 for (child_die = die->child; child_die && child_die->tag;
9196 child_die = sibling_die (child_die))
9198 /* DWARF-4: A Fortran use statement with a “rename list” may be
9199 represented by an imported module entry with an import attribute
9200 referring to the module and owned entries corresponding to those
9201 entities that are renamed as part of being imported. */
9203 if (child_die->tag != DW_TAG_imported_declaration)
9205 complaint (&symfile_complaints,
9206 _("child DW_TAG_imported_declaration expected "
9207 "- DIE at 0x%x [in module %s]"),
9208 to_underlying (child_die->sect_off), objfile_name (objfile));
9212 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9213 if (import_attr == NULL)
9215 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9216 dwarf_tag_name (child_die->tag));
9221 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9223 imported_name = dwarf2_name (imported_die, imported_cu);
9224 if (imported_name == NULL)
9226 complaint (&symfile_complaints,
9227 _("child DW_TAG_imported_declaration has unknown "
9228 "imported name - DIE at 0x%x [in module %s]"),
9229 to_underlying (child_die->sect_off), objfile_name (objfile));
9233 VEC_safe_push (const_char_ptr, excludes, imported_name);
9235 process_die (child_die, cu);
9238 add_using_directive (using_directives (cu->language),
9242 imported_declaration,
9245 &objfile->objfile_obstack);
9247 do_cleanups (cleanups);
9250 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9251 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9252 this, it was first present in GCC release 4.3.0. */
9255 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9257 if (!cu->checked_producer)
9258 check_producer (cu);
9260 return cu->producer_is_gcc_lt_4_3;
9263 static file_and_directory
9264 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9266 file_and_directory res;
9268 /* Find the filename. Do not use dwarf2_name here, since the filename
9269 is not a source language identifier. */
9270 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9271 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9273 if (res.comp_dir == NULL
9274 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9275 && IS_ABSOLUTE_PATH (res.name))
9277 res.comp_dir_storage = ldirname (res.name);
9278 if (!res.comp_dir_storage.empty ())
9279 res.comp_dir = res.comp_dir_storage.c_str ();
9281 if (res.comp_dir != NULL)
9283 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9284 directory, get rid of it. */
9285 const char *cp = strchr (res.comp_dir, ':');
9287 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9288 res.comp_dir = cp + 1;
9291 if (res.name == NULL)
9292 res.name = "<unknown>";
9297 /* Handle DW_AT_stmt_list for a compilation unit.
9298 DIE is the DW_TAG_compile_unit die for CU.
9299 COMP_DIR is the compilation directory. LOWPC is passed to
9300 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9303 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9304 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9306 struct objfile *objfile = dwarf2_per_objfile->objfile;
9307 struct attribute *attr;
9308 struct line_header line_header_local;
9309 hashval_t line_header_local_hash;
9314 gdb_assert (! cu->per_cu->is_debug_types);
9316 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9320 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9322 /* The line header hash table is only created if needed (it exists to
9323 prevent redundant reading of the line table for partial_units).
9324 If we're given a partial_unit, we'll need it. If we're given a
9325 compile_unit, then use the line header hash table if it's already
9326 created, but don't create one just yet. */
9328 if (dwarf2_per_objfile->line_header_hash == NULL
9329 && die->tag == DW_TAG_partial_unit)
9331 dwarf2_per_objfile->line_header_hash
9332 = htab_create_alloc_ex (127, line_header_hash_voidp,
9333 line_header_eq_voidp,
9334 free_line_header_voidp,
9335 &objfile->objfile_obstack,
9336 hashtab_obstack_allocate,
9337 dummy_obstack_deallocate);
9340 line_header_local.sect_off = line_offset;
9341 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9342 line_header_local_hash = line_header_hash (&line_header_local);
9343 if (dwarf2_per_objfile->line_header_hash != NULL)
9345 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9347 line_header_local_hash, NO_INSERT);
9349 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9350 is not present in *SLOT (since if there is something in *SLOT then
9351 it will be for a partial_unit). */
9352 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9354 gdb_assert (*slot != NULL);
9355 cu->line_header = (struct line_header *) *slot;
9360 /* dwarf_decode_line_header does not yet provide sufficient information.
9361 We always have to call also dwarf_decode_lines for it. */
9362 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9365 cu->line_header = lh.get ();
9367 if (dwarf2_per_objfile->line_header_hash == NULL)
9371 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9373 line_header_local_hash, INSERT);
9374 gdb_assert (slot != NULL);
9376 if (slot != NULL && *slot == NULL)
9378 /* This newly decoded line number information unit will be owned
9379 by line_header_hash hash table. */
9380 *slot = cu->line_header;
9384 /* We cannot free any current entry in (*slot) as that struct line_header
9385 may be already used by multiple CUs. Create only temporary decoded
9386 line_header for this CU - it may happen at most once for each line
9387 number information unit. And if we're not using line_header_hash
9388 then this is what we want as well. */
9389 gdb_assert (die->tag != DW_TAG_partial_unit);
9391 decode_mapping = (die->tag != DW_TAG_partial_unit);
9392 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9398 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9401 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9403 struct objfile *objfile = dwarf2_per_objfile->objfile;
9404 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9405 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9406 CORE_ADDR highpc = ((CORE_ADDR) 0);
9407 struct attribute *attr;
9408 struct die_info *child_die;
9411 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9413 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9415 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9416 from finish_block. */
9417 if (lowpc == ((CORE_ADDR) -1))
9419 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9421 file_and_directory fnd = find_file_and_directory (die, cu);
9423 prepare_one_comp_unit (cu, die, cu->language);
9425 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9426 standardised yet. As a workaround for the language detection we fall
9427 back to the DW_AT_producer string. */
9428 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9429 cu->language = language_opencl;
9431 /* Similar hack for Go. */
9432 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9433 set_cu_language (DW_LANG_Go, cu);
9435 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
9437 /* Decode line number information if present. We do this before
9438 processing child DIEs, so that the line header table is available
9439 for DW_AT_decl_file. */
9440 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
9442 /* Process all dies in compilation unit. */
9443 if (die->child != NULL)
9445 child_die = die->child;
9446 while (child_die && child_die->tag)
9448 process_die (child_die, cu);
9449 child_die = sibling_die (child_die);
9453 /* Decode macro information, if present. Dwarf 2 macro information
9454 refers to information in the line number info statement program
9455 header, so we can only read it if we've read the header
9457 attr = dwarf2_attr (die, DW_AT_macros, cu);
9459 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9460 if (attr && cu->line_header)
9462 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9463 complaint (&symfile_complaints,
9464 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9466 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9470 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9471 if (attr && cu->line_header)
9473 unsigned int macro_offset = DW_UNSND (attr);
9475 dwarf_decode_macros (cu, macro_offset, 0);
9480 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9481 Create the set of symtabs used by this TU, or if this TU is sharing
9482 symtabs with another TU and the symtabs have already been created
9483 then restore those symtabs in the line header.
9484 We don't need the pc/line-number mapping for type units. */
9487 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9489 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9490 struct type_unit_group *tu_group;
9492 struct attribute *attr;
9494 struct signatured_type *sig_type;
9496 gdb_assert (per_cu->is_debug_types);
9497 sig_type = (struct signatured_type *) per_cu;
9499 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9501 /* If we're using .gdb_index (includes -readnow) then
9502 per_cu->type_unit_group may not have been set up yet. */
9503 if (sig_type->type_unit_group == NULL)
9504 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9505 tu_group = sig_type->type_unit_group;
9507 /* If we've already processed this stmt_list there's no real need to
9508 do it again, we could fake it and just recreate the part we need
9509 (file name,index -> symtab mapping). If data shows this optimization
9510 is useful we can do it then. */
9511 first_time = tu_group->compunit_symtab == NULL;
9513 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9518 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9519 lh = dwarf_decode_line_header (line_offset, cu);
9524 dwarf2_start_symtab (cu, "", NULL, 0);
9527 gdb_assert (tu_group->symtabs == NULL);
9528 restart_symtab (tu_group->compunit_symtab, "", 0);
9533 cu->line_header = lh.get ();
9537 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9539 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9540 still initializing it, and our caller (a few levels up)
9541 process_full_type_unit still needs to know if this is the first
9544 tu_group->num_symtabs = lh->file_names.size ();
9545 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
9547 for (i = 0; i < lh->file_names.size (); ++i)
9549 file_entry &fe = lh->file_names[i];
9551 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
9553 if (current_subfile->symtab == NULL)
9555 /* NOTE: start_subfile will recognize when it's been passed
9556 a file it has already seen. So we can't assume there's a
9557 simple mapping from lh->file_names to subfiles, plus
9558 lh->file_names may contain dups. */
9559 current_subfile->symtab
9560 = allocate_symtab (cust, current_subfile->name);
9563 fe.symtab = current_subfile->symtab;
9564 tu_group->symtabs[i] = fe.symtab;
9569 restart_symtab (tu_group->compunit_symtab, "", 0);
9571 for (i = 0; i < lh->file_names.size (); ++i)
9573 struct file_entry *fe = &lh->file_names[i];
9575 fe->symtab = tu_group->symtabs[i];
9581 /* The main symtab is allocated last. Type units don't have DW_AT_name
9582 so they don't have a "real" (so to speak) symtab anyway.
9583 There is later code that will assign the main symtab to all symbols
9584 that don't have one. We need to handle the case of a symbol with a
9585 missing symtab (DW_AT_decl_file) anyway. */
9588 /* Process DW_TAG_type_unit.
9589 For TUs we want to skip the first top level sibling if it's not the
9590 actual type being defined by this TU. In this case the first top
9591 level sibling is there to provide context only. */
9594 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9596 struct die_info *child_die;
9598 prepare_one_comp_unit (cu, die, language_minimal);
9600 /* Initialize (or reinitialize) the machinery for building symtabs.
9601 We do this before processing child DIEs, so that the line header table
9602 is available for DW_AT_decl_file. */
9603 setup_type_unit_groups (die, cu);
9605 if (die->child != NULL)
9607 child_die = die->child;
9608 while (child_die && child_die->tag)
9610 process_die (child_die, cu);
9611 child_die = sibling_die (child_die);
9618 http://gcc.gnu.org/wiki/DebugFission
9619 http://gcc.gnu.org/wiki/DebugFissionDWP
9621 To simplify handling of both DWO files ("object" files with the DWARF info)
9622 and DWP files (a file with the DWOs packaged up into one file), we treat
9623 DWP files as having a collection of virtual DWO files. */
9626 hash_dwo_file (const void *item)
9628 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9631 hash = htab_hash_string (dwo_file->dwo_name);
9632 if (dwo_file->comp_dir != NULL)
9633 hash += htab_hash_string (dwo_file->comp_dir);
9638 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9640 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9641 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9643 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9645 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9646 return lhs->comp_dir == rhs->comp_dir;
9647 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9650 /* Allocate a hash table for DWO files. */
9653 allocate_dwo_file_hash_table (void)
9655 struct objfile *objfile = dwarf2_per_objfile->objfile;
9657 return htab_create_alloc_ex (41,
9661 &objfile->objfile_obstack,
9662 hashtab_obstack_allocate,
9663 dummy_obstack_deallocate);
9666 /* Lookup DWO file DWO_NAME. */
9669 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9671 struct dwo_file find_entry;
9674 if (dwarf2_per_objfile->dwo_files == NULL)
9675 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9677 memset (&find_entry, 0, sizeof (find_entry));
9678 find_entry.dwo_name = dwo_name;
9679 find_entry.comp_dir = comp_dir;
9680 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9686 hash_dwo_unit (const void *item)
9688 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9690 /* This drops the top 32 bits of the id, but is ok for a hash. */
9691 return dwo_unit->signature;
9695 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9697 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9698 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9700 /* The signature is assumed to be unique within the DWO file.
9701 So while object file CU dwo_id's always have the value zero,
9702 that's OK, assuming each object file DWO file has only one CU,
9703 and that's the rule for now. */
9704 return lhs->signature == rhs->signature;
9707 /* Allocate a hash table for DWO CUs,TUs.
9708 There is one of these tables for each of CUs,TUs for each DWO file. */
9711 allocate_dwo_unit_table (struct objfile *objfile)
9713 /* Start out with a pretty small number.
9714 Generally DWO files contain only one CU and maybe some TUs. */
9715 return htab_create_alloc_ex (3,
9719 &objfile->objfile_obstack,
9720 hashtab_obstack_allocate,
9721 dummy_obstack_deallocate);
9724 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9726 struct create_dwo_cu_data
9728 struct dwo_file *dwo_file;
9729 struct dwo_unit dwo_unit;
9732 /* die_reader_func for create_dwo_cu. */
9735 create_dwo_cu_reader (const struct die_reader_specs *reader,
9736 const gdb_byte *info_ptr,
9737 struct die_info *comp_unit_die,
9741 struct dwarf2_cu *cu = reader->cu;
9742 sect_offset sect_off = cu->per_cu->sect_off;
9743 struct dwarf2_section_info *section = cu->per_cu->section;
9744 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9745 struct dwo_file *dwo_file = data->dwo_file;
9746 struct dwo_unit *dwo_unit = &data->dwo_unit;
9747 struct attribute *attr;
9749 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9752 complaint (&symfile_complaints,
9753 _("Dwarf Error: debug entry at offset 0x%x is missing"
9754 " its dwo_id [in module %s]"),
9755 to_underlying (sect_off), dwo_file->dwo_name);
9759 dwo_unit->dwo_file = dwo_file;
9760 dwo_unit->signature = DW_UNSND (attr);
9761 dwo_unit->section = section;
9762 dwo_unit->sect_off = sect_off;
9763 dwo_unit->length = cu->per_cu->length;
9765 if (dwarf_read_debug)
9766 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9767 to_underlying (sect_off),
9768 hex_string (dwo_unit->signature));
9771 /* Create the dwo_units for the CUs in a DWO_FILE.
9772 Note: This function processes DWO files only, not DWP files. */
9775 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info §ion,
9778 struct objfile *objfile = dwarf2_per_objfile->objfile;
9779 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
9780 const gdb_byte *info_ptr, *end_ptr;
9782 dwarf2_read_section (objfile, §ion);
9783 info_ptr = section.buffer;
9785 if (info_ptr == NULL)
9788 if (dwarf_read_debug)
9790 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9791 get_section_name (§ion),
9792 get_section_file_name (§ion));
9795 end_ptr = info_ptr + section.size;
9796 while (info_ptr < end_ptr)
9798 struct dwarf2_per_cu_data per_cu;
9799 struct create_dwo_cu_data create_dwo_cu_data;
9800 struct dwo_unit *dwo_unit;
9802 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9804 memset (&create_dwo_cu_data.dwo_unit, 0,
9805 sizeof (create_dwo_cu_data.dwo_unit));
9806 memset (&per_cu, 0, sizeof (per_cu));
9807 per_cu.objfile = objfile;
9808 per_cu.is_debug_types = 0;
9809 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9810 per_cu.section = §ion;
9812 init_cutu_and_read_dies_no_follow (
9813 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9814 info_ptr += per_cu.length;
9816 // If the unit could not be parsed, skip it.
9817 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9820 if (cus_htab == NULL)
9821 cus_htab = allocate_dwo_unit_table (objfile);
9823 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9824 *dwo_unit = create_dwo_cu_data.dwo_unit;
9825 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9826 gdb_assert (slot != NULL);
9829 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9830 sect_offset dup_sect_off = dup_cu->sect_off;
9832 complaint (&symfile_complaints,
9833 _("debug cu entry at offset 0x%x is duplicate to"
9834 " the entry at offset 0x%x, signature %s"),
9835 to_underlying (sect_off), to_underlying (dup_sect_off),
9836 hex_string (dwo_unit->signature));
9838 *slot = (void *)dwo_unit;
9842 /* DWP file .debug_{cu,tu}_index section format:
9843 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9847 Both index sections have the same format, and serve to map a 64-bit
9848 signature to a set of section numbers. Each section begins with a header,
9849 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9850 indexes, and a pool of 32-bit section numbers. The index sections will be
9851 aligned at 8-byte boundaries in the file.
9853 The index section header consists of:
9855 V, 32 bit version number
9857 N, 32 bit number of compilation units or type units in the index
9858 M, 32 bit number of slots in the hash table
9860 Numbers are recorded using the byte order of the application binary.
9862 The hash table begins at offset 16 in the section, and consists of an array
9863 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9864 order of the application binary). Unused slots in the hash table are 0.
9865 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9867 The parallel table begins immediately after the hash table
9868 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9869 array of 32-bit indexes (using the byte order of the application binary),
9870 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9871 table contains a 32-bit index into the pool of section numbers. For unused
9872 hash table slots, the corresponding entry in the parallel table will be 0.
9874 The pool of section numbers begins immediately following the hash table
9875 (at offset 16 + 12 * M from the beginning of the section). The pool of
9876 section numbers consists of an array of 32-bit words (using the byte order
9877 of the application binary). Each item in the array is indexed starting
9878 from 0. The hash table entry provides the index of the first section
9879 number in the set. Additional section numbers in the set follow, and the
9880 set is terminated by a 0 entry (section number 0 is not used in ELF).
9882 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9883 section must be the first entry in the set, and the .debug_abbrev.dwo must
9884 be the second entry. Other members of the set may follow in any order.
9890 DWP Version 2 combines all the .debug_info, etc. sections into one,
9891 and the entries in the index tables are now offsets into these sections.
9892 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9895 Index Section Contents:
9897 Hash Table of Signatures dwp_hash_table.hash_table
9898 Parallel Table of Indices dwp_hash_table.unit_table
9899 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9900 Table of Section Sizes dwp_hash_table.v2.sizes
9902 The index section header consists of:
9904 V, 32 bit version number
9905 L, 32 bit number of columns in the table of section offsets
9906 N, 32 bit number of compilation units or type units in the index
9907 M, 32 bit number of slots in the hash table
9909 Numbers are recorded using the byte order of the application binary.
9911 The hash table has the same format as version 1.
9912 The parallel table of indices has the same format as version 1,
9913 except that the entries are origin-1 indices into the table of sections
9914 offsets and the table of section sizes.
9916 The table of offsets begins immediately following the parallel table
9917 (at offset 16 + 12 * M from the beginning of the section). The table is
9918 a two-dimensional array of 32-bit words (using the byte order of the
9919 application binary), with L columns and N+1 rows, in row-major order.
9920 Each row in the array is indexed starting from 0. The first row provides
9921 a key to the remaining rows: each column in this row provides an identifier
9922 for a debug section, and the offsets in the same column of subsequent rows
9923 refer to that section. The section identifiers are:
9925 DW_SECT_INFO 1 .debug_info.dwo
9926 DW_SECT_TYPES 2 .debug_types.dwo
9927 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9928 DW_SECT_LINE 4 .debug_line.dwo
9929 DW_SECT_LOC 5 .debug_loc.dwo
9930 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9931 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9932 DW_SECT_MACRO 8 .debug_macro.dwo
9934 The offsets provided by the CU and TU index sections are the base offsets
9935 for the contributions made by each CU or TU to the corresponding section
9936 in the package file. Each CU and TU header contains an abbrev_offset
9937 field, used to find the abbreviations table for that CU or TU within the
9938 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9939 be interpreted as relative to the base offset given in the index section.
9940 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9941 should be interpreted as relative to the base offset for .debug_line.dwo,
9942 and offsets into other debug sections obtained from DWARF attributes should
9943 also be interpreted as relative to the corresponding base offset.
9945 The table of sizes begins immediately following the table of offsets.
9946 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9947 with L columns and N rows, in row-major order. Each row in the array is
9948 indexed starting from 1 (row 0 is shared by the two tables).
9952 Hash table lookup is handled the same in version 1 and 2:
9954 We assume that N and M will not exceed 2^32 - 1.
9955 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9957 Given a 64-bit compilation unit signature or a type signature S, an entry
9958 in the hash table is located as follows:
9960 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9961 the low-order k bits all set to 1.
9963 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9965 3) If the hash table entry at index H matches the signature, use that
9966 entry. If the hash table entry at index H is unused (all zeroes),
9967 terminate the search: the signature is not present in the table.
9969 4) Let H = (H + H') modulo M. Repeat at Step 3.
9971 Because M > N and H' and M are relatively prime, the search is guaranteed
9972 to stop at an unused slot or find the match. */
9974 /* Create a hash table to map DWO IDs to their CU/TU entry in
9975 .debug_{info,types}.dwo in DWP_FILE.
9976 Returns NULL if there isn't one.
9977 Note: This function processes DWP files only, not DWO files. */
9979 static struct dwp_hash_table *
9980 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9982 struct objfile *objfile = dwarf2_per_objfile->objfile;
9983 bfd *dbfd = dwp_file->dbfd;
9984 const gdb_byte *index_ptr, *index_end;
9985 struct dwarf2_section_info *index;
9986 uint32_t version, nr_columns, nr_units, nr_slots;
9987 struct dwp_hash_table *htab;
9990 index = &dwp_file->sections.tu_index;
9992 index = &dwp_file->sections.cu_index;
9994 if (dwarf2_section_empty_p (index))
9996 dwarf2_read_section (objfile, index);
9998 index_ptr = index->buffer;
9999 index_end = index_ptr + index->size;
10001 version = read_4_bytes (dbfd, index_ptr);
10004 nr_columns = read_4_bytes (dbfd, index_ptr);
10008 nr_units = read_4_bytes (dbfd, index_ptr);
10010 nr_slots = read_4_bytes (dbfd, index_ptr);
10013 if (version != 1 && version != 2)
10015 error (_("Dwarf Error: unsupported DWP file version (%s)"
10016 " [in module %s]"),
10017 pulongest (version), dwp_file->name);
10019 if (nr_slots != (nr_slots & -nr_slots))
10021 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10022 " is not power of 2 [in module %s]"),
10023 pulongest (nr_slots), dwp_file->name);
10026 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
10027 htab->version = version;
10028 htab->nr_columns = nr_columns;
10029 htab->nr_units = nr_units;
10030 htab->nr_slots = nr_slots;
10031 htab->hash_table = index_ptr;
10032 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10034 /* Exit early if the table is empty. */
10035 if (nr_slots == 0 || nr_units == 0
10036 || (version == 2 && nr_columns == 0))
10038 /* All must be zero. */
10039 if (nr_slots != 0 || nr_units != 0
10040 || (version == 2 && nr_columns != 0))
10042 complaint (&symfile_complaints,
10043 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10044 " all zero [in modules %s]"),
10052 htab->section_pool.v1.indices =
10053 htab->unit_table + sizeof (uint32_t) * nr_slots;
10054 /* It's harder to decide whether the section is too small in v1.
10055 V1 is deprecated anyway so we punt. */
10059 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10060 int *ids = htab->section_pool.v2.section_ids;
10061 /* Reverse map for error checking. */
10062 int ids_seen[DW_SECT_MAX + 1];
10065 if (nr_columns < 2)
10067 error (_("Dwarf Error: bad DWP hash table, too few columns"
10068 " in section table [in module %s]"),
10071 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10073 error (_("Dwarf Error: bad DWP hash table, too many columns"
10074 " in section table [in module %s]"),
10077 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10078 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10079 for (i = 0; i < nr_columns; ++i)
10081 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10083 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10085 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10086 " in section table [in module %s]"),
10087 id, dwp_file->name);
10089 if (ids_seen[id] != -1)
10091 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10092 " id %d in section table [in module %s]"),
10093 id, dwp_file->name);
10098 /* Must have exactly one info or types section. */
10099 if (((ids_seen[DW_SECT_INFO] != -1)
10100 + (ids_seen[DW_SECT_TYPES] != -1))
10103 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10104 " DWO info/types section [in module %s]"),
10107 /* Must have an abbrev section. */
10108 if (ids_seen[DW_SECT_ABBREV] == -1)
10110 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10111 " section [in module %s]"),
10114 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10115 htab->section_pool.v2.sizes =
10116 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10117 * nr_units * nr_columns);
10118 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10119 * nr_units * nr_columns))
10122 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10123 " [in module %s]"),
10131 /* Update SECTIONS with the data from SECTP.
10133 This function is like the other "locate" section routines that are
10134 passed to bfd_map_over_sections, but in this context the sections to
10135 read comes from the DWP V1 hash table, not the full ELF section table.
10137 The result is non-zero for success, or zero if an error was found. */
10140 locate_v1_virtual_dwo_sections (asection *sectp,
10141 struct virtual_v1_dwo_sections *sections)
10143 const struct dwop_section_names *names = &dwop_section_names;
10145 if (section_is_p (sectp->name, &names->abbrev_dwo))
10147 /* There can be only one. */
10148 if (sections->abbrev.s.section != NULL)
10150 sections->abbrev.s.section = sectp;
10151 sections->abbrev.size = bfd_get_section_size (sectp);
10153 else if (section_is_p (sectp->name, &names->info_dwo)
10154 || section_is_p (sectp->name, &names->types_dwo))
10156 /* There can be only one. */
10157 if (sections->info_or_types.s.section != NULL)
10159 sections->info_or_types.s.section = sectp;
10160 sections->info_or_types.size = bfd_get_section_size (sectp);
10162 else if (section_is_p (sectp->name, &names->line_dwo))
10164 /* There can be only one. */
10165 if (sections->line.s.section != NULL)
10167 sections->line.s.section = sectp;
10168 sections->line.size = bfd_get_section_size (sectp);
10170 else if (section_is_p (sectp->name, &names->loc_dwo))
10172 /* There can be only one. */
10173 if (sections->loc.s.section != NULL)
10175 sections->loc.s.section = sectp;
10176 sections->loc.size = bfd_get_section_size (sectp);
10178 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10180 /* There can be only one. */
10181 if (sections->macinfo.s.section != NULL)
10183 sections->macinfo.s.section = sectp;
10184 sections->macinfo.size = bfd_get_section_size (sectp);
10186 else if (section_is_p (sectp->name, &names->macro_dwo))
10188 /* There can be only one. */
10189 if (sections->macro.s.section != NULL)
10191 sections->macro.s.section = sectp;
10192 sections->macro.size = bfd_get_section_size (sectp);
10194 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10196 /* There can be only one. */
10197 if (sections->str_offsets.s.section != NULL)
10199 sections->str_offsets.s.section = sectp;
10200 sections->str_offsets.size = bfd_get_section_size (sectp);
10204 /* No other kind of section is valid. */
10211 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10212 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10213 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10214 This is for DWP version 1 files. */
10216 static struct dwo_unit *
10217 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10218 uint32_t unit_index,
10219 const char *comp_dir,
10220 ULONGEST signature, int is_debug_types)
10222 struct objfile *objfile = dwarf2_per_objfile->objfile;
10223 const struct dwp_hash_table *dwp_htab =
10224 is_debug_types ? dwp_file->tus : dwp_file->cus;
10225 bfd *dbfd = dwp_file->dbfd;
10226 const char *kind = is_debug_types ? "TU" : "CU";
10227 struct dwo_file *dwo_file;
10228 struct dwo_unit *dwo_unit;
10229 struct virtual_v1_dwo_sections sections;
10230 void **dwo_file_slot;
10231 char *virtual_dwo_name;
10232 struct cleanup *cleanups;
10235 gdb_assert (dwp_file->version == 1);
10237 if (dwarf_read_debug)
10239 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10241 pulongest (unit_index), hex_string (signature),
10245 /* Fetch the sections of this DWO unit.
10246 Put a limit on the number of sections we look for so that bad data
10247 doesn't cause us to loop forever. */
10249 #define MAX_NR_V1_DWO_SECTIONS \
10250 (1 /* .debug_info or .debug_types */ \
10251 + 1 /* .debug_abbrev */ \
10252 + 1 /* .debug_line */ \
10253 + 1 /* .debug_loc */ \
10254 + 1 /* .debug_str_offsets */ \
10255 + 1 /* .debug_macro or .debug_macinfo */ \
10256 + 1 /* trailing zero */)
10258 memset (§ions, 0, sizeof (sections));
10259 cleanups = make_cleanup (null_cleanup, 0);
10261 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10264 uint32_t section_nr =
10265 read_4_bytes (dbfd,
10266 dwp_htab->section_pool.v1.indices
10267 + (unit_index + i) * sizeof (uint32_t));
10269 if (section_nr == 0)
10271 if (section_nr >= dwp_file->num_sections)
10273 error (_("Dwarf Error: bad DWP hash table, section number too large"
10274 " [in module %s]"),
10278 sectp = dwp_file->elf_sections[section_nr];
10279 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
10281 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10282 " [in module %s]"),
10288 || dwarf2_section_empty_p (§ions.info_or_types)
10289 || dwarf2_section_empty_p (§ions.abbrev))
10291 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10292 " [in module %s]"),
10295 if (i == MAX_NR_V1_DWO_SECTIONS)
10297 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10298 " [in module %s]"),
10302 /* It's easier for the rest of the code if we fake a struct dwo_file and
10303 have dwo_unit "live" in that. At least for now.
10305 The DWP file can be made up of a random collection of CUs and TUs.
10306 However, for each CU + set of TUs that came from the same original DWO
10307 file, we can combine them back into a virtual DWO file to save space
10308 (fewer struct dwo_file objects to allocate). Remember that for really
10309 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10312 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10313 get_section_id (§ions.abbrev),
10314 get_section_id (§ions.line),
10315 get_section_id (§ions.loc),
10316 get_section_id (§ions.str_offsets));
10317 make_cleanup (xfree, virtual_dwo_name);
10318 /* Can we use an existing virtual DWO file? */
10319 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10320 /* Create one if necessary. */
10321 if (*dwo_file_slot == NULL)
10323 if (dwarf_read_debug)
10325 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10328 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10330 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10332 strlen (virtual_dwo_name));
10333 dwo_file->comp_dir = comp_dir;
10334 dwo_file->sections.abbrev = sections.abbrev;
10335 dwo_file->sections.line = sections.line;
10336 dwo_file->sections.loc = sections.loc;
10337 dwo_file->sections.macinfo = sections.macinfo;
10338 dwo_file->sections.macro = sections.macro;
10339 dwo_file->sections.str_offsets = sections.str_offsets;
10340 /* The "str" section is global to the entire DWP file. */
10341 dwo_file->sections.str = dwp_file->sections.str;
10342 /* The info or types section is assigned below to dwo_unit,
10343 there's no need to record it in dwo_file.
10344 Also, we can't simply record type sections in dwo_file because
10345 we record a pointer into the vector in dwo_unit. As we collect more
10346 types we'll grow the vector and eventually have to reallocate space
10347 for it, invalidating all copies of pointers into the previous
10349 *dwo_file_slot = dwo_file;
10353 if (dwarf_read_debug)
10355 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10358 dwo_file = (struct dwo_file *) *dwo_file_slot;
10360 do_cleanups (cleanups);
10362 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10363 dwo_unit->dwo_file = dwo_file;
10364 dwo_unit->signature = signature;
10365 dwo_unit->section =
10366 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10367 *dwo_unit->section = sections.info_or_types;
10368 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10373 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10374 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10375 piece within that section used by a TU/CU, return a virtual section
10376 of just that piece. */
10378 static struct dwarf2_section_info
10379 create_dwp_v2_section (struct dwarf2_section_info *section,
10380 bfd_size_type offset, bfd_size_type size)
10382 struct dwarf2_section_info result;
10385 gdb_assert (section != NULL);
10386 gdb_assert (!section->is_virtual);
10388 memset (&result, 0, sizeof (result));
10389 result.s.containing_section = section;
10390 result.is_virtual = 1;
10395 sectp = get_section_bfd_section (section);
10397 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10398 bounds of the real section. This is a pretty-rare event, so just
10399 flag an error (easier) instead of a warning and trying to cope. */
10401 || offset + size > bfd_get_section_size (sectp))
10403 bfd *abfd = sectp->owner;
10405 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10406 " in section %s [in module %s]"),
10407 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10408 objfile_name (dwarf2_per_objfile->objfile));
10411 result.virtual_offset = offset;
10412 result.size = size;
10416 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10417 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10418 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10419 This is for DWP version 2 files. */
10421 static struct dwo_unit *
10422 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10423 uint32_t unit_index,
10424 const char *comp_dir,
10425 ULONGEST signature, int is_debug_types)
10427 struct objfile *objfile = dwarf2_per_objfile->objfile;
10428 const struct dwp_hash_table *dwp_htab =
10429 is_debug_types ? dwp_file->tus : dwp_file->cus;
10430 bfd *dbfd = dwp_file->dbfd;
10431 const char *kind = is_debug_types ? "TU" : "CU";
10432 struct dwo_file *dwo_file;
10433 struct dwo_unit *dwo_unit;
10434 struct virtual_v2_dwo_sections sections;
10435 void **dwo_file_slot;
10436 char *virtual_dwo_name;
10437 struct cleanup *cleanups;
10440 gdb_assert (dwp_file->version == 2);
10442 if (dwarf_read_debug)
10444 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10446 pulongest (unit_index), hex_string (signature),
10450 /* Fetch the section offsets of this DWO unit. */
10452 memset (§ions, 0, sizeof (sections));
10453 cleanups = make_cleanup (null_cleanup, 0);
10455 for (i = 0; i < dwp_htab->nr_columns; ++i)
10457 uint32_t offset = read_4_bytes (dbfd,
10458 dwp_htab->section_pool.v2.offsets
10459 + (((unit_index - 1) * dwp_htab->nr_columns
10461 * sizeof (uint32_t)));
10462 uint32_t size = read_4_bytes (dbfd,
10463 dwp_htab->section_pool.v2.sizes
10464 + (((unit_index - 1) * dwp_htab->nr_columns
10466 * sizeof (uint32_t)));
10468 switch (dwp_htab->section_pool.v2.section_ids[i])
10471 case DW_SECT_TYPES:
10472 sections.info_or_types_offset = offset;
10473 sections.info_or_types_size = size;
10475 case DW_SECT_ABBREV:
10476 sections.abbrev_offset = offset;
10477 sections.abbrev_size = size;
10480 sections.line_offset = offset;
10481 sections.line_size = size;
10484 sections.loc_offset = offset;
10485 sections.loc_size = size;
10487 case DW_SECT_STR_OFFSETS:
10488 sections.str_offsets_offset = offset;
10489 sections.str_offsets_size = size;
10491 case DW_SECT_MACINFO:
10492 sections.macinfo_offset = offset;
10493 sections.macinfo_size = size;
10495 case DW_SECT_MACRO:
10496 sections.macro_offset = offset;
10497 sections.macro_size = size;
10502 /* It's easier for the rest of the code if we fake a struct dwo_file and
10503 have dwo_unit "live" in that. At least for now.
10505 The DWP file can be made up of a random collection of CUs and TUs.
10506 However, for each CU + set of TUs that came from the same original DWO
10507 file, we can combine them back into a virtual DWO file to save space
10508 (fewer struct dwo_file objects to allocate). Remember that for really
10509 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10512 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10513 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10514 (long) (sections.line_size ? sections.line_offset : 0),
10515 (long) (sections.loc_size ? sections.loc_offset : 0),
10516 (long) (sections.str_offsets_size
10517 ? sections.str_offsets_offset : 0));
10518 make_cleanup (xfree, virtual_dwo_name);
10519 /* Can we use an existing virtual DWO file? */
10520 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10521 /* Create one if necessary. */
10522 if (*dwo_file_slot == NULL)
10524 if (dwarf_read_debug)
10526 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10529 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10531 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10533 strlen (virtual_dwo_name));
10534 dwo_file->comp_dir = comp_dir;
10535 dwo_file->sections.abbrev =
10536 create_dwp_v2_section (&dwp_file->sections.abbrev,
10537 sections.abbrev_offset, sections.abbrev_size);
10538 dwo_file->sections.line =
10539 create_dwp_v2_section (&dwp_file->sections.line,
10540 sections.line_offset, sections.line_size);
10541 dwo_file->sections.loc =
10542 create_dwp_v2_section (&dwp_file->sections.loc,
10543 sections.loc_offset, sections.loc_size);
10544 dwo_file->sections.macinfo =
10545 create_dwp_v2_section (&dwp_file->sections.macinfo,
10546 sections.macinfo_offset, sections.macinfo_size);
10547 dwo_file->sections.macro =
10548 create_dwp_v2_section (&dwp_file->sections.macro,
10549 sections.macro_offset, sections.macro_size);
10550 dwo_file->sections.str_offsets =
10551 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10552 sections.str_offsets_offset,
10553 sections.str_offsets_size);
10554 /* The "str" section is global to the entire DWP file. */
10555 dwo_file->sections.str = dwp_file->sections.str;
10556 /* The info or types section is assigned below to dwo_unit,
10557 there's no need to record it in dwo_file.
10558 Also, we can't simply record type sections in dwo_file because
10559 we record a pointer into the vector in dwo_unit. As we collect more
10560 types we'll grow the vector and eventually have to reallocate space
10561 for it, invalidating all copies of pointers into the previous
10563 *dwo_file_slot = dwo_file;
10567 if (dwarf_read_debug)
10569 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10572 dwo_file = (struct dwo_file *) *dwo_file_slot;
10574 do_cleanups (cleanups);
10576 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10577 dwo_unit->dwo_file = dwo_file;
10578 dwo_unit->signature = signature;
10579 dwo_unit->section =
10580 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10581 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10582 ? &dwp_file->sections.types
10583 : &dwp_file->sections.info,
10584 sections.info_or_types_offset,
10585 sections.info_or_types_size);
10586 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10591 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10592 Returns NULL if the signature isn't found. */
10594 static struct dwo_unit *
10595 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10596 ULONGEST signature, int is_debug_types)
10598 const struct dwp_hash_table *dwp_htab =
10599 is_debug_types ? dwp_file->tus : dwp_file->cus;
10600 bfd *dbfd = dwp_file->dbfd;
10601 uint32_t mask = dwp_htab->nr_slots - 1;
10602 uint32_t hash = signature & mask;
10603 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10606 struct dwo_unit find_dwo_cu;
10608 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10609 find_dwo_cu.signature = signature;
10610 slot = htab_find_slot (is_debug_types
10611 ? dwp_file->loaded_tus
10612 : dwp_file->loaded_cus,
10613 &find_dwo_cu, INSERT);
10616 return (struct dwo_unit *) *slot;
10618 /* Use a for loop so that we don't loop forever on bad debug info. */
10619 for (i = 0; i < dwp_htab->nr_slots; ++i)
10621 ULONGEST signature_in_table;
10623 signature_in_table =
10624 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10625 if (signature_in_table == signature)
10627 uint32_t unit_index =
10628 read_4_bytes (dbfd,
10629 dwp_htab->unit_table + hash * sizeof (uint32_t));
10631 if (dwp_file->version == 1)
10633 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10634 comp_dir, signature,
10639 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10640 comp_dir, signature,
10643 return (struct dwo_unit *) *slot;
10645 if (signature_in_table == 0)
10647 hash = (hash + hash2) & mask;
10650 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10651 " [in module %s]"),
10655 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10656 Open the file specified by FILE_NAME and hand it off to BFD for
10657 preliminary analysis. Return a newly initialized bfd *, which
10658 includes a canonicalized copy of FILE_NAME.
10659 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10660 SEARCH_CWD is true if the current directory is to be searched.
10661 It will be searched before debug-file-directory.
10662 If successful, the file is added to the bfd include table of the
10663 objfile's bfd (see gdb_bfd_record_inclusion).
10664 If unable to find/open the file, return NULL.
10665 NOTE: This function is derived from symfile_bfd_open. */
10667 static gdb_bfd_ref_ptr
10668 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10671 char *absolute_name;
10672 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10673 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10674 to debug_file_directory. */
10676 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10680 if (*debug_file_directory != '\0')
10681 search_path = concat (".", dirname_separator_string,
10682 debug_file_directory, (char *) NULL);
10684 search_path = xstrdup (".");
10687 search_path = xstrdup (debug_file_directory);
10689 flags = OPF_RETURN_REALPATH;
10691 flags |= OPF_SEARCH_IN_PATH;
10692 desc = openp (search_path, flags, file_name,
10693 O_RDONLY | O_BINARY, &absolute_name);
10694 xfree (search_path);
10698 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10699 xfree (absolute_name);
10700 if (sym_bfd == NULL)
10702 bfd_set_cacheable (sym_bfd.get (), 1);
10704 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10707 /* Success. Record the bfd as having been included by the objfile's bfd.
10708 This is important because things like demangled_names_hash lives in the
10709 objfile's per_bfd space and may have references to things like symbol
10710 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10711 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10716 /* Try to open DWO file FILE_NAME.
10717 COMP_DIR is the DW_AT_comp_dir attribute.
10718 The result is the bfd handle of the file.
10719 If there is a problem finding or opening the file, return NULL.
10720 Upon success, the canonicalized path of the file is stored in the bfd,
10721 same as symfile_bfd_open. */
10723 static gdb_bfd_ref_ptr
10724 open_dwo_file (const char *file_name, const char *comp_dir)
10726 if (IS_ABSOLUTE_PATH (file_name))
10727 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10729 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10731 if (comp_dir != NULL)
10733 char *path_to_try = concat (comp_dir, SLASH_STRING,
10734 file_name, (char *) NULL);
10736 /* NOTE: If comp_dir is a relative path, this will also try the
10737 search path, which seems useful. */
10738 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10739 1 /*search_cwd*/));
10740 xfree (path_to_try);
10745 /* That didn't work, try debug-file-directory, which, despite its name,
10746 is a list of paths. */
10748 if (*debug_file_directory == '\0')
10751 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10754 /* This function is mapped across the sections and remembers the offset and
10755 size of each of the DWO debugging sections we are interested in. */
10758 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10760 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10761 const struct dwop_section_names *names = &dwop_section_names;
10763 if (section_is_p (sectp->name, &names->abbrev_dwo))
10765 dwo_sections->abbrev.s.section = sectp;
10766 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10768 else if (section_is_p (sectp->name, &names->info_dwo))
10770 dwo_sections->info.s.section = sectp;
10771 dwo_sections->info.size = bfd_get_section_size (sectp);
10773 else if (section_is_p (sectp->name, &names->line_dwo))
10775 dwo_sections->line.s.section = sectp;
10776 dwo_sections->line.size = bfd_get_section_size (sectp);
10778 else if (section_is_p (sectp->name, &names->loc_dwo))
10780 dwo_sections->loc.s.section = sectp;
10781 dwo_sections->loc.size = bfd_get_section_size (sectp);
10783 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10785 dwo_sections->macinfo.s.section = sectp;
10786 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10788 else if (section_is_p (sectp->name, &names->macro_dwo))
10790 dwo_sections->macro.s.section = sectp;
10791 dwo_sections->macro.size = bfd_get_section_size (sectp);
10793 else if (section_is_p (sectp->name, &names->str_dwo))
10795 dwo_sections->str.s.section = sectp;
10796 dwo_sections->str.size = bfd_get_section_size (sectp);
10798 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10800 dwo_sections->str_offsets.s.section = sectp;
10801 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10803 else if (section_is_p (sectp->name, &names->types_dwo))
10805 struct dwarf2_section_info type_section;
10807 memset (&type_section, 0, sizeof (type_section));
10808 type_section.s.section = sectp;
10809 type_section.size = bfd_get_section_size (sectp);
10810 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10815 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10816 by PER_CU. This is for the non-DWP case.
10817 The result is NULL if DWO_NAME can't be found. */
10819 static struct dwo_file *
10820 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10821 const char *dwo_name, const char *comp_dir)
10823 struct objfile *objfile = dwarf2_per_objfile->objfile;
10824 struct dwo_file *dwo_file;
10825 struct cleanup *cleanups;
10827 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10830 if (dwarf_read_debug)
10831 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10834 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10835 dwo_file->dwo_name = dwo_name;
10836 dwo_file->comp_dir = comp_dir;
10837 dwo_file->dbfd = dbfd.release ();
10839 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10841 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10842 &dwo_file->sections);
10844 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
10846 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10849 discard_cleanups (cleanups);
10851 if (dwarf_read_debug)
10852 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10857 /* This function is mapped across the sections and remembers the offset and
10858 size of each of the DWP debugging sections common to version 1 and 2 that
10859 we are interested in. */
10862 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10863 void *dwp_file_ptr)
10865 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10866 const struct dwop_section_names *names = &dwop_section_names;
10867 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10869 /* Record the ELF section number for later lookup: this is what the
10870 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10871 gdb_assert (elf_section_nr < dwp_file->num_sections);
10872 dwp_file->elf_sections[elf_section_nr] = sectp;
10874 /* Look for specific sections that we need. */
10875 if (section_is_p (sectp->name, &names->str_dwo))
10877 dwp_file->sections.str.s.section = sectp;
10878 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10880 else if (section_is_p (sectp->name, &names->cu_index))
10882 dwp_file->sections.cu_index.s.section = sectp;
10883 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10885 else if (section_is_p (sectp->name, &names->tu_index))
10887 dwp_file->sections.tu_index.s.section = sectp;
10888 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10892 /* This function is mapped across the sections and remembers the offset and
10893 size of each of the DWP version 2 debugging sections that we are interested
10894 in. This is split into a separate function because we don't know if we
10895 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10898 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10900 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10901 const struct dwop_section_names *names = &dwop_section_names;
10902 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10904 /* Record the ELF section number for later lookup: this is what the
10905 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10906 gdb_assert (elf_section_nr < dwp_file->num_sections);
10907 dwp_file->elf_sections[elf_section_nr] = sectp;
10909 /* Look for specific sections that we need. */
10910 if (section_is_p (sectp->name, &names->abbrev_dwo))
10912 dwp_file->sections.abbrev.s.section = sectp;
10913 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10915 else if (section_is_p (sectp->name, &names->info_dwo))
10917 dwp_file->sections.info.s.section = sectp;
10918 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10920 else if (section_is_p (sectp->name, &names->line_dwo))
10922 dwp_file->sections.line.s.section = sectp;
10923 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10925 else if (section_is_p (sectp->name, &names->loc_dwo))
10927 dwp_file->sections.loc.s.section = sectp;
10928 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10930 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10932 dwp_file->sections.macinfo.s.section = sectp;
10933 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10935 else if (section_is_p (sectp->name, &names->macro_dwo))
10937 dwp_file->sections.macro.s.section = sectp;
10938 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10940 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10942 dwp_file->sections.str_offsets.s.section = sectp;
10943 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10945 else if (section_is_p (sectp->name, &names->types_dwo))
10947 dwp_file->sections.types.s.section = sectp;
10948 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10952 /* Hash function for dwp_file loaded CUs/TUs. */
10955 hash_dwp_loaded_cutus (const void *item)
10957 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10959 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10960 return dwo_unit->signature;
10963 /* Equality function for dwp_file loaded CUs/TUs. */
10966 eq_dwp_loaded_cutus (const void *a, const void *b)
10968 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10969 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10971 return dua->signature == dub->signature;
10974 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10977 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10979 return htab_create_alloc_ex (3,
10980 hash_dwp_loaded_cutus,
10981 eq_dwp_loaded_cutus,
10983 &objfile->objfile_obstack,
10984 hashtab_obstack_allocate,
10985 dummy_obstack_deallocate);
10988 /* Try to open DWP file FILE_NAME.
10989 The result is the bfd handle of the file.
10990 If there is a problem finding or opening the file, return NULL.
10991 Upon success, the canonicalized path of the file is stored in the bfd,
10992 same as symfile_bfd_open. */
10994 static gdb_bfd_ref_ptr
10995 open_dwp_file (const char *file_name)
10997 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10998 1 /*search_cwd*/));
11002 /* Work around upstream bug 15652.
11003 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11004 [Whether that's a "bug" is debatable, but it is getting in our way.]
11005 We have no real idea where the dwp file is, because gdb's realpath-ing
11006 of the executable's path may have discarded the needed info.
11007 [IWBN if the dwp file name was recorded in the executable, akin to
11008 .gnu_debuglink, but that doesn't exist yet.]
11009 Strip the directory from FILE_NAME and search again. */
11010 if (*debug_file_directory != '\0')
11012 /* Don't implicitly search the current directory here.
11013 If the user wants to search "." to handle this case,
11014 it must be added to debug-file-directory. */
11015 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11022 /* Initialize the use of the DWP file for the current objfile.
11023 By convention the name of the DWP file is ${objfile}.dwp.
11024 The result is NULL if it can't be found. */
11026 static struct dwp_file *
11027 open_and_init_dwp_file (void)
11029 struct objfile *objfile = dwarf2_per_objfile->objfile;
11030 struct dwp_file *dwp_file;
11032 /* Try to find first .dwp for the binary file before any symbolic links
11035 /* If the objfile is a debug file, find the name of the real binary
11036 file and get the name of dwp file from there. */
11037 std::string dwp_name;
11038 if (objfile->separate_debug_objfile_backlink != NULL)
11040 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11041 const char *backlink_basename = lbasename (backlink->original_name);
11043 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11046 dwp_name = objfile->original_name;
11048 dwp_name += ".dwp";
11050 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
11052 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11054 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11055 dwp_name = objfile_name (objfile);
11056 dwp_name += ".dwp";
11057 dbfd = open_dwp_file (dwp_name.c_str ());
11062 if (dwarf_read_debug)
11063 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
11066 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11067 dwp_file->name = bfd_get_filename (dbfd.get ());
11068 dwp_file->dbfd = dbfd.release ();
11070 /* +1: section 0 is unused */
11071 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11072 dwp_file->elf_sections =
11073 OBSTACK_CALLOC (&objfile->objfile_obstack,
11074 dwp_file->num_sections, asection *);
11076 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11079 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11081 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11083 /* The DWP file version is stored in the hash table. Oh well. */
11084 if (dwp_file->cus->version != dwp_file->tus->version)
11086 /* Technically speaking, we should try to limp along, but this is
11087 pretty bizarre. We use pulongest here because that's the established
11088 portability solution (e.g, we cannot use %u for uint32_t). */
11089 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11090 " TU version %s [in DWP file %s]"),
11091 pulongest (dwp_file->cus->version),
11092 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11094 dwp_file->version = dwp_file->cus->version;
11096 if (dwp_file->version == 2)
11097 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11100 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11101 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11103 if (dwarf_read_debug)
11105 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11106 fprintf_unfiltered (gdb_stdlog,
11107 " %s CUs, %s TUs\n",
11108 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11109 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11115 /* Wrapper around open_and_init_dwp_file, only open it once. */
11117 static struct dwp_file *
11118 get_dwp_file (void)
11120 if (! dwarf2_per_objfile->dwp_checked)
11122 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11123 dwarf2_per_objfile->dwp_checked = 1;
11125 return dwarf2_per_objfile->dwp_file;
11128 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11129 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11130 or in the DWP file for the objfile, referenced by THIS_UNIT.
11131 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11132 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11134 This is called, for example, when wanting to read a variable with a
11135 complex location. Therefore we don't want to do file i/o for every call.
11136 Therefore we don't want to look for a DWO file on every call.
11137 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11138 then we check if we've already seen DWO_NAME, and only THEN do we check
11141 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11142 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11144 static struct dwo_unit *
11145 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11146 const char *dwo_name, const char *comp_dir,
11147 ULONGEST signature, int is_debug_types)
11149 struct objfile *objfile = dwarf2_per_objfile->objfile;
11150 const char *kind = is_debug_types ? "TU" : "CU";
11151 void **dwo_file_slot;
11152 struct dwo_file *dwo_file;
11153 struct dwp_file *dwp_file;
11155 /* First see if there's a DWP file.
11156 If we have a DWP file but didn't find the DWO inside it, don't
11157 look for the original DWO file. It makes gdb behave differently
11158 depending on whether one is debugging in the build tree. */
11160 dwp_file = get_dwp_file ();
11161 if (dwp_file != NULL)
11163 const struct dwp_hash_table *dwp_htab =
11164 is_debug_types ? dwp_file->tus : dwp_file->cus;
11166 if (dwp_htab != NULL)
11168 struct dwo_unit *dwo_cutu =
11169 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11170 signature, is_debug_types);
11172 if (dwo_cutu != NULL)
11174 if (dwarf_read_debug)
11176 fprintf_unfiltered (gdb_stdlog,
11177 "Virtual DWO %s %s found: @%s\n",
11178 kind, hex_string (signature),
11179 host_address_to_string (dwo_cutu));
11187 /* No DWP file, look for the DWO file. */
11189 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11190 if (*dwo_file_slot == NULL)
11192 /* Read in the file and build a table of the CUs/TUs it contains. */
11193 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11195 /* NOTE: This will be NULL if unable to open the file. */
11196 dwo_file = (struct dwo_file *) *dwo_file_slot;
11198 if (dwo_file != NULL)
11200 struct dwo_unit *dwo_cutu = NULL;
11202 if (is_debug_types && dwo_file->tus)
11204 struct dwo_unit find_dwo_cutu;
11206 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11207 find_dwo_cutu.signature = signature;
11209 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11211 else if (!is_debug_types && dwo_file->cus)
11213 struct dwo_unit find_dwo_cutu;
11215 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11216 find_dwo_cutu.signature = signature;
11217 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11221 if (dwo_cutu != NULL)
11223 if (dwarf_read_debug)
11225 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11226 kind, dwo_name, hex_string (signature),
11227 host_address_to_string (dwo_cutu));
11234 /* We didn't find it. This could mean a dwo_id mismatch, or
11235 someone deleted the DWO/DWP file, or the search path isn't set up
11236 correctly to find the file. */
11238 if (dwarf_read_debug)
11240 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11241 kind, dwo_name, hex_string (signature));
11244 /* This is a warning and not a complaint because it can be caused by
11245 pilot error (e.g., user accidentally deleting the DWO). */
11247 /* Print the name of the DWP file if we looked there, helps the user
11248 better diagnose the problem. */
11249 char *dwp_text = NULL;
11250 struct cleanup *cleanups;
11252 if (dwp_file != NULL)
11253 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11254 cleanups = make_cleanup (xfree, dwp_text);
11256 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11257 " [in module %s]"),
11258 kind, dwo_name, hex_string (signature),
11259 dwp_text != NULL ? dwp_text : "",
11260 this_unit->is_debug_types ? "TU" : "CU",
11261 to_underlying (this_unit->sect_off), objfile_name (objfile));
11263 do_cleanups (cleanups);
11268 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11269 See lookup_dwo_cutu_unit for details. */
11271 static struct dwo_unit *
11272 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11273 const char *dwo_name, const char *comp_dir,
11274 ULONGEST signature)
11276 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11279 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11280 See lookup_dwo_cutu_unit for details. */
11282 static struct dwo_unit *
11283 lookup_dwo_type_unit (struct signatured_type *this_tu,
11284 const char *dwo_name, const char *comp_dir)
11286 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11289 /* Traversal function for queue_and_load_all_dwo_tus. */
11292 queue_and_load_dwo_tu (void **slot, void *info)
11294 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11295 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11296 ULONGEST signature = dwo_unit->signature;
11297 struct signatured_type *sig_type =
11298 lookup_dwo_signatured_type (per_cu->cu, signature);
11300 if (sig_type != NULL)
11302 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11304 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11305 a real dependency of PER_CU on SIG_TYPE. That is detected later
11306 while processing PER_CU. */
11307 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11308 load_full_type_unit (sig_cu);
11309 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11315 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11316 The DWO may have the only definition of the type, though it may not be
11317 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11318 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11321 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11323 struct dwo_unit *dwo_unit;
11324 struct dwo_file *dwo_file;
11326 gdb_assert (!per_cu->is_debug_types);
11327 gdb_assert (get_dwp_file () == NULL);
11328 gdb_assert (per_cu->cu != NULL);
11330 dwo_unit = per_cu->cu->dwo_unit;
11331 gdb_assert (dwo_unit != NULL);
11333 dwo_file = dwo_unit->dwo_file;
11334 if (dwo_file->tus != NULL)
11335 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11338 /* Free all resources associated with DWO_FILE.
11339 Close the DWO file and munmap the sections.
11340 All memory should be on the objfile obstack. */
11343 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11346 /* Note: dbfd is NULL for virtual DWO files. */
11347 gdb_bfd_unref (dwo_file->dbfd);
11349 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11352 /* Wrapper for free_dwo_file for use in cleanups. */
11355 free_dwo_file_cleanup (void *arg)
11357 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11358 struct objfile *objfile = dwarf2_per_objfile->objfile;
11360 free_dwo_file (dwo_file, objfile);
11363 /* Traversal function for free_dwo_files. */
11366 free_dwo_file_from_slot (void **slot, void *info)
11368 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11369 struct objfile *objfile = (struct objfile *) info;
11371 free_dwo_file (dwo_file, objfile);
11376 /* Free all resources associated with DWO_FILES. */
11379 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11381 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11384 /* Read in various DIEs. */
11386 /* qsort helper for inherit_abstract_dies. */
11389 unsigned_int_compar (const void *ap, const void *bp)
11391 unsigned int a = *(unsigned int *) ap;
11392 unsigned int b = *(unsigned int *) bp;
11394 return (a > b) - (b > a);
11397 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11398 Inherit only the children of the DW_AT_abstract_origin DIE not being
11399 already referenced by DW_AT_abstract_origin from the children of the
11403 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11405 struct die_info *child_die;
11406 unsigned die_children_count;
11407 /* CU offsets which were referenced by children of the current DIE. */
11408 sect_offset *offsets;
11409 sect_offset *offsets_end, *offsetp;
11410 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11411 struct die_info *origin_die;
11412 /* Iterator of the ORIGIN_DIE children. */
11413 struct die_info *origin_child_die;
11414 struct cleanup *cleanups;
11415 struct attribute *attr;
11416 struct dwarf2_cu *origin_cu;
11417 struct pending **origin_previous_list_in_scope;
11419 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11423 /* Note that following die references may follow to a die in a
11427 origin_die = follow_die_ref (die, attr, &origin_cu);
11429 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11431 origin_previous_list_in_scope = origin_cu->list_in_scope;
11432 origin_cu->list_in_scope = cu->list_in_scope;
11434 if (die->tag != origin_die->tag
11435 && !(die->tag == DW_TAG_inlined_subroutine
11436 && origin_die->tag == DW_TAG_subprogram))
11437 complaint (&symfile_complaints,
11438 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11439 to_underlying (die->sect_off),
11440 to_underlying (origin_die->sect_off));
11442 child_die = die->child;
11443 die_children_count = 0;
11444 while (child_die && child_die->tag)
11446 child_die = sibling_die (child_die);
11447 die_children_count++;
11449 offsets = XNEWVEC (sect_offset, die_children_count);
11450 cleanups = make_cleanup (xfree, offsets);
11452 offsets_end = offsets;
11453 for (child_die = die->child;
11454 child_die && child_die->tag;
11455 child_die = sibling_die (child_die))
11457 struct die_info *child_origin_die;
11458 struct dwarf2_cu *child_origin_cu;
11460 /* We are trying to process concrete instance entries:
11461 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11462 it's not relevant to our analysis here. i.e. detecting DIEs that are
11463 present in the abstract instance but not referenced in the concrete
11465 if (child_die->tag == DW_TAG_call_site
11466 || child_die->tag == DW_TAG_GNU_call_site)
11469 /* For each CHILD_DIE, find the corresponding child of
11470 ORIGIN_DIE. If there is more than one layer of
11471 DW_AT_abstract_origin, follow them all; there shouldn't be,
11472 but GCC versions at least through 4.4 generate this (GCC PR
11474 child_origin_die = child_die;
11475 child_origin_cu = cu;
11478 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11482 child_origin_die = follow_die_ref (child_origin_die, attr,
11486 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11487 counterpart may exist. */
11488 if (child_origin_die != child_die)
11490 if (child_die->tag != child_origin_die->tag
11491 && !(child_die->tag == DW_TAG_inlined_subroutine
11492 && child_origin_die->tag == DW_TAG_subprogram))
11493 complaint (&symfile_complaints,
11494 _("Child DIE 0x%x and its abstract origin 0x%x have "
11496 to_underlying (child_die->sect_off),
11497 to_underlying (child_origin_die->sect_off));
11498 if (child_origin_die->parent != origin_die)
11499 complaint (&symfile_complaints,
11500 _("Child DIE 0x%x and its abstract origin 0x%x have "
11501 "different parents"),
11502 to_underlying (child_die->sect_off),
11503 to_underlying (child_origin_die->sect_off));
11505 *offsets_end++ = child_origin_die->sect_off;
11508 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11509 unsigned_int_compar);
11510 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11511 if (offsetp[-1] == *offsetp)
11512 complaint (&symfile_complaints,
11513 _("Multiple children of DIE 0x%x refer "
11514 "to DIE 0x%x as their abstract origin"),
11515 to_underlying (die->sect_off), to_underlying (*offsetp));
11518 origin_child_die = origin_die->child;
11519 while (origin_child_die && origin_child_die->tag)
11521 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11522 while (offsetp < offsets_end
11523 && *offsetp < origin_child_die->sect_off)
11525 if (offsetp >= offsets_end
11526 || *offsetp > origin_child_die->sect_off)
11528 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11529 Check whether we're already processing ORIGIN_CHILD_DIE.
11530 This can happen with mutually referenced abstract_origins.
11532 if (!origin_child_die->in_process)
11533 process_die (origin_child_die, origin_cu);
11535 origin_child_die = sibling_die (origin_child_die);
11537 origin_cu->list_in_scope = origin_previous_list_in_scope;
11539 do_cleanups (cleanups);
11543 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11545 struct objfile *objfile = cu->objfile;
11546 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11547 struct context_stack *newobj;
11550 struct die_info *child_die;
11551 struct attribute *attr, *call_line, *call_file;
11553 CORE_ADDR baseaddr;
11554 struct block *block;
11555 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11556 VEC (symbolp) *template_args = NULL;
11557 struct template_symbol *templ_func = NULL;
11561 /* If we do not have call site information, we can't show the
11562 caller of this inlined function. That's too confusing, so
11563 only use the scope for local variables. */
11564 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11565 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11566 if (call_line == NULL || call_file == NULL)
11568 read_lexical_block_scope (die, cu);
11573 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11575 name = dwarf2_name (die, cu);
11577 /* Ignore functions with missing or empty names. These are actually
11578 illegal according to the DWARF standard. */
11581 complaint (&symfile_complaints,
11582 _("missing name for subprogram DIE at %d"),
11583 to_underlying (die->sect_off));
11587 /* Ignore functions with missing or invalid low and high pc attributes. */
11588 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11589 <= PC_BOUNDS_INVALID)
11591 attr = dwarf2_attr (die, DW_AT_external, cu);
11592 if (!attr || !DW_UNSND (attr))
11593 complaint (&symfile_complaints,
11594 _("cannot get low and high bounds "
11595 "for subprogram DIE at %d"),
11596 to_underlying (die->sect_off));
11600 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11601 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11603 /* If we have any template arguments, then we must allocate a
11604 different sort of symbol. */
11605 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11607 if (child_die->tag == DW_TAG_template_type_param
11608 || child_die->tag == DW_TAG_template_value_param)
11610 templ_func = allocate_template_symbol (objfile);
11611 templ_func->base.is_cplus_template_function = 1;
11616 newobj = push_context (0, lowpc);
11617 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11618 (struct symbol *) templ_func);
11620 /* If there is a location expression for DW_AT_frame_base, record
11622 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11624 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11626 /* If there is a location for the static link, record it. */
11627 newobj->static_link = NULL;
11628 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11631 newobj->static_link
11632 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11633 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11636 cu->list_in_scope = &local_symbols;
11638 if (die->child != NULL)
11640 child_die = die->child;
11641 while (child_die && child_die->tag)
11643 if (child_die->tag == DW_TAG_template_type_param
11644 || child_die->tag == DW_TAG_template_value_param)
11646 struct symbol *arg = new_symbol (child_die, NULL, cu);
11649 VEC_safe_push (symbolp, template_args, arg);
11652 process_die (child_die, cu);
11653 child_die = sibling_die (child_die);
11657 inherit_abstract_dies (die, cu);
11659 /* If we have a DW_AT_specification, we might need to import using
11660 directives from the context of the specification DIE. See the
11661 comment in determine_prefix. */
11662 if (cu->language == language_cplus
11663 && dwarf2_attr (die, DW_AT_specification, cu))
11665 struct dwarf2_cu *spec_cu = cu;
11666 struct die_info *spec_die = die_specification (die, &spec_cu);
11670 child_die = spec_die->child;
11671 while (child_die && child_die->tag)
11673 if (child_die->tag == DW_TAG_imported_module)
11674 process_die (child_die, spec_cu);
11675 child_die = sibling_die (child_die);
11678 /* In some cases, GCC generates specification DIEs that
11679 themselves contain DW_AT_specification attributes. */
11680 spec_die = die_specification (spec_die, &spec_cu);
11684 newobj = pop_context ();
11685 /* Make a block for the local symbols within. */
11686 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11687 newobj->static_link, lowpc, highpc);
11689 /* For C++, set the block's scope. */
11690 if ((cu->language == language_cplus
11691 || cu->language == language_fortran
11692 || cu->language == language_d
11693 || cu->language == language_rust)
11694 && cu->processing_has_namespace_info)
11695 block_set_scope (block, determine_prefix (die, cu),
11696 &objfile->objfile_obstack);
11698 /* If we have address ranges, record them. */
11699 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11701 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11703 /* Attach template arguments to function. */
11704 if (! VEC_empty (symbolp, template_args))
11706 gdb_assert (templ_func != NULL);
11708 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11709 templ_func->template_arguments
11710 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11711 templ_func->n_template_arguments);
11712 memcpy (templ_func->template_arguments,
11713 VEC_address (symbolp, template_args),
11714 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11715 VEC_free (symbolp, template_args);
11718 /* In C++, we can have functions nested inside functions (e.g., when
11719 a function declares a class that has methods). This means that
11720 when we finish processing a function scope, we may need to go
11721 back to building a containing block's symbol lists. */
11722 local_symbols = newobj->locals;
11723 local_using_directives = newobj->local_using_directives;
11725 /* If we've finished processing a top-level function, subsequent
11726 symbols go in the file symbol list. */
11727 if (outermost_context_p ())
11728 cu->list_in_scope = &file_symbols;
11731 /* Process all the DIES contained within a lexical block scope. Start
11732 a new scope, process the dies, and then close the scope. */
11735 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11737 struct objfile *objfile = cu->objfile;
11738 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11739 struct context_stack *newobj;
11740 CORE_ADDR lowpc, highpc;
11741 struct die_info *child_die;
11742 CORE_ADDR baseaddr;
11744 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11746 /* Ignore blocks with missing or invalid low and high pc attributes. */
11747 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11748 as multiple lexical blocks? Handling children in a sane way would
11749 be nasty. Might be easier to properly extend generic blocks to
11750 describe ranges. */
11751 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11753 case PC_BOUNDS_NOT_PRESENT:
11754 /* DW_TAG_lexical_block has no attributes, process its children as if
11755 there was no wrapping by that DW_TAG_lexical_block.
11756 GCC does no longer produces such DWARF since GCC r224161. */
11757 for (child_die = die->child;
11758 child_die != NULL && child_die->tag;
11759 child_die = sibling_die (child_die))
11760 process_die (child_die, cu);
11762 case PC_BOUNDS_INVALID:
11765 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11766 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11768 push_context (0, lowpc);
11769 if (die->child != NULL)
11771 child_die = die->child;
11772 while (child_die && child_die->tag)
11774 process_die (child_die, cu);
11775 child_die = sibling_die (child_die);
11778 inherit_abstract_dies (die, cu);
11779 newobj = pop_context ();
11781 if (local_symbols != NULL || local_using_directives != NULL)
11783 struct block *block
11784 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11785 newobj->start_addr, highpc);
11787 /* Note that recording ranges after traversing children, as we
11788 do here, means that recording a parent's ranges entails
11789 walking across all its children's ranges as they appear in
11790 the address map, which is quadratic behavior.
11792 It would be nicer to record the parent's ranges before
11793 traversing its children, simply overriding whatever you find
11794 there. But since we don't even decide whether to create a
11795 block until after we've traversed its children, that's hard
11797 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11799 local_symbols = newobj->locals;
11800 local_using_directives = newobj->local_using_directives;
11803 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11806 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11808 struct objfile *objfile = cu->objfile;
11809 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11810 CORE_ADDR pc, baseaddr;
11811 struct attribute *attr;
11812 struct call_site *call_site, call_site_local;
11815 struct die_info *child_die;
11817 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11819 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11822 /* This was a pre-DWARF-5 GNU extension alias
11823 for DW_AT_call_return_pc. */
11824 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11828 complaint (&symfile_complaints,
11829 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11830 "DIE 0x%x [in module %s]"),
11831 to_underlying (die->sect_off), objfile_name (objfile));
11834 pc = attr_value_as_address (attr) + baseaddr;
11835 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11837 if (cu->call_site_htab == NULL)
11838 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11839 NULL, &objfile->objfile_obstack,
11840 hashtab_obstack_allocate, NULL);
11841 call_site_local.pc = pc;
11842 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11845 complaint (&symfile_complaints,
11846 _("Duplicate PC %s for DW_TAG_call_site "
11847 "DIE 0x%x [in module %s]"),
11848 paddress (gdbarch, pc), to_underlying (die->sect_off),
11849 objfile_name (objfile));
11853 /* Count parameters at the caller. */
11856 for (child_die = die->child; child_die && child_die->tag;
11857 child_die = sibling_die (child_die))
11859 if (child_die->tag != DW_TAG_call_site_parameter
11860 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11862 complaint (&symfile_complaints,
11863 _("Tag %d is not DW_TAG_call_site_parameter in "
11864 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11865 child_die->tag, to_underlying (child_die->sect_off),
11866 objfile_name (objfile));
11874 = ((struct call_site *)
11875 obstack_alloc (&objfile->objfile_obstack,
11876 sizeof (*call_site)
11877 + (sizeof (*call_site->parameter) * (nparams - 1))));
11879 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11880 call_site->pc = pc;
11882 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11883 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11885 struct die_info *func_die;
11887 /* Skip also over DW_TAG_inlined_subroutine. */
11888 for (func_die = die->parent;
11889 func_die && func_die->tag != DW_TAG_subprogram
11890 && func_die->tag != DW_TAG_subroutine_type;
11891 func_die = func_die->parent);
11893 /* DW_AT_call_all_calls is a superset
11894 of DW_AT_call_all_tail_calls. */
11896 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11897 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11898 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11899 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11901 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11902 not complete. But keep CALL_SITE for look ups via call_site_htab,
11903 both the initial caller containing the real return address PC and
11904 the final callee containing the current PC of a chain of tail
11905 calls do not need to have the tail call list complete. But any
11906 function candidate for a virtual tail call frame searched via
11907 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11908 determined unambiguously. */
11912 struct type *func_type = NULL;
11915 func_type = get_die_type (func_die, cu);
11916 if (func_type != NULL)
11918 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11920 /* Enlist this call site to the function. */
11921 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11922 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11925 complaint (&symfile_complaints,
11926 _("Cannot find function owning DW_TAG_call_site "
11927 "DIE 0x%x [in module %s]"),
11928 to_underlying (die->sect_off), objfile_name (objfile));
11932 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11934 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11936 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11939 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11940 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11942 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11943 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11944 /* Keep NULL DWARF_BLOCK. */;
11945 else if (attr_form_is_block (attr))
11947 struct dwarf2_locexpr_baton *dlbaton;
11949 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11950 dlbaton->data = DW_BLOCK (attr)->data;
11951 dlbaton->size = DW_BLOCK (attr)->size;
11952 dlbaton->per_cu = cu->per_cu;
11954 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11956 else if (attr_form_is_ref (attr))
11958 struct dwarf2_cu *target_cu = cu;
11959 struct die_info *target_die;
11961 target_die = follow_die_ref (die, attr, &target_cu);
11962 gdb_assert (target_cu->objfile == objfile);
11963 if (die_is_declaration (target_die, target_cu))
11965 const char *target_physname;
11967 /* Prefer the mangled name; otherwise compute the demangled one. */
11968 target_physname = dwarf2_string_attr (target_die,
11969 DW_AT_linkage_name,
11971 if (target_physname == NULL)
11972 target_physname = dwarf2_string_attr (target_die,
11973 DW_AT_MIPS_linkage_name,
11975 if (target_physname == NULL)
11976 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11977 if (target_physname == NULL)
11978 complaint (&symfile_complaints,
11979 _("DW_AT_call_target target DIE has invalid "
11980 "physname, for referencing DIE 0x%x [in module %s]"),
11981 to_underlying (die->sect_off), objfile_name (objfile));
11983 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11989 /* DW_AT_entry_pc should be preferred. */
11990 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11991 <= PC_BOUNDS_INVALID)
11992 complaint (&symfile_complaints,
11993 _("DW_AT_call_target target DIE has invalid "
11994 "low pc, for referencing DIE 0x%x [in module %s]"),
11995 to_underlying (die->sect_off), objfile_name (objfile));
11998 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11999 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12004 complaint (&symfile_complaints,
12005 _("DW_TAG_call_site DW_AT_call_target is neither "
12006 "block nor reference, for DIE 0x%x [in module %s]"),
12007 to_underlying (die->sect_off), objfile_name (objfile));
12009 call_site->per_cu = cu->per_cu;
12011 for (child_die = die->child;
12012 child_die && child_die->tag;
12013 child_die = sibling_die (child_die))
12015 struct call_site_parameter *parameter;
12016 struct attribute *loc, *origin;
12018 if (child_die->tag != DW_TAG_call_site_parameter
12019 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12021 /* Already printed the complaint above. */
12025 gdb_assert (call_site->parameter_count < nparams);
12026 parameter = &call_site->parameter[call_site->parameter_count];
12028 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12029 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12030 register is contained in DW_AT_call_value. */
12032 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12033 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12034 if (origin == NULL)
12036 /* This was a pre-DWARF-5 GNU extension alias
12037 for DW_AT_call_parameter. */
12038 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12040 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
12042 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12044 sect_offset sect_off
12045 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12046 if (!offset_in_cu_p (&cu->header, sect_off))
12048 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12049 binding can be done only inside one CU. Such referenced DIE
12050 therefore cannot be even moved to DW_TAG_partial_unit. */
12051 complaint (&symfile_complaints,
12052 _("DW_AT_call_parameter offset is not in CU for "
12053 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12054 to_underlying (child_die->sect_off),
12055 objfile_name (objfile));
12058 parameter->u.param_cu_off
12059 = (cu_offset) (sect_off - cu->header.sect_off);
12061 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
12063 complaint (&symfile_complaints,
12064 _("No DW_FORM_block* DW_AT_location for "
12065 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12066 to_underlying (child_die->sect_off), objfile_name (objfile));
12071 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12072 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12073 if (parameter->u.dwarf_reg != -1)
12074 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12075 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12076 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12077 ¶meter->u.fb_offset))
12078 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12081 complaint (&symfile_complaints,
12082 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12083 "for DW_FORM_block* DW_AT_location is supported for "
12084 "DW_TAG_call_site child DIE 0x%x "
12086 to_underlying (child_die->sect_off),
12087 objfile_name (objfile));
12092 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12094 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12095 if (!attr_form_is_block (attr))
12097 complaint (&symfile_complaints,
12098 _("No DW_FORM_block* DW_AT_call_value for "
12099 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12100 to_underlying (child_die->sect_off),
12101 objfile_name (objfile));
12104 parameter->value = DW_BLOCK (attr)->data;
12105 parameter->value_size = DW_BLOCK (attr)->size;
12107 /* Parameters are not pre-cleared by memset above. */
12108 parameter->data_value = NULL;
12109 parameter->data_value_size = 0;
12110 call_site->parameter_count++;
12112 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12114 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12117 if (!attr_form_is_block (attr))
12118 complaint (&symfile_complaints,
12119 _("No DW_FORM_block* DW_AT_call_data_value for "
12120 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12121 to_underlying (child_die->sect_off),
12122 objfile_name (objfile));
12125 parameter->data_value = DW_BLOCK (attr)->data;
12126 parameter->data_value_size = DW_BLOCK (attr)->size;
12132 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12133 reading .debug_rnglists.
12134 Callback's type should be:
12135 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12136 Return true if the attributes are present and valid, otherwise,
12139 template <typename Callback>
12141 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12142 Callback &&callback)
12144 struct objfile *objfile = cu->objfile;
12145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12146 struct comp_unit_head *cu_header = &cu->header;
12147 bfd *obfd = objfile->obfd;
12148 unsigned int addr_size = cu_header->addr_size;
12149 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12150 /* Base address selection entry. */
12153 unsigned int dummy;
12154 const gdb_byte *buffer;
12156 CORE_ADDR high = 0;
12157 CORE_ADDR baseaddr;
12158 bool overflow = false;
12160 found_base = cu->base_known;
12161 base = cu->base_address;
12163 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12164 if (offset >= dwarf2_per_objfile->rnglists.size)
12166 complaint (&symfile_complaints,
12167 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12171 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12173 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12177 /* Initialize it due to a false compiler warning. */
12178 CORE_ADDR range_beginning = 0, range_end = 0;
12179 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12180 + dwarf2_per_objfile->rnglists.size);
12181 unsigned int bytes_read;
12183 if (buffer == buf_end)
12188 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12191 case DW_RLE_end_of_list:
12193 case DW_RLE_base_address:
12194 if (buffer + cu->header.addr_size > buf_end)
12199 base = read_address (obfd, buffer, cu, &bytes_read);
12201 buffer += bytes_read;
12203 case DW_RLE_start_length:
12204 if (buffer + cu->header.addr_size > buf_end)
12209 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12210 buffer += bytes_read;
12211 range_end = (range_beginning
12212 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12213 buffer += bytes_read;
12214 if (buffer > buf_end)
12220 case DW_RLE_offset_pair:
12221 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12222 buffer += bytes_read;
12223 if (buffer > buf_end)
12228 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12229 buffer += bytes_read;
12230 if (buffer > buf_end)
12236 case DW_RLE_start_end:
12237 if (buffer + 2 * cu->header.addr_size > buf_end)
12242 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12243 buffer += bytes_read;
12244 range_end = read_address (obfd, buffer, cu, &bytes_read);
12245 buffer += bytes_read;
12248 complaint (&symfile_complaints,
12249 _("Invalid .debug_rnglists data (no base address)"));
12252 if (rlet == DW_RLE_end_of_list || overflow)
12254 if (rlet == DW_RLE_base_address)
12259 /* We have no valid base address for the ranges
12261 complaint (&symfile_complaints,
12262 _("Invalid .debug_rnglists data (no base address)"));
12266 if (range_beginning > range_end)
12268 /* Inverted range entries are invalid. */
12269 complaint (&symfile_complaints,
12270 _("Invalid .debug_rnglists data (inverted range)"));
12274 /* Empty range entries have no effect. */
12275 if (range_beginning == range_end)
12278 range_beginning += base;
12281 /* A not-uncommon case of bad debug info.
12282 Don't pollute the addrmap with bad data. */
12283 if (range_beginning + baseaddr == 0
12284 && !dwarf2_per_objfile->has_section_at_zero)
12286 complaint (&symfile_complaints,
12287 _(".debug_rnglists entry has start address of zero"
12288 " [in module %s]"), objfile_name (objfile));
12292 callback (range_beginning, range_end);
12297 complaint (&symfile_complaints,
12298 _("Offset %d is not terminated "
12299 "for DW_AT_ranges attribute"),
12307 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12308 Callback's type should be:
12309 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12310 Return 1 if the attributes are present and valid, otherwise, return 0. */
12312 template <typename Callback>
12314 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12315 Callback &&callback)
12317 struct objfile *objfile = cu->objfile;
12318 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12319 struct comp_unit_head *cu_header = &cu->header;
12320 bfd *obfd = objfile->obfd;
12321 unsigned int addr_size = cu_header->addr_size;
12322 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12323 /* Base address selection entry. */
12326 unsigned int dummy;
12327 const gdb_byte *buffer;
12328 CORE_ADDR baseaddr;
12330 if (cu_header->version >= 5)
12331 return dwarf2_rnglists_process (offset, cu, callback);
12333 found_base = cu->base_known;
12334 base = cu->base_address;
12336 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12337 if (offset >= dwarf2_per_objfile->ranges.size)
12339 complaint (&symfile_complaints,
12340 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12344 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12346 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12350 CORE_ADDR range_beginning, range_end;
12352 range_beginning = read_address (obfd, buffer, cu, &dummy);
12353 buffer += addr_size;
12354 range_end = read_address (obfd, buffer, cu, &dummy);
12355 buffer += addr_size;
12356 offset += 2 * addr_size;
12358 /* An end of list marker is a pair of zero addresses. */
12359 if (range_beginning == 0 && range_end == 0)
12360 /* Found the end of list entry. */
12363 /* Each base address selection entry is a pair of 2 values.
12364 The first is the largest possible address, the second is
12365 the base address. Check for a base address here. */
12366 if ((range_beginning & mask) == mask)
12368 /* If we found the largest possible address, then we already
12369 have the base address in range_end. */
12377 /* We have no valid base address for the ranges
12379 complaint (&symfile_complaints,
12380 _("Invalid .debug_ranges data (no base address)"));
12384 if (range_beginning > range_end)
12386 /* Inverted range entries are invalid. */
12387 complaint (&symfile_complaints,
12388 _("Invalid .debug_ranges data (inverted range)"));
12392 /* Empty range entries have no effect. */
12393 if (range_beginning == range_end)
12396 range_beginning += base;
12399 /* A not-uncommon case of bad debug info.
12400 Don't pollute the addrmap with bad data. */
12401 if (range_beginning + baseaddr == 0
12402 && !dwarf2_per_objfile->has_section_at_zero)
12404 complaint (&symfile_complaints,
12405 _(".debug_ranges entry has start address of zero"
12406 " [in module %s]"), objfile_name (objfile));
12410 callback (range_beginning, range_end);
12416 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12417 Return 1 if the attributes are present and valid, otherwise, return 0.
12418 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12421 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12422 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12423 struct partial_symtab *ranges_pst)
12425 struct objfile *objfile = cu->objfile;
12426 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12427 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12428 SECT_OFF_TEXT (objfile));
12431 CORE_ADDR high = 0;
12434 retval = dwarf2_ranges_process (offset, cu,
12435 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12437 if (ranges_pst != NULL)
12442 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12443 range_beginning + baseaddr);
12444 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12445 range_end + baseaddr);
12446 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12450 /* FIXME: This is recording everything as a low-high
12451 segment of consecutive addresses. We should have a
12452 data structure for discontiguous block ranges
12456 low = range_beginning;
12462 if (range_beginning < low)
12463 low = range_beginning;
12464 if (range_end > high)
12472 /* If the first entry is an end-of-list marker, the range
12473 describes an empty scope, i.e. no instructions. */
12479 *high_return = high;
12483 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12484 definition for the return value. *LOWPC and *HIGHPC are set iff
12485 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12487 static enum pc_bounds_kind
12488 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12489 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12490 struct partial_symtab *pst)
12492 struct attribute *attr;
12493 struct attribute *attr_high;
12495 CORE_ADDR high = 0;
12496 enum pc_bounds_kind ret;
12498 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12501 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12504 low = attr_value_as_address (attr);
12505 high = attr_value_as_address (attr_high);
12506 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12510 /* Found high w/o low attribute. */
12511 return PC_BOUNDS_INVALID;
12513 /* Found consecutive range of addresses. */
12514 ret = PC_BOUNDS_HIGH_LOW;
12518 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12521 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12522 We take advantage of the fact that DW_AT_ranges does not appear
12523 in DW_TAG_compile_unit of DWO files. */
12524 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12525 unsigned int ranges_offset = (DW_UNSND (attr)
12526 + (need_ranges_base
12530 /* Value of the DW_AT_ranges attribute is the offset in the
12531 .debug_ranges section. */
12532 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12533 return PC_BOUNDS_INVALID;
12534 /* Found discontinuous range of addresses. */
12535 ret = PC_BOUNDS_RANGES;
12538 return PC_BOUNDS_NOT_PRESENT;
12541 /* read_partial_die has also the strict LOW < HIGH requirement. */
12543 return PC_BOUNDS_INVALID;
12545 /* When using the GNU linker, .gnu.linkonce. sections are used to
12546 eliminate duplicate copies of functions and vtables and such.
12547 The linker will arbitrarily choose one and discard the others.
12548 The AT_*_pc values for such functions refer to local labels in
12549 these sections. If the section from that file was discarded, the
12550 labels are not in the output, so the relocs get a value of 0.
12551 If this is a discarded function, mark the pc bounds as invalid,
12552 so that GDB will ignore it. */
12553 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12554 return PC_BOUNDS_INVALID;
12562 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12563 its low and high PC addresses. Do nothing if these addresses could not
12564 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12565 and HIGHPC to the high address if greater than HIGHPC. */
12568 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12569 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12570 struct dwarf2_cu *cu)
12572 CORE_ADDR low, high;
12573 struct die_info *child = die->child;
12575 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12577 *lowpc = std::min (*lowpc, low);
12578 *highpc = std::max (*highpc, high);
12581 /* If the language does not allow nested subprograms (either inside
12582 subprograms or lexical blocks), we're done. */
12583 if (cu->language != language_ada)
12586 /* Check all the children of the given DIE. If it contains nested
12587 subprograms, then check their pc bounds. Likewise, we need to
12588 check lexical blocks as well, as they may also contain subprogram
12590 while (child && child->tag)
12592 if (child->tag == DW_TAG_subprogram
12593 || child->tag == DW_TAG_lexical_block)
12594 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12595 child = sibling_die (child);
12599 /* Get the low and high pc's represented by the scope DIE, and store
12600 them in *LOWPC and *HIGHPC. If the correct values can't be
12601 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12604 get_scope_pc_bounds (struct die_info *die,
12605 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12606 struct dwarf2_cu *cu)
12608 CORE_ADDR best_low = (CORE_ADDR) -1;
12609 CORE_ADDR best_high = (CORE_ADDR) 0;
12610 CORE_ADDR current_low, current_high;
12612 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
12613 >= PC_BOUNDS_RANGES)
12615 best_low = current_low;
12616 best_high = current_high;
12620 struct die_info *child = die->child;
12622 while (child && child->tag)
12624 switch (child->tag) {
12625 case DW_TAG_subprogram:
12626 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12628 case DW_TAG_namespace:
12629 case DW_TAG_module:
12630 /* FIXME: carlton/2004-01-16: Should we do this for
12631 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12632 that current GCC's always emit the DIEs corresponding
12633 to definitions of methods of classes as children of a
12634 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12635 the DIEs giving the declarations, which could be
12636 anywhere). But I don't see any reason why the
12637 standards says that they have to be there. */
12638 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
12640 if (current_low != ((CORE_ADDR) -1))
12642 best_low = std::min (best_low, current_low);
12643 best_high = std::max (best_high, current_high);
12651 child = sibling_die (child);
12656 *highpc = best_high;
12659 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12663 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12664 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12666 struct objfile *objfile = cu->objfile;
12667 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12668 struct attribute *attr;
12669 struct attribute *attr_high;
12671 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12674 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12677 CORE_ADDR low = attr_value_as_address (attr);
12678 CORE_ADDR high = attr_value_as_address (attr_high);
12680 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12683 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12684 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12685 record_block_range (block, low, high - 1);
12689 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12692 bfd *obfd = objfile->obfd;
12693 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12694 We take advantage of the fact that DW_AT_ranges does not appear
12695 in DW_TAG_compile_unit of DWO files. */
12696 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12698 /* The value of the DW_AT_ranges attribute is the offset of the
12699 address range list in the .debug_ranges section. */
12700 unsigned long offset = (DW_UNSND (attr)
12701 + (need_ranges_base ? cu->ranges_base : 0));
12702 const gdb_byte *buffer;
12704 /* For some target architectures, but not others, the
12705 read_address function sign-extends the addresses it returns.
12706 To recognize base address selection entries, we need a
12708 unsigned int addr_size = cu->header.addr_size;
12709 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12711 /* The base address, to which the next pair is relative. Note
12712 that this 'base' is a DWARF concept: most entries in a range
12713 list are relative, to reduce the number of relocs against the
12714 debugging information. This is separate from this function's
12715 'baseaddr' argument, which GDB uses to relocate debugging
12716 information from a shared library based on the address at
12717 which the library was loaded. */
12718 CORE_ADDR base = cu->base_address;
12719 int base_known = cu->base_known;
12721 dwarf2_ranges_process (offset, cu,
12722 [&] (CORE_ADDR start, CORE_ADDR end)
12726 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12727 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12728 record_block_range (block, start, end - 1);
12733 /* Check whether the producer field indicates either of GCC < 4.6, or the
12734 Intel C/C++ compiler, and cache the result in CU. */
12737 check_producer (struct dwarf2_cu *cu)
12741 if (cu->producer == NULL)
12743 /* For unknown compilers expect their behavior is DWARF version
12746 GCC started to support .debug_types sections by -gdwarf-4 since
12747 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12748 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12749 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12750 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12752 else if (producer_is_gcc (cu->producer, &major, &minor))
12754 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12755 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12757 else if (startswith (cu->producer, "Intel(R) C"))
12758 cu->producer_is_icc = 1;
12761 /* For other non-GCC compilers, expect their behavior is DWARF version
12765 cu->checked_producer = 1;
12768 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12769 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12770 during 4.6.0 experimental. */
12773 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12775 if (!cu->checked_producer)
12776 check_producer (cu);
12778 return cu->producer_is_gxx_lt_4_6;
12781 /* Return the default accessibility type if it is not overriden by
12782 DW_AT_accessibility. */
12784 static enum dwarf_access_attribute
12785 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12787 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12789 /* The default DWARF 2 accessibility for members is public, the default
12790 accessibility for inheritance is private. */
12792 if (die->tag != DW_TAG_inheritance)
12793 return DW_ACCESS_public;
12795 return DW_ACCESS_private;
12799 /* DWARF 3+ defines the default accessibility a different way. The same
12800 rules apply now for DW_TAG_inheritance as for the members and it only
12801 depends on the container kind. */
12803 if (die->parent->tag == DW_TAG_class_type)
12804 return DW_ACCESS_private;
12806 return DW_ACCESS_public;
12810 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12811 offset. If the attribute was not found return 0, otherwise return
12812 1. If it was found but could not properly be handled, set *OFFSET
12816 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12819 struct attribute *attr;
12821 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12826 /* Note that we do not check for a section offset first here.
12827 This is because DW_AT_data_member_location is new in DWARF 4,
12828 so if we see it, we can assume that a constant form is really
12829 a constant and not a section offset. */
12830 if (attr_form_is_constant (attr))
12831 *offset = dwarf2_get_attr_constant_value (attr, 0);
12832 else if (attr_form_is_section_offset (attr))
12833 dwarf2_complex_location_expr_complaint ();
12834 else if (attr_form_is_block (attr))
12835 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12837 dwarf2_complex_location_expr_complaint ();
12845 /* Add an aggregate field to the field list. */
12848 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12849 struct dwarf2_cu *cu)
12851 struct objfile *objfile = cu->objfile;
12852 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12853 struct nextfield *new_field;
12854 struct attribute *attr;
12856 const char *fieldname = "";
12858 /* Allocate a new field list entry and link it in. */
12859 new_field = XNEW (struct nextfield);
12860 make_cleanup (xfree, new_field);
12861 memset (new_field, 0, sizeof (struct nextfield));
12863 if (die->tag == DW_TAG_inheritance)
12865 new_field->next = fip->baseclasses;
12866 fip->baseclasses = new_field;
12870 new_field->next = fip->fields;
12871 fip->fields = new_field;
12875 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12877 new_field->accessibility = DW_UNSND (attr);
12879 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12880 if (new_field->accessibility != DW_ACCESS_public)
12881 fip->non_public_fields = 1;
12883 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12885 new_field->virtuality = DW_UNSND (attr);
12887 new_field->virtuality = DW_VIRTUALITY_none;
12889 fp = &new_field->field;
12891 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12895 /* Data member other than a C++ static data member. */
12897 /* Get type of field. */
12898 fp->type = die_type (die, cu);
12900 SET_FIELD_BITPOS (*fp, 0);
12902 /* Get bit size of field (zero if none). */
12903 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12906 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12910 FIELD_BITSIZE (*fp) = 0;
12913 /* Get bit offset of field. */
12914 if (handle_data_member_location (die, cu, &offset))
12915 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12916 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12919 if (gdbarch_bits_big_endian (gdbarch))
12921 /* For big endian bits, the DW_AT_bit_offset gives the
12922 additional bit offset from the MSB of the containing
12923 anonymous object to the MSB of the field. We don't
12924 have to do anything special since we don't need to
12925 know the size of the anonymous object. */
12926 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12930 /* For little endian bits, compute the bit offset to the
12931 MSB of the anonymous object, subtract off the number of
12932 bits from the MSB of the field to the MSB of the
12933 object, and then subtract off the number of bits of
12934 the field itself. The result is the bit offset of
12935 the LSB of the field. */
12936 int anonymous_size;
12937 int bit_offset = DW_UNSND (attr);
12939 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12942 /* The size of the anonymous object containing
12943 the bit field is explicit, so use the
12944 indicated size (in bytes). */
12945 anonymous_size = DW_UNSND (attr);
12949 /* The size of the anonymous object containing
12950 the bit field must be inferred from the type
12951 attribute of the data member containing the
12953 anonymous_size = TYPE_LENGTH (fp->type);
12955 SET_FIELD_BITPOS (*fp,
12956 (FIELD_BITPOS (*fp)
12957 + anonymous_size * bits_per_byte
12958 - bit_offset - FIELD_BITSIZE (*fp)));
12961 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12963 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12964 + dwarf2_get_attr_constant_value (attr, 0)));
12966 /* Get name of field. */
12967 fieldname = dwarf2_name (die, cu);
12968 if (fieldname == NULL)
12971 /* The name is already allocated along with this objfile, so we don't
12972 need to duplicate it for the type. */
12973 fp->name = fieldname;
12975 /* Change accessibility for artificial fields (e.g. virtual table
12976 pointer or virtual base class pointer) to private. */
12977 if (dwarf2_attr (die, DW_AT_artificial, cu))
12979 FIELD_ARTIFICIAL (*fp) = 1;
12980 new_field->accessibility = DW_ACCESS_private;
12981 fip->non_public_fields = 1;
12984 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12986 /* C++ static member. */
12988 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12989 is a declaration, but all versions of G++ as of this writing
12990 (so through at least 3.2.1) incorrectly generate
12991 DW_TAG_variable tags. */
12993 const char *physname;
12995 /* Get name of field. */
12996 fieldname = dwarf2_name (die, cu);
12997 if (fieldname == NULL)
13000 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13002 /* Only create a symbol if this is an external value.
13003 new_symbol checks this and puts the value in the global symbol
13004 table, which we want. If it is not external, new_symbol
13005 will try to put the value in cu->list_in_scope which is wrong. */
13006 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13008 /* A static const member, not much different than an enum as far as
13009 we're concerned, except that we can support more types. */
13010 new_symbol (die, NULL, cu);
13013 /* Get physical name. */
13014 physname = dwarf2_physname (fieldname, die, cu);
13016 /* The name is already allocated along with this objfile, so we don't
13017 need to duplicate it for the type. */
13018 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
13019 FIELD_TYPE (*fp) = die_type (die, cu);
13020 FIELD_NAME (*fp) = fieldname;
13022 else if (die->tag == DW_TAG_inheritance)
13026 /* C++ base class field. */
13027 if (handle_data_member_location (die, cu, &offset))
13028 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13029 FIELD_BITSIZE (*fp) = 0;
13030 FIELD_TYPE (*fp) = die_type (die, cu);
13031 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13032 fip->nbaseclasses++;
13036 /* Add a typedef defined in the scope of the FIP's class. */
13039 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13040 struct dwarf2_cu *cu)
13042 struct typedef_field_list *new_field;
13043 struct typedef_field *fp;
13045 /* Allocate a new field list entry and link it in. */
13046 new_field = XCNEW (struct typedef_field_list);
13047 make_cleanup (xfree, new_field);
13049 gdb_assert (die->tag == DW_TAG_typedef);
13051 fp = &new_field->field;
13053 /* Get name of field. */
13054 fp->name = dwarf2_name (die, cu);
13055 if (fp->name == NULL)
13058 fp->type = read_type_die (die, cu);
13060 new_field->next = fip->typedef_field_list;
13061 fip->typedef_field_list = new_field;
13062 fip->typedef_field_list_count++;
13065 /* Create the vector of fields, and attach it to the type. */
13068 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13069 struct dwarf2_cu *cu)
13071 int nfields = fip->nfields;
13073 /* Record the field count, allocate space for the array of fields,
13074 and create blank accessibility bitfields if necessary. */
13075 TYPE_NFIELDS (type) = nfields;
13076 TYPE_FIELDS (type) = (struct field *)
13077 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13078 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13080 if (fip->non_public_fields && cu->language != language_ada)
13082 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13084 TYPE_FIELD_PRIVATE_BITS (type) =
13085 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13086 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13088 TYPE_FIELD_PROTECTED_BITS (type) =
13089 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13090 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13092 TYPE_FIELD_IGNORE_BITS (type) =
13093 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13094 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13097 /* If the type has baseclasses, allocate and clear a bit vector for
13098 TYPE_FIELD_VIRTUAL_BITS. */
13099 if (fip->nbaseclasses && cu->language != language_ada)
13101 int num_bytes = B_BYTES (fip->nbaseclasses);
13102 unsigned char *pointer;
13104 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13105 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13106 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13107 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13108 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13111 /* Copy the saved-up fields into the field vector. Start from the head of
13112 the list, adding to the tail of the field array, so that they end up in
13113 the same order in the array in which they were added to the list. */
13114 while (nfields-- > 0)
13116 struct nextfield *fieldp;
13120 fieldp = fip->fields;
13121 fip->fields = fieldp->next;
13125 fieldp = fip->baseclasses;
13126 fip->baseclasses = fieldp->next;
13129 TYPE_FIELD (type, nfields) = fieldp->field;
13130 switch (fieldp->accessibility)
13132 case DW_ACCESS_private:
13133 if (cu->language != language_ada)
13134 SET_TYPE_FIELD_PRIVATE (type, nfields);
13137 case DW_ACCESS_protected:
13138 if (cu->language != language_ada)
13139 SET_TYPE_FIELD_PROTECTED (type, nfields);
13142 case DW_ACCESS_public:
13146 /* Unknown accessibility. Complain and treat it as public. */
13148 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13149 fieldp->accessibility);
13153 if (nfields < fip->nbaseclasses)
13155 switch (fieldp->virtuality)
13157 case DW_VIRTUALITY_virtual:
13158 case DW_VIRTUALITY_pure_virtual:
13159 if (cu->language == language_ada)
13160 error (_("unexpected virtuality in component of Ada type"));
13161 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13168 /* Return true if this member function is a constructor, false
13172 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13174 const char *fieldname;
13175 const char *type_name;
13178 if (die->parent == NULL)
13181 if (die->parent->tag != DW_TAG_structure_type
13182 && die->parent->tag != DW_TAG_union_type
13183 && die->parent->tag != DW_TAG_class_type)
13186 fieldname = dwarf2_name (die, cu);
13187 type_name = dwarf2_name (die->parent, cu);
13188 if (fieldname == NULL || type_name == NULL)
13191 len = strlen (fieldname);
13192 return (strncmp (fieldname, type_name, len) == 0
13193 && (type_name[len] == '\0' || type_name[len] == '<'));
13196 /* Add a member function to the proper fieldlist. */
13199 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13200 struct type *type, struct dwarf2_cu *cu)
13202 struct objfile *objfile = cu->objfile;
13203 struct attribute *attr;
13204 struct fnfieldlist *flp;
13206 struct fn_field *fnp;
13207 const char *fieldname;
13208 struct nextfnfield *new_fnfield;
13209 struct type *this_type;
13210 enum dwarf_access_attribute accessibility;
13212 if (cu->language == language_ada)
13213 error (_("unexpected member function in Ada type"));
13215 /* Get name of member function. */
13216 fieldname = dwarf2_name (die, cu);
13217 if (fieldname == NULL)
13220 /* Look up member function name in fieldlist. */
13221 for (i = 0; i < fip->nfnfields; i++)
13223 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13227 /* Create new list element if necessary. */
13228 if (i < fip->nfnfields)
13229 flp = &fip->fnfieldlists[i];
13232 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13234 fip->fnfieldlists = (struct fnfieldlist *)
13235 xrealloc (fip->fnfieldlists,
13236 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13237 * sizeof (struct fnfieldlist));
13238 if (fip->nfnfields == 0)
13239 make_cleanup (free_current_contents, &fip->fnfieldlists);
13241 flp = &fip->fnfieldlists[fip->nfnfields];
13242 flp->name = fieldname;
13245 i = fip->nfnfields++;
13248 /* Create a new member function field and chain it to the field list
13250 new_fnfield = XNEW (struct nextfnfield);
13251 make_cleanup (xfree, new_fnfield);
13252 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13253 new_fnfield->next = flp->head;
13254 flp->head = new_fnfield;
13257 /* Fill in the member function field info. */
13258 fnp = &new_fnfield->fnfield;
13260 /* Delay processing of the physname until later. */
13261 if (cu->language == language_cplus)
13263 add_to_method_list (type, i, flp->length - 1, fieldname,
13268 const char *physname = dwarf2_physname (fieldname, die, cu);
13269 fnp->physname = physname ? physname : "";
13272 fnp->type = alloc_type (objfile);
13273 this_type = read_type_die (die, cu);
13274 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13276 int nparams = TYPE_NFIELDS (this_type);
13278 /* TYPE is the domain of this method, and THIS_TYPE is the type
13279 of the method itself (TYPE_CODE_METHOD). */
13280 smash_to_method_type (fnp->type, type,
13281 TYPE_TARGET_TYPE (this_type),
13282 TYPE_FIELDS (this_type),
13283 TYPE_NFIELDS (this_type),
13284 TYPE_VARARGS (this_type));
13286 /* Handle static member functions.
13287 Dwarf2 has no clean way to discern C++ static and non-static
13288 member functions. G++ helps GDB by marking the first
13289 parameter for non-static member functions (which is the this
13290 pointer) as artificial. We obtain this information from
13291 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13292 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13293 fnp->voffset = VOFFSET_STATIC;
13296 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13297 dwarf2_full_name (fieldname, die, cu));
13299 /* Get fcontext from DW_AT_containing_type if present. */
13300 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13301 fnp->fcontext = die_containing_type (die, cu);
13303 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13304 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13306 /* Get accessibility. */
13307 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13309 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13311 accessibility = dwarf2_default_access_attribute (die, cu);
13312 switch (accessibility)
13314 case DW_ACCESS_private:
13315 fnp->is_private = 1;
13317 case DW_ACCESS_protected:
13318 fnp->is_protected = 1;
13322 /* Check for artificial methods. */
13323 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13324 if (attr && DW_UNSND (attr) != 0)
13325 fnp->is_artificial = 1;
13327 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13329 /* Get index in virtual function table if it is a virtual member
13330 function. For older versions of GCC, this is an offset in the
13331 appropriate virtual table, as specified by DW_AT_containing_type.
13332 For everyone else, it is an expression to be evaluated relative
13333 to the object address. */
13335 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13338 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13340 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13342 /* Old-style GCC. */
13343 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13345 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13346 || (DW_BLOCK (attr)->size > 1
13347 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13348 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13350 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13351 if ((fnp->voffset % cu->header.addr_size) != 0)
13352 dwarf2_complex_location_expr_complaint ();
13354 fnp->voffset /= cu->header.addr_size;
13358 dwarf2_complex_location_expr_complaint ();
13360 if (!fnp->fcontext)
13362 /* If there is no `this' field and no DW_AT_containing_type,
13363 we cannot actually find a base class context for the
13365 if (TYPE_NFIELDS (this_type) == 0
13366 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13368 complaint (&symfile_complaints,
13369 _("cannot determine context for virtual member "
13370 "function \"%s\" (offset %d)"),
13371 fieldname, to_underlying (die->sect_off));
13376 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13380 else if (attr_form_is_section_offset (attr))
13382 dwarf2_complex_location_expr_complaint ();
13386 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13392 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13393 if (attr && DW_UNSND (attr))
13395 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13396 complaint (&symfile_complaints,
13397 _("Member function \"%s\" (offset %d) is virtual "
13398 "but the vtable offset is not specified"),
13399 fieldname, to_underlying (die->sect_off));
13400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13401 TYPE_CPLUS_DYNAMIC (type) = 1;
13406 /* Create the vector of member function fields, and attach it to the type. */
13409 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13410 struct dwarf2_cu *cu)
13412 struct fnfieldlist *flp;
13415 if (cu->language == language_ada)
13416 error (_("unexpected member functions in Ada type"));
13418 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13419 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13420 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13422 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13424 struct nextfnfield *nfp = flp->head;
13425 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13428 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13429 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13430 fn_flp->fn_fields = (struct fn_field *)
13431 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13432 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13433 fn_flp->fn_fields[k] = nfp->fnfield;
13436 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13439 /* Returns non-zero if NAME is the name of a vtable member in CU's
13440 language, zero otherwise. */
13442 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13444 static const char vptr[] = "_vptr";
13445 static const char vtable[] = "vtable";
13447 /* Look for the C++ form of the vtable. */
13448 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13454 /* GCC outputs unnamed structures that are really pointers to member
13455 functions, with the ABI-specified layout. If TYPE describes
13456 such a structure, smash it into a member function type.
13458 GCC shouldn't do this; it should just output pointer to member DIEs.
13459 This is GCC PR debug/28767. */
13462 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13464 struct type *pfn_type, *self_type, *new_type;
13466 /* Check for a structure with no name and two children. */
13467 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13470 /* Check for __pfn and __delta members. */
13471 if (TYPE_FIELD_NAME (type, 0) == NULL
13472 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13473 || TYPE_FIELD_NAME (type, 1) == NULL
13474 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13477 /* Find the type of the method. */
13478 pfn_type = TYPE_FIELD_TYPE (type, 0);
13479 if (pfn_type == NULL
13480 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13481 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13484 /* Look for the "this" argument. */
13485 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13486 if (TYPE_NFIELDS (pfn_type) == 0
13487 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13488 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13491 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13492 new_type = alloc_type (objfile);
13493 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13494 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13495 TYPE_VARARGS (pfn_type));
13496 smash_to_methodptr_type (type, new_type);
13499 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13503 producer_is_icc (struct dwarf2_cu *cu)
13505 if (!cu->checked_producer)
13506 check_producer (cu);
13508 return cu->producer_is_icc;
13511 /* Called when we find the DIE that starts a structure or union scope
13512 (definition) to create a type for the structure or union. Fill in
13513 the type's name and general properties; the members will not be
13514 processed until process_structure_scope. A symbol table entry for
13515 the type will also not be done until process_structure_scope (assuming
13516 the type has a name).
13518 NOTE: we need to call these functions regardless of whether or not the
13519 DIE has a DW_AT_name attribute, since it might be an anonymous
13520 structure or union. This gets the type entered into our set of
13521 user defined types. */
13523 static struct type *
13524 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13526 struct objfile *objfile = cu->objfile;
13528 struct attribute *attr;
13531 /* If the definition of this type lives in .debug_types, read that type.
13532 Don't follow DW_AT_specification though, that will take us back up
13533 the chain and we want to go down. */
13534 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13537 type = get_DW_AT_signature_type (die, attr, cu);
13539 /* The type's CU may not be the same as CU.
13540 Ensure TYPE is recorded with CU in die_type_hash. */
13541 return set_die_type (die, type, cu);
13544 type = alloc_type (objfile);
13545 INIT_CPLUS_SPECIFIC (type);
13547 name = dwarf2_name (die, cu);
13550 if (cu->language == language_cplus
13551 || cu->language == language_d
13552 || cu->language == language_rust)
13554 const char *full_name = dwarf2_full_name (name, die, cu);
13556 /* dwarf2_full_name might have already finished building the DIE's
13557 type. If so, there is no need to continue. */
13558 if (get_die_type (die, cu) != NULL)
13559 return get_die_type (die, cu);
13561 TYPE_TAG_NAME (type) = full_name;
13562 if (die->tag == DW_TAG_structure_type
13563 || die->tag == DW_TAG_class_type)
13564 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13568 /* The name is already allocated along with this objfile, so
13569 we don't need to duplicate it for the type. */
13570 TYPE_TAG_NAME (type) = name;
13571 if (die->tag == DW_TAG_class_type)
13572 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13576 if (die->tag == DW_TAG_structure_type)
13578 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13580 else if (die->tag == DW_TAG_union_type)
13582 TYPE_CODE (type) = TYPE_CODE_UNION;
13586 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13589 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13590 TYPE_DECLARED_CLASS (type) = 1;
13592 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13595 if (attr_form_is_constant (attr))
13596 TYPE_LENGTH (type) = DW_UNSND (attr);
13599 /* For the moment, dynamic type sizes are not supported
13600 by GDB's struct type. The actual size is determined
13601 on-demand when resolving the type of a given object,
13602 so set the type's length to zero for now. Otherwise,
13603 we record an expression as the length, and that expression
13604 could lead to a very large value, which could eventually
13605 lead to us trying to allocate that much memory when creating
13606 a value of that type. */
13607 TYPE_LENGTH (type) = 0;
13612 TYPE_LENGTH (type) = 0;
13615 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13617 /* ICC does not output the required DW_AT_declaration
13618 on incomplete types, but gives them a size of zero. */
13619 TYPE_STUB (type) = 1;
13622 TYPE_STUB_SUPPORTED (type) = 1;
13624 if (die_is_declaration (die, cu))
13625 TYPE_STUB (type) = 1;
13626 else if (attr == NULL && die->child == NULL
13627 && producer_is_realview (cu->producer))
13628 /* RealView does not output the required DW_AT_declaration
13629 on incomplete types. */
13630 TYPE_STUB (type) = 1;
13632 /* We need to add the type field to the die immediately so we don't
13633 infinitely recurse when dealing with pointers to the structure
13634 type within the structure itself. */
13635 set_die_type (die, type, cu);
13637 /* set_die_type should be already done. */
13638 set_descriptive_type (type, die, cu);
13643 /* Finish creating a structure or union type, including filling in
13644 its members and creating a symbol for it. */
13647 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13649 struct objfile *objfile = cu->objfile;
13650 struct die_info *child_die;
13653 type = get_die_type (die, cu);
13655 type = read_structure_type (die, cu);
13657 if (die->child != NULL && ! die_is_declaration (die, cu))
13659 struct field_info fi;
13660 VEC (symbolp) *template_args = NULL;
13661 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13663 memset (&fi, 0, sizeof (struct field_info));
13665 child_die = die->child;
13667 while (child_die && child_die->tag)
13669 if (child_die->tag == DW_TAG_member
13670 || child_die->tag == DW_TAG_variable)
13672 /* NOTE: carlton/2002-11-05: A C++ static data member
13673 should be a DW_TAG_member that is a declaration, but
13674 all versions of G++ as of this writing (so through at
13675 least 3.2.1) incorrectly generate DW_TAG_variable
13676 tags for them instead. */
13677 dwarf2_add_field (&fi, child_die, cu);
13679 else if (child_die->tag == DW_TAG_subprogram)
13681 /* Rust doesn't have member functions in the C++ sense.
13682 However, it does emit ordinary functions as children
13683 of a struct DIE. */
13684 if (cu->language == language_rust)
13685 read_func_scope (child_die, cu);
13688 /* C++ member function. */
13689 dwarf2_add_member_fn (&fi, child_die, type, cu);
13692 else if (child_die->tag == DW_TAG_inheritance)
13694 /* C++ base class field. */
13695 dwarf2_add_field (&fi, child_die, cu);
13697 else if (child_die->tag == DW_TAG_typedef)
13698 dwarf2_add_typedef (&fi, child_die, cu);
13699 else if (child_die->tag == DW_TAG_template_type_param
13700 || child_die->tag == DW_TAG_template_value_param)
13702 struct symbol *arg = new_symbol (child_die, NULL, cu);
13705 VEC_safe_push (symbolp, template_args, arg);
13708 child_die = sibling_die (child_die);
13711 /* Attach template arguments to type. */
13712 if (! VEC_empty (symbolp, template_args))
13714 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13715 TYPE_N_TEMPLATE_ARGUMENTS (type)
13716 = VEC_length (symbolp, template_args);
13717 TYPE_TEMPLATE_ARGUMENTS (type)
13718 = XOBNEWVEC (&objfile->objfile_obstack,
13720 TYPE_N_TEMPLATE_ARGUMENTS (type));
13721 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13722 VEC_address (symbolp, template_args),
13723 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13724 * sizeof (struct symbol *)));
13725 VEC_free (symbolp, template_args);
13728 /* Attach fields and member functions to the type. */
13730 dwarf2_attach_fields_to_type (&fi, type, cu);
13733 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13735 /* Get the type which refers to the base class (possibly this
13736 class itself) which contains the vtable pointer for the current
13737 class from the DW_AT_containing_type attribute. This use of
13738 DW_AT_containing_type is a GNU extension. */
13740 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13742 struct type *t = die_containing_type (die, cu);
13744 set_type_vptr_basetype (type, t);
13749 /* Our own class provides vtbl ptr. */
13750 for (i = TYPE_NFIELDS (t) - 1;
13751 i >= TYPE_N_BASECLASSES (t);
13754 const char *fieldname = TYPE_FIELD_NAME (t, i);
13756 if (is_vtable_name (fieldname, cu))
13758 set_type_vptr_fieldno (type, i);
13763 /* Complain if virtual function table field not found. */
13764 if (i < TYPE_N_BASECLASSES (t))
13765 complaint (&symfile_complaints,
13766 _("virtual function table pointer "
13767 "not found when defining class '%s'"),
13768 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13773 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13776 else if (cu->producer
13777 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13779 /* The IBM XLC compiler does not provide direct indication
13780 of the containing type, but the vtable pointer is
13781 always named __vfp. */
13785 for (i = TYPE_NFIELDS (type) - 1;
13786 i >= TYPE_N_BASECLASSES (type);
13789 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13791 set_type_vptr_fieldno (type, i);
13792 set_type_vptr_basetype (type, type);
13799 /* Copy fi.typedef_field_list linked list elements content into the
13800 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13801 if (fi.typedef_field_list)
13803 int i = fi.typedef_field_list_count;
13805 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13806 TYPE_TYPEDEF_FIELD_ARRAY (type)
13807 = ((struct typedef_field *)
13808 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13809 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13811 /* Reverse the list order to keep the debug info elements order. */
13814 struct typedef_field *dest, *src;
13816 dest = &TYPE_TYPEDEF_FIELD (type, i);
13817 src = &fi.typedef_field_list->field;
13818 fi.typedef_field_list = fi.typedef_field_list->next;
13823 do_cleanups (back_to);
13826 quirk_gcc_member_function_pointer (type, objfile);
13828 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13829 snapshots) has been known to create a die giving a declaration
13830 for a class that has, as a child, a die giving a definition for a
13831 nested class. So we have to process our children even if the
13832 current die is a declaration. Normally, of course, a declaration
13833 won't have any children at all. */
13835 child_die = die->child;
13837 while (child_die != NULL && child_die->tag)
13839 if (child_die->tag == DW_TAG_member
13840 || child_die->tag == DW_TAG_variable
13841 || child_die->tag == DW_TAG_inheritance
13842 || child_die->tag == DW_TAG_template_value_param
13843 || child_die->tag == DW_TAG_template_type_param)
13848 process_die (child_die, cu);
13850 child_die = sibling_die (child_die);
13853 /* Do not consider external references. According to the DWARF standard,
13854 these DIEs are identified by the fact that they have no byte_size
13855 attribute, and a declaration attribute. */
13856 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13857 || !die_is_declaration (die, cu))
13858 new_symbol (die, type, cu);
13861 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13862 update TYPE using some information only available in DIE's children. */
13865 update_enumeration_type_from_children (struct die_info *die,
13867 struct dwarf2_cu *cu)
13869 struct die_info *child_die;
13870 int unsigned_enum = 1;
13874 auto_obstack obstack;
13876 for (child_die = die->child;
13877 child_die != NULL && child_die->tag;
13878 child_die = sibling_die (child_die))
13880 struct attribute *attr;
13882 const gdb_byte *bytes;
13883 struct dwarf2_locexpr_baton *baton;
13886 if (child_die->tag != DW_TAG_enumerator)
13889 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13893 name = dwarf2_name (child_die, cu);
13895 name = "<anonymous enumerator>";
13897 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13898 &value, &bytes, &baton);
13904 else if ((mask & value) != 0)
13909 /* If we already know that the enum type is neither unsigned, nor
13910 a flag type, no need to look at the rest of the enumerates. */
13911 if (!unsigned_enum && !flag_enum)
13916 TYPE_UNSIGNED (type) = 1;
13918 TYPE_FLAG_ENUM (type) = 1;
13921 /* Given a DW_AT_enumeration_type die, set its type. We do not
13922 complete the type's fields yet, or create any symbols. */
13924 static struct type *
13925 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13927 struct objfile *objfile = cu->objfile;
13929 struct attribute *attr;
13932 /* If the definition of this type lives in .debug_types, read that type.
13933 Don't follow DW_AT_specification though, that will take us back up
13934 the chain and we want to go down. */
13935 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13938 type = get_DW_AT_signature_type (die, attr, cu);
13940 /* The type's CU may not be the same as CU.
13941 Ensure TYPE is recorded with CU in die_type_hash. */
13942 return set_die_type (die, type, cu);
13945 type = alloc_type (objfile);
13947 TYPE_CODE (type) = TYPE_CODE_ENUM;
13948 name = dwarf2_full_name (NULL, die, cu);
13950 TYPE_TAG_NAME (type) = name;
13952 attr = dwarf2_attr (die, DW_AT_type, cu);
13955 struct type *underlying_type = die_type (die, cu);
13957 TYPE_TARGET_TYPE (type) = underlying_type;
13960 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13963 TYPE_LENGTH (type) = DW_UNSND (attr);
13967 TYPE_LENGTH (type) = 0;
13970 /* The enumeration DIE can be incomplete. In Ada, any type can be
13971 declared as private in the package spec, and then defined only
13972 inside the package body. Such types are known as Taft Amendment
13973 Types. When another package uses such a type, an incomplete DIE
13974 may be generated by the compiler. */
13975 if (die_is_declaration (die, cu))
13976 TYPE_STUB (type) = 1;
13978 /* Finish the creation of this type by using the enum's children.
13979 We must call this even when the underlying type has been provided
13980 so that we can determine if we're looking at a "flag" enum. */
13981 update_enumeration_type_from_children (die, type, cu);
13983 /* If this type has an underlying type that is not a stub, then we
13984 may use its attributes. We always use the "unsigned" attribute
13985 in this situation, because ordinarily we guess whether the type
13986 is unsigned -- but the guess can be wrong and the underlying type
13987 can tell us the reality. However, we defer to a local size
13988 attribute if one exists, because this lets the compiler override
13989 the underlying type if needed. */
13990 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13992 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13993 if (TYPE_LENGTH (type) == 0)
13994 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13997 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13999 return set_die_type (die, type, cu);
14002 /* Given a pointer to a die which begins an enumeration, process all
14003 the dies that define the members of the enumeration, and create the
14004 symbol for the enumeration type.
14006 NOTE: We reverse the order of the element list. */
14009 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14011 struct type *this_type;
14013 this_type = get_die_type (die, cu);
14014 if (this_type == NULL)
14015 this_type = read_enumeration_type (die, cu);
14017 if (die->child != NULL)
14019 struct die_info *child_die;
14020 struct symbol *sym;
14021 struct field *fields = NULL;
14022 int num_fields = 0;
14025 child_die = die->child;
14026 while (child_die && child_die->tag)
14028 if (child_die->tag != DW_TAG_enumerator)
14030 process_die (child_die, cu);
14034 name = dwarf2_name (child_die, cu);
14037 sym = new_symbol (child_die, this_type, cu);
14039 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14041 fields = (struct field *)
14043 (num_fields + DW_FIELD_ALLOC_CHUNK)
14044 * sizeof (struct field));
14047 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
14048 FIELD_TYPE (fields[num_fields]) = NULL;
14049 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
14050 FIELD_BITSIZE (fields[num_fields]) = 0;
14056 child_die = sibling_die (child_die);
14061 TYPE_NFIELDS (this_type) = num_fields;
14062 TYPE_FIELDS (this_type) = (struct field *)
14063 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14064 memcpy (TYPE_FIELDS (this_type), fields,
14065 sizeof (struct field) * num_fields);
14070 /* If we are reading an enum from a .debug_types unit, and the enum
14071 is a declaration, and the enum is not the signatured type in the
14072 unit, then we do not want to add a symbol for it. Adding a
14073 symbol would in some cases obscure the true definition of the
14074 enum, giving users an incomplete type when the definition is
14075 actually available. Note that we do not want to do this for all
14076 enums which are just declarations, because C++0x allows forward
14077 enum declarations. */
14078 if (cu->per_cu->is_debug_types
14079 && die_is_declaration (die, cu))
14081 struct signatured_type *sig_type;
14083 sig_type = (struct signatured_type *) cu->per_cu;
14084 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14085 if (sig_type->type_offset_in_section != die->sect_off)
14089 new_symbol (die, this_type, cu);
14092 /* Extract all information from a DW_TAG_array_type DIE and put it in
14093 the DIE's type field. For now, this only handles one dimensional
14096 static struct type *
14097 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14099 struct objfile *objfile = cu->objfile;
14100 struct die_info *child_die;
14102 struct type *element_type, *range_type, *index_type;
14103 struct type **range_types = NULL;
14104 struct attribute *attr;
14106 struct cleanup *back_to;
14108 unsigned int bit_stride = 0;
14110 element_type = die_type (die, cu);
14112 /* The die_type call above may have already set the type for this DIE. */
14113 type = get_die_type (die, cu);
14117 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14119 bit_stride = DW_UNSND (attr) * 8;
14121 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14123 bit_stride = DW_UNSND (attr);
14125 /* Irix 6.2 native cc creates array types without children for
14126 arrays with unspecified length. */
14127 if (die->child == NULL)
14129 index_type = objfile_type (objfile)->builtin_int;
14130 range_type = create_static_range_type (NULL, index_type, 0, -1);
14131 type = create_array_type_with_stride (NULL, element_type, range_type,
14133 return set_die_type (die, type, cu);
14136 back_to = make_cleanup (null_cleanup, NULL);
14137 child_die = die->child;
14138 while (child_die && child_die->tag)
14140 if (child_die->tag == DW_TAG_subrange_type)
14142 struct type *child_type = read_type_die (child_die, cu);
14144 if (child_type != NULL)
14146 /* The range type was succesfully read. Save it for the
14147 array type creation. */
14148 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14150 range_types = (struct type **)
14151 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14152 * sizeof (struct type *));
14154 make_cleanup (free_current_contents, &range_types);
14156 range_types[ndim++] = child_type;
14159 child_die = sibling_die (child_die);
14162 /* Dwarf2 dimensions are output from left to right, create the
14163 necessary array types in backwards order. */
14165 type = element_type;
14167 if (read_array_order (die, cu) == DW_ORD_col_major)
14172 type = create_array_type_with_stride (NULL, type, range_types[i++],
14178 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14182 /* Understand Dwarf2 support for vector types (like they occur on
14183 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14184 array type. This is not part of the Dwarf2/3 standard yet, but a
14185 custom vendor extension. The main difference between a regular
14186 array and the vector variant is that vectors are passed by value
14188 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14190 make_vector_type (type);
14192 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14193 implementation may choose to implement triple vectors using this
14195 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14198 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14199 TYPE_LENGTH (type) = DW_UNSND (attr);
14201 complaint (&symfile_complaints,
14202 _("DW_AT_byte_size for array type smaller "
14203 "than the total size of elements"));
14206 name = dwarf2_name (die, cu);
14208 TYPE_NAME (type) = name;
14210 /* Install the type in the die. */
14211 set_die_type (die, type, cu);
14213 /* set_die_type should be already done. */
14214 set_descriptive_type (type, die, cu);
14216 do_cleanups (back_to);
14221 static enum dwarf_array_dim_ordering
14222 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14224 struct attribute *attr;
14226 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14229 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14231 /* GNU F77 is a special case, as at 08/2004 array type info is the
14232 opposite order to the dwarf2 specification, but data is still
14233 laid out as per normal fortran.
14235 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14236 version checking. */
14238 if (cu->language == language_fortran
14239 && cu->producer && strstr (cu->producer, "GNU F77"))
14241 return DW_ORD_row_major;
14244 switch (cu->language_defn->la_array_ordering)
14246 case array_column_major:
14247 return DW_ORD_col_major;
14248 case array_row_major:
14250 return DW_ORD_row_major;
14254 /* Extract all information from a DW_TAG_set_type DIE and put it in
14255 the DIE's type field. */
14257 static struct type *
14258 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14260 struct type *domain_type, *set_type;
14261 struct attribute *attr;
14263 domain_type = die_type (die, cu);
14265 /* The die_type call above may have already set the type for this DIE. */
14266 set_type = get_die_type (die, cu);
14270 set_type = create_set_type (NULL, domain_type);
14272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14274 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14276 return set_die_type (die, set_type, cu);
14279 /* A helper for read_common_block that creates a locexpr baton.
14280 SYM is the symbol which we are marking as computed.
14281 COMMON_DIE is the DIE for the common block.
14282 COMMON_LOC is the location expression attribute for the common
14284 MEMBER_LOC is the location expression attribute for the particular
14285 member of the common block that we are processing.
14286 CU is the CU from which the above come. */
14289 mark_common_block_symbol_computed (struct symbol *sym,
14290 struct die_info *common_die,
14291 struct attribute *common_loc,
14292 struct attribute *member_loc,
14293 struct dwarf2_cu *cu)
14295 struct objfile *objfile = dwarf2_per_objfile->objfile;
14296 struct dwarf2_locexpr_baton *baton;
14298 unsigned int cu_off;
14299 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14300 LONGEST offset = 0;
14302 gdb_assert (common_loc && member_loc);
14303 gdb_assert (attr_form_is_block (common_loc));
14304 gdb_assert (attr_form_is_block (member_loc)
14305 || attr_form_is_constant (member_loc));
14307 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14308 baton->per_cu = cu->per_cu;
14309 gdb_assert (baton->per_cu);
14311 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14313 if (attr_form_is_constant (member_loc))
14315 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14316 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14319 baton->size += DW_BLOCK (member_loc)->size;
14321 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14324 *ptr++ = DW_OP_call4;
14325 cu_off = common_die->sect_off - cu->per_cu->sect_off;
14326 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14329 if (attr_form_is_constant (member_loc))
14331 *ptr++ = DW_OP_addr;
14332 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14333 ptr += cu->header.addr_size;
14337 /* We have to copy the data here, because DW_OP_call4 will only
14338 use a DW_AT_location attribute. */
14339 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14340 ptr += DW_BLOCK (member_loc)->size;
14343 *ptr++ = DW_OP_plus;
14344 gdb_assert (ptr - baton->data == baton->size);
14346 SYMBOL_LOCATION_BATON (sym) = baton;
14347 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14350 /* Create appropriate locally-scoped variables for all the
14351 DW_TAG_common_block entries. Also create a struct common_block
14352 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14353 is used to sepate the common blocks name namespace from regular
14357 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14359 struct attribute *attr;
14361 attr = dwarf2_attr (die, DW_AT_location, cu);
14364 /* Support the .debug_loc offsets. */
14365 if (attr_form_is_block (attr))
14369 else if (attr_form_is_section_offset (attr))
14371 dwarf2_complex_location_expr_complaint ();
14376 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14377 "common block member");
14382 if (die->child != NULL)
14384 struct objfile *objfile = cu->objfile;
14385 struct die_info *child_die;
14386 size_t n_entries = 0, size;
14387 struct common_block *common_block;
14388 struct symbol *sym;
14390 for (child_die = die->child;
14391 child_die && child_die->tag;
14392 child_die = sibling_die (child_die))
14395 size = (sizeof (struct common_block)
14396 + (n_entries - 1) * sizeof (struct symbol *));
14398 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14400 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14401 common_block->n_entries = 0;
14403 for (child_die = die->child;
14404 child_die && child_die->tag;
14405 child_die = sibling_die (child_die))
14407 /* Create the symbol in the DW_TAG_common_block block in the current
14409 sym = new_symbol (child_die, NULL, cu);
14412 struct attribute *member_loc;
14414 common_block->contents[common_block->n_entries++] = sym;
14416 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14420 /* GDB has handled this for a long time, but it is
14421 not specified by DWARF. It seems to have been
14422 emitted by gfortran at least as recently as:
14423 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14424 complaint (&symfile_complaints,
14425 _("Variable in common block has "
14426 "DW_AT_data_member_location "
14427 "- DIE at 0x%x [in module %s]"),
14428 to_underlying (child_die->sect_off),
14429 objfile_name (cu->objfile));
14431 if (attr_form_is_section_offset (member_loc))
14432 dwarf2_complex_location_expr_complaint ();
14433 else if (attr_form_is_constant (member_loc)
14434 || attr_form_is_block (member_loc))
14437 mark_common_block_symbol_computed (sym, die, attr,
14441 dwarf2_complex_location_expr_complaint ();
14446 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14447 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14451 /* Create a type for a C++ namespace. */
14453 static struct type *
14454 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14456 struct objfile *objfile = cu->objfile;
14457 const char *previous_prefix, *name;
14461 /* For extensions, reuse the type of the original namespace. */
14462 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14464 struct die_info *ext_die;
14465 struct dwarf2_cu *ext_cu = cu;
14467 ext_die = dwarf2_extension (die, &ext_cu);
14468 type = read_type_die (ext_die, ext_cu);
14470 /* EXT_CU may not be the same as CU.
14471 Ensure TYPE is recorded with CU in die_type_hash. */
14472 return set_die_type (die, type, cu);
14475 name = namespace_name (die, &is_anonymous, cu);
14477 /* Now build the name of the current namespace. */
14479 previous_prefix = determine_prefix (die, cu);
14480 if (previous_prefix[0] != '\0')
14481 name = typename_concat (&objfile->objfile_obstack,
14482 previous_prefix, name, 0, cu);
14484 /* Create the type. */
14485 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14486 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14488 return set_die_type (die, type, cu);
14491 /* Read a namespace scope. */
14494 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14496 struct objfile *objfile = cu->objfile;
14499 /* Add a symbol associated to this if we haven't seen the namespace
14500 before. Also, add a using directive if it's an anonymous
14503 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14507 type = read_type_die (die, cu);
14508 new_symbol (die, type, cu);
14510 namespace_name (die, &is_anonymous, cu);
14513 const char *previous_prefix = determine_prefix (die, cu);
14515 add_using_directive (using_directives (cu->language),
14516 previous_prefix, TYPE_NAME (type), NULL,
14517 NULL, NULL, 0, &objfile->objfile_obstack);
14521 if (die->child != NULL)
14523 struct die_info *child_die = die->child;
14525 while (child_die && child_die->tag)
14527 process_die (child_die, cu);
14528 child_die = sibling_die (child_die);
14533 /* Read a Fortran module as type. This DIE can be only a declaration used for
14534 imported module. Still we need that type as local Fortran "use ... only"
14535 declaration imports depend on the created type in determine_prefix. */
14537 static struct type *
14538 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14540 struct objfile *objfile = cu->objfile;
14541 const char *module_name;
14544 module_name = dwarf2_name (die, cu);
14546 complaint (&symfile_complaints,
14547 _("DW_TAG_module has no name, offset 0x%x"),
14548 to_underlying (die->sect_off));
14549 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14551 /* determine_prefix uses TYPE_TAG_NAME. */
14552 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14554 return set_die_type (die, type, cu);
14557 /* Read a Fortran module. */
14560 read_module (struct die_info *die, struct dwarf2_cu *cu)
14562 struct die_info *child_die = die->child;
14565 type = read_type_die (die, cu);
14566 new_symbol (die, type, cu);
14568 while (child_die && child_die->tag)
14570 process_die (child_die, cu);
14571 child_die = sibling_die (child_die);
14575 /* Return the name of the namespace represented by DIE. Set
14576 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14579 static const char *
14580 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14582 struct die_info *current_die;
14583 const char *name = NULL;
14585 /* Loop through the extensions until we find a name. */
14587 for (current_die = die;
14588 current_die != NULL;
14589 current_die = dwarf2_extension (die, &cu))
14591 /* We don't use dwarf2_name here so that we can detect the absence
14592 of a name -> anonymous namespace. */
14593 name = dwarf2_string_attr (die, DW_AT_name, cu);
14599 /* Is it an anonymous namespace? */
14601 *is_anonymous = (name == NULL);
14603 name = CP_ANONYMOUS_NAMESPACE_STR;
14608 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14609 the user defined type vector. */
14611 static struct type *
14612 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14614 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14615 struct comp_unit_head *cu_header = &cu->header;
14617 struct attribute *attr_byte_size;
14618 struct attribute *attr_address_class;
14619 int byte_size, addr_class;
14620 struct type *target_type;
14622 target_type = die_type (die, cu);
14624 /* The die_type call above may have already set the type for this DIE. */
14625 type = get_die_type (die, cu);
14629 type = lookup_pointer_type (target_type);
14631 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14632 if (attr_byte_size)
14633 byte_size = DW_UNSND (attr_byte_size);
14635 byte_size = cu_header->addr_size;
14637 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14638 if (attr_address_class)
14639 addr_class = DW_UNSND (attr_address_class);
14641 addr_class = DW_ADDR_none;
14643 /* If the pointer size or address class is different than the
14644 default, create a type variant marked as such and set the
14645 length accordingly. */
14646 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14648 if (gdbarch_address_class_type_flags_p (gdbarch))
14652 type_flags = gdbarch_address_class_type_flags
14653 (gdbarch, byte_size, addr_class);
14654 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14656 type = make_type_with_address_space (type, type_flags);
14658 else if (TYPE_LENGTH (type) != byte_size)
14660 complaint (&symfile_complaints,
14661 _("invalid pointer size %d"), byte_size);
14665 /* Should we also complain about unhandled address classes? */
14669 TYPE_LENGTH (type) = byte_size;
14670 return set_die_type (die, type, cu);
14673 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14674 the user defined type vector. */
14676 static struct type *
14677 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14680 struct type *to_type;
14681 struct type *domain;
14683 to_type = die_type (die, cu);
14684 domain = die_containing_type (die, cu);
14686 /* The calls above may have already set the type for this DIE. */
14687 type = get_die_type (die, cu);
14691 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14692 type = lookup_methodptr_type (to_type);
14693 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14695 struct type *new_type = alloc_type (cu->objfile);
14697 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14698 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14699 TYPE_VARARGS (to_type));
14700 type = lookup_methodptr_type (new_type);
14703 type = lookup_memberptr_type (to_type, domain);
14705 return set_die_type (die, type, cu);
14708 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14709 the user defined type vector. */
14711 static struct type *
14712 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14713 enum type_code refcode)
14715 struct comp_unit_head *cu_header = &cu->header;
14716 struct type *type, *target_type;
14717 struct attribute *attr;
14719 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14721 target_type = die_type (die, cu);
14723 /* The die_type call above may have already set the type for this DIE. */
14724 type = get_die_type (die, cu);
14728 type = lookup_reference_type (target_type, refcode);
14729 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14732 TYPE_LENGTH (type) = DW_UNSND (attr);
14736 TYPE_LENGTH (type) = cu_header->addr_size;
14738 return set_die_type (die, type, cu);
14741 /* Add the given cv-qualifiers to the element type of the array. GCC
14742 outputs DWARF type qualifiers that apply to an array, not the
14743 element type. But GDB relies on the array element type to carry
14744 the cv-qualifiers. This mimics section 6.7.3 of the C99
14747 static struct type *
14748 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14749 struct type *base_type, int cnst, int voltl)
14751 struct type *el_type, *inner_array;
14753 base_type = copy_type (base_type);
14754 inner_array = base_type;
14756 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14758 TYPE_TARGET_TYPE (inner_array) =
14759 copy_type (TYPE_TARGET_TYPE (inner_array));
14760 inner_array = TYPE_TARGET_TYPE (inner_array);
14763 el_type = TYPE_TARGET_TYPE (inner_array);
14764 cnst |= TYPE_CONST (el_type);
14765 voltl |= TYPE_VOLATILE (el_type);
14766 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14768 return set_die_type (die, base_type, cu);
14771 static struct type *
14772 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14774 struct type *base_type, *cv_type;
14776 base_type = die_type (die, cu);
14778 /* The die_type call above may have already set the type for this DIE. */
14779 cv_type = get_die_type (die, cu);
14783 /* In case the const qualifier is applied to an array type, the element type
14784 is so qualified, not the array type (section 6.7.3 of C99). */
14785 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14786 return add_array_cv_type (die, cu, base_type, 1, 0);
14788 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14789 return set_die_type (die, cv_type, cu);
14792 static struct type *
14793 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14795 struct type *base_type, *cv_type;
14797 base_type = die_type (die, cu);
14799 /* The die_type call above may have already set the type for this DIE. */
14800 cv_type = get_die_type (die, cu);
14804 /* In case the volatile qualifier is applied to an array type, the
14805 element type is so qualified, not the array type (section 6.7.3
14807 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14808 return add_array_cv_type (die, cu, base_type, 0, 1);
14810 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14811 return set_die_type (die, cv_type, cu);
14814 /* Handle DW_TAG_restrict_type. */
14816 static struct type *
14817 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14819 struct type *base_type, *cv_type;
14821 base_type = die_type (die, cu);
14823 /* The die_type call above may have already set the type for this DIE. */
14824 cv_type = get_die_type (die, cu);
14828 cv_type = make_restrict_type (base_type);
14829 return set_die_type (die, cv_type, cu);
14832 /* Handle DW_TAG_atomic_type. */
14834 static struct type *
14835 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14837 struct type *base_type, *cv_type;
14839 base_type = die_type (die, cu);
14841 /* The die_type call above may have already set the type for this DIE. */
14842 cv_type = get_die_type (die, cu);
14846 cv_type = make_atomic_type (base_type);
14847 return set_die_type (die, cv_type, cu);
14850 /* Extract all information from a DW_TAG_string_type DIE and add to
14851 the user defined type vector. It isn't really a user defined type,
14852 but it behaves like one, with other DIE's using an AT_user_def_type
14853 attribute to reference it. */
14855 static struct type *
14856 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14858 struct objfile *objfile = cu->objfile;
14859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14860 struct type *type, *range_type, *index_type, *char_type;
14861 struct attribute *attr;
14862 unsigned int length;
14864 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14867 length = DW_UNSND (attr);
14871 /* Check for the DW_AT_byte_size attribute. */
14872 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14875 length = DW_UNSND (attr);
14883 index_type = objfile_type (objfile)->builtin_int;
14884 range_type = create_static_range_type (NULL, index_type, 1, length);
14885 char_type = language_string_char_type (cu->language_defn, gdbarch);
14886 type = create_string_type (NULL, char_type, range_type);
14888 return set_die_type (die, type, cu);
14891 /* Assuming that DIE corresponds to a function, returns nonzero
14892 if the function is prototyped. */
14895 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14897 struct attribute *attr;
14899 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14900 if (attr && (DW_UNSND (attr) != 0))
14903 /* The DWARF standard implies that the DW_AT_prototyped attribute
14904 is only meaninful for C, but the concept also extends to other
14905 languages that allow unprototyped functions (Eg: Objective C).
14906 For all other languages, assume that functions are always
14908 if (cu->language != language_c
14909 && cu->language != language_objc
14910 && cu->language != language_opencl)
14913 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14914 prototyped and unprototyped functions; default to prototyped,
14915 since that is more common in modern code (and RealView warns
14916 about unprototyped functions). */
14917 if (producer_is_realview (cu->producer))
14923 /* Handle DIES due to C code like:
14927 int (*funcp)(int a, long l);
14931 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14933 static struct type *
14934 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14936 struct objfile *objfile = cu->objfile;
14937 struct type *type; /* Type that this function returns. */
14938 struct type *ftype; /* Function that returns above type. */
14939 struct attribute *attr;
14941 type = die_type (die, cu);
14943 /* The die_type call above may have already set the type for this DIE. */
14944 ftype = get_die_type (die, cu);
14948 ftype = lookup_function_type (type);
14950 if (prototyped_function_p (die, cu))
14951 TYPE_PROTOTYPED (ftype) = 1;
14953 /* Store the calling convention in the type if it's available in
14954 the subroutine die. Otherwise set the calling convention to
14955 the default value DW_CC_normal. */
14956 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14958 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14959 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14960 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14962 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14964 /* Record whether the function returns normally to its caller or not
14965 if the DWARF producer set that information. */
14966 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14967 if (attr && (DW_UNSND (attr) != 0))
14968 TYPE_NO_RETURN (ftype) = 1;
14970 /* We need to add the subroutine type to the die immediately so
14971 we don't infinitely recurse when dealing with parameters
14972 declared as the same subroutine type. */
14973 set_die_type (die, ftype, cu);
14975 if (die->child != NULL)
14977 struct type *void_type = objfile_type (objfile)->builtin_void;
14978 struct die_info *child_die;
14979 int nparams, iparams;
14981 /* Count the number of parameters.
14982 FIXME: GDB currently ignores vararg functions, but knows about
14983 vararg member functions. */
14985 child_die = die->child;
14986 while (child_die && child_die->tag)
14988 if (child_die->tag == DW_TAG_formal_parameter)
14990 else if (child_die->tag == DW_TAG_unspecified_parameters)
14991 TYPE_VARARGS (ftype) = 1;
14992 child_die = sibling_die (child_die);
14995 /* Allocate storage for parameters and fill them in. */
14996 TYPE_NFIELDS (ftype) = nparams;
14997 TYPE_FIELDS (ftype) = (struct field *)
14998 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
15000 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15001 even if we error out during the parameters reading below. */
15002 for (iparams = 0; iparams < nparams; iparams++)
15003 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15006 child_die = die->child;
15007 while (child_die && child_die->tag)
15009 if (child_die->tag == DW_TAG_formal_parameter)
15011 struct type *arg_type;
15013 /* DWARF version 2 has no clean way to discern C++
15014 static and non-static member functions. G++ helps
15015 GDB by marking the first parameter for non-static
15016 member functions (which is the this pointer) as
15017 artificial. We pass this information to
15018 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15020 DWARF version 3 added DW_AT_object_pointer, which GCC
15021 4.5 does not yet generate. */
15022 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
15024 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15026 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
15027 arg_type = die_type (child_die, cu);
15029 /* RealView does not mark THIS as const, which the testsuite
15030 expects. GCC marks THIS as const in method definitions,
15031 but not in the class specifications (GCC PR 43053). */
15032 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15033 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15036 struct dwarf2_cu *arg_cu = cu;
15037 const char *name = dwarf2_name (child_die, cu);
15039 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15042 /* If the compiler emits this, use it. */
15043 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15046 else if (name && strcmp (name, "this") == 0)
15047 /* Function definitions will have the argument names. */
15049 else if (name == NULL && iparams == 0)
15050 /* Declarations may not have the names, so like
15051 elsewhere in GDB, assume an artificial first
15052 argument is "this". */
15056 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15060 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
15063 child_die = sibling_die (child_die);
15070 static struct type *
15071 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15073 struct objfile *objfile = cu->objfile;
15074 const char *name = NULL;
15075 struct type *this_type, *target_type;
15077 name = dwarf2_full_name (NULL, die, cu);
15078 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15079 TYPE_TARGET_STUB (this_type) = 1;
15080 set_die_type (die, this_type, cu);
15081 target_type = die_type (die, cu);
15082 if (target_type != this_type)
15083 TYPE_TARGET_TYPE (this_type) = target_type;
15086 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15087 spec and cause infinite loops in GDB. */
15088 complaint (&symfile_complaints,
15089 _("Self-referential DW_TAG_typedef "
15090 "- DIE at 0x%x [in module %s]"),
15091 to_underlying (die->sect_off), objfile_name (objfile));
15092 TYPE_TARGET_TYPE (this_type) = NULL;
15097 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15098 (which may be different from NAME) to the architecture back-end to allow
15099 it to guess the correct format if necessary. */
15101 static struct type *
15102 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15103 const char *name_hint)
15105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15106 const struct floatformat **format;
15109 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15111 type = init_float_type (objfile, bits, name, format);
15113 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15118 /* Find a representation of a given base type and install
15119 it in the TYPE field of the die. */
15121 static struct type *
15122 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15124 struct objfile *objfile = cu->objfile;
15126 struct attribute *attr;
15127 int encoding = 0, bits = 0;
15130 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15133 encoding = DW_UNSND (attr);
15135 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15138 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15140 name = dwarf2_name (die, cu);
15143 complaint (&symfile_complaints,
15144 _("DW_AT_name missing from DW_TAG_base_type"));
15149 case DW_ATE_address:
15150 /* Turn DW_ATE_address into a void * pointer. */
15151 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15152 type = init_pointer_type (objfile, bits, name, type);
15154 case DW_ATE_boolean:
15155 type = init_boolean_type (objfile, bits, 1, name);
15157 case DW_ATE_complex_float:
15158 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15159 type = init_complex_type (objfile, name, type);
15161 case DW_ATE_decimal_float:
15162 type = init_decfloat_type (objfile, bits, name);
15165 type = dwarf2_init_float_type (objfile, bits, name, name);
15167 case DW_ATE_signed:
15168 type = init_integer_type (objfile, bits, 0, name);
15170 case DW_ATE_unsigned:
15171 if (cu->language == language_fortran
15173 && startswith (name, "character("))
15174 type = init_character_type (objfile, bits, 1, name);
15176 type = init_integer_type (objfile, bits, 1, name);
15178 case DW_ATE_signed_char:
15179 if (cu->language == language_ada || cu->language == language_m2
15180 || cu->language == language_pascal
15181 || cu->language == language_fortran)
15182 type = init_character_type (objfile, bits, 0, name);
15184 type = init_integer_type (objfile, bits, 0, name);
15186 case DW_ATE_unsigned_char:
15187 if (cu->language == language_ada || cu->language == language_m2
15188 || cu->language == language_pascal
15189 || cu->language == language_fortran
15190 || cu->language == language_rust)
15191 type = init_character_type (objfile, bits, 1, name);
15193 type = init_integer_type (objfile, bits, 1, name);
15197 gdbarch *arch = get_objfile_arch (objfile);
15200 type = builtin_type (arch)->builtin_char16;
15201 else if (bits == 32)
15202 type = builtin_type (arch)->builtin_char32;
15205 complaint (&symfile_complaints,
15206 _("unsupported DW_ATE_UTF bit size: '%d'"),
15208 type = init_integer_type (objfile, bits, 1, name);
15210 return set_die_type (die, type, cu);
15215 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15216 dwarf_type_encoding_name (encoding));
15217 type = init_type (objfile, TYPE_CODE_ERROR,
15218 bits / TARGET_CHAR_BIT, name);
15222 if (name && strcmp (name, "char") == 0)
15223 TYPE_NOSIGN (type) = 1;
15225 return set_die_type (die, type, cu);
15228 /* Parse dwarf attribute if it's a block, reference or constant and put the
15229 resulting value of the attribute into struct bound_prop.
15230 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15233 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15234 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15236 struct dwarf2_property_baton *baton;
15237 struct obstack *obstack = &cu->objfile->objfile_obstack;
15239 if (attr == NULL || prop == NULL)
15242 if (attr_form_is_block (attr))
15244 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15245 baton->referenced_type = NULL;
15246 baton->locexpr.per_cu = cu->per_cu;
15247 baton->locexpr.size = DW_BLOCK (attr)->size;
15248 baton->locexpr.data = DW_BLOCK (attr)->data;
15249 prop->data.baton = baton;
15250 prop->kind = PROP_LOCEXPR;
15251 gdb_assert (prop->data.baton != NULL);
15253 else if (attr_form_is_ref (attr))
15255 struct dwarf2_cu *target_cu = cu;
15256 struct die_info *target_die;
15257 struct attribute *target_attr;
15259 target_die = follow_die_ref (die, attr, &target_cu);
15260 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15261 if (target_attr == NULL)
15262 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15264 if (target_attr == NULL)
15267 switch (target_attr->name)
15269 case DW_AT_location:
15270 if (attr_form_is_section_offset (target_attr))
15272 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15273 baton->referenced_type = die_type (target_die, target_cu);
15274 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15275 prop->data.baton = baton;
15276 prop->kind = PROP_LOCLIST;
15277 gdb_assert (prop->data.baton != NULL);
15279 else if (attr_form_is_block (target_attr))
15281 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15282 baton->referenced_type = die_type (target_die, target_cu);
15283 baton->locexpr.per_cu = cu->per_cu;
15284 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15285 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15286 prop->data.baton = baton;
15287 prop->kind = PROP_LOCEXPR;
15288 gdb_assert (prop->data.baton != NULL);
15292 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15293 "dynamic property");
15297 case DW_AT_data_member_location:
15301 if (!handle_data_member_location (target_die, target_cu,
15305 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15306 baton->referenced_type = read_type_die (target_die->parent,
15308 baton->offset_info.offset = offset;
15309 baton->offset_info.type = die_type (target_die, target_cu);
15310 prop->data.baton = baton;
15311 prop->kind = PROP_ADDR_OFFSET;
15316 else if (attr_form_is_constant (attr))
15318 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15319 prop->kind = PROP_CONST;
15323 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15324 dwarf2_name (die, cu));
15331 /* Read the given DW_AT_subrange DIE. */
15333 static struct type *
15334 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15336 struct type *base_type, *orig_base_type;
15337 struct type *range_type;
15338 struct attribute *attr;
15339 struct dynamic_prop low, high;
15340 int low_default_is_valid;
15341 int high_bound_is_count = 0;
15343 LONGEST negative_mask;
15345 orig_base_type = die_type (die, cu);
15346 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15347 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15348 creating the range type, but we use the result of check_typedef
15349 when examining properties of the type. */
15350 base_type = check_typedef (orig_base_type);
15352 /* The die_type call above may have already set the type for this DIE. */
15353 range_type = get_die_type (die, cu);
15357 low.kind = PROP_CONST;
15358 high.kind = PROP_CONST;
15359 high.data.const_val = 0;
15361 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15362 omitting DW_AT_lower_bound. */
15363 switch (cu->language)
15366 case language_cplus:
15367 low.data.const_val = 0;
15368 low_default_is_valid = 1;
15370 case language_fortran:
15371 low.data.const_val = 1;
15372 low_default_is_valid = 1;
15375 case language_objc:
15376 case language_rust:
15377 low.data.const_val = 0;
15378 low_default_is_valid = (cu->header.version >= 4);
15382 case language_pascal:
15383 low.data.const_val = 1;
15384 low_default_is_valid = (cu->header.version >= 4);
15387 low.data.const_val = 0;
15388 low_default_is_valid = 0;
15392 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15394 attr_to_dynamic_prop (attr, die, cu, &low);
15395 else if (!low_default_is_valid)
15396 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15397 "- DIE at 0x%x [in module %s]"),
15398 to_underlying (die->sect_off), objfile_name (cu->objfile));
15400 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15401 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15403 attr = dwarf2_attr (die, DW_AT_count, cu);
15404 if (attr_to_dynamic_prop (attr, die, cu, &high))
15406 /* If bounds are constant do the final calculation here. */
15407 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15408 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15410 high_bound_is_count = 1;
15414 /* Dwarf-2 specifications explicitly allows to create subrange types
15415 without specifying a base type.
15416 In that case, the base type must be set to the type of
15417 the lower bound, upper bound or count, in that order, if any of these
15418 three attributes references an object that has a type.
15419 If no base type is found, the Dwarf-2 specifications say that
15420 a signed integer type of size equal to the size of an address should
15422 For the following C code: `extern char gdb_int [];'
15423 GCC produces an empty range DIE.
15424 FIXME: muller/2010-05-28: Possible references to object for low bound,
15425 high bound or count are not yet handled by this code. */
15426 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15428 struct objfile *objfile = cu->objfile;
15429 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15430 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15431 struct type *int_type = objfile_type (objfile)->builtin_int;
15433 /* Test "int", "long int", and "long long int" objfile types,
15434 and select the first one having a size above or equal to the
15435 architecture address size. */
15436 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15437 base_type = int_type;
15440 int_type = objfile_type (objfile)->builtin_long;
15441 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15442 base_type = int_type;
15445 int_type = objfile_type (objfile)->builtin_long_long;
15446 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15447 base_type = int_type;
15452 /* Normally, the DWARF producers are expected to use a signed
15453 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15454 But this is unfortunately not always the case, as witnessed
15455 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15456 is used instead. To work around that ambiguity, we treat
15457 the bounds as signed, and thus sign-extend their values, when
15458 the base type is signed. */
15460 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15461 if (low.kind == PROP_CONST
15462 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15463 low.data.const_val |= negative_mask;
15464 if (high.kind == PROP_CONST
15465 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15466 high.data.const_val |= negative_mask;
15468 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15470 if (high_bound_is_count)
15471 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15473 /* Ada expects an empty array on no boundary attributes. */
15474 if (attr == NULL && cu->language != language_ada)
15475 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15477 name = dwarf2_name (die, cu);
15479 TYPE_NAME (range_type) = name;
15481 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15483 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15485 set_die_type (die, range_type, cu);
15487 /* set_die_type should be already done. */
15488 set_descriptive_type (range_type, die, cu);
15493 static struct type *
15494 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15498 /* For now, we only support the C meaning of an unspecified type: void. */
15500 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15501 TYPE_NAME (type) = dwarf2_name (die, cu);
15503 return set_die_type (die, type, cu);
15506 /* Read a single die and all its descendents. Set the die's sibling
15507 field to NULL; set other fields in the die correctly, and set all
15508 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15509 location of the info_ptr after reading all of those dies. PARENT
15510 is the parent of the die in question. */
15512 static struct die_info *
15513 read_die_and_children (const struct die_reader_specs *reader,
15514 const gdb_byte *info_ptr,
15515 const gdb_byte **new_info_ptr,
15516 struct die_info *parent)
15518 struct die_info *die;
15519 const gdb_byte *cur_ptr;
15522 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15525 *new_info_ptr = cur_ptr;
15528 store_in_ref_table (die, reader->cu);
15531 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15535 *new_info_ptr = cur_ptr;
15538 die->sibling = NULL;
15539 die->parent = parent;
15543 /* Read a die, all of its descendents, and all of its siblings; set
15544 all of the fields of all of the dies correctly. Arguments are as
15545 in read_die_and_children. */
15547 static struct die_info *
15548 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15549 const gdb_byte *info_ptr,
15550 const gdb_byte **new_info_ptr,
15551 struct die_info *parent)
15553 struct die_info *first_die, *last_sibling;
15554 const gdb_byte *cur_ptr;
15556 cur_ptr = info_ptr;
15557 first_die = last_sibling = NULL;
15561 struct die_info *die
15562 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15566 *new_info_ptr = cur_ptr;
15573 last_sibling->sibling = die;
15575 last_sibling = die;
15579 /* Read a die, all of its descendents, and all of its siblings; set
15580 all of the fields of all of the dies correctly. Arguments are as
15581 in read_die_and_children.
15582 This the main entry point for reading a DIE and all its children. */
15584 static struct die_info *
15585 read_die_and_siblings (const struct die_reader_specs *reader,
15586 const gdb_byte *info_ptr,
15587 const gdb_byte **new_info_ptr,
15588 struct die_info *parent)
15590 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15591 new_info_ptr, parent);
15593 if (dwarf_die_debug)
15595 fprintf_unfiltered (gdb_stdlog,
15596 "Read die from %s@0x%x of %s:\n",
15597 get_section_name (reader->die_section),
15598 (unsigned) (info_ptr - reader->die_section->buffer),
15599 bfd_get_filename (reader->abfd));
15600 dump_die (die, dwarf_die_debug);
15606 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15608 The caller is responsible for filling in the extra attributes
15609 and updating (*DIEP)->num_attrs.
15610 Set DIEP to point to a newly allocated die with its information,
15611 except for its child, sibling, and parent fields.
15612 Set HAS_CHILDREN to tell whether the die has children or not. */
15614 static const gdb_byte *
15615 read_full_die_1 (const struct die_reader_specs *reader,
15616 struct die_info **diep, const gdb_byte *info_ptr,
15617 int *has_children, int num_extra_attrs)
15619 unsigned int abbrev_number, bytes_read, i;
15620 struct abbrev_info *abbrev;
15621 struct die_info *die;
15622 struct dwarf2_cu *cu = reader->cu;
15623 bfd *abfd = reader->abfd;
15625 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15626 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15627 info_ptr += bytes_read;
15628 if (!abbrev_number)
15635 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15637 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15639 bfd_get_filename (abfd));
15641 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15642 die->sect_off = sect_off;
15643 die->tag = abbrev->tag;
15644 die->abbrev = abbrev_number;
15646 /* Make the result usable.
15647 The caller needs to update num_attrs after adding the extra
15649 die->num_attrs = abbrev->num_attrs;
15651 for (i = 0; i < abbrev->num_attrs; ++i)
15652 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15656 *has_children = abbrev->has_children;
15660 /* Read a die and all its attributes.
15661 Set DIEP to point to a newly allocated die with its information,
15662 except for its child, sibling, and parent fields.
15663 Set HAS_CHILDREN to tell whether the die has children or not. */
15665 static const gdb_byte *
15666 read_full_die (const struct die_reader_specs *reader,
15667 struct die_info **diep, const gdb_byte *info_ptr,
15670 const gdb_byte *result;
15672 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15674 if (dwarf_die_debug)
15676 fprintf_unfiltered (gdb_stdlog,
15677 "Read die from %s@0x%x of %s:\n",
15678 get_section_name (reader->die_section),
15679 (unsigned) (info_ptr - reader->die_section->buffer),
15680 bfd_get_filename (reader->abfd));
15681 dump_die (*diep, dwarf_die_debug);
15687 /* Abbreviation tables.
15689 In DWARF version 2, the description of the debugging information is
15690 stored in a separate .debug_abbrev section. Before we read any
15691 dies from a section we read in all abbreviations and install them
15692 in a hash table. */
15694 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15696 static struct abbrev_info *
15697 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15699 struct abbrev_info *abbrev;
15701 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15702 memset (abbrev, 0, sizeof (struct abbrev_info));
15707 /* Add an abbreviation to the table. */
15710 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15711 unsigned int abbrev_number,
15712 struct abbrev_info *abbrev)
15714 unsigned int hash_number;
15716 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15717 abbrev->next = abbrev_table->abbrevs[hash_number];
15718 abbrev_table->abbrevs[hash_number] = abbrev;
15721 /* Look up an abbrev in the table.
15722 Returns NULL if the abbrev is not found. */
15724 static struct abbrev_info *
15725 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15726 unsigned int abbrev_number)
15728 unsigned int hash_number;
15729 struct abbrev_info *abbrev;
15731 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15732 abbrev = abbrev_table->abbrevs[hash_number];
15736 if (abbrev->number == abbrev_number)
15738 abbrev = abbrev->next;
15743 /* Read in an abbrev table. */
15745 static struct abbrev_table *
15746 abbrev_table_read_table (struct dwarf2_section_info *section,
15747 sect_offset sect_off)
15749 struct objfile *objfile = dwarf2_per_objfile->objfile;
15750 bfd *abfd = get_section_bfd_owner (section);
15751 struct abbrev_table *abbrev_table;
15752 const gdb_byte *abbrev_ptr;
15753 struct abbrev_info *cur_abbrev;
15754 unsigned int abbrev_number, bytes_read, abbrev_name;
15755 unsigned int abbrev_form;
15756 struct attr_abbrev *cur_attrs;
15757 unsigned int allocated_attrs;
15759 abbrev_table = XNEW (struct abbrev_table);
15760 abbrev_table->sect_off = sect_off;
15761 obstack_init (&abbrev_table->abbrev_obstack);
15762 abbrev_table->abbrevs =
15763 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15765 memset (abbrev_table->abbrevs, 0,
15766 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15768 dwarf2_read_section (objfile, section);
15769 abbrev_ptr = section->buffer + to_underlying (sect_off);
15770 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15771 abbrev_ptr += bytes_read;
15773 allocated_attrs = ATTR_ALLOC_CHUNK;
15774 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15776 /* Loop until we reach an abbrev number of 0. */
15777 while (abbrev_number)
15779 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15781 /* read in abbrev header */
15782 cur_abbrev->number = abbrev_number;
15784 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15785 abbrev_ptr += bytes_read;
15786 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15789 /* now read in declarations */
15792 LONGEST implicit_const;
15794 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15795 abbrev_ptr += bytes_read;
15796 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15797 abbrev_ptr += bytes_read;
15798 if (abbrev_form == DW_FORM_implicit_const)
15800 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15802 abbrev_ptr += bytes_read;
15806 /* Initialize it due to a false compiler warning. */
15807 implicit_const = -1;
15810 if (abbrev_name == 0)
15813 if (cur_abbrev->num_attrs == allocated_attrs)
15815 allocated_attrs += ATTR_ALLOC_CHUNK;
15817 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15820 cur_attrs[cur_abbrev->num_attrs].name
15821 = (enum dwarf_attribute) abbrev_name;
15822 cur_attrs[cur_abbrev->num_attrs].form
15823 = (enum dwarf_form) abbrev_form;
15824 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15825 ++cur_abbrev->num_attrs;
15828 cur_abbrev->attrs =
15829 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15830 cur_abbrev->num_attrs);
15831 memcpy (cur_abbrev->attrs, cur_attrs,
15832 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15834 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15836 /* Get next abbreviation.
15837 Under Irix6 the abbreviations for a compilation unit are not
15838 always properly terminated with an abbrev number of 0.
15839 Exit loop if we encounter an abbreviation which we have
15840 already read (which means we are about to read the abbreviations
15841 for the next compile unit) or if the end of the abbreviation
15842 table is reached. */
15843 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15845 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15846 abbrev_ptr += bytes_read;
15847 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15852 return abbrev_table;
15855 /* Free the resources held by ABBREV_TABLE. */
15858 abbrev_table_free (struct abbrev_table *abbrev_table)
15860 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15861 xfree (abbrev_table);
15864 /* Same as abbrev_table_free but as a cleanup.
15865 We pass in a pointer to the pointer to the table so that we can
15866 set the pointer to NULL when we're done. It also simplifies
15867 build_type_psymtabs_1. */
15870 abbrev_table_free_cleanup (void *table_ptr)
15872 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15874 if (*abbrev_table_ptr != NULL)
15875 abbrev_table_free (*abbrev_table_ptr);
15876 *abbrev_table_ptr = NULL;
15879 /* Read the abbrev table for CU from ABBREV_SECTION. */
15882 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15883 struct dwarf2_section_info *abbrev_section)
15886 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
15889 /* Release the memory used by the abbrev table for a compilation unit. */
15892 dwarf2_free_abbrev_table (void *ptr_to_cu)
15894 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15896 if (cu->abbrev_table != NULL)
15897 abbrev_table_free (cu->abbrev_table);
15898 /* Set this to NULL so that we SEGV if we try to read it later,
15899 and also because free_comp_unit verifies this is NULL. */
15900 cu->abbrev_table = NULL;
15903 /* Returns nonzero if TAG represents a type that we might generate a partial
15907 is_type_tag_for_partial (int tag)
15912 /* Some types that would be reasonable to generate partial symbols for,
15913 that we don't at present. */
15914 case DW_TAG_array_type:
15915 case DW_TAG_file_type:
15916 case DW_TAG_ptr_to_member_type:
15917 case DW_TAG_set_type:
15918 case DW_TAG_string_type:
15919 case DW_TAG_subroutine_type:
15921 case DW_TAG_base_type:
15922 case DW_TAG_class_type:
15923 case DW_TAG_interface_type:
15924 case DW_TAG_enumeration_type:
15925 case DW_TAG_structure_type:
15926 case DW_TAG_subrange_type:
15927 case DW_TAG_typedef:
15928 case DW_TAG_union_type:
15935 /* Load all DIEs that are interesting for partial symbols into memory. */
15937 static struct partial_die_info *
15938 load_partial_dies (const struct die_reader_specs *reader,
15939 const gdb_byte *info_ptr, int building_psymtab)
15941 struct dwarf2_cu *cu = reader->cu;
15942 struct objfile *objfile = cu->objfile;
15943 struct partial_die_info *part_die;
15944 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15945 struct abbrev_info *abbrev;
15946 unsigned int bytes_read;
15947 unsigned int load_all = 0;
15948 int nesting_level = 1;
15953 gdb_assert (cu->per_cu != NULL);
15954 if (cu->per_cu->load_all_dies)
15958 = htab_create_alloc_ex (cu->header.length / 12,
15962 &cu->comp_unit_obstack,
15963 hashtab_obstack_allocate,
15964 dummy_obstack_deallocate);
15966 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15970 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15972 /* A NULL abbrev means the end of a series of children. */
15973 if (abbrev == NULL)
15975 if (--nesting_level == 0)
15977 /* PART_DIE was probably the last thing allocated on the
15978 comp_unit_obstack, so we could call obstack_free
15979 here. We don't do that because the waste is small,
15980 and will be cleaned up when we're done with this
15981 compilation unit. This way, we're also more robust
15982 against other users of the comp_unit_obstack. */
15985 info_ptr += bytes_read;
15986 last_die = parent_die;
15987 parent_die = parent_die->die_parent;
15991 /* Check for template arguments. We never save these; if
15992 they're seen, we just mark the parent, and go on our way. */
15993 if (parent_die != NULL
15994 && cu->language == language_cplus
15995 && (abbrev->tag == DW_TAG_template_type_param
15996 || abbrev->tag == DW_TAG_template_value_param))
15998 parent_die->has_template_arguments = 1;
16002 /* We don't need a partial DIE for the template argument. */
16003 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16008 /* We only recurse into c++ subprograms looking for template arguments.
16009 Skip their other children. */
16011 && cu->language == language_cplus
16012 && parent_die != NULL
16013 && parent_die->tag == DW_TAG_subprogram)
16015 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16019 /* Check whether this DIE is interesting enough to save. Normally
16020 we would not be interested in members here, but there may be
16021 later variables referencing them via DW_AT_specification (for
16022 static members). */
16024 && !is_type_tag_for_partial (abbrev->tag)
16025 && abbrev->tag != DW_TAG_constant
16026 && abbrev->tag != DW_TAG_enumerator
16027 && abbrev->tag != DW_TAG_subprogram
16028 && abbrev->tag != DW_TAG_lexical_block
16029 && abbrev->tag != DW_TAG_variable
16030 && abbrev->tag != DW_TAG_namespace
16031 && abbrev->tag != DW_TAG_module
16032 && abbrev->tag != DW_TAG_member
16033 && abbrev->tag != DW_TAG_imported_unit
16034 && abbrev->tag != DW_TAG_imported_declaration)
16036 /* Otherwise we skip to the next sibling, if any. */
16037 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16041 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16044 /* This two-pass algorithm for processing partial symbols has a
16045 high cost in cache pressure. Thus, handle some simple cases
16046 here which cover the majority of C partial symbols. DIEs
16047 which neither have specification tags in them, nor could have
16048 specification tags elsewhere pointing at them, can simply be
16049 processed and discarded.
16051 This segment is also optional; scan_partial_symbols and
16052 add_partial_symbol will handle these DIEs if we chain
16053 them in normally. When compilers which do not emit large
16054 quantities of duplicate debug information are more common,
16055 this code can probably be removed. */
16057 /* Any complete simple types at the top level (pretty much all
16058 of them, for a language without namespaces), can be processed
16060 if (parent_die == NULL
16061 && part_die->has_specification == 0
16062 && part_die->is_declaration == 0
16063 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
16064 || part_die->tag == DW_TAG_base_type
16065 || part_die->tag == DW_TAG_subrange_type))
16067 if (building_psymtab && part_die->name != NULL)
16068 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16069 VAR_DOMAIN, LOC_TYPEDEF,
16070 &objfile->static_psymbols,
16071 0, cu->language, objfile);
16072 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16076 /* The exception for DW_TAG_typedef with has_children above is
16077 a workaround of GCC PR debug/47510. In the case of this complaint
16078 type_name_no_tag_or_error will error on such types later.
16080 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16081 it could not find the child DIEs referenced later, this is checked
16082 above. In correct DWARF DW_TAG_typedef should have no children. */
16084 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16085 complaint (&symfile_complaints,
16086 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16087 "- DIE at 0x%x [in module %s]"),
16088 to_underlying (part_die->sect_off), objfile_name (objfile));
16090 /* If we're at the second level, and we're an enumerator, and
16091 our parent has no specification (meaning possibly lives in a
16092 namespace elsewhere), then we can add the partial symbol now
16093 instead of queueing it. */
16094 if (part_die->tag == DW_TAG_enumerator
16095 && parent_die != NULL
16096 && parent_die->die_parent == NULL
16097 && parent_die->tag == DW_TAG_enumeration_type
16098 && parent_die->has_specification == 0)
16100 if (part_die->name == NULL)
16101 complaint (&symfile_complaints,
16102 _("malformed enumerator DIE ignored"));
16103 else if (building_psymtab)
16104 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16105 VAR_DOMAIN, LOC_CONST,
16106 cu->language == language_cplus
16107 ? &objfile->global_psymbols
16108 : &objfile->static_psymbols,
16109 0, cu->language, objfile);
16111 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16115 /* We'll save this DIE so link it in. */
16116 part_die->die_parent = parent_die;
16117 part_die->die_sibling = NULL;
16118 part_die->die_child = NULL;
16120 if (last_die && last_die == parent_die)
16121 last_die->die_child = part_die;
16123 last_die->die_sibling = part_die;
16125 last_die = part_die;
16127 if (first_die == NULL)
16128 first_die = part_die;
16130 /* Maybe add the DIE to the hash table. Not all DIEs that we
16131 find interesting need to be in the hash table, because we
16132 also have the parent/sibling/child chains; only those that we
16133 might refer to by offset later during partial symbol reading.
16135 For now this means things that might have be the target of a
16136 DW_AT_specification, DW_AT_abstract_origin, or
16137 DW_AT_extension. DW_AT_extension will refer only to
16138 namespaces; DW_AT_abstract_origin refers to functions (and
16139 many things under the function DIE, but we do not recurse
16140 into function DIEs during partial symbol reading) and
16141 possibly variables as well; DW_AT_specification refers to
16142 declarations. Declarations ought to have the DW_AT_declaration
16143 flag. It happens that GCC forgets to put it in sometimes, but
16144 only for functions, not for types.
16146 Adding more things than necessary to the hash table is harmless
16147 except for the performance cost. Adding too few will result in
16148 wasted time in find_partial_die, when we reread the compilation
16149 unit with load_all_dies set. */
16152 || abbrev->tag == DW_TAG_constant
16153 || abbrev->tag == DW_TAG_subprogram
16154 || abbrev->tag == DW_TAG_variable
16155 || abbrev->tag == DW_TAG_namespace
16156 || part_die->is_declaration)
16160 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16161 to_underlying (part_die->sect_off),
16166 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16168 /* For some DIEs we want to follow their children (if any). For C
16169 we have no reason to follow the children of structures; for other
16170 languages we have to, so that we can get at method physnames
16171 to infer fully qualified class names, for DW_AT_specification,
16172 and for C++ template arguments. For C++, we also look one level
16173 inside functions to find template arguments (if the name of the
16174 function does not already contain the template arguments).
16176 For Ada, we need to scan the children of subprograms and lexical
16177 blocks as well because Ada allows the definition of nested
16178 entities that could be interesting for the debugger, such as
16179 nested subprograms for instance. */
16180 if (last_die->has_children
16182 || last_die->tag == DW_TAG_namespace
16183 || last_die->tag == DW_TAG_module
16184 || last_die->tag == DW_TAG_enumeration_type
16185 || (cu->language == language_cplus
16186 && last_die->tag == DW_TAG_subprogram
16187 && (last_die->name == NULL
16188 || strchr (last_die->name, '<') == NULL))
16189 || (cu->language != language_c
16190 && (last_die->tag == DW_TAG_class_type
16191 || last_die->tag == DW_TAG_interface_type
16192 || last_die->tag == DW_TAG_structure_type
16193 || last_die->tag == DW_TAG_union_type))
16194 || (cu->language == language_ada
16195 && (last_die->tag == DW_TAG_subprogram
16196 || last_die->tag == DW_TAG_lexical_block))))
16199 parent_die = last_die;
16203 /* Otherwise we skip to the next sibling, if any. */
16204 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16206 /* Back to the top, do it again. */
16210 /* Read a minimal amount of information into the minimal die structure. */
16212 static const gdb_byte *
16213 read_partial_die (const struct die_reader_specs *reader,
16214 struct partial_die_info *part_die,
16215 struct abbrev_info *abbrev, unsigned int abbrev_len,
16216 const gdb_byte *info_ptr)
16218 struct dwarf2_cu *cu = reader->cu;
16219 struct objfile *objfile = cu->objfile;
16220 const gdb_byte *buffer = reader->buffer;
16222 struct attribute attr;
16223 int has_low_pc_attr = 0;
16224 int has_high_pc_attr = 0;
16225 int high_pc_relative = 0;
16227 memset (part_die, 0, sizeof (struct partial_die_info));
16229 part_die->sect_off = (sect_offset) (info_ptr - buffer);
16231 info_ptr += abbrev_len;
16233 if (abbrev == NULL)
16236 part_die->tag = abbrev->tag;
16237 part_die->has_children = abbrev->has_children;
16239 for (i = 0; i < abbrev->num_attrs; ++i)
16241 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16243 /* Store the data if it is of an attribute we want to keep in a
16244 partial symbol table. */
16248 switch (part_die->tag)
16250 case DW_TAG_compile_unit:
16251 case DW_TAG_partial_unit:
16252 case DW_TAG_type_unit:
16253 /* Compilation units have a DW_AT_name that is a filename, not
16254 a source language identifier. */
16255 case DW_TAG_enumeration_type:
16256 case DW_TAG_enumerator:
16257 /* These tags always have simple identifiers already; no need
16258 to canonicalize them. */
16259 part_die->name = DW_STRING (&attr);
16263 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16264 &objfile->per_bfd->storage_obstack);
16268 case DW_AT_linkage_name:
16269 case DW_AT_MIPS_linkage_name:
16270 /* Note that both forms of linkage name might appear. We
16271 assume they will be the same, and we only store the last
16273 if (cu->language == language_ada)
16274 part_die->name = DW_STRING (&attr);
16275 part_die->linkage_name = DW_STRING (&attr);
16278 has_low_pc_attr = 1;
16279 part_die->lowpc = attr_value_as_address (&attr);
16281 case DW_AT_high_pc:
16282 has_high_pc_attr = 1;
16283 part_die->highpc = attr_value_as_address (&attr);
16284 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16285 high_pc_relative = 1;
16287 case DW_AT_location:
16288 /* Support the .debug_loc offsets. */
16289 if (attr_form_is_block (&attr))
16291 part_die->d.locdesc = DW_BLOCK (&attr);
16293 else if (attr_form_is_section_offset (&attr))
16295 dwarf2_complex_location_expr_complaint ();
16299 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16300 "partial symbol information");
16303 case DW_AT_external:
16304 part_die->is_external = DW_UNSND (&attr);
16306 case DW_AT_declaration:
16307 part_die->is_declaration = DW_UNSND (&attr);
16310 part_die->has_type = 1;
16312 case DW_AT_abstract_origin:
16313 case DW_AT_specification:
16314 case DW_AT_extension:
16315 part_die->has_specification = 1;
16316 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16317 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16318 || cu->per_cu->is_dwz);
16320 case DW_AT_sibling:
16321 /* Ignore absolute siblings, they might point outside of
16322 the current compile unit. */
16323 if (attr.form == DW_FORM_ref_addr)
16324 complaint (&symfile_complaints,
16325 _("ignoring absolute DW_AT_sibling"));
16328 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16329 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
16331 if (sibling_ptr < info_ptr)
16332 complaint (&symfile_complaints,
16333 _("DW_AT_sibling points backwards"));
16334 else if (sibling_ptr > reader->buffer_end)
16335 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16337 part_die->sibling = sibling_ptr;
16340 case DW_AT_byte_size:
16341 part_die->has_byte_size = 1;
16343 case DW_AT_const_value:
16344 part_die->has_const_value = 1;
16346 case DW_AT_calling_convention:
16347 /* DWARF doesn't provide a way to identify a program's source-level
16348 entry point. DW_AT_calling_convention attributes are only meant
16349 to describe functions' calling conventions.
16351 However, because it's a necessary piece of information in
16352 Fortran, and before DWARF 4 DW_CC_program was the only
16353 piece of debugging information whose definition refers to
16354 a 'main program' at all, several compilers marked Fortran
16355 main programs with DW_CC_program --- even when those
16356 functions use the standard calling conventions.
16358 Although DWARF now specifies a way to provide this
16359 information, we support this practice for backward
16361 if (DW_UNSND (&attr) == DW_CC_program
16362 && cu->language == language_fortran)
16363 part_die->main_subprogram = 1;
16366 if (DW_UNSND (&attr) == DW_INL_inlined
16367 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16368 part_die->may_be_inlined = 1;
16372 if (part_die->tag == DW_TAG_imported_unit)
16374 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
16375 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16376 || cu->per_cu->is_dwz);
16380 case DW_AT_main_subprogram:
16381 part_die->main_subprogram = DW_UNSND (&attr);
16389 if (high_pc_relative)
16390 part_die->highpc += part_die->lowpc;
16392 if (has_low_pc_attr && has_high_pc_attr)
16394 /* When using the GNU linker, .gnu.linkonce. sections are used to
16395 eliminate duplicate copies of functions and vtables and such.
16396 The linker will arbitrarily choose one and discard the others.
16397 The AT_*_pc values for such functions refer to local labels in
16398 these sections. If the section from that file was discarded, the
16399 labels are not in the output, so the relocs get a value of 0.
16400 If this is a discarded function, mark the pc bounds as invalid,
16401 so that GDB will ignore it. */
16402 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16404 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16406 complaint (&symfile_complaints,
16407 _("DW_AT_low_pc %s is zero "
16408 "for DIE at 0x%x [in module %s]"),
16409 paddress (gdbarch, part_die->lowpc),
16410 to_underlying (part_die->sect_off), objfile_name (objfile));
16412 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16413 else if (part_die->lowpc >= part_die->highpc)
16415 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16417 complaint (&symfile_complaints,
16418 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16419 "for DIE at 0x%x [in module %s]"),
16420 paddress (gdbarch, part_die->lowpc),
16421 paddress (gdbarch, part_die->highpc),
16422 to_underlying (part_die->sect_off),
16423 objfile_name (objfile));
16426 part_die->has_pc_info = 1;
16432 /* Find a cached partial DIE at OFFSET in CU. */
16434 static struct partial_die_info *
16435 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
16437 struct partial_die_info *lookup_die = NULL;
16438 struct partial_die_info part_die;
16440 part_die.sect_off = sect_off;
16441 lookup_die = ((struct partial_die_info *)
16442 htab_find_with_hash (cu->partial_dies, &part_die,
16443 to_underlying (sect_off)));
16448 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16449 except in the case of .debug_types DIEs which do not reference
16450 outside their CU (they do however referencing other types via
16451 DW_FORM_ref_sig8). */
16453 static struct partial_die_info *
16454 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
16456 struct objfile *objfile = cu->objfile;
16457 struct dwarf2_per_cu_data *per_cu = NULL;
16458 struct partial_die_info *pd = NULL;
16460 if (offset_in_dwz == cu->per_cu->is_dwz
16461 && offset_in_cu_p (&cu->header, sect_off))
16463 pd = find_partial_die_in_comp_unit (sect_off, cu);
16466 /* We missed recording what we needed.
16467 Load all dies and try again. */
16468 per_cu = cu->per_cu;
16472 /* TUs don't reference other CUs/TUs (except via type signatures). */
16473 if (cu->per_cu->is_debug_types)
16475 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16476 " external reference to offset 0x%x [in module %s].\n"),
16477 to_underlying (cu->header.sect_off), to_underlying (sect_off),
16478 bfd_get_filename (objfile->obfd));
16480 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
16483 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16484 load_partial_comp_unit (per_cu);
16486 per_cu->cu->last_used = 0;
16487 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16490 /* If we didn't find it, and not all dies have been loaded,
16491 load them all and try again. */
16493 if (pd == NULL && per_cu->load_all_dies == 0)
16495 per_cu->load_all_dies = 1;
16497 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16498 THIS_CU->cu may already be in use. So we can't just free it and
16499 replace its DIEs with the ones we read in. Instead, we leave those
16500 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16501 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16503 load_partial_comp_unit (per_cu);
16505 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16509 internal_error (__FILE__, __LINE__,
16510 _("could not find partial DIE 0x%x "
16511 "in cache [from module %s]\n"),
16512 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
16516 /* See if we can figure out if the class lives in a namespace. We do
16517 this by looking for a member function; its demangled name will
16518 contain namespace info, if there is any. */
16521 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16522 struct dwarf2_cu *cu)
16524 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16525 what template types look like, because the demangler
16526 frequently doesn't give the same name as the debug info. We
16527 could fix this by only using the demangled name to get the
16528 prefix (but see comment in read_structure_type). */
16530 struct partial_die_info *real_pdi;
16531 struct partial_die_info *child_pdi;
16533 /* If this DIE (this DIE's specification, if any) has a parent, then
16534 we should not do this. We'll prepend the parent's fully qualified
16535 name when we create the partial symbol. */
16537 real_pdi = struct_pdi;
16538 while (real_pdi->has_specification)
16539 real_pdi = find_partial_die (real_pdi->spec_offset,
16540 real_pdi->spec_is_dwz, cu);
16542 if (real_pdi->die_parent != NULL)
16545 for (child_pdi = struct_pdi->die_child;
16547 child_pdi = child_pdi->die_sibling)
16549 if (child_pdi->tag == DW_TAG_subprogram
16550 && child_pdi->linkage_name != NULL)
16552 char *actual_class_name
16553 = language_class_name_from_physname (cu->language_defn,
16554 child_pdi->linkage_name);
16555 if (actual_class_name != NULL)
16559 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16561 strlen (actual_class_name)));
16562 xfree (actual_class_name);
16569 /* Adjust PART_DIE before generating a symbol for it. This function
16570 may set the is_external flag or change the DIE's name. */
16573 fixup_partial_die (struct partial_die_info *part_die,
16574 struct dwarf2_cu *cu)
16576 /* Once we've fixed up a die, there's no point in doing so again.
16577 This also avoids a memory leak if we were to call
16578 guess_partial_die_structure_name multiple times. */
16579 if (part_die->fixup_called)
16582 /* If we found a reference attribute and the DIE has no name, try
16583 to find a name in the referred to DIE. */
16585 if (part_die->name == NULL && part_die->has_specification)
16587 struct partial_die_info *spec_die;
16589 spec_die = find_partial_die (part_die->spec_offset,
16590 part_die->spec_is_dwz, cu);
16592 fixup_partial_die (spec_die, cu);
16594 if (spec_die->name)
16596 part_die->name = spec_die->name;
16598 /* Copy DW_AT_external attribute if it is set. */
16599 if (spec_die->is_external)
16600 part_die->is_external = spec_die->is_external;
16604 /* Set default names for some unnamed DIEs. */
16606 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16607 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16609 /* If there is no parent die to provide a namespace, and there are
16610 children, see if we can determine the namespace from their linkage
16612 if (cu->language == language_cplus
16613 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16614 && part_die->die_parent == NULL
16615 && part_die->has_children
16616 && (part_die->tag == DW_TAG_class_type
16617 || part_die->tag == DW_TAG_structure_type
16618 || part_die->tag == DW_TAG_union_type))
16619 guess_partial_die_structure_name (part_die, cu);
16621 /* GCC might emit a nameless struct or union that has a linkage
16622 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16623 if (part_die->name == NULL
16624 && (part_die->tag == DW_TAG_class_type
16625 || part_die->tag == DW_TAG_interface_type
16626 || part_die->tag == DW_TAG_structure_type
16627 || part_die->tag == DW_TAG_union_type)
16628 && part_die->linkage_name != NULL)
16632 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16637 /* Strip any leading namespaces/classes, keep only the base name.
16638 DW_AT_name for named DIEs does not contain the prefixes. */
16639 base = strrchr (demangled, ':');
16640 if (base && base > demangled && base[-1] == ':')
16647 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16648 base, strlen (base)));
16653 part_die->fixup_called = 1;
16656 /* Read an attribute value described by an attribute form. */
16658 static const gdb_byte *
16659 read_attribute_value (const struct die_reader_specs *reader,
16660 struct attribute *attr, unsigned form,
16661 LONGEST implicit_const, const gdb_byte *info_ptr)
16663 struct dwarf2_cu *cu = reader->cu;
16664 struct objfile *objfile = cu->objfile;
16665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16666 bfd *abfd = reader->abfd;
16667 struct comp_unit_head *cu_header = &cu->header;
16668 unsigned int bytes_read;
16669 struct dwarf_block *blk;
16671 attr->form = (enum dwarf_form) form;
16674 case DW_FORM_ref_addr:
16675 if (cu->header.version == 2)
16676 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16678 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16679 &cu->header, &bytes_read);
16680 info_ptr += bytes_read;
16682 case DW_FORM_GNU_ref_alt:
16683 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16684 info_ptr += bytes_read;
16687 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16688 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16689 info_ptr += bytes_read;
16691 case DW_FORM_block2:
16692 blk = dwarf_alloc_block (cu);
16693 blk->size = read_2_bytes (abfd, info_ptr);
16695 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16696 info_ptr += blk->size;
16697 DW_BLOCK (attr) = blk;
16699 case DW_FORM_block4:
16700 blk = dwarf_alloc_block (cu);
16701 blk->size = read_4_bytes (abfd, info_ptr);
16703 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16704 info_ptr += blk->size;
16705 DW_BLOCK (attr) = blk;
16707 case DW_FORM_data2:
16708 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16711 case DW_FORM_data4:
16712 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16715 case DW_FORM_data8:
16716 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16719 case DW_FORM_data16:
16720 blk = dwarf_alloc_block (cu);
16722 blk->data = read_n_bytes (abfd, info_ptr, 16);
16724 DW_BLOCK (attr) = blk;
16726 case DW_FORM_sec_offset:
16727 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16728 info_ptr += bytes_read;
16730 case DW_FORM_string:
16731 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16732 DW_STRING_IS_CANONICAL (attr) = 0;
16733 info_ptr += bytes_read;
16736 if (!cu->per_cu->is_dwz)
16738 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16740 DW_STRING_IS_CANONICAL (attr) = 0;
16741 info_ptr += bytes_read;
16745 case DW_FORM_line_strp:
16746 if (!cu->per_cu->is_dwz)
16748 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16749 cu_header, &bytes_read);
16750 DW_STRING_IS_CANONICAL (attr) = 0;
16751 info_ptr += bytes_read;
16755 case DW_FORM_GNU_strp_alt:
16757 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16758 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16761 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16762 DW_STRING_IS_CANONICAL (attr) = 0;
16763 info_ptr += bytes_read;
16766 case DW_FORM_exprloc:
16767 case DW_FORM_block:
16768 blk = dwarf_alloc_block (cu);
16769 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16770 info_ptr += bytes_read;
16771 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16772 info_ptr += blk->size;
16773 DW_BLOCK (attr) = blk;
16775 case DW_FORM_block1:
16776 blk = dwarf_alloc_block (cu);
16777 blk->size = read_1_byte (abfd, info_ptr);
16779 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16780 info_ptr += blk->size;
16781 DW_BLOCK (attr) = blk;
16783 case DW_FORM_data1:
16784 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16788 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16791 case DW_FORM_flag_present:
16792 DW_UNSND (attr) = 1;
16794 case DW_FORM_sdata:
16795 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16796 info_ptr += bytes_read;
16798 case DW_FORM_udata:
16799 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16800 info_ptr += bytes_read;
16803 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16804 + read_1_byte (abfd, info_ptr));
16808 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16809 + read_2_bytes (abfd, info_ptr));
16813 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16814 + read_4_bytes (abfd, info_ptr));
16818 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16819 + read_8_bytes (abfd, info_ptr));
16822 case DW_FORM_ref_sig8:
16823 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16826 case DW_FORM_ref_udata:
16827 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16828 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16829 info_ptr += bytes_read;
16831 case DW_FORM_indirect:
16832 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16833 info_ptr += bytes_read;
16834 if (form == DW_FORM_implicit_const)
16836 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16837 info_ptr += bytes_read;
16839 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16842 case DW_FORM_implicit_const:
16843 DW_SND (attr) = implicit_const;
16845 case DW_FORM_GNU_addr_index:
16846 if (reader->dwo_file == NULL)
16848 /* For now flag a hard error.
16849 Later we can turn this into a complaint. */
16850 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16851 dwarf_form_name (form),
16852 bfd_get_filename (abfd));
16854 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16855 info_ptr += bytes_read;
16857 case DW_FORM_GNU_str_index:
16858 if (reader->dwo_file == NULL)
16860 /* For now flag a hard error.
16861 Later we can turn this into a complaint if warranted. */
16862 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16863 dwarf_form_name (form),
16864 bfd_get_filename (abfd));
16867 ULONGEST str_index =
16868 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16870 DW_STRING (attr) = read_str_index (reader, str_index);
16871 DW_STRING_IS_CANONICAL (attr) = 0;
16872 info_ptr += bytes_read;
16876 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16877 dwarf_form_name (form),
16878 bfd_get_filename (abfd));
16882 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16883 attr->form = DW_FORM_GNU_ref_alt;
16885 /* We have seen instances where the compiler tried to emit a byte
16886 size attribute of -1 which ended up being encoded as an unsigned
16887 0xffffffff. Although 0xffffffff is technically a valid size value,
16888 an object of this size seems pretty unlikely so we can relatively
16889 safely treat these cases as if the size attribute was invalid and
16890 treat them as zero by default. */
16891 if (attr->name == DW_AT_byte_size
16892 && form == DW_FORM_data4
16893 && DW_UNSND (attr) >= 0xffffffff)
16896 (&symfile_complaints,
16897 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16898 hex_string (DW_UNSND (attr)));
16899 DW_UNSND (attr) = 0;
16905 /* Read an attribute described by an abbreviated attribute. */
16907 static const gdb_byte *
16908 read_attribute (const struct die_reader_specs *reader,
16909 struct attribute *attr, struct attr_abbrev *abbrev,
16910 const gdb_byte *info_ptr)
16912 attr->name = abbrev->name;
16913 return read_attribute_value (reader, attr, abbrev->form,
16914 abbrev->implicit_const, info_ptr);
16917 /* Read dwarf information from a buffer. */
16919 static unsigned int
16920 read_1_byte (bfd *abfd, const gdb_byte *buf)
16922 return bfd_get_8 (abfd, buf);
16926 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16928 return bfd_get_signed_8 (abfd, buf);
16931 static unsigned int
16932 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16934 return bfd_get_16 (abfd, buf);
16938 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16940 return bfd_get_signed_16 (abfd, buf);
16943 static unsigned int
16944 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16946 return bfd_get_32 (abfd, buf);
16950 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16952 return bfd_get_signed_32 (abfd, buf);
16956 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16958 return bfd_get_64 (abfd, buf);
16962 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16963 unsigned int *bytes_read)
16965 struct comp_unit_head *cu_header = &cu->header;
16966 CORE_ADDR retval = 0;
16968 if (cu_header->signed_addr_p)
16970 switch (cu_header->addr_size)
16973 retval = bfd_get_signed_16 (abfd, buf);
16976 retval = bfd_get_signed_32 (abfd, buf);
16979 retval = bfd_get_signed_64 (abfd, buf);
16982 internal_error (__FILE__, __LINE__,
16983 _("read_address: bad switch, signed [in module %s]"),
16984 bfd_get_filename (abfd));
16989 switch (cu_header->addr_size)
16992 retval = bfd_get_16 (abfd, buf);
16995 retval = bfd_get_32 (abfd, buf);
16998 retval = bfd_get_64 (abfd, buf);
17001 internal_error (__FILE__, __LINE__,
17002 _("read_address: bad switch, "
17003 "unsigned [in module %s]"),
17004 bfd_get_filename (abfd));
17008 *bytes_read = cu_header->addr_size;
17012 /* Read the initial length from a section. The (draft) DWARF 3
17013 specification allows the initial length to take up either 4 bytes
17014 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17015 bytes describe the length and all offsets will be 8 bytes in length
17018 An older, non-standard 64-bit format is also handled by this
17019 function. The older format in question stores the initial length
17020 as an 8-byte quantity without an escape value. Lengths greater
17021 than 2^32 aren't very common which means that the initial 4 bytes
17022 is almost always zero. Since a length value of zero doesn't make
17023 sense for the 32-bit format, this initial zero can be considered to
17024 be an escape value which indicates the presence of the older 64-bit
17025 format. As written, the code can't detect (old format) lengths
17026 greater than 4GB. If it becomes necessary to handle lengths
17027 somewhat larger than 4GB, we could allow other small values (such
17028 as the non-sensical values of 1, 2, and 3) to also be used as
17029 escape values indicating the presence of the old format.
17031 The value returned via bytes_read should be used to increment the
17032 relevant pointer after calling read_initial_length().
17034 [ Note: read_initial_length() and read_offset() are based on the
17035 document entitled "DWARF Debugging Information Format", revision
17036 3, draft 8, dated November 19, 2001. This document was obtained
17039 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
17041 This document is only a draft and is subject to change. (So beware.)
17043 Details regarding the older, non-standard 64-bit format were
17044 determined empirically by examining 64-bit ELF files produced by
17045 the SGI toolchain on an IRIX 6.5 machine.
17047 - Kevin, July 16, 2002
17051 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
17053 LONGEST length = bfd_get_32 (abfd, buf);
17055 if (length == 0xffffffff)
17057 length = bfd_get_64 (abfd, buf + 4);
17060 else if (length == 0)
17062 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
17063 length = bfd_get_64 (abfd, buf);
17074 /* Cover function for read_initial_length.
17075 Returns the length of the object at BUF, and stores the size of the
17076 initial length in *BYTES_READ and stores the size that offsets will be in
17078 If the initial length size is not equivalent to that specified in
17079 CU_HEADER then issue a complaint.
17080 This is useful when reading non-comp-unit headers. */
17083 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17084 const struct comp_unit_head *cu_header,
17085 unsigned int *bytes_read,
17086 unsigned int *offset_size)
17088 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17090 gdb_assert (cu_header->initial_length_size == 4
17091 || cu_header->initial_length_size == 8
17092 || cu_header->initial_length_size == 12);
17094 if (cu_header->initial_length_size != *bytes_read)
17095 complaint (&symfile_complaints,
17096 _("intermixed 32-bit and 64-bit DWARF sections"));
17098 *offset_size = (*bytes_read == 4) ? 4 : 8;
17102 /* Read an offset from the data stream. The size of the offset is
17103 given by cu_header->offset_size. */
17106 read_offset (bfd *abfd, const gdb_byte *buf,
17107 const struct comp_unit_head *cu_header,
17108 unsigned int *bytes_read)
17110 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17112 *bytes_read = cu_header->offset_size;
17116 /* Read an offset from the data stream. */
17119 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17121 LONGEST retval = 0;
17123 switch (offset_size)
17126 retval = bfd_get_32 (abfd, buf);
17129 retval = bfd_get_64 (abfd, buf);
17132 internal_error (__FILE__, __LINE__,
17133 _("read_offset_1: bad switch [in module %s]"),
17134 bfd_get_filename (abfd));
17140 static const gdb_byte *
17141 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17143 /* If the size of a host char is 8 bits, we can return a pointer
17144 to the buffer, otherwise we have to copy the data to a buffer
17145 allocated on the temporary obstack. */
17146 gdb_assert (HOST_CHAR_BIT == 8);
17150 static const char *
17151 read_direct_string (bfd *abfd, const gdb_byte *buf,
17152 unsigned int *bytes_read_ptr)
17154 /* If the size of a host char is 8 bits, we can return a pointer
17155 to the string, otherwise we have to copy the string to a buffer
17156 allocated on the temporary obstack. */
17157 gdb_assert (HOST_CHAR_BIT == 8);
17160 *bytes_read_ptr = 1;
17163 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17164 return (const char *) buf;
17167 /* Return pointer to string at section SECT offset STR_OFFSET with error
17168 reporting strings FORM_NAME and SECT_NAME. */
17170 static const char *
17171 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17172 struct dwarf2_section_info *sect,
17173 const char *form_name,
17174 const char *sect_name)
17176 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17177 if (sect->buffer == NULL)
17178 error (_("%s used without %s section [in module %s]"),
17179 form_name, sect_name, bfd_get_filename (abfd));
17180 if (str_offset >= sect->size)
17181 error (_("%s pointing outside of %s section [in module %s]"),
17182 form_name, sect_name, bfd_get_filename (abfd));
17183 gdb_assert (HOST_CHAR_BIT == 8);
17184 if (sect->buffer[str_offset] == '\0')
17186 return (const char *) (sect->buffer + str_offset);
17189 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17191 static const char *
17192 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17194 return read_indirect_string_at_offset_from (abfd, str_offset,
17195 &dwarf2_per_objfile->str,
17196 "DW_FORM_strp", ".debug_str");
17199 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17201 static const char *
17202 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17204 return read_indirect_string_at_offset_from (abfd, str_offset,
17205 &dwarf2_per_objfile->line_str,
17206 "DW_FORM_line_strp",
17207 ".debug_line_str");
17210 /* Read a string at offset STR_OFFSET in the .debug_str section from
17211 the .dwz file DWZ. Throw an error if the offset is too large. If
17212 the string consists of a single NUL byte, return NULL; otherwise
17213 return a pointer to the string. */
17215 static const char *
17216 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17218 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17220 if (dwz->str.buffer == NULL)
17221 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17222 "section [in module %s]"),
17223 bfd_get_filename (dwz->dwz_bfd));
17224 if (str_offset >= dwz->str.size)
17225 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17226 ".debug_str section [in module %s]"),
17227 bfd_get_filename (dwz->dwz_bfd));
17228 gdb_assert (HOST_CHAR_BIT == 8);
17229 if (dwz->str.buffer[str_offset] == '\0')
17231 return (const char *) (dwz->str.buffer + str_offset);
17234 /* Return pointer to string at .debug_str offset as read from BUF.
17235 BUF is assumed to be in a compilation unit described by CU_HEADER.
17236 Return *BYTES_READ_PTR count of bytes read from BUF. */
17238 static const char *
17239 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17240 const struct comp_unit_head *cu_header,
17241 unsigned int *bytes_read_ptr)
17243 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17245 return read_indirect_string_at_offset (abfd, str_offset);
17248 /* Return pointer to string at .debug_line_str offset as read from BUF.
17249 BUF is assumed to be in a compilation unit described by CU_HEADER.
17250 Return *BYTES_READ_PTR count of bytes read from BUF. */
17252 static const char *
17253 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17254 const struct comp_unit_head *cu_header,
17255 unsigned int *bytes_read_ptr)
17257 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17259 return read_indirect_line_string_at_offset (abfd, str_offset);
17263 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17264 unsigned int *bytes_read_ptr)
17267 unsigned int num_read;
17269 unsigned char byte;
17276 byte = bfd_get_8 (abfd, buf);
17279 result |= ((ULONGEST) (byte & 127) << shift);
17280 if ((byte & 128) == 0)
17286 *bytes_read_ptr = num_read;
17291 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17292 unsigned int *bytes_read_ptr)
17295 int shift, num_read;
17296 unsigned char byte;
17303 byte = bfd_get_8 (abfd, buf);
17306 result |= ((LONGEST) (byte & 127) << shift);
17308 if ((byte & 128) == 0)
17313 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17314 result |= -(((LONGEST) 1) << shift);
17315 *bytes_read_ptr = num_read;
17319 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17320 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17321 ADDR_SIZE is the size of addresses from the CU header. */
17324 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17326 struct objfile *objfile = dwarf2_per_objfile->objfile;
17327 bfd *abfd = objfile->obfd;
17328 const gdb_byte *info_ptr;
17330 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17331 if (dwarf2_per_objfile->addr.buffer == NULL)
17332 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17333 objfile_name (objfile));
17334 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17335 error (_("DW_FORM_addr_index pointing outside of "
17336 ".debug_addr section [in module %s]"),
17337 objfile_name (objfile));
17338 info_ptr = (dwarf2_per_objfile->addr.buffer
17339 + addr_base + addr_index * addr_size);
17340 if (addr_size == 4)
17341 return bfd_get_32 (abfd, info_ptr);
17343 return bfd_get_64 (abfd, info_ptr);
17346 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17349 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17351 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17354 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17357 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17358 unsigned int *bytes_read)
17360 bfd *abfd = cu->objfile->obfd;
17361 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17363 return read_addr_index (cu, addr_index);
17366 /* Data structure to pass results from dwarf2_read_addr_index_reader
17367 back to dwarf2_read_addr_index. */
17369 struct dwarf2_read_addr_index_data
17371 ULONGEST addr_base;
17375 /* die_reader_func for dwarf2_read_addr_index. */
17378 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17379 const gdb_byte *info_ptr,
17380 struct die_info *comp_unit_die,
17384 struct dwarf2_cu *cu = reader->cu;
17385 struct dwarf2_read_addr_index_data *aidata =
17386 (struct dwarf2_read_addr_index_data *) data;
17388 aidata->addr_base = cu->addr_base;
17389 aidata->addr_size = cu->header.addr_size;
17392 /* Given an index in .debug_addr, fetch the value.
17393 NOTE: This can be called during dwarf expression evaluation,
17394 long after the debug information has been read, and thus per_cu->cu
17395 may no longer exist. */
17398 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17399 unsigned int addr_index)
17401 struct objfile *objfile = per_cu->objfile;
17402 struct dwarf2_cu *cu = per_cu->cu;
17403 ULONGEST addr_base;
17406 /* This is intended to be called from outside this file. */
17407 dw2_setup (objfile);
17409 /* We need addr_base and addr_size.
17410 If we don't have PER_CU->cu, we have to get it.
17411 Nasty, but the alternative is storing the needed info in PER_CU,
17412 which at this point doesn't seem justified: it's not clear how frequently
17413 it would get used and it would increase the size of every PER_CU.
17414 Entry points like dwarf2_per_cu_addr_size do a similar thing
17415 so we're not in uncharted territory here.
17416 Alas we need to be a bit more complicated as addr_base is contained
17419 We don't need to read the entire CU(/TU).
17420 We just need the header and top level die.
17422 IWBN to use the aging mechanism to let us lazily later discard the CU.
17423 For now we skip this optimization. */
17427 addr_base = cu->addr_base;
17428 addr_size = cu->header.addr_size;
17432 struct dwarf2_read_addr_index_data aidata;
17434 /* Note: We can't use init_cutu_and_read_dies_simple here,
17435 we need addr_base. */
17436 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17437 dwarf2_read_addr_index_reader, &aidata);
17438 addr_base = aidata.addr_base;
17439 addr_size = aidata.addr_size;
17442 return read_addr_index_1 (addr_index, addr_base, addr_size);
17445 /* Given a DW_FORM_GNU_str_index, fetch the string.
17446 This is only used by the Fission support. */
17448 static const char *
17449 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17451 struct objfile *objfile = dwarf2_per_objfile->objfile;
17452 const char *objf_name = objfile_name (objfile);
17453 bfd *abfd = objfile->obfd;
17454 struct dwarf2_cu *cu = reader->cu;
17455 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17456 struct dwarf2_section_info *str_offsets_section =
17457 &reader->dwo_file->sections.str_offsets;
17458 const gdb_byte *info_ptr;
17459 ULONGEST str_offset;
17460 static const char form_name[] = "DW_FORM_GNU_str_index";
17462 dwarf2_read_section (objfile, str_section);
17463 dwarf2_read_section (objfile, str_offsets_section);
17464 if (str_section->buffer == NULL)
17465 error (_("%s used without .debug_str.dwo section"
17466 " in CU at offset 0x%x [in module %s]"),
17467 form_name, to_underlying (cu->header.sect_off), objf_name);
17468 if (str_offsets_section->buffer == NULL)
17469 error (_("%s used without .debug_str_offsets.dwo section"
17470 " in CU at offset 0x%x [in module %s]"),
17471 form_name, to_underlying (cu->header.sect_off), objf_name);
17472 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17473 error (_("%s pointing outside of .debug_str_offsets.dwo"
17474 " section in CU at offset 0x%x [in module %s]"),
17475 form_name, to_underlying (cu->header.sect_off), objf_name);
17476 info_ptr = (str_offsets_section->buffer
17477 + str_index * cu->header.offset_size);
17478 if (cu->header.offset_size == 4)
17479 str_offset = bfd_get_32 (abfd, info_ptr);
17481 str_offset = bfd_get_64 (abfd, info_ptr);
17482 if (str_offset >= str_section->size)
17483 error (_("Offset from %s pointing outside of"
17484 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17485 form_name, to_underlying (cu->header.sect_off), objf_name);
17486 return (const char *) (str_section->buffer + str_offset);
17489 /* Return the length of an LEB128 number in BUF. */
17492 leb128_size (const gdb_byte *buf)
17494 const gdb_byte *begin = buf;
17500 if ((byte & 128) == 0)
17501 return buf - begin;
17506 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17515 cu->language = language_c;
17518 case DW_LANG_C_plus_plus:
17519 case DW_LANG_C_plus_plus_11:
17520 case DW_LANG_C_plus_plus_14:
17521 cu->language = language_cplus;
17524 cu->language = language_d;
17526 case DW_LANG_Fortran77:
17527 case DW_LANG_Fortran90:
17528 case DW_LANG_Fortran95:
17529 case DW_LANG_Fortran03:
17530 case DW_LANG_Fortran08:
17531 cu->language = language_fortran;
17534 cu->language = language_go;
17536 case DW_LANG_Mips_Assembler:
17537 cu->language = language_asm;
17539 case DW_LANG_Ada83:
17540 case DW_LANG_Ada95:
17541 cu->language = language_ada;
17543 case DW_LANG_Modula2:
17544 cu->language = language_m2;
17546 case DW_LANG_Pascal83:
17547 cu->language = language_pascal;
17550 cu->language = language_objc;
17553 case DW_LANG_Rust_old:
17554 cu->language = language_rust;
17556 case DW_LANG_Cobol74:
17557 case DW_LANG_Cobol85:
17559 cu->language = language_minimal;
17562 cu->language_defn = language_def (cu->language);
17565 /* Return the named attribute or NULL if not there. */
17567 static struct attribute *
17568 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17573 struct attribute *spec = NULL;
17575 for (i = 0; i < die->num_attrs; ++i)
17577 if (die->attrs[i].name == name)
17578 return &die->attrs[i];
17579 if (die->attrs[i].name == DW_AT_specification
17580 || die->attrs[i].name == DW_AT_abstract_origin)
17581 spec = &die->attrs[i];
17587 die = follow_die_ref (die, spec, &cu);
17593 /* Return the named attribute or NULL if not there,
17594 but do not follow DW_AT_specification, etc.
17595 This is for use in contexts where we're reading .debug_types dies.
17596 Following DW_AT_specification, DW_AT_abstract_origin will take us
17597 back up the chain, and we want to go down. */
17599 static struct attribute *
17600 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17604 for (i = 0; i < die->num_attrs; ++i)
17605 if (die->attrs[i].name == name)
17606 return &die->attrs[i];
17611 /* Return the string associated with a string-typed attribute, or NULL if it
17612 is either not found or is of an incorrect type. */
17614 static const char *
17615 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17617 struct attribute *attr;
17618 const char *str = NULL;
17620 attr = dwarf2_attr (die, name, cu);
17624 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17625 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17626 str = DW_STRING (attr);
17628 complaint (&symfile_complaints,
17629 _("string type expected for attribute %s for "
17630 "DIE at 0x%x in module %s"),
17631 dwarf_attr_name (name), to_underlying (die->sect_off),
17632 objfile_name (cu->objfile));
17638 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17639 and holds a non-zero value. This function should only be used for
17640 DW_FORM_flag or DW_FORM_flag_present attributes. */
17643 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17645 struct attribute *attr = dwarf2_attr (die, name, cu);
17647 return (attr && DW_UNSND (attr));
17651 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17653 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17654 which value is non-zero. However, we have to be careful with
17655 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17656 (via dwarf2_flag_true_p) follows this attribute. So we may
17657 end up accidently finding a declaration attribute that belongs
17658 to a different DIE referenced by the specification attribute,
17659 even though the given DIE does not have a declaration attribute. */
17660 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17661 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17664 /* Return the die giving the specification for DIE, if there is
17665 one. *SPEC_CU is the CU containing DIE on input, and the CU
17666 containing the return value on output. If there is no
17667 specification, but there is an abstract origin, that is
17670 static struct die_info *
17671 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17673 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17676 if (spec_attr == NULL)
17677 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17679 if (spec_attr == NULL)
17682 return follow_die_ref (die, spec_attr, spec_cu);
17685 /* Stub for free_line_header to match void * callback types. */
17688 free_line_header_voidp (void *arg)
17690 struct line_header *lh = (struct line_header *) arg;
17696 line_header::add_include_dir (const char *include_dir)
17698 if (dwarf_line_debug >= 2)
17699 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17700 include_dirs.size () + 1, include_dir);
17702 include_dirs.push_back (include_dir);
17706 line_header::add_file_name (const char *name,
17708 unsigned int mod_time,
17709 unsigned int length)
17711 if (dwarf_line_debug >= 2)
17712 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17713 (unsigned) file_names.size () + 1, name);
17715 file_names.emplace_back (name, d_index, mod_time, length);
17718 /* A convenience function to find the proper .debug_line section for a CU. */
17720 static struct dwarf2_section_info *
17721 get_debug_line_section (struct dwarf2_cu *cu)
17723 struct dwarf2_section_info *section;
17725 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17727 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17728 section = &cu->dwo_unit->dwo_file->sections.line;
17729 else if (cu->per_cu->is_dwz)
17731 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17733 section = &dwz->line;
17736 section = &dwarf2_per_objfile->line;
17741 /* Read directory or file name entry format, starting with byte of
17742 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17743 entries count and the entries themselves in the described entry
17747 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17748 struct line_header *lh,
17749 const struct comp_unit_head *cu_header,
17750 void (*callback) (struct line_header *lh,
17753 unsigned int mod_time,
17754 unsigned int length))
17756 gdb_byte format_count, formati;
17757 ULONGEST data_count, datai;
17758 const gdb_byte *buf = *bufp;
17759 const gdb_byte *format_header_data;
17761 unsigned int bytes_read;
17763 format_count = read_1_byte (abfd, buf);
17765 format_header_data = buf;
17766 for (formati = 0; formati < format_count; formati++)
17768 read_unsigned_leb128 (abfd, buf, &bytes_read);
17770 read_unsigned_leb128 (abfd, buf, &bytes_read);
17774 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17776 for (datai = 0; datai < data_count; datai++)
17778 const gdb_byte *format = format_header_data;
17779 struct file_entry fe;
17781 for (formati = 0; formati < format_count; formati++)
17783 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17784 format += bytes_read;
17786 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
17787 format += bytes_read;
17789 gdb::optional<const char *> string;
17790 gdb::optional<unsigned int> uint;
17794 case DW_FORM_string:
17795 string.emplace (read_direct_string (abfd, buf, &bytes_read));
17799 case DW_FORM_line_strp:
17800 string.emplace (read_indirect_line_string (abfd, buf,
17806 case DW_FORM_data1:
17807 uint.emplace (read_1_byte (abfd, buf));
17811 case DW_FORM_data2:
17812 uint.emplace (read_2_bytes (abfd, buf));
17816 case DW_FORM_data4:
17817 uint.emplace (read_4_bytes (abfd, buf));
17821 case DW_FORM_data8:
17822 uint.emplace (read_8_bytes (abfd, buf));
17826 case DW_FORM_udata:
17827 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
17831 case DW_FORM_block:
17832 /* It is valid only for DW_LNCT_timestamp which is ignored by
17837 switch (content_type)
17840 if (string.has_value ())
17843 case DW_LNCT_directory_index:
17844 if (uint.has_value ())
17845 fe.d_index = (dir_index) *uint;
17847 case DW_LNCT_timestamp:
17848 if (uint.has_value ())
17849 fe.mod_time = *uint;
17852 if (uint.has_value ())
17858 complaint (&symfile_complaints,
17859 _("Unknown format content type %s"),
17860 pulongest (content_type));
17864 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
17870 /* Read the statement program header starting at OFFSET in
17871 .debug_line, or .debug_line.dwo. Return a pointer
17872 to a struct line_header, allocated using xmalloc.
17873 Returns NULL if there is a problem reading the header, e.g., if it
17874 has a version we don't understand.
17876 NOTE: the strings in the include directory and file name tables of
17877 the returned object point into the dwarf line section buffer,
17878 and must not be freed. */
17880 static line_header_up
17881 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
17883 const gdb_byte *line_ptr;
17884 unsigned int bytes_read, offset_size;
17886 const char *cur_dir, *cur_file;
17887 struct dwarf2_section_info *section;
17890 section = get_debug_line_section (cu);
17891 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17892 if (section->buffer == NULL)
17894 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17895 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17897 complaint (&symfile_complaints, _("missing .debug_line section"));
17901 /* We can't do this until we know the section is non-empty.
17902 Only then do we know we have such a section. */
17903 abfd = get_section_bfd_owner (section);
17905 /* Make sure that at least there's room for the total_length field.
17906 That could be 12 bytes long, but we're just going to fudge that. */
17907 if (to_underlying (sect_off) + 4 >= section->size)
17909 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17913 line_header_up lh (new line_header ());
17915 lh->sect_off = sect_off;
17916 lh->offset_in_dwz = cu->per_cu->is_dwz;
17918 line_ptr = section->buffer + to_underlying (sect_off);
17920 /* Read in the header. */
17922 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17923 &bytes_read, &offset_size);
17924 line_ptr += bytes_read;
17925 if (line_ptr + lh->total_length > (section->buffer + section->size))
17927 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17930 lh->statement_program_end = line_ptr + lh->total_length;
17931 lh->version = read_2_bytes (abfd, line_ptr);
17933 if (lh->version > 5)
17935 /* This is a version we don't understand. The format could have
17936 changed in ways we don't handle properly so just punt. */
17937 complaint (&symfile_complaints,
17938 _("unsupported version in .debug_line section"));
17941 if (lh->version >= 5)
17943 gdb_byte segment_selector_size;
17945 /* Skip address size. */
17946 read_1_byte (abfd, line_ptr);
17949 segment_selector_size = read_1_byte (abfd, line_ptr);
17951 if (segment_selector_size != 0)
17953 complaint (&symfile_complaints,
17954 _("unsupported segment selector size %u "
17955 "in .debug_line section"),
17956 segment_selector_size);
17960 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17961 line_ptr += offset_size;
17962 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17964 if (lh->version >= 4)
17966 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17970 lh->maximum_ops_per_instruction = 1;
17972 if (lh->maximum_ops_per_instruction == 0)
17974 lh->maximum_ops_per_instruction = 1;
17975 complaint (&symfile_complaints,
17976 _("invalid maximum_ops_per_instruction "
17977 "in `.debug_line' section"));
17980 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17982 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17984 lh->line_range = read_1_byte (abfd, line_ptr);
17986 lh->opcode_base = read_1_byte (abfd, line_ptr);
17988 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
17990 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17991 for (i = 1; i < lh->opcode_base; ++i)
17993 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17997 if (lh->version >= 5)
17999 /* Read directory table. */
18000 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18001 [] (struct line_header *lh, const char *name,
18002 dir_index d_index, unsigned int mod_time,
18003 unsigned int length)
18005 lh->add_include_dir (name);
18008 /* Read file name table. */
18009 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18010 [] (struct line_header *lh, const char *name,
18011 dir_index d_index, unsigned int mod_time,
18012 unsigned int length)
18014 lh->add_file_name (name, d_index, mod_time, length);
18019 /* Read directory table. */
18020 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18022 line_ptr += bytes_read;
18023 lh->add_include_dir (cur_dir);
18025 line_ptr += bytes_read;
18027 /* Read file name table. */
18028 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18030 unsigned int mod_time, length;
18033 line_ptr += bytes_read;
18034 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18035 line_ptr += bytes_read;
18036 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18037 line_ptr += bytes_read;
18038 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18039 line_ptr += bytes_read;
18041 lh->add_file_name (cur_file, d_index, mod_time, length);
18043 line_ptr += bytes_read;
18045 lh->statement_program_start = line_ptr;
18047 if (line_ptr > (section->buffer + section->size))
18048 complaint (&symfile_complaints,
18049 _("line number info header doesn't "
18050 "fit in `.debug_line' section"));
18055 /* Subroutine of dwarf_decode_lines to simplify it.
18056 Return the file name of the psymtab for included file FILE_INDEX
18057 in line header LH of PST.
18058 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18059 If space for the result is malloc'd, it will be freed by a cleanup.
18060 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18062 The function creates dangling cleanup registration. */
18064 static const char *
18065 psymtab_include_file_name (const struct line_header *lh, int file_index,
18066 const struct partial_symtab *pst,
18067 const char *comp_dir)
18069 const file_entry &fe = lh->file_names[file_index];
18070 const char *include_name = fe.name;
18071 const char *include_name_to_compare = include_name;
18072 const char *pst_filename;
18073 char *copied_name = NULL;
18076 const char *dir_name = fe.include_dir (lh);
18078 if (!IS_ABSOLUTE_PATH (include_name)
18079 && (dir_name != NULL || comp_dir != NULL))
18081 /* Avoid creating a duplicate psymtab for PST.
18082 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18083 Before we do the comparison, however, we need to account
18084 for DIR_NAME and COMP_DIR.
18085 First prepend dir_name (if non-NULL). If we still don't
18086 have an absolute path prepend comp_dir (if non-NULL).
18087 However, the directory we record in the include-file's
18088 psymtab does not contain COMP_DIR (to match the
18089 corresponding symtab(s)).
18094 bash$ gcc -g ./hello.c
18095 include_name = "hello.c"
18097 DW_AT_comp_dir = comp_dir = "/tmp"
18098 DW_AT_name = "./hello.c"
18102 if (dir_name != NULL)
18104 char *tem = concat (dir_name, SLASH_STRING,
18105 include_name, (char *)NULL);
18107 make_cleanup (xfree, tem);
18108 include_name = tem;
18109 include_name_to_compare = include_name;
18111 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18113 char *tem = concat (comp_dir, SLASH_STRING,
18114 include_name, (char *)NULL);
18116 make_cleanup (xfree, tem);
18117 include_name_to_compare = tem;
18121 pst_filename = pst->filename;
18122 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18124 copied_name = concat (pst->dirname, SLASH_STRING,
18125 pst_filename, (char *)NULL);
18126 pst_filename = copied_name;
18129 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18131 if (copied_name != NULL)
18132 xfree (copied_name);
18136 return include_name;
18139 /* State machine to track the state of the line number program. */
18141 class lnp_state_machine
18144 /* Initialize a machine state for the start of a line number
18146 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18148 file_entry *current_file ()
18150 /* lh->file_names is 0-based, but the file name numbers in the
18151 statement program are 1-based. */
18152 return m_line_header->file_name_at (m_file);
18155 /* Record the line in the state machine. END_SEQUENCE is true if
18156 we're processing the end of a sequence. */
18157 void record_line (bool end_sequence);
18159 /* Check address and if invalid nop-out the rest of the lines in this
18161 void check_line_address (struct dwarf2_cu *cu,
18162 const gdb_byte *line_ptr,
18163 CORE_ADDR lowpc, CORE_ADDR address);
18165 void handle_set_discriminator (unsigned int discriminator)
18167 m_discriminator = discriminator;
18168 m_line_has_non_zero_discriminator |= discriminator != 0;
18171 /* Handle DW_LNE_set_address. */
18172 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18175 address += baseaddr;
18176 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18179 /* Handle DW_LNS_advance_pc. */
18180 void handle_advance_pc (CORE_ADDR adjust);
18182 /* Handle a special opcode. */
18183 void handle_special_opcode (unsigned char op_code);
18185 /* Handle DW_LNS_advance_line. */
18186 void handle_advance_line (int line_delta)
18188 advance_line (line_delta);
18191 /* Handle DW_LNS_set_file. */
18192 void handle_set_file (file_name_index file);
18194 /* Handle DW_LNS_negate_stmt. */
18195 void handle_negate_stmt ()
18197 m_is_stmt = !m_is_stmt;
18200 /* Handle DW_LNS_const_add_pc. */
18201 void handle_const_add_pc ();
18203 /* Handle DW_LNS_fixed_advance_pc. */
18204 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18206 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18210 /* Handle DW_LNS_copy. */
18211 void handle_copy ()
18213 record_line (false);
18214 m_discriminator = 0;
18217 /* Handle DW_LNE_end_sequence. */
18218 void handle_end_sequence ()
18220 m_record_line_callback = ::record_line;
18224 /* Advance the line by LINE_DELTA. */
18225 void advance_line (int line_delta)
18227 m_line += line_delta;
18229 if (line_delta != 0)
18230 m_line_has_non_zero_discriminator = m_discriminator != 0;
18233 gdbarch *m_gdbarch;
18235 /* True if we're recording lines.
18236 Otherwise we're building partial symtabs and are just interested in
18237 finding include files mentioned by the line number program. */
18238 bool m_record_lines_p;
18240 /* The line number header. */
18241 line_header *m_line_header;
18243 /* These are part of the standard DWARF line number state machine,
18244 and initialized according to the DWARF spec. */
18246 unsigned char m_op_index = 0;
18247 /* The line table index (1-based) of the current file. */
18248 file_name_index m_file = (file_name_index) 1;
18249 unsigned int m_line = 1;
18251 /* These are initialized in the constructor. */
18253 CORE_ADDR m_address;
18255 unsigned int m_discriminator;
18257 /* Additional bits of state we need to track. */
18259 /* The last file that we called dwarf2_start_subfile for.
18260 This is only used for TLLs. */
18261 unsigned int m_last_file = 0;
18262 /* The last file a line number was recorded for. */
18263 struct subfile *m_last_subfile = NULL;
18265 /* The function to call to record a line. */
18266 record_line_ftype *m_record_line_callback = NULL;
18268 /* The last line number that was recorded, used to coalesce
18269 consecutive entries for the same line. This can happen, for
18270 example, when discriminators are present. PR 17276. */
18271 unsigned int m_last_line = 0;
18272 bool m_line_has_non_zero_discriminator = false;
18276 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18278 CORE_ADDR addr_adj = (((m_op_index + adjust)
18279 / m_line_header->maximum_ops_per_instruction)
18280 * m_line_header->minimum_instruction_length);
18281 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18282 m_op_index = ((m_op_index + adjust)
18283 % m_line_header->maximum_ops_per_instruction);
18287 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18289 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18290 CORE_ADDR addr_adj = (((m_op_index
18291 + (adj_opcode / m_line_header->line_range))
18292 / m_line_header->maximum_ops_per_instruction)
18293 * m_line_header->minimum_instruction_length);
18294 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18295 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18296 % m_line_header->maximum_ops_per_instruction);
18298 int line_delta = (m_line_header->line_base
18299 + (adj_opcode % m_line_header->line_range));
18300 advance_line (line_delta);
18301 record_line (false);
18302 m_discriminator = 0;
18306 lnp_state_machine::handle_set_file (file_name_index file)
18310 const file_entry *fe = current_file ();
18312 dwarf2_debug_line_missing_file_complaint ();
18313 else if (m_record_lines_p)
18315 const char *dir = fe->include_dir (m_line_header);
18317 m_last_subfile = current_subfile;
18318 m_line_has_non_zero_discriminator = m_discriminator != 0;
18319 dwarf2_start_subfile (fe->name, dir);
18324 lnp_state_machine::handle_const_add_pc ()
18327 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18330 = (((m_op_index + adjust)
18331 / m_line_header->maximum_ops_per_instruction)
18332 * m_line_header->minimum_instruction_length);
18334 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18335 m_op_index = ((m_op_index + adjust)
18336 % m_line_header->maximum_ops_per_instruction);
18339 /* Ignore this record_line request. */
18342 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18347 /* Return non-zero if we should add LINE to the line number table.
18348 LINE is the line to add, LAST_LINE is the last line that was added,
18349 LAST_SUBFILE is the subfile for LAST_LINE.
18350 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18351 had a non-zero discriminator.
18353 We have to be careful in the presence of discriminators.
18354 E.g., for this line:
18356 for (i = 0; i < 100000; i++);
18358 clang can emit four line number entries for that one line,
18359 each with a different discriminator.
18360 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18362 However, we want gdb to coalesce all four entries into one.
18363 Otherwise the user could stepi into the middle of the line and
18364 gdb would get confused about whether the pc really was in the
18365 middle of the line.
18367 Things are further complicated by the fact that two consecutive
18368 line number entries for the same line is a heuristic used by gcc
18369 to denote the end of the prologue. So we can't just discard duplicate
18370 entries, we have to be selective about it. The heuristic we use is
18371 that we only collapse consecutive entries for the same line if at least
18372 one of those entries has a non-zero discriminator. PR 17276.
18374 Note: Addresses in the line number state machine can never go backwards
18375 within one sequence, thus this coalescing is ok. */
18378 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18379 int line_has_non_zero_discriminator,
18380 struct subfile *last_subfile)
18382 if (current_subfile != last_subfile)
18384 if (line != last_line)
18386 /* Same line for the same file that we've seen already.
18387 As a last check, for pr 17276, only record the line if the line
18388 has never had a non-zero discriminator. */
18389 if (!line_has_non_zero_discriminator)
18394 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18395 in the line table of subfile SUBFILE. */
18398 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18399 unsigned int line, CORE_ADDR address,
18400 record_line_ftype p_record_line)
18402 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18404 if (dwarf_line_debug)
18406 fprintf_unfiltered (gdb_stdlog,
18407 "Recording line %u, file %s, address %s\n",
18408 line, lbasename (subfile->name),
18409 paddress (gdbarch, address));
18412 (*p_record_line) (subfile, line, addr);
18415 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18416 Mark the end of a set of line number records.
18417 The arguments are the same as for dwarf_record_line_1.
18418 If SUBFILE is NULL the request is ignored. */
18421 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18422 CORE_ADDR address, record_line_ftype p_record_line)
18424 if (subfile == NULL)
18427 if (dwarf_line_debug)
18429 fprintf_unfiltered (gdb_stdlog,
18430 "Finishing current line, file %s, address %s\n",
18431 lbasename (subfile->name),
18432 paddress (gdbarch, address));
18435 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18439 lnp_state_machine::record_line (bool end_sequence)
18441 if (dwarf_line_debug)
18443 fprintf_unfiltered (gdb_stdlog,
18444 "Processing actual line %u: file %u,"
18445 " address %s, is_stmt %u, discrim %u\n",
18446 m_line, to_underlying (m_file),
18447 paddress (m_gdbarch, m_address),
18448 m_is_stmt, m_discriminator);
18451 file_entry *fe = current_file ();
18454 dwarf2_debug_line_missing_file_complaint ();
18455 /* For now we ignore lines not starting on an instruction boundary.
18456 But not when processing end_sequence for compatibility with the
18457 previous version of the code. */
18458 else if (m_op_index == 0 || end_sequence)
18460 fe->included_p = 1;
18461 if (m_record_lines_p && m_is_stmt)
18463 if (m_last_subfile != current_subfile || end_sequence)
18465 dwarf_finish_line (m_gdbarch, m_last_subfile,
18466 m_address, m_record_line_callback);
18471 if (dwarf_record_line_p (m_line, m_last_line,
18472 m_line_has_non_zero_discriminator,
18475 dwarf_record_line_1 (m_gdbarch, current_subfile,
18477 m_record_line_callback);
18479 m_last_subfile = current_subfile;
18480 m_last_line = m_line;
18486 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18487 bool record_lines_p)
18490 m_record_lines_p = record_lines_p;
18491 m_line_header = lh;
18493 m_record_line_callback = ::record_line;
18495 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18496 was a line entry for it so that the backend has a chance to adjust it
18497 and also record it in case it needs it. This is currently used by MIPS
18498 code, cf. `mips_adjust_dwarf2_line'. */
18499 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18500 m_is_stmt = lh->default_is_stmt;
18501 m_discriminator = 0;
18505 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18506 const gdb_byte *line_ptr,
18507 CORE_ADDR lowpc, CORE_ADDR address)
18509 /* If address < lowpc then it's not a usable value, it's outside the
18510 pc range of the CU. However, we restrict the test to only address
18511 values of zero to preserve GDB's previous behaviour which is to
18512 handle the specific case of a function being GC'd by the linker. */
18514 if (address == 0 && address < lowpc)
18516 /* This line table is for a function which has been
18517 GCd by the linker. Ignore it. PR gdb/12528 */
18519 struct objfile *objfile = cu->objfile;
18520 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18522 complaint (&symfile_complaints,
18523 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18524 line_offset, objfile_name (objfile));
18525 m_record_line_callback = noop_record_line;
18526 /* Note: record_line_callback is left as noop_record_line until
18527 we see DW_LNE_end_sequence. */
18531 /* Subroutine of dwarf_decode_lines to simplify it.
18532 Process the line number information in LH.
18533 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18534 program in order to set included_p for every referenced header. */
18537 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18538 const int decode_for_pst_p, CORE_ADDR lowpc)
18540 const gdb_byte *line_ptr, *extended_end;
18541 const gdb_byte *line_end;
18542 unsigned int bytes_read, extended_len;
18543 unsigned char op_code, extended_op;
18544 CORE_ADDR baseaddr;
18545 struct objfile *objfile = cu->objfile;
18546 bfd *abfd = objfile->obfd;
18547 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18548 /* True if we're recording line info (as opposed to building partial
18549 symtabs and just interested in finding include files mentioned by
18550 the line number program). */
18551 bool record_lines_p = !decode_for_pst_p;
18553 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18555 line_ptr = lh->statement_program_start;
18556 line_end = lh->statement_program_end;
18558 /* Read the statement sequences until there's nothing left. */
18559 while (line_ptr < line_end)
18561 /* The DWARF line number program state machine. Reset the state
18562 machine at the start of each sequence. */
18563 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18564 bool end_sequence = false;
18566 if (record_lines_p)
18568 /* Start a subfile for the current file of the state
18570 const file_entry *fe = state_machine.current_file ();
18573 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
18576 /* Decode the table. */
18577 while (line_ptr < line_end && !end_sequence)
18579 op_code = read_1_byte (abfd, line_ptr);
18582 if (op_code >= lh->opcode_base)
18584 /* Special opcode. */
18585 state_machine.handle_special_opcode (op_code);
18587 else switch (op_code)
18589 case DW_LNS_extended_op:
18590 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18592 line_ptr += bytes_read;
18593 extended_end = line_ptr + extended_len;
18594 extended_op = read_1_byte (abfd, line_ptr);
18596 switch (extended_op)
18598 case DW_LNE_end_sequence:
18599 state_machine.handle_end_sequence ();
18600 end_sequence = true;
18602 case DW_LNE_set_address:
18605 = read_address (abfd, line_ptr, cu, &bytes_read);
18606 line_ptr += bytes_read;
18608 state_machine.check_line_address (cu, line_ptr,
18610 state_machine.handle_set_address (baseaddr, address);
18613 case DW_LNE_define_file:
18615 const char *cur_file;
18616 unsigned int mod_time, length;
18619 cur_file = read_direct_string (abfd, line_ptr,
18621 line_ptr += bytes_read;
18622 dindex = (dir_index)
18623 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18624 line_ptr += bytes_read;
18626 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18627 line_ptr += bytes_read;
18629 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18630 line_ptr += bytes_read;
18631 lh->add_file_name (cur_file, dindex, mod_time, length);
18634 case DW_LNE_set_discriminator:
18636 /* The discriminator is not interesting to the
18637 debugger; just ignore it. We still need to
18638 check its value though:
18639 if there are consecutive entries for the same
18640 (non-prologue) line we want to coalesce them.
18643 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18644 line_ptr += bytes_read;
18646 state_machine.handle_set_discriminator (discr);
18650 complaint (&symfile_complaints,
18651 _("mangled .debug_line section"));
18654 /* Make sure that we parsed the extended op correctly. If e.g.
18655 we expected a different address size than the producer used,
18656 we may have read the wrong number of bytes. */
18657 if (line_ptr != extended_end)
18659 complaint (&symfile_complaints,
18660 _("mangled .debug_line section"));
18665 state_machine.handle_copy ();
18667 case DW_LNS_advance_pc:
18670 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18671 line_ptr += bytes_read;
18673 state_machine.handle_advance_pc (adjust);
18676 case DW_LNS_advance_line:
18679 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18680 line_ptr += bytes_read;
18682 state_machine.handle_advance_line (line_delta);
18685 case DW_LNS_set_file:
18687 file_name_index file
18688 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18690 line_ptr += bytes_read;
18692 state_machine.handle_set_file (file);
18695 case DW_LNS_set_column:
18696 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18697 line_ptr += bytes_read;
18699 case DW_LNS_negate_stmt:
18700 state_machine.handle_negate_stmt ();
18702 case DW_LNS_set_basic_block:
18704 /* Add to the address register of the state machine the
18705 address increment value corresponding to special opcode
18706 255. I.e., this value is scaled by the minimum
18707 instruction length since special opcode 255 would have
18708 scaled the increment. */
18709 case DW_LNS_const_add_pc:
18710 state_machine.handle_const_add_pc ();
18712 case DW_LNS_fixed_advance_pc:
18714 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18717 state_machine.handle_fixed_advance_pc (addr_adj);
18722 /* Unknown standard opcode, ignore it. */
18725 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18727 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18728 line_ptr += bytes_read;
18735 dwarf2_debug_line_missing_end_sequence_complaint ();
18737 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18738 in which case we still finish recording the last line). */
18739 state_machine.record_line (true);
18743 /* Decode the Line Number Program (LNP) for the given line_header
18744 structure and CU. The actual information extracted and the type
18745 of structures created from the LNP depends on the value of PST.
18747 1. If PST is NULL, then this procedure uses the data from the program
18748 to create all necessary symbol tables, and their linetables.
18750 2. If PST is not NULL, this procedure reads the program to determine
18751 the list of files included by the unit represented by PST, and
18752 builds all the associated partial symbol tables.
18754 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18755 It is used for relative paths in the line table.
18756 NOTE: When processing partial symtabs (pst != NULL),
18757 comp_dir == pst->dirname.
18759 NOTE: It is important that psymtabs have the same file name (via strcmp)
18760 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18761 symtab we don't use it in the name of the psymtabs we create.
18762 E.g. expand_line_sal requires this when finding psymtabs to expand.
18763 A good testcase for this is mb-inline.exp.
18765 LOWPC is the lowest address in CU (or 0 if not known).
18767 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18768 for its PC<->lines mapping information. Otherwise only the filename
18769 table is read in. */
18772 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18773 struct dwarf2_cu *cu, struct partial_symtab *pst,
18774 CORE_ADDR lowpc, int decode_mapping)
18776 struct objfile *objfile = cu->objfile;
18777 const int decode_for_pst_p = (pst != NULL);
18779 if (decode_mapping)
18780 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18782 if (decode_for_pst_p)
18786 /* Now that we're done scanning the Line Header Program, we can
18787 create the psymtab of each included file. */
18788 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
18789 if (lh->file_names[file_index].included_p == 1)
18791 const char *include_name =
18792 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18793 if (include_name != NULL)
18794 dwarf2_create_include_psymtab (include_name, pst, objfile);
18799 /* Make sure a symtab is created for every file, even files
18800 which contain only variables (i.e. no code with associated
18802 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18805 for (i = 0; i < lh->file_names.size (); i++)
18807 file_entry &fe = lh->file_names[i];
18809 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
18811 if (current_subfile->symtab == NULL)
18813 current_subfile->symtab
18814 = allocate_symtab (cust, current_subfile->name);
18816 fe.symtab = current_subfile->symtab;
18821 /* Start a subfile for DWARF. FILENAME is the name of the file and
18822 DIRNAME the name of the source directory which contains FILENAME
18823 or NULL if not known.
18824 This routine tries to keep line numbers from identical absolute and
18825 relative file names in a common subfile.
18827 Using the `list' example from the GDB testsuite, which resides in
18828 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18829 of /srcdir/list0.c yields the following debugging information for list0.c:
18831 DW_AT_name: /srcdir/list0.c
18832 DW_AT_comp_dir: /compdir
18833 files.files[0].name: list0.h
18834 files.files[0].dir: /srcdir
18835 files.files[1].name: list0.c
18836 files.files[1].dir: /srcdir
18838 The line number information for list0.c has to end up in a single
18839 subfile, so that `break /srcdir/list0.c:1' works as expected.
18840 start_subfile will ensure that this happens provided that we pass the
18841 concatenation of files.files[1].dir and files.files[1].name as the
18845 dwarf2_start_subfile (const char *filename, const char *dirname)
18849 /* In order not to lose the line information directory,
18850 we concatenate it to the filename when it makes sense.
18851 Note that the Dwarf3 standard says (speaking of filenames in line
18852 information): ``The directory index is ignored for file names
18853 that represent full path names''. Thus ignoring dirname in the
18854 `else' branch below isn't an issue. */
18856 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18858 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18862 start_subfile (filename);
18868 /* Start a symtab for DWARF.
18869 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18871 static struct compunit_symtab *
18872 dwarf2_start_symtab (struct dwarf2_cu *cu,
18873 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18875 struct compunit_symtab *cust
18876 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18878 record_debugformat ("DWARF 2");
18879 record_producer (cu->producer);
18881 /* We assume that we're processing GCC output. */
18882 processing_gcc_compilation = 2;
18884 cu->processing_has_namespace_info = 0;
18890 var_decode_location (struct attribute *attr, struct symbol *sym,
18891 struct dwarf2_cu *cu)
18893 struct objfile *objfile = cu->objfile;
18894 struct comp_unit_head *cu_header = &cu->header;
18896 /* NOTE drow/2003-01-30: There used to be a comment and some special
18897 code here to turn a symbol with DW_AT_external and a
18898 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18899 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18900 with some versions of binutils) where shared libraries could have
18901 relocations against symbols in their debug information - the
18902 minimal symbol would have the right address, but the debug info
18903 would not. It's no longer necessary, because we will explicitly
18904 apply relocations when we read in the debug information now. */
18906 /* A DW_AT_location attribute with no contents indicates that a
18907 variable has been optimized away. */
18908 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18910 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18914 /* Handle one degenerate form of location expression specially, to
18915 preserve GDB's previous behavior when section offsets are
18916 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18917 then mark this symbol as LOC_STATIC. */
18919 if (attr_form_is_block (attr)
18920 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18921 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18922 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18923 && (DW_BLOCK (attr)->size
18924 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18926 unsigned int dummy;
18928 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18929 SYMBOL_VALUE_ADDRESS (sym) =
18930 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18932 SYMBOL_VALUE_ADDRESS (sym) =
18933 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18934 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18935 fixup_symbol_section (sym, objfile);
18936 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18937 SYMBOL_SECTION (sym));
18941 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18942 expression evaluator, and use LOC_COMPUTED only when necessary
18943 (i.e. when the value of a register or memory location is
18944 referenced, or a thread-local block, etc.). Then again, it might
18945 not be worthwhile. I'm assuming that it isn't unless performance
18946 or memory numbers show me otherwise. */
18948 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18950 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18951 cu->has_loclist = 1;
18954 /* Given a pointer to a DWARF information entry, figure out if we need
18955 to make a symbol table entry for it, and if so, create a new entry
18956 and return a pointer to it.
18957 If TYPE is NULL, determine symbol type from the die, otherwise
18958 used the passed type.
18959 If SPACE is not NULL, use it to hold the new symbol. If it is
18960 NULL, allocate a new symbol on the objfile's obstack. */
18962 static struct symbol *
18963 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18964 struct symbol *space)
18966 struct objfile *objfile = cu->objfile;
18967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18968 struct symbol *sym = NULL;
18970 struct attribute *attr = NULL;
18971 struct attribute *attr2 = NULL;
18972 CORE_ADDR baseaddr;
18973 struct pending **list_to_add = NULL;
18975 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18979 name = dwarf2_name (die, cu);
18982 const char *linkagename;
18983 int suppress_add = 0;
18988 sym = allocate_symbol (objfile);
18989 OBJSTAT (objfile, n_syms++);
18991 /* Cache this symbol's name and the name's demangled form (if any). */
18992 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18993 linkagename = dwarf2_physname (name, die, cu);
18994 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18996 /* Fortran does not have mangling standard and the mangling does differ
18997 between gfortran, iFort etc. */
18998 if (cu->language == language_fortran
18999 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
19000 symbol_set_demangled_name (&(sym->ginfo),
19001 dwarf2_full_name (name, die, cu),
19004 /* Default assumptions.
19005 Use the passed type or decode it from the die. */
19006 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19007 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19009 SYMBOL_TYPE (sym) = type;
19011 SYMBOL_TYPE (sym) = die_type (die, cu);
19012 attr = dwarf2_attr (die,
19013 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19017 SYMBOL_LINE (sym) = DW_UNSND (attr);
19020 attr = dwarf2_attr (die,
19021 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19025 file_name_index file_index = (file_name_index) DW_UNSND (attr);
19026 struct file_entry *fe;
19028 if (cu->line_header != NULL)
19029 fe = cu->line_header->file_name_at (file_index);
19034 complaint (&symfile_complaints,
19035 _("file index out of range"));
19037 symbol_set_symtab (sym, fe->symtab);
19043 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
19048 addr = attr_value_as_address (attr);
19049 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19050 SYMBOL_VALUE_ADDRESS (sym) = addr;
19052 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19053 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
19054 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
19055 add_symbol_to_list (sym, cu->list_in_scope);
19057 case DW_TAG_subprogram:
19058 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19060 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19061 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19062 if ((attr2 && (DW_UNSND (attr2) != 0))
19063 || cu->language == language_ada)
19065 /* Subprograms marked external are stored as a global symbol.
19066 Ada subprograms, whether marked external or not, are always
19067 stored as a global symbol, because we want to be able to
19068 access them globally. For instance, we want to be able
19069 to break on a nested subprogram without having to
19070 specify the context. */
19071 list_to_add = &global_symbols;
19075 list_to_add = cu->list_in_scope;
19078 case DW_TAG_inlined_subroutine:
19079 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19081 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19082 SYMBOL_INLINED (sym) = 1;
19083 list_to_add = cu->list_in_scope;
19085 case DW_TAG_template_value_param:
19087 /* Fall through. */
19088 case DW_TAG_constant:
19089 case DW_TAG_variable:
19090 case DW_TAG_member:
19091 /* Compilation with minimal debug info may result in
19092 variables with missing type entries. Change the
19093 misleading `void' type to something sensible. */
19094 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19096 = objfile_type (objfile)->nodebug_data_symbol;
19098 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19099 /* In the case of DW_TAG_member, we should only be called for
19100 static const members. */
19101 if (die->tag == DW_TAG_member)
19103 /* dwarf2_add_field uses die_is_declaration,
19104 so we do the same. */
19105 gdb_assert (die_is_declaration (die, cu));
19110 dwarf2_const_value (attr, sym, cu);
19111 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19114 if (attr2 && (DW_UNSND (attr2) != 0))
19115 list_to_add = &global_symbols;
19117 list_to_add = cu->list_in_scope;
19121 attr = dwarf2_attr (die, DW_AT_location, cu);
19124 var_decode_location (attr, sym, cu);
19125 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19127 /* Fortran explicitly imports any global symbols to the local
19128 scope by DW_TAG_common_block. */
19129 if (cu->language == language_fortran && die->parent
19130 && die->parent->tag == DW_TAG_common_block)
19133 if (SYMBOL_CLASS (sym) == LOC_STATIC
19134 && SYMBOL_VALUE_ADDRESS (sym) == 0
19135 && !dwarf2_per_objfile->has_section_at_zero)
19137 /* When a static variable is eliminated by the linker,
19138 the corresponding debug information is not stripped
19139 out, but the variable address is set to null;
19140 do not add such variables into symbol table. */
19142 else if (attr2 && (DW_UNSND (attr2) != 0))
19144 /* Workaround gfortran PR debug/40040 - it uses
19145 DW_AT_location for variables in -fPIC libraries which may
19146 get overriden by other libraries/executable and get
19147 a different address. Resolve it by the minimal symbol
19148 which may come from inferior's executable using copy
19149 relocation. Make this workaround only for gfortran as for
19150 other compilers GDB cannot guess the minimal symbol
19151 Fortran mangling kind. */
19152 if (cu->language == language_fortran && die->parent
19153 && die->parent->tag == DW_TAG_module
19155 && startswith (cu->producer, "GNU Fortran"))
19156 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19158 /* A variable with DW_AT_external is never static,
19159 but it may be block-scoped. */
19160 list_to_add = (cu->list_in_scope == &file_symbols
19161 ? &global_symbols : cu->list_in_scope);
19164 list_to_add = cu->list_in_scope;
19168 /* We do not know the address of this symbol.
19169 If it is an external symbol and we have type information
19170 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19171 The address of the variable will then be determined from
19172 the minimal symbol table whenever the variable is
19174 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19176 /* Fortran explicitly imports any global symbols to the local
19177 scope by DW_TAG_common_block. */
19178 if (cu->language == language_fortran && die->parent
19179 && die->parent->tag == DW_TAG_common_block)
19181 /* SYMBOL_CLASS doesn't matter here because
19182 read_common_block is going to reset it. */
19184 list_to_add = cu->list_in_scope;
19186 else if (attr2 && (DW_UNSND (attr2) != 0)
19187 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19189 /* A variable with DW_AT_external is never static, but it
19190 may be block-scoped. */
19191 list_to_add = (cu->list_in_scope == &file_symbols
19192 ? &global_symbols : cu->list_in_scope);
19194 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19196 else if (!die_is_declaration (die, cu))
19198 /* Use the default LOC_OPTIMIZED_OUT class. */
19199 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19201 list_to_add = cu->list_in_scope;
19205 case DW_TAG_formal_parameter:
19206 /* If we are inside a function, mark this as an argument. If
19207 not, we might be looking at an argument to an inlined function
19208 when we do not have enough information to show inlined frames;
19209 pretend it's a local variable in that case so that the user can
19211 if (context_stack_depth > 0
19212 && context_stack[context_stack_depth - 1].name != NULL)
19213 SYMBOL_IS_ARGUMENT (sym) = 1;
19214 attr = dwarf2_attr (die, DW_AT_location, cu);
19217 var_decode_location (attr, sym, cu);
19219 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19222 dwarf2_const_value (attr, sym, cu);
19225 list_to_add = cu->list_in_scope;
19227 case DW_TAG_unspecified_parameters:
19228 /* From varargs functions; gdb doesn't seem to have any
19229 interest in this information, so just ignore it for now.
19232 case DW_TAG_template_type_param:
19234 /* Fall through. */
19235 case DW_TAG_class_type:
19236 case DW_TAG_interface_type:
19237 case DW_TAG_structure_type:
19238 case DW_TAG_union_type:
19239 case DW_TAG_set_type:
19240 case DW_TAG_enumeration_type:
19241 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19242 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19245 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19246 really ever be static objects: otherwise, if you try
19247 to, say, break of a class's method and you're in a file
19248 which doesn't mention that class, it won't work unless
19249 the check for all static symbols in lookup_symbol_aux
19250 saves you. See the OtherFileClass tests in
19251 gdb.c++/namespace.exp. */
19255 list_to_add = (cu->list_in_scope == &file_symbols
19256 && cu->language == language_cplus
19257 ? &global_symbols : cu->list_in_scope);
19259 /* The semantics of C++ state that "struct foo {
19260 ... }" also defines a typedef for "foo". */
19261 if (cu->language == language_cplus
19262 || cu->language == language_ada
19263 || cu->language == language_d
19264 || cu->language == language_rust)
19266 /* The symbol's name is already allocated along
19267 with this objfile, so we don't need to
19268 duplicate it for the type. */
19269 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19270 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19275 case DW_TAG_typedef:
19276 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19277 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19278 list_to_add = cu->list_in_scope;
19280 case DW_TAG_base_type:
19281 case DW_TAG_subrange_type:
19282 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19283 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19284 list_to_add = cu->list_in_scope;
19286 case DW_TAG_enumerator:
19287 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19290 dwarf2_const_value (attr, sym, cu);
19293 /* NOTE: carlton/2003-11-10: See comment above in the
19294 DW_TAG_class_type, etc. block. */
19296 list_to_add = (cu->list_in_scope == &file_symbols
19297 && cu->language == language_cplus
19298 ? &global_symbols : cu->list_in_scope);
19301 case DW_TAG_imported_declaration:
19302 case DW_TAG_namespace:
19303 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19304 list_to_add = &global_symbols;
19306 case DW_TAG_module:
19307 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19308 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19309 list_to_add = &global_symbols;
19311 case DW_TAG_common_block:
19312 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19313 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19314 add_symbol_to_list (sym, cu->list_in_scope);
19317 /* Not a tag we recognize. Hopefully we aren't processing
19318 trash data, but since we must specifically ignore things
19319 we don't recognize, there is nothing else we should do at
19321 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19322 dwarf_tag_name (die->tag));
19328 sym->hash_next = objfile->template_symbols;
19329 objfile->template_symbols = sym;
19330 list_to_add = NULL;
19333 if (list_to_add != NULL)
19334 add_symbol_to_list (sym, list_to_add);
19336 /* For the benefit of old versions of GCC, check for anonymous
19337 namespaces based on the demangled name. */
19338 if (!cu->processing_has_namespace_info
19339 && cu->language == language_cplus)
19340 cp_scan_for_anonymous_namespaces (sym, objfile);
19345 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19347 static struct symbol *
19348 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19350 return new_symbol_full (die, type, cu, NULL);
19353 /* Given an attr with a DW_FORM_dataN value in host byte order,
19354 zero-extend it as appropriate for the symbol's type. The DWARF
19355 standard (v4) is not entirely clear about the meaning of using
19356 DW_FORM_dataN for a constant with a signed type, where the type is
19357 wider than the data. The conclusion of a discussion on the DWARF
19358 list was that this is unspecified. We choose to always zero-extend
19359 because that is the interpretation long in use by GCC. */
19362 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19363 struct dwarf2_cu *cu, LONGEST *value, int bits)
19365 struct objfile *objfile = cu->objfile;
19366 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19367 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19368 LONGEST l = DW_UNSND (attr);
19370 if (bits < sizeof (*value) * 8)
19372 l &= ((LONGEST) 1 << bits) - 1;
19375 else if (bits == sizeof (*value) * 8)
19379 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19380 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19387 /* Read a constant value from an attribute. Either set *VALUE, or if
19388 the value does not fit in *VALUE, set *BYTES - either already
19389 allocated on the objfile obstack, or newly allocated on OBSTACK,
19390 or, set *BATON, if we translated the constant to a location
19394 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19395 const char *name, struct obstack *obstack,
19396 struct dwarf2_cu *cu,
19397 LONGEST *value, const gdb_byte **bytes,
19398 struct dwarf2_locexpr_baton **baton)
19400 struct objfile *objfile = cu->objfile;
19401 struct comp_unit_head *cu_header = &cu->header;
19402 struct dwarf_block *blk;
19403 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19404 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19410 switch (attr->form)
19413 case DW_FORM_GNU_addr_index:
19417 if (TYPE_LENGTH (type) != cu_header->addr_size)
19418 dwarf2_const_value_length_mismatch_complaint (name,
19419 cu_header->addr_size,
19420 TYPE_LENGTH (type));
19421 /* Symbols of this form are reasonably rare, so we just
19422 piggyback on the existing location code rather than writing
19423 a new implementation of symbol_computed_ops. */
19424 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19425 (*baton)->per_cu = cu->per_cu;
19426 gdb_assert ((*baton)->per_cu);
19428 (*baton)->size = 2 + cu_header->addr_size;
19429 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19430 (*baton)->data = data;
19432 data[0] = DW_OP_addr;
19433 store_unsigned_integer (&data[1], cu_header->addr_size,
19434 byte_order, DW_ADDR (attr));
19435 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19438 case DW_FORM_string:
19440 case DW_FORM_GNU_str_index:
19441 case DW_FORM_GNU_strp_alt:
19442 /* DW_STRING is already allocated on the objfile obstack, point
19444 *bytes = (const gdb_byte *) DW_STRING (attr);
19446 case DW_FORM_block1:
19447 case DW_FORM_block2:
19448 case DW_FORM_block4:
19449 case DW_FORM_block:
19450 case DW_FORM_exprloc:
19451 case DW_FORM_data16:
19452 blk = DW_BLOCK (attr);
19453 if (TYPE_LENGTH (type) != blk->size)
19454 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19455 TYPE_LENGTH (type));
19456 *bytes = blk->data;
19459 /* The DW_AT_const_value attributes are supposed to carry the
19460 symbol's value "represented as it would be on the target
19461 architecture." By the time we get here, it's already been
19462 converted to host endianness, so we just need to sign- or
19463 zero-extend it as appropriate. */
19464 case DW_FORM_data1:
19465 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19467 case DW_FORM_data2:
19468 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19470 case DW_FORM_data4:
19471 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19473 case DW_FORM_data8:
19474 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19477 case DW_FORM_sdata:
19478 *value = DW_SND (attr);
19481 case DW_FORM_udata:
19482 *value = DW_UNSND (attr);
19486 complaint (&symfile_complaints,
19487 _("unsupported const value attribute form: '%s'"),
19488 dwarf_form_name (attr->form));
19495 /* Copy constant value from an attribute to a symbol. */
19498 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19499 struct dwarf2_cu *cu)
19501 struct objfile *objfile = cu->objfile;
19503 const gdb_byte *bytes;
19504 struct dwarf2_locexpr_baton *baton;
19506 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19507 SYMBOL_PRINT_NAME (sym),
19508 &objfile->objfile_obstack, cu,
19509 &value, &bytes, &baton);
19513 SYMBOL_LOCATION_BATON (sym) = baton;
19514 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19516 else if (bytes != NULL)
19518 SYMBOL_VALUE_BYTES (sym) = bytes;
19519 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19523 SYMBOL_VALUE (sym) = value;
19524 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19528 /* Return the type of the die in question using its DW_AT_type attribute. */
19530 static struct type *
19531 die_type (struct die_info *die, struct dwarf2_cu *cu)
19533 struct attribute *type_attr;
19535 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19538 /* A missing DW_AT_type represents a void type. */
19539 return objfile_type (cu->objfile)->builtin_void;
19542 return lookup_die_type (die, type_attr, cu);
19545 /* True iff CU's producer generates GNAT Ada auxiliary information
19546 that allows to find parallel types through that information instead
19547 of having to do expensive parallel lookups by type name. */
19550 need_gnat_info (struct dwarf2_cu *cu)
19552 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19553 of GNAT produces this auxiliary information, without any indication
19554 that it is produced. Part of enhancing the FSF version of GNAT
19555 to produce that information will be to put in place an indicator
19556 that we can use in order to determine whether the descriptive type
19557 info is available or not. One suggestion that has been made is
19558 to use a new attribute, attached to the CU die. For now, assume
19559 that the descriptive type info is not available. */
19563 /* Return the auxiliary type of the die in question using its
19564 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19565 attribute is not present. */
19567 static struct type *
19568 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19570 struct attribute *type_attr;
19572 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19576 return lookup_die_type (die, type_attr, cu);
19579 /* If DIE has a descriptive_type attribute, then set the TYPE's
19580 descriptive type accordingly. */
19583 set_descriptive_type (struct type *type, struct die_info *die,
19584 struct dwarf2_cu *cu)
19586 struct type *descriptive_type = die_descriptive_type (die, cu);
19588 if (descriptive_type)
19590 ALLOCATE_GNAT_AUX_TYPE (type);
19591 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19595 /* Return the containing type of the die in question using its
19596 DW_AT_containing_type attribute. */
19598 static struct type *
19599 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19601 struct attribute *type_attr;
19603 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19605 error (_("Dwarf Error: Problem turning containing type into gdb type "
19606 "[in module %s]"), objfile_name (cu->objfile));
19608 return lookup_die_type (die, type_attr, cu);
19611 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19613 static struct type *
19614 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19616 struct objfile *objfile = dwarf2_per_objfile->objfile;
19617 char *message, *saved;
19619 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19620 objfile_name (objfile),
19621 to_underlying (cu->header.sect_off),
19622 to_underlying (die->sect_off));
19623 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19624 message, strlen (message));
19627 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19630 /* Look up the type of DIE in CU using its type attribute ATTR.
19631 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19632 DW_AT_containing_type.
19633 If there is no type substitute an error marker. */
19635 static struct type *
19636 lookup_die_type (struct die_info *die, const struct attribute *attr,
19637 struct dwarf2_cu *cu)
19639 struct objfile *objfile = cu->objfile;
19640 struct type *this_type;
19642 gdb_assert (attr->name == DW_AT_type
19643 || attr->name == DW_AT_GNAT_descriptive_type
19644 || attr->name == DW_AT_containing_type);
19646 /* First see if we have it cached. */
19648 if (attr->form == DW_FORM_GNU_ref_alt)
19650 struct dwarf2_per_cu_data *per_cu;
19651 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19653 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19654 this_type = get_die_type_at_offset (sect_off, per_cu);
19656 else if (attr_form_is_ref (attr))
19658 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19660 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
19662 else if (attr->form == DW_FORM_ref_sig8)
19664 ULONGEST signature = DW_SIGNATURE (attr);
19666 return get_signatured_type (die, signature, cu);
19670 complaint (&symfile_complaints,
19671 _("Dwarf Error: Bad type attribute %s in DIE"
19672 " at 0x%x [in module %s]"),
19673 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
19674 objfile_name (objfile));
19675 return build_error_marker_type (cu, die);
19678 /* If not cached we need to read it in. */
19680 if (this_type == NULL)
19682 struct die_info *type_die = NULL;
19683 struct dwarf2_cu *type_cu = cu;
19685 if (attr_form_is_ref (attr))
19686 type_die = follow_die_ref (die, attr, &type_cu);
19687 if (type_die == NULL)
19688 return build_error_marker_type (cu, die);
19689 /* If we find the type now, it's probably because the type came
19690 from an inter-CU reference and the type's CU got expanded before
19692 this_type = read_type_die (type_die, type_cu);
19695 /* If we still don't have a type use an error marker. */
19697 if (this_type == NULL)
19698 return build_error_marker_type (cu, die);
19703 /* Return the type in DIE, CU.
19704 Returns NULL for invalid types.
19706 This first does a lookup in die_type_hash,
19707 and only reads the die in if necessary.
19709 NOTE: This can be called when reading in partial or full symbols. */
19711 static struct type *
19712 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19714 struct type *this_type;
19716 this_type = get_die_type (die, cu);
19720 return read_type_die_1 (die, cu);
19723 /* Read the type in DIE, CU.
19724 Returns NULL for invalid types. */
19726 static struct type *
19727 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19729 struct type *this_type = NULL;
19733 case DW_TAG_class_type:
19734 case DW_TAG_interface_type:
19735 case DW_TAG_structure_type:
19736 case DW_TAG_union_type:
19737 this_type = read_structure_type (die, cu);
19739 case DW_TAG_enumeration_type:
19740 this_type = read_enumeration_type (die, cu);
19742 case DW_TAG_subprogram:
19743 case DW_TAG_subroutine_type:
19744 case DW_TAG_inlined_subroutine:
19745 this_type = read_subroutine_type (die, cu);
19747 case DW_TAG_array_type:
19748 this_type = read_array_type (die, cu);
19750 case DW_TAG_set_type:
19751 this_type = read_set_type (die, cu);
19753 case DW_TAG_pointer_type:
19754 this_type = read_tag_pointer_type (die, cu);
19756 case DW_TAG_ptr_to_member_type:
19757 this_type = read_tag_ptr_to_member_type (die, cu);
19759 case DW_TAG_reference_type:
19760 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19762 case DW_TAG_rvalue_reference_type:
19763 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19765 case DW_TAG_const_type:
19766 this_type = read_tag_const_type (die, cu);
19768 case DW_TAG_volatile_type:
19769 this_type = read_tag_volatile_type (die, cu);
19771 case DW_TAG_restrict_type:
19772 this_type = read_tag_restrict_type (die, cu);
19774 case DW_TAG_string_type:
19775 this_type = read_tag_string_type (die, cu);
19777 case DW_TAG_typedef:
19778 this_type = read_typedef (die, cu);
19780 case DW_TAG_subrange_type:
19781 this_type = read_subrange_type (die, cu);
19783 case DW_TAG_base_type:
19784 this_type = read_base_type (die, cu);
19786 case DW_TAG_unspecified_type:
19787 this_type = read_unspecified_type (die, cu);
19789 case DW_TAG_namespace:
19790 this_type = read_namespace_type (die, cu);
19792 case DW_TAG_module:
19793 this_type = read_module_type (die, cu);
19795 case DW_TAG_atomic_type:
19796 this_type = read_tag_atomic_type (die, cu);
19799 complaint (&symfile_complaints,
19800 _("unexpected tag in read_type_die: '%s'"),
19801 dwarf_tag_name (die->tag));
19808 /* See if we can figure out if the class lives in a namespace. We do
19809 this by looking for a member function; its demangled name will
19810 contain namespace info, if there is any.
19811 Return the computed name or NULL.
19812 Space for the result is allocated on the objfile's obstack.
19813 This is the full-die version of guess_partial_die_structure_name.
19814 In this case we know DIE has no useful parent. */
19817 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19819 struct die_info *spec_die;
19820 struct dwarf2_cu *spec_cu;
19821 struct die_info *child;
19824 spec_die = die_specification (die, &spec_cu);
19825 if (spec_die != NULL)
19831 for (child = die->child;
19833 child = child->sibling)
19835 if (child->tag == DW_TAG_subprogram)
19837 const char *linkage_name;
19839 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19840 if (linkage_name == NULL)
19841 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19843 if (linkage_name != NULL)
19846 = language_class_name_from_physname (cu->language_defn,
19850 if (actual_name != NULL)
19852 const char *die_name = dwarf2_name (die, cu);
19854 if (die_name != NULL
19855 && strcmp (die_name, actual_name) != 0)
19857 /* Strip off the class name from the full name.
19858 We want the prefix. */
19859 int die_name_len = strlen (die_name);
19860 int actual_name_len = strlen (actual_name);
19862 /* Test for '::' as a sanity check. */
19863 if (actual_name_len > die_name_len + 2
19864 && actual_name[actual_name_len
19865 - die_name_len - 1] == ':')
19866 name = (char *) obstack_copy0 (
19867 &cu->objfile->per_bfd->storage_obstack,
19868 actual_name, actual_name_len - die_name_len - 2);
19871 xfree (actual_name);
19880 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19881 prefix part in such case. See
19882 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19884 static const char *
19885 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19887 struct attribute *attr;
19890 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19891 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19894 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19897 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19899 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19900 if (attr == NULL || DW_STRING (attr) == NULL)
19903 /* dwarf2_name had to be already called. */
19904 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19906 /* Strip the base name, keep any leading namespaces/classes. */
19907 base = strrchr (DW_STRING (attr), ':');
19908 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19911 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19913 &base[-1] - DW_STRING (attr));
19916 /* Return the name of the namespace/class that DIE is defined within,
19917 or "" if we can't tell. The caller should not xfree the result.
19919 For example, if we're within the method foo() in the following
19929 then determine_prefix on foo's die will return "N::C". */
19931 static const char *
19932 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19934 struct die_info *parent, *spec_die;
19935 struct dwarf2_cu *spec_cu;
19936 struct type *parent_type;
19937 const char *retval;
19939 if (cu->language != language_cplus
19940 && cu->language != language_fortran && cu->language != language_d
19941 && cu->language != language_rust)
19944 retval = anonymous_struct_prefix (die, cu);
19948 /* We have to be careful in the presence of DW_AT_specification.
19949 For example, with GCC 3.4, given the code
19953 // Definition of N::foo.
19957 then we'll have a tree of DIEs like this:
19959 1: DW_TAG_compile_unit
19960 2: DW_TAG_namespace // N
19961 3: DW_TAG_subprogram // declaration of N::foo
19962 4: DW_TAG_subprogram // definition of N::foo
19963 DW_AT_specification // refers to die #3
19965 Thus, when processing die #4, we have to pretend that we're in
19966 the context of its DW_AT_specification, namely the contex of die
19969 spec_die = die_specification (die, &spec_cu);
19970 if (spec_die == NULL)
19971 parent = die->parent;
19974 parent = spec_die->parent;
19978 if (parent == NULL)
19980 else if (parent->building_fullname)
19983 const char *parent_name;
19985 /* It has been seen on RealView 2.2 built binaries,
19986 DW_TAG_template_type_param types actually _defined_ as
19987 children of the parent class:
19990 template class <class Enum> Class{};
19991 Class<enum E> class_e;
19993 1: DW_TAG_class_type (Class)
19994 2: DW_TAG_enumeration_type (E)
19995 3: DW_TAG_enumerator (enum1:0)
19996 3: DW_TAG_enumerator (enum2:1)
19998 2: DW_TAG_template_type_param
19999 DW_AT_type DW_FORM_ref_udata (E)
20001 Besides being broken debug info, it can put GDB into an
20002 infinite loop. Consider:
20004 When we're building the full name for Class<E>, we'll start
20005 at Class, and go look over its template type parameters,
20006 finding E. We'll then try to build the full name of E, and
20007 reach here. We're now trying to build the full name of E,
20008 and look over the parent DIE for containing scope. In the
20009 broken case, if we followed the parent DIE of E, we'd again
20010 find Class, and once again go look at its template type
20011 arguments, etc., etc. Simply don't consider such parent die
20012 as source-level parent of this die (it can't be, the language
20013 doesn't allow it), and break the loop here. */
20014 name = dwarf2_name (die, cu);
20015 parent_name = dwarf2_name (parent, cu);
20016 complaint (&symfile_complaints,
20017 _("template param type '%s' defined within parent '%s'"),
20018 name ? name : "<unknown>",
20019 parent_name ? parent_name : "<unknown>");
20023 switch (parent->tag)
20025 case DW_TAG_namespace:
20026 parent_type = read_type_die (parent, cu);
20027 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20028 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20029 Work around this problem here. */
20030 if (cu->language == language_cplus
20031 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20033 /* We give a name to even anonymous namespaces. */
20034 return TYPE_TAG_NAME (parent_type);
20035 case DW_TAG_class_type:
20036 case DW_TAG_interface_type:
20037 case DW_TAG_structure_type:
20038 case DW_TAG_union_type:
20039 case DW_TAG_module:
20040 parent_type = read_type_die (parent, cu);
20041 if (TYPE_TAG_NAME (parent_type) != NULL)
20042 return TYPE_TAG_NAME (parent_type);
20044 /* An anonymous structure is only allowed non-static data
20045 members; no typedefs, no member functions, et cetera.
20046 So it does not need a prefix. */
20048 case DW_TAG_compile_unit:
20049 case DW_TAG_partial_unit:
20050 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20051 if (cu->language == language_cplus
20052 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
20053 && die->child != NULL
20054 && (die->tag == DW_TAG_class_type
20055 || die->tag == DW_TAG_structure_type
20056 || die->tag == DW_TAG_union_type))
20058 char *name = guess_full_die_structure_name (die, cu);
20063 case DW_TAG_enumeration_type:
20064 parent_type = read_type_die (parent, cu);
20065 if (TYPE_DECLARED_CLASS (parent_type))
20067 if (TYPE_TAG_NAME (parent_type) != NULL)
20068 return TYPE_TAG_NAME (parent_type);
20071 /* Fall through. */
20073 return determine_prefix (parent, cu);
20077 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20078 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20079 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20080 an obconcat, otherwise allocate storage for the result. The CU argument is
20081 used to determine the language and hence, the appropriate separator. */
20083 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20086 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20087 int physname, struct dwarf2_cu *cu)
20089 const char *lead = "";
20092 if (suffix == NULL || suffix[0] == '\0'
20093 || prefix == NULL || prefix[0] == '\0')
20095 else if (cu->language == language_d)
20097 /* For D, the 'main' function could be defined in any module, but it
20098 should never be prefixed. */
20099 if (strcmp (suffix, "D main") == 0)
20107 else if (cu->language == language_fortran && physname)
20109 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20110 DW_AT_MIPS_linkage_name is preferred and used instead. */
20118 if (prefix == NULL)
20120 if (suffix == NULL)
20127 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20129 strcpy (retval, lead);
20130 strcat (retval, prefix);
20131 strcat (retval, sep);
20132 strcat (retval, suffix);
20137 /* We have an obstack. */
20138 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20142 /* Return sibling of die, NULL if no sibling. */
20144 static struct die_info *
20145 sibling_die (struct die_info *die)
20147 return die->sibling;
20150 /* Get name of a die, return NULL if not found. */
20152 static const char *
20153 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20154 struct obstack *obstack)
20156 if (name && cu->language == language_cplus)
20158 std::string canon_name = cp_canonicalize_string (name);
20160 if (!canon_name.empty ())
20162 if (canon_name != name)
20163 name = (const char *) obstack_copy0 (obstack,
20164 canon_name.c_str (),
20165 canon_name.length ());
20172 /* Get name of a die, return NULL if not found.
20173 Anonymous namespaces are converted to their magic string. */
20175 static const char *
20176 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20178 struct attribute *attr;
20180 attr = dwarf2_attr (die, DW_AT_name, cu);
20181 if ((!attr || !DW_STRING (attr))
20182 && die->tag != DW_TAG_namespace
20183 && die->tag != DW_TAG_class_type
20184 && die->tag != DW_TAG_interface_type
20185 && die->tag != DW_TAG_structure_type
20186 && die->tag != DW_TAG_union_type)
20191 case DW_TAG_compile_unit:
20192 case DW_TAG_partial_unit:
20193 /* Compilation units have a DW_AT_name that is a filename, not
20194 a source language identifier. */
20195 case DW_TAG_enumeration_type:
20196 case DW_TAG_enumerator:
20197 /* These tags always have simple identifiers already; no need
20198 to canonicalize them. */
20199 return DW_STRING (attr);
20201 case DW_TAG_namespace:
20202 if (attr != NULL && DW_STRING (attr) != NULL)
20203 return DW_STRING (attr);
20204 return CP_ANONYMOUS_NAMESPACE_STR;
20206 case DW_TAG_class_type:
20207 case DW_TAG_interface_type:
20208 case DW_TAG_structure_type:
20209 case DW_TAG_union_type:
20210 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20211 structures or unions. These were of the form "._%d" in GCC 4.1,
20212 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20213 and GCC 4.4. We work around this problem by ignoring these. */
20214 if (attr && DW_STRING (attr)
20215 && (startswith (DW_STRING (attr), "._")
20216 || startswith (DW_STRING (attr), "<anonymous")))
20219 /* GCC might emit a nameless typedef that has a linkage name. See
20220 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20221 if (!attr || DW_STRING (attr) == NULL)
20223 char *demangled = NULL;
20225 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20227 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20229 if (attr == NULL || DW_STRING (attr) == NULL)
20232 /* Avoid demangling DW_STRING (attr) the second time on a second
20233 call for the same DIE. */
20234 if (!DW_STRING_IS_CANONICAL (attr))
20235 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20241 /* FIXME: we already did this for the partial symbol... */
20244 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20245 demangled, strlen (demangled)));
20246 DW_STRING_IS_CANONICAL (attr) = 1;
20249 /* Strip any leading namespaces/classes, keep only the base name.
20250 DW_AT_name for named DIEs does not contain the prefixes. */
20251 base = strrchr (DW_STRING (attr), ':');
20252 if (base && base > DW_STRING (attr) && base[-1] == ':')
20255 return DW_STRING (attr);
20264 if (!DW_STRING_IS_CANONICAL (attr))
20267 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20268 &cu->objfile->per_bfd->storage_obstack);
20269 DW_STRING_IS_CANONICAL (attr) = 1;
20271 return DW_STRING (attr);
20274 /* Return the die that this die in an extension of, or NULL if there
20275 is none. *EXT_CU is the CU containing DIE on input, and the CU
20276 containing the return value on output. */
20278 static struct die_info *
20279 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20281 struct attribute *attr;
20283 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20287 return follow_die_ref (die, attr, ext_cu);
20290 /* Convert a DIE tag into its string name. */
20292 static const char *
20293 dwarf_tag_name (unsigned tag)
20295 const char *name = get_DW_TAG_name (tag);
20298 return "DW_TAG_<unknown>";
20303 /* Convert a DWARF attribute code into its string name. */
20305 static const char *
20306 dwarf_attr_name (unsigned attr)
20310 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20311 if (attr == DW_AT_MIPS_fde)
20312 return "DW_AT_MIPS_fde";
20314 if (attr == DW_AT_HP_block_index)
20315 return "DW_AT_HP_block_index";
20318 name = get_DW_AT_name (attr);
20321 return "DW_AT_<unknown>";
20326 /* Convert a DWARF value form code into its string name. */
20328 static const char *
20329 dwarf_form_name (unsigned form)
20331 const char *name = get_DW_FORM_name (form);
20334 return "DW_FORM_<unknown>";
20339 static const char *
20340 dwarf_bool_name (unsigned mybool)
20348 /* Convert a DWARF type code into its string name. */
20350 static const char *
20351 dwarf_type_encoding_name (unsigned enc)
20353 const char *name = get_DW_ATE_name (enc);
20356 return "DW_ATE_<unknown>";
20362 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20366 print_spaces (indent, f);
20367 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20368 dwarf_tag_name (die->tag), die->abbrev,
20369 to_underlying (die->sect_off));
20371 if (die->parent != NULL)
20373 print_spaces (indent, f);
20374 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20375 to_underlying (die->parent->sect_off));
20378 print_spaces (indent, f);
20379 fprintf_unfiltered (f, " has children: %s\n",
20380 dwarf_bool_name (die->child != NULL));
20382 print_spaces (indent, f);
20383 fprintf_unfiltered (f, " attributes:\n");
20385 for (i = 0; i < die->num_attrs; ++i)
20387 print_spaces (indent, f);
20388 fprintf_unfiltered (f, " %s (%s) ",
20389 dwarf_attr_name (die->attrs[i].name),
20390 dwarf_form_name (die->attrs[i].form));
20392 switch (die->attrs[i].form)
20395 case DW_FORM_GNU_addr_index:
20396 fprintf_unfiltered (f, "address: ");
20397 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20399 case DW_FORM_block2:
20400 case DW_FORM_block4:
20401 case DW_FORM_block:
20402 case DW_FORM_block1:
20403 fprintf_unfiltered (f, "block: size %s",
20404 pulongest (DW_BLOCK (&die->attrs[i])->size));
20406 case DW_FORM_exprloc:
20407 fprintf_unfiltered (f, "expression: size %s",
20408 pulongest (DW_BLOCK (&die->attrs[i])->size));
20410 case DW_FORM_data16:
20411 fprintf_unfiltered (f, "constant of 16 bytes");
20413 case DW_FORM_ref_addr:
20414 fprintf_unfiltered (f, "ref address: ");
20415 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20417 case DW_FORM_GNU_ref_alt:
20418 fprintf_unfiltered (f, "alt ref address: ");
20419 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20425 case DW_FORM_ref_udata:
20426 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20427 (long) (DW_UNSND (&die->attrs[i])));
20429 case DW_FORM_data1:
20430 case DW_FORM_data2:
20431 case DW_FORM_data4:
20432 case DW_FORM_data8:
20433 case DW_FORM_udata:
20434 case DW_FORM_sdata:
20435 fprintf_unfiltered (f, "constant: %s",
20436 pulongest (DW_UNSND (&die->attrs[i])));
20438 case DW_FORM_sec_offset:
20439 fprintf_unfiltered (f, "section offset: %s",
20440 pulongest (DW_UNSND (&die->attrs[i])));
20442 case DW_FORM_ref_sig8:
20443 fprintf_unfiltered (f, "signature: %s",
20444 hex_string (DW_SIGNATURE (&die->attrs[i])));
20446 case DW_FORM_string:
20448 case DW_FORM_line_strp:
20449 case DW_FORM_GNU_str_index:
20450 case DW_FORM_GNU_strp_alt:
20451 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20452 DW_STRING (&die->attrs[i])
20453 ? DW_STRING (&die->attrs[i]) : "",
20454 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20457 if (DW_UNSND (&die->attrs[i]))
20458 fprintf_unfiltered (f, "flag: TRUE");
20460 fprintf_unfiltered (f, "flag: FALSE");
20462 case DW_FORM_flag_present:
20463 fprintf_unfiltered (f, "flag: TRUE");
20465 case DW_FORM_indirect:
20466 /* The reader will have reduced the indirect form to
20467 the "base form" so this form should not occur. */
20468 fprintf_unfiltered (f,
20469 "unexpected attribute form: DW_FORM_indirect");
20472 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20473 die->attrs[i].form);
20476 fprintf_unfiltered (f, "\n");
20481 dump_die_for_error (struct die_info *die)
20483 dump_die_shallow (gdb_stderr, 0, die);
20487 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20489 int indent = level * 4;
20491 gdb_assert (die != NULL);
20493 if (level >= max_level)
20496 dump_die_shallow (f, indent, die);
20498 if (die->child != NULL)
20500 print_spaces (indent, f);
20501 fprintf_unfiltered (f, " Children:");
20502 if (level + 1 < max_level)
20504 fprintf_unfiltered (f, "\n");
20505 dump_die_1 (f, level + 1, max_level, die->child);
20509 fprintf_unfiltered (f,
20510 " [not printed, max nesting level reached]\n");
20514 if (die->sibling != NULL && level > 0)
20516 dump_die_1 (f, level, max_level, die->sibling);
20520 /* This is called from the pdie macro in gdbinit.in.
20521 It's not static so gcc will keep a copy callable from gdb. */
20524 dump_die (struct die_info *die, int max_level)
20526 dump_die_1 (gdb_stdlog, 0, max_level, die);
20530 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20534 slot = htab_find_slot_with_hash (cu->die_hash, die,
20535 to_underlying (die->sect_off),
20541 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20545 dwarf2_get_ref_die_offset (const struct attribute *attr)
20547 if (attr_form_is_ref (attr))
20548 return (sect_offset) DW_UNSND (attr);
20550 complaint (&symfile_complaints,
20551 _("unsupported die ref attribute form: '%s'"),
20552 dwarf_form_name (attr->form));
20556 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20557 * the value held by the attribute is not constant. */
20560 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20562 if (attr->form == DW_FORM_sdata)
20563 return DW_SND (attr);
20564 else if (attr->form == DW_FORM_udata
20565 || attr->form == DW_FORM_data1
20566 || attr->form == DW_FORM_data2
20567 || attr->form == DW_FORM_data4
20568 || attr->form == DW_FORM_data8)
20569 return DW_UNSND (attr);
20572 /* For DW_FORM_data16 see attr_form_is_constant. */
20573 complaint (&symfile_complaints,
20574 _("Attribute value is not a constant (%s)"),
20575 dwarf_form_name (attr->form));
20576 return default_value;
20580 /* Follow reference or signature attribute ATTR of SRC_DIE.
20581 On entry *REF_CU is the CU of SRC_DIE.
20582 On exit *REF_CU is the CU of the result. */
20584 static struct die_info *
20585 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20586 struct dwarf2_cu **ref_cu)
20588 struct die_info *die;
20590 if (attr_form_is_ref (attr))
20591 die = follow_die_ref (src_die, attr, ref_cu);
20592 else if (attr->form == DW_FORM_ref_sig8)
20593 die = follow_die_sig (src_die, attr, ref_cu);
20596 dump_die_for_error (src_die);
20597 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20598 objfile_name ((*ref_cu)->objfile));
20604 /* Follow reference OFFSET.
20605 On entry *REF_CU is the CU of the source die referencing OFFSET.
20606 On exit *REF_CU is the CU of the result.
20607 Returns NULL if OFFSET is invalid. */
20609 static struct die_info *
20610 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20611 struct dwarf2_cu **ref_cu)
20613 struct die_info temp_die;
20614 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20616 gdb_assert (cu->per_cu != NULL);
20620 if (cu->per_cu->is_debug_types)
20622 /* .debug_types CUs cannot reference anything outside their CU.
20623 If they need to, they have to reference a signatured type via
20624 DW_FORM_ref_sig8. */
20625 if (!offset_in_cu_p (&cu->header, sect_off))
20628 else if (offset_in_dwz != cu->per_cu->is_dwz
20629 || !offset_in_cu_p (&cu->header, sect_off))
20631 struct dwarf2_per_cu_data *per_cu;
20633 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20636 /* If necessary, add it to the queue and load its DIEs. */
20637 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20638 load_full_comp_unit (per_cu, cu->language);
20640 target_cu = per_cu->cu;
20642 else if (cu->dies == NULL)
20644 /* We're loading full DIEs during partial symbol reading. */
20645 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20646 load_full_comp_unit (cu->per_cu, language_minimal);
20649 *ref_cu = target_cu;
20650 temp_die.sect_off = sect_off;
20651 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20653 to_underlying (sect_off));
20656 /* Follow reference attribute ATTR of SRC_DIE.
20657 On entry *REF_CU is the CU of SRC_DIE.
20658 On exit *REF_CU is the CU of the result. */
20660 static struct die_info *
20661 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20662 struct dwarf2_cu **ref_cu)
20664 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20665 struct dwarf2_cu *cu = *ref_cu;
20666 struct die_info *die;
20668 die = follow_die_offset (sect_off,
20669 (attr->form == DW_FORM_GNU_ref_alt
20670 || cu->per_cu->is_dwz),
20673 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20674 "at 0x%x [in module %s]"),
20675 to_underlying (sect_off), to_underlying (src_die->sect_off),
20676 objfile_name (cu->objfile));
20681 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
20682 Returned value is intended for DW_OP_call*. Returned
20683 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20685 struct dwarf2_locexpr_baton
20686 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20687 struct dwarf2_per_cu_data *per_cu,
20688 CORE_ADDR (*get_frame_pc) (void *baton),
20691 struct dwarf2_cu *cu;
20692 struct die_info *die;
20693 struct attribute *attr;
20694 struct dwarf2_locexpr_baton retval;
20696 dw2_setup (per_cu->objfile);
20698 if (per_cu->cu == NULL)
20703 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20704 Instead just throw an error, not much else we can do. */
20705 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20706 to_underlying (sect_off), objfile_name (per_cu->objfile));
20709 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20711 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20712 to_underlying (sect_off), objfile_name (per_cu->objfile));
20714 attr = dwarf2_attr (die, DW_AT_location, cu);
20717 /* DWARF: "If there is no such attribute, then there is no effect.".
20718 DATA is ignored if SIZE is 0. */
20720 retval.data = NULL;
20723 else if (attr_form_is_section_offset (attr))
20725 struct dwarf2_loclist_baton loclist_baton;
20726 CORE_ADDR pc = (*get_frame_pc) (baton);
20729 fill_in_loclist_baton (cu, &loclist_baton, attr);
20731 retval.data = dwarf2_find_location_expression (&loclist_baton,
20733 retval.size = size;
20737 if (!attr_form_is_block (attr))
20738 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20739 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20740 to_underlying (sect_off), objfile_name (per_cu->objfile));
20742 retval.data = DW_BLOCK (attr)->data;
20743 retval.size = DW_BLOCK (attr)->size;
20745 retval.per_cu = cu->per_cu;
20747 age_cached_comp_units ();
20752 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20755 struct dwarf2_locexpr_baton
20756 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20757 struct dwarf2_per_cu_data *per_cu,
20758 CORE_ADDR (*get_frame_pc) (void *baton),
20761 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20763 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
20766 /* Write a constant of a given type as target-ordered bytes into
20769 static const gdb_byte *
20770 write_constant_as_bytes (struct obstack *obstack,
20771 enum bfd_endian byte_order,
20778 *len = TYPE_LENGTH (type);
20779 result = (gdb_byte *) obstack_alloc (obstack, *len);
20780 store_unsigned_integer (result, *len, byte_order, value);
20785 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20786 pointer to the constant bytes and set LEN to the length of the
20787 data. If memory is needed, allocate it on OBSTACK. If the DIE
20788 does not have a DW_AT_const_value, return NULL. */
20791 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20792 struct dwarf2_per_cu_data *per_cu,
20793 struct obstack *obstack,
20796 struct dwarf2_cu *cu;
20797 struct die_info *die;
20798 struct attribute *attr;
20799 const gdb_byte *result = NULL;
20802 enum bfd_endian byte_order;
20804 dw2_setup (per_cu->objfile);
20806 if (per_cu->cu == NULL)
20811 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20812 Instead just throw an error, not much else we can do. */
20813 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20814 to_underlying (sect_off), objfile_name (per_cu->objfile));
20817 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20819 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20820 to_underlying (sect_off), objfile_name (per_cu->objfile));
20823 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20827 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20828 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20830 switch (attr->form)
20833 case DW_FORM_GNU_addr_index:
20837 *len = cu->header.addr_size;
20838 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20839 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20843 case DW_FORM_string:
20845 case DW_FORM_GNU_str_index:
20846 case DW_FORM_GNU_strp_alt:
20847 /* DW_STRING is already allocated on the objfile obstack, point
20849 result = (const gdb_byte *) DW_STRING (attr);
20850 *len = strlen (DW_STRING (attr));
20852 case DW_FORM_block1:
20853 case DW_FORM_block2:
20854 case DW_FORM_block4:
20855 case DW_FORM_block:
20856 case DW_FORM_exprloc:
20857 case DW_FORM_data16:
20858 result = DW_BLOCK (attr)->data;
20859 *len = DW_BLOCK (attr)->size;
20862 /* The DW_AT_const_value attributes are supposed to carry the
20863 symbol's value "represented as it would be on the target
20864 architecture." By the time we get here, it's already been
20865 converted to host endianness, so we just need to sign- or
20866 zero-extend it as appropriate. */
20867 case DW_FORM_data1:
20868 type = die_type (die, cu);
20869 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20870 if (result == NULL)
20871 result = write_constant_as_bytes (obstack, byte_order,
20874 case DW_FORM_data2:
20875 type = die_type (die, cu);
20876 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20877 if (result == NULL)
20878 result = write_constant_as_bytes (obstack, byte_order,
20881 case DW_FORM_data4:
20882 type = die_type (die, cu);
20883 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20884 if (result == NULL)
20885 result = write_constant_as_bytes (obstack, byte_order,
20888 case DW_FORM_data8:
20889 type = die_type (die, cu);
20890 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20891 if (result == NULL)
20892 result = write_constant_as_bytes (obstack, byte_order,
20896 case DW_FORM_sdata:
20897 type = die_type (die, cu);
20898 result = write_constant_as_bytes (obstack, byte_order,
20899 type, DW_SND (attr), len);
20902 case DW_FORM_udata:
20903 type = die_type (die, cu);
20904 result = write_constant_as_bytes (obstack, byte_order,
20905 type, DW_UNSND (attr), len);
20909 complaint (&symfile_complaints,
20910 _("unsupported const value attribute form: '%s'"),
20911 dwarf_form_name (attr->form));
20918 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20919 valid type for this die is found. */
20922 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20923 struct dwarf2_per_cu_data *per_cu)
20925 struct dwarf2_cu *cu;
20926 struct die_info *die;
20928 dw2_setup (per_cu->objfile);
20930 if (per_cu->cu == NULL)
20936 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20940 return die_type (die, cu);
20943 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20947 dwarf2_get_die_type (cu_offset die_offset,
20948 struct dwarf2_per_cu_data *per_cu)
20950 dw2_setup (per_cu->objfile);
20952 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20953 return get_die_type_at_offset (die_offset_sect, per_cu);
20956 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20957 On entry *REF_CU is the CU of SRC_DIE.
20958 On exit *REF_CU is the CU of the result.
20959 Returns NULL if the referenced DIE isn't found. */
20961 static struct die_info *
20962 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20963 struct dwarf2_cu **ref_cu)
20965 struct die_info temp_die;
20966 struct dwarf2_cu *sig_cu;
20967 struct die_info *die;
20969 /* While it might be nice to assert sig_type->type == NULL here,
20970 we can get here for DW_AT_imported_declaration where we need
20971 the DIE not the type. */
20973 /* If necessary, add it to the queue and load its DIEs. */
20975 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20976 read_signatured_type (sig_type);
20978 sig_cu = sig_type->per_cu.cu;
20979 gdb_assert (sig_cu != NULL);
20980 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20981 temp_die.sect_off = sig_type->type_offset_in_section;
20982 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20983 to_underlying (temp_die.sect_off));
20986 /* For .gdb_index version 7 keep track of included TUs.
20987 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20988 if (dwarf2_per_objfile->index_table != NULL
20989 && dwarf2_per_objfile->index_table->version <= 7)
20991 VEC_safe_push (dwarf2_per_cu_ptr,
20992 (*ref_cu)->per_cu->imported_symtabs,
21003 /* Follow signatured type referenced by ATTR in SRC_DIE.
21004 On entry *REF_CU is the CU of SRC_DIE.
21005 On exit *REF_CU is the CU of the result.
21006 The result is the DIE of the type.
21007 If the referenced type cannot be found an error is thrown. */
21009 static struct die_info *
21010 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21011 struct dwarf2_cu **ref_cu)
21013 ULONGEST signature = DW_SIGNATURE (attr);
21014 struct signatured_type *sig_type;
21015 struct die_info *die;
21017 gdb_assert (attr->form == DW_FORM_ref_sig8);
21019 sig_type = lookup_signatured_type (*ref_cu, signature);
21020 /* sig_type will be NULL if the signatured type is missing from
21022 if (sig_type == NULL)
21024 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21025 " from DIE at 0x%x [in module %s]"),
21026 hex_string (signature), to_underlying (src_die->sect_off),
21027 objfile_name ((*ref_cu)->objfile));
21030 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21033 dump_die_for_error (src_die);
21034 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21035 " from DIE at 0x%x [in module %s]"),
21036 hex_string (signature), to_underlying (src_die->sect_off),
21037 objfile_name ((*ref_cu)->objfile));
21043 /* Get the type specified by SIGNATURE referenced in DIE/CU,
21044 reading in and processing the type unit if necessary. */
21046 static struct type *
21047 get_signatured_type (struct die_info *die, ULONGEST signature,
21048 struct dwarf2_cu *cu)
21050 struct signatured_type *sig_type;
21051 struct dwarf2_cu *type_cu;
21052 struct die_info *type_die;
21055 sig_type = lookup_signatured_type (cu, signature);
21056 /* sig_type will be NULL if the signatured type is missing from
21058 if (sig_type == NULL)
21060 complaint (&symfile_complaints,
21061 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21062 " from DIE at 0x%x [in module %s]"),
21063 hex_string (signature), to_underlying (die->sect_off),
21064 objfile_name (dwarf2_per_objfile->objfile));
21065 return build_error_marker_type (cu, die);
21068 /* If we already know the type we're done. */
21069 if (sig_type->type != NULL)
21070 return sig_type->type;
21073 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21074 if (type_die != NULL)
21076 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21077 is created. This is important, for example, because for c++ classes
21078 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21079 type = read_type_die (type_die, type_cu);
21082 complaint (&symfile_complaints,
21083 _("Dwarf Error: Cannot build signatured type %s"
21084 " referenced from DIE at 0x%x [in module %s]"),
21085 hex_string (signature), to_underlying (die->sect_off),
21086 objfile_name (dwarf2_per_objfile->objfile));
21087 type = build_error_marker_type (cu, die);
21092 complaint (&symfile_complaints,
21093 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21094 " from DIE at 0x%x [in module %s]"),
21095 hex_string (signature), to_underlying (die->sect_off),
21096 objfile_name (dwarf2_per_objfile->objfile));
21097 type = build_error_marker_type (cu, die);
21099 sig_type->type = type;
21104 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21105 reading in and processing the type unit if necessary. */
21107 static struct type *
21108 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21109 struct dwarf2_cu *cu) /* ARI: editCase function */
21111 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21112 if (attr_form_is_ref (attr))
21114 struct dwarf2_cu *type_cu = cu;
21115 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21117 return read_type_die (type_die, type_cu);
21119 else if (attr->form == DW_FORM_ref_sig8)
21121 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21125 complaint (&symfile_complaints,
21126 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21127 " at 0x%x [in module %s]"),
21128 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21129 objfile_name (dwarf2_per_objfile->objfile));
21130 return build_error_marker_type (cu, die);
21134 /* Load the DIEs associated with type unit PER_CU into memory. */
21137 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21139 struct signatured_type *sig_type;
21141 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21142 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21144 /* We have the per_cu, but we need the signatured_type.
21145 Fortunately this is an easy translation. */
21146 gdb_assert (per_cu->is_debug_types);
21147 sig_type = (struct signatured_type *) per_cu;
21149 gdb_assert (per_cu->cu == NULL);
21151 read_signatured_type (sig_type);
21153 gdb_assert (per_cu->cu != NULL);
21156 /* die_reader_func for read_signatured_type.
21157 This is identical to load_full_comp_unit_reader,
21158 but is kept separate for now. */
21161 read_signatured_type_reader (const struct die_reader_specs *reader,
21162 const gdb_byte *info_ptr,
21163 struct die_info *comp_unit_die,
21167 struct dwarf2_cu *cu = reader->cu;
21169 gdb_assert (cu->die_hash == NULL);
21171 htab_create_alloc_ex (cu->header.length / 12,
21175 &cu->comp_unit_obstack,
21176 hashtab_obstack_allocate,
21177 dummy_obstack_deallocate);
21180 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21181 &info_ptr, comp_unit_die);
21182 cu->dies = comp_unit_die;
21183 /* comp_unit_die is not stored in die_hash, no need. */
21185 /* We try not to read any attributes in this function, because not
21186 all CUs needed for references have been loaded yet, and symbol
21187 table processing isn't initialized. But we have to set the CU language,
21188 or we won't be able to build types correctly.
21189 Similarly, if we do not read the producer, we can not apply
21190 producer-specific interpretation. */
21191 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21194 /* Read in a signatured type and build its CU and DIEs.
21195 If the type is a stub for the real type in a DWO file,
21196 read in the real type from the DWO file as well. */
21199 read_signatured_type (struct signatured_type *sig_type)
21201 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21203 gdb_assert (per_cu->is_debug_types);
21204 gdb_assert (per_cu->cu == NULL);
21206 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21207 read_signatured_type_reader, NULL);
21208 sig_type->per_cu.tu_read = 1;
21211 /* Decode simple location descriptions.
21212 Given a pointer to a dwarf block that defines a location, compute
21213 the location and return the value.
21215 NOTE drow/2003-11-18: This function is called in two situations
21216 now: for the address of static or global variables (partial symbols
21217 only) and for offsets into structures which are expected to be
21218 (more or less) constant. The partial symbol case should go away,
21219 and only the constant case should remain. That will let this
21220 function complain more accurately. A few special modes are allowed
21221 without complaint for global variables (for instance, global
21222 register values and thread-local values).
21224 A location description containing no operations indicates that the
21225 object is optimized out. The return value is 0 for that case.
21226 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21227 callers will only want a very basic result and this can become a
21230 Note that stack[0] is unused except as a default error return. */
21233 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21235 struct objfile *objfile = cu->objfile;
21237 size_t size = blk->size;
21238 const gdb_byte *data = blk->data;
21239 CORE_ADDR stack[64];
21241 unsigned int bytes_read, unsnd;
21247 stack[++stacki] = 0;
21286 stack[++stacki] = op - DW_OP_lit0;
21321 stack[++stacki] = op - DW_OP_reg0;
21323 dwarf2_complex_location_expr_complaint ();
21327 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21329 stack[++stacki] = unsnd;
21331 dwarf2_complex_location_expr_complaint ();
21335 stack[++stacki] = read_address (objfile->obfd, &data[i],
21340 case DW_OP_const1u:
21341 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21345 case DW_OP_const1s:
21346 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21350 case DW_OP_const2u:
21351 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21355 case DW_OP_const2s:
21356 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21360 case DW_OP_const4u:
21361 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21365 case DW_OP_const4s:
21366 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21370 case DW_OP_const8u:
21371 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21376 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21382 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21387 stack[stacki + 1] = stack[stacki];
21392 stack[stacki - 1] += stack[stacki];
21396 case DW_OP_plus_uconst:
21397 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21403 stack[stacki - 1] -= stack[stacki];
21408 /* If we're not the last op, then we definitely can't encode
21409 this using GDB's address_class enum. This is valid for partial
21410 global symbols, although the variable's address will be bogus
21413 dwarf2_complex_location_expr_complaint ();
21416 case DW_OP_GNU_push_tls_address:
21417 case DW_OP_form_tls_address:
21418 /* The top of the stack has the offset from the beginning
21419 of the thread control block at which the variable is located. */
21420 /* Nothing should follow this operator, so the top of stack would
21422 /* This is valid for partial global symbols, but the variable's
21423 address will be bogus in the psymtab. Make it always at least
21424 non-zero to not look as a variable garbage collected by linker
21425 which have DW_OP_addr 0. */
21427 dwarf2_complex_location_expr_complaint ();
21431 case DW_OP_GNU_uninit:
21434 case DW_OP_GNU_addr_index:
21435 case DW_OP_GNU_const_index:
21436 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21443 const char *name = get_DW_OP_name (op);
21446 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21449 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21453 return (stack[stacki]);
21456 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21457 outside of the allocated space. Also enforce minimum>0. */
21458 if (stacki >= ARRAY_SIZE (stack) - 1)
21460 complaint (&symfile_complaints,
21461 _("location description stack overflow"));
21467 complaint (&symfile_complaints,
21468 _("location description stack underflow"));
21472 return (stack[stacki]);
21475 /* memory allocation interface */
21477 static struct dwarf_block *
21478 dwarf_alloc_block (struct dwarf2_cu *cu)
21480 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21483 static struct die_info *
21484 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21486 struct die_info *die;
21487 size_t size = sizeof (struct die_info);
21490 size += (num_attrs - 1) * sizeof (struct attribute);
21492 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21493 memset (die, 0, sizeof (struct die_info));
21498 /* Macro support. */
21500 /* Return file name relative to the compilation directory of file number I in
21501 *LH's file name table. The result is allocated using xmalloc; the caller is
21502 responsible for freeing it. */
21505 file_file_name (int file, struct line_header *lh)
21507 /* Is the file number a valid index into the line header's file name
21508 table? Remember that file numbers start with one, not zero. */
21509 if (1 <= file && file <= lh->file_names.size ())
21511 const file_entry &fe = lh->file_names[file - 1];
21513 if (!IS_ABSOLUTE_PATH (fe.name))
21515 const char *dir = fe.include_dir (lh);
21517 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21519 return xstrdup (fe.name);
21523 /* The compiler produced a bogus file number. We can at least
21524 record the macro definitions made in the file, even if we
21525 won't be able to find the file by name. */
21526 char fake_name[80];
21528 xsnprintf (fake_name, sizeof (fake_name),
21529 "<bad macro file number %d>", file);
21531 complaint (&symfile_complaints,
21532 _("bad file number in macro information (%d)"),
21535 return xstrdup (fake_name);
21539 /* Return the full name of file number I in *LH's file name table.
21540 Use COMP_DIR as the name of the current directory of the
21541 compilation. The result is allocated using xmalloc; the caller is
21542 responsible for freeing it. */
21544 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21546 /* Is the file number a valid index into the line header's file name
21547 table? Remember that file numbers start with one, not zero. */
21548 if (1 <= file && file <= lh->file_names.size ())
21550 char *relative = file_file_name (file, lh);
21552 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21554 return reconcat (relative, comp_dir, SLASH_STRING,
21555 relative, (char *) NULL);
21558 return file_file_name (file, lh);
21562 static struct macro_source_file *
21563 macro_start_file (int file, int line,
21564 struct macro_source_file *current_file,
21565 struct line_header *lh)
21567 /* File name relative to the compilation directory of this source file. */
21568 char *file_name = file_file_name (file, lh);
21570 if (! current_file)
21572 /* Note: We don't create a macro table for this compilation unit
21573 at all until we actually get a filename. */
21574 struct macro_table *macro_table = get_macro_table ();
21576 /* If we have no current file, then this must be the start_file
21577 directive for the compilation unit's main source file. */
21578 current_file = macro_set_main (macro_table, file_name);
21579 macro_define_special (macro_table);
21582 current_file = macro_include (current_file, line, file_name);
21586 return current_file;
21590 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21591 followed by a null byte. */
21593 copy_string (const char *buf, int len)
21595 char *s = (char *) xmalloc (len + 1);
21597 memcpy (s, buf, len);
21603 static const char *
21604 consume_improper_spaces (const char *p, const char *body)
21608 complaint (&symfile_complaints,
21609 _("macro definition contains spaces "
21610 "in formal argument list:\n`%s'"),
21622 parse_macro_definition (struct macro_source_file *file, int line,
21627 /* The body string takes one of two forms. For object-like macro
21628 definitions, it should be:
21630 <macro name> " " <definition>
21632 For function-like macro definitions, it should be:
21634 <macro name> "() " <definition>
21636 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21638 Spaces may appear only where explicitly indicated, and in the
21641 The Dwarf 2 spec says that an object-like macro's name is always
21642 followed by a space, but versions of GCC around March 2002 omit
21643 the space when the macro's definition is the empty string.
21645 The Dwarf 2 spec says that there should be no spaces between the
21646 formal arguments in a function-like macro's formal argument list,
21647 but versions of GCC around March 2002 include spaces after the
21651 /* Find the extent of the macro name. The macro name is terminated
21652 by either a space or null character (for an object-like macro) or
21653 an opening paren (for a function-like macro). */
21654 for (p = body; *p; p++)
21655 if (*p == ' ' || *p == '(')
21658 if (*p == ' ' || *p == '\0')
21660 /* It's an object-like macro. */
21661 int name_len = p - body;
21662 char *name = copy_string (body, name_len);
21663 const char *replacement;
21666 replacement = body + name_len + 1;
21669 dwarf2_macro_malformed_definition_complaint (body);
21670 replacement = body + name_len;
21673 macro_define_object (file, line, name, replacement);
21677 else if (*p == '(')
21679 /* It's a function-like macro. */
21680 char *name = copy_string (body, p - body);
21683 char **argv = XNEWVEC (char *, argv_size);
21687 p = consume_improper_spaces (p, body);
21689 /* Parse the formal argument list. */
21690 while (*p && *p != ')')
21692 /* Find the extent of the current argument name. */
21693 const char *arg_start = p;
21695 while (*p && *p != ',' && *p != ')' && *p != ' ')
21698 if (! *p || p == arg_start)
21699 dwarf2_macro_malformed_definition_complaint (body);
21702 /* Make sure argv has room for the new argument. */
21703 if (argc >= argv_size)
21706 argv = XRESIZEVEC (char *, argv, argv_size);
21709 argv[argc++] = copy_string (arg_start, p - arg_start);
21712 p = consume_improper_spaces (p, body);
21714 /* Consume the comma, if present. */
21719 p = consume_improper_spaces (p, body);
21728 /* Perfectly formed definition, no complaints. */
21729 macro_define_function (file, line, name,
21730 argc, (const char **) argv,
21732 else if (*p == '\0')
21734 /* Complain, but do define it. */
21735 dwarf2_macro_malformed_definition_complaint (body);
21736 macro_define_function (file, line, name,
21737 argc, (const char **) argv,
21741 /* Just complain. */
21742 dwarf2_macro_malformed_definition_complaint (body);
21745 /* Just complain. */
21746 dwarf2_macro_malformed_definition_complaint (body);
21752 for (i = 0; i < argc; i++)
21758 dwarf2_macro_malformed_definition_complaint (body);
21761 /* Skip some bytes from BYTES according to the form given in FORM.
21762 Returns the new pointer. */
21764 static const gdb_byte *
21765 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21766 enum dwarf_form form,
21767 unsigned int offset_size,
21768 struct dwarf2_section_info *section)
21770 unsigned int bytes_read;
21774 case DW_FORM_data1:
21779 case DW_FORM_data2:
21783 case DW_FORM_data4:
21787 case DW_FORM_data8:
21791 case DW_FORM_data16:
21795 case DW_FORM_string:
21796 read_direct_string (abfd, bytes, &bytes_read);
21797 bytes += bytes_read;
21800 case DW_FORM_sec_offset:
21802 case DW_FORM_GNU_strp_alt:
21803 bytes += offset_size;
21806 case DW_FORM_block:
21807 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21808 bytes += bytes_read;
21811 case DW_FORM_block1:
21812 bytes += 1 + read_1_byte (abfd, bytes);
21814 case DW_FORM_block2:
21815 bytes += 2 + read_2_bytes (abfd, bytes);
21817 case DW_FORM_block4:
21818 bytes += 4 + read_4_bytes (abfd, bytes);
21821 case DW_FORM_sdata:
21822 case DW_FORM_udata:
21823 case DW_FORM_GNU_addr_index:
21824 case DW_FORM_GNU_str_index:
21825 bytes = gdb_skip_leb128 (bytes, buffer_end);
21828 dwarf2_section_buffer_overflow_complaint (section);
21836 complaint (&symfile_complaints,
21837 _("invalid form 0x%x in `%s'"),
21838 form, get_section_name (section));
21846 /* A helper for dwarf_decode_macros that handles skipping an unknown
21847 opcode. Returns an updated pointer to the macro data buffer; or,
21848 on error, issues a complaint and returns NULL. */
21850 static const gdb_byte *
21851 skip_unknown_opcode (unsigned int opcode,
21852 const gdb_byte **opcode_definitions,
21853 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21855 unsigned int offset_size,
21856 struct dwarf2_section_info *section)
21858 unsigned int bytes_read, i;
21860 const gdb_byte *defn;
21862 if (opcode_definitions[opcode] == NULL)
21864 complaint (&symfile_complaints,
21865 _("unrecognized DW_MACFINO opcode 0x%x"),
21870 defn = opcode_definitions[opcode];
21871 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21872 defn += bytes_read;
21874 for (i = 0; i < arg; ++i)
21876 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21877 (enum dwarf_form) defn[i], offset_size,
21879 if (mac_ptr == NULL)
21881 /* skip_form_bytes already issued the complaint. */
21889 /* A helper function which parses the header of a macro section.
21890 If the macro section is the extended (for now called "GNU") type,
21891 then this updates *OFFSET_SIZE. Returns a pointer to just after
21892 the header, or issues a complaint and returns NULL on error. */
21894 static const gdb_byte *
21895 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21897 const gdb_byte *mac_ptr,
21898 unsigned int *offset_size,
21899 int section_is_gnu)
21901 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21903 if (section_is_gnu)
21905 unsigned int version, flags;
21907 version = read_2_bytes (abfd, mac_ptr);
21908 if (version != 4 && version != 5)
21910 complaint (&symfile_complaints,
21911 _("unrecognized version `%d' in .debug_macro section"),
21917 flags = read_1_byte (abfd, mac_ptr);
21919 *offset_size = (flags & 1) ? 8 : 4;
21921 if ((flags & 2) != 0)
21922 /* We don't need the line table offset. */
21923 mac_ptr += *offset_size;
21925 /* Vendor opcode descriptions. */
21926 if ((flags & 4) != 0)
21928 unsigned int i, count;
21930 count = read_1_byte (abfd, mac_ptr);
21932 for (i = 0; i < count; ++i)
21934 unsigned int opcode, bytes_read;
21937 opcode = read_1_byte (abfd, mac_ptr);
21939 opcode_definitions[opcode] = mac_ptr;
21940 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21941 mac_ptr += bytes_read;
21950 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21951 including DW_MACRO_import. */
21954 dwarf_decode_macro_bytes (bfd *abfd,
21955 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21956 struct macro_source_file *current_file,
21957 struct line_header *lh,
21958 struct dwarf2_section_info *section,
21959 int section_is_gnu, int section_is_dwz,
21960 unsigned int offset_size,
21961 htab_t include_hash)
21963 struct objfile *objfile = dwarf2_per_objfile->objfile;
21964 enum dwarf_macro_record_type macinfo_type;
21965 int at_commandline;
21966 const gdb_byte *opcode_definitions[256];
21968 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21969 &offset_size, section_is_gnu);
21970 if (mac_ptr == NULL)
21972 /* We already issued a complaint. */
21976 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21977 GDB is still reading the definitions from command line. First
21978 DW_MACINFO_start_file will need to be ignored as it was already executed
21979 to create CURRENT_FILE for the main source holding also the command line
21980 definitions. On first met DW_MACINFO_start_file this flag is reset to
21981 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21983 at_commandline = 1;
21987 /* Do we at least have room for a macinfo type byte? */
21988 if (mac_ptr >= mac_end)
21990 dwarf2_section_buffer_overflow_complaint (section);
21994 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21997 /* Note that we rely on the fact that the corresponding GNU and
21998 DWARF constants are the same. */
21999 switch (macinfo_type)
22001 /* A zero macinfo type indicates the end of the macro
22006 case DW_MACRO_define:
22007 case DW_MACRO_undef:
22008 case DW_MACRO_define_strp:
22009 case DW_MACRO_undef_strp:
22010 case DW_MACRO_define_sup:
22011 case DW_MACRO_undef_sup:
22013 unsigned int bytes_read;
22018 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22019 mac_ptr += bytes_read;
22021 if (macinfo_type == DW_MACRO_define
22022 || macinfo_type == DW_MACRO_undef)
22024 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22025 mac_ptr += bytes_read;
22029 LONGEST str_offset;
22031 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22032 mac_ptr += offset_size;
22034 if (macinfo_type == DW_MACRO_define_sup
22035 || macinfo_type == DW_MACRO_undef_sup
22038 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22040 body = read_indirect_string_from_dwz (dwz, str_offset);
22043 body = read_indirect_string_at_offset (abfd, str_offset);
22046 is_define = (macinfo_type == DW_MACRO_define
22047 || macinfo_type == DW_MACRO_define_strp
22048 || macinfo_type == DW_MACRO_define_sup);
22049 if (! current_file)
22051 /* DWARF violation as no main source is present. */
22052 complaint (&symfile_complaints,
22053 _("debug info with no main source gives macro %s "
22055 is_define ? _("definition") : _("undefinition"),
22059 if ((line == 0 && !at_commandline)
22060 || (line != 0 && at_commandline))
22061 complaint (&symfile_complaints,
22062 _("debug info gives %s macro %s with %s line %d: %s"),
22063 at_commandline ? _("command-line") : _("in-file"),
22064 is_define ? _("definition") : _("undefinition"),
22065 line == 0 ? _("zero") : _("non-zero"), line, body);
22068 parse_macro_definition (current_file, line, body);
22071 gdb_assert (macinfo_type == DW_MACRO_undef
22072 || macinfo_type == DW_MACRO_undef_strp
22073 || macinfo_type == DW_MACRO_undef_sup);
22074 macro_undef (current_file, line, body);
22079 case DW_MACRO_start_file:
22081 unsigned int bytes_read;
22084 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22085 mac_ptr += bytes_read;
22086 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22087 mac_ptr += bytes_read;
22089 if ((line == 0 && !at_commandline)
22090 || (line != 0 && at_commandline))
22091 complaint (&symfile_complaints,
22092 _("debug info gives source %d included "
22093 "from %s at %s line %d"),
22094 file, at_commandline ? _("command-line") : _("file"),
22095 line == 0 ? _("zero") : _("non-zero"), line);
22097 if (at_commandline)
22099 /* This DW_MACRO_start_file was executed in the
22101 at_commandline = 0;
22104 current_file = macro_start_file (file, line, current_file, lh);
22108 case DW_MACRO_end_file:
22109 if (! current_file)
22110 complaint (&symfile_complaints,
22111 _("macro debug info has an unmatched "
22112 "`close_file' directive"));
22115 current_file = current_file->included_by;
22116 if (! current_file)
22118 enum dwarf_macro_record_type next_type;
22120 /* GCC circa March 2002 doesn't produce the zero
22121 type byte marking the end of the compilation
22122 unit. Complain if it's not there, but exit no
22125 /* Do we at least have room for a macinfo type byte? */
22126 if (mac_ptr >= mac_end)
22128 dwarf2_section_buffer_overflow_complaint (section);
22132 /* We don't increment mac_ptr here, so this is just
22135 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22137 if (next_type != 0)
22138 complaint (&symfile_complaints,
22139 _("no terminating 0-type entry for "
22140 "macros in `.debug_macinfo' section"));
22147 case DW_MACRO_import:
22148 case DW_MACRO_import_sup:
22152 bfd *include_bfd = abfd;
22153 struct dwarf2_section_info *include_section = section;
22154 const gdb_byte *include_mac_end = mac_end;
22155 int is_dwz = section_is_dwz;
22156 const gdb_byte *new_mac_ptr;
22158 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22159 mac_ptr += offset_size;
22161 if (macinfo_type == DW_MACRO_import_sup)
22163 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22165 dwarf2_read_section (objfile, &dwz->macro);
22167 include_section = &dwz->macro;
22168 include_bfd = get_section_bfd_owner (include_section);
22169 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22173 new_mac_ptr = include_section->buffer + offset;
22174 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22178 /* This has actually happened; see
22179 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22180 complaint (&symfile_complaints,
22181 _("recursive DW_MACRO_import in "
22182 ".debug_macro section"));
22186 *slot = (void *) new_mac_ptr;
22188 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22189 include_mac_end, current_file, lh,
22190 section, section_is_gnu, is_dwz,
22191 offset_size, include_hash);
22193 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22198 case DW_MACINFO_vendor_ext:
22199 if (!section_is_gnu)
22201 unsigned int bytes_read;
22203 /* This reads the constant, but since we don't recognize
22204 any vendor extensions, we ignore it. */
22205 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22206 mac_ptr += bytes_read;
22207 read_direct_string (abfd, mac_ptr, &bytes_read);
22208 mac_ptr += bytes_read;
22210 /* We don't recognize any vendor extensions. */
22216 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22217 mac_ptr, mac_end, abfd, offset_size,
22219 if (mac_ptr == NULL)
22223 } while (macinfo_type != 0);
22227 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22228 int section_is_gnu)
22230 struct objfile *objfile = dwarf2_per_objfile->objfile;
22231 struct line_header *lh = cu->line_header;
22233 const gdb_byte *mac_ptr, *mac_end;
22234 struct macro_source_file *current_file = 0;
22235 enum dwarf_macro_record_type macinfo_type;
22236 unsigned int offset_size = cu->header.offset_size;
22237 const gdb_byte *opcode_definitions[256];
22238 struct cleanup *cleanup;
22240 struct dwarf2_section_info *section;
22241 const char *section_name;
22243 if (cu->dwo_unit != NULL)
22245 if (section_is_gnu)
22247 section = &cu->dwo_unit->dwo_file->sections.macro;
22248 section_name = ".debug_macro.dwo";
22252 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22253 section_name = ".debug_macinfo.dwo";
22258 if (section_is_gnu)
22260 section = &dwarf2_per_objfile->macro;
22261 section_name = ".debug_macro";
22265 section = &dwarf2_per_objfile->macinfo;
22266 section_name = ".debug_macinfo";
22270 dwarf2_read_section (objfile, section);
22271 if (section->buffer == NULL)
22273 complaint (&symfile_complaints, _("missing %s section"), section_name);
22276 abfd = get_section_bfd_owner (section);
22278 /* First pass: Find the name of the base filename.
22279 This filename is needed in order to process all macros whose definition
22280 (or undefinition) comes from the command line. These macros are defined
22281 before the first DW_MACINFO_start_file entry, and yet still need to be
22282 associated to the base file.
22284 To determine the base file name, we scan the macro definitions until we
22285 reach the first DW_MACINFO_start_file entry. We then initialize
22286 CURRENT_FILE accordingly so that any macro definition found before the
22287 first DW_MACINFO_start_file can still be associated to the base file. */
22289 mac_ptr = section->buffer + offset;
22290 mac_end = section->buffer + section->size;
22292 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22293 &offset_size, section_is_gnu);
22294 if (mac_ptr == NULL)
22296 /* We already issued a complaint. */
22302 /* Do we at least have room for a macinfo type byte? */
22303 if (mac_ptr >= mac_end)
22305 /* Complaint is printed during the second pass as GDB will probably
22306 stop the first pass earlier upon finding
22307 DW_MACINFO_start_file. */
22311 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22314 /* Note that we rely on the fact that the corresponding GNU and
22315 DWARF constants are the same. */
22316 switch (macinfo_type)
22318 /* A zero macinfo type indicates the end of the macro
22323 case DW_MACRO_define:
22324 case DW_MACRO_undef:
22325 /* Only skip the data by MAC_PTR. */
22327 unsigned int bytes_read;
22329 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22330 mac_ptr += bytes_read;
22331 read_direct_string (abfd, mac_ptr, &bytes_read);
22332 mac_ptr += bytes_read;
22336 case DW_MACRO_start_file:
22338 unsigned int bytes_read;
22341 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22342 mac_ptr += bytes_read;
22343 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22344 mac_ptr += bytes_read;
22346 current_file = macro_start_file (file, line, current_file, lh);
22350 case DW_MACRO_end_file:
22351 /* No data to skip by MAC_PTR. */
22354 case DW_MACRO_define_strp:
22355 case DW_MACRO_undef_strp:
22356 case DW_MACRO_define_sup:
22357 case DW_MACRO_undef_sup:
22359 unsigned int bytes_read;
22361 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22362 mac_ptr += bytes_read;
22363 mac_ptr += offset_size;
22367 case DW_MACRO_import:
22368 case DW_MACRO_import_sup:
22369 /* Note that, according to the spec, a transparent include
22370 chain cannot call DW_MACRO_start_file. So, we can just
22371 skip this opcode. */
22372 mac_ptr += offset_size;
22375 case DW_MACINFO_vendor_ext:
22376 /* Only skip the data by MAC_PTR. */
22377 if (!section_is_gnu)
22379 unsigned int bytes_read;
22381 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22382 mac_ptr += bytes_read;
22383 read_direct_string (abfd, mac_ptr, &bytes_read);
22384 mac_ptr += bytes_read;
22389 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22390 mac_ptr, mac_end, abfd, offset_size,
22392 if (mac_ptr == NULL)
22396 } while (macinfo_type != 0 && current_file == NULL);
22398 /* Second pass: Process all entries.
22400 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22401 command-line macro definitions/undefinitions. This flag is unset when we
22402 reach the first DW_MACINFO_start_file entry. */
22404 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22406 NULL, xcalloc, xfree));
22407 mac_ptr = section->buffer + offset;
22408 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22409 *slot = (void *) mac_ptr;
22410 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22411 current_file, lh, section,
22412 section_is_gnu, 0, offset_size,
22413 include_hash.get ());
22416 /* Check if the attribute's form is a DW_FORM_block*
22417 if so return true else false. */
22420 attr_form_is_block (const struct attribute *attr)
22422 return (attr == NULL ? 0 :
22423 attr->form == DW_FORM_block1
22424 || attr->form == DW_FORM_block2
22425 || attr->form == DW_FORM_block4
22426 || attr->form == DW_FORM_block
22427 || attr->form == DW_FORM_exprloc);
22430 /* Return non-zero if ATTR's value is a section offset --- classes
22431 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22432 You may use DW_UNSND (attr) to retrieve such offsets.
22434 Section 7.5.4, "Attribute Encodings", explains that no attribute
22435 may have a value that belongs to more than one of these classes; it
22436 would be ambiguous if we did, because we use the same forms for all
22440 attr_form_is_section_offset (const struct attribute *attr)
22442 return (attr->form == DW_FORM_data4
22443 || attr->form == DW_FORM_data8
22444 || attr->form == DW_FORM_sec_offset);
22447 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22448 zero otherwise. When this function returns true, you can apply
22449 dwarf2_get_attr_constant_value to it.
22451 However, note that for some attributes you must check
22452 attr_form_is_section_offset before using this test. DW_FORM_data4
22453 and DW_FORM_data8 are members of both the constant class, and of
22454 the classes that contain offsets into other debug sections
22455 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22456 that, if an attribute's can be either a constant or one of the
22457 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22458 taken as section offsets, not constants.
22460 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22461 cannot handle that. */
22464 attr_form_is_constant (const struct attribute *attr)
22466 switch (attr->form)
22468 case DW_FORM_sdata:
22469 case DW_FORM_udata:
22470 case DW_FORM_data1:
22471 case DW_FORM_data2:
22472 case DW_FORM_data4:
22473 case DW_FORM_data8:
22481 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22482 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22485 attr_form_is_ref (const struct attribute *attr)
22487 switch (attr->form)
22489 case DW_FORM_ref_addr:
22494 case DW_FORM_ref_udata:
22495 case DW_FORM_GNU_ref_alt:
22502 /* Return the .debug_loc section to use for CU.
22503 For DWO files use .debug_loc.dwo. */
22505 static struct dwarf2_section_info *
22506 cu_debug_loc_section (struct dwarf2_cu *cu)
22510 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22512 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
22514 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22515 : &dwarf2_per_objfile->loc);
22518 /* A helper function that fills in a dwarf2_loclist_baton. */
22521 fill_in_loclist_baton (struct dwarf2_cu *cu,
22522 struct dwarf2_loclist_baton *baton,
22523 const struct attribute *attr)
22525 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22527 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22529 baton->per_cu = cu->per_cu;
22530 gdb_assert (baton->per_cu);
22531 /* We don't know how long the location list is, but make sure we
22532 don't run off the edge of the section. */
22533 baton->size = section->size - DW_UNSND (attr);
22534 baton->data = section->buffer + DW_UNSND (attr);
22535 baton->base_address = cu->base_address;
22536 baton->from_dwo = cu->dwo_unit != NULL;
22540 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22541 struct dwarf2_cu *cu, int is_block)
22543 struct objfile *objfile = dwarf2_per_objfile->objfile;
22544 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22546 if (attr_form_is_section_offset (attr)
22547 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22548 the section. If so, fall through to the complaint in the
22550 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22552 struct dwarf2_loclist_baton *baton;
22554 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22556 fill_in_loclist_baton (cu, baton, attr);
22558 if (cu->base_known == 0)
22559 complaint (&symfile_complaints,
22560 _("Location list used without "
22561 "specifying the CU base address."));
22563 SYMBOL_ACLASS_INDEX (sym) = (is_block
22564 ? dwarf2_loclist_block_index
22565 : dwarf2_loclist_index);
22566 SYMBOL_LOCATION_BATON (sym) = baton;
22570 struct dwarf2_locexpr_baton *baton;
22572 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22573 baton->per_cu = cu->per_cu;
22574 gdb_assert (baton->per_cu);
22576 if (attr_form_is_block (attr))
22578 /* Note that we're just copying the block's data pointer
22579 here, not the actual data. We're still pointing into the
22580 info_buffer for SYM's objfile; right now we never release
22581 that buffer, but when we do clean up properly this may
22583 baton->size = DW_BLOCK (attr)->size;
22584 baton->data = DW_BLOCK (attr)->data;
22588 dwarf2_invalid_attrib_class_complaint ("location description",
22589 SYMBOL_NATURAL_NAME (sym));
22593 SYMBOL_ACLASS_INDEX (sym) = (is_block
22594 ? dwarf2_locexpr_block_index
22595 : dwarf2_locexpr_index);
22596 SYMBOL_LOCATION_BATON (sym) = baton;
22600 /* Return the OBJFILE associated with the compilation unit CU. If CU
22601 came from a separate debuginfo file, then the master objfile is
22605 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22607 struct objfile *objfile = per_cu->objfile;
22609 /* Return the master objfile, so that we can report and look up the
22610 correct file containing this variable. */
22611 if (objfile->separate_debug_objfile_backlink)
22612 objfile = objfile->separate_debug_objfile_backlink;
22617 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22618 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22619 CU_HEADERP first. */
22621 static const struct comp_unit_head *
22622 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22623 struct dwarf2_per_cu_data *per_cu)
22625 const gdb_byte *info_ptr;
22628 return &per_cu->cu->header;
22630 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22632 memset (cu_headerp, 0, sizeof (*cu_headerp));
22633 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22634 rcuh_kind::COMPILE);
22639 /* Return the address size given in the compilation unit header for CU. */
22642 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22644 struct comp_unit_head cu_header_local;
22645 const struct comp_unit_head *cu_headerp;
22647 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22649 return cu_headerp->addr_size;
22652 /* Return the offset size given in the compilation unit header for CU. */
22655 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22657 struct comp_unit_head cu_header_local;
22658 const struct comp_unit_head *cu_headerp;
22660 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22662 return cu_headerp->offset_size;
22665 /* See its dwarf2loc.h declaration. */
22668 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22670 struct comp_unit_head cu_header_local;
22671 const struct comp_unit_head *cu_headerp;
22673 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22675 if (cu_headerp->version == 2)
22676 return cu_headerp->addr_size;
22678 return cu_headerp->offset_size;
22681 /* Return the text offset of the CU. The returned offset comes from
22682 this CU's objfile. If this objfile came from a separate debuginfo
22683 file, then the offset may be different from the corresponding
22684 offset in the parent objfile. */
22687 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22689 struct objfile *objfile = per_cu->objfile;
22691 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22694 /* Return DWARF version number of PER_CU. */
22697 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22699 return per_cu->dwarf_version;
22702 /* Locate the .debug_info compilation unit from CU's objfile which contains
22703 the DIE at OFFSET. Raises an error on failure. */
22705 static struct dwarf2_per_cu_data *
22706 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22707 unsigned int offset_in_dwz,
22708 struct objfile *objfile)
22710 struct dwarf2_per_cu_data *this_cu;
22712 const sect_offset *cu_off;
22715 high = dwarf2_per_objfile->n_comp_units - 1;
22718 struct dwarf2_per_cu_data *mid_cu;
22719 int mid = low + (high - low) / 2;
22721 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22722 cu_off = &mid_cu->sect_off;
22723 if (mid_cu->is_dwz > offset_in_dwz
22724 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
22729 gdb_assert (low == high);
22730 this_cu = dwarf2_per_objfile->all_comp_units[low];
22731 cu_off = &this_cu->sect_off;
22732 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
22734 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22735 error (_("Dwarf Error: could not find partial DIE containing "
22736 "offset 0x%x [in module %s]"),
22737 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
22739 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22741 return dwarf2_per_objfile->all_comp_units[low-1];
22745 this_cu = dwarf2_per_objfile->all_comp_units[low];
22746 if (low == dwarf2_per_objfile->n_comp_units - 1
22747 && sect_off >= this_cu->sect_off + this_cu->length)
22748 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22749 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22754 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22757 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22759 memset (cu, 0, sizeof (*cu));
22761 cu->per_cu = per_cu;
22762 cu->objfile = per_cu->objfile;
22763 obstack_init (&cu->comp_unit_obstack);
22766 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22769 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22770 enum language pretend_language)
22772 struct attribute *attr;
22774 /* Set the language we're debugging. */
22775 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22777 set_cu_language (DW_UNSND (attr), cu);
22780 cu->language = pretend_language;
22781 cu->language_defn = language_def (cu->language);
22784 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22787 /* Release one cached compilation unit, CU. We unlink it from the tree
22788 of compilation units, but we don't remove it from the read_in_chain;
22789 the caller is responsible for that.
22790 NOTE: DATA is a void * because this function is also used as a
22791 cleanup routine. */
22794 free_heap_comp_unit (void *data)
22796 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22798 gdb_assert (cu->per_cu != NULL);
22799 cu->per_cu->cu = NULL;
22802 obstack_free (&cu->comp_unit_obstack, NULL);
22807 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22808 when we're finished with it. We can't free the pointer itself, but be
22809 sure to unlink it from the cache. Also release any associated storage. */
22812 free_stack_comp_unit (void *data)
22814 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22816 gdb_assert (cu->per_cu != NULL);
22817 cu->per_cu->cu = NULL;
22820 obstack_free (&cu->comp_unit_obstack, NULL);
22821 cu->partial_dies = NULL;
22824 /* Free all cached compilation units. */
22827 free_cached_comp_units (void *data)
22829 dwarf2_per_objfile->free_cached_comp_units ();
22832 /* Increase the age counter on each cached compilation unit, and free
22833 any that are too old. */
22836 age_cached_comp_units (void)
22838 struct dwarf2_per_cu_data *per_cu, **last_chain;
22840 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22841 per_cu = dwarf2_per_objfile->read_in_chain;
22842 while (per_cu != NULL)
22844 per_cu->cu->last_used ++;
22845 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22846 dwarf2_mark (per_cu->cu);
22847 per_cu = per_cu->cu->read_in_chain;
22850 per_cu = dwarf2_per_objfile->read_in_chain;
22851 last_chain = &dwarf2_per_objfile->read_in_chain;
22852 while (per_cu != NULL)
22854 struct dwarf2_per_cu_data *next_cu;
22856 next_cu = per_cu->cu->read_in_chain;
22858 if (!per_cu->cu->mark)
22860 free_heap_comp_unit (per_cu->cu);
22861 *last_chain = next_cu;
22864 last_chain = &per_cu->cu->read_in_chain;
22870 /* Remove a single compilation unit from the cache. */
22873 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22875 struct dwarf2_per_cu_data *per_cu, **last_chain;
22877 per_cu = dwarf2_per_objfile->read_in_chain;
22878 last_chain = &dwarf2_per_objfile->read_in_chain;
22879 while (per_cu != NULL)
22881 struct dwarf2_per_cu_data *next_cu;
22883 next_cu = per_cu->cu->read_in_chain;
22885 if (per_cu == target_per_cu)
22887 free_heap_comp_unit (per_cu->cu);
22889 *last_chain = next_cu;
22893 last_chain = &per_cu->cu->read_in_chain;
22899 /* Release all extra memory associated with OBJFILE. */
22902 dwarf2_free_objfile (struct objfile *objfile)
22905 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22906 dwarf2_objfile_data_key);
22908 if (dwarf2_per_objfile == NULL)
22911 dwarf2_per_objfile->~dwarf2_per_objfile ();
22914 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22915 We store these in a hash table separate from the DIEs, and preserve them
22916 when the DIEs are flushed out of cache.
22918 The CU "per_cu" pointer is needed because offset alone is not enough to
22919 uniquely identify the type. A file may have multiple .debug_types sections,
22920 or the type may come from a DWO file. Furthermore, while it's more logical
22921 to use per_cu->section+offset, with Fission the section with the data is in
22922 the DWO file but we don't know that section at the point we need it.
22923 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22924 because we can enter the lookup routine, get_die_type_at_offset, from
22925 outside this file, and thus won't necessarily have PER_CU->cu.
22926 Fortunately, PER_CU is stable for the life of the objfile. */
22928 struct dwarf2_per_cu_offset_and_type
22930 const struct dwarf2_per_cu_data *per_cu;
22931 sect_offset sect_off;
22935 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22938 per_cu_offset_and_type_hash (const void *item)
22940 const struct dwarf2_per_cu_offset_and_type *ofs
22941 = (const struct dwarf2_per_cu_offset_and_type *) item;
22943 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
22946 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22949 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22951 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22952 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22953 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22954 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22956 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22957 && ofs_lhs->sect_off == ofs_rhs->sect_off);
22960 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22961 table if necessary. For convenience, return TYPE.
22963 The DIEs reading must have careful ordering to:
22964 * Not cause infite loops trying to read in DIEs as a prerequisite for
22965 reading current DIE.
22966 * Not trying to dereference contents of still incompletely read in types
22967 while reading in other DIEs.
22968 * Enable referencing still incompletely read in types just by a pointer to
22969 the type without accessing its fields.
22971 Therefore caller should follow these rules:
22972 * Try to fetch any prerequisite types we may need to build this DIE type
22973 before building the type and calling set_die_type.
22974 * After building type call set_die_type for current DIE as soon as
22975 possible before fetching more types to complete the current type.
22976 * Make the type as complete as possible before fetching more types. */
22978 static struct type *
22979 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22981 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22982 struct objfile *objfile = cu->objfile;
22983 struct attribute *attr;
22984 struct dynamic_prop prop;
22986 /* For Ada types, make sure that the gnat-specific data is always
22987 initialized (if not already set). There are a few types where
22988 we should not be doing so, because the type-specific area is
22989 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22990 where the type-specific area is used to store the floatformat).
22991 But this is not a problem, because the gnat-specific information
22992 is actually not needed for these types. */
22993 if (need_gnat_info (cu)
22994 && TYPE_CODE (type) != TYPE_CODE_FUNC
22995 && TYPE_CODE (type) != TYPE_CODE_FLT
22996 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22997 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22998 && TYPE_CODE (type) != TYPE_CODE_METHOD
22999 && !HAVE_GNAT_AUX_INFO (type))
23000 INIT_GNAT_SPECIFIC (type);
23002 /* Read DW_AT_allocated and set in type. */
23003 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23004 if (attr_form_is_block (attr))
23006 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23007 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23009 else if (attr != NULL)
23011 complaint (&symfile_complaints,
23012 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23013 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23014 to_underlying (die->sect_off));
23017 /* Read DW_AT_associated and set in type. */
23018 attr = dwarf2_attr (die, DW_AT_associated, cu);
23019 if (attr_form_is_block (attr))
23021 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23022 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23024 else if (attr != NULL)
23026 complaint (&symfile_complaints,
23027 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23028 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23029 to_underlying (die->sect_off));
23032 /* Read DW_AT_data_location and set in type. */
23033 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23034 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23035 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
23037 if (dwarf2_per_objfile->die_type_hash == NULL)
23039 dwarf2_per_objfile->die_type_hash =
23040 htab_create_alloc_ex (127,
23041 per_cu_offset_and_type_hash,
23042 per_cu_offset_and_type_eq,
23044 &objfile->objfile_obstack,
23045 hashtab_obstack_allocate,
23046 dummy_obstack_deallocate);
23049 ofs.per_cu = cu->per_cu;
23050 ofs.sect_off = die->sect_off;
23052 slot = (struct dwarf2_per_cu_offset_and_type **)
23053 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23055 complaint (&symfile_complaints,
23056 _("A problem internal to GDB: DIE 0x%x has type already set"),
23057 to_underlying (die->sect_off));
23058 *slot = XOBNEW (&objfile->objfile_obstack,
23059 struct dwarf2_per_cu_offset_and_type);
23064 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23065 or return NULL if the die does not have a saved type. */
23067 static struct type *
23068 get_die_type_at_offset (sect_offset sect_off,
23069 struct dwarf2_per_cu_data *per_cu)
23071 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23073 if (dwarf2_per_objfile->die_type_hash == NULL)
23076 ofs.per_cu = per_cu;
23077 ofs.sect_off = sect_off;
23078 slot = ((struct dwarf2_per_cu_offset_and_type *)
23079 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23086 /* Look up the type for DIE in CU in die_type_hash,
23087 or return NULL if DIE does not have a saved type. */
23089 static struct type *
23090 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23092 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23095 /* Add a dependence relationship from CU to REF_PER_CU. */
23098 dwarf2_add_dependence (struct dwarf2_cu *cu,
23099 struct dwarf2_per_cu_data *ref_per_cu)
23103 if (cu->dependencies == NULL)
23105 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23106 NULL, &cu->comp_unit_obstack,
23107 hashtab_obstack_allocate,
23108 dummy_obstack_deallocate);
23110 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23112 *slot = ref_per_cu;
23115 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23116 Set the mark field in every compilation unit in the
23117 cache that we must keep because we are keeping CU. */
23120 dwarf2_mark_helper (void **slot, void *data)
23122 struct dwarf2_per_cu_data *per_cu;
23124 per_cu = (struct dwarf2_per_cu_data *) *slot;
23126 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23127 reading of the chain. As such dependencies remain valid it is not much
23128 useful to track and undo them during QUIT cleanups. */
23129 if (per_cu->cu == NULL)
23132 if (per_cu->cu->mark)
23134 per_cu->cu->mark = 1;
23136 if (per_cu->cu->dependencies != NULL)
23137 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23142 /* Set the mark field in CU and in every other compilation unit in the
23143 cache that we must keep because we are keeping CU. */
23146 dwarf2_mark (struct dwarf2_cu *cu)
23151 if (cu->dependencies != NULL)
23152 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23156 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23160 per_cu->cu->mark = 0;
23161 per_cu = per_cu->cu->read_in_chain;
23165 /* Trivial hash function for partial_die_info: the hash value of a DIE
23166 is its offset in .debug_info for this objfile. */
23169 partial_die_hash (const void *item)
23171 const struct partial_die_info *part_die
23172 = (const struct partial_die_info *) item;
23174 return to_underlying (part_die->sect_off);
23177 /* Trivial comparison function for partial_die_info structures: two DIEs
23178 are equal if they have the same offset. */
23181 partial_die_eq (const void *item_lhs, const void *item_rhs)
23183 const struct partial_die_info *part_die_lhs
23184 = (const struct partial_die_info *) item_lhs;
23185 const struct partial_die_info *part_die_rhs
23186 = (const struct partial_die_info *) item_rhs;
23188 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23191 static struct cmd_list_element *set_dwarf_cmdlist;
23192 static struct cmd_list_element *show_dwarf_cmdlist;
23195 set_dwarf_cmd (char *args, int from_tty)
23197 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23202 show_dwarf_cmd (char *args, int from_tty)
23204 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23207 /* Free data associated with OBJFILE, if necessary. */
23210 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23212 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23215 /* Make sure we don't accidentally use dwarf2_per_objfile while
23217 dwarf2_per_objfile = NULL;
23219 for (ix = 0; ix < data->n_comp_units; ++ix)
23220 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23222 for (ix = 0; ix < data->n_type_units; ++ix)
23223 VEC_free (dwarf2_per_cu_ptr,
23224 data->all_type_units[ix]->per_cu.imported_symtabs);
23225 xfree (data->all_type_units);
23227 VEC_free (dwarf2_section_info_def, data->types);
23229 if (data->dwo_files)
23230 free_dwo_files (data->dwo_files, objfile);
23231 if (data->dwp_file)
23232 gdb_bfd_unref (data->dwp_file->dbfd);
23234 if (data->dwz_file && data->dwz_file->dwz_bfd)
23235 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23239 /* The "save gdb-index" command. */
23241 /* In-memory buffer to prepare data to be written later to a file. */
23245 /* Copy DATA to the end of the buffer. */
23246 template<typename T>
23247 void append_data (const T &data)
23249 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23250 reinterpret_cast<const gdb_byte *> (&data + 1),
23251 grow (sizeof (data)));
23254 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23255 terminating zero is appended too. */
23256 void append_cstr0 (const char *cstr)
23258 const size_t size = strlen (cstr) + 1;
23259 std::copy (cstr, cstr + size, grow (size));
23262 /* Accept a host-format integer in VAL and append it to the buffer
23263 as a target-format integer which is LEN bytes long. */
23264 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23266 ::store_unsigned_integer (grow (len), len, byte_order, val);
23269 /* Return the size of the buffer. */
23270 size_t size () const
23272 return m_vec.size ();
23275 /* Write the buffer to FILE. */
23276 void file_write (FILE *file) const
23278 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23279 error (_("couldn't write data to file"));
23283 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23284 the start of the new block. */
23285 gdb_byte *grow (size_t size)
23287 m_vec.resize (m_vec.size () + size);
23288 return &*m_vec.end () - size;
23291 gdb::byte_vector m_vec;
23294 /* An entry in the symbol table. */
23295 struct symtab_index_entry
23297 /* The name of the symbol. */
23299 /* The offset of the name in the constant pool. */
23300 offset_type index_offset;
23301 /* A sorted vector of the indices of all the CUs that hold an object
23303 std::vector<offset_type> cu_indices;
23306 /* The symbol table. This is a power-of-2-sized hash table. */
23307 struct mapped_symtab
23311 data.resize (1024);
23314 offset_type n_elements = 0;
23315 std::vector<symtab_index_entry> data;
23318 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
23321 Function is used only during write_hash_table so no index format backward
23322 compatibility is needed. */
23324 static symtab_index_entry &
23325 find_slot (struct mapped_symtab *symtab, const char *name)
23327 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23329 index = hash & (symtab->data.size () - 1);
23330 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
23334 if (symtab->data[index].name == NULL
23335 || strcmp (name, symtab->data[index].name) == 0)
23336 return symtab->data[index];
23337 index = (index + step) & (symtab->data.size () - 1);
23341 /* Expand SYMTAB's hash table. */
23344 hash_expand (struct mapped_symtab *symtab)
23346 auto old_entries = std::move (symtab->data);
23348 symtab->data.clear ();
23349 symtab->data.resize (old_entries.size () * 2);
23351 for (auto &it : old_entries)
23352 if (it.name != NULL)
23354 auto &ref = find_slot (symtab, it.name);
23355 ref = std::move (it);
23359 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23360 CU_INDEX is the index of the CU in which the symbol appears.
23361 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23364 add_index_entry (struct mapped_symtab *symtab, const char *name,
23365 int is_static, gdb_index_symbol_kind kind,
23366 offset_type cu_index)
23368 offset_type cu_index_and_attrs;
23370 ++symtab->n_elements;
23371 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
23372 hash_expand (symtab);
23374 symtab_index_entry &slot = find_slot (symtab, name);
23375 if (slot.name == NULL)
23378 /* index_offset is set later. */
23381 cu_index_and_attrs = 0;
23382 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23383 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23384 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23386 /* We don't want to record an index value twice as we want to avoid the
23388 We process all global symbols and then all static symbols
23389 (which would allow us to avoid the duplication by only having to check
23390 the last entry pushed), but a symbol could have multiple kinds in one CU.
23391 To keep things simple we don't worry about the duplication here and
23392 sort and uniqufy the list after we've processed all symbols. */
23393 slot.cu_indices.push_back (cu_index_and_attrs);
23396 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23399 uniquify_cu_indices (struct mapped_symtab *symtab)
23401 for (auto &entry : symtab->data)
23403 if (entry.name != NULL && !entry.cu_indices.empty ())
23405 auto &cu_indices = entry.cu_indices;
23406 std::sort (cu_indices.begin (), cu_indices.end ());
23407 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23408 cu_indices.erase (from, cu_indices.end ());
23413 /* A form of 'const char *' suitable for container keys. Only the
23414 pointer is stored. The strings themselves are compared, not the
23419 c_str_view (const char *cstr)
23423 bool operator== (const c_str_view &other) const
23425 return strcmp (m_cstr, other.m_cstr) == 0;
23429 friend class c_str_view_hasher;
23430 const char *const m_cstr;
23433 /* A std::unordered_map::hasher for c_str_view that uses the right
23434 hash function for strings in a mapped index. */
23435 class c_str_view_hasher
23438 size_t operator () (const c_str_view &x) const
23440 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23444 /* A std::unordered_map::hasher for std::vector<>. */
23445 template<typename T>
23446 class vector_hasher
23449 size_t operator () (const std::vector<T> &key) const
23451 return iterative_hash (key.data (),
23452 sizeof (key.front ()) * key.size (), 0);
23456 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23457 constant pool entries going into the data buffer CPOOL. */
23460 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23463 /* Elements are sorted vectors of the indices of all the CUs that
23464 hold an object of this name. */
23465 std::unordered_map<std::vector<offset_type>, offset_type,
23466 vector_hasher<offset_type>>
23469 /* We add all the index vectors to the constant pool first, to
23470 ensure alignment is ok. */
23471 for (symtab_index_entry &entry : symtab->data)
23473 if (entry.name == NULL)
23475 gdb_assert (entry.index_offset == 0);
23477 /* Finding before inserting is faster than always trying to
23478 insert, because inserting always allocates a node, does the
23479 lookup, and then destroys the new node if another node
23480 already had the same key. C++17 try_emplace will avoid
23483 = symbol_hash_table.find (entry.cu_indices);
23484 if (found != symbol_hash_table.end ())
23486 entry.index_offset = found->second;
23490 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23491 entry.index_offset = cpool.size ();
23492 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23493 for (const auto index : entry.cu_indices)
23494 cpool.append_data (MAYBE_SWAP (index));
23498 /* Now write out the hash table. */
23499 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
23500 for (const auto &entry : symtab->data)
23502 offset_type str_off, vec_off;
23504 if (entry.name != NULL)
23506 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
23507 if (insertpair.second)
23508 cpool.append_cstr0 (entry.name);
23509 str_off = insertpair.first->second;
23510 vec_off = entry.index_offset;
23514 /* While 0 is a valid constant pool index, it is not valid
23515 to have 0 for both offsets. */
23520 output.append_data (MAYBE_SWAP (str_off));
23521 output.append_data (MAYBE_SWAP (vec_off));
23525 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
23527 /* Helper struct for building the address table. */
23528 struct addrmap_index_data
23530 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23531 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23534 struct objfile *objfile;
23535 data_buf &addr_vec;
23536 psym_index_map &cu_index_htab;
23538 /* Non-zero if the previous_* fields are valid.
23539 We can't write an entry until we see the next entry (since it is only then
23540 that we know the end of the entry). */
23541 int previous_valid;
23542 /* Index of the CU in the table of all CUs in the index file. */
23543 unsigned int previous_cu_index;
23544 /* Start address of the CU. */
23545 CORE_ADDR previous_cu_start;
23548 /* Write an address entry to ADDR_VEC. */
23551 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
23552 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23554 CORE_ADDR baseaddr;
23556 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23558 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23559 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
23560 addr_vec.append_data (MAYBE_SWAP (cu_index));
23563 /* Worker function for traversing an addrmap to build the address table. */
23566 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23568 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23569 struct partial_symtab *pst = (struct partial_symtab *) obj;
23571 if (data->previous_valid)
23572 add_address_entry (data->objfile, data->addr_vec,
23573 data->previous_cu_start, start_addr,
23574 data->previous_cu_index);
23576 data->previous_cu_start = start_addr;
23579 const auto it = data->cu_index_htab.find (pst);
23580 gdb_assert (it != data->cu_index_htab.cend ());
23581 data->previous_cu_index = it->second;
23582 data->previous_valid = 1;
23585 data->previous_valid = 0;
23590 /* Write OBJFILE's address map to ADDR_VEC.
23591 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23592 in the index file. */
23595 write_address_map (struct objfile *objfile, data_buf &addr_vec,
23596 psym_index_map &cu_index_htab)
23598 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
23600 /* When writing the address table, we have to cope with the fact that
23601 the addrmap iterator only provides the start of a region; we have to
23602 wait until the next invocation to get the start of the next region. */
23604 addrmap_index_data.objfile = objfile;
23605 addrmap_index_data.previous_valid = 0;
23607 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23608 &addrmap_index_data);
23610 /* It's highly unlikely the last entry (end address = 0xff...ff)
23611 is valid, but we should still handle it.
23612 The end address is recorded as the start of the next region, but that
23613 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23615 if (addrmap_index_data.previous_valid)
23616 add_address_entry (objfile, addr_vec,
23617 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23618 addrmap_index_data.previous_cu_index);
23621 /* Return the symbol kind of PSYM. */
23623 static gdb_index_symbol_kind
23624 symbol_kind (struct partial_symbol *psym)
23626 domain_enum domain = PSYMBOL_DOMAIN (psym);
23627 enum address_class aclass = PSYMBOL_CLASS (psym);
23635 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23637 return GDB_INDEX_SYMBOL_KIND_TYPE;
23639 case LOC_CONST_BYTES:
23640 case LOC_OPTIMIZED_OUT:
23642 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23644 /* Note: It's currently impossible to recognize psyms as enum values
23645 short of reading the type info. For now punt. */
23646 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23648 /* There are other LOC_FOO values that one might want to classify
23649 as variables, but dwarf2read.c doesn't currently use them. */
23650 return GDB_INDEX_SYMBOL_KIND_OTHER;
23652 case STRUCT_DOMAIN:
23653 return GDB_INDEX_SYMBOL_KIND_TYPE;
23655 return GDB_INDEX_SYMBOL_KIND_OTHER;
23659 /* Add a list of partial symbols to SYMTAB. */
23662 write_psymbols (struct mapped_symtab *symtab,
23663 std::unordered_set<partial_symbol *> &psyms_seen,
23664 struct partial_symbol **psymp,
23666 offset_type cu_index,
23669 for (; count-- > 0; ++psymp)
23671 struct partial_symbol *psym = *psymp;
23673 if (SYMBOL_LANGUAGE (psym) == language_ada)
23674 error (_("Ada is not currently supported by the index"));
23676 /* Only add a given psymbol once. */
23677 if (psyms_seen.insert (psym).second)
23679 gdb_index_symbol_kind kind = symbol_kind (psym);
23681 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23682 is_static, kind, cu_index);
23687 /* A helper struct used when iterating over debug_types. */
23688 struct signatured_type_index_data
23690 signatured_type_index_data (data_buf &types_list_,
23691 std::unordered_set<partial_symbol *> &psyms_seen_)
23692 : types_list (types_list_), psyms_seen (psyms_seen_)
23695 struct objfile *objfile;
23696 struct mapped_symtab *symtab;
23697 data_buf &types_list;
23698 std::unordered_set<partial_symbol *> &psyms_seen;
23702 /* A helper function that writes a single signatured_type to an
23706 write_one_signatured_type (void **slot, void *d)
23708 struct signatured_type_index_data *info
23709 = (struct signatured_type_index_data *) d;
23710 struct signatured_type *entry = (struct signatured_type *) *slot;
23711 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23713 write_psymbols (info->symtab,
23715 info->objfile->global_psymbols.list
23716 + psymtab->globals_offset,
23717 psymtab->n_global_syms, info->cu_index,
23719 write_psymbols (info->symtab,
23721 info->objfile->static_psymbols.list
23722 + psymtab->statics_offset,
23723 psymtab->n_static_syms, info->cu_index,
23726 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23727 to_underlying (entry->per_cu.sect_off));
23728 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23729 to_underlying (entry->type_offset_in_tu));
23730 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
23737 /* Recurse into all "included" dependencies and count their symbols as
23738 if they appeared in this psymtab. */
23741 recursively_count_psymbols (struct partial_symtab *psymtab,
23742 size_t &psyms_seen)
23744 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23745 if (psymtab->dependencies[i]->user != NULL)
23746 recursively_count_psymbols (psymtab->dependencies[i],
23749 psyms_seen += psymtab->n_global_syms;
23750 psyms_seen += psymtab->n_static_syms;
23753 /* Recurse into all "included" dependencies and write their symbols as
23754 if they appeared in this psymtab. */
23757 recursively_write_psymbols (struct objfile *objfile,
23758 struct partial_symtab *psymtab,
23759 struct mapped_symtab *symtab,
23760 std::unordered_set<partial_symbol *> &psyms_seen,
23761 offset_type cu_index)
23765 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23766 if (psymtab->dependencies[i]->user != NULL)
23767 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23768 symtab, psyms_seen, cu_index);
23770 write_psymbols (symtab,
23772 objfile->global_psymbols.list + psymtab->globals_offset,
23773 psymtab->n_global_syms, cu_index,
23775 write_psymbols (symtab,
23777 objfile->static_psymbols.list + psymtab->statics_offset,
23778 psymtab->n_static_syms, cu_index,
23782 /* Closes FILE on scope exit. */
23785 explicit file_closer (FILE *file)
23790 { fclose (m_file); }
23796 /* Create an index file for OBJFILE in the directory DIR. */
23799 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23801 if (dwarf2_per_objfile->using_index)
23802 error (_("Cannot use an index to create the index"));
23804 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23805 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23807 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23811 if (stat (objfile_name (objfile), &st) < 0)
23812 perror_with_name (objfile_name (objfile));
23814 std::string filename (std::string (dir) + SLASH_STRING
23815 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
23817 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb");
23819 error (_("Can't open `%s' for writing"), filename.c_str ());
23821 /* Order matters here; we want FILE to be closed before FILENAME is
23822 unlinked, because on MS-Windows one cannot delete a file that is
23823 still open. (Don't call anything here that might throw until
23824 file_closer is created.) */
23825 gdb::unlinker unlink_file (filename.c_str ());
23826 file_closer close_out_file (out_file);
23828 mapped_symtab symtab;
23831 /* While we're scanning CU's create a table that maps a psymtab pointer
23832 (which is what addrmap records) to its index (which is what is recorded
23833 in the index file). This will later be needed to write the address
23835 psym_index_map cu_index_htab;
23836 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
23838 /* The CU list is already sorted, so we don't need to do additional
23839 work here. Also, the debug_types entries do not appear in
23840 all_comp_units, but only in their own hash table. */
23842 /* The psyms_seen set is potentially going to be largish (~40k
23843 elements when indexing a -g3 build of GDB itself). Estimate the
23844 number of elements in order to avoid too many rehashes, which
23845 require rebuilding buckets and thus many trips to
23847 size_t psyms_count = 0;
23848 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23850 struct dwarf2_per_cu_data *per_cu
23851 = dwarf2_per_objfile->all_comp_units[i];
23852 struct partial_symtab *psymtab = per_cu->v.psymtab;
23854 if (psymtab != NULL && psymtab->user == NULL)
23855 recursively_count_psymbols (psymtab, psyms_count);
23857 /* Generating an index for gdb itself shows a ratio of
23858 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23859 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
23860 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23862 struct dwarf2_per_cu_data *per_cu
23863 = dwarf2_per_objfile->all_comp_units[i];
23864 struct partial_symtab *psymtab = per_cu->v.psymtab;
23866 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23867 It may be referenced from a local scope but in such case it does not
23868 need to be present in .gdb_index. */
23869 if (psymtab == NULL)
23872 if (psymtab->user == NULL)
23873 recursively_write_psymbols (objfile, psymtab, &symtab,
23876 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23877 gdb_assert (insertpair.second);
23879 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23880 to_underlying (per_cu->sect_off));
23881 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
23884 /* Dump the address map. */
23886 write_address_map (objfile, addr_vec, cu_index_htab);
23888 /* Write out the .debug_type entries, if any. */
23889 data_buf types_cu_list;
23890 if (dwarf2_per_objfile->signatured_types)
23892 signatured_type_index_data sig_data (types_cu_list,
23895 sig_data.objfile = objfile;
23896 sig_data.symtab = &symtab;
23897 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23898 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23899 write_one_signatured_type, &sig_data);
23902 /* Now that we've processed all symbols we can shrink their cu_indices
23904 uniquify_cu_indices (&symtab);
23906 data_buf symtab_vec, constant_pool;
23907 write_hash_table (&symtab, symtab_vec, constant_pool);
23910 const offset_type size_of_contents = 6 * sizeof (offset_type);
23911 offset_type total_len = size_of_contents;
23913 /* The version number. */
23914 contents.append_data (MAYBE_SWAP (8));
23916 /* The offset of the CU list from the start of the file. */
23917 contents.append_data (MAYBE_SWAP (total_len));
23918 total_len += cu_list.size ();
23920 /* The offset of the types CU list from the start of the file. */
23921 contents.append_data (MAYBE_SWAP (total_len));
23922 total_len += types_cu_list.size ();
23924 /* The offset of the address table from the start of the file. */
23925 contents.append_data (MAYBE_SWAP (total_len));
23926 total_len += addr_vec.size ();
23928 /* The offset of the symbol table from the start of the file. */
23929 contents.append_data (MAYBE_SWAP (total_len));
23930 total_len += symtab_vec.size ();
23932 /* The offset of the constant pool from the start of the file. */
23933 contents.append_data (MAYBE_SWAP (total_len));
23934 total_len += constant_pool.size ();
23936 gdb_assert (contents.size () == size_of_contents);
23938 contents.file_write (out_file);
23939 cu_list.file_write (out_file);
23940 types_cu_list.file_write (out_file);
23941 addr_vec.file_write (out_file);
23942 symtab_vec.file_write (out_file);
23943 constant_pool.file_write (out_file);
23945 /* We want to keep the file. */
23946 unlink_file.keep ();
23949 /* Implementation of the `save gdb-index' command.
23951 Note that the file format used by this command is documented in the
23952 GDB manual. Any changes here must be documented there. */
23955 save_gdb_index_command (char *arg, int from_tty)
23957 struct objfile *objfile;
23960 error (_("usage: save gdb-index DIRECTORY"));
23962 ALL_OBJFILES (objfile)
23966 /* If the objfile does not correspond to an actual file, skip it. */
23967 if (stat (objfile_name (objfile), &st) < 0)
23971 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23972 dwarf2_objfile_data_key);
23973 if (dwarf2_per_objfile)
23978 write_psymtabs_to_index (objfile, arg);
23980 CATCH (except, RETURN_MASK_ERROR)
23982 exception_fprintf (gdb_stderr, except,
23983 _("Error while writing index for `%s': "),
23984 objfile_name (objfile));
23993 int dwarf_always_disassemble;
23996 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23997 struct cmd_list_element *c, const char *value)
23999 fprintf_filtered (file,
24000 _("Whether to always disassemble "
24001 "DWARF expressions is %s.\n"),
24006 show_check_physname (struct ui_file *file, int from_tty,
24007 struct cmd_list_element *c, const char *value)
24009 fprintf_filtered (file,
24010 _("Whether to check \"physname\" is %s.\n"),
24014 void _initialize_dwarf2_read (void);
24017 _initialize_dwarf2_read (void)
24019 struct cmd_list_element *c;
24021 dwarf2_objfile_data_key
24022 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24024 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24025 Set DWARF specific variables.\n\
24026 Configure DWARF variables such as the cache size"),
24027 &set_dwarf_cmdlist, "maintenance set dwarf ",
24028 0/*allow-unknown*/, &maintenance_set_cmdlist);
24030 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24031 Show DWARF specific variables\n\
24032 Show DWARF variables such as the cache size"),
24033 &show_dwarf_cmdlist, "maintenance show dwarf ",
24034 0/*allow-unknown*/, &maintenance_show_cmdlist);
24036 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24037 &dwarf_max_cache_age, _("\
24038 Set the upper bound on the age of cached DWARF compilation units."), _("\
24039 Show the upper bound on the age of cached DWARF compilation units."), _("\
24040 A higher limit means that cached compilation units will be stored\n\
24041 in memory longer, and more total memory will be used. Zero disables\n\
24042 caching, which can slow down startup."),
24044 show_dwarf_max_cache_age,
24045 &set_dwarf_cmdlist,
24046 &show_dwarf_cmdlist);
24048 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24049 &dwarf_always_disassemble, _("\
24050 Set whether `info address' always disassembles DWARF expressions."), _("\
24051 Show whether `info address' always disassembles DWARF expressions."), _("\
24052 When enabled, DWARF expressions are always printed in an assembly-like\n\
24053 syntax. When disabled, expressions will be printed in a more\n\
24054 conversational style, when possible."),
24056 show_dwarf_always_disassemble,
24057 &set_dwarf_cmdlist,
24058 &show_dwarf_cmdlist);
24060 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24061 Set debugging of the DWARF reader."), _("\
24062 Show debugging of the DWARF reader."), _("\
24063 When enabled (non-zero), debugging messages are printed during DWARF\n\
24064 reading and symtab expansion. A value of 1 (one) provides basic\n\
24065 information. A value greater than 1 provides more verbose information."),
24068 &setdebuglist, &showdebuglist);
24070 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24071 Set debugging of the DWARF DIE reader."), _("\
24072 Show debugging of the DWARF DIE reader."), _("\
24073 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24074 The value is the maximum depth to print."),
24077 &setdebuglist, &showdebuglist);
24079 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24080 Set debugging of the dwarf line reader."), _("\
24081 Show debugging of the dwarf line reader."), _("\
24082 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24083 A value of 1 (one) provides basic information.\n\
24084 A value greater than 1 provides more verbose information."),
24087 &setdebuglist, &showdebuglist);
24089 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24090 Set cross-checking of \"physname\" code against demangler."), _("\
24091 Show cross-checking of \"physname\" code against demangler."), _("\
24092 When enabled, GDB's internal \"physname\" code is checked against\n\
24094 NULL, show_check_physname,
24095 &setdebuglist, &showdebuglist);
24097 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24098 no_class, &use_deprecated_index_sections, _("\
24099 Set whether to use deprecated gdb_index sections."), _("\
24100 Show whether to use deprecated gdb_index sections."), _("\
24101 When enabled, deprecated .gdb_index sections are used anyway.\n\
24102 Normally they are ignored either because of a missing feature or\n\
24103 performance issue.\n\
24104 Warning: This option must be enabled before gdb reads the file."),
24107 &setlist, &showlist);
24109 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24111 Save a gdb-index file.\n\
24112 Usage: save gdb-index DIRECTORY"),
24114 set_cmd_completer (c, filename_completer);
24116 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24117 &dwarf2_locexpr_funcs);
24118 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24119 &dwarf2_loclist_funcs);
24121 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24122 &dwarf2_block_frame_base_locexpr_funcs);
24123 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24124 &dwarf2_block_frame_base_loclist_funcs);