1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
45 #include "complaints.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
55 #include "typeprint.h"
58 #include "completer.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
69 #include "filestuff.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "filename-seen-cache.h"
80 #include <sys/types.h>
82 #include <unordered_set>
83 #include <unordered_map>
85 typedef struct symbol *symbolp;
88 /* When == 1, print basic high level tracing messages.
89 When > 1, be more verbose.
90 This is in contrast to the low level DIE reading of dwarf_die_debug. */
91 static unsigned int dwarf_read_debug = 0;
93 /* When non-zero, dump DIEs after they are read in. */
94 static unsigned int dwarf_die_debug = 0;
96 /* When non-zero, dump line number entries as they are read in. */
97 static unsigned int dwarf_line_debug = 0;
99 /* When non-zero, cross-check physname against demangler. */
100 static int check_physname = 0;
102 /* When non-zero, do not reject deprecated .gdb_index sections. */
103 static int use_deprecated_index_sections = 0;
105 static const struct objfile_data *dwarf2_objfile_data_key;
107 /* The "aclass" indices for various kinds of computed DWARF symbols. */
109 static int dwarf2_locexpr_index;
110 static int dwarf2_loclist_index;
111 static int dwarf2_locexpr_block_index;
112 static int dwarf2_loclist_block_index;
114 /* A descriptor for dwarf sections.
116 S.ASECTION, SIZE are typically initialized when the objfile is first
117 scanned. BUFFER, READIN are filled in later when the section is read.
118 If the section contained compressed data then SIZE is updated to record
119 the uncompressed size of the section.
121 DWP file format V2 introduces a wrinkle that is easiest to handle by
122 creating the concept of virtual sections contained within a real section.
123 In DWP V2 the sections of the input DWO files are concatenated together
124 into one section, but section offsets are kept relative to the original
126 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
127 the real section this "virtual" section is contained in, and BUFFER,SIZE
128 describe the virtual section. */
130 struct dwarf2_section_info
134 /* If this is a real section, the bfd section. */
136 /* If this is a virtual section, pointer to the containing ("real")
138 struct dwarf2_section_info *containing_section;
140 /* Pointer to section data, only valid if readin. */
141 const gdb_byte *buffer;
142 /* The size of the section, real or virtual. */
144 /* If this is a virtual section, the offset in the real section.
145 Only valid if is_virtual. */
146 bfd_size_type virtual_offset;
147 /* True if we have tried to read this section. */
149 /* True if this is a virtual section, False otherwise.
150 This specifies which of s.section and s.containing_section to use. */
154 typedef struct dwarf2_section_info dwarf2_section_info_def;
155 DEF_VEC_O (dwarf2_section_info_def);
157 /* All offsets in the index are of this type. It must be
158 architecture-independent. */
159 typedef uint32_t offset_type;
161 DEF_VEC_I (offset_type);
163 /* Ensure only legit values are used. */
164 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
166 gdb_assert ((unsigned int) (value) <= 1); \
167 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 /* Ensure only legit values are used. */
171 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
173 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
174 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
175 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
179 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
181 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
182 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 /* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
189 /* Index data format version. */
192 /* The total length of the buffer. */
195 /* A pointer to the address table data. */
196 const gdb_byte *address_table;
198 /* Size of the address table data in bytes. */
199 offset_type address_table_size;
201 /* The symbol table, implemented as a hash table. */
202 const offset_type *symbol_table;
204 /* Size in slots, each slot is 2 offset_types. */
205 offset_type symbol_table_slots;
207 /* A pointer to the constant pool. */
208 const char *constant_pool;
211 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
212 DEF_VEC_P (dwarf2_per_cu_ptr);
216 int nr_uniq_abbrev_tables;
218 int nr_symtab_sharers;
219 int nr_stmt_less_type_units;
220 int nr_all_type_units_reallocs;
223 /* Collection of data recorded per objfile.
224 This hangs off of dwarf2_objfile_data_key. */
226 struct dwarf2_per_objfile
228 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
229 dwarf2 section names, or is NULL if the standard ELF names are
231 dwarf2_per_objfile (struct objfile *objfile,
232 const dwarf2_debug_sections *names);
234 ~dwarf2_per_objfile ();
236 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
238 /* Free all cached compilation units. */
239 void free_cached_comp_units ();
241 /* This function is mapped across the sections and remembers the
242 offset and size of each of the debugging sections we are
244 void locate_sections (bfd *abfd, asection *sectp,
245 const dwarf2_debug_sections &names);
248 dwarf2_section_info info {};
249 dwarf2_section_info abbrev {};
250 dwarf2_section_info line {};
251 dwarf2_section_info loc {};
252 dwarf2_section_info loclists {};
253 dwarf2_section_info macinfo {};
254 dwarf2_section_info macro {};
255 dwarf2_section_info str {};
256 dwarf2_section_info line_str {};
257 dwarf2_section_info ranges {};
258 dwarf2_section_info rnglists {};
259 dwarf2_section_info addr {};
260 dwarf2_section_info frame {};
261 dwarf2_section_info eh_frame {};
262 dwarf2_section_info gdb_index {};
264 VEC (dwarf2_section_info_def) *types = NULL;
267 struct objfile *objfile = NULL;
269 /* Table of all the compilation units. This is used to locate
270 the target compilation unit of a particular reference. */
271 struct dwarf2_per_cu_data **all_comp_units = NULL;
273 /* The number of compilation units in ALL_COMP_UNITS. */
274 int n_comp_units = 0;
276 /* The number of .debug_types-related CUs. */
277 int n_type_units = 0;
279 /* The number of elements allocated in all_type_units.
280 If there are skeleton-less TUs, we add them to all_type_units lazily. */
281 int n_allocated_type_units = 0;
283 /* The .debug_types-related CUs (TUs).
284 This is stored in malloc space because we may realloc it. */
285 struct signatured_type **all_type_units = NULL;
287 /* Table of struct type_unit_group objects.
288 The hash key is the DW_AT_stmt_list value. */
289 htab_t type_unit_groups {};
291 /* A table mapping .debug_types signatures to its signatured_type entry.
292 This is NULL if the .debug_types section hasn't been read in yet. */
293 htab_t signatured_types {};
295 /* Type unit statistics, to see how well the scaling improvements
297 struct tu_stats tu_stats {};
299 /* A chain of compilation units that are currently read in, so that
300 they can be freed later. */
301 dwarf2_per_cu_data *read_in_chain = NULL;
303 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
304 This is NULL if the table hasn't been allocated yet. */
307 /* True if we've checked for whether there is a DWP file. */
308 bool dwp_checked = false;
310 /* The DWP file if there is one, or NULL. */
311 struct dwp_file *dwp_file = NULL;
313 /* The shared '.dwz' file, if one exists. This is used when the
314 original data was compressed using 'dwz -m'. */
315 struct dwz_file *dwz_file = NULL;
317 /* A flag indicating whether this objfile has a section loaded at a
319 bool has_section_at_zero = false;
321 /* True if we are using the mapped index,
322 or we are faking it for OBJF_READNOW's sake. */
323 bool using_index = false;
325 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
326 mapped_index *index_table = NULL;
328 /* When using index_table, this keeps track of all quick_file_names entries.
329 TUs typically share line table entries with a CU, so we maintain a
330 separate table of all line table entries to support the sharing.
331 Note that while there can be way more TUs than CUs, we've already
332 sorted all the TUs into "type unit groups", grouped by their
333 DW_AT_stmt_list value. Therefore the only sharing done here is with a
334 CU and its associated TU group if there is one. */
335 htab_t quick_file_names_table {};
337 /* Set during partial symbol reading, to prevent queueing of full
339 bool reading_partial_symbols = false;
341 /* Table mapping type DIEs to their struct type *.
342 This is NULL if not allocated yet.
343 The mapping is done via (CU/TU + DIE offset) -> type. */
344 htab_t die_type_hash {};
346 /* The CUs we recently read. */
347 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
349 /* Table containing line_header indexed by offset and offset_in_dwz. */
350 htab_t line_header_hash {};
352 /* Table containing all filenames. This is an optional because the
353 table is lazily constructed on first access. */
354 gdb::optional<filename_seen_cache> filenames_cache;
357 static struct dwarf2_per_objfile *dwarf2_per_objfile;
359 /* Default names of the debugging sections. */
361 /* Note that if the debugging section has been compressed, it might
362 have a name like .zdebug_info. */
364 static const struct dwarf2_debug_sections dwarf2_elf_names =
366 { ".debug_info", ".zdebug_info" },
367 { ".debug_abbrev", ".zdebug_abbrev" },
368 { ".debug_line", ".zdebug_line" },
369 { ".debug_loc", ".zdebug_loc" },
370 { ".debug_loclists", ".zdebug_loclists" },
371 { ".debug_macinfo", ".zdebug_macinfo" },
372 { ".debug_macro", ".zdebug_macro" },
373 { ".debug_str", ".zdebug_str" },
374 { ".debug_line_str", ".zdebug_line_str" },
375 { ".debug_ranges", ".zdebug_ranges" },
376 { ".debug_rnglists", ".zdebug_rnglists" },
377 { ".debug_types", ".zdebug_types" },
378 { ".debug_addr", ".zdebug_addr" },
379 { ".debug_frame", ".zdebug_frame" },
380 { ".eh_frame", NULL },
381 { ".gdb_index", ".zgdb_index" },
385 /* List of DWO/DWP sections. */
387 static const struct dwop_section_names
389 struct dwarf2_section_names abbrev_dwo;
390 struct dwarf2_section_names info_dwo;
391 struct dwarf2_section_names line_dwo;
392 struct dwarf2_section_names loc_dwo;
393 struct dwarf2_section_names loclists_dwo;
394 struct dwarf2_section_names macinfo_dwo;
395 struct dwarf2_section_names macro_dwo;
396 struct dwarf2_section_names str_dwo;
397 struct dwarf2_section_names str_offsets_dwo;
398 struct dwarf2_section_names types_dwo;
399 struct dwarf2_section_names cu_index;
400 struct dwarf2_section_names tu_index;
404 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
405 { ".debug_info.dwo", ".zdebug_info.dwo" },
406 { ".debug_line.dwo", ".zdebug_line.dwo" },
407 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
408 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
409 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
410 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
411 { ".debug_str.dwo", ".zdebug_str.dwo" },
412 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
413 { ".debug_types.dwo", ".zdebug_types.dwo" },
414 { ".debug_cu_index", ".zdebug_cu_index" },
415 { ".debug_tu_index", ".zdebug_tu_index" },
418 /* local data types */
420 /* The data in a compilation unit header, after target2host
421 translation, looks like this. */
422 struct comp_unit_head
426 unsigned char addr_size;
427 unsigned char signed_addr_p;
428 sect_offset abbrev_sect_off;
430 /* Size of file offsets; either 4 or 8. */
431 unsigned int offset_size;
433 /* Size of the length field; either 4 or 12. */
434 unsigned int initial_length_size;
436 enum dwarf_unit_type unit_type;
438 /* Offset to the first byte of this compilation unit header in the
439 .debug_info section, for resolving relative reference dies. */
440 sect_offset sect_off;
442 /* Offset to first die in this cu from the start of the cu.
443 This will be the first byte following the compilation unit header. */
444 cu_offset first_die_cu_offset;
446 /* 64-bit signature of this type unit - it is valid only for
447 UNIT_TYPE DW_UT_type. */
450 /* For types, offset in the type's DIE of the type defined by this TU. */
451 cu_offset type_cu_offset_in_tu;
454 /* Type used for delaying computation of method physnames.
455 See comments for compute_delayed_physnames. */
456 struct delayed_method_info
458 /* The type to which the method is attached, i.e., its parent class. */
461 /* The index of the method in the type's function fieldlists. */
464 /* The index of the method in the fieldlist. */
467 /* The name of the DIE. */
470 /* The DIE associated with this method. */
471 struct die_info *die;
474 typedef struct delayed_method_info delayed_method_info;
475 DEF_VEC_O (delayed_method_info);
477 /* Internal state when decoding a particular compilation unit. */
480 /* The objfile containing this compilation unit. */
481 struct objfile *objfile;
483 /* The header of the compilation unit. */
484 struct comp_unit_head header;
486 /* Base address of this compilation unit. */
487 CORE_ADDR base_address;
489 /* Non-zero if base_address has been set. */
492 /* The language we are debugging. */
493 enum language language;
494 const struct language_defn *language_defn;
496 const char *producer;
498 /* The generic symbol table building routines have separate lists for
499 file scope symbols and all all other scopes (local scopes). So
500 we need to select the right one to pass to add_symbol_to_list().
501 We do it by keeping a pointer to the correct list in list_in_scope.
503 FIXME: The original dwarf code just treated the file scope as the
504 first local scope, and all other local scopes as nested local
505 scopes, and worked fine. Check to see if we really need to
506 distinguish these in buildsym.c. */
507 struct pending **list_in_scope;
509 /* The abbrev table for this CU.
510 Normally this points to the abbrev table in the objfile.
511 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
512 struct abbrev_table *abbrev_table;
514 /* Hash table holding all the loaded partial DIEs
515 with partial_die->offset.SECT_OFF as hash. */
518 /* Storage for things with the same lifetime as this read-in compilation
519 unit, including partial DIEs. */
520 struct obstack comp_unit_obstack;
522 /* When multiple dwarf2_cu structures are living in memory, this field
523 chains them all together, so that they can be released efficiently.
524 We will probably also want a generation counter so that most-recently-used
525 compilation units are cached... */
526 struct dwarf2_per_cu_data *read_in_chain;
528 /* Backlink to our per_cu entry. */
529 struct dwarf2_per_cu_data *per_cu;
531 /* How many compilation units ago was this CU last referenced? */
534 /* A hash table of DIE cu_offset for following references with
535 die_info->offset.sect_off as hash. */
538 /* Full DIEs if read in. */
539 struct die_info *dies;
541 /* A set of pointers to dwarf2_per_cu_data objects for compilation
542 units referenced by this one. Only set during full symbol processing;
543 partial symbol tables do not have dependencies. */
546 /* Header data from the line table, during full symbol processing. */
547 struct line_header *line_header;
548 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
549 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
550 this is the DW_TAG_compile_unit die for this CU. We'll hold on
551 to the line header as long as this DIE is being processed. See
552 process_die_scope. */
553 die_info *line_header_die_owner;
555 /* A list of methods which need to have physnames computed
556 after all type information has been read. */
557 VEC (delayed_method_info) *method_list;
559 /* To be copied to symtab->call_site_htab. */
560 htab_t call_site_htab;
562 /* Non-NULL if this CU came from a DWO file.
563 There is an invariant here that is important to remember:
564 Except for attributes copied from the top level DIE in the "main"
565 (or "stub") file in preparation for reading the DWO file
566 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
567 Either there isn't a DWO file (in which case this is NULL and the point
568 is moot), or there is and either we're not going to read it (in which
569 case this is NULL) or there is and we are reading it (in which case this
571 struct dwo_unit *dwo_unit;
573 /* The DW_AT_addr_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. */
578 /* The DW_AT_ranges_base attribute if present, zero otherwise
579 (zero is a valid value though).
580 Note this value comes from the Fission stub CU/TU's DIE.
581 Also note that the value is zero in the non-DWO case so this value can
582 be used without needing to know whether DWO files are in use or not.
583 N.B. This does not apply to DW_AT_ranges appearing in
584 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
585 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
586 DW_AT_ranges_base *would* have to be applied, and we'd have to care
587 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
588 ULONGEST ranges_base;
590 /* Mark used when releasing cached dies. */
591 unsigned int mark : 1;
593 /* This CU references .debug_loc. See the symtab->locations_valid field.
594 This test is imperfect as there may exist optimized debug code not using
595 any location list and still facing inlining issues if handled as
596 unoptimized code. For a future better test see GCC PR other/32998. */
597 unsigned int has_loclist : 1;
599 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
600 if all the producer_is_* fields are valid. This information is cached
601 because profiling CU expansion showed excessive time spent in
602 producer_is_gxx_lt_4_6. */
603 unsigned int checked_producer : 1;
604 unsigned int producer_is_gxx_lt_4_6 : 1;
605 unsigned int producer_is_gcc_lt_4_3 : 1;
606 unsigned int producer_is_icc_lt_14 : 1;
608 /* When set, the file that we're processing is known to have
609 debugging info for C++ namespaces. GCC 3.3.x did not produce
610 this information, but later versions do. */
612 unsigned int processing_has_namespace_info : 1;
615 /* Persistent data held for a compilation unit, even when not
616 processing it. We put a pointer to this structure in the
617 read_symtab_private field of the psymtab. */
619 struct dwarf2_per_cu_data
621 /* The start offset and length of this compilation unit.
622 NOTE: Unlike comp_unit_head.length, this length includes
624 If the DIE refers to a DWO file, this is always of the original die,
626 sect_offset sect_off;
629 /* DWARF standard version this data has been read from (such as 4 or 5). */
632 /* Flag indicating this compilation unit will be read in before
633 any of the current compilation units are processed. */
634 unsigned int queued : 1;
636 /* This flag will be set when reading partial DIEs if we need to load
637 absolutely all DIEs for this compilation unit, instead of just the ones
638 we think are interesting. It gets set if we look for a DIE in the
639 hash table and don't find it. */
640 unsigned int load_all_dies : 1;
642 /* Non-zero if this CU is from .debug_types.
643 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
645 unsigned int is_debug_types : 1;
647 /* Non-zero if this CU is from the .dwz file. */
648 unsigned int is_dwz : 1;
650 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
651 This flag is only valid if is_debug_types is true.
652 We can't read a CU directly from a DWO file: There are required
653 attributes in the stub. */
654 unsigned int reading_dwo_directly : 1;
656 /* Non-zero if the TU has been read.
657 This is used to assist the "Stay in DWO Optimization" for Fission:
658 When reading a DWO, it's faster to read TUs from the DWO instead of
659 fetching them from random other DWOs (due to comdat folding).
660 If the TU has already been read, the optimization is unnecessary
661 (and unwise - we don't want to change where gdb thinks the TU lives
663 This flag is only valid if is_debug_types is true. */
664 unsigned int tu_read : 1;
666 /* The section this CU/TU lives in.
667 If the DIE refers to a DWO file, this is always the original die,
669 struct dwarf2_section_info *section;
671 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
672 of the CU cache it gets reset to NULL again. This is left as NULL for
673 dummy CUs (a CU header, but nothing else). */
674 struct dwarf2_cu *cu;
676 /* The corresponding objfile.
677 Normally we can get the objfile from dwarf2_per_objfile.
678 However we can enter this file with just a "per_cu" handle. */
679 struct objfile *objfile;
681 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
682 is active. Otherwise, the 'psymtab' field is active. */
685 /* The partial symbol table associated with this compilation unit,
686 or NULL for unread partial units. */
687 struct partial_symtab *psymtab;
689 /* Data needed by the "quick" functions. */
690 struct dwarf2_per_cu_quick_data *quick;
693 /* The CUs we import using DW_TAG_imported_unit. This is filled in
694 while reading psymtabs, used to compute the psymtab dependencies,
695 and then cleared. Then it is filled in again while reading full
696 symbols, and only deleted when the objfile is destroyed.
698 This is also used to work around a difference between the way gold
699 generates .gdb_index version <=7 and the way gdb does. Arguably this
700 is a gold bug. For symbols coming from TUs, gold records in the index
701 the CU that includes the TU instead of the TU itself. This breaks
702 dw2_lookup_symbol: It assumes that if the index says symbol X lives
703 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
704 will find X. Alas TUs live in their own symtab, so after expanding CU Y
705 we need to look in TU Z to find X. Fortunately, this is akin to
706 DW_TAG_imported_unit, so we just use the same mechanism: For
707 .gdb_index version <=7 this also records the TUs that the CU referred
708 to. Concurrently with this change gdb was modified to emit version 8
709 indices so we only pay a price for gold generated indices.
710 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
711 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
714 /* Entry in the signatured_types hash table. */
716 struct signatured_type
718 /* The "per_cu" object of this type.
719 This struct is used iff per_cu.is_debug_types.
720 N.B.: This is the first member so that it's easy to convert pointers
722 struct dwarf2_per_cu_data per_cu;
724 /* The type's signature. */
727 /* Offset in the TU of the type's DIE, as read from the TU header.
728 If this TU is a DWO stub and the definition lives in a DWO file
729 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
730 cu_offset type_offset_in_tu;
732 /* Offset in the section of the type's DIE.
733 If the definition lives in a DWO file, this is the offset in the
734 .debug_types.dwo section.
735 The value is zero until the actual value is known.
736 Zero is otherwise not a valid section offset. */
737 sect_offset type_offset_in_section;
739 /* Type units are grouped by their DW_AT_stmt_list entry so that they
740 can share them. This points to the containing symtab. */
741 struct type_unit_group *type_unit_group;
744 The first time we encounter this type we fully read it in and install it
745 in the symbol tables. Subsequent times we only need the type. */
748 /* Containing DWO unit.
749 This field is valid iff per_cu.reading_dwo_directly. */
750 struct dwo_unit *dwo_unit;
753 typedef struct signatured_type *sig_type_ptr;
754 DEF_VEC_P (sig_type_ptr);
756 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
757 This includes type_unit_group and quick_file_names. */
759 struct stmt_list_hash
761 /* The DWO unit this table is from or NULL if there is none. */
762 struct dwo_unit *dwo_unit;
764 /* Offset in .debug_line or .debug_line.dwo. */
765 sect_offset line_sect_off;
768 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
769 an object of this type. */
771 struct type_unit_group
773 /* dwarf2read.c's main "handle" on a TU symtab.
774 To simplify things we create an artificial CU that "includes" all the
775 type units using this stmt_list so that the rest of the code still has
776 a "per_cu" handle on the symtab.
777 This PER_CU is recognized by having no section. */
778 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
779 struct dwarf2_per_cu_data per_cu;
781 /* The TUs that share this DW_AT_stmt_list entry.
782 This is added to while parsing type units to build partial symtabs,
783 and is deleted afterwards and not used again. */
784 VEC (sig_type_ptr) *tus;
786 /* The compunit symtab.
787 Type units in a group needn't all be defined in the same source file,
788 so we create an essentially anonymous symtab as the compunit symtab. */
789 struct compunit_symtab *compunit_symtab;
791 /* The data used to construct the hash key. */
792 struct stmt_list_hash hash;
794 /* The number of symtabs from the line header.
795 The value here must match line_header.num_file_names. */
796 unsigned int num_symtabs;
798 /* The symbol tables for this TU (obtained from the files listed in
800 WARNING: The order of entries here must match the order of entries
801 in the line header. After the first TU using this type_unit_group, the
802 line header for the subsequent TUs is recreated from this. This is done
803 because we need to use the same symtabs for each TU using the same
804 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
805 there's no guarantee the line header doesn't have duplicate entries. */
806 struct symtab **symtabs;
809 /* These sections are what may appear in a (real or virtual) DWO file. */
813 struct dwarf2_section_info abbrev;
814 struct dwarf2_section_info line;
815 struct dwarf2_section_info loc;
816 struct dwarf2_section_info loclists;
817 struct dwarf2_section_info macinfo;
818 struct dwarf2_section_info macro;
819 struct dwarf2_section_info str;
820 struct dwarf2_section_info str_offsets;
821 /* In the case of a virtual DWO file, these two are unused. */
822 struct dwarf2_section_info info;
823 VEC (dwarf2_section_info_def) *types;
826 /* CUs/TUs in DWP/DWO files. */
830 /* Backlink to the containing struct dwo_file. */
831 struct dwo_file *dwo_file;
833 /* The "id" that distinguishes this CU/TU.
834 .debug_info calls this "dwo_id", .debug_types calls this "signature".
835 Since signatures came first, we stick with it for consistency. */
838 /* The section this CU/TU lives in, in the DWO file. */
839 struct dwarf2_section_info *section;
841 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
842 sect_offset sect_off;
845 /* For types, offset in the type's DIE of the type defined by this TU. */
846 cu_offset type_offset_in_tu;
849 /* include/dwarf2.h defines the DWP section codes.
850 It defines a max value but it doesn't define a min value, which we
851 use for error checking, so provide one. */
853 enum dwp_v2_section_ids
858 /* Data for one DWO file.
860 This includes virtual DWO files (a virtual DWO file is a DWO file as it
861 appears in a DWP file). DWP files don't really have DWO files per se -
862 comdat folding of types "loses" the DWO file they came from, and from
863 a high level view DWP files appear to contain a mass of random types.
864 However, to maintain consistency with the non-DWP case we pretend DWP
865 files contain virtual DWO files, and we assign each TU with one virtual
866 DWO file (generally based on the line and abbrev section offsets -
867 a heuristic that seems to work in practice). */
871 /* The DW_AT_GNU_dwo_name attribute.
872 For virtual DWO files the name is constructed from the section offsets
873 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
874 from related CU+TUs. */
875 const char *dwo_name;
877 /* The DW_AT_comp_dir attribute. */
878 const char *comp_dir;
880 /* The bfd, when the file is open. Otherwise this is NULL.
881 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
884 /* The sections that make up this DWO file.
885 Remember that for virtual DWO files in DWP V2, these are virtual
886 sections (for lack of a better name). */
887 struct dwo_sections sections;
889 /* The CUs in the file.
890 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
891 an extension to handle LLVM's Link Time Optimization output (where
892 multiple source files may be compiled into a single object/dwo pair). */
895 /* Table of TUs in the file.
896 Each element is a struct dwo_unit. */
900 /* These sections are what may appear in a DWP file. */
904 /* These are used by both DWP version 1 and 2. */
905 struct dwarf2_section_info str;
906 struct dwarf2_section_info cu_index;
907 struct dwarf2_section_info tu_index;
909 /* These are only used by DWP version 2 files.
910 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
911 sections are referenced by section number, and are not recorded here.
912 In DWP version 2 there is at most one copy of all these sections, each
913 section being (effectively) comprised of the concatenation of all of the
914 individual sections that exist in the version 1 format.
915 To keep the code simple we treat each of these concatenated pieces as a
916 section itself (a virtual section?). */
917 struct dwarf2_section_info abbrev;
918 struct dwarf2_section_info info;
919 struct dwarf2_section_info line;
920 struct dwarf2_section_info loc;
921 struct dwarf2_section_info macinfo;
922 struct dwarf2_section_info macro;
923 struct dwarf2_section_info str_offsets;
924 struct dwarf2_section_info types;
927 /* These sections are what may appear in a virtual DWO file in DWP version 1.
928 A virtual DWO file is a DWO file as it appears in a DWP file. */
930 struct virtual_v1_dwo_sections
932 struct dwarf2_section_info abbrev;
933 struct dwarf2_section_info line;
934 struct dwarf2_section_info loc;
935 struct dwarf2_section_info macinfo;
936 struct dwarf2_section_info macro;
937 struct dwarf2_section_info str_offsets;
938 /* Each DWP hash table entry records one CU or one TU.
939 That is recorded here, and copied to dwo_unit.section. */
940 struct dwarf2_section_info info_or_types;
943 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
944 In version 2, the sections of the DWO files are concatenated together
945 and stored in one section of that name. Thus each ELF section contains
946 several "virtual" sections. */
948 struct virtual_v2_dwo_sections
950 bfd_size_type abbrev_offset;
951 bfd_size_type abbrev_size;
953 bfd_size_type line_offset;
954 bfd_size_type line_size;
956 bfd_size_type loc_offset;
957 bfd_size_type loc_size;
959 bfd_size_type macinfo_offset;
960 bfd_size_type macinfo_size;
962 bfd_size_type macro_offset;
963 bfd_size_type macro_size;
965 bfd_size_type str_offsets_offset;
966 bfd_size_type str_offsets_size;
968 /* Each DWP hash table entry records one CU or one TU.
969 That is recorded here, and copied to dwo_unit.section. */
970 bfd_size_type info_or_types_offset;
971 bfd_size_type info_or_types_size;
974 /* Contents of DWP hash tables. */
976 struct dwp_hash_table
978 uint32_t version, nr_columns;
979 uint32_t nr_units, nr_slots;
980 const gdb_byte *hash_table, *unit_table;
985 const gdb_byte *indices;
989 /* This is indexed by column number and gives the id of the section
991 #define MAX_NR_V2_DWO_SECTIONS \
992 (1 /* .debug_info or .debug_types */ \
993 + 1 /* .debug_abbrev */ \
994 + 1 /* .debug_line */ \
995 + 1 /* .debug_loc */ \
996 + 1 /* .debug_str_offsets */ \
997 + 1 /* .debug_macro or .debug_macinfo */)
998 int section_ids[MAX_NR_V2_DWO_SECTIONS];
999 const gdb_byte *offsets;
1000 const gdb_byte *sizes;
1005 /* Data for one DWP file. */
1009 /* Name of the file. */
1012 /* File format version. */
1018 /* Section info for this file. */
1019 struct dwp_sections sections;
1021 /* Table of CUs in the file. */
1022 const struct dwp_hash_table *cus;
1024 /* Table of TUs in the file. */
1025 const struct dwp_hash_table *tus;
1027 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1031 /* Table to map ELF section numbers to their sections.
1032 This is only needed for the DWP V1 file format. */
1033 unsigned int num_sections;
1034 asection **elf_sections;
1037 /* This represents a '.dwz' file. */
1041 /* A dwz file can only contain a few sections. */
1042 struct dwarf2_section_info abbrev;
1043 struct dwarf2_section_info info;
1044 struct dwarf2_section_info str;
1045 struct dwarf2_section_info line;
1046 struct dwarf2_section_info macro;
1047 struct dwarf2_section_info gdb_index;
1049 /* The dwz's BFD. */
1053 /* Struct used to pass misc. parameters to read_die_and_children, et
1054 al. which are used for both .debug_info and .debug_types dies.
1055 All parameters here are unchanging for the life of the call. This
1056 struct exists to abstract away the constant parameters of die reading. */
1058 struct die_reader_specs
1060 /* The bfd of die_section. */
1063 /* The CU of the DIE we are parsing. */
1064 struct dwarf2_cu *cu;
1066 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1067 struct dwo_file *dwo_file;
1069 /* The section the die comes from.
1070 This is either .debug_info or .debug_types, or the .dwo variants. */
1071 struct dwarf2_section_info *die_section;
1073 /* die_section->buffer. */
1074 const gdb_byte *buffer;
1076 /* The end of the buffer. */
1077 const gdb_byte *buffer_end;
1079 /* The value of the DW_AT_comp_dir attribute. */
1080 const char *comp_dir;
1083 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1084 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1085 const gdb_byte *info_ptr,
1086 struct die_info *comp_unit_die,
1090 /* A 1-based directory index. This is a strong typedef to prevent
1091 accidentally using a directory index as a 0-based index into an
1093 enum class dir_index : unsigned int {};
1095 /* Likewise, a 1-based file name index. */
1096 enum class file_name_index : unsigned int {};
1100 file_entry () = default;
1102 file_entry (const char *name_, dir_index d_index_,
1103 unsigned int mod_time_, unsigned int length_)
1106 mod_time (mod_time_),
1110 /* Return the include directory at D_INDEX stored in LH. Returns
1111 NULL if D_INDEX is out of bounds. */
1112 const char *include_dir (const line_header *lh) const;
1114 /* The file name. Note this is an observing pointer. The memory is
1115 owned by debug_line_buffer. */
1116 const char *name {};
1118 /* The directory index (1-based). */
1119 dir_index d_index {};
1121 unsigned int mod_time {};
1123 unsigned int length {};
1125 /* True if referenced by the Line Number Program. */
1128 /* The associated symbol table, if any. */
1129 struct symtab *symtab {};
1132 /* The line number information for a compilation unit (found in the
1133 .debug_line section) begins with a "statement program header",
1134 which contains the following information. */
1141 /* Add an entry to the include directory table. */
1142 void add_include_dir (const char *include_dir);
1144 /* Add an entry to the file name table. */
1145 void add_file_name (const char *name, dir_index d_index,
1146 unsigned int mod_time, unsigned int length);
1148 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1149 is out of bounds. */
1150 const char *include_dir_at (dir_index index) const
1152 /* Convert directory index number (1-based) to vector index
1154 size_t vec_index = to_underlying (index) - 1;
1156 if (vec_index >= include_dirs.size ())
1158 return include_dirs[vec_index];
1161 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1162 is out of bounds. */
1163 file_entry *file_name_at (file_name_index index)
1165 /* Convert file name index number (1-based) to vector index
1167 size_t vec_index = to_underlying (index) - 1;
1169 if (vec_index >= file_names.size ())
1171 return &file_names[vec_index];
1174 /* Const version of the above. */
1175 const file_entry *file_name_at (unsigned int index) const
1177 if (index >= file_names.size ())
1179 return &file_names[index];
1182 /* Offset of line number information in .debug_line section. */
1183 sect_offset sect_off {};
1185 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1186 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1188 unsigned int total_length {};
1189 unsigned short version {};
1190 unsigned int header_length {};
1191 unsigned char minimum_instruction_length {};
1192 unsigned char maximum_ops_per_instruction {};
1193 unsigned char default_is_stmt {};
1195 unsigned char line_range {};
1196 unsigned char opcode_base {};
1198 /* standard_opcode_lengths[i] is the number of operands for the
1199 standard opcode whose value is i. This means that
1200 standard_opcode_lengths[0] is unused, and the last meaningful
1201 element is standard_opcode_lengths[opcode_base - 1]. */
1202 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1204 /* The include_directories table. Note these are observing
1205 pointers. The memory is owned by debug_line_buffer. */
1206 std::vector<const char *> include_dirs;
1208 /* The file_names table. */
1209 std::vector<file_entry> file_names;
1211 /* The start and end of the statement program following this
1212 header. These point into dwarf2_per_objfile->line_buffer. */
1213 const gdb_byte *statement_program_start {}, *statement_program_end {};
1216 typedef std::unique_ptr<line_header> line_header_up;
1219 file_entry::include_dir (const line_header *lh) const
1221 return lh->include_dir_at (d_index);
1224 /* When we construct a partial symbol table entry we only
1225 need this much information. */
1226 struct partial_die_info
1228 /* Offset of this DIE. */
1229 sect_offset sect_off;
1231 /* DWARF-2 tag for this DIE. */
1232 ENUM_BITFIELD(dwarf_tag) tag : 16;
1234 /* Assorted flags describing the data found in this DIE. */
1235 unsigned int has_children : 1;
1236 unsigned int is_external : 1;
1237 unsigned int is_declaration : 1;
1238 unsigned int has_type : 1;
1239 unsigned int has_specification : 1;
1240 unsigned int has_pc_info : 1;
1241 unsigned int may_be_inlined : 1;
1243 /* This DIE has been marked DW_AT_main_subprogram. */
1244 unsigned int main_subprogram : 1;
1246 /* Flag set if the SCOPE field of this structure has been
1248 unsigned int scope_set : 1;
1250 /* Flag set if the DIE has a byte_size attribute. */
1251 unsigned int has_byte_size : 1;
1253 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1254 unsigned int has_const_value : 1;
1256 /* Flag set if any of the DIE's children are template arguments. */
1257 unsigned int has_template_arguments : 1;
1259 /* Flag set if fixup_partial_die has been called on this die. */
1260 unsigned int fixup_called : 1;
1262 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1263 unsigned int is_dwz : 1;
1265 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1266 unsigned int spec_is_dwz : 1;
1268 /* The name of this DIE. Normally the value of DW_AT_name, but
1269 sometimes a default name for unnamed DIEs. */
1272 /* The linkage name, if present. */
1273 const char *linkage_name;
1275 /* The scope to prepend to our children. This is generally
1276 allocated on the comp_unit_obstack, so will disappear
1277 when this compilation unit leaves the cache. */
1280 /* Some data associated with the partial DIE. The tag determines
1281 which field is live. */
1284 /* The location description associated with this DIE, if any. */
1285 struct dwarf_block *locdesc;
1286 /* The offset of an import, for DW_TAG_imported_unit. */
1287 sect_offset sect_off;
1290 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1294 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1295 DW_AT_sibling, if any. */
1296 /* NOTE: This member isn't strictly necessary, read_partial_die could
1297 return DW_AT_sibling values to its caller load_partial_dies. */
1298 const gdb_byte *sibling;
1300 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1301 DW_AT_specification (or DW_AT_abstract_origin or
1302 DW_AT_extension). */
1303 sect_offset spec_offset;
1305 /* Pointers to this DIE's parent, first child, and next sibling,
1307 struct partial_die_info *die_parent, *die_child, *die_sibling;
1310 /* This data structure holds the information of an abbrev. */
1313 unsigned int number; /* number identifying abbrev */
1314 enum dwarf_tag tag; /* dwarf tag */
1315 unsigned short has_children; /* boolean */
1316 unsigned short num_attrs; /* number of attributes */
1317 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1318 struct abbrev_info *next; /* next in chain */
1323 ENUM_BITFIELD(dwarf_attribute) name : 16;
1324 ENUM_BITFIELD(dwarf_form) form : 16;
1326 /* It is valid only if FORM is DW_FORM_implicit_const. */
1327 LONGEST implicit_const;
1330 /* Size of abbrev_table.abbrev_hash_table. */
1331 #define ABBREV_HASH_SIZE 121
1333 /* Top level data structure to contain an abbreviation table. */
1337 /* Where the abbrev table came from.
1338 This is used as a sanity check when the table is used. */
1339 sect_offset sect_off;
1341 /* Storage for the abbrev table. */
1342 struct obstack abbrev_obstack;
1344 /* Hash table of abbrevs.
1345 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1346 It could be statically allocated, but the previous code didn't so we
1348 struct abbrev_info **abbrevs;
1351 /* Attributes have a name and a value. */
1354 ENUM_BITFIELD(dwarf_attribute) name : 16;
1355 ENUM_BITFIELD(dwarf_form) form : 15;
1357 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1358 field should be in u.str (existing only for DW_STRING) but it is kept
1359 here for better struct attribute alignment. */
1360 unsigned int string_is_canonical : 1;
1365 struct dwarf_block *blk;
1374 /* This data structure holds a complete die structure. */
1377 /* DWARF-2 tag for this DIE. */
1378 ENUM_BITFIELD(dwarf_tag) tag : 16;
1380 /* Number of attributes */
1381 unsigned char num_attrs;
1383 /* True if we're presently building the full type name for the
1384 type derived from this DIE. */
1385 unsigned char building_fullname : 1;
1387 /* True if this die is in process. PR 16581. */
1388 unsigned char in_process : 1;
1391 unsigned int abbrev;
1393 /* Offset in .debug_info or .debug_types section. */
1394 sect_offset sect_off;
1396 /* The dies in a compilation unit form an n-ary tree. PARENT
1397 points to this die's parent; CHILD points to the first child of
1398 this node; and all the children of a given node are chained
1399 together via their SIBLING fields. */
1400 struct die_info *child; /* Its first child, if any. */
1401 struct die_info *sibling; /* Its next sibling, if any. */
1402 struct die_info *parent; /* Its parent, if any. */
1404 /* An array of attributes, with NUM_ATTRS elements. There may be
1405 zero, but it's not common and zero-sized arrays are not
1406 sufficiently portable C. */
1407 struct attribute attrs[1];
1410 /* Get at parts of an attribute structure. */
1412 #define DW_STRING(attr) ((attr)->u.str)
1413 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1414 #define DW_UNSND(attr) ((attr)->u.unsnd)
1415 #define DW_BLOCK(attr) ((attr)->u.blk)
1416 #define DW_SND(attr) ((attr)->u.snd)
1417 #define DW_ADDR(attr) ((attr)->u.addr)
1418 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1420 /* Blocks are a bunch of untyped bytes. */
1425 /* Valid only if SIZE is not zero. */
1426 const gdb_byte *data;
1429 #ifndef ATTR_ALLOC_CHUNK
1430 #define ATTR_ALLOC_CHUNK 4
1433 /* Allocate fields for structs, unions and enums in this size. */
1434 #ifndef DW_FIELD_ALLOC_CHUNK
1435 #define DW_FIELD_ALLOC_CHUNK 4
1438 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1439 but this would require a corresponding change in unpack_field_as_long
1441 static int bits_per_byte = 8;
1445 struct nextfield *next;
1453 struct nextfnfield *next;
1454 struct fn_field fnfield;
1461 struct nextfnfield *head;
1464 struct typedef_field_list
1466 struct typedef_field field;
1467 struct typedef_field_list *next;
1470 /* The routines that read and process dies for a C struct or C++ class
1471 pass lists of data member fields and lists of member function fields
1472 in an instance of a field_info structure, as defined below. */
1475 /* List of data member and baseclasses fields. */
1476 struct nextfield *fields, *baseclasses;
1478 /* Number of fields (including baseclasses). */
1481 /* Number of baseclasses. */
1484 /* Set if the accesibility of one of the fields is not public. */
1485 int non_public_fields;
1487 /* Member function fieldlist array, contains name of possibly overloaded
1488 member function, number of overloaded member functions and a pointer
1489 to the head of the member function field chain. */
1490 struct fnfieldlist *fnfieldlists;
1492 /* Number of entries in the fnfieldlists array. */
1495 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1496 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1497 struct typedef_field_list *typedef_field_list;
1498 unsigned typedef_field_list_count;
1501 /* One item on the queue of compilation units to read in full symbols
1503 struct dwarf2_queue_item
1505 struct dwarf2_per_cu_data *per_cu;
1506 enum language pretend_language;
1507 struct dwarf2_queue_item *next;
1510 /* The current queue. */
1511 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1513 /* Loaded secondary compilation units are kept in memory until they
1514 have not been referenced for the processing of this many
1515 compilation units. Set this to zero to disable caching. Cache
1516 sizes of up to at least twenty will improve startup time for
1517 typical inter-CU-reference binaries, at an obvious memory cost. */
1518 static int dwarf_max_cache_age = 5;
1520 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1521 struct cmd_list_element *c, const char *value)
1523 fprintf_filtered (file, _("The upper bound on the age of cached "
1524 "DWARF compilation units is %s.\n"),
1528 /* local function prototypes */
1530 static const char *get_section_name (const struct dwarf2_section_info *);
1532 static const char *get_section_file_name (const struct dwarf2_section_info *);
1534 static void dwarf2_find_base_address (struct die_info *die,
1535 struct dwarf2_cu *cu);
1537 static struct partial_symtab *create_partial_symtab
1538 (struct dwarf2_per_cu_data *per_cu, const char *name);
1540 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct die_info *type_unit_die,
1543 int has_children, void *data);
1545 static void dwarf2_build_psymtabs_hard (struct objfile *);
1547 static void scan_partial_symbols (struct partial_die_info *,
1548 CORE_ADDR *, CORE_ADDR *,
1549 int, struct dwarf2_cu *);
1551 static void add_partial_symbol (struct partial_die_info *,
1552 struct dwarf2_cu *);
1554 static void add_partial_namespace (struct partial_die_info *pdi,
1555 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1556 int set_addrmap, struct dwarf2_cu *cu);
1558 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1559 CORE_ADDR *highpc, int set_addrmap,
1560 struct dwarf2_cu *cu);
1562 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1563 struct dwarf2_cu *cu);
1565 static void add_partial_subprogram (struct partial_die_info *pdi,
1566 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1567 int need_pc, struct dwarf2_cu *cu);
1569 static void dwarf2_read_symtab (struct partial_symtab *,
1572 static void psymtab_to_symtab_1 (struct partial_symtab *);
1574 static struct abbrev_info *abbrev_table_lookup_abbrev
1575 (const struct abbrev_table *, unsigned int);
1577 static struct abbrev_table *abbrev_table_read_table
1578 (struct dwarf2_section_info *, sect_offset);
1580 static void abbrev_table_free (struct abbrev_table *);
1582 static void abbrev_table_free_cleanup (void *);
1584 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1585 struct dwarf2_section_info *);
1587 static void dwarf2_free_abbrev_table (void *);
1589 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1591 static struct partial_die_info *load_partial_dies
1592 (const struct die_reader_specs *, const gdb_byte *, int);
1594 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1595 struct partial_die_info *,
1596 struct abbrev_info *,
1600 static struct partial_die_info *find_partial_die (sect_offset, int,
1601 struct dwarf2_cu *);
1603 static void fixup_partial_die (struct partial_die_info *,
1604 struct dwarf2_cu *);
1606 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1607 struct attribute *, struct attr_abbrev *,
1610 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1612 static int read_1_signed_byte (bfd *, const gdb_byte *);
1614 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1616 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1618 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1620 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1623 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1625 static LONGEST read_checked_initial_length_and_offset
1626 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1627 unsigned int *, unsigned int *);
1629 static LONGEST read_offset (bfd *, const gdb_byte *,
1630 const struct comp_unit_head *,
1633 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1635 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1638 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1640 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1642 static const char *read_indirect_string (bfd *, const gdb_byte *,
1643 const struct comp_unit_head *,
1646 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1647 const struct comp_unit_head *,
1650 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1652 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1654 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1658 static const char *read_str_index (const struct die_reader_specs *reader,
1659 ULONGEST str_index);
1661 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1663 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1664 struct dwarf2_cu *);
1666 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1669 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1670 struct dwarf2_cu *cu);
1672 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1673 struct dwarf2_cu *cu);
1675 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1677 static struct die_info *die_specification (struct die_info *die,
1678 struct dwarf2_cu **);
1680 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1681 struct dwarf2_cu *cu);
1683 static void dwarf_decode_lines (struct line_header *, const char *,
1684 struct dwarf2_cu *, struct partial_symtab *,
1685 CORE_ADDR, int decode_mapping);
1687 static void dwarf2_start_subfile (const char *, const char *);
1689 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1690 const char *, const char *,
1693 static struct symbol *new_symbol (struct die_info *, struct type *,
1694 struct dwarf2_cu *);
1696 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1697 struct dwarf2_cu *, struct symbol *);
1699 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1700 struct dwarf2_cu *);
1702 static void dwarf2_const_value_attr (const struct attribute *attr,
1705 struct obstack *obstack,
1706 struct dwarf2_cu *cu, LONGEST *value,
1707 const gdb_byte **bytes,
1708 struct dwarf2_locexpr_baton **baton);
1710 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1712 static int need_gnat_info (struct dwarf2_cu *);
1714 static struct type *die_descriptive_type (struct die_info *,
1715 struct dwarf2_cu *);
1717 static void set_descriptive_type (struct type *, struct die_info *,
1718 struct dwarf2_cu *);
1720 static struct type *die_containing_type (struct die_info *,
1721 struct dwarf2_cu *);
1723 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1724 struct dwarf2_cu *);
1726 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1728 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1730 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1732 static char *typename_concat (struct obstack *obs, const char *prefix,
1733 const char *suffix, int physname,
1734 struct dwarf2_cu *cu);
1736 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1738 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1740 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1742 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1744 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1746 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1747 struct dwarf2_cu *, struct partial_symtab *);
1749 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1750 values. Keep the items ordered with increasing constraints compliance. */
1753 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1754 PC_BOUNDS_NOT_PRESENT,
1756 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1757 were present but they do not form a valid range of PC addresses. */
1760 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1763 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1767 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1768 CORE_ADDR *, CORE_ADDR *,
1770 struct partial_symtab *);
1772 static void get_scope_pc_bounds (struct die_info *,
1773 CORE_ADDR *, CORE_ADDR *,
1774 struct dwarf2_cu *);
1776 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1777 CORE_ADDR, struct dwarf2_cu *);
1779 static void dwarf2_add_field (struct field_info *, struct die_info *,
1780 struct dwarf2_cu *);
1782 static void dwarf2_attach_fields_to_type (struct field_info *,
1783 struct type *, struct dwarf2_cu *);
1785 static void dwarf2_add_member_fn (struct field_info *,
1786 struct die_info *, struct type *,
1787 struct dwarf2_cu *);
1789 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1791 struct dwarf2_cu *);
1793 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1795 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1797 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1799 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1801 static struct using_direct **using_directives (enum language);
1803 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1805 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1807 static struct type *read_module_type (struct die_info *die,
1808 struct dwarf2_cu *cu);
1810 static const char *namespace_name (struct die_info *die,
1811 int *is_anonymous, struct dwarf2_cu *);
1813 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1815 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1817 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1818 struct dwarf2_cu *);
1820 static struct die_info *read_die_and_siblings_1
1821 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1824 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1825 const gdb_byte *info_ptr,
1826 const gdb_byte **new_info_ptr,
1827 struct die_info *parent);
1829 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1830 struct die_info **, const gdb_byte *,
1833 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1834 struct die_info **, const gdb_byte *,
1837 static void process_die (struct die_info *, struct dwarf2_cu *);
1839 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1842 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1844 static const char *dwarf2_full_name (const char *name,
1845 struct die_info *die,
1846 struct dwarf2_cu *cu);
1848 static const char *dwarf2_physname (const char *name, struct die_info *die,
1849 struct dwarf2_cu *cu);
1851 static struct die_info *dwarf2_extension (struct die_info *die,
1852 struct dwarf2_cu **);
1854 static const char *dwarf_tag_name (unsigned int);
1856 static const char *dwarf_attr_name (unsigned int);
1858 static const char *dwarf_form_name (unsigned int);
1860 static const char *dwarf_bool_name (unsigned int);
1862 static const char *dwarf_type_encoding_name (unsigned int);
1864 static struct die_info *sibling_die (struct die_info *);
1866 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1868 static void dump_die_for_error (struct die_info *);
1870 static void dump_die_1 (struct ui_file *, int level, int max_level,
1873 /*static*/ void dump_die (struct die_info *, int max_level);
1875 static void store_in_ref_table (struct die_info *,
1876 struct dwarf2_cu *);
1878 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1880 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1882 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1883 const struct attribute *,
1884 struct dwarf2_cu **);
1886 static struct die_info *follow_die_ref (struct die_info *,
1887 const struct attribute *,
1888 struct dwarf2_cu **);
1890 static struct die_info *follow_die_sig (struct die_info *,
1891 const struct attribute *,
1892 struct dwarf2_cu **);
1894 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1895 struct dwarf2_cu *);
1897 static struct type *get_DW_AT_signature_type (struct die_info *,
1898 const struct attribute *,
1899 struct dwarf2_cu *);
1901 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1903 static void read_signatured_type (struct signatured_type *);
1905 static int attr_to_dynamic_prop (const struct attribute *attr,
1906 struct die_info *die, struct dwarf2_cu *cu,
1907 struct dynamic_prop *prop);
1909 /* memory allocation interface */
1911 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1913 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1915 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1917 static int attr_form_is_block (const struct attribute *);
1919 static int attr_form_is_section_offset (const struct attribute *);
1921 static int attr_form_is_constant (const struct attribute *);
1923 static int attr_form_is_ref (const struct attribute *);
1925 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1926 struct dwarf2_loclist_baton *baton,
1927 const struct attribute *attr);
1929 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1931 struct dwarf2_cu *cu,
1934 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1935 const gdb_byte *info_ptr,
1936 struct abbrev_info *abbrev);
1938 static void free_stack_comp_unit (void *);
1940 static hashval_t partial_die_hash (const void *item);
1942 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1944 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1945 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
1947 static void init_one_comp_unit (struct dwarf2_cu *cu,
1948 struct dwarf2_per_cu_data *per_cu);
1950 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1951 struct die_info *comp_unit_die,
1952 enum language pretend_language);
1954 static void free_heap_comp_unit (void *);
1956 static void free_cached_comp_units (void *);
1958 static void age_cached_comp_units (void);
1960 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1962 static struct type *set_die_type (struct die_info *, struct type *,
1963 struct dwarf2_cu *);
1965 static void create_all_comp_units (struct objfile *);
1967 static int create_all_type_units (struct objfile *);
1969 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1972 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1975 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1978 static void dwarf2_add_dependence (struct dwarf2_cu *,
1979 struct dwarf2_per_cu_data *);
1981 static void dwarf2_mark (struct dwarf2_cu *);
1983 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1985 static struct type *get_die_type_at_offset (sect_offset,
1986 struct dwarf2_per_cu_data *);
1988 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1990 static void dwarf2_release_queue (void *dummy);
1992 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1993 enum language pretend_language);
1995 static void process_queue (void);
1997 /* The return type of find_file_and_directory. Note, the enclosed
1998 string pointers are only valid while this object is valid. */
2000 struct file_and_directory
2002 /* The filename. This is never NULL. */
2005 /* The compilation directory. NULL if not known. If we needed to
2006 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2007 points directly to the DW_AT_comp_dir string attribute owned by
2008 the obstack that owns the DIE. */
2009 const char *comp_dir;
2011 /* If we needed to build a new string for comp_dir, this is what
2012 owns the storage. */
2013 std::string comp_dir_storage;
2016 static file_and_directory find_file_and_directory (struct die_info *die,
2017 struct dwarf2_cu *cu);
2019 static char *file_full_name (int file, struct line_header *lh,
2020 const char *comp_dir);
2022 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2023 enum class rcuh_kind { COMPILE, TYPE };
2025 static const gdb_byte *read_and_check_comp_unit_head
2026 (struct comp_unit_head *header,
2027 struct dwarf2_section_info *section,
2028 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2029 rcuh_kind section_kind);
2031 static void init_cutu_and_read_dies
2032 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2033 int use_existing_cu, int keep,
2034 die_reader_func_ftype *die_reader_func, void *data);
2036 static void init_cutu_and_read_dies_simple
2037 (struct dwarf2_per_cu_data *this_cu,
2038 die_reader_func_ftype *die_reader_func, void *data);
2040 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2042 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2044 static struct dwo_unit *lookup_dwo_unit_in_dwp
2045 (struct dwp_file *dwp_file, const char *comp_dir,
2046 ULONGEST signature, int is_debug_types);
2048 static struct dwp_file *get_dwp_file (void);
2050 static struct dwo_unit *lookup_dwo_comp_unit
2051 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2053 static struct dwo_unit *lookup_dwo_type_unit
2054 (struct signatured_type *, const char *, const char *);
2056 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2058 static void free_dwo_file_cleanup (void *);
2060 static void process_cu_includes (void);
2062 static void check_producer (struct dwarf2_cu *cu);
2064 static void free_line_header_voidp (void *arg);
2066 /* Various complaints about symbol reading that don't abort the process. */
2069 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2071 complaint (&symfile_complaints,
2072 _("statement list doesn't fit in .debug_line section"));
2076 dwarf2_debug_line_missing_file_complaint (void)
2078 complaint (&symfile_complaints,
2079 _(".debug_line section has line data without a file"));
2083 dwarf2_debug_line_missing_end_sequence_complaint (void)
2085 complaint (&symfile_complaints,
2086 _(".debug_line section has line "
2087 "program sequence without an end"));
2091 dwarf2_complex_location_expr_complaint (void)
2093 complaint (&symfile_complaints, _("location expression too complex"));
2097 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2100 complaint (&symfile_complaints,
2101 _("const value length mismatch for '%s', got %d, expected %d"),
2106 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2108 complaint (&symfile_complaints,
2109 _("debug info runs off end of %s section"
2111 get_section_name (section),
2112 get_section_file_name (section));
2116 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2118 complaint (&symfile_complaints,
2119 _("macro debug info contains a "
2120 "malformed macro definition:\n`%s'"),
2125 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2127 complaint (&symfile_complaints,
2128 _("invalid attribute class or form for '%s' in '%s'"),
2132 /* Hash function for line_header_hash. */
2135 line_header_hash (const struct line_header *ofs)
2137 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2140 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2143 line_header_hash_voidp (const void *item)
2145 const struct line_header *ofs = (const struct line_header *) item;
2147 return line_header_hash (ofs);
2150 /* Equality function for line_header_hash. */
2153 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2155 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2156 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2158 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2159 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2165 /* Convert VALUE between big- and little-endian. */
2167 byte_swap (offset_type value)
2171 result = (value & 0xff) << 24;
2172 result |= (value & 0xff00) << 8;
2173 result |= (value & 0xff0000) >> 8;
2174 result |= (value & 0xff000000) >> 24;
2178 #define MAYBE_SWAP(V) byte_swap (V)
2181 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
2182 #endif /* WORDS_BIGENDIAN */
2184 /* Read the given attribute value as an address, taking the attribute's
2185 form into account. */
2188 attr_value_as_address (struct attribute *attr)
2192 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2194 /* Aside from a few clearly defined exceptions, attributes that
2195 contain an address must always be in DW_FORM_addr form.
2196 Unfortunately, some compilers happen to be violating this
2197 requirement by encoding addresses using other forms, such
2198 as DW_FORM_data4 for example. For those broken compilers,
2199 we try to do our best, without any guarantee of success,
2200 to interpret the address correctly. It would also be nice
2201 to generate a complaint, but that would require us to maintain
2202 a list of legitimate cases where a non-address form is allowed,
2203 as well as update callers to pass in at least the CU's DWARF
2204 version. This is more overhead than what we're willing to
2205 expand for a pretty rare case. */
2206 addr = DW_UNSND (attr);
2209 addr = DW_ADDR (attr);
2214 /* The suffix for an index file. */
2215 #define INDEX_SUFFIX ".gdb-index"
2217 /* See declaration. */
2219 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2220 const dwarf2_debug_sections *names)
2221 : objfile (objfile_)
2224 names = &dwarf2_elf_names;
2226 bfd *obfd = objfile->obfd;
2228 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2229 locate_sections (obfd, sec, *names);
2232 dwarf2_per_objfile::~dwarf2_per_objfile ()
2234 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2235 free_cached_comp_units ();
2237 if (quick_file_names_table)
2238 htab_delete (quick_file_names_table);
2240 if (line_header_hash)
2241 htab_delete (line_header_hash);
2243 /* Everything else should be on the objfile obstack. */
2246 /* See declaration. */
2249 dwarf2_per_objfile::free_cached_comp_units ()
2251 dwarf2_per_cu_data *per_cu = read_in_chain;
2252 dwarf2_per_cu_data **last_chain = &read_in_chain;
2253 while (per_cu != NULL)
2255 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2257 free_heap_comp_unit (per_cu->cu);
2258 *last_chain = next_cu;
2263 /* Try to locate the sections we need for DWARF 2 debugging
2264 information and return true if we have enough to do something.
2265 NAMES points to the dwarf2 section names, or is NULL if the standard
2266 ELF names are used. */
2269 dwarf2_has_info (struct objfile *objfile,
2270 const struct dwarf2_debug_sections *names)
2272 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2273 objfile_data (objfile, dwarf2_objfile_data_key));
2274 if (!dwarf2_per_objfile)
2276 /* Initialize per-objfile state. */
2277 struct dwarf2_per_objfile *data
2278 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2280 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2281 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2283 return (!dwarf2_per_objfile->info.is_virtual
2284 && dwarf2_per_objfile->info.s.section != NULL
2285 && !dwarf2_per_objfile->abbrev.is_virtual
2286 && dwarf2_per_objfile->abbrev.s.section != NULL);
2289 /* Return the containing section of virtual section SECTION. */
2291 static struct dwarf2_section_info *
2292 get_containing_section (const struct dwarf2_section_info *section)
2294 gdb_assert (section->is_virtual);
2295 return section->s.containing_section;
2298 /* Return the bfd owner of SECTION. */
2301 get_section_bfd_owner (const struct dwarf2_section_info *section)
2303 if (section->is_virtual)
2305 section = get_containing_section (section);
2306 gdb_assert (!section->is_virtual);
2308 return section->s.section->owner;
2311 /* Return the bfd section of SECTION.
2312 Returns NULL if the section is not present. */
2315 get_section_bfd_section (const struct dwarf2_section_info *section)
2317 if (section->is_virtual)
2319 section = get_containing_section (section);
2320 gdb_assert (!section->is_virtual);
2322 return section->s.section;
2325 /* Return the name of SECTION. */
2328 get_section_name (const struct dwarf2_section_info *section)
2330 asection *sectp = get_section_bfd_section (section);
2332 gdb_assert (sectp != NULL);
2333 return bfd_section_name (get_section_bfd_owner (section), sectp);
2336 /* Return the name of the file SECTION is in. */
2339 get_section_file_name (const struct dwarf2_section_info *section)
2341 bfd *abfd = get_section_bfd_owner (section);
2343 return bfd_get_filename (abfd);
2346 /* Return the id of SECTION.
2347 Returns 0 if SECTION doesn't exist. */
2350 get_section_id (const struct dwarf2_section_info *section)
2352 asection *sectp = get_section_bfd_section (section);
2359 /* Return the flags of SECTION.
2360 SECTION (or containing section if this is a virtual section) must exist. */
2363 get_section_flags (const struct dwarf2_section_info *section)
2365 asection *sectp = get_section_bfd_section (section);
2367 gdb_assert (sectp != NULL);
2368 return bfd_get_section_flags (sectp->owner, sectp);
2371 /* When loading sections, we look either for uncompressed section or for
2372 compressed section names. */
2375 section_is_p (const char *section_name,
2376 const struct dwarf2_section_names *names)
2378 if (names->normal != NULL
2379 && strcmp (section_name, names->normal) == 0)
2381 if (names->compressed != NULL
2382 && strcmp (section_name, names->compressed) == 0)
2387 /* See declaration. */
2390 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2391 const dwarf2_debug_sections &names)
2393 flagword aflag = bfd_get_section_flags (abfd, sectp);
2395 if ((aflag & SEC_HAS_CONTENTS) == 0)
2398 else if (section_is_p (sectp->name, &names.info))
2400 this->info.s.section = sectp;
2401 this->info.size = bfd_get_section_size (sectp);
2403 else if (section_is_p (sectp->name, &names.abbrev))
2405 this->abbrev.s.section = sectp;
2406 this->abbrev.size = bfd_get_section_size (sectp);
2408 else if (section_is_p (sectp->name, &names.line))
2410 this->line.s.section = sectp;
2411 this->line.size = bfd_get_section_size (sectp);
2413 else if (section_is_p (sectp->name, &names.loc))
2415 this->loc.s.section = sectp;
2416 this->loc.size = bfd_get_section_size (sectp);
2418 else if (section_is_p (sectp->name, &names.loclists))
2420 this->loclists.s.section = sectp;
2421 this->loclists.size = bfd_get_section_size (sectp);
2423 else if (section_is_p (sectp->name, &names.macinfo))
2425 this->macinfo.s.section = sectp;
2426 this->macinfo.size = bfd_get_section_size (sectp);
2428 else if (section_is_p (sectp->name, &names.macro))
2430 this->macro.s.section = sectp;
2431 this->macro.size = bfd_get_section_size (sectp);
2433 else if (section_is_p (sectp->name, &names.str))
2435 this->str.s.section = sectp;
2436 this->str.size = bfd_get_section_size (sectp);
2438 else if (section_is_p (sectp->name, &names.line_str))
2440 this->line_str.s.section = sectp;
2441 this->line_str.size = bfd_get_section_size (sectp);
2443 else if (section_is_p (sectp->name, &names.addr))
2445 this->addr.s.section = sectp;
2446 this->addr.size = bfd_get_section_size (sectp);
2448 else if (section_is_p (sectp->name, &names.frame))
2450 this->frame.s.section = sectp;
2451 this->frame.size = bfd_get_section_size (sectp);
2453 else if (section_is_p (sectp->name, &names.eh_frame))
2455 this->eh_frame.s.section = sectp;
2456 this->eh_frame.size = bfd_get_section_size (sectp);
2458 else if (section_is_p (sectp->name, &names.ranges))
2460 this->ranges.s.section = sectp;
2461 this->ranges.size = bfd_get_section_size (sectp);
2463 else if (section_is_p (sectp->name, &names.rnglists))
2465 this->rnglists.s.section = sectp;
2466 this->rnglists.size = bfd_get_section_size (sectp);
2468 else if (section_is_p (sectp->name, &names.types))
2470 struct dwarf2_section_info type_section;
2472 memset (&type_section, 0, sizeof (type_section));
2473 type_section.s.section = sectp;
2474 type_section.size = bfd_get_section_size (sectp);
2476 VEC_safe_push (dwarf2_section_info_def, this->types,
2479 else if (section_is_p (sectp->name, &names.gdb_index))
2481 this->gdb_index.s.section = sectp;
2482 this->gdb_index.size = bfd_get_section_size (sectp);
2485 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2486 && bfd_section_vma (abfd, sectp) == 0)
2487 this->has_section_at_zero = true;
2490 /* A helper function that decides whether a section is empty,
2494 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2496 if (section->is_virtual)
2497 return section->size == 0;
2498 return section->s.section == NULL || section->size == 0;
2501 /* Read the contents of the section INFO.
2502 OBJFILE is the main object file, but not necessarily the file where
2503 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2505 If the section is compressed, uncompress it before returning. */
2508 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2512 gdb_byte *buf, *retbuf;
2516 info->buffer = NULL;
2519 if (dwarf2_section_empty_p (info))
2522 sectp = get_section_bfd_section (info);
2524 /* If this is a virtual section we need to read in the real one first. */
2525 if (info->is_virtual)
2527 struct dwarf2_section_info *containing_section =
2528 get_containing_section (info);
2530 gdb_assert (sectp != NULL);
2531 if ((sectp->flags & SEC_RELOC) != 0)
2533 error (_("Dwarf Error: DWP format V2 with relocations is not"
2534 " supported in section %s [in module %s]"),
2535 get_section_name (info), get_section_file_name (info));
2537 dwarf2_read_section (objfile, containing_section);
2538 /* Other code should have already caught virtual sections that don't
2540 gdb_assert (info->virtual_offset + info->size
2541 <= containing_section->size);
2542 /* If the real section is empty or there was a problem reading the
2543 section we shouldn't get here. */
2544 gdb_assert (containing_section->buffer != NULL);
2545 info->buffer = containing_section->buffer + info->virtual_offset;
2549 /* If the section has relocations, we must read it ourselves.
2550 Otherwise we attach it to the BFD. */
2551 if ((sectp->flags & SEC_RELOC) == 0)
2553 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2557 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2560 /* When debugging .o files, we may need to apply relocations; see
2561 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2562 We never compress sections in .o files, so we only need to
2563 try this when the section is not compressed. */
2564 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2567 info->buffer = retbuf;
2571 abfd = get_section_bfd_owner (info);
2572 gdb_assert (abfd != NULL);
2574 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2575 || bfd_bread (buf, info->size, abfd) != info->size)
2577 error (_("Dwarf Error: Can't read DWARF data"
2578 " in section %s [in module %s]"),
2579 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2583 /* A helper function that returns the size of a section in a safe way.
2584 If you are positive that the section has been read before using the
2585 size, then it is safe to refer to the dwarf2_section_info object's
2586 "size" field directly. In other cases, you must call this
2587 function, because for compressed sections the size field is not set
2588 correctly until the section has been read. */
2590 static bfd_size_type
2591 dwarf2_section_size (struct objfile *objfile,
2592 struct dwarf2_section_info *info)
2595 dwarf2_read_section (objfile, info);
2599 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2603 dwarf2_get_section_info (struct objfile *objfile,
2604 enum dwarf2_section_enum sect,
2605 asection **sectp, const gdb_byte **bufp,
2606 bfd_size_type *sizep)
2608 struct dwarf2_per_objfile *data
2609 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2610 dwarf2_objfile_data_key);
2611 struct dwarf2_section_info *info;
2613 /* We may see an objfile without any DWARF, in which case we just
2624 case DWARF2_DEBUG_FRAME:
2625 info = &data->frame;
2627 case DWARF2_EH_FRAME:
2628 info = &data->eh_frame;
2631 gdb_assert_not_reached ("unexpected section");
2634 dwarf2_read_section (objfile, info);
2636 *sectp = get_section_bfd_section (info);
2637 *bufp = info->buffer;
2638 *sizep = info->size;
2641 /* A helper function to find the sections for a .dwz file. */
2644 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2646 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2648 /* Note that we only support the standard ELF names, because .dwz
2649 is ELF-only (at the time of writing). */
2650 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2652 dwz_file->abbrev.s.section = sectp;
2653 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2657 dwz_file->info.s.section = sectp;
2658 dwz_file->info.size = bfd_get_section_size (sectp);
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2662 dwz_file->str.s.section = sectp;
2663 dwz_file->str.size = bfd_get_section_size (sectp);
2665 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2667 dwz_file->line.s.section = sectp;
2668 dwz_file->line.size = bfd_get_section_size (sectp);
2670 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2672 dwz_file->macro.s.section = sectp;
2673 dwz_file->macro.size = bfd_get_section_size (sectp);
2675 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2677 dwz_file->gdb_index.s.section = sectp;
2678 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2682 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2683 there is no .gnu_debugaltlink section in the file. Error if there
2684 is such a section but the file cannot be found. */
2686 static struct dwz_file *
2687 dwarf2_get_dwz_file (void)
2690 struct cleanup *cleanup;
2691 const char *filename;
2692 struct dwz_file *result;
2693 bfd_size_type buildid_len_arg;
2697 if (dwarf2_per_objfile->dwz_file != NULL)
2698 return dwarf2_per_objfile->dwz_file;
2700 bfd_set_error (bfd_error_no_error);
2701 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2702 &buildid_len_arg, &buildid);
2705 if (bfd_get_error () == bfd_error_no_error)
2707 error (_("could not read '.gnu_debugaltlink' section: %s"),
2708 bfd_errmsg (bfd_get_error ()));
2710 cleanup = make_cleanup (xfree, data);
2711 make_cleanup (xfree, buildid);
2713 buildid_len = (size_t) buildid_len_arg;
2715 filename = (const char *) data;
2717 std::string abs_storage;
2718 if (!IS_ABSOLUTE_PATH (filename))
2720 gdb::unique_xmalloc_ptr<char> abs
2721 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2723 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2724 filename = abs_storage.c_str ();
2727 /* First try the file name given in the section. If that doesn't
2728 work, try to use the build-id instead. */
2729 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2730 if (dwz_bfd != NULL)
2732 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2736 if (dwz_bfd == NULL)
2737 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2739 if (dwz_bfd == NULL)
2740 error (_("could not find '.gnu_debugaltlink' file for %s"),
2741 objfile_name (dwarf2_per_objfile->objfile));
2743 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2745 result->dwz_bfd = dwz_bfd.release ();
2747 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2749 do_cleanups (cleanup);
2751 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2752 dwarf2_per_objfile->dwz_file = result;
2756 /* DWARF quick_symbols_functions support. */
2758 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2759 unique line tables, so we maintain a separate table of all .debug_line
2760 derived entries to support the sharing.
2761 All the quick functions need is the list of file names. We discard the
2762 line_header when we're done and don't need to record it here. */
2763 struct quick_file_names
2765 /* The data used to construct the hash key. */
2766 struct stmt_list_hash hash;
2768 /* The number of entries in file_names, real_names. */
2769 unsigned int num_file_names;
2771 /* The file names from the line table, after being run through
2773 const char **file_names;
2775 /* The file names from the line table after being run through
2776 gdb_realpath. These are computed lazily. */
2777 const char **real_names;
2780 /* When using the index (and thus not using psymtabs), each CU has an
2781 object of this type. This is used to hold information needed by
2782 the various "quick" methods. */
2783 struct dwarf2_per_cu_quick_data
2785 /* The file table. This can be NULL if there was no file table
2786 or it's currently not read in.
2787 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2788 struct quick_file_names *file_names;
2790 /* The corresponding symbol table. This is NULL if symbols for this
2791 CU have not yet been read. */
2792 struct compunit_symtab *compunit_symtab;
2794 /* A temporary mark bit used when iterating over all CUs in
2795 expand_symtabs_matching. */
2796 unsigned int mark : 1;
2798 /* True if we've tried to read the file table and found there isn't one.
2799 There will be no point in trying to read it again next time. */
2800 unsigned int no_file_data : 1;
2803 /* Utility hash function for a stmt_list_hash. */
2806 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2810 if (stmt_list_hash->dwo_unit != NULL)
2811 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2812 v += to_underlying (stmt_list_hash->line_sect_off);
2816 /* Utility equality function for a stmt_list_hash. */
2819 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2820 const struct stmt_list_hash *rhs)
2822 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2824 if (lhs->dwo_unit != NULL
2825 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2828 return lhs->line_sect_off == rhs->line_sect_off;
2831 /* Hash function for a quick_file_names. */
2834 hash_file_name_entry (const void *e)
2836 const struct quick_file_names *file_data
2837 = (const struct quick_file_names *) e;
2839 return hash_stmt_list_entry (&file_data->hash);
2842 /* Equality function for a quick_file_names. */
2845 eq_file_name_entry (const void *a, const void *b)
2847 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2848 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2850 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2853 /* Delete function for a quick_file_names. */
2856 delete_file_name_entry (void *e)
2858 struct quick_file_names *file_data = (struct quick_file_names *) e;
2861 for (i = 0; i < file_data->num_file_names; ++i)
2863 xfree ((void*) file_data->file_names[i]);
2864 if (file_data->real_names)
2865 xfree ((void*) file_data->real_names[i]);
2868 /* The space for the struct itself lives on objfile_obstack,
2869 so we don't free it here. */
2872 /* Create a quick_file_names hash table. */
2875 create_quick_file_names_table (unsigned int nr_initial_entries)
2877 return htab_create_alloc (nr_initial_entries,
2878 hash_file_name_entry, eq_file_name_entry,
2879 delete_file_name_entry, xcalloc, xfree);
2882 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2883 have to be created afterwards. You should call age_cached_comp_units after
2884 processing PER_CU->CU. dw2_setup must have been already called. */
2887 load_cu (struct dwarf2_per_cu_data *per_cu)
2889 if (per_cu->is_debug_types)
2890 load_full_type_unit (per_cu);
2892 load_full_comp_unit (per_cu, language_minimal);
2894 if (per_cu->cu == NULL)
2895 return; /* Dummy CU. */
2897 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2900 /* Read in the symbols for PER_CU. */
2903 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2905 struct cleanup *back_to;
2907 /* Skip type_unit_groups, reading the type units they contain
2908 is handled elsewhere. */
2909 if (IS_TYPE_UNIT_GROUP (per_cu))
2912 back_to = make_cleanup (dwarf2_release_queue, NULL);
2914 if (dwarf2_per_objfile->using_index
2915 ? per_cu->v.quick->compunit_symtab == NULL
2916 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2918 queue_comp_unit (per_cu, language_minimal);
2921 /* If we just loaded a CU from a DWO, and we're working with an index
2922 that may badly handle TUs, load all the TUs in that DWO as well.
2923 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2924 if (!per_cu->is_debug_types
2925 && per_cu->cu != NULL
2926 && per_cu->cu->dwo_unit != NULL
2927 && dwarf2_per_objfile->index_table != NULL
2928 && dwarf2_per_objfile->index_table->version <= 7
2929 /* DWP files aren't supported yet. */
2930 && get_dwp_file () == NULL)
2931 queue_and_load_all_dwo_tus (per_cu);
2936 /* Age the cache, releasing compilation units that have not
2937 been used recently. */
2938 age_cached_comp_units ();
2940 do_cleanups (back_to);
2943 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2944 the objfile from which this CU came. Returns the resulting symbol
2947 static struct compunit_symtab *
2948 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2950 gdb_assert (dwarf2_per_objfile->using_index);
2951 if (!per_cu->v.quick->compunit_symtab)
2953 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2954 scoped_restore decrementer = increment_reading_symtab ();
2955 dw2_do_instantiate_symtab (per_cu);
2956 process_cu_includes ();
2957 do_cleanups (back_to);
2960 return per_cu->v.quick->compunit_symtab;
2963 /* Return the CU/TU given its index.
2965 This is intended for loops like:
2967 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2968 + dwarf2_per_objfile->n_type_units); ++i)
2970 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2976 static struct dwarf2_per_cu_data *
2977 dw2_get_cutu (int index)
2979 if (index >= dwarf2_per_objfile->n_comp_units)
2981 index -= dwarf2_per_objfile->n_comp_units;
2982 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2983 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2986 return dwarf2_per_objfile->all_comp_units[index];
2989 /* Return the CU given its index.
2990 This differs from dw2_get_cutu in that it's for when you know INDEX
2993 static struct dwarf2_per_cu_data *
2994 dw2_get_cu (int index)
2996 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2998 return dwarf2_per_objfile->all_comp_units[index];
3001 /* A helper for create_cus_from_index that handles a given list of
3005 create_cus_from_index_list (struct objfile *objfile,
3006 const gdb_byte *cu_list, offset_type n_elements,
3007 struct dwarf2_section_info *section,
3013 for (i = 0; i < n_elements; i += 2)
3015 gdb_static_assert (sizeof (ULONGEST) >= 8);
3017 sect_offset sect_off
3018 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3019 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3022 dwarf2_per_cu_data *the_cu
3023 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3024 struct dwarf2_per_cu_data);
3025 the_cu->sect_off = sect_off;
3026 the_cu->length = length;
3027 the_cu->objfile = objfile;
3028 the_cu->section = section;
3029 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3030 struct dwarf2_per_cu_quick_data);
3031 the_cu->is_dwz = is_dwz;
3032 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
3036 /* Read the CU list from the mapped index, and use it to create all
3037 the CU objects for this objfile. */
3040 create_cus_from_index (struct objfile *objfile,
3041 const gdb_byte *cu_list, offset_type cu_list_elements,
3042 const gdb_byte *dwz_list, offset_type dwz_elements)
3044 struct dwz_file *dwz;
3046 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3047 dwarf2_per_objfile->all_comp_units =
3048 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3049 dwarf2_per_objfile->n_comp_units);
3051 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3052 &dwarf2_per_objfile->info, 0, 0);
3054 if (dwz_elements == 0)
3057 dwz = dwarf2_get_dwz_file ();
3058 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3059 cu_list_elements / 2);
3062 /* Create the signatured type hash table from the index. */
3065 create_signatured_type_table_from_index (struct objfile *objfile,
3066 struct dwarf2_section_info *section,
3067 const gdb_byte *bytes,
3068 offset_type elements)
3071 htab_t sig_types_hash;
3073 dwarf2_per_objfile->n_type_units
3074 = dwarf2_per_objfile->n_allocated_type_units
3076 dwarf2_per_objfile->all_type_units =
3077 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3079 sig_types_hash = allocate_signatured_type_table (objfile);
3081 for (i = 0; i < elements; i += 3)
3083 struct signatured_type *sig_type;
3086 cu_offset type_offset_in_tu;
3088 gdb_static_assert (sizeof (ULONGEST) >= 8);
3089 sect_offset sect_off
3090 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3092 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3094 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3097 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3098 struct signatured_type);
3099 sig_type->signature = signature;
3100 sig_type->type_offset_in_tu = type_offset_in_tu;
3101 sig_type->per_cu.is_debug_types = 1;
3102 sig_type->per_cu.section = section;
3103 sig_type->per_cu.sect_off = sect_off;
3104 sig_type->per_cu.objfile = objfile;
3105 sig_type->per_cu.v.quick
3106 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3107 struct dwarf2_per_cu_quick_data);
3109 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3112 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3115 dwarf2_per_objfile->signatured_types = sig_types_hash;
3118 /* Read the address map data from the mapped index, and use it to
3119 populate the objfile's psymtabs_addrmap. */
3122 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3124 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3125 const gdb_byte *iter, *end;
3126 struct addrmap *mutable_map;
3129 auto_obstack temp_obstack;
3131 mutable_map = addrmap_create_mutable (&temp_obstack);
3133 iter = index->address_table;
3134 end = iter + index->address_table_size;
3136 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3140 ULONGEST hi, lo, cu_index;
3141 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3143 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3145 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3150 complaint (&symfile_complaints,
3151 _(".gdb_index address table has invalid range (%s - %s)"),
3152 hex_string (lo), hex_string (hi));
3156 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3158 complaint (&symfile_complaints,
3159 _(".gdb_index address table has invalid CU number %u"),
3160 (unsigned) cu_index);
3164 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3165 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3166 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3169 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3170 &objfile->objfile_obstack);
3173 /* The hash function for strings in the mapped index. This is the same as
3174 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3175 implementation. This is necessary because the hash function is tied to the
3176 format of the mapped index file. The hash values do not have to match with
3179 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3182 mapped_index_string_hash (int index_version, const void *p)
3184 const unsigned char *str = (const unsigned char *) p;
3188 while ((c = *str++) != 0)
3190 if (index_version >= 5)
3192 r = r * 67 + c - 113;
3198 /* Find a slot in the mapped index INDEX for the object named NAME.
3199 If NAME is found, set *VEC_OUT to point to the CU vector in the
3200 constant pool and return 1. If NAME cannot be found, return 0. */
3203 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3204 offset_type **vec_out)
3206 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3208 offset_type slot, step;
3209 int (*cmp) (const char *, const char *);
3211 if (current_language->la_language == language_cplus
3212 || current_language->la_language == language_fortran
3213 || current_language->la_language == language_d)
3215 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3218 if (strchr (name, '(') != NULL)
3220 char *without_params = cp_remove_params (name);
3222 if (without_params != NULL)
3224 make_cleanup (xfree, without_params);
3225 name = without_params;
3230 /* Index version 4 did not support case insensitive searches. But the
3231 indices for case insensitive languages are built in lowercase, therefore
3232 simulate our NAME being searched is also lowercased. */
3233 hash = mapped_index_string_hash ((index->version == 4
3234 && case_sensitivity == case_sensitive_off
3235 ? 5 : index->version),
3238 slot = hash & (index->symbol_table_slots - 1);
3239 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3240 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3244 /* Convert a slot number to an offset into the table. */
3245 offset_type i = 2 * slot;
3247 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3249 do_cleanups (back_to);
3253 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3254 if (!cmp (name, str))
3256 *vec_out = (offset_type *) (index->constant_pool
3257 + MAYBE_SWAP (index->symbol_table[i + 1]));
3258 do_cleanups (back_to);
3262 slot = (slot + step) & (index->symbol_table_slots - 1);
3266 /* A helper function that reads the .gdb_index from SECTION and fills
3267 in MAP. FILENAME is the name of the file containing the section;
3268 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3269 ok to use deprecated sections.
3271 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3272 out parameters that are filled in with information about the CU and
3273 TU lists in the section.
3275 Returns 1 if all went well, 0 otherwise. */
3278 read_index_from_section (struct objfile *objfile,
3279 const char *filename,
3281 struct dwarf2_section_info *section,
3282 struct mapped_index *map,
3283 const gdb_byte **cu_list,
3284 offset_type *cu_list_elements,
3285 const gdb_byte **types_list,
3286 offset_type *types_list_elements)
3288 const gdb_byte *addr;
3289 offset_type version;
3290 offset_type *metadata;
3293 if (dwarf2_section_empty_p (section))
3296 /* Older elfutils strip versions could keep the section in the main
3297 executable while splitting it for the separate debug info file. */
3298 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3301 dwarf2_read_section (objfile, section);
3303 addr = section->buffer;
3304 /* Version check. */
3305 version = MAYBE_SWAP (*(offset_type *) addr);
3306 /* Versions earlier than 3 emitted every copy of a psymbol. This
3307 causes the index to behave very poorly for certain requests. Version 3
3308 contained incomplete addrmap. So, it seems better to just ignore such
3312 static int warning_printed = 0;
3313 if (!warning_printed)
3315 warning (_("Skipping obsolete .gdb_index section in %s."),
3317 warning_printed = 1;
3321 /* Index version 4 uses a different hash function than index version
3324 Versions earlier than 6 did not emit psymbols for inlined
3325 functions. Using these files will cause GDB not to be able to
3326 set breakpoints on inlined functions by name, so we ignore these
3327 indices unless the user has done
3328 "set use-deprecated-index-sections on". */
3329 if (version < 6 && !deprecated_ok)
3331 static int warning_printed = 0;
3332 if (!warning_printed)
3335 Skipping deprecated .gdb_index section in %s.\n\
3336 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3337 to use the section anyway."),
3339 warning_printed = 1;
3343 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3344 of the TU (for symbols coming from TUs),
3345 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3346 Plus gold-generated indices can have duplicate entries for global symbols,
3347 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3348 These are just performance bugs, and we can't distinguish gdb-generated
3349 indices from gold-generated ones, so issue no warning here. */
3351 /* Indexes with higher version than the one supported by GDB may be no
3352 longer backward compatible. */
3356 map->version = version;
3357 map->total_size = section->size;
3359 metadata = (offset_type *) (addr + sizeof (offset_type));
3362 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3363 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3367 *types_list = addr + MAYBE_SWAP (metadata[i]);
3368 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3369 - MAYBE_SWAP (metadata[i]))
3373 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3374 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3375 - MAYBE_SWAP (metadata[i]));
3378 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3379 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3380 - MAYBE_SWAP (metadata[i]))
3381 / (2 * sizeof (offset_type)));
3384 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3390 /* Read the index file. If everything went ok, initialize the "quick"
3391 elements of all the CUs and return 1. Otherwise, return 0. */
3394 dwarf2_read_index (struct objfile *objfile)
3396 struct mapped_index local_map, *map;
3397 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3398 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3399 struct dwz_file *dwz;
3401 if (!read_index_from_section (objfile, objfile_name (objfile),
3402 use_deprecated_index_sections,
3403 &dwarf2_per_objfile->gdb_index, &local_map,
3404 &cu_list, &cu_list_elements,
3405 &types_list, &types_list_elements))
3408 /* Don't use the index if it's empty. */
3409 if (local_map.symbol_table_slots == 0)
3412 /* If there is a .dwz file, read it so we can get its CU list as
3414 dwz = dwarf2_get_dwz_file ();
3417 struct mapped_index dwz_map;
3418 const gdb_byte *dwz_types_ignore;
3419 offset_type dwz_types_elements_ignore;
3421 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3423 &dwz->gdb_index, &dwz_map,
3424 &dwz_list, &dwz_list_elements,
3426 &dwz_types_elements_ignore))
3428 warning (_("could not read '.gdb_index' section from %s; skipping"),
3429 bfd_get_filename (dwz->dwz_bfd));
3434 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3437 if (types_list_elements)
3439 struct dwarf2_section_info *section;
3441 /* We can only handle a single .debug_types when we have an
3443 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3446 section = VEC_index (dwarf2_section_info_def,
3447 dwarf2_per_objfile->types, 0);
3449 create_signatured_type_table_from_index (objfile, section, types_list,
3450 types_list_elements);
3453 create_addrmap_from_index (objfile, &local_map);
3455 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3458 dwarf2_per_objfile->index_table = map;
3459 dwarf2_per_objfile->using_index = 1;
3460 dwarf2_per_objfile->quick_file_names_table =
3461 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3466 /* A helper for the "quick" functions which sets the global
3467 dwarf2_per_objfile according to OBJFILE. */
3470 dw2_setup (struct objfile *objfile)
3472 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3473 objfile_data (objfile, dwarf2_objfile_data_key));
3474 gdb_assert (dwarf2_per_objfile);
3477 /* die_reader_func for dw2_get_file_names. */
3480 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3481 const gdb_byte *info_ptr,
3482 struct die_info *comp_unit_die,
3486 struct dwarf2_cu *cu = reader->cu;
3487 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3488 struct objfile *objfile = dwarf2_per_objfile->objfile;
3489 struct dwarf2_per_cu_data *lh_cu;
3490 struct attribute *attr;
3493 struct quick_file_names *qfn;
3495 gdb_assert (! this_cu->is_debug_types);
3497 /* Our callers never want to match partial units -- instead they
3498 will match the enclosing full CU. */
3499 if (comp_unit_die->tag == DW_TAG_partial_unit)
3501 this_cu->v.quick->no_file_data = 1;
3509 sect_offset line_offset {};
3511 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3514 struct quick_file_names find_entry;
3516 line_offset = (sect_offset) DW_UNSND (attr);
3518 /* We may have already read in this line header (TU line header sharing).
3519 If we have we're done. */
3520 find_entry.hash.dwo_unit = cu->dwo_unit;
3521 find_entry.hash.line_sect_off = line_offset;
3522 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3523 &find_entry, INSERT);
3526 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3530 lh = dwarf_decode_line_header (line_offset, cu);
3534 lh_cu->v.quick->no_file_data = 1;
3538 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3539 qfn->hash.dwo_unit = cu->dwo_unit;
3540 qfn->hash.line_sect_off = line_offset;
3541 gdb_assert (slot != NULL);
3544 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3546 qfn->num_file_names = lh->file_names.size ();
3548 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3549 for (i = 0; i < lh->file_names.size (); ++i)
3550 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3551 qfn->real_names = NULL;
3553 lh_cu->v.quick->file_names = qfn;
3556 /* A helper for the "quick" functions which attempts to read the line
3557 table for THIS_CU. */
3559 static struct quick_file_names *
3560 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3562 /* This should never be called for TUs. */
3563 gdb_assert (! this_cu->is_debug_types);
3564 /* Nor type unit groups. */
3565 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3567 if (this_cu->v.quick->file_names != NULL)
3568 return this_cu->v.quick->file_names;
3569 /* If we know there is no line data, no point in looking again. */
3570 if (this_cu->v.quick->no_file_data)
3573 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3575 if (this_cu->v.quick->no_file_data)
3577 return this_cu->v.quick->file_names;
3580 /* A helper for the "quick" functions which computes and caches the
3581 real path for a given file name from the line table. */
3584 dw2_get_real_path (struct objfile *objfile,
3585 struct quick_file_names *qfn, int index)
3587 if (qfn->real_names == NULL)
3588 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3589 qfn->num_file_names, const char *);
3591 if (qfn->real_names[index] == NULL)
3592 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3594 return qfn->real_names[index];
3597 static struct symtab *
3598 dw2_find_last_source_symtab (struct objfile *objfile)
3600 struct compunit_symtab *cust;
3603 dw2_setup (objfile);
3604 index = dwarf2_per_objfile->n_comp_units - 1;
3605 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3608 return compunit_primary_filetab (cust);
3611 /* Traversal function for dw2_forget_cached_source_info. */
3614 dw2_free_cached_file_names (void **slot, void *info)
3616 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3618 if (file_data->real_names)
3622 for (i = 0; i < file_data->num_file_names; ++i)
3624 xfree ((void*) file_data->real_names[i]);
3625 file_data->real_names[i] = NULL;
3633 dw2_forget_cached_source_info (struct objfile *objfile)
3635 dw2_setup (objfile);
3637 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3638 dw2_free_cached_file_names, NULL);
3641 /* Helper function for dw2_map_symtabs_matching_filename that expands
3642 the symtabs and calls the iterator. */
3645 dw2_map_expand_apply (struct objfile *objfile,
3646 struct dwarf2_per_cu_data *per_cu,
3647 const char *name, const char *real_path,
3648 gdb::function_view<bool (symtab *)> callback)
3650 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3652 /* Don't visit already-expanded CUs. */
3653 if (per_cu->v.quick->compunit_symtab)
3656 /* This may expand more than one symtab, and we want to iterate over
3658 dw2_instantiate_symtab (per_cu);
3660 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3661 last_made, callback);
3664 /* Implementation of the map_symtabs_matching_filename method. */
3667 dw2_map_symtabs_matching_filename
3668 (struct objfile *objfile, const char *name, const char *real_path,
3669 gdb::function_view<bool (symtab *)> callback)
3672 const char *name_basename = lbasename (name);
3674 dw2_setup (objfile);
3676 /* The rule is CUs specify all the files, including those used by
3677 any TU, so there's no need to scan TUs here. */
3679 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3682 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3683 struct quick_file_names *file_data;
3685 /* We only need to look at symtabs not already expanded. */
3686 if (per_cu->v.quick->compunit_symtab)
3689 file_data = dw2_get_file_names (per_cu);
3690 if (file_data == NULL)
3693 for (j = 0; j < file_data->num_file_names; ++j)
3695 const char *this_name = file_data->file_names[j];
3696 const char *this_real_name;
3698 if (compare_filenames_for_search (this_name, name))
3700 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3706 /* Before we invoke realpath, which can get expensive when many
3707 files are involved, do a quick comparison of the basenames. */
3708 if (! basenames_may_differ
3709 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3712 this_real_name = dw2_get_real_path (objfile, file_data, j);
3713 if (compare_filenames_for_search (this_real_name, name))
3715 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3721 if (real_path != NULL)
3723 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3724 gdb_assert (IS_ABSOLUTE_PATH (name));
3725 if (this_real_name != NULL
3726 && FILENAME_CMP (real_path, this_real_name) == 0)
3728 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3740 /* Struct used to manage iterating over all CUs looking for a symbol. */
3742 struct dw2_symtab_iterator
3744 /* The internalized form of .gdb_index. */
3745 struct mapped_index *index;
3746 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3747 int want_specific_block;
3748 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3749 Unused if !WANT_SPECIFIC_BLOCK. */
3751 /* The kind of symbol we're looking for. */
3753 /* The list of CUs from the index entry of the symbol,
3754 or NULL if not found. */
3756 /* The next element in VEC to look at. */
3758 /* The number of elements in VEC, or zero if there is no match. */
3760 /* Have we seen a global version of the symbol?
3761 If so we can ignore all further global instances.
3762 This is to work around gold/15646, inefficient gold-generated
3767 /* Initialize the index symtab iterator ITER.
3768 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3769 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3772 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3773 struct mapped_index *index,
3774 int want_specific_block,
3779 iter->index = index;
3780 iter->want_specific_block = want_specific_block;
3781 iter->block_index = block_index;
3782 iter->domain = domain;
3784 iter->global_seen = 0;
3786 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3787 iter->length = MAYBE_SWAP (*iter->vec);
3795 /* Return the next matching CU or NULL if there are no more. */
3797 static struct dwarf2_per_cu_data *
3798 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3800 for ( ; iter->next < iter->length; ++iter->next)
3802 offset_type cu_index_and_attrs =
3803 MAYBE_SWAP (iter->vec[iter->next + 1]);
3804 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3805 struct dwarf2_per_cu_data *per_cu;
3806 int want_static = iter->block_index != GLOBAL_BLOCK;
3807 /* This value is only valid for index versions >= 7. */
3808 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3809 gdb_index_symbol_kind symbol_kind =
3810 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3811 /* Only check the symbol attributes if they're present.
3812 Indices prior to version 7 don't record them,
3813 and indices >= 7 may elide them for certain symbols
3814 (gold does this). */
3816 (iter->index->version >= 7
3817 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3819 /* Don't crash on bad data. */
3820 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3821 + dwarf2_per_objfile->n_type_units))
3823 complaint (&symfile_complaints,
3824 _(".gdb_index entry has bad CU index"
3826 objfile_name (dwarf2_per_objfile->objfile));
3830 per_cu = dw2_get_cutu (cu_index);
3832 /* Skip if already read in. */
3833 if (per_cu->v.quick->compunit_symtab)
3836 /* Check static vs global. */
3839 if (iter->want_specific_block
3840 && want_static != is_static)
3842 /* Work around gold/15646. */
3843 if (!is_static && iter->global_seen)
3846 iter->global_seen = 1;
3849 /* Only check the symbol's kind if it has one. */
3852 switch (iter->domain)
3855 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3856 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3857 /* Some types are also in VAR_DOMAIN. */
3858 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3862 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3866 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3881 static struct compunit_symtab *
3882 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3883 const char *name, domain_enum domain)
3885 struct compunit_symtab *stab_best = NULL;
3886 struct mapped_index *index;
3888 dw2_setup (objfile);
3890 index = dwarf2_per_objfile->index_table;
3892 /* index is NULL if OBJF_READNOW. */
3895 struct dw2_symtab_iterator iter;
3896 struct dwarf2_per_cu_data *per_cu;
3898 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3900 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3902 struct symbol *sym, *with_opaque = NULL;
3903 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3904 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3905 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3907 sym = block_find_symbol (block, name, domain,
3908 block_find_non_opaque_type_preferred,
3911 /* Some caution must be observed with overloaded functions
3912 and methods, since the index will not contain any overload
3913 information (but NAME might contain it). */
3916 && SYMBOL_MATCHES_SEARCH_NAME (sym, name))
3918 if (with_opaque != NULL
3919 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, name))
3922 /* Keep looking through other CUs. */
3930 dw2_print_stats (struct objfile *objfile)
3932 int i, total, count;
3934 dw2_setup (objfile);
3935 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3937 for (i = 0; i < total; ++i)
3939 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3941 if (!per_cu->v.quick->compunit_symtab)
3944 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3945 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3948 /* This dumps minimal information about the index.
3949 It is called via "mt print objfiles".
3950 One use is to verify .gdb_index has been loaded by the
3951 gdb.dwarf2/gdb-index.exp testcase. */
3954 dw2_dump (struct objfile *objfile)
3956 dw2_setup (objfile);
3957 gdb_assert (dwarf2_per_objfile->using_index);
3958 printf_filtered (".gdb_index:");
3959 if (dwarf2_per_objfile->index_table != NULL)
3961 printf_filtered (" version %d\n",
3962 dwarf2_per_objfile->index_table->version);
3965 printf_filtered (" faked for \"readnow\"\n");
3966 printf_filtered ("\n");
3970 dw2_relocate (struct objfile *objfile,
3971 const struct section_offsets *new_offsets,
3972 const struct section_offsets *delta)
3974 /* There's nothing to relocate here. */
3978 dw2_expand_symtabs_for_function (struct objfile *objfile,
3979 const char *func_name)
3981 struct mapped_index *index;
3983 dw2_setup (objfile);
3985 index = dwarf2_per_objfile->index_table;
3987 /* index is NULL if OBJF_READNOW. */
3990 struct dw2_symtab_iterator iter;
3991 struct dwarf2_per_cu_data *per_cu;
3993 /* Note: It doesn't matter what we pass for block_index here. */
3994 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3997 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3998 dw2_instantiate_symtab (per_cu);
4003 dw2_expand_all_symtabs (struct objfile *objfile)
4007 dw2_setup (objfile);
4009 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4010 + dwarf2_per_objfile->n_type_units); ++i)
4012 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4014 dw2_instantiate_symtab (per_cu);
4019 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4020 const char *fullname)
4024 dw2_setup (objfile);
4026 /* We don't need to consider type units here.
4027 This is only called for examining code, e.g. expand_line_sal.
4028 There can be an order of magnitude (or more) more type units
4029 than comp units, and we avoid them if we can. */
4031 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4034 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4035 struct quick_file_names *file_data;
4037 /* We only need to look at symtabs not already expanded. */
4038 if (per_cu->v.quick->compunit_symtab)
4041 file_data = dw2_get_file_names (per_cu);
4042 if (file_data == NULL)
4045 for (j = 0; j < file_data->num_file_names; ++j)
4047 const char *this_fullname = file_data->file_names[j];
4049 if (filename_cmp (this_fullname, fullname) == 0)
4051 dw2_instantiate_symtab (per_cu);
4059 dw2_map_matching_symbols (struct objfile *objfile,
4060 const char * name, domain_enum domain,
4062 int (*callback) (struct block *,
4063 struct symbol *, void *),
4064 void *data, symbol_compare_ftype *match,
4065 symbol_compare_ftype *ordered_compare)
4067 /* Currently unimplemented; used for Ada. The function can be called if the
4068 current language is Ada for a non-Ada objfile using GNU index. As Ada
4069 does not look for non-Ada symbols this function should just return. */
4073 dw2_expand_symtabs_matching
4074 (struct objfile *objfile,
4075 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4076 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4077 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4078 enum search_domain kind)
4082 struct mapped_index *index;
4084 dw2_setup (objfile);
4086 /* index_table is NULL if OBJF_READNOW. */
4087 if (!dwarf2_per_objfile->index_table)
4089 index = dwarf2_per_objfile->index_table;
4091 if (file_matcher != NULL)
4093 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4095 NULL, xcalloc, xfree));
4096 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4098 NULL, xcalloc, xfree));
4100 /* The rule is CUs specify all the files, including those used by
4101 any TU, so there's no need to scan TUs here. */
4103 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4106 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4107 struct quick_file_names *file_data;
4112 per_cu->v.quick->mark = 0;
4114 /* We only need to look at symtabs not already expanded. */
4115 if (per_cu->v.quick->compunit_symtab)
4118 file_data = dw2_get_file_names (per_cu);
4119 if (file_data == NULL)
4122 if (htab_find (visited_not_found.get (), file_data) != NULL)
4124 else if (htab_find (visited_found.get (), file_data) != NULL)
4126 per_cu->v.quick->mark = 1;
4130 for (j = 0; j < file_data->num_file_names; ++j)
4132 const char *this_real_name;
4134 if (file_matcher (file_data->file_names[j], false))
4136 per_cu->v.quick->mark = 1;
4140 /* Before we invoke realpath, which can get expensive when many
4141 files are involved, do a quick comparison of the basenames. */
4142 if (!basenames_may_differ
4143 && !file_matcher (lbasename (file_data->file_names[j]),
4147 this_real_name = dw2_get_real_path (objfile, file_data, j);
4148 if (file_matcher (this_real_name, false))
4150 per_cu->v.quick->mark = 1;
4155 slot = htab_find_slot (per_cu->v.quick->mark
4156 ? visited_found.get ()
4157 : visited_not_found.get (),
4163 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4165 offset_type idx = 2 * iter;
4167 offset_type *vec, vec_len, vec_idx;
4168 int global_seen = 0;
4172 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4175 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4177 if (!symbol_matcher (name))
4180 /* The name was matched, now expand corresponding CUs that were
4182 vec = (offset_type *) (index->constant_pool
4183 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4184 vec_len = MAYBE_SWAP (vec[0]);
4185 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4187 struct dwarf2_per_cu_data *per_cu;
4188 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4189 /* This value is only valid for index versions >= 7. */
4190 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4191 gdb_index_symbol_kind symbol_kind =
4192 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4193 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4194 /* Only check the symbol attributes if they're present.
4195 Indices prior to version 7 don't record them,
4196 and indices >= 7 may elide them for certain symbols
4197 (gold does this). */
4199 (index->version >= 7
4200 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4202 /* Work around gold/15646. */
4205 if (!is_static && global_seen)
4211 /* Only check the symbol's kind if it has one. */
4216 case VARIABLES_DOMAIN:
4217 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4220 case FUNCTIONS_DOMAIN:
4221 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4225 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4233 /* Don't crash on bad data. */
4234 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4235 + dwarf2_per_objfile->n_type_units))
4237 complaint (&symfile_complaints,
4238 _(".gdb_index entry has bad CU index"
4239 " [in module %s]"), objfile_name (objfile));
4243 per_cu = dw2_get_cutu (cu_index);
4244 if (file_matcher == NULL || per_cu->v.quick->mark)
4246 int symtab_was_null =
4247 (per_cu->v.quick->compunit_symtab == NULL);
4249 dw2_instantiate_symtab (per_cu);
4251 if (expansion_notify != NULL
4253 && per_cu->v.quick->compunit_symtab != NULL)
4255 expansion_notify (per_cu->v.quick->compunit_symtab);
4262 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4265 static struct compunit_symtab *
4266 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4271 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4272 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4275 if (cust->includes == NULL)
4278 for (i = 0; cust->includes[i]; ++i)
4280 struct compunit_symtab *s = cust->includes[i];
4282 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4290 static struct compunit_symtab *
4291 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4292 struct bound_minimal_symbol msymbol,
4294 struct obj_section *section,
4297 struct dwarf2_per_cu_data *data;
4298 struct compunit_symtab *result;
4300 dw2_setup (objfile);
4302 if (!objfile->psymtabs_addrmap)
4305 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4310 if (warn_if_readin && data->v.quick->compunit_symtab)
4311 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4312 paddress (get_objfile_arch (objfile), pc));
4315 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4317 gdb_assert (result != NULL);
4322 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4323 void *data, int need_fullname)
4325 dw2_setup (objfile);
4327 if (!dwarf2_per_objfile->filenames_cache)
4329 dwarf2_per_objfile->filenames_cache.emplace ();
4331 htab_up visited (htab_create_alloc (10,
4332 htab_hash_pointer, htab_eq_pointer,
4333 NULL, xcalloc, xfree));
4335 /* The rule is CUs specify all the files, including those used
4336 by any TU, so there's no need to scan TUs here. We can
4337 ignore file names coming from already-expanded CUs. */
4339 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4341 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4343 if (per_cu->v.quick->compunit_symtab)
4345 void **slot = htab_find_slot (visited.get (),
4346 per_cu->v.quick->file_names,
4349 *slot = per_cu->v.quick->file_names;
4353 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4356 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4357 struct quick_file_names *file_data;
4360 /* We only need to look at symtabs not already expanded. */
4361 if (per_cu->v.quick->compunit_symtab)
4364 file_data = dw2_get_file_names (per_cu);
4365 if (file_data == NULL)
4368 slot = htab_find_slot (visited.get (), file_data, INSERT);
4371 /* Already visited. */
4376 for (int j = 0; j < file_data->num_file_names; ++j)
4378 const char *filename = file_data->file_names[j];
4379 dwarf2_per_objfile->filenames_cache->seen (filename);
4384 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4386 gdb::unique_xmalloc_ptr<char> this_real_name;
4389 this_real_name = gdb_realpath (filename);
4390 (*fun) (filename, this_real_name.get (), data);
4395 dw2_has_symbols (struct objfile *objfile)
4400 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4403 dw2_find_last_source_symtab,
4404 dw2_forget_cached_source_info,
4405 dw2_map_symtabs_matching_filename,
4410 dw2_expand_symtabs_for_function,
4411 dw2_expand_all_symtabs,
4412 dw2_expand_symtabs_with_fullname,
4413 dw2_map_matching_symbols,
4414 dw2_expand_symtabs_matching,
4415 dw2_find_pc_sect_compunit_symtab,
4416 dw2_map_symbol_filenames
4419 /* Initialize for reading DWARF for this objfile. Return 0 if this
4420 file will use psymtabs, or 1 if using the GNU index. */
4423 dwarf2_initialize_objfile (struct objfile *objfile)
4425 /* If we're about to read full symbols, don't bother with the
4426 indices. In this case we also don't care if some other debug
4427 format is making psymtabs, because they are all about to be
4429 if ((objfile->flags & OBJF_READNOW))
4433 dwarf2_per_objfile->using_index = 1;
4434 create_all_comp_units (objfile);
4435 create_all_type_units (objfile);
4436 dwarf2_per_objfile->quick_file_names_table =
4437 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4439 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4440 + dwarf2_per_objfile->n_type_units); ++i)
4442 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4444 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4445 struct dwarf2_per_cu_quick_data);
4448 /* Return 1 so that gdb sees the "quick" functions. However,
4449 these functions will be no-ops because we will have expanded
4454 if (dwarf2_read_index (objfile))
4462 /* Build a partial symbol table. */
4465 dwarf2_build_psymtabs (struct objfile *objfile)
4468 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4470 init_psymbol_list (objfile, 1024);
4475 /* This isn't really ideal: all the data we allocate on the
4476 objfile's obstack is still uselessly kept around. However,
4477 freeing it seems unsafe. */
4478 psymtab_discarder psymtabs (objfile);
4479 dwarf2_build_psymtabs_hard (objfile);
4482 CATCH (except, RETURN_MASK_ERROR)
4484 exception_print (gdb_stderr, except);
4489 /* Return the total length of the CU described by HEADER. */
4492 get_cu_length (const struct comp_unit_head *header)
4494 return header->initial_length_size + header->length;
4497 /* Return TRUE if SECT_OFF is within CU_HEADER. */
4500 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
4502 sect_offset bottom = cu_header->sect_off;
4503 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
4505 return sect_off >= bottom && sect_off < top;
4508 /* Find the base address of the compilation unit for range lists and
4509 location lists. It will normally be specified by DW_AT_low_pc.
4510 In DWARF-3 draft 4, the base address could be overridden by
4511 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4512 compilation units with discontinuous ranges. */
4515 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4517 struct attribute *attr;
4520 cu->base_address = 0;
4522 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4525 cu->base_address = attr_value_as_address (attr);
4530 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4533 cu->base_address = attr_value_as_address (attr);
4539 /* Read in the comp unit header information from the debug_info at info_ptr.
4540 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4541 NOTE: This leaves members offset, first_die_offset to be filled in
4544 static const gdb_byte *
4545 read_comp_unit_head (struct comp_unit_head *cu_header,
4546 const gdb_byte *info_ptr,
4547 struct dwarf2_section_info *section,
4548 rcuh_kind section_kind)
4551 unsigned int bytes_read;
4552 const char *filename = get_section_file_name (section);
4553 bfd *abfd = get_section_bfd_owner (section);
4555 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4556 cu_header->initial_length_size = bytes_read;
4557 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4558 info_ptr += bytes_read;
4559 cu_header->version = read_2_bytes (abfd, info_ptr);
4561 if (cu_header->version < 5)
4562 switch (section_kind)
4564 case rcuh_kind::COMPILE:
4565 cu_header->unit_type = DW_UT_compile;
4567 case rcuh_kind::TYPE:
4568 cu_header->unit_type = DW_UT_type;
4571 internal_error (__FILE__, __LINE__,
4572 _("read_comp_unit_head: invalid section_kind"));
4576 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4577 (read_1_byte (abfd, info_ptr));
4579 switch (cu_header->unit_type)
4582 if (section_kind != rcuh_kind::COMPILE)
4583 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4584 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4588 section_kind = rcuh_kind::TYPE;
4591 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4592 "(is %d, should be %d or %d) [in module %s]"),
4593 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4596 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4599 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4602 info_ptr += bytes_read;
4603 if (cu_header->version < 5)
4605 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4608 signed_addr = bfd_get_sign_extend_vma (abfd);
4609 if (signed_addr < 0)
4610 internal_error (__FILE__, __LINE__,
4611 _("read_comp_unit_head: dwarf from non elf file"));
4612 cu_header->signed_addr_p = signed_addr;
4614 if (section_kind == rcuh_kind::TYPE)
4616 LONGEST type_offset;
4618 cu_header->signature = read_8_bytes (abfd, info_ptr);
4621 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4622 info_ptr += bytes_read;
4623 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4624 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
4625 error (_("Dwarf Error: Too big type_offset in compilation unit "
4626 "header (is %s) [in module %s]"), plongest (type_offset),
4633 /* Helper function that returns the proper abbrev section for
4636 static struct dwarf2_section_info *
4637 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4639 struct dwarf2_section_info *abbrev;
4641 if (this_cu->is_dwz)
4642 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4644 abbrev = &dwarf2_per_objfile->abbrev;
4649 /* Subroutine of read_and_check_comp_unit_head and
4650 read_and_check_type_unit_head to simplify them.
4651 Perform various error checking on the header. */
4654 error_check_comp_unit_head (struct comp_unit_head *header,
4655 struct dwarf2_section_info *section,
4656 struct dwarf2_section_info *abbrev_section)
4658 const char *filename = get_section_file_name (section);
4660 if (header->version < 2 || header->version > 5)
4661 error (_("Dwarf Error: wrong version in compilation unit header "
4662 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4665 if (to_underlying (header->abbrev_sect_off)
4666 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4667 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4668 "(offset 0x%x + 6) [in module %s]"),
4669 to_underlying (header->abbrev_sect_off),
4670 to_underlying (header->sect_off),
4673 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
4674 avoid potential 32-bit overflow. */
4675 if (((ULONGEST) header->sect_off + get_cu_length (header))
4677 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4678 "(offset 0x%x + 0) [in module %s]"),
4679 header->length, to_underlying (header->sect_off),
4683 /* Read in a CU/TU header and perform some basic error checking.
4684 The contents of the header are stored in HEADER.
4685 The result is a pointer to the start of the first DIE. */
4687 static const gdb_byte *
4688 read_and_check_comp_unit_head (struct comp_unit_head *header,
4689 struct dwarf2_section_info *section,
4690 struct dwarf2_section_info *abbrev_section,
4691 const gdb_byte *info_ptr,
4692 rcuh_kind section_kind)
4694 const gdb_byte *beg_of_comp_unit = info_ptr;
4695 bfd *abfd = get_section_bfd_owner (section);
4697 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
4699 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4701 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
4703 error_check_comp_unit_head (header, section, abbrev_section);
4708 /* Fetch the abbreviation table offset from a comp or type unit header. */
4711 read_abbrev_offset (struct dwarf2_section_info *section,
4712 sect_offset sect_off)
4714 bfd *abfd = get_section_bfd_owner (section);
4715 const gdb_byte *info_ptr;
4716 unsigned int initial_length_size, offset_size;
4719 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4720 info_ptr = section->buffer + to_underlying (sect_off);
4721 read_initial_length (abfd, info_ptr, &initial_length_size);
4722 offset_size = initial_length_size == 4 ? 4 : 8;
4723 info_ptr += initial_length_size;
4725 version = read_2_bytes (abfd, info_ptr);
4729 /* Skip unit type and address size. */
4733 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
4736 /* Allocate a new partial symtab for file named NAME and mark this new
4737 partial symtab as being an include of PST. */
4740 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4741 struct objfile *objfile)
4743 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4745 if (!IS_ABSOLUTE_PATH (subpst->filename))
4747 /* It shares objfile->objfile_obstack. */
4748 subpst->dirname = pst->dirname;
4751 subpst->textlow = 0;
4752 subpst->texthigh = 0;
4754 subpst->dependencies
4755 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4756 subpst->dependencies[0] = pst;
4757 subpst->number_of_dependencies = 1;
4759 subpst->globals_offset = 0;
4760 subpst->n_global_syms = 0;
4761 subpst->statics_offset = 0;
4762 subpst->n_static_syms = 0;
4763 subpst->compunit_symtab = NULL;
4764 subpst->read_symtab = pst->read_symtab;
4767 /* No private part is necessary for include psymtabs. This property
4768 can be used to differentiate between such include psymtabs and
4769 the regular ones. */
4770 subpst->read_symtab_private = NULL;
4773 /* Read the Line Number Program data and extract the list of files
4774 included by the source file represented by PST. Build an include
4775 partial symtab for each of these included files. */
4778 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4779 struct die_info *die,
4780 struct partial_symtab *pst)
4783 struct attribute *attr;
4785 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4787 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
4789 return; /* No linetable, so no includes. */
4791 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4792 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
4796 hash_signatured_type (const void *item)
4798 const struct signatured_type *sig_type
4799 = (const struct signatured_type *) item;
4801 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4802 return sig_type->signature;
4806 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4808 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4809 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4811 return lhs->signature == rhs->signature;
4814 /* Allocate a hash table for signatured types. */
4817 allocate_signatured_type_table (struct objfile *objfile)
4819 return htab_create_alloc_ex (41,
4820 hash_signatured_type,
4823 &objfile->objfile_obstack,
4824 hashtab_obstack_allocate,
4825 dummy_obstack_deallocate);
4828 /* A helper function to add a signatured type CU to a table. */
4831 add_signatured_type_cu_to_table (void **slot, void *datum)
4833 struct signatured_type *sigt = (struct signatured_type *) *slot;
4834 struct signatured_type ***datap = (struct signatured_type ***) datum;
4842 /* A helper for create_debug_types_hash_table. Read types from SECTION
4843 and fill them into TYPES_HTAB. It will process only type units,
4844 therefore DW_UT_type. */
4847 create_debug_type_hash_table (struct dwo_file *dwo_file,
4848 dwarf2_section_info *section, htab_t &types_htab,
4849 rcuh_kind section_kind)
4851 struct objfile *objfile = dwarf2_per_objfile->objfile;
4852 struct dwarf2_section_info *abbrev_section;
4854 const gdb_byte *info_ptr, *end_ptr;
4856 abbrev_section = (dwo_file != NULL
4857 ? &dwo_file->sections.abbrev
4858 : &dwarf2_per_objfile->abbrev);
4860 if (dwarf_read_debug)
4861 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4862 get_section_name (section),
4863 get_section_file_name (abbrev_section));
4865 dwarf2_read_section (objfile, section);
4866 info_ptr = section->buffer;
4868 if (info_ptr == NULL)
4871 /* We can't set abfd until now because the section may be empty or
4872 not present, in which case the bfd is unknown. */
4873 abfd = get_section_bfd_owner (section);
4875 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4876 because we don't need to read any dies: the signature is in the
4879 end_ptr = info_ptr + section->size;
4880 while (info_ptr < end_ptr)
4882 struct signatured_type *sig_type;
4883 struct dwo_unit *dwo_tu;
4885 const gdb_byte *ptr = info_ptr;
4886 struct comp_unit_head header;
4887 unsigned int length;
4889 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
4891 /* Initialize it due to a false compiler warning. */
4892 header.signature = -1;
4893 header.type_cu_offset_in_tu = (cu_offset) -1;
4895 /* We need to read the type's signature in order to build the hash
4896 table, but we don't need anything else just yet. */
4898 ptr = read_and_check_comp_unit_head (&header, section,
4899 abbrev_section, ptr, section_kind);
4901 length = get_cu_length (&header);
4903 /* Skip dummy type units. */
4904 if (ptr >= info_ptr + length
4905 || peek_abbrev_code (abfd, ptr) == 0
4906 || header.unit_type != DW_UT_type)
4912 if (types_htab == NULL)
4915 types_htab = allocate_dwo_unit_table (objfile);
4917 types_htab = allocate_signatured_type_table (objfile);
4923 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4925 dwo_tu->dwo_file = dwo_file;
4926 dwo_tu->signature = header.signature;
4927 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
4928 dwo_tu->section = section;
4929 dwo_tu->sect_off = sect_off;
4930 dwo_tu->length = length;
4934 /* N.B.: type_offset is not usable if this type uses a DWO file.
4935 The real type_offset is in the DWO file. */
4937 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4938 struct signatured_type);
4939 sig_type->signature = header.signature;
4940 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
4941 sig_type->per_cu.objfile = objfile;
4942 sig_type->per_cu.is_debug_types = 1;
4943 sig_type->per_cu.section = section;
4944 sig_type->per_cu.sect_off = sect_off;
4945 sig_type->per_cu.length = length;
4948 slot = htab_find_slot (types_htab,
4949 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4951 gdb_assert (slot != NULL);
4954 sect_offset dup_sect_off;
4958 const struct dwo_unit *dup_tu
4959 = (const struct dwo_unit *) *slot;
4961 dup_sect_off = dup_tu->sect_off;
4965 const struct signatured_type *dup_tu
4966 = (const struct signatured_type *) *slot;
4968 dup_sect_off = dup_tu->per_cu.sect_off;
4971 complaint (&symfile_complaints,
4972 _("debug type entry at offset 0x%x is duplicate to"
4973 " the entry at offset 0x%x, signature %s"),
4974 to_underlying (sect_off), to_underlying (dup_sect_off),
4975 hex_string (header.signature));
4977 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4979 if (dwarf_read_debug > 1)
4980 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4981 to_underlying (sect_off),
4982 hex_string (header.signature));
4988 /* Create the hash table of all entries in the .debug_types
4989 (or .debug_types.dwo) section(s).
4990 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4991 otherwise it is NULL.
4993 The result is a pointer to the hash table or NULL if there are no types.
4995 Note: This function processes DWO files only, not DWP files. */
4998 create_debug_types_hash_table (struct dwo_file *dwo_file,
4999 VEC (dwarf2_section_info_def) *types,
5003 struct dwarf2_section_info *section;
5005 if (VEC_empty (dwarf2_section_info_def, types))
5009 VEC_iterate (dwarf2_section_info_def, types, ix, section);
5011 create_debug_type_hash_table (dwo_file, section, types_htab,
5015 /* Create the hash table of all entries in the .debug_types section,
5016 and initialize all_type_units.
5017 The result is zero if there is an error (e.g. missing .debug_types section),
5018 otherwise non-zero. */
5021 create_all_type_units (struct objfile *objfile)
5023 htab_t types_htab = NULL;
5024 struct signatured_type **iter;
5026 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
5027 rcuh_kind::COMPILE);
5028 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
5029 if (types_htab == NULL)
5031 dwarf2_per_objfile->signatured_types = NULL;
5035 dwarf2_per_objfile->signatured_types = types_htab;
5037 dwarf2_per_objfile->n_type_units
5038 = dwarf2_per_objfile->n_allocated_type_units
5039 = htab_elements (types_htab);
5040 dwarf2_per_objfile->all_type_units =
5041 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
5042 iter = &dwarf2_per_objfile->all_type_units[0];
5043 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
5044 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
5045 == dwarf2_per_objfile->n_type_units);
5050 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
5051 If SLOT is non-NULL, it is the entry to use in the hash table.
5052 Otherwise we find one. */
5054 static struct signatured_type *
5055 add_type_unit (ULONGEST sig, void **slot)
5057 struct objfile *objfile = dwarf2_per_objfile->objfile;
5058 int n_type_units = dwarf2_per_objfile->n_type_units;
5059 struct signatured_type *sig_type;
5061 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
5063 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
5065 if (dwarf2_per_objfile->n_allocated_type_units == 0)
5066 dwarf2_per_objfile->n_allocated_type_units = 1;
5067 dwarf2_per_objfile->n_allocated_type_units *= 2;
5068 dwarf2_per_objfile->all_type_units
5069 = XRESIZEVEC (struct signatured_type *,
5070 dwarf2_per_objfile->all_type_units,
5071 dwarf2_per_objfile->n_allocated_type_units);
5072 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5074 dwarf2_per_objfile->n_type_units = n_type_units;
5076 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5077 struct signatured_type);
5078 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5079 sig_type->signature = sig;
5080 sig_type->per_cu.is_debug_types = 1;
5081 if (dwarf2_per_objfile->using_index)
5083 sig_type->per_cu.v.quick =
5084 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5085 struct dwarf2_per_cu_quick_data);
5090 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5093 gdb_assert (*slot == NULL);
5095 /* The rest of sig_type must be filled in by the caller. */
5099 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5100 Fill in SIG_ENTRY with DWO_ENTRY. */
5103 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5104 struct signatured_type *sig_entry,
5105 struct dwo_unit *dwo_entry)
5107 /* Make sure we're not clobbering something we don't expect to. */
5108 gdb_assert (! sig_entry->per_cu.queued);
5109 gdb_assert (sig_entry->per_cu.cu == NULL);
5110 if (dwarf2_per_objfile->using_index)
5112 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5113 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5116 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5117 gdb_assert (sig_entry->signature == dwo_entry->signature);
5118 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5119 gdb_assert (sig_entry->type_unit_group == NULL);
5120 gdb_assert (sig_entry->dwo_unit == NULL);
5122 sig_entry->per_cu.section = dwo_entry->section;
5123 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
5124 sig_entry->per_cu.length = dwo_entry->length;
5125 sig_entry->per_cu.reading_dwo_directly = 1;
5126 sig_entry->per_cu.objfile = objfile;
5127 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5128 sig_entry->dwo_unit = dwo_entry;
5131 /* Subroutine of lookup_signatured_type.
5132 If we haven't read the TU yet, create the signatured_type data structure
5133 for a TU to be read in directly from a DWO file, bypassing the stub.
5134 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5135 using .gdb_index, then when reading a CU we want to stay in the DWO file
5136 containing that CU. Otherwise we could end up reading several other DWO
5137 files (due to comdat folding) to process the transitive closure of all the
5138 mentioned TUs, and that can be slow. The current DWO file will have every
5139 type signature that it needs.
5140 We only do this for .gdb_index because in the psymtab case we already have
5141 to read all the DWOs to build the type unit groups. */
5143 static struct signatured_type *
5144 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5146 struct objfile *objfile = dwarf2_per_objfile->objfile;
5147 struct dwo_file *dwo_file;
5148 struct dwo_unit find_dwo_entry, *dwo_entry;
5149 struct signatured_type find_sig_entry, *sig_entry;
5152 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5154 /* If TU skeletons have been removed then we may not have read in any
5156 if (dwarf2_per_objfile->signatured_types == NULL)
5158 dwarf2_per_objfile->signatured_types
5159 = allocate_signatured_type_table (objfile);
5162 /* We only ever need to read in one copy of a signatured type.
5163 Use the global signatured_types array to do our own comdat-folding
5164 of types. If this is the first time we're reading this TU, and
5165 the TU has an entry in .gdb_index, replace the recorded data from
5166 .gdb_index with this TU. */
5168 find_sig_entry.signature = sig;
5169 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5170 &find_sig_entry, INSERT);
5171 sig_entry = (struct signatured_type *) *slot;
5173 /* We can get here with the TU already read, *or* in the process of being
5174 read. Don't reassign the global entry to point to this DWO if that's
5175 the case. Also note that if the TU is already being read, it may not
5176 have come from a DWO, the program may be a mix of Fission-compiled
5177 code and non-Fission-compiled code. */
5179 /* Have we already tried to read this TU?
5180 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5181 needn't exist in the global table yet). */
5182 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5185 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5186 dwo_unit of the TU itself. */
5187 dwo_file = cu->dwo_unit->dwo_file;
5189 /* Ok, this is the first time we're reading this TU. */
5190 if (dwo_file->tus == NULL)
5192 find_dwo_entry.signature = sig;
5193 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5194 if (dwo_entry == NULL)
5197 /* If the global table doesn't have an entry for this TU, add one. */
5198 if (sig_entry == NULL)
5199 sig_entry = add_type_unit (sig, slot);
5201 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5202 sig_entry->per_cu.tu_read = 1;
5206 /* Subroutine of lookup_signatured_type.
5207 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5208 then try the DWP file. If the TU stub (skeleton) has been removed then
5209 it won't be in .gdb_index. */
5211 static struct signatured_type *
5212 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5214 struct objfile *objfile = dwarf2_per_objfile->objfile;
5215 struct dwp_file *dwp_file = get_dwp_file ();
5216 struct dwo_unit *dwo_entry;
5217 struct signatured_type find_sig_entry, *sig_entry;
5220 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5221 gdb_assert (dwp_file != NULL);
5223 /* If TU skeletons have been removed then we may not have read in any
5225 if (dwarf2_per_objfile->signatured_types == NULL)
5227 dwarf2_per_objfile->signatured_types
5228 = allocate_signatured_type_table (objfile);
5231 find_sig_entry.signature = sig;
5232 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5233 &find_sig_entry, INSERT);
5234 sig_entry = (struct signatured_type *) *slot;
5236 /* Have we already tried to read this TU?
5237 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5238 needn't exist in the global table yet). */
5239 if (sig_entry != NULL)
5242 if (dwp_file->tus == NULL)
5244 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5245 sig, 1 /* is_debug_types */);
5246 if (dwo_entry == NULL)
5249 sig_entry = add_type_unit (sig, slot);
5250 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5255 /* Lookup a signature based type for DW_FORM_ref_sig8.
5256 Returns NULL if signature SIG is not present in the table.
5257 It is up to the caller to complain about this. */
5259 static struct signatured_type *
5260 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5263 && dwarf2_per_objfile->using_index)
5265 /* We're in a DWO/DWP file, and we're using .gdb_index.
5266 These cases require special processing. */
5267 if (get_dwp_file () == NULL)
5268 return lookup_dwo_signatured_type (cu, sig);
5270 return lookup_dwp_signatured_type (cu, sig);
5274 struct signatured_type find_entry, *entry;
5276 if (dwarf2_per_objfile->signatured_types == NULL)
5278 find_entry.signature = sig;
5279 entry = ((struct signatured_type *)
5280 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5285 /* Low level DIE reading support. */
5287 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5290 init_cu_die_reader (struct die_reader_specs *reader,
5291 struct dwarf2_cu *cu,
5292 struct dwarf2_section_info *section,
5293 struct dwo_file *dwo_file)
5295 gdb_assert (section->readin && section->buffer != NULL);
5296 reader->abfd = get_section_bfd_owner (section);
5298 reader->dwo_file = dwo_file;
5299 reader->die_section = section;
5300 reader->buffer = section->buffer;
5301 reader->buffer_end = section->buffer + section->size;
5302 reader->comp_dir = NULL;
5305 /* Subroutine of init_cutu_and_read_dies to simplify it.
5306 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5307 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5310 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5311 from it to the DIE in the DWO. If NULL we are skipping the stub.
5312 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5313 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5314 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5315 STUB_COMP_DIR may be non-NULL.
5316 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5317 are filled in with the info of the DIE from the DWO file.
5318 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5319 provided an abbrev table to use.
5320 The result is non-zero if a valid (non-dummy) DIE was found. */
5323 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5324 struct dwo_unit *dwo_unit,
5325 int abbrev_table_provided,
5326 struct die_info *stub_comp_unit_die,
5327 const char *stub_comp_dir,
5328 struct die_reader_specs *result_reader,
5329 const gdb_byte **result_info_ptr,
5330 struct die_info **result_comp_unit_die,
5331 int *result_has_children)
5333 struct objfile *objfile = dwarf2_per_objfile->objfile;
5334 struct dwarf2_cu *cu = this_cu->cu;
5335 struct dwarf2_section_info *section;
5337 const gdb_byte *begin_info_ptr, *info_ptr;
5338 ULONGEST signature; /* Or dwo_id. */
5339 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5340 int i,num_extra_attrs;
5341 struct dwarf2_section_info *dwo_abbrev_section;
5342 struct attribute *attr;
5343 struct die_info *comp_unit_die;
5345 /* At most one of these may be provided. */
5346 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5348 /* These attributes aren't processed until later:
5349 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5350 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5351 referenced later. However, these attributes are found in the stub
5352 which we won't have later. In order to not impose this complication
5353 on the rest of the code, we read them here and copy them to the
5362 if (stub_comp_unit_die != NULL)
5364 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5366 if (! this_cu->is_debug_types)
5367 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5368 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5369 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5370 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5371 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5373 /* There should be a DW_AT_addr_base attribute here (if needed).
5374 We need the value before we can process DW_FORM_GNU_addr_index. */
5376 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5378 cu->addr_base = DW_UNSND (attr);
5380 /* There should be a DW_AT_ranges_base attribute here (if needed).
5381 We need the value before we can process DW_AT_ranges. */
5382 cu->ranges_base = 0;
5383 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5385 cu->ranges_base = DW_UNSND (attr);
5387 else if (stub_comp_dir != NULL)
5389 /* Reconstruct the comp_dir attribute to simplify the code below. */
5390 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5391 comp_dir->name = DW_AT_comp_dir;
5392 comp_dir->form = DW_FORM_string;
5393 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5394 DW_STRING (comp_dir) = stub_comp_dir;
5397 /* Set up for reading the DWO CU/TU. */
5398 cu->dwo_unit = dwo_unit;
5399 section = dwo_unit->section;
5400 dwarf2_read_section (objfile, section);
5401 abfd = get_section_bfd_owner (section);
5402 begin_info_ptr = info_ptr = (section->buffer
5403 + to_underlying (dwo_unit->sect_off));
5404 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5405 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5407 if (this_cu->is_debug_types)
5409 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5411 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5413 info_ptr, rcuh_kind::TYPE);
5414 /* This is not an assert because it can be caused by bad debug info. */
5415 if (sig_type->signature != cu->header.signature)
5417 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5418 " TU at offset 0x%x [in module %s]"),
5419 hex_string (sig_type->signature),
5420 hex_string (cu->header.signature),
5421 to_underlying (dwo_unit->sect_off),
5422 bfd_get_filename (abfd));
5424 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5425 /* For DWOs coming from DWP files, we don't know the CU length
5426 nor the type's offset in the TU until now. */
5427 dwo_unit->length = get_cu_length (&cu->header);
5428 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5430 /* Establish the type offset that can be used to lookup the type.
5431 For DWO files, we don't know it until now. */
5432 sig_type->type_offset_in_section
5433 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5437 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5439 info_ptr, rcuh_kind::COMPILE);
5440 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5441 /* For DWOs coming from DWP files, we don't know the CU length
5443 dwo_unit->length = get_cu_length (&cu->header);
5446 /* Replace the CU's original abbrev table with the DWO's.
5447 Reminder: We can't read the abbrev table until we've read the header. */
5448 if (abbrev_table_provided)
5450 /* Don't free the provided abbrev table, the caller of
5451 init_cutu_and_read_dies owns it. */
5452 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5453 /* Ensure the DWO abbrev table gets freed. */
5454 make_cleanup (dwarf2_free_abbrev_table, cu);
5458 dwarf2_free_abbrev_table (cu);
5459 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5460 /* Leave any existing abbrev table cleanup as is. */
5463 /* Read in the die, but leave space to copy over the attributes
5464 from the stub. This has the benefit of simplifying the rest of
5465 the code - all the work to maintain the illusion of a single
5466 DW_TAG_{compile,type}_unit DIE is done here. */
5467 num_extra_attrs = ((stmt_list != NULL)
5471 + (comp_dir != NULL));
5472 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5473 result_has_children, num_extra_attrs);
5475 /* Copy over the attributes from the stub to the DIE we just read in. */
5476 comp_unit_die = *result_comp_unit_die;
5477 i = comp_unit_die->num_attrs;
5478 if (stmt_list != NULL)
5479 comp_unit_die->attrs[i++] = *stmt_list;
5481 comp_unit_die->attrs[i++] = *low_pc;
5482 if (high_pc != NULL)
5483 comp_unit_die->attrs[i++] = *high_pc;
5485 comp_unit_die->attrs[i++] = *ranges;
5486 if (comp_dir != NULL)
5487 comp_unit_die->attrs[i++] = *comp_dir;
5488 comp_unit_die->num_attrs += num_extra_attrs;
5490 if (dwarf_die_debug)
5492 fprintf_unfiltered (gdb_stdlog,
5493 "Read die from %s@0x%x of %s:\n",
5494 get_section_name (section),
5495 (unsigned) (begin_info_ptr - section->buffer),
5496 bfd_get_filename (abfd));
5497 dump_die (comp_unit_die, dwarf_die_debug);
5500 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5501 TUs by skipping the stub and going directly to the entry in the DWO file.
5502 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5503 to get it via circuitous means. Blech. */
5504 if (comp_dir != NULL)
5505 result_reader->comp_dir = DW_STRING (comp_dir);
5507 /* Skip dummy compilation units. */
5508 if (info_ptr >= begin_info_ptr + dwo_unit->length
5509 || peek_abbrev_code (abfd, info_ptr) == 0)
5512 *result_info_ptr = info_ptr;
5516 /* Subroutine of init_cutu_and_read_dies to simplify it.
5517 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5518 Returns NULL if the specified DWO unit cannot be found. */
5520 static struct dwo_unit *
5521 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5522 struct die_info *comp_unit_die)
5524 struct dwarf2_cu *cu = this_cu->cu;
5525 struct attribute *attr;
5527 struct dwo_unit *dwo_unit;
5528 const char *comp_dir, *dwo_name;
5530 gdb_assert (cu != NULL);
5532 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5533 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5534 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5536 if (this_cu->is_debug_types)
5538 struct signatured_type *sig_type;
5540 /* Since this_cu is the first member of struct signatured_type,
5541 we can go from a pointer to one to a pointer to the other. */
5542 sig_type = (struct signatured_type *) this_cu;
5543 signature = sig_type->signature;
5544 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5548 struct attribute *attr;
5550 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5552 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5554 dwo_name, objfile_name (this_cu->objfile));
5555 signature = DW_UNSND (attr);
5556 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5563 /* Subroutine of init_cutu_and_read_dies to simplify it.
5564 See it for a description of the parameters.
5565 Read a TU directly from a DWO file, bypassing the stub.
5567 Note: This function could be a little bit simpler if we shared cleanups
5568 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5569 to do, so we keep this function self-contained. Or we could move this
5570 into our caller, but it's complex enough already. */
5573 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5574 int use_existing_cu, int keep,
5575 die_reader_func_ftype *die_reader_func,
5578 struct dwarf2_cu *cu;
5579 struct signatured_type *sig_type;
5580 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5581 struct die_reader_specs reader;
5582 const gdb_byte *info_ptr;
5583 struct die_info *comp_unit_die;
5586 /* Verify we can do the following downcast, and that we have the
5588 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5589 sig_type = (struct signatured_type *) this_cu;
5590 gdb_assert (sig_type->dwo_unit != NULL);
5592 cleanups = make_cleanup (null_cleanup, NULL);
5594 if (use_existing_cu && this_cu->cu != NULL)
5596 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5598 /* There's no need to do the rereading_dwo_cu handling that
5599 init_cutu_and_read_dies does since we don't read the stub. */
5603 /* If !use_existing_cu, this_cu->cu must be NULL. */
5604 gdb_assert (this_cu->cu == NULL);
5605 cu = XNEW (struct dwarf2_cu);
5606 init_one_comp_unit (cu, this_cu);
5607 /* If an error occurs while loading, release our storage. */
5608 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5611 /* A future optimization, if needed, would be to use an existing
5612 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5613 could share abbrev tables. */
5615 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5616 0 /* abbrev_table_provided */,
5617 NULL /* stub_comp_unit_die */,
5618 sig_type->dwo_unit->dwo_file->comp_dir,
5620 &comp_unit_die, &has_children) == 0)
5623 do_cleanups (cleanups);
5627 /* All the "real" work is done here. */
5628 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5630 /* This duplicates the code in init_cutu_and_read_dies,
5631 but the alternative is making the latter more complex.
5632 This function is only for the special case of using DWO files directly:
5633 no point in overly complicating the general case just to handle this. */
5634 if (free_cu_cleanup != NULL)
5638 /* We've successfully allocated this compilation unit. Let our
5639 caller clean it up when finished with it. */
5640 discard_cleanups (free_cu_cleanup);
5642 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5643 So we have to manually free the abbrev table. */
5644 dwarf2_free_abbrev_table (cu);
5646 /* Link this CU into read_in_chain. */
5647 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5648 dwarf2_per_objfile->read_in_chain = this_cu;
5651 do_cleanups (free_cu_cleanup);
5654 do_cleanups (cleanups);
5657 /* Initialize a CU (or TU) and read its DIEs.
5658 If the CU defers to a DWO file, read the DWO file as well.
5660 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5661 Otherwise the table specified in the comp unit header is read in and used.
5662 This is an optimization for when we already have the abbrev table.
5664 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5665 Otherwise, a new CU is allocated with xmalloc.
5667 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5668 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5670 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5671 linker) then DIE_READER_FUNC will not get called. */
5674 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5675 struct abbrev_table *abbrev_table,
5676 int use_existing_cu, int keep,
5677 die_reader_func_ftype *die_reader_func,
5680 struct objfile *objfile = dwarf2_per_objfile->objfile;
5681 struct dwarf2_section_info *section = this_cu->section;
5682 bfd *abfd = get_section_bfd_owner (section);
5683 struct dwarf2_cu *cu;
5684 const gdb_byte *begin_info_ptr, *info_ptr;
5685 struct die_reader_specs reader;
5686 struct die_info *comp_unit_die;
5688 struct attribute *attr;
5689 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5690 struct signatured_type *sig_type = NULL;
5691 struct dwarf2_section_info *abbrev_section;
5692 /* Non-zero if CU currently points to a DWO file and we need to
5693 reread it. When this happens we need to reread the skeleton die
5694 before we can reread the DWO file (this only applies to CUs, not TUs). */
5695 int rereading_dwo_cu = 0;
5697 if (dwarf_die_debug)
5698 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5699 this_cu->is_debug_types ? "type" : "comp",
5700 to_underlying (this_cu->sect_off));
5702 if (use_existing_cu)
5705 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5706 file (instead of going through the stub), short-circuit all of this. */
5707 if (this_cu->reading_dwo_directly)
5709 /* Narrow down the scope of possibilities to have to understand. */
5710 gdb_assert (this_cu->is_debug_types);
5711 gdb_assert (abbrev_table == NULL);
5712 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5713 die_reader_func, data);
5717 cleanups = make_cleanup (null_cleanup, NULL);
5719 /* This is cheap if the section is already read in. */
5720 dwarf2_read_section (objfile, section);
5722 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5724 abbrev_section = get_abbrev_section_for_cu (this_cu);
5726 if (use_existing_cu && this_cu->cu != NULL)
5729 /* If this CU is from a DWO file we need to start over, we need to
5730 refetch the attributes from the skeleton CU.
5731 This could be optimized by retrieving those attributes from when we
5732 were here the first time: the previous comp_unit_die was stored in
5733 comp_unit_obstack. But there's no data yet that we need this
5735 if (cu->dwo_unit != NULL)
5736 rereading_dwo_cu = 1;
5740 /* If !use_existing_cu, this_cu->cu must be NULL. */
5741 gdb_assert (this_cu->cu == NULL);
5742 cu = XNEW (struct dwarf2_cu);
5743 init_one_comp_unit (cu, this_cu);
5744 /* If an error occurs while loading, release our storage. */
5745 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5748 /* Get the header. */
5749 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
5751 /* We already have the header, there's no need to read it in again. */
5752 info_ptr += to_underlying (cu->header.first_die_cu_offset);
5756 if (this_cu->is_debug_types)
5758 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5759 abbrev_section, info_ptr,
5762 /* Since per_cu is the first member of struct signatured_type,
5763 we can go from a pointer to one to a pointer to the other. */
5764 sig_type = (struct signatured_type *) this_cu;
5765 gdb_assert (sig_type->signature == cu->header.signature);
5766 gdb_assert (sig_type->type_offset_in_tu
5767 == cu->header.type_cu_offset_in_tu);
5768 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5770 /* LENGTH has not been set yet for type units if we're
5771 using .gdb_index. */
5772 this_cu->length = get_cu_length (&cu->header);
5774 /* Establish the type offset that can be used to lookup the type. */
5775 sig_type->type_offset_in_section =
5776 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
5778 this_cu->dwarf_version = cu->header.version;
5782 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5785 rcuh_kind::COMPILE);
5787 gdb_assert (this_cu->sect_off == cu->header.sect_off);
5788 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5789 this_cu->dwarf_version = cu->header.version;
5793 /* Skip dummy compilation units. */
5794 if (info_ptr >= begin_info_ptr + this_cu->length
5795 || peek_abbrev_code (abfd, info_ptr) == 0)
5797 do_cleanups (cleanups);
5801 /* If we don't have them yet, read the abbrevs for this compilation unit.
5802 And if we need to read them now, make sure they're freed when we're
5803 done. Note that it's important that if the CU had an abbrev table
5804 on entry we don't free it when we're done: Somewhere up the call stack
5805 it may be in use. */
5806 if (abbrev_table != NULL)
5808 gdb_assert (cu->abbrev_table == NULL);
5809 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
5810 cu->abbrev_table = abbrev_table;
5812 else if (cu->abbrev_table == NULL)
5814 dwarf2_read_abbrevs (cu, abbrev_section);
5815 make_cleanup (dwarf2_free_abbrev_table, cu);
5817 else if (rereading_dwo_cu)
5819 dwarf2_free_abbrev_table (cu);
5820 dwarf2_read_abbrevs (cu, abbrev_section);
5823 /* Read the top level CU/TU die. */
5824 init_cu_die_reader (&reader, cu, section, NULL);
5825 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5827 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5829 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5830 DWO CU, that this test will fail (the attribute will not be present). */
5831 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5834 struct dwo_unit *dwo_unit;
5835 struct die_info *dwo_comp_unit_die;
5839 complaint (&symfile_complaints,
5840 _("compilation unit with DW_AT_GNU_dwo_name"
5841 " has children (offset 0x%x) [in module %s]"),
5842 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
5844 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5845 if (dwo_unit != NULL)
5847 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5848 abbrev_table != NULL,
5849 comp_unit_die, NULL,
5851 &dwo_comp_unit_die, &has_children) == 0)
5854 do_cleanups (cleanups);
5857 comp_unit_die = dwo_comp_unit_die;
5861 /* Yikes, we couldn't find the rest of the DIE, we only have
5862 the stub. A complaint has already been logged. There's
5863 not much more we can do except pass on the stub DIE to
5864 die_reader_func. We don't want to throw an error on bad
5869 /* All of the above is setup for this call. Yikes. */
5870 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5872 /* Done, clean up. */
5873 if (free_cu_cleanup != NULL)
5877 /* We've successfully allocated this compilation unit. Let our
5878 caller clean it up when finished with it. */
5879 discard_cleanups (free_cu_cleanup);
5881 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5882 So we have to manually free the abbrev table. */
5883 dwarf2_free_abbrev_table (cu);
5885 /* Link this CU into read_in_chain. */
5886 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5887 dwarf2_per_objfile->read_in_chain = this_cu;
5890 do_cleanups (free_cu_cleanup);
5893 do_cleanups (cleanups);
5896 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5897 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5898 to have already done the lookup to find the DWO file).
5900 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5901 THIS_CU->is_debug_types, but nothing else.
5903 We fill in THIS_CU->length.
5905 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5906 linker) then DIE_READER_FUNC will not get called.
5908 THIS_CU->cu is always freed when done.
5909 This is done in order to not leave THIS_CU->cu in a state where we have
5910 to care whether it refers to the "main" CU or the DWO CU. */
5913 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5914 struct dwo_file *dwo_file,
5915 die_reader_func_ftype *die_reader_func,
5918 struct objfile *objfile = dwarf2_per_objfile->objfile;
5919 struct dwarf2_section_info *section = this_cu->section;
5920 bfd *abfd = get_section_bfd_owner (section);
5921 struct dwarf2_section_info *abbrev_section;
5922 struct dwarf2_cu cu;
5923 const gdb_byte *begin_info_ptr, *info_ptr;
5924 struct die_reader_specs reader;
5925 struct cleanup *cleanups;
5926 struct die_info *comp_unit_die;
5929 if (dwarf_die_debug)
5930 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5931 this_cu->is_debug_types ? "type" : "comp",
5932 to_underlying (this_cu->sect_off));
5934 gdb_assert (this_cu->cu == NULL);
5936 abbrev_section = (dwo_file != NULL
5937 ? &dwo_file->sections.abbrev
5938 : get_abbrev_section_for_cu (this_cu));
5940 /* This is cheap if the section is already read in. */
5941 dwarf2_read_section (objfile, section);
5943 init_one_comp_unit (&cu, this_cu);
5945 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5947 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
5948 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5949 abbrev_section, info_ptr,
5950 (this_cu->is_debug_types
5952 : rcuh_kind::COMPILE));
5954 this_cu->length = get_cu_length (&cu.header);
5956 /* Skip dummy compilation units. */
5957 if (info_ptr >= begin_info_ptr + this_cu->length
5958 || peek_abbrev_code (abfd, info_ptr) == 0)
5960 do_cleanups (cleanups);
5964 dwarf2_read_abbrevs (&cu, abbrev_section);
5965 make_cleanup (dwarf2_free_abbrev_table, &cu);
5967 init_cu_die_reader (&reader, &cu, section, dwo_file);
5968 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5970 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5972 do_cleanups (cleanups);
5975 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5976 does not lookup the specified DWO file.
5977 This cannot be used to read DWO files.
5979 THIS_CU->cu is always freed when done.
5980 This is done in order to not leave THIS_CU->cu in a state where we have
5981 to care whether it refers to the "main" CU or the DWO CU.
5982 We can revisit this if the data shows there's a performance issue. */
5985 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5986 die_reader_func_ftype *die_reader_func,
5989 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5992 /* Type Unit Groups.
5994 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5995 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5996 so that all types coming from the same compilation (.o file) are grouped
5997 together. A future step could be to put the types in the same symtab as
5998 the CU the types ultimately came from. */
6001 hash_type_unit_group (const void *item)
6003 const struct type_unit_group *tu_group
6004 = (const struct type_unit_group *) item;
6006 return hash_stmt_list_entry (&tu_group->hash);
6010 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6012 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6013 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6015 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6018 /* Allocate a hash table for type unit groups. */
6021 allocate_type_unit_groups_table (void)
6023 return htab_create_alloc_ex (3,
6024 hash_type_unit_group,
6027 &dwarf2_per_objfile->objfile->objfile_obstack,
6028 hashtab_obstack_allocate,
6029 dummy_obstack_deallocate);
6032 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6033 partial symtabs. We combine several TUs per psymtab to not let the size
6034 of any one psymtab grow too big. */
6035 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6036 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6038 /* Helper routine for get_type_unit_group.
6039 Create the type_unit_group object used to hold one or more TUs. */
6041 static struct type_unit_group *
6042 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6044 struct objfile *objfile = dwarf2_per_objfile->objfile;
6045 struct dwarf2_per_cu_data *per_cu;
6046 struct type_unit_group *tu_group;
6048 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6049 struct type_unit_group);
6050 per_cu = &tu_group->per_cu;
6051 per_cu->objfile = objfile;
6053 if (dwarf2_per_objfile->using_index)
6055 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6056 struct dwarf2_per_cu_quick_data);
6060 unsigned int line_offset = to_underlying (line_offset_struct);
6061 struct partial_symtab *pst;
6064 /* Give the symtab a useful name for debug purposes. */
6065 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6066 name = xstrprintf ("<type_units_%d>",
6067 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6069 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6071 pst = create_partial_symtab (per_cu, name);
6077 tu_group->hash.dwo_unit = cu->dwo_unit;
6078 tu_group->hash.line_sect_off = line_offset_struct;
6083 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6084 STMT_LIST is a DW_AT_stmt_list attribute. */
6086 static struct type_unit_group *
6087 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6089 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6090 struct type_unit_group *tu_group;
6092 unsigned int line_offset;
6093 struct type_unit_group type_unit_group_for_lookup;
6095 if (dwarf2_per_objfile->type_unit_groups == NULL)
6097 dwarf2_per_objfile->type_unit_groups =
6098 allocate_type_unit_groups_table ();
6101 /* Do we need to create a new group, or can we use an existing one? */
6105 line_offset = DW_UNSND (stmt_list);
6106 ++tu_stats->nr_symtab_sharers;
6110 /* Ugh, no stmt_list. Rare, but we have to handle it.
6111 We can do various things here like create one group per TU or
6112 spread them over multiple groups to split up the expansion work.
6113 To avoid worst case scenarios (too many groups or too large groups)
6114 we, umm, group them in bunches. */
6115 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6116 | (tu_stats->nr_stmt_less_type_units
6117 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6118 ++tu_stats->nr_stmt_less_type_units;
6121 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6122 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6123 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6124 &type_unit_group_for_lookup, INSERT);
6127 tu_group = (struct type_unit_group *) *slot;
6128 gdb_assert (tu_group != NULL);
6132 sect_offset line_offset_struct = (sect_offset) line_offset;
6133 tu_group = create_type_unit_group (cu, line_offset_struct);
6135 ++tu_stats->nr_symtabs;
6141 /* Partial symbol tables. */
6143 /* Create a psymtab named NAME and assign it to PER_CU.
6145 The caller must fill in the following details:
6146 dirname, textlow, texthigh. */
6148 static struct partial_symtab *
6149 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6151 struct objfile *objfile = per_cu->objfile;
6152 struct partial_symtab *pst;
6154 pst = start_psymtab_common (objfile, name, 0,
6155 objfile->global_psymbols.next,
6156 objfile->static_psymbols.next);
6158 pst->psymtabs_addrmap_supported = 1;
6160 /* This is the glue that links PST into GDB's symbol API. */
6161 pst->read_symtab_private = per_cu;
6162 pst->read_symtab = dwarf2_read_symtab;
6163 per_cu->v.psymtab = pst;
6168 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6171 struct process_psymtab_comp_unit_data
6173 /* True if we are reading a DW_TAG_partial_unit. */
6175 int want_partial_unit;
6177 /* The "pretend" language that is used if the CU doesn't declare a
6180 enum language pretend_language;
6183 /* die_reader_func for process_psymtab_comp_unit. */
6186 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6187 const gdb_byte *info_ptr,
6188 struct die_info *comp_unit_die,
6192 struct dwarf2_cu *cu = reader->cu;
6193 struct objfile *objfile = cu->objfile;
6194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6195 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6197 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6198 struct partial_symtab *pst;
6199 enum pc_bounds_kind cu_bounds_kind;
6200 const char *filename;
6201 struct process_psymtab_comp_unit_data *info
6202 = (struct process_psymtab_comp_unit_data *) data;
6204 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6207 gdb_assert (! per_cu->is_debug_types);
6209 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6211 cu->list_in_scope = &file_symbols;
6213 /* Allocate a new partial symbol table structure. */
6214 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6215 if (filename == NULL)
6218 pst = create_partial_symtab (per_cu, filename);
6220 /* This must be done before calling dwarf2_build_include_psymtabs. */
6221 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6223 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6225 dwarf2_find_base_address (comp_unit_die, cu);
6227 /* Possibly set the default values of LOWPC and HIGHPC from
6229 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6230 &best_highpc, cu, pst);
6231 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6232 /* Store the contiguous range if it is not empty; it can be empty for
6233 CUs with no code. */
6234 addrmap_set_empty (objfile->psymtabs_addrmap,
6235 gdbarch_adjust_dwarf2_addr (gdbarch,
6236 best_lowpc + baseaddr),
6237 gdbarch_adjust_dwarf2_addr (gdbarch,
6238 best_highpc + baseaddr) - 1,
6241 /* Check if comp unit has_children.
6242 If so, read the rest of the partial symbols from this comp unit.
6243 If not, there's no more debug_info for this comp unit. */
6246 struct partial_die_info *first_die;
6247 CORE_ADDR lowpc, highpc;
6249 lowpc = ((CORE_ADDR) -1);
6250 highpc = ((CORE_ADDR) 0);
6252 first_die = load_partial_dies (reader, info_ptr, 1);
6254 scan_partial_symbols (first_die, &lowpc, &highpc,
6255 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6257 /* If we didn't find a lowpc, set it to highpc to avoid
6258 complaints from `maint check'. */
6259 if (lowpc == ((CORE_ADDR) -1))
6262 /* If the compilation unit didn't have an explicit address range,
6263 then use the information extracted from its child dies. */
6264 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6267 best_highpc = highpc;
6270 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6271 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6273 end_psymtab_common (objfile, pst);
6275 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6278 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6279 struct dwarf2_per_cu_data *iter;
6281 /* Fill in 'dependencies' here; we fill in 'users' in a
6283 pst->number_of_dependencies = len;
6285 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6287 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6290 pst->dependencies[i] = iter->v.psymtab;
6292 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6295 /* Get the list of files included in the current compilation unit,
6296 and build a psymtab for each of them. */
6297 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6299 if (dwarf_read_debug)
6301 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6303 fprintf_unfiltered (gdb_stdlog,
6304 "Psymtab for %s unit @0x%x: %s - %s"
6305 ", %d global, %d static syms\n",
6306 per_cu->is_debug_types ? "type" : "comp",
6307 to_underlying (per_cu->sect_off),
6308 paddress (gdbarch, pst->textlow),
6309 paddress (gdbarch, pst->texthigh),
6310 pst->n_global_syms, pst->n_static_syms);
6314 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6315 Process compilation unit THIS_CU for a psymtab. */
6318 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6319 int want_partial_unit,
6320 enum language pretend_language)
6322 /* If this compilation unit was already read in, free the
6323 cached copy in order to read it in again. This is
6324 necessary because we skipped some symbols when we first
6325 read in the compilation unit (see load_partial_dies).
6326 This problem could be avoided, but the benefit is unclear. */
6327 if (this_cu->cu != NULL)
6328 free_one_cached_comp_unit (this_cu);
6330 if (this_cu->is_debug_types)
6331 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
6335 process_psymtab_comp_unit_data info;
6336 info.want_partial_unit = want_partial_unit;
6337 info.pretend_language = pretend_language;
6338 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6339 process_psymtab_comp_unit_reader, &info);
6342 /* Age out any secondary CUs. */
6343 age_cached_comp_units ();
6346 /* Reader function for build_type_psymtabs. */
6349 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6350 const gdb_byte *info_ptr,
6351 struct die_info *type_unit_die,
6355 struct objfile *objfile = dwarf2_per_objfile->objfile;
6356 struct dwarf2_cu *cu = reader->cu;
6357 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6358 struct signatured_type *sig_type;
6359 struct type_unit_group *tu_group;
6360 struct attribute *attr;
6361 struct partial_die_info *first_die;
6362 CORE_ADDR lowpc, highpc;
6363 struct partial_symtab *pst;
6365 gdb_assert (data == NULL);
6366 gdb_assert (per_cu->is_debug_types);
6367 sig_type = (struct signatured_type *) per_cu;
6372 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6373 tu_group = get_type_unit_group (cu, attr);
6375 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6377 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6378 cu->list_in_scope = &file_symbols;
6379 pst = create_partial_symtab (per_cu, "");
6382 first_die = load_partial_dies (reader, info_ptr, 1);
6384 lowpc = (CORE_ADDR) -1;
6385 highpc = (CORE_ADDR) 0;
6386 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6388 end_psymtab_common (objfile, pst);
6391 /* Struct used to sort TUs by their abbreviation table offset. */
6393 struct tu_abbrev_offset
6395 struct signatured_type *sig_type;
6396 sect_offset abbrev_offset;
6399 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6402 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6404 const struct tu_abbrev_offset * const *a
6405 = (const struct tu_abbrev_offset * const*) ap;
6406 const struct tu_abbrev_offset * const *b
6407 = (const struct tu_abbrev_offset * const*) bp;
6408 sect_offset aoff = (*a)->abbrev_offset;
6409 sect_offset boff = (*b)->abbrev_offset;
6411 return (aoff > boff) - (aoff < boff);
6414 /* Efficiently read all the type units.
6415 This does the bulk of the work for build_type_psymtabs.
6417 The efficiency is because we sort TUs by the abbrev table they use and
6418 only read each abbrev table once. In one program there are 200K TUs
6419 sharing 8K abbrev tables.
6421 The main purpose of this function is to support building the
6422 dwarf2_per_objfile->type_unit_groups table.
6423 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6424 can collapse the search space by grouping them by stmt_list.
6425 The savings can be significant, in the same program from above the 200K TUs
6426 share 8K stmt_list tables.
6428 FUNC is expected to call get_type_unit_group, which will create the
6429 struct type_unit_group if necessary and add it to
6430 dwarf2_per_objfile->type_unit_groups. */
6433 build_type_psymtabs_1 (void)
6435 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6436 struct cleanup *cleanups;
6437 struct abbrev_table *abbrev_table;
6438 sect_offset abbrev_offset;
6439 struct tu_abbrev_offset *sorted_by_abbrev;
6442 /* It's up to the caller to not call us multiple times. */
6443 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6445 if (dwarf2_per_objfile->n_type_units == 0)
6448 /* TUs typically share abbrev tables, and there can be way more TUs than
6449 abbrev tables. Sort by abbrev table to reduce the number of times we
6450 read each abbrev table in.
6451 Alternatives are to punt or to maintain a cache of abbrev tables.
6452 This is simpler and efficient enough for now.
6454 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6455 symtab to use). Typically TUs with the same abbrev offset have the same
6456 stmt_list value too so in practice this should work well.
6458 The basic algorithm here is:
6460 sort TUs by abbrev table
6461 for each TU with same abbrev table:
6462 read abbrev table if first user
6463 read TU top level DIE
6464 [IWBN if DWO skeletons had DW_AT_stmt_list]
6467 if (dwarf_read_debug)
6468 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6470 /* Sort in a separate table to maintain the order of all_type_units
6471 for .gdb_index: TU indices directly index all_type_units. */
6472 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6473 dwarf2_per_objfile->n_type_units);
6474 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6476 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6478 sorted_by_abbrev[i].sig_type = sig_type;
6479 sorted_by_abbrev[i].abbrev_offset =
6480 read_abbrev_offset (sig_type->per_cu.section,
6481 sig_type->per_cu.sect_off);
6483 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6484 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6485 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6487 abbrev_offset = (sect_offset) ~(unsigned) 0;
6488 abbrev_table = NULL;
6489 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6491 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6493 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6495 /* Switch to the next abbrev table if necessary. */
6496 if (abbrev_table == NULL
6497 || tu->abbrev_offset != abbrev_offset)
6499 if (abbrev_table != NULL)
6501 abbrev_table_free (abbrev_table);
6502 /* Reset to NULL in case abbrev_table_read_table throws
6503 an error: abbrev_table_free_cleanup will get called. */
6504 abbrev_table = NULL;
6506 abbrev_offset = tu->abbrev_offset;
6508 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6510 ++tu_stats->nr_uniq_abbrev_tables;
6513 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6514 build_type_psymtabs_reader, NULL);
6517 do_cleanups (cleanups);
6520 /* Print collected type unit statistics. */
6523 print_tu_stats (void)
6525 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6527 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6528 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6529 dwarf2_per_objfile->n_type_units);
6530 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6531 tu_stats->nr_uniq_abbrev_tables);
6532 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6533 tu_stats->nr_symtabs);
6534 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6535 tu_stats->nr_symtab_sharers);
6536 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6537 tu_stats->nr_stmt_less_type_units);
6538 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6539 tu_stats->nr_all_type_units_reallocs);
6542 /* Traversal function for build_type_psymtabs. */
6545 build_type_psymtab_dependencies (void **slot, void *info)
6547 struct objfile *objfile = dwarf2_per_objfile->objfile;
6548 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6549 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6550 struct partial_symtab *pst = per_cu->v.psymtab;
6551 int len = VEC_length (sig_type_ptr, tu_group->tus);
6552 struct signatured_type *iter;
6555 gdb_assert (len > 0);
6556 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6558 pst->number_of_dependencies = len;
6560 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6562 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6565 gdb_assert (iter->per_cu.is_debug_types);
6566 pst->dependencies[i] = iter->per_cu.v.psymtab;
6567 iter->type_unit_group = tu_group;
6570 VEC_free (sig_type_ptr, tu_group->tus);
6575 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6576 Build partial symbol tables for the .debug_types comp-units. */
6579 build_type_psymtabs (struct objfile *objfile)
6581 if (! create_all_type_units (objfile))
6584 build_type_psymtabs_1 ();
6587 /* Traversal function for process_skeletonless_type_unit.
6588 Read a TU in a DWO file and build partial symbols for it. */
6591 process_skeletonless_type_unit (void **slot, void *info)
6593 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6594 struct objfile *objfile = (struct objfile *) info;
6595 struct signatured_type find_entry, *entry;
6597 /* If this TU doesn't exist in the global table, add it and read it in. */
6599 if (dwarf2_per_objfile->signatured_types == NULL)
6601 dwarf2_per_objfile->signatured_types
6602 = allocate_signatured_type_table (objfile);
6605 find_entry.signature = dwo_unit->signature;
6606 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6608 /* If we've already seen this type there's nothing to do. What's happening
6609 is we're doing our own version of comdat-folding here. */
6613 /* This does the job that create_all_type_units would have done for
6615 entry = add_type_unit (dwo_unit->signature, slot);
6616 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6619 /* This does the job that build_type_psymtabs_1 would have done. */
6620 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6621 build_type_psymtabs_reader, NULL);
6626 /* Traversal function for process_skeletonless_type_units. */
6629 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6631 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6633 if (dwo_file->tus != NULL)
6635 htab_traverse_noresize (dwo_file->tus,
6636 process_skeletonless_type_unit, info);
6642 /* Scan all TUs of DWO files, verifying we've processed them.
6643 This is needed in case a TU was emitted without its skeleton.
6644 Note: This can't be done until we know what all the DWO files are. */
6647 process_skeletonless_type_units (struct objfile *objfile)
6649 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6650 if (get_dwp_file () == NULL
6651 && dwarf2_per_objfile->dwo_files != NULL)
6653 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6654 process_dwo_file_for_skeletonless_type_units,
6659 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6662 psymtabs_addrmap_cleanup (void *o)
6664 struct objfile *objfile = (struct objfile *) o;
6666 objfile->psymtabs_addrmap = NULL;
6669 /* Compute the 'user' field for each psymtab in OBJFILE. */
6672 set_partial_user (struct objfile *objfile)
6676 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6678 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6679 struct partial_symtab *pst = per_cu->v.psymtab;
6685 for (j = 0; j < pst->number_of_dependencies; ++j)
6687 /* Set the 'user' field only if it is not already set. */
6688 if (pst->dependencies[j]->user == NULL)
6689 pst->dependencies[j]->user = pst;
6694 /* Build the partial symbol table by doing a quick pass through the
6695 .debug_info and .debug_abbrev sections. */
6698 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6700 struct cleanup *back_to, *addrmap_cleanup;
6703 if (dwarf_read_debug)
6705 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6706 objfile_name (objfile));
6709 dwarf2_per_objfile->reading_partial_symbols = 1;
6711 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6713 /* Any cached compilation units will be linked by the per-objfile
6714 read_in_chain. Make sure to free them when we're done. */
6715 back_to = make_cleanup (free_cached_comp_units, NULL);
6717 build_type_psymtabs (objfile);
6719 create_all_comp_units (objfile);
6721 /* Create a temporary address map on a temporary obstack. We later
6722 copy this to the final obstack. */
6723 auto_obstack temp_obstack;
6724 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6725 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6727 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6729 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6731 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6734 /* This has to wait until we read the CUs, we need the list of DWOs. */
6735 process_skeletonless_type_units (objfile);
6737 /* Now that all TUs have been processed we can fill in the dependencies. */
6738 if (dwarf2_per_objfile->type_unit_groups != NULL)
6740 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6741 build_type_psymtab_dependencies, NULL);
6744 if (dwarf_read_debug)
6747 set_partial_user (objfile);
6749 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6750 &objfile->objfile_obstack);
6751 discard_cleanups (addrmap_cleanup);
6753 do_cleanups (back_to);
6755 if (dwarf_read_debug)
6756 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6757 objfile_name (objfile));
6760 /* die_reader_func for load_partial_comp_unit. */
6763 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6764 const gdb_byte *info_ptr,
6765 struct die_info *comp_unit_die,
6769 struct dwarf2_cu *cu = reader->cu;
6771 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6773 /* Check if comp unit has_children.
6774 If so, read the rest of the partial symbols from this comp unit.
6775 If not, there's no more debug_info for this comp unit. */
6777 load_partial_dies (reader, info_ptr, 0);
6780 /* Load the partial DIEs for a secondary CU into memory.
6781 This is also used when rereading a primary CU with load_all_dies. */
6784 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6786 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6787 load_partial_comp_unit_reader, NULL);
6791 read_comp_units_from_section (struct objfile *objfile,
6792 struct dwarf2_section_info *section,
6793 struct dwarf2_section_info *abbrev_section,
6794 unsigned int is_dwz,
6797 struct dwarf2_per_cu_data ***all_comp_units)
6799 const gdb_byte *info_ptr;
6800 bfd *abfd = get_section_bfd_owner (section);
6802 if (dwarf_read_debug)
6803 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6804 get_section_name (section),
6805 get_section_file_name (section));
6807 dwarf2_read_section (objfile, section);
6809 info_ptr = section->buffer;
6811 while (info_ptr < section->buffer + section->size)
6813 struct dwarf2_per_cu_data *this_cu;
6815 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
6817 comp_unit_head cu_header;
6818 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
6819 info_ptr, rcuh_kind::COMPILE);
6821 /* Save the compilation unit for later lookup. */
6822 if (cu_header.unit_type != DW_UT_type)
6824 this_cu = XOBNEW (&objfile->objfile_obstack,
6825 struct dwarf2_per_cu_data);
6826 memset (this_cu, 0, sizeof (*this_cu));
6830 auto sig_type = XOBNEW (&objfile->objfile_obstack,
6831 struct signatured_type);
6832 memset (sig_type, 0, sizeof (*sig_type));
6833 sig_type->signature = cu_header.signature;
6834 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
6835 this_cu = &sig_type->per_cu;
6837 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
6838 this_cu->sect_off = sect_off;
6839 this_cu->length = cu_header.length + cu_header.initial_length_size;
6840 this_cu->is_dwz = is_dwz;
6841 this_cu->objfile = objfile;
6842 this_cu->section = section;
6844 if (*n_comp_units == *n_allocated)
6847 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6848 *all_comp_units, *n_allocated);
6850 (*all_comp_units)[*n_comp_units] = this_cu;
6853 info_ptr = info_ptr + this_cu->length;
6857 /* Create a list of all compilation units in OBJFILE.
6858 This is only done for -readnow and building partial symtabs. */
6861 create_all_comp_units (struct objfile *objfile)
6865 struct dwarf2_per_cu_data **all_comp_units;
6866 struct dwz_file *dwz;
6870 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6872 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
6873 &dwarf2_per_objfile->abbrev, 0,
6874 &n_allocated, &n_comp_units, &all_comp_units);
6876 dwz = dwarf2_get_dwz_file ();
6878 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
6879 &n_allocated, &n_comp_units,
6882 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6883 struct dwarf2_per_cu_data *,
6885 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6886 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6887 xfree (all_comp_units);
6888 dwarf2_per_objfile->n_comp_units = n_comp_units;
6891 /* Process all loaded DIEs for compilation unit CU, starting at
6892 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6893 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6894 DW_AT_ranges). See the comments of add_partial_subprogram on how
6895 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6898 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6899 CORE_ADDR *highpc, int set_addrmap,
6900 struct dwarf2_cu *cu)
6902 struct partial_die_info *pdi;
6904 /* Now, march along the PDI's, descending into ones which have
6905 interesting children but skipping the children of the other ones,
6906 until we reach the end of the compilation unit. */
6912 fixup_partial_die (pdi, cu);
6914 /* Anonymous namespaces or modules have no name but have interesting
6915 children, so we need to look at them. Ditto for anonymous
6918 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6919 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6920 || pdi->tag == DW_TAG_imported_unit)
6924 case DW_TAG_subprogram:
6925 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6927 case DW_TAG_constant:
6928 case DW_TAG_variable:
6929 case DW_TAG_typedef:
6930 case DW_TAG_union_type:
6931 if (!pdi->is_declaration)
6933 add_partial_symbol (pdi, cu);
6936 case DW_TAG_class_type:
6937 case DW_TAG_interface_type:
6938 case DW_TAG_structure_type:
6939 if (!pdi->is_declaration)
6941 add_partial_symbol (pdi, cu);
6943 if (cu->language == language_rust && pdi->has_children)
6944 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6947 case DW_TAG_enumeration_type:
6948 if (!pdi->is_declaration)
6949 add_partial_enumeration (pdi, cu);
6951 case DW_TAG_base_type:
6952 case DW_TAG_subrange_type:
6953 /* File scope base type definitions are added to the partial
6955 add_partial_symbol (pdi, cu);
6957 case DW_TAG_namespace:
6958 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6961 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6963 case DW_TAG_imported_unit:
6965 struct dwarf2_per_cu_data *per_cu;
6967 /* For now we don't handle imported units in type units. */
6968 if (cu->per_cu->is_debug_types)
6970 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6971 " supported in type units [in module %s]"),
6972 objfile_name (cu->objfile));
6975 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
6979 /* Go read the partial unit, if needed. */
6980 if (per_cu->v.psymtab == NULL)
6981 process_psymtab_comp_unit (per_cu, 1, cu->language);
6983 VEC_safe_push (dwarf2_per_cu_ptr,
6984 cu->per_cu->imported_symtabs, per_cu);
6987 case DW_TAG_imported_declaration:
6988 add_partial_symbol (pdi, cu);
6995 /* If the die has a sibling, skip to the sibling. */
6997 pdi = pdi->die_sibling;
7001 /* Functions used to compute the fully scoped name of a partial DIE.
7003 Normally, this is simple. For C++, the parent DIE's fully scoped
7004 name is concatenated with "::" and the partial DIE's name.
7005 Enumerators are an exception; they use the scope of their parent
7006 enumeration type, i.e. the name of the enumeration type is not
7007 prepended to the enumerator.
7009 There are two complexities. One is DW_AT_specification; in this
7010 case "parent" means the parent of the target of the specification,
7011 instead of the direct parent of the DIE. The other is compilers
7012 which do not emit DW_TAG_namespace; in this case we try to guess
7013 the fully qualified name of structure types from their members'
7014 linkage names. This must be done using the DIE's children rather
7015 than the children of any DW_AT_specification target. We only need
7016 to do this for structures at the top level, i.e. if the target of
7017 any DW_AT_specification (if any; otherwise the DIE itself) does not
7020 /* Compute the scope prefix associated with PDI's parent, in
7021 compilation unit CU. The result will be allocated on CU's
7022 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7023 field. NULL is returned if no prefix is necessary. */
7025 partial_die_parent_scope (struct partial_die_info *pdi,
7026 struct dwarf2_cu *cu)
7028 const char *grandparent_scope;
7029 struct partial_die_info *parent, *real_pdi;
7031 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7032 then this means the parent of the specification DIE. */
7035 while (real_pdi->has_specification)
7036 real_pdi = find_partial_die (real_pdi->spec_offset,
7037 real_pdi->spec_is_dwz, cu);
7039 parent = real_pdi->die_parent;
7043 if (parent->scope_set)
7044 return parent->scope;
7046 fixup_partial_die (parent, cu);
7048 grandparent_scope = partial_die_parent_scope (parent, cu);
7050 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7051 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7052 Work around this problem here. */
7053 if (cu->language == language_cplus
7054 && parent->tag == DW_TAG_namespace
7055 && strcmp (parent->name, "::") == 0
7056 && grandparent_scope == NULL)
7058 parent->scope = NULL;
7059 parent->scope_set = 1;
7063 if (pdi->tag == DW_TAG_enumerator)
7064 /* Enumerators should not get the name of the enumeration as a prefix. */
7065 parent->scope = grandparent_scope;
7066 else if (parent->tag == DW_TAG_namespace
7067 || parent->tag == DW_TAG_module
7068 || parent->tag == DW_TAG_structure_type
7069 || parent->tag == DW_TAG_class_type
7070 || parent->tag == DW_TAG_interface_type
7071 || parent->tag == DW_TAG_union_type
7072 || parent->tag == DW_TAG_enumeration_type)
7074 if (grandparent_scope == NULL)
7075 parent->scope = parent->name;
7077 parent->scope = typename_concat (&cu->comp_unit_obstack,
7079 parent->name, 0, cu);
7083 /* FIXME drow/2004-04-01: What should we be doing with
7084 function-local names? For partial symbols, we should probably be
7086 complaint (&symfile_complaints,
7087 _("unhandled containing DIE tag %d for DIE at %d"),
7088 parent->tag, to_underlying (pdi->sect_off));
7089 parent->scope = grandparent_scope;
7092 parent->scope_set = 1;
7093 return parent->scope;
7096 /* Return the fully scoped name associated with PDI, from compilation unit
7097 CU. The result will be allocated with malloc. */
7100 partial_die_full_name (struct partial_die_info *pdi,
7101 struct dwarf2_cu *cu)
7103 const char *parent_scope;
7105 /* If this is a template instantiation, we can not work out the
7106 template arguments from partial DIEs. So, unfortunately, we have
7107 to go through the full DIEs. At least any work we do building
7108 types here will be reused if full symbols are loaded later. */
7109 if (pdi->has_template_arguments)
7111 fixup_partial_die (pdi, cu);
7113 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7115 struct die_info *die;
7116 struct attribute attr;
7117 struct dwarf2_cu *ref_cu = cu;
7119 /* DW_FORM_ref_addr is using section offset. */
7120 attr.name = (enum dwarf_attribute) 0;
7121 attr.form = DW_FORM_ref_addr;
7122 attr.u.unsnd = to_underlying (pdi->sect_off);
7123 die = follow_die_ref (NULL, &attr, &ref_cu);
7125 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7129 parent_scope = partial_die_parent_scope (pdi, cu);
7130 if (parent_scope == NULL)
7133 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7137 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7139 struct objfile *objfile = cu->objfile;
7140 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7142 const char *actual_name = NULL;
7144 char *built_actual_name;
7146 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7148 built_actual_name = partial_die_full_name (pdi, cu);
7149 if (built_actual_name != NULL)
7150 actual_name = built_actual_name;
7152 if (actual_name == NULL)
7153 actual_name = pdi->name;
7157 case DW_TAG_subprogram:
7158 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7159 if (pdi->is_external || cu->language == language_ada)
7161 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7162 of the global scope. But in Ada, we want to be able to access
7163 nested procedures globally. So all Ada subprograms are stored
7164 in the global scope. */
7165 add_psymbol_to_list (actual_name, strlen (actual_name),
7166 built_actual_name != NULL,
7167 VAR_DOMAIN, LOC_BLOCK,
7168 &objfile->global_psymbols,
7169 addr, cu->language, objfile);
7173 add_psymbol_to_list (actual_name, strlen (actual_name),
7174 built_actual_name != NULL,
7175 VAR_DOMAIN, LOC_BLOCK,
7176 &objfile->static_psymbols,
7177 addr, cu->language, objfile);
7180 if (pdi->main_subprogram && actual_name != NULL)
7181 set_objfile_main_name (objfile, actual_name, cu->language);
7183 case DW_TAG_constant:
7185 struct psymbol_allocation_list *list;
7187 if (pdi->is_external)
7188 list = &objfile->global_psymbols;
7190 list = &objfile->static_psymbols;
7191 add_psymbol_to_list (actual_name, strlen (actual_name),
7192 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7193 list, 0, cu->language, objfile);
7196 case DW_TAG_variable:
7198 addr = decode_locdesc (pdi->d.locdesc, cu);
7202 && !dwarf2_per_objfile->has_section_at_zero)
7204 /* A global or static variable may also have been stripped
7205 out by the linker if unused, in which case its address
7206 will be nullified; do not add such variables into partial
7207 symbol table then. */
7209 else if (pdi->is_external)
7212 Don't enter into the minimal symbol tables as there is
7213 a minimal symbol table entry from the ELF symbols already.
7214 Enter into partial symbol table if it has a location
7215 descriptor or a type.
7216 If the location descriptor is missing, new_symbol will create
7217 a LOC_UNRESOLVED symbol, the address of the variable will then
7218 be determined from the minimal symbol table whenever the variable
7220 The address for the partial symbol table entry is not
7221 used by GDB, but it comes in handy for debugging partial symbol
7224 if (pdi->d.locdesc || pdi->has_type)
7225 add_psymbol_to_list (actual_name, strlen (actual_name),
7226 built_actual_name != NULL,
7227 VAR_DOMAIN, LOC_STATIC,
7228 &objfile->global_psymbols,
7230 cu->language, objfile);
7234 int has_loc = pdi->d.locdesc != NULL;
7236 /* Static Variable. Skip symbols whose value we cannot know (those
7237 without location descriptors or constant values). */
7238 if (!has_loc && !pdi->has_const_value)
7240 xfree (built_actual_name);
7244 add_psymbol_to_list (actual_name, strlen (actual_name),
7245 built_actual_name != NULL,
7246 VAR_DOMAIN, LOC_STATIC,
7247 &objfile->static_psymbols,
7248 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7249 cu->language, objfile);
7252 case DW_TAG_typedef:
7253 case DW_TAG_base_type:
7254 case DW_TAG_subrange_type:
7255 add_psymbol_to_list (actual_name, strlen (actual_name),
7256 built_actual_name != NULL,
7257 VAR_DOMAIN, LOC_TYPEDEF,
7258 &objfile->static_psymbols,
7259 0, cu->language, objfile);
7261 case DW_TAG_imported_declaration:
7262 case DW_TAG_namespace:
7263 add_psymbol_to_list (actual_name, strlen (actual_name),
7264 built_actual_name != NULL,
7265 VAR_DOMAIN, LOC_TYPEDEF,
7266 &objfile->global_psymbols,
7267 0, cu->language, objfile);
7270 add_psymbol_to_list (actual_name, strlen (actual_name),
7271 built_actual_name != NULL,
7272 MODULE_DOMAIN, LOC_TYPEDEF,
7273 &objfile->global_psymbols,
7274 0, cu->language, objfile);
7276 case DW_TAG_class_type:
7277 case DW_TAG_interface_type:
7278 case DW_TAG_structure_type:
7279 case DW_TAG_union_type:
7280 case DW_TAG_enumeration_type:
7281 /* Skip external references. The DWARF standard says in the section
7282 about "Structure, Union, and Class Type Entries": "An incomplete
7283 structure, union or class type is represented by a structure,
7284 union or class entry that does not have a byte size attribute
7285 and that has a DW_AT_declaration attribute." */
7286 if (!pdi->has_byte_size && pdi->is_declaration)
7288 xfree (built_actual_name);
7292 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7293 static vs. global. */
7294 add_psymbol_to_list (actual_name, strlen (actual_name),
7295 built_actual_name != NULL,
7296 STRUCT_DOMAIN, LOC_TYPEDEF,
7297 cu->language == language_cplus
7298 ? &objfile->global_psymbols
7299 : &objfile->static_psymbols,
7300 0, cu->language, objfile);
7303 case DW_TAG_enumerator:
7304 add_psymbol_to_list (actual_name, strlen (actual_name),
7305 built_actual_name != NULL,
7306 VAR_DOMAIN, LOC_CONST,
7307 cu->language == language_cplus
7308 ? &objfile->global_psymbols
7309 : &objfile->static_psymbols,
7310 0, cu->language, objfile);
7316 xfree (built_actual_name);
7319 /* Read a partial die corresponding to a namespace; also, add a symbol
7320 corresponding to that namespace to the symbol table. NAMESPACE is
7321 the name of the enclosing namespace. */
7324 add_partial_namespace (struct partial_die_info *pdi,
7325 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7326 int set_addrmap, struct dwarf2_cu *cu)
7328 /* Add a symbol for the namespace. */
7330 add_partial_symbol (pdi, cu);
7332 /* Now scan partial symbols in that namespace. */
7334 if (pdi->has_children)
7335 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7338 /* Read a partial die corresponding to a Fortran module. */
7341 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7342 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7344 /* Add a symbol for the namespace. */
7346 add_partial_symbol (pdi, cu);
7348 /* Now scan partial symbols in that module. */
7350 if (pdi->has_children)
7351 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7354 /* Read a partial die corresponding to a subprogram and create a partial
7355 symbol for that subprogram. When the CU language allows it, this
7356 routine also defines a partial symbol for each nested subprogram
7357 that this subprogram contains. If SET_ADDRMAP is true, record the
7358 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7359 and highest PC values found in PDI.
7361 PDI may also be a lexical block, in which case we simply search
7362 recursively for subprograms defined inside that lexical block.
7363 Again, this is only performed when the CU language allows this
7364 type of definitions. */
7367 add_partial_subprogram (struct partial_die_info *pdi,
7368 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7369 int set_addrmap, struct dwarf2_cu *cu)
7371 if (pdi->tag == DW_TAG_subprogram)
7373 if (pdi->has_pc_info)
7375 if (pdi->lowpc < *lowpc)
7376 *lowpc = pdi->lowpc;
7377 if (pdi->highpc > *highpc)
7378 *highpc = pdi->highpc;
7381 struct objfile *objfile = cu->objfile;
7382 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7387 baseaddr = ANOFFSET (objfile->section_offsets,
7388 SECT_OFF_TEXT (objfile));
7389 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7390 pdi->lowpc + baseaddr);
7391 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7392 pdi->highpc + baseaddr);
7393 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7394 cu->per_cu->v.psymtab);
7398 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7400 if (!pdi->is_declaration)
7401 /* Ignore subprogram DIEs that do not have a name, they are
7402 illegal. Do not emit a complaint at this point, we will
7403 do so when we convert this psymtab into a symtab. */
7405 add_partial_symbol (pdi, cu);
7409 if (! pdi->has_children)
7412 if (cu->language == language_ada)
7414 pdi = pdi->die_child;
7417 fixup_partial_die (pdi, cu);
7418 if (pdi->tag == DW_TAG_subprogram
7419 || pdi->tag == DW_TAG_lexical_block)
7420 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7421 pdi = pdi->die_sibling;
7426 /* Read a partial die corresponding to an enumeration type. */
7429 add_partial_enumeration (struct partial_die_info *enum_pdi,
7430 struct dwarf2_cu *cu)
7432 struct partial_die_info *pdi;
7434 if (enum_pdi->name != NULL)
7435 add_partial_symbol (enum_pdi, cu);
7437 pdi = enum_pdi->die_child;
7440 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7441 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7443 add_partial_symbol (pdi, cu);
7444 pdi = pdi->die_sibling;
7448 /* Return the initial uleb128 in the die at INFO_PTR. */
7451 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7453 unsigned int bytes_read;
7455 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7458 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7459 Return the corresponding abbrev, or NULL if the number is zero (indicating
7460 an empty DIE). In either case *BYTES_READ will be set to the length of
7461 the initial number. */
7463 static struct abbrev_info *
7464 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7465 struct dwarf2_cu *cu)
7467 bfd *abfd = cu->objfile->obfd;
7468 unsigned int abbrev_number;
7469 struct abbrev_info *abbrev;
7471 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7473 if (abbrev_number == 0)
7476 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7479 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7480 " at offset 0x%x [in module %s]"),
7481 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7482 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
7488 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7489 Returns a pointer to the end of a series of DIEs, terminated by an empty
7490 DIE. Any children of the skipped DIEs will also be skipped. */
7492 static const gdb_byte *
7493 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7495 struct dwarf2_cu *cu = reader->cu;
7496 struct abbrev_info *abbrev;
7497 unsigned int bytes_read;
7501 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7503 return info_ptr + bytes_read;
7505 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7509 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7510 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7511 abbrev corresponding to that skipped uleb128 should be passed in
7512 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7515 static const gdb_byte *
7516 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7517 struct abbrev_info *abbrev)
7519 unsigned int bytes_read;
7520 struct attribute attr;
7521 bfd *abfd = reader->abfd;
7522 struct dwarf2_cu *cu = reader->cu;
7523 const gdb_byte *buffer = reader->buffer;
7524 const gdb_byte *buffer_end = reader->buffer_end;
7525 unsigned int form, i;
7527 for (i = 0; i < abbrev->num_attrs; i++)
7529 /* The only abbrev we care about is DW_AT_sibling. */
7530 if (abbrev->attrs[i].name == DW_AT_sibling)
7532 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7533 if (attr.form == DW_FORM_ref_addr)
7534 complaint (&symfile_complaints,
7535 _("ignoring absolute DW_AT_sibling"));
7538 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7539 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7541 if (sibling_ptr < info_ptr)
7542 complaint (&symfile_complaints,
7543 _("DW_AT_sibling points backwards"));
7544 else if (sibling_ptr > reader->buffer_end)
7545 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7551 /* If it isn't DW_AT_sibling, skip this attribute. */
7552 form = abbrev->attrs[i].form;
7556 case DW_FORM_ref_addr:
7557 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7558 and later it is offset sized. */
7559 if (cu->header.version == 2)
7560 info_ptr += cu->header.addr_size;
7562 info_ptr += cu->header.offset_size;
7564 case DW_FORM_GNU_ref_alt:
7565 info_ptr += cu->header.offset_size;
7568 info_ptr += cu->header.addr_size;
7575 case DW_FORM_flag_present:
7576 case DW_FORM_implicit_const:
7588 case DW_FORM_ref_sig8:
7591 case DW_FORM_data16:
7594 case DW_FORM_string:
7595 read_direct_string (abfd, info_ptr, &bytes_read);
7596 info_ptr += bytes_read;
7598 case DW_FORM_sec_offset:
7600 case DW_FORM_GNU_strp_alt:
7601 info_ptr += cu->header.offset_size;
7603 case DW_FORM_exprloc:
7605 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7606 info_ptr += bytes_read;
7608 case DW_FORM_block1:
7609 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7611 case DW_FORM_block2:
7612 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7614 case DW_FORM_block4:
7615 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7619 case DW_FORM_ref_udata:
7620 case DW_FORM_GNU_addr_index:
7621 case DW_FORM_GNU_str_index:
7622 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7624 case DW_FORM_indirect:
7625 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7626 info_ptr += bytes_read;
7627 /* We need to continue parsing from here, so just go back to
7629 goto skip_attribute;
7632 error (_("Dwarf Error: Cannot handle %s "
7633 "in DWARF reader [in module %s]"),
7634 dwarf_form_name (form),
7635 bfd_get_filename (abfd));
7639 if (abbrev->has_children)
7640 return skip_children (reader, info_ptr);
7645 /* Locate ORIG_PDI's sibling.
7646 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7648 static const gdb_byte *
7649 locate_pdi_sibling (const struct die_reader_specs *reader,
7650 struct partial_die_info *orig_pdi,
7651 const gdb_byte *info_ptr)
7653 /* Do we know the sibling already? */
7655 if (orig_pdi->sibling)
7656 return orig_pdi->sibling;
7658 /* Are there any children to deal with? */
7660 if (!orig_pdi->has_children)
7663 /* Skip the children the long way. */
7665 return skip_children (reader, info_ptr);
7668 /* Expand this partial symbol table into a full symbol table. SELF is
7672 dwarf2_read_symtab (struct partial_symtab *self,
7673 struct objfile *objfile)
7677 warning (_("bug: psymtab for %s is already read in."),
7684 printf_filtered (_("Reading in symbols for %s..."),
7686 gdb_flush (gdb_stdout);
7689 /* Restore our global data. */
7691 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7692 dwarf2_objfile_data_key);
7694 /* If this psymtab is constructed from a debug-only objfile, the
7695 has_section_at_zero flag will not necessarily be correct. We
7696 can get the correct value for this flag by looking at the data
7697 associated with the (presumably stripped) associated objfile. */
7698 if (objfile->separate_debug_objfile_backlink)
7700 struct dwarf2_per_objfile *dpo_backlink
7701 = ((struct dwarf2_per_objfile *)
7702 objfile_data (objfile->separate_debug_objfile_backlink,
7703 dwarf2_objfile_data_key));
7705 dwarf2_per_objfile->has_section_at_zero
7706 = dpo_backlink->has_section_at_zero;
7709 dwarf2_per_objfile->reading_partial_symbols = 0;
7711 psymtab_to_symtab_1 (self);
7713 /* Finish up the debug error message. */
7715 printf_filtered (_("done.\n"));
7718 process_cu_includes ();
7721 /* Reading in full CUs. */
7723 /* Add PER_CU to the queue. */
7726 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7727 enum language pretend_language)
7729 struct dwarf2_queue_item *item;
7732 item = XNEW (struct dwarf2_queue_item);
7733 item->per_cu = per_cu;
7734 item->pretend_language = pretend_language;
7737 if (dwarf2_queue == NULL)
7738 dwarf2_queue = item;
7740 dwarf2_queue_tail->next = item;
7742 dwarf2_queue_tail = item;
7745 /* If PER_CU is not yet queued, add it to the queue.
7746 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7748 The result is non-zero if PER_CU was queued, otherwise the result is zero
7749 meaning either PER_CU is already queued or it is already loaded.
7751 N.B. There is an invariant here that if a CU is queued then it is loaded.
7752 The caller is required to load PER_CU if we return non-zero. */
7755 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7756 struct dwarf2_per_cu_data *per_cu,
7757 enum language pretend_language)
7759 /* We may arrive here during partial symbol reading, if we need full
7760 DIEs to process an unusual case (e.g. template arguments). Do
7761 not queue PER_CU, just tell our caller to load its DIEs. */
7762 if (dwarf2_per_objfile->reading_partial_symbols)
7764 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7769 /* Mark the dependence relation so that we don't flush PER_CU
7771 if (dependent_cu != NULL)
7772 dwarf2_add_dependence (dependent_cu, per_cu);
7774 /* If it's already on the queue, we have nothing to do. */
7778 /* If the compilation unit is already loaded, just mark it as
7780 if (per_cu->cu != NULL)
7782 per_cu->cu->last_used = 0;
7786 /* Add it to the queue. */
7787 queue_comp_unit (per_cu, pretend_language);
7792 /* Process the queue. */
7795 process_queue (void)
7797 struct dwarf2_queue_item *item, *next_item;
7799 if (dwarf_read_debug)
7801 fprintf_unfiltered (gdb_stdlog,
7802 "Expanding one or more symtabs of objfile %s ...\n",
7803 objfile_name (dwarf2_per_objfile->objfile));
7806 /* The queue starts out with one item, but following a DIE reference
7807 may load a new CU, adding it to the end of the queue. */
7808 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7810 if ((dwarf2_per_objfile->using_index
7811 ? !item->per_cu->v.quick->compunit_symtab
7812 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7813 /* Skip dummy CUs. */
7814 && item->per_cu->cu != NULL)
7816 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7817 unsigned int debug_print_threshold;
7820 if (per_cu->is_debug_types)
7822 struct signatured_type *sig_type =
7823 (struct signatured_type *) per_cu;
7825 sprintf (buf, "TU %s at offset 0x%x",
7826 hex_string (sig_type->signature),
7827 to_underlying (per_cu->sect_off));
7828 /* There can be 100s of TUs.
7829 Only print them in verbose mode. */
7830 debug_print_threshold = 2;
7834 sprintf (buf, "CU at offset 0x%x",
7835 to_underlying (per_cu->sect_off));
7836 debug_print_threshold = 1;
7839 if (dwarf_read_debug >= debug_print_threshold)
7840 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7842 if (per_cu->is_debug_types)
7843 process_full_type_unit (per_cu, item->pretend_language);
7845 process_full_comp_unit (per_cu, item->pretend_language);
7847 if (dwarf_read_debug >= debug_print_threshold)
7848 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7851 item->per_cu->queued = 0;
7852 next_item = item->next;
7856 dwarf2_queue_tail = NULL;
7858 if (dwarf_read_debug)
7860 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7861 objfile_name (dwarf2_per_objfile->objfile));
7865 /* Free all allocated queue entries. This function only releases anything if
7866 an error was thrown; if the queue was processed then it would have been
7867 freed as we went along. */
7870 dwarf2_release_queue (void *dummy)
7872 struct dwarf2_queue_item *item, *last;
7874 item = dwarf2_queue;
7877 /* Anything still marked queued is likely to be in an
7878 inconsistent state, so discard it. */
7879 if (item->per_cu->queued)
7881 if (item->per_cu->cu != NULL)
7882 free_one_cached_comp_unit (item->per_cu);
7883 item->per_cu->queued = 0;
7891 dwarf2_queue = dwarf2_queue_tail = NULL;
7894 /* Read in full symbols for PST, and anything it depends on. */
7897 psymtab_to_symtab_1 (struct partial_symtab *pst)
7899 struct dwarf2_per_cu_data *per_cu;
7905 for (i = 0; i < pst->number_of_dependencies; i++)
7906 if (!pst->dependencies[i]->readin
7907 && pst->dependencies[i]->user == NULL)
7909 /* Inform about additional files that need to be read in. */
7912 /* FIXME: i18n: Need to make this a single string. */
7913 fputs_filtered (" ", gdb_stdout);
7915 fputs_filtered ("and ", gdb_stdout);
7917 printf_filtered ("%s...", pst->dependencies[i]->filename);
7918 wrap_here (""); /* Flush output. */
7919 gdb_flush (gdb_stdout);
7921 psymtab_to_symtab_1 (pst->dependencies[i]);
7924 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7928 /* It's an include file, no symbols to read for it.
7929 Everything is in the parent symtab. */
7934 dw2_do_instantiate_symtab (per_cu);
7937 /* Trivial hash function for die_info: the hash value of a DIE
7938 is its offset in .debug_info for this objfile. */
7941 die_hash (const void *item)
7943 const struct die_info *die = (const struct die_info *) item;
7945 return to_underlying (die->sect_off);
7948 /* Trivial comparison function for die_info structures: two DIEs
7949 are equal if they have the same offset. */
7952 die_eq (const void *item_lhs, const void *item_rhs)
7954 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7955 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7957 return die_lhs->sect_off == die_rhs->sect_off;
7960 /* die_reader_func for load_full_comp_unit.
7961 This is identical to read_signatured_type_reader,
7962 but is kept separate for now. */
7965 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7966 const gdb_byte *info_ptr,
7967 struct die_info *comp_unit_die,
7971 struct dwarf2_cu *cu = reader->cu;
7972 enum language *language_ptr = (enum language *) data;
7974 gdb_assert (cu->die_hash == NULL);
7976 htab_create_alloc_ex (cu->header.length / 12,
7980 &cu->comp_unit_obstack,
7981 hashtab_obstack_allocate,
7982 dummy_obstack_deallocate);
7985 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7986 &info_ptr, comp_unit_die);
7987 cu->dies = comp_unit_die;
7988 /* comp_unit_die is not stored in die_hash, no need. */
7990 /* We try not to read any attributes in this function, because not
7991 all CUs needed for references have been loaded yet, and symbol
7992 table processing isn't initialized. But we have to set the CU language,
7993 or we won't be able to build types correctly.
7994 Similarly, if we do not read the producer, we can not apply
7995 producer-specific interpretation. */
7996 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7999 /* Load the DIEs associated with PER_CU into memory. */
8002 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8003 enum language pretend_language)
8005 gdb_assert (! this_cu->is_debug_types);
8007 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8008 load_full_comp_unit_reader, &pretend_language);
8011 /* Add a DIE to the delayed physname list. */
8014 add_to_method_list (struct type *type, int fnfield_index, int index,
8015 const char *name, struct die_info *die,
8016 struct dwarf2_cu *cu)
8018 struct delayed_method_info mi;
8020 mi.fnfield_index = fnfield_index;
8024 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
8027 /* A cleanup for freeing the delayed method list. */
8030 free_delayed_list (void *ptr)
8032 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
8033 if (cu->method_list != NULL)
8035 VEC_free (delayed_method_info, cu->method_list);
8036 cu->method_list = NULL;
8040 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8041 "const" / "volatile". If so, decrements LEN by the length of the
8042 modifier and return true. Otherwise return false. */
8046 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8048 size_t mod_len = sizeof (mod) - 1;
8049 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8057 /* Compute the physnames of any methods on the CU's method list.
8059 The computation of method physnames is delayed in order to avoid the
8060 (bad) condition that one of the method's formal parameters is of an as yet
8064 compute_delayed_physnames (struct dwarf2_cu *cu)
8067 struct delayed_method_info *mi;
8069 /* Only C++ delays computing physnames. */
8070 if (VEC_empty (delayed_method_info, cu->method_list))
8072 gdb_assert (cu->language == language_cplus);
8074 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
8076 const char *physname;
8077 struct fn_fieldlist *fn_flp
8078 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
8079 physname = dwarf2_physname (mi->name, mi->die, cu);
8080 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
8081 = physname ? physname : "";
8083 /* Since there's no tag to indicate whether a method is a
8084 const/volatile overload, extract that information out of the
8086 if (physname != NULL)
8088 size_t len = strlen (physname);
8092 if (physname[len] == ')') /* shortcut */
8094 else if (check_modifier (physname, len, " const"))
8095 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
8096 else if (check_modifier (physname, len, " volatile"))
8097 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
8105 /* Go objects should be embedded in a DW_TAG_module DIE,
8106 and it's not clear if/how imported objects will appear.
8107 To keep Go support simple until that's worked out,
8108 go back through what we've read and create something usable.
8109 We could do this while processing each DIE, and feels kinda cleaner,
8110 but that way is more invasive.
8111 This is to, for example, allow the user to type "p var" or "b main"
8112 without having to specify the package name, and allow lookups
8113 of module.object to work in contexts that use the expression
8117 fixup_go_packaging (struct dwarf2_cu *cu)
8119 char *package_name = NULL;
8120 struct pending *list;
8123 for (list = global_symbols; list != NULL; list = list->next)
8125 for (i = 0; i < list->nsyms; ++i)
8127 struct symbol *sym = list->symbol[i];
8129 if (SYMBOL_LANGUAGE (sym) == language_go
8130 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8132 char *this_package_name = go_symbol_package_name (sym);
8134 if (this_package_name == NULL)
8136 if (package_name == NULL)
8137 package_name = this_package_name;
8140 if (strcmp (package_name, this_package_name) != 0)
8141 complaint (&symfile_complaints,
8142 _("Symtab %s has objects from two different Go packages: %s and %s"),
8143 (symbol_symtab (sym) != NULL
8144 ? symtab_to_filename_for_display
8145 (symbol_symtab (sym))
8146 : objfile_name (cu->objfile)),
8147 this_package_name, package_name);
8148 xfree (this_package_name);
8154 if (package_name != NULL)
8156 struct objfile *objfile = cu->objfile;
8157 const char *saved_package_name
8158 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8160 strlen (package_name));
8161 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8162 saved_package_name);
8165 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8167 sym = allocate_symbol (objfile);
8168 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8169 SYMBOL_SET_NAMES (sym, saved_package_name,
8170 strlen (saved_package_name), 0, objfile);
8171 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8172 e.g., "main" finds the "main" module and not C's main(). */
8173 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8174 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8175 SYMBOL_TYPE (sym) = type;
8177 add_symbol_to_list (sym, &global_symbols);
8179 xfree (package_name);
8183 /* Return the symtab for PER_CU. This works properly regardless of
8184 whether we're using the index or psymtabs. */
8186 static struct compunit_symtab *
8187 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8189 return (dwarf2_per_objfile->using_index
8190 ? per_cu->v.quick->compunit_symtab
8191 : per_cu->v.psymtab->compunit_symtab);
8194 /* A helper function for computing the list of all symbol tables
8195 included by PER_CU. */
8198 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8199 htab_t all_children, htab_t all_type_symtabs,
8200 struct dwarf2_per_cu_data *per_cu,
8201 struct compunit_symtab *immediate_parent)
8205 struct compunit_symtab *cust;
8206 struct dwarf2_per_cu_data *iter;
8208 slot = htab_find_slot (all_children, per_cu, INSERT);
8211 /* This inclusion and its children have been processed. */
8216 /* Only add a CU if it has a symbol table. */
8217 cust = get_compunit_symtab (per_cu);
8220 /* If this is a type unit only add its symbol table if we haven't
8221 seen it yet (type unit per_cu's can share symtabs). */
8222 if (per_cu->is_debug_types)
8224 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8228 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8229 if (cust->user == NULL)
8230 cust->user = immediate_parent;
8235 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8236 if (cust->user == NULL)
8237 cust->user = immediate_parent;
8242 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8245 recursively_compute_inclusions (result, all_children,
8246 all_type_symtabs, iter, cust);
8250 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8254 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8256 gdb_assert (! per_cu->is_debug_types);
8258 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8261 struct dwarf2_per_cu_data *per_cu_iter;
8262 struct compunit_symtab *compunit_symtab_iter;
8263 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8264 htab_t all_children, all_type_symtabs;
8265 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8267 /* If we don't have a symtab, we can just skip this case. */
8271 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8272 NULL, xcalloc, xfree);
8273 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8274 NULL, xcalloc, xfree);
8277 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8281 recursively_compute_inclusions (&result_symtabs, all_children,
8282 all_type_symtabs, per_cu_iter,
8286 /* Now we have a transitive closure of all the included symtabs. */
8287 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8289 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8290 struct compunit_symtab *, len + 1);
8292 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8293 compunit_symtab_iter);
8295 cust->includes[ix] = compunit_symtab_iter;
8296 cust->includes[len] = NULL;
8298 VEC_free (compunit_symtab_ptr, result_symtabs);
8299 htab_delete (all_children);
8300 htab_delete (all_type_symtabs);
8304 /* Compute the 'includes' field for the symtabs of all the CUs we just
8308 process_cu_includes (void)
8311 struct dwarf2_per_cu_data *iter;
8314 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8318 if (! iter->is_debug_types)
8319 compute_compunit_symtab_includes (iter);
8322 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8325 /* Generate full symbol information for PER_CU, whose DIEs have
8326 already been loaded into memory. */
8329 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8330 enum language pretend_language)
8332 struct dwarf2_cu *cu = per_cu->cu;
8333 struct objfile *objfile = per_cu->objfile;
8334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8335 CORE_ADDR lowpc, highpc;
8336 struct compunit_symtab *cust;
8337 struct cleanup *back_to, *delayed_list_cleanup;
8339 struct block *static_block;
8342 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8345 back_to = make_cleanup (really_free_pendings, NULL);
8346 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8348 cu->list_in_scope = &file_symbols;
8350 cu->language = pretend_language;
8351 cu->language_defn = language_def (cu->language);
8353 /* Do line number decoding in read_file_scope () */
8354 process_die (cu->dies, cu);
8356 /* For now fudge the Go package. */
8357 if (cu->language == language_go)
8358 fixup_go_packaging (cu);
8360 /* Now that we have processed all the DIEs in the CU, all the types
8361 should be complete, and it should now be safe to compute all of the
8363 compute_delayed_physnames (cu);
8364 do_cleanups (delayed_list_cleanup);
8366 /* Some compilers don't define a DW_AT_high_pc attribute for the
8367 compilation unit. If the DW_AT_high_pc is missing, synthesize
8368 it, by scanning the DIE's below the compilation unit. */
8369 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8371 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8372 static_block = end_symtab_get_static_block (addr, 0, 1);
8374 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8375 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8376 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8377 addrmap to help ensure it has an accurate map of pc values belonging to
8379 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8381 cust = end_symtab_from_static_block (static_block,
8382 SECT_OFF_TEXT (objfile), 0);
8386 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8388 /* Set symtab language to language from DW_AT_language. If the
8389 compilation is from a C file generated by language preprocessors, do
8390 not set the language if it was already deduced by start_subfile. */
8391 if (!(cu->language == language_c
8392 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8393 COMPUNIT_FILETABS (cust)->language = cu->language;
8395 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8396 produce DW_AT_location with location lists but it can be possibly
8397 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8398 there were bugs in prologue debug info, fixed later in GCC-4.5
8399 by "unwind info for epilogues" patch (which is not directly related).
8401 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8402 needed, it would be wrong due to missing DW_AT_producer there.
8404 Still one can confuse GDB by using non-standard GCC compilation
8405 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8407 if (cu->has_loclist && gcc_4_minor >= 5)
8408 cust->locations_valid = 1;
8410 if (gcc_4_minor >= 5)
8411 cust->epilogue_unwind_valid = 1;
8413 cust->call_site_htab = cu->call_site_htab;
8416 if (dwarf2_per_objfile->using_index)
8417 per_cu->v.quick->compunit_symtab = cust;
8420 struct partial_symtab *pst = per_cu->v.psymtab;
8421 pst->compunit_symtab = cust;
8425 /* Push it for inclusion processing later. */
8426 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8428 do_cleanups (back_to);
8431 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8432 already been loaded into memory. */
8435 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8436 enum language pretend_language)
8438 struct dwarf2_cu *cu = per_cu->cu;
8439 struct objfile *objfile = per_cu->objfile;
8440 struct compunit_symtab *cust;
8441 struct cleanup *back_to, *delayed_list_cleanup;
8442 struct signatured_type *sig_type;
8444 gdb_assert (per_cu->is_debug_types);
8445 sig_type = (struct signatured_type *) per_cu;
8448 back_to = make_cleanup (really_free_pendings, NULL);
8449 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8451 cu->list_in_scope = &file_symbols;
8453 cu->language = pretend_language;
8454 cu->language_defn = language_def (cu->language);
8456 /* The symbol tables are set up in read_type_unit_scope. */
8457 process_die (cu->dies, cu);
8459 /* For now fudge the Go package. */
8460 if (cu->language == language_go)
8461 fixup_go_packaging (cu);
8463 /* Now that we have processed all the DIEs in the CU, all the types
8464 should be complete, and it should now be safe to compute all of the
8466 compute_delayed_physnames (cu);
8467 do_cleanups (delayed_list_cleanup);
8469 /* TUs share symbol tables.
8470 If this is the first TU to use this symtab, complete the construction
8471 of it with end_expandable_symtab. Otherwise, complete the addition of
8472 this TU's symbols to the existing symtab. */
8473 if (sig_type->type_unit_group->compunit_symtab == NULL)
8475 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8476 sig_type->type_unit_group->compunit_symtab = cust;
8480 /* Set symtab language to language from DW_AT_language. If the
8481 compilation is from a C file generated by language preprocessors,
8482 do not set the language if it was already deduced by
8484 if (!(cu->language == language_c
8485 && COMPUNIT_FILETABS (cust)->language != language_c))
8486 COMPUNIT_FILETABS (cust)->language = cu->language;
8491 augment_type_symtab ();
8492 cust = sig_type->type_unit_group->compunit_symtab;
8495 if (dwarf2_per_objfile->using_index)
8496 per_cu->v.quick->compunit_symtab = cust;
8499 struct partial_symtab *pst = per_cu->v.psymtab;
8500 pst->compunit_symtab = cust;
8504 do_cleanups (back_to);
8507 /* Process an imported unit DIE. */
8510 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8512 struct attribute *attr;
8514 /* For now we don't handle imported units in type units. */
8515 if (cu->per_cu->is_debug_types)
8517 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8518 " supported in type units [in module %s]"),
8519 objfile_name (cu->objfile));
8522 attr = dwarf2_attr (die, DW_AT_import, cu);
8525 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8526 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8527 dwarf2_per_cu_data *per_cu
8528 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
8530 /* If necessary, add it to the queue and load its DIEs. */
8531 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8532 load_full_comp_unit (per_cu, cu->language);
8534 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8539 /* RAII object that represents a process_die scope: i.e.,
8540 starts/finishes processing a DIE. */
8541 class process_die_scope
8544 process_die_scope (die_info *die, dwarf2_cu *cu)
8545 : m_die (die), m_cu (cu)
8547 /* We should only be processing DIEs not already in process. */
8548 gdb_assert (!m_die->in_process);
8549 m_die->in_process = true;
8552 ~process_die_scope ()
8554 m_die->in_process = false;
8556 /* If we're done processing the DIE for the CU that owns the line
8557 header, we don't need the line header anymore. */
8558 if (m_cu->line_header_die_owner == m_die)
8560 delete m_cu->line_header;
8561 m_cu->line_header = NULL;
8562 m_cu->line_header_die_owner = NULL;
8571 /* Process a die and its children. */
8574 process_die (struct die_info *die, struct dwarf2_cu *cu)
8576 process_die_scope scope (die, cu);
8580 case DW_TAG_padding:
8582 case DW_TAG_compile_unit:
8583 case DW_TAG_partial_unit:
8584 read_file_scope (die, cu);
8586 case DW_TAG_type_unit:
8587 read_type_unit_scope (die, cu);
8589 case DW_TAG_subprogram:
8590 case DW_TAG_inlined_subroutine:
8591 read_func_scope (die, cu);
8593 case DW_TAG_lexical_block:
8594 case DW_TAG_try_block:
8595 case DW_TAG_catch_block:
8596 read_lexical_block_scope (die, cu);
8598 case DW_TAG_call_site:
8599 case DW_TAG_GNU_call_site:
8600 read_call_site_scope (die, cu);
8602 case DW_TAG_class_type:
8603 case DW_TAG_interface_type:
8604 case DW_TAG_structure_type:
8605 case DW_TAG_union_type:
8606 process_structure_scope (die, cu);
8608 case DW_TAG_enumeration_type:
8609 process_enumeration_scope (die, cu);
8612 /* These dies have a type, but processing them does not create
8613 a symbol or recurse to process the children. Therefore we can
8614 read them on-demand through read_type_die. */
8615 case DW_TAG_subroutine_type:
8616 case DW_TAG_set_type:
8617 case DW_TAG_array_type:
8618 case DW_TAG_pointer_type:
8619 case DW_TAG_ptr_to_member_type:
8620 case DW_TAG_reference_type:
8621 case DW_TAG_rvalue_reference_type:
8622 case DW_TAG_string_type:
8625 case DW_TAG_base_type:
8626 case DW_TAG_subrange_type:
8627 case DW_TAG_typedef:
8628 /* Add a typedef symbol for the type definition, if it has a
8630 new_symbol (die, read_type_die (die, cu), cu);
8632 case DW_TAG_common_block:
8633 read_common_block (die, cu);
8635 case DW_TAG_common_inclusion:
8637 case DW_TAG_namespace:
8638 cu->processing_has_namespace_info = 1;
8639 read_namespace (die, cu);
8642 cu->processing_has_namespace_info = 1;
8643 read_module (die, cu);
8645 case DW_TAG_imported_declaration:
8646 cu->processing_has_namespace_info = 1;
8647 if (read_namespace_alias (die, cu))
8649 /* The declaration is not a global namespace alias: fall through. */
8650 case DW_TAG_imported_module:
8651 cu->processing_has_namespace_info = 1;
8652 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8653 || cu->language != language_fortran))
8654 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8655 dwarf_tag_name (die->tag));
8656 read_import_statement (die, cu);
8659 case DW_TAG_imported_unit:
8660 process_imported_unit_die (die, cu);
8664 new_symbol (die, NULL, cu);
8669 /* DWARF name computation. */
8671 /* A helper function for dwarf2_compute_name which determines whether DIE
8672 needs to have the name of the scope prepended to the name listed in the
8676 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8678 struct attribute *attr;
8682 case DW_TAG_namespace:
8683 case DW_TAG_typedef:
8684 case DW_TAG_class_type:
8685 case DW_TAG_interface_type:
8686 case DW_TAG_structure_type:
8687 case DW_TAG_union_type:
8688 case DW_TAG_enumeration_type:
8689 case DW_TAG_enumerator:
8690 case DW_TAG_subprogram:
8691 case DW_TAG_inlined_subroutine:
8693 case DW_TAG_imported_declaration:
8696 case DW_TAG_variable:
8697 case DW_TAG_constant:
8698 /* We only need to prefix "globally" visible variables. These include
8699 any variable marked with DW_AT_external or any variable that
8700 lives in a namespace. [Variables in anonymous namespaces
8701 require prefixing, but they are not DW_AT_external.] */
8703 if (dwarf2_attr (die, DW_AT_specification, cu))
8705 struct dwarf2_cu *spec_cu = cu;
8707 return die_needs_namespace (die_specification (die, &spec_cu),
8711 attr = dwarf2_attr (die, DW_AT_external, cu);
8712 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8713 && die->parent->tag != DW_TAG_module)
8715 /* A variable in a lexical block of some kind does not need a
8716 namespace, even though in C++ such variables may be external
8717 and have a mangled name. */
8718 if (die->parent->tag == DW_TAG_lexical_block
8719 || die->parent->tag == DW_TAG_try_block
8720 || die->parent->tag == DW_TAG_catch_block
8721 || die->parent->tag == DW_TAG_subprogram)
8730 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8731 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8732 defined for the given DIE. */
8734 static struct attribute *
8735 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8737 struct attribute *attr;
8739 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8741 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8746 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8747 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8748 defined for the given DIE. */
8751 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8753 const char *linkage_name;
8755 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8756 if (linkage_name == NULL)
8757 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8759 return linkage_name;
8762 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8763 compute the physname for the object, which include a method's:
8764 - formal parameters (C++),
8765 - receiver type (Go),
8767 The term "physname" is a bit confusing.
8768 For C++, for example, it is the demangled name.
8769 For Go, for example, it's the mangled name.
8771 For Ada, return the DIE's linkage name rather than the fully qualified
8772 name. PHYSNAME is ignored..
8774 The result is allocated on the objfile_obstack and canonicalized. */
8777 dwarf2_compute_name (const char *name,
8778 struct die_info *die, struct dwarf2_cu *cu,
8781 struct objfile *objfile = cu->objfile;
8784 name = dwarf2_name (die, cu);
8786 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8787 but otherwise compute it by typename_concat inside GDB.
8788 FIXME: Actually this is not really true, or at least not always true.
8789 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8790 Fortran names because there is no mangling standard. So new_symbol_full
8791 will set the demangled name to the result of dwarf2_full_name, and it is
8792 the demangled name that GDB uses if it exists. */
8793 if (cu->language == language_ada
8794 || (cu->language == language_fortran && physname))
8796 /* For Ada unit, we prefer the linkage name over the name, as
8797 the former contains the exported name, which the user expects
8798 to be able to reference. Ideally, we want the user to be able
8799 to reference this entity using either natural or linkage name,
8800 but we haven't started looking at this enhancement yet. */
8801 const char *linkage_name = dw2_linkage_name (die, cu);
8803 if (linkage_name != NULL)
8804 return linkage_name;
8807 /* These are the only languages we know how to qualify names in. */
8809 && (cu->language == language_cplus
8810 || cu->language == language_fortran || cu->language == language_d
8811 || cu->language == language_rust))
8813 if (die_needs_namespace (die, cu))
8817 const char *canonical_name = NULL;
8821 prefix = determine_prefix (die, cu);
8822 if (*prefix != '\0')
8824 char *prefixed_name = typename_concat (NULL, prefix, name,
8827 buf.puts (prefixed_name);
8828 xfree (prefixed_name);
8833 /* Template parameters may be specified in the DIE's DW_AT_name, or
8834 as children with DW_TAG_template_type_param or
8835 DW_TAG_value_type_param. If the latter, add them to the name
8836 here. If the name already has template parameters, then
8837 skip this step; some versions of GCC emit both, and
8838 it is more efficient to use the pre-computed name.
8840 Something to keep in mind about this process: it is very
8841 unlikely, or in some cases downright impossible, to produce
8842 something that will match the mangled name of a function.
8843 If the definition of the function has the same debug info,
8844 we should be able to match up with it anyway. But fallbacks
8845 using the minimal symbol, for instance to find a method
8846 implemented in a stripped copy of libstdc++, will not work.
8847 If we do not have debug info for the definition, we will have to
8848 match them up some other way.
8850 When we do name matching there is a related problem with function
8851 templates; two instantiated function templates are allowed to
8852 differ only by their return types, which we do not add here. */
8854 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8856 struct attribute *attr;
8857 struct die_info *child;
8860 die->building_fullname = 1;
8862 for (child = die->child; child != NULL; child = child->sibling)
8866 const gdb_byte *bytes;
8867 struct dwarf2_locexpr_baton *baton;
8870 if (child->tag != DW_TAG_template_type_param
8871 && child->tag != DW_TAG_template_value_param)
8882 attr = dwarf2_attr (child, DW_AT_type, cu);
8885 complaint (&symfile_complaints,
8886 _("template parameter missing DW_AT_type"));
8887 buf.puts ("UNKNOWN_TYPE");
8890 type = die_type (child, cu);
8892 if (child->tag == DW_TAG_template_type_param)
8894 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8898 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8901 complaint (&symfile_complaints,
8902 _("template parameter missing "
8903 "DW_AT_const_value"));
8904 buf.puts ("UNKNOWN_VALUE");
8908 dwarf2_const_value_attr (attr, type, name,
8909 &cu->comp_unit_obstack, cu,
8910 &value, &bytes, &baton);
8912 if (TYPE_NOSIGN (type))
8913 /* GDB prints characters as NUMBER 'CHAR'. If that's
8914 changed, this can use value_print instead. */
8915 c_printchar (value, type, &buf);
8918 struct value_print_options opts;
8921 v = dwarf2_evaluate_loc_desc (type, NULL,
8925 else if (bytes != NULL)
8927 v = allocate_value (type);
8928 memcpy (value_contents_writeable (v), bytes,
8929 TYPE_LENGTH (type));
8932 v = value_from_longest (type, value);
8934 /* Specify decimal so that we do not depend on
8936 get_formatted_print_options (&opts, 'd');
8938 value_print (v, &buf, &opts);
8944 die->building_fullname = 0;
8948 /* Close the argument list, with a space if necessary
8949 (nested templates). */
8950 if (!buf.empty () && buf.string ().back () == '>')
8957 /* For C++ methods, append formal parameter type
8958 information, if PHYSNAME. */
8960 if (physname && die->tag == DW_TAG_subprogram
8961 && cu->language == language_cplus)
8963 struct type *type = read_type_die (die, cu);
8965 c_type_print_args (type, &buf, 1, cu->language,
8966 &type_print_raw_options);
8968 if (cu->language == language_cplus)
8970 /* Assume that an artificial first parameter is
8971 "this", but do not crash if it is not. RealView
8972 marks unnamed (and thus unused) parameters as
8973 artificial; there is no way to differentiate
8975 if (TYPE_NFIELDS (type) > 0
8976 && TYPE_FIELD_ARTIFICIAL (type, 0)
8977 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8978 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8980 buf.puts (" const");
8984 const std::string &intermediate_name = buf.string ();
8986 if (cu->language == language_cplus)
8988 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8989 &objfile->per_bfd->storage_obstack);
8991 /* If we only computed INTERMEDIATE_NAME, or if
8992 INTERMEDIATE_NAME is already canonical, then we need to
8993 copy it to the appropriate obstack. */
8994 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8995 name = ((const char *)
8996 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8997 intermediate_name.c_str (),
8998 intermediate_name.length ()));
9000 name = canonical_name;
9007 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9008 If scope qualifiers are appropriate they will be added. The result
9009 will be allocated on the storage_obstack, or NULL if the DIE does
9010 not have a name. NAME may either be from a previous call to
9011 dwarf2_name or NULL.
9013 The output string will be canonicalized (if C++). */
9016 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9018 return dwarf2_compute_name (name, die, cu, 0);
9021 /* Construct a physname for the given DIE in CU. NAME may either be
9022 from a previous call to dwarf2_name or NULL. The result will be
9023 allocated on the objfile_objstack or NULL if the DIE does not have a
9026 The output string will be canonicalized (if C++). */
9029 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9031 struct objfile *objfile = cu->objfile;
9032 const char *retval, *mangled = NULL, *canon = NULL;
9033 struct cleanup *back_to;
9036 /* In this case dwarf2_compute_name is just a shortcut not building anything
9038 if (!die_needs_namespace (die, cu))
9039 return dwarf2_compute_name (name, die, cu, 1);
9041 back_to = make_cleanup (null_cleanup, NULL);
9043 mangled = dw2_linkage_name (die, cu);
9045 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9046 See https://github.com/rust-lang/rust/issues/32925. */
9047 if (cu->language == language_rust && mangled != NULL
9048 && strchr (mangled, '{') != NULL)
9051 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9053 if (mangled != NULL)
9057 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
9058 type. It is easier for GDB users to search for such functions as
9059 `name(params)' than `long name(params)'. In such case the minimal
9060 symbol names do not match the full symbol names but for template
9061 functions there is never a need to look up their definition from their
9062 declaration so the only disadvantage remains the minimal symbol
9063 variant `long name(params)' does not have the proper inferior type.
9066 if (cu->language == language_go)
9068 /* This is a lie, but we already lie to the caller new_symbol_full.
9069 new_symbol_full assumes we return the mangled name.
9070 This just undoes that lie until things are cleaned up. */
9075 demangled = gdb_demangle (mangled,
9076 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
9080 make_cleanup (xfree, demangled);
9090 if (canon == NULL || check_physname)
9092 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9094 if (canon != NULL && strcmp (physname, canon) != 0)
9096 /* It may not mean a bug in GDB. The compiler could also
9097 compute DW_AT_linkage_name incorrectly. But in such case
9098 GDB would need to be bug-to-bug compatible. */
9100 complaint (&symfile_complaints,
9101 _("Computed physname <%s> does not match demangled <%s> "
9102 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9103 physname, canon, mangled, to_underlying (die->sect_off),
9104 objfile_name (objfile));
9106 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9107 is available here - over computed PHYSNAME. It is safer
9108 against both buggy GDB and buggy compilers. */
9122 retval = ((const char *)
9123 obstack_copy0 (&objfile->per_bfd->storage_obstack,
9124 retval, strlen (retval)));
9126 do_cleanups (back_to);
9130 /* Inspect DIE in CU for a namespace alias. If one exists, record
9131 a new symbol for it.
9133 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9136 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9138 struct attribute *attr;
9140 /* If the die does not have a name, this is not a namespace
9142 attr = dwarf2_attr (die, DW_AT_name, cu);
9146 struct die_info *d = die;
9147 struct dwarf2_cu *imported_cu = cu;
9149 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9150 keep inspecting DIEs until we hit the underlying import. */
9151 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9152 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9154 attr = dwarf2_attr (d, DW_AT_import, cu);
9158 d = follow_die_ref (d, attr, &imported_cu);
9159 if (d->tag != DW_TAG_imported_declaration)
9163 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9165 complaint (&symfile_complaints,
9166 _("DIE at 0x%x has too many recursively imported "
9167 "declarations"), to_underlying (d->sect_off));
9174 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9176 type = get_die_type_at_offset (sect_off, cu->per_cu);
9177 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9179 /* This declaration is a global namespace alias. Add
9180 a symbol for it whose type is the aliased namespace. */
9181 new_symbol (die, type, cu);
9190 /* Return the using directives repository (global or local?) to use in the
9191 current context for LANGUAGE.
9193 For Ada, imported declarations can materialize renamings, which *may* be
9194 global. However it is impossible (for now?) in DWARF to distinguish
9195 "external" imported declarations and "static" ones. As all imported
9196 declarations seem to be static in all other languages, make them all CU-wide
9197 global only in Ada. */
9199 static struct using_direct **
9200 using_directives (enum language language)
9202 if (language == language_ada && context_stack_depth == 0)
9203 return &global_using_directives;
9205 return &local_using_directives;
9208 /* Read the import statement specified by the given die and record it. */
9211 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9213 struct objfile *objfile = cu->objfile;
9214 struct attribute *import_attr;
9215 struct die_info *imported_die, *child_die;
9216 struct dwarf2_cu *imported_cu;
9217 const char *imported_name;
9218 const char *imported_name_prefix;
9219 const char *canonical_name;
9220 const char *import_alias;
9221 const char *imported_declaration = NULL;
9222 const char *import_prefix;
9223 std::vector<const char *> excludes;
9225 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9226 if (import_attr == NULL)
9228 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9229 dwarf_tag_name (die->tag));
9234 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9235 imported_name = dwarf2_name (imported_die, imported_cu);
9236 if (imported_name == NULL)
9238 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9240 The import in the following code:
9254 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9255 <52> DW_AT_decl_file : 1
9256 <53> DW_AT_decl_line : 6
9257 <54> DW_AT_import : <0x75>
9258 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9260 <5b> DW_AT_decl_file : 1
9261 <5c> DW_AT_decl_line : 2
9262 <5d> DW_AT_type : <0x6e>
9264 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9265 <76> DW_AT_byte_size : 4
9266 <77> DW_AT_encoding : 5 (signed)
9268 imports the wrong die ( 0x75 instead of 0x58 ).
9269 This case will be ignored until the gcc bug is fixed. */
9273 /* Figure out the local name after import. */
9274 import_alias = dwarf2_name (die, cu);
9276 /* Figure out where the statement is being imported to. */
9277 import_prefix = determine_prefix (die, cu);
9279 /* Figure out what the scope of the imported die is and prepend it
9280 to the name of the imported die. */
9281 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9283 if (imported_die->tag != DW_TAG_namespace
9284 && imported_die->tag != DW_TAG_module)
9286 imported_declaration = imported_name;
9287 canonical_name = imported_name_prefix;
9289 else if (strlen (imported_name_prefix) > 0)
9290 canonical_name = obconcat (&objfile->objfile_obstack,
9291 imported_name_prefix,
9292 (cu->language == language_d ? "." : "::"),
9293 imported_name, (char *) NULL);
9295 canonical_name = imported_name;
9297 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9298 for (child_die = die->child; child_die && child_die->tag;
9299 child_die = sibling_die (child_die))
9301 /* DWARF-4: A Fortran use statement with a “rename list” may be
9302 represented by an imported module entry with an import attribute
9303 referring to the module and owned entries corresponding to those
9304 entities that are renamed as part of being imported. */
9306 if (child_die->tag != DW_TAG_imported_declaration)
9308 complaint (&symfile_complaints,
9309 _("child DW_TAG_imported_declaration expected "
9310 "- DIE at 0x%x [in module %s]"),
9311 to_underlying (child_die->sect_off), objfile_name (objfile));
9315 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9316 if (import_attr == NULL)
9318 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9319 dwarf_tag_name (child_die->tag));
9324 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9326 imported_name = dwarf2_name (imported_die, imported_cu);
9327 if (imported_name == NULL)
9329 complaint (&symfile_complaints,
9330 _("child DW_TAG_imported_declaration has unknown "
9331 "imported name - DIE at 0x%x [in module %s]"),
9332 to_underlying (child_die->sect_off), objfile_name (objfile));
9336 excludes.push_back (imported_name);
9338 process_die (child_die, cu);
9341 add_using_directive (using_directives (cu->language),
9345 imported_declaration,
9348 &objfile->objfile_obstack);
9351 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9352 types, but gives them a size of zero. Starting with version 14,
9353 ICC is compatible with GCC. */
9356 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9358 if (!cu->checked_producer)
9359 check_producer (cu);
9361 return cu->producer_is_icc_lt_14;
9364 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9365 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9366 this, it was first present in GCC release 4.3.0. */
9369 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9371 if (!cu->checked_producer)
9372 check_producer (cu);
9374 return cu->producer_is_gcc_lt_4_3;
9377 static file_and_directory
9378 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9380 file_and_directory res;
9382 /* Find the filename. Do not use dwarf2_name here, since the filename
9383 is not a source language identifier. */
9384 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9385 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9387 if (res.comp_dir == NULL
9388 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9389 && IS_ABSOLUTE_PATH (res.name))
9391 res.comp_dir_storage = ldirname (res.name);
9392 if (!res.comp_dir_storage.empty ())
9393 res.comp_dir = res.comp_dir_storage.c_str ();
9395 if (res.comp_dir != NULL)
9397 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9398 directory, get rid of it. */
9399 const char *cp = strchr (res.comp_dir, ':');
9401 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9402 res.comp_dir = cp + 1;
9405 if (res.name == NULL)
9406 res.name = "<unknown>";
9411 /* Handle DW_AT_stmt_list for a compilation unit.
9412 DIE is the DW_TAG_compile_unit die for CU.
9413 COMP_DIR is the compilation directory. LOWPC is passed to
9414 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9417 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9418 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9420 struct objfile *objfile = dwarf2_per_objfile->objfile;
9421 struct attribute *attr;
9422 struct line_header line_header_local;
9423 hashval_t line_header_local_hash;
9428 gdb_assert (! cu->per_cu->is_debug_types);
9430 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9434 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9436 /* The line header hash table is only created if needed (it exists to
9437 prevent redundant reading of the line table for partial_units).
9438 If we're given a partial_unit, we'll need it. If we're given a
9439 compile_unit, then use the line header hash table if it's already
9440 created, but don't create one just yet. */
9442 if (dwarf2_per_objfile->line_header_hash == NULL
9443 && die->tag == DW_TAG_partial_unit)
9445 dwarf2_per_objfile->line_header_hash
9446 = htab_create_alloc_ex (127, line_header_hash_voidp,
9447 line_header_eq_voidp,
9448 free_line_header_voidp,
9449 &objfile->objfile_obstack,
9450 hashtab_obstack_allocate,
9451 dummy_obstack_deallocate);
9454 line_header_local.sect_off = line_offset;
9455 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9456 line_header_local_hash = line_header_hash (&line_header_local);
9457 if (dwarf2_per_objfile->line_header_hash != NULL)
9459 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9461 line_header_local_hash, NO_INSERT);
9463 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9464 is not present in *SLOT (since if there is something in *SLOT then
9465 it will be for a partial_unit). */
9466 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9468 gdb_assert (*slot != NULL);
9469 cu->line_header = (struct line_header *) *slot;
9474 /* dwarf_decode_line_header does not yet provide sufficient information.
9475 We always have to call also dwarf_decode_lines for it. */
9476 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9480 cu->line_header = lh.release ();
9481 cu->line_header_die_owner = die;
9483 if (dwarf2_per_objfile->line_header_hash == NULL)
9487 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9489 line_header_local_hash, INSERT);
9490 gdb_assert (slot != NULL);
9492 if (slot != NULL && *slot == NULL)
9494 /* This newly decoded line number information unit will be owned
9495 by line_header_hash hash table. */
9496 *slot = cu->line_header;
9497 cu->line_header_die_owner = NULL;
9501 /* We cannot free any current entry in (*slot) as that struct line_header
9502 may be already used by multiple CUs. Create only temporary decoded
9503 line_header for this CU - it may happen at most once for each line
9504 number information unit. And if we're not using line_header_hash
9505 then this is what we want as well. */
9506 gdb_assert (die->tag != DW_TAG_partial_unit);
9508 decode_mapping = (die->tag != DW_TAG_partial_unit);
9509 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9514 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9517 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9519 struct objfile *objfile = dwarf2_per_objfile->objfile;
9520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9521 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9522 CORE_ADDR highpc = ((CORE_ADDR) 0);
9523 struct attribute *attr;
9524 struct die_info *child_die;
9527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9529 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9531 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9532 from finish_block. */
9533 if (lowpc == ((CORE_ADDR) -1))
9535 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9537 file_and_directory fnd = find_file_and_directory (die, cu);
9539 prepare_one_comp_unit (cu, die, cu->language);
9541 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9542 standardised yet. As a workaround for the language detection we fall
9543 back to the DW_AT_producer string. */
9544 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9545 cu->language = language_opencl;
9547 /* Similar hack for Go. */
9548 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9549 set_cu_language (DW_LANG_Go, cu);
9551 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
9553 /* Decode line number information if present. We do this before
9554 processing child DIEs, so that the line header table is available
9555 for DW_AT_decl_file. */
9556 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
9558 /* Process all dies in compilation unit. */
9559 if (die->child != NULL)
9561 child_die = die->child;
9562 while (child_die && child_die->tag)
9564 process_die (child_die, cu);
9565 child_die = sibling_die (child_die);
9569 /* Decode macro information, if present. Dwarf 2 macro information
9570 refers to information in the line number info statement program
9571 header, so we can only read it if we've read the header
9573 attr = dwarf2_attr (die, DW_AT_macros, cu);
9575 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9576 if (attr && cu->line_header)
9578 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9579 complaint (&symfile_complaints,
9580 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9582 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9586 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9587 if (attr && cu->line_header)
9589 unsigned int macro_offset = DW_UNSND (attr);
9591 dwarf_decode_macros (cu, macro_offset, 0);
9596 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9597 Create the set of symtabs used by this TU, or if this TU is sharing
9598 symtabs with another TU and the symtabs have already been created
9599 then restore those symtabs in the line header.
9600 We don't need the pc/line-number mapping for type units. */
9603 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9605 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9606 struct type_unit_group *tu_group;
9608 struct attribute *attr;
9610 struct signatured_type *sig_type;
9612 gdb_assert (per_cu->is_debug_types);
9613 sig_type = (struct signatured_type *) per_cu;
9615 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9617 /* If we're using .gdb_index (includes -readnow) then
9618 per_cu->type_unit_group may not have been set up yet. */
9619 if (sig_type->type_unit_group == NULL)
9620 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9621 tu_group = sig_type->type_unit_group;
9623 /* If we've already processed this stmt_list there's no real need to
9624 do it again, we could fake it and just recreate the part we need
9625 (file name,index -> symtab mapping). If data shows this optimization
9626 is useful we can do it then. */
9627 first_time = tu_group->compunit_symtab == NULL;
9629 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9634 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
9635 lh = dwarf_decode_line_header (line_offset, cu);
9640 dwarf2_start_symtab (cu, "", NULL, 0);
9643 gdb_assert (tu_group->symtabs == NULL);
9644 restart_symtab (tu_group->compunit_symtab, "", 0);
9649 cu->line_header = lh.release ();
9650 cu->line_header_die_owner = die;
9654 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9656 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9657 still initializing it, and our caller (a few levels up)
9658 process_full_type_unit still needs to know if this is the first
9661 tu_group->num_symtabs = cu->line_header->file_names.size ();
9662 tu_group->symtabs = XNEWVEC (struct symtab *,
9663 cu->line_header->file_names.size ());
9665 for (i = 0; i < cu->line_header->file_names.size (); ++i)
9667 file_entry &fe = cu->line_header->file_names[i];
9669 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
9671 if (current_subfile->symtab == NULL)
9673 /* NOTE: start_subfile will recognize when it's been
9674 passed a file it has already seen. So we can't
9675 assume there's a simple mapping from
9676 cu->line_header->file_names to subfiles, plus
9677 cu->line_header->file_names may contain dups. */
9678 current_subfile->symtab
9679 = allocate_symtab (cust, current_subfile->name);
9682 fe.symtab = current_subfile->symtab;
9683 tu_group->symtabs[i] = fe.symtab;
9688 restart_symtab (tu_group->compunit_symtab, "", 0);
9690 for (i = 0; i < cu->line_header->file_names.size (); ++i)
9692 file_entry &fe = cu->line_header->file_names[i];
9694 fe.symtab = tu_group->symtabs[i];
9698 /* The main symtab is allocated last. Type units don't have DW_AT_name
9699 so they don't have a "real" (so to speak) symtab anyway.
9700 There is later code that will assign the main symtab to all symbols
9701 that don't have one. We need to handle the case of a symbol with a
9702 missing symtab (DW_AT_decl_file) anyway. */
9705 /* Process DW_TAG_type_unit.
9706 For TUs we want to skip the first top level sibling if it's not the
9707 actual type being defined by this TU. In this case the first top
9708 level sibling is there to provide context only. */
9711 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9713 struct die_info *child_die;
9715 prepare_one_comp_unit (cu, die, language_minimal);
9717 /* Initialize (or reinitialize) the machinery for building symtabs.
9718 We do this before processing child DIEs, so that the line header table
9719 is available for DW_AT_decl_file. */
9720 setup_type_unit_groups (die, cu);
9722 if (die->child != NULL)
9724 child_die = die->child;
9725 while (child_die && child_die->tag)
9727 process_die (child_die, cu);
9728 child_die = sibling_die (child_die);
9735 http://gcc.gnu.org/wiki/DebugFission
9736 http://gcc.gnu.org/wiki/DebugFissionDWP
9738 To simplify handling of both DWO files ("object" files with the DWARF info)
9739 and DWP files (a file with the DWOs packaged up into one file), we treat
9740 DWP files as having a collection of virtual DWO files. */
9743 hash_dwo_file (const void *item)
9745 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9748 hash = htab_hash_string (dwo_file->dwo_name);
9749 if (dwo_file->comp_dir != NULL)
9750 hash += htab_hash_string (dwo_file->comp_dir);
9755 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9757 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9758 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9760 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9762 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9763 return lhs->comp_dir == rhs->comp_dir;
9764 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9767 /* Allocate a hash table for DWO files. */
9770 allocate_dwo_file_hash_table (void)
9772 struct objfile *objfile = dwarf2_per_objfile->objfile;
9774 return htab_create_alloc_ex (41,
9778 &objfile->objfile_obstack,
9779 hashtab_obstack_allocate,
9780 dummy_obstack_deallocate);
9783 /* Lookup DWO file DWO_NAME. */
9786 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9788 struct dwo_file find_entry;
9791 if (dwarf2_per_objfile->dwo_files == NULL)
9792 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9794 memset (&find_entry, 0, sizeof (find_entry));
9795 find_entry.dwo_name = dwo_name;
9796 find_entry.comp_dir = comp_dir;
9797 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9803 hash_dwo_unit (const void *item)
9805 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9807 /* This drops the top 32 bits of the id, but is ok for a hash. */
9808 return dwo_unit->signature;
9812 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9814 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9815 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9817 /* The signature is assumed to be unique within the DWO file.
9818 So while object file CU dwo_id's always have the value zero,
9819 that's OK, assuming each object file DWO file has only one CU,
9820 and that's the rule for now. */
9821 return lhs->signature == rhs->signature;
9824 /* Allocate a hash table for DWO CUs,TUs.
9825 There is one of these tables for each of CUs,TUs for each DWO file. */
9828 allocate_dwo_unit_table (struct objfile *objfile)
9830 /* Start out with a pretty small number.
9831 Generally DWO files contain only one CU and maybe some TUs. */
9832 return htab_create_alloc_ex (3,
9836 &objfile->objfile_obstack,
9837 hashtab_obstack_allocate,
9838 dummy_obstack_deallocate);
9841 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9843 struct create_dwo_cu_data
9845 struct dwo_file *dwo_file;
9846 struct dwo_unit dwo_unit;
9849 /* die_reader_func for create_dwo_cu. */
9852 create_dwo_cu_reader (const struct die_reader_specs *reader,
9853 const gdb_byte *info_ptr,
9854 struct die_info *comp_unit_die,
9858 struct dwarf2_cu *cu = reader->cu;
9859 sect_offset sect_off = cu->per_cu->sect_off;
9860 struct dwarf2_section_info *section = cu->per_cu->section;
9861 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9862 struct dwo_file *dwo_file = data->dwo_file;
9863 struct dwo_unit *dwo_unit = &data->dwo_unit;
9864 struct attribute *attr;
9866 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9869 complaint (&symfile_complaints,
9870 _("Dwarf Error: debug entry at offset 0x%x is missing"
9871 " its dwo_id [in module %s]"),
9872 to_underlying (sect_off), dwo_file->dwo_name);
9876 dwo_unit->dwo_file = dwo_file;
9877 dwo_unit->signature = DW_UNSND (attr);
9878 dwo_unit->section = section;
9879 dwo_unit->sect_off = sect_off;
9880 dwo_unit->length = cu->per_cu->length;
9882 if (dwarf_read_debug)
9883 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9884 to_underlying (sect_off),
9885 hex_string (dwo_unit->signature));
9888 /* Create the dwo_units for the CUs in a DWO_FILE.
9889 Note: This function processes DWO files only, not DWP files. */
9892 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info §ion,
9895 struct objfile *objfile = dwarf2_per_objfile->objfile;
9896 const struct dwarf2_section_info *abbrev_section = &dwo_file.sections.abbrev;
9897 const gdb_byte *info_ptr, *end_ptr;
9899 dwarf2_read_section (objfile, §ion);
9900 info_ptr = section.buffer;
9902 if (info_ptr == NULL)
9905 if (dwarf_read_debug)
9907 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9908 get_section_name (§ion),
9909 get_section_file_name (§ion));
9912 end_ptr = info_ptr + section.size;
9913 while (info_ptr < end_ptr)
9915 struct dwarf2_per_cu_data per_cu;
9916 struct create_dwo_cu_data create_dwo_cu_data;
9917 struct dwo_unit *dwo_unit;
9919 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9921 memset (&create_dwo_cu_data.dwo_unit, 0,
9922 sizeof (create_dwo_cu_data.dwo_unit));
9923 memset (&per_cu, 0, sizeof (per_cu));
9924 per_cu.objfile = objfile;
9925 per_cu.is_debug_types = 0;
9926 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9927 per_cu.section = §ion;
9928 create_dwo_cu_data.dwo_file = &dwo_file;
9930 init_cutu_and_read_dies_no_follow (
9931 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
9932 info_ptr += per_cu.length;
9934 // If the unit could not be parsed, skip it.
9935 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
9938 if (cus_htab == NULL)
9939 cus_htab = allocate_dwo_unit_table (objfile);
9941 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9942 *dwo_unit = create_dwo_cu_data.dwo_unit;
9943 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
9944 gdb_assert (slot != NULL);
9947 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9948 sect_offset dup_sect_off = dup_cu->sect_off;
9950 complaint (&symfile_complaints,
9951 _("debug cu entry at offset 0x%x is duplicate to"
9952 " the entry at offset 0x%x, signature %s"),
9953 to_underlying (sect_off), to_underlying (dup_sect_off),
9954 hex_string (dwo_unit->signature));
9956 *slot = (void *)dwo_unit;
9960 /* DWP file .debug_{cu,tu}_index section format:
9961 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9965 Both index sections have the same format, and serve to map a 64-bit
9966 signature to a set of section numbers. Each section begins with a header,
9967 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9968 indexes, and a pool of 32-bit section numbers. The index sections will be
9969 aligned at 8-byte boundaries in the file.
9971 The index section header consists of:
9973 V, 32 bit version number
9975 N, 32 bit number of compilation units or type units in the index
9976 M, 32 bit number of slots in the hash table
9978 Numbers are recorded using the byte order of the application binary.
9980 The hash table begins at offset 16 in the section, and consists of an array
9981 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9982 order of the application binary). Unused slots in the hash table are 0.
9983 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9985 The parallel table begins immediately after the hash table
9986 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9987 array of 32-bit indexes (using the byte order of the application binary),
9988 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9989 table contains a 32-bit index into the pool of section numbers. For unused
9990 hash table slots, the corresponding entry in the parallel table will be 0.
9992 The pool of section numbers begins immediately following the hash table
9993 (at offset 16 + 12 * M from the beginning of the section). The pool of
9994 section numbers consists of an array of 32-bit words (using the byte order
9995 of the application binary). Each item in the array is indexed starting
9996 from 0. The hash table entry provides the index of the first section
9997 number in the set. Additional section numbers in the set follow, and the
9998 set is terminated by a 0 entry (section number 0 is not used in ELF).
10000 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10001 section must be the first entry in the set, and the .debug_abbrev.dwo must
10002 be the second entry. Other members of the set may follow in any order.
10008 DWP Version 2 combines all the .debug_info, etc. sections into one,
10009 and the entries in the index tables are now offsets into these sections.
10010 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10013 Index Section Contents:
10015 Hash Table of Signatures dwp_hash_table.hash_table
10016 Parallel Table of Indices dwp_hash_table.unit_table
10017 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
10018 Table of Section Sizes dwp_hash_table.v2.sizes
10020 The index section header consists of:
10022 V, 32 bit version number
10023 L, 32 bit number of columns in the table of section offsets
10024 N, 32 bit number of compilation units or type units in the index
10025 M, 32 bit number of slots in the hash table
10027 Numbers are recorded using the byte order of the application binary.
10029 The hash table has the same format as version 1.
10030 The parallel table of indices has the same format as version 1,
10031 except that the entries are origin-1 indices into the table of sections
10032 offsets and the table of section sizes.
10034 The table of offsets begins immediately following the parallel table
10035 (at offset 16 + 12 * M from the beginning of the section). The table is
10036 a two-dimensional array of 32-bit words (using the byte order of the
10037 application binary), with L columns and N+1 rows, in row-major order.
10038 Each row in the array is indexed starting from 0. The first row provides
10039 a key to the remaining rows: each column in this row provides an identifier
10040 for a debug section, and the offsets in the same column of subsequent rows
10041 refer to that section. The section identifiers are:
10043 DW_SECT_INFO 1 .debug_info.dwo
10044 DW_SECT_TYPES 2 .debug_types.dwo
10045 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10046 DW_SECT_LINE 4 .debug_line.dwo
10047 DW_SECT_LOC 5 .debug_loc.dwo
10048 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10049 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10050 DW_SECT_MACRO 8 .debug_macro.dwo
10052 The offsets provided by the CU and TU index sections are the base offsets
10053 for the contributions made by each CU or TU to the corresponding section
10054 in the package file. Each CU and TU header contains an abbrev_offset
10055 field, used to find the abbreviations table for that CU or TU within the
10056 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10057 be interpreted as relative to the base offset given in the index section.
10058 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10059 should be interpreted as relative to the base offset for .debug_line.dwo,
10060 and offsets into other debug sections obtained from DWARF attributes should
10061 also be interpreted as relative to the corresponding base offset.
10063 The table of sizes begins immediately following the table of offsets.
10064 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10065 with L columns and N rows, in row-major order. Each row in the array is
10066 indexed starting from 1 (row 0 is shared by the two tables).
10070 Hash table lookup is handled the same in version 1 and 2:
10072 We assume that N and M will not exceed 2^32 - 1.
10073 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10075 Given a 64-bit compilation unit signature or a type signature S, an entry
10076 in the hash table is located as follows:
10078 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10079 the low-order k bits all set to 1.
10081 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10083 3) If the hash table entry at index H matches the signature, use that
10084 entry. If the hash table entry at index H is unused (all zeroes),
10085 terminate the search: the signature is not present in the table.
10087 4) Let H = (H + H') modulo M. Repeat at Step 3.
10089 Because M > N and H' and M are relatively prime, the search is guaranteed
10090 to stop at an unused slot or find the match. */
10092 /* Create a hash table to map DWO IDs to their CU/TU entry in
10093 .debug_{info,types}.dwo in DWP_FILE.
10094 Returns NULL if there isn't one.
10095 Note: This function processes DWP files only, not DWO files. */
10097 static struct dwp_hash_table *
10098 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
10100 struct objfile *objfile = dwarf2_per_objfile->objfile;
10101 bfd *dbfd = dwp_file->dbfd;
10102 const gdb_byte *index_ptr, *index_end;
10103 struct dwarf2_section_info *index;
10104 uint32_t version, nr_columns, nr_units, nr_slots;
10105 struct dwp_hash_table *htab;
10107 if (is_debug_types)
10108 index = &dwp_file->sections.tu_index;
10110 index = &dwp_file->sections.cu_index;
10112 if (dwarf2_section_empty_p (index))
10114 dwarf2_read_section (objfile, index);
10116 index_ptr = index->buffer;
10117 index_end = index_ptr + index->size;
10119 version = read_4_bytes (dbfd, index_ptr);
10122 nr_columns = read_4_bytes (dbfd, index_ptr);
10126 nr_units = read_4_bytes (dbfd, index_ptr);
10128 nr_slots = read_4_bytes (dbfd, index_ptr);
10131 if (version != 1 && version != 2)
10133 error (_("Dwarf Error: unsupported DWP file version (%s)"
10134 " [in module %s]"),
10135 pulongest (version), dwp_file->name);
10137 if (nr_slots != (nr_slots & -nr_slots))
10139 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10140 " is not power of 2 [in module %s]"),
10141 pulongest (nr_slots), dwp_file->name);
10144 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
10145 htab->version = version;
10146 htab->nr_columns = nr_columns;
10147 htab->nr_units = nr_units;
10148 htab->nr_slots = nr_slots;
10149 htab->hash_table = index_ptr;
10150 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10152 /* Exit early if the table is empty. */
10153 if (nr_slots == 0 || nr_units == 0
10154 || (version == 2 && nr_columns == 0))
10156 /* All must be zero. */
10157 if (nr_slots != 0 || nr_units != 0
10158 || (version == 2 && nr_columns != 0))
10160 complaint (&symfile_complaints,
10161 _("Empty DWP but nr_slots,nr_units,nr_columns not"
10162 " all zero [in modules %s]"),
10170 htab->section_pool.v1.indices =
10171 htab->unit_table + sizeof (uint32_t) * nr_slots;
10172 /* It's harder to decide whether the section is too small in v1.
10173 V1 is deprecated anyway so we punt. */
10177 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10178 int *ids = htab->section_pool.v2.section_ids;
10179 /* Reverse map for error checking. */
10180 int ids_seen[DW_SECT_MAX + 1];
10183 if (nr_columns < 2)
10185 error (_("Dwarf Error: bad DWP hash table, too few columns"
10186 " in section table [in module %s]"),
10189 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10191 error (_("Dwarf Error: bad DWP hash table, too many columns"
10192 " in section table [in module %s]"),
10195 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10196 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10197 for (i = 0; i < nr_columns; ++i)
10199 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10201 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10203 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10204 " in section table [in module %s]"),
10205 id, dwp_file->name);
10207 if (ids_seen[id] != -1)
10209 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10210 " id %d in section table [in module %s]"),
10211 id, dwp_file->name);
10216 /* Must have exactly one info or types section. */
10217 if (((ids_seen[DW_SECT_INFO] != -1)
10218 + (ids_seen[DW_SECT_TYPES] != -1))
10221 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10222 " DWO info/types section [in module %s]"),
10225 /* Must have an abbrev section. */
10226 if (ids_seen[DW_SECT_ABBREV] == -1)
10228 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10229 " section [in module %s]"),
10232 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10233 htab->section_pool.v2.sizes =
10234 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10235 * nr_units * nr_columns);
10236 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10237 * nr_units * nr_columns))
10240 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10241 " [in module %s]"),
10249 /* Update SECTIONS with the data from SECTP.
10251 This function is like the other "locate" section routines that are
10252 passed to bfd_map_over_sections, but in this context the sections to
10253 read comes from the DWP V1 hash table, not the full ELF section table.
10255 The result is non-zero for success, or zero if an error was found. */
10258 locate_v1_virtual_dwo_sections (asection *sectp,
10259 struct virtual_v1_dwo_sections *sections)
10261 const struct dwop_section_names *names = &dwop_section_names;
10263 if (section_is_p (sectp->name, &names->abbrev_dwo))
10265 /* There can be only one. */
10266 if (sections->abbrev.s.section != NULL)
10268 sections->abbrev.s.section = sectp;
10269 sections->abbrev.size = bfd_get_section_size (sectp);
10271 else if (section_is_p (sectp->name, &names->info_dwo)
10272 || section_is_p (sectp->name, &names->types_dwo))
10274 /* There can be only one. */
10275 if (sections->info_or_types.s.section != NULL)
10277 sections->info_or_types.s.section = sectp;
10278 sections->info_or_types.size = bfd_get_section_size (sectp);
10280 else if (section_is_p (sectp->name, &names->line_dwo))
10282 /* There can be only one. */
10283 if (sections->line.s.section != NULL)
10285 sections->line.s.section = sectp;
10286 sections->line.size = bfd_get_section_size (sectp);
10288 else if (section_is_p (sectp->name, &names->loc_dwo))
10290 /* There can be only one. */
10291 if (sections->loc.s.section != NULL)
10293 sections->loc.s.section = sectp;
10294 sections->loc.size = bfd_get_section_size (sectp);
10296 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10298 /* There can be only one. */
10299 if (sections->macinfo.s.section != NULL)
10301 sections->macinfo.s.section = sectp;
10302 sections->macinfo.size = bfd_get_section_size (sectp);
10304 else if (section_is_p (sectp->name, &names->macro_dwo))
10306 /* There can be only one. */
10307 if (sections->macro.s.section != NULL)
10309 sections->macro.s.section = sectp;
10310 sections->macro.size = bfd_get_section_size (sectp);
10312 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10314 /* There can be only one. */
10315 if (sections->str_offsets.s.section != NULL)
10317 sections->str_offsets.s.section = sectp;
10318 sections->str_offsets.size = bfd_get_section_size (sectp);
10322 /* No other kind of section is valid. */
10329 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10330 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10331 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10332 This is for DWP version 1 files. */
10334 static struct dwo_unit *
10335 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10336 uint32_t unit_index,
10337 const char *comp_dir,
10338 ULONGEST signature, int is_debug_types)
10340 struct objfile *objfile = dwarf2_per_objfile->objfile;
10341 const struct dwp_hash_table *dwp_htab =
10342 is_debug_types ? dwp_file->tus : dwp_file->cus;
10343 bfd *dbfd = dwp_file->dbfd;
10344 const char *kind = is_debug_types ? "TU" : "CU";
10345 struct dwo_file *dwo_file;
10346 struct dwo_unit *dwo_unit;
10347 struct virtual_v1_dwo_sections sections;
10348 void **dwo_file_slot;
10349 char *virtual_dwo_name;
10350 struct cleanup *cleanups;
10353 gdb_assert (dwp_file->version == 1);
10355 if (dwarf_read_debug)
10357 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10359 pulongest (unit_index), hex_string (signature),
10363 /* Fetch the sections of this DWO unit.
10364 Put a limit on the number of sections we look for so that bad data
10365 doesn't cause us to loop forever. */
10367 #define MAX_NR_V1_DWO_SECTIONS \
10368 (1 /* .debug_info or .debug_types */ \
10369 + 1 /* .debug_abbrev */ \
10370 + 1 /* .debug_line */ \
10371 + 1 /* .debug_loc */ \
10372 + 1 /* .debug_str_offsets */ \
10373 + 1 /* .debug_macro or .debug_macinfo */ \
10374 + 1 /* trailing zero */)
10376 memset (§ions, 0, sizeof (sections));
10377 cleanups = make_cleanup (null_cleanup, 0);
10379 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10382 uint32_t section_nr =
10383 read_4_bytes (dbfd,
10384 dwp_htab->section_pool.v1.indices
10385 + (unit_index + i) * sizeof (uint32_t));
10387 if (section_nr == 0)
10389 if (section_nr >= dwp_file->num_sections)
10391 error (_("Dwarf Error: bad DWP hash table, section number too large"
10392 " [in module %s]"),
10396 sectp = dwp_file->elf_sections[section_nr];
10397 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
10399 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10400 " [in module %s]"),
10406 || dwarf2_section_empty_p (§ions.info_or_types)
10407 || dwarf2_section_empty_p (§ions.abbrev))
10409 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10410 " [in module %s]"),
10413 if (i == MAX_NR_V1_DWO_SECTIONS)
10415 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10416 " [in module %s]"),
10420 /* It's easier for the rest of the code if we fake a struct dwo_file and
10421 have dwo_unit "live" in that. At least for now.
10423 The DWP file can be made up of a random collection of CUs and TUs.
10424 However, for each CU + set of TUs that came from the same original DWO
10425 file, we can combine them back into a virtual DWO file to save space
10426 (fewer struct dwo_file objects to allocate). Remember that for really
10427 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10430 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10431 get_section_id (§ions.abbrev),
10432 get_section_id (§ions.line),
10433 get_section_id (§ions.loc),
10434 get_section_id (§ions.str_offsets));
10435 make_cleanup (xfree, virtual_dwo_name);
10436 /* Can we use an existing virtual DWO file? */
10437 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10438 /* Create one if necessary. */
10439 if (*dwo_file_slot == NULL)
10441 if (dwarf_read_debug)
10443 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10446 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10448 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10450 strlen (virtual_dwo_name));
10451 dwo_file->comp_dir = comp_dir;
10452 dwo_file->sections.abbrev = sections.abbrev;
10453 dwo_file->sections.line = sections.line;
10454 dwo_file->sections.loc = sections.loc;
10455 dwo_file->sections.macinfo = sections.macinfo;
10456 dwo_file->sections.macro = sections.macro;
10457 dwo_file->sections.str_offsets = sections.str_offsets;
10458 /* The "str" section is global to the entire DWP file. */
10459 dwo_file->sections.str = dwp_file->sections.str;
10460 /* The info or types section is assigned below to dwo_unit,
10461 there's no need to record it in dwo_file.
10462 Also, we can't simply record type sections in dwo_file because
10463 we record a pointer into the vector in dwo_unit. As we collect more
10464 types we'll grow the vector and eventually have to reallocate space
10465 for it, invalidating all copies of pointers into the previous
10467 *dwo_file_slot = dwo_file;
10471 if (dwarf_read_debug)
10473 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10476 dwo_file = (struct dwo_file *) *dwo_file_slot;
10478 do_cleanups (cleanups);
10480 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10481 dwo_unit->dwo_file = dwo_file;
10482 dwo_unit->signature = signature;
10483 dwo_unit->section =
10484 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10485 *dwo_unit->section = sections.info_or_types;
10486 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10491 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10492 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10493 piece within that section used by a TU/CU, return a virtual section
10494 of just that piece. */
10496 static struct dwarf2_section_info
10497 create_dwp_v2_section (struct dwarf2_section_info *section,
10498 bfd_size_type offset, bfd_size_type size)
10500 struct dwarf2_section_info result;
10503 gdb_assert (section != NULL);
10504 gdb_assert (!section->is_virtual);
10506 memset (&result, 0, sizeof (result));
10507 result.s.containing_section = section;
10508 result.is_virtual = 1;
10513 sectp = get_section_bfd_section (section);
10515 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10516 bounds of the real section. This is a pretty-rare event, so just
10517 flag an error (easier) instead of a warning and trying to cope. */
10519 || offset + size > bfd_get_section_size (sectp))
10521 bfd *abfd = sectp->owner;
10523 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10524 " in section %s [in module %s]"),
10525 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10526 objfile_name (dwarf2_per_objfile->objfile));
10529 result.virtual_offset = offset;
10530 result.size = size;
10534 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10535 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10536 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10537 This is for DWP version 2 files. */
10539 static struct dwo_unit *
10540 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10541 uint32_t unit_index,
10542 const char *comp_dir,
10543 ULONGEST signature, int is_debug_types)
10545 struct objfile *objfile = dwarf2_per_objfile->objfile;
10546 const struct dwp_hash_table *dwp_htab =
10547 is_debug_types ? dwp_file->tus : dwp_file->cus;
10548 bfd *dbfd = dwp_file->dbfd;
10549 const char *kind = is_debug_types ? "TU" : "CU";
10550 struct dwo_file *dwo_file;
10551 struct dwo_unit *dwo_unit;
10552 struct virtual_v2_dwo_sections sections;
10553 void **dwo_file_slot;
10554 char *virtual_dwo_name;
10555 struct cleanup *cleanups;
10558 gdb_assert (dwp_file->version == 2);
10560 if (dwarf_read_debug)
10562 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10564 pulongest (unit_index), hex_string (signature),
10568 /* Fetch the section offsets of this DWO unit. */
10570 memset (§ions, 0, sizeof (sections));
10571 cleanups = make_cleanup (null_cleanup, 0);
10573 for (i = 0; i < dwp_htab->nr_columns; ++i)
10575 uint32_t offset = read_4_bytes (dbfd,
10576 dwp_htab->section_pool.v2.offsets
10577 + (((unit_index - 1) * dwp_htab->nr_columns
10579 * sizeof (uint32_t)));
10580 uint32_t size = read_4_bytes (dbfd,
10581 dwp_htab->section_pool.v2.sizes
10582 + (((unit_index - 1) * dwp_htab->nr_columns
10584 * sizeof (uint32_t)));
10586 switch (dwp_htab->section_pool.v2.section_ids[i])
10589 case DW_SECT_TYPES:
10590 sections.info_or_types_offset = offset;
10591 sections.info_or_types_size = size;
10593 case DW_SECT_ABBREV:
10594 sections.abbrev_offset = offset;
10595 sections.abbrev_size = size;
10598 sections.line_offset = offset;
10599 sections.line_size = size;
10602 sections.loc_offset = offset;
10603 sections.loc_size = size;
10605 case DW_SECT_STR_OFFSETS:
10606 sections.str_offsets_offset = offset;
10607 sections.str_offsets_size = size;
10609 case DW_SECT_MACINFO:
10610 sections.macinfo_offset = offset;
10611 sections.macinfo_size = size;
10613 case DW_SECT_MACRO:
10614 sections.macro_offset = offset;
10615 sections.macro_size = size;
10620 /* It's easier for the rest of the code if we fake a struct dwo_file and
10621 have dwo_unit "live" in that. At least for now.
10623 The DWP file can be made up of a random collection of CUs and TUs.
10624 However, for each CU + set of TUs that came from the same original DWO
10625 file, we can combine them back into a virtual DWO file to save space
10626 (fewer struct dwo_file objects to allocate). Remember that for really
10627 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10630 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10631 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10632 (long) (sections.line_size ? sections.line_offset : 0),
10633 (long) (sections.loc_size ? sections.loc_offset : 0),
10634 (long) (sections.str_offsets_size
10635 ? sections.str_offsets_offset : 0));
10636 make_cleanup (xfree, virtual_dwo_name);
10637 /* Can we use an existing virtual DWO file? */
10638 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10639 /* Create one if necessary. */
10640 if (*dwo_file_slot == NULL)
10642 if (dwarf_read_debug)
10644 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10647 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10649 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10651 strlen (virtual_dwo_name));
10652 dwo_file->comp_dir = comp_dir;
10653 dwo_file->sections.abbrev =
10654 create_dwp_v2_section (&dwp_file->sections.abbrev,
10655 sections.abbrev_offset, sections.abbrev_size);
10656 dwo_file->sections.line =
10657 create_dwp_v2_section (&dwp_file->sections.line,
10658 sections.line_offset, sections.line_size);
10659 dwo_file->sections.loc =
10660 create_dwp_v2_section (&dwp_file->sections.loc,
10661 sections.loc_offset, sections.loc_size);
10662 dwo_file->sections.macinfo =
10663 create_dwp_v2_section (&dwp_file->sections.macinfo,
10664 sections.macinfo_offset, sections.macinfo_size);
10665 dwo_file->sections.macro =
10666 create_dwp_v2_section (&dwp_file->sections.macro,
10667 sections.macro_offset, sections.macro_size);
10668 dwo_file->sections.str_offsets =
10669 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10670 sections.str_offsets_offset,
10671 sections.str_offsets_size);
10672 /* The "str" section is global to the entire DWP file. */
10673 dwo_file->sections.str = dwp_file->sections.str;
10674 /* The info or types section is assigned below to dwo_unit,
10675 there's no need to record it in dwo_file.
10676 Also, we can't simply record type sections in dwo_file because
10677 we record a pointer into the vector in dwo_unit. As we collect more
10678 types we'll grow the vector and eventually have to reallocate space
10679 for it, invalidating all copies of pointers into the previous
10681 *dwo_file_slot = dwo_file;
10685 if (dwarf_read_debug)
10687 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10690 dwo_file = (struct dwo_file *) *dwo_file_slot;
10692 do_cleanups (cleanups);
10694 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10695 dwo_unit->dwo_file = dwo_file;
10696 dwo_unit->signature = signature;
10697 dwo_unit->section =
10698 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10699 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10700 ? &dwp_file->sections.types
10701 : &dwp_file->sections.info,
10702 sections.info_or_types_offset,
10703 sections.info_or_types_size);
10704 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10709 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10710 Returns NULL if the signature isn't found. */
10712 static struct dwo_unit *
10713 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10714 ULONGEST signature, int is_debug_types)
10716 const struct dwp_hash_table *dwp_htab =
10717 is_debug_types ? dwp_file->tus : dwp_file->cus;
10718 bfd *dbfd = dwp_file->dbfd;
10719 uint32_t mask = dwp_htab->nr_slots - 1;
10720 uint32_t hash = signature & mask;
10721 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10724 struct dwo_unit find_dwo_cu;
10726 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10727 find_dwo_cu.signature = signature;
10728 slot = htab_find_slot (is_debug_types
10729 ? dwp_file->loaded_tus
10730 : dwp_file->loaded_cus,
10731 &find_dwo_cu, INSERT);
10734 return (struct dwo_unit *) *slot;
10736 /* Use a for loop so that we don't loop forever on bad debug info. */
10737 for (i = 0; i < dwp_htab->nr_slots; ++i)
10739 ULONGEST signature_in_table;
10741 signature_in_table =
10742 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10743 if (signature_in_table == signature)
10745 uint32_t unit_index =
10746 read_4_bytes (dbfd,
10747 dwp_htab->unit_table + hash * sizeof (uint32_t));
10749 if (dwp_file->version == 1)
10751 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10752 comp_dir, signature,
10757 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10758 comp_dir, signature,
10761 return (struct dwo_unit *) *slot;
10763 if (signature_in_table == 0)
10765 hash = (hash + hash2) & mask;
10768 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10769 " [in module %s]"),
10773 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10774 Open the file specified by FILE_NAME and hand it off to BFD for
10775 preliminary analysis. Return a newly initialized bfd *, which
10776 includes a canonicalized copy of FILE_NAME.
10777 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10778 SEARCH_CWD is true if the current directory is to be searched.
10779 It will be searched before debug-file-directory.
10780 If successful, the file is added to the bfd include table of the
10781 objfile's bfd (see gdb_bfd_record_inclusion).
10782 If unable to find/open the file, return NULL.
10783 NOTE: This function is derived from symfile_bfd_open. */
10785 static gdb_bfd_ref_ptr
10786 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10789 char *absolute_name;
10790 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10791 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10792 to debug_file_directory. */
10794 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10798 if (*debug_file_directory != '\0')
10799 search_path = concat (".", dirname_separator_string,
10800 debug_file_directory, (char *) NULL);
10802 search_path = xstrdup (".");
10805 search_path = xstrdup (debug_file_directory);
10807 flags = OPF_RETURN_REALPATH;
10809 flags |= OPF_SEARCH_IN_PATH;
10810 desc = openp (search_path, flags, file_name,
10811 O_RDONLY | O_BINARY, &absolute_name);
10812 xfree (search_path);
10816 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10817 xfree (absolute_name);
10818 if (sym_bfd == NULL)
10820 bfd_set_cacheable (sym_bfd.get (), 1);
10822 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10825 /* Success. Record the bfd as having been included by the objfile's bfd.
10826 This is important because things like demangled_names_hash lives in the
10827 objfile's per_bfd space and may have references to things like symbol
10828 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10829 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10834 /* Try to open DWO file FILE_NAME.
10835 COMP_DIR is the DW_AT_comp_dir attribute.
10836 The result is the bfd handle of the file.
10837 If there is a problem finding or opening the file, return NULL.
10838 Upon success, the canonicalized path of the file is stored in the bfd,
10839 same as symfile_bfd_open. */
10841 static gdb_bfd_ref_ptr
10842 open_dwo_file (const char *file_name, const char *comp_dir)
10844 if (IS_ABSOLUTE_PATH (file_name))
10845 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10847 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10849 if (comp_dir != NULL)
10851 char *path_to_try = concat (comp_dir, SLASH_STRING,
10852 file_name, (char *) NULL);
10854 /* NOTE: If comp_dir is a relative path, this will also try the
10855 search path, which seems useful. */
10856 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10857 1 /*search_cwd*/));
10858 xfree (path_to_try);
10863 /* That didn't work, try debug-file-directory, which, despite its name,
10864 is a list of paths. */
10866 if (*debug_file_directory == '\0')
10869 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10872 /* This function is mapped across the sections and remembers the offset and
10873 size of each of the DWO debugging sections we are interested in. */
10876 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10878 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10879 const struct dwop_section_names *names = &dwop_section_names;
10881 if (section_is_p (sectp->name, &names->abbrev_dwo))
10883 dwo_sections->abbrev.s.section = sectp;
10884 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10886 else if (section_is_p (sectp->name, &names->info_dwo))
10888 dwo_sections->info.s.section = sectp;
10889 dwo_sections->info.size = bfd_get_section_size (sectp);
10891 else if (section_is_p (sectp->name, &names->line_dwo))
10893 dwo_sections->line.s.section = sectp;
10894 dwo_sections->line.size = bfd_get_section_size (sectp);
10896 else if (section_is_p (sectp->name, &names->loc_dwo))
10898 dwo_sections->loc.s.section = sectp;
10899 dwo_sections->loc.size = bfd_get_section_size (sectp);
10901 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10903 dwo_sections->macinfo.s.section = sectp;
10904 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10906 else if (section_is_p (sectp->name, &names->macro_dwo))
10908 dwo_sections->macro.s.section = sectp;
10909 dwo_sections->macro.size = bfd_get_section_size (sectp);
10911 else if (section_is_p (sectp->name, &names->str_dwo))
10913 dwo_sections->str.s.section = sectp;
10914 dwo_sections->str.size = bfd_get_section_size (sectp);
10916 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10918 dwo_sections->str_offsets.s.section = sectp;
10919 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10921 else if (section_is_p (sectp->name, &names->types_dwo))
10923 struct dwarf2_section_info type_section;
10925 memset (&type_section, 0, sizeof (type_section));
10926 type_section.s.section = sectp;
10927 type_section.size = bfd_get_section_size (sectp);
10928 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10933 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10934 by PER_CU. This is for the non-DWP case.
10935 The result is NULL if DWO_NAME can't be found. */
10937 static struct dwo_file *
10938 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10939 const char *dwo_name, const char *comp_dir)
10941 struct objfile *objfile = dwarf2_per_objfile->objfile;
10942 struct dwo_file *dwo_file;
10943 struct cleanup *cleanups;
10945 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10948 if (dwarf_read_debug)
10949 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10952 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10953 dwo_file->dwo_name = dwo_name;
10954 dwo_file->comp_dir = comp_dir;
10955 dwo_file->dbfd = dbfd.release ();
10957 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10959 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10960 &dwo_file->sections);
10962 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
10964 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10967 discard_cleanups (cleanups);
10969 if (dwarf_read_debug)
10970 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10975 /* This function is mapped across the sections and remembers the offset and
10976 size of each of the DWP debugging sections common to version 1 and 2 that
10977 we are interested in. */
10980 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10981 void *dwp_file_ptr)
10983 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10984 const struct dwop_section_names *names = &dwop_section_names;
10985 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10987 /* Record the ELF section number for later lookup: this is what the
10988 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10989 gdb_assert (elf_section_nr < dwp_file->num_sections);
10990 dwp_file->elf_sections[elf_section_nr] = sectp;
10992 /* Look for specific sections that we need. */
10993 if (section_is_p (sectp->name, &names->str_dwo))
10995 dwp_file->sections.str.s.section = sectp;
10996 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10998 else if (section_is_p (sectp->name, &names->cu_index))
11000 dwp_file->sections.cu_index.s.section = sectp;
11001 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
11003 else if (section_is_p (sectp->name, &names->tu_index))
11005 dwp_file->sections.tu_index.s.section = sectp;
11006 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
11010 /* This function is mapped across the sections and remembers the offset and
11011 size of each of the DWP version 2 debugging sections that we are interested
11012 in. This is split into a separate function because we don't know if we
11013 have version 1 or 2 until we parse the cu_index/tu_index sections. */
11016 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
11018 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11019 const struct dwop_section_names *names = &dwop_section_names;
11020 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11022 /* Record the ELF section number for later lookup: this is what the
11023 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11024 gdb_assert (elf_section_nr < dwp_file->num_sections);
11025 dwp_file->elf_sections[elf_section_nr] = sectp;
11027 /* Look for specific sections that we need. */
11028 if (section_is_p (sectp->name, &names->abbrev_dwo))
11030 dwp_file->sections.abbrev.s.section = sectp;
11031 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
11033 else if (section_is_p (sectp->name, &names->info_dwo))
11035 dwp_file->sections.info.s.section = sectp;
11036 dwp_file->sections.info.size = bfd_get_section_size (sectp);
11038 else if (section_is_p (sectp->name, &names->line_dwo))
11040 dwp_file->sections.line.s.section = sectp;
11041 dwp_file->sections.line.size = bfd_get_section_size (sectp);
11043 else if (section_is_p (sectp->name, &names->loc_dwo))
11045 dwp_file->sections.loc.s.section = sectp;
11046 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
11048 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11050 dwp_file->sections.macinfo.s.section = sectp;
11051 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
11053 else if (section_is_p (sectp->name, &names->macro_dwo))
11055 dwp_file->sections.macro.s.section = sectp;
11056 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
11058 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11060 dwp_file->sections.str_offsets.s.section = sectp;
11061 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
11063 else if (section_is_p (sectp->name, &names->types_dwo))
11065 dwp_file->sections.types.s.section = sectp;
11066 dwp_file->sections.types.size = bfd_get_section_size (sectp);
11070 /* Hash function for dwp_file loaded CUs/TUs. */
11073 hash_dwp_loaded_cutus (const void *item)
11075 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11077 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11078 return dwo_unit->signature;
11081 /* Equality function for dwp_file loaded CUs/TUs. */
11084 eq_dwp_loaded_cutus (const void *a, const void *b)
11086 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11087 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11089 return dua->signature == dub->signature;
11092 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11095 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
11097 return htab_create_alloc_ex (3,
11098 hash_dwp_loaded_cutus,
11099 eq_dwp_loaded_cutus,
11101 &objfile->objfile_obstack,
11102 hashtab_obstack_allocate,
11103 dummy_obstack_deallocate);
11106 /* Try to open DWP file FILE_NAME.
11107 The result is the bfd handle of the file.
11108 If there is a problem finding or opening the file, return NULL.
11109 Upon success, the canonicalized path of the file is stored in the bfd,
11110 same as symfile_bfd_open. */
11112 static gdb_bfd_ref_ptr
11113 open_dwp_file (const char *file_name)
11115 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
11116 1 /*search_cwd*/));
11120 /* Work around upstream bug 15652.
11121 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11122 [Whether that's a "bug" is debatable, but it is getting in our way.]
11123 We have no real idea where the dwp file is, because gdb's realpath-ing
11124 of the executable's path may have discarded the needed info.
11125 [IWBN if the dwp file name was recorded in the executable, akin to
11126 .gnu_debuglink, but that doesn't exist yet.]
11127 Strip the directory from FILE_NAME and search again. */
11128 if (*debug_file_directory != '\0')
11130 /* Don't implicitly search the current directory here.
11131 If the user wants to search "." to handle this case,
11132 it must be added to debug-file-directory. */
11133 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
11140 /* Initialize the use of the DWP file for the current objfile.
11141 By convention the name of the DWP file is ${objfile}.dwp.
11142 The result is NULL if it can't be found. */
11144 static struct dwp_file *
11145 open_and_init_dwp_file (void)
11147 struct objfile *objfile = dwarf2_per_objfile->objfile;
11148 struct dwp_file *dwp_file;
11150 /* Try to find first .dwp for the binary file before any symbolic links
11153 /* If the objfile is a debug file, find the name of the real binary
11154 file and get the name of dwp file from there. */
11155 std::string dwp_name;
11156 if (objfile->separate_debug_objfile_backlink != NULL)
11158 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11159 const char *backlink_basename = lbasename (backlink->original_name);
11161 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11164 dwp_name = objfile->original_name;
11166 dwp_name += ".dwp";
11168 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
11170 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11172 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11173 dwp_name = objfile_name (objfile);
11174 dwp_name += ".dwp";
11175 dbfd = open_dwp_file (dwp_name.c_str ());
11180 if (dwarf_read_debug)
11181 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
11184 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11185 dwp_file->name = bfd_get_filename (dbfd.get ());
11186 dwp_file->dbfd = dbfd.release ();
11188 /* +1: section 0 is unused */
11189 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11190 dwp_file->elf_sections =
11191 OBSTACK_CALLOC (&objfile->objfile_obstack,
11192 dwp_file->num_sections, asection *);
11194 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11197 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11199 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11201 /* The DWP file version is stored in the hash table. Oh well. */
11202 if (dwp_file->cus->version != dwp_file->tus->version)
11204 /* Technically speaking, we should try to limp along, but this is
11205 pretty bizarre. We use pulongest here because that's the established
11206 portability solution (e.g, we cannot use %u for uint32_t). */
11207 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11208 " TU version %s [in DWP file %s]"),
11209 pulongest (dwp_file->cus->version),
11210 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11212 dwp_file->version = dwp_file->cus->version;
11214 if (dwp_file->version == 2)
11215 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11218 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11219 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11221 if (dwarf_read_debug)
11223 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11224 fprintf_unfiltered (gdb_stdlog,
11225 " %s CUs, %s TUs\n",
11226 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11227 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11233 /* Wrapper around open_and_init_dwp_file, only open it once. */
11235 static struct dwp_file *
11236 get_dwp_file (void)
11238 if (! dwarf2_per_objfile->dwp_checked)
11240 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11241 dwarf2_per_objfile->dwp_checked = 1;
11243 return dwarf2_per_objfile->dwp_file;
11246 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11247 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11248 or in the DWP file for the objfile, referenced by THIS_UNIT.
11249 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11250 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11252 This is called, for example, when wanting to read a variable with a
11253 complex location. Therefore we don't want to do file i/o for every call.
11254 Therefore we don't want to look for a DWO file on every call.
11255 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11256 then we check if we've already seen DWO_NAME, and only THEN do we check
11259 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11260 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11262 static struct dwo_unit *
11263 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11264 const char *dwo_name, const char *comp_dir,
11265 ULONGEST signature, int is_debug_types)
11267 struct objfile *objfile = dwarf2_per_objfile->objfile;
11268 const char *kind = is_debug_types ? "TU" : "CU";
11269 void **dwo_file_slot;
11270 struct dwo_file *dwo_file;
11271 struct dwp_file *dwp_file;
11273 /* First see if there's a DWP file.
11274 If we have a DWP file but didn't find the DWO inside it, don't
11275 look for the original DWO file. It makes gdb behave differently
11276 depending on whether one is debugging in the build tree. */
11278 dwp_file = get_dwp_file ();
11279 if (dwp_file != NULL)
11281 const struct dwp_hash_table *dwp_htab =
11282 is_debug_types ? dwp_file->tus : dwp_file->cus;
11284 if (dwp_htab != NULL)
11286 struct dwo_unit *dwo_cutu =
11287 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11288 signature, is_debug_types);
11290 if (dwo_cutu != NULL)
11292 if (dwarf_read_debug)
11294 fprintf_unfiltered (gdb_stdlog,
11295 "Virtual DWO %s %s found: @%s\n",
11296 kind, hex_string (signature),
11297 host_address_to_string (dwo_cutu));
11305 /* No DWP file, look for the DWO file. */
11307 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11308 if (*dwo_file_slot == NULL)
11310 /* Read in the file and build a table of the CUs/TUs it contains. */
11311 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11313 /* NOTE: This will be NULL if unable to open the file. */
11314 dwo_file = (struct dwo_file *) *dwo_file_slot;
11316 if (dwo_file != NULL)
11318 struct dwo_unit *dwo_cutu = NULL;
11320 if (is_debug_types && dwo_file->tus)
11322 struct dwo_unit find_dwo_cutu;
11324 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11325 find_dwo_cutu.signature = signature;
11327 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11329 else if (!is_debug_types && dwo_file->cus)
11331 struct dwo_unit find_dwo_cutu;
11333 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11334 find_dwo_cutu.signature = signature;
11335 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
11339 if (dwo_cutu != NULL)
11341 if (dwarf_read_debug)
11343 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11344 kind, dwo_name, hex_string (signature),
11345 host_address_to_string (dwo_cutu));
11352 /* We didn't find it. This could mean a dwo_id mismatch, or
11353 someone deleted the DWO/DWP file, or the search path isn't set up
11354 correctly to find the file. */
11356 if (dwarf_read_debug)
11358 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11359 kind, dwo_name, hex_string (signature));
11362 /* This is a warning and not a complaint because it can be caused by
11363 pilot error (e.g., user accidentally deleting the DWO). */
11365 /* Print the name of the DWP file if we looked there, helps the user
11366 better diagnose the problem. */
11367 char *dwp_text = NULL;
11368 struct cleanup *cleanups;
11370 if (dwp_file != NULL)
11371 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11372 cleanups = make_cleanup (xfree, dwp_text);
11374 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11375 " [in module %s]"),
11376 kind, dwo_name, hex_string (signature),
11377 dwp_text != NULL ? dwp_text : "",
11378 this_unit->is_debug_types ? "TU" : "CU",
11379 to_underlying (this_unit->sect_off), objfile_name (objfile));
11381 do_cleanups (cleanups);
11386 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11387 See lookup_dwo_cutu_unit for details. */
11389 static struct dwo_unit *
11390 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11391 const char *dwo_name, const char *comp_dir,
11392 ULONGEST signature)
11394 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11397 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11398 See lookup_dwo_cutu_unit for details. */
11400 static struct dwo_unit *
11401 lookup_dwo_type_unit (struct signatured_type *this_tu,
11402 const char *dwo_name, const char *comp_dir)
11404 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11407 /* Traversal function for queue_and_load_all_dwo_tus. */
11410 queue_and_load_dwo_tu (void **slot, void *info)
11412 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11413 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11414 ULONGEST signature = dwo_unit->signature;
11415 struct signatured_type *sig_type =
11416 lookup_dwo_signatured_type (per_cu->cu, signature);
11418 if (sig_type != NULL)
11420 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11422 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11423 a real dependency of PER_CU on SIG_TYPE. That is detected later
11424 while processing PER_CU. */
11425 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11426 load_full_type_unit (sig_cu);
11427 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11433 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11434 The DWO may have the only definition of the type, though it may not be
11435 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11436 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11439 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11441 struct dwo_unit *dwo_unit;
11442 struct dwo_file *dwo_file;
11444 gdb_assert (!per_cu->is_debug_types);
11445 gdb_assert (get_dwp_file () == NULL);
11446 gdb_assert (per_cu->cu != NULL);
11448 dwo_unit = per_cu->cu->dwo_unit;
11449 gdb_assert (dwo_unit != NULL);
11451 dwo_file = dwo_unit->dwo_file;
11452 if (dwo_file->tus != NULL)
11453 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11456 /* Free all resources associated with DWO_FILE.
11457 Close the DWO file and munmap the sections.
11458 All memory should be on the objfile obstack. */
11461 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11464 /* Note: dbfd is NULL for virtual DWO files. */
11465 gdb_bfd_unref (dwo_file->dbfd);
11467 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11470 /* Wrapper for free_dwo_file for use in cleanups. */
11473 free_dwo_file_cleanup (void *arg)
11475 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11476 struct objfile *objfile = dwarf2_per_objfile->objfile;
11478 free_dwo_file (dwo_file, objfile);
11481 /* Traversal function for free_dwo_files. */
11484 free_dwo_file_from_slot (void **slot, void *info)
11486 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11487 struct objfile *objfile = (struct objfile *) info;
11489 free_dwo_file (dwo_file, objfile);
11494 /* Free all resources associated with DWO_FILES. */
11497 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11499 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11502 /* Read in various DIEs. */
11504 /* qsort helper for inherit_abstract_dies. */
11507 unsigned_int_compar (const void *ap, const void *bp)
11509 unsigned int a = *(unsigned int *) ap;
11510 unsigned int b = *(unsigned int *) bp;
11512 return (a > b) - (b > a);
11515 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11516 Inherit only the children of the DW_AT_abstract_origin DIE not being
11517 already referenced by DW_AT_abstract_origin from the children of the
11521 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11523 struct die_info *child_die;
11524 unsigned die_children_count;
11525 /* CU offsets which were referenced by children of the current DIE. */
11526 sect_offset *offsets;
11527 sect_offset *offsets_end, *offsetp;
11528 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11529 struct die_info *origin_die;
11530 /* Iterator of the ORIGIN_DIE children. */
11531 struct die_info *origin_child_die;
11532 struct cleanup *cleanups;
11533 struct attribute *attr;
11534 struct dwarf2_cu *origin_cu;
11535 struct pending **origin_previous_list_in_scope;
11537 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11541 /* Note that following die references may follow to a die in a
11545 origin_die = follow_die_ref (die, attr, &origin_cu);
11547 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11549 origin_previous_list_in_scope = origin_cu->list_in_scope;
11550 origin_cu->list_in_scope = cu->list_in_scope;
11552 if (die->tag != origin_die->tag
11553 && !(die->tag == DW_TAG_inlined_subroutine
11554 && origin_die->tag == DW_TAG_subprogram))
11555 complaint (&symfile_complaints,
11556 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11557 to_underlying (die->sect_off),
11558 to_underlying (origin_die->sect_off));
11560 child_die = die->child;
11561 die_children_count = 0;
11562 while (child_die && child_die->tag)
11564 child_die = sibling_die (child_die);
11565 die_children_count++;
11567 offsets = XNEWVEC (sect_offset, die_children_count);
11568 cleanups = make_cleanup (xfree, offsets);
11570 offsets_end = offsets;
11571 for (child_die = die->child;
11572 child_die && child_die->tag;
11573 child_die = sibling_die (child_die))
11575 struct die_info *child_origin_die;
11576 struct dwarf2_cu *child_origin_cu;
11578 /* We are trying to process concrete instance entries:
11579 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11580 it's not relevant to our analysis here. i.e. detecting DIEs that are
11581 present in the abstract instance but not referenced in the concrete
11583 if (child_die->tag == DW_TAG_call_site
11584 || child_die->tag == DW_TAG_GNU_call_site)
11587 /* For each CHILD_DIE, find the corresponding child of
11588 ORIGIN_DIE. If there is more than one layer of
11589 DW_AT_abstract_origin, follow them all; there shouldn't be,
11590 but GCC versions at least through 4.4 generate this (GCC PR
11592 child_origin_die = child_die;
11593 child_origin_cu = cu;
11596 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11600 child_origin_die = follow_die_ref (child_origin_die, attr,
11604 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11605 counterpart may exist. */
11606 if (child_origin_die != child_die)
11608 if (child_die->tag != child_origin_die->tag
11609 && !(child_die->tag == DW_TAG_inlined_subroutine
11610 && child_origin_die->tag == DW_TAG_subprogram))
11611 complaint (&symfile_complaints,
11612 _("Child DIE 0x%x and its abstract origin 0x%x have "
11614 to_underlying (child_die->sect_off),
11615 to_underlying (child_origin_die->sect_off));
11616 if (child_origin_die->parent != origin_die)
11617 complaint (&symfile_complaints,
11618 _("Child DIE 0x%x and its abstract origin 0x%x have "
11619 "different parents"),
11620 to_underlying (child_die->sect_off),
11621 to_underlying (child_origin_die->sect_off));
11623 *offsets_end++ = child_origin_die->sect_off;
11626 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11627 unsigned_int_compar);
11628 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11629 if (offsetp[-1] == *offsetp)
11630 complaint (&symfile_complaints,
11631 _("Multiple children of DIE 0x%x refer "
11632 "to DIE 0x%x as their abstract origin"),
11633 to_underlying (die->sect_off), to_underlying (*offsetp));
11636 origin_child_die = origin_die->child;
11637 while (origin_child_die && origin_child_die->tag)
11639 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11640 while (offsetp < offsets_end
11641 && *offsetp < origin_child_die->sect_off)
11643 if (offsetp >= offsets_end
11644 || *offsetp > origin_child_die->sect_off)
11646 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11647 Check whether we're already processing ORIGIN_CHILD_DIE.
11648 This can happen with mutually referenced abstract_origins.
11650 if (!origin_child_die->in_process)
11651 process_die (origin_child_die, origin_cu);
11653 origin_child_die = sibling_die (origin_child_die);
11655 origin_cu->list_in_scope = origin_previous_list_in_scope;
11657 do_cleanups (cleanups);
11661 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11663 struct objfile *objfile = cu->objfile;
11664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11665 struct context_stack *newobj;
11668 struct die_info *child_die;
11669 struct attribute *attr, *call_line, *call_file;
11671 CORE_ADDR baseaddr;
11672 struct block *block;
11673 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11674 VEC (symbolp) *template_args = NULL;
11675 struct template_symbol *templ_func = NULL;
11679 /* If we do not have call site information, we can't show the
11680 caller of this inlined function. That's too confusing, so
11681 only use the scope for local variables. */
11682 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11683 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11684 if (call_line == NULL || call_file == NULL)
11686 read_lexical_block_scope (die, cu);
11691 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11693 name = dwarf2_name (die, cu);
11695 /* Ignore functions with missing or empty names. These are actually
11696 illegal according to the DWARF standard. */
11699 complaint (&symfile_complaints,
11700 _("missing name for subprogram DIE at %d"),
11701 to_underlying (die->sect_off));
11705 /* Ignore functions with missing or invalid low and high pc attributes. */
11706 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11707 <= PC_BOUNDS_INVALID)
11709 attr = dwarf2_attr (die, DW_AT_external, cu);
11710 if (!attr || !DW_UNSND (attr))
11711 complaint (&symfile_complaints,
11712 _("cannot get low and high bounds "
11713 "for subprogram DIE at %d"),
11714 to_underlying (die->sect_off));
11718 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11719 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11721 /* If we have any template arguments, then we must allocate a
11722 different sort of symbol. */
11723 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11725 if (child_die->tag == DW_TAG_template_type_param
11726 || child_die->tag == DW_TAG_template_value_param)
11728 templ_func = allocate_template_symbol (objfile);
11729 templ_func->base.is_cplus_template_function = 1;
11734 newobj = push_context (0, lowpc);
11735 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11736 (struct symbol *) templ_func);
11738 /* If there is a location expression for DW_AT_frame_base, record
11740 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11742 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11744 /* If there is a location for the static link, record it. */
11745 newobj->static_link = NULL;
11746 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11749 newobj->static_link
11750 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11751 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11754 cu->list_in_scope = &local_symbols;
11756 if (die->child != NULL)
11758 child_die = die->child;
11759 while (child_die && child_die->tag)
11761 if (child_die->tag == DW_TAG_template_type_param
11762 || child_die->tag == DW_TAG_template_value_param)
11764 struct symbol *arg = new_symbol (child_die, NULL, cu);
11767 VEC_safe_push (symbolp, template_args, arg);
11770 process_die (child_die, cu);
11771 child_die = sibling_die (child_die);
11775 inherit_abstract_dies (die, cu);
11777 /* If we have a DW_AT_specification, we might need to import using
11778 directives from the context of the specification DIE. See the
11779 comment in determine_prefix. */
11780 if (cu->language == language_cplus
11781 && dwarf2_attr (die, DW_AT_specification, cu))
11783 struct dwarf2_cu *spec_cu = cu;
11784 struct die_info *spec_die = die_specification (die, &spec_cu);
11788 child_die = spec_die->child;
11789 while (child_die && child_die->tag)
11791 if (child_die->tag == DW_TAG_imported_module)
11792 process_die (child_die, spec_cu);
11793 child_die = sibling_die (child_die);
11796 /* In some cases, GCC generates specification DIEs that
11797 themselves contain DW_AT_specification attributes. */
11798 spec_die = die_specification (spec_die, &spec_cu);
11802 newobj = pop_context ();
11803 /* Make a block for the local symbols within. */
11804 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11805 newobj->static_link, lowpc, highpc);
11807 /* For C++, set the block's scope. */
11808 if ((cu->language == language_cplus
11809 || cu->language == language_fortran
11810 || cu->language == language_d
11811 || cu->language == language_rust)
11812 && cu->processing_has_namespace_info)
11813 block_set_scope (block, determine_prefix (die, cu),
11814 &objfile->objfile_obstack);
11816 /* If we have address ranges, record them. */
11817 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11819 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11821 /* Attach template arguments to function. */
11822 if (! VEC_empty (symbolp, template_args))
11824 gdb_assert (templ_func != NULL);
11826 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11827 templ_func->template_arguments
11828 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11829 templ_func->n_template_arguments);
11830 memcpy (templ_func->template_arguments,
11831 VEC_address (symbolp, template_args),
11832 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11833 VEC_free (symbolp, template_args);
11836 /* In C++, we can have functions nested inside functions (e.g., when
11837 a function declares a class that has methods). This means that
11838 when we finish processing a function scope, we may need to go
11839 back to building a containing block's symbol lists. */
11840 local_symbols = newobj->locals;
11841 local_using_directives = newobj->local_using_directives;
11843 /* If we've finished processing a top-level function, subsequent
11844 symbols go in the file symbol list. */
11845 if (outermost_context_p ())
11846 cu->list_in_scope = &file_symbols;
11849 /* Process all the DIES contained within a lexical block scope. Start
11850 a new scope, process the dies, and then close the scope. */
11853 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11855 struct objfile *objfile = cu->objfile;
11856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11857 struct context_stack *newobj;
11858 CORE_ADDR lowpc, highpc;
11859 struct die_info *child_die;
11860 CORE_ADDR baseaddr;
11862 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11864 /* Ignore blocks with missing or invalid low and high pc attributes. */
11865 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11866 as multiple lexical blocks? Handling children in a sane way would
11867 be nasty. Might be easier to properly extend generic blocks to
11868 describe ranges. */
11869 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11871 case PC_BOUNDS_NOT_PRESENT:
11872 /* DW_TAG_lexical_block has no attributes, process its children as if
11873 there was no wrapping by that DW_TAG_lexical_block.
11874 GCC does no longer produces such DWARF since GCC r224161. */
11875 for (child_die = die->child;
11876 child_die != NULL && child_die->tag;
11877 child_die = sibling_die (child_die))
11878 process_die (child_die, cu);
11880 case PC_BOUNDS_INVALID:
11883 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11884 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11886 push_context (0, lowpc);
11887 if (die->child != NULL)
11889 child_die = die->child;
11890 while (child_die && child_die->tag)
11892 process_die (child_die, cu);
11893 child_die = sibling_die (child_die);
11896 inherit_abstract_dies (die, cu);
11897 newobj = pop_context ();
11899 if (local_symbols != NULL || local_using_directives != NULL)
11901 struct block *block
11902 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11903 newobj->start_addr, highpc);
11905 /* Note that recording ranges after traversing children, as we
11906 do here, means that recording a parent's ranges entails
11907 walking across all its children's ranges as they appear in
11908 the address map, which is quadratic behavior.
11910 It would be nicer to record the parent's ranges before
11911 traversing its children, simply overriding whatever you find
11912 there. But since we don't even decide whether to create a
11913 block until after we've traversed its children, that's hard
11915 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11917 local_symbols = newobj->locals;
11918 local_using_directives = newobj->local_using_directives;
11921 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11924 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11926 struct objfile *objfile = cu->objfile;
11927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11928 CORE_ADDR pc, baseaddr;
11929 struct attribute *attr;
11930 struct call_site *call_site, call_site_local;
11933 struct die_info *child_die;
11935 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11937 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11940 /* This was a pre-DWARF-5 GNU extension alias
11941 for DW_AT_call_return_pc. */
11942 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11946 complaint (&symfile_complaints,
11947 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11948 "DIE 0x%x [in module %s]"),
11949 to_underlying (die->sect_off), objfile_name (objfile));
11952 pc = attr_value_as_address (attr) + baseaddr;
11953 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11955 if (cu->call_site_htab == NULL)
11956 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11957 NULL, &objfile->objfile_obstack,
11958 hashtab_obstack_allocate, NULL);
11959 call_site_local.pc = pc;
11960 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11963 complaint (&symfile_complaints,
11964 _("Duplicate PC %s for DW_TAG_call_site "
11965 "DIE 0x%x [in module %s]"),
11966 paddress (gdbarch, pc), to_underlying (die->sect_off),
11967 objfile_name (objfile));
11971 /* Count parameters at the caller. */
11974 for (child_die = die->child; child_die && child_die->tag;
11975 child_die = sibling_die (child_die))
11977 if (child_die->tag != DW_TAG_call_site_parameter
11978 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11980 complaint (&symfile_complaints,
11981 _("Tag %d is not DW_TAG_call_site_parameter in "
11982 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11983 child_die->tag, to_underlying (child_die->sect_off),
11984 objfile_name (objfile));
11992 = ((struct call_site *)
11993 obstack_alloc (&objfile->objfile_obstack,
11994 sizeof (*call_site)
11995 + (sizeof (*call_site->parameter) * (nparams - 1))));
11997 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11998 call_site->pc = pc;
12000 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12001 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12003 struct die_info *func_die;
12005 /* Skip also over DW_TAG_inlined_subroutine. */
12006 for (func_die = die->parent;
12007 func_die && func_die->tag != DW_TAG_subprogram
12008 && func_die->tag != DW_TAG_subroutine_type;
12009 func_die = func_die->parent);
12011 /* DW_AT_call_all_calls is a superset
12012 of DW_AT_call_all_tail_calls. */
12014 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12015 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12016 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12017 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12019 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12020 not complete. But keep CALL_SITE for look ups via call_site_htab,
12021 both the initial caller containing the real return address PC and
12022 the final callee containing the current PC of a chain of tail
12023 calls do not need to have the tail call list complete. But any
12024 function candidate for a virtual tail call frame searched via
12025 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12026 determined unambiguously. */
12030 struct type *func_type = NULL;
12033 func_type = get_die_type (func_die, cu);
12034 if (func_type != NULL)
12036 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
12038 /* Enlist this call site to the function. */
12039 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12040 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12043 complaint (&symfile_complaints,
12044 _("Cannot find function owning DW_TAG_call_site "
12045 "DIE 0x%x [in module %s]"),
12046 to_underlying (die->sect_off), objfile_name (objfile));
12050 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12052 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12054 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12057 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12058 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12060 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
12061 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
12062 /* Keep NULL DWARF_BLOCK. */;
12063 else if (attr_form_is_block (attr))
12065 struct dwarf2_locexpr_baton *dlbaton;
12067 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12068 dlbaton->data = DW_BLOCK (attr)->data;
12069 dlbaton->size = DW_BLOCK (attr)->size;
12070 dlbaton->per_cu = cu->per_cu;
12072 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
12074 else if (attr_form_is_ref (attr))
12076 struct dwarf2_cu *target_cu = cu;
12077 struct die_info *target_die;
12079 target_die = follow_die_ref (die, attr, &target_cu);
12080 gdb_assert (target_cu->objfile == objfile);
12081 if (die_is_declaration (target_die, target_cu))
12083 const char *target_physname;
12085 /* Prefer the mangled name; otherwise compute the demangled one. */
12086 target_physname = dw2_linkage_name (target_die, target_cu);
12087 if (target_physname == NULL)
12088 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12089 if (target_physname == NULL)
12090 complaint (&symfile_complaints,
12091 _("DW_AT_call_target target DIE has invalid "
12092 "physname, for referencing DIE 0x%x [in module %s]"),
12093 to_underlying (die->sect_off), objfile_name (objfile));
12095 SET_FIELD_PHYSNAME (call_site->target, target_physname);
12101 /* DW_AT_entry_pc should be preferred. */
12102 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
12103 <= PC_BOUNDS_INVALID)
12104 complaint (&symfile_complaints,
12105 _("DW_AT_call_target target DIE has invalid "
12106 "low pc, for referencing DIE 0x%x [in module %s]"),
12107 to_underlying (die->sect_off), objfile_name (objfile));
12110 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12111 SET_FIELD_PHYSADDR (call_site->target, lowpc);
12116 complaint (&symfile_complaints,
12117 _("DW_TAG_call_site DW_AT_call_target is neither "
12118 "block nor reference, for DIE 0x%x [in module %s]"),
12119 to_underlying (die->sect_off), objfile_name (objfile));
12121 call_site->per_cu = cu->per_cu;
12123 for (child_die = die->child;
12124 child_die && child_die->tag;
12125 child_die = sibling_die (child_die))
12127 struct call_site_parameter *parameter;
12128 struct attribute *loc, *origin;
12130 if (child_die->tag != DW_TAG_call_site_parameter
12131 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12133 /* Already printed the complaint above. */
12137 gdb_assert (call_site->parameter_count < nparams);
12138 parameter = &call_site->parameter[call_site->parameter_count];
12140 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12141 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12142 register is contained in DW_AT_call_value. */
12144 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12145 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12146 if (origin == NULL)
12148 /* This was a pre-DWARF-5 GNU extension alias
12149 for DW_AT_call_parameter. */
12150 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12152 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
12154 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12156 sect_offset sect_off
12157 = (sect_offset) dwarf2_get_ref_die_offset (origin);
12158 if (!offset_in_cu_p (&cu->header, sect_off))
12160 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12161 binding can be done only inside one CU. Such referenced DIE
12162 therefore cannot be even moved to DW_TAG_partial_unit. */
12163 complaint (&symfile_complaints,
12164 _("DW_AT_call_parameter offset is not in CU for "
12165 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12166 to_underlying (child_die->sect_off),
12167 objfile_name (objfile));
12170 parameter->u.param_cu_off
12171 = (cu_offset) (sect_off - cu->header.sect_off);
12173 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
12175 complaint (&symfile_complaints,
12176 _("No DW_FORM_block* DW_AT_location for "
12177 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12178 to_underlying (child_die->sect_off), objfile_name (objfile));
12183 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12184 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12185 if (parameter->u.dwarf_reg != -1)
12186 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12187 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12188 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12189 ¶meter->u.fb_offset))
12190 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12193 complaint (&symfile_complaints,
12194 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12195 "for DW_FORM_block* DW_AT_location is supported for "
12196 "DW_TAG_call_site child DIE 0x%x "
12198 to_underlying (child_die->sect_off),
12199 objfile_name (objfile));
12204 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12206 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12207 if (!attr_form_is_block (attr))
12209 complaint (&symfile_complaints,
12210 _("No DW_FORM_block* DW_AT_call_value for "
12211 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12212 to_underlying (child_die->sect_off),
12213 objfile_name (objfile));
12216 parameter->value = DW_BLOCK (attr)->data;
12217 parameter->value_size = DW_BLOCK (attr)->size;
12219 /* Parameters are not pre-cleared by memset above. */
12220 parameter->data_value = NULL;
12221 parameter->data_value_size = 0;
12222 call_site->parameter_count++;
12224 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12226 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12229 if (!attr_form_is_block (attr))
12230 complaint (&symfile_complaints,
12231 _("No DW_FORM_block* DW_AT_call_data_value for "
12232 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12233 to_underlying (child_die->sect_off),
12234 objfile_name (objfile));
12237 parameter->data_value = DW_BLOCK (attr)->data;
12238 parameter->data_value_size = DW_BLOCK (attr)->size;
12244 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12245 reading .debug_rnglists.
12246 Callback's type should be:
12247 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12248 Return true if the attributes are present and valid, otherwise,
12251 template <typename Callback>
12253 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12254 Callback &&callback)
12256 struct objfile *objfile = cu->objfile;
12257 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12258 struct comp_unit_head *cu_header = &cu->header;
12259 bfd *obfd = objfile->obfd;
12260 unsigned int addr_size = cu_header->addr_size;
12261 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12262 /* Base address selection entry. */
12265 unsigned int dummy;
12266 const gdb_byte *buffer;
12268 CORE_ADDR high = 0;
12269 CORE_ADDR baseaddr;
12270 bool overflow = false;
12272 found_base = cu->base_known;
12273 base = cu->base_address;
12275 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12276 if (offset >= dwarf2_per_objfile->rnglists.size)
12278 complaint (&symfile_complaints,
12279 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12283 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12285 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12289 /* Initialize it due to a false compiler warning. */
12290 CORE_ADDR range_beginning = 0, range_end = 0;
12291 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12292 + dwarf2_per_objfile->rnglists.size);
12293 unsigned int bytes_read;
12295 if (buffer == buf_end)
12300 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12303 case DW_RLE_end_of_list:
12305 case DW_RLE_base_address:
12306 if (buffer + cu->header.addr_size > buf_end)
12311 base = read_address (obfd, buffer, cu, &bytes_read);
12313 buffer += bytes_read;
12315 case DW_RLE_start_length:
12316 if (buffer + cu->header.addr_size > buf_end)
12321 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12322 buffer += bytes_read;
12323 range_end = (range_beginning
12324 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12325 buffer += bytes_read;
12326 if (buffer > buf_end)
12332 case DW_RLE_offset_pair:
12333 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12334 buffer += bytes_read;
12335 if (buffer > buf_end)
12340 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12341 buffer += bytes_read;
12342 if (buffer > buf_end)
12348 case DW_RLE_start_end:
12349 if (buffer + 2 * cu->header.addr_size > buf_end)
12354 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12355 buffer += bytes_read;
12356 range_end = read_address (obfd, buffer, cu, &bytes_read);
12357 buffer += bytes_read;
12360 complaint (&symfile_complaints,
12361 _("Invalid .debug_rnglists data (no base address)"));
12364 if (rlet == DW_RLE_end_of_list || overflow)
12366 if (rlet == DW_RLE_base_address)
12371 /* We have no valid base address for the ranges
12373 complaint (&symfile_complaints,
12374 _("Invalid .debug_rnglists data (no base address)"));
12378 if (range_beginning > range_end)
12380 /* Inverted range entries are invalid. */
12381 complaint (&symfile_complaints,
12382 _("Invalid .debug_rnglists data (inverted range)"));
12386 /* Empty range entries have no effect. */
12387 if (range_beginning == range_end)
12390 range_beginning += base;
12393 /* A not-uncommon case of bad debug info.
12394 Don't pollute the addrmap with bad data. */
12395 if (range_beginning + baseaddr == 0
12396 && !dwarf2_per_objfile->has_section_at_zero)
12398 complaint (&symfile_complaints,
12399 _(".debug_rnglists entry has start address of zero"
12400 " [in module %s]"), objfile_name (objfile));
12404 callback (range_beginning, range_end);
12409 complaint (&symfile_complaints,
12410 _("Offset %d is not terminated "
12411 "for DW_AT_ranges attribute"),
12419 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12420 Callback's type should be:
12421 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12422 Return 1 if the attributes are present and valid, otherwise, return 0. */
12424 template <typename Callback>
12426 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12427 Callback &&callback)
12429 struct objfile *objfile = cu->objfile;
12430 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12431 struct comp_unit_head *cu_header = &cu->header;
12432 bfd *obfd = objfile->obfd;
12433 unsigned int addr_size = cu_header->addr_size;
12434 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12435 /* Base address selection entry. */
12438 unsigned int dummy;
12439 const gdb_byte *buffer;
12440 CORE_ADDR baseaddr;
12442 if (cu_header->version >= 5)
12443 return dwarf2_rnglists_process (offset, cu, callback);
12445 found_base = cu->base_known;
12446 base = cu->base_address;
12448 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12449 if (offset >= dwarf2_per_objfile->ranges.size)
12451 complaint (&symfile_complaints,
12452 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12456 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12458 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12462 CORE_ADDR range_beginning, range_end;
12464 range_beginning = read_address (obfd, buffer, cu, &dummy);
12465 buffer += addr_size;
12466 range_end = read_address (obfd, buffer, cu, &dummy);
12467 buffer += addr_size;
12468 offset += 2 * addr_size;
12470 /* An end of list marker is a pair of zero addresses. */
12471 if (range_beginning == 0 && range_end == 0)
12472 /* Found the end of list entry. */
12475 /* Each base address selection entry is a pair of 2 values.
12476 The first is the largest possible address, the second is
12477 the base address. Check for a base address here. */
12478 if ((range_beginning & mask) == mask)
12480 /* If we found the largest possible address, then we already
12481 have the base address in range_end. */
12489 /* We have no valid base address for the ranges
12491 complaint (&symfile_complaints,
12492 _("Invalid .debug_ranges data (no base address)"));
12496 if (range_beginning > range_end)
12498 /* Inverted range entries are invalid. */
12499 complaint (&symfile_complaints,
12500 _("Invalid .debug_ranges data (inverted range)"));
12504 /* Empty range entries have no effect. */
12505 if (range_beginning == range_end)
12508 range_beginning += base;
12511 /* A not-uncommon case of bad debug info.
12512 Don't pollute the addrmap with bad data. */
12513 if (range_beginning + baseaddr == 0
12514 && !dwarf2_per_objfile->has_section_at_zero)
12516 complaint (&symfile_complaints,
12517 _(".debug_ranges entry has start address of zero"
12518 " [in module %s]"), objfile_name (objfile));
12522 callback (range_beginning, range_end);
12528 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12529 Return 1 if the attributes are present and valid, otherwise, return 0.
12530 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12533 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12534 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12535 struct partial_symtab *ranges_pst)
12537 struct objfile *objfile = cu->objfile;
12538 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12539 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12540 SECT_OFF_TEXT (objfile));
12543 CORE_ADDR high = 0;
12546 retval = dwarf2_ranges_process (offset, cu,
12547 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12549 if (ranges_pst != NULL)
12554 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12555 range_beginning + baseaddr);
12556 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12557 range_end + baseaddr);
12558 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12562 /* FIXME: This is recording everything as a low-high
12563 segment of consecutive addresses. We should have a
12564 data structure for discontiguous block ranges
12568 low = range_beginning;
12574 if (range_beginning < low)
12575 low = range_beginning;
12576 if (range_end > high)
12584 /* If the first entry is an end-of-list marker, the range
12585 describes an empty scope, i.e. no instructions. */
12591 *high_return = high;
12595 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12596 definition for the return value. *LOWPC and *HIGHPC are set iff
12597 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12599 static enum pc_bounds_kind
12600 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12601 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12602 struct partial_symtab *pst)
12604 struct attribute *attr;
12605 struct attribute *attr_high;
12607 CORE_ADDR high = 0;
12608 enum pc_bounds_kind ret;
12610 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12613 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12616 low = attr_value_as_address (attr);
12617 high = attr_value_as_address (attr_high);
12618 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12622 /* Found high w/o low attribute. */
12623 return PC_BOUNDS_INVALID;
12625 /* Found consecutive range of addresses. */
12626 ret = PC_BOUNDS_HIGH_LOW;
12630 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12633 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12634 We take advantage of the fact that DW_AT_ranges does not appear
12635 in DW_TAG_compile_unit of DWO files. */
12636 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12637 unsigned int ranges_offset = (DW_UNSND (attr)
12638 + (need_ranges_base
12642 /* Value of the DW_AT_ranges attribute is the offset in the
12643 .debug_ranges section. */
12644 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12645 return PC_BOUNDS_INVALID;
12646 /* Found discontinuous range of addresses. */
12647 ret = PC_BOUNDS_RANGES;
12650 return PC_BOUNDS_NOT_PRESENT;
12653 /* read_partial_die has also the strict LOW < HIGH requirement. */
12655 return PC_BOUNDS_INVALID;
12657 /* When using the GNU linker, .gnu.linkonce. sections are used to
12658 eliminate duplicate copies of functions and vtables and such.
12659 The linker will arbitrarily choose one and discard the others.
12660 The AT_*_pc values for such functions refer to local labels in
12661 these sections. If the section from that file was discarded, the
12662 labels are not in the output, so the relocs get a value of 0.
12663 If this is a discarded function, mark the pc bounds as invalid,
12664 so that GDB will ignore it. */
12665 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12666 return PC_BOUNDS_INVALID;
12674 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12675 its low and high PC addresses. Do nothing if these addresses could not
12676 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12677 and HIGHPC to the high address if greater than HIGHPC. */
12680 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12681 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12682 struct dwarf2_cu *cu)
12684 CORE_ADDR low, high;
12685 struct die_info *child = die->child;
12687 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12689 *lowpc = std::min (*lowpc, low);
12690 *highpc = std::max (*highpc, high);
12693 /* If the language does not allow nested subprograms (either inside
12694 subprograms or lexical blocks), we're done. */
12695 if (cu->language != language_ada)
12698 /* Check all the children of the given DIE. If it contains nested
12699 subprograms, then check their pc bounds. Likewise, we need to
12700 check lexical blocks as well, as they may also contain subprogram
12702 while (child && child->tag)
12704 if (child->tag == DW_TAG_subprogram
12705 || child->tag == DW_TAG_lexical_block)
12706 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12707 child = sibling_die (child);
12711 /* Get the low and high pc's represented by the scope DIE, and store
12712 them in *LOWPC and *HIGHPC. If the correct values can't be
12713 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12716 get_scope_pc_bounds (struct die_info *die,
12717 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12718 struct dwarf2_cu *cu)
12720 CORE_ADDR best_low = (CORE_ADDR) -1;
12721 CORE_ADDR best_high = (CORE_ADDR) 0;
12722 CORE_ADDR current_low, current_high;
12724 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
12725 >= PC_BOUNDS_RANGES)
12727 best_low = current_low;
12728 best_high = current_high;
12732 struct die_info *child = die->child;
12734 while (child && child->tag)
12736 switch (child->tag) {
12737 case DW_TAG_subprogram:
12738 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12740 case DW_TAG_namespace:
12741 case DW_TAG_module:
12742 /* FIXME: carlton/2004-01-16: Should we do this for
12743 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12744 that current GCC's always emit the DIEs corresponding
12745 to definitions of methods of classes as children of a
12746 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12747 the DIEs giving the declarations, which could be
12748 anywhere). But I don't see any reason why the
12749 standards says that they have to be there. */
12750 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
12752 if (current_low != ((CORE_ADDR) -1))
12754 best_low = std::min (best_low, current_low);
12755 best_high = std::max (best_high, current_high);
12763 child = sibling_die (child);
12768 *highpc = best_high;
12771 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12775 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12776 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12778 struct objfile *objfile = cu->objfile;
12779 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12780 struct attribute *attr;
12781 struct attribute *attr_high;
12783 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12786 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12789 CORE_ADDR low = attr_value_as_address (attr);
12790 CORE_ADDR high = attr_value_as_address (attr_high);
12792 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12795 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12796 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12797 record_block_range (block, low, high - 1);
12801 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12804 bfd *obfd = objfile->obfd;
12805 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12806 We take advantage of the fact that DW_AT_ranges does not appear
12807 in DW_TAG_compile_unit of DWO files. */
12808 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12810 /* The value of the DW_AT_ranges attribute is the offset of the
12811 address range list in the .debug_ranges section. */
12812 unsigned long offset = (DW_UNSND (attr)
12813 + (need_ranges_base ? cu->ranges_base : 0));
12814 const gdb_byte *buffer;
12816 /* For some target architectures, but not others, the
12817 read_address function sign-extends the addresses it returns.
12818 To recognize base address selection entries, we need a
12820 unsigned int addr_size = cu->header.addr_size;
12821 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12823 /* The base address, to which the next pair is relative. Note
12824 that this 'base' is a DWARF concept: most entries in a range
12825 list are relative, to reduce the number of relocs against the
12826 debugging information. This is separate from this function's
12827 'baseaddr' argument, which GDB uses to relocate debugging
12828 information from a shared library based on the address at
12829 which the library was loaded. */
12830 CORE_ADDR base = cu->base_address;
12831 int base_known = cu->base_known;
12833 dwarf2_ranges_process (offset, cu,
12834 [&] (CORE_ADDR start, CORE_ADDR end)
12838 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12839 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12840 record_block_range (block, start, end - 1);
12845 /* Check whether the producer field indicates either of GCC < 4.6, or the
12846 Intel C/C++ compiler, and cache the result in CU. */
12849 check_producer (struct dwarf2_cu *cu)
12853 if (cu->producer == NULL)
12855 /* For unknown compilers expect their behavior is DWARF version
12858 GCC started to support .debug_types sections by -gdwarf-4 since
12859 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12860 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12861 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12862 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12864 else if (producer_is_gcc (cu->producer, &major, &minor))
12866 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12867 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12869 else if (producer_is_icc (cu->producer, &major, &minor))
12870 cu->producer_is_icc_lt_14 = major < 14;
12873 /* For other non-GCC compilers, expect their behavior is DWARF version
12877 cu->checked_producer = 1;
12880 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12881 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12882 during 4.6.0 experimental. */
12885 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12887 if (!cu->checked_producer)
12888 check_producer (cu);
12890 return cu->producer_is_gxx_lt_4_6;
12893 /* Return the default accessibility type if it is not overriden by
12894 DW_AT_accessibility. */
12896 static enum dwarf_access_attribute
12897 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12899 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12901 /* The default DWARF 2 accessibility for members is public, the default
12902 accessibility for inheritance is private. */
12904 if (die->tag != DW_TAG_inheritance)
12905 return DW_ACCESS_public;
12907 return DW_ACCESS_private;
12911 /* DWARF 3+ defines the default accessibility a different way. The same
12912 rules apply now for DW_TAG_inheritance as for the members and it only
12913 depends on the container kind. */
12915 if (die->parent->tag == DW_TAG_class_type)
12916 return DW_ACCESS_private;
12918 return DW_ACCESS_public;
12922 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12923 offset. If the attribute was not found return 0, otherwise return
12924 1. If it was found but could not properly be handled, set *OFFSET
12928 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12931 struct attribute *attr;
12933 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12938 /* Note that we do not check for a section offset first here.
12939 This is because DW_AT_data_member_location is new in DWARF 4,
12940 so if we see it, we can assume that a constant form is really
12941 a constant and not a section offset. */
12942 if (attr_form_is_constant (attr))
12943 *offset = dwarf2_get_attr_constant_value (attr, 0);
12944 else if (attr_form_is_section_offset (attr))
12945 dwarf2_complex_location_expr_complaint ();
12946 else if (attr_form_is_block (attr))
12947 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12949 dwarf2_complex_location_expr_complaint ();
12957 /* Add an aggregate field to the field list. */
12960 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12961 struct dwarf2_cu *cu)
12963 struct objfile *objfile = cu->objfile;
12964 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12965 struct nextfield *new_field;
12966 struct attribute *attr;
12968 const char *fieldname = "";
12970 /* Allocate a new field list entry and link it in. */
12971 new_field = XNEW (struct nextfield);
12972 make_cleanup (xfree, new_field);
12973 memset (new_field, 0, sizeof (struct nextfield));
12975 if (die->tag == DW_TAG_inheritance)
12977 new_field->next = fip->baseclasses;
12978 fip->baseclasses = new_field;
12982 new_field->next = fip->fields;
12983 fip->fields = new_field;
12987 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12989 new_field->accessibility = DW_UNSND (attr);
12991 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12992 if (new_field->accessibility != DW_ACCESS_public)
12993 fip->non_public_fields = 1;
12995 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12997 new_field->virtuality = DW_UNSND (attr);
12999 new_field->virtuality = DW_VIRTUALITY_none;
13001 fp = &new_field->field;
13003 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
13007 /* Data member other than a C++ static data member. */
13009 /* Get type of field. */
13010 fp->type = die_type (die, cu);
13012 SET_FIELD_BITPOS (*fp, 0);
13014 /* Get bit size of field (zero if none). */
13015 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13018 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
13022 FIELD_BITSIZE (*fp) = 0;
13025 /* Get bit offset of field. */
13026 if (handle_data_member_location (die, cu, &offset))
13027 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13028 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13031 if (gdbarch_bits_big_endian (gdbarch))
13033 /* For big endian bits, the DW_AT_bit_offset gives the
13034 additional bit offset from the MSB of the containing
13035 anonymous object to the MSB of the field. We don't
13036 have to do anything special since we don't need to
13037 know the size of the anonymous object. */
13038 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
13042 /* For little endian bits, compute the bit offset to the
13043 MSB of the anonymous object, subtract off the number of
13044 bits from the MSB of the field to the MSB of the
13045 object, and then subtract off the number of bits of
13046 the field itself. The result is the bit offset of
13047 the LSB of the field. */
13048 int anonymous_size;
13049 int bit_offset = DW_UNSND (attr);
13051 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13054 /* The size of the anonymous object containing
13055 the bit field is explicit, so use the
13056 indicated size (in bytes). */
13057 anonymous_size = DW_UNSND (attr);
13061 /* The size of the anonymous object containing
13062 the bit field must be inferred from the type
13063 attribute of the data member containing the
13065 anonymous_size = TYPE_LENGTH (fp->type);
13067 SET_FIELD_BITPOS (*fp,
13068 (FIELD_BITPOS (*fp)
13069 + anonymous_size * bits_per_byte
13070 - bit_offset - FIELD_BITSIZE (*fp)));
13073 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13075 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
13076 + dwarf2_get_attr_constant_value (attr, 0)));
13078 /* Get name of field. */
13079 fieldname = dwarf2_name (die, cu);
13080 if (fieldname == NULL)
13083 /* The name is already allocated along with this objfile, so we don't
13084 need to duplicate it for the type. */
13085 fp->name = fieldname;
13087 /* Change accessibility for artificial fields (e.g. virtual table
13088 pointer or virtual base class pointer) to private. */
13089 if (dwarf2_attr (die, DW_AT_artificial, cu))
13091 FIELD_ARTIFICIAL (*fp) = 1;
13092 new_field->accessibility = DW_ACCESS_private;
13093 fip->non_public_fields = 1;
13096 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13098 /* C++ static member. */
13100 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13101 is a declaration, but all versions of G++ as of this writing
13102 (so through at least 3.2.1) incorrectly generate
13103 DW_TAG_variable tags. */
13105 const char *physname;
13107 /* Get name of field. */
13108 fieldname = dwarf2_name (die, cu);
13109 if (fieldname == NULL)
13112 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13114 /* Only create a symbol if this is an external value.
13115 new_symbol checks this and puts the value in the global symbol
13116 table, which we want. If it is not external, new_symbol
13117 will try to put the value in cu->list_in_scope which is wrong. */
13118 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13120 /* A static const member, not much different than an enum as far as
13121 we're concerned, except that we can support more types. */
13122 new_symbol (die, NULL, cu);
13125 /* Get physical name. */
13126 physname = dwarf2_physname (fieldname, die, cu);
13128 /* The name is already allocated along with this objfile, so we don't
13129 need to duplicate it for the type. */
13130 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
13131 FIELD_TYPE (*fp) = die_type (die, cu);
13132 FIELD_NAME (*fp) = fieldname;
13134 else if (die->tag == DW_TAG_inheritance)
13138 /* C++ base class field. */
13139 if (handle_data_member_location (die, cu, &offset))
13140 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
13141 FIELD_BITSIZE (*fp) = 0;
13142 FIELD_TYPE (*fp) = die_type (die, cu);
13143 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
13144 fip->nbaseclasses++;
13148 /* Add a typedef defined in the scope of the FIP's class. */
13151 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
13152 struct dwarf2_cu *cu)
13154 struct typedef_field_list *new_field;
13155 struct typedef_field *fp;
13157 /* Allocate a new field list entry and link it in. */
13158 new_field = XCNEW (struct typedef_field_list);
13159 make_cleanup (xfree, new_field);
13161 gdb_assert (die->tag == DW_TAG_typedef);
13163 fp = &new_field->field;
13165 /* Get name of field. */
13166 fp->name = dwarf2_name (die, cu);
13167 if (fp->name == NULL)
13170 fp->type = read_type_die (die, cu);
13172 new_field->next = fip->typedef_field_list;
13173 fip->typedef_field_list = new_field;
13174 fip->typedef_field_list_count++;
13177 /* Create the vector of fields, and attach it to the type. */
13180 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13181 struct dwarf2_cu *cu)
13183 int nfields = fip->nfields;
13185 /* Record the field count, allocate space for the array of fields,
13186 and create blank accessibility bitfields if necessary. */
13187 TYPE_NFIELDS (type) = nfields;
13188 TYPE_FIELDS (type) = (struct field *)
13189 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13190 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13192 if (fip->non_public_fields && cu->language != language_ada)
13194 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13196 TYPE_FIELD_PRIVATE_BITS (type) =
13197 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13198 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13200 TYPE_FIELD_PROTECTED_BITS (type) =
13201 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13202 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13204 TYPE_FIELD_IGNORE_BITS (type) =
13205 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13206 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13209 /* If the type has baseclasses, allocate and clear a bit vector for
13210 TYPE_FIELD_VIRTUAL_BITS. */
13211 if (fip->nbaseclasses && cu->language != language_ada)
13213 int num_bytes = B_BYTES (fip->nbaseclasses);
13214 unsigned char *pointer;
13216 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13217 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13218 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13219 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13220 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13223 /* Copy the saved-up fields into the field vector. Start from the head of
13224 the list, adding to the tail of the field array, so that they end up in
13225 the same order in the array in which they were added to the list. */
13226 while (nfields-- > 0)
13228 struct nextfield *fieldp;
13232 fieldp = fip->fields;
13233 fip->fields = fieldp->next;
13237 fieldp = fip->baseclasses;
13238 fip->baseclasses = fieldp->next;
13241 TYPE_FIELD (type, nfields) = fieldp->field;
13242 switch (fieldp->accessibility)
13244 case DW_ACCESS_private:
13245 if (cu->language != language_ada)
13246 SET_TYPE_FIELD_PRIVATE (type, nfields);
13249 case DW_ACCESS_protected:
13250 if (cu->language != language_ada)
13251 SET_TYPE_FIELD_PROTECTED (type, nfields);
13254 case DW_ACCESS_public:
13258 /* Unknown accessibility. Complain and treat it as public. */
13260 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13261 fieldp->accessibility);
13265 if (nfields < fip->nbaseclasses)
13267 switch (fieldp->virtuality)
13269 case DW_VIRTUALITY_virtual:
13270 case DW_VIRTUALITY_pure_virtual:
13271 if (cu->language == language_ada)
13272 error (_("unexpected virtuality in component of Ada type"));
13273 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13280 /* Return true if this member function is a constructor, false
13284 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13286 const char *fieldname;
13287 const char *type_name;
13290 if (die->parent == NULL)
13293 if (die->parent->tag != DW_TAG_structure_type
13294 && die->parent->tag != DW_TAG_union_type
13295 && die->parent->tag != DW_TAG_class_type)
13298 fieldname = dwarf2_name (die, cu);
13299 type_name = dwarf2_name (die->parent, cu);
13300 if (fieldname == NULL || type_name == NULL)
13303 len = strlen (fieldname);
13304 return (strncmp (fieldname, type_name, len) == 0
13305 && (type_name[len] == '\0' || type_name[len] == '<'));
13308 /* Add a member function to the proper fieldlist. */
13311 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13312 struct type *type, struct dwarf2_cu *cu)
13314 struct objfile *objfile = cu->objfile;
13315 struct attribute *attr;
13316 struct fnfieldlist *flp;
13318 struct fn_field *fnp;
13319 const char *fieldname;
13320 struct nextfnfield *new_fnfield;
13321 struct type *this_type;
13322 enum dwarf_access_attribute accessibility;
13324 if (cu->language == language_ada)
13325 error (_("unexpected member function in Ada type"));
13327 /* Get name of member function. */
13328 fieldname = dwarf2_name (die, cu);
13329 if (fieldname == NULL)
13332 /* Look up member function name in fieldlist. */
13333 for (i = 0; i < fip->nfnfields; i++)
13335 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13339 /* Create new list element if necessary. */
13340 if (i < fip->nfnfields)
13341 flp = &fip->fnfieldlists[i];
13344 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13346 fip->fnfieldlists = (struct fnfieldlist *)
13347 xrealloc (fip->fnfieldlists,
13348 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13349 * sizeof (struct fnfieldlist));
13350 if (fip->nfnfields == 0)
13351 make_cleanup (free_current_contents, &fip->fnfieldlists);
13353 flp = &fip->fnfieldlists[fip->nfnfields];
13354 flp->name = fieldname;
13357 i = fip->nfnfields++;
13360 /* Create a new member function field and chain it to the field list
13362 new_fnfield = XNEW (struct nextfnfield);
13363 make_cleanup (xfree, new_fnfield);
13364 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13365 new_fnfield->next = flp->head;
13366 flp->head = new_fnfield;
13369 /* Fill in the member function field info. */
13370 fnp = &new_fnfield->fnfield;
13372 /* Delay processing of the physname until later. */
13373 if (cu->language == language_cplus)
13375 add_to_method_list (type, i, flp->length - 1, fieldname,
13380 const char *physname = dwarf2_physname (fieldname, die, cu);
13381 fnp->physname = physname ? physname : "";
13384 fnp->type = alloc_type (objfile);
13385 this_type = read_type_die (die, cu);
13386 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13388 int nparams = TYPE_NFIELDS (this_type);
13390 /* TYPE is the domain of this method, and THIS_TYPE is the type
13391 of the method itself (TYPE_CODE_METHOD). */
13392 smash_to_method_type (fnp->type, type,
13393 TYPE_TARGET_TYPE (this_type),
13394 TYPE_FIELDS (this_type),
13395 TYPE_NFIELDS (this_type),
13396 TYPE_VARARGS (this_type));
13398 /* Handle static member functions.
13399 Dwarf2 has no clean way to discern C++ static and non-static
13400 member functions. G++ helps GDB by marking the first
13401 parameter for non-static member functions (which is the this
13402 pointer) as artificial. We obtain this information from
13403 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13404 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13405 fnp->voffset = VOFFSET_STATIC;
13408 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13409 dwarf2_full_name (fieldname, die, cu));
13411 /* Get fcontext from DW_AT_containing_type if present. */
13412 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13413 fnp->fcontext = die_containing_type (die, cu);
13415 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13416 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13418 /* Get accessibility. */
13419 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13421 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13423 accessibility = dwarf2_default_access_attribute (die, cu);
13424 switch (accessibility)
13426 case DW_ACCESS_private:
13427 fnp->is_private = 1;
13429 case DW_ACCESS_protected:
13430 fnp->is_protected = 1;
13434 /* Check for artificial methods. */
13435 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13436 if (attr && DW_UNSND (attr) != 0)
13437 fnp->is_artificial = 1;
13439 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13441 /* Get index in virtual function table if it is a virtual member
13442 function. For older versions of GCC, this is an offset in the
13443 appropriate virtual table, as specified by DW_AT_containing_type.
13444 For everyone else, it is an expression to be evaluated relative
13445 to the object address. */
13447 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13450 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13452 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13454 /* Old-style GCC. */
13455 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13457 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13458 || (DW_BLOCK (attr)->size > 1
13459 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13460 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13462 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13463 if ((fnp->voffset % cu->header.addr_size) != 0)
13464 dwarf2_complex_location_expr_complaint ();
13466 fnp->voffset /= cu->header.addr_size;
13470 dwarf2_complex_location_expr_complaint ();
13472 if (!fnp->fcontext)
13474 /* If there is no `this' field and no DW_AT_containing_type,
13475 we cannot actually find a base class context for the
13477 if (TYPE_NFIELDS (this_type) == 0
13478 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13480 complaint (&symfile_complaints,
13481 _("cannot determine context for virtual member "
13482 "function \"%s\" (offset %d)"),
13483 fieldname, to_underlying (die->sect_off));
13488 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13492 else if (attr_form_is_section_offset (attr))
13494 dwarf2_complex_location_expr_complaint ();
13498 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13504 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13505 if (attr && DW_UNSND (attr))
13507 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13508 complaint (&symfile_complaints,
13509 _("Member function \"%s\" (offset %d) is virtual "
13510 "but the vtable offset is not specified"),
13511 fieldname, to_underlying (die->sect_off));
13512 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13513 TYPE_CPLUS_DYNAMIC (type) = 1;
13518 /* Create the vector of member function fields, and attach it to the type. */
13521 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13522 struct dwarf2_cu *cu)
13524 struct fnfieldlist *flp;
13527 if (cu->language == language_ada)
13528 error (_("unexpected member functions in Ada type"));
13530 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13531 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13532 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13534 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13536 struct nextfnfield *nfp = flp->head;
13537 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13540 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13541 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13542 fn_flp->fn_fields = (struct fn_field *)
13543 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13544 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13545 fn_flp->fn_fields[k] = nfp->fnfield;
13548 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13551 /* Returns non-zero if NAME is the name of a vtable member in CU's
13552 language, zero otherwise. */
13554 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13556 static const char vptr[] = "_vptr";
13557 static const char vtable[] = "vtable";
13559 /* Look for the C++ form of the vtable. */
13560 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13566 /* GCC outputs unnamed structures that are really pointers to member
13567 functions, with the ABI-specified layout. If TYPE describes
13568 such a structure, smash it into a member function type.
13570 GCC shouldn't do this; it should just output pointer to member DIEs.
13571 This is GCC PR debug/28767. */
13574 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13576 struct type *pfn_type, *self_type, *new_type;
13578 /* Check for a structure with no name and two children. */
13579 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13582 /* Check for __pfn and __delta members. */
13583 if (TYPE_FIELD_NAME (type, 0) == NULL
13584 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13585 || TYPE_FIELD_NAME (type, 1) == NULL
13586 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13589 /* Find the type of the method. */
13590 pfn_type = TYPE_FIELD_TYPE (type, 0);
13591 if (pfn_type == NULL
13592 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13593 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13596 /* Look for the "this" argument. */
13597 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13598 if (TYPE_NFIELDS (pfn_type) == 0
13599 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13600 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13603 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13604 new_type = alloc_type (objfile);
13605 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13606 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13607 TYPE_VARARGS (pfn_type));
13608 smash_to_methodptr_type (type, new_type);
13612 /* Called when we find the DIE that starts a structure or union scope
13613 (definition) to create a type for the structure or union. Fill in
13614 the type's name and general properties; the members will not be
13615 processed until process_structure_scope. A symbol table entry for
13616 the type will also not be done until process_structure_scope (assuming
13617 the type has a name).
13619 NOTE: we need to call these functions regardless of whether or not the
13620 DIE has a DW_AT_name attribute, since it might be an anonymous
13621 structure or union. This gets the type entered into our set of
13622 user defined types. */
13624 static struct type *
13625 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13627 struct objfile *objfile = cu->objfile;
13629 struct attribute *attr;
13632 /* If the definition of this type lives in .debug_types, read that type.
13633 Don't follow DW_AT_specification though, that will take us back up
13634 the chain and we want to go down. */
13635 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13638 type = get_DW_AT_signature_type (die, attr, cu);
13640 /* The type's CU may not be the same as CU.
13641 Ensure TYPE is recorded with CU in die_type_hash. */
13642 return set_die_type (die, type, cu);
13645 type = alloc_type (objfile);
13646 INIT_CPLUS_SPECIFIC (type);
13648 name = dwarf2_name (die, cu);
13651 if (cu->language == language_cplus
13652 || cu->language == language_d
13653 || cu->language == language_rust)
13655 const char *full_name = dwarf2_full_name (name, die, cu);
13657 /* dwarf2_full_name might have already finished building the DIE's
13658 type. If so, there is no need to continue. */
13659 if (get_die_type (die, cu) != NULL)
13660 return get_die_type (die, cu);
13662 TYPE_TAG_NAME (type) = full_name;
13663 if (die->tag == DW_TAG_structure_type
13664 || die->tag == DW_TAG_class_type)
13665 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13669 /* The name is already allocated along with this objfile, so
13670 we don't need to duplicate it for the type. */
13671 TYPE_TAG_NAME (type) = name;
13672 if (die->tag == DW_TAG_class_type)
13673 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13677 if (die->tag == DW_TAG_structure_type)
13679 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13681 else if (die->tag == DW_TAG_union_type)
13683 TYPE_CODE (type) = TYPE_CODE_UNION;
13687 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13690 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13691 TYPE_DECLARED_CLASS (type) = 1;
13693 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13696 if (attr_form_is_constant (attr))
13697 TYPE_LENGTH (type) = DW_UNSND (attr);
13700 /* For the moment, dynamic type sizes are not supported
13701 by GDB's struct type. The actual size is determined
13702 on-demand when resolving the type of a given object,
13703 so set the type's length to zero for now. Otherwise,
13704 we record an expression as the length, and that expression
13705 could lead to a very large value, which could eventually
13706 lead to us trying to allocate that much memory when creating
13707 a value of that type. */
13708 TYPE_LENGTH (type) = 0;
13713 TYPE_LENGTH (type) = 0;
13716 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
13718 /* ICC<14 does not output the required DW_AT_declaration on
13719 incomplete types, but gives them a size of zero. */
13720 TYPE_STUB (type) = 1;
13723 TYPE_STUB_SUPPORTED (type) = 1;
13725 if (die_is_declaration (die, cu))
13726 TYPE_STUB (type) = 1;
13727 else if (attr == NULL && die->child == NULL
13728 && producer_is_realview (cu->producer))
13729 /* RealView does not output the required DW_AT_declaration
13730 on incomplete types. */
13731 TYPE_STUB (type) = 1;
13733 /* We need to add the type field to the die immediately so we don't
13734 infinitely recurse when dealing with pointers to the structure
13735 type within the structure itself. */
13736 set_die_type (die, type, cu);
13738 /* set_die_type should be already done. */
13739 set_descriptive_type (type, die, cu);
13744 /* Finish creating a structure or union type, including filling in
13745 its members and creating a symbol for it. */
13748 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13750 struct objfile *objfile = cu->objfile;
13751 struct die_info *child_die;
13754 type = get_die_type (die, cu);
13756 type = read_structure_type (die, cu);
13758 if (die->child != NULL && ! die_is_declaration (die, cu))
13760 struct field_info fi;
13761 VEC (symbolp) *template_args = NULL;
13762 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13764 memset (&fi, 0, sizeof (struct field_info));
13766 child_die = die->child;
13768 while (child_die && child_die->tag)
13770 if (child_die->tag == DW_TAG_member
13771 || child_die->tag == DW_TAG_variable)
13773 /* NOTE: carlton/2002-11-05: A C++ static data member
13774 should be a DW_TAG_member that is a declaration, but
13775 all versions of G++ as of this writing (so through at
13776 least 3.2.1) incorrectly generate DW_TAG_variable
13777 tags for them instead. */
13778 dwarf2_add_field (&fi, child_die, cu);
13780 else if (child_die->tag == DW_TAG_subprogram)
13782 /* Rust doesn't have member functions in the C++ sense.
13783 However, it does emit ordinary functions as children
13784 of a struct DIE. */
13785 if (cu->language == language_rust)
13786 read_func_scope (child_die, cu);
13789 /* C++ member function. */
13790 dwarf2_add_member_fn (&fi, child_die, type, cu);
13793 else if (child_die->tag == DW_TAG_inheritance)
13795 /* C++ base class field. */
13796 dwarf2_add_field (&fi, child_die, cu);
13798 else if (child_die->tag == DW_TAG_typedef)
13799 dwarf2_add_typedef (&fi, child_die, cu);
13800 else if (child_die->tag == DW_TAG_template_type_param
13801 || child_die->tag == DW_TAG_template_value_param)
13803 struct symbol *arg = new_symbol (child_die, NULL, cu);
13806 VEC_safe_push (symbolp, template_args, arg);
13809 child_die = sibling_die (child_die);
13812 /* Attach template arguments to type. */
13813 if (! VEC_empty (symbolp, template_args))
13815 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13816 TYPE_N_TEMPLATE_ARGUMENTS (type)
13817 = VEC_length (symbolp, template_args);
13818 TYPE_TEMPLATE_ARGUMENTS (type)
13819 = XOBNEWVEC (&objfile->objfile_obstack,
13821 TYPE_N_TEMPLATE_ARGUMENTS (type));
13822 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13823 VEC_address (symbolp, template_args),
13824 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13825 * sizeof (struct symbol *)));
13826 VEC_free (symbolp, template_args);
13829 /* Attach fields and member functions to the type. */
13831 dwarf2_attach_fields_to_type (&fi, type, cu);
13834 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13836 /* Get the type which refers to the base class (possibly this
13837 class itself) which contains the vtable pointer for the current
13838 class from the DW_AT_containing_type attribute. This use of
13839 DW_AT_containing_type is a GNU extension. */
13841 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13843 struct type *t = die_containing_type (die, cu);
13845 set_type_vptr_basetype (type, t);
13850 /* Our own class provides vtbl ptr. */
13851 for (i = TYPE_NFIELDS (t) - 1;
13852 i >= TYPE_N_BASECLASSES (t);
13855 const char *fieldname = TYPE_FIELD_NAME (t, i);
13857 if (is_vtable_name (fieldname, cu))
13859 set_type_vptr_fieldno (type, i);
13864 /* Complain if virtual function table field not found. */
13865 if (i < TYPE_N_BASECLASSES (t))
13866 complaint (&symfile_complaints,
13867 _("virtual function table pointer "
13868 "not found when defining class '%s'"),
13869 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13874 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13877 else if (cu->producer
13878 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13880 /* The IBM XLC compiler does not provide direct indication
13881 of the containing type, but the vtable pointer is
13882 always named __vfp. */
13886 for (i = TYPE_NFIELDS (type) - 1;
13887 i >= TYPE_N_BASECLASSES (type);
13890 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13892 set_type_vptr_fieldno (type, i);
13893 set_type_vptr_basetype (type, type);
13900 /* Copy fi.typedef_field_list linked list elements content into the
13901 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13902 if (fi.typedef_field_list)
13904 int i = fi.typedef_field_list_count;
13906 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13907 TYPE_TYPEDEF_FIELD_ARRAY (type)
13908 = ((struct typedef_field *)
13909 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13910 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13912 /* Reverse the list order to keep the debug info elements order. */
13915 struct typedef_field *dest, *src;
13917 dest = &TYPE_TYPEDEF_FIELD (type, i);
13918 src = &fi.typedef_field_list->field;
13919 fi.typedef_field_list = fi.typedef_field_list->next;
13924 do_cleanups (back_to);
13927 quirk_gcc_member_function_pointer (type, objfile);
13929 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13930 snapshots) has been known to create a die giving a declaration
13931 for a class that has, as a child, a die giving a definition for a
13932 nested class. So we have to process our children even if the
13933 current die is a declaration. Normally, of course, a declaration
13934 won't have any children at all. */
13936 child_die = die->child;
13938 while (child_die != NULL && child_die->tag)
13940 if (child_die->tag == DW_TAG_member
13941 || child_die->tag == DW_TAG_variable
13942 || child_die->tag == DW_TAG_inheritance
13943 || child_die->tag == DW_TAG_template_value_param
13944 || child_die->tag == DW_TAG_template_type_param)
13949 process_die (child_die, cu);
13951 child_die = sibling_die (child_die);
13954 /* Do not consider external references. According to the DWARF standard,
13955 these DIEs are identified by the fact that they have no byte_size
13956 attribute, and a declaration attribute. */
13957 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13958 || !die_is_declaration (die, cu))
13959 new_symbol (die, type, cu);
13962 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13963 update TYPE using some information only available in DIE's children. */
13966 update_enumeration_type_from_children (struct die_info *die,
13968 struct dwarf2_cu *cu)
13970 struct die_info *child_die;
13971 int unsigned_enum = 1;
13975 auto_obstack obstack;
13977 for (child_die = die->child;
13978 child_die != NULL && child_die->tag;
13979 child_die = sibling_die (child_die))
13981 struct attribute *attr;
13983 const gdb_byte *bytes;
13984 struct dwarf2_locexpr_baton *baton;
13987 if (child_die->tag != DW_TAG_enumerator)
13990 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13994 name = dwarf2_name (child_die, cu);
13996 name = "<anonymous enumerator>";
13998 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13999 &value, &bytes, &baton);
14005 else if ((mask & value) != 0)
14010 /* If we already know that the enum type is neither unsigned, nor
14011 a flag type, no need to look at the rest of the enumerates. */
14012 if (!unsigned_enum && !flag_enum)
14017 TYPE_UNSIGNED (type) = 1;
14019 TYPE_FLAG_ENUM (type) = 1;
14022 /* Given a DW_AT_enumeration_type die, set its type. We do not
14023 complete the type's fields yet, or create any symbols. */
14025 static struct type *
14026 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
14028 struct objfile *objfile = cu->objfile;
14030 struct attribute *attr;
14033 /* If the definition of this type lives in .debug_types, read that type.
14034 Don't follow DW_AT_specification though, that will take us back up
14035 the chain and we want to go down. */
14036 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14039 type = get_DW_AT_signature_type (die, attr, cu);
14041 /* The type's CU may not be the same as CU.
14042 Ensure TYPE is recorded with CU in die_type_hash. */
14043 return set_die_type (die, type, cu);
14046 type = alloc_type (objfile);
14048 TYPE_CODE (type) = TYPE_CODE_ENUM;
14049 name = dwarf2_full_name (NULL, die, cu);
14051 TYPE_TAG_NAME (type) = name;
14053 attr = dwarf2_attr (die, DW_AT_type, cu);
14056 struct type *underlying_type = die_type (die, cu);
14058 TYPE_TARGET_TYPE (type) = underlying_type;
14061 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14064 TYPE_LENGTH (type) = DW_UNSND (attr);
14068 TYPE_LENGTH (type) = 0;
14071 /* The enumeration DIE can be incomplete. In Ada, any type can be
14072 declared as private in the package spec, and then defined only
14073 inside the package body. Such types are known as Taft Amendment
14074 Types. When another package uses such a type, an incomplete DIE
14075 may be generated by the compiler. */
14076 if (die_is_declaration (die, cu))
14077 TYPE_STUB (type) = 1;
14079 /* Finish the creation of this type by using the enum's children.
14080 We must call this even when the underlying type has been provided
14081 so that we can determine if we're looking at a "flag" enum. */
14082 update_enumeration_type_from_children (die, type, cu);
14084 /* If this type has an underlying type that is not a stub, then we
14085 may use its attributes. We always use the "unsigned" attribute
14086 in this situation, because ordinarily we guess whether the type
14087 is unsigned -- but the guess can be wrong and the underlying type
14088 can tell us the reality. However, we defer to a local size
14089 attribute if one exists, because this lets the compiler override
14090 the underlying type if needed. */
14091 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
14093 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
14094 if (TYPE_LENGTH (type) == 0)
14095 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
14098 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
14100 return set_die_type (die, type, cu);
14103 /* Given a pointer to a die which begins an enumeration, process all
14104 the dies that define the members of the enumeration, and create the
14105 symbol for the enumeration type.
14107 NOTE: We reverse the order of the element list. */
14110 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
14112 struct type *this_type;
14114 this_type = get_die_type (die, cu);
14115 if (this_type == NULL)
14116 this_type = read_enumeration_type (die, cu);
14118 if (die->child != NULL)
14120 struct die_info *child_die;
14121 struct symbol *sym;
14122 struct field *fields = NULL;
14123 int num_fields = 0;
14126 child_die = die->child;
14127 while (child_die && child_die->tag)
14129 if (child_die->tag != DW_TAG_enumerator)
14131 process_die (child_die, cu);
14135 name = dwarf2_name (child_die, cu);
14138 sym = new_symbol (child_die, this_type, cu);
14140 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
14142 fields = (struct field *)
14144 (num_fields + DW_FIELD_ALLOC_CHUNK)
14145 * sizeof (struct field));
14148 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
14149 FIELD_TYPE (fields[num_fields]) = NULL;
14150 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
14151 FIELD_BITSIZE (fields[num_fields]) = 0;
14157 child_die = sibling_die (child_die);
14162 TYPE_NFIELDS (this_type) = num_fields;
14163 TYPE_FIELDS (this_type) = (struct field *)
14164 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
14165 memcpy (TYPE_FIELDS (this_type), fields,
14166 sizeof (struct field) * num_fields);
14171 /* If we are reading an enum from a .debug_types unit, and the enum
14172 is a declaration, and the enum is not the signatured type in the
14173 unit, then we do not want to add a symbol for it. Adding a
14174 symbol would in some cases obscure the true definition of the
14175 enum, giving users an incomplete type when the definition is
14176 actually available. Note that we do not want to do this for all
14177 enums which are just declarations, because C++0x allows forward
14178 enum declarations. */
14179 if (cu->per_cu->is_debug_types
14180 && die_is_declaration (die, cu))
14182 struct signatured_type *sig_type;
14184 sig_type = (struct signatured_type *) cu->per_cu;
14185 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14186 if (sig_type->type_offset_in_section != die->sect_off)
14190 new_symbol (die, this_type, cu);
14193 /* Extract all information from a DW_TAG_array_type DIE and put it in
14194 the DIE's type field. For now, this only handles one dimensional
14197 static struct type *
14198 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14200 struct objfile *objfile = cu->objfile;
14201 struct die_info *child_die;
14203 struct type *element_type, *range_type, *index_type;
14204 struct type **range_types = NULL;
14205 struct attribute *attr;
14207 struct cleanup *back_to;
14209 unsigned int bit_stride = 0;
14211 element_type = die_type (die, cu);
14213 /* The die_type call above may have already set the type for this DIE. */
14214 type = get_die_type (die, cu);
14218 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14220 bit_stride = DW_UNSND (attr) * 8;
14222 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14224 bit_stride = DW_UNSND (attr);
14226 /* Irix 6.2 native cc creates array types without children for
14227 arrays with unspecified length. */
14228 if (die->child == NULL)
14230 index_type = objfile_type (objfile)->builtin_int;
14231 range_type = create_static_range_type (NULL, index_type, 0, -1);
14232 type = create_array_type_with_stride (NULL, element_type, range_type,
14234 return set_die_type (die, type, cu);
14237 back_to = make_cleanup (null_cleanup, NULL);
14238 child_die = die->child;
14239 while (child_die && child_die->tag)
14241 if (child_die->tag == DW_TAG_subrange_type)
14243 struct type *child_type = read_type_die (child_die, cu);
14245 if (child_type != NULL)
14247 /* The range type was succesfully read. Save it for the
14248 array type creation. */
14249 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14251 range_types = (struct type **)
14252 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14253 * sizeof (struct type *));
14255 make_cleanup (free_current_contents, &range_types);
14257 range_types[ndim++] = child_type;
14260 child_die = sibling_die (child_die);
14263 /* Dwarf2 dimensions are output from left to right, create the
14264 necessary array types in backwards order. */
14266 type = element_type;
14268 if (read_array_order (die, cu) == DW_ORD_col_major)
14273 type = create_array_type_with_stride (NULL, type, range_types[i++],
14279 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14283 /* Understand Dwarf2 support for vector types (like they occur on
14284 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14285 array type. This is not part of the Dwarf2/3 standard yet, but a
14286 custom vendor extension. The main difference between a regular
14287 array and the vector variant is that vectors are passed by value
14289 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14291 make_vector_type (type);
14293 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14294 implementation may choose to implement triple vectors using this
14296 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14299 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14300 TYPE_LENGTH (type) = DW_UNSND (attr);
14302 complaint (&symfile_complaints,
14303 _("DW_AT_byte_size for array type smaller "
14304 "than the total size of elements"));
14307 name = dwarf2_name (die, cu);
14309 TYPE_NAME (type) = name;
14311 /* Install the type in the die. */
14312 set_die_type (die, type, cu);
14314 /* set_die_type should be already done. */
14315 set_descriptive_type (type, die, cu);
14317 do_cleanups (back_to);
14322 static enum dwarf_array_dim_ordering
14323 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14325 struct attribute *attr;
14327 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14330 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14332 /* GNU F77 is a special case, as at 08/2004 array type info is the
14333 opposite order to the dwarf2 specification, but data is still
14334 laid out as per normal fortran.
14336 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14337 version checking. */
14339 if (cu->language == language_fortran
14340 && cu->producer && strstr (cu->producer, "GNU F77"))
14342 return DW_ORD_row_major;
14345 switch (cu->language_defn->la_array_ordering)
14347 case array_column_major:
14348 return DW_ORD_col_major;
14349 case array_row_major:
14351 return DW_ORD_row_major;
14355 /* Extract all information from a DW_TAG_set_type DIE and put it in
14356 the DIE's type field. */
14358 static struct type *
14359 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14361 struct type *domain_type, *set_type;
14362 struct attribute *attr;
14364 domain_type = die_type (die, cu);
14366 /* The die_type call above may have already set the type for this DIE. */
14367 set_type = get_die_type (die, cu);
14371 set_type = create_set_type (NULL, domain_type);
14373 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14375 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14377 return set_die_type (die, set_type, cu);
14380 /* A helper for read_common_block that creates a locexpr baton.
14381 SYM is the symbol which we are marking as computed.
14382 COMMON_DIE is the DIE for the common block.
14383 COMMON_LOC is the location expression attribute for the common
14385 MEMBER_LOC is the location expression attribute for the particular
14386 member of the common block that we are processing.
14387 CU is the CU from which the above come. */
14390 mark_common_block_symbol_computed (struct symbol *sym,
14391 struct die_info *common_die,
14392 struct attribute *common_loc,
14393 struct attribute *member_loc,
14394 struct dwarf2_cu *cu)
14396 struct objfile *objfile = dwarf2_per_objfile->objfile;
14397 struct dwarf2_locexpr_baton *baton;
14399 unsigned int cu_off;
14400 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14401 LONGEST offset = 0;
14403 gdb_assert (common_loc && member_loc);
14404 gdb_assert (attr_form_is_block (common_loc));
14405 gdb_assert (attr_form_is_block (member_loc)
14406 || attr_form_is_constant (member_loc));
14408 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14409 baton->per_cu = cu->per_cu;
14410 gdb_assert (baton->per_cu);
14412 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14414 if (attr_form_is_constant (member_loc))
14416 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14417 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14420 baton->size += DW_BLOCK (member_loc)->size;
14422 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14425 *ptr++ = DW_OP_call4;
14426 cu_off = common_die->sect_off - cu->per_cu->sect_off;
14427 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14430 if (attr_form_is_constant (member_loc))
14432 *ptr++ = DW_OP_addr;
14433 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14434 ptr += cu->header.addr_size;
14438 /* We have to copy the data here, because DW_OP_call4 will only
14439 use a DW_AT_location attribute. */
14440 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14441 ptr += DW_BLOCK (member_loc)->size;
14444 *ptr++ = DW_OP_plus;
14445 gdb_assert (ptr - baton->data == baton->size);
14447 SYMBOL_LOCATION_BATON (sym) = baton;
14448 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14451 /* Create appropriate locally-scoped variables for all the
14452 DW_TAG_common_block entries. Also create a struct common_block
14453 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14454 is used to sepate the common blocks name namespace from regular
14458 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14460 struct attribute *attr;
14462 attr = dwarf2_attr (die, DW_AT_location, cu);
14465 /* Support the .debug_loc offsets. */
14466 if (attr_form_is_block (attr))
14470 else if (attr_form_is_section_offset (attr))
14472 dwarf2_complex_location_expr_complaint ();
14477 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14478 "common block member");
14483 if (die->child != NULL)
14485 struct objfile *objfile = cu->objfile;
14486 struct die_info *child_die;
14487 size_t n_entries = 0, size;
14488 struct common_block *common_block;
14489 struct symbol *sym;
14491 for (child_die = die->child;
14492 child_die && child_die->tag;
14493 child_die = sibling_die (child_die))
14496 size = (sizeof (struct common_block)
14497 + (n_entries - 1) * sizeof (struct symbol *));
14499 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14501 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14502 common_block->n_entries = 0;
14504 for (child_die = die->child;
14505 child_die && child_die->tag;
14506 child_die = sibling_die (child_die))
14508 /* Create the symbol in the DW_TAG_common_block block in the current
14510 sym = new_symbol (child_die, NULL, cu);
14513 struct attribute *member_loc;
14515 common_block->contents[common_block->n_entries++] = sym;
14517 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14521 /* GDB has handled this for a long time, but it is
14522 not specified by DWARF. It seems to have been
14523 emitted by gfortran at least as recently as:
14524 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14525 complaint (&symfile_complaints,
14526 _("Variable in common block has "
14527 "DW_AT_data_member_location "
14528 "- DIE at 0x%x [in module %s]"),
14529 to_underlying (child_die->sect_off),
14530 objfile_name (cu->objfile));
14532 if (attr_form_is_section_offset (member_loc))
14533 dwarf2_complex_location_expr_complaint ();
14534 else if (attr_form_is_constant (member_loc)
14535 || attr_form_is_block (member_loc))
14538 mark_common_block_symbol_computed (sym, die, attr,
14542 dwarf2_complex_location_expr_complaint ();
14547 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14548 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14552 /* Create a type for a C++ namespace. */
14554 static struct type *
14555 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14557 struct objfile *objfile = cu->objfile;
14558 const char *previous_prefix, *name;
14562 /* For extensions, reuse the type of the original namespace. */
14563 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14565 struct die_info *ext_die;
14566 struct dwarf2_cu *ext_cu = cu;
14568 ext_die = dwarf2_extension (die, &ext_cu);
14569 type = read_type_die (ext_die, ext_cu);
14571 /* EXT_CU may not be the same as CU.
14572 Ensure TYPE is recorded with CU in die_type_hash. */
14573 return set_die_type (die, type, cu);
14576 name = namespace_name (die, &is_anonymous, cu);
14578 /* Now build the name of the current namespace. */
14580 previous_prefix = determine_prefix (die, cu);
14581 if (previous_prefix[0] != '\0')
14582 name = typename_concat (&objfile->objfile_obstack,
14583 previous_prefix, name, 0, cu);
14585 /* Create the type. */
14586 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14587 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14589 return set_die_type (die, type, cu);
14592 /* Read a namespace scope. */
14595 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14597 struct objfile *objfile = cu->objfile;
14600 /* Add a symbol associated to this if we haven't seen the namespace
14601 before. Also, add a using directive if it's an anonymous
14604 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14608 type = read_type_die (die, cu);
14609 new_symbol (die, type, cu);
14611 namespace_name (die, &is_anonymous, cu);
14614 const char *previous_prefix = determine_prefix (die, cu);
14616 std::vector<const char *> excludes;
14617 add_using_directive (using_directives (cu->language),
14618 previous_prefix, TYPE_NAME (type), NULL,
14619 NULL, excludes, 0, &objfile->objfile_obstack);
14623 if (die->child != NULL)
14625 struct die_info *child_die = die->child;
14627 while (child_die && child_die->tag)
14629 process_die (child_die, cu);
14630 child_die = sibling_die (child_die);
14635 /* Read a Fortran module as type. This DIE can be only a declaration used for
14636 imported module. Still we need that type as local Fortran "use ... only"
14637 declaration imports depend on the created type in determine_prefix. */
14639 static struct type *
14640 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14642 struct objfile *objfile = cu->objfile;
14643 const char *module_name;
14646 module_name = dwarf2_name (die, cu);
14648 complaint (&symfile_complaints,
14649 _("DW_TAG_module has no name, offset 0x%x"),
14650 to_underlying (die->sect_off));
14651 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14653 /* determine_prefix uses TYPE_TAG_NAME. */
14654 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14656 return set_die_type (die, type, cu);
14659 /* Read a Fortran module. */
14662 read_module (struct die_info *die, struct dwarf2_cu *cu)
14664 struct die_info *child_die = die->child;
14667 type = read_type_die (die, cu);
14668 new_symbol (die, type, cu);
14670 while (child_die && child_die->tag)
14672 process_die (child_die, cu);
14673 child_die = sibling_die (child_die);
14677 /* Return the name of the namespace represented by DIE. Set
14678 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14681 static const char *
14682 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14684 struct die_info *current_die;
14685 const char *name = NULL;
14687 /* Loop through the extensions until we find a name. */
14689 for (current_die = die;
14690 current_die != NULL;
14691 current_die = dwarf2_extension (die, &cu))
14693 /* We don't use dwarf2_name here so that we can detect the absence
14694 of a name -> anonymous namespace. */
14695 name = dwarf2_string_attr (die, DW_AT_name, cu);
14701 /* Is it an anonymous namespace? */
14703 *is_anonymous = (name == NULL);
14705 name = CP_ANONYMOUS_NAMESPACE_STR;
14710 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14711 the user defined type vector. */
14713 static struct type *
14714 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14716 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14717 struct comp_unit_head *cu_header = &cu->header;
14719 struct attribute *attr_byte_size;
14720 struct attribute *attr_address_class;
14721 int byte_size, addr_class;
14722 struct type *target_type;
14724 target_type = die_type (die, cu);
14726 /* The die_type call above may have already set the type for this DIE. */
14727 type = get_die_type (die, cu);
14731 type = lookup_pointer_type (target_type);
14733 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14734 if (attr_byte_size)
14735 byte_size = DW_UNSND (attr_byte_size);
14737 byte_size = cu_header->addr_size;
14739 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14740 if (attr_address_class)
14741 addr_class = DW_UNSND (attr_address_class);
14743 addr_class = DW_ADDR_none;
14745 /* If the pointer size or address class is different than the
14746 default, create a type variant marked as such and set the
14747 length accordingly. */
14748 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14750 if (gdbarch_address_class_type_flags_p (gdbarch))
14754 type_flags = gdbarch_address_class_type_flags
14755 (gdbarch, byte_size, addr_class);
14756 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14758 type = make_type_with_address_space (type, type_flags);
14760 else if (TYPE_LENGTH (type) != byte_size)
14762 complaint (&symfile_complaints,
14763 _("invalid pointer size %d"), byte_size);
14767 /* Should we also complain about unhandled address classes? */
14771 TYPE_LENGTH (type) = byte_size;
14772 return set_die_type (die, type, cu);
14775 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14776 the user defined type vector. */
14778 static struct type *
14779 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14782 struct type *to_type;
14783 struct type *domain;
14785 to_type = die_type (die, cu);
14786 domain = die_containing_type (die, cu);
14788 /* The calls above may have already set the type for this DIE. */
14789 type = get_die_type (die, cu);
14793 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14794 type = lookup_methodptr_type (to_type);
14795 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14797 struct type *new_type = alloc_type (cu->objfile);
14799 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14800 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14801 TYPE_VARARGS (to_type));
14802 type = lookup_methodptr_type (new_type);
14805 type = lookup_memberptr_type (to_type, domain);
14807 return set_die_type (die, type, cu);
14810 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14811 the user defined type vector. */
14813 static struct type *
14814 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14815 enum type_code refcode)
14817 struct comp_unit_head *cu_header = &cu->header;
14818 struct type *type, *target_type;
14819 struct attribute *attr;
14821 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14823 target_type = die_type (die, cu);
14825 /* The die_type call above may have already set the type for this DIE. */
14826 type = get_die_type (die, cu);
14830 type = lookup_reference_type (target_type, refcode);
14831 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14834 TYPE_LENGTH (type) = DW_UNSND (attr);
14838 TYPE_LENGTH (type) = cu_header->addr_size;
14840 return set_die_type (die, type, cu);
14843 /* Add the given cv-qualifiers to the element type of the array. GCC
14844 outputs DWARF type qualifiers that apply to an array, not the
14845 element type. But GDB relies on the array element type to carry
14846 the cv-qualifiers. This mimics section 6.7.3 of the C99
14849 static struct type *
14850 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14851 struct type *base_type, int cnst, int voltl)
14853 struct type *el_type, *inner_array;
14855 base_type = copy_type (base_type);
14856 inner_array = base_type;
14858 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14860 TYPE_TARGET_TYPE (inner_array) =
14861 copy_type (TYPE_TARGET_TYPE (inner_array));
14862 inner_array = TYPE_TARGET_TYPE (inner_array);
14865 el_type = TYPE_TARGET_TYPE (inner_array);
14866 cnst |= TYPE_CONST (el_type);
14867 voltl |= TYPE_VOLATILE (el_type);
14868 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14870 return set_die_type (die, base_type, cu);
14873 static struct type *
14874 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14876 struct type *base_type, *cv_type;
14878 base_type = die_type (die, cu);
14880 /* The die_type call above may have already set the type for this DIE. */
14881 cv_type = get_die_type (die, cu);
14885 /* In case the const qualifier is applied to an array type, the element type
14886 is so qualified, not the array type (section 6.7.3 of C99). */
14887 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14888 return add_array_cv_type (die, cu, base_type, 1, 0);
14890 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14891 return set_die_type (die, cv_type, cu);
14894 static struct type *
14895 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14897 struct type *base_type, *cv_type;
14899 base_type = die_type (die, cu);
14901 /* The die_type call above may have already set the type for this DIE. */
14902 cv_type = get_die_type (die, cu);
14906 /* In case the volatile qualifier is applied to an array type, the
14907 element type is so qualified, not the array type (section 6.7.3
14909 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14910 return add_array_cv_type (die, cu, base_type, 0, 1);
14912 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14913 return set_die_type (die, cv_type, cu);
14916 /* Handle DW_TAG_restrict_type. */
14918 static struct type *
14919 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14921 struct type *base_type, *cv_type;
14923 base_type = die_type (die, cu);
14925 /* The die_type call above may have already set the type for this DIE. */
14926 cv_type = get_die_type (die, cu);
14930 cv_type = make_restrict_type (base_type);
14931 return set_die_type (die, cv_type, cu);
14934 /* Handle DW_TAG_atomic_type. */
14936 static struct type *
14937 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14939 struct type *base_type, *cv_type;
14941 base_type = die_type (die, cu);
14943 /* The die_type call above may have already set the type for this DIE. */
14944 cv_type = get_die_type (die, cu);
14948 cv_type = make_atomic_type (base_type);
14949 return set_die_type (die, cv_type, cu);
14952 /* Extract all information from a DW_TAG_string_type DIE and add to
14953 the user defined type vector. It isn't really a user defined type,
14954 but it behaves like one, with other DIE's using an AT_user_def_type
14955 attribute to reference it. */
14957 static struct type *
14958 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14960 struct objfile *objfile = cu->objfile;
14961 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14962 struct type *type, *range_type, *index_type, *char_type;
14963 struct attribute *attr;
14964 unsigned int length;
14966 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14969 length = DW_UNSND (attr);
14973 /* Check for the DW_AT_byte_size attribute. */
14974 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14977 length = DW_UNSND (attr);
14985 index_type = objfile_type (objfile)->builtin_int;
14986 range_type = create_static_range_type (NULL, index_type, 1, length);
14987 char_type = language_string_char_type (cu->language_defn, gdbarch);
14988 type = create_string_type (NULL, char_type, range_type);
14990 return set_die_type (die, type, cu);
14993 /* Assuming that DIE corresponds to a function, returns nonzero
14994 if the function is prototyped. */
14997 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14999 struct attribute *attr;
15001 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
15002 if (attr && (DW_UNSND (attr) != 0))
15005 /* The DWARF standard implies that the DW_AT_prototyped attribute
15006 is only meaninful for C, but the concept also extends to other
15007 languages that allow unprototyped functions (Eg: Objective C).
15008 For all other languages, assume that functions are always
15010 if (cu->language != language_c
15011 && cu->language != language_objc
15012 && cu->language != language_opencl)
15015 /* RealView does not emit DW_AT_prototyped. We can not distinguish
15016 prototyped and unprototyped functions; default to prototyped,
15017 since that is more common in modern code (and RealView warns
15018 about unprototyped functions). */
15019 if (producer_is_realview (cu->producer))
15025 /* Handle DIES due to C code like:
15029 int (*funcp)(int a, long l);
15033 ('funcp' generates a DW_TAG_subroutine_type DIE). */
15035 static struct type *
15036 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
15038 struct objfile *objfile = cu->objfile;
15039 struct type *type; /* Type that this function returns. */
15040 struct type *ftype; /* Function that returns above type. */
15041 struct attribute *attr;
15043 type = die_type (die, cu);
15045 /* The die_type call above may have already set the type for this DIE. */
15046 ftype = get_die_type (die, cu);
15050 ftype = lookup_function_type (type);
15052 if (prototyped_function_p (die, cu))
15053 TYPE_PROTOTYPED (ftype) = 1;
15055 /* Store the calling convention in the type if it's available in
15056 the subroutine die. Otherwise set the calling convention to
15057 the default value DW_CC_normal. */
15058 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15060 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
15061 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
15062 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
15064 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
15066 /* Record whether the function returns normally to its caller or not
15067 if the DWARF producer set that information. */
15068 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
15069 if (attr && (DW_UNSND (attr) != 0))
15070 TYPE_NO_RETURN (ftype) = 1;
15072 /* We need to add the subroutine type to the die immediately so
15073 we don't infinitely recurse when dealing with parameters
15074 declared as the same subroutine type. */
15075 set_die_type (die, ftype, cu);
15077 if (die->child != NULL)
15079 struct type *void_type = objfile_type (objfile)->builtin_void;
15080 struct die_info *child_die;
15081 int nparams, iparams;
15083 /* Count the number of parameters.
15084 FIXME: GDB currently ignores vararg functions, but knows about
15085 vararg member functions. */
15087 child_die = die->child;
15088 while (child_die && child_die->tag)
15090 if (child_die->tag == DW_TAG_formal_parameter)
15092 else if (child_die->tag == DW_TAG_unspecified_parameters)
15093 TYPE_VARARGS (ftype) = 1;
15094 child_die = sibling_die (child_die);
15097 /* Allocate storage for parameters and fill them in. */
15098 TYPE_NFIELDS (ftype) = nparams;
15099 TYPE_FIELDS (ftype) = (struct field *)
15100 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
15102 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
15103 even if we error out during the parameters reading below. */
15104 for (iparams = 0; iparams < nparams; iparams++)
15105 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
15108 child_die = die->child;
15109 while (child_die && child_die->tag)
15111 if (child_die->tag == DW_TAG_formal_parameter)
15113 struct type *arg_type;
15115 /* DWARF version 2 has no clean way to discern C++
15116 static and non-static member functions. G++ helps
15117 GDB by marking the first parameter for non-static
15118 member functions (which is the this pointer) as
15119 artificial. We pass this information to
15120 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
15122 DWARF version 3 added DW_AT_object_pointer, which GCC
15123 4.5 does not yet generate. */
15124 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
15126 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
15128 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
15129 arg_type = die_type (child_die, cu);
15131 /* RealView does not mark THIS as const, which the testsuite
15132 expects. GCC marks THIS as const in method definitions,
15133 but not in the class specifications (GCC PR 43053). */
15134 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
15135 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
15138 struct dwarf2_cu *arg_cu = cu;
15139 const char *name = dwarf2_name (child_die, cu);
15141 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
15144 /* If the compiler emits this, use it. */
15145 if (follow_die_ref (die, attr, &arg_cu) == child_die)
15148 else if (name && strcmp (name, "this") == 0)
15149 /* Function definitions will have the argument names. */
15151 else if (name == NULL && iparams == 0)
15152 /* Declarations may not have the names, so like
15153 elsewhere in GDB, assume an artificial first
15154 argument is "this". */
15158 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
15162 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
15165 child_die = sibling_die (child_die);
15172 static struct type *
15173 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15175 struct objfile *objfile = cu->objfile;
15176 const char *name = NULL;
15177 struct type *this_type, *target_type;
15179 name = dwarf2_full_name (NULL, die, cu);
15180 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15181 TYPE_TARGET_STUB (this_type) = 1;
15182 set_die_type (die, this_type, cu);
15183 target_type = die_type (die, cu);
15184 if (target_type != this_type)
15185 TYPE_TARGET_TYPE (this_type) = target_type;
15188 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15189 spec and cause infinite loops in GDB. */
15190 complaint (&symfile_complaints,
15191 _("Self-referential DW_TAG_typedef "
15192 "- DIE at 0x%x [in module %s]"),
15193 to_underlying (die->sect_off), objfile_name (objfile));
15194 TYPE_TARGET_TYPE (this_type) = NULL;
15199 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15200 (which may be different from NAME) to the architecture back-end to allow
15201 it to guess the correct format if necessary. */
15203 static struct type *
15204 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15205 const char *name_hint)
15207 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15208 const struct floatformat **format;
15211 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15213 type = init_float_type (objfile, bits, name, format);
15215 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15220 /* Find a representation of a given base type and install
15221 it in the TYPE field of the die. */
15223 static struct type *
15224 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15226 struct objfile *objfile = cu->objfile;
15228 struct attribute *attr;
15229 int encoding = 0, bits = 0;
15232 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15235 encoding = DW_UNSND (attr);
15237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15240 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15242 name = dwarf2_name (die, cu);
15245 complaint (&symfile_complaints,
15246 _("DW_AT_name missing from DW_TAG_base_type"));
15251 case DW_ATE_address:
15252 /* Turn DW_ATE_address into a void * pointer. */
15253 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15254 type = init_pointer_type (objfile, bits, name, type);
15256 case DW_ATE_boolean:
15257 type = init_boolean_type (objfile, bits, 1, name);
15259 case DW_ATE_complex_float:
15260 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15261 type = init_complex_type (objfile, name, type);
15263 case DW_ATE_decimal_float:
15264 type = init_decfloat_type (objfile, bits, name);
15267 type = dwarf2_init_float_type (objfile, bits, name, name);
15269 case DW_ATE_signed:
15270 type = init_integer_type (objfile, bits, 0, name);
15272 case DW_ATE_unsigned:
15273 if (cu->language == language_fortran
15275 && startswith (name, "character("))
15276 type = init_character_type (objfile, bits, 1, name);
15278 type = init_integer_type (objfile, bits, 1, name);
15280 case DW_ATE_signed_char:
15281 if (cu->language == language_ada || cu->language == language_m2
15282 || cu->language == language_pascal
15283 || cu->language == language_fortran)
15284 type = init_character_type (objfile, bits, 0, name);
15286 type = init_integer_type (objfile, bits, 0, name);
15288 case DW_ATE_unsigned_char:
15289 if (cu->language == language_ada || cu->language == language_m2
15290 || cu->language == language_pascal
15291 || cu->language == language_fortran
15292 || cu->language == language_rust)
15293 type = init_character_type (objfile, bits, 1, name);
15295 type = init_integer_type (objfile, bits, 1, name);
15299 gdbarch *arch = get_objfile_arch (objfile);
15302 type = builtin_type (arch)->builtin_char16;
15303 else if (bits == 32)
15304 type = builtin_type (arch)->builtin_char32;
15307 complaint (&symfile_complaints,
15308 _("unsupported DW_ATE_UTF bit size: '%d'"),
15310 type = init_integer_type (objfile, bits, 1, name);
15312 return set_die_type (die, type, cu);
15317 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15318 dwarf_type_encoding_name (encoding));
15319 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15323 if (name && strcmp (name, "char") == 0)
15324 TYPE_NOSIGN (type) = 1;
15326 return set_die_type (die, type, cu);
15329 /* Parse dwarf attribute if it's a block, reference or constant and put the
15330 resulting value of the attribute into struct bound_prop.
15331 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15334 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15335 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15337 struct dwarf2_property_baton *baton;
15338 struct obstack *obstack = &cu->objfile->objfile_obstack;
15340 if (attr == NULL || prop == NULL)
15343 if (attr_form_is_block (attr))
15345 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15346 baton->referenced_type = NULL;
15347 baton->locexpr.per_cu = cu->per_cu;
15348 baton->locexpr.size = DW_BLOCK (attr)->size;
15349 baton->locexpr.data = DW_BLOCK (attr)->data;
15350 prop->data.baton = baton;
15351 prop->kind = PROP_LOCEXPR;
15352 gdb_assert (prop->data.baton != NULL);
15354 else if (attr_form_is_ref (attr))
15356 struct dwarf2_cu *target_cu = cu;
15357 struct die_info *target_die;
15358 struct attribute *target_attr;
15360 target_die = follow_die_ref (die, attr, &target_cu);
15361 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15362 if (target_attr == NULL)
15363 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15365 if (target_attr == NULL)
15368 switch (target_attr->name)
15370 case DW_AT_location:
15371 if (attr_form_is_section_offset (target_attr))
15373 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15374 baton->referenced_type = die_type (target_die, target_cu);
15375 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15376 prop->data.baton = baton;
15377 prop->kind = PROP_LOCLIST;
15378 gdb_assert (prop->data.baton != NULL);
15380 else if (attr_form_is_block (target_attr))
15382 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15383 baton->referenced_type = die_type (target_die, target_cu);
15384 baton->locexpr.per_cu = cu->per_cu;
15385 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15386 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15387 prop->data.baton = baton;
15388 prop->kind = PROP_LOCEXPR;
15389 gdb_assert (prop->data.baton != NULL);
15393 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15394 "dynamic property");
15398 case DW_AT_data_member_location:
15402 if (!handle_data_member_location (target_die, target_cu,
15406 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15407 baton->referenced_type = read_type_die (target_die->parent,
15409 baton->offset_info.offset = offset;
15410 baton->offset_info.type = die_type (target_die, target_cu);
15411 prop->data.baton = baton;
15412 prop->kind = PROP_ADDR_OFFSET;
15417 else if (attr_form_is_constant (attr))
15419 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15420 prop->kind = PROP_CONST;
15424 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15425 dwarf2_name (die, cu));
15432 /* Read the given DW_AT_subrange DIE. */
15434 static struct type *
15435 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15437 struct type *base_type, *orig_base_type;
15438 struct type *range_type;
15439 struct attribute *attr;
15440 struct dynamic_prop low, high;
15441 int low_default_is_valid;
15442 int high_bound_is_count = 0;
15444 LONGEST negative_mask;
15446 orig_base_type = die_type (die, cu);
15447 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15448 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15449 creating the range type, but we use the result of check_typedef
15450 when examining properties of the type. */
15451 base_type = check_typedef (orig_base_type);
15453 /* The die_type call above may have already set the type for this DIE. */
15454 range_type = get_die_type (die, cu);
15458 low.kind = PROP_CONST;
15459 high.kind = PROP_CONST;
15460 high.data.const_val = 0;
15462 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15463 omitting DW_AT_lower_bound. */
15464 switch (cu->language)
15467 case language_cplus:
15468 low.data.const_val = 0;
15469 low_default_is_valid = 1;
15471 case language_fortran:
15472 low.data.const_val = 1;
15473 low_default_is_valid = 1;
15476 case language_objc:
15477 case language_rust:
15478 low.data.const_val = 0;
15479 low_default_is_valid = (cu->header.version >= 4);
15483 case language_pascal:
15484 low.data.const_val = 1;
15485 low_default_is_valid = (cu->header.version >= 4);
15488 low.data.const_val = 0;
15489 low_default_is_valid = 0;
15493 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15495 attr_to_dynamic_prop (attr, die, cu, &low);
15496 else if (!low_default_is_valid)
15497 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15498 "- DIE at 0x%x [in module %s]"),
15499 to_underlying (die->sect_off), objfile_name (cu->objfile));
15501 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15502 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15504 attr = dwarf2_attr (die, DW_AT_count, cu);
15505 if (attr_to_dynamic_prop (attr, die, cu, &high))
15507 /* If bounds are constant do the final calculation here. */
15508 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15509 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15511 high_bound_is_count = 1;
15515 /* Dwarf-2 specifications explicitly allows to create subrange types
15516 without specifying a base type.
15517 In that case, the base type must be set to the type of
15518 the lower bound, upper bound or count, in that order, if any of these
15519 three attributes references an object that has a type.
15520 If no base type is found, the Dwarf-2 specifications say that
15521 a signed integer type of size equal to the size of an address should
15523 For the following C code: `extern char gdb_int [];'
15524 GCC produces an empty range DIE.
15525 FIXME: muller/2010-05-28: Possible references to object for low bound,
15526 high bound or count are not yet handled by this code. */
15527 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15529 struct objfile *objfile = cu->objfile;
15530 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15531 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15532 struct type *int_type = objfile_type (objfile)->builtin_int;
15534 /* Test "int", "long int", and "long long int" objfile types,
15535 and select the first one having a size above or equal to the
15536 architecture address size. */
15537 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15538 base_type = int_type;
15541 int_type = objfile_type (objfile)->builtin_long;
15542 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15543 base_type = int_type;
15546 int_type = objfile_type (objfile)->builtin_long_long;
15547 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15548 base_type = int_type;
15553 /* Normally, the DWARF producers are expected to use a signed
15554 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15555 But this is unfortunately not always the case, as witnessed
15556 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15557 is used instead. To work around that ambiguity, we treat
15558 the bounds as signed, and thus sign-extend their values, when
15559 the base type is signed. */
15561 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15562 if (low.kind == PROP_CONST
15563 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15564 low.data.const_val |= negative_mask;
15565 if (high.kind == PROP_CONST
15566 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15567 high.data.const_val |= negative_mask;
15569 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15571 if (high_bound_is_count)
15572 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15574 /* Ada expects an empty array on no boundary attributes. */
15575 if (attr == NULL && cu->language != language_ada)
15576 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15578 name = dwarf2_name (die, cu);
15580 TYPE_NAME (range_type) = name;
15582 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15584 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15586 set_die_type (die, range_type, cu);
15588 /* set_die_type should be already done. */
15589 set_descriptive_type (range_type, die, cu);
15594 static struct type *
15595 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15599 /* For now, we only support the C meaning of an unspecified type: void. */
15601 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15602 TYPE_NAME (type) = dwarf2_name (die, cu);
15604 return set_die_type (die, type, cu);
15607 /* Read a single die and all its descendents. Set the die's sibling
15608 field to NULL; set other fields in the die correctly, and set all
15609 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15610 location of the info_ptr after reading all of those dies. PARENT
15611 is the parent of the die in question. */
15613 static struct die_info *
15614 read_die_and_children (const struct die_reader_specs *reader,
15615 const gdb_byte *info_ptr,
15616 const gdb_byte **new_info_ptr,
15617 struct die_info *parent)
15619 struct die_info *die;
15620 const gdb_byte *cur_ptr;
15623 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15626 *new_info_ptr = cur_ptr;
15629 store_in_ref_table (die, reader->cu);
15632 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15636 *new_info_ptr = cur_ptr;
15639 die->sibling = NULL;
15640 die->parent = parent;
15644 /* Read a die, all of its descendents, and all of its siblings; set
15645 all of the fields of all of the dies correctly. Arguments are as
15646 in read_die_and_children. */
15648 static struct die_info *
15649 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15650 const gdb_byte *info_ptr,
15651 const gdb_byte **new_info_ptr,
15652 struct die_info *parent)
15654 struct die_info *first_die, *last_sibling;
15655 const gdb_byte *cur_ptr;
15657 cur_ptr = info_ptr;
15658 first_die = last_sibling = NULL;
15662 struct die_info *die
15663 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15667 *new_info_ptr = cur_ptr;
15674 last_sibling->sibling = die;
15676 last_sibling = die;
15680 /* Read a die, all of its descendents, and all of its siblings; set
15681 all of the fields of all of the dies correctly. Arguments are as
15682 in read_die_and_children.
15683 This the main entry point for reading a DIE and all its children. */
15685 static struct die_info *
15686 read_die_and_siblings (const struct die_reader_specs *reader,
15687 const gdb_byte *info_ptr,
15688 const gdb_byte **new_info_ptr,
15689 struct die_info *parent)
15691 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15692 new_info_ptr, parent);
15694 if (dwarf_die_debug)
15696 fprintf_unfiltered (gdb_stdlog,
15697 "Read die from %s@0x%x of %s:\n",
15698 get_section_name (reader->die_section),
15699 (unsigned) (info_ptr - reader->die_section->buffer),
15700 bfd_get_filename (reader->abfd));
15701 dump_die (die, dwarf_die_debug);
15707 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15709 The caller is responsible for filling in the extra attributes
15710 and updating (*DIEP)->num_attrs.
15711 Set DIEP to point to a newly allocated die with its information,
15712 except for its child, sibling, and parent fields.
15713 Set HAS_CHILDREN to tell whether the die has children or not. */
15715 static const gdb_byte *
15716 read_full_die_1 (const struct die_reader_specs *reader,
15717 struct die_info **diep, const gdb_byte *info_ptr,
15718 int *has_children, int num_extra_attrs)
15720 unsigned int abbrev_number, bytes_read, i;
15721 struct abbrev_info *abbrev;
15722 struct die_info *die;
15723 struct dwarf2_cu *cu = reader->cu;
15724 bfd *abfd = reader->abfd;
15726 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15727 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15728 info_ptr += bytes_read;
15729 if (!abbrev_number)
15736 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15738 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15740 bfd_get_filename (abfd));
15742 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15743 die->sect_off = sect_off;
15744 die->tag = abbrev->tag;
15745 die->abbrev = abbrev_number;
15747 /* Make the result usable.
15748 The caller needs to update num_attrs after adding the extra
15750 die->num_attrs = abbrev->num_attrs;
15752 for (i = 0; i < abbrev->num_attrs; ++i)
15753 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15757 *has_children = abbrev->has_children;
15761 /* Read a die and all its attributes.
15762 Set DIEP to point to a newly allocated die with its information,
15763 except for its child, sibling, and parent fields.
15764 Set HAS_CHILDREN to tell whether the die has children or not. */
15766 static const gdb_byte *
15767 read_full_die (const struct die_reader_specs *reader,
15768 struct die_info **diep, const gdb_byte *info_ptr,
15771 const gdb_byte *result;
15773 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15775 if (dwarf_die_debug)
15777 fprintf_unfiltered (gdb_stdlog,
15778 "Read die from %s@0x%x of %s:\n",
15779 get_section_name (reader->die_section),
15780 (unsigned) (info_ptr - reader->die_section->buffer),
15781 bfd_get_filename (reader->abfd));
15782 dump_die (*diep, dwarf_die_debug);
15788 /* Abbreviation tables.
15790 In DWARF version 2, the description of the debugging information is
15791 stored in a separate .debug_abbrev section. Before we read any
15792 dies from a section we read in all abbreviations and install them
15793 in a hash table. */
15795 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15797 static struct abbrev_info *
15798 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15800 struct abbrev_info *abbrev;
15802 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15803 memset (abbrev, 0, sizeof (struct abbrev_info));
15808 /* Add an abbreviation to the table. */
15811 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15812 unsigned int abbrev_number,
15813 struct abbrev_info *abbrev)
15815 unsigned int hash_number;
15817 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15818 abbrev->next = abbrev_table->abbrevs[hash_number];
15819 abbrev_table->abbrevs[hash_number] = abbrev;
15822 /* Look up an abbrev in the table.
15823 Returns NULL if the abbrev is not found. */
15825 static struct abbrev_info *
15826 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15827 unsigned int abbrev_number)
15829 unsigned int hash_number;
15830 struct abbrev_info *abbrev;
15832 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15833 abbrev = abbrev_table->abbrevs[hash_number];
15837 if (abbrev->number == abbrev_number)
15839 abbrev = abbrev->next;
15844 /* Read in an abbrev table. */
15846 static struct abbrev_table *
15847 abbrev_table_read_table (struct dwarf2_section_info *section,
15848 sect_offset sect_off)
15850 struct objfile *objfile = dwarf2_per_objfile->objfile;
15851 bfd *abfd = get_section_bfd_owner (section);
15852 struct abbrev_table *abbrev_table;
15853 const gdb_byte *abbrev_ptr;
15854 struct abbrev_info *cur_abbrev;
15855 unsigned int abbrev_number, bytes_read, abbrev_name;
15856 unsigned int abbrev_form;
15857 struct attr_abbrev *cur_attrs;
15858 unsigned int allocated_attrs;
15860 abbrev_table = XNEW (struct abbrev_table);
15861 abbrev_table->sect_off = sect_off;
15862 obstack_init (&abbrev_table->abbrev_obstack);
15863 abbrev_table->abbrevs =
15864 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15866 memset (abbrev_table->abbrevs, 0,
15867 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15869 dwarf2_read_section (objfile, section);
15870 abbrev_ptr = section->buffer + to_underlying (sect_off);
15871 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15872 abbrev_ptr += bytes_read;
15874 allocated_attrs = ATTR_ALLOC_CHUNK;
15875 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15877 /* Loop until we reach an abbrev number of 0. */
15878 while (abbrev_number)
15880 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15882 /* read in abbrev header */
15883 cur_abbrev->number = abbrev_number;
15885 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15886 abbrev_ptr += bytes_read;
15887 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15890 /* now read in declarations */
15893 LONGEST implicit_const;
15895 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15896 abbrev_ptr += bytes_read;
15897 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15898 abbrev_ptr += bytes_read;
15899 if (abbrev_form == DW_FORM_implicit_const)
15901 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15903 abbrev_ptr += bytes_read;
15907 /* Initialize it due to a false compiler warning. */
15908 implicit_const = -1;
15911 if (abbrev_name == 0)
15914 if (cur_abbrev->num_attrs == allocated_attrs)
15916 allocated_attrs += ATTR_ALLOC_CHUNK;
15918 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15921 cur_attrs[cur_abbrev->num_attrs].name
15922 = (enum dwarf_attribute) abbrev_name;
15923 cur_attrs[cur_abbrev->num_attrs].form
15924 = (enum dwarf_form) abbrev_form;
15925 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15926 ++cur_abbrev->num_attrs;
15929 cur_abbrev->attrs =
15930 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15931 cur_abbrev->num_attrs);
15932 memcpy (cur_abbrev->attrs, cur_attrs,
15933 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15935 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15937 /* Get next abbreviation.
15938 Under Irix6 the abbreviations for a compilation unit are not
15939 always properly terminated with an abbrev number of 0.
15940 Exit loop if we encounter an abbreviation which we have
15941 already read (which means we are about to read the abbreviations
15942 for the next compile unit) or if the end of the abbreviation
15943 table is reached. */
15944 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15946 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15947 abbrev_ptr += bytes_read;
15948 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15953 return abbrev_table;
15956 /* Free the resources held by ABBREV_TABLE. */
15959 abbrev_table_free (struct abbrev_table *abbrev_table)
15961 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15962 xfree (abbrev_table);
15965 /* Same as abbrev_table_free but as a cleanup.
15966 We pass in a pointer to the pointer to the table so that we can
15967 set the pointer to NULL when we're done. It also simplifies
15968 build_type_psymtabs_1. */
15971 abbrev_table_free_cleanup (void *table_ptr)
15973 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15975 if (*abbrev_table_ptr != NULL)
15976 abbrev_table_free (*abbrev_table_ptr);
15977 *abbrev_table_ptr = NULL;
15980 /* Read the abbrev table for CU from ABBREV_SECTION. */
15983 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15984 struct dwarf2_section_info *abbrev_section)
15987 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
15990 /* Release the memory used by the abbrev table for a compilation unit. */
15993 dwarf2_free_abbrev_table (void *ptr_to_cu)
15995 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15997 if (cu->abbrev_table != NULL)
15998 abbrev_table_free (cu->abbrev_table);
15999 /* Set this to NULL so that we SEGV if we try to read it later,
16000 and also because free_comp_unit verifies this is NULL. */
16001 cu->abbrev_table = NULL;
16004 /* Returns nonzero if TAG represents a type that we might generate a partial
16008 is_type_tag_for_partial (int tag)
16013 /* Some types that would be reasonable to generate partial symbols for,
16014 that we don't at present. */
16015 case DW_TAG_array_type:
16016 case DW_TAG_file_type:
16017 case DW_TAG_ptr_to_member_type:
16018 case DW_TAG_set_type:
16019 case DW_TAG_string_type:
16020 case DW_TAG_subroutine_type:
16022 case DW_TAG_base_type:
16023 case DW_TAG_class_type:
16024 case DW_TAG_interface_type:
16025 case DW_TAG_enumeration_type:
16026 case DW_TAG_structure_type:
16027 case DW_TAG_subrange_type:
16028 case DW_TAG_typedef:
16029 case DW_TAG_union_type:
16036 /* Load all DIEs that are interesting for partial symbols into memory. */
16038 static struct partial_die_info *
16039 load_partial_dies (const struct die_reader_specs *reader,
16040 const gdb_byte *info_ptr, int building_psymtab)
16042 struct dwarf2_cu *cu = reader->cu;
16043 struct objfile *objfile = cu->objfile;
16044 struct partial_die_info *part_die;
16045 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
16046 struct abbrev_info *abbrev;
16047 unsigned int bytes_read;
16048 unsigned int load_all = 0;
16049 int nesting_level = 1;
16054 gdb_assert (cu->per_cu != NULL);
16055 if (cu->per_cu->load_all_dies)
16059 = htab_create_alloc_ex (cu->header.length / 12,
16063 &cu->comp_unit_obstack,
16064 hashtab_obstack_allocate,
16065 dummy_obstack_deallocate);
16067 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16071 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
16073 /* A NULL abbrev means the end of a series of children. */
16074 if (abbrev == NULL)
16076 if (--nesting_level == 0)
16078 /* PART_DIE was probably the last thing allocated on the
16079 comp_unit_obstack, so we could call obstack_free
16080 here. We don't do that because the waste is small,
16081 and will be cleaned up when we're done with this
16082 compilation unit. This way, we're also more robust
16083 against other users of the comp_unit_obstack. */
16086 info_ptr += bytes_read;
16087 last_die = parent_die;
16088 parent_die = parent_die->die_parent;
16092 /* Check for template arguments. We never save these; if
16093 they're seen, we just mark the parent, and go on our way. */
16094 if (parent_die != NULL
16095 && cu->language == language_cplus
16096 && (abbrev->tag == DW_TAG_template_type_param
16097 || abbrev->tag == DW_TAG_template_value_param))
16099 parent_die->has_template_arguments = 1;
16103 /* We don't need a partial DIE for the template argument. */
16104 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16109 /* We only recurse into c++ subprograms looking for template arguments.
16110 Skip their other children. */
16112 && cu->language == language_cplus
16113 && parent_die != NULL
16114 && parent_die->tag == DW_TAG_subprogram)
16116 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16120 /* Check whether this DIE is interesting enough to save. Normally
16121 we would not be interested in members here, but there may be
16122 later variables referencing them via DW_AT_specification (for
16123 static members). */
16125 && !is_type_tag_for_partial (abbrev->tag)
16126 && abbrev->tag != DW_TAG_constant
16127 && abbrev->tag != DW_TAG_enumerator
16128 && abbrev->tag != DW_TAG_subprogram
16129 && abbrev->tag != DW_TAG_lexical_block
16130 && abbrev->tag != DW_TAG_variable
16131 && abbrev->tag != DW_TAG_namespace
16132 && abbrev->tag != DW_TAG_module
16133 && abbrev->tag != DW_TAG_member
16134 && abbrev->tag != DW_TAG_imported_unit
16135 && abbrev->tag != DW_TAG_imported_declaration)
16137 /* Otherwise we skip to the next sibling, if any. */
16138 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
16142 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
16145 /* This two-pass algorithm for processing partial symbols has a
16146 high cost in cache pressure. Thus, handle some simple cases
16147 here which cover the majority of C partial symbols. DIEs
16148 which neither have specification tags in them, nor could have
16149 specification tags elsewhere pointing at them, can simply be
16150 processed and discarded.
16152 This segment is also optional; scan_partial_symbols and
16153 add_partial_symbol will handle these DIEs if we chain
16154 them in normally. When compilers which do not emit large
16155 quantities of duplicate debug information are more common,
16156 this code can probably be removed. */
16158 /* Any complete simple types at the top level (pretty much all
16159 of them, for a language without namespaces), can be processed
16161 if (parent_die == NULL
16162 && part_die->has_specification == 0
16163 && part_die->is_declaration == 0
16164 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
16165 || part_die->tag == DW_TAG_base_type
16166 || part_die->tag == DW_TAG_subrange_type))
16168 if (building_psymtab && part_die->name != NULL)
16169 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16170 VAR_DOMAIN, LOC_TYPEDEF,
16171 &objfile->static_psymbols,
16172 0, cu->language, objfile);
16173 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16177 /* The exception for DW_TAG_typedef with has_children above is
16178 a workaround of GCC PR debug/47510. In the case of this complaint
16179 type_name_no_tag_or_error will error on such types later.
16181 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16182 it could not find the child DIEs referenced later, this is checked
16183 above. In correct DWARF DW_TAG_typedef should have no children. */
16185 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16186 complaint (&symfile_complaints,
16187 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16188 "- DIE at 0x%x [in module %s]"),
16189 to_underlying (part_die->sect_off), objfile_name (objfile));
16191 /* If we're at the second level, and we're an enumerator, and
16192 our parent has no specification (meaning possibly lives in a
16193 namespace elsewhere), then we can add the partial symbol now
16194 instead of queueing it. */
16195 if (part_die->tag == DW_TAG_enumerator
16196 && parent_die != NULL
16197 && parent_die->die_parent == NULL
16198 && parent_die->tag == DW_TAG_enumeration_type
16199 && parent_die->has_specification == 0)
16201 if (part_die->name == NULL)
16202 complaint (&symfile_complaints,
16203 _("malformed enumerator DIE ignored"));
16204 else if (building_psymtab)
16205 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16206 VAR_DOMAIN, LOC_CONST,
16207 cu->language == language_cplus
16208 ? &objfile->global_psymbols
16209 : &objfile->static_psymbols,
16210 0, cu->language, objfile);
16212 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16216 /* We'll save this DIE so link it in. */
16217 part_die->die_parent = parent_die;
16218 part_die->die_sibling = NULL;
16219 part_die->die_child = NULL;
16221 if (last_die && last_die == parent_die)
16222 last_die->die_child = part_die;
16224 last_die->die_sibling = part_die;
16226 last_die = part_die;
16228 if (first_die == NULL)
16229 first_die = part_die;
16231 /* Maybe add the DIE to the hash table. Not all DIEs that we
16232 find interesting need to be in the hash table, because we
16233 also have the parent/sibling/child chains; only those that we
16234 might refer to by offset later during partial symbol reading.
16236 For now this means things that might have be the target of a
16237 DW_AT_specification, DW_AT_abstract_origin, or
16238 DW_AT_extension. DW_AT_extension will refer only to
16239 namespaces; DW_AT_abstract_origin refers to functions (and
16240 many things under the function DIE, but we do not recurse
16241 into function DIEs during partial symbol reading) and
16242 possibly variables as well; DW_AT_specification refers to
16243 declarations. Declarations ought to have the DW_AT_declaration
16244 flag. It happens that GCC forgets to put it in sometimes, but
16245 only for functions, not for types.
16247 Adding more things than necessary to the hash table is harmless
16248 except for the performance cost. Adding too few will result in
16249 wasted time in find_partial_die, when we reread the compilation
16250 unit with load_all_dies set. */
16253 || abbrev->tag == DW_TAG_constant
16254 || abbrev->tag == DW_TAG_subprogram
16255 || abbrev->tag == DW_TAG_variable
16256 || abbrev->tag == DW_TAG_namespace
16257 || part_die->is_declaration)
16261 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16262 to_underlying (part_die->sect_off),
16267 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16269 /* For some DIEs we want to follow their children (if any). For C
16270 we have no reason to follow the children of structures; for other
16271 languages we have to, so that we can get at method physnames
16272 to infer fully qualified class names, for DW_AT_specification,
16273 and for C++ template arguments. For C++, we also look one level
16274 inside functions to find template arguments (if the name of the
16275 function does not already contain the template arguments).
16277 For Ada, we need to scan the children of subprograms and lexical
16278 blocks as well because Ada allows the definition of nested
16279 entities that could be interesting for the debugger, such as
16280 nested subprograms for instance. */
16281 if (last_die->has_children
16283 || last_die->tag == DW_TAG_namespace
16284 || last_die->tag == DW_TAG_module
16285 || last_die->tag == DW_TAG_enumeration_type
16286 || (cu->language == language_cplus
16287 && last_die->tag == DW_TAG_subprogram
16288 && (last_die->name == NULL
16289 || strchr (last_die->name, '<') == NULL))
16290 || (cu->language != language_c
16291 && (last_die->tag == DW_TAG_class_type
16292 || last_die->tag == DW_TAG_interface_type
16293 || last_die->tag == DW_TAG_structure_type
16294 || last_die->tag == DW_TAG_union_type))
16295 || (cu->language == language_ada
16296 && (last_die->tag == DW_TAG_subprogram
16297 || last_die->tag == DW_TAG_lexical_block))))
16300 parent_die = last_die;
16304 /* Otherwise we skip to the next sibling, if any. */
16305 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16307 /* Back to the top, do it again. */
16311 /* Read a minimal amount of information into the minimal die structure. */
16313 static const gdb_byte *
16314 read_partial_die (const struct die_reader_specs *reader,
16315 struct partial_die_info *part_die,
16316 struct abbrev_info *abbrev, unsigned int abbrev_len,
16317 const gdb_byte *info_ptr)
16319 struct dwarf2_cu *cu = reader->cu;
16320 struct objfile *objfile = cu->objfile;
16321 const gdb_byte *buffer = reader->buffer;
16323 struct attribute attr;
16324 int has_low_pc_attr = 0;
16325 int has_high_pc_attr = 0;
16326 int high_pc_relative = 0;
16328 memset (part_die, 0, sizeof (struct partial_die_info));
16330 part_die->sect_off = (sect_offset) (info_ptr - buffer);
16332 info_ptr += abbrev_len;
16334 if (abbrev == NULL)
16337 part_die->tag = abbrev->tag;
16338 part_die->has_children = abbrev->has_children;
16340 for (i = 0; i < abbrev->num_attrs; ++i)
16342 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16344 /* Store the data if it is of an attribute we want to keep in a
16345 partial symbol table. */
16349 switch (part_die->tag)
16351 case DW_TAG_compile_unit:
16352 case DW_TAG_partial_unit:
16353 case DW_TAG_type_unit:
16354 /* Compilation units have a DW_AT_name that is a filename, not
16355 a source language identifier. */
16356 case DW_TAG_enumeration_type:
16357 case DW_TAG_enumerator:
16358 /* These tags always have simple identifiers already; no need
16359 to canonicalize them. */
16360 part_die->name = DW_STRING (&attr);
16364 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16365 &objfile->per_bfd->storage_obstack);
16369 case DW_AT_linkage_name:
16370 case DW_AT_MIPS_linkage_name:
16371 /* Note that both forms of linkage name might appear. We
16372 assume they will be the same, and we only store the last
16374 if (cu->language == language_ada)
16375 part_die->name = DW_STRING (&attr);
16376 part_die->linkage_name = DW_STRING (&attr);
16379 has_low_pc_attr = 1;
16380 part_die->lowpc = attr_value_as_address (&attr);
16382 case DW_AT_high_pc:
16383 has_high_pc_attr = 1;
16384 part_die->highpc = attr_value_as_address (&attr);
16385 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16386 high_pc_relative = 1;
16388 case DW_AT_location:
16389 /* Support the .debug_loc offsets. */
16390 if (attr_form_is_block (&attr))
16392 part_die->d.locdesc = DW_BLOCK (&attr);
16394 else if (attr_form_is_section_offset (&attr))
16396 dwarf2_complex_location_expr_complaint ();
16400 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16401 "partial symbol information");
16404 case DW_AT_external:
16405 part_die->is_external = DW_UNSND (&attr);
16407 case DW_AT_declaration:
16408 part_die->is_declaration = DW_UNSND (&attr);
16411 part_die->has_type = 1;
16413 case DW_AT_abstract_origin:
16414 case DW_AT_specification:
16415 case DW_AT_extension:
16416 part_die->has_specification = 1;
16417 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16418 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16419 || cu->per_cu->is_dwz);
16421 case DW_AT_sibling:
16422 /* Ignore absolute siblings, they might point outside of
16423 the current compile unit. */
16424 if (attr.form == DW_FORM_ref_addr)
16425 complaint (&symfile_complaints,
16426 _("ignoring absolute DW_AT_sibling"));
16429 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16430 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
16432 if (sibling_ptr < info_ptr)
16433 complaint (&symfile_complaints,
16434 _("DW_AT_sibling points backwards"));
16435 else if (sibling_ptr > reader->buffer_end)
16436 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16438 part_die->sibling = sibling_ptr;
16441 case DW_AT_byte_size:
16442 part_die->has_byte_size = 1;
16444 case DW_AT_const_value:
16445 part_die->has_const_value = 1;
16447 case DW_AT_calling_convention:
16448 /* DWARF doesn't provide a way to identify a program's source-level
16449 entry point. DW_AT_calling_convention attributes are only meant
16450 to describe functions' calling conventions.
16452 However, because it's a necessary piece of information in
16453 Fortran, and before DWARF 4 DW_CC_program was the only
16454 piece of debugging information whose definition refers to
16455 a 'main program' at all, several compilers marked Fortran
16456 main programs with DW_CC_program --- even when those
16457 functions use the standard calling conventions.
16459 Although DWARF now specifies a way to provide this
16460 information, we support this practice for backward
16462 if (DW_UNSND (&attr) == DW_CC_program
16463 && cu->language == language_fortran)
16464 part_die->main_subprogram = 1;
16467 if (DW_UNSND (&attr) == DW_INL_inlined
16468 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16469 part_die->may_be_inlined = 1;
16473 if (part_die->tag == DW_TAG_imported_unit)
16475 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
16476 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16477 || cu->per_cu->is_dwz);
16481 case DW_AT_main_subprogram:
16482 part_die->main_subprogram = DW_UNSND (&attr);
16490 if (high_pc_relative)
16491 part_die->highpc += part_die->lowpc;
16493 if (has_low_pc_attr && has_high_pc_attr)
16495 /* When using the GNU linker, .gnu.linkonce. sections are used to
16496 eliminate duplicate copies of functions and vtables and such.
16497 The linker will arbitrarily choose one and discard the others.
16498 The AT_*_pc values for such functions refer to local labels in
16499 these sections. If the section from that file was discarded, the
16500 labels are not in the output, so the relocs get a value of 0.
16501 If this is a discarded function, mark the pc bounds as invalid,
16502 so that GDB will ignore it. */
16503 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16505 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16507 complaint (&symfile_complaints,
16508 _("DW_AT_low_pc %s is zero "
16509 "for DIE at 0x%x [in module %s]"),
16510 paddress (gdbarch, part_die->lowpc),
16511 to_underlying (part_die->sect_off), objfile_name (objfile));
16513 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16514 else if (part_die->lowpc >= part_die->highpc)
16516 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16518 complaint (&symfile_complaints,
16519 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16520 "for DIE at 0x%x [in module %s]"),
16521 paddress (gdbarch, part_die->lowpc),
16522 paddress (gdbarch, part_die->highpc),
16523 to_underlying (part_die->sect_off),
16524 objfile_name (objfile));
16527 part_die->has_pc_info = 1;
16533 /* Find a cached partial DIE at OFFSET in CU. */
16535 static struct partial_die_info *
16536 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
16538 struct partial_die_info *lookup_die = NULL;
16539 struct partial_die_info part_die;
16541 part_die.sect_off = sect_off;
16542 lookup_die = ((struct partial_die_info *)
16543 htab_find_with_hash (cu->partial_dies, &part_die,
16544 to_underlying (sect_off)));
16549 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16550 except in the case of .debug_types DIEs which do not reference
16551 outside their CU (they do however referencing other types via
16552 DW_FORM_ref_sig8). */
16554 static struct partial_die_info *
16555 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
16557 struct objfile *objfile = cu->objfile;
16558 struct dwarf2_per_cu_data *per_cu = NULL;
16559 struct partial_die_info *pd = NULL;
16561 if (offset_in_dwz == cu->per_cu->is_dwz
16562 && offset_in_cu_p (&cu->header, sect_off))
16564 pd = find_partial_die_in_comp_unit (sect_off, cu);
16567 /* We missed recording what we needed.
16568 Load all dies and try again. */
16569 per_cu = cu->per_cu;
16573 /* TUs don't reference other CUs/TUs (except via type signatures). */
16574 if (cu->per_cu->is_debug_types)
16576 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16577 " external reference to offset 0x%x [in module %s].\n"),
16578 to_underlying (cu->header.sect_off), to_underlying (sect_off),
16579 bfd_get_filename (objfile->obfd));
16581 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
16584 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16585 load_partial_comp_unit (per_cu);
16587 per_cu->cu->last_used = 0;
16588 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16591 /* If we didn't find it, and not all dies have been loaded,
16592 load them all and try again. */
16594 if (pd == NULL && per_cu->load_all_dies == 0)
16596 per_cu->load_all_dies = 1;
16598 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16599 THIS_CU->cu may already be in use. So we can't just free it and
16600 replace its DIEs with the ones we read in. Instead, we leave those
16601 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16602 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16604 load_partial_comp_unit (per_cu);
16606 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
16610 internal_error (__FILE__, __LINE__,
16611 _("could not find partial DIE 0x%x "
16612 "in cache [from module %s]\n"),
16613 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
16617 /* See if we can figure out if the class lives in a namespace. We do
16618 this by looking for a member function; its demangled name will
16619 contain namespace info, if there is any. */
16622 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16623 struct dwarf2_cu *cu)
16625 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16626 what template types look like, because the demangler
16627 frequently doesn't give the same name as the debug info. We
16628 could fix this by only using the demangled name to get the
16629 prefix (but see comment in read_structure_type). */
16631 struct partial_die_info *real_pdi;
16632 struct partial_die_info *child_pdi;
16634 /* If this DIE (this DIE's specification, if any) has a parent, then
16635 we should not do this. We'll prepend the parent's fully qualified
16636 name when we create the partial symbol. */
16638 real_pdi = struct_pdi;
16639 while (real_pdi->has_specification)
16640 real_pdi = find_partial_die (real_pdi->spec_offset,
16641 real_pdi->spec_is_dwz, cu);
16643 if (real_pdi->die_parent != NULL)
16646 for (child_pdi = struct_pdi->die_child;
16648 child_pdi = child_pdi->die_sibling)
16650 if (child_pdi->tag == DW_TAG_subprogram
16651 && child_pdi->linkage_name != NULL)
16653 char *actual_class_name
16654 = language_class_name_from_physname (cu->language_defn,
16655 child_pdi->linkage_name);
16656 if (actual_class_name != NULL)
16660 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16662 strlen (actual_class_name)));
16663 xfree (actual_class_name);
16670 /* Adjust PART_DIE before generating a symbol for it. This function
16671 may set the is_external flag or change the DIE's name. */
16674 fixup_partial_die (struct partial_die_info *part_die,
16675 struct dwarf2_cu *cu)
16677 /* Once we've fixed up a die, there's no point in doing so again.
16678 This also avoids a memory leak if we were to call
16679 guess_partial_die_structure_name multiple times. */
16680 if (part_die->fixup_called)
16683 /* If we found a reference attribute and the DIE has no name, try
16684 to find a name in the referred to DIE. */
16686 if (part_die->name == NULL && part_die->has_specification)
16688 struct partial_die_info *spec_die;
16690 spec_die = find_partial_die (part_die->spec_offset,
16691 part_die->spec_is_dwz, cu);
16693 fixup_partial_die (spec_die, cu);
16695 if (spec_die->name)
16697 part_die->name = spec_die->name;
16699 /* Copy DW_AT_external attribute if it is set. */
16700 if (spec_die->is_external)
16701 part_die->is_external = spec_die->is_external;
16705 /* Set default names for some unnamed DIEs. */
16707 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16708 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16710 /* If there is no parent die to provide a namespace, and there are
16711 children, see if we can determine the namespace from their linkage
16713 if (cu->language == language_cplus
16714 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16715 && part_die->die_parent == NULL
16716 && part_die->has_children
16717 && (part_die->tag == DW_TAG_class_type
16718 || part_die->tag == DW_TAG_structure_type
16719 || part_die->tag == DW_TAG_union_type))
16720 guess_partial_die_structure_name (part_die, cu);
16722 /* GCC might emit a nameless struct or union that has a linkage
16723 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16724 if (part_die->name == NULL
16725 && (part_die->tag == DW_TAG_class_type
16726 || part_die->tag == DW_TAG_interface_type
16727 || part_die->tag == DW_TAG_structure_type
16728 || part_die->tag == DW_TAG_union_type)
16729 && part_die->linkage_name != NULL)
16733 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16738 /* Strip any leading namespaces/classes, keep only the base name.
16739 DW_AT_name for named DIEs does not contain the prefixes. */
16740 base = strrchr (demangled, ':');
16741 if (base && base > demangled && base[-1] == ':')
16748 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16749 base, strlen (base)));
16754 part_die->fixup_called = 1;
16757 /* Read an attribute value described by an attribute form. */
16759 static const gdb_byte *
16760 read_attribute_value (const struct die_reader_specs *reader,
16761 struct attribute *attr, unsigned form,
16762 LONGEST implicit_const, const gdb_byte *info_ptr)
16764 struct dwarf2_cu *cu = reader->cu;
16765 struct objfile *objfile = cu->objfile;
16766 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16767 bfd *abfd = reader->abfd;
16768 struct comp_unit_head *cu_header = &cu->header;
16769 unsigned int bytes_read;
16770 struct dwarf_block *blk;
16772 attr->form = (enum dwarf_form) form;
16775 case DW_FORM_ref_addr:
16776 if (cu->header.version == 2)
16777 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16779 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16780 &cu->header, &bytes_read);
16781 info_ptr += bytes_read;
16783 case DW_FORM_GNU_ref_alt:
16784 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16785 info_ptr += bytes_read;
16788 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16789 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16790 info_ptr += bytes_read;
16792 case DW_FORM_block2:
16793 blk = dwarf_alloc_block (cu);
16794 blk->size = read_2_bytes (abfd, info_ptr);
16796 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16797 info_ptr += blk->size;
16798 DW_BLOCK (attr) = blk;
16800 case DW_FORM_block4:
16801 blk = dwarf_alloc_block (cu);
16802 blk->size = read_4_bytes (abfd, info_ptr);
16804 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16805 info_ptr += blk->size;
16806 DW_BLOCK (attr) = blk;
16808 case DW_FORM_data2:
16809 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16812 case DW_FORM_data4:
16813 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16816 case DW_FORM_data8:
16817 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16820 case DW_FORM_data16:
16821 blk = dwarf_alloc_block (cu);
16823 blk->data = read_n_bytes (abfd, info_ptr, 16);
16825 DW_BLOCK (attr) = blk;
16827 case DW_FORM_sec_offset:
16828 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16829 info_ptr += bytes_read;
16831 case DW_FORM_string:
16832 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16833 DW_STRING_IS_CANONICAL (attr) = 0;
16834 info_ptr += bytes_read;
16837 if (!cu->per_cu->is_dwz)
16839 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16841 DW_STRING_IS_CANONICAL (attr) = 0;
16842 info_ptr += bytes_read;
16846 case DW_FORM_line_strp:
16847 if (!cu->per_cu->is_dwz)
16849 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16850 cu_header, &bytes_read);
16851 DW_STRING_IS_CANONICAL (attr) = 0;
16852 info_ptr += bytes_read;
16856 case DW_FORM_GNU_strp_alt:
16858 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16859 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16862 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16863 DW_STRING_IS_CANONICAL (attr) = 0;
16864 info_ptr += bytes_read;
16867 case DW_FORM_exprloc:
16868 case DW_FORM_block:
16869 blk = dwarf_alloc_block (cu);
16870 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16871 info_ptr += bytes_read;
16872 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16873 info_ptr += blk->size;
16874 DW_BLOCK (attr) = blk;
16876 case DW_FORM_block1:
16877 blk = dwarf_alloc_block (cu);
16878 blk->size = read_1_byte (abfd, info_ptr);
16880 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16881 info_ptr += blk->size;
16882 DW_BLOCK (attr) = blk;
16884 case DW_FORM_data1:
16885 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16889 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16892 case DW_FORM_flag_present:
16893 DW_UNSND (attr) = 1;
16895 case DW_FORM_sdata:
16896 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16897 info_ptr += bytes_read;
16899 case DW_FORM_udata:
16900 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16901 info_ptr += bytes_read;
16904 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16905 + read_1_byte (abfd, info_ptr));
16909 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16910 + read_2_bytes (abfd, info_ptr));
16914 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16915 + read_4_bytes (abfd, info_ptr));
16919 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16920 + read_8_bytes (abfd, info_ptr));
16923 case DW_FORM_ref_sig8:
16924 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16927 case DW_FORM_ref_udata:
16928 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
16929 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16930 info_ptr += bytes_read;
16932 case DW_FORM_indirect:
16933 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16934 info_ptr += bytes_read;
16935 if (form == DW_FORM_implicit_const)
16937 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16938 info_ptr += bytes_read;
16940 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16943 case DW_FORM_implicit_const:
16944 DW_SND (attr) = implicit_const;
16946 case DW_FORM_GNU_addr_index:
16947 if (reader->dwo_file == NULL)
16949 /* For now flag a hard error.
16950 Later we can turn this into a complaint. */
16951 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16952 dwarf_form_name (form),
16953 bfd_get_filename (abfd));
16955 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16956 info_ptr += bytes_read;
16958 case DW_FORM_GNU_str_index:
16959 if (reader->dwo_file == NULL)
16961 /* For now flag a hard error.
16962 Later we can turn this into a complaint if warranted. */
16963 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16964 dwarf_form_name (form),
16965 bfd_get_filename (abfd));
16968 ULONGEST str_index =
16969 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16971 DW_STRING (attr) = read_str_index (reader, str_index);
16972 DW_STRING_IS_CANONICAL (attr) = 0;
16973 info_ptr += bytes_read;
16977 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16978 dwarf_form_name (form),
16979 bfd_get_filename (abfd));
16983 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16984 attr->form = DW_FORM_GNU_ref_alt;
16986 /* We have seen instances where the compiler tried to emit a byte
16987 size attribute of -1 which ended up being encoded as an unsigned
16988 0xffffffff. Although 0xffffffff is technically a valid size value,
16989 an object of this size seems pretty unlikely so we can relatively
16990 safely treat these cases as if the size attribute was invalid and
16991 treat them as zero by default. */
16992 if (attr->name == DW_AT_byte_size
16993 && form == DW_FORM_data4
16994 && DW_UNSND (attr) >= 0xffffffff)
16997 (&symfile_complaints,
16998 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16999 hex_string (DW_UNSND (attr)));
17000 DW_UNSND (attr) = 0;
17006 /* Read an attribute described by an abbreviated attribute. */
17008 static const gdb_byte *
17009 read_attribute (const struct die_reader_specs *reader,
17010 struct attribute *attr, struct attr_abbrev *abbrev,
17011 const gdb_byte *info_ptr)
17013 attr->name = abbrev->name;
17014 return read_attribute_value (reader, attr, abbrev->form,
17015 abbrev->implicit_const, info_ptr);
17018 /* Read dwarf information from a buffer. */
17020 static unsigned int
17021 read_1_byte (bfd *abfd, const gdb_byte *buf)
17023 return bfd_get_8 (abfd, buf);
17027 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
17029 return bfd_get_signed_8 (abfd, buf);
17032 static unsigned int
17033 read_2_bytes (bfd *abfd, const gdb_byte *buf)
17035 return bfd_get_16 (abfd, buf);
17039 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
17041 return bfd_get_signed_16 (abfd, buf);
17044 static unsigned int
17045 read_4_bytes (bfd *abfd, const gdb_byte *buf)
17047 return bfd_get_32 (abfd, buf);
17051 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
17053 return bfd_get_signed_32 (abfd, buf);
17057 read_8_bytes (bfd *abfd, const gdb_byte *buf)
17059 return bfd_get_64 (abfd, buf);
17063 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
17064 unsigned int *bytes_read)
17066 struct comp_unit_head *cu_header = &cu->header;
17067 CORE_ADDR retval = 0;
17069 if (cu_header->signed_addr_p)
17071 switch (cu_header->addr_size)
17074 retval = bfd_get_signed_16 (abfd, buf);
17077 retval = bfd_get_signed_32 (abfd, buf);
17080 retval = bfd_get_signed_64 (abfd, buf);
17083 internal_error (__FILE__, __LINE__,
17084 _("read_address: bad switch, signed [in module %s]"),
17085 bfd_get_filename (abfd));
17090 switch (cu_header->addr_size)
17093 retval = bfd_get_16 (abfd, buf);
17096 retval = bfd_get_32 (abfd, buf);
17099 retval = bfd_get_64 (abfd, buf);
17102 internal_error (__FILE__, __LINE__,
17103 _("read_address: bad switch, "
17104 "unsigned [in module %s]"),
17105 bfd_get_filename (abfd));
17109 *bytes_read = cu_header->addr_size;
17113 /* Read the initial length from a section. The (draft) DWARF 3
17114 specification allows the initial length to take up either 4 bytes
17115 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
17116 bytes describe the length and all offsets will be 8 bytes in length
17119 An older, non-standard 64-bit format is also handled by this
17120 function. The older format in question stores the initial length
17121 as an 8-byte quantity without an escape value. Lengths greater
17122 than 2^32 aren't very common which means that the initial 4 bytes
17123 is almost always zero. Since a length value of zero doesn't make
17124 sense for the 32-bit format, this initial zero can be considered to
17125 be an escape value which indicates the presence of the older 64-bit
17126 format. As written, the code can't detect (old format) lengths
17127 greater than 4GB. If it becomes necessary to handle lengths
17128 somewhat larger than 4GB, we could allow other small values (such
17129 as the non-sensical values of 1, 2, and 3) to also be used as
17130 escape values indicating the presence of the old format.
17132 The value returned via bytes_read should be used to increment the
17133 relevant pointer after calling read_initial_length().
17135 [ Note: read_initial_length() and read_offset() are based on the
17136 document entitled "DWARF Debugging Information Format", revision
17137 3, draft 8, dated November 19, 2001. This document was obtained
17140 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
17142 This document is only a draft and is subject to change. (So beware.)
17144 Details regarding the older, non-standard 64-bit format were
17145 determined empirically by examining 64-bit ELF files produced by
17146 the SGI toolchain on an IRIX 6.5 machine.
17148 - Kevin, July 16, 2002
17152 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
17154 LONGEST length = bfd_get_32 (abfd, buf);
17156 if (length == 0xffffffff)
17158 length = bfd_get_64 (abfd, buf + 4);
17161 else if (length == 0)
17163 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
17164 length = bfd_get_64 (abfd, buf);
17175 /* Cover function for read_initial_length.
17176 Returns the length of the object at BUF, and stores the size of the
17177 initial length in *BYTES_READ and stores the size that offsets will be in
17179 If the initial length size is not equivalent to that specified in
17180 CU_HEADER then issue a complaint.
17181 This is useful when reading non-comp-unit headers. */
17184 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
17185 const struct comp_unit_head *cu_header,
17186 unsigned int *bytes_read,
17187 unsigned int *offset_size)
17189 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17191 gdb_assert (cu_header->initial_length_size == 4
17192 || cu_header->initial_length_size == 8
17193 || cu_header->initial_length_size == 12);
17195 if (cu_header->initial_length_size != *bytes_read)
17196 complaint (&symfile_complaints,
17197 _("intermixed 32-bit and 64-bit DWARF sections"));
17199 *offset_size = (*bytes_read == 4) ? 4 : 8;
17203 /* Read an offset from the data stream. The size of the offset is
17204 given by cu_header->offset_size. */
17207 read_offset (bfd *abfd, const gdb_byte *buf,
17208 const struct comp_unit_head *cu_header,
17209 unsigned int *bytes_read)
17211 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17213 *bytes_read = cu_header->offset_size;
17217 /* Read an offset from the data stream. */
17220 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17222 LONGEST retval = 0;
17224 switch (offset_size)
17227 retval = bfd_get_32 (abfd, buf);
17230 retval = bfd_get_64 (abfd, buf);
17233 internal_error (__FILE__, __LINE__,
17234 _("read_offset_1: bad switch [in module %s]"),
17235 bfd_get_filename (abfd));
17241 static const gdb_byte *
17242 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17244 /* If the size of a host char is 8 bits, we can return a pointer
17245 to the buffer, otherwise we have to copy the data to a buffer
17246 allocated on the temporary obstack. */
17247 gdb_assert (HOST_CHAR_BIT == 8);
17251 static const char *
17252 read_direct_string (bfd *abfd, const gdb_byte *buf,
17253 unsigned int *bytes_read_ptr)
17255 /* If the size of a host char is 8 bits, we can return a pointer
17256 to the string, otherwise we have to copy the string to a buffer
17257 allocated on the temporary obstack. */
17258 gdb_assert (HOST_CHAR_BIT == 8);
17261 *bytes_read_ptr = 1;
17264 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17265 return (const char *) buf;
17268 /* Return pointer to string at section SECT offset STR_OFFSET with error
17269 reporting strings FORM_NAME and SECT_NAME. */
17271 static const char *
17272 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17273 struct dwarf2_section_info *sect,
17274 const char *form_name,
17275 const char *sect_name)
17277 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17278 if (sect->buffer == NULL)
17279 error (_("%s used without %s section [in module %s]"),
17280 form_name, sect_name, bfd_get_filename (abfd));
17281 if (str_offset >= sect->size)
17282 error (_("%s pointing outside of %s section [in module %s]"),
17283 form_name, sect_name, bfd_get_filename (abfd));
17284 gdb_assert (HOST_CHAR_BIT == 8);
17285 if (sect->buffer[str_offset] == '\0')
17287 return (const char *) (sect->buffer + str_offset);
17290 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17292 static const char *
17293 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17295 return read_indirect_string_at_offset_from (abfd, str_offset,
17296 &dwarf2_per_objfile->str,
17297 "DW_FORM_strp", ".debug_str");
17300 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17302 static const char *
17303 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17305 return read_indirect_string_at_offset_from (abfd, str_offset,
17306 &dwarf2_per_objfile->line_str,
17307 "DW_FORM_line_strp",
17308 ".debug_line_str");
17311 /* Read a string at offset STR_OFFSET in the .debug_str section from
17312 the .dwz file DWZ. Throw an error if the offset is too large. If
17313 the string consists of a single NUL byte, return NULL; otherwise
17314 return a pointer to the string. */
17316 static const char *
17317 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17319 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17321 if (dwz->str.buffer == NULL)
17322 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17323 "section [in module %s]"),
17324 bfd_get_filename (dwz->dwz_bfd));
17325 if (str_offset >= dwz->str.size)
17326 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17327 ".debug_str section [in module %s]"),
17328 bfd_get_filename (dwz->dwz_bfd));
17329 gdb_assert (HOST_CHAR_BIT == 8);
17330 if (dwz->str.buffer[str_offset] == '\0')
17332 return (const char *) (dwz->str.buffer + str_offset);
17335 /* Return pointer to string at .debug_str offset as read from BUF.
17336 BUF is assumed to be in a compilation unit described by CU_HEADER.
17337 Return *BYTES_READ_PTR count of bytes read from BUF. */
17339 static const char *
17340 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17341 const struct comp_unit_head *cu_header,
17342 unsigned int *bytes_read_ptr)
17344 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17346 return read_indirect_string_at_offset (abfd, str_offset);
17349 /* Return pointer to string at .debug_line_str offset as read from BUF.
17350 BUF is assumed to be in a compilation unit described by CU_HEADER.
17351 Return *BYTES_READ_PTR count of bytes read from BUF. */
17353 static const char *
17354 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17355 const struct comp_unit_head *cu_header,
17356 unsigned int *bytes_read_ptr)
17358 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17360 return read_indirect_line_string_at_offset (abfd, str_offset);
17364 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17365 unsigned int *bytes_read_ptr)
17368 unsigned int num_read;
17370 unsigned char byte;
17377 byte = bfd_get_8 (abfd, buf);
17380 result |= ((ULONGEST) (byte & 127) << shift);
17381 if ((byte & 128) == 0)
17387 *bytes_read_ptr = num_read;
17392 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17393 unsigned int *bytes_read_ptr)
17396 int shift, num_read;
17397 unsigned char byte;
17404 byte = bfd_get_8 (abfd, buf);
17407 result |= ((LONGEST) (byte & 127) << shift);
17409 if ((byte & 128) == 0)
17414 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17415 result |= -(((LONGEST) 1) << shift);
17416 *bytes_read_ptr = num_read;
17420 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17421 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17422 ADDR_SIZE is the size of addresses from the CU header. */
17425 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17427 struct objfile *objfile = dwarf2_per_objfile->objfile;
17428 bfd *abfd = objfile->obfd;
17429 const gdb_byte *info_ptr;
17431 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17432 if (dwarf2_per_objfile->addr.buffer == NULL)
17433 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17434 objfile_name (objfile));
17435 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17436 error (_("DW_FORM_addr_index pointing outside of "
17437 ".debug_addr section [in module %s]"),
17438 objfile_name (objfile));
17439 info_ptr = (dwarf2_per_objfile->addr.buffer
17440 + addr_base + addr_index * addr_size);
17441 if (addr_size == 4)
17442 return bfd_get_32 (abfd, info_ptr);
17444 return bfd_get_64 (abfd, info_ptr);
17447 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17450 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17452 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17455 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17458 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17459 unsigned int *bytes_read)
17461 bfd *abfd = cu->objfile->obfd;
17462 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17464 return read_addr_index (cu, addr_index);
17467 /* Data structure to pass results from dwarf2_read_addr_index_reader
17468 back to dwarf2_read_addr_index. */
17470 struct dwarf2_read_addr_index_data
17472 ULONGEST addr_base;
17476 /* die_reader_func for dwarf2_read_addr_index. */
17479 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17480 const gdb_byte *info_ptr,
17481 struct die_info *comp_unit_die,
17485 struct dwarf2_cu *cu = reader->cu;
17486 struct dwarf2_read_addr_index_data *aidata =
17487 (struct dwarf2_read_addr_index_data *) data;
17489 aidata->addr_base = cu->addr_base;
17490 aidata->addr_size = cu->header.addr_size;
17493 /* Given an index in .debug_addr, fetch the value.
17494 NOTE: This can be called during dwarf expression evaluation,
17495 long after the debug information has been read, and thus per_cu->cu
17496 may no longer exist. */
17499 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17500 unsigned int addr_index)
17502 struct objfile *objfile = per_cu->objfile;
17503 struct dwarf2_cu *cu = per_cu->cu;
17504 ULONGEST addr_base;
17507 /* This is intended to be called from outside this file. */
17508 dw2_setup (objfile);
17510 /* We need addr_base and addr_size.
17511 If we don't have PER_CU->cu, we have to get it.
17512 Nasty, but the alternative is storing the needed info in PER_CU,
17513 which at this point doesn't seem justified: it's not clear how frequently
17514 it would get used and it would increase the size of every PER_CU.
17515 Entry points like dwarf2_per_cu_addr_size do a similar thing
17516 so we're not in uncharted territory here.
17517 Alas we need to be a bit more complicated as addr_base is contained
17520 We don't need to read the entire CU(/TU).
17521 We just need the header and top level die.
17523 IWBN to use the aging mechanism to let us lazily later discard the CU.
17524 For now we skip this optimization. */
17528 addr_base = cu->addr_base;
17529 addr_size = cu->header.addr_size;
17533 struct dwarf2_read_addr_index_data aidata;
17535 /* Note: We can't use init_cutu_and_read_dies_simple here,
17536 we need addr_base. */
17537 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17538 dwarf2_read_addr_index_reader, &aidata);
17539 addr_base = aidata.addr_base;
17540 addr_size = aidata.addr_size;
17543 return read_addr_index_1 (addr_index, addr_base, addr_size);
17546 /* Given a DW_FORM_GNU_str_index, fetch the string.
17547 This is only used by the Fission support. */
17549 static const char *
17550 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17552 struct objfile *objfile = dwarf2_per_objfile->objfile;
17553 const char *objf_name = objfile_name (objfile);
17554 bfd *abfd = objfile->obfd;
17555 struct dwarf2_cu *cu = reader->cu;
17556 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17557 struct dwarf2_section_info *str_offsets_section =
17558 &reader->dwo_file->sections.str_offsets;
17559 const gdb_byte *info_ptr;
17560 ULONGEST str_offset;
17561 static const char form_name[] = "DW_FORM_GNU_str_index";
17563 dwarf2_read_section (objfile, str_section);
17564 dwarf2_read_section (objfile, str_offsets_section);
17565 if (str_section->buffer == NULL)
17566 error (_("%s used without .debug_str.dwo section"
17567 " in CU at offset 0x%x [in module %s]"),
17568 form_name, to_underlying (cu->header.sect_off), objf_name);
17569 if (str_offsets_section->buffer == NULL)
17570 error (_("%s used without .debug_str_offsets.dwo section"
17571 " in CU at offset 0x%x [in module %s]"),
17572 form_name, to_underlying (cu->header.sect_off), objf_name);
17573 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17574 error (_("%s pointing outside of .debug_str_offsets.dwo"
17575 " section in CU at offset 0x%x [in module %s]"),
17576 form_name, to_underlying (cu->header.sect_off), objf_name);
17577 info_ptr = (str_offsets_section->buffer
17578 + str_index * cu->header.offset_size);
17579 if (cu->header.offset_size == 4)
17580 str_offset = bfd_get_32 (abfd, info_ptr);
17582 str_offset = bfd_get_64 (abfd, info_ptr);
17583 if (str_offset >= str_section->size)
17584 error (_("Offset from %s pointing outside of"
17585 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17586 form_name, to_underlying (cu->header.sect_off), objf_name);
17587 return (const char *) (str_section->buffer + str_offset);
17590 /* Return the length of an LEB128 number in BUF. */
17593 leb128_size (const gdb_byte *buf)
17595 const gdb_byte *begin = buf;
17601 if ((byte & 128) == 0)
17602 return buf - begin;
17607 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17616 cu->language = language_c;
17619 case DW_LANG_C_plus_plus:
17620 case DW_LANG_C_plus_plus_11:
17621 case DW_LANG_C_plus_plus_14:
17622 cu->language = language_cplus;
17625 cu->language = language_d;
17627 case DW_LANG_Fortran77:
17628 case DW_LANG_Fortran90:
17629 case DW_LANG_Fortran95:
17630 case DW_LANG_Fortran03:
17631 case DW_LANG_Fortran08:
17632 cu->language = language_fortran;
17635 cu->language = language_go;
17637 case DW_LANG_Mips_Assembler:
17638 cu->language = language_asm;
17640 case DW_LANG_Ada83:
17641 case DW_LANG_Ada95:
17642 cu->language = language_ada;
17644 case DW_LANG_Modula2:
17645 cu->language = language_m2;
17647 case DW_LANG_Pascal83:
17648 cu->language = language_pascal;
17651 cu->language = language_objc;
17654 case DW_LANG_Rust_old:
17655 cu->language = language_rust;
17657 case DW_LANG_Cobol74:
17658 case DW_LANG_Cobol85:
17660 cu->language = language_minimal;
17663 cu->language_defn = language_def (cu->language);
17666 /* Return the named attribute or NULL if not there. */
17668 static struct attribute *
17669 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17674 struct attribute *spec = NULL;
17676 for (i = 0; i < die->num_attrs; ++i)
17678 if (die->attrs[i].name == name)
17679 return &die->attrs[i];
17680 if (die->attrs[i].name == DW_AT_specification
17681 || die->attrs[i].name == DW_AT_abstract_origin)
17682 spec = &die->attrs[i];
17688 die = follow_die_ref (die, spec, &cu);
17694 /* Return the named attribute or NULL if not there,
17695 but do not follow DW_AT_specification, etc.
17696 This is for use in contexts where we're reading .debug_types dies.
17697 Following DW_AT_specification, DW_AT_abstract_origin will take us
17698 back up the chain, and we want to go down. */
17700 static struct attribute *
17701 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17705 for (i = 0; i < die->num_attrs; ++i)
17706 if (die->attrs[i].name == name)
17707 return &die->attrs[i];
17712 /* Return the string associated with a string-typed attribute, or NULL if it
17713 is either not found or is of an incorrect type. */
17715 static const char *
17716 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17718 struct attribute *attr;
17719 const char *str = NULL;
17721 attr = dwarf2_attr (die, name, cu);
17725 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17726 || attr->form == DW_FORM_string
17727 || attr->form == DW_FORM_GNU_str_index
17728 || attr->form == DW_FORM_GNU_strp_alt)
17729 str = DW_STRING (attr);
17731 complaint (&symfile_complaints,
17732 _("string type expected for attribute %s for "
17733 "DIE at 0x%x in module %s"),
17734 dwarf_attr_name (name), to_underlying (die->sect_off),
17735 objfile_name (cu->objfile));
17741 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17742 and holds a non-zero value. This function should only be used for
17743 DW_FORM_flag or DW_FORM_flag_present attributes. */
17746 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17748 struct attribute *attr = dwarf2_attr (die, name, cu);
17750 return (attr && DW_UNSND (attr));
17754 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17756 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17757 which value is non-zero. However, we have to be careful with
17758 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17759 (via dwarf2_flag_true_p) follows this attribute. So we may
17760 end up accidently finding a declaration attribute that belongs
17761 to a different DIE referenced by the specification attribute,
17762 even though the given DIE does not have a declaration attribute. */
17763 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17764 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17767 /* Return the die giving the specification for DIE, if there is
17768 one. *SPEC_CU is the CU containing DIE on input, and the CU
17769 containing the return value on output. If there is no
17770 specification, but there is an abstract origin, that is
17773 static struct die_info *
17774 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17776 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17779 if (spec_attr == NULL)
17780 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17782 if (spec_attr == NULL)
17785 return follow_die_ref (die, spec_attr, spec_cu);
17788 /* Stub for free_line_header to match void * callback types. */
17791 free_line_header_voidp (void *arg)
17793 struct line_header *lh = (struct line_header *) arg;
17799 line_header::add_include_dir (const char *include_dir)
17801 if (dwarf_line_debug >= 2)
17802 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17803 include_dirs.size () + 1, include_dir);
17805 include_dirs.push_back (include_dir);
17809 line_header::add_file_name (const char *name,
17811 unsigned int mod_time,
17812 unsigned int length)
17814 if (dwarf_line_debug >= 2)
17815 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17816 (unsigned) file_names.size () + 1, name);
17818 file_names.emplace_back (name, d_index, mod_time, length);
17821 /* A convenience function to find the proper .debug_line section for a CU. */
17823 static struct dwarf2_section_info *
17824 get_debug_line_section (struct dwarf2_cu *cu)
17826 struct dwarf2_section_info *section;
17828 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17830 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17831 section = &cu->dwo_unit->dwo_file->sections.line;
17832 else if (cu->per_cu->is_dwz)
17834 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17836 section = &dwz->line;
17839 section = &dwarf2_per_objfile->line;
17844 /* Read directory or file name entry format, starting with byte of
17845 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17846 entries count and the entries themselves in the described entry
17850 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17851 struct line_header *lh,
17852 const struct comp_unit_head *cu_header,
17853 void (*callback) (struct line_header *lh,
17856 unsigned int mod_time,
17857 unsigned int length))
17859 gdb_byte format_count, formati;
17860 ULONGEST data_count, datai;
17861 const gdb_byte *buf = *bufp;
17862 const gdb_byte *format_header_data;
17864 unsigned int bytes_read;
17866 format_count = read_1_byte (abfd, buf);
17868 format_header_data = buf;
17869 for (formati = 0; formati < format_count; formati++)
17871 read_unsigned_leb128 (abfd, buf, &bytes_read);
17873 read_unsigned_leb128 (abfd, buf, &bytes_read);
17877 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17879 for (datai = 0; datai < data_count; datai++)
17881 const gdb_byte *format = format_header_data;
17882 struct file_entry fe;
17884 for (formati = 0; formati < format_count; formati++)
17886 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17887 format += bytes_read;
17889 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
17890 format += bytes_read;
17892 gdb::optional<const char *> string;
17893 gdb::optional<unsigned int> uint;
17897 case DW_FORM_string:
17898 string.emplace (read_direct_string (abfd, buf, &bytes_read));
17902 case DW_FORM_line_strp:
17903 string.emplace (read_indirect_line_string (abfd, buf,
17909 case DW_FORM_data1:
17910 uint.emplace (read_1_byte (abfd, buf));
17914 case DW_FORM_data2:
17915 uint.emplace (read_2_bytes (abfd, buf));
17919 case DW_FORM_data4:
17920 uint.emplace (read_4_bytes (abfd, buf));
17924 case DW_FORM_data8:
17925 uint.emplace (read_8_bytes (abfd, buf));
17929 case DW_FORM_udata:
17930 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
17934 case DW_FORM_block:
17935 /* It is valid only for DW_LNCT_timestamp which is ignored by
17940 switch (content_type)
17943 if (string.has_value ())
17946 case DW_LNCT_directory_index:
17947 if (uint.has_value ())
17948 fe.d_index = (dir_index) *uint;
17950 case DW_LNCT_timestamp:
17951 if (uint.has_value ())
17952 fe.mod_time = *uint;
17955 if (uint.has_value ())
17961 complaint (&symfile_complaints,
17962 _("Unknown format content type %s"),
17963 pulongest (content_type));
17967 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
17973 /* Read the statement program header starting at OFFSET in
17974 .debug_line, or .debug_line.dwo. Return a pointer
17975 to a struct line_header, allocated using xmalloc.
17976 Returns NULL if there is a problem reading the header, e.g., if it
17977 has a version we don't understand.
17979 NOTE: the strings in the include directory and file name tables of
17980 the returned object point into the dwarf line section buffer,
17981 and must not be freed. */
17983 static line_header_up
17984 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
17986 const gdb_byte *line_ptr;
17987 unsigned int bytes_read, offset_size;
17989 const char *cur_dir, *cur_file;
17990 struct dwarf2_section_info *section;
17993 section = get_debug_line_section (cu);
17994 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17995 if (section->buffer == NULL)
17997 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17998 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
18000 complaint (&symfile_complaints, _("missing .debug_line section"));
18004 /* We can't do this until we know the section is non-empty.
18005 Only then do we know we have such a section. */
18006 abfd = get_section_bfd_owner (section);
18008 /* Make sure that at least there's room for the total_length field.
18009 That could be 12 bytes long, but we're just going to fudge that. */
18010 if (to_underlying (sect_off) + 4 >= section->size)
18012 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18016 line_header_up lh (new line_header ());
18018 lh->sect_off = sect_off;
18019 lh->offset_in_dwz = cu->per_cu->is_dwz;
18021 line_ptr = section->buffer + to_underlying (sect_off);
18023 /* Read in the header. */
18025 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
18026 &bytes_read, &offset_size);
18027 line_ptr += bytes_read;
18028 if (line_ptr + lh->total_length > (section->buffer + section->size))
18030 dwarf2_statement_list_fits_in_line_number_section_complaint ();
18033 lh->statement_program_end = line_ptr + lh->total_length;
18034 lh->version = read_2_bytes (abfd, line_ptr);
18036 if (lh->version > 5)
18038 /* This is a version we don't understand. The format could have
18039 changed in ways we don't handle properly so just punt. */
18040 complaint (&symfile_complaints,
18041 _("unsupported version in .debug_line section"));
18044 if (lh->version >= 5)
18046 gdb_byte segment_selector_size;
18048 /* Skip address size. */
18049 read_1_byte (abfd, line_ptr);
18052 segment_selector_size = read_1_byte (abfd, line_ptr);
18054 if (segment_selector_size != 0)
18056 complaint (&symfile_complaints,
18057 _("unsupported segment selector size %u "
18058 "in .debug_line section"),
18059 segment_selector_size);
18063 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
18064 line_ptr += offset_size;
18065 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
18067 if (lh->version >= 4)
18069 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
18073 lh->maximum_ops_per_instruction = 1;
18075 if (lh->maximum_ops_per_instruction == 0)
18077 lh->maximum_ops_per_instruction = 1;
18078 complaint (&symfile_complaints,
18079 _("invalid maximum_ops_per_instruction "
18080 "in `.debug_line' section"));
18083 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
18085 lh->line_base = read_1_signed_byte (abfd, line_ptr);
18087 lh->line_range = read_1_byte (abfd, line_ptr);
18089 lh->opcode_base = read_1_byte (abfd, line_ptr);
18091 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
18093 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
18094 for (i = 1; i < lh->opcode_base; ++i)
18096 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
18100 if (lh->version >= 5)
18102 /* Read directory table. */
18103 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18104 [] (struct line_header *lh, const char *name,
18105 dir_index d_index, unsigned int mod_time,
18106 unsigned int length)
18108 lh->add_include_dir (name);
18111 /* Read file name table. */
18112 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
18113 [] (struct line_header *lh, const char *name,
18114 dir_index d_index, unsigned int mod_time,
18115 unsigned int length)
18117 lh->add_file_name (name, d_index, mod_time, length);
18122 /* Read directory table. */
18123 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18125 line_ptr += bytes_read;
18126 lh->add_include_dir (cur_dir);
18128 line_ptr += bytes_read;
18130 /* Read file name table. */
18131 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
18133 unsigned int mod_time, length;
18136 line_ptr += bytes_read;
18137 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18138 line_ptr += bytes_read;
18139 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18140 line_ptr += bytes_read;
18141 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18142 line_ptr += bytes_read;
18144 lh->add_file_name (cur_file, d_index, mod_time, length);
18146 line_ptr += bytes_read;
18148 lh->statement_program_start = line_ptr;
18150 if (line_ptr > (section->buffer + section->size))
18151 complaint (&symfile_complaints,
18152 _("line number info header doesn't "
18153 "fit in `.debug_line' section"));
18158 /* Subroutine of dwarf_decode_lines to simplify it.
18159 Return the file name of the psymtab for included file FILE_INDEX
18160 in line header LH of PST.
18161 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18162 If space for the result is malloc'd, it will be freed by a cleanup.
18163 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
18165 The function creates dangling cleanup registration. */
18167 static const char *
18168 psymtab_include_file_name (const struct line_header *lh, int file_index,
18169 const struct partial_symtab *pst,
18170 const char *comp_dir)
18172 const file_entry &fe = lh->file_names[file_index];
18173 const char *include_name = fe.name;
18174 const char *include_name_to_compare = include_name;
18175 const char *pst_filename;
18176 char *copied_name = NULL;
18179 const char *dir_name = fe.include_dir (lh);
18181 if (!IS_ABSOLUTE_PATH (include_name)
18182 && (dir_name != NULL || comp_dir != NULL))
18184 /* Avoid creating a duplicate psymtab for PST.
18185 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18186 Before we do the comparison, however, we need to account
18187 for DIR_NAME and COMP_DIR.
18188 First prepend dir_name (if non-NULL). If we still don't
18189 have an absolute path prepend comp_dir (if non-NULL).
18190 However, the directory we record in the include-file's
18191 psymtab does not contain COMP_DIR (to match the
18192 corresponding symtab(s)).
18197 bash$ gcc -g ./hello.c
18198 include_name = "hello.c"
18200 DW_AT_comp_dir = comp_dir = "/tmp"
18201 DW_AT_name = "./hello.c"
18205 if (dir_name != NULL)
18207 char *tem = concat (dir_name, SLASH_STRING,
18208 include_name, (char *)NULL);
18210 make_cleanup (xfree, tem);
18211 include_name = tem;
18212 include_name_to_compare = include_name;
18214 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18216 char *tem = concat (comp_dir, SLASH_STRING,
18217 include_name, (char *)NULL);
18219 make_cleanup (xfree, tem);
18220 include_name_to_compare = tem;
18224 pst_filename = pst->filename;
18225 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18227 copied_name = concat (pst->dirname, SLASH_STRING,
18228 pst_filename, (char *)NULL);
18229 pst_filename = copied_name;
18232 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18234 if (copied_name != NULL)
18235 xfree (copied_name);
18239 return include_name;
18242 /* State machine to track the state of the line number program. */
18244 class lnp_state_machine
18247 /* Initialize a machine state for the start of a line number
18249 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18251 file_entry *current_file ()
18253 /* lh->file_names is 0-based, but the file name numbers in the
18254 statement program are 1-based. */
18255 return m_line_header->file_name_at (m_file);
18258 /* Record the line in the state machine. END_SEQUENCE is true if
18259 we're processing the end of a sequence. */
18260 void record_line (bool end_sequence);
18262 /* Check address and if invalid nop-out the rest of the lines in this
18264 void check_line_address (struct dwarf2_cu *cu,
18265 const gdb_byte *line_ptr,
18266 CORE_ADDR lowpc, CORE_ADDR address);
18268 void handle_set_discriminator (unsigned int discriminator)
18270 m_discriminator = discriminator;
18271 m_line_has_non_zero_discriminator |= discriminator != 0;
18274 /* Handle DW_LNE_set_address. */
18275 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18278 address += baseaddr;
18279 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18282 /* Handle DW_LNS_advance_pc. */
18283 void handle_advance_pc (CORE_ADDR adjust);
18285 /* Handle a special opcode. */
18286 void handle_special_opcode (unsigned char op_code);
18288 /* Handle DW_LNS_advance_line. */
18289 void handle_advance_line (int line_delta)
18291 advance_line (line_delta);
18294 /* Handle DW_LNS_set_file. */
18295 void handle_set_file (file_name_index file);
18297 /* Handle DW_LNS_negate_stmt. */
18298 void handle_negate_stmt ()
18300 m_is_stmt = !m_is_stmt;
18303 /* Handle DW_LNS_const_add_pc. */
18304 void handle_const_add_pc ();
18306 /* Handle DW_LNS_fixed_advance_pc. */
18307 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18309 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18313 /* Handle DW_LNS_copy. */
18314 void handle_copy ()
18316 record_line (false);
18317 m_discriminator = 0;
18320 /* Handle DW_LNE_end_sequence. */
18321 void handle_end_sequence ()
18323 m_record_line_callback = ::record_line;
18327 /* Advance the line by LINE_DELTA. */
18328 void advance_line (int line_delta)
18330 m_line += line_delta;
18332 if (line_delta != 0)
18333 m_line_has_non_zero_discriminator = m_discriminator != 0;
18336 gdbarch *m_gdbarch;
18338 /* True if we're recording lines.
18339 Otherwise we're building partial symtabs and are just interested in
18340 finding include files mentioned by the line number program. */
18341 bool m_record_lines_p;
18343 /* The line number header. */
18344 line_header *m_line_header;
18346 /* These are part of the standard DWARF line number state machine,
18347 and initialized according to the DWARF spec. */
18349 unsigned char m_op_index = 0;
18350 /* The line table index (1-based) of the current file. */
18351 file_name_index m_file = (file_name_index) 1;
18352 unsigned int m_line = 1;
18354 /* These are initialized in the constructor. */
18356 CORE_ADDR m_address;
18358 unsigned int m_discriminator;
18360 /* Additional bits of state we need to track. */
18362 /* The last file that we called dwarf2_start_subfile for.
18363 This is only used for TLLs. */
18364 unsigned int m_last_file = 0;
18365 /* The last file a line number was recorded for. */
18366 struct subfile *m_last_subfile = NULL;
18368 /* The function to call to record a line. */
18369 record_line_ftype *m_record_line_callback = NULL;
18371 /* The last line number that was recorded, used to coalesce
18372 consecutive entries for the same line. This can happen, for
18373 example, when discriminators are present. PR 17276. */
18374 unsigned int m_last_line = 0;
18375 bool m_line_has_non_zero_discriminator = false;
18379 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18381 CORE_ADDR addr_adj = (((m_op_index + adjust)
18382 / m_line_header->maximum_ops_per_instruction)
18383 * m_line_header->minimum_instruction_length);
18384 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18385 m_op_index = ((m_op_index + adjust)
18386 % m_line_header->maximum_ops_per_instruction);
18390 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18392 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18393 CORE_ADDR addr_adj = (((m_op_index
18394 + (adj_opcode / m_line_header->line_range))
18395 / m_line_header->maximum_ops_per_instruction)
18396 * m_line_header->minimum_instruction_length);
18397 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18398 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18399 % m_line_header->maximum_ops_per_instruction);
18401 int line_delta = (m_line_header->line_base
18402 + (adj_opcode % m_line_header->line_range));
18403 advance_line (line_delta);
18404 record_line (false);
18405 m_discriminator = 0;
18409 lnp_state_machine::handle_set_file (file_name_index file)
18413 const file_entry *fe = current_file ();
18415 dwarf2_debug_line_missing_file_complaint ();
18416 else if (m_record_lines_p)
18418 const char *dir = fe->include_dir (m_line_header);
18420 m_last_subfile = current_subfile;
18421 m_line_has_non_zero_discriminator = m_discriminator != 0;
18422 dwarf2_start_subfile (fe->name, dir);
18427 lnp_state_machine::handle_const_add_pc ()
18430 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18433 = (((m_op_index + adjust)
18434 / m_line_header->maximum_ops_per_instruction)
18435 * m_line_header->minimum_instruction_length);
18437 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18438 m_op_index = ((m_op_index + adjust)
18439 % m_line_header->maximum_ops_per_instruction);
18442 /* Ignore this record_line request. */
18445 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18450 /* Return non-zero if we should add LINE to the line number table.
18451 LINE is the line to add, LAST_LINE is the last line that was added,
18452 LAST_SUBFILE is the subfile for LAST_LINE.
18453 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18454 had a non-zero discriminator.
18456 We have to be careful in the presence of discriminators.
18457 E.g., for this line:
18459 for (i = 0; i < 100000; i++);
18461 clang can emit four line number entries for that one line,
18462 each with a different discriminator.
18463 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18465 However, we want gdb to coalesce all four entries into one.
18466 Otherwise the user could stepi into the middle of the line and
18467 gdb would get confused about whether the pc really was in the
18468 middle of the line.
18470 Things are further complicated by the fact that two consecutive
18471 line number entries for the same line is a heuristic used by gcc
18472 to denote the end of the prologue. So we can't just discard duplicate
18473 entries, we have to be selective about it. The heuristic we use is
18474 that we only collapse consecutive entries for the same line if at least
18475 one of those entries has a non-zero discriminator. PR 17276.
18477 Note: Addresses in the line number state machine can never go backwards
18478 within one sequence, thus this coalescing is ok. */
18481 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18482 int line_has_non_zero_discriminator,
18483 struct subfile *last_subfile)
18485 if (current_subfile != last_subfile)
18487 if (line != last_line)
18489 /* Same line for the same file that we've seen already.
18490 As a last check, for pr 17276, only record the line if the line
18491 has never had a non-zero discriminator. */
18492 if (!line_has_non_zero_discriminator)
18497 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18498 in the line table of subfile SUBFILE. */
18501 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18502 unsigned int line, CORE_ADDR address,
18503 record_line_ftype p_record_line)
18505 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18507 if (dwarf_line_debug)
18509 fprintf_unfiltered (gdb_stdlog,
18510 "Recording line %u, file %s, address %s\n",
18511 line, lbasename (subfile->name),
18512 paddress (gdbarch, address));
18515 (*p_record_line) (subfile, line, addr);
18518 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18519 Mark the end of a set of line number records.
18520 The arguments are the same as for dwarf_record_line_1.
18521 If SUBFILE is NULL the request is ignored. */
18524 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18525 CORE_ADDR address, record_line_ftype p_record_line)
18527 if (subfile == NULL)
18530 if (dwarf_line_debug)
18532 fprintf_unfiltered (gdb_stdlog,
18533 "Finishing current line, file %s, address %s\n",
18534 lbasename (subfile->name),
18535 paddress (gdbarch, address));
18538 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18542 lnp_state_machine::record_line (bool end_sequence)
18544 if (dwarf_line_debug)
18546 fprintf_unfiltered (gdb_stdlog,
18547 "Processing actual line %u: file %u,"
18548 " address %s, is_stmt %u, discrim %u\n",
18549 m_line, to_underlying (m_file),
18550 paddress (m_gdbarch, m_address),
18551 m_is_stmt, m_discriminator);
18554 file_entry *fe = current_file ();
18557 dwarf2_debug_line_missing_file_complaint ();
18558 /* For now we ignore lines not starting on an instruction boundary.
18559 But not when processing end_sequence for compatibility with the
18560 previous version of the code. */
18561 else if (m_op_index == 0 || end_sequence)
18563 fe->included_p = 1;
18564 if (m_record_lines_p && m_is_stmt)
18566 if (m_last_subfile != current_subfile || end_sequence)
18568 dwarf_finish_line (m_gdbarch, m_last_subfile,
18569 m_address, m_record_line_callback);
18574 if (dwarf_record_line_p (m_line, m_last_line,
18575 m_line_has_non_zero_discriminator,
18578 dwarf_record_line_1 (m_gdbarch, current_subfile,
18580 m_record_line_callback);
18582 m_last_subfile = current_subfile;
18583 m_last_line = m_line;
18589 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18590 bool record_lines_p)
18593 m_record_lines_p = record_lines_p;
18594 m_line_header = lh;
18596 m_record_line_callback = ::record_line;
18598 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18599 was a line entry for it so that the backend has a chance to adjust it
18600 and also record it in case it needs it. This is currently used by MIPS
18601 code, cf. `mips_adjust_dwarf2_line'. */
18602 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18603 m_is_stmt = lh->default_is_stmt;
18604 m_discriminator = 0;
18608 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18609 const gdb_byte *line_ptr,
18610 CORE_ADDR lowpc, CORE_ADDR address)
18612 /* If address < lowpc then it's not a usable value, it's outside the
18613 pc range of the CU. However, we restrict the test to only address
18614 values of zero to preserve GDB's previous behaviour which is to
18615 handle the specific case of a function being GC'd by the linker. */
18617 if (address == 0 && address < lowpc)
18619 /* This line table is for a function which has been
18620 GCd by the linker. Ignore it. PR gdb/12528 */
18622 struct objfile *objfile = cu->objfile;
18623 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18625 complaint (&symfile_complaints,
18626 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18627 line_offset, objfile_name (objfile));
18628 m_record_line_callback = noop_record_line;
18629 /* Note: record_line_callback is left as noop_record_line until
18630 we see DW_LNE_end_sequence. */
18634 /* Subroutine of dwarf_decode_lines to simplify it.
18635 Process the line number information in LH.
18636 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18637 program in order to set included_p for every referenced header. */
18640 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18641 const int decode_for_pst_p, CORE_ADDR lowpc)
18643 const gdb_byte *line_ptr, *extended_end;
18644 const gdb_byte *line_end;
18645 unsigned int bytes_read, extended_len;
18646 unsigned char op_code, extended_op;
18647 CORE_ADDR baseaddr;
18648 struct objfile *objfile = cu->objfile;
18649 bfd *abfd = objfile->obfd;
18650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18651 /* True if we're recording line info (as opposed to building partial
18652 symtabs and just interested in finding include files mentioned by
18653 the line number program). */
18654 bool record_lines_p = !decode_for_pst_p;
18656 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18658 line_ptr = lh->statement_program_start;
18659 line_end = lh->statement_program_end;
18661 /* Read the statement sequences until there's nothing left. */
18662 while (line_ptr < line_end)
18664 /* The DWARF line number program state machine. Reset the state
18665 machine at the start of each sequence. */
18666 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18667 bool end_sequence = false;
18669 if (record_lines_p)
18671 /* Start a subfile for the current file of the state
18673 const file_entry *fe = state_machine.current_file ();
18676 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
18679 /* Decode the table. */
18680 while (line_ptr < line_end && !end_sequence)
18682 op_code = read_1_byte (abfd, line_ptr);
18685 if (op_code >= lh->opcode_base)
18687 /* Special opcode. */
18688 state_machine.handle_special_opcode (op_code);
18690 else switch (op_code)
18692 case DW_LNS_extended_op:
18693 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18695 line_ptr += bytes_read;
18696 extended_end = line_ptr + extended_len;
18697 extended_op = read_1_byte (abfd, line_ptr);
18699 switch (extended_op)
18701 case DW_LNE_end_sequence:
18702 state_machine.handle_end_sequence ();
18703 end_sequence = true;
18705 case DW_LNE_set_address:
18708 = read_address (abfd, line_ptr, cu, &bytes_read);
18709 line_ptr += bytes_read;
18711 state_machine.check_line_address (cu, line_ptr,
18713 state_machine.handle_set_address (baseaddr, address);
18716 case DW_LNE_define_file:
18718 const char *cur_file;
18719 unsigned int mod_time, length;
18722 cur_file = read_direct_string (abfd, line_ptr,
18724 line_ptr += bytes_read;
18725 dindex = (dir_index)
18726 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18727 line_ptr += bytes_read;
18729 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18730 line_ptr += bytes_read;
18732 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18733 line_ptr += bytes_read;
18734 lh->add_file_name (cur_file, dindex, mod_time, length);
18737 case DW_LNE_set_discriminator:
18739 /* The discriminator is not interesting to the
18740 debugger; just ignore it. We still need to
18741 check its value though:
18742 if there are consecutive entries for the same
18743 (non-prologue) line we want to coalesce them.
18746 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18747 line_ptr += bytes_read;
18749 state_machine.handle_set_discriminator (discr);
18753 complaint (&symfile_complaints,
18754 _("mangled .debug_line section"));
18757 /* Make sure that we parsed the extended op correctly. If e.g.
18758 we expected a different address size than the producer used,
18759 we may have read the wrong number of bytes. */
18760 if (line_ptr != extended_end)
18762 complaint (&symfile_complaints,
18763 _("mangled .debug_line section"));
18768 state_machine.handle_copy ();
18770 case DW_LNS_advance_pc:
18773 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18774 line_ptr += bytes_read;
18776 state_machine.handle_advance_pc (adjust);
18779 case DW_LNS_advance_line:
18782 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18783 line_ptr += bytes_read;
18785 state_machine.handle_advance_line (line_delta);
18788 case DW_LNS_set_file:
18790 file_name_index file
18791 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18793 line_ptr += bytes_read;
18795 state_machine.handle_set_file (file);
18798 case DW_LNS_set_column:
18799 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18800 line_ptr += bytes_read;
18802 case DW_LNS_negate_stmt:
18803 state_machine.handle_negate_stmt ();
18805 case DW_LNS_set_basic_block:
18807 /* Add to the address register of the state machine the
18808 address increment value corresponding to special opcode
18809 255. I.e., this value is scaled by the minimum
18810 instruction length since special opcode 255 would have
18811 scaled the increment. */
18812 case DW_LNS_const_add_pc:
18813 state_machine.handle_const_add_pc ();
18815 case DW_LNS_fixed_advance_pc:
18817 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18820 state_machine.handle_fixed_advance_pc (addr_adj);
18825 /* Unknown standard opcode, ignore it. */
18828 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18830 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18831 line_ptr += bytes_read;
18838 dwarf2_debug_line_missing_end_sequence_complaint ();
18840 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18841 in which case we still finish recording the last line). */
18842 state_machine.record_line (true);
18846 /* Decode the Line Number Program (LNP) for the given line_header
18847 structure and CU. The actual information extracted and the type
18848 of structures created from the LNP depends on the value of PST.
18850 1. If PST is NULL, then this procedure uses the data from the program
18851 to create all necessary symbol tables, and their linetables.
18853 2. If PST is not NULL, this procedure reads the program to determine
18854 the list of files included by the unit represented by PST, and
18855 builds all the associated partial symbol tables.
18857 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18858 It is used for relative paths in the line table.
18859 NOTE: When processing partial symtabs (pst != NULL),
18860 comp_dir == pst->dirname.
18862 NOTE: It is important that psymtabs have the same file name (via strcmp)
18863 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18864 symtab we don't use it in the name of the psymtabs we create.
18865 E.g. expand_line_sal requires this when finding psymtabs to expand.
18866 A good testcase for this is mb-inline.exp.
18868 LOWPC is the lowest address in CU (or 0 if not known).
18870 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18871 for its PC<->lines mapping information. Otherwise only the filename
18872 table is read in. */
18875 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18876 struct dwarf2_cu *cu, struct partial_symtab *pst,
18877 CORE_ADDR lowpc, int decode_mapping)
18879 struct objfile *objfile = cu->objfile;
18880 const int decode_for_pst_p = (pst != NULL);
18882 if (decode_mapping)
18883 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18885 if (decode_for_pst_p)
18889 /* Now that we're done scanning the Line Header Program, we can
18890 create the psymtab of each included file. */
18891 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
18892 if (lh->file_names[file_index].included_p == 1)
18894 const char *include_name =
18895 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18896 if (include_name != NULL)
18897 dwarf2_create_include_psymtab (include_name, pst, objfile);
18902 /* Make sure a symtab is created for every file, even files
18903 which contain only variables (i.e. no code with associated
18905 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18908 for (i = 0; i < lh->file_names.size (); i++)
18910 file_entry &fe = lh->file_names[i];
18912 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
18914 if (current_subfile->symtab == NULL)
18916 current_subfile->symtab
18917 = allocate_symtab (cust, current_subfile->name);
18919 fe.symtab = current_subfile->symtab;
18924 /* Start a subfile for DWARF. FILENAME is the name of the file and
18925 DIRNAME the name of the source directory which contains FILENAME
18926 or NULL if not known.
18927 This routine tries to keep line numbers from identical absolute and
18928 relative file names in a common subfile.
18930 Using the `list' example from the GDB testsuite, which resides in
18931 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18932 of /srcdir/list0.c yields the following debugging information for list0.c:
18934 DW_AT_name: /srcdir/list0.c
18935 DW_AT_comp_dir: /compdir
18936 files.files[0].name: list0.h
18937 files.files[0].dir: /srcdir
18938 files.files[1].name: list0.c
18939 files.files[1].dir: /srcdir
18941 The line number information for list0.c has to end up in a single
18942 subfile, so that `break /srcdir/list0.c:1' works as expected.
18943 start_subfile will ensure that this happens provided that we pass the
18944 concatenation of files.files[1].dir and files.files[1].name as the
18948 dwarf2_start_subfile (const char *filename, const char *dirname)
18952 /* In order not to lose the line information directory,
18953 we concatenate it to the filename when it makes sense.
18954 Note that the Dwarf3 standard says (speaking of filenames in line
18955 information): ``The directory index is ignored for file names
18956 that represent full path names''. Thus ignoring dirname in the
18957 `else' branch below isn't an issue. */
18959 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18961 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18965 start_subfile (filename);
18971 /* Start a symtab for DWARF.
18972 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18974 static struct compunit_symtab *
18975 dwarf2_start_symtab (struct dwarf2_cu *cu,
18976 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18978 struct compunit_symtab *cust
18979 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18981 record_debugformat ("DWARF 2");
18982 record_producer (cu->producer);
18984 /* We assume that we're processing GCC output. */
18985 processing_gcc_compilation = 2;
18987 cu->processing_has_namespace_info = 0;
18993 var_decode_location (struct attribute *attr, struct symbol *sym,
18994 struct dwarf2_cu *cu)
18996 struct objfile *objfile = cu->objfile;
18997 struct comp_unit_head *cu_header = &cu->header;
18999 /* NOTE drow/2003-01-30: There used to be a comment and some special
19000 code here to turn a symbol with DW_AT_external and a
19001 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19002 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19003 with some versions of binutils) where shared libraries could have
19004 relocations against symbols in their debug information - the
19005 minimal symbol would have the right address, but the debug info
19006 would not. It's no longer necessary, because we will explicitly
19007 apply relocations when we read in the debug information now. */
19009 /* A DW_AT_location attribute with no contents indicates that a
19010 variable has been optimized away. */
19011 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
19013 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19017 /* Handle one degenerate form of location expression specially, to
19018 preserve GDB's previous behavior when section offsets are
19019 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
19020 then mark this symbol as LOC_STATIC. */
19022 if (attr_form_is_block (attr)
19023 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19024 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19025 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19026 && (DW_BLOCK (attr)->size
19027 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19029 unsigned int dummy;
19031 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19032 SYMBOL_VALUE_ADDRESS (sym) =
19033 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
19035 SYMBOL_VALUE_ADDRESS (sym) =
19036 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
19037 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19038 fixup_symbol_section (sym, objfile);
19039 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
19040 SYMBOL_SECTION (sym));
19044 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19045 expression evaluator, and use LOC_COMPUTED only when necessary
19046 (i.e. when the value of a register or memory location is
19047 referenced, or a thread-local block, etc.). Then again, it might
19048 not be worthwhile. I'm assuming that it isn't unless performance
19049 or memory numbers show me otherwise. */
19051 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19053 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19054 cu->has_loclist = 1;
19057 /* Given a pointer to a DWARF information entry, figure out if we need
19058 to make a symbol table entry for it, and if so, create a new entry
19059 and return a pointer to it.
19060 If TYPE is NULL, determine symbol type from the die, otherwise
19061 used the passed type.
19062 If SPACE is not NULL, use it to hold the new symbol. If it is
19063 NULL, allocate a new symbol on the objfile's obstack. */
19065 static struct symbol *
19066 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
19067 struct symbol *space)
19069 struct objfile *objfile = cu->objfile;
19070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19071 struct symbol *sym = NULL;
19073 struct attribute *attr = NULL;
19074 struct attribute *attr2 = NULL;
19075 CORE_ADDR baseaddr;
19076 struct pending **list_to_add = NULL;
19078 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
19080 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19082 name = dwarf2_name (die, cu);
19085 const char *linkagename;
19086 int suppress_add = 0;
19091 sym = allocate_symbol (objfile);
19092 OBJSTAT (objfile, n_syms++);
19094 /* Cache this symbol's name and the name's demangled form (if any). */
19095 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
19096 linkagename = dwarf2_physname (name, die, cu);
19097 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
19099 /* Fortran does not have mangling standard and the mangling does differ
19100 between gfortran, iFort etc. */
19101 if (cu->language == language_fortran
19102 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
19103 symbol_set_demangled_name (&(sym->ginfo),
19104 dwarf2_full_name (name, die, cu),
19107 /* Default assumptions.
19108 Use the passed type or decode it from the die. */
19109 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19110 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19112 SYMBOL_TYPE (sym) = type;
19114 SYMBOL_TYPE (sym) = die_type (die, cu);
19115 attr = dwarf2_attr (die,
19116 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19120 SYMBOL_LINE (sym) = DW_UNSND (attr);
19123 attr = dwarf2_attr (die,
19124 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19128 file_name_index file_index = (file_name_index) DW_UNSND (attr);
19129 struct file_entry *fe;
19131 if (cu->line_header != NULL)
19132 fe = cu->line_header->file_name_at (file_index);
19137 complaint (&symfile_complaints,
19138 _("file index out of range"));
19140 symbol_set_symtab (sym, fe->symtab);
19146 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
19151 addr = attr_value_as_address (attr);
19152 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
19153 SYMBOL_VALUE_ADDRESS (sym) = addr;
19155 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
19156 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
19157 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
19158 add_symbol_to_list (sym, cu->list_in_scope);
19160 case DW_TAG_subprogram:
19161 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19163 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19164 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19165 if ((attr2 && (DW_UNSND (attr2) != 0))
19166 || cu->language == language_ada)
19168 /* Subprograms marked external are stored as a global symbol.
19169 Ada subprograms, whether marked external or not, are always
19170 stored as a global symbol, because we want to be able to
19171 access them globally. For instance, we want to be able
19172 to break on a nested subprogram without having to
19173 specify the context. */
19174 list_to_add = &global_symbols;
19178 list_to_add = cu->list_in_scope;
19181 case DW_TAG_inlined_subroutine:
19182 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19184 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
19185 SYMBOL_INLINED (sym) = 1;
19186 list_to_add = cu->list_in_scope;
19188 case DW_TAG_template_value_param:
19190 /* Fall through. */
19191 case DW_TAG_constant:
19192 case DW_TAG_variable:
19193 case DW_TAG_member:
19194 /* Compilation with minimal debug info may result in
19195 variables with missing type entries. Change the
19196 misleading `void' type to something sensible. */
19197 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
19198 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
19200 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19201 /* In the case of DW_TAG_member, we should only be called for
19202 static const members. */
19203 if (die->tag == DW_TAG_member)
19205 /* dwarf2_add_field uses die_is_declaration,
19206 so we do the same. */
19207 gdb_assert (die_is_declaration (die, cu));
19212 dwarf2_const_value (attr, sym, cu);
19213 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19216 if (attr2 && (DW_UNSND (attr2) != 0))
19217 list_to_add = &global_symbols;
19219 list_to_add = cu->list_in_scope;
19223 attr = dwarf2_attr (die, DW_AT_location, cu);
19226 var_decode_location (attr, sym, cu);
19227 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19229 /* Fortran explicitly imports any global symbols to the local
19230 scope by DW_TAG_common_block. */
19231 if (cu->language == language_fortran && die->parent
19232 && die->parent->tag == DW_TAG_common_block)
19235 if (SYMBOL_CLASS (sym) == LOC_STATIC
19236 && SYMBOL_VALUE_ADDRESS (sym) == 0
19237 && !dwarf2_per_objfile->has_section_at_zero)
19239 /* When a static variable is eliminated by the linker,
19240 the corresponding debug information is not stripped
19241 out, but the variable address is set to null;
19242 do not add such variables into symbol table. */
19244 else if (attr2 && (DW_UNSND (attr2) != 0))
19246 /* Workaround gfortran PR debug/40040 - it uses
19247 DW_AT_location for variables in -fPIC libraries which may
19248 get overriden by other libraries/executable and get
19249 a different address. Resolve it by the minimal symbol
19250 which may come from inferior's executable using copy
19251 relocation. Make this workaround only for gfortran as for
19252 other compilers GDB cannot guess the minimal symbol
19253 Fortran mangling kind. */
19254 if (cu->language == language_fortran && die->parent
19255 && die->parent->tag == DW_TAG_module
19257 && startswith (cu->producer, "GNU Fortran"))
19258 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19260 /* A variable with DW_AT_external is never static,
19261 but it may be block-scoped. */
19262 list_to_add = (cu->list_in_scope == &file_symbols
19263 ? &global_symbols : cu->list_in_scope);
19266 list_to_add = cu->list_in_scope;
19270 /* We do not know the address of this symbol.
19271 If it is an external symbol and we have type information
19272 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19273 The address of the variable will then be determined from
19274 the minimal symbol table whenever the variable is
19276 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19278 /* Fortran explicitly imports any global symbols to the local
19279 scope by DW_TAG_common_block. */
19280 if (cu->language == language_fortran && die->parent
19281 && die->parent->tag == DW_TAG_common_block)
19283 /* SYMBOL_CLASS doesn't matter here because
19284 read_common_block is going to reset it. */
19286 list_to_add = cu->list_in_scope;
19288 else if (attr2 && (DW_UNSND (attr2) != 0)
19289 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19291 /* A variable with DW_AT_external is never static, but it
19292 may be block-scoped. */
19293 list_to_add = (cu->list_in_scope == &file_symbols
19294 ? &global_symbols : cu->list_in_scope);
19296 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19298 else if (!die_is_declaration (die, cu))
19300 /* Use the default LOC_OPTIMIZED_OUT class. */
19301 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19303 list_to_add = cu->list_in_scope;
19307 case DW_TAG_formal_parameter:
19308 /* If we are inside a function, mark this as an argument. If
19309 not, we might be looking at an argument to an inlined function
19310 when we do not have enough information to show inlined frames;
19311 pretend it's a local variable in that case so that the user can
19313 if (context_stack_depth > 0
19314 && context_stack[context_stack_depth - 1].name != NULL)
19315 SYMBOL_IS_ARGUMENT (sym) = 1;
19316 attr = dwarf2_attr (die, DW_AT_location, cu);
19319 var_decode_location (attr, sym, cu);
19321 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19324 dwarf2_const_value (attr, sym, cu);
19327 list_to_add = cu->list_in_scope;
19329 case DW_TAG_unspecified_parameters:
19330 /* From varargs functions; gdb doesn't seem to have any
19331 interest in this information, so just ignore it for now.
19334 case DW_TAG_template_type_param:
19336 /* Fall through. */
19337 case DW_TAG_class_type:
19338 case DW_TAG_interface_type:
19339 case DW_TAG_structure_type:
19340 case DW_TAG_union_type:
19341 case DW_TAG_set_type:
19342 case DW_TAG_enumeration_type:
19343 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19344 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19347 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19348 really ever be static objects: otherwise, if you try
19349 to, say, break of a class's method and you're in a file
19350 which doesn't mention that class, it won't work unless
19351 the check for all static symbols in lookup_symbol_aux
19352 saves you. See the OtherFileClass tests in
19353 gdb.c++/namespace.exp. */
19357 list_to_add = (cu->list_in_scope == &file_symbols
19358 && cu->language == language_cplus
19359 ? &global_symbols : cu->list_in_scope);
19361 /* The semantics of C++ state that "struct foo {
19362 ... }" also defines a typedef for "foo". */
19363 if (cu->language == language_cplus
19364 || cu->language == language_ada
19365 || cu->language == language_d
19366 || cu->language == language_rust)
19368 /* The symbol's name is already allocated along
19369 with this objfile, so we don't need to
19370 duplicate it for the type. */
19371 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19372 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19377 case DW_TAG_typedef:
19378 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19379 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19380 list_to_add = cu->list_in_scope;
19382 case DW_TAG_base_type:
19383 case DW_TAG_subrange_type:
19384 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19385 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19386 list_to_add = cu->list_in_scope;
19388 case DW_TAG_enumerator:
19389 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19392 dwarf2_const_value (attr, sym, cu);
19395 /* NOTE: carlton/2003-11-10: See comment above in the
19396 DW_TAG_class_type, etc. block. */
19398 list_to_add = (cu->list_in_scope == &file_symbols
19399 && cu->language == language_cplus
19400 ? &global_symbols : cu->list_in_scope);
19403 case DW_TAG_imported_declaration:
19404 case DW_TAG_namespace:
19405 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19406 list_to_add = &global_symbols;
19408 case DW_TAG_module:
19409 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19410 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19411 list_to_add = &global_symbols;
19413 case DW_TAG_common_block:
19414 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19415 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19416 add_symbol_to_list (sym, cu->list_in_scope);
19419 /* Not a tag we recognize. Hopefully we aren't processing
19420 trash data, but since we must specifically ignore things
19421 we don't recognize, there is nothing else we should do at
19423 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19424 dwarf_tag_name (die->tag));
19430 sym->hash_next = objfile->template_symbols;
19431 objfile->template_symbols = sym;
19432 list_to_add = NULL;
19435 if (list_to_add != NULL)
19436 add_symbol_to_list (sym, list_to_add);
19438 /* For the benefit of old versions of GCC, check for anonymous
19439 namespaces based on the demangled name. */
19440 if (!cu->processing_has_namespace_info
19441 && cu->language == language_cplus)
19442 cp_scan_for_anonymous_namespaces (sym, objfile);
19447 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19449 static struct symbol *
19450 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19452 return new_symbol_full (die, type, cu, NULL);
19455 /* Given an attr with a DW_FORM_dataN value in host byte order,
19456 zero-extend it as appropriate for the symbol's type. The DWARF
19457 standard (v4) is not entirely clear about the meaning of using
19458 DW_FORM_dataN for a constant with a signed type, where the type is
19459 wider than the data. The conclusion of a discussion on the DWARF
19460 list was that this is unspecified. We choose to always zero-extend
19461 because that is the interpretation long in use by GCC. */
19464 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19465 struct dwarf2_cu *cu, LONGEST *value, int bits)
19467 struct objfile *objfile = cu->objfile;
19468 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19469 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19470 LONGEST l = DW_UNSND (attr);
19472 if (bits < sizeof (*value) * 8)
19474 l &= ((LONGEST) 1 << bits) - 1;
19477 else if (bits == sizeof (*value) * 8)
19481 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19482 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19489 /* Read a constant value from an attribute. Either set *VALUE, or if
19490 the value does not fit in *VALUE, set *BYTES - either already
19491 allocated on the objfile obstack, or newly allocated on OBSTACK,
19492 or, set *BATON, if we translated the constant to a location
19496 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19497 const char *name, struct obstack *obstack,
19498 struct dwarf2_cu *cu,
19499 LONGEST *value, const gdb_byte **bytes,
19500 struct dwarf2_locexpr_baton **baton)
19502 struct objfile *objfile = cu->objfile;
19503 struct comp_unit_head *cu_header = &cu->header;
19504 struct dwarf_block *blk;
19505 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19506 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19512 switch (attr->form)
19515 case DW_FORM_GNU_addr_index:
19519 if (TYPE_LENGTH (type) != cu_header->addr_size)
19520 dwarf2_const_value_length_mismatch_complaint (name,
19521 cu_header->addr_size,
19522 TYPE_LENGTH (type));
19523 /* Symbols of this form are reasonably rare, so we just
19524 piggyback on the existing location code rather than writing
19525 a new implementation of symbol_computed_ops. */
19526 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19527 (*baton)->per_cu = cu->per_cu;
19528 gdb_assert ((*baton)->per_cu);
19530 (*baton)->size = 2 + cu_header->addr_size;
19531 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19532 (*baton)->data = data;
19534 data[0] = DW_OP_addr;
19535 store_unsigned_integer (&data[1], cu_header->addr_size,
19536 byte_order, DW_ADDR (attr));
19537 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19540 case DW_FORM_string:
19542 case DW_FORM_GNU_str_index:
19543 case DW_FORM_GNU_strp_alt:
19544 /* DW_STRING is already allocated on the objfile obstack, point
19546 *bytes = (const gdb_byte *) DW_STRING (attr);
19548 case DW_FORM_block1:
19549 case DW_FORM_block2:
19550 case DW_FORM_block4:
19551 case DW_FORM_block:
19552 case DW_FORM_exprloc:
19553 case DW_FORM_data16:
19554 blk = DW_BLOCK (attr);
19555 if (TYPE_LENGTH (type) != blk->size)
19556 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19557 TYPE_LENGTH (type));
19558 *bytes = blk->data;
19561 /* The DW_AT_const_value attributes are supposed to carry the
19562 symbol's value "represented as it would be on the target
19563 architecture." By the time we get here, it's already been
19564 converted to host endianness, so we just need to sign- or
19565 zero-extend it as appropriate. */
19566 case DW_FORM_data1:
19567 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19569 case DW_FORM_data2:
19570 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19572 case DW_FORM_data4:
19573 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19575 case DW_FORM_data8:
19576 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19579 case DW_FORM_sdata:
19580 case DW_FORM_implicit_const:
19581 *value = DW_SND (attr);
19584 case DW_FORM_udata:
19585 *value = DW_UNSND (attr);
19589 complaint (&symfile_complaints,
19590 _("unsupported const value attribute form: '%s'"),
19591 dwarf_form_name (attr->form));
19598 /* Copy constant value from an attribute to a symbol. */
19601 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19602 struct dwarf2_cu *cu)
19604 struct objfile *objfile = cu->objfile;
19606 const gdb_byte *bytes;
19607 struct dwarf2_locexpr_baton *baton;
19609 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19610 SYMBOL_PRINT_NAME (sym),
19611 &objfile->objfile_obstack, cu,
19612 &value, &bytes, &baton);
19616 SYMBOL_LOCATION_BATON (sym) = baton;
19617 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19619 else if (bytes != NULL)
19621 SYMBOL_VALUE_BYTES (sym) = bytes;
19622 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19626 SYMBOL_VALUE (sym) = value;
19627 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19631 /* Return the type of the die in question using its DW_AT_type attribute. */
19633 static struct type *
19634 die_type (struct die_info *die, struct dwarf2_cu *cu)
19636 struct attribute *type_attr;
19638 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19641 /* A missing DW_AT_type represents a void type. */
19642 return objfile_type (cu->objfile)->builtin_void;
19645 return lookup_die_type (die, type_attr, cu);
19648 /* True iff CU's producer generates GNAT Ada auxiliary information
19649 that allows to find parallel types through that information instead
19650 of having to do expensive parallel lookups by type name. */
19653 need_gnat_info (struct dwarf2_cu *cu)
19655 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19656 of GNAT produces this auxiliary information, without any indication
19657 that it is produced. Part of enhancing the FSF version of GNAT
19658 to produce that information will be to put in place an indicator
19659 that we can use in order to determine whether the descriptive type
19660 info is available or not. One suggestion that has been made is
19661 to use a new attribute, attached to the CU die. For now, assume
19662 that the descriptive type info is not available. */
19666 /* Return the auxiliary type of the die in question using its
19667 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19668 attribute is not present. */
19670 static struct type *
19671 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19673 struct attribute *type_attr;
19675 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19679 return lookup_die_type (die, type_attr, cu);
19682 /* If DIE has a descriptive_type attribute, then set the TYPE's
19683 descriptive type accordingly. */
19686 set_descriptive_type (struct type *type, struct die_info *die,
19687 struct dwarf2_cu *cu)
19689 struct type *descriptive_type = die_descriptive_type (die, cu);
19691 if (descriptive_type)
19693 ALLOCATE_GNAT_AUX_TYPE (type);
19694 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19698 /* Return the containing type of the die in question using its
19699 DW_AT_containing_type attribute. */
19701 static struct type *
19702 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19704 struct attribute *type_attr;
19706 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19708 error (_("Dwarf Error: Problem turning containing type into gdb type "
19709 "[in module %s]"), objfile_name (cu->objfile));
19711 return lookup_die_type (die, type_attr, cu);
19714 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19716 static struct type *
19717 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19719 struct objfile *objfile = dwarf2_per_objfile->objfile;
19720 char *message, *saved;
19722 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19723 objfile_name (objfile),
19724 to_underlying (cu->header.sect_off),
19725 to_underlying (die->sect_off));
19726 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19727 message, strlen (message));
19730 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19733 /* Look up the type of DIE in CU using its type attribute ATTR.
19734 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19735 DW_AT_containing_type.
19736 If there is no type substitute an error marker. */
19738 static struct type *
19739 lookup_die_type (struct die_info *die, const struct attribute *attr,
19740 struct dwarf2_cu *cu)
19742 struct objfile *objfile = cu->objfile;
19743 struct type *this_type;
19745 gdb_assert (attr->name == DW_AT_type
19746 || attr->name == DW_AT_GNAT_descriptive_type
19747 || attr->name == DW_AT_containing_type);
19749 /* First see if we have it cached. */
19751 if (attr->form == DW_FORM_GNU_ref_alt)
19753 struct dwarf2_per_cu_data *per_cu;
19754 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19756 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19757 this_type = get_die_type_at_offset (sect_off, per_cu);
19759 else if (attr_form_is_ref (attr))
19761 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
19763 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
19765 else if (attr->form == DW_FORM_ref_sig8)
19767 ULONGEST signature = DW_SIGNATURE (attr);
19769 return get_signatured_type (die, signature, cu);
19773 complaint (&symfile_complaints,
19774 _("Dwarf Error: Bad type attribute %s in DIE"
19775 " at 0x%x [in module %s]"),
19776 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
19777 objfile_name (objfile));
19778 return build_error_marker_type (cu, die);
19781 /* If not cached we need to read it in. */
19783 if (this_type == NULL)
19785 struct die_info *type_die = NULL;
19786 struct dwarf2_cu *type_cu = cu;
19788 if (attr_form_is_ref (attr))
19789 type_die = follow_die_ref (die, attr, &type_cu);
19790 if (type_die == NULL)
19791 return build_error_marker_type (cu, die);
19792 /* If we find the type now, it's probably because the type came
19793 from an inter-CU reference and the type's CU got expanded before
19795 this_type = read_type_die (type_die, type_cu);
19798 /* If we still don't have a type use an error marker. */
19800 if (this_type == NULL)
19801 return build_error_marker_type (cu, die);
19806 /* Return the type in DIE, CU.
19807 Returns NULL for invalid types.
19809 This first does a lookup in die_type_hash,
19810 and only reads the die in if necessary.
19812 NOTE: This can be called when reading in partial or full symbols. */
19814 static struct type *
19815 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19817 struct type *this_type;
19819 this_type = get_die_type (die, cu);
19823 return read_type_die_1 (die, cu);
19826 /* Read the type in DIE, CU.
19827 Returns NULL for invalid types. */
19829 static struct type *
19830 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19832 struct type *this_type = NULL;
19836 case DW_TAG_class_type:
19837 case DW_TAG_interface_type:
19838 case DW_TAG_structure_type:
19839 case DW_TAG_union_type:
19840 this_type = read_structure_type (die, cu);
19842 case DW_TAG_enumeration_type:
19843 this_type = read_enumeration_type (die, cu);
19845 case DW_TAG_subprogram:
19846 case DW_TAG_subroutine_type:
19847 case DW_TAG_inlined_subroutine:
19848 this_type = read_subroutine_type (die, cu);
19850 case DW_TAG_array_type:
19851 this_type = read_array_type (die, cu);
19853 case DW_TAG_set_type:
19854 this_type = read_set_type (die, cu);
19856 case DW_TAG_pointer_type:
19857 this_type = read_tag_pointer_type (die, cu);
19859 case DW_TAG_ptr_to_member_type:
19860 this_type = read_tag_ptr_to_member_type (die, cu);
19862 case DW_TAG_reference_type:
19863 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19865 case DW_TAG_rvalue_reference_type:
19866 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19868 case DW_TAG_const_type:
19869 this_type = read_tag_const_type (die, cu);
19871 case DW_TAG_volatile_type:
19872 this_type = read_tag_volatile_type (die, cu);
19874 case DW_TAG_restrict_type:
19875 this_type = read_tag_restrict_type (die, cu);
19877 case DW_TAG_string_type:
19878 this_type = read_tag_string_type (die, cu);
19880 case DW_TAG_typedef:
19881 this_type = read_typedef (die, cu);
19883 case DW_TAG_subrange_type:
19884 this_type = read_subrange_type (die, cu);
19886 case DW_TAG_base_type:
19887 this_type = read_base_type (die, cu);
19889 case DW_TAG_unspecified_type:
19890 this_type = read_unspecified_type (die, cu);
19892 case DW_TAG_namespace:
19893 this_type = read_namespace_type (die, cu);
19895 case DW_TAG_module:
19896 this_type = read_module_type (die, cu);
19898 case DW_TAG_atomic_type:
19899 this_type = read_tag_atomic_type (die, cu);
19902 complaint (&symfile_complaints,
19903 _("unexpected tag in read_type_die: '%s'"),
19904 dwarf_tag_name (die->tag));
19911 /* See if we can figure out if the class lives in a namespace. We do
19912 this by looking for a member function; its demangled name will
19913 contain namespace info, if there is any.
19914 Return the computed name or NULL.
19915 Space for the result is allocated on the objfile's obstack.
19916 This is the full-die version of guess_partial_die_structure_name.
19917 In this case we know DIE has no useful parent. */
19920 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19922 struct die_info *spec_die;
19923 struct dwarf2_cu *spec_cu;
19924 struct die_info *child;
19927 spec_die = die_specification (die, &spec_cu);
19928 if (spec_die != NULL)
19934 for (child = die->child;
19936 child = child->sibling)
19938 if (child->tag == DW_TAG_subprogram)
19940 const char *linkage_name = dw2_linkage_name (child, cu);
19942 if (linkage_name != NULL)
19945 = language_class_name_from_physname (cu->language_defn,
19949 if (actual_name != NULL)
19951 const char *die_name = dwarf2_name (die, cu);
19953 if (die_name != NULL
19954 && strcmp (die_name, actual_name) != 0)
19956 /* Strip off the class name from the full name.
19957 We want the prefix. */
19958 int die_name_len = strlen (die_name);
19959 int actual_name_len = strlen (actual_name);
19961 /* Test for '::' as a sanity check. */
19962 if (actual_name_len > die_name_len + 2
19963 && actual_name[actual_name_len
19964 - die_name_len - 1] == ':')
19965 name = (char *) obstack_copy0 (
19966 &cu->objfile->per_bfd->storage_obstack,
19967 actual_name, actual_name_len - die_name_len - 2);
19970 xfree (actual_name);
19979 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19980 prefix part in such case. See
19981 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19983 static const char *
19984 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19986 struct attribute *attr;
19989 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19990 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19993 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19996 attr = dw2_linkage_name_attr (die, cu);
19997 if (attr == NULL || DW_STRING (attr) == NULL)
20000 /* dwarf2_name had to be already called. */
20001 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20003 /* Strip the base name, keep any leading namespaces/classes. */
20004 base = strrchr (DW_STRING (attr), ':');
20005 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20008 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20010 &base[-1] - DW_STRING (attr));
20013 /* Return the name of the namespace/class that DIE is defined within,
20014 or "" if we can't tell. The caller should not xfree the result.
20016 For example, if we're within the method foo() in the following
20026 then determine_prefix on foo's die will return "N::C". */
20028 static const char *
20029 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20031 struct die_info *parent, *spec_die;
20032 struct dwarf2_cu *spec_cu;
20033 struct type *parent_type;
20034 const char *retval;
20036 if (cu->language != language_cplus
20037 && cu->language != language_fortran && cu->language != language_d
20038 && cu->language != language_rust)
20041 retval = anonymous_struct_prefix (die, cu);
20045 /* We have to be careful in the presence of DW_AT_specification.
20046 For example, with GCC 3.4, given the code
20050 // Definition of N::foo.
20054 then we'll have a tree of DIEs like this:
20056 1: DW_TAG_compile_unit
20057 2: DW_TAG_namespace // N
20058 3: DW_TAG_subprogram // declaration of N::foo
20059 4: DW_TAG_subprogram // definition of N::foo
20060 DW_AT_specification // refers to die #3
20062 Thus, when processing die #4, we have to pretend that we're in
20063 the context of its DW_AT_specification, namely the contex of die
20066 spec_die = die_specification (die, &spec_cu);
20067 if (spec_die == NULL)
20068 parent = die->parent;
20071 parent = spec_die->parent;
20075 if (parent == NULL)
20077 else if (parent->building_fullname)
20080 const char *parent_name;
20082 /* It has been seen on RealView 2.2 built binaries,
20083 DW_TAG_template_type_param types actually _defined_ as
20084 children of the parent class:
20087 template class <class Enum> Class{};
20088 Class<enum E> class_e;
20090 1: DW_TAG_class_type (Class)
20091 2: DW_TAG_enumeration_type (E)
20092 3: DW_TAG_enumerator (enum1:0)
20093 3: DW_TAG_enumerator (enum2:1)
20095 2: DW_TAG_template_type_param
20096 DW_AT_type DW_FORM_ref_udata (E)
20098 Besides being broken debug info, it can put GDB into an
20099 infinite loop. Consider:
20101 When we're building the full name for Class<E>, we'll start
20102 at Class, and go look over its template type parameters,
20103 finding E. We'll then try to build the full name of E, and
20104 reach here. We're now trying to build the full name of E,
20105 and look over the parent DIE for containing scope. In the
20106 broken case, if we followed the parent DIE of E, we'd again
20107 find Class, and once again go look at its template type
20108 arguments, etc., etc. Simply don't consider such parent die
20109 as source-level parent of this die (it can't be, the language
20110 doesn't allow it), and break the loop here. */
20111 name = dwarf2_name (die, cu);
20112 parent_name = dwarf2_name (parent, cu);
20113 complaint (&symfile_complaints,
20114 _("template param type '%s' defined within parent '%s'"),
20115 name ? name : "<unknown>",
20116 parent_name ? parent_name : "<unknown>");
20120 switch (parent->tag)
20122 case DW_TAG_namespace:
20123 parent_type = read_type_die (parent, cu);
20124 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20125 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20126 Work around this problem here. */
20127 if (cu->language == language_cplus
20128 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
20130 /* We give a name to even anonymous namespaces. */
20131 return TYPE_TAG_NAME (parent_type);
20132 case DW_TAG_class_type:
20133 case DW_TAG_interface_type:
20134 case DW_TAG_structure_type:
20135 case DW_TAG_union_type:
20136 case DW_TAG_module:
20137 parent_type = read_type_die (parent, cu);
20138 if (TYPE_TAG_NAME (parent_type) != NULL)
20139 return TYPE_TAG_NAME (parent_type);
20141 /* An anonymous structure is only allowed non-static data
20142 members; no typedefs, no member functions, et cetera.
20143 So it does not need a prefix. */
20145 case DW_TAG_compile_unit:
20146 case DW_TAG_partial_unit:
20147 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20148 if (cu->language == language_cplus
20149 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
20150 && die->child != NULL
20151 && (die->tag == DW_TAG_class_type
20152 || die->tag == DW_TAG_structure_type
20153 || die->tag == DW_TAG_union_type))
20155 char *name = guess_full_die_structure_name (die, cu);
20160 case DW_TAG_enumeration_type:
20161 parent_type = read_type_die (parent, cu);
20162 if (TYPE_DECLARED_CLASS (parent_type))
20164 if (TYPE_TAG_NAME (parent_type) != NULL)
20165 return TYPE_TAG_NAME (parent_type);
20168 /* Fall through. */
20170 return determine_prefix (parent, cu);
20174 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20175 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20176 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20177 an obconcat, otherwise allocate storage for the result. The CU argument is
20178 used to determine the language and hence, the appropriate separator. */
20180 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20183 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20184 int physname, struct dwarf2_cu *cu)
20186 const char *lead = "";
20189 if (suffix == NULL || suffix[0] == '\0'
20190 || prefix == NULL || prefix[0] == '\0')
20192 else if (cu->language == language_d)
20194 /* For D, the 'main' function could be defined in any module, but it
20195 should never be prefixed. */
20196 if (strcmp (suffix, "D main") == 0)
20204 else if (cu->language == language_fortran && physname)
20206 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20207 DW_AT_MIPS_linkage_name is preferred and used instead. */
20215 if (prefix == NULL)
20217 if (suffix == NULL)
20224 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20226 strcpy (retval, lead);
20227 strcat (retval, prefix);
20228 strcat (retval, sep);
20229 strcat (retval, suffix);
20234 /* We have an obstack. */
20235 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20239 /* Return sibling of die, NULL if no sibling. */
20241 static struct die_info *
20242 sibling_die (struct die_info *die)
20244 return die->sibling;
20247 /* Get name of a die, return NULL if not found. */
20249 static const char *
20250 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20251 struct obstack *obstack)
20253 if (name && cu->language == language_cplus)
20255 std::string canon_name = cp_canonicalize_string (name);
20257 if (!canon_name.empty ())
20259 if (canon_name != name)
20260 name = (const char *) obstack_copy0 (obstack,
20261 canon_name.c_str (),
20262 canon_name.length ());
20269 /* Get name of a die, return NULL if not found.
20270 Anonymous namespaces are converted to their magic string. */
20272 static const char *
20273 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20275 struct attribute *attr;
20277 attr = dwarf2_attr (die, DW_AT_name, cu);
20278 if ((!attr || !DW_STRING (attr))
20279 && die->tag != DW_TAG_namespace
20280 && die->tag != DW_TAG_class_type
20281 && die->tag != DW_TAG_interface_type
20282 && die->tag != DW_TAG_structure_type
20283 && die->tag != DW_TAG_union_type)
20288 case DW_TAG_compile_unit:
20289 case DW_TAG_partial_unit:
20290 /* Compilation units have a DW_AT_name that is a filename, not
20291 a source language identifier. */
20292 case DW_TAG_enumeration_type:
20293 case DW_TAG_enumerator:
20294 /* These tags always have simple identifiers already; no need
20295 to canonicalize them. */
20296 return DW_STRING (attr);
20298 case DW_TAG_namespace:
20299 if (attr != NULL && DW_STRING (attr) != NULL)
20300 return DW_STRING (attr);
20301 return CP_ANONYMOUS_NAMESPACE_STR;
20303 case DW_TAG_class_type:
20304 case DW_TAG_interface_type:
20305 case DW_TAG_structure_type:
20306 case DW_TAG_union_type:
20307 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20308 structures or unions. These were of the form "._%d" in GCC 4.1,
20309 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20310 and GCC 4.4. We work around this problem by ignoring these. */
20311 if (attr && DW_STRING (attr)
20312 && (startswith (DW_STRING (attr), "._")
20313 || startswith (DW_STRING (attr), "<anonymous")))
20316 /* GCC might emit a nameless typedef that has a linkage name. See
20317 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20318 if (!attr || DW_STRING (attr) == NULL)
20320 char *demangled = NULL;
20322 attr = dw2_linkage_name_attr (die, cu);
20323 if (attr == NULL || DW_STRING (attr) == NULL)
20326 /* Avoid demangling DW_STRING (attr) the second time on a second
20327 call for the same DIE. */
20328 if (!DW_STRING_IS_CANONICAL (attr))
20329 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20335 /* FIXME: we already did this for the partial symbol... */
20338 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20339 demangled, strlen (demangled)));
20340 DW_STRING_IS_CANONICAL (attr) = 1;
20343 /* Strip any leading namespaces/classes, keep only the base name.
20344 DW_AT_name for named DIEs does not contain the prefixes. */
20345 base = strrchr (DW_STRING (attr), ':');
20346 if (base && base > DW_STRING (attr) && base[-1] == ':')
20349 return DW_STRING (attr);
20358 if (!DW_STRING_IS_CANONICAL (attr))
20361 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20362 &cu->objfile->per_bfd->storage_obstack);
20363 DW_STRING_IS_CANONICAL (attr) = 1;
20365 return DW_STRING (attr);
20368 /* Return the die that this die in an extension of, or NULL if there
20369 is none. *EXT_CU is the CU containing DIE on input, and the CU
20370 containing the return value on output. */
20372 static struct die_info *
20373 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20375 struct attribute *attr;
20377 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20381 return follow_die_ref (die, attr, ext_cu);
20384 /* Convert a DIE tag into its string name. */
20386 static const char *
20387 dwarf_tag_name (unsigned tag)
20389 const char *name = get_DW_TAG_name (tag);
20392 return "DW_TAG_<unknown>";
20397 /* Convert a DWARF attribute code into its string name. */
20399 static const char *
20400 dwarf_attr_name (unsigned attr)
20404 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20405 if (attr == DW_AT_MIPS_fde)
20406 return "DW_AT_MIPS_fde";
20408 if (attr == DW_AT_HP_block_index)
20409 return "DW_AT_HP_block_index";
20412 name = get_DW_AT_name (attr);
20415 return "DW_AT_<unknown>";
20420 /* Convert a DWARF value form code into its string name. */
20422 static const char *
20423 dwarf_form_name (unsigned form)
20425 const char *name = get_DW_FORM_name (form);
20428 return "DW_FORM_<unknown>";
20433 static const char *
20434 dwarf_bool_name (unsigned mybool)
20442 /* Convert a DWARF type code into its string name. */
20444 static const char *
20445 dwarf_type_encoding_name (unsigned enc)
20447 const char *name = get_DW_ATE_name (enc);
20450 return "DW_ATE_<unknown>";
20456 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20460 print_spaces (indent, f);
20461 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20462 dwarf_tag_name (die->tag), die->abbrev,
20463 to_underlying (die->sect_off));
20465 if (die->parent != NULL)
20467 print_spaces (indent, f);
20468 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20469 to_underlying (die->parent->sect_off));
20472 print_spaces (indent, f);
20473 fprintf_unfiltered (f, " has children: %s\n",
20474 dwarf_bool_name (die->child != NULL));
20476 print_spaces (indent, f);
20477 fprintf_unfiltered (f, " attributes:\n");
20479 for (i = 0; i < die->num_attrs; ++i)
20481 print_spaces (indent, f);
20482 fprintf_unfiltered (f, " %s (%s) ",
20483 dwarf_attr_name (die->attrs[i].name),
20484 dwarf_form_name (die->attrs[i].form));
20486 switch (die->attrs[i].form)
20489 case DW_FORM_GNU_addr_index:
20490 fprintf_unfiltered (f, "address: ");
20491 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20493 case DW_FORM_block2:
20494 case DW_FORM_block4:
20495 case DW_FORM_block:
20496 case DW_FORM_block1:
20497 fprintf_unfiltered (f, "block: size %s",
20498 pulongest (DW_BLOCK (&die->attrs[i])->size));
20500 case DW_FORM_exprloc:
20501 fprintf_unfiltered (f, "expression: size %s",
20502 pulongest (DW_BLOCK (&die->attrs[i])->size));
20504 case DW_FORM_data16:
20505 fprintf_unfiltered (f, "constant of 16 bytes");
20507 case DW_FORM_ref_addr:
20508 fprintf_unfiltered (f, "ref address: ");
20509 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20511 case DW_FORM_GNU_ref_alt:
20512 fprintf_unfiltered (f, "alt ref address: ");
20513 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20519 case DW_FORM_ref_udata:
20520 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20521 (long) (DW_UNSND (&die->attrs[i])));
20523 case DW_FORM_data1:
20524 case DW_FORM_data2:
20525 case DW_FORM_data4:
20526 case DW_FORM_data8:
20527 case DW_FORM_udata:
20528 case DW_FORM_sdata:
20529 fprintf_unfiltered (f, "constant: %s",
20530 pulongest (DW_UNSND (&die->attrs[i])));
20532 case DW_FORM_sec_offset:
20533 fprintf_unfiltered (f, "section offset: %s",
20534 pulongest (DW_UNSND (&die->attrs[i])));
20536 case DW_FORM_ref_sig8:
20537 fprintf_unfiltered (f, "signature: %s",
20538 hex_string (DW_SIGNATURE (&die->attrs[i])));
20540 case DW_FORM_string:
20542 case DW_FORM_line_strp:
20543 case DW_FORM_GNU_str_index:
20544 case DW_FORM_GNU_strp_alt:
20545 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20546 DW_STRING (&die->attrs[i])
20547 ? DW_STRING (&die->attrs[i]) : "",
20548 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20551 if (DW_UNSND (&die->attrs[i]))
20552 fprintf_unfiltered (f, "flag: TRUE");
20554 fprintf_unfiltered (f, "flag: FALSE");
20556 case DW_FORM_flag_present:
20557 fprintf_unfiltered (f, "flag: TRUE");
20559 case DW_FORM_indirect:
20560 /* The reader will have reduced the indirect form to
20561 the "base form" so this form should not occur. */
20562 fprintf_unfiltered (f,
20563 "unexpected attribute form: DW_FORM_indirect");
20565 case DW_FORM_implicit_const:
20566 fprintf_unfiltered (f, "constant: %s",
20567 plongest (DW_SND (&die->attrs[i])));
20570 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20571 die->attrs[i].form);
20574 fprintf_unfiltered (f, "\n");
20579 dump_die_for_error (struct die_info *die)
20581 dump_die_shallow (gdb_stderr, 0, die);
20585 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20587 int indent = level * 4;
20589 gdb_assert (die != NULL);
20591 if (level >= max_level)
20594 dump_die_shallow (f, indent, die);
20596 if (die->child != NULL)
20598 print_spaces (indent, f);
20599 fprintf_unfiltered (f, " Children:");
20600 if (level + 1 < max_level)
20602 fprintf_unfiltered (f, "\n");
20603 dump_die_1 (f, level + 1, max_level, die->child);
20607 fprintf_unfiltered (f,
20608 " [not printed, max nesting level reached]\n");
20612 if (die->sibling != NULL && level > 0)
20614 dump_die_1 (f, level, max_level, die->sibling);
20618 /* This is called from the pdie macro in gdbinit.in.
20619 It's not static so gcc will keep a copy callable from gdb. */
20622 dump_die (struct die_info *die, int max_level)
20624 dump_die_1 (gdb_stdlog, 0, max_level, die);
20628 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20632 slot = htab_find_slot_with_hash (cu->die_hash, die,
20633 to_underlying (die->sect_off),
20639 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20643 dwarf2_get_ref_die_offset (const struct attribute *attr)
20645 if (attr_form_is_ref (attr))
20646 return (sect_offset) DW_UNSND (attr);
20648 complaint (&symfile_complaints,
20649 _("unsupported die ref attribute form: '%s'"),
20650 dwarf_form_name (attr->form));
20654 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20655 * the value held by the attribute is not constant. */
20658 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20660 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
20661 return DW_SND (attr);
20662 else if (attr->form == DW_FORM_udata
20663 || attr->form == DW_FORM_data1
20664 || attr->form == DW_FORM_data2
20665 || attr->form == DW_FORM_data4
20666 || attr->form == DW_FORM_data8)
20667 return DW_UNSND (attr);
20670 /* For DW_FORM_data16 see attr_form_is_constant. */
20671 complaint (&symfile_complaints,
20672 _("Attribute value is not a constant (%s)"),
20673 dwarf_form_name (attr->form));
20674 return default_value;
20678 /* Follow reference or signature attribute ATTR of SRC_DIE.
20679 On entry *REF_CU is the CU of SRC_DIE.
20680 On exit *REF_CU is the CU of the result. */
20682 static struct die_info *
20683 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20684 struct dwarf2_cu **ref_cu)
20686 struct die_info *die;
20688 if (attr_form_is_ref (attr))
20689 die = follow_die_ref (src_die, attr, ref_cu);
20690 else if (attr->form == DW_FORM_ref_sig8)
20691 die = follow_die_sig (src_die, attr, ref_cu);
20694 dump_die_for_error (src_die);
20695 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20696 objfile_name ((*ref_cu)->objfile));
20702 /* Follow reference OFFSET.
20703 On entry *REF_CU is the CU of the source die referencing OFFSET.
20704 On exit *REF_CU is the CU of the result.
20705 Returns NULL if OFFSET is invalid. */
20707 static struct die_info *
20708 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20709 struct dwarf2_cu **ref_cu)
20711 struct die_info temp_die;
20712 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20714 gdb_assert (cu->per_cu != NULL);
20718 if (cu->per_cu->is_debug_types)
20720 /* .debug_types CUs cannot reference anything outside their CU.
20721 If they need to, they have to reference a signatured type via
20722 DW_FORM_ref_sig8. */
20723 if (!offset_in_cu_p (&cu->header, sect_off))
20726 else if (offset_in_dwz != cu->per_cu->is_dwz
20727 || !offset_in_cu_p (&cu->header, sect_off))
20729 struct dwarf2_per_cu_data *per_cu;
20731 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20734 /* If necessary, add it to the queue and load its DIEs. */
20735 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20736 load_full_comp_unit (per_cu, cu->language);
20738 target_cu = per_cu->cu;
20740 else if (cu->dies == NULL)
20742 /* We're loading full DIEs during partial symbol reading. */
20743 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20744 load_full_comp_unit (cu->per_cu, language_minimal);
20747 *ref_cu = target_cu;
20748 temp_die.sect_off = sect_off;
20749 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20751 to_underlying (sect_off));
20754 /* Follow reference attribute ATTR of SRC_DIE.
20755 On entry *REF_CU is the CU of SRC_DIE.
20756 On exit *REF_CU is the CU of the result. */
20758 static struct die_info *
20759 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20760 struct dwarf2_cu **ref_cu)
20762 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20763 struct dwarf2_cu *cu = *ref_cu;
20764 struct die_info *die;
20766 die = follow_die_offset (sect_off,
20767 (attr->form == DW_FORM_GNU_ref_alt
20768 || cu->per_cu->is_dwz),
20771 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20772 "at 0x%x [in module %s]"),
20773 to_underlying (sect_off), to_underlying (src_die->sect_off),
20774 objfile_name (cu->objfile));
20779 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
20780 Returned value is intended for DW_OP_call*. Returned
20781 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20783 struct dwarf2_locexpr_baton
20784 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20785 struct dwarf2_per_cu_data *per_cu,
20786 CORE_ADDR (*get_frame_pc) (void *baton),
20789 struct dwarf2_cu *cu;
20790 struct die_info *die;
20791 struct attribute *attr;
20792 struct dwarf2_locexpr_baton retval;
20794 dw2_setup (per_cu->objfile);
20796 if (per_cu->cu == NULL)
20801 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20802 Instead just throw an error, not much else we can do. */
20803 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20804 to_underlying (sect_off), objfile_name (per_cu->objfile));
20807 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20809 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20810 to_underlying (sect_off), objfile_name (per_cu->objfile));
20812 attr = dwarf2_attr (die, DW_AT_location, cu);
20815 /* DWARF: "If there is no such attribute, then there is no effect.".
20816 DATA is ignored if SIZE is 0. */
20818 retval.data = NULL;
20821 else if (attr_form_is_section_offset (attr))
20823 struct dwarf2_loclist_baton loclist_baton;
20824 CORE_ADDR pc = (*get_frame_pc) (baton);
20827 fill_in_loclist_baton (cu, &loclist_baton, attr);
20829 retval.data = dwarf2_find_location_expression (&loclist_baton,
20831 retval.size = size;
20835 if (!attr_form_is_block (attr))
20836 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20837 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20838 to_underlying (sect_off), objfile_name (per_cu->objfile));
20840 retval.data = DW_BLOCK (attr)->data;
20841 retval.size = DW_BLOCK (attr)->size;
20843 retval.per_cu = cu->per_cu;
20845 age_cached_comp_units ();
20850 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20853 struct dwarf2_locexpr_baton
20854 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20855 struct dwarf2_per_cu_data *per_cu,
20856 CORE_ADDR (*get_frame_pc) (void *baton),
20859 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20861 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
20864 /* Write a constant of a given type as target-ordered bytes into
20867 static const gdb_byte *
20868 write_constant_as_bytes (struct obstack *obstack,
20869 enum bfd_endian byte_order,
20876 *len = TYPE_LENGTH (type);
20877 result = (gdb_byte *) obstack_alloc (obstack, *len);
20878 store_unsigned_integer (result, *len, byte_order, value);
20883 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20884 pointer to the constant bytes and set LEN to the length of the
20885 data. If memory is needed, allocate it on OBSTACK. If the DIE
20886 does not have a DW_AT_const_value, return NULL. */
20889 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20890 struct dwarf2_per_cu_data *per_cu,
20891 struct obstack *obstack,
20894 struct dwarf2_cu *cu;
20895 struct die_info *die;
20896 struct attribute *attr;
20897 const gdb_byte *result = NULL;
20900 enum bfd_endian byte_order;
20902 dw2_setup (per_cu->objfile);
20904 if (per_cu->cu == NULL)
20909 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20910 Instead just throw an error, not much else we can do. */
20911 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20912 to_underlying (sect_off), objfile_name (per_cu->objfile));
20915 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20917 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20918 to_underlying (sect_off), objfile_name (per_cu->objfile));
20921 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20925 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20926 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20928 switch (attr->form)
20931 case DW_FORM_GNU_addr_index:
20935 *len = cu->header.addr_size;
20936 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20937 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20941 case DW_FORM_string:
20943 case DW_FORM_GNU_str_index:
20944 case DW_FORM_GNU_strp_alt:
20945 /* DW_STRING is already allocated on the objfile obstack, point
20947 result = (const gdb_byte *) DW_STRING (attr);
20948 *len = strlen (DW_STRING (attr));
20950 case DW_FORM_block1:
20951 case DW_FORM_block2:
20952 case DW_FORM_block4:
20953 case DW_FORM_block:
20954 case DW_FORM_exprloc:
20955 case DW_FORM_data16:
20956 result = DW_BLOCK (attr)->data;
20957 *len = DW_BLOCK (attr)->size;
20960 /* The DW_AT_const_value attributes are supposed to carry the
20961 symbol's value "represented as it would be on the target
20962 architecture." By the time we get here, it's already been
20963 converted to host endianness, so we just need to sign- or
20964 zero-extend it as appropriate. */
20965 case DW_FORM_data1:
20966 type = die_type (die, cu);
20967 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20968 if (result == NULL)
20969 result = write_constant_as_bytes (obstack, byte_order,
20972 case DW_FORM_data2:
20973 type = die_type (die, cu);
20974 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20975 if (result == NULL)
20976 result = write_constant_as_bytes (obstack, byte_order,
20979 case DW_FORM_data4:
20980 type = die_type (die, cu);
20981 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20982 if (result == NULL)
20983 result = write_constant_as_bytes (obstack, byte_order,
20986 case DW_FORM_data8:
20987 type = die_type (die, cu);
20988 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20989 if (result == NULL)
20990 result = write_constant_as_bytes (obstack, byte_order,
20994 case DW_FORM_sdata:
20995 case DW_FORM_implicit_const:
20996 type = die_type (die, cu);
20997 result = write_constant_as_bytes (obstack, byte_order,
20998 type, DW_SND (attr), len);
21001 case DW_FORM_udata:
21002 type = die_type (die, cu);
21003 result = write_constant_as_bytes (obstack, byte_order,
21004 type, DW_UNSND (attr), len);
21008 complaint (&symfile_complaints,
21009 _("unsupported const value attribute form: '%s'"),
21010 dwarf_form_name (attr->form));
21017 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
21018 valid type for this die is found. */
21021 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21022 struct dwarf2_per_cu_data *per_cu)
21024 struct dwarf2_cu *cu;
21025 struct die_info *die;
21027 dw2_setup (per_cu->objfile);
21029 if (per_cu->cu == NULL)
21035 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21039 return die_type (die, cu);
21042 /* Return the type of the DIE at DIE_OFFSET in the CU named by
21046 dwarf2_get_die_type (cu_offset die_offset,
21047 struct dwarf2_per_cu_data *per_cu)
21049 dw2_setup (per_cu->objfile);
21051 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21052 return get_die_type_at_offset (die_offset_sect, per_cu);
21055 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21056 On entry *REF_CU is the CU of SRC_DIE.
21057 On exit *REF_CU is the CU of the result.
21058 Returns NULL if the referenced DIE isn't found. */
21060 static struct die_info *
21061 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21062 struct dwarf2_cu **ref_cu)
21064 struct die_info temp_die;
21065 struct dwarf2_cu *sig_cu;
21066 struct die_info *die;
21068 /* While it might be nice to assert sig_type->type == NULL here,
21069 we can get here for DW_AT_imported_declaration where we need
21070 the DIE not the type. */
21072 /* If necessary, add it to the queue and load its DIEs. */
21074 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21075 read_signatured_type (sig_type);
21077 sig_cu = sig_type->per_cu.cu;
21078 gdb_assert (sig_cu != NULL);
21079 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21080 temp_die.sect_off = sig_type->type_offset_in_section;
21081 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21082 to_underlying (temp_die.sect_off));
21085 /* For .gdb_index version 7 keep track of included TUs.
21086 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21087 if (dwarf2_per_objfile->index_table != NULL
21088 && dwarf2_per_objfile->index_table->version <= 7)
21090 VEC_safe_push (dwarf2_per_cu_ptr,
21091 (*ref_cu)->per_cu->imported_symtabs,
21102 /* Follow signatured type referenced by ATTR in SRC_DIE.
21103 On entry *REF_CU is the CU of SRC_DIE.
21104 On exit *REF_CU is the CU of the result.
21105 The result is the DIE of the type.
21106 If the referenced type cannot be found an error is thrown. */
21108 static struct die_info *
21109 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21110 struct dwarf2_cu **ref_cu)
21112 ULONGEST signature = DW_SIGNATURE (attr);
21113 struct signatured_type *sig_type;
21114 struct die_info *die;
21116 gdb_assert (attr->form == DW_FORM_ref_sig8);
21118 sig_type = lookup_signatured_type (*ref_cu, signature);
21119 /* sig_type will be NULL if the signatured type is missing from
21121 if (sig_type == NULL)
21123 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21124 " from DIE at 0x%x [in module %s]"),
21125 hex_string (signature), to_underlying (src_die->sect_off),
21126 objfile_name ((*ref_cu)->objfile));
21129 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21132 dump_die_for_error (src_die);
21133 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21134 " from DIE at 0x%x [in module %s]"),
21135 hex_string (signature), to_underlying (src_die->sect_off),
21136 objfile_name ((*ref_cu)->objfile));
21142 /* Get the type specified by SIGNATURE referenced in DIE/CU,
21143 reading in and processing the type unit if necessary. */
21145 static struct type *
21146 get_signatured_type (struct die_info *die, ULONGEST signature,
21147 struct dwarf2_cu *cu)
21149 struct signatured_type *sig_type;
21150 struct dwarf2_cu *type_cu;
21151 struct die_info *type_die;
21154 sig_type = lookup_signatured_type (cu, signature);
21155 /* sig_type will be NULL if the signatured type is missing from
21157 if (sig_type == NULL)
21159 complaint (&symfile_complaints,
21160 _("Dwarf Error: Cannot find signatured DIE %s referenced"
21161 " from DIE at 0x%x [in module %s]"),
21162 hex_string (signature), to_underlying (die->sect_off),
21163 objfile_name (dwarf2_per_objfile->objfile));
21164 return build_error_marker_type (cu, die);
21167 /* If we already know the type we're done. */
21168 if (sig_type->type != NULL)
21169 return sig_type->type;
21172 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21173 if (type_die != NULL)
21175 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21176 is created. This is important, for example, because for c++ classes
21177 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21178 type = read_type_die (type_die, type_cu);
21181 complaint (&symfile_complaints,
21182 _("Dwarf Error: Cannot build signatured type %s"
21183 " referenced from DIE at 0x%x [in module %s]"),
21184 hex_string (signature), to_underlying (die->sect_off),
21185 objfile_name (dwarf2_per_objfile->objfile));
21186 type = build_error_marker_type (cu, die);
21191 complaint (&symfile_complaints,
21192 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21193 " from DIE at 0x%x [in module %s]"),
21194 hex_string (signature), to_underlying (die->sect_off),
21195 objfile_name (dwarf2_per_objfile->objfile));
21196 type = build_error_marker_type (cu, die);
21198 sig_type->type = type;
21203 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21204 reading in and processing the type unit if necessary. */
21206 static struct type *
21207 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21208 struct dwarf2_cu *cu) /* ARI: editCase function */
21210 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21211 if (attr_form_is_ref (attr))
21213 struct dwarf2_cu *type_cu = cu;
21214 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21216 return read_type_die (type_die, type_cu);
21218 else if (attr->form == DW_FORM_ref_sig8)
21220 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21224 complaint (&symfile_complaints,
21225 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21226 " at 0x%x [in module %s]"),
21227 dwarf_form_name (attr->form), to_underlying (die->sect_off),
21228 objfile_name (dwarf2_per_objfile->objfile));
21229 return build_error_marker_type (cu, die);
21233 /* Load the DIEs associated with type unit PER_CU into memory. */
21236 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21238 struct signatured_type *sig_type;
21240 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21241 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21243 /* We have the per_cu, but we need the signatured_type.
21244 Fortunately this is an easy translation. */
21245 gdb_assert (per_cu->is_debug_types);
21246 sig_type = (struct signatured_type *) per_cu;
21248 gdb_assert (per_cu->cu == NULL);
21250 read_signatured_type (sig_type);
21252 gdb_assert (per_cu->cu != NULL);
21255 /* die_reader_func for read_signatured_type.
21256 This is identical to load_full_comp_unit_reader,
21257 but is kept separate for now. */
21260 read_signatured_type_reader (const struct die_reader_specs *reader,
21261 const gdb_byte *info_ptr,
21262 struct die_info *comp_unit_die,
21266 struct dwarf2_cu *cu = reader->cu;
21268 gdb_assert (cu->die_hash == NULL);
21270 htab_create_alloc_ex (cu->header.length / 12,
21274 &cu->comp_unit_obstack,
21275 hashtab_obstack_allocate,
21276 dummy_obstack_deallocate);
21279 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21280 &info_ptr, comp_unit_die);
21281 cu->dies = comp_unit_die;
21282 /* comp_unit_die is not stored in die_hash, no need. */
21284 /* We try not to read any attributes in this function, because not
21285 all CUs needed for references have been loaded yet, and symbol
21286 table processing isn't initialized. But we have to set the CU language,
21287 or we won't be able to build types correctly.
21288 Similarly, if we do not read the producer, we can not apply
21289 producer-specific interpretation. */
21290 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21293 /* Read in a signatured type and build its CU and DIEs.
21294 If the type is a stub for the real type in a DWO file,
21295 read in the real type from the DWO file as well. */
21298 read_signatured_type (struct signatured_type *sig_type)
21300 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21302 gdb_assert (per_cu->is_debug_types);
21303 gdb_assert (per_cu->cu == NULL);
21305 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21306 read_signatured_type_reader, NULL);
21307 sig_type->per_cu.tu_read = 1;
21310 /* Decode simple location descriptions.
21311 Given a pointer to a dwarf block that defines a location, compute
21312 the location and return the value.
21314 NOTE drow/2003-11-18: This function is called in two situations
21315 now: for the address of static or global variables (partial symbols
21316 only) and for offsets into structures which are expected to be
21317 (more or less) constant. The partial symbol case should go away,
21318 and only the constant case should remain. That will let this
21319 function complain more accurately. A few special modes are allowed
21320 without complaint for global variables (for instance, global
21321 register values and thread-local values).
21323 A location description containing no operations indicates that the
21324 object is optimized out. The return value is 0 for that case.
21325 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21326 callers will only want a very basic result and this can become a
21329 Note that stack[0] is unused except as a default error return. */
21332 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21334 struct objfile *objfile = cu->objfile;
21336 size_t size = blk->size;
21337 const gdb_byte *data = blk->data;
21338 CORE_ADDR stack[64];
21340 unsigned int bytes_read, unsnd;
21346 stack[++stacki] = 0;
21385 stack[++stacki] = op - DW_OP_lit0;
21420 stack[++stacki] = op - DW_OP_reg0;
21422 dwarf2_complex_location_expr_complaint ();
21426 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21428 stack[++stacki] = unsnd;
21430 dwarf2_complex_location_expr_complaint ();
21434 stack[++stacki] = read_address (objfile->obfd, &data[i],
21439 case DW_OP_const1u:
21440 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21444 case DW_OP_const1s:
21445 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21449 case DW_OP_const2u:
21450 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21454 case DW_OP_const2s:
21455 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21459 case DW_OP_const4u:
21460 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21464 case DW_OP_const4s:
21465 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21469 case DW_OP_const8u:
21470 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21475 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21481 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21486 stack[stacki + 1] = stack[stacki];
21491 stack[stacki - 1] += stack[stacki];
21495 case DW_OP_plus_uconst:
21496 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21502 stack[stacki - 1] -= stack[stacki];
21507 /* If we're not the last op, then we definitely can't encode
21508 this using GDB's address_class enum. This is valid for partial
21509 global symbols, although the variable's address will be bogus
21512 dwarf2_complex_location_expr_complaint ();
21515 case DW_OP_GNU_push_tls_address:
21516 case DW_OP_form_tls_address:
21517 /* The top of the stack has the offset from the beginning
21518 of the thread control block at which the variable is located. */
21519 /* Nothing should follow this operator, so the top of stack would
21521 /* This is valid for partial global symbols, but the variable's
21522 address will be bogus in the psymtab. Make it always at least
21523 non-zero to not look as a variable garbage collected by linker
21524 which have DW_OP_addr 0. */
21526 dwarf2_complex_location_expr_complaint ();
21530 case DW_OP_GNU_uninit:
21533 case DW_OP_GNU_addr_index:
21534 case DW_OP_GNU_const_index:
21535 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21542 const char *name = get_DW_OP_name (op);
21545 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21548 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21552 return (stack[stacki]);
21555 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21556 outside of the allocated space. Also enforce minimum>0. */
21557 if (stacki >= ARRAY_SIZE (stack) - 1)
21559 complaint (&symfile_complaints,
21560 _("location description stack overflow"));
21566 complaint (&symfile_complaints,
21567 _("location description stack underflow"));
21571 return (stack[stacki]);
21574 /* memory allocation interface */
21576 static struct dwarf_block *
21577 dwarf_alloc_block (struct dwarf2_cu *cu)
21579 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21582 static struct die_info *
21583 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21585 struct die_info *die;
21586 size_t size = sizeof (struct die_info);
21589 size += (num_attrs - 1) * sizeof (struct attribute);
21591 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21592 memset (die, 0, sizeof (struct die_info));
21597 /* Macro support. */
21599 /* Return file name relative to the compilation directory of file number I in
21600 *LH's file name table. The result is allocated using xmalloc; the caller is
21601 responsible for freeing it. */
21604 file_file_name (int file, struct line_header *lh)
21606 /* Is the file number a valid index into the line header's file name
21607 table? Remember that file numbers start with one, not zero. */
21608 if (1 <= file && file <= lh->file_names.size ())
21610 const file_entry &fe = lh->file_names[file - 1];
21612 if (!IS_ABSOLUTE_PATH (fe.name))
21614 const char *dir = fe.include_dir (lh);
21616 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21618 return xstrdup (fe.name);
21622 /* The compiler produced a bogus file number. We can at least
21623 record the macro definitions made in the file, even if we
21624 won't be able to find the file by name. */
21625 char fake_name[80];
21627 xsnprintf (fake_name, sizeof (fake_name),
21628 "<bad macro file number %d>", file);
21630 complaint (&symfile_complaints,
21631 _("bad file number in macro information (%d)"),
21634 return xstrdup (fake_name);
21638 /* Return the full name of file number I in *LH's file name table.
21639 Use COMP_DIR as the name of the current directory of the
21640 compilation. The result is allocated using xmalloc; the caller is
21641 responsible for freeing it. */
21643 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21645 /* Is the file number a valid index into the line header's file name
21646 table? Remember that file numbers start with one, not zero. */
21647 if (1 <= file && file <= lh->file_names.size ())
21649 char *relative = file_file_name (file, lh);
21651 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21653 return reconcat (relative, comp_dir, SLASH_STRING,
21654 relative, (char *) NULL);
21657 return file_file_name (file, lh);
21661 static struct macro_source_file *
21662 macro_start_file (int file, int line,
21663 struct macro_source_file *current_file,
21664 struct line_header *lh)
21666 /* File name relative to the compilation directory of this source file. */
21667 char *file_name = file_file_name (file, lh);
21669 if (! current_file)
21671 /* Note: We don't create a macro table for this compilation unit
21672 at all until we actually get a filename. */
21673 struct macro_table *macro_table = get_macro_table ();
21675 /* If we have no current file, then this must be the start_file
21676 directive for the compilation unit's main source file. */
21677 current_file = macro_set_main (macro_table, file_name);
21678 macro_define_special (macro_table);
21681 current_file = macro_include (current_file, line, file_name);
21685 return current_file;
21688 static const char *
21689 consume_improper_spaces (const char *p, const char *body)
21693 complaint (&symfile_complaints,
21694 _("macro definition contains spaces "
21695 "in formal argument list:\n`%s'"),
21707 parse_macro_definition (struct macro_source_file *file, int line,
21712 /* The body string takes one of two forms. For object-like macro
21713 definitions, it should be:
21715 <macro name> " " <definition>
21717 For function-like macro definitions, it should be:
21719 <macro name> "() " <definition>
21721 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21723 Spaces may appear only where explicitly indicated, and in the
21726 The Dwarf 2 spec says that an object-like macro's name is always
21727 followed by a space, but versions of GCC around March 2002 omit
21728 the space when the macro's definition is the empty string.
21730 The Dwarf 2 spec says that there should be no spaces between the
21731 formal arguments in a function-like macro's formal argument list,
21732 but versions of GCC around March 2002 include spaces after the
21736 /* Find the extent of the macro name. The macro name is terminated
21737 by either a space or null character (for an object-like macro) or
21738 an opening paren (for a function-like macro). */
21739 for (p = body; *p; p++)
21740 if (*p == ' ' || *p == '(')
21743 if (*p == ' ' || *p == '\0')
21745 /* It's an object-like macro. */
21746 int name_len = p - body;
21747 char *name = savestring (body, name_len);
21748 const char *replacement;
21751 replacement = body + name_len + 1;
21754 dwarf2_macro_malformed_definition_complaint (body);
21755 replacement = body + name_len;
21758 macro_define_object (file, line, name, replacement);
21762 else if (*p == '(')
21764 /* It's a function-like macro. */
21765 char *name = savestring (body, p - body);
21768 char **argv = XNEWVEC (char *, argv_size);
21772 p = consume_improper_spaces (p, body);
21774 /* Parse the formal argument list. */
21775 while (*p && *p != ')')
21777 /* Find the extent of the current argument name. */
21778 const char *arg_start = p;
21780 while (*p && *p != ',' && *p != ')' && *p != ' ')
21783 if (! *p || p == arg_start)
21784 dwarf2_macro_malformed_definition_complaint (body);
21787 /* Make sure argv has room for the new argument. */
21788 if (argc >= argv_size)
21791 argv = XRESIZEVEC (char *, argv, argv_size);
21794 argv[argc++] = savestring (arg_start, p - arg_start);
21797 p = consume_improper_spaces (p, body);
21799 /* Consume the comma, if present. */
21804 p = consume_improper_spaces (p, body);
21813 /* Perfectly formed definition, no complaints. */
21814 macro_define_function (file, line, name,
21815 argc, (const char **) argv,
21817 else if (*p == '\0')
21819 /* Complain, but do define it. */
21820 dwarf2_macro_malformed_definition_complaint (body);
21821 macro_define_function (file, line, name,
21822 argc, (const char **) argv,
21826 /* Just complain. */
21827 dwarf2_macro_malformed_definition_complaint (body);
21830 /* Just complain. */
21831 dwarf2_macro_malformed_definition_complaint (body);
21837 for (i = 0; i < argc; i++)
21843 dwarf2_macro_malformed_definition_complaint (body);
21846 /* Skip some bytes from BYTES according to the form given in FORM.
21847 Returns the new pointer. */
21849 static const gdb_byte *
21850 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21851 enum dwarf_form form,
21852 unsigned int offset_size,
21853 struct dwarf2_section_info *section)
21855 unsigned int bytes_read;
21859 case DW_FORM_data1:
21864 case DW_FORM_data2:
21868 case DW_FORM_data4:
21872 case DW_FORM_data8:
21876 case DW_FORM_data16:
21880 case DW_FORM_string:
21881 read_direct_string (abfd, bytes, &bytes_read);
21882 bytes += bytes_read;
21885 case DW_FORM_sec_offset:
21887 case DW_FORM_GNU_strp_alt:
21888 bytes += offset_size;
21891 case DW_FORM_block:
21892 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21893 bytes += bytes_read;
21896 case DW_FORM_block1:
21897 bytes += 1 + read_1_byte (abfd, bytes);
21899 case DW_FORM_block2:
21900 bytes += 2 + read_2_bytes (abfd, bytes);
21902 case DW_FORM_block4:
21903 bytes += 4 + read_4_bytes (abfd, bytes);
21906 case DW_FORM_sdata:
21907 case DW_FORM_udata:
21908 case DW_FORM_GNU_addr_index:
21909 case DW_FORM_GNU_str_index:
21910 bytes = gdb_skip_leb128 (bytes, buffer_end);
21913 dwarf2_section_buffer_overflow_complaint (section);
21918 case DW_FORM_implicit_const:
21924 complaint (&symfile_complaints,
21925 _("invalid form 0x%x in `%s'"),
21926 form, get_section_name (section));
21934 /* A helper for dwarf_decode_macros that handles skipping an unknown
21935 opcode. Returns an updated pointer to the macro data buffer; or,
21936 on error, issues a complaint and returns NULL. */
21938 static const gdb_byte *
21939 skip_unknown_opcode (unsigned int opcode,
21940 const gdb_byte **opcode_definitions,
21941 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21943 unsigned int offset_size,
21944 struct dwarf2_section_info *section)
21946 unsigned int bytes_read, i;
21948 const gdb_byte *defn;
21950 if (opcode_definitions[opcode] == NULL)
21952 complaint (&symfile_complaints,
21953 _("unrecognized DW_MACFINO opcode 0x%x"),
21958 defn = opcode_definitions[opcode];
21959 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21960 defn += bytes_read;
21962 for (i = 0; i < arg; ++i)
21964 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21965 (enum dwarf_form) defn[i], offset_size,
21967 if (mac_ptr == NULL)
21969 /* skip_form_bytes already issued the complaint. */
21977 /* A helper function which parses the header of a macro section.
21978 If the macro section is the extended (for now called "GNU") type,
21979 then this updates *OFFSET_SIZE. Returns a pointer to just after
21980 the header, or issues a complaint and returns NULL on error. */
21982 static const gdb_byte *
21983 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21985 const gdb_byte *mac_ptr,
21986 unsigned int *offset_size,
21987 int section_is_gnu)
21989 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21991 if (section_is_gnu)
21993 unsigned int version, flags;
21995 version = read_2_bytes (abfd, mac_ptr);
21996 if (version != 4 && version != 5)
21998 complaint (&symfile_complaints,
21999 _("unrecognized version `%d' in .debug_macro section"),
22005 flags = read_1_byte (abfd, mac_ptr);
22007 *offset_size = (flags & 1) ? 8 : 4;
22009 if ((flags & 2) != 0)
22010 /* We don't need the line table offset. */
22011 mac_ptr += *offset_size;
22013 /* Vendor opcode descriptions. */
22014 if ((flags & 4) != 0)
22016 unsigned int i, count;
22018 count = read_1_byte (abfd, mac_ptr);
22020 for (i = 0; i < count; ++i)
22022 unsigned int opcode, bytes_read;
22025 opcode = read_1_byte (abfd, mac_ptr);
22027 opcode_definitions[opcode] = mac_ptr;
22028 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22029 mac_ptr += bytes_read;
22038 /* A helper for dwarf_decode_macros that handles the GNU extensions,
22039 including DW_MACRO_import. */
22042 dwarf_decode_macro_bytes (bfd *abfd,
22043 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
22044 struct macro_source_file *current_file,
22045 struct line_header *lh,
22046 struct dwarf2_section_info *section,
22047 int section_is_gnu, int section_is_dwz,
22048 unsigned int offset_size,
22049 htab_t include_hash)
22051 struct objfile *objfile = dwarf2_per_objfile->objfile;
22052 enum dwarf_macro_record_type macinfo_type;
22053 int at_commandline;
22054 const gdb_byte *opcode_definitions[256];
22056 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22057 &offset_size, section_is_gnu);
22058 if (mac_ptr == NULL)
22060 /* We already issued a complaint. */
22064 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
22065 GDB is still reading the definitions from command line. First
22066 DW_MACINFO_start_file will need to be ignored as it was already executed
22067 to create CURRENT_FILE for the main source holding also the command line
22068 definitions. On first met DW_MACINFO_start_file this flag is reset to
22069 normally execute all the remaining DW_MACINFO_start_file macinfos. */
22071 at_commandline = 1;
22075 /* Do we at least have room for a macinfo type byte? */
22076 if (mac_ptr >= mac_end)
22078 dwarf2_section_buffer_overflow_complaint (section);
22082 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22085 /* Note that we rely on the fact that the corresponding GNU and
22086 DWARF constants are the same. */
22087 switch (macinfo_type)
22089 /* A zero macinfo type indicates the end of the macro
22094 case DW_MACRO_define:
22095 case DW_MACRO_undef:
22096 case DW_MACRO_define_strp:
22097 case DW_MACRO_undef_strp:
22098 case DW_MACRO_define_sup:
22099 case DW_MACRO_undef_sup:
22101 unsigned int bytes_read;
22106 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22107 mac_ptr += bytes_read;
22109 if (macinfo_type == DW_MACRO_define
22110 || macinfo_type == DW_MACRO_undef)
22112 body = read_direct_string (abfd, mac_ptr, &bytes_read);
22113 mac_ptr += bytes_read;
22117 LONGEST str_offset;
22119 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
22120 mac_ptr += offset_size;
22122 if (macinfo_type == DW_MACRO_define_sup
22123 || macinfo_type == DW_MACRO_undef_sup
22126 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22128 body = read_indirect_string_from_dwz (dwz, str_offset);
22131 body = read_indirect_string_at_offset (abfd, str_offset);
22134 is_define = (macinfo_type == DW_MACRO_define
22135 || macinfo_type == DW_MACRO_define_strp
22136 || macinfo_type == DW_MACRO_define_sup);
22137 if (! current_file)
22139 /* DWARF violation as no main source is present. */
22140 complaint (&symfile_complaints,
22141 _("debug info with no main source gives macro %s "
22143 is_define ? _("definition") : _("undefinition"),
22147 if ((line == 0 && !at_commandline)
22148 || (line != 0 && at_commandline))
22149 complaint (&symfile_complaints,
22150 _("debug info gives %s macro %s with %s line %d: %s"),
22151 at_commandline ? _("command-line") : _("in-file"),
22152 is_define ? _("definition") : _("undefinition"),
22153 line == 0 ? _("zero") : _("non-zero"), line, body);
22156 parse_macro_definition (current_file, line, body);
22159 gdb_assert (macinfo_type == DW_MACRO_undef
22160 || macinfo_type == DW_MACRO_undef_strp
22161 || macinfo_type == DW_MACRO_undef_sup);
22162 macro_undef (current_file, line, body);
22167 case DW_MACRO_start_file:
22169 unsigned int bytes_read;
22172 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22173 mac_ptr += bytes_read;
22174 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22175 mac_ptr += bytes_read;
22177 if ((line == 0 && !at_commandline)
22178 || (line != 0 && at_commandline))
22179 complaint (&symfile_complaints,
22180 _("debug info gives source %d included "
22181 "from %s at %s line %d"),
22182 file, at_commandline ? _("command-line") : _("file"),
22183 line == 0 ? _("zero") : _("non-zero"), line);
22185 if (at_commandline)
22187 /* This DW_MACRO_start_file was executed in the
22189 at_commandline = 0;
22192 current_file = macro_start_file (file, line, current_file, lh);
22196 case DW_MACRO_end_file:
22197 if (! current_file)
22198 complaint (&symfile_complaints,
22199 _("macro debug info has an unmatched "
22200 "`close_file' directive"));
22203 current_file = current_file->included_by;
22204 if (! current_file)
22206 enum dwarf_macro_record_type next_type;
22208 /* GCC circa March 2002 doesn't produce the zero
22209 type byte marking the end of the compilation
22210 unit. Complain if it's not there, but exit no
22213 /* Do we at least have room for a macinfo type byte? */
22214 if (mac_ptr >= mac_end)
22216 dwarf2_section_buffer_overflow_complaint (section);
22220 /* We don't increment mac_ptr here, so this is just
22223 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22225 if (next_type != 0)
22226 complaint (&symfile_complaints,
22227 _("no terminating 0-type entry for "
22228 "macros in `.debug_macinfo' section"));
22235 case DW_MACRO_import:
22236 case DW_MACRO_import_sup:
22240 bfd *include_bfd = abfd;
22241 struct dwarf2_section_info *include_section = section;
22242 const gdb_byte *include_mac_end = mac_end;
22243 int is_dwz = section_is_dwz;
22244 const gdb_byte *new_mac_ptr;
22246 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22247 mac_ptr += offset_size;
22249 if (macinfo_type == DW_MACRO_import_sup)
22251 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22253 dwarf2_read_section (objfile, &dwz->macro);
22255 include_section = &dwz->macro;
22256 include_bfd = get_section_bfd_owner (include_section);
22257 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22261 new_mac_ptr = include_section->buffer + offset;
22262 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22266 /* This has actually happened; see
22267 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22268 complaint (&symfile_complaints,
22269 _("recursive DW_MACRO_import in "
22270 ".debug_macro section"));
22274 *slot = (void *) new_mac_ptr;
22276 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22277 include_mac_end, current_file, lh,
22278 section, section_is_gnu, is_dwz,
22279 offset_size, include_hash);
22281 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22286 case DW_MACINFO_vendor_ext:
22287 if (!section_is_gnu)
22289 unsigned int bytes_read;
22291 /* This reads the constant, but since we don't recognize
22292 any vendor extensions, we ignore it. */
22293 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22294 mac_ptr += bytes_read;
22295 read_direct_string (abfd, mac_ptr, &bytes_read);
22296 mac_ptr += bytes_read;
22298 /* We don't recognize any vendor extensions. */
22304 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22305 mac_ptr, mac_end, abfd, offset_size,
22307 if (mac_ptr == NULL)
22311 } while (macinfo_type != 0);
22315 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22316 int section_is_gnu)
22318 struct objfile *objfile = dwarf2_per_objfile->objfile;
22319 struct line_header *lh = cu->line_header;
22321 const gdb_byte *mac_ptr, *mac_end;
22322 struct macro_source_file *current_file = 0;
22323 enum dwarf_macro_record_type macinfo_type;
22324 unsigned int offset_size = cu->header.offset_size;
22325 const gdb_byte *opcode_definitions[256];
22326 struct cleanup *cleanup;
22328 struct dwarf2_section_info *section;
22329 const char *section_name;
22331 if (cu->dwo_unit != NULL)
22333 if (section_is_gnu)
22335 section = &cu->dwo_unit->dwo_file->sections.macro;
22336 section_name = ".debug_macro.dwo";
22340 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22341 section_name = ".debug_macinfo.dwo";
22346 if (section_is_gnu)
22348 section = &dwarf2_per_objfile->macro;
22349 section_name = ".debug_macro";
22353 section = &dwarf2_per_objfile->macinfo;
22354 section_name = ".debug_macinfo";
22358 dwarf2_read_section (objfile, section);
22359 if (section->buffer == NULL)
22361 complaint (&symfile_complaints, _("missing %s section"), section_name);
22364 abfd = get_section_bfd_owner (section);
22366 /* First pass: Find the name of the base filename.
22367 This filename is needed in order to process all macros whose definition
22368 (or undefinition) comes from the command line. These macros are defined
22369 before the first DW_MACINFO_start_file entry, and yet still need to be
22370 associated to the base file.
22372 To determine the base file name, we scan the macro definitions until we
22373 reach the first DW_MACINFO_start_file entry. We then initialize
22374 CURRENT_FILE accordingly so that any macro definition found before the
22375 first DW_MACINFO_start_file can still be associated to the base file. */
22377 mac_ptr = section->buffer + offset;
22378 mac_end = section->buffer + section->size;
22380 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22381 &offset_size, section_is_gnu);
22382 if (mac_ptr == NULL)
22384 /* We already issued a complaint. */
22390 /* Do we at least have room for a macinfo type byte? */
22391 if (mac_ptr >= mac_end)
22393 /* Complaint is printed during the second pass as GDB will probably
22394 stop the first pass earlier upon finding
22395 DW_MACINFO_start_file. */
22399 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22402 /* Note that we rely on the fact that the corresponding GNU and
22403 DWARF constants are the same. */
22404 switch (macinfo_type)
22406 /* A zero macinfo type indicates the end of the macro
22411 case DW_MACRO_define:
22412 case DW_MACRO_undef:
22413 /* Only skip the data by MAC_PTR. */
22415 unsigned int bytes_read;
22417 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22418 mac_ptr += bytes_read;
22419 read_direct_string (abfd, mac_ptr, &bytes_read);
22420 mac_ptr += bytes_read;
22424 case DW_MACRO_start_file:
22426 unsigned int bytes_read;
22429 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22430 mac_ptr += bytes_read;
22431 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22432 mac_ptr += bytes_read;
22434 current_file = macro_start_file (file, line, current_file, lh);
22438 case DW_MACRO_end_file:
22439 /* No data to skip by MAC_PTR. */
22442 case DW_MACRO_define_strp:
22443 case DW_MACRO_undef_strp:
22444 case DW_MACRO_define_sup:
22445 case DW_MACRO_undef_sup:
22447 unsigned int bytes_read;
22449 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22450 mac_ptr += bytes_read;
22451 mac_ptr += offset_size;
22455 case DW_MACRO_import:
22456 case DW_MACRO_import_sup:
22457 /* Note that, according to the spec, a transparent include
22458 chain cannot call DW_MACRO_start_file. So, we can just
22459 skip this opcode. */
22460 mac_ptr += offset_size;
22463 case DW_MACINFO_vendor_ext:
22464 /* Only skip the data by MAC_PTR. */
22465 if (!section_is_gnu)
22467 unsigned int bytes_read;
22469 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22470 mac_ptr += bytes_read;
22471 read_direct_string (abfd, mac_ptr, &bytes_read);
22472 mac_ptr += bytes_read;
22477 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22478 mac_ptr, mac_end, abfd, offset_size,
22480 if (mac_ptr == NULL)
22484 } while (macinfo_type != 0 && current_file == NULL);
22486 /* Second pass: Process all entries.
22488 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22489 command-line macro definitions/undefinitions. This flag is unset when we
22490 reach the first DW_MACINFO_start_file entry. */
22492 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22494 NULL, xcalloc, xfree));
22495 mac_ptr = section->buffer + offset;
22496 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22497 *slot = (void *) mac_ptr;
22498 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22499 current_file, lh, section,
22500 section_is_gnu, 0, offset_size,
22501 include_hash.get ());
22504 /* Check if the attribute's form is a DW_FORM_block*
22505 if so return true else false. */
22508 attr_form_is_block (const struct attribute *attr)
22510 return (attr == NULL ? 0 :
22511 attr->form == DW_FORM_block1
22512 || attr->form == DW_FORM_block2
22513 || attr->form == DW_FORM_block4
22514 || attr->form == DW_FORM_block
22515 || attr->form == DW_FORM_exprloc);
22518 /* Return non-zero if ATTR's value is a section offset --- classes
22519 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22520 You may use DW_UNSND (attr) to retrieve such offsets.
22522 Section 7.5.4, "Attribute Encodings", explains that no attribute
22523 may have a value that belongs to more than one of these classes; it
22524 would be ambiguous if we did, because we use the same forms for all
22528 attr_form_is_section_offset (const struct attribute *attr)
22530 return (attr->form == DW_FORM_data4
22531 || attr->form == DW_FORM_data8
22532 || attr->form == DW_FORM_sec_offset);
22535 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22536 zero otherwise. When this function returns true, you can apply
22537 dwarf2_get_attr_constant_value to it.
22539 However, note that for some attributes you must check
22540 attr_form_is_section_offset before using this test. DW_FORM_data4
22541 and DW_FORM_data8 are members of both the constant class, and of
22542 the classes that contain offsets into other debug sections
22543 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22544 that, if an attribute's can be either a constant or one of the
22545 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22546 taken as section offsets, not constants.
22548 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22549 cannot handle that. */
22552 attr_form_is_constant (const struct attribute *attr)
22554 switch (attr->form)
22556 case DW_FORM_sdata:
22557 case DW_FORM_udata:
22558 case DW_FORM_data1:
22559 case DW_FORM_data2:
22560 case DW_FORM_data4:
22561 case DW_FORM_data8:
22562 case DW_FORM_implicit_const:
22570 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22571 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22574 attr_form_is_ref (const struct attribute *attr)
22576 switch (attr->form)
22578 case DW_FORM_ref_addr:
22583 case DW_FORM_ref_udata:
22584 case DW_FORM_GNU_ref_alt:
22591 /* Return the .debug_loc section to use for CU.
22592 For DWO files use .debug_loc.dwo. */
22594 static struct dwarf2_section_info *
22595 cu_debug_loc_section (struct dwarf2_cu *cu)
22599 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22601 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
22603 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22604 : &dwarf2_per_objfile->loc);
22607 /* A helper function that fills in a dwarf2_loclist_baton. */
22610 fill_in_loclist_baton (struct dwarf2_cu *cu,
22611 struct dwarf2_loclist_baton *baton,
22612 const struct attribute *attr)
22614 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22616 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22618 baton->per_cu = cu->per_cu;
22619 gdb_assert (baton->per_cu);
22620 /* We don't know how long the location list is, but make sure we
22621 don't run off the edge of the section. */
22622 baton->size = section->size - DW_UNSND (attr);
22623 baton->data = section->buffer + DW_UNSND (attr);
22624 baton->base_address = cu->base_address;
22625 baton->from_dwo = cu->dwo_unit != NULL;
22629 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22630 struct dwarf2_cu *cu, int is_block)
22632 struct objfile *objfile = dwarf2_per_objfile->objfile;
22633 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22635 if (attr_form_is_section_offset (attr)
22636 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22637 the section. If so, fall through to the complaint in the
22639 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22641 struct dwarf2_loclist_baton *baton;
22643 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22645 fill_in_loclist_baton (cu, baton, attr);
22647 if (cu->base_known == 0)
22648 complaint (&symfile_complaints,
22649 _("Location list used without "
22650 "specifying the CU base address."));
22652 SYMBOL_ACLASS_INDEX (sym) = (is_block
22653 ? dwarf2_loclist_block_index
22654 : dwarf2_loclist_index);
22655 SYMBOL_LOCATION_BATON (sym) = baton;
22659 struct dwarf2_locexpr_baton *baton;
22661 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22662 baton->per_cu = cu->per_cu;
22663 gdb_assert (baton->per_cu);
22665 if (attr_form_is_block (attr))
22667 /* Note that we're just copying the block's data pointer
22668 here, not the actual data. We're still pointing into the
22669 info_buffer for SYM's objfile; right now we never release
22670 that buffer, but when we do clean up properly this may
22672 baton->size = DW_BLOCK (attr)->size;
22673 baton->data = DW_BLOCK (attr)->data;
22677 dwarf2_invalid_attrib_class_complaint ("location description",
22678 SYMBOL_NATURAL_NAME (sym));
22682 SYMBOL_ACLASS_INDEX (sym) = (is_block
22683 ? dwarf2_locexpr_block_index
22684 : dwarf2_locexpr_index);
22685 SYMBOL_LOCATION_BATON (sym) = baton;
22689 /* Return the OBJFILE associated with the compilation unit CU. If CU
22690 came from a separate debuginfo file, then the master objfile is
22694 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22696 struct objfile *objfile = per_cu->objfile;
22698 /* Return the master objfile, so that we can report and look up the
22699 correct file containing this variable. */
22700 if (objfile->separate_debug_objfile_backlink)
22701 objfile = objfile->separate_debug_objfile_backlink;
22706 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22707 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22708 CU_HEADERP first. */
22710 static const struct comp_unit_head *
22711 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22712 struct dwarf2_per_cu_data *per_cu)
22714 const gdb_byte *info_ptr;
22717 return &per_cu->cu->header;
22719 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22721 memset (cu_headerp, 0, sizeof (*cu_headerp));
22722 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22723 rcuh_kind::COMPILE);
22728 /* Return the address size given in the compilation unit header for CU. */
22731 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22733 struct comp_unit_head cu_header_local;
22734 const struct comp_unit_head *cu_headerp;
22736 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22738 return cu_headerp->addr_size;
22741 /* Return the offset size given in the compilation unit header for CU. */
22744 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22746 struct comp_unit_head cu_header_local;
22747 const struct comp_unit_head *cu_headerp;
22749 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22751 return cu_headerp->offset_size;
22754 /* See its dwarf2loc.h declaration. */
22757 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22759 struct comp_unit_head cu_header_local;
22760 const struct comp_unit_head *cu_headerp;
22762 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22764 if (cu_headerp->version == 2)
22765 return cu_headerp->addr_size;
22767 return cu_headerp->offset_size;
22770 /* Return the text offset of the CU. The returned offset comes from
22771 this CU's objfile. If this objfile came from a separate debuginfo
22772 file, then the offset may be different from the corresponding
22773 offset in the parent objfile. */
22776 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22778 struct objfile *objfile = per_cu->objfile;
22780 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22783 /* Return DWARF version number of PER_CU. */
22786 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22788 return per_cu->dwarf_version;
22791 /* Locate the .debug_info compilation unit from CU's objfile which contains
22792 the DIE at OFFSET. Raises an error on failure. */
22794 static struct dwarf2_per_cu_data *
22795 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22796 unsigned int offset_in_dwz,
22797 struct objfile *objfile)
22799 struct dwarf2_per_cu_data *this_cu;
22801 const sect_offset *cu_off;
22804 high = dwarf2_per_objfile->n_comp_units - 1;
22807 struct dwarf2_per_cu_data *mid_cu;
22808 int mid = low + (high - low) / 2;
22810 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22811 cu_off = &mid_cu->sect_off;
22812 if (mid_cu->is_dwz > offset_in_dwz
22813 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
22818 gdb_assert (low == high);
22819 this_cu = dwarf2_per_objfile->all_comp_units[low];
22820 cu_off = &this_cu->sect_off;
22821 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
22823 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22824 error (_("Dwarf Error: could not find partial DIE containing "
22825 "offset 0x%x [in module %s]"),
22826 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
22828 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22830 return dwarf2_per_objfile->all_comp_units[low-1];
22834 this_cu = dwarf2_per_objfile->all_comp_units[low];
22835 if (low == dwarf2_per_objfile->n_comp_units - 1
22836 && sect_off >= this_cu->sect_off + this_cu->length)
22837 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22838 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22843 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22846 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22848 memset (cu, 0, sizeof (*cu));
22850 cu->per_cu = per_cu;
22851 cu->objfile = per_cu->objfile;
22852 obstack_init (&cu->comp_unit_obstack);
22855 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22858 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22859 enum language pretend_language)
22861 struct attribute *attr;
22863 /* Set the language we're debugging. */
22864 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22866 set_cu_language (DW_UNSND (attr), cu);
22869 cu->language = pretend_language;
22870 cu->language_defn = language_def (cu->language);
22873 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22876 /* Release one cached compilation unit, CU. We unlink it from the tree
22877 of compilation units, but we don't remove it from the read_in_chain;
22878 the caller is responsible for that.
22879 NOTE: DATA is a void * because this function is also used as a
22880 cleanup routine. */
22883 free_heap_comp_unit (void *data)
22885 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22887 gdb_assert (cu->per_cu != NULL);
22888 cu->per_cu->cu = NULL;
22891 obstack_free (&cu->comp_unit_obstack, NULL);
22896 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22897 when we're finished with it. We can't free the pointer itself, but be
22898 sure to unlink it from the cache. Also release any associated storage. */
22901 free_stack_comp_unit (void *data)
22903 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22905 gdb_assert (cu->per_cu != NULL);
22906 cu->per_cu->cu = NULL;
22909 obstack_free (&cu->comp_unit_obstack, NULL);
22910 cu->partial_dies = NULL;
22913 /* Free all cached compilation units. */
22916 free_cached_comp_units (void *data)
22918 dwarf2_per_objfile->free_cached_comp_units ();
22921 /* Increase the age counter on each cached compilation unit, and free
22922 any that are too old. */
22925 age_cached_comp_units (void)
22927 struct dwarf2_per_cu_data *per_cu, **last_chain;
22929 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22930 per_cu = dwarf2_per_objfile->read_in_chain;
22931 while (per_cu != NULL)
22933 per_cu->cu->last_used ++;
22934 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22935 dwarf2_mark (per_cu->cu);
22936 per_cu = per_cu->cu->read_in_chain;
22939 per_cu = dwarf2_per_objfile->read_in_chain;
22940 last_chain = &dwarf2_per_objfile->read_in_chain;
22941 while (per_cu != NULL)
22943 struct dwarf2_per_cu_data *next_cu;
22945 next_cu = per_cu->cu->read_in_chain;
22947 if (!per_cu->cu->mark)
22949 free_heap_comp_unit (per_cu->cu);
22950 *last_chain = next_cu;
22953 last_chain = &per_cu->cu->read_in_chain;
22959 /* Remove a single compilation unit from the cache. */
22962 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22964 struct dwarf2_per_cu_data *per_cu, **last_chain;
22966 per_cu = dwarf2_per_objfile->read_in_chain;
22967 last_chain = &dwarf2_per_objfile->read_in_chain;
22968 while (per_cu != NULL)
22970 struct dwarf2_per_cu_data *next_cu;
22972 next_cu = per_cu->cu->read_in_chain;
22974 if (per_cu == target_per_cu)
22976 free_heap_comp_unit (per_cu->cu);
22978 *last_chain = next_cu;
22982 last_chain = &per_cu->cu->read_in_chain;
22988 /* Release all extra memory associated with OBJFILE. */
22991 dwarf2_free_objfile (struct objfile *objfile)
22994 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22995 dwarf2_objfile_data_key);
22997 if (dwarf2_per_objfile == NULL)
23000 dwarf2_per_objfile->~dwarf2_per_objfile ();
23003 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23004 We store these in a hash table separate from the DIEs, and preserve them
23005 when the DIEs are flushed out of cache.
23007 The CU "per_cu" pointer is needed because offset alone is not enough to
23008 uniquely identify the type. A file may have multiple .debug_types sections,
23009 or the type may come from a DWO file. Furthermore, while it's more logical
23010 to use per_cu->section+offset, with Fission the section with the data is in
23011 the DWO file but we don't know that section at the point we need it.
23012 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23013 because we can enter the lookup routine, get_die_type_at_offset, from
23014 outside this file, and thus won't necessarily have PER_CU->cu.
23015 Fortunately, PER_CU is stable for the life of the objfile. */
23017 struct dwarf2_per_cu_offset_and_type
23019 const struct dwarf2_per_cu_data *per_cu;
23020 sect_offset sect_off;
23024 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23027 per_cu_offset_and_type_hash (const void *item)
23029 const struct dwarf2_per_cu_offset_and_type *ofs
23030 = (const struct dwarf2_per_cu_offset_and_type *) item;
23032 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23035 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23038 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23040 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23041 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23042 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23043 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23045 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23046 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23049 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23050 table if necessary. For convenience, return TYPE.
23052 The DIEs reading must have careful ordering to:
23053 * Not cause infite loops trying to read in DIEs as a prerequisite for
23054 reading current DIE.
23055 * Not trying to dereference contents of still incompletely read in types
23056 while reading in other DIEs.
23057 * Enable referencing still incompletely read in types just by a pointer to
23058 the type without accessing its fields.
23060 Therefore caller should follow these rules:
23061 * Try to fetch any prerequisite types we may need to build this DIE type
23062 before building the type and calling set_die_type.
23063 * After building type call set_die_type for current DIE as soon as
23064 possible before fetching more types to complete the current type.
23065 * Make the type as complete as possible before fetching more types. */
23067 static struct type *
23068 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23070 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23071 struct objfile *objfile = cu->objfile;
23072 struct attribute *attr;
23073 struct dynamic_prop prop;
23075 /* For Ada types, make sure that the gnat-specific data is always
23076 initialized (if not already set). There are a few types where
23077 we should not be doing so, because the type-specific area is
23078 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23079 where the type-specific area is used to store the floatformat).
23080 But this is not a problem, because the gnat-specific information
23081 is actually not needed for these types. */
23082 if (need_gnat_info (cu)
23083 && TYPE_CODE (type) != TYPE_CODE_FUNC
23084 && TYPE_CODE (type) != TYPE_CODE_FLT
23085 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23086 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23087 && TYPE_CODE (type) != TYPE_CODE_METHOD
23088 && !HAVE_GNAT_AUX_INFO (type))
23089 INIT_GNAT_SPECIFIC (type);
23091 /* Read DW_AT_allocated and set in type. */
23092 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23093 if (attr_form_is_block (attr))
23095 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23096 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
23098 else if (attr != NULL)
23100 complaint (&symfile_complaints,
23101 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
23102 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23103 to_underlying (die->sect_off));
23106 /* Read DW_AT_associated and set in type. */
23107 attr = dwarf2_attr (die, DW_AT_associated, cu);
23108 if (attr_form_is_block (attr))
23110 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23111 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
23113 else if (attr != NULL)
23115 complaint (&symfile_complaints,
23116 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
23117 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23118 to_underlying (die->sect_off));
23121 /* Read DW_AT_data_location and set in type. */
23122 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23123 if (attr_to_dynamic_prop (attr, die, cu, &prop))
23124 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
23126 if (dwarf2_per_objfile->die_type_hash == NULL)
23128 dwarf2_per_objfile->die_type_hash =
23129 htab_create_alloc_ex (127,
23130 per_cu_offset_and_type_hash,
23131 per_cu_offset_and_type_eq,
23133 &objfile->objfile_obstack,
23134 hashtab_obstack_allocate,
23135 dummy_obstack_deallocate);
23138 ofs.per_cu = cu->per_cu;
23139 ofs.sect_off = die->sect_off;
23141 slot = (struct dwarf2_per_cu_offset_and_type **)
23142 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
23144 complaint (&symfile_complaints,
23145 _("A problem internal to GDB: DIE 0x%x has type already set"),
23146 to_underlying (die->sect_off));
23147 *slot = XOBNEW (&objfile->objfile_obstack,
23148 struct dwarf2_per_cu_offset_and_type);
23153 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23154 or return NULL if the die does not have a saved type. */
23156 static struct type *
23157 get_die_type_at_offset (sect_offset sect_off,
23158 struct dwarf2_per_cu_data *per_cu)
23160 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23162 if (dwarf2_per_objfile->die_type_hash == NULL)
23165 ofs.per_cu = per_cu;
23166 ofs.sect_off = sect_off;
23167 slot = ((struct dwarf2_per_cu_offset_and_type *)
23168 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
23175 /* Look up the type for DIE in CU in die_type_hash,
23176 or return NULL if DIE does not have a saved type. */
23178 static struct type *
23179 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23181 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23184 /* Add a dependence relationship from CU to REF_PER_CU. */
23187 dwarf2_add_dependence (struct dwarf2_cu *cu,
23188 struct dwarf2_per_cu_data *ref_per_cu)
23192 if (cu->dependencies == NULL)
23194 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23195 NULL, &cu->comp_unit_obstack,
23196 hashtab_obstack_allocate,
23197 dummy_obstack_deallocate);
23199 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23201 *slot = ref_per_cu;
23204 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23205 Set the mark field in every compilation unit in the
23206 cache that we must keep because we are keeping CU. */
23209 dwarf2_mark_helper (void **slot, void *data)
23211 struct dwarf2_per_cu_data *per_cu;
23213 per_cu = (struct dwarf2_per_cu_data *) *slot;
23215 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23216 reading of the chain. As such dependencies remain valid it is not much
23217 useful to track and undo them during QUIT cleanups. */
23218 if (per_cu->cu == NULL)
23221 if (per_cu->cu->mark)
23223 per_cu->cu->mark = 1;
23225 if (per_cu->cu->dependencies != NULL)
23226 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23231 /* Set the mark field in CU and in every other compilation unit in the
23232 cache that we must keep because we are keeping CU. */
23235 dwarf2_mark (struct dwarf2_cu *cu)
23240 if (cu->dependencies != NULL)
23241 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23245 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23249 per_cu->cu->mark = 0;
23250 per_cu = per_cu->cu->read_in_chain;
23254 /* Trivial hash function for partial_die_info: the hash value of a DIE
23255 is its offset in .debug_info for this objfile. */
23258 partial_die_hash (const void *item)
23260 const struct partial_die_info *part_die
23261 = (const struct partial_die_info *) item;
23263 return to_underlying (part_die->sect_off);
23266 /* Trivial comparison function for partial_die_info structures: two DIEs
23267 are equal if they have the same offset. */
23270 partial_die_eq (const void *item_lhs, const void *item_rhs)
23272 const struct partial_die_info *part_die_lhs
23273 = (const struct partial_die_info *) item_lhs;
23274 const struct partial_die_info *part_die_rhs
23275 = (const struct partial_die_info *) item_rhs;
23277 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23280 static struct cmd_list_element *set_dwarf_cmdlist;
23281 static struct cmd_list_element *show_dwarf_cmdlist;
23284 set_dwarf_cmd (char *args, int from_tty)
23286 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23291 show_dwarf_cmd (char *args, int from_tty)
23293 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23296 /* Free data associated with OBJFILE, if necessary. */
23299 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23301 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23304 /* Make sure we don't accidentally use dwarf2_per_objfile while
23306 dwarf2_per_objfile = NULL;
23308 for (ix = 0; ix < data->n_comp_units; ++ix)
23309 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23311 for (ix = 0; ix < data->n_type_units; ++ix)
23312 VEC_free (dwarf2_per_cu_ptr,
23313 data->all_type_units[ix]->per_cu.imported_symtabs);
23314 xfree (data->all_type_units);
23316 VEC_free (dwarf2_section_info_def, data->types);
23318 if (data->dwo_files)
23319 free_dwo_files (data->dwo_files, objfile);
23320 if (data->dwp_file)
23321 gdb_bfd_unref (data->dwp_file->dbfd);
23323 if (data->dwz_file && data->dwz_file->dwz_bfd)
23324 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23328 /* The "save gdb-index" command. */
23330 /* In-memory buffer to prepare data to be written later to a file. */
23334 /* Copy DATA to the end of the buffer. */
23335 template<typename T>
23336 void append_data (const T &data)
23338 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23339 reinterpret_cast<const gdb_byte *> (&data + 1),
23340 grow (sizeof (data)));
23343 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23344 terminating zero is appended too. */
23345 void append_cstr0 (const char *cstr)
23347 const size_t size = strlen (cstr) + 1;
23348 std::copy (cstr, cstr + size, grow (size));
23351 /* Accept a host-format integer in VAL and append it to the buffer
23352 as a target-format integer which is LEN bytes long. */
23353 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23355 ::store_unsigned_integer (grow (len), len, byte_order, val);
23358 /* Return the size of the buffer. */
23359 size_t size () const
23361 return m_vec.size ();
23364 /* Write the buffer to FILE. */
23365 void file_write (FILE *file) const
23367 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23368 error (_("couldn't write data to file"));
23372 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23373 the start of the new block. */
23374 gdb_byte *grow (size_t size)
23376 m_vec.resize (m_vec.size () + size);
23377 return &*m_vec.end () - size;
23380 gdb::byte_vector m_vec;
23383 /* An entry in the symbol table. */
23384 struct symtab_index_entry
23386 /* The name of the symbol. */
23388 /* The offset of the name in the constant pool. */
23389 offset_type index_offset;
23390 /* A sorted vector of the indices of all the CUs that hold an object
23392 std::vector<offset_type> cu_indices;
23395 /* The symbol table. This is a power-of-2-sized hash table. */
23396 struct mapped_symtab
23400 data.resize (1024);
23403 offset_type n_elements = 0;
23404 std::vector<symtab_index_entry> data;
23407 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
23410 Function is used only during write_hash_table so no index format backward
23411 compatibility is needed. */
23413 static symtab_index_entry &
23414 find_slot (struct mapped_symtab *symtab, const char *name)
23416 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23418 index = hash & (symtab->data.size () - 1);
23419 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
23423 if (symtab->data[index].name == NULL
23424 || strcmp (name, symtab->data[index].name) == 0)
23425 return symtab->data[index];
23426 index = (index + step) & (symtab->data.size () - 1);
23430 /* Expand SYMTAB's hash table. */
23433 hash_expand (struct mapped_symtab *symtab)
23435 auto old_entries = std::move (symtab->data);
23437 symtab->data.clear ();
23438 symtab->data.resize (old_entries.size () * 2);
23440 for (auto &it : old_entries)
23441 if (it.name != NULL)
23443 auto &ref = find_slot (symtab, it.name);
23444 ref = std::move (it);
23448 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23449 CU_INDEX is the index of the CU in which the symbol appears.
23450 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23453 add_index_entry (struct mapped_symtab *symtab, const char *name,
23454 int is_static, gdb_index_symbol_kind kind,
23455 offset_type cu_index)
23457 offset_type cu_index_and_attrs;
23459 ++symtab->n_elements;
23460 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
23461 hash_expand (symtab);
23463 symtab_index_entry &slot = find_slot (symtab, name);
23464 if (slot.name == NULL)
23467 /* index_offset is set later. */
23470 cu_index_and_attrs = 0;
23471 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23472 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23473 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23475 /* We don't want to record an index value twice as we want to avoid the
23477 We process all global symbols and then all static symbols
23478 (which would allow us to avoid the duplication by only having to check
23479 the last entry pushed), but a symbol could have multiple kinds in one CU.
23480 To keep things simple we don't worry about the duplication here and
23481 sort and uniqufy the list after we've processed all symbols. */
23482 slot.cu_indices.push_back (cu_index_and_attrs);
23485 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23488 uniquify_cu_indices (struct mapped_symtab *symtab)
23490 for (auto &entry : symtab->data)
23492 if (entry.name != NULL && !entry.cu_indices.empty ())
23494 auto &cu_indices = entry.cu_indices;
23495 std::sort (cu_indices.begin (), cu_indices.end ());
23496 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23497 cu_indices.erase (from, cu_indices.end ());
23502 /* A form of 'const char *' suitable for container keys. Only the
23503 pointer is stored. The strings themselves are compared, not the
23508 c_str_view (const char *cstr)
23512 bool operator== (const c_str_view &other) const
23514 return strcmp (m_cstr, other.m_cstr) == 0;
23518 friend class c_str_view_hasher;
23519 const char *const m_cstr;
23522 /* A std::unordered_map::hasher for c_str_view that uses the right
23523 hash function for strings in a mapped index. */
23524 class c_str_view_hasher
23527 size_t operator () (const c_str_view &x) const
23529 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23533 /* A std::unordered_map::hasher for std::vector<>. */
23534 template<typename T>
23535 class vector_hasher
23538 size_t operator () (const std::vector<T> &key) const
23540 return iterative_hash (key.data (),
23541 sizeof (key.front ()) * key.size (), 0);
23545 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23546 constant pool entries going into the data buffer CPOOL. */
23549 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23552 /* Elements are sorted vectors of the indices of all the CUs that
23553 hold an object of this name. */
23554 std::unordered_map<std::vector<offset_type>, offset_type,
23555 vector_hasher<offset_type>>
23558 /* We add all the index vectors to the constant pool first, to
23559 ensure alignment is ok. */
23560 for (symtab_index_entry &entry : symtab->data)
23562 if (entry.name == NULL)
23564 gdb_assert (entry.index_offset == 0);
23566 /* Finding before inserting is faster than always trying to
23567 insert, because inserting always allocates a node, does the
23568 lookup, and then destroys the new node if another node
23569 already had the same key. C++17 try_emplace will avoid
23572 = symbol_hash_table.find (entry.cu_indices);
23573 if (found != symbol_hash_table.end ())
23575 entry.index_offset = found->second;
23579 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23580 entry.index_offset = cpool.size ();
23581 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23582 for (const auto index : entry.cu_indices)
23583 cpool.append_data (MAYBE_SWAP (index));
23587 /* Now write out the hash table. */
23588 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
23589 for (const auto &entry : symtab->data)
23591 offset_type str_off, vec_off;
23593 if (entry.name != NULL)
23595 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
23596 if (insertpair.second)
23597 cpool.append_cstr0 (entry.name);
23598 str_off = insertpair.first->second;
23599 vec_off = entry.index_offset;
23603 /* While 0 is a valid constant pool index, it is not valid
23604 to have 0 for both offsets. */
23609 output.append_data (MAYBE_SWAP (str_off));
23610 output.append_data (MAYBE_SWAP (vec_off));
23614 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
23616 /* Helper struct for building the address table. */
23617 struct addrmap_index_data
23619 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23620 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23623 struct objfile *objfile;
23624 data_buf &addr_vec;
23625 psym_index_map &cu_index_htab;
23627 /* Non-zero if the previous_* fields are valid.
23628 We can't write an entry until we see the next entry (since it is only then
23629 that we know the end of the entry). */
23630 int previous_valid;
23631 /* Index of the CU in the table of all CUs in the index file. */
23632 unsigned int previous_cu_index;
23633 /* Start address of the CU. */
23634 CORE_ADDR previous_cu_start;
23637 /* Write an address entry to ADDR_VEC. */
23640 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
23641 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23643 CORE_ADDR baseaddr;
23645 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23647 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23648 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
23649 addr_vec.append_data (MAYBE_SWAP (cu_index));
23652 /* Worker function for traversing an addrmap to build the address table. */
23655 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23657 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23658 struct partial_symtab *pst = (struct partial_symtab *) obj;
23660 if (data->previous_valid)
23661 add_address_entry (data->objfile, data->addr_vec,
23662 data->previous_cu_start, start_addr,
23663 data->previous_cu_index);
23665 data->previous_cu_start = start_addr;
23668 const auto it = data->cu_index_htab.find (pst);
23669 gdb_assert (it != data->cu_index_htab.cend ());
23670 data->previous_cu_index = it->second;
23671 data->previous_valid = 1;
23674 data->previous_valid = 0;
23679 /* Write OBJFILE's address map to ADDR_VEC.
23680 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23681 in the index file. */
23684 write_address_map (struct objfile *objfile, data_buf &addr_vec,
23685 psym_index_map &cu_index_htab)
23687 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
23689 /* When writing the address table, we have to cope with the fact that
23690 the addrmap iterator only provides the start of a region; we have to
23691 wait until the next invocation to get the start of the next region. */
23693 addrmap_index_data.objfile = objfile;
23694 addrmap_index_data.previous_valid = 0;
23696 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23697 &addrmap_index_data);
23699 /* It's highly unlikely the last entry (end address = 0xff...ff)
23700 is valid, but we should still handle it.
23701 The end address is recorded as the start of the next region, but that
23702 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23704 if (addrmap_index_data.previous_valid)
23705 add_address_entry (objfile, addr_vec,
23706 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23707 addrmap_index_data.previous_cu_index);
23710 /* Return the symbol kind of PSYM. */
23712 static gdb_index_symbol_kind
23713 symbol_kind (struct partial_symbol *psym)
23715 domain_enum domain = PSYMBOL_DOMAIN (psym);
23716 enum address_class aclass = PSYMBOL_CLASS (psym);
23724 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23726 return GDB_INDEX_SYMBOL_KIND_TYPE;
23728 case LOC_CONST_BYTES:
23729 case LOC_OPTIMIZED_OUT:
23731 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23733 /* Note: It's currently impossible to recognize psyms as enum values
23734 short of reading the type info. For now punt. */
23735 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23737 /* There are other LOC_FOO values that one might want to classify
23738 as variables, but dwarf2read.c doesn't currently use them. */
23739 return GDB_INDEX_SYMBOL_KIND_OTHER;
23741 case STRUCT_DOMAIN:
23742 return GDB_INDEX_SYMBOL_KIND_TYPE;
23744 return GDB_INDEX_SYMBOL_KIND_OTHER;
23748 /* Add a list of partial symbols to SYMTAB. */
23751 write_psymbols (struct mapped_symtab *symtab,
23752 std::unordered_set<partial_symbol *> &psyms_seen,
23753 struct partial_symbol **psymp,
23755 offset_type cu_index,
23758 for (; count-- > 0; ++psymp)
23760 struct partial_symbol *psym = *psymp;
23762 if (SYMBOL_LANGUAGE (psym) == language_ada)
23763 error (_("Ada is not currently supported by the index"));
23765 /* Only add a given psymbol once. */
23766 if (psyms_seen.insert (psym).second)
23768 gdb_index_symbol_kind kind = symbol_kind (psym);
23770 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23771 is_static, kind, cu_index);
23776 /* A helper struct used when iterating over debug_types. */
23777 struct signatured_type_index_data
23779 signatured_type_index_data (data_buf &types_list_,
23780 std::unordered_set<partial_symbol *> &psyms_seen_)
23781 : types_list (types_list_), psyms_seen (psyms_seen_)
23784 struct objfile *objfile;
23785 struct mapped_symtab *symtab;
23786 data_buf &types_list;
23787 std::unordered_set<partial_symbol *> &psyms_seen;
23791 /* A helper function that writes a single signatured_type to an
23795 write_one_signatured_type (void **slot, void *d)
23797 struct signatured_type_index_data *info
23798 = (struct signatured_type_index_data *) d;
23799 struct signatured_type *entry = (struct signatured_type *) *slot;
23800 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23802 write_psymbols (info->symtab,
23804 info->objfile->global_psymbols.list
23805 + psymtab->globals_offset,
23806 psymtab->n_global_syms, info->cu_index,
23808 write_psymbols (info->symtab,
23810 info->objfile->static_psymbols.list
23811 + psymtab->statics_offset,
23812 psymtab->n_static_syms, info->cu_index,
23815 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23816 to_underlying (entry->per_cu.sect_off));
23817 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23818 to_underlying (entry->type_offset_in_tu));
23819 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
23826 /* Recurse into all "included" dependencies and count their symbols as
23827 if they appeared in this psymtab. */
23830 recursively_count_psymbols (struct partial_symtab *psymtab,
23831 size_t &psyms_seen)
23833 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23834 if (psymtab->dependencies[i]->user != NULL)
23835 recursively_count_psymbols (psymtab->dependencies[i],
23838 psyms_seen += psymtab->n_global_syms;
23839 psyms_seen += psymtab->n_static_syms;
23842 /* Recurse into all "included" dependencies and write their symbols as
23843 if they appeared in this psymtab. */
23846 recursively_write_psymbols (struct objfile *objfile,
23847 struct partial_symtab *psymtab,
23848 struct mapped_symtab *symtab,
23849 std::unordered_set<partial_symbol *> &psyms_seen,
23850 offset_type cu_index)
23854 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23855 if (psymtab->dependencies[i]->user != NULL)
23856 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23857 symtab, psyms_seen, cu_index);
23859 write_psymbols (symtab,
23861 objfile->global_psymbols.list + psymtab->globals_offset,
23862 psymtab->n_global_syms, cu_index,
23864 write_psymbols (symtab,
23866 objfile->static_psymbols.list + psymtab->statics_offset,
23867 psymtab->n_static_syms, cu_index,
23871 /* Create an index file for OBJFILE in the directory DIR. */
23874 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23876 if (dwarf2_per_objfile->using_index)
23877 error (_("Cannot use an index to create the index"));
23879 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23880 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23882 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23886 if (stat (objfile_name (objfile), &st) < 0)
23887 perror_with_name (objfile_name (objfile));
23889 std::string filename (std::string (dir) + SLASH_STRING
23890 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
23892 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
23894 error (_("Can't open `%s' for writing"), filename.c_str ());
23896 /* Order matters here; we want FILE to be closed before FILENAME is
23897 unlinked, because on MS-Windows one cannot delete a file that is
23898 still open. (Don't call anything here that might throw until
23899 file_closer is created.) */
23900 gdb::unlinker unlink_file (filename.c_str ());
23901 gdb_file_up close_out_file (out_file);
23903 mapped_symtab symtab;
23906 /* While we're scanning CU's create a table that maps a psymtab pointer
23907 (which is what addrmap records) to its index (which is what is recorded
23908 in the index file). This will later be needed to write the address
23910 psym_index_map cu_index_htab;
23911 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
23913 /* The CU list is already sorted, so we don't need to do additional
23914 work here. Also, the debug_types entries do not appear in
23915 all_comp_units, but only in their own hash table. */
23917 /* The psyms_seen set is potentially going to be largish (~40k
23918 elements when indexing a -g3 build of GDB itself). Estimate the
23919 number of elements in order to avoid too many rehashes, which
23920 require rebuilding buckets and thus many trips to
23922 size_t psyms_count = 0;
23923 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23925 struct dwarf2_per_cu_data *per_cu
23926 = dwarf2_per_objfile->all_comp_units[i];
23927 struct partial_symtab *psymtab = per_cu->v.psymtab;
23929 if (psymtab != NULL && psymtab->user == NULL)
23930 recursively_count_psymbols (psymtab, psyms_count);
23932 /* Generating an index for gdb itself shows a ratio of
23933 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23934 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
23935 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23937 struct dwarf2_per_cu_data *per_cu
23938 = dwarf2_per_objfile->all_comp_units[i];
23939 struct partial_symtab *psymtab = per_cu->v.psymtab;
23941 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23942 It may be referenced from a local scope but in such case it does not
23943 need to be present in .gdb_index. */
23944 if (psymtab == NULL)
23947 if (psymtab->user == NULL)
23948 recursively_write_psymbols (objfile, psymtab, &symtab,
23951 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23952 gdb_assert (insertpair.second);
23954 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23955 to_underlying (per_cu->sect_off));
23956 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
23959 /* Dump the address map. */
23961 write_address_map (objfile, addr_vec, cu_index_htab);
23963 /* Write out the .debug_type entries, if any. */
23964 data_buf types_cu_list;
23965 if (dwarf2_per_objfile->signatured_types)
23967 signatured_type_index_data sig_data (types_cu_list,
23970 sig_data.objfile = objfile;
23971 sig_data.symtab = &symtab;
23972 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23973 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23974 write_one_signatured_type, &sig_data);
23977 /* Now that we've processed all symbols we can shrink their cu_indices
23979 uniquify_cu_indices (&symtab);
23981 data_buf symtab_vec, constant_pool;
23982 write_hash_table (&symtab, symtab_vec, constant_pool);
23985 const offset_type size_of_contents = 6 * sizeof (offset_type);
23986 offset_type total_len = size_of_contents;
23988 /* The version number. */
23989 contents.append_data (MAYBE_SWAP (8));
23991 /* The offset of the CU list from the start of the file. */
23992 contents.append_data (MAYBE_SWAP (total_len));
23993 total_len += cu_list.size ();
23995 /* The offset of the types CU list from the start of the file. */
23996 contents.append_data (MAYBE_SWAP (total_len));
23997 total_len += types_cu_list.size ();
23999 /* The offset of the address table from the start of the file. */
24000 contents.append_data (MAYBE_SWAP (total_len));
24001 total_len += addr_vec.size ();
24003 /* The offset of the symbol table from the start of the file. */
24004 contents.append_data (MAYBE_SWAP (total_len));
24005 total_len += symtab_vec.size ();
24007 /* The offset of the constant pool from the start of the file. */
24008 contents.append_data (MAYBE_SWAP (total_len));
24009 total_len += constant_pool.size ();
24011 gdb_assert (contents.size () == size_of_contents);
24013 contents.file_write (out_file);
24014 cu_list.file_write (out_file);
24015 types_cu_list.file_write (out_file);
24016 addr_vec.file_write (out_file);
24017 symtab_vec.file_write (out_file);
24018 constant_pool.file_write (out_file);
24020 /* We want to keep the file. */
24021 unlink_file.keep ();
24024 /* Implementation of the `save gdb-index' command.
24026 Note that the file format used by this command is documented in the
24027 GDB manual. Any changes here must be documented there. */
24030 save_gdb_index_command (const char *arg, int from_tty)
24032 struct objfile *objfile;
24035 error (_("usage: save gdb-index DIRECTORY"));
24037 ALL_OBJFILES (objfile)
24041 /* If the objfile does not correspond to an actual file, skip it. */
24042 if (stat (objfile_name (objfile), &st) < 0)
24046 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24047 dwarf2_objfile_data_key);
24048 if (dwarf2_per_objfile)
24053 write_psymtabs_to_index (objfile, arg);
24055 CATCH (except, RETURN_MASK_ERROR)
24057 exception_fprintf (gdb_stderr, except,
24058 _("Error while writing index for `%s': "),
24059 objfile_name (objfile));
24068 int dwarf_always_disassemble;
24071 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24072 struct cmd_list_element *c, const char *value)
24074 fprintf_filtered (file,
24075 _("Whether to always disassemble "
24076 "DWARF expressions is %s.\n"),
24081 show_check_physname (struct ui_file *file, int from_tty,
24082 struct cmd_list_element *c, const char *value)
24084 fprintf_filtered (file,
24085 _("Whether to check \"physname\" is %s.\n"),
24090 _initialize_dwarf2_read (void)
24092 struct cmd_list_element *c;
24094 dwarf2_objfile_data_key
24095 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24097 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24098 Set DWARF specific variables.\n\
24099 Configure DWARF variables such as the cache size"),
24100 &set_dwarf_cmdlist, "maintenance set dwarf ",
24101 0/*allow-unknown*/, &maintenance_set_cmdlist);
24103 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24104 Show DWARF specific variables\n\
24105 Show DWARF variables such as the cache size"),
24106 &show_dwarf_cmdlist, "maintenance show dwarf ",
24107 0/*allow-unknown*/, &maintenance_show_cmdlist);
24109 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24110 &dwarf_max_cache_age, _("\
24111 Set the upper bound on the age of cached DWARF compilation units."), _("\
24112 Show the upper bound on the age of cached DWARF compilation units."), _("\
24113 A higher limit means that cached compilation units will be stored\n\
24114 in memory longer, and more total memory will be used. Zero disables\n\
24115 caching, which can slow down startup."),
24117 show_dwarf_max_cache_age,
24118 &set_dwarf_cmdlist,
24119 &show_dwarf_cmdlist);
24121 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24122 &dwarf_always_disassemble, _("\
24123 Set whether `info address' always disassembles DWARF expressions."), _("\
24124 Show whether `info address' always disassembles DWARF expressions."), _("\
24125 When enabled, DWARF expressions are always printed in an assembly-like\n\
24126 syntax. When disabled, expressions will be printed in a more\n\
24127 conversational style, when possible."),
24129 show_dwarf_always_disassemble,
24130 &set_dwarf_cmdlist,
24131 &show_dwarf_cmdlist);
24133 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24134 Set debugging of the DWARF reader."), _("\
24135 Show debugging of the DWARF reader."), _("\
24136 When enabled (non-zero), debugging messages are printed during DWARF\n\
24137 reading and symtab expansion. A value of 1 (one) provides basic\n\
24138 information. A value greater than 1 provides more verbose information."),
24141 &setdebuglist, &showdebuglist);
24143 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24144 Set debugging of the DWARF DIE reader."), _("\
24145 Show debugging of the DWARF DIE reader."), _("\
24146 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24147 The value is the maximum depth to print."),
24150 &setdebuglist, &showdebuglist);
24152 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24153 Set debugging of the dwarf line reader."), _("\
24154 Show debugging of the dwarf line reader."), _("\
24155 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24156 A value of 1 (one) provides basic information.\n\
24157 A value greater than 1 provides more verbose information."),
24160 &setdebuglist, &showdebuglist);
24162 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24163 Set cross-checking of \"physname\" code against demangler."), _("\
24164 Show cross-checking of \"physname\" code against demangler."), _("\
24165 When enabled, GDB's internal \"physname\" code is checked against\n\
24167 NULL, show_check_physname,
24168 &setdebuglist, &showdebuglist);
24170 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24171 no_class, &use_deprecated_index_sections, _("\
24172 Set whether to use deprecated gdb_index sections."), _("\
24173 Show whether to use deprecated gdb_index sections."), _("\
24174 When enabled, deprecated .gdb_index sections are used anyway.\n\
24175 Normally they are ignored either because of a missing feature or\n\
24176 performance issue.\n\
24177 Warning: This option must be enabled before gdb reads the file."),
24180 &setlist, &showlist);
24182 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24184 Save a gdb-index file.\n\
24185 Usage: save gdb-index DIRECTORY"),
24187 set_cmd_completer (c, filename_completer);
24189 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24190 &dwarf2_locexpr_funcs);
24191 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24192 &dwarf2_loclist_funcs);
24194 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24195 &dwarf2_block_frame_base_locexpr_funcs);
24196 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24197 &dwarf2_block_frame_base_loclist_funcs);