1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2013 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 "exceptions.h"
60 #include "completer.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
71 #include "filestuff.h"
74 #include "gdb_string.h"
75 #include "gdb_assert.h"
76 #include <sys/types.h>
78 typedef struct symbol *symbolp;
81 /* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83 static int dwarf2_read_debug = 0;
85 /* When non-zero, dump DIEs after they are read in. */
86 static unsigned int dwarf2_die_debug = 0;
88 /* When non-zero, cross-check physname against demangler. */
89 static int check_physname = 0;
91 /* When non-zero, do not reject deprecated .gdb_index sections. */
92 static int use_deprecated_index_sections = 0;
94 static const struct objfile_data *dwarf2_objfile_data_key;
96 /* The "aclass" indices for various kinds of computed DWARF symbols. */
98 static int dwarf2_locexpr_index;
99 static int dwarf2_loclist_index;
100 static int dwarf2_locexpr_block_index;
101 static int dwarf2_loclist_block_index;
103 struct dwarf2_section_info
106 const gdb_byte *buffer;
108 /* True if we have tried to read this section. */
112 typedef struct dwarf2_section_info dwarf2_section_info_def;
113 DEF_VEC_O (dwarf2_section_info_def);
115 /* All offsets in the index are of this type. It must be
116 architecture-independent. */
117 typedef uint32_t offset_type;
119 DEF_VEC_I (offset_type);
121 /* Ensure only legit values are used. */
122 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
128 /* Ensure only legit values are used. */
129 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
136 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
137 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
143 /* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
147 /* Index data format version. */
150 /* The total length of the buffer. */
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
162 /* Size in slots, each slot is 2 offset_types. */
163 offset_type symbol_table_slots;
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
169 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170 DEF_VEC_P (dwarf2_per_cu_ptr);
172 /* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
175 struct dwarf2_per_objfile
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
182 struct dwarf2_section_info macro;
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
185 struct dwarf2_section_info addr;
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
188 struct dwarf2_section_info gdb_index;
190 VEC (dwarf2_section_info_def) *types;
193 struct objfile *objfile;
195 /* Table of all the compilation units. This is used to locate
196 the target compilation unit of a particular reference. */
197 struct dwarf2_per_cu_data **all_comp_units;
199 /* The number of compilation units in ALL_COMP_UNITS. */
202 /* The number of .debug_types-related CUs. */
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
207 struct signatured_type **all_type_units;
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
224 /* Type unit statistics, to see how well the scaling improvements
228 int nr_uniq_abbrev_tables;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
242 /* Non-zero if we've check for whether there is a DWP file. */
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
252 /* A flag indicating wether this objfile has a section loaded at a
254 int has_section_at_zero;
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
258 unsigned char using_index;
260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
261 struct mapped_index *index_table;
263 /* When using index_table, this keeps track of all quick_file_names entries.
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
270 htab_t quick_file_names_table;
272 /* Set during partial symbol reading, to prevent queueing of full
274 int reading_partial_symbols;
276 /* Table mapping type DIEs to their struct type *.
277 This is NULL if not allocated yet.
278 The mapping is done via (CU/TU + DIE offset) -> type. */
279 htab_t die_type_hash;
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
285 static struct dwarf2_per_objfile *dwarf2_per_objfile;
287 /* Default names of the debugging sections. */
289 /* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
292 static const struct dwarf2_debug_sections dwarf2_elf_names =
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
299 { ".debug_macro", ".zdebug_macro" },
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
303 { ".debug_addr", ".zdebug_addr" },
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
306 { ".gdb_index", ".zgdb_index" },
310 /* List of DWO/DWP sections. */
312 static const struct dwop_section_names
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
341 /* local data types */
343 /* The data in a compilation unit header, after target2host
344 translation, looks like this. */
345 struct comp_unit_head
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
351 sect_offset abbrev_offset;
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
365 cu_offset first_die_offset;
368 /* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370 struct delayed_method_info
372 /* The type to which the method is attached, i.e., its parent class. */
375 /* The index of the method in the type's function fieldlists. */
378 /* The index of the method in the fieldlist. */
381 /* The name of the DIE. */
384 /* The DIE associated with this method. */
385 struct die_info *die;
388 typedef struct delayed_method_info delayed_method_info;
389 DEF_VEC_O (delayed_method_info);
391 /* Internal state when decoding a particular compilation unit. */
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
397 /* The header of the compilation unit. */
398 struct comp_unit_head header;
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
403 /* Non-zero if base_address has been set. */
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
410 const char *producer;
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
442 /* Backlink to our per_cu entry. */
443 struct dwarf2_per_cu_data *per_cu;
445 /* How many compilation units ago was this CU last referenced? */
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
452 /* Full DIEs if read in. */
453 struct die_info *dies;
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
479 struct dwo_unit *dwo_unit;
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
496 ULONGEST ranges_base;
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
505 unsigned int has_loclist : 1;
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
513 unsigned int producer_is_gcc_lt_4_3 : 1;
514 unsigned int producer_is_icc : 1;
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
520 unsigned int processing_has_namespace_info : 1;
523 /* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
525 read_symtab_private field of the psymtab. */
527 struct dwarf2_per_cu_data
529 /* The start offset and length of this compilation unit.
530 NOTE: Unlike comp_unit_head.length, this length includes
532 If the DIE refers to a DWO file, this is always of the original die,
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
539 unsigned int queued : 1;
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
550 unsigned int is_debug_types : 1;
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
574 struct dwarf2_section_info *section;
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
578 struct dwarf2_cu *cu;
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
583 struct objfile *objfile;
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
589 /* The partial symbol table associated with this compilation unit,
590 or NULL for unread partial units. */
591 struct partial_symtab *psymtab;
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
613 indices so we only pay a price for gold generated indices.
614 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
615 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
618 /* Entry in the signatured_types hash table. */
620 struct signatured_type
622 /* The "per_cu" object of this type.
623 This struct is used iff per_cu.is_debug_types.
624 N.B.: This is the first member so that it's easy to convert pointers
626 struct dwarf2_per_cu_data per_cu;
628 /* The type's signature. */
631 /* Offset in the TU of the type's DIE, as read from the TU header.
632 If this TU is a DWO stub and the definition lives in a DWO file
633 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
634 cu_offset type_offset_in_tu;
636 /* Offset in the section of the type's DIE.
637 If the definition lives in a DWO file, this is the offset in the
638 .debug_types.dwo section.
639 The value is zero until the actual value is known.
640 Zero is otherwise not a valid section offset. */
641 sect_offset type_offset_in_section;
643 /* Type units are grouped by their DW_AT_stmt_list entry so that they
644 can share them. This points to the containing symtab. */
645 struct type_unit_group *type_unit_group;
648 The first time we encounter this type we fully read it in and install it
649 in the symbol tables. Subsequent times we only need the type. */
652 /* Containing DWO unit.
653 This field is valid iff per_cu.reading_dwo_directly. */
654 struct dwo_unit *dwo_unit;
657 typedef struct signatured_type *sig_type_ptr;
658 DEF_VEC_P (sig_type_ptr);
660 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
661 This includes type_unit_group and quick_file_names. */
663 struct stmt_list_hash
665 /* The DWO unit this table is from or NULL if there is none. */
666 struct dwo_unit *dwo_unit;
668 /* Offset in .debug_line or .debug_line.dwo. */
669 sect_offset line_offset;
672 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
673 an object of this type. */
675 struct type_unit_group
677 /* dwarf2read.c's main "handle" on a TU symtab.
678 To simplify things we create an artificial CU that "includes" all the
679 type units using this stmt_list so that the rest of the code still has
680 a "per_cu" handle on the symtab.
681 This PER_CU is recognized by having no section. */
682 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
683 struct dwarf2_per_cu_data per_cu;
685 /* The TUs that share this DW_AT_stmt_list entry.
686 This is added to while parsing type units to build partial symtabs,
687 and is deleted afterwards and not used again. */
688 VEC (sig_type_ptr) *tus;
690 /* The primary symtab.
691 Type units in a group needn't all be defined in the same source file,
692 so we create an essentially anonymous symtab as the primary symtab. */
693 struct symtab *primary_symtab;
695 /* The data used to construct the hash key. */
696 struct stmt_list_hash hash;
698 /* The number of symtabs from the line header.
699 The value here must match line_header.num_file_names. */
700 unsigned int num_symtabs;
702 /* The symbol tables for this TU (obtained from the files listed in
704 WARNING: The order of entries here must match the order of entries
705 in the line header. After the first TU using this type_unit_group, the
706 line header for the subsequent TUs is recreated from this. This is done
707 because we need to use the same symtabs for each TU using the same
708 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
709 there's no guarantee the line header doesn't have duplicate entries. */
710 struct symtab **symtabs;
713 /* These sections are what may appear in a DWO file. */
717 struct dwarf2_section_info abbrev;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
722 struct dwarf2_section_info str;
723 struct dwarf2_section_info str_offsets;
724 /* In the case of a virtual DWO file, these two are unused. */
725 struct dwarf2_section_info info;
726 VEC (dwarf2_section_info_def) *types;
729 /* CUs/TUs in DWP/DWO files. */
733 /* Backlink to the containing struct dwo_file. */
734 struct dwo_file *dwo_file;
736 /* The "id" that distinguishes this CU/TU.
737 .debug_info calls this "dwo_id", .debug_types calls this "signature".
738 Since signatures came first, we stick with it for consistency. */
741 /* The section this CU/TU lives in, in the DWO file. */
742 struct dwarf2_section_info *section;
744 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
748 /* For types, offset in the type's DIE of the type defined by this TU. */
749 cu_offset type_offset_in_tu;
752 /* Data for one DWO file.
753 This includes virtual DWO files that have been packaged into a
758 /* The DW_AT_GNU_dwo_name attribute.
759 For virtual DWO files the name is constructed from the section offsets
760 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
761 from related CU+TUs. */
762 const char *dwo_name;
764 /* The DW_AT_comp_dir attribute. */
765 const char *comp_dir;
767 /* The bfd, when the file is open. Otherwise this is NULL.
768 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
771 /* Section info for this file. */
772 struct dwo_sections sections;
774 /* The CU in the file.
775 We only support one because having more than one requires hacking the
776 dwo_name of each to match, which is highly unlikely to happen.
777 Doing this means all TUs can share comp_dir: We also assume that
778 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
781 /* Table of TUs in the file.
782 Each element is a struct dwo_unit. */
786 /* These sections are what may appear in a DWP file. */
790 struct dwarf2_section_info str;
791 struct dwarf2_section_info cu_index;
792 struct dwarf2_section_info tu_index;
793 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
794 by section number. We don't need to record them here. */
797 /* These sections are what may appear in a virtual DWO file. */
799 struct virtual_dwo_sections
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info line;
803 struct dwarf2_section_info loc;
804 struct dwarf2_section_info macinfo;
805 struct dwarf2_section_info macro;
806 struct dwarf2_section_info str_offsets;
807 /* Each DWP hash table entry records one CU or one TU.
808 That is recorded here, and copied to dwo_unit.section. */
809 struct dwarf2_section_info info_or_types;
812 /* Contents of DWP hash tables. */
814 struct dwp_hash_table
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table, *section_pool;
820 /* Data for one DWP file. */
824 /* Name of the file. */
830 /* Section info for this file. */
831 struct dwp_sections sections;
833 /* Table of CUs in the file. */
834 const struct dwp_hash_table *cus;
836 /* Table of TUs in the file. */
837 const struct dwp_hash_table *tus;
839 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
842 /* Table to map ELF section numbers to their sections. */
843 unsigned int num_sections;
844 asection **elf_sections;
847 /* This represents a '.dwz' file. */
851 /* A dwz file can only contain a few sections. */
852 struct dwarf2_section_info abbrev;
853 struct dwarf2_section_info info;
854 struct dwarf2_section_info str;
855 struct dwarf2_section_info line;
856 struct dwarf2_section_info macro;
857 struct dwarf2_section_info gdb_index;
863 /* Struct used to pass misc. parameters to read_die_and_children, et
864 al. which are used for both .debug_info and .debug_types dies.
865 All parameters here are unchanging for the life of the call. This
866 struct exists to abstract away the constant parameters of die reading. */
868 struct die_reader_specs
870 /* die_section->asection->owner. */
873 /* The CU of the DIE we are parsing. */
874 struct dwarf2_cu *cu;
876 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
877 struct dwo_file *dwo_file;
879 /* The section the die comes from.
880 This is either .debug_info or .debug_types, or the .dwo variants. */
881 struct dwarf2_section_info *die_section;
883 /* die_section->buffer. */
884 const gdb_byte *buffer;
886 /* The end of the buffer. */
887 const gdb_byte *buffer_end;
889 /* The value of the DW_AT_comp_dir attribute. */
890 const char *comp_dir;
893 /* Type of function passed to init_cutu_and_read_dies, et.al. */
894 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
895 const gdb_byte *info_ptr,
896 struct die_info *comp_unit_die,
900 /* The line number information for a compilation unit (found in the
901 .debug_line section) begins with a "statement program header",
902 which contains the following information. */
905 unsigned int total_length;
906 unsigned short version;
907 unsigned int header_length;
908 unsigned char minimum_instruction_length;
909 unsigned char maximum_ops_per_instruction;
910 unsigned char default_is_stmt;
912 unsigned char line_range;
913 unsigned char opcode_base;
915 /* standard_opcode_lengths[i] is the number of operands for the
916 standard opcode whose value is i. This means that
917 standard_opcode_lengths[0] is unused, and the last meaningful
918 element is standard_opcode_lengths[opcode_base - 1]. */
919 unsigned char *standard_opcode_lengths;
921 /* The include_directories table. NOTE! These strings are not
922 allocated with xmalloc; instead, they are pointers into
923 debug_line_buffer. If you try to free them, `free' will get
925 unsigned int num_include_dirs, include_dirs_size;
926 const char **include_dirs;
928 /* The file_names table. NOTE! These strings are not allocated
929 with xmalloc; instead, they are pointers into debug_line_buffer.
930 Don't try to free them directly. */
931 unsigned int num_file_names, file_names_size;
935 unsigned int dir_index;
936 unsigned int mod_time;
938 int included_p; /* Non-zero if referenced by the Line Number Program. */
939 struct symtab *symtab; /* The associated symbol table, if any. */
942 /* The start and end of the statement program following this
943 header. These point into dwarf2_per_objfile->line_buffer. */
944 const gdb_byte *statement_program_start, *statement_program_end;
947 /* When we construct a partial symbol table entry we only
948 need this much information. */
949 struct partial_die_info
951 /* Offset of this DIE. */
954 /* DWARF-2 tag for this DIE. */
955 ENUM_BITFIELD(dwarf_tag) tag : 16;
957 /* Assorted flags describing the data found in this DIE. */
958 unsigned int has_children : 1;
959 unsigned int is_external : 1;
960 unsigned int is_declaration : 1;
961 unsigned int has_type : 1;
962 unsigned int has_specification : 1;
963 unsigned int has_pc_info : 1;
964 unsigned int may_be_inlined : 1;
966 /* Flag set if the SCOPE field of this structure has been
968 unsigned int scope_set : 1;
970 /* Flag set if the DIE has a byte_size attribute. */
971 unsigned int has_byte_size : 1;
973 /* Flag set if any of the DIE's children are template arguments. */
974 unsigned int has_template_arguments : 1;
976 /* Flag set if fixup_partial_die has been called on this die. */
977 unsigned int fixup_called : 1;
979 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
980 unsigned int is_dwz : 1;
982 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
983 unsigned int spec_is_dwz : 1;
985 /* The name of this DIE. Normally the value of DW_AT_name, but
986 sometimes a default name for unnamed DIEs. */
989 /* The linkage name, if present. */
990 const char *linkage_name;
992 /* The scope to prepend to our children. This is generally
993 allocated on the comp_unit_obstack, so will disappear
994 when this compilation unit leaves the cache. */
997 /* Some data associated with the partial DIE. The tag determines
998 which field is live. */
1001 /* The location description associated with this DIE, if any. */
1002 struct dwarf_block *locdesc;
1003 /* The offset of an import, for DW_TAG_imported_unit. */
1007 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1011 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1012 DW_AT_sibling, if any. */
1013 /* NOTE: This member isn't strictly necessary, read_partial_die could
1014 return DW_AT_sibling values to its caller load_partial_dies. */
1015 const gdb_byte *sibling;
1017 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1018 DW_AT_specification (or DW_AT_abstract_origin or
1019 DW_AT_extension). */
1020 sect_offset spec_offset;
1022 /* Pointers to this DIE's parent, first child, and next sibling,
1024 struct partial_die_info *die_parent, *die_child, *die_sibling;
1027 /* This data structure holds the information of an abbrev. */
1030 unsigned int number; /* number identifying abbrev */
1031 enum dwarf_tag tag; /* dwarf tag */
1032 unsigned short has_children; /* boolean */
1033 unsigned short num_attrs; /* number of attributes */
1034 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1035 struct abbrev_info *next; /* next in chain */
1040 ENUM_BITFIELD(dwarf_attribute) name : 16;
1041 ENUM_BITFIELD(dwarf_form) form : 16;
1044 /* Size of abbrev_table.abbrev_hash_table. */
1045 #define ABBREV_HASH_SIZE 121
1047 /* Top level data structure to contain an abbreviation table. */
1051 /* Where the abbrev table came from.
1052 This is used as a sanity check when the table is used. */
1055 /* Storage for the abbrev table. */
1056 struct obstack abbrev_obstack;
1058 /* Hash table of abbrevs.
1059 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1060 It could be statically allocated, but the previous code didn't so we
1062 struct abbrev_info **abbrevs;
1065 /* Attributes have a name and a value. */
1068 ENUM_BITFIELD(dwarf_attribute) name : 16;
1069 ENUM_BITFIELD(dwarf_form) form : 15;
1071 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1072 field should be in u.str (existing only for DW_STRING) but it is kept
1073 here for better struct attribute alignment. */
1074 unsigned int string_is_canonical : 1;
1079 struct dwarf_block *blk;
1088 /* This data structure holds a complete die structure. */
1091 /* DWARF-2 tag for this DIE. */
1092 ENUM_BITFIELD(dwarf_tag) tag : 16;
1094 /* Number of attributes */
1095 unsigned char num_attrs;
1097 /* True if we're presently building the full type name for the
1098 type derived from this DIE. */
1099 unsigned char building_fullname : 1;
1102 unsigned int abbrev;
1104 /* Offset in .debug_info or .debug_types section. */
1107 /* The dies in a compilation unit form an n-ary tree. PARENT
1108 points to this die's parent; CHILD points to the first child of
1109 this node; and all the children of a given node are chained
1110 together via their SIBLING fields. */
1111 struct die_info *child; /* Its first child, if any. */
1112 struct die_info *sibling; /* Its next sibling, if any. */
1113 struct die_info *parent; /* Its parent, if any. */
1115 /* An array of attributes, with NUM_ATTRS elements. There may be
1116 zero, but it's not common and zero-sized arrays are not
1117 sufficiently portable C. */
1118 struct attribute attrs[1];
1121 /* Get at parts of an attribute structure. */
1123 #define DW_STRING(attr) ((attr)->u.str)
1124 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1125 #define DW_UNSND(attr) ((attr)->u.unsnd)
1126 #define DW_BLOCK(attr) ((attr)->u.blk)
1127 #define DW_SND(attr) ((attr)->u.snd)
1128 #define DW_ADDR(attr) ((attr)->u.addr)
1129 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1131 /* Blocks are a bunch of untyped bytes. */
1136 /* Valid only if SIZE is not zero. */
1137 const gdb_byte *data;
1140 #ifndef ATTR_ALLOC_CHUNK
1141 #define ATTR_ALLOC_CHUNK 4
1144 /* Allocate fields for structs, unions and enums in this size. */
1145 #ifndef DW_FIELD_ALLOC_CHUNK
1146 #define DW_FIELD_ALLOC_CHUNK 4
1149 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1150 but this would require a corresponding change in unpack_field_as_long
1152 static int bits_per_byte = 8;
1154 /* The routines that read and process dies for a C struct or C++ class
1155 pass lists of data member fields and lists of member function fields
1156 in an instance of a field_info structure, as defined below. */
1159 /* List of data member and baseclasses fields. */
1162 struct nextfield *next;
1167 *fields, *baseclasses;
1169 /* Number of fields (including baseclasses). */
1172 /* Number of baseclasses. */
1175 /* Set if the accesibility of one of the fields is not public. */
1176 int non_public_fields;
1178 /* Member function fields array, entries are allocated in the order they
1179 are encountered in the object file. */
1182 struct nextfnfield *next;
1183 struct fn_field fnfield;
1187 /* Member function fieldlist array, contains name of possibly overloaded
1188 member function, number of overloaded member functions and a pointer
1189 to the head of the member function field chain. */
1194 struct nextfnfield *head;
1198 /* Number of entries in the fnfieldlists array. */
1201 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1202 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1203 struct typedef_field_list
1205 struct typedef_field field;
1206 struct typedef_field_list *next;
1208 *typedef_field_list;
1209 unsigned typedef_field_list_count;
1212 /* One item on the queue of compilation units to read in full symbols
1214 struct dwarf2_queue_item
1216 struct dwarf2_per_cu_data *per_cu;
1217 enum language pretend_language;
1218 struct dwarf2_queue_item *next;
1221 /* The current queue. */
1222 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1224 /* Loaded secondary compilation units are kept in memory until they
1225 have not been referenced for the processing of this many
1226 compilation units. Set this to zero to disable caching. Cache
1227 sizes of up to at least twenty will improve startup time for
1228 typical inter-CU-reference binaries, at an obvious memory cost. */
1229 static int dwarf2_max_cache_age = 5;
1231 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1232 struct cmd_list_element *c, const char *value)
1234 fprintf_filtered (file, _("The upper bound on the age of cached "
1235 "dwarf2 compilation units is %s.\n"),
1239 /* local function prototypes */
1241 static void dwarf2_locate_sections (bfd *, asection *, void *);
1243 static void dwarf2_find_base_address (struct die_info *die,
1244 struct dwarf2_cu *cu);
1246 static struct partial_symtab *create_partial_symtab
1247 (struct dwarf2_per_cu_data *per_cu, const char *name);
1249 static void dwarf2_build_psymtabs_hard (struct objfile *);
1251 static void scan_partial_symbols (struct partial_die_info *,
1252 CORE_ADDR *, CORE_ADDR *,
1253 int, struct dwarf2_cu *);
1255 static void add_partial_symbol (struct partial_die_info *,
1256 struct dwarf2_cu *);
1258 static void add_partial_namespace (struct partial_die_info *pdi,
1259 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1260 int need_pc, struct dwarf2_cu *cu);
1262 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1263 CORE_ADDR *highpc, int need_pc,
1264 struct dwarf2_cu *cu);
1266 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1267 struct dwarf2_cu *cu);
1269 static void add_partial_subprogram (struct partial_die_info *pdi,
1270 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1271 int need_pc, struct dwarf2_cu *cu);
1273 static void dwarf2_read_symtab (struct partial_symtab *,
1276 static void psymtab_to_symtab_1 (struct partial_symtab *);
1278 static struct abbrev_info *abbrev_table_lookup_abbrev
1279 (const struct abbrev_table *, unsigned int);
1281 static struct abbrev_table *abbrev_table_read_table
1282 (struct dwarf2_section_info *, sect_offset);
1284 static void abbrev_table_free (struct abbrev_table *);
1286 static void abbrev_table_free_cleanup (void *);
1288 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1289 struct dwarf2_section_info *);
1291 static void dwarf2_free_abbrev_table (void *);
1293 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1295 static struct partial_die_info *load_partial_dies
1296 (const struct die_reader_specs *, const gdb_byte *, int);
1298 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1299 struct partial_die_info *,
1300 struct abbrev_info *,
1304 static struct partial_die_info *find_partial_die (sect_offset, int,
1305 struct dwarf2_cu *);
1307 static void fixup_partial_die (struct partial_die_info *,
1308 struct dwarf2_cu *);
1310 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1311 struct attribute *, struct attr_abbrev *,
1314 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1316 static int read_1_signed_byte (bfd *, const gdb_byte *);
1318 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1320 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1322 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1324 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1327 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1329 static LONGEST read_checked_initial_length_and_offset
1330 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1331 unsigned int *, unsigned int *);
1333 static LONGEST read_offset (bfd *, const gdb_byte *,
1334 const struct comp_unit_head *,
1337 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1339 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1342 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1344 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1346 static const char *read_indirect_string (bfd *, const gdb_byte *,
1347 const struct comp_unit_head *,
1350 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1352 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1354 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1356 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1360 static const char *read_str_index (const struct die_reader_specs *reader,
1361 struct dwarf2_cu *cu, ULONGEST str_index);
1363 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1365 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1366 struct dwarf2_cu *);
1368 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1371 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1372 struct dwarf2_cu *cu);
1374 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1376 static struct die_info *die_specification (struct die_info *die,
1377 struct dwarf2_cu **);
1379 static void free_line_header (struct line_header *lh);
1381 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1382 struct dwarf2_cu *cu);
1384 static void dwarf_decode_lines (struct line_header *, const char *,
1385 struct dwarf2_cu *, struct partial_symtab *,
1388 static void dwarf2_start_subfile (const char *, const char *, const char *);
1390 static void dwarf2_start_symtab (struct dwarf2_cu *,
1391 const char *, const char *, CORE_ADDR);
1393 static struct symbol *new_symbol (struct die_info *, struct type *,
1394 struct dwarf2_cu *);
1396 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1397 struct dwarf2_cu *, struct symbol *);
1399 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1400 struct dwarf2_cu *);
1402 static void dwarf2_const_value_attr (const struct attribute *attr,
1405 struct obstack *obstack,
1406 struct dwarf2_cu *cu, LONGEST *value,
1407 const gdb_byte **bytes,
1408 struct dwarf2_locexpr_baton **baton);
1410 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1412 static int need_gnat_info (struct dwarf2_cu *);
1414 static struct type *die_descriptive_type (struct die_info *,
1415 struct dwarf2_cu *);
1417 static void set_descriptive_type (struct type *, struct die_info *,
1418 struct dwarf2_cu *);
1420 static struct type *die_containing_type (struct die_info *,
1421 struct dwarf2_cu *);
1423 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1424 struct dwarf2_cu *);
1426 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1428 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1430 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1432 static char *typename_concat (struct obstack *obs, const char *prefix,
1433 const char *suffix, int physname,
1434 struct dwarf2_cu *cu);
1436 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1438 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1440 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1442 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1444 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1446 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1447 struct dwarf2_cu *, struct partial_symtab *);
1449 static int dwarf2_get_pc_bounds (struct die_info *,
1450 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1451 struct partial_symtab *);
1453 static void get_scope_pc_bounds (struct die_info *,
1454 CORE_ADDR *, CORE_ADDR *,
1455 struct dwarf2_cu *);
1457 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1458 CORE_ADDR, struct dwarf2_cu *);
1460 static void dwarf2_add_field (struct field_info *, struct die_info *,
1461 struct dwarf2_cu *);
1463 static void dwarf2_attach_fields_to_type (struct field_info *,
1464 struct type *, struct dwarf2_cu *);
1466 static void dwarf2_add_member_fn (struct field_info *,
1467 struct die_info *, struct type *,
1468 struct dwarf2_cu *);
1470 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1472 struct dwarf2_cu *);
1474 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1476 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1478 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1480 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1482 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1484 static struct type *read_module_type (struct die_info *die,
1485 struct dwarf2_cu *cu);
1487 static const char *namespace_name (struct die_info *die,
1488 int *is_anonymous, struct dwarf2_cu *);
1490 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1492 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1494 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1495 struct dwarf2_cu *);
1497 static struct die_info *read_die_and_siblings_1
1498 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1501 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1502 const gdb_byte *info_ptr,
1503 const gdb_byte **new_info_ptr,
1504 struct die_info *parent);
1506 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1507 struct die_info **, const gdb_byte *,
1510 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1511 struct die_info **, const gdb_byte *,
1514 static void process_die (struct die_info *, struct dwarf2_cu *);
1516 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1519 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1521 static const char *dwarf2_full_name (const char *name,
1522 struct die_info *die,
1523 struct dwarf2_cu *cu);
1525 static const char *dwarf2_physname (const char *name, struct die_info *die,
1526 struct dwarf2_cu *cu);
1528 static struct die_info *dwarf2_extension (struct die_info *die,
1529 struct dwarf2_cu **);
1531 static const char *dwarf_tag_name (unsigned int);
1533 static const char *dwarf_attr_name (unsigned int);
1535 static const char *dwarf_form_name (unsigned int);
1537 static char *dwarf_bool_name (unsigned int);
1539 static const char *dwarf_type_encoding_name (unsigned int);
1541 static struct die_info *sibling_die (struct die_info *);
1543 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1545 static void dump_die_for_error (struct die_info *);
1547 static void dump_die_1 (struct ui_file *, int level, int max_level,
1550 /*static*/ void dump_die (struct die_info *, int max_level);
1552 static void store_in_ref_table (struct die_info *,
1553 struct dwarf2_cu *);
1555 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1557 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1559 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1560 const struct attribute *,
1561 struct dwarf2_cu **);
1563 static struct die_info *follow_die_ref (struct die_info *,
1564 const struct attribute *,
1565 struct dwarf2_cu **);
1567 static struct die_info *follow_die_sig (struct die_info *,
1568 const struct attribute *,
1569 struct dwarf2_cu **);
1571 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1572 struct dwarf2_cu *);
1574 static struct type *get_DW_AT_signature_type (struct die_info *,
1575 const struct attribute *,
1576 struct dwarf2_cu *);
1578 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1580 static void read_signatured_type (struct signatured_type *);
1582 static struct type_unit_group *get_type_unit_group
1583 (struct dwarf2_cu *, const struct attribute *);
1585 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1587 /* memory allocation interface */
1589 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1591 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1593 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1596 static int attr_form_is_block (const struct attribute *);
1598 static int attr_form_is_section_offset (const struct attribute *);
1600 static int attr_form_is_constant (const struct attribute *);
1602 static int attr_form_is_ref (const struct attribute *);
1604 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1605 struct dwarf2_loclist_baton *baton,
1606 const struct attribute *attr);
1608 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1610 struct dwarf2_cu *cu,
1613 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1614 const gdb_byte *info_ptr,
1615 struct abbrev_info *abbrev);
1617 static void free_stack_comp_unit (void *);
1619 static hashval_t partial_die_hash (const void *item);
1621 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1623 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1624 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1626 static void init_one_comp_unit (struct dwarf2_cu *cu,
1627 struct dwarf2_per_cu_data *per_cu);
1629 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1630 struct die_info *comp_unit_die,
1631 enum language pretend_language);
1633 static void free_heap_comp_unit (void *);
1635 static void free_cached_comp_units (void *);
1637 static void age_cached_comp_units (void);
1639 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1641 static struct type *set_die_type (struct die_info *, struct type *,
1642 struct dwarf2_cu *);
1644 static void create_all_comp_units (struct objfile *);
1646 static int create_all_type_units (struct objfile *);
1648 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1651 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1654 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1657 static void dwarf2_add_dependence (struct dwarf2_cu *,
1658 struct dwarf2_per_cu_data *);
1660 static void dwarf2_mark (struct dwarf2_cu *);
1662 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1664 static struct type *get_die_type_at_offset (sect_offset,
1665 struct dwarf2_per_cu_data *);
1667 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1669 static void dwarf2_release_queue (void *dummy);
1671 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1672 enum language pretend_language);
1674 static void process_queue (void);
1676 static void find_file_and_directory (struct die_info *die,
1677 struct dwarf2_cu *cu,
1678 const char **name, const char **comp_dir);
1680 static char *file_full_name (int file, struct line_header *lh,
1681 const char *comp_dir);
1683 static const gdb_byte *read_and_check_comp_unit_head
1684 (struct comp_unit_head *header,
1685 struct dwarf2_section_info *section,
1686 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1687 int is_debug_types_section);
1689 static void init_cutu_and_read_dies
1690 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1691 int use_existing_cu, int keep,
1692 die_reader_func_ftype *die_reader_func, void *data);
1694 static void init_cutu_and_read_dies_simple
1695 (struct dwarf2_per_cu_data *this_cu,
1696 die_reader_func_ftype *die_reader_func, void *data);
1698 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1700 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1702 static struct dwo_unit *lookup_dwo_in_dwp
1703 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1704 const char *comp_dir, ULONGEST signature, int is_debug_types);
1706 static struct dwp_file *get_dwp_file (void);
1708 static struct dwo_unit *lookup_dwo_comp_unit
1709 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1711 static struct dwo_unit *lookup_dwo_type_unit
1712 (struct signatured_type *, const char *, const char *);
1714 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1716 static void free_dwo_file_cleanup (void *);
1718 static void process_cu_includes (void);
1720 static void check_producer (struct dwarf2_cu *cu);
1722 /* Various complaints about symbol reading that don't abort the process. */
1725 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1727 complaint (&symfile_complaints,
1728 _("statement list doesn't fit in .debug_line section"));
1732 dwarf2_debug_line_missing_file_complaint (void)
1734 complaint (&symfile_complaints,
1735 _(".debug_line section has line data without a file"));
1739 dwarf2_debug_line_missing_end_sequence_complaint (void)
1741 complaint (&symfile_complaints,
1742 _(".debug_line section has line "
1743 "program sequence without an end"));
1747 dwarf2_complex_location_expr_complaint (void)
1749 complaint (&symfile_complaints, _("location expression too complex"));
1753 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1756 complaint (&symfile_complaints,
1757 _("const value length mismatch for '%s', got %d, expected %d"),
1762 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1764 complaint (&symfile_complaints,
1765 _("debug info runs off end of %s section"
1767 section->asection->name,
1768 bfd_get_filename (section->asection->owner));
1772 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1774 complaint (&symfile_complaints,
1775 _("macro debug info contains a "
1776 "malformed macro definition:\n`%s'"),
1781 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1783 complaint (&symfile_complaints,
1784 _("invalid attribute class or form for '%s' in '%s'"),
1790 /* Convert VALUE between big- and little-endian. */
1792 byte_swap (offset_type value)
1796 result = (value & 0xff) << 24;
1797 result |= (value & 0xff00) << 8;
1798 result |= (value & 0xff0000) >> 8;
1799 result |= (value & 0xff000000) >> 24;
1803 #define MAYBE_SWAP(V) byte_swap (V)
1806 #define MAYBE_SWAP(V) (V)
1807 #endif /* WORDS_BIGENDIAN */
1809 /* The suffix for an index file. */
1810 #define INDEX_SUFFIX ".gdb-index"
1812 /* Try to locate the sections we need for DWARF 2 debugging
1813 information and return true if we have enough to do something.
1814 NAMES points to the dwarf2 section names, or is NULL if the standard
1815 ELF names are used. */
1818 dwarf2_has_info (struct objfile *objfile,
1819 const struct dwarf2_debug_sections *names)
1821 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1822 if (!dwarf2_per_objfile)
1824 /* Initialize per-objfile state. */
1825 struct dwarf2_per_objfile *data
1826 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1828 memset (data, 0, sizeof (*data));
1829 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1830 dwarf2_per_objfile = data;
1832 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1834 dwarf2_per_objfile->objfile = objfile;
1836 return (dwarf2_per_objfile->info.asection != NULL
1837 && dwarf2_per_objfile->abbrev.asection != NULL);
1840 /* When loading sections, we look either for uncompressed section or for
1841 compressed section names. */
1844 section_is_p (const char *section_name,
1845 const struct dwarf2_section_names *names)
1847 if (names->normal != NULL
1848 && strcmp (section_name, names->normal) == 0)
1850 if (names->compressed != NULL
1851 && strcmp (section_name, names->compressed) == 0)
1856 /* This function is mapped across the sections and remembers the
1857 offset and size of each of the debugging sections we are interested
1861 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1863 const struct dwarf2_debug_sections *names;
1864 flagword aflag = bfd_get_section_flags (abfd, sectp);
1867 names = &dwarf2_elf_names;
1869 names = (const struct dwarf2_debug_sections *) vnames;
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1874 else if (section_is_p (sectp->name, &names->info))
1876 dwarf2_per_objfile->info.asection = sectp;
1877 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1879 else if (section_is_p (sectp->name, &names->abbrev))
1881 dwarf2_per_objfile->abbrev.asection = sectp;
1882 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1884 else if (section_is_p (sectp->name, &names->line))
1886 dwarf2_per_objfile->line.asection = sectp;
1887 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1889 else if (section_is_p (sectp->name, &names->loc))
1891 dwarf2_per_objfile->loc.asection = sectp;
1892 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1894 else if (section_is_p (sectp->name, &names->macinfo))
1896 dwarf2_per_objfile->macinfo.asection = sectp;
1897 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1899 else if (section_is_p (sectp->name, &names->macro))
1901 dwarf2_per_objfile->macro.asection = sectp;
1902 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1904 else if (section_is_p (sectp->name, &names->str))
1906 dwarf2_per_objfile->str.asection = sectp;
1907 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1909 else if (section_is_p (sectp->name, &names->addr))
1911 dwarf2_per_objfile->addr.asection = sectp;
1912 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1914 else if (section_is_p (sectp->name, &names->frame))
1916 dwarf2_per_objfile->frame.asection = sectp;
1917 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1919 else if (section_is_p (sectp->name, &names->eh_frame))
1921 dwarf2_per_objfile->eh_frame.asection = sectp;
1922 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1924 else if (section_is_p (sectp->name, &names->ranges))
1926 dwarf2_per_objfile->ranges.asection = sectp;
1927 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1929 else if (section_is_p (sectp->name, &names->types))
1931 struct dwarf2_section_info type_section;
1933 memset (&type_section, 0, sizeof (type_section));
1934 type_section.asection = sectp;
1935 type_section.size = bfd_get_section_size (sectp);
1937 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1940 else if (section_is_p (sectp->name, &names->gdb_index))
1942 dwarf2_per_objfile->gdb_index.asection = sectp;
1943 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1946 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1947 && bfd_section_vma (abfd, sectp) == 0)
1948 dwarf2_per_objfile->has_section_at_zero = 1;
1951 /* A helper function that decides whether a section is empty,
1955 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1957 return info->asection == NULL || info->size == 0;
1960 /* Read the contents of the section INFO.
1961 OBJFILE is the main object file, but not necessarily the file where
1962 the section comes from. E.g., for DWO files INFO->asection->owner
1963 is the bfd of the DWO file.
1964 If the section is compressed, uncompress it before returning. */
1967 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1969 asection *sectp = info->asection;
1971 gdb_byte *buf, *retbuf;
1972 unsigned char header[4];
1976 info->buffer = NULL;
1979 if (dwarf2_section_empty_p (info))
1982 abfd = sectp->owner;
1984 /* If the section has relocations, we must read it ourselves.
1985 Otherwise we attach it to the BFD. */
1986 if ((sectp->flags & SEC_RELOC) == 0)
1988 info->buffer = gdb_bfd_map_section (sectp, &info->size);
1992 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1995 /* When debugging .o files, we may need to apply relocations; see
1996 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1997 We never compress sections in .o files, so we only need to
1998 try this when the section is not compressed. */
1999 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2002 info->buffer = retbuf;
2006 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2007 || bfd_bread (buf, info->size, abfd) != info->size)
2008 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2009 bfd_get_filename (abfd));
2012 /* A helper function that returns the size of a section in a safe way.
2013 If you are positive that the section has been read before using the
2014 size, then it is safe to refer to the dwarf2_section_info object's
2015 "size" field directly. In other cases, you must call this
2016 function, because for compressed sections the size field is not set
2017 correctly until the section has been read. */
2019 static bfd_size_type
2020 dwarf2_section_size (struct objfile *objfile,
2021 struct dwarf2_section_info *info)
2024 dwarf2_read_section (objfile, info);
2028 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2032 dwarf2_get_section_info (struct objfile *objfile,
2033 enum dwarf2_section_enum sect,
2034 asection **sectp, const gdb_byte **bufp,
2035 bfd_size_type *sizep)
2037 struct dwarf2_per_objfile *data
2038 = objfile_data (objfile, dwarf2_objfile_data_key);
2039 struct dwarf2_section_info *info;
2041 /* We may see an objfile without any DWARF, in which case we just
2052 case DWARF2_DEBUG_FRAME:
2053 info = &data->frame;
2055 case DWARF2_EH_FRAME:
2056 info = &data->eh_frame;
2059 gdb_assert_not_reached ("unexpected section");
2062 dwarf2_read_section (objfile, info);
2064 *sectp = info->asection;
2065 *bufp = info->buffer;
2066 *sizep = info->size;
2069 /* A helper function to find the sections for a .dwz file. */
2072 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2074 struct dwz_file *dwz_file = arg;
2076 /* Note that we only support the standard ELF names, because .dwz
2077 is ELF-only (at the time of writing). */
2078 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2080 dwz_file->abbrev.asection = sectp;
2081 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2083 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2085 dwz_file->info.asection = sectp;
2086 dwz_file->info.size = bfd_get_section_size (sectp);
2088 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2090 dwz_file->str.asection = sectp;
2091 dwz_file->str.size = bfd_get_section_size (sectp);
2093 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2095 dwz_file->line.asection = sectp;
2096 dwz_file->line.size = bfd_get_section_size (sectp);
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2100 dwz_file->macro.asection = sectp;
2101 dwz_file->macro.size = bfd_get_section_size (sectp);
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2105 dwz_file->gdb_index.asection = sectp;
2106 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2110 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2111 there is no .gnu_debugaltlink section in the file. Error if there
2112 is such a section but the file cannot be found. */
2114 static struct dwz_file *
2115 dwarf2_get_dwz_file (void)
2119 struct cleanup *cleanup;
2120 const char *filename;
2121 struct dwz_file *result;
2122 unsigned long buildid;
2124 if (dwarf2_per_objfile->dwz_file != NULL)
2125 return dwarf2_per_objfile->dwz_file;
2127 bfd_set_error (bfd_error_no_error);
2128 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2132 if (bfd_get_error () == bfd_error_no_error)
2134 error (_("could not read '.gnu_debugaltlink' section: %s"),
2135 bfd_errmsg (bfd_get_error ()));
2137 cleanup = make_cleanup (xfree, data);
2139 filename = (const char *) data;
2140 if (!IS_ABSOLUTE_PATH (filename))
2142 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2145 make_cleanup (xfree, abs);
2146 abs = ldirname (abs);
2147 make_cleanup (xfree, abs);
2149 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2150 make_cleanup (xfree, rel);
2154 /* The format is just a NUL-terminated file name, followed by the
2155 build-id. For now, though, we ignore the build-id. */
2156 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2157 if (dwz_bfd == NULL)
2158 error (_("could not read '%s': %s"), filename,
2159 bfd_errmsg (bfd_get_error ()));
2161 if (!bfd_check_format (dwz_bfd, bfd_object))
2163 gdb_bfd_unref (dwz_bfd);
2164 error (_("file '%s' was not usable: %s"), filename,
2165 bfd_errmsg (bfd_get_error ()));
2168 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2170 result->dwz_bfd = dwz_bfd;
2172 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2174 do_cleanups (cleanup);
2176 dwarf2_per_objfile->dwz_file = result;
2180 /* DWARF quick_symbols_functions support. */
2182 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2183 unique line tables, so we maintain a separate table of all .debug_line
2184 derived entries to support the sharing.
2185 All the quick functions need is the list of file names. We discard the
2186 line_header when we're done and don't need to record it here. */
2187 struct quick_file_names
2189 /* The data used to construct the hash key. */
2190 struct stmt_list_hash hash;
2192 /* The number of entries in file_names, real_names. */
2193 unsigned int num_file_names;
2195 /* The file names from the line table, after being run through
2197 const char **file_names;
2199 /* The file names from the line table after being run through
2200 gdb_realpath. These are computed lazily. */
2201 const char **real_names;
2204 /* When using the index (and thus not using psymtabs), each CU has an
2205 object of this type. This is used to hold information needed by
2206 the various "quick" methods. */
2207 struct dwarf2_per_cu_quick_data
2209 /* The file table. This can be NULL if there was no file table
2210 or it's currently not read in.
2211 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2212 struct quick_file_names *file_names;
2214 /* The corresponding symbol table. This is NULL if symbols for this
2215 CU have not yet been read. */
2216 struct symtab *symtab;
2218 /* A temporary mark bit used when iterating over all CUs in
2219 expand_symtabs_matching. */
2220 unsigned int mark : 1;
2222 /* True if we've tried to read the file table and found there isn't one.
2223 There will be no point in trying to read it again next time. */
2224 unsigned int no_file_data : 1;
2227 /* Utility hash function for a stmt_list_hash. */
2230 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2234 if (stmt_list_hash->dwo_unit != NULL)
2235 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2236 v += stmt_list_hash->line_offset.sect_off;
2240 /* Utility equality function for a stmt_list_hash. */
2243 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2244 const struct stmt_list_hash *rhs)
2246 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2248 if (lhs->dwo_unit != NULL
2249 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2252 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2255 /* Hash function for a quick_file_names. */
2258 hash_file_name_entry (const void *e)
2260 const struct quick_file_names *file_data = e;
2262 return hash_stmt_list_entry (&file_data->hash);
2265 /* Equality function for a quick_file_names. */
2268 eq_file_name_entry (const void *a, const void *b)
2270 const struct quick_file_names *ea = a;
2271 const struct quick_file_names *eb = b;
2273 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2276 /* Delete function for a quick_file_names. */
2279 delete_file_name_entry (void *e)
2281 struct quick_file_names *file_data = e;
2284 for (i = 0; i < file_data->num_file_names; ++i)
2286 xfree ((void*) file_data->file_names[i]);
2287 if (file_data->real_names)
2288 xfree ((void*) file_data->real_names[i]);
2291 /* The space for the struct itself lives on objfile_obstack,
2292 so we don't free it here. */
2295 /* Create a quick_file_names hash table. */
2298 create_quick_file_names_table (unsigned int nr_initial_entries)
2300 return htab_create_alloc (nr_initial_entries,
2301 hash_file_name_entry, eq_file_name_entry,
2302 delete_file_name_entry, xcalloc, xfree);
2305 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2306 have to be created afterwards. You should call age_cached_comp_units after
2307 processing PER_CU->CU. dw2_setup must have been already called. */
2310 load_cu (struct dwarf2_per_cu_data *per_cu)
2312 if (per_cu->is_debug_types)
2313 load_full_type_unit (per_cu);
2315 load_full_comp_unit (per_cu, language_minimal);
2317 gdb_assert (per_cu->cu != NULL);
2319 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2322 /* Read in the symbols for PER_CU. */
2325 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2327 struct cleanup *back_to;
2329 /* Skip type_unit_groups, reading the type units they contain
2330 is handled elsewhere. */
2331 if (IS_TYPE_UNIT_GROUP (per_cu))
2334 back_to = make_cleanup (dwarf2_release_queue, NULL);
2336 if (dwarf2_per_objfile->using_index
2337 ? per_cu->v.quick->symtab == NULL
2338 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2340 queue_comp_unit (per_cu, language_minimal);
2343 /* If we just loaded a CU from a DWO, and we're working with an index
2344 that may badly handle TUs, load all the TUs in that DWO as well.
2345 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2346 if (!per_cu->is_debug_types
2347 && per_cu->cu->dwo_unit != NULL
2348 && dwarf2_per_objfile->index_table != NULL
2349 && dwarf2_per_objfile->index_table->version <= 7
2350 /* DWP files aren't supported yet. */
2351 && get_dwp_file () == NULL)
2352 queue_and_load_all_dwo_tus (per_cu);
2357 /* Age the cache, releasing compilation units that have not
2358 been used recently. */
2359 age_cached_comp_units ();
2361 do_cleanups (back_to);
2364 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2365 the objfile from which this CU came. Returns the resulting symbol
2368 static struct symtab *
2369 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2371 gdb_assert (dwarf2_per_objfile->using_index);
2372 if (!per_cu->v.quick->symtab)
2374 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2375 increment_reading_symtab ();
2376 dw2_do_instantiate_symtab (per_cu);
2377 process_cu_includes ();
2378 do_cleanups (back_to);
2380 return per_cu->v.quick->symtab;
2383 /* Return the CU given its index.
2385 This is intended for loops like:
2387 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2388 + dwarf2_per_objfile->n_type_units); ++i)
2390 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2396 static struct dwarf2_per_cu_data *
2397 dw2_get_cu (int index)
2399 if (index >= dwarf2_per_objfile->n_comp_units)
2401 index -= dwarf2_per_objfile->n_comp_units;
2402 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2403 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2406 return dwarf2_per_objfile->all_comp_units[index];
2409 /* Return the primary CU given its index.
2410 The difference between this function and dw2_get_cu is in the handling
2411 of type units (TUs). Here we return the type_unit_group object.
2413 This is intended for loops like:
2415 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2416 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2418 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2424 static struct dwarf2_per_cu_data *
2425 dw2_get_primary_cu (int index)
2427 if (index >= dwarf2_per_objfile->n_comp_units)
2429 index -= dwarf2_per_objfile->n_comp_units;
2430 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2431 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2434 return dwarf2_per_objfile->all_comp_units[index];
2437 /* A helper for create_cus_from_index that handles a given list of
2441 create_cus_from_index_list (struct objfile *objfile,
2442 const gdb_byte *cu_list, offset_type n_elements,
2443 struct dwarf2_section_info *section,
2449 for (i = 0; i < n_elements; i += 2)
2451 struct dwarf2_per_cu_data *the_cu;
2452 ULONGEST offset, length;
2454 gdb_static_assert (sizeof (ULONGEST) >= 8);
2455 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2456 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2459 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2460 struct dwarf2_per_cu_data);
2461 the_cu->offset.sect_off = offset;
2462 the_cu->length = length;
2463 the_cu->objfile = objfile;
2464 the_cu->section = section;
2465 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2466 struct dwarf2_per_cu_quick_data);
2467 the_cu->is_dwz = is_dwz;
2468 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2472 /* Read the CU list from the mapped index, and use it to create all
2473 the CU objects for this objfile. */
2476 create_cus_from_index (struct objfile *objfile,
2477 const gdb_byte *cu_list, offset_type cu_list_elements,
2478 const gdb_byte *dwz_list, offset_type dwz_elements)
2480 struct dwz_file *dwz;
2482 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2483 dwarf2_per_objfile->all_comp_units
2484 = obstack_alloc (&objfile->objfile_obstack,
2485 dwarf2_per_objfile->n_comp_units
2486 * sizeof (struct dwarf2_per_cu_data *));
2488 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2489 &dwarf2_per_objfile->info, 0, 0);
2491 if (dwz_elements == 0)
2494 dwz = dwarf2_get_dwz_file ();
2495 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2496 cu_list_elements / 2);
2499 /* Create the signatured type hash table from the index. */
2502 create_signatured_type_table_from_index (struct objfile *objfile,
2503 struct dwarf2_section_info *section,
2504 const gdb_byte *bytes,
2505 offset_type elements)
2508 htab_t sig_types_hash;
2510 dwarf2_per_objfile->n_type_units = elements / 3;
2511 dwarf2_per_objfile->all_type_units
2512 = xmalloc (dwarf2_per_objfile->n_type_units
2513 * sizeof (struct signatured_type *));
2515 sig_types_hash = allocate_signatured_type_table (objfile);
2517 for (i = 0; i < elements; i += 3)
2519 struct signatured_type *sig_type;
2520 ULONGEST offset, type_offset_in_tu, signature;
2523 gdb_static_assert (sizeof (ULONGEST) >= 8);
2524 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2525 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2527 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2530 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2531 struct signatured_type);
2532 sig_type->signature = signature;
2533 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2534 sig_type->per_cu.is_debug_types = 1;
2535 sig_type->per_cu.section = section;
2536 sig_type->per_cu.offset.sect_off = offset;
2537 sig_type->per_cu.objfile = objfile;
2538 sig_type->per_cu.v.quick
2539 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2540 struct dwarf2_per_cu_quick_data);
2542 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2545 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2548 dwarf2_per_objfile->signatured_types = sig_types_hash;
2551 /* Read the address map data from the mapped index, and use it to
2552 populate the objfile's psymtabs_addrmap. */
2555 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2557 const gdb_byte *iter, *end;
2558 struct obstack temp_obstack;
2559 struct addrmap *mutable_map;
2560 struct cleanup *cleanup;
2563 obstack_init (&temp_obstack);
2564 cleanup = make_cleanup_obstack_free (&temp_obstack);
2565 mutable_map = addrmap_create_mutable (&temp_obstack);
2567 iter = index->address_table;
2568 end = iter + index->address_table_size;
2570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2574 ULONGEST hi, lo, cu_index;
2575 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2577 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2579 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2584 complaint (&symfile_complaints,
2585 _(".gdb_index address table has invalid range (%s - %s)"),
2586 hex_string (lo), hex_string (hi));
2590 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2592 complaint (&symfile_complaints,
2593 _(".gdb_index address table has invalid CU number %u"),
2594 (unsigned) cu_index);
2598 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2599 dw2_get_cu (cu_index));
2602 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2603 &objfile->objfile_obstack);
2604 do_cleanups (cleanup);
2607 /* The hash function for strings in the mapped index. This is the same as
2608 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2609 implementation. This is necessary because the hash function is tied to the
2610 format of the mapped index file. The hash values do not have to match with
2613 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2616 mapped_index_string_hash (int index_version, const void *p)
2618 const unsigned char *str = (const unsigned char *) p;
2622 while ((c = *str++) != 0)
2624 if (index_version >= 5)
2626 r = r * 67 + c - 113;
2632 /* Find a slot in the mapped index INDEX for the object named NAME.
2633 If NAME is found, set *VEC_OUT to point to the CU vector in the
2634 constant pool and return 1. If NAME cannot be found, return 0. */
2637 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2638 offset_type **vec_out)
2640 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2642 offset_type slot, step;
2643 int (*cmp) (const char *, const char *);
2645 if (current_language->la_language == language_cplus
2646 || current_language->la_language == language_java
2647 || current_language->la_language == language_fortran)
2649 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2651 const char *paren = strchr (name, '(');
2657 dup = xmalloc (paren - name + 1);
2658 memcpy (dup, name, paren - name);
2659 dup[paren - name] = 0;
2661 make_cleanup (xfree, dup);
2666 /* Index version 4 did not support case insensitive searches. But the
2667 indices for case insensitive languages are built in lowercase, therefore
2668 simulate our NAME being searched is also lowercased. */
2669 hash = mapped_index_string_hash ((index->version == 4
2670 && case_sensitivity == case_sensitive_off
2671 ? 5 : index->version),
2674 slot = hash & (index->symbol_table_slots - 1);
2675 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2676 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2680 /* Convert a slot number to an offset into the table. */
2681 offset_type i = 2 * slot;
2683 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2685 do_cleanups (back_to);
2689 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2690 if (!cmp (name, str))
2692 *vec_out = (offset_type *) (index->constant_pool
2693 + MAYBE_SWAP (index->symbol_table[i + 1]));
2694 do_cleanups (back_to);
2698 slot = (slot + step) & (index->symbol_table_slots - 1);
2702 /* A helper function that reads the .gdb_index from SECTION and fills
2703 in MAP. FILENAME is the name of the file containing the section;
2704 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2705 ok to use deprecated sections.
2707 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2708 out parameters that are filled in with information about the CU and
2709 TU lists in the section.
2711 Returns 1 if all went well, 0 otherwise. */
2714 read_index_from_section (struct objfile *objfile,
2715 const char *filename,
2717 struct dwarf2_section_info *section,
2718 struct mapped_index *map,
2719 const gdb_byte **cu_list,
2720 offset_type *cu_list_elements,
2721 const gdb_byte **types_list,
2722 offset_type *types_list_elements)
2724 const gdb_byte *addr;
2725 offset_type version;
2726 offset_type *metadata;
2729 if (dwarf2_section_empty_p (section))
2732 /* Older elfutils strip versions could keep the section in the main
2733 executable while splitting it for the separate debug info file. */
2734 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2737 dwarf2_read_section (objfile, section);
2739 addr = section->buffer;
2740 /* Version check. */
2741 version = MAYBE_SWAP (*(offset_type *) addr);
2742 /* Versions earlier than 3 emitted every copy of a psymbol. This
2743 causes the index to behave very poorly for certain requests. Version 3
2744 contained incomplete addrmap. So, it seems better to just ignore such
2748 static int warning_printed = 0;
2749 if (!warning_printed)
2751 warning (_("Skipping obsolete .gdb_index section in %s."),
2753 warning_printed = 1;
2757 /* Index version 4 uses a different hash function than index version
2760 Versions earlier than 6 did not emit psymbols for inlined
2761 functions. Using these files will cause GDB not to be able to
2762 set breakpoints on inlined functions by name, so we ignore these
2763 indices unless the user has done
2764 "set use-deprecated-index-sections on". */
2765 if (version < 6 && !deprecated_ok)
2767 static int warning_printed = 0;
2768 if (!warning_printed)
2771 Skipping deprecated .gdb_index section in %s.\n\
2772 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2773 to use the section anyway."),
2775 warning_printed = 1;
2779 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2780 of the TU (for symbols coming from TUs). It's just a performance bug, and
2781 we can't distinguish gdb-generated indices from gold-generated ones, so
2782 nothing to do here. */
2784 /* Indexes with higher version than the one supported by GDB may be no
2785 longer backward compatible. */
2789 map->version = version;
2790 map->total_size = section->size;
2792 metadata = (offset_type *) (addr + sizeof (offset_type));
2795 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2796 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2800 *types_list = addr + MAYBE_SWAP (metadata[i]);
2801 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2802 - MAYBE_SWAP (metadata[i]))
2806 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2807 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2808 - MAYBE_SWAP (metadata[i]));
2811 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2812 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2813 - MAYBE_SWAP (metadata[i]))
2814 / (2 * sizeof (offset_type)));
2817 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2823 /* Read the index file. If everything went ok, initialize the "quick"
2824 elements of all the CUs and return 1. Otherwise, return 0. */
2827 dwarf2_read_index (struct objfile *objfile)
2829 struct mapped_index local_map, *map;
2830 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2831 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2832 struct dwz_file *dwz;
2834 if (!read_index_from_section (objfile, objfile_name (objfile),
2835 use_deprecated_index_sections,
2836 &dwarf2_per_objfile->gdb_index, &local_map,
2837 &cu_list, &cu_list_elements,
2838 &types_list, &types_list_elements))
2841 /* Don't use the index if it's empty. */
2842 if (local_map.symbol_table_slots == 0)
2845 /* If there is a .dwz file, read it so we can get its CU list as
2847 dwz = dwarf2_get_dwz_file ();
2850 struct mapped_index dwz_map;
2851 const gdb_byte *dwz_types_ignore;
2852 offset_type dwz_types_elements_ignore;
2854 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2856 &dwz->gdb_index, &dwz_map,
2857 &dwz_list, &dwz_list_elements,
2859 &dwz_types_elements_ignore))
2861 warning (_("could not read '.gdb_index' section from %s; skipping"),
2862 bfd_get_filename (dwz->dwz_bfd));
2867 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2870 if (types_list_elements)
2872 struct dwarf2_section_info *section;
2874 /* We can only handle a single .debug_types when we have an
2876 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2879 section = VEC_index (dwarf2_section_info_def,
2880 dwarf2_per_objfile->types, 0);
2882 create_signatured_type_table_from_index (objfile, section, types_list,
2883 types_list_elements);
2886 create_addrmap_from_index (objfile, &local_map);
2888 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2891 dwarf2_per_objfile->index_table = map;
2892 dwarf2_per_objfile->using_index = 1;
2893 dwarf2_per_objfile->quick_file_names_table =
2894 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2899 /* A helper for the "quick" functions which sets the global
2900 dwarf2_per_objfile according to OBJFILE. */
2903 dw2_setup (struct objfile *objfile)
2905 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2906 gdb_assert (dwarf2_per_objfile);
2909 /* die_reader_func for dw2_get_file_names. */
2912 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2913 const gdb_byte *info_ptr,
2914 struct die_info *comp_unit_die,
2918 struct dwarf2_cu *cu = reader->cu;
2919 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2920 struct objfile *objfile = dwarf2_per_objfile->objfile;
2921 struct dwarf2_per_cu_data *lh_cu;
2922 struct line_header *lh;
2923 struct attribute *attr;
2925 const char *name, *comp_dir;
2927 struct quick_file_names *qfn;
2928 unsigned int line_offset;
2930 gdb_assert (! this_cu->is_debug_types);
2932 /* Our callers never want to match partial units -- instead they
2933 will match the enclosing full CU. */
2934 if (comp_unit_die->tag == DW_TAG_partial_unit)
2936 this_cu->v.quick->no_file_data = 1;
2945 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2948 struct quick_file_names find_entry;
2950 line_offset = DW_UNSND (attr);
2952 /* We may have already read in this line header (TU line header sharing).
2953 If we have we're done. */
2954 find_entry.hash.dwo_unit = cu->dwo_unit;
2955 find_entry.hash.line_offset.sect_off = line_offset;
2956 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2957 &find_entry, INSERT);
2960 lh_cu->v.quick->file_names = *slot;
2964 lh = dwarf_decode_line_header (line_offset, cu);
2968 lh_cu->v.quick->no_file_data = 1;
2972 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2973 qfn->hash.dwo_unit = cu->dwo_unit;
2974 qfn->hash.line_offset.sect_off = line_offset;
2975 gdb_assert (slot != NULL);
2978 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2980 qfn->num_file_names = lh->num_file_names;
2981 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2982 lh->num_file_names * sizeof (char *));
2983 for (i = 0; i < lh->num_file_names; ++i)
2984 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2985 qfn->real_names = NULL;
2987 free_line_header (lh);
2989 lh_cu->v.quick->file_names = qfn;
2992 /* A helper for the "quick" functions which attempts to read the line
2993 table for THIS_CU. */
2995 static struct quick_file_names *
2996 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2998 /* This should never be called for TUs. */
2999 gdb_assert (! this_cu->is_debug_types);
3000 /* Nor type unit groups. */
3001 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3003 if (this_cu->v.quick->file_names != NULL)
3004 return this_cu->v.quick->file_names;
3005 /* If we know there is no line data, no point in looking again. */
3006 if (this_cu->v.quick->no_file_data)
3009 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3011 if (this_cu->v.quick->no_file_data)
3013 return this_cu->v.quick->file_names;
3016 /* A helper for the "quick" functions which computes and caches the
3017 real path for a given file name from the line table. */
3020 dw2_get_real_path (struct objfile *objfile,
3021 struct quick_file_names *qfn, int index)
3023 if (qfn->real_names == NULL)
3024 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3025 qfn->num_file_names, sizeof (char *));
3027 if (qfn->real_names[index] == NULL)
3028 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3030 return qfn->real_names[index];
3033 static struct symtab *
3034 dw2_find_last_source_symtab (struct objfile *objfile)
3038 dw2_setup (objfile);
3039 index = dwarf2_per_objfile->n_comp_units - 1;
3040 return dw2_instantiate_symtab (dw2_get_cu (index));
3043 /* Traversal function for dw2_forget_cached_source_info. */
3046 dw2_free_cached_file_names (void **slot, void *info)
3048 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3050 if (file_data->real_names)
3054 for (i = 0; i < file_data->num_file_names; ++i)
3056 xfree ((void*) file_data->real_names[i]);
3057 file_data->real_names[i] = NULL;
3065 dw2_forget_cached_source_info (struct objfile *objfile)
3067 dw2_setup (objfile);
3069 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3070 dw2_free_cached_file_names, NULL);
3073 /* Helper function for dw2_map_symtabs_matching_filename that expands
3074 the symtabs and calls the iterator. */
3077 dw2_map_expand_apply (struct objfile *objfile,
3078 struct dwarf2_per_cu_data *per_cu,
3079 const char *name, const char *real_path,
3080 int (*callback) (struct symtab *, void *),
3083 struct symtab *last_made = objfile->symtabs;
3085 /* Don't visit already-expanded CUs. */
3086 if (per_cu->v.quick->symtab)
3089 /* This may expand more than one symtab, and we want to iterate over
3091 dw2_instantiate_symtab (per_cu);
3093 return iterate_over_some_symtabs (name, real_path, callback, data,
3094 objfile->symtabs, last_made);
3097 /* Implementation of the map_symtabs_matching_filename method. */
3100 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3101 const char *real_path,
3102 int (*callback) (struct symtab *, void *),
3106 const char *name_basename = lbasename (name);
3108 dw2_setup (objfile);
3110 /* The rule is CUs specify all the files, including those used by
3111 any TU, so there's no need to scan TUs here. */
3113 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3116 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3117 struct quick_file_names *file_data;
3119 /* We only need to look at symtabs not already expanded. */
3120 if (per_cu->v.quick->symtab)
3123 file_data = dw2_get_file_names (per_cu);
3124 if (file_data == NULL)
3127 for (j = 0; j < file_data->num_file_names; ++j)
3129 const char *this_name = file_data->file_names[j];
3130 const char *this_real_name;
3132 if (compare_filenames_for_search (this_name, name))
3134 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3140 /* Before we invoke realpath, which can get expensive when many
3141 files are involved, do a quick comparison of the basenames. */
3142 if (! basenames_may_differ
3143 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3146 this_real_name = dw2_get_real_path (objfile, file_data, j);
3147 if (compare_filenames_for_search (this_real_name, name))
3149 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3155 if (real_path != NULL)
3157 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3158 gdb_assert (IS_ABSOLUTE_PATH (name));
3159 if (this_real_name != NULL
3160 && FILENAME_CMP (real_path, this_real_name) == 0)
3162 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3174 /* Struct used to manage iterating over all CUs looking for a symbol. */
3176 struct dw2_symtab_iterator
3178 /* The internalized form of .gdb_index. */
3179 struct mapped_index *index;
3180 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3181 int want_specific_block;
3182 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3183 Unused if !WANT_SPECIFIC_BLOCK. */
3185 /* The kind of symbol we're looking for. */
3187 /* The list of CUs from the index entry of the symbol,
3188 or NULL if not found. */
3190 /* The next element in VEC to look at. */
3192 /* The number of elements in VEC, or zero if there is no match. */
3196 /* Initialize the index symtab iterator ITER.
3197 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3198 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3201 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3202 struct mapped_index *index,
3203 int want_specific_block,
3208 iter->index = index;
3209 iter->want_specific_block = want_specific_block;
3210 iter->block_index = block_index;
3211 iter->domain = domain;
3214 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3215 iter->length = MAYBE_SWAP (*iter->vec);
3223 /* Return the next matching CU or NULL if there are no more. */
3225 static struct dwarf2_per_cu_data *
3226 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3228 for ( ; iter->next < iter->length; ++iter->next)
3230 offset_type cu_index_and_attrs =
3231 MAYBE_SWAP (iter->vec[iter->next + 1]);
3232 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3233 struct dwarf2_per_cu_data *per_cu;
3234 int want_static = iter->block_index != GLOBAL_BLOCK;
3235 /* This value is only valid for index versions >= 7. */
3236 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3237 gdb_index_symbol_kind symbol_kind =
3238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3239 /* Only check the symbol attributes if they're present.
3240 Indices prior to version 7 don't record them,
3241 and indices >= 7 may elide them for certain symbols
3242 (gold does this). */
3244 (iter->index->version >= 7
3245 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3247 /* Don't crash on bad data. */
3248 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3249 + dwarf2_per_objfile->n_type_units))
3251 complaint (&symfile_complaints,
3252 _(".gdb_index entry has bad CU index"
3254 objfile_name (dwarf2_per_objfile->objfile));
3258 per_cu = dw2_get_cu (cu_index);
3260 /* Skip if already read in. */
3261 if (per_cu->v.quick->symtab)
3265 && iter->want_specific_block
3266 && want_static != is_static)
3269 /* Only check the symbol's kind if it has one. */
3272 switch (iter->domain)
3275 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3276 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3277 /* Some types are also in VAR_DOMAIN. */
3278 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3282 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3286 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3301 static struct symtab *
3302 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3303 const char *name, domain_enum domain)
3305 struct symtab *stab_best = NULL;
3306 struct mapped_index *index;
3308 dw2_setup (objfile);
3310 index = dwarf2_per_objfile->index_table;
3312 /* index is NULL if OBJF_READNOW. */
3315 struct dw2_symtab_iterator iter;
3316 struct dwarf2_per_cu_data *per_cu;
3318 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3320 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3322 struct symbol *sym = NULL;
3323 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3325 /* Some caution must be observed with overloaded functions
3326 and methods, since the index will not contain any overload
3327 information (but NAME might contain it). */
3330 struct blockvector *bv = BLOCKVECTOR (stab);
3331 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3333 sym = lookup_block_symbol (block, name, domain);
3336 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3338 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3344 /* Keep looking through other CUs. */
3352 dw2_print_stats (struct objfile *objfile)
3354 int i, total, count;
3356 dw2_setup (objfile);
3357 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3359 for (i = 0; i < total; ++i)
3361 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3363 if (!per_cu->v.quick->symtab)
3366 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3367 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3370 /* This dumps minimal information about the index.
3371 It is called via "mt print objfiles".
3372 One use is to verify .gdb_index has been loaded by the
3373 gdb.dwarf2/gdb-index.exp testcase. */
3376 dw2_dump (struct objfile *objfile)
3378 dw2_setup (objfile);
3379 gdb_assert (dwarf2_per_objfile->using_index);
3380 printf_filtered (".gdb_index:");
3381 if (dwarf2_per_objfile->index_table != NULL)
3383 printf_filtered (" version %d\n",
3384 dwarf2_per_objfile->index_table->version);
3387 printf_filtered (" faked for \"readnow\"\n");
3388 printf_filtered ("\n");
3392 dw2_relocate (struct objfile *objfile,
3393 const struct section_offsets *new_offsets,
3394 const struct section_offsets *delta)
3396 /* There's nothing to relocate here. */
3400 dw2_expand_symtabs_for_function (struct objfile *objfile,
3401 const char *func_name)
3403 struct mapped_index *index;
3405 dw2_setup (objfile);
3407 index = dwarf2_per_objfile->index_table;
3409 /* index is NULL if OBJF_READNOW. */
3412 struct dw2_symtab_iterator iter;
3413 struct dwarf2_per_cu_data *per_cu;
3415 /* Note: It doesn't matter what we pass for block_index here. */
3416 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3419 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3420 dw2_instantiate_symtab (per_cu);
3425 dw2_expand_all_symtabs (struct objfile *objfile)
3429 dw2_setup (objfile);
3431 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3432 + dwarf2_per_objfile->n_type_units); ++i)
3434 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3436 dw2_instantiate_symtab (per_cu);
3441 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3442 const char *fullname)
3446 dw2_setup (objfile);
3448 /* We don't need to consider type units here.
3449 This is only called for examining code, e.g. expand_line_sal.
3450 There can be an order of magnitude (or more) more type units
3451 than comp units, and we avoid them if we can. */
3453 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3457 struct quick_file_names *file_data;
3459 /* We only need to look at symtabs not already expanded. */
3460 if (per_cu->v.quick->symtab)
3463 file_data = dw2_get_file_names (per_cu);
3464 if (file_data == NULL)
3467 for (j = 0; j < file_data->num_file_names; ++j)
3469 const char *this_fullname = file_data->file_names[j];
3471 if (filename_cmp (this_fullname, fullname) == 0)
3473 dw2_instantiate_symtab (per_cu);
3481 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3482 struct objfile *objfile, int global,
3483 int (*callback) (struct block *,
3484 struct symbol *, void *),
3485 void *data, symbol_compare_ftype *match,
3486 symbol_compare_ftype *ordered_compare)
3488 /* Currently unimplemented; used for Ada. The function can be called if the
3489 current language is Ada for a non-Ada objfile using GNU index. As Ada
3490 does not look for non-Ada symbols this function should just return. */
3494 dw2_expand_symtabs_matching
3495 (struct objfile *objfile,
3496 int (*file_matcher) (const char *, void *, int basenames),
3497 int (*name_matcher) (const char *, void *),
3498 enum search_domain kind,
3503 struct mapped_index *index;
3505 dw2_setup (objfile);
3507 /* index_table is NULL if OBJF_READNOW. */
3508 if (!dwarf2_per_objfile->index_table)
3510 index = dwarf2_per_objfile->index_table;
3512 if (file_matcher != NULL)
3514 struct cleanup *cleanup;
3515 htab_t visited_found, visited_not_found;
3517 visited_found = htab_create_alloc (10,
3518 htab_hash_pointer, htab_eq_pointer,
3519 NULL, xcalloc, xfree);
3520 cleanup = make_cleanup_htab_delete (visited_found);
3521 visited_not_found = htab_create_alloc (10,
3522 htab_hash_pointer, htab_eq_pointer,
3523 NULL, xcalloc, xfree);
3524 make_cleanup_htab_delete (visited_not_found);
3526 /* The rule is CUs specify all the files, including those used by
3527 any TU, so there's no need to scan TUs here. */
3529 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3532 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3533 struct quick_file_names *file_data;
3536 per_cu->v.quick->mark = 0;
3538 /* We only need to look at symtabs not already expanded. */
3539 if (per_cu->v.quick->symtab)
3542 file_data = dw2_get_file_names (per_cu);
3543 if (file_data == NULL)
3546 if (htab_find (visited_not_found, file_data) != NULL)
3548 else if (htab_find (visited_found, file_data) != NULL)
3550 per_cu->v.quick->mark = 1;
3554 for (j = 0; j < file_data->num_file_names; ++j)
3556 const char *this_real_name;
3558 if (file_matcher (file_data->file_names[j], data, 0))
3560 per_cu->v.quick->mark = 1;
3564 /* Before we invoke realpath, which can get expensive when many
3565 files are involved, do a quick comparison of the basenames. */
3566 if (!basenames_may_differ
3567 && !file_matcher (lbasename (file_data->file_names[j]),
3571 this_real_name = dw2_get_real_path (objfile, file_data, j);
3572 if (file_matcher (this_real_name, data, 0))
3574 per_cu->v.quick->mark = 1;
3579 slot = htab_find_slot (per_cu->v.quick->mark
3581 : visited_not_found,
3586 do_cleanups (cleanup);
3589 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3591 offset_type idx = 2 * iter;
3593 offset_type *vec, vec_len, vec_idx;
3595 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3598 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3600 if (! (*name_matcher) (name, data))
3603 /* The name was matched, now expand corresponding CUs that were
3605 vec = (offset_type *) (index->constant_pool
3606 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3607 vec_len = MAYBE_SWAP (vec[0]);
3608 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3610 struct dwarf2_per_cu_data *per_cu;
3611 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3612 gdb_index_symbol_kind symbol_kind =
3613 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3614 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3615 /* Only check the symbol attributes if they're present.
3616 Indices prior to version 7 don't record them,
3617 and indices >= 7 may elide them for certain symbols
3618 (gold does this). */
3620 (index->version >= 7
3621 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3623 /* Only check the symbol's kind if it has one. */
3628 case VARIABLES_DOMAIN:
3629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3632 case FUNCTIONS_DOMAIN:
3633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3645 /* Don't crash on bad data. */
3646 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3647 + dwarf2_per_objfile->n_type_units))
3649 complaint (&symfile_complaints,
3650 _(".gdb_index entry has bad CU index"
3651 " [in module %s]"), objfile_name (objfile));
3655 per_cu = dw2_get_cu (cu_index);
3656 if (file_matcher == NULL || per_cu->v.quick->mark)
3657 dw2_instantiate_symtab (per_cu);
3662 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3665 static struct symtab *
3666 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3670 if (BLOCKVECTOR (symtab) != NULL
3671 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3674 if (symtab->includes == NULL)
3677 for (i = 0; symtab->includes[i]; ++i)
3679 struct symtab *s = symtab->includes[i];
3681 s = recursively_find_pc_sect_symtab (s, pc);
3689 static struct symtab *
3690 dw2_find_pc_sect_symtab (struct objfile *objfile,
3691 struct minimal_symbol *msymbol,
3693 struct obj_section *section,
3696 struct dwarf2_per_cu_data *data;
3697 struct symtab *result;
3699 dw2_setup (objfile);
3701 if (!objfile->psymtabs_addrmap)
3704 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3708 if (warn_if_readin && data->v.quick->symtab)
3709 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3710 paddress (get_objfile_arch (objfile), pc));
3712 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3713 gdb_assert (result != NULL);
3718 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3719 void *data, int need_fullname)
3722 struct cleanup *cleanup;
3723 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3724 NULL, xcalloc, xfree);
3726 cleanup = make_cleanup_htab_delete (visited);
3727 dw2_setup (objfile);
3729 /* The rule is CUs specify all the files, including those used by
3730 any TU, so there's no need to scan TUs here.
3731 We can ignore file names coming from already-expanded CUs. */
3733 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3735 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3737 if (per_cu->v.quick->symtab)
3739 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3742 *slot = per_cu->v.quick->file_names;
3746 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3749 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3750 struct quick_file_names *file_data;
3753 /* We only need to look at symtabs not already expanded. */
3754 if (per_cu->v.quick->symtab)
3757 file_data = dw2_get_file_names (per_cu);
3758 if (file_data == NULL)
3761 slot = htab_find_slot (visited, file_data, INSERT);
3764 /* Already visited. */
3769 for (j = 0; j < file_data->num_file_names; ++j)
3771 const char *this_real_name;
3774 this_real_name = dw2_get_real_path (objfile, file_data, j);
3776 this_real_name = NULL;
3777 (*fun) (file_data->file_names[j], this_real_name, data);
3781 do_cleanups (cleanup);
3785 dw2_has_symbols (struct objfile *objfile)
3790 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3793 dw2_find_last_source_symtab,
3794 dw2_forget_cached_source_info,
3795 dw2_map_symtabs_matching_filename,
3800 dw2_expand_symtabs_for_function,
3801 dw2_expand_all_symtabs,
3802 dw2_expand_symtabs_with_fullname,
3803 dw2_map_matching_symbols,
3804 dw2_expand_symtabs_matching,
3805 dw2_find_pc_sect_symtab,
3806 dw2_map_symbol_filenames
3809 /* Initialize for reading DWARF for this objfile. Return 0 if this
3810 file will use psymtabs, or 1 if using the GNU index. */
3813 dwarf2_initialize_objfile (struct objfile *objfile)
3815 /* If we're about to read full symbols, don't bother with the
3816 indices. In this case we also don't care if some other debug
3817 format is making psymtabs, because they are all about to be
3819 if ((objfile->flags & OBJF_READNOW))
3823 dwarf2_per_objfile->using_index = 1;
3824 create_all_comp_units (objfile);
3825 create_all_type_units (objfile);
3826 dwarf2_per_objfile->quick_file_names_table =
3827 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3829 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3830 + dwarf2_per_objfile->n_type_units); ++i)
3832 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3834 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3835 struct dwarf2_per_cu_quick_data);
3838 /* Return 1 so that gdb sees the "quick" functions. However,
3839 these functions will be no-ops because we will have expanded
3844 if (dwarf2_read_index (objfile))
3852 /* Build a partial symbol table. */
3855 dwarf2_build_psymtabs (struct objfile *objfile)
3857 volatile struct gdb_exception except;
3859 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3861 init_psymbol_list (objfile, 1024);
3864 TRY_CATCH (except, RETURN_MASK_ERROR)
3866 /* This isn't really ideal: all the data we allocate on the
3867 objfile's obstack is still uselessly kept around. However,
3868 freeing it seems unsafe. */
3869 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3871 dwarf2_build_psymtabs_hard (objfile);
3872 discard_cleanups (cleanups);
3874 if (except.reason < 0)
3875 exception_print (gdb_stderr, except);
3878 /* Return the total length of the CU described by HEADER. */
3881 get_cu_length (const struct comp_unit_head *header)
3883 return header->initial_length_size + header->length;
3886 /* Return TRUE if OFFSET is within CU_HEADER. */
3889 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3891 sect_offset bottom = { cu_header->offset.sect_off };
3892 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3894 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3897 /* Find the base address of the compilation unit for range lists and
3898 location lists. It will normally be specified by DW_AT_low_pc.
3899 In DWARF-3 draft 4, the base address could be overridden by
3900 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3901 compilation units with discontinuous ranges. */
3904 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3906 struct attribute *attr;
3909 cu->base_address = 0;
3911 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3914 cu->base_address = DW_ADDR (attr);
3919 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3922 cu->base_address = DW_ADDR (attr);
3928 /* Read in the comp unit header information from the debug_info at info_ptr.
3929 NOTE: This leaves members offset, first_die_offset to be filled in
3932 static const gdb_byte *
3933 read_comp_unit_head (struct comp_unit_head *cu_header,
3934 const gdb_byte *info_ptr, bfd *abfd)
3937 unsigned int bytes_read;
3939 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3940 cu_header->initial_length_size = bytes_read;
3941 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3942 info_ptr += bytes_read;
3943 cu_header->version = read_2_bytes (abfd, info_ptr);
3945 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3947 info_ptr += bytes_read;
3948 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3950 signed_addr = bfd_get_sign_extend_vma (abfd);
3951 if (signed_addr < 0)
3952 internal_error (__FILE__, __LINE__,
3953 _("read_comp_unit_head: dwarf from non elf file"));
3954 cu_header->signed_addr_p = signed_addr;
3959 /* Helper function that returns the proper abbrev section for
3962 static struct dwarf2_section_info *
3963 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3965 struct dwarf2_section_info *abbrev;
3967 if (this_cu->is_dwz)
3968 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3970 abbrev = &dwarf2_per_objfile->abbrev;
3975 /* Subroutine of read_and_check_comp_unit_head and
3976 read_and_check_type_unit_head to simplify them.
3977 Perform various error checking on the header. */
3980 error_check_comp_unit_head (struct comp_unit_head *header,
3981 struct dwarf2_section_info *section,
3982 struct dwarf2_section_info *abbrev_section)
3984 bfd *abfd = section->asection->owner;
3985 const char *filename = bfd_get_filename (abfd);
3987 if (header->version != 2 && header->version != 3 && header->version != 4)
3988 error (_("Dwarf Error: wrong version in compilation unit header "
3989 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3992 if (header->abbrev_offset.sect_off
3993 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3994 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3995 "(offset 0x%lx + 6) [in module %s]"),
3996 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3999 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4000 avoid potential 32-bit overflow. */
4001 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4003 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4004 "(offset 0x%lx + 0) [in module %s]"),
4005 (long) header->length, (long) header->offset.sect_off,
4009 /* Read in a CU/TU header and perform some basic error checking.
4010 The contents of the header are stored in HEADER.
4011 The result is a pointer to the start of the first DIE. */
4013 static const gdb_byte *
4014 read_and_check_comp_unit_head (struct comp_unit_head *header,
4015 struct dwarf2_section_info *section,
4016 struct dwarf2_section_info *abbrev_section,
4017 const gdb_byte *info_ptr,
4018 int is_debug_types_section)
4020 const gdb_byte *beg_of_comp_unit = info_ptr;
4021 bfd *abfd = section->asection->owner;
4023 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4025 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4027 /* If we're reading a type unit, skip over the signature and
4028 type_offset fields. */
4029 if (is_debug_types_section)
4030 info_ptr += 8 /*signature*/ + header->offset_size;
4032 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4034 error_check_comp_unit_head (header, section, abbrev_section);
4039 /* Read in the types comp unit header information from .debug_types entry at
4040 types_ptr. The result is a pointer to one past the end of the header. */
4042 static const gdb_byte *
4043 read_and_check_type_unit_head (struct comp_unit_head *header,
4044 struct dwarf2_section_info *section,
4045 struct dwarf2_section_info *abbrev_section,
4046 const gdb_byte *info_ptr,
4047 ULONGEST *signature,
4048 cu_offset *type_offset_in_tu)
4050 const gdb_byte *beg_of_comp_unit = info_ptr;
4051 bfd *abfd = section->asection->owner;
4053 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4055 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4057 /* If we're reading a type unit, skip over the signature and
4058 type_offset fields. */
4059 if (signature != NULL)
4060 *signature = read_8_bytes (abfd, info_ptr);
4062 if (type_offset_in_tu != NULL)
4063 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4064 header->offset_size);
4065 info_ptr += header->offset_size;
4067 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4069 error_check_comp_unit_head (header, section, abbrev_section);
4074 /* Fetch the abbreviation table offset from a comp or type unit header. */
4077 read_abbrev_offset (struct dwarf2_section_info *section,
4080 bfd *abfd = section->asection->owner;
4081 const gdb_byte *info_ptr;
4082 unsigned int length, initial_length_size, offset_size;
4083 sect_offset abbrev_offset;
4085 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4086 info_ptr = section->buffer + offset.sect_off;
4087 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4088 offset_size = initial_length_size == 4 ? 4 : 8;
4089 info_ptr += initial_length_size + 2 /*version*/;
4090 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4091 return abbrev_offset;
4094 /* Allocate a new partial symtab for file named NAME and mark this new
4095 partial symtab as being an include of PST. */
4098 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4099 struct objfile *objfile)
4101 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4103 if (!IS_ABSOLUTE_PATH (subpst->filename))
4105 /* It shares objfile->objfile_obstack. */
4106 subpst->dirname = pst->dirname;
4109 subpst->section_offsets = pst->section_offsets;
4110 subpst->textlow = 0;
4111 subpst->texthigh = 0;
4113 subpst->dependencies = (struct partial_symtab **)
4114 obstack_alloc (&objfile->objfile_obstack,
4115 sizeof (struct partial_symtab *));
4116 subpst->dependencies[0] = pst;
4117 subpst->number_of_dependencies = 1;
4119 subpst->globals_offset = 0;
4120 subpst->n_global_syms = 0;
4121 subpst->statics_offset = 0;
4122 subpst->n_static_syms = 0;
4123 subpst->symtab = NULL;
4124 subpst->read_symtab = pst->read_symtab;
4127 /* No private part is necessary for include psymtabs. This property
4128 can be used to differentiate between such include psymtabs and
4129 the regular ones. */
4130 subpst->read_symtab_private = NULL;
4133 /* Read the Line Number Program data and extract the list of files
4134 included by the source file represented by PST. Build an include
4135 partial symtab for each of these included files. */
4138 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4139 struct die_info *die,
4140 struct partial_symtab *pst)
4142 struct line_header *lh = NULL;
4143 struct attribute *attr;
4145 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4147 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4149 return; /* No linetable, so no includes. */
4151 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4152 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4154 free_line_header (lh);
4158 hash_signatured_type (const void *item)
4160 const struct signatured_type *sig_type = item;
4162 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4163 return sig_type->signature;
4167 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4169 const struct signatured_type *lhs = item_lhs;
4170 const struct signatured_type *rhs = item_rhs;
4172 return lhs->signature == rhs->signature;
4175 /* Allocate a hash table for signatured types. */
4178 allocate_signatured_type_table (struct objfile *objfile)
4180 return htab_create_alloc_ex (41,
4181 hash_signatured_type,
4184 &objfile->objfile_obstack,
4185 hashtab_obstack_allocate,
4186 dummy_obstack_deallocate);
4189 /* A helper function to add a signatured type CU to a table. */
4192 add_signatured_type_cu_to_table (void **slot, void *datum)
4194 struct signatured_type *sigt = *slot;
4195 struct signatured_type ***datap = datum;
4203 /* Create the hash table of all entries in the .debug_types
4204 (or .debug_types.dwo) section(s).
4205 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4206 otherwise it is NULL.
4208 The result is a pointer to the hash table or NULL if there are no types.
4210 Note: This function processes DWO files only, not DWP files. */
4213 create_debug_types_hash_table (struct dwo_file *dwo_file,
4214 VEC (dwarf2_section_info_def) *types)
4216 struct objfile *objfile = dwarf2_per_objfile->objfile;
4217 htab_t types_htab = NULL;
4219 struct dwarf2_section_info *section;
4220 struct dwarf2_section_info *abbrev_section;
4222 if (VEC_empty (dwarf2_section_info_def, types))
4225 abbrev_section = (dwo_file != NULL
4226 ? &dwo_file->sections.abbrev
4227 : &dwarf2_per_objfile->abbrev);
4229 if (dwarf2_read_debug)
4230 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4231 dwo_file ? ".dwo" : "",
4232 bfd_get_filename (abbrev_section->asection->owner));
4235 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4239 const gdb_byte *info_ptr, *end_ptr;
4240 struct dwarf2_section_info *abbrev_section;
4242 dwarf2_read_section (objfile, section);
4243 info_ptr = section->buffer;
4245 if (info_ptr == NULL)
4248 /* We can't set abfd until now because the section may be empty or
4249 not present, in which case section->asection will be NULL. */
4250 abfd = section->asection->owner;
4253 abbrev_section = &dwo_file->sections.abbrev;
4255 abbrev_section = &dwarf2_per_objfile->abbrev;
4257 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4258 because we don't need to read any dies: the signature is in the
4261 end_ptr = info_ptr + section->size;
4262 while (info_ptr < end_ptr)
4265 cu_offset type_offset_in_tu;
4267 struct signatured_type *sig_type;
4268 struct dwo_unit *dwo_tu;
4270 const gdb_byte *ptr = info_ptr;
4271 struct comp_unit_head header;
4272 unsigned int length;
4274 offset.sect_off = ptr - section->buffer;
4276 /* We need to read the type's signature in order to build the hash
4277 table, but we don't need anything else just yet. */
4279 ptr = read_and_check_type_unit_head (&header, section,
4280 abbrev_section, ptr,
4281 &signature, &type_offset_in_tu);
4283 length = get_cu_length (&header);
4285 /* Skip dummy type units. */
4286 if (ptr >= info_ptr + length
4287 || peek_abbrev_code (abfd, ptr) == 0)
4293 if (types_htab == NULL)
4296 types_htab = allocate_dwo_unit_table (objfile);
4298 types_htab = allocate_signatured_type_table (objfile);
4304 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4306 dwo_tu->dwo_file = dwo_file;
4307 dwo_tu->signature = signature;
4308 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4309 dwo_tu->section = section;
4310 dwo_tu->offset = offset;
4311 dwo_tu->length = length;
4315 /* N.B.: type_offset is not usable if this type uses a DWO file.
4316 The real type_offset is in the DWO file. */
4318 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4319 struct signatured_type);
4320 sig_type->signature = signature;
4321 sig_type->type_offset_in_tu = type_offset_in_tu;
4322 sig_type->per_cu.objfile = objfile;
4323 sig_type->per_cu.is_debug_types = 1;
4324 sig_type->per_cu.section = section;
4325 sig_type->per_cu.offset = offset;
4326 sig_type->per_cu.length = length;
4329 slot = htab_find_slot (types_htab,
4330 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4332 gdb_assert (slot != NULL);
4335 sect_offset dup_offset;
4339 const struct dwo_unit *dup_tu = *slot;
4341 dup_offset = dup_tu->offset;
4345 const struct signatured_type *dup_tu = *slot;
4347 dup_offset = dup_tu->per_cu.offset;
4350 complaint (&symfile_complaints,
4351 _("debug type entry at offset 0x%x is duplicate to"
4352 " the entry at offset 0x%x, signature %s"),
4353 offset.sect_off, dup_offset.sect_off,
4354 hex_string (signature));
4356 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4358 if (dwarf2_read_debug)
4359 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4361 hex_string (signature));
4370 /* Create the hash table of all entries in the .debug_types section,
4371 and initialize all_type_units.
4372 The result is zero if there is an error (e.g. missing .debug_types section),
4373 otherwise non-zero. */
4376 create_all_type_units (struct objfile *objfile)
4379 struct signatured_type **iter;
4381 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4382 if (types_htab == NULL)
4384 dwarf2_per_objfile->signatured_types = NULL;
4388 dwarf2_per_objfile->signatured_types = types_htab;
4390 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4391 dwarf2_per_objfile->all_type_units
4392 = xmalloc (dwarf2_per_objfile->n_type_units
4393 * sizeof (struct signatured_type *));
4394 iter = &dwarf2_per_objfile->all_type_units[0];
4395 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4396 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4397 == dwarf2_per_objfile->n_type_units);
4402 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4403 Fill in SIG_ENTRY with DWO_ENTRY. */
4406 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4407 struct signatured_type *sig_entry,
4408 struct dwo_unit *dwo_entry)
4410 /* Make sure we're not clobbering something we don't expect to. */
4411 gdb_assert (! sig_entry->per_cu.queued);
4412 gdb_assert (sig_entry->per_cu.cu == NULL);
4413 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4414 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4415 gdb_assert (sig_entry->signature == dwo_entry->signature);
4416 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4417 gdb_assert (sig_entry->type_unit_group == NULL);
4418 gdb_assert (sig_entry->dwo_unit == NULL);
4420 sig_entry->per_cu.section = dwo_entry->section;
4421 sig_entry->per_cu.offset = dwo_entry->offset;
4422 sig_entry->per_cu.length = dwo_entry->length;
4423 sig_entry->per_cu.reading_dwo_directly = 1;
4424 sig_entry->per_cu.objfile = objfile;
4425 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4426 sig_entry->dwo_unit = dwo_entry;
4429 /* Subroutine of lookup_signatured_type.
4430 If we haven't read the TU yet, create the signatured_type data structure
4431 for a TU to be read in directly from a DWO file, bypassing the stub.
4432 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4433 using .gdb_index, then when reading a CU we want to stay in the DWO file
4434 containing that CU. Otherwise we could end up reading several other DWO
4435 files (due to comdat folding) to process the transitive closure of all the
4436 mentioned TUs, and that can be slow. The current DWO file will have every
4437 type signature that it needs.
4438 We only do this for .gdb_index because in the psymtab case we already have
4439 to read all the DWOs to build the type unit groups. */
4441 static struct signatured_type *
4442 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4444 struct objfile *objfile = dwarf2_per_objfile->objfile;
4445 struct dwo_file *dwo_file;
4446 struct dwo_unit find_dwo_entry, *dwo_entry;
4447 struct signatured_type find_sig_entry, *sig_entry;
4449 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4451 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4452 dwo_unit of the TU itself. */
4453 dwo_file = cu->dwo_unit->dwo_file;
4455 /* We only ever need to read in one copy of a signatured type.
4456 Just use the global signatured_types array. If this is the first time
4457 we're reading this type, replace the recorded data from .gdb_index with
4460 if (dwarf2_per_objfile->signatured_types == NULL)
4462 find_sig_entry.signature = sig;
4463 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4464 if (sig_entry == NULL)
4467 /* We can get here with the TU already read, *or* in the process of being
4468 read. Don't reassign it if that's the case. Also note that if the TU is
4469 already being read, it may not have come from a DWO, the program may be
4470 a mix of Fission-compiled code and non-Fission-compiled code. */
4471 /* Have we already tried to read this TU? */
4472 if (sig_entry->per_cu.tu_read)
4475 /* Ok, this is the first time we're reading this TU. */
4476 if (dwo_file->tus == NULL)
4478 find_dwo_entry.signature = sig;
4479 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4480 if (dwo_entry == NULL)
4483 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4484 sig_entry->per_cu.tu_read = 1;
4488 /* Subroutine of lookup_dwp_signatured_type.
4489 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4491 static struct signatured_type *
4492 add_type_unit (ULONGEST sig)
4494 struct objfile *objfile = dwarf2_per_objfile->objfile;
4495 int n_type_units = dwarf2_per_objfile->n_type_units;
4496 struct signatured_type *sig_type;
4500 dwarf2_per_objfile->all_type_units =
4501 xrealloc (dwarf2_per_objfile->all_type_units,
4502 n_type_units * sizeof (struct signatured_type *));
4503 dwarf2_per_objfile->n_type_units = n_type_units;
4504 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4505 struct signatured_type);
4506 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4507 sig_type->signature = sig;
4508 sig_type->per_cu.is_debug_types = 1;
4509 sig_type->per_cu.v.quick =
4510 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4511 struct dwarf2_per_cu_quick_data);
4512 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4514 gdb_assert (*slot == NULL);
4516 /* The rest of sig_type must be filled in by the caller. */
4520 /* Subroutine of lookup_signatured_type.
4521 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4522 then try the DWP file.
4523 Normally this "can't happen", but if there's a bug in signature
4524 generation and/or the DWP file is built incorrectly, it can happen.
4525 Using the type directly from the DWP file means we don't have the stub
4526 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4527 not critical. [Eventually the stub may go away for type units anyway.] */
4529 static struct signatured_type *
4530 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4532 struct objfile *objfile = dwarf2_per_objfile->objfile;
4533 struct dwp_file *dwp_file = get_dwp_file ();
4534 struct dwo_unit *dwo_entry;
4535 struct signatured_type find_sig_entry, *sig_entry;
4537 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4538 gdb_assert (dwp_file != NULL);
4540 if (dwarf2_per_objfile->signatured_types != NULL)
4542 find_sig_entry.signature = sig;
4543 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4545 if (sig_entry != NULL)
4549 /* This is the "shouldn't happen" case.
4550 Try the DWP file and hope for the best. */
4551 if (dwp_file->tus == NULL)
4553 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4554 sig, 1 /* is_debug_types */);
4555 if (dwo_entry == NULL)
4558 sig_entry = add_type_unit (sig);
4559 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4561 /* The caller will signal a complaint if we return NULL.
4562 Here we don't return NULL but we still want to complain. */
4563 complaint (&symfile_complaints,
4564 _("Bad type signature %s referenced by %s at 0x%x,"
4565 " coping by using copy in DWP [in module %s]"),
4567 cu->per_cu->is_debug_types ? "TU" : "CU",
4568 cu->per_cu->offset.sect_off,
4569 objfile_name (objfile));
4574 /* Lookup a signature based type for DW_FORM_ref_sig8.
4575 Returns NULL if signature SIG is not present in the table.
4576 It is up to the caller to complain about this. */
4578 static struct signatured_type *
4579 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4582 && dwarf2_per_objfile->using_index)
4584 /* We're in a DWO/DWP file, and we're using .gdb_index.
4585 These cases require special processing. */
4586 if (get_dwp_file () == NULL)
4587 return lookup_dwo_signatured_type (cu, sig);
4589 return lookup_dwp_signatured_type (cu, sig);
4593 struct signatured_type find_entry, *entry;
4595 if (dwarf2_per_objfile->signatured_types == NULL)
4597 find_entry.signature = sig;
4598 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4603 /* Low level DIE reading support. */
4605 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4608 init_cu_die_reader (struct die_reader_specs *reader,
4609 struct dwarf2_cu *cu,
4610 struct dwarf2_section_info *section,
4611 struct dwo_file *dwo_file)
4613 gdb_assert (section->readin && section->buffer != NULL);
4614 reader->abfd = section->asection->owner;
4616 reader->dwo_file = dwo_file;
4617 reader->die_section = section;
4618 reader->buffer = section->buffer;
4619 reader->buffer_end = section->buffer + section->size;
4620 reader->comp_dir = NULL;
4623 /* Subroutine of init_cutu_and_read_dies to simplify it.
4624 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4625 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4628 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4629 from it to the DIE in the DWO. If NULL we are skipping the stub.
4630 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4631 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4632 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4633 COMP_DIR must be non-NULL.
4634 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4635 are filled in with the info of the DIE from the DWO file.
4636 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4637 provided an abbrev table to use.
4638 The result is non-zero if a valid (non-dummy) DIE was found. */
4641 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4642 struct dwo_unit *dwo_unit,
4643 int abbrev_table_provided,
4644 struct die_info *stub_comp_unit_die,
4645 const char *stub_comp_dir,
4646 struct die_reader_specs *result_reader,
4647 const gdb_byte **result_info_ptr,
4648 struct die_info **result_comp_unit_die,
4649 int *result_has_children)
4651 struct objfile *objfile = dwarf2_per_objfile->objfile;
4652 struct dwarf2_cu *cu = this_cu->cu;
4653 struct dwarf2_section_info *section;
4655 const gdb_byte *begin_info_ptr, *info_ptr;
4656 const char *comp_dir_string;
4657 ULONGEST signature; /* Or dwo_id. */
4658 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4659 int i,num_extra_attrs;
4660 struct dwarf2_section_info *dwo_abbrev_section;
4661 struct attribute *attr;
4662 struct attribute comp_dir_attr;
4663 struct die_info *comp_unit_die;
4665 /* Both can't be provided. */
4666 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4668 /* These attributes aren't processed until later:
4669 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4670 However, the attribute is found in the stub which we won't have later.
4671 In order to not impose this complication on the rest of the code,
4672 we read them here and copy them to the DWO CU/TU die. */
4680 if (stub_comp_unit_die != NULL)
4682 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4684 if (! this_cu->is_debug_types)
4685 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4686 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4687 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4688 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4689 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4691 /* There should be a DW_AT_addr_base attribute here (if needed).
4692 We need the value before we can process DW_FORM_GNU_addr_index. */
4694 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4696 cu->addr_base = DW_UNSND (attr);
4698 /* There should be a DW_AT_ranges_base attribute here (if needed).
4699 We need the value before we can process DW_AT_ranges. */
4700 cu->ranges_base = 0;
4701 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4703 cu->ranges_base = DW_UNSND (attr);
4705 else if (stub_comp_dir != NULL)
4707 /* Reconstruct the comp_dir attribute to simplify the code below. */
4708 comp_dir = (struct attribute *)
4709 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4710 comp_dir->name = DW_AT_comp_dir;
4711 comp_dir->form = DW_FORM_string;
4712 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4713 DW_STRING (comp_dir) = stub_comp_dir;
4716 /* Set up for reading the DWO CU/TU. */
4717 cu->dwo_unit = dwo_unit;
4718 section = dwo_unit->section;
4719 dwarf2_read_section (objfile, section);
4720 abfd = section->asection->owner;
4721 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4722 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4723 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4725 if (this_cu->is_debug_types)
4727 ULONGEST header_signature;
4728 cu_offset type_offset_in_tu;
4729 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4731 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4735 &type_offset_in_tu);
4736 /* This is not an assert because it can be caused by bad debug info. */
4737 if (sig_type->signature != header_signature)
4739 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4740 " TU at offset 0x%x [in module %s]"),
4741 hex_string (sig_type->signature),
4742 hex_string (header_signature),
4743 dwo_unit->offset.sect_off,
4744 bfd_get_filename (abfd));
4746 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4747 /* For DWOs coming from DWP files, we don't know the CU length
4748 nor the type's offset in the TU until now. */
4749 dwo_unit->length = get_cu_length (&cu->header);
4750 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4752 /* Establish the type offset that can be used to lookup the type.
4753 For DWO files, we don't know it until now. */
4754 sig_type->type_offset_in_section.sect_off =
4755 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4759 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4762 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4763 /* For DWOs coming from DWP files, we don't know the CU length
4765 dwo_unit->length = get_cu_length (&cu->header);
4768 /* Replace the CU's original abbrev table with the DWO's.
4769 Reminder: We can't read the abbrev table until we've read the header. */
4770 if (abbrev_table_provided)
4772 /* Don't free the provided abbrev table, the caller of
4773 init_cutu_and_read_dies owns it. */
4774 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4775 /* Ensure the DWO abbrev table gets freed. */
4776 make_cleanup (dwarf2_free_abbrev_table, cu);
4780 dwarf2_free_abbrev_table (cu);
4781 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4782 /* Leave any existing abbrev table cleanup as is. */
4785 /* Read in the die, but leave space to copy over the attributes
4786 from the stub. This has the benefit of simplifying the rest of
4787 the code - all the work to maintain the illusion of a single
4788 DW_TAG_{compile,type}_unit DIE is done here. */
4789 num_extra_attrs = ((stmt_list != NULL)
4793 + (comp_dir != NULL));
4794 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4795 result_has_children, num_extra_attrs);
4797 /* Copy over the attributes from the stub to the DIE we just read in. */
4798 comp_unit_die = *result_comp_unit_die;
4799 i = comp_unit_die->num_attrs;
4800 if (stmt_list != NULL)
4801 comp_unit_die->attrs[i++] = *stmt_list;
4803 comp_unit_die->attrs[i++] = *low_pc;
4804 if (high_pc != NULL)
4805 comp_unit_die->attrs[i++] = *high_pc;
4807 comp_unit_die->attrs[i++] = *ranges;
4808 if (comp_dir != NULL)
4809 comp_unit_die->attrs[i++] = *comp_dir;
4810 comp_unit_die->num_attrs += num_extra_attrs;
4812 if (dwarf2_die_debug)
4814 fprintf_unfiltered (gdb_stdlog,
4815 "Read die from %s@0x%x of %s:\n",
4816 bfd_section_name (abfd, section->asection),
4817 (unsigned) (begin_info_ptr - section->buffer),
4818 bfd_get_filename (abfd));
4819 dump_die (comp_unit_die, dwarf2_die_debug);
4822 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4823 TUs by skipping the stub and going directly to the entry in the DWO file.
4824 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4825 to get it via circuitous means. Blech. */
4826 if (comp_dir != NULL)
4827 result_reader->comp_dir = DW_STRING (comp_dir);
4829 /* Skip dummy compilation units. */
4830 if (info_ptr >= begin_info_ptr + dwo_unit->length
4831 || peek_abbrev_code (abfd, info_ptr) == 0)
4834 *result_info_ptr = info_ptr;
4838 /* Subroutine of init_cutu_and_read_dies to simplify it.
4839 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4840 Returns NULL if the specified DWO unit cannot be found. */
4842 static struct dwo_unit *
4843 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4844 struct die_info *comp_unit_die)
4846 struct dwarf2_cu *cu = this_cu->cu;
4847 struct attribute *attr;
4849 struct dwo_unit *dwo_unit;
4850 const char *comp_dir, *dwo_name;
4852 gdb_assert (cu != NULL);
4854 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4855 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4856 gdb_assert (attr != NULL);
4857 dwo_name = DW_STRING (attr);
4859 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4861 comp_dir = DW_STRING (attr);
4863 if (this_cu->is_debug_types)
4865 struct signatured_type *sig_type;
4867 /* Since this_cu is the first member of struct signatured_type,
4868 we can go from a pointer to one to a pointer to the other. */
4869 sig_type = (struct signatured_type *) this_cu;
4870 signature = sig_type->signature;
4871 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4875 struct attribute *attr;
4877 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4879 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4881 dwo_name, objfile_name (this_cu->objfile));
4882 signature = DW_UNSND (attr);
4883 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4890 /* Subroutine of init_cutu_and_read_dies to simplify it.
4891 Read a TU directly from a DWO file, bypassing the stub. */
4894 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4895 die_reader_func_ftype *die_reader_func,
4898 struct dwarf2_cu *cu;
4899 struct signatured_type *sig_type;
4900 struct cleanup *cleanups, *free_cu_cleanup;
4901 struct die_reader_specs reader;
4902 const gdb_byte *info_ptr;
4903 struct die_info *comp_unit_die;
4906 /* Verify we can do the following downcast, and that we have the
4908 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4909 sig_type = (struct signatured_type *) this_cu;
4910 gdb_assert (sig_type->dwo_unit != NULL);
4912 cleanups = make_cleanup (null_cleanup, NULL);
4914 gdb_assert (this_cu->cu == NULL);
4915 cu = xmalloc (sizeof (*cu));
4916 init_one_comp_unit (cu, this_cu);
4917 /* If an error occurs while loading, release our storage. */
4918 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4920 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4921 0 /* abbrev_table_provided */,
4922 NULL /* stub_comp_unit_die */,
4923 sig_type->dwo_unit->dwo_file->comp_dir,
4925 &comp_unit_die, &has_children) == 0)
4928 do_cleanups (cleanups);
4932 /* All the "real" work is done here. */
4933 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4935 /* This duplicates some code in init_cutu_and_read_dies,
4936 but the alternative is making the latter more complex.
4937 This function is only for the special case of using DWO files directly:
4938 no point in overly complicating the general case just to handle this. */
4941 /* We've successfully allocated this compilation unit. Let our
4942 caller clean it up when finished with it. */
4943 discard_cleanups (free_cu_cleanup);
4945 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4946 So we have to manually free the abbrev table. */
4947 dwarf2_free_abbrev_table (cu);
4949 /* Link this CU into read_in_chain. */
4950 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4951 dwarf2_per_objfile->read_in_chain = this_cu;
4954 do_cleanups (free_cu_cleanup);
4956 do_cleanups (cleanups);
4959 /* Initialize a CU (or TU) and read its DIEs.
4960 If the CU defers to a DWO file, read the DWO file as well.
4962 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4963 Otherwise the table specified in the comp unit header is read in and used.
4964 This is an optimization for when we already have the abbrev table.
4966 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4967 Otherwise, a new CU is allocated with xmalloc.
4969 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4970 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4972 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4973 linker) then DIE_READER_FUNC will not get called. */
4976 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4977 struct abbrev_table *abbrev_table,
4978 int use_existing_cu, int keep,
4979 die_reader_func_ftype *die_reader_func,
4982 struct objfile *objfile = dwarf2_per_objfile->objfile;
4983 struct dwarf2_section_info *section = this_cu->section;
4984 bfd *abfd = section->asection->owner;
4985 struct dwarf2_cu *cu;
4986 const gdb_byte *begin_info_ptr, *info_ptr;
4987 struct die_reader_specs reader;
4988 struct die_info *comp_unit_die;
4990 struct attribute *attr;
4991 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4992 struct signatured_type *sig_type = NULL;
4993 struct dwarf2_section_info *abbrev_section;
4994 /* Non-zero if CU currently points to a DWO file and we need to
4995 reread it. When this happens we need to reread the skeleton die
4996 before we can reread the DWO file (this only applies to CUs, not TUs). */
4997 int rereading_dwo_cu = 0;
4999 if (dwarf2_die_debug)
5000 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5001 this_cu->is_debug_types ? "type" : "comp",
5002 this_cu->offset.sect_off);
5004 if (use_existing_cu)
5007 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5008 file (instead of going through the stub), short-circuit all of this. */
5009 if (this_cu->reading_dwo_directly)
5011 /* Narrow down the scope of possibilities to have to understand. */
5012 gdb_assert (this_cu->is_debug_types);
5013 gdb_assert (abbrev_table == NULL);
5014 gdb_assert (!use_existing_cu);
5015 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5019 cleanups = make_cleanup (null_cleanup, NULL);
5021 /* This is cheap if the section is already read in. */
5022 dwarf2_read_section (objfile, section);
5024 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5026 abbrev_section = get_abbrev_section_for_cu (this_cu);
5028 if (use_existing_cu && this_cu->cu != NULL)
5032 /* If this CU is from a DWO file we need to start over, we need to
5033 refetch the attributes from the skeleton CU.
5034 This could be optimized by retrieving those attributes from when we
5035 were here the first time: the previous comp_unit_die was stored in
5036 comp_unit_obstack. But there's no data yet that we need this
5038 if (cu->dwo_unit != NULL)
5039 rereading_dwo_cu = 1;
5043 /* If !use_existing_cu, this_cu->cu must be NULL. */
5044 gdb_assert (this_cu->cu == NULL);
5046 cu = xmalloc (sizeof (*cu));
5047 init_one_comp_unit (cu, this_cu);
5049 /* If an error occurs while loading, release our storage. */
5050 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5053 /* Get the header. */
5054 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5056 /* We already have the header, there's no need to read it in again. */
5057 info_ptr += cu->header.first_die_offset.cu_off;
5061 if (this_cu->is_debug_types)
5064 cu_offset type_offset_in_tu;
5066 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5067 abbrev_section, info_ptr,
5069 &type_offset_in_tu);
5071 /* Since per_cu is the first member of struct signatured_type,
5072 we can go from a pointer to one to a pointer to the other. */
5073 sig_type = (struct signatured_type *) this_cu;
5074 gdb_assert (sig_type->signature == signature);
5075 gdb_assert (sig_type->type_offset_in_tu.cu_off
5076 == type_offset_in_tu.cu_off);
5077 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5079 /* LENGTH has not been set yet for type units if we're
5080 using .gdb_index. */
5081 this_cu->length = get_cu_length (&cu->header);
5083 /* Establish the type offset that can be used to lookup the type. */
5084 sig_type->type_offset_in_section.sect_off =
5085 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5089 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5093 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5094 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5098 /* Skip dummy compilation units. */
5099 if (info_ptr >= begin_info_ptr + this_cu->length
5100 || peek_abbrev_code (abfd, info_ptr) == 0)
5102 do_cleanups (cleanups);
5106 /* If we don't have them yet, read the abbrevs for this compilation unit.
5107 And if we need to read them now, make sure they're freed when we're
5108 done. Note that it's important that if the CU had an abbrev table
5109 on entry we don't free it when we're done: Somewhere up the call stack
5110 it may be in use. */
5111 if (abbrev_table != NULL)
5113 gdb_assert (cu->abbrev_table == NULL);
5114 gdb_assert (cu->header.abbrev_offset.sect_off
5115 == abbrev_table->offset.sect_off);
5116 cu->abbrev_table = abbrev_table;
5118 else if (cu->abbrev_table == NULL)
5120 dwarf2_read_abbrevs (cu, abbrev_section);
5121 make_cleanup (dwarf2_free_abbrev_table, cu);
5123 else if (rereading_dwo_cu)
5125 dwarf2_free_abbrev_table (cu);
5126 dwarf2_read_abbrevs (cu, abbrev_section);
5129 /* Read the top level CU/TU die. */
5130 init_cu_die_reader (&reader, cu, section, NULL);
5131 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5133 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5135 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5136 DWO CU, that this test will fail (the attribute will not be present). */
5137 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5140 struct dwo_unit *dwo_unit;
5141 struct die_info *dwo_comp_unit_die;
5145 complaint (&symfile_complaints,
5146 _("compilation unit with DW_AT_GNU_dwo_name"
5147 " has children (offset 0x%x) [in module %s]"),
5148 this_cu->offset.sect_off, bfd_get_filename (abfd));
5150 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5151 if (dwo_unit != NULL)
5153 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5154 abbrev_table != NULL,
5155 comp_unit_die, NULL,
5157 &dwo_comp_unit_die, &has_children) == 0)
5160 do_cleanups (cleanups);
5163 comp_unit_die = dwo_comp_unit_die;
5167 /* Yikes, we couldn't find the rest of the DIE, we only have
5168 the stub. A complaint has already been logged. There's
5169 not much more we can do except pass on the stub DIE to
5170 die_reader_func. We don't want to throw an error on bad
5175 /* All of the above is setup for this call. Yikes. */
5176 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5178 /* Done, clean up. */
5179 if (free_cu_cleanup != NULL)
5183 /* We've successfully allocated this compilation unit. Let our
5184 caller clean it up when finished with it. */
5185 discard_cleanups (free_cu_cleanup);
5187 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5188 So we have to manually free the abbrev table. */
5189 dwarf2_free_abbrev_table (cu);
5191 /* Link this CU into read_in_chain. */
5192 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5193 dwarf2_per_objfile->read_in_chain = this_cu;
5196 do_cleanups (free_cu_cleanup);
5199 do_cleanups (cleanups);
5202 /* Read CU/TU THIS_CU in section SECTION,
5203 but do not follow DW_AT_GNU_dwo_name if present.
5204 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5205 to have already done the lookup to find the DWO/DWP file).
5207 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5208 THIS_CU->is_debug_types, but nothing else.
5210 We fill in THIS_CU->length.
5212 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5213 linker) then DIE_READER_FUNC will not get called.
5215 THIS_CU->cu is always freed when done.
5216 This is done in order to not leave THIS_CU->cu in a state where we have
5217 to care whether it refers to the "main" CU or the DWO CU. */
5220 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5221 struct dwarf2_section_info *abbrev_section,
5222 struct dwo_file *dwo_file,
5223 die_reader_func_ftype *die_reader_func,
5226 struct objfile *objfile = dwarf2_per_objfile->objfile;
5227 struct dwarf2_section_info *section = this_cu->section;
5228 bfd *abfd = section->asection->owner;
5229 struct dwarf2_cu cu;
5230 const gdb_byte *begin_info_ptr, *info_ptr;
5231 struct die_reader_specs reader;
5232 struct cleanup *cleanups;
5233 struct die_info *comp_unit_die;
5236 if (dwarf2_die_debug)
5237 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5238 this_cu->is_debug_types ? "type" : "comp",
5239 this_cu->offset.sect_off);
5241 gdb_assert (this_cu->cu == NULL);
5243 /* This is cheap if the section is already read in. */
5244 dwarf2_read_section (objfile, section);
5246 init_one_comp_unit (&cu, this_cu);
5248 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5250 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5251 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5252 abbrev_section, info_ptr,
5253 this_cu->is_debug_types);
5255 this_cu->length = get_cu_length (&cu.header);
5257 /* Skip dummy compilation units. */
5258 if (info_ptr >= begin_info_ptr + this_cu->length
5259 || peek_abbrev_code (abfd, info_ptr) == 0)
5261 do_cleanups (cleanups);
5265 dwarf2_read_abbrevs (&cu, abbrev_section);
5266 make_cleanup (dwarf2_free_abbrev_table, &cu);
5268 init_cu_die_reader (&reader, &cu, section, dwo_file);
5269 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5271 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5273 do_cleanups (cleanups);
5276 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5277 does not lookup the specified DWO file.
5278 This cannot be used to read DWO files.
5280 THIS_CU->cu is always freed when done.
5281 This is done in order to not leave THIS_CU->cu in a state where we have
5282 to care whether it refers to the "main" CU or the DWO CU.
5283 We can revisit this if the data shows there's a performance issue. */
5286 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5287 die_reader_func_ftype *die_reader_func,
5290 init_cutu_and_read_dies_no_follow (this_cu,
5291 get_abbrev_section_for_cu (this_cu),
5293 die_reader_func, data);
5296 /* Type Unit Groups.
5298 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5299 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5300 so that all types coming from the same compilation (.o file) are grouped
5301 together. A future step could be to put the types in the same symtab as
5302 the CU the types ultimately came from. */
5305 hash_type_unit_group (const void *item)
5307 const struct type_unit_group *tu_group = item;
5309 return hash_stmt_list_entry (&tu_group->hash);
5313 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5315 const struct type_unit_group *lhs = item_lhs;
5316 const struct type_unit_group *rhs = item_rhs;
5318 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5321 /* Allocate a hash table for type unit groups. */
5324 allocate_type_unit_groups_table (void)
5326 return htab_create_alloc_ex (3,
5327 hash_type_unit_group,
5330 &dwarf2_per_objfile->objfile->objfile_obstack,
5331 hashtab_obstack_allocate,
5332 dummy_obstack_deallocate);
5335 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5336 partial symtabs. We combine several TUs per psymtab to not let the size
5337 of any one psymtab grow too big. */
5338 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5339 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5341 /* Helper routine for get_type_unit_group.
5342 Create the type_unit_group object used to hold one or more TUs. */
5344 static struct type_unit_group *
5345 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5347 struct objfile *objfile = dwarf2_per_objfile->objfile;
5348 struct dwarf2_per_cu_data *per_cu;
5349 struct type_unit_group *tu_group;
5351 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5352 struct type_unit_group);
5353 per_cu = &tu_group->per_cu;
5354 per_cu->objfile = objfile;
5356 if (dwarf2_per_objfile->using_index)
5358 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5359 struct dwarf2_per_cu_quick_data);
5363 unsigned int line_offset = line_offset_struct.sect_off;
5364 struct partial_symtab *pst;
5367 /* Give the symtab a useful name for debug purposes. */
5368 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5369 name = xstrprintf ("<type_units_%d>",
5370 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5372 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5374 pst = create_partial_symtab (per_cu, name);
5380 tu_group->hash.dwo_unit = cu->dwo_unit;
5381 tu_group->hash.line_offset = line_offset_struct;
5386 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5387 STMT_LIST is a DW_AT_stmt_list attribute. */
5389 static struct type_unit_group *
5390 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5392 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5393 struct type_unit_group *tu_group;
5395 unsigned int line_offset;
5396 struct type_unit_group type_unit_group_for_lookup;
5398 if (dwarf2_per_objfile->type_unit_groups == NULL)
5400 dwarf2_per_objfile->type_unit_groups =
5401 allocate_type_unit_groups_table ();
5404 /* Do we need to create a new group, or can we use an existing one? */
5408 line_offset = DW_UNSND (stmt_list);
5409 ++tu_stats->nr_symtab_sharers;
5413 /* Ugh, no stmt_list. Rare, but we have to handle it.
5414 We can do various things here like create one group per TU or
5415 spread them over multiple groups to split up the expansion work.
5416 To avoid worst case scenarios (too many groups or too large groups)
5417 we, umm, group them in bunches. */
5418 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5419 | (tu_stats->nr_stmt_less_type_units
5420 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5421 ++tu_stats->nr_stmt_less_type_units;
5424 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5425 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5426 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5427 &type_unit_group_for_lookup, INSERT);
5431 gdb_assert (tu_group != NULL);
5435 sect_offset line_offset_struct;
5437 line_offset_struct.sect_off = line_offset;
5438 tu_group = create_type_unit_group (cu, line_offset_struct);
5440 ++tu_stats->nr_symtabs;
5446 /* Struct used to sort TUs by their abbreviation table offset. */
5448 struct tu_abbrev_offset
5450 struct signatured_type *sig_type;
5451 sect_offset abbrev_offset;
5454 /* Helper routine for build_type_unit_groups, passed to qsort. */
5457 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5459 const struct tu_abbrev_offset * const *a = ap;
5460 const struct tu_abbrev_offset * const *b = bp;
5461 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5462 unsigned int boff = (*b)->abbrev_offset.sect_off;
5464 return (aoff > boff) - (aoff < boff);
5467 /* A helper function to add a type_unit_group to a table. */
5470 add_type_unit_group_to_table (void **slot, void *datum)
5472 struct type_unit_group *tu_group = *slot;
5473 struct type_unit_group ***datap = datum;
5481 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5482 each one passing FUNC,DATA.
5484 The efficiency is because we sort TUs by the abbrev table they use and
5485 only read each abbrev table once. In one program there are 200K TUs
5486 sharing 8K abbrev tables.
5488 The main purpose of this function is to support building the
5489 dwarf2_per_objfile->type_unit_groups table.
5490 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5491 can collapse the search space by grouping them by stmt_list.
5492 The savings can be significant, in the same program from above the 200K TUs
5493 share 8K stmt_list tables.
5495 FUNC is expected to call get_type_unit_group, which will create the
5496 struct type_unit_group if necessary and add it to
5497 dwarf2_per_objfile->type_unit_groups. */
5500 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5502 struct objfile *objfile = dwarf2_per_objfile->objfile;
5503 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5504 struct cleanup *cleanups;
5505 struct abbrev_table *abbrev_table;
5506 sect_offset abbrev_offset;
5507 struct tu_abbrev_offset *sorted_by_abbrev;
5508 struct type_unit_group **iter;
5511 /* It's up to the caller to not call us multiple times. */
5512 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5514 if (dwarf2_per_objfile->n_type_units == 0)
5517 /* TUs typically share abbrev tables, and there can be way more TUs than
5518 abbrev tables. Sort by abbrev table to reduce the number of times we
5519 read each abbrev table in.
5520 Alternatives are to punt or to maintain a cache of abbrev tables.
5521 This is simpler and efficient enough for now.
5523 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5524 symtab to use). Typically TUs with the same abbrev offset have the same
5525 stmt_list value too so in practice this should work well.
5527 The basic algorithm here is:
5529 sort TUs by abbrev table
5530 for each TU with same abbrev table:
5531 read abbrev table if first user
5532 read TU top level DIE
5533 [IWBN if DWO skeletons had DW_AT_stmt_list]
5536 if (dwarf2_read_debug)
5537 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5539 /* Sort in a separate table to maintain the order of all_type_units
5540 for .gdb_index: TU indices directly index all_type_units. */
5541 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5542 dwarf2_per_objfile->n_type_units);
5543 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5545 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5547 sorted_by_abbrev[i].sig_type = sig_type;
5548 sorted_by_abbrev[i].abbrev_offset =
5549 read_abbrev_offset (sig_type->per_cu.section,
5550 sig_type->per_cu.offset);
5552 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5553 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5554 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5556 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5557 called any number of times, so we don't reset tu_stats here. */
5559 abbrev_offset.sect_off = ~(unsigned) 0;
5560 abbrev_table = NULL;
5561 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5563 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5565 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5567 /* Switch to the next abbrev table if necessary. */
5568 if (abbrev_table == NULL
5569 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5571 if (abbrev_table != NULL)
5573 abbrev_table_free (abbrev_table);
5574 /* Reset to NULL in case abbrev_table_read_table throws
5575 an error: abbrev_table_free_cleanup will get called. */
5576 abbrev_table = NULL;
5578 abbrev_offset = tu->abbrev_offset;
5580 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5582 ++tu_stats->nr_uniq_abbrev_tables;
5585 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5589 /* type_unit_groups can be NULL if there is an error in the debug info.
5590 Just create an empty table so the rest of gdb doesn't have to watch
5591 for this error case. */
5592 if (dwarf2_per_objfile->type_unit_groups == NULL)
5594 dwarf2_per_objfile->type_unit_groups =
5595 allocate_type_unit_groups_table ();
5596 dwarf2_per_objfile->n_type_unit_groups = 0;
5599 /* Create a vector of pointers to primary type units to make it easy to
5600 iterate over them and CUs. See dw2_get_primary_cu. */
5601 dwarf2_per_objfile->n_type_unit_groups =
5602 htab_elements (dwarf2_per_objfile->type_unit_groups);
5603 dwarf2_per_objfile->all_type_unit_groups =
5604 obstack_alloc (&objfile->objfile_obstack,
5605 dwarf2_per_objfile->n_type_unit_groups
5606 * sizeof (struct type_unit_group *));
5607 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5608 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5609 add_type_unit_group_to_table, &iter);
5610 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5611 == dwarf2_per_objfile->n_type_unit_groups);
5613 do_cleanups (cleanups);
5615 if (dwarf2_read_debug)
5617 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5618 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5619 dwarf2_per_objfile->n_type_units);
5620 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5621 tu_stats->nr_uniq_abbrev_tables);
5622 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5623 tu_stats->nr_symtabs);
5624 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5625 tu_stats->nr_symtab_sharers);
5626 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5627 tu_stats->nr_stmt_less_type_units);
5631 /* Partial symbol tables. */
5633 /* Create a psymtab named NAME and assign it to PER_CU.
5635 The caller must fill in the following details:
5636 dirname, textlow, texthigh. */
5638 static struct partial_symtab *
5639 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5641 struct objfile *objfile = per_cu->objfile;
5642 struct partial_symtab *pst;
5644 pst = start_psymtab_common (objfile, objfile->section_offsets,
5646 objfile->global_psymbols.next,
5647 objfile->static_psymbols.next);
5649 pst->psymtabs_addrmap_supported = 1;
5651 /* This is the glue that links PST into GDB's symbol API. */
5652 pst->read_symtab_private = per_cu;
5653 pst->read_symtab = dwarf2_read_symtab;
5654 per_cu->v.psymtab = pst;
5659 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5662 struct process_psymtab_comp_unit_data
5664 /* True if we are reading a DW_TAG_partial_unit. */
5666 int want_partial_unit;
5668 /* The "pretend" language that is used if the CU doesn't declare a
5671 enum language pretend_language;
5674 /* die_reader_func for process_psymtab_comp_unit. */
5677 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5678 const gdb_byte *info_ptr,
5679 struct die_info *comp_unit_die,
5683 struct dwarf2_cu *cu = reader->cu;
5684 struct objfile *objfile = cu->objfile;
5685 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5686 struct attribute *attr;
5688 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5689 struct partial_symtab *pst;
5691 const char *filename;
5692 struct process_psymtab_comp_unit_data *info = data;
5694 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5697 gdb_assert (! per_cu->is_debug_types);
5699 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5701 cu->list_in_scope = &file_symbols;
5703 /* Allocate a new partial symbol table structure. */
5704 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5705 if (attr == NULL || !DW_STRING (attr))
5708 filename = DW_STRING (attr);
5710 pst = create_partial_symtab (per_cu, filename);
5712 /* This must be done before calling dwarf2_build_include_psymtabs. */
5713 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5715 pst->dirname = DW_STRING (attr);
5717 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5719 dwarf2_find_base_address (comp_unit_die, cu);
5721 /* Possibly set the default values of LOWPC and HIGHPC from
5723 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5724 &best_highpc, cu, pst);
5725 if (has_pc_info == 1 && best_lowpc < best_highpc)
5726 /* Store the contiguous range if it is not empty; it can be empty for
5727 CUs with no code. */
5728 addrmap_set_empty (objfile->psymtabs_addrmap,
5729 best_lowpc + baseaddr,
5730 best_highpc + baseaddr - 1, pst);
5732 /* Check if comp unit has_children.
5733 If so, read the rest of the partial symbols from this comp unit.
5734 If not, there's no more debug_info for this comp unit. */
5737 struct partial_die_info *first_die;
5738 CORE_ADDR lowpc, highpc;
5740 lowpc = ((CORE_ADDR) -1);
5741 highpc = ((CORE_ADDR) 0);
5743 first_die = load_partial_dies (reader, info_ptr, 1);
5745 scan_partial_symbols (first_die, &lowpc, &highpc,
5748 /* If we didn't find a lowpc, set it to highpc to avoid
5749 complaints from `maint check'. */
5750 if (lowpc == ((CORE_ADDR) -1))
5753 /* If the compilation unit didn't have an explicit address range,
5754 then use the information extracted from its child dies. */
5758 best_highpc = highpc;
5761 pst->textlow = best_lowpc + baseaddr;
5762 pst->texthigh = best_highpc + baseaddr;
5764 pst->n_global_syms = objfile->global_psymbols.next -
5765 (objfile->global_psymbols.list + pst->globals_offset);
5766 pst->n_static_syms = objfile->static_psymbols.next -
5767 (objfile->static_psymbols.list + pst->statics_offset);
5768 sort_pst_symbols (objfile, pst);
5770 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5773 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5774 struct dwarf2_per_cu_data *iter;
5776 /* Fill in 'dependencies' here; we fill in 'users' in a
5778 pst->number_of_dependencies = len;
5779 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5780 len * sizeof (struct symtab *));
5782 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5785 pst->dependencies[i] = iter->v.psymtab;
5787 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5790 /* Get the list of files included in the current compilation unit,
5791 and build a psymtab for each of them. */
5792 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5794 if (dwarf2_read_debug)
5796 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5798 fprintf_unfiltered (gdb_stdlog,
5799 "Psymtab for %s unit @0x%x: %s - %s"
5800 ", %d global, %d static syms\n",
5801 per_cu->is_debug_types ? "type" : "comp",
5802 per_cu->offset.sect_off,
5803 paddress (gdbarch, pst->textlow),
5804 paddress (gdbarch, pst->texthigh),
5805 pst->n_global_syms, pst->n_static_syms);
5809 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5810 Process compilation unit THIS_CU for a psymtab. */
5813 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5814 int want_partial_unit,
5815 enum language pretend_language)
5817 struct process_psymtab_comp_unit_data info;
5819 /* If this compilation unit was already read in, free the
5820 cached copy in order to read it in again. This is
5821 necessary because we skipped some symbols when we first
5822 read in the compilation unit (see load_partial_dies).
5823 This problem could be avoided, but the benefit is unclear. */
5824 if (this_cu->cu != NULL)
5825 free_one_cached_comp_unit (this_cu);
5827 gdb_assert (! this_cu->is_debug_types);
5828 info.want_partial_unit = want_partial_unit;
5829 info.pretend_language = pretend_language;
5830 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5831 process_psymtab_comp_unit_reader,
5834 /* Age out any secondary CUs. */
5835 age_cached_comp_units ();
5838 /* Reader function for build_type_psymtabs. */
5841 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5842 const gdb_byte *info_ptr,
5843 struct die_info *type_unit_die,
5847 struct objfile *objfile = dwarf2_per_objfile->objfile;
5848 struct dwarf2_cu *cu = reader->cu;
5849 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5850 struct signatured_type *sig_type;
5851 struct type_unit_group *tu_group;
5852 struct attribute *attr;
5853 struct partial_die_info *first_die;
5854 CORE_ADDR lowpc, highpc;
5855 struct partial_symtab *pst;
5857 gdb_assert (data == NULL);
5858 gdb_assert (per_cu->is_debug_types);
5859 sig_type = (struct signatured_type *) per_cu;
5864 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5865 tu_group = get_type_unit_group (cu, attr);
5867 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5869 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5870 cu->list_in_scope = &file_symbols;
5871 pst = create_partial_symtab (per_cu, "");
5874 first_die = load_partial_dies (reader, info_ptr, 1);
5876 lowpc = (CORE_ADDR) -1;
5877 highpc = (CORE_ADDR) 0;
5878 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5880 pst->n_global_syms = objfile->global_psymbols.next -
5881 (objfile->global_psymbols.list + pst->globals_offset);
5882 pst->n_static_syms = objfile->static_psymbols.next -
5883 (objfile->static_psymbols.list + pst->statics_offset);
5884 sort_pst_symbols (objfile, pst);
5887 /* Traversal function for build_type_psymtabs. */
5890 build_type_psymtab_dependencies (void **slot, void *info)
5892 struct objfile *objfile = dwarf2_per_objfile->objfile;
5893 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5894 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5895 struct partial_symtab *pst = per_cu->v.psymtab;
5896 int len = VEC_length (sig_type_ptr, tu_group->tus);
5897 struct signatured_type *iter;
5900 gdb_assert (len > 0);
5901 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5903 pst->number_of_dependencies = len;
5904 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5905 len * sizeof (struct psymtab *));
5907 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5910 gdb_assert (iter->per_cu.is_debug_types);
5911 pst->dependencies[i] = iter->per_cu.v.psymtab;
5912 iter->type_unit_group = tu_group;
5915 VEC_free (sig_type_ptr, tu_group->tus);
5920 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5921 Build partial symbol tables for the .debug_types comp-units. */
5924 build_type_psymtabs (struct objfile *objfile)
5926 if (! create_all_type_units (objfile))
5929 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5931 /* Now that all TUs have been processed we can fill in the dependencies. */
5932 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5933 build_type_psymtab_dependencies, NULL);
5936 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5939 psymtabs_addrmap_cleanup (void *o)
5941 struct objfile *objfile = o;
5943 objfile->psymtabs_addrmap = NULL;
5946 /* Compute the 'user' field for each psymtab in OBJFILE. */
5949 set_partial_user (struct objfile *objfile)
5953 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5955 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5956 struct partial_symtab *pst = per_cu->v.psymtab;
5962 for (j = 0; j < pst->number_of_dependencies; ++j)
5964 /* Set the 'user' field only if it is not already set. */
5965 if (pst->dependencies[j]->user == NULL)
5966 pst->dependencies[j]->user = pst;
5971 /* Build the partial symbol table by doing a quick pass through the
5972 .debug_info and .debug_abbrev sections. */
5975 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5977 struct cleanup *back_to, *addrmap_cleanup;
5978 struct obstack temp_obstack;
5981 if (dwarf2_read_debug)
5983 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5984 objfile_name (objfile));
5987 dwarf2_per_objfile->reading_partial_symbols = 1;
5989 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5991 /* Any cached compilation units will be linked by the per-objfile
5992 read_in_chain. Make sure to free them when we're done. */
5993 back_to = make_cleanup (free_cached_comp_units, NULL);
5995 build_type_psymtabs (objfile);
5997 create_all_comp_units (objfile);
5999 /* Create a temporary address map on a temporary obstack. We later
6000 copy this to the final obstack. */
6001 obstack_init (&temp_obstack);
6002 make_cleanup_obstack_free (&temp_obstack);
6003 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6004 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6006 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6008 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
6010 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6013 set_partial_user (objfile);
6015 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6016 &objfile->objfile_obstack);
6017 discard_cleanups (addrmap_cleanup);
6019 do_cleanups (back_to);
6021 if (dwarf2_read_debug)
6022 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6023 objfile_name (objfile));
6026 /* die_reader_func for load_partial_comp_unit. */
6029 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6030 const gdb_byte *info_ptr,
6031 struct die_info *comp_unit_die,
6035 struct dwarf2_cu *cu = reader->cu;
6037 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6039 /* Check if comp unit has_children.
6040 If so, read the rest of the partial symbols from this comp unit.
6041 If not, there's no more debug_info for this comp unit. */
6043 load_partial_dies (reader, info_ptr, 0);
6046 /* Load the partial DIEs for a secondary CU into memory.
6047 This is also used when rereading a primary CU with load_all_dies. */
6050 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6052 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6053 load_partial_comp_unit_reader, NULL);
6057 read_comp_units_from_section (struct objfile *objfile,
6058 struct dwarf2_section_info *section,
6059 unsigned int is_dwz,
6062 struct dwarf2_per_cu_data ***all_comp_units)
6064 const gdb_byte *info_ptr;
6065 bfd *abfd = section->asection->owner;
6067 if (dwarf2_read_debug)
6068 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6069 section->asection->name, bfd_get_filename (abfd));
6071 dwarf2_read_section (objfile, section);
6073 info_ptr = section->buffer;
6075 while (info_ptr < section->buffer + section->size)
6077 unsigned int length, initial_length_size;
6078 struct dwarf2_per_cu_data *this_cu;
6081 offset.sect_off = info_ptr - section->buffer;
6083 /* Read just enough information to find out where the next
6084 compilation unit is. */
6085 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6087 /* Save the compilation unit for later lookup. */
6088 this_cu = obstack_alloc (&objfile->objfile_obstack,
6089 sizeof (struct dwarf2_per_cu_data));
6090 memset (this_cu, 0, sizeof (*this_cu));
6091 this_cu->offset = offset;
6092 this_cu->length = length + initial_length_size;
6093 this_cu->is_dwz = is_dwz;
6094 this_cu->objfile = objfile;
6095 this_cu->section = section;
6097 if (*n_comp_units == *n_allocated)
6100 *all_comp_units = xrealloc (*all_comp_units,
6102 * sizeof (struct dwarf2_per_cu_data *));
6104 (*all_comp_units)[*n_comp_units] = this_cu;
6107 info_ptr = info_ptr + this_cu->length;
6111 /* Create a list of all compilation units in OBJFILE.
6112 This is only done for -readnow and building partial symtabs. */
6115 create_all_comp_units (struct objfile *objfile)
6119 struct dwarf2_per_cu_data **all_comp_units;
6120 struct dwz_file *dwz;
6124 all_comp_units = xmalloc (n_allocated
6125 * sizeof (struct dwarf2_per_cu_data *));
6127 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6128 &n_allocated, &n_comp_units, &all_comp_units);
6130 dwz = dwarf2_get_dwz_file ();
6132 read_comp_units_from_section (objfile, &dwz->info, 1,
6133 &n_allocated, &n_comp_units,
6136 dwarf2_per_objfile->all_comp_units
6137 = obstack_alloc (&objfile->objfile_obstack,
6138 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6139 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6140 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6141 xfree (all_comp_units);
6142 dwarf2_per_objfile->n_comp_units = n_comp_units;
6145 /* Process all loaded DIEs for compilation unit CU, starting at
6146 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6147 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6148 DW_AT_ranges). If NEED_PC is set, then this function will set
6149 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6150 and record the covered ranges in the addrmap. */
6153 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6154 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6156 struct partial_die_info *pdi;
6158 /* Now, march along the PDI's, descending into ones which have
6159 interesting children but skipping the children of the other ones,
6160 until we reach the end of the compilation unit. */
6166 fixup_partial_die (pdi, cu);
6168 /* Anonymous namespaces or modules have no name but have interesting
6169 children, so we need to look at them. Ditto for anonymous
6172 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6173 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6174 || pdi->tag == DW_TAG_imported_unit)
6178 case DW_TAG_subprogram:
6179 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6181 case DW_TAG_constant:
6182 case DW_TAG_variable:
6183 case DW_TAG_typedef:
6184 case DW_TAG_union_type:
6185 if (!pdi->is_declaration)
6187 add_partial_symbol (pdi, cu);
6190 case DW_TAG_class_type:
6191 case DW_TAG_interface_type:
6192 case DW_TAG_structure_type:
6193 if (!pdi->is_declaration)
6195 add_partial_symbol (pdi, cu);
6198 case DW_TAG_enumeration_type:
6199 if (!pdi->is_declaration)
6200 add_partial_enumeration (pdi, cu);
6202 case DW_TAG_base_type:
6203 case DW_TAG_subrange_type:
6204 /* File scope base type definitions are added to the partial
6206 add_partial_symbol (pdi, cu);
6208 case DW_TAG_namespace:
6209 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
6212 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6214 case DW_TAG_imported_unit:
6216 struct dwarf2_per_cu_data *per_cu;
6218 /* For now we don't handle imported units in type units. */
6219 if (cu->per_cu->is_debug_types)
6221 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6222 " supported in type units [in module %s]"),
6223 objfile_name (cu->objfile));
6226 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6230 /* Go read the partial unit, if needed. */
6231 if (per_cu->v.psymtab == NULL)
6232 process_psymtab_comp_unit (per_cu, 1, cu->language);
6234 VEC_safe_push (dwarf2_per_cu_ptr,
6235 cu->per_cu->imported_symtabs, per_cu);
6243 /* If the die has a sibling, skip to the sibling. */
6245 pdi = pdi->die_sibling;
6249 /* Functions used to compute the fully scoped name of a partial DIE.
6251 Normally, this is simple. For C++, the parent DIE's fully scoped
6252 name is concatenated with "::" and the partial DIE's name. For
6253 Java, the same thing occurs except that "." is used instead of "::".
6254 Enumerators are an exception; they use the scope of their parent
6255 enumeration type, i.e. the name of the enumeration type is not
6256 prepended to the enumerator.
6258 There are two complexities. One is DW_AT_specification; in this
6259 case "parent" means the parent of the target of the specification,
6260 instead of the direct parent of the DIE. The other is compilers
6261 which do not emit DW_TAG_namespace; in this case we try to guess
6262 the fully qualified name of structure types from their members'
6263 linkage names. This must be done using the DIE's children rather
6264 than the children of any DW_AT_specification target. We only need
6265 to do this for structures at the top level, i.e. if the target of
6266 any DW_AT_specification (if any; otherwise the DIE itself) does not
6269 /* Compute the scope prefix associated with PDI's parent, in
6270 compilation unit CU. The result will be allocated on CU's
6271 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6272 field. NULL is returned if no prefix is necessary. */
6274 partial_die_parent_scope (struct partial_die_info *pdi,
6275 struct dwarf2_cu *cu)
6277 const char *grandparent_scope;
6278 struct partial_die_info *parent, *real_pdi;
6280 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6281 then this means the parent of the specification DIE. */
6284 while (real_pdi->has_specification)
6285 real_pdi = find_partial_die (real_pdi->spec_offset,
6286 real_pdi->spec_is_dwz, cu);
6288 parent = real_pdi->die_parent;
6292 if (parent->scope_set)
6293 return parent->scope;
6295 fixup_partial_die (parent, cu);
6297 grandparent_scope = partial_die_parent_scope (parent, cu);
6299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6300 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6301 Work around this problem here. */
6302 if (cu->language == language_cplus
6303 && parent->tag == DW_TAG_namespace
6304 && strcmp (parent->name, "::") == 0
6305 && grandparent_scope == NULL)
6307 parent->scope = NULL;
6308 parent->scope_set = 1;
6312 if (pdi->tag == DW_TAG_enumerator)
6313 /* Enumerators should not get the name of the enumeration as a prefix. */
6314 parent->scope = grandparent_scope;
6315 else if (parent->tag == DW_TAG_namespace
6316 || parent->tag == DW_TAG_module
6317 || parent->tag == DW_TAG_structure_type
6318 || parent->tag == DW_TAG_class_type
6319 || parent->tag == DW_TAG_interface_type
6320 || parent->tag == DW_TAG_union_type
6321 || parent->tag == DW_TAG_enumeration_type)
6323 if (grandparent_scope == NULL)
6324 parent->scope = parent->name;
6326 parent->scope = typename_concat (&cu->comp_unit_obstack,
6328 parent->name, 0, cu);
6332 /* FIXME drow/2004-04-01: What should we be doing with
6333 function-local names? For partial symbols, we should probably be
6335 complaint (&symfile_complaints,
6336 _("unhandled containing DIE tag %d for DIE at %d"),
6337 parent->tag, pdi->offset.sect_off);
6338 parent->scope = grandparent_scope;
6341 parent->scope_set = 1;
6342 return parent->scope;
6345 /* Return the fully scoped name associated with PDI, from compilation unit
6346 CU. The result will be allocated with malloc. */
6349 partial_die_full_name (struct partial_die_info *pdi,
6350 struct dwarf2_cu *cu)
6352 const char *parent_scope;
6354 /* If this is a template instantiation, we can not work out the
6355 template arguments from partial DIEs. So, unfortunately, we have
6356 to go through the full DIEs. At least any work we do building
6357 types here will be reused if full symbols are loaded later. */
6358 if (pdi->has_template_arguments)
6360 fixup_partial_die (pdi, cu);
6362 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6364 struct die_info *die;
6365 struct attribute attr;
6366 struct dwarf2_cu *ref_cu = cu;
6368 /* DW_FORM_ref_addr is using section offset. */
6370 attr.form = DW_FORM_ref_addr;
6371 attr.u.unsnd = pdi->offset.sect_off;
6372 die = follow_die_ref (NULL, &attr, &ref_cu);
6374 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6378 parent_scope = partial_die_parent_scope (pdi, cu);
6379 if (parent_scope == NULL)
6382 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6386 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6388 struct objfile *objfile = cu->objfile;
6390 const char *actual_name = NULL;
6392 char *built_actual_name;
6394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6396 built_actual_name = partial_die_full_name (pdi, cu);
6397 if (built_actual_name != NULL)
6398 actual_name = built_actual_name;
6400 if (actual_name == NULL)
6401 actual_name = pdi->name;
6405 case DW_TAG_subprogram:
6406 if (pdi->is_external || cu->language == language_ada)
6408 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6409 of the global scope. But in Ada, we want to be able to access
6410 nested procedures globally. So all Ada subprograms are stored
6411 in the global scope. */
6412 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6413 mst_text, objfile); */
6414 add_psymbol_to_list (actual_name, strlen (actual_name),
6415 built_actual_name != NULL,
6416 VAR_DOMAIN, LOC_BLOCK,
6417 &objfile->global_psymbols,
6418 0, pdi->lowpc + baseaddr,
6419 cu->language, objfile);
6423 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6424 mst_file_text, objfile); */
6425 add_psymbol_to_list (actual_name, strlen (actual_name),
6426 built_actual_name != NULL,
6427 VAR_DOMAIN, LOC_BLOCK,
6428 &objfile->static_psymbols,
6429 0, pdi->lowpc + baseaddr,
6430 cu->language, objfile);
6433 case DW_TAG_constant:
6435 struct psymbol_allocation_list *list;
6437 if (pdi->is_external)
6438 list = &objfile->global_psymbols;
6440 list = &objfile->static_psymbols;
6441 add_psymbol_to_list (actual_name, strlen (actual_name),
6442 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6443 list, 0, 0, cu->language, objfile);
6446 case DW_TAG_variable:
6448 addr = decode_locdesc (pdi->d.locdesc, cu);
6452 && !dwarf2_per_objfile->has_section_at_zero)
6454 /* A global or static variable may also have been stripped
6455 out by the linker if unused, in which case its address
6456 will be nullified; do not add such variables into partial
6457 symbol table then. */
6459 else if (pdi->is_external)
6462 Don't enter into the minimal symbol tables as there is
6463 a minimal symbol table entry from the ELF symbols already.
6464 Enter into partial symbol table if it has a location
6465 descriptor or a type.
6466 If the location descriptor is missing, new_symbol will create
6467 a LOC_UNRESOLVED symbol, the address of the variable will then
6468 be determined from the minimal symbol table whenever the variable
6470 The address for the partial symbol table entry is not
6471 used by GDB, but it comes in handy for debugging partial symbol
6474 if (pdi->d.locdesc || pdi->has_type)
6475 add_psymbol_to_list (actual_name, strlen (actual_name),
6476 built_actual_name != NULL,
6477 VAR_DOMAIN, LOC_STATIC,
6478 &objfile->global_psymbols,
6480 cu->language, objfile);
6484 /* Static Variable. Skip symbols without location descriptors. */
6485 if (pdi->d.locdesc == NULL)
6487 xfree (built_actual_name);
6490 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6491 mst_file_data, objfile); */
6492 add_psymbol_to_list (actual_name, strlen (actual_name),
6493 built_actual_name != NULL,
6494 VAR_DOMAIN, LOC_STATIC,
6495 &objfile->static_psymbols,
6497 cu->language, objfile);
6500 case DW_TAG_typedef:
6501 case DW_TAG_base_type:
6502 case DW_TAG_subrange_type:
6503 add_psymbol_to_list (actual_name, strlen (actual_name),
6504 built_actual_name != NULL,
6505 VAR_DOMAIN, LOC_TYPEDEF,
6506 &objfile->static_psymbols,
6507 0, (CORE_ADDR) 0, cu->language, objfile);
6509 case DW_TAG_namespace:
6510 add_psymbol_to_list (actual_name, strlen (actual_name),
6511 built_actual_name != NULL,
6512 VAR_DOMAIN, LOC_TYPEDEF,
6513 &objfile->global_psymbols,
6514 0, (CORE_ADDR) 0, cu->language, objfile);
6516 case DW_TAG_class_type:
6517 case DW_TAG_interface_type:
6518 case DW_TAG_structure_type:
6519 case DW_TAG_union_type:
6520 case DW_TAG_enumeration_type:
6521 /* Skip external references. The DWARF standard says in the section
6522 about "Structure, Union, and Class Type Entries": "An incomplete
6523 structure, union or class type is represented by a structure,
6524 union or class entry that does not have a byte size attribute
6525 and that has a DW_AT_declaration attribute." */
6526 if (!pdi->has_byte_size && pdi->is_declaration)
6528 xfree (built_actual_name);
6532 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6533 static vs. global. */
6534 add_psymbol_to_list (actual_name, strlen (actual_name),
6535 built_actual_name != NULL,
6536 STRUCT_DOMAIN, LOC_TYPEDEF,
6537 (cu->language == language_cplus
6538 || cu->language == language_java)
6539 ? &objfile->global_psymbols
6540 : &objfile->static_psymbols,
6541 0, (CORE_ADDR) 0, cu->language, objfile);
6544 case DW_TAG_enumerator:
6545 add_psymbol_to_list (actual_name, strlen (actual_name),
6546 built_actual_name != NULL,
6547 VAR_DOMAIN, LOC_CONST,
6548 (cu->language == language_cplus
6549 || cu->language == language_java)
6550 ? &objfile->global_psymbols
6551 : &objfile->static_psymbols,
6552 0, (CORE_ADDR) 0, cu->language, objfile);
6558 xfree (built_actual_name);
6561 /* Read a partial die corresponding to a namespace; also, add a symbol
6562 corresponding to that namespace to the symbol table. NAMESPACE is
6563 the name of the enclosing namespace. */
6566 add_partial_namespace (struct partial_die_info *pdi,
6567 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6568 int need_pc, struct dwarf2_cu *cu)
6570 /* Add a symbol for the namespace. */
6572 add_partial_symbol (pdi, cu);
6574 /* Now scan partial symbols in that namespace. */
6576 if (pdi->has_children)
6577 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6580 /* Read a partial die corresponding to a Fortran module. */
6583 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6584 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6586 /* Now scan partial symbols in that module. */
6588 if (pdi->has_children)
6589 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6592 /* Read a partial die corresponding to a subprogram and create a partial
6593 symbol for that subprogram. When the CU language allows it, this
6594 routine also defines a partial symbol for each nested subprogram
6595 that this subprogram contains.
6597 DIE my also be a lexical block, in which case we simply search
6598 recursively for suprograms defined inside that lexical block.
6599 Again, this is only performed when the CU language allows this
6600 type of definitions. */
6603 add_partial_subprogram (struct partial_die_info *pdi,
6604 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6605 int need_pc, struct dwarf2_cu *cu)
6607 if (pdi->tag == DW_TAG_subprogram)
6609 if (pdi->has_pc_info)
6611 if (pdi->lowpc < *lowpc)
6612 *lowpc = pdi->lowpc;
6613 if (pdi->highpc > *highpc)
6614 *highpc = pdi->highpc;
6618 struct objfile *objfile = cu->objfile;
6620 baseaddr = ANOFFSET (objfile->section_offsets,
6621 SECT_OFF_TEXT (objfile));
6622 addrmap_set_empty (objfile->psymtabs_addrmap,
6623 pdi->lowpc + baseaddr,
6624 pdi->highpc - 1 + baseaddr,
6625 cu->per_cu->v.psymtab);
6629 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6631 if (!pdi->is_declaration)
6632 /* Ignore subprogram DIEs that do not have a name, they are
6633 illegal. Do not emit a complaint at this point, we will
6634 do so when we convert this psymtab into a symtab. */
6636 add_partial_symbol (pdi, cu);
6640 if (! pdi->has_children)
6643 if (cu->language == language_ada)
6645 pdi = pdi->die_child;
6648 fixup_partial_die (pdi, cu);
6649 if (pdi->tag == DW_TAG_subprogram
6650 || pdi->tag == DW_TAG_lexical_block)
6651 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6652 pdi = pdi->die_sibling;
6657 /* Read a partial die corresponding to an enumeration type. */
6660 add_partial_enumeration (struct partial_die_info *enum_pdi,
6661 struct dwarf2_cu *cu)
6663 struct partial_die_info *pdi;
6665 if (enum_pdi->name != NULL)
6666 add_partial_symbol (enum_pdi, cu);
6668 pdi = enum_pdi->die_child;
6671 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6672 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6674 add_partial_symbol (pdi, cu);
6675 pdi = pdi->die_sibling;
6679 /* Return the initial uleb128 in the die at INFO_PTR. */
6682 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6684 unsigned int bytes_read;
6686 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6689 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6690 Return the corresponding abbrev, or NULL if the number is zero (indicating
6691 an empty DIE). In either case *BYTES_READ will be set to the length of
6692 the initial number. */
6694 static struct abbrev_info *
6695 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
6696 struct dwarf2_cu *cu)
6698 bfd *abfd = cu->objfile->obfd;
6699 unsigned int abbrev_number;
6700 struct abbrev_info *abbrev;
6702 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6704 if (abbrev_number == 0)
6707 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6710 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6711 abbrev_number, bfd_get_filename (abfd));
6717 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6718 Returns a pointer to the end of a series of DIEs, terminated by an empty
6719 DIE. Any children of the skipped DIEs will also be skipped. */
6721 static const gdb_byte *
6722 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
6724 struct dwarf2_cu *cu = reader->cu;
6725 struct abbrev_info *abbrev;
6726 unsigned int bytes_read;
6730 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6732 return info_ptr + bytes_read;
6734 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6738 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6739 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6740 abbrev corresponding to that skipped uleb128 should be passed in
6741 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6744 static const gdb_byte *
6745 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
6746 struct abbrev_info *abbrev)
6748 unsigned int bytes_read;
6749 struct attribute attr;
6750 bfd *abfd = reader->abfd;
6751 struct dwarf2_cu *cu = reader->cu;
6752 const gdb_byte *buffer = reader->buffer;
6753 const gdb_byte *buffer_end = reader->buffer_end;
6754 const gdb_byte *start_info_ptr = info_ptr;
6755 unsigned int form, i;
6757 for (i = 0; i < abbrev->num_attrs; i++)
6759 /* The only abbrev we care about is DW_AT_sibling. */
6760 if (abbrev->attrs[i].name == DW_AT_sibling)
6762 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6763 if (attr.form == DW_FORM_ref_addr)
6764 complaint (&symfile_complaints,
6765 _("ignoring absolute DW_AT_sibling"));
6767 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6770 /* If it isn't DW_AT_sibling, skip this attribute. */
6771 form = abbrev->attrs[i].form;
6775 case DW_FORM_ref_addr:
6776 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6777 and later it is offset sized. */
6778 if (cu->header.version == 2)
6779 info_ptr += cu->header.addr_size;
6781 info_ptr += cu->header.offset_size;
6783 case DW_FORM_GNU_ref_alt:
6784 info_ptr += cu->header.offset_size;
6787 info_ptr += cu->header.addr_size;
6794 case DW_FORM_flag_present:
6806 case DW_FORM_ref_sig8:
6809 case DW_FORM_string:
6810 read_direct_string (abfd, info_ptr, &bytes_read);
6811 info_ptr += bytes_read;
6813 case DW_FORM_sec_offset:
6815 case DW_FORM_GNU_strp_alt:
6816 info_ptr += cu->header.offset_size;
6818 case DW_FORM_exprloc:
6820 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6821 info_ptr += bytes_read;
6823 case DW_FORM_block1:
6824 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6826 case DW_FORM_block2:
6827 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6829 case DW_FORM_block4:
6830 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6834 case DW_FORM_ref_udata:
6835 case DW_FORM_GNU_addr_index:
6836 case DW_FORM_GNU_str_index:
6837 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
6839 case DW_FORM_indirect:
6840 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6841 info_ptr += bytes_read;
6842 /* We need to continue parsing from here, so just go back to
6844 goto skip_attribute;
6847 error (_("Dwarf Error: Cannot handle %s "
6848 "in DWARF reader [in module %s]"),
6849 dwarf_form_name (form),
6850 bfd_get_filename (abfd));
6854 if (abbrev->has_children)
6855 return skip_children (reader, info_ptr);
6860 /* Locate ORIG_PDI's sibling.
6861 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6863 static const gdb_byte *
6864 locate_pdi_sibling (const struct die_reader_specs *reader,
6865 struct partial_die_info *orig_pdi,
6866 const gdb_byte *info_ptr)
6868 /* Do we know the sibling already? */
6870 if (orig_pdi->sibling)
6871 return orig_pdi->sibling;
6873 /* Are there any children to deal with? */
6875 if (!orig_pdi->has_children)
6878 /* Skip the children the long way. */
6880 return skip_children (reader, info_ptr);
6883 /* Expand this partial symbol table into a full symbol table. SELF is
6887 dwarf2_read_symtab (struct partial_symtab *self,
6888 struct objfile *objfile)
6892 warning (_("bug: psymtab for %s is already read in."),
6899 printf_filtered (_("Reading in symbols for %s..."),
6901 gdb_flush (gdb_stdout);
6904 /* Restore our global data. */
6905 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6907 /* If this psymtab is constructed from a debug-only objfile, the
6908 has_section_at_zero flag will not necessarily be correct. We
6909 can get the correct value for this flag by looking at the data
6910 associated with the (presumably stripped) associated objfile. */
6911 if (objfile->separate_debug_objfile_backlink)
6913 struct dwarf2_per_objfile *dpo_backlink
6914 = objfile_data (objfile->separate_debug_objfile_backlink,
6915 dwarf2_objfile_data_key);
6917 dwarf2_per_objfile->has_section_at_zero
6918 = dpo_backlink->has_section_at_zero;
6921 dwarf2_per_objfile->reading_partial_symbols = 0;
6923 psymtab_to_symtab_1 (self);
6925 /* Finish up the debug error message. */
6927 printf_filtered (_("done.\n"));
6930 process_cu_includes ();
6933 /* Reading in full CUs. */
6935 /* Add PER_CU to the queue. */
6938 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6939 enum language pretend_language)
6941 struct dwarf2_queue_item *item;
6944 item = xmalloc (sizeof (*item));
6945 item->per_cu = per_cu;
6946 item->pretend_language = pretend_language;
6949 if (dwarf2_queue == NULL)
6950 dwarf2_queue = item;
6952 dwarf2_queue_tail->next = item;
6954 dwarf2_queue_tail = item;
6957 /* If PER_CU is not yet queued, add it to the queue.
6958 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
6960 The result is non-zero if PER_CU was queued, otherwise the result is zero
6961 meaning either PER_CU is already queued or it is already loaded.
6963 N.B. There is an invariant here that if a CU is queued then it is loaded.
6964 The caller is required to load PER_CU if we return non-zero. */
6967 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
6968 struct dwarf2_per_cu_data *per_cu,
6969 enum language pretend_language)
6971 /* We may arrive here during partial symbol reading, if we need full
6972 DIEs to process an unusual case (e.g. template arguments). Do
6973 not queue PER_CU, just tell our caller to load its DIEs. */
6974 if (dwarf2_per_objfile->reading_partial_symbols)
6976 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6981 /* Mark the dependence relation so that we don't flush PER_CU
6983 if (dependent_cu != NULL)
6984 dwarf2_add_dependence (dependent_cu, per_cu);
6986 /* If it's already on the queue, we have nothing to do. */
6990 /* If the compilation unit is already loaded, just mark it as
6992 if (per_cu->cu != NULL)
6994 per_cu->cu->last_used = 0;
6998 /* Add it to the queue. */
6999 queue_comp_unit (per_cu, pretend_language);
7004 /* Process the queue. */
7007 process_queue (void)
7009 struct dwarf2_queue_item *item, *next_item;
7011 if (dwarf2_read_debug)
7013 fprintf_unfiltered (gdb_stdlog,
7014 "Expanding one or more symtabs of objfile %s ...\n",
7015 objfile_name (dwarf2_per_objfile->objfile));
7018 /* The queue starts out with one item, but following a DIE reference
7019 may load a new CU, adding it to the end of the queue. */
7020 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7022 if (dwarf2_per_objfile->using_index
7023 ? !item->per_cu->v.quick->symtab
7024 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7026 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7029 if (per_cu->is_debug_types)
7031 struct signatured_type *sig_type =
7032 (struct signatured_type *) per_cu;
7034 sprintf (buf, "TU %s at offset 0x%x",
7035 hex_string (sig_type->signature), per_cu->offset.sect_off);
7038 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7040 if (dwarf2_read_debug)
7041 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7043 if (per_cu->is_debug_types)
7044 process_full_type_unit (per_cu, item->pretend_language);
7046 process_full_comp_unit (per_cu, item->pretend_language);
7048 if (dwarf2_read_debug)
7049 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7052 item->per_cu->queued = 0;
7053 next_item = item->next;
7057 dwarf2_queue_tail = NULL;
7059 if (dwarf2_read_debug)
7061 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7062 objfile_name (dwarf2_per_objfile->objfile));
7066 /* Free all allocated queue entries. This function only releases anything if
7067 an error was thrown; if the queue was processed then it would have been
7068 freed as we went along. */
7071 dwarf2_release_queue (void *dummy)
7073 struct dwarf2_queue_item *item, *last;
7075 item = dwarf2_queue;
7078 /* Anything still marked queued is likely to be in an
7079 inconsistent state, so discard it. */
7080 if (item->per_cu->queued)
7082 if (item->per_cu->cu != NULL)
7083 free_one_cached_comp_unit (item->per_cu);
7084 item->per_cu->queued = 0;
7092 dwarf2_queue = dwarf2_queue_tail = NULL;
7095 /* Read in full symbols for PST, and anything it depends on. */
7098 psymtab_to_symtab_1 (struct partial_symtab *pst)
7100 struct dwarf2_per_cu_data *per_cu;
7106 for (i = 0; i < pst->number_of_dependencies; i++)
7107 if (!pst->dependencies[i]->readin
7108 && pst->dependencies[i]->user == NULL)
7110 /* Inform about additional files that need to be read in. */
7113 /* FIXME: i18n: Need to make this a single string. */
7114 fputs_filtered (" ", gdb_stdout);
7116 fputs_filtered ("and ", gdb_stdout);
7118 printf_filtered ("%s...", pst->dependencies[i]->filename);
7119 wrap_here (""); /* Flush output. */
7120 gdb_flush (gdb_stdout);
7122 psymtab_to_symtab_1 (pst->dependencies[i]);
7125 per_cu = pst->read_symtab_private;
7129 /* It's an include file, no symbols to read for it.
7130 Everything is in the parent symtab. */
7135 dw2_do_instantiate_symtab (per_cu);
7138 /* Trivial hash function for die_info: the hash value of a DIE
7139 is its offset in .debug_info for this objfile. */
7142 die_hash (const void *item)
7144 const struct die_info *die = item;
7146 return die->offset.sect_off;
7149 /* Trivial comparison function for die_info structures: two DIEs
7150 are equal if they have the same offset. */
7153 die_eq (const void *item_lhs, const void *item_rhs)
7155 const struct die_info *die_lhs = item_lhs;
7156 const struct die_info *die_rhs = item_rhs;
7158 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7161 /* die_reader_func for load_full_comp_unit.
7162 This is identical to read_signatured_type_reader,
7163 but is kept separate for now. */
7166 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7167 const gdb_byte *info_ptr,
7168 struct die_info *comp_unit_die,
7172 struct dwarf2_cu *cu = reader->cu;
7173 enum language *language_ptr = data;
7175 gdb_assert (cu->die_hash == NULL);
7177 htab_create_alloc_ex (cu->header.length / 12,
7181 &cu->comp_unit_obstack,
7182 hashtab_obstack_allocate,
7183 dummy_obstack_deallocate);
7186 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7187 &info_ptr, comp_unit_die);
7188 cu->dies = comp_unit_die;
7189 /* comp_unit_die is not stored in die_hash, no need. */
7191 /* We try not to read any attributes in this function, because not
7192 all CUs needed for references have been loaded yet, and symbol
7193 table processing isn't initialized. But we have to set the CU language,
7194 or we won't be able to build types correctly.
7195 Similarly, if we do not read the producer, we can not apply
7196 producer-specific interpretation. */
7197 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7200 /* Load the DIEs associated with PER_CU into memory. */
7203 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7204 enum language pretend_language)
7206 gdb_assert (! this_cu->is_debug_types);
7208 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7209 load_full_comp_unit_reader, &pretend_language);
7212 /* Add a DIE to the delayed physname list. */
7215 add_to_method_list (struct type *type, int fnfield_index, int index,
7216 const char *name, struct die_info *die,
7217 struct dwarf2_cu *cu)
7219 struct delayed_method_info mi;
7221 mi.fnfield_index = fnfield_index;
7225 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7228 /* A cleanup for freeing the delayed method list. */
7231 free_delayed_list (void *ptr)
7233 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7234 if (cu->method_list != NULL)
7236 VEC_free (delayed_method_info, cu->method_list);
7237 cu->method_list = NULL;
7241 /* Compute the physnames of any methods on the CU's method list.
7243 The computation of method physnames is delayed in order to avoid the
7244 (bad) condition that one of the method's formal parameters is of an as yet
7248 compute_delayed_physnames (struct dwarf2_cu *cu)
7251 struct delayed_method_info *mi;
7252 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7254 const char *physname;
7255 struct fn_fieldlist *fn_flp
7256 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7257 physname = dwarf2_physname (mi->name, mi->die, cu);
7258 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7262 /* Go objects should be embedded in a DW_TAG_module DIE,
7263 and it's not clear if/how imported objects will appear.
7264 To keep Go support simple until that's worked out,
7265 go back through what we've read and create something usable.
7266 We could do this while processing each DIE, and feels kinda cleaner,
7267 but that way is more invasive.
7268 This is to, for example, allow the user to type "p var" or "b main"
7269 without having to specify the package name, and allow lookups
7270 of module.object to work in contexts that use the expression
7274 fixup_go_packaging (struct dwarf2_cu *cu)
7276 char *package_name = NULL;
7277 struct pending *list;
7280 for (list = global_symbols; list != NULL; list = list->next)
7282 for (i = 0; i < list->nsyms; ++i)
7284 struct symbol *sym = list->symbol[i];
7286 if (SYMBOL_LANGUAGE (sym) == language_go
7287 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7289 char *this_package_name = go_symbol_package_name (sym);
7291 if (this_package_name == NULL)
7293 if (package_name == NULL)
7294 package_name = this_package_name;
7297 if (strcmp (package_name, this_package_name) != 0)
7298 complaint (&symfile_complaints,
7299 _("Symtab %s has objects from two different Go packages: %s and %s"),
7300 (SYMBOL_SYMTAB (sym)
7301 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
7302 : objfile_name (cu->objfile)),
7303 this_package_name, package_name);
7304 xfree (this_package_name);
7310 if (package_name != NULL)
7312 struct objfile *objfile = cu->objfile;
7313 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7315 strlen (package_name));
7316 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7317 saved_package_name, objfile);
7320 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7322 sym = allocate_symbol (objfile);
7323 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7324 SYMBOL_SET_NAMES (sym, saved_package_name,
7325 strlen (saved_package_name), 0, objfile);
7326 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7327 e.g., "main" finds the "main" module and not C's main(). */
7328 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7329 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7330 SYMBOL_TYPE (sym) = type;
7332 add_symbol_to_list (sym, &global_symbols);
7334 xfree (package_name);
7338 /* Return the symtab for PER_CU. This works properly regardless of
7339 whether we're using the index or psymtabs. */
7341 static struct symtab *
7342 get_symtab (struct dwarf2_per_cu_data *per_cu)
7344 return (dwarf2_per_objfile->using_index
7345 ? per_cu->v.quick->symtab
7346 : per_cu->v.psymtab->symtab);
7349 /* A helper function for computing the list of all symbol tables
7350 included by PER_CU. */
7353 recursively_compute_inclusions (VEC (symtab_ptr) **result,
7354 htab_t all_children, htab_t all_type_symtabs,
7355 struct dwarf2_per_cu_data *per_cu,
7356 struct symtab *immediate_parent)
7360 struct symtab *symtab;
7361 struct dwarf2_per_cu_data *iter;
7363 slot = htab_find_slot (all_children, per_cu, INSERT);
7366 /* This inclusion and its children have been processed. */
7371 /* Only add a CU if it has a symbol table. */
7372 symtab = get_symtab (per_cu);
7375 /* If this is a type unit only add its symbol table if we haven't
7376 seen it yet (type unit per_cu's can share symtabs). */
7377 if (per_cu->is_debug_types)
7379 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7383 VEC_safe_push (symtab_ptr, *result, symtab);
7384 if (symtab->user == NULL)
7385 symtab->user = immediate_parent;
7390 VEC_safe_push (symtab_ptr, *result, symtab);
7391 if (symtab->user == NULL)
7392 symtab->user = immediate_parent;
7397 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7400 recursively_compute_inclusions (result, all_children,
7401 all_type_symtabs, iter, symtab);
7405 /* Compute the symtab 'includes' fields for the symtab related to
7409 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7411 gdb_assert (! per_cu->is_debug_types);
7413 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7416 struct dwarf2_per_cu_data *per_cu_iter;
7417 struct symtab *symtab_iter;
7418 VEC (symtab_ptr) *result_symtabs = NULL;
7419 htab_t all_children, all_type_symtabs;
7420 struct symtab *symtab = get_symtab (per_cu);
7422 /* If we don't have a symtab, we can just skip this case. */
7426 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7427 NULL, xcalloc, xfree);
7428 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7429 NULL, xcalloc, xfree);
7432 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7436 recursively_compute_inclusions (&result_symtabs, all_children,
7437 all_type_symtabs, per_cu_iter,
7441 /* Now we have a transitive closure of all the included symtabs. */
7442 len = VEC_length (symtab_ptr, result_symtabs);
7444 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7445 (len + 1) * sizeof (struct symtab *));
7447 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
7449 symtab->includes[ix] = symtab_iter;
7450 symtab->includes[len] = NULL;
7452 VEC_free (symtab_ptr, result_symtabs);
7453 htab_delete (all_children);
7454 htab_delete (all_type_symtabs);
7458 /* Compute the 'includes' field for the symtabs of all the CUs we just
7462 process_cu_includes (void)
7465 struct dwarf2_per_cu_data *iter;
7468 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7472 if (! iter->is_debug_types)
7473 compute_symtab_includes (iter);
7476 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7479 /* Generate full symbol information for PER_CU, whose DIEs have
7480 already been loaded into memory. */
7483 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7484 enum language pretend_language)
7486 struct dwarf2_cu *cu = per_cu->cu;
7487 struct objfile *objfile = per_cu->objfile;
7488 CORE_ADDR lowpc, highpc;
7489 struct symtab *symtab;
7490 struct cleanup *back_to, *delayed_list_cleanup;
7492 struct block *static_block;
7494 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7497 back_to = make_cleanup (really_free_pendings, NULL);
7498 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7500 cu->list_in_scope = &file_symbols;
7502 cu->language = pretend_language;
7503 cu->language_defn = language_def (cu->language);
7505 /* Do line number decoding in read_file_scope () */
7506 process_die (cu->dies, cu);
7508 /* For now fudge the Go package. */
7509 if (cu->language == language_go)
7510 fixup_go_packaging (cu);
7512 /* Now that we have processed all the DIEs in the CU, all the types
7513 should be complete, and it should now be safe to compute all of the
7515 compute_delayed_physnames (cu);
7516 do_cleanups (delayed_list_cleanup);
7518 /* Some compilers don't define a DW_AT_high_pc attribute for the
7519 compilation unit. If the DW_AT_high_pc is missing, synthesize
7520 it, by scanning the DIE's below the compilation unit. */
7521 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7524 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
7526 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7527 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7528 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7529 addrmap to help ensure it has an accurate map of pc values belonging to
7531 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7533 symtab = end_symtab_from_static_block (static_block, objfile,
7534 SECT_OFF_TEXT (objfile), 0);
7538 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7540 /* Set symtab language to language from DW_AT_language. If the
7541 compilation is from a C file generated by language preprocessors, do
7542 not set the language if it was already deduced by start_subfile. */
7543 if (!(cu->language == language_c && symtab->language != language_c))
7544 symtab->language = cu->language;
7546 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7547 produce DW_AT_location with location lists but it can be possibly
7548 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7549 there were bugs in prologue debug info, fixed later in GCC-4.5
7550 by "unwind info for epilogues" patch (which is not directly related).
7552 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7553 needed, it would be wrong due to missing DW_AT_producer there.
7555 Still one can confuse GDB by using non-standard GCC compilation
7556 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7558 if (cu->has_loclist && gcc_4_minor >= 5)
7559 symtab->locations_valid = 1;
7561 if (gcc_4_minor >= 5)
7562 symtab->epilogue_unwind_valid = 1;
7564 symtab->call_site_htab = cu->call_site_htab;
7567 if (dwarf2_per_objfile->using_index)
7568 per_cu->v.quick->symtab = symtab;
7571 struct partial_symtab *pst = per_cu->v.psymtab;
7572 pst->symtab = symtab;
7576 /* Push it for inclusion processing later. */
7577 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7579 do_cleanups (back_to);
7582 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7583 already been loaded into memory. */
7586 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7587 enum language pretend_language)
7589 struct dwarf2_cu *cu = per_cu->cu;
7590 struct objfile *objfile = per_cu->objfile;
7591 struct symtab *symtab;
7592 struct cleanup *back_to, *delayed_list_cleanup;
7593 struct signatured_type *sig_type;
7595 gdb_assert (per_cu->is_debug_types);
7596 sig_type = (struct signatured_type *) per_cu;
7599 back_to = make_cleanup (really_free_pendings, NULL);
7600 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7602 cu->list_in_scope = &file_symbols;
7604 cu->language = pretend_language;
7605 cu->language_defn = language_def (cu->language);
7607 /* The symbol tables are set up in read_type_unit_scope. */
7608 process_die (cu->dies, cu);
7610 /* For now fudge the Go package. */
7611 if (cu->language == language_go)
7612 fixup_go_packaging (cu);
7614 /* Now that we have processed all the DIEs in the CU, all the types
7615 should be complete, and it should now be safe to compute all of the
7617 compute_delayed_physnames (cu);
7618 do_cleanups (delayed_list_cleanup);
7620 /* TUs share symbol tables.
7621 If this is the first TU to use this symtab, complete the construction
7622 of it with end_expandable_symtab. Otherwise, complete the addition of
7623 this TU's symbols to the existing symtab. */
7624 if (sig_type->type_unit_group->primary_symtab == NULL)
7626 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7627 sig_type->type_unit_group->primary_symtab = symtab;
7631 /* Set symtab language to language from DW_AT_language. If the
7632 compilation is from a C file generated by language preprocessors,
7633 do not set the language if it was already deduced by
7635 if (!(cu->language == language_c && symtab->language != language_c))
7636 symtab->language = cu->language;
7641 augment_type_symtab (objfile,
7642 sig_type->type_unit_group->primary_symtab);
7643 symtab = sig_type->type_unit_group->primary_symtab;
7646 if (dwarf2_per_objfile->using_index)
7647 per_cu->v.quick->symtab = symtab;
7650 struct partial_symtab *pst = per_cu->v.psymtab;
7651 pst->symtab = symtab;
7655 do_cleanups (back_to);
7658 /* Process an imported unit DIE. */
7661 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7663 struct attribute *attr;
7665 /* For now we don't handle imported units in type units. */
7666 if (cu->per_cu->is_debug_types)
7668 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7669 " supported in type units [in module %s]"),
7670 objfile_name (cu->objfile));
7673 attr = dwarf2_attr (die, DW_AT_import, cu);
7676 struct dwarf2_per_cu_data *per_cu;
7677 struct symtab *imported_symtab;
7681 offset = dwarf2_get_ref_die_offset (attr);
7682 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7683 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7685 /* If necessary, add it to the queue and load its DIEs. */
7686 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7687 load_full_comp_unit (per_cu, cu->language);
7689 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7694 /* Process a die and its children. */
7697 process_die (struct die_info *die, struct dwarf2_cu *cu)
7701 case DW_TAG_padding:
7703 case DW_TAG_compile_unit:
7704 case DW_TAG_partial_unit:
7705 read_file_scope (die, cu);
7707 case DW_TAG_type_unit:
7708 read_type_unit_scope (die, cu);
7710 case DW_TAG_subprogram:
7711 case DW_TAG_inlined_subroutine:
7712 read_func_scope (die, cu);
7714 case DW_TAG_lexical_block:
7715 case DW_TAG_try_block:
7716 case DW_TAG_catch_block:
7717 read_lexical_block_scope (die, cu);
7719 case DW_TAG_GNU_call_site:
7720 read_call_site_scope (die, cu);
7722 case DW_TAG_class_type:
7723 case DW_TAG_interface_type:
7724 case DW_TAG_structure_type:
7725 case DW_TAG_union_type:
7726 process_structure_scope (die, cu);
7728 case DW_TAG_enumeration_type:
7729 process_enumeration_scope (die, cu);
7732 /* These dies have a type, but processing them does not create
7733 a symbol or recurse to process the children. Therefore we can
7734 read them on-demand through read_type_die. */
7735 case DW_TAG_subroutine_type:
7736 case DW_TAG_set_type:
7737 case DW_TAG_array_type:
7738 case DW_TAG_pointer_type:
7739 case DW_TAG_ptr_to_member_type:
7740 case DW_TAG_reference_type:
7741 case DW_TAG_string_type:
7744 case DW_TAG_base_type:
7745 case DW_TAG_subrange_type:
7746 case DW_TAG_typedef:
7747 /* Add a typedef symbol for the type definition, if it has a
7749 new_symbol (die, read_type_die (die, cu), cu);
7751 case DW_TAG_common_block:
7752 read_common_block (die, cu);
7754 case DW_TAG_common_inclusion:
7756 case DW_TAG_namespace:
7757 cu->processing_has_namespace_info = 1;
7758 read_namespace (die, cu);
7761 cu->processing_has_namespace_info = 1;
7762 read_module (die, cu);
7764 case DW_TAG_imported_declaration:
7765 case DW_TAG_imported_module:
7766 cu->processing_has_namespace_info = 1;
7767 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7768 || cu->language != language_fortran))
7769 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7770 dwarf_tag_name (die->tag));
7771 read_import_statement (die, cu);
7774 case DW_TAG_imported_unit:
7775 process_imported_unit_die (die, cu);
7779 new_symbol (die, NULL, cu);
7784 /* DWARF name computation. */
7786 /* A helper function for dwarf2_compute_name which determines whether DIE
7787 needs to have the name of the scope prepended to the name listed in the
7791 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7793 struct attribute *attr;
7797 case DW_TAG_namespace:
7798 case DW_TAG_typedef:
7799 case DW_TAG_class_type:
7800 case DW_TAG_interface_type:
7801 case DW_TAG_structure_type:
7802 case DW_TAG_union_type:
7803 case DW_TAG_enumeration_type:
7804 case DW_TAG_enumerator:
7805 case DW_TAG_subprogram:
7809 case DW_TAG_variable:
7810 case DW_TAG_constant:
7811 /* We only need to prefix "globally" visible variables. These include
7812 any variable marked with DW_AT_external or any variable that
7813 lives in a namespace. [Variables in anonymous namespaces
7814 require prefixing, but they are not DW_AT_external.] */
7816 if (dwarf2_attr (die, DW_AT_specification, cu))
7818 struct dwarf2_cu *spec_cu = cu;
7820 return die_needs_namespace (die_specification (die, &spec_cu),
7824 attr = dwarf2_attr (die, DW_AT_external, cu);
7825 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7826 && die->parent->tag != DW_TAG_module)
7828 /* A variable in a lexical block of some kind does not need a
7829 namespace, even though in C++ such variables may be external
7830 and have a mangled name. */
7831 if (die->parent->tag == DW_TAG_lexical_block
7832 || die->parent->tag == DW_TAG_try_block
7833 || die->parent->tag == DW_TAG_catch_block
7834 || die->parent->tag == DW_TAG_subprogram)
7843 /* Retrieve the last character from a mem_file. */
7846 do_ui_file_peek_last (void *object, const char *buffer, long length)
7848 char *last_char_p = (char *) object;
7851 *last_char_p = buffer[length - 1];
7854 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7855 compute the physname for the object, which include a method's:
7856 - formal parameters (C++/Java),
7857 - receiver type (Go),
7858 - return type (Java).
7860 The term "physname" is a bit confusing.
7861 For C++, for example, it is the demangled name.
7862 For Go, for example, it's the mangled name.
7864 For Ada, return the DIE's linkage name rather than the fully qualified
7865 name. PHYSNAME is ignored..
7867 The result is allocated on the objfile_obstack and canonicalized. */
7870 dwarf2_compute_name (const char *name,
7871 struct die_info *die, struct dwarf2_cu *cu,
7874 struct objfile *objfile = cu->objfile;
7877 name = dwarf2_name (die, cu);
7879 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7880 compute it by typename_concat inside GDB. */
7881 if (cu->language == language_ada
7882 || (cu->language == language_fortran && physname))
7884 /* For Ada unit, we prefer the linkage name over the name, as
7885 the former contains the exported name, which the user expects
7886 to be able to reference. Ideally, we want the user to be able
7887 to reference this entity using either natural or linkage name,
7888 but we haven't started looking at this enhancement yet. */
7889 struct attribute *attr;
7891 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7893 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7894 if (attr && DW_STRING (attr))
7895 return DW_STRING (attr);
7898 /* These are the only languages we know how to qualify names in. */
7900 && (cu->language == language_cplus || cu->language == language_java
7901 || cu->language == language_fortran))
7903 if (die_needs_namespace (die, cu))
7907 struct ui_file *buf;
7909 prefix = determine_prefix (die, cu);
7910 buf = mem_fileopen ();
7911 if (*prefix != '\0')
7913 char *prefixed_name = typename_concat (NULL, prefix, name,
7916 fputs_unfiltered (prefixed_name, buf);
7917 xfree (prefixed_name);
7920 fputs_unfiltered (name, buf);
7922 /* Template parameters may be specified in the DIE's DW_AT_name, or
7923 as children with DW_TAG_template_type_param or
7924 DW_TAG_value_type_param. If the latter, add them to the name
7925 here. If the name already has template parameters, then
7926 skip this step; some versions of GCC emit both, and
7927 it is more efficient to use the pre-computed name.
7929 Something to keep in mind about this process: it is very
7930 unlikely, or in some cases downright impossible, to produce
7931 something that will match the mangled name of a function.
7932 If the definition of the function has the same debug info,
7933 we should be able to match up with it anyway. But fallbacks
7934 using the minimal symbol, for instance to find a method
7935 implemented in a stripped copy of libstdc++, will not work.
7936 If we do not have debug info for the definition, we will have to
7937 match them up some other way.
7939 When we do name matching there is a related problem with function
7940 templates; two instantiated function templates are allowed to
7941 differ only by their return types, which we do not add here. */
7943 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7945 struct attribute *attr;
7946 struct die_info *child;
7949 die->building_fullname = 1;
7951 for (child = die->child; child != NULL; child = child->sibling)
7955 const gdb_byte *bytes;
7956 struct dwarf2_locexpr_baton *baton;
7959 if (child->tag != DW_TAG_template_type_param
7960 && child->tag != DW_TAG_template_value_param)
7965 fputs_unfiltered ("<", buf);
7969 fputs_unfiltered (", ", buf);
7971 attr = dwarf2_attr (child, DW_AT_type, cu);
7974 complaint (&symfile_complaints,
7975 _("template parameter missing DW_AT_type"));
7976 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7979 type = die_type (child, cu);
7981 if (child->tag == DW_TAG_template_type_param)
7983 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7987 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7990 complaint (&symfile_complaints,
7991 _("template parameter missing "
7992 "DW_AT_const_value"));
7993 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7997 dwarf2_const_value_attr (attr, type, name,
7998 &cu->comp_unit_obstack, cu,
7999 &value, &bytes, &baton);
8001 if (TYPE_NOSIGN (type))
8002 /* GDB prints characters as NUMBER 'CHAR'. If that's
8003 changed, this can use value_print instead. */
8004 c_printchar (value, type, buf);
8007 struct value_print_options opts;
8010 v = dwarf2_evaluate_loc_desc (type, NULL,
8014 else if (bytes != NULL)
8016 v = allocate_value (type);
8017 memcpy (value_contents_writeable (v), bytes,
8018 TYPE_LENGTH (type));
8021 v = value_from_longest (type, value);
8023 /* Specify decimal so that we do not depend on
8025 get_formatted_print_options (&opts, 'd');
8027 value_print (v, buf, &opts);
8033 die->building_fullname = 0;
8037 /* Close the argument list, with a space if necessary
8038 (nested templates). */
8039 char last_char = '\0';
8040 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8041 if (last_char == '>')
8042 fputs_unfiltered (" >", buf);
8044 fputs_unfiltered (">", buf);
8048 /* For Java and C++ methods, append formal parameter type
8049 information, if PHYSNAME. */
8051 if (physname && die->tag == DW_TAG_subprogram
8052 && (cu->language == language_cplus
8053 || cu->language == language_java))
8055 struct type *type = read_type_die (die, cu);
8057 c_type_print_args (type, buf, 1, cu->language,
8058 &type_print_raw_options);
8060 if (cu->language == language_java)
8062 /* For java, we must append the return type to method
8064 if (die->tag == DW_TAG_subprogram)
8065 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8066 0, 0, &type_print_raw_options);
8068 else if (cu->language == language_cplus)
8070 /* Assume that an artificial first parameter is
8071 "this", but do not crash if it is not. RealView
8072 marks unnamed (and thus unused) parameters as
8073 artificial; there is no way to differentiate
8075 if (TYPE_NFIELDS (type) > 0
8076 && TYPE_FIELD_ARTIFICIAL (type, 0)
8077 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8078 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8080 fputs_unfiltered (" const", buf);
8084 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
8086 ui_file_delete (buf);
8088 if (cu->language == language_cplus)
8091 = dwarf2_canonicalize_name (name, cu,
8092 &objfile->objfile_obstack);
8103 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8104 If scope qualifiers are appropriate they will be added. The result
8105 will be allocated on the objfile_obstack, or NULL if the DIE does
8106 not have a name. NAME may either be from a previous call to
8107 dwarf2_name or NULL.
8109 The output string will be canonicalized (if C++/Java). */
8112 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8114 return dwarf2_compute_name (name, die, cu, 0);
8117 /* Construct a physname for the given DIE in CU. NAME may either be
8118 from a previous call to dwarf2_name or NULL. The result will be
8119 allocated on the objfile_objstack or NULL if the DIE does not have a
8122 The output string will be canonicalized (if C++/Java). */
8125 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8127 struct objfile *objfile = cu->objfile;
8128 struct attribute *attr;
8129 const char *retval, *mangled = NULL, *canon = NULL;
8130 struct cleanup *back_to;
8133 /* In this case dwarf2_compute_name is just a shortcut not building anything
8135 if (!die_needs_namespace (die, cu))
8136 return dwarf2_compute_name (name, die, cu, 1);
8138 back_to = make_cleanup (null_cleanup, NULL);
8140 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8142 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8144 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8146 if (attr && DW_STRING (attr))
8150 mangled = DW_STRING (attr);
8152 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8153 type. It is easier for GDB users to search for such functions as
8154 `name(params)' than `long name(params)'. In such case the minimal
8155 symbol names do not match the full symbol names but for template
8156 functions there is never a need to look up their definition from their
8157 declaration so the only disadvantage remains the minimal symbol
8158 variant `long name(params)' does not have the proper inferior type.
8161 if (cu->language == language_go)
8163 /* This is a lie, but we already lie to the caller new_symbol_full.
8164 new_symbol_full assumes we return the mangled name.
8165 This just undoes that lie until things are cleaned up. */
8170 demangled = gdb_demangle (mangled,
8171 (DMGL_PARAMS | DMGL_ANSI
8172 | (cu->language == language_java
8173 ? DMGL_JAVA | DMGL_RET_POSTFIX
8178 make_cleanup (xfree, demangled);
8188 if (canon == NULL || check_physname)
8190 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8192 if (canon != NULL && strcmp (physname, canon) != 0)
8194 /* It may not mean a bug in GDB. The compiler could also
8195 compute DW_AT_linkage_name incorrectly. But in such case
8196 GDB would need to be bug-to-bug compatible. */
8198 complaint (&symfile_complaints,
8199 _("Computed physname <%s> does not match demangled <%s> "
8200 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8201 physname, canon, mangled, die->offset.sect_off,
8202 objfile_name (objfile));
8204 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8205 is available here - over computed PHYSNAME. It is safer
8206 against both buggy GDB and buggy compilers. */
8220 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
8222 do_cleanups (back_to);
8226 /* Read the import statement specified by the given die and record it. */
8229 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8231 struct objfile *objfile = cu->objfile;
8232 struct attribute *import_attr;
8233 struct die_info *imported_die, *child_die;
8234 struct dwarf2_cu *imported_cu;
8235 const char *imported_name;
8236 const char *imported_name_prefix;
8237 const char *canonical_name;
8238 const char *import_alias;
8239 const char *imported_declaration = NULL;
8240 const char *import_prefix;
8241 VEC (const_char_ptr) *excludes = NULL;
8242 struct cleanup *cleanups;
8244 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8245 if (import_attr == NULL)
8247 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8248 dwarf_tag_name (die->tag));
8253 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8254 imported_name = dwarf2_name (imported_die, imported_cu);
8255 if (imported_name == NULL)
8257 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8259 The import in the following code:
8273 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8274 <52> DW_AT_decl_file : 1
8275 <53> DW_AT_decl_line : 6
8276 <54> DW_AT_import : <0x75>
8277 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8279 <5b> DW_AT_decl_file : 1
8280 <5c> DW_AT_decl_line : 2
8281 <5d> DW_AT_type : <0x6e>
8283 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8284 <76> DW_AT_byte_size : 4
8285 <77> DW_AT_encoding : 5 (signed)
8287 imports the wrong die ( 0x75 instead of 0x58 ).
8288 This case will be ignored until the gcc bug is fixed. */
8292 /* Figure out the local name after import. */
8293 import_alias = dwarf2_name (die, cu);
8295 /* Figure out where the statement is being imported to. */
8296 import_prefix = determine_prefix (die, cu);
8298 /* Figure out what the scope of the imported die is and prepend it
8299 to the name of the imported die. */
8300 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8302 if (imported_die->tag != DW_TAG_namespace
8303 && imported_die->tag != DW_TAG_module)
8305 imported_declaration = imported_name;
8306 canonical_name = imported_name_prefix;
8308 else if (strlen (imported_name_prefix) > 0)
8309 canonical_name = obconcat (&objfile->objfile_obstack,
8310 imported_name_prefix, "::", imported_name,
8313 canonical_name = imported_name;
8315 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8317 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8318 for (child_die = die->child; child_die && child_die->tag;
8319 child_die = sibling_die (child_die))
8321 /* DWARF-4: A Fortran use statement with a “rename list” may be
8322 represented by an imported module entry with an import attribute
8323 referring to the module and owned entries corresponding to those
8324 entities that are renamed as part of being imported. */
8326 if (child_die->tag != DW_TAG_imported_declaration)
8328 complaint (&symfile_complaints,
8329 _("child DW_TAG_imported_declaration expected "
8330 "- DIE at 0x%x [in module %s]"),
8331 child_die->offset.sect_off, objfile_name (objfile));
8335 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8336 if (import_attr == NULL)
8338 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8339 dwarf_tag_name (child_die->tag));
8344 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8346 imported_name = dwarf2_name (imported_die, imported_cu);
8347 if (imported_name == NULL)
8349 complaint (&symfile_complaints,
8350 _("child DW_TAG_imported_declaration has unknown "
8351 "imported name - DIE at 0x%x [in module %s]"),
8352 child_die->offset.sect_off, objfile_name (objfile));
8356 VEC_safe_push (const_char_ptr, excludes, imported_name);
8358 process_die (child_die, cu);
8361 cp_add_using_directive (import_prefix,
8364 imported_declaration,
8367 &objfile->objfile_obstack);
8369 do_cleanups (cleanups);
8372 /* Cleanup function for handle_DW_AT_stmt_list. */
8375 free_cu_line_header (void *arg)
8377 struct dwarf2_cu *cu = arg;
8379 free_line_header (cu->line_header);
8380 cu->line_header = NULL;
8383 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8384 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8385 this, it was first present in GCC release 4.3.0. */
8388 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8390 if (!cu->checked_producer)
8391 check_producer (cu);
8393 return cu->producer_is_gcc_lt_4_3;
8397 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
8398 const char **name, const char **comp_dir)
8400 struct attribute *attr;
8405 /* Find the filename. Do not use dwarf2_name here, since the filename
8406 is not a source language identifier. */
8407 attr = dwarf2_attr (die, DW_AT_name, cu);
8410 *name = DW_STRING (attr);
8413 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8415 *comp_dir = DW_STRING (attr);
8416 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8417 && IS_ABSOLUTE_PATH (*name))
8419 char *d = ldirname (*name);
8423 make_cleanup (xfree, d);
8425 if (*comp_dir != NULL)
8427 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8428 directory, get rid of it. */
8429 char *cp = strchr (*comp_dir, ':');
8431 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8436 *name = "<unknown>";
8439 /* Handle DW_AT_stmt_list for a compilation unit.
8440 DIE is the DW_TAG_compile_unit die for CU.
8441 COMP_DIR is the compilation directory.
8442 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8445 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8446 const char *comp_dir) /* ARI: editCase function */
8448 struct attribute *attr;
8450 gdb_assert (! cu->per_cu->is_debug_types);
8452 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8455 unsigned int line_offset = DW_UNSND (attr);
8456 struct line_header *line_header
8457 = dwarf_decode_line_header (line_offset, cu);
8461 cu->line_header = line_header;
8462 make_cleanup (free_cu_line_header, cu);
8463 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8468 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8471 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8473 struct objfile *objfile = dwarf2_per_objfile->objfile;
8474 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8475 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8476 CORE_ADDR highpc = ((CORE_ADDR) 0);
8477 struct attribute *attr;
8478 const char *name = NULL;
8479 const char *comp_dir = NULL;
8480 struct die_info *child_die;
8481 bfd *abfd = objfile->obfd;
8484 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8486 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8488 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8489 from finish_block. */
8490 if (lowpc == ((CORE_ADDR) -1))
8495 find_file_and_directory (die, cu, &name, &comp_dir);
8497 prepare_one_comp_unit (cu, die, cu->language);
8499 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8500 standardised yet. As a workaround for the language detection we fall
8501 back to the DW_AT_producer string. */
8502 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8503 cu->language = language_opencl;
8505 /* Similar hack for Go. */
8506 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8507 set_cu_language (DW_LANG_Go, cu);
8509 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8511 /* Decode line number information if present. We do this before
8512 processing child DIEs, so that the line header table is available
8513 for DW_AT_decl_file. */
8514 handle_DW_AT_stmt_list (die, cu, comp_dir);
8516 /* Process all dies in compilation unit. */
8517 if (die->child != NULL)
8519 child_die = die->child;
8520 while (child_die && child_die->tag)
8522 process_die (child_die, cu);
8523 child_die = sibling_die (child_die);
8527 /* Decode macro information, if present. Dwarf 2 macro information
8528 refers to information in the line number info statement program
8529 header, so we can only read it if we've read the header
8531 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8532 if (attr && cu->line_header)
8534 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8535 complaint (&symfile_complaints,
8536 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8538 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8542 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8543 if (attr && cu->line_header)
8545 unsigned int macro_offset = DW_UNSND (attr);
8547 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8551 do_cleanups (back_to);
8554 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8555 Create the set of symtabs used by this TU, or if this TU is sharing
8556 symtabs with another TU and the symtabs have already been created
8557 then restore those symtabs in the line header.
8558 We don't need the pc/line-number mapping for type units. */
8561 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8563 struct objfile *objfile = dwarf2_per_objfile->objfile;
8564 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8565 struct type_unit_group *tu_group;
8567 struct line_header *lh;
8568 struct attribute *attr;
8569 unsigned int i, line_offset;
8570 struct signatured_type *sig_type;
8572 gdb_assert (per_cu->is_debug_types);
8573 sig_type = (struct signatured_type *) per_cu;
8575 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8577 /* If we're using .gdb_index (includes -readnow) then
8578 per_cu->type_unit_group may not have been set up yet. */
8579 if (sig_type->type_unit_group == NULL)
8580 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8581 tu_group = sig_type->type_unit_group;
8583 /* If we've already processed this stmt_list there's no real need to
8584 do it again, we could fake it and just recreate the part we need
8585 (file name,index -> symtab mapping). If data shows this optimization
8586 is useful we can do it then. */
8587 first_time = tu_group->primary_symtab == NULL;
8589 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8594 line_offset = DW_UNSND (attr);
8595 lh = dwarf_decode_line_header (line_offset, cu);
8600 dwarf2_start_symtab (cu, "", NULL, 0);
8603 gdb_assert (tu_group->symtabs == NULL);
8606 /* Note: The primary symtab will get allocated at the end. */
8610 cu->line_header = lh;
8611 make_cleanup (free_cu_line_header, cu);
8615 dwarf2_start_symtab (cu, "", NULL, 0);
8617 tu_group->num_symtabs = lh->num_file_names;
8618 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8620 for (i = 0; i < lh->num_file_names; ++i)
8622 const char *dir = NULL;
8623 struct file_entry *fe = &lh->file_names[i];
8626 dir = lh->include_dirs[fe->dir_index - 1];
8627 dwarf2_start_subfile (fe->name, dir, NULL);
8629 /* Note: We don't have to watch for the main subfile here, type units
8630 don't have DW_AT_name. */
8632 if (current_subfile->symtab == NULL)
8634 /* NOTE: start_subfile will recognize when it's been passed
8635 a file it has already seen. So we can't assume there's a
8636 simple mapping from lh->file_names to subfiles,
8637 lh->file_names may contain dups. */
8638 current_subfile->symtab = allocate_symtab (current_subfile->name,
8642 fe->symtab = current_subfile->symtab;
8643 tu_group->symtabs[i] = fe->symtab;
8650 for (i = 0; i < lh->num_file_names; ++i)
8652 struct file_entry *fe = &lh->file_names[i];
8654 fe->symtab = tu_group->symtabs[i];
8658 /* The main symtab is allocated last. Type units don't have DW_AT_name
8659 so they don't have a "real" (so to speak) symtab anyway.
8660 There is later code that will assign the main symtab to all symbols
8661 that don't have one. We need to handle the case of a symbol with a
8662 missing symtab (DW_AT_decl_file) anyway. */
8665 /* Process DW_TAG_type_unit.
8666 For TUs we want to skip the first top level sibling if it's not the
8667 actual type being defined by this TU. In this case the first top
8668 level sibling is there to provide context only. */
8671 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8673 struct die_info *child_die;
8675 prepare_one_comp_unit (cu, die, language_minimal);
8677 /* Initialize (or reinitialize) the machinery for building symtabs.
8678 We do this before processing child DIEs, so that the line header table
8679 is available for DW_AT_decl_file. */
8680 setup_type_unit_groups (die, cu);
8682 if (die->child != NULL)
8684 child_die = die->child;
8685 while (child_die && child_die->tag)
8687 process_die (child_die, cu);
8688 child_die = sibling_die (child_die);
8695 http://gcc.gnu.org/wiki/DebugFission
8696 http://gcc.gnu.org/wiki/DebugFissionDWP
8698 To simplify handling of both DWO files ("object" files with the DWARF info)
8699 and DWP files (a file with the DWOs packaged up into one file), we treat
8700 DWP files as having a collection of virtual DWO files. */
8703 hash_dwo_file (const void *item)
8705 const struct dwo_file *dwo_file = item;
8708 hash = htab_hash_string (dwo_file->dwo_name);
8709 if (dwo_file->comp_dir != NULL)
8710 hash += htab_hash_string (dwo_file->comp_dir);
8715 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8717 const struct dwo_file *lhs = item_lhs;
8718 const struct dwo_file *rhs = item_rhs;
8720 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8722 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8723 return lhs->comp_dir == rhs->comp_dir;
8724 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
8727 /* Allocate a hash table for DWO files. */
8730 allocate_dwo_file_hash_table (void)
8732 struct objfile *objfile = dwarf2_per_objfile->objfile;
8734 return htab_create_alloc_ex (41,
8738 &objfile->objfile_obstack,
8739 hashtab_obstack_allocate,
8740 dummy_obstack_deallocate);
8743 /* Lookup DWO file DWO_NAME. */
8746 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8748 struct dwo_file find_entry;
8751 if (dwarf2_per_objfile->dwo_files == NULL)
8752 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8754 memset (&find_entry, 0, sizeof (find_entry));
8755 find_entry.dwo_name = dwo_name;
8756 find_entry.comp_dir = comp_dir;
8757 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8763 hash_dwo_unit (const void *item)
8765 const struct dwo_unit *dwo_unit = item;
8767 /* This drops the top 32 bits of the id, but is ok for a hash. */
8768 return dwo_unit->signature;
8772 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8774 const struct dwo_unit *lhs = item_lhs;
8775 const struct dwo_unit *rhs = item_rhs;
8777 /* The signature is assumed to be unique within the DWO file.
8778 So while object file CU dwo_id's always have the value zero,
8779 that's OK, assuming each object file DWO file has only one CU,
8780 and that's the rule for now. */
8781 return lhs->signature == rhs->signature;
8784 /* Allocate a hash table for DWO CUs,TUs.
8785 There is one of these tables for each of CUs,TUs for each DWO file. */
8788 allocate_dwo_unit_table (struct objfile *objfile)
8790 /* Start out with a pretty small number.
8791 Generally DWO files contain only one CU and maybe some TUs. */
8792 return htab_create_alloc_ex (3,
8796 &objfile->objfile_obstack,
8797 hashtab_obstack_allocate,
8798 dummy_obstack_deallocate);
8801 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8803 struct create_dwo_cu_data
8805 struct dwo_file *dwo_file;
8806 struct dwo_unit dwo_unit;
8809 /* die_reader_func for create_dwo_cu. */
8812 create_dwo_cu_reader (const struct die_reader_specs *reader,
8813 const gdb_byte *info_ptr,
8814 struct die_info *comp_unit_die,
8818 struct dwarf2_cu *cu = reader->cu;
8819 struct objfile *objfile = dwarf2_per_objfile->objfile;
8820 sect_offset offset = cu->per_cu->offset;
8821 struct dwarf2_section_info *section = cu->per_cu->section;
8822 struct create_dwo_cu_data *data = datap;
8823 struct dwo_file *dwo_file = data->dwo_file;
8824 struct dwo_unit *dwo_unit = &data->dwo_unit;
8825 struct attribute *attr;
8827 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8830 complaint (&symfile_complaints,
8831 _("Dwarf Error: debug entry at offset 0x%x is missing"
8832 " its dwo_id [in module %s]"),
8833 offset.sect_off, dwo_file->dwo_name);
8837 dwo_unit->dwo_file = dwo_file;
8838 dwo_unit->signature = DW_UNSND (attr);
8839 dwo_unit->section = section;
8840 dwo_unit->offset = offset;
8841 dwo_unit->length = cu->per_cu->length;
8843 if (dwarf2_read_debug)
8844 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8845 offset.sect_off, hex_string (dwo_unit->signature));
8848 /* Create the dwo_unit for the lone CU in DWO_FILE.
8849 Note: This function processes DWO files only, not DWP files. */
8851 static struct dwo_unit *
8852 create_dwo_cu (struct dwo_file *dwo_file)
8854 struct objfile *objfile = dwarf2_per_objfile->objfile;
8855 struct dwarf2_section_info *section = &dwo_file->sections.info;
8858 const gdb_byte *info_ptr, *end_ptr;
8859 struct create_dwo_cu_data create_dwo_cu_data;
8860 struct dwo_unit *dwo_unit;
8862 dwarf2_read_section (objfile, section);
8863 info_ptr = section->buffer;
8865 if (info_ptr == NULL)
8868 /* We can't set abfd until now because the section may be empty or
8869 not present, in which case section->asection will be NULL. */
8870 abfd = section->asection->owner;
8872 if (dwarf2_read_debug)
8874 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8875 bfd_section_name (abfd, section->asection),
8876 bfd_get_filename (abfd));
8879 create_dwo_cu_data.dwo_file = dwo_file;
8882 end_ptr = info_ptr + section->size;
8883 while (info_ptr < end_ptr)
8885 struct dwarf2_per_cu_data per_cu;
8887 memset (&create_dwo_cu_data.dwo_unit, 0,
8888 sizeof (create_dwo_cu_data.dwo_unit));
8889 memset (&per_cu, 0, sizeof (per_cu));
8890 per_cu.objfile = objfile;
8891 per_cu.is_debug_types = 0;
8892 per_cu.offset.sect_off = info_ptr - section->buffer;
8893 per_cu.section = section;
8895 init_cutu_and_read_dies_no_follow (&per_cu,
8896 &dwo_file->sections.abbrev,
8898 create_dwo_cu_reader,
8899 &create_dwo_cu_data);
8901 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8903 /* If we've already found one, complain. We only support one
8904 because having more than one requires hacking the dwo_name of
8905 each to match, which is highly unlikely to happen. */
8906 if (dwo_unit != NULL)
8908 complaint (&symfile_complaints,
8909 _("Multiple CUs in DWO file %s [in module %s]"),
8910 dwo_file->dwo_name, objfile_name (objfile));
8914 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8915 *dwo_unit = create_dwo_cu_data.dwo_unit;
8918 info_ptr += per_cu.length;
8924 /* DWP file .debug_{cu,tu}_index section format:
8925 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8929 Both index sections have the same format, and serve to map a 64-bit
8930 signature to a set of section numbers. Each section begins with a header,
8931 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8932 indexes, and a pool of 32-bit section numbers. The index sections will be
8933 aligned at 8-byte boundaries in the file.
8935 The index section header consists of:
8937 V, 32 bit version number
8939 N, 32 bit number of compilation units or type units in the index
8940 M, 32 bit number of slots in the hash table
8942 Numbers are recorded using the byte order of the application binary.
8944 We assume that N and M will not exceed 2^32 - 1.
8946 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8948 The hash table begins at offset 16 in the section, and consists of an array
8949 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8950 order of the application binary). Unused slots in the hash table are 0.
8951 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8953 The parallel table begins immediately after the hash table
8954 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8955 array of 32-bit indexes (using the byte order of the application binary),
8956 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8957 table contains a 32-bit index into the pool of section numbers. For unused
8958 hash table slots, the corresponding entry in the parallel table will be 0.
8960 Given a 64-bit compilation unit signature or a type signature S, an entry
8961 in the hash table is located as follows:
8963 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8964 the low-order k bits all set to 1.
8966 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8968 3) If the hash table entry at index H matches the signature, use that
8969 entry. If the hash table entry at index H is unused (all zeroes),
8970 terminate the search: the signature is not present in the table.
8972 4) Let H = (H + H') modulo M. Repeat at Step 3.
8974 Because M > N and H' and M are relatively prime, the search is guaranteed
8975 to stop at an unused slot or find the match.
8977 The pool of section numbers begins immediately following the hash table
8978 (at offset 16 + 12 * M from the beginning of the section). The pool of
8979 section numbers consists of an array of 32-bit words (using the byte order
8980 of the application binary). Each item in the array is indexed starting
8981 from 0. The hash table entry provides the index of the first section
8982 number in the set. Additional section numbers in the set follow, and the
8983 set is terminated by a 0 entry (section number 0 is not used in ELF).
8985 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8986 section must be the first entry in the set, and the .debug_abbrev.dwo must
8987 be the second entry. Other members of the set may follow in any order. */
8989 /* Create a hash table to map DWO IDs to their CU/TU entry in
8990 .debug_{info,types}.dwo in DWP_FILE.
8991 Returns NULL if there isn't one.
8992 Note: This function processes DWP files only, not DWO files. */
8994 static struct dwp_hash_table *
8995 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8997 struct objfile *objfile = dwarf2_per_objfile->objfile;
8998 bfd *dbfd = dwp_file->dbfd;
8999 const gdb_byte *index_ptr, *index_end;
9000 struct dwarf2_section_info *index;
9001 uint32_t version, nr_units, nr_slots;
9002 struct dwp_hash_table *htab;
9005 index = &dwp_file->sections.tu_index;
9007 index = &dwp_file->sections.cu_index;
9009 if (dwarf2_section_empty_p (index))
9011 dwarf2_read_section (objfile, index);
9013 index_ptr = index->buffer;
9014 index_end = index_ptr + index->size;
9016 version = read_4_bytes (dbfd, index_ptr);
9017 index_ptr += 8; /* Skip the unused word. */
9018 nr_units = read_4_bytes (dbfd, index_ptr);
9020 nr_slots = read_4_bytes (dbfd, index_ptr);
9025 error (_("Dwarf Error: unsupported DWP file version (%s)"
9027 pulongest (version), dwp_file->name);
9029 if (nr_slots != (nr_slots & -nr_slots))
9031 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9032 " is not power of 2 [in module %s]"),
9033 pulongest (nr_slots), dwp_file->name);
9036 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9037 htab->nr_units = nr_units;
9038 htab->nr_slots = nr_slots;
9039 htab->hash_table = index_ptr;
9040 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9041 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9046 /* Update SECTIONS with the data from SECTP.
9048 This function is like the other "locate" section routines that are
9049 passed to bfd_map_over_sections, but in this context the sections to
9050 read comes from the DWP hash table, not the full ELF section table.
9052 The result is non-zero for success, or zero if an error was found. */
9055 locate_virtual_dwo_sections (asection *sectp,
9056 struct virtual_dwo_sections *sections)
9058 const struct dwop_section_names *names = &dwop_section_names;
9060 if (section_is_p (sectp->name, &names->abbrev_dwo))
9062 /* There can be only one. */
9063 if (sections->abbrev.asection != NULL)
9065 sections->abbrev.asection = sectp;
9066 sections->abbrev.size = bfd_get_section_size (sectp);
9068 else if (section_is_p (sectp->name, &names->info_dwo)
9069 || section_is_p (sectp->name, &names->types_dwo))
9071 /* There can be only one. */
9072 if (sections->info_or_types.asection != NULL)
9074 sections->info_or_types.asection = sectp;
9075 sections->info_or_types.size = bfd_get_section_size (sectp);
9077 else if (section_is_p (sectp->name, &names->line_dwo))
9079 /* There can be only one. */
9080 if (sections->line.asection != NULL)
9082 sections->line.asection = sectp;
9083 sections->line.size = bfd_get_section_size (sectp);
9085 else if (section_is_p (sectp->name, &names->loc_dwo))
9087 /* There can be only one. */
9088 if (sections->loc.asection != NULL)
9090 sections->loc.asection = sectp;
9091 sections->loc.size = bfd_get_section_size (sectp);
9093 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9095 /* There can be only one. */
9096 if (sections->macinfo.asection != NULL)
9098 sections->macinfo.asection = sectp;
9099 sections->macinfo.size = bfd_get_section_size (sectp);
9101 else if (section_is_p (sectp->name, &names->macro_dwo))
9103 /* There can be only one. */
9104 if (sections->macro.asection != NULL)
9106 sections->macro.asection = sectp;
9107 sections->macro.size = bfd_get_section_size (sectp);
9109 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9111 /* There can be only one. */
9112 if (sections->str_offsets.asection != NULL)
9114 sections->str_offsets.asection = sectp;
9115 sections->str_offsets.size = bfd_get_section_size (sectp);
9119 /* No other kind of section is valid. */
9126 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
9127 HTAB is the hash table from the DWP file.
9128 SECTION_INDEX is the index of the DWO in HTAB.
9129 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
9131 static struct dwo_unit *
9132 create_dwo_in_dwp (struct dwp_file *dwp_file,
9133 const struct dwp_hash_table *htab,
9134 uint32_t section_index,
9135 const char *comp_dir,
9136 ULONGEST signature, int is_debug_types)
9138 struct objfile *objfile = dwarf2_per_objfile->objfile;
9139 bfd *dbfd = dwp_file->dbfd;
9140 const char *kind = is_debug_types ? "TU" : "CU";
9141 struct dwo_file *dwo_file;
9142 struct dwo_unit *dwo_unit;
9143 struct virtual_dwo_sections sections;
9144 void **dwo_file_slot;
9145 char *virtual_dwo_name;
9146 struct dwarf2_section_info *cutu;
9147 struct cleanup *cleanups;
9150 if (dwarf2_read_debug)
9152 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
9154 pulongest (section_index), hex_string (signature),
9158 /* Fetch the sections of this DWO.
9159 Put a limit on the number of sections we look for so that bad data
9160 doesn't cause us to loop forever. */
9162 #define MAX_NR_DWO_SECTIONS \
9163 (1 /* .debug_info or .debug_types */ \
9164 + 1 /* .debug_abbrev */ \
9165 + 1 /* .debug_line */ \
9166 + 1 /* .debug_loc */ \
9167 + 1 /* .debug_str_offsets */ \
9168 + 1 /* .debug_macro */ \
9169 + 1 /* .debug_macinfo */ \
9170 + 1 /* trailing zero */)
9172 memset (§ions, 0, sizeof (sections));
9173 cleanups = make_cleanup (null_cleanup, 0);
9175 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9178 uint32_t section_nr =
9181 + (section_index + i) * sizeof (uint32_t));
9183 if (section_nr == 0)
9185 if (section_nr >= dwp_file->num_sections)
9187 error (_("Dwarf Error: bad DWP hash table, section number too large"
9192 sectp = dwp_file->elf_sections[section_nr];
9193 if (! locate_virtual_dwo_sections (sectp, §ions))
9195 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9202 || sections.info_or_types.asection == NULL
9203 || sections.abbrev.asection == NULL)
9205 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9209 if (i == MAX_NR_DWO_SECTIONS)
9211 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9216 /* It's easier for the rest of the code if we fake a struct dwo_file and
9217 have dwo_unit "live" in that. At least for now.
9219 The DWP file can be made up of a random collection of CUs and TUs.
9220 However, for each CU + set of TUs that came from the same original DWO
9221 file, we want to combine them back into a virtual DWO file to save space
9222 (fewer struct dwo_file objects to allocated). Remember that for really
9223 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9226 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9227 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9228 sections.line.asection ? sections.line.asection->id : 0,
9229 sections.loc.asection ? sections.loc.asection->id : 0,
9230 (sections.str_offsets.asection
9231 ? sections.str_offsets.asection->id
9233 make_cleanup (xfree, virtual_dwo_name);
9234 /* Can we use an existing virtual DWO file? */
9235 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9236 /* Create one if necessary. */
9237 if (*dwo_file_slot == NULL)
9239 if (dwarf2_read_debug)
9241 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9244 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9245 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9247 strlen (virtual_dwo_name));
9248 dwo_file->comp_dir = comp_dir;
9249 dwo_file->sections.abbrev = sections.abbrev;
9250 dwo_file->sections.line = sections.line;
9251 dwo_file->sections.loc = sections.loc;
9252 dwo_file->sections.macinfo = sections.macinfo;
9253 dwo_file->sections.macro = sections.macro;
9254 dwo_file->sections.str_offsets = sections.str_offsets;
9255 /* The "str" section is global to the entire DWP file. */
9256 dwo_file->sections.str = dwp_file->sections.str;
9257 /* The info or types section is assigned later to dwo_unit,
9258 there's no need to record it in dwo_file.
9259 Also, we can't simply record type sections in dwo_file because
9260 we record a pointer into the vector in dwo_unit. As we collect more
9261 types we'll grow the vector and eventually have to reallocate space
9262 for it, invalidating all the pointers into the current copy. */
9263 *dwo_file_slot = dwo_file;
9267 if (dwarf2_read_debug)
9269 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9272 dwo_file = *dwo_file_slot;
9274 do_cleanups (cleanups);
9276 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9277 dwo_unit->dwo_file = dwo_file;
9278 dwo_unit->signature = signature;
9279 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9280 sizeof (struct dwarf2_section_info));
9281 *dwo_unit->section = sections.info_or_types;
9282 /* offset, length, type_offset_in_tu are set later. */
9287 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
9289 static struct dwo_unit *
9290 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9291 const struct dwp_hash_table *htab,
9292 const char *comp_dir,
9293 ULONGEST signature, int is_debug_types)
9295 bfd *dbfd = dwp_file->dbfd;
9296 uint32_t mask = htab->nr_slots - 1;
9297 uint32_t hash = signature & mask;
9298 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9301 struct dwo_unit find_dwo_cu, *dwo_cu;
9303 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9304 find_dwo_cu.signature = signature;
9305 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9310 /* Use a for loop so that we don't loop forever on bad debug info. */
9311 for (i = 0; i < htab->nr_slots; ++i)
9313 ULONGEST signature_in_table;
9315 signature_in_table =
9316 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9317 if (signature_in_table == signature)
9319 uint32_t section_index =
9320 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9322 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
9323 comp_dir, signature, is_debug_types);
9326 if (signature_in_table == 0)
9328 hash = (hash + hash2) & mask;
9331 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9336 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9337 Open the file specified by FILE_NAME and hand it off to BFD for
9338 preliminary analysis. Return a newly initialized bfd *, which
9339 includes a canonicalized copy of FILE_NAME.
9340 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9341 SEARCH_CWD is true if the current directory is to be searched.
9342 It will be searched before debug-file-directory.
9343 If unable to find/open the file, return NULL.
9344 NOTE: This function is derived from symfile_bfd_open. */
9347 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
9351 char *absolute_name;
9352 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9353 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9354 to debug_file_directory. */
9356 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9360 if (*debug_file_directory != '\0')
9361 search_path = concat (".", dirname_separator_string,
9362 debug_file_directory, NULL);
9364 search_path = xstrdup (".");
9367 search_path = xstrdup (debug_file_directory);
9369 flags = OPF_RETURN_REALPATH;
9371 flags |= OPF_SEARCH_IN_PATH;
9372 desc = openp (search_path, flags, file_name,
9373 O_RDONLY | O_BINARY, &absolute_name);
9374 xfree (search_path);
9378 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
9379 xfree (absolute_name);
9380 if (sym_bfd == NULL)
9382 bfd_set_cacheable (sym_bfd, 1);
9384 if (!bfd_check_format (sym_bfd, bfd_object))
9386 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
9393 /* Try to open DWO file FILE_NAME.
9394 COMP_DIR is the DW_AT_comp_dir attribute.
9395 The result is the bfd handle of the file.
9396 If there is a problem finding or opening the file, return NULL.
9397 Upon success, the canonicalized path of the file is stored in the bfd,
9398 same as symfile_bfd_open. */
9401 open_dwo_file (const char *file_name, const char *comp_dir)
9405 if (IS_ABSOLUTE_PATH (file_name))
9406 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
9408 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9410 if (comp_dir != NULL)
9412 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
9414 /* NOTE: If comp_dir is a relative path, this will also try the
9415 search path, which seems useful. */
9416 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
9417 xfree (path_to_try);
9422 /* That didn't work, try debug-file-directory, which, despite its name,
9423 is a list of paths. */
9425 if (*debug_file_directory == '\0')
9428 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
9431 /* This function is mapped across the sections and remembers the offset and
9432 size of each of the DWO debugging sections we are interested in. */
9435 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9437 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9438 const struct dwop_section_names *names = &dwop_section_names;
9440 if (section_is_p (sectp->name, &names->abbrev_dwo))
9442 dwo_sections->abbrev.asection = sectp;
9443 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9445 else if (section_is_p (sectp->name, &names->info_dwo))
9447 dwo_sections->info.asection = sectp;
9448 dwo_sections->info.size = bfd_get_section_size (sectp);
9450 else if (section_is_p (sectp->name, &names->line_dwo))
9452 dwo_sections->line.asection = sectp;
9453 dwo_sections->line.size = bfd_get_section_size (sectp);
9455 else if (section_is_p (sectp->name, &names->loc_dwo))
9457 dwo_sections->loc.asection = sectp;
9458 dwo_sections->loc.size = bfd_get_section_size (sectp);
9460 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9462 dwo_sections->macinfo.asection = sectp;
9463 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9465 else if (section_is_p (sectp->name, &names->macro_dwo))
9467 dwo_sections->macro.asection = sectp;
9468 dwo_sections->macro.size = bfd_get_section_size (sectp);
9470 else if (section_is_p (sectp->name, &names->str_dwo))
9472 dwo_sections->str.asection = sectp;
9473 dwo_sections->str.size = bfd_get_section_size (sectp);
9475 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9477 dwo_sections->str_offsets.asection = sectp;
9478 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9480 else if (section_is_p (sectp->name, &names->types_dwo))
9482 struct dwarf2_section_info type_section;
9484 memset (&type_section, 0, sizeof (type_section));
9485 type_section.asection = sectp;
9486 type_section.size = bfd_get_section_size (sectp);
9487 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9492 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9493 by PER_CU. This is for the non-DWP case.
9494 The result is NULL if DWO_NAME can't be found. */
9496 static struct dwo_file *
9497 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9498 const char *dwo_name, const char *comp_dir)
9500 struct objfile *objfile = dwarf2_per_objfile->objfile;
9501 struct dwo_file *dwo_file;
9503 struct cleanup *cleanups;
9505 dbfd = open_dwo_file (dwo_name, comp_dir);
9508 if (dwarf2_read_debug)
9509 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9512 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9513 dwo_file->dwo_name = dwo_name;
9514 dwo_file->comp_dir = comp_dir;
9515 dwo_file->dbfd = dbfd;
9517 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9519 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9521 dwo_file->cu = create_dwo_cu (dwo_file);
9523 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9524 dwo_file->sections.types);
9526 discard_cleanups (cleanups);
9528 if (dwarf2_read_debug)
9529 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9534 /* This function is mapped across the sections and remembers the offset and
9535 size of each of the DWP debugging sections we are interested in. */
9538 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9540 struct dwp_file *dwp_file = dwp_file_ptr;
9541 const struct dwop_section_names *names = &dwop_section_names;
9542 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9544 /* Record the ELF section number for later lookup: this is what the
9545 .debug_cu_index,.debug_tu_index tables use. */
9546 gdb_assert (elf_section_nr < dwp_file->num_sections);
9547 dwp_file->elf_sections[elf_section_nr] = sectp;
9549 /* Look for specific sections that we need. */
9550 if (section_is_p (sectp->name, &names->str_dwo))
9552 dwp_file->sections.str.asection = sectp;
9553 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9555 else if (section_is_p (sectp->name, &names->cu_index))
9557 dwp_file->sections.cu_index.asection = sectp;
9558 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9560 else if (section_is_p (sectp->name, &names->tu_index))
9562 dwp_file->sections.tu_index.asection = sectp;
9563 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9567 /* Hash function for dwp_file loaded CUs/TUs. */
9570 hash_dwp_loaded_cutus (const void *item)
9572 const struct dwo_unit *dwo_unit = item;
9574 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9575 return dwo_unit->signature;
9578 /* Equality function for dwp_file loaded CUs/TUs. */
9581 eq_dwp_loaded_cutus (const void *a, const void *b)
9583 const struct dwo_unit *dua = a;
9584 const struct dwo_unit *dub = b;
9586 return dua->signature == dub->signature;
9589 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9592 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9594 return htab_create_alloc_ex (3,
9595 hash_dwp_loaded_cutus,
9596 eq_dwp_loaded_cutus,
9598 &objfile->objfile_obstack,
9599 hashtab_obstack_allocate,
9600 dummy_obstack_deallocate);
9603 /* Try to open DWP file FILE_NAME.
9604 The result is the bfd handle of the file.
9605 If there is a problem finding or opening the file, return NULL.
9606 Upon success, the canonicalized path of the file is stored in the bfd,
9607 same as symfile_bfd_open. */
9610 open_dwp_file (const char *file_name)
9614 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9618 /* Work around upstream bug 15652.
9619 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9620 [Whether that's a "bug" is debatable, but it is getting in our way.]
9621 We have no real idea where the dwp file is, because gdb's realpath-ing
9622 of the executable's path may have discarded the needed info.
9623 [IWBN if the dwp file name was recorded in the executable, akin to
9624 .gnu_debuglink, but that doesn't exist yet.]
9625 Strip the directory from FILE_NAME and search again. */
9626 if (*debug_file_directory != '\0')
9628 /* Don't implicitly search the current directory here.
9629 If the user wants to search "." to handle this case,
9630 it must be added to debug-file-directory. */
9631 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9638 /* Initialize the use of the DWP file for the current objfile.
9639 By convention the name of the DWP file is ${objfile}.dwp.
9640 The result is NULL if it can't be found. */
9642 static struct dwp_file *
9643 open_and_init_dwp_file (void)
9645 struct objfile *objfile = dwarf2_per_objfile->objfile;
9646 struct dwp_file *dwp_file;
9649 struct cleanup *cleanups;
9651 /* Try to find first .dwp for the binary file before any symbolic links
9653 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
9654 cleanups = make_cleanup (xfree, dwp_name);
9656 dbfd = open_dwp_file (dwp_name);
9658 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9660 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9661 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
9662 make_cleanup (xfree, dwp_name);
9663 dbfd = open_dwp_file (dwp_name);
9668 if (dwarf2_read_debug)
9669 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9670 do_cleanups (cleanups);
9673 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9674 dwp_file->name = bfd_get_filename (dbfd);
9675 dwp_file->dbfd = dbfd;
9676 do_cleanups (cleanups);
9678 /* +1: section 0 is unused */
9679 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9680 dwp_file->elf_sections =
9681 OBSTACK_CALLOC (&objfile->objfile_obstack,
9682 dwp_file->num_sections, asection *);
9684 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9686 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9688 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9690 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9692 if (dwarf2_read_debug)
9694 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9695 fprintf_unfiltered (gdb_stdlog,
9696 " %s CUs, %s TUs\n",
9697 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9698 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9704 /* Wrapper around open_and_init_dwp_file, only open it once. */
9706 static struct dwp_file *
9709 if (! dwarf2_per_objfile->dwp_checked)
9711 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9712 dwarf2_per_objfile->dwp_checked = 1;
9714 return dwarf2_per_objfile->dwp_file;
9717 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9718 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9719 or in the DWP file for the objfile, referenced by THIS_UNIT.
9720 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9721 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9723 This is called, for example, when wanting to read a variable with a
9724 complex location. Therefore we don't want to do file i/o for every call.
9725 Therefore we don't want to look for a DWO file on every call.
9726 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9727 then we check if we've already seen DWO_NAME, and only THEN do we check
9730 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9731 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9733 static struct dwo_unit *
9734 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9735 const char *dwo_name, const char *comp_dir,
9736 ULONGEST signature, int is_debug_types)
9738 struct objfile *objfile = dwarf2_per_objfile->objfile;
9739 const char *kind = is_debug_types ? "TU" : "CU";
9740 void **dwo_file_slot;
9741 struct dwo_file *dwo_file;
9742 struct dwp_file *dwp_file;
9744 /* First see if there's a DWP file.
9745 If we have a DWP file but didn't find the DWO inside it, don't
9746 look for the original DWO file. It makes gdb behave differently
9747 depending on whether one is debugging in the build tree. */
9749 dwp_file = get_dwp_file ();
9750 if (dwp_file != NULL)
9752 const struct dwp_hash_table *dwp_htab =
9753 is_debug_types ? dwp_file->tus : dwp_file->cus;
9755 if (dwp_htab != NULL)
9757 struct dwo_unit *dwo_cutu =
9758 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9759 signature, is_debug_types);
9761 if (dwo_cutu != NULL)
9763 if (dwarf2_read_debug)
9765 fprintf_unfiltered (gdb_stdlog,
9766 "Virtual DWO %s %s found: @%s\n",
9767 kind, hex_string (signature),
9768 host_address_to_string (dwo_cutu));
9776 /* No DWP file, look for the DWO file. */
9778 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9779 if (*dwo_file_slot == NULL)
9781 /* Read in the file and build a table of the CUs/TUs it contains. */
9782 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9784 /* NOTE: This will be NULL if unable to open the file. */
9785 dwo_file = *dwo_file_slot;
9787 if (dwo_file != NULL)
9789 struct dwo_unit *dwo_cutu = NULL;
9791 if (is_debug_types && dwo_file->tus)
9793 struct dwo_unit find_dwo_cutu;
9795 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9796 find_dwo_cutu.signature = signature;
9797 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9799 else if (!is_debug_types && dwo_file->cu)
9801 if (signature == dwo_file->cu->signature)
9802 dwo_cutu = dwo_file->cu;
9805 if (dwo_cutu != NULL)
9807 if (dwarf2_read_debug)
9809 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9810 kind, dwo_name, hex_string (signature),
9811 host_address_to_string (dwo_cutu));
9818 /* We didn't find it. This could mean a dwo_id mismatch, or
9819 someone deleted the DWO/DWP file, or the search path isn't set up
9820 correctly to find the file. */
9822 if (dwarf2_read_debug)
9824 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9825 kind, dwo_name, hex_string (signature));
9828 /* This is a warning and not a complaint because it can be caused by
9829 pilot error (e.g., user accidentally deleting the DWO). */
9830 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9832 kind, dwo_name, hex_string (signature),
9833 this_unit->is_debug_types ? "TU" : "CU",
9834 this_unit->offset.sect_off, objfile_name (objfile));
9838 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9839 See lookup_dwo_cutu_unit for details. */
9841 static struct dwo_unit *
9842 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9843 const char *dwo_name, const char *comp_dir,
9846 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9849 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9850 See lookup_dwo_cutu_unit for details. */
9852 static struct dwo_unit *
9853 lookup_dwo_type_unit (struct signatured_type *this_tu,
9854 const char *dwo_name, const char *comp_dir)
9856 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9859 /* Traversal function for queue_and_load_all_dwo_tus. */
9862 queue_and_load_dwo_tu (void **slot, void *info)
9864 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9865 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
9866 ULONGEST signature = dwo_unit->signature;
9867 struct signatured_type *sig_type =
9868 lookup_dwo_signatured_type (per_cu->cu, signature);
9870 if (sig_type != NULL)
9872 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
9874 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9875 a real dependency of PER_CU on SIG_TYPE. That is detected later
9876 while processing PER_CU. */
9877 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
9878 load_full_type_unit (sig_cu);
9879 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
9885 /* Queue all TUs contained in the DWO of PER_CU to be read in.
9886 The DWO may have the only definition of the type, though it may not be
9887 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9888 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9891 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
9893 struct dwo_unit *dwo_unit;
9894 struct dwo_file *dwo_file;
9896 gdb_assert (!per_cu->is_debug_types);
9897 gdb_assert (get_dwp_file () == NULL);
9898 gdb_assert (per_cu->cu != NULL);
9900 dwo_unit = per_cu->cu->dwo_unit;
9901 gdb_assert (dwo_unit != NULL);
9903 dwo_file = dwo_unit->dwo_file;
9904 if (dwo_file->tus != NULL)
9905 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
9908 /* Free all resources associated with DWO_FILE.
9909 Close the DWO file and munmap the sections.
9910 All memory should be on the objfile obstack. */
9913 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9916 struct dwarf2_section_info *section;
9918 /* Note: dbfd is NULL for virtual DWO files. */
9919 gdb_bfd_unref (dwo_file->dbfd);
9921 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9924 /* Wrapper for free_dwo_file for use in cleanups. */
9927 free_dwo_file_cleanup (void *arg)
9929 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9930 struct objfile *objfile = dwarf2_per_objfile->objfile;
9932 free_dwo_file (dwo_file, objfile);
9935 /* Traversal function for free_dwo_files. */
9938 free_dwo_file_from_slot (void **slot, void *info)
9940 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9941 struct objfile *objfile = (struct objfile *) info;
9943 free_dwo_file (dwo_file, objfile);
9948 /* Free all resources associated with DWO_FILES. */
9951 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9953 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9956 /* Read in various DIEs. */
9958 /* qsort helper for inherit_abstract_dies. */
9961 unsigned_int_compar (const void *ap, const void *bp)
9963 unsigned int a = *(unsigned int *) ap;
9964 unsigned int b = *(unsigned int *) bp;
9966 return (a > b) - (b > a);
9969 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9970 Inherit only the children of the DW_AT_abstract_origin DIE not being
9971 already referenced by DW_AT_abstract_origin from the children of the
9975 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9977 struct die_info *child_die;
9978 unsigned die_children_count;
9979 /* CU offsets which were referenced by children of the current DIE. */
9980 sect_offset *offsets;
9981 sect_offset *offsets_end, *offsetp;
9982 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9983 struct die_info *origin_die;
9984 /* Iterator of the ORIGIN_DIE children. */
9985 struct die_info *origin_child_die;
9986 struct cleanup *cleanups;
9987 struct attribute *attr;
9988 struct dwarf2_cu *origin_cu;
9989 struct pending **origin_previous_list_in_scope;
9991 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9995 /* Note that following die references may follow to a die in a
9999 origin_die = follow_die_ref (die, attr, &origin_cu);
10001 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
10003 origin_previous_list_in_scope = origin_cu->list_in_scope;
10004 origin_cu->list_in_scope = cu->list_in_scope;
10006 if (die->tag != origin_die->tag
10007 && !(die->tag == DW_TAG_inlined_subroutine
10008 && origin_die->tag == DW_TAG_subprogram))
10009 complaint (&symfile_complaints,
10010 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
10011 die->offset.sect_off, origin_die->offset.sect_off);
10013 child_die = die->child;
10014 die_children_count = 0;
10015 while (child_die && child_die->tag)
10017 child_die = sibling_die (child_die);
10018 die_children_count++;
10020 offsets = xmalloc (sizeof (*offsets) * die_children_count);
10021 cleanups = make_cleanup (xfree, offsets);
10023 offsets_end = offsets;
10024 child_die = die->child;
10025 while (child_die && child_die->tag)
10027 /* For each CHILD_DIE, find the corresponding child of
10028 ORIGIN_DIE. If there is more than one layer of
10029 DW_AT_abstract_origin, follow them all; there shouldn't be,
10030 but GCC versions at least through 4.4 generate this (GCC PR
10032 struct die_info *child_origin_die = child_die;
10033 struct dwarf2_cu *child_origin_cu = cu;
10037 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10041 child_origin_die = follow_die_ref (child_origin_die, attr,
10045 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10046 counterpart may exist. */
10047 if (child_origin_die != child_die)
10049 if (child_die->tag != child_origin_die->tag
10050 && !(child_die->tag == DW_TAG_inlined_subroutine
10051 && child_origin_die->tag == DW_TAG_subprogram))
10052 complaint (&symfile_complaints,
10053 _("Child DIE 0x%x and its abstract origin 0x%x have "
10054 "different tags"), child_die->offset.sect_off,
10055 child_origin_die->offset.sect_off);
10056 if (child_origin_die->parent != origin_die)
10057 complaint (&symfile_complaints,
10058 _("Child DIE 0x%x and its abstract origin 0x%x have "
10059 "different parents"), child_die->offset.sect_off,
10060 child_origin_die->offset.sect_off);
10062 *offsets_end++ = child_origin_die->offset;
10064 child_die = sibling_die (child_die);
10066 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10067 unsigned_int_compar);
10068 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
10069 if (offsetp[-1].sect_off == offsetp->sect_off)
10070 complaint (&symfile_complaints,
10071 _("Multiple children of DIE 0x%x refer "
10072 "to DIE 0x%x as their abstract origin"),
10073 die->offset.sect_off, offsetp->sect_off);
10076 origin_child_die = origin_die->child;
10077 while (origin_child_die && origin_child_die->tag)
10079 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
10080 while (offsetp < offsets_end
10081 && offsetp->sect_off < origin_child_die->offset.sect_off)
10083 if (offsetp >= offsets_end
10084 || offsetp->sect_off > origin_child_die->offset.sect_off)
10086 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
10087 process_die (origin_child_die, origin_cu);
10089 origin_child_die = sibling_die (origin_child_die);
10091 origin_cu->list_in_scope = origin_previous_list_in_scope;
10093 do_cleanups (cleanups);
10097 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10099 struct objfile *objfile = cu->objfile;
10100 struct context_stack *new;
10103 struct die_info *child_die;
10104 struct attribute *attr, *call_line, *call_file;
10106 CORE_ADDR baseaddr;
10107 struct block *block;
10108 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10109 VEC (symbolp) *template_args = NULL;
10110 struct template_symbol *templ_func = NULL;
10114 /* If we do not have call site information, we can't show the
10115 caller of this inlined function. That's too confusing, so
10116 only use the scope for local variables. */
10117 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10118 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10119 if (call_line == NULL || call_file == NULL)
10121 read_lexical_block_scope (die, cu);
10126 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10128 name = dwarf2_name (die, cu);
10130 /* Ignore functions with missing or empty names. These are actually
10131 illegal according to the DWARF standard. */
10134 complaint (&symfile_complaints,
10135 _("missing name for subprogram DIE at %d"),
10136 die->offset.sect_off);
10140 /* Ignore functions with missing or invalid low and high pc attributes. */
10141 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10143 attr = dwarf2_attr (die, DW_AT_external, cu);
10144 if (!attr || !DW_UNSND (attr))
10145 complaint (&symfile_complaints,
10146 _("cannot get low and high bounds "
10147 "for subprogram DIE at %d"),
10148 die->offset.sect_off);
10153 highpc += baseaddr;
10155 /* If we have any template arguments, then we must allocate a
10156 different sort of symbol. */
10157 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10159 if (child_die->tag == DW_TAG_template_type_param
10160 || child_die->tag == DW_TAG_template_value_param)
10162 templ_func = allocate_template_symbol (objfile);
10163 templ_func->base.is_cplus_template_function = 1;
10168 new = push_context (0, lowpc);
10169 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10170 (struct symbol *) templ_func);
10172 /* If there is a location expression for DW_AT_frame_base, record
10174 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10176 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10178 cu->list_in_scope = &local_symbols;
10180 if (die->child != NULL)
10182 child_die = die->child;
10183 while (child_die && child_die->tag)
10185 if (child_die->tag == DW_TAG_template_type_param
10186 || child_die->tag == DW_TAG_template_value_param)
10188 struct symbol *arg = new_symbol (child_die, NULL, cu);
10191 VEC_safe_push (symbolp, template_args, arg);
10194 process_die (child_die, cu);
10195 child_die = sibling_die (child_die);
10199 inherit_abstract_dies (die, cu);
10201 /* If we have a DW_AT_specification, we might need to import using
10202 directives from the context of the specification DIE. See the
10203 comment in determine_prefix. */
10204 if (cu->language == language_cplus
10205 && dwarf2_attr (die, DW_AT_specification, cu))
10207 struct dwarf2_cu *spec_cu = cu;
10208 struct die_info *spec_die = die_specification (die, &spec_cu);
10212 child_die = spec_die->child;
10213 while (child_die && child_die->tag)
10215 if (child_die->tag == DW_TAG_imported_module)
10216 process_die (child_die, spec_cu);
10217 child_die = sibling_die (child_die);
10220 /* In some cases, GCC generates specification DIEs that
10221 themselves contain DW_AT_specification attributes. */
10222 spec_die = die_specification (spec_die, &spec_cu);
10226 new = pop_context ();
10227 /* Make a block for the local symbols within. */
10228 block = finish_block (new->name, &local_symbols, new->old_blocks,
10229 lowpc, highpc, objfile);
10231 /* For C++, set the block's scope. */
10232 if ((cu->language == language_cplus || cu->language == language_fortran)
10233 && cu->processing_has_namespace_info)
10234 block_set_scope (block, determine_prefix (die, cu),
10235 &objfile->objfile_obstack);
10237 /* If we have address ranges, record them. */
10238 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10240 /* Attach template arguments to function. */
10241 if (! VEC_empty (symbolp, template_args))
10243 gdb_assert (templ_func != NULL);
10245 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10246 templ_func->template_arguments
10247 = obstack_alloc (&objfile->objfile_obstack,
10248 (templ_func->n_template_arguments
10249 * sizeof (struct symbol *)));
10250 memcpy (templ_func->template_arguments,
10251 VEC_address (symbolp, template_args),
10252 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10253 VEC_free (symbolp, template_args);
10256 /* In C++, we can have functions nested inside functions (e.g., when
10257 a function declares a class that has methods). This means that
10258 when we finish processing a function scope, we may need to go
10259 back to building a containing block's symbol lists. */
10260 local_symbols = new->locals;
10261 using_directives = new->using_directives;
10263 /* If we've finished processing a top-level function, subsequent
10264 symbols go in the file symbol list. */
10265 if (outermost_context_p ())
10266 cu->list_in_scope = &file_symbols;
10269 /* Process all the DIES contained within a lexical block scope. Start
10270 a new scope, process the dies, and then close the scope. */
10273 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10275 struct objfile *objfile = cu->objfile;
10276 struct context_stack *new;
10277 CORE_ADDR lowpc, highpc;
10278 struct die_info *child_die;
10279 CORE_ADDR baseaddr;
10281 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10283 /* Ignore blocks with missing or invalid low and high pc attributes. */
10284 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10285 as multiple lexical blocks? Handling children in a sane way would
10286 be nasty. Might be easier to properly extend generic blocks to
10287 describe ranges. */
10288 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10291 highpc += baseaddr;
10293 push_context (0, lowpc);
10294 if (die->child != NULL)
10296 child_die = die->child;
10297 while (child_die && child_die->tag)
10299 process_die (child_die, cu);
10300 child_die = sibling_die (child_die);
10303 new = pop_context ();
10305 if (local_symbols != NULL || using_directives != NULL)
10307 struct block *block
10308 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10311 /* Note that recording ranges after traversing children, as we
10312 do here, means that recording a parent's ranges entails
10313 walking across all its children's ranges as they appear in
10314 the address map, which is quadratic behavior.
10316 It would be nicer to record the parent's ranges before
10317 traversing its children, simply overriding whatever you find
10318 there. But since we don't even decide whether to create a
10319 block until after we've traversed its children, that's hard
10321 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10323 local_symbols = new->locals;
10324 using_directives = new->using_directives;
10327 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10330 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10332 struct objfile *objfile = cu->objfile;
10333 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10334 CORE_ADDR pc, baseaddr;
10335 struct attribute *attr;
10336 struct call_site *call_site, call_site_local;
10339 struct die_info *child_die;
10341 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10343 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10346 complaint (&symfile_complaints,
10347 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10348 "DIE 0x%x [in module %s]"),
10349 die->offset.sect_off, objfile_name (objfile));
10352 pc = DW_ADDR (attr) + baseaddr;
10354 if (cu->call_site_htab == NULL)
10355 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10356 NULL, &objfile->objfile_obstack,
10357 hashtab_obstack_allocate, NULL);
10358 call_site_local.pc = pc;
10359 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10362 complaint (&symfile_complaints,
10363 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10364 "DIE 0x%x [in module %s]"),
10365 paddress (gdbarch, pc), die->offset.sect_off,
10366 objfile_name (objfile));
10370 /* Count parameters at the caller. */
10373 for (child_die = die->child; child_die && child_die->tag;
10374 child_die = sibling_die (child_die))
10376 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10378 complaint (&symfile_complaints,
10379 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10380 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10381 child_die->tag, child_die->offset.sect_off,
10382 objfile_name (objfile));
10389 call_site = obstack_alloc (&objfile->objfile_obstack,
10390 (sizeof (*call_site)
10391 + (sizeof (*call_site->parameter)
10392 * (nparams - 1))));
10394 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10395 call_site->pc = pc;
10397 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10399 struct die_info *func_die;
10401 /* Skip also over DW_TAG_inlined_subroutine. */
10402 for (func_die = die->parent;
10403 func_die && func_die->tag != DW_TAG_subprogram
10404 && func_die->tag != DW_TAG_subroutine_type;
10405 func_die = func_die->parent);
10407 /* DW_AT_GNU_all_call_sites is a superset
10408 of DW_AT_GNU_all_tail_call_sites. */
10410 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10411 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10413 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10414 not complete. But keep CALL_SITE for look ups via call_site_htab,
10415 both the initial caller containing the real return address PC and
10416 the final callee containing the current PC of a chain of tail
10417 calls do not need to have the tail call list complete. But any
10418 function candidate for a virtual tail call frame searched via
10419 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10420 determined unambiguously. */
10424 struct type *func_type = NULL;
10427 func_type = get_die_type (func_die, cu);
10428 if (func_type != NULL)
10430 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10432 /* Enlist this call site to the function. */
10433 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10434 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10437 complaint (&symfile_complaints,
10438 _("Cannot find function owning DW_TAG_GNU_call_site "
10439 "DIE 0x%x [in module %s]"),
10440 die->offset.sect_off, objfile_name (objfile));
10444 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10446 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10447 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10448 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10449 /* Keep NULL DWARF_BLOCK. */;
10450 else if (attr_form_is_block (attr))
10452 struct dwarf2_locexpr_baton *dlbaton;
10454 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10455 dlbaton->data = DW_BLOCK (attr)->data;
10456 dlbaton->size = DW_BLOCK (attr)->size;
10457 dlbaton->per_cu = cu->per_cu;
10459 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10461 else if (attr_form_is_ref (attr))
10463 struct dwarf2_cu *target_cu = cu;
10464 struct die_info *target_die;
10466 target_die = follow_die_ref (die, attr, &target_cu);
10467 gdb_assert (target_cu->objfile == objfile);
10468 if (die_is_declaration (target_die, target_cu))
10470 const char *target_physname = NULL;
10471 struct attribute *target_attr;
10473 /* Prefer the mangled name; otherwise compute the demangled one. */
10474 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10475 if (target_attr == NULL)
10476 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10478 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10479 target_physname = DW_STRING (target_attr);
10481 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10482 if (target_physname == NULL)
10483 complaint (&symfile_complaints,
10484 _("DW_AT_GNU_call_site_target target DIE has invalid "
10485 "physname, for referencing DIE 0x%x [in module %s]"),
10486 die->offset.sect_off, objfile_name (objfile));
10488 SET_FIELD_PHYSNAME (call_site->target, target_physname);
10494 /* DW_AT_entry_pc should be preferred. */
10495 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10496 complaint (&symfile_complaints,
10497 _("DW_AT_GNU_call_site_target target DIE has invalid "
10498 "low pc, for referencing DIE 0x%x [in module %s]"),
10499 die->offset.sect_off, objfile_name (objfile));
10501 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10505 complaint (&symfile_complaints,
10506 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10507 "block nor reference, for DIE 0x%x [in module %s]"),
10508 die->offset.sect_off, objfile_name (objfile));
10510 call_site->per_cu = cu->per_cu;
10512 for (child_die = die->child;
10513 child_die && child_die->tag;
10514 child_die = sibling_die (child_die))
10516 struct call_site_parameter *parameter;
10517 struct attribute *loc, *origin;
10519 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10521 /* Already printed the complaint above. */
10525 gdb_assert (call_site->parameter_count < nparams);
10526 parameter = &call_site->parameter[call_site->parameter_count];
10528 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10529 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10530 register is contained in DW_AT_GNU_call_site_value. */
10532 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10533 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10534 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
10536 sect_offset offset;
10538 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10539 offset = dwarf2_get_ref_die_offset (origin);
10540 if (!offset_in_cu_p (&cu->header, offset))
10542 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10543 binding can be done only inside one CU. Such referenced DIE
10544 therefore cannot be even moved to DW_TAG_partial_unit. */
10545 complaint (&symfile_complaints,
10546 _("DW_AT_abstract_origin offset is not in CU for "
10547 "DW_TAG_GNU_call_site child DIE 0x%x "
10549 child_die->offset.sect_off, objfile_name (objfile));
10552 parameter->u.param_offset.cu_off = (offset.sect_off
10553 - cu->header.offset.sect_off);
10555 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10557 complaint (&symfile_complaints,
10558 _("No DW_FORM_block* DW_AT_location for "
10559 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10560 child_die->offset.sect_off, objfile_name (objfile));
10565 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10566 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10567 if (parameter->u.dwarf_reg != -1)
10568 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10569 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10570 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10571 ¶meter->u.fb_offset))
10572 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10575 complaint (&symfile_complaints,
10576 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10577 "for DW_FORM_block* DW_AT_location is supported for "
10578 "DW_TAG_GNU_call_site child DIE 0x%x "
10580 child_die->offset.sect_off, objfile_name (objfile));
10585 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10586 if (!attr_form_is_block (attr))
10588 complaint (&symfile_complaints,
10589 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10590 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10591 child_die->offset.sect_off, objfile_name (objfile));
10594 parameter->value = DW_BLOCK (attr)->data;
10595 parameter->value_size = DW_BLOCK (attr)->size;
10597 /* Parameters are not pre-cleared by memset above. */
10598 parameter->data_value = NULL;
10599 parameter->data_value_size = 0;
10600 call_site->parameter_count++;
10602 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10605 if (!attr_form_is_block (attr))
10606 complaint (&symfile_complaints,
10607 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10608 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10609 child_die->offset.sect_off, objfile_name (objfile));
10612 parameter->data_value = DW_BLOCK (attr)->data;
10613 parameter->data_value_size = DW_BLOCK (attr)->size;
10619 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10620 Return 1 if the attributes are present and valid, otherwise, return 0.
10621 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10624 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10625 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10626 struct partial_symtab *ranges_pst)
10628 struct objfile *objfile = cu->objfile;
10629 struct comp_unit_head *cu_header = &cu->header;
10630 bfd *obfd = objfile->obfd;
10631 unsigned int addr_size = cu_header->addr_size;
10632 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10633 /* Base address selection entry. */
10636 unsigned int dummy;
10637 const gdb_byte *buffer;
10641 CORE_ADDR high = 0;
10642 CORE_ADDR baseaddr;
10644 found_base = cu->base_known;
10645 base = cu->base_address;
10647 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10648 if (offset >= dwarf2_per_objfile->ranges.size)
10650 complaint (&symfile_complaints,
10651 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10655 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10657 /* Read in the largest possible address. */
10658 marker = read_address (obfd, buffer, cu, &dummy);
10659 if ((marker & mask) == mask)
10661 /* If we found the largest possible address, then
10662 read the base address. */
10663 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10664 buffer += 2 * addr_size;
10665 offset += 2 * addr_size;
10671 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10675 CORE_ADDR range_beginning, range_end;
10677 range_beginning = read_address (obfd, buffer, cu, &dummy);
10678 buffer += addr_size;
10679 range_end = read_address (obfd, buffer, cu, &dummy);
10680 buffer += addr_size;
10681 offset += 2 * addr_size;
10683 /* An end of list marker is a pair of zero addresses. */
10684 if (range_beginning == 0 && range_end == 0)
10685 /* Found the end of list entry. */
10688 /* Each base address selection entry is a pair of 2 values.
10689 The first is the largest possible address, the second is
10690 the base address. Check for a base address here. */
10691 if ((range_beginning & mask) == mask)
10693 /* If we found the largest possible address, then
10694 read the base address. */
10695 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10702 /* We have no valid base address for the ranges
10704 complaint (&symfile_complaints,
10705 _("Invalid .debug_ranges data (no base address)"));
10709 if (range_beginning > range_end)
10711 /* Inverted range entries are invalid. */
10712 complaint (&symfile_complaints,
10713 _("Invalid .debug_ranges data (inverted range)"));
10717 /* Empty range entries have no effect. */
10718 if (range_beginning == range_end)
10721 range_beginning += base;
10724 /* A not-uncommon case of bad debug info.
10725 Don't pollute the addrmap with bad data. */
10726 if (range_beginning + baseaddr == 0
10727 && !dwarf2_per_objfile->has_section_at_zero)
10729 complaint (&symfile_complaints,
10730 _(".debug_ranges entry has start address of zero"
10731 " [in module %s]"), objfile_name (objfile));
10735 if (ranges_pst != NULL)
10736 addrmap_set_empty (objfile->psymtabs_addrmap,
10737 range_beginning + baseaddr,
10738 range_end - 1 + baseaddr,
10741 /* FIXME: This is recording everything as a low-high
10742 segment of consecutive addresses. We should have a
10743 data structure for discontiguous block ranges
10747 low = range_beginning;
10753 if (range_beginning < low)
10754 low = range_beginning;
10755 if (range_end > high)
10761 /* If the first entry is an end-of-list marker, the range
10762 describes an empty scope, i.e. no instructions. */
10768 *high_return = high;
10772 /* Get low and high pc attributes from a die. Return 1 if the attributes
10773 are present and valid, otherwise, return 0. Return -1 if the range is
10774 discontinuous, i.e. derived from DW_AT_ranges information. */
10777 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10778 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10779 struct partial_symtab *pst)
10781 struct attribute *attr;
10782 struct attribute *attr_high;
10784 CORE_ADDR high = 0;
10787 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10790 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10793 low = DW_ADDR (attr);
10794 if (attr_high->form == DW_FORM_addr
10795 || attr_high->form == DW_FORM_GNU_addr_index)
10796 high = DW_ADDR (attr_high);
10798 high = low + DW_UNSND (attr_high);
10801 /* Found high w/o low attribute. */
10804 /* Found consecutive range of addresses. */
10809 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10812 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10813 We take advantage of the fact that DW_AT_ranges does not appear
10814 in DW_TAG_compile_unit of DWO files. */
10815 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10816 unsigned int ranges_offset = (DW_UNSND (attr)
10817 + (need_ranges_base
10821 /* Value of the DW_AT_ranges attribute is the offset in the
10822 .debug_ranges section. */
10823 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10825 /* Found discontinuous range of addresses. */
10830 /* read_partial_die has also the strict LOW < HIGH requirement. */
10834 /* When using the GNU linker, .gnu.linkonce. sections are used to
10835 eliminate duplicate copies of functions and vtables and such.
10836 The linker will arbitrarily choose one and discard the others.
10837 The AT_*_pc values for such functions refer to local labels in
10838 these sections. If the section from that file was discarded, the
10839 labels are not in the output, so the relocs get a value of 0.
10840 If this is a discarded function, mark the pc bounds as invalid,
10841 so that GDB will ignore it. */
10842 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10851 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10852 its low and high PC addresses. Do nothing if these addresses could not
10853 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10854 and HIGHPC to the high address if greater than HIGHPC. */
10857 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10858 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10859 struct dwarf2_cu *cu)
10861 CORE_ADDR low, high;
10862 struct die_info *child = die->child;
10864 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10866 *lowpc = min (*lowpc, low);
10867 *highpc = max (*highpc, high);
10870 /* If the language does not allow nested subprograms (either inside
10871 subprograms or lexical blocks), we're done. */
10872 if (cu->language != language_ada)
10875 /* Check all the children of the given DIE. If it contains nested
10876 subprograms, then check their pc bounds. Likewise, we need to
10877 check lexical blocks as well, as they may also contain subprogram
10879 while (child && child->tag)
10881 if (child->tag == DW_TAG_subprogram
10882 || child->tag == DW_TAG_lexical_block)
10883 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10884 child = sibling_die (child);
10888 /* Get the low and high pc's represented by the scope DIE, and store
10889 them in *LOWPC and *HIGHPC. If the correct values can't be
10890 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10893 get_scope_pc_bounds (struct die_info *die,
10894 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10895 struct dwarf2_cu *cu)
10897 CORE_ADDR best_low = (CORE_ADDR) -1;
10898 CORE_ADDR best_high = (CORE_ADDR) 0;
10899 CORE_ADDR current_low, current_high;
10901 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
10903 best_low = current_low;
10904 best_high = current_high;
10908 struct die_info *child = die->child;
10910 while (child && child->tag)
10912 switch (child->tag) {
10913 case DW_TAG_subprogram:
10914 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10916 case DW_TAG_namespace:
10917 case DW_TAG_module:
10918 /* FIXME: carlton/2004-01-16: Should we do this for
10919 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10920 that current GCC's always emit the DIEs corresponding
10921 to definitions of methods of classes as children of a
10922 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10923 the DIEs giving the declarations, which could be
10924 anywhere). But I don't see any reason why the
10925 standards says that they have to be there. */
10926 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
10928 if (current_low != ((CORE_ADDR) -1))
10930 best_low = min (best_low, current_low);
10931 best_high = max (best_high, current_high);
10939 child = sibling_die (child);
10944 *highpc = best_high;
10947 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10951 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10952 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10954 struct objfile *objfile = cu->objfile;
10955 struct attribute *attr;
10956 struct attribute *attr_high;
10958 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10961 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10964 CORE_ADDR low = DW_ADDR (attr);
10966 if (attr_high->form == DW_FORM_addr
10967 || attr_high->form == DW_FORM_GNU_addr_index)
10968 high = DW_ADDR (attr_high);
10970 high = low + DW_UNSND (attr_high);
10972 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10976 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10979 bfd *obfd = objfile->obfd;
10980 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10981 We take advantage of the fact that DW_AT_ranges does not appear
10982 in DW_TAG_compile_unit of DWO files. */
10983 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10985 /* The value of the DW_AT_ranges attribute is the offset of the
10986 address range list in the .debug_ranges section. */
10987 unsigned long offset = (DW_UNSND (attr)
10988 + (need_ranges_base ? cu->ranges_base : 0));
10989 const gdb_byte *buffer;
10991 /* For some target architectures, but not others, the
10992 read_address function sign-extends the addresses it returns.
10993 To recognize base address selection entries, we need a
10995 unsigned int addr_size = cu->header.addr_size;
10996 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10998 /* The base address, to which the next pair is relative. Note
10999 that this 'base' is a DWARF concept: most entries in a range
11000 list are relative, to reduce the number of relocs against the
11001 debugging information. This is separate from this function's
11002 'baseaddr' argument, which GDB uses to relocate debugging
11003 information from a shared library based on the address at
11004 which the library was loaded. */
11005 CORE_ADDR base = cu->base_address;
11006 int base_known = cu->base_known;
11008 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11009 if (offset >= dwarf2_per_objfile->ranges.size)
11011 complaint (&symfile_complaints,
11012 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
11016 buffer = dwarf2_per_objfile->ranges.buffer + offset;
11020 unsigned int bytes_read;
11021 CORE_ADDR start, end;
11023 start = read_address (obfd, buffer, cu, &bytes_read);
11024 buffer += bytes_read;
11025 end = read_address (obfd, buffer, cu, &bytes_read);
11026 buffer += bytes_read;
11028 /* Did we find the end of the range list? */
11029 if (start == 0 && end == 0)
11032 /* Did we find a base address selection entry? */
11033 else if ((start & base_select_mask) == base_select_mask)
11039 /* We found an ordinary address range. */
11044 complaint (&symfile_complaints,
11045 _("Invalid .debug_ranges data "
11046 "(no base address)"));
11052 /* Inverted range entries are invalid. */
11053 complaint (&symfile_complaints,
11054 _("Invalid .debug_ranges data "
11055 "(inverted range)"));
11059 /* Empty range entries have no effect. */
11063 start += base + baseaddr;
11064 end += base + baseaddr;
11066 /* A not-uncommon case of bad debug info.
11067 Don't pollute the addrmap with bad data. */
11068 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11070 complaint (&symfile_complaints,
11071 _(".debug_ranges entry has start address of zero"
11072 " [in module %s]"), objfile_name (objfile));
11076 record_block_range (block, start, end - 1);
11082 /* Check whether the producer field indicates either of GCC < 4.6, or the
11083 Intel C/C++ compiler, and cache the result in CU. */
11086 check_producer (struct dwarf2_cu *cu)
11089 int major, minor, release;
11091 if (cu->producer == NULL)
11093 /* For unknown compilers expect their behavior is DWARF version
11096 GCC started to support .debug_types sections by -gdwarf-4 since
11097 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11098 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11099 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11100 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11102 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
11104 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11106 cs = &cu->producer[strlen ("GNU ")];
11107 while (*cs && !isdigit (*cs))
11109 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11111 /* Not recognized as GCC. */
11115 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11116 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11119 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11120 cu->producer_is_icc = 1;
11123 /* For other non-GCC compilers, expect their behavior is DWARF version
11127 cu->checked_producer = 1;
11130 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11131 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11132 during 4.6.0 experimental. */
11135 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11137 if (!cu->checked_producer)
11138 check_producer (cu);
11140 return cu->producer_is_gxx_lt_4_6;
11143 /* Return the default accessibility type if it is not overriden by
11144 DW_AT_accessibility. */
11146 static enum dwarf_access_attribute
11147 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11149 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11151 /* The default DWARF 2 accessibility for members is public, the default
11152 accessibility for inheritance is private. */
11154 if (die->tag != DW_TAG_inheritance)
11155 return DW_ACCESS_public;
11157 return DW_ACCESS_private;
11161 /* DWARF 3+ defines the default accessibility a different way. The same
11162 rules apply now for DW_TAG_inheritance as for the members and it only
11163 depends on the container kind. */
11165 if (die->parent->tag == DW_TAG_class_type)
11166 return DW_ACCESS_private;
11168 return DW_ACCESS_public;
11172 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11173 offset. If the attribute was not found return 0, otherwise return
11174 1. If it was found but could not properly be handled, set *OFFSET
11178 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11181 struct attribute *attr;
11183 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11188 /* Note that we do not check for a section offset first here.
11189 This is because DW_AT_data_member_location is new in DWARF 4,
11190 so if we see it, we can assume that a constant form is really
11191 a constant and not a section offset. */
11192 if (attr_form_is_constant (attr))
11193 *offset = dwarf2_get_attr_constant_value (attr, 0);
11194 else if (attr_form_is_section_offset (attr))
11195 dwarf2_complex_location_expr_complaint ();
11196 else if (attr_form_is_block (attr))
11197 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11199 dwarf2_complex_location_expr_complaint ();
11207 /* Add an aggregate field to the field list. */
11210 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11211 struct dwarf2_cu *cu)
11213 struct objfile *objfile = cu->objfile;
11214 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11215 struct nextfield *new_field;
11216 struct attribute *attr;
11218 const char *fieldname = "";
11220 /* Allocate a new field list entry and link it in. */
11221 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11222 make_cleanup (xfree, new_field);
11223 memset (new_field, 0, sizeof (struct nextfield));
11225 if (die->tag == DW_TAG_inheritance)
11227 new_field->next = fip->baseclasses;
11228 fip->baseclasses = new_field;
11232 new_field->next = fip->fields;
11233 fip->fields = new_field;
11237 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11239 new_field->accessibility = DW_UNSND (attr);
11241 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11242 if (new_field->accessibility != DW_ACCESS_public)
11243 fip->non_public_fields = 1;
11245 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11247 new_field->virtuality = DW_UNSND (attr);
11249 new_field->virtuality = DW_VIRTUALITY_none;
11251 fp = &new_field->field;
11253 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11257 /* Data member other than a C++ static data member. */
11259 /* Get type of field. */
11260 fp->type = die_type (die, cu);
11262 SET_FIELD_BITPOS (*fp, 0);
11264 /* Get bit size of field (zero if none). */
11265 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11268 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11272 FIELD_BITSIZE (*fp) = 0;
11275 /* Get bit offset of field. */
11276 if (handle_data_member_location (die, cu, &offset))
11277 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11278 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11281 if (gdbarch_bits_big_endian (gdbarch))
11283 /* For big endian bits, the DW_AT_bit_offset gives the
11284 additional bit offset from the MSB of the containing
11285 anonymous object to the MSB of the field. We don't
11286 have to do anything special since we don't need to
11287 know the size of the anonymous object. */
11288 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
11292 /* For little endian bits, compute the bit offset to the
11293 MSB of the anonymous object, subtract off the number of
11294 bits from the MSB of the field to the MSB of the
11295 object, and then subtract off the number of bits of
11296 the field itself. The result is the bit offset of
11297 the LSB of the field. */
11298 int anonymous_size;
11299 int bit_offset = DW_UNSND (attr);
11301 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11304 /* The size of the anonymous object containing
11305 the bit field is explicit, so use the
11306 indicated size (in bytes). */
11307 anonymous_size = DW_UNSND (attr);
11311 /* The size of the anonymous object containing
11312 the bit field must be inferred from the type
11313 attribute of the data member containing the
11315 anonymous_size = TYPE_LENGTH (fp->type);
11317 SET_FIELD_BITPOS (*fp,
11318 (FIELD_BITPOS (*fp)
11319 + anonymous_size * bits_per_byte
11320 - bit_offset - FIELD_BITSIZE (*fp)));
11324 /* Get name of field. */
11325 fieldname = dwarf2_name (die, cu);
11326 if (fieldname == NULL)
11329 /* The name is already allocated along with this objfile, so we don't
11330 need to duplicate it for the type. */
11331 fp->name = fieldname;
11333 /* Change accessibility for artificial fields (e.g. virtual table
11334 pointer or virtual base class pointer) to private. */
11335 if (dwarf2_attr (die, DW_AT_artificial, cu))
11337 FIELD_ARTIFICIAL (*fp) = 1;
11338 new_field->accessibility = DW_ACCESS_private;
11339 fip->non_public_fields = 1;
11342 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11344 /* C++ static member. */
11346 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11347 is a declaration, but all versions of G++ as of this writing
11348 (so through at least 3.2.1) incorrectly generate
11349 DW_TAG_variable tags. */
11351 const char *physname;
11353 /* Get name of field. */
11354 fieldname = dwarf2_name (die, cu);
11355 if (fieldname == NULL)
11358 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11360 /* Only create a symbol if this is an external value.
11361 new_symbol checks this and puts the value in the global symbol
11362 table, which we want. If it is not external, new_symbol
11363 will try to put the value in cu->list_in_scope which is wrong. */
11364 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11366 /* A static const member, not much different than an enum as far as
11367 we're concerned, except that we can support more types. */
11368 new_symbol (die, NULL, cu);
11371 /* Get physical name. */
11372 physname = dwarf2_physname (fieldname, die, cu);
11374 /* The name is already allocated along with this objfile, so we don't
11375 need to duplicate it for the type. */
11376 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
11377 FIELD_TYPE (*fp) = die_type (die, cu);
11378 FIELD_NAME (*fp) = fieldname;
11380 else if (die->tag == DW_TAG_inheritance)
11384 /* C++ base class field. */
11385 if (handle_data_member_location (die, cu, &offset))
11386 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11387 FIELD_BITSIZE (*fp) = 0;
11388 FIELD_TYPE (*fp) = die_type (die, cu);
11389 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11390 fip->nbaseclasses++;
11394 /* Add a typedef defined in the scope of the FIP's class. */
11397 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11398 struct dwarf2_cu *cu)
11400 struct objfile *objfile = cu->objfile;
11401 struct typedef_field_list *new_field;
11402 struct attribute *attr;
11403 struct typedef_field *fp;
11404 char *fieldname = "";
11406 /* Allocate a new field list entry and link it in. */
11407 new_field = xzalloc (sizeof (*new_field));
11408 make_cleanup (xfree, new_field);
11410 gdb_assert (die->tag == DW_TAG_typedef);
11412 fp = &new_field->field;
11414 /* Get name of field. */
11415 fp->name = dwarf2_name (die, cu);
11416 if (fp->name == NULL)
11419 fp->type = read_type_die (die, cu);
11421 new_field->next = fip->typedef_field_list;
11422 fip->typedef_field_list = new_field;
11423 fip->typedef_field_list_count++;
11426 /* Create the vector of fields, and attach it to the type. */
11429 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11430 struct dwarf2_cu *cu)
11432 int nfields = fip->nfields;
11434 /* Record the field count, allocate space for the array of fields,
11435 and create blank accessibility bitfields if necessary. */
11436 TYPE_NFIELDS (type) = nfields;
11437 TYPE_FIELDS (type) = (struct field *)
11438 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11439 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11441 if (fip->non_public_fields && cu->language != language_ada)
11443 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11445 TYPE_FIELD_PRIVATE_BITS (type) =
11446 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11447 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11449 TYPE_FIELD_PROTECTED_BITS (type) =
11450 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11451 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11453 TYPE_FIELD_IGNORE_BITS (type) =
11454 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11455 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
11458 /* If the type has baseclasses, allocate and clear a bit vector for
11459 TYPE_FIELD_VIRTUAL_BITS. */
11460 if (fip->nbaseclasses && cu->language != language_ada)
11462 int num_bytes = B_BYTES (fip->nbaseclasses);
11463 unsigned char *pointer;
11465 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11466 pointer = TYPE_ALLOC (type, num_bytes);
11467 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
11468 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11469 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11472 /* Copy the saved-up fields into the field vector. Start from the head of
11473 the list, adding to the tail of the field array, so that they end up in
11474 the same order in the array in which they were added to the list. */
11475 while (nfields-- > 0)
11477 struct nextfield *fieldp;
11481 fieldp = fip->fields;
11482 fip->fields = fieldp->next;
11486 fieldp = fip->baseclasses;
11487 fip->baseclasses = fieldp->next;
11490 TYPE_FIELD (type, nfields) = fieldp->field;
11491 switch (fieldp->accessibility)
11493 case DW_ACCESS_private:
11494 if (cu->language != language_ada)
11495 SET_TYPE_FIELD_PRIVATE (type, nfields);
11498 case DW_ACCESS_protected:
11499 if (cu->language != language_ada)
11500 SET_TYPE_FIELD_PROTECTED (type, nfields);
11503 case DW_ACCESS_public:
11507 /* Unknown accessibility. Complain and treat it as public. */
11509 complaint (&symfile_complaints, _("unsupported accessibility %d"),
11510 fieldp->accessibility);
11514 if (nfields < fip->nbaseclasses)
11516 switch (fieldp->virtuality)
11518 case DW_VIRTUALITY_virtual:
11519 case DW_VIRTUALITY_pure_virtual:
11520 if (cu->language == language_ada)
11521 error (_("unexpected virtuality in component of Ada type"));
11522 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11529 /* Return true if this member function is a constructor, false
11533 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11535 const char *fieldname;
11536 const char *typename;
11539 if (die->parent == NULL)
11542 if (die->parent->tag != DW_TAG_structure_type
11543 && die->parent->tag != DW_TAG_union_type
11544 && die->parent->tag != DW_TAG_class_type)
11547 fieldname = dwarf2_name (die, cu);
11548 typename = dwarf2_name (die->parent, cu);
11549 if (fieldname == NULL || typename == NULL)
11552 len = strlen (fieldname);
11553 return (strncmp (fieldname, typename, len) == 0
11554 && (typename[len] == '\0' || typename[len] == '<'));
11557 /* Add a member function to the proper fieldlist. */
11560 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11561 struct type *type, struct dwarf2_cu *cu)
11563 struct objfile *objfile = cu->objfile;
11564 struct attribute *attr;
11565 struct fnfieldlist *flp;
11567 struct fn_field *fnp;
11568 const char *fieldname;
11569 struct nextfnfield *new_fnfield;
11570 struct type *this_type;
11571 enum dwarf_access_attribute accessibility;
11573 if (cu->language == language_ada)
11574 error (_("unexpected member function in Ada type"));
11576 /* Get name of member function. */
11577 fieldname = dwarf2_name (die, cu);
11578 if (fieldname == NULL)
11581 /* Look up member function name in fieldlist. */
11582 for (i = 0; i < fip->nfnfields; i++)
11584 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11588 /* Create new list element if necessary. */
11589 if (i < fip->nfnfields)
11590 flp = &fip->fnfieldlists[i];
11593 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11595 fip->fnfieldlists = (struct fnfieldlist *)
11596 xrealloc (fip->fnfieldlists,
11597 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11598 * sizeof (struct fnfieldlist));
11599 if (fip->nfnfields == 0)
11600 make_cleanup (free_current_contents, &fip->fnfieldlists);
11602 flp = &fip->fnfieldlists[fip->nfnfields];
11603 flp->name = fieldname;
11606 i = fip->nfnfields++;
11609 /* Create a new member function field and chain it to the field list
11611 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11612 make_cleanup (xfree, new_fnfield);
11613 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11614 new_fnfield->next = flp->head;
11615 flp->head = new_fnfield;
11618 /* Fill in the member function field info. */
11619 fnp = &new_fnfield->fnfield;
11621 /* Delay processing of the physname until later. */
11622 if (cu->language == language_cplus || cu->language == language_java)
11624 add_to_method_list (type, i, flp->length - 1, fieldname,
11629 const char *physname = dwarf2_physname (fieldname, die, cu);
11630 fnp->physname = physname ? physname : "";
11633 fnp->type = alloc_type (objfile);
11634 this_type = read_type_die (die, cu);
11635 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11637 int nparams = TYPE_NFIELDS (this_type);
11639 /* TYPE is the domain of this method, and THIS_TYPE is the type
11640 of the method itself (TYPE_CODE_METHOD). */
11641 smash_to_method_type (fnp->type, type,
11642 TYPE_TARGET_TYPE (this_type),
11643 TYPE_FIELDS (this_type),
11644 TYPE_NFIELDS (this_type),
11645 TYPE_VARARGS (this_type));
11647 /* Handle static member functions.
11648 Dwarf2 has no clean way to discern C++ static and non-static
11649 member functions. G++ helps GDB by marking the first
11650 parameter for non-static member functions (which is the this
11651 pointer) as artificial. We obtain this information from
11652 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11653 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11654 fnp->voffset = VOFFSET_STATIC;
11657 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11658 dwarf2_full_name (fieldname, die, cu));
11660 /* Get fcontext from DW_AT_containing_type if present. */
11661 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11662 fnp->fcontext = die_containing_type (die, cu);
11664 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11665 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11667 /* Get accessibility. */
11668 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11670 accessibility = DW_UNSND (attr);
11672 accessibility = dwarf2_default_access_attribute (die, cu);
11673 switch (accessibility)
11675 case DW_ACCESS_private:
11676 fnp->is_private = 1;
11678 case DW_ACCESS_protected:
11679 fnp->is_protected = 1;
11683 /* Check for artificial methods. */
11684 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11685 if (attr && DW_UNSND (attr) != 0)
11686 fnp->is_artificial = 1;
11688 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11690 /* Get index in virtual function table if it is a virtual member
11691 function. For older versions of GCC, this is an offset in the
11692 appropriate virtual table, as specified by DW_AT_containing_type.
11693 For everyone else, it is an expression to be evaluated relative
11694 to the object address. */
11696 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11699 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11701 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11703 /* Old-style GCC. */
11704 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11706 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11707 || (DW_BLOCK (attr)->size > 1
11708 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11709 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11711 struct dwarf_block blk;
11714 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11716 blk.size = DW_BLOCK (attr)->size - offset;
11717 blk.data = DW_BLOCK (attr)->data + offset;
11718 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11719 if ((fnp->voffset % cu->header.addr_size) != 0)
11720 dwarf2_complex_location_expr_complaint ();
11722 fnp->voffset /= cu->header.addr_size;
11726 dwarf2_complex_location_expr_complaint ();
11728 if (!fnp->fcontext)
11729 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11731 else if (attr_form_is_section_offset (attr))
11733 dwarf2_complex_location_expr_complaint ();
11737 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11743 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11744 if (attr && DW_UNSND (attr))
11746 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11747 complaint (&symfile_complaints,
11748 _("Member function \"%s\" (offset %d) is virtual "
11749 "but the vtable offset is not specified"),
11750 fieldname, die->offset.sect_off);
11751 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11752 TYPE_CPLUS_DYNAMIC (type) = 1;
11757 /* Create the vector of member function fields, and attach it to the type. */
11760 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11761 struct dwarf2_cu *cu)
11763 struct fnfieldlist *flp;
11766 if (cu->language == language_ada)
11767 error (_("unexpected member functions in Ada type"));
11769 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11770 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11771 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11773 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11775 struct nextfnfield *nfp = flp->head;
11776 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11779 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11780 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11781 fn_flp->fn_fields = (struct fn_field *)
11782 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11783 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11784 fn_flp->fn_fields[k] = nfp->fnfield;
11787 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11790 /* Returns non-zero if NAME is the name of a vtable member in CU's
11791 language, zero otherwise. */
11793 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11795 static const char vptr[] = "_vptr";
11796 static const char vtable[] = "vtable";
11798 /* Look for the C++ and Java forms of the vtable. */
11799 if ((cu->language == language_java
11800 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11801 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11802 && is_cplus_marker (name[sizeof (vptr) - 1])))
11808 /* GCC outputs unnamed structures that are really pointers to member
11809 functions, with the ABI-specified layout. If TYPE describes
11810 such a structure, smash it into a member function type.
11812 GCC shouldn't do this; it should just output pointer to member DIEs.
11813 This is GCC PR debug/28767. */
11816 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11818 struct type *pfn_type, *domain_type, *new_type;
11820 /* Check for a structure with no name and two children. */
11821 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11824 /* Check for __pfn and __delta members. */
11825 if (TYPE_FIELD_NAME (type, 0) == NULL
11826 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11827 || TYPE_FIELD_NAME (type, 1) == NULL
11828 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11831 /* Find the type of the method. */
11832 pfn_type = TYPE_FIELD_TYPE (type, 0);
11833 if (pfn_type == NULL
11834 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11835 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11838 /* Look for the "this" argument. */
11839 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11840 if (TYPE_NFIELDS (pfn_type) == 0
11841 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11842 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11845 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11846 new_type = alloc_type (objfile);
11847 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11848 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11849 TYPE_VARARGS (pfn_type));
11850 smash_to_methodptr_type (type, new_type);
11853 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11857 producer_is_icc (struct dwarf2_cu *cu)
11859 if (!cu->checked_producer)
11860 check_producer (cu);
11862 return cu->producer_is_icc;
11865 /* Called when we find the DIE that starts a structure or union scope
11866 (definition) to create a type for the structure or union. Fill in
11867 the type's name and general properties; the members will not be
11868 processed until process_structure_scope.
11870 NOTE: we need to call these functions regardless of whether or not the
11871 DIE has a DW_AT_name attribute, since it might be an anonymous
11872 structure or union. This gets the type entered into our set of
11873 user defined types.
11875 However, if the structure is incomplete (an opaque struct/union)
11876 then suppress creating a symbol table entry for it since gdb only
11877 wants to find the one with the complete definition. Note that if
11878 it is complete, we just call new_symbol, which does it's own
11879 checking about whether the struct/union is anonymous or not (and
11880 suppresses creating a symbol table entry itself). */
11882 static struct type *
11883 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11885 struct objfile *objfile = cu->objfile;
11887 struct attribute *attr;
11890 /* If the definition of this type lives in .debug_types, read that type.
11891 Don't follow DW_AT_specification though, that will take us back up
11892 the chain and we want to go down. */
11893 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11896 type = get_DW_AT_signature_type (die, attr, cu);
11898 /* The type's CU may not be the same as CU.
11899 Ensure TYPE is recorded with CU in die_type_hash. */
11900 return set_die_type (die, type, cu);
11903 type = alloc_type (objfile);
11904 INIT_CPLUS_SPECIFIC (type);
11906 name = dwarf2_name (die, cu);
11909 if (cu->language == language_cplus
11910 || cu->language == language_java)
11912 const char *full_name = dwarf2_full_name (name, die, cu);
11914 /* dwarf2_full_name might have already finished building the DIE's
11915 type. If so, there is no need to continue. */
11916 if (get_die_type (die, cu) != NULL)
11917 return get_die_type (die, cu);
11919 TYPE_TAG_NAME (type) = full_name;
11920 if (die->tag == DW_TAG_structure_type
11921 || die->tag == DW_TAG_class_type)
11922 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11926 /* The name is already allocated along with this objfile, so
11927 we don't need to duplicate it for the type. */
11928 TYPE_TAG_NAME (type) = name;
11929 if (die->tag == DW_TAG_class_type)
11930 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11934 if (die->tag == DW_TAG_structure_type)
11936 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11938 else if (die->tag == DW_TAG_union_type)
11940 TYPE_CODE (type) = TYPE_CODE_UNION;
11944 TYPE_CODE (type) = TYPE_CODE_CLASS;
11947 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11948 TYPE_DECLARED_CLASS (type) = 1;
11950 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11953 TYPE_LENGTH (type) = DW_UNSND (attr);
11957 TYPE_LENGTH (type) = 0;
11960 if (producer_is_icc (cu))
11962 /* ICC does not output the required DW_AT_declaration
11963 on incomplete types, but gives them a size of zero. */
11966 TYPE_STUB_SUPPORTED (type) = 1;
11968 if (die_is_declaration (die, cu))
11969 TYPE_STUB (type) = 1;
11970 else if (attr == NULL && die->child == NULL
11971 && producer_is_realview (cu->producer))
11972 /* RealView does not output the required DW_AT_declaration
11973 on incomplete types. */
11974 TYPE_STUB (type) = 1;
11976 /* We need to add the type field to the die immediately so we don't
11977 infinitely recurse when dealing with pointers to the structure
11978 type within the structure itself. */
11979 set_die_type (die, type, cu);
11981 /* set_die_type should be already done. */
11982 set_descriptive_type (type, die, cu);
11987 /* Finish creating a structure or union type, including filling in
11988 its members and creating a symbol for it. */
11991 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11993 struct objfile *objfile = cu->objfile;
11994 struct die_info *child_die = die->child;
11997 type = get_die_type (die, cu);
11999 type = read_structure_type (die, cu);
12001 if (die->child != NULL && ! die_is_declaration (die, cu))
12003 struct field_info fi;
12004 struct die_info *child_die;
12005 VEC (symbolp) *template_args = NULL;
12006 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
12008 memset (&fi, 0, sizeof (struct field_info));
12010 child_die = die->child;
12012 while (child_die && child_die->tag)
12014 if (child_die->tag == DW_TAG_member
12015 || child_die->tag == DW_TAG_variable)
12017 /* NOTE: carlton/2002-11-05: A C++ static data member
12018 should be a DW_TAG_member that is a declaration, but
12019 all versions of G++ as of this writing (so through at
12020 least 3.2.1) incorrectly generate DW_TAG_variable
12021 tags for them instead. */
12022 dwarf2_add_field (&fi, child_die, cu);
12024 else if (child_die->tag == DW_TAG_subprogram)
12026 /* C++ member function. */
12027 dwarf2_add_member_fn (&fi, child_die, type, cu);
12029 else if (child_die->tag == DW_TAG_inheritance)
12031 /* C++ base class field. */
12032 dwarf2_add_field (&fi, child_die, cu);
12034 else if (child_die->tag == DW_TAG_typedef)
12035 dwarf2_add_typedef (&fi, child_die, cu);
12036 else if (child_die->tag == DW_TAG_template_type_param
12037 || child_die->tag == DW_TAG_template_value_param)
12039 struct symbol *arg = new_symbol (child_die, NULL, cu);
12042 VEC_safe_push (symbolp, template_args, arg);
12045 child_die = sibling_die (child_die);
12048 /* Attach template arguments to type. */
12049 if (! VEC_empty (symbolp, template_args))
12051 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12052 TYPE_N_TEMPLATE_ARGUMENTS (type)
12053 = VEC_length (symbolp, template_args);
12054 TYPE_TEMPLATE_ARGUMENTS (type)
12055 = obstack_alloc (&objfile->objfile_obstack,
12056 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12057 * sizeof (struct symbol *)));
12058 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12059 VEC_address (symbolp, template_args),
12060 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12061 * sizeof (struct symbol *)));
12062 VEC_free (symbolp, template_args);
12065 /* Attach fields and member functions to the type. */
12067 dwarf2_attach_fields_to_type (&fi, type, cu);
12070 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12072 /* Get the type which refers to the base class (possibly this
12073 class itself) which contains the vtable pointer for the current
12074 class from the DW_AT_containing_type attribute. This use of
12075 DW_AT_containing_type is a GNU extension. */
12077 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12079 struct type *t = die_containing_type (die, cu);
12081 TYPE_VPTR_BASETYPE (type) = t;
12086 /* Our own class provides vtbl ptr. */
12087 for (i = TYPE_NFIELDS (t) - 1;
12088 i >= TYPE_N_BASECLASSES (t);
12091 const char *fieldname = TYPE_FIELD_NAME (t, i);
12093 if (is_vtable_name (fieldname, cu))
12095 TYPE_VPTR_FIELDNO (type) = i;
12100 /* Complain if virtual function table field not found. */
12101 if (i < TYPE_N_BASECLASSES (t))
12102 complaint (&symfile_complaints,
12103 _("virtual function table pointer "
12104 "not found when defining class '%s'"),
12105 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12110 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12113 else if (cu->producer
12114 && strncmp (cu->producer,
12115 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12117 /* The IBM XLC compiler does not provide direct indication
12118 of the containing type, but the vtable pointer is
12119 always named __vfp. */
12123 for (i = TYPE_NFIELDS (type) - 1;
12124 i >= TYPE_N_BASECLASSES (type);
12127 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12129 TYPE_VPTR_FIELDNO (type) = i;
12130 TYPE_VPTR_BASETYPE (type) = type;
12137 /* Copy fi.typedef_field_list linked list elements content into the
12138 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12139 if (fi.typedef_field_list)
12141 int i = fi.typedef_field_list_count;
12143 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12144 TYPE_TYPEDEF_FIELD_ARRAY (type)
12145 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12146 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12148 /* Reverse the list order to keep the debug info elements order. */
12151 struct typedef_field *dest, *src;
12153 dest = &TYPE_TYPEDEF_FIELD (type, i);
12154 src = &fi.typedef_field_list->field;
12155 fi.typedef_field_list = fi.typedef_field_list->next;
12160 do_cleanups (back_to);
12162 if (HAVE_CPLUS_STRUCT (type))
12163 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12166 quirk_gcc_member_function_pointer (type, objfile);
12168 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12169 snapshots) has been known to create a die giving a declaration
12170 for a class that has, as a child, a die giving a definition for a
12171 nested class. So we have to process our children even if the
12172 current die is a declaration. Normally, of course, a declaration
12173 won't have any children at all. */
12175 while (child_die != NULL && child_die->tag)
12177 if (child_die->tag == DW_TAG_member
12178 || child_die->tag == DW_TAG_variable
12179 || child_die->tag == DW_TAG_inheritance
12180 || child_die->tag == DW_TAG_template_value_param
12181 || child_die->tag == DW_TAG_template_type_param)
12186 process_die (child_die, cu);
12188 child_die = sibling_die (child_die);
12191 /* Do not consider external references. According to the DWARF standard,
12192 these DIEs are identified by the fact that they have no byte_size
12193 attribute, and a declaration attribute. */
12194 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12195 || !die_is_declaration (die, cu))
12196 new_symbol (die, type, cu);
12199 /* Given a DW_AT_enumeration_type die, set its type. We do not
12200 complete the type's fields yet, or create any symbols. */
12202 static struct type *
12203 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12205 struct objfile *objfile = cu->objfile;
12207 struct attribute *attr;
12210 /* If the definition of this type lives in .debug_types, read that type.
12211 Don't follow DW_AT_specification though, that will take us back up
12212 the chain and we want to go down. */
12213 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12216 type = get_DW_AT_signature_type (die, attr, cu);
12218 /* The type's CU may not be the same as CU.
12219 Ensure TYPE is recorded with CU in die_type_hash. */
12220 return set_die_type (die, type, cu);
12223 type = alloc_type (objfile);
12225 TYPE_CODE (type) = TYPE_CODE_ENUM;
12226 name = dwarf2_full_name (NULL, die, cu);
12228 TYPE_TAG_NAME (type) = name;
12230 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12233 TYPE_LENGTH (type) = DW_UNSND (attr);
12237 TYPE_LENGTH (type) = 0;
12240 /* The enumeration DIE can be incomplete. In Ada, any type can be
12241 declared as private in the package spec, and then defined only
12242 inside the package body. Such types are known as Taft Amendment
12243 Types. When another package uses such a type, an incomplete DIE
12244 may be generated by the compiler. */
12245 if (die_is_declaration (die, cu))
12246 TYPE_STUB (type) = 1;
12248 return set_die_type (die, type, cu);
12251 /* Given a pointer to a die which begins an enumeration, process all
12252 the dies that define the members of the enumeration, and create the
12253 symbol for the enumeration type.
12255 NOTE: We reverse the order of the element list. */
12258 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12260 struct type *this_type;
12262 this_type = get_die_type (die, cu);
12263 if (this_type == NULL)
12264 this_type = read_enumeration_type (die, cu);
12266 if (die->child != NULL)
12268 struct die_info *child_die;
12269 struct symbol *sym;
12270 struct field *fields = NULL;
12271 int num_fields = 0;
12272 int unsigned_enum = 1;
12277 child_die = die->child;
12278 while (child_die && child_die->tag)
12280 if (child_die->tag != DW_TAG_enumerator)
12282 process_die (child_die, cu);
12286 name = dwarf2_name (child_die, cu);
12289 sym = new_symbol (child_die, this_type, cu);
12290 if (SYMBOL_VALUE (sym) < 0)
12295 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12298 mask |= SYMBOL_VALUE (sym);
12300 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12302 fields = (struct field *)
12304 (num_fields + DW_FIELD_ALLOC_CHUNK)
12305 * sizeof (struct field));
12308 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
12309 FIELD_TYPE (fields[num_fields]) = NULL;
12310 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
12311 FIELD_BITSIZE (fields[num_fields]) = 0;
12317 child_die = sibling_die (child_die);
12322 TYPE_NFIELDS (this_type) = num_fields;
12323 TYPE_FIELDS (this_type) = (struct field *)
12324 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12325 memcpy (TYPE_FIELDS (this_type), fields,
12326 sizeof (struct field) * num_fields);
12330 TYPE_UNSIGNED (this_type) = 1;
12332 TYPE_FLAG_ENUM (this_type) = 1;
12335 /* If we are reading an enum from a .debug_types unit, and the enum
12336 is a declaration, and the enum is not the signatured type in the
12337 unit, then we do not want to add a symbol for it. Adding a
12338 symbol would in some cases obscure the true definition of the
12339 enum, giving users an incomplete type when the definition is
12340 actually available. Note that we do not want to do this for all
12341 enums which are just declarations, because C++0x allows forward
12342 enum declarations. */
12343 if (cu->per_cu->is_debug_types
12344 && die_is_declaration (die, cu))
12346 struct signatured_type *sig_type;
12348 sig_type = (struct signatured_type *) cu->per_cu;
12349 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12350 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
12354 new_symbol (die, this_type, cu);
12357 /* Extract all information from a DW_TAG_array_type DIE and put it in
12358 the DIE's type field. For now, this only handles one dimensional
12361 static struct type *
12362 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
12364 struct objfile *objfile = cu->objfile;
12365 struct die_info *child_die;
12367 struct type *element_type, *range_type, *index_type;
12368 struct type **range_types = NULL;
12369 struct attribute *attr;
12371 struct cleanup *back_to;
12374 element_type = die_type (die, cu);
12376 /* The die_type call above may have already set the type for this DIE. */
12377 type = get_die_type (die, cu);
12381 /* Irix 6.2 native cc creates array types without children for
12382 arrays with unspecified length. */
12383 if (die->child == NULL)
12385 index_type = objfile_type (objfile)->builtin_int;
12386 range_type = create_range_type (NULL, index_type, 0, -1);
12387 type = create_array_type (NULL, element_type, range_type);
12388 return set_die_type (die, type, cu);
12391 back_to = make_cleanup (null_cleanup, NULL);
12392 child_die = die->child;
12393 while (child_die && child_die->tag)
12395 if (child_die->tag == DW_TAG_subrange_type)
12397 struct type *child_type = read_type_die (child_die, cu);
12399 if (child_type != NULL)
12401 /* The range type was succesfully read. Save it for the
12402 array type creation. */
12403 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12405 range_types = (struct type **)
12406 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12407 * sizeof (struct type *));
12409 make_cleanup (free_current_contents, &range_types);
12411 range_types[ndim++] = child_type;
12414 child_die = sibling_die (child_die);
12417 /* Dwarf2 dimensions are output from left to right, create the
12418 necessary array types in backwards order. */
12420 type = element_type;
12422 if (read_array_order (die, cu) == DW_ORD_col_major)
12427 type = create_array_type (NULL, type, range_types[i++]);
12432 type = create_array_type (NULL, type, range_types[ndim]);
12435 /* Understand Dwarf2 support for vector types (like they occur on
12436 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12437 array type. This is not part of the Dwarf2/3 standard yet, but a
12438 custom vendor extension. The main difference between a regular
12439 array and the vector variant is that vectors are passed by value
12441 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
12443 make_vector_type (type);
12445 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12446 implementation may choose to implement triple vectors using this
12448 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12451 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12452 TYPE_LENGTH (type) = DW_UNSND (attr);
12454 complaint (&symfile_complaints,
12455 _("DW_AT_byte_size for array type smaller "
12456 "than the total size of elements"));
12459 name = dwarf2_name (die, cu);
12461 TYPE_NAME (type) = name;
12463 /* Install the type in the die. */
12464 set_die_type (die, type, cu);
12466 /* set_die_type should be already done. */
12467 set_descriptive_type (type, die, cu);
12469 do_cleanups (back_to);
12474 static enum dwarf_array_dim_ordering
12475 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
12477 struct attribute *attr;
12479 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12481 if (attr) return DW_SND (attr);
12483 /* GNU F77 is a special case, as at 08/2004 array type info is the
12484 opposite order to the dwarf2 specification, but data is still
12485 laid out as per normal fortran.
12487 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12488 version checking. */
12490 if (cu->language == language_fortran
12491 && cu->producer && strstr (cu->producer, "GNU F77"))
12493 return DW_ORD_row_major;
12496 switch (cu->language_defn->la_array_ordering)
12498 case array_column_major:
12499 return DW_ORD_col_major;
12500 case array_row_major:
12502 return DW_ORD_row_major;
12506 /* Extract all information from a DW_TAG_set_type DIE and put it in
12507 the DIE's type field. */
12509 static struct type *
12510 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12512 struct type *domain_type, *set_type;
12513 struct attribute *attr;
12515 domain_type = die_type (die, cu);
12517 /* The die_type call above may have already set the type for this DIE. */
12518 set_type = get_die_type (die, cu);
12522 set_type = create_set_type (NULL, domain_type);
12524 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12526 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12528 return set_die_type (die, set_type, cu);
12531 /* A helper for read_common_block that creates a locexpr baton.
12532 SYM is the symbol which we are marking as computed.
12533 COMMON_DIE is the DIE for the common block.
12534 COMMON_LOC is the location expression attribute for the common
12536 MEMBER_LOC is the location expression attribute for the particular
12537 member of the common block that we are processing.
12538 CU is the CU from which the above come. */
12541 mark_common_block_symbol_computed (struct symbol *sym,
12542 struct die_info *common_die,
12543 struct attribute *common_loc,
12544 struct attribute *member_loc,
12545 struct dwarf2_cu *cu)
12547 struct objfile *objfile = dwarf2_per_objfile->objfile;
12548 struct dwarf2_locexpr_baton *baton;
12550 unsigned int cu_off;
12551 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12552 LONGEST offset = 0;
12554 gdb_assert (common_loc && member_loc);
12555 gdb_assert (attr_form_is_block (common_loc));
12556 gdb_assert (attr_form_is_block (member_loc)
12557 || attr_form_is_constant (member_loc));
12559 baton = obstack_alloc (&objfile->objfile_obstack,
12560 sizeof (struct dwarf2_locexpr_baton));
12561 baton->per_cu = cu->per_cu;
12562 gdb_assert (baton->per_cu);
12564 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12566 if (attr_form_is_constant (member_loc))
12568 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12569 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12572 baton->size += DW_BLOCK (member_loc)->size;
12574 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12577 *ptr++ = DW_OP_call4;
12578 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12579 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12582 if (attr_form_is_constant (member_loc))
12584 *ptr++ = DW_OP_addr;
12585 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12586 ptr += cu->header.addr_size;
12590 /* We have to copy the data here, because DW_OP_call4 will only
12591 use a DW_AT_location attribute. */
12592 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12593 ptr += DW_BLOCK (member_loc)->size;
12596 *ptr++ = DW_OP_plus;
12597 gdb_assert (ptr - baton->data == baton->size);
12599 SYMBOL_LOCATION_BATON (sym) = baton;
12600 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12603 /* Create appropriate locally-scoped variables for all the
12604 DW_TAG_common_block entries. Also create a struct common_block
12605 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12606 is used to sepate the common blocks name namespace from regular
12610 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12612 struct attribute *attr;
12614 attr = dwarf2_attr (die, DW_AT_location, cu);
12617 /* Support the .debug_loc offsets. */
12618 if (attr_form_is_block (attr))
12622 else if (attr_form_is_section_offset (attr))
12624 dwarf2_complex_location_expr_complaint ();
12629 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12630 "common block member");
12635 if (die->child != NULL)
12637 struct objfile *objfile = cu->objfile;
12638 struct die_info *child_die;
12639 size_t n_entries = 0, size;
12640 struct common_block *common_block;
12641 struct symbol *sym;
12643 for (child_die = die->child;
12644 child_die && child_die->tag;
12645 child_die = sibling_die (child_die))
12648 size = (sizeof (struct common_block)
12649 + (n_entries - 1) * sizeof (struct symbol *));
12650 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12651 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12652 common_block->n_entries = 0;
12654 for (child_die = die->child;
12655 child_die && child_die->tag;
12656 child_die = sibling_die (child_die))
12658 /* Create the symbol in the DW_TAG_common_block block in the current
12660 sym = new_symbol (child_die, NULL, cu);
12663 struct attribute *member_loc;
12665 common_block->contents[common_block->n_entries++] = sym;
12667 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12671 /* GDB has handled this for a long time, but it is
12672 not specified by DWARF. It seems to have been
12673 emitted by gfortran at least as recently as:
12674 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12675 complaint (&symfile_complaints,
12676 _("Variable in common block has "
12677 "DW_AT_data_member_location "
12678 "- DIE at 0x%x [in module %s]"),
12679 child_die->offset.sect_off,
12680 objfile_name (cu->objfile));
12682 if (attr_form_is_section_offset (member_loc))
12683 dwarf2_complex_location_expr_complaint ();
12684 else if (attr_form_is_constant (member_loc)
12685 || attr_form_is_block (member_loc))
12688 mark_common_block_symbol_computed (sym, die, attr,
12692 dwarf2_complex_location_expr_complaint ();
12697 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12698 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12702 /* Create a type for a C++ namespace. */
12704 static struct type *
12705 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12707 struct objfile *objfile = cu->objfile;
12708 const char *previous_prefix, *name;
12712 /* For extensions, reuse the type of the original namespace. */
12713 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12715 struct die_info *ext_die;
12716 struct dwarf2_cu *ext_cu = cu;
12718 ext_die = dwarf2_extension (die, &ext_cu);
12719 type = read_type_die (ext_die, ext_cu);
12721 /* EXT_CU may not be the same as CU.
12722 Ensure TYPE is recorded with CU in die_type_hash. */
12723 return set_die_type (die, type, cu);
12726 name = namespace_name (die, &is_anonymous, cu);
12728 /* Now build the name of the current namespace. */
12730 previous_prefix = determine_prefix (die, cu);
12731 if (previous_prefix[0] != '\0')
12732 name = typename_concat (&objfile->objfile_obstack,
12733 previous_prefix, name, 0, cu);
12735 /* Create the type. */
12736 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12738 TYPE_NAME (type) = name;
12739 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12741 return set_die_type (die, type, cu);
12744 /* Read a C++ namespace. */
12747 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12749 struct objfile *objfile = cu->objfile;
12752 /* Add a symbol associated to this if we haven't seen the namespace
12753 before. Also, add a using directive if it's an anonymous
12756 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12760 type = read_type_die (die, cu);
12761 new_symbol (die, type, cu);
12763 namespace_name (die, &is_anonymous, cu);
12766 const char *previous_prefix = determine_prefix (die, cu);
12768 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12769 NULL, NULL, 0, &objfile->objfile_obstack);
12773 if (die->child != NULL)
12775 struct die_info *child_die = die->child;
12777 while (child_die && child_die->tag)
12779 process_die (child_die, cu);
12780 child_die = sibling_die (child_die);
12785 /* Read a Fortran module as type. This DIE can be only a declaration used for
12786 imported module. Still we need that type as local Fortran "use ... only"
12787 declaration imports depend on the created type in determine_prefix. */
12789 static struct type *
12790 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12792 struct objfile *objfile = cu->objfile;
12793 const char *module_name;
12796 module_name = dwarf2_name (die, cu);
12798 complaint (&symfile_complaints,
12799 _("DW_TAG_module has no name, offset 0x%x"),
12800 die->offset.sect_off);
12801 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12803 /* determine_prefix uses TYPE_TAG_NAME. */
12804 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12806 return set_die_type (die, type, cu);
12809 /* Read a Fortran module. */
12812 read_module (struct die_info *die, struct dwarf2_cu *cu)
12814 struct die_info *child_die = die->child;
12816 while (child_die && child_die->tag)
12818 process_die (child_die, cu);
12819 child_die = sibling_die (child_die);
12823 /* Return the name of the namespace represented by DIE. Set
12824 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12827 static const char *
12828 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12830 struct die_info *current_die;
12831 const char *name = NULL;
12833 /* Loop through the extensions until we find a name. */
12835 for (current_die = die;
12836 current_die != NULL;
12837 current_die = dwarf2_extension (die, &cu))
12839 name = dwarf2_name (current_die, cu);
12844 /* Is it an anonymous namespace? */
12846 *is_anonymous = (name == NULL);
12848 name = CP_ANONYMOUS_NAMESPACE_STR;
12853 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12854 the user defined type vector. */
12856 static struct type *
12857 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12859 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12860 struct comp_unit_head *cu_header = &cu->header;
12862 struct attribute *attr_byte_size;
12863 struct attribute *attr_address_class;
12864 int byte_size, addr_class;
12865 struct type *target_type;
12867 target_type = die_type (die, cu);
12869 /* The die_type call above may have already set the type for this DIE. */
12870 type = get_die_type (die, cu);
12874 type = lookup_pointer_type (target_type);
12876 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12877 if (attr_byte_size)
12878 byte_size = DW_UNSND (attr_byte_size);
12880 byte_size = cu_header->addr_size;
12882 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12883 if (attr_address_class)
12884 addr_class = DW_UNSND (attr_address_class);
12886 addr_class = DW_ADDR_none;
12888 /* If the pointer size or address class is different than the
12889 default, create a type variant marked as such and set the
12890 length accordingly. */
12891 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12893 if (gdbarch_address_class_type_flags_p (gdbarch))
12897 type_flags = gdbarch_address_class_type_flags
12898 (gdbarch, byte_size, addr_class);
12899 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12901 type = make_type_with_address_space (type, type_flags);
12903 else if (TYPE_LENGTH (type) != byte_size)
12905 complaint (&symfile_complaints,
12906 _("invalid pointer size %d"), byte_size);
12910 /* Should we also complain about unhandled address classes? */
12914 TYPE_LENGTH (type) = byte_size;
12915 return set_die_type (die, type, cu);
12918 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12919 the user defined type vector. */
12921 static struct type *
12922 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12925 struct type *to_type;
12926 struct type *domain;
12928 to_type = die_type (die, cu);
12929 domain = die_containing_type (die, cu);
12931 /* The calls above may have already set the type for this DIE. */
12932 type = get_die_type (die, cu);
12936 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12937 type = lookup_methodptr_type (to_type);
12938 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12940 struct type *new_type = alloc_type (cu->objfile);
12942 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12943 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12944 TYPE_VARARGS (to_type));
12945 type = lookup_methodptr_type (new_type);
12948 type = lookup_memberptr_type (to_type, domain);
12950 return set_die_type (die, type, cu);
12953 /* Extract all information from a DW_TAG_reference_type DIE and add to
12954 the user defined type vector. */
12956 static struct type *
12957 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12959 struct comp_unit_head *cu_header = &cu->header;
12960 struct type *type, *target_type;
12961 struct attribute *attr;
12963 target_type = die_type (die, cu);
12965 /* The die_type call above may have already set the type for this DIE. */
12966 type = get_die_type (die, cu);
12970 type = lookup_reference_type (target_type);
12971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12974 TYPE_LENGTH (type) = DW_UNSND (attr);
12978 TYPE_LENGTH (type) = cu_header->addr_size;
12980 return set_die_type (die, type, cu);
12983 static struct type *
12984 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12986 struct type *base_type, *cv_type;
12988 base_type = die_type (die, cu);
12990 /* The die_type call above may have already set the type for this DIE. */
12991 cv_type = get_die_type (die, cu);
12995 /* In case the const qualifier is applied to an array type, the element type
12996 is so qualified, not the array type (section 6.7.3 of C99). */
12997 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12999 struct type *el_type, *inner_array;
13001 base_type = copy_type (base_type);
13002 inner_array = base_type;
13004 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
13006 TYPE_TARGET_TYPE (inner_array) =
13007 copy_type (TYPE_TARGET_TYPE (inner_array));
13008 inner_array = TYPE_TARGET_TYPE (inner_array);
13011 el_type = TYPE_TARGET_TYPE (inner_array);
13012 TYPE_TARGET_TYPE (inner_array) =
13013 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
13015 return set_die_type (die, base_type, cu);
13018 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13019 return set_die_type (die, cv_type, cu);
13022 static struct type *
13023 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
13025 struct type *base_type, *cv_type;
13027 base_type = die_type (die, cu);
13029 /* The die_type call above may have already set the type for this DIE. */
13030 cv_type = get_die_type (die, cu);
13034 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13035 return set_die_type (die, cv_type, cu);
13038 /* Handle DW_TAG_restrict_type. */
13040 static struct type *
13041 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13043 struct type *base_type, *cv_type;
13045 base_type = die_type (die, cu);
13047 /* The die_type call above may have already set the type for this DIE. */
13048 cv_type = get_die_type (die, cu);
13052 cv_type = make_restrict_type (base_type);
13053 return set_die_type (die, cv_type, cu);
13056 /* Extract all information from a DW_TAG_string_type DIE and add to
13057 the user defined type vector. It isn't really a user defined type,
13058 but it behaves like one, with other DIE's using an AT_user_def_type
13059 attribute to reference it. */
13061 static struct type *
13062 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
13064 struct objfile *objfile = cu->objfile;
13065 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13066 struct type *type, *range_type, *index_type, *char_type;
13067 struct attribute *attr;
13068 unsigned int length;
13070 attr = dwarf2_attr (die, DW_AT_string_length, cu);
13073 length = DW_UNSND (attr);
13077 /* Check for the DW_AT_byte_size attribute. */
13078 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13081 length = DW_UNSND (attr);
13089 index_type = objfile_type (objfile)->builtin_int;
13090 range_type = create_range_type (NULL, index_type, 1, length);
13091 char_type = language_string_char_type (cu->language_defn, gdbarch);
13092 type = create_string_type (NULL, char_type, range_type);
13094 return set_die_type (die, type, cu);
13097 /* Assuming that DIE corresponds to a function, returns nonzero
13098 if the function is prototyped. */
13101 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13103 struct attribute *attr;
13105 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13106 if (attr && (DW_UNSND (attr) != 0))
13109 /* The DWARF standard implies that the DW_AT_prototyped attribute
13110 is only meaninful for C, but the concept also extends to other
13111 languages that allow unprototyped functions (Eg: Objective C).
13112 For all other languages, assume that functions are always
13114 if (cu->language != language_c
13115 && cu->language != language_objc
13116 && cu->language != language_opencl)
13119 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13120 prototyped and unprototyped functions; default to prototyped,
13121 since that is more common in modern code (and RealView warns
13122 about unprototyped functions). */
13123 if (producer_is_realview (cu->producer))
13129 /* Handle DIES due to C code like:
13133 int (*funcp)(int a, long l);
13137 ('funcp' generates a DW_TAG_subroutine_type DIE). */
13139 static struct type *
13140 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
13142 struct objfile *objfile = cu->objfile;
13143 struct type *type; /* Type that this function returns. */
13144 struct type *ftype; /* Function that returns above type. */
13145 struct attribute *attr;
13147 type = die_type (die, cu);
13149 /* The die_type call above may have already set the type for this DIE. */
13150 ftype = get_die_type (die, cu);
13154 ftype = lookup_function_type (type);
13156 if (prototyped_function_p (die, cu))
13157 TYPE_PROTOTYPED (ftype) = 1;
13159 /* Store the calling convention in the type if it's available in
13160 the subroutine die. Otherwise set the calling convention to
13161 the default value DW_CC_normal. */
13162 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13164 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13165 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13166 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13168 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13170 /* We need to add the subroutine type to the die immediately so
13171 we don't infinitely recurse when dealing with parameters
13172 declared as the same subroutine type. */
13173 set_die_type (die, ftype, cu);
13175 if (die->child != NULL)
13177 struct type *void_type = objfile_type (objfile)->builtin_void;
13178 struct die_info *child_die;
13179 int nparams, iparams;
13181 /* Count the number of parameters.
13182 FIXME: GDB currently ignores vararg functions, but knows about
13183 vararg member functions. */
13185 child_die = die->child;
13186 while (child_die && child_die->tag)
13188 if (child_die->tag == DW_TAG_formal_parameter)
13190 else if (child_die->tag == DW_TAG_unspecified_parameters)
13191 TYPE_VARARGS (ftype) = 1;
13192 child_die = sibling_die (child_die);
13195 /* Allocate storage for parameters and fill them in. */
13196 TYPE_NFIELDS (ftype) = nparams;
13197 TYPE_FIELDS (ftype) = (struct field *)
13198 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13200 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13201 even if we error out during the parameters reading below. */
13202 for (iparams = 0; iparams < nparams; iparams++)
13203 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13206 child_die = die->child;
13207 while (child_die && child_die->tag)
13209 if (child_die->tag == DW_TAG_formal_parameter)
13211 struct type *arg_type;
13213 /* DWARF version 2 has no clean way to discern C++
13214 static and non-static member functions. G++ helps
13215 GDB by marking the first parameter for non-static
13216 member functions (which is the this pointer) as
13217 artificial. We pass this information to
13218 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13220 DWARF version 3 added DW_AT_object_pointer, which GCC
13221 4.5 does not yet generate. */
13222 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13224 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13227 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13229 /* GCC/43521: In java, the formal parameter
13230 "this" is sometimes not marked with DW_AT_artificial. */
13231 if (cu->language == language_java)
13233 const char *name = dwarf2_name (child_die, cu);
13235 if (name && !strcmp (name, "this"))
13236 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13239 arg_type = die_type (child_die, cu);
13241 /* RealView does not mark THIS as const, which the testsuite
13242 expects. GCC marks THIS as const in method definitions,
13243 but not in the class specifications (GCC PR 43053). */
13244 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13245 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13248 struct dwarf2_cu *arg_cu = cu;
13249 const char *name = dwarf2_name (child_die, cu);
13251 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13254 /* If the compiler emits this, use it. */
13255 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13258 else if (name && strcmp (name, "this") == 0)
13259 /* Function definitions will have the argument names. */
13261 else if (name == NULL && iparams == 0)
13262 /* Declarations may not have the names, so like
13263 elsewhere in GDB, assume an artificial first
13264 argument is "this". */
13268 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13272 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
13275 child_die = sibling_die (child_die);
13282 static struct type *
13283 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
13285 struct objfile *objfile = cu->objfile;
13286 const char *name = NULL;
13287 struct type *this_type, *target_type;
13289 name = dwarf2_full_name (NULL, die, cu);
13290 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
13291 TYPE_FLAG_TARGET_STUB, NULL, objfile);
13292 TYPE_NAME (this_type) = name;
13293 set_die_type (die, this_type, cu);
13294 target_type = die_type (die, cu);
13295 if (target_type != this_type)
13296 TYPE_TARGET_TYPE (this_type) = target_type;
13299 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13300 spec and cause infinite loops in GDB. */
13301 complaint (&symfile_complaints,
13302 _("Self-referential DW_TAG_typedef "
13303 "- DIE at 0x%x [in module %s]"),
13304 die->offset.sect_off, objfile_name (objfile));
13305 TYPE_TARGET_TYPE (this_type) = NULL;
13310 /* Find a representation of a given base type and install
13311 it in the TYPE field of the die. */
13313 static struct type *
13314 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
13316 struct objfile *objfile = cu->objfile;
13318 struct attribute *attr;
13319 int encoding = 0, size = 0;
13321 enum type_code code = TYPE_CODE_INT;
13322 int type_flags = 0;
13323 struct type *target_type = NULL;
13325 attr = dwarf2_attr (die, DW_AT_encoding, cu);
13328 encoding = DW_UNSND (attr);
13330 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13333 size = DW_UNSND (attr);
13335 name = dwarf2_name (die, cu);
13338 complaint (&symfile_complaints,
13339 _("DW_AT_name missing from DW_TAG_base_type"));
13344 case DW_ATE_address:
13345 /* Turn DW_ATE_address into a void * pointer. */
13346 code = TYPE_CODE_PTR;
13347 type_flags |= TYPE_FLAG_UNSIGNED;
13348 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13350 case DW_ATE_boolean:
13351 code = TYPE_CODE_BOOL;
13352 type_flags |= TYPE_FLAG_UNSIGNED;
13354 case DW_ATE_complex_float:
13355 code = TYPE_CODE_COMPLEX;
13356 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13358 case DW_ATE_decimal_float:
13359 code = TYPE_CODE_DECFLOAT;
13362 code = TYPE_CODE_FLT;
13364 case DW_ATE_signed:
13366 case DW_ATE_unsigned:
13367 type_flags |= TYPE_FLAG_UNSIGNED;
13368 if (cu->language == language_fortran
13370 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13371 code = TYPE_CODE_CHAR;
13373 case DW_ATE_signed_char:
13374 if (cu->language == language_ada || cu->language == language_m2
13375 || cu->language == language_pascal
13376 || cu->language == language_fortran)
13377 code = TYPE_CODE_CHAR;
13379 case DW_ATE_unsigned_char:
13380 if (cu->language == language_ada || cu->language == language_m2
13381 || cu->language == language_pascal
13382 || cu->language == language_fortran)
13383 code = TYPE_CODE_CHAR;
13384 type_flags |= TYPE_FLAG_UNSIGNED;
13387 /* We just treat this as an integer and then recognize the
13388 type by name elsewhere. */
13392 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13393 dwarf_type_encoding_name (encoding));
13397 type = init_type (code, size, type_flags, NULL, objfile);
13398 TYPE_NAME (type) = name;
13399 TYPE_TARGET_TYPE (type) = target_type;
13401 if (name && strcmp (name, "char") == 0)
13402 TYPE_NOSIGN (type) = 1;
13404 return set_die_type (die, type, cu);
13407 /* Read the given DW_AT_subrange DIE. */
13409 static struct type *
13410 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13412 struct type *base_type, *orig_base_type;
13413 struct type *range_type;
13414 struct attribute *attr;
13416 int low_default_is_valid;
13418 LONGEST negative_mask;
13420 orig_base_type = die_type (die, cu);
13421 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13422 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13423 creating the range type, but we use the result of check_typedef
13424 when examining properties of the type. */
13425 base_type = check_typedef (orig_base_type);
13427 /* The die_type call above may have already set the type for this DIE. */
13428 range_type = get_die_type (die, cu);
13432 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13433 omitting DW_AT_lower_bound. */
13434 switch (cu->language)
13437 case language_cplus:
13439 low_default_is_valid = 1;
13441 case language_fortran:
13443 low_default_is_valid = 1;
13446 case language_java:
13447 case language_objc:
13449 low_default_is_valid = (cu->header.version >= 4);
13453 case language_pascal:
13455 low_default_is_valid = (cu->header.version >= 4);
13459 low_default_is_valid = 0;
13463 /* FIXME: For variable sized arrays either of these could be
13464 a variable rather than a constant value. We'll allow it,
13465 but we don't know how to handle it. */
13466 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
13468 low = dwarf2_get_attr_constant_value (attr, low);
13469 else if (!low_default_is_valid)
13470 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13471 "- DIE at 0x%x [in module %s]"),
13472 die->offset.sect_off, objfile_name (cu->objfile));
13474 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
13477 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
13479 /* GCC encodes arrays with unspecified or dynamic length
13480 with a DW_FORM_block1 attribute or a reference attribute.
13481 FIXME: GDB does not yet know how to handle dynamic
13482 arrays properly, treat them as arrays with unspecified
13485 FIXME: jimb/2003-09-22: GDB does not really know
13486 how to handle arrays of unspecified length
13487 either; we just represent them as zero-length
13488 arrays. Choose an appropriate upper bound given
13489 the lower bound we've computed above. */
13493 high = dwarf2_get_attr_constant_value (attr, 1);
13497 attr = dwarf2_attr (die, DW_AT_count, cu);
13500 int count = dwarf2_get_attr_constant_value (attr, 1);
13501 high = low + count - 1;
13505 /* Unspecified array length. */
13510 /* Dwarf-2 specifications explicitly allows to create subrange types
13511 without specifying a base type.
13512 In that case, the base type must be set to the type of
13513 the lower bound, upper bound or count, in that order, if any of these
13514 three attributes references an object that has a type.
13515 If no base type is found, the Dwarf-2 specifications say that
13516 a signed integer type of size equal to the size of an address should
13518 For the following C code: `extern char gdb_int [];'
13519 GCC produces an empty range DIE.
13520 FIXME: muller/2010-05-28: Possible references to object for low bound,
13521 high bound or count are not yet handled by this code. */
13522 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13524 struct objfile *objfile = cu->objfile;
13525 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13526 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13527 struct type *int_type = objfile_type (objfile)->builtin_int;
13529 /* Test "int", "long int", and "long long int" objfile types,
13530 and select the first one having a size above or equal to the
13531 architecture address size. */
13532 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13533 base_type = int_type;
13536 int_type = objfile_type (objfile)->builtin_long;
13537 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13538 base_type = int_type;
13541 int_type = objfile_type (objfile)->builtin_long_long;
13542 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13543 base_type = int_type;
13549 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13550 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13551 low |= negative_mask;
13552 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13553 high |= negative_mask;
13555 range_type = create_range_type (NULL, orig_base_type, low, high);
13557 /* Mark arrays with dynamic length at least as an array of unspecified
13558 length. GDB could check the boundary but before it gets implemented at
13559 least allow accessing the array elements. */
13560 if (attr && attr_form_is_block (attr))
13561 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13563 /* Ada expects an empty array on no boundary attributes. */
13564 if (attr == NULL && cu->language != language_ada)
13565 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13567 name = dwarf2_name (die, cu);
13569 TYPE_NAME (range_type) = name;
13571 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13573 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13575 set_die_type (die, range_type, cu);
13577 /* set_die_type should be already done. */
13578 set_descriptive_type (range_type, die, cu);
13583 static struct type *
13584 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13588 /* For now, we only support the C meaning of an unspecified type: void. */
13590 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13591 TYPE_NAME (type) = dwarf2_name (die, cu);
13593 return set_die_type (die, type, cu);
13596 /* Read a single die and all its descendents. Set the die's sibling
13597 field to NULL; set other fields in the die correctly, and set all
13598 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13599 location of the info_ptr after reading all of those dies. PARENT
13600 is the parent of the die in question. */
13602 static struct die_info *
13603 read_die_and_children (const struct die_reader_specs *reader,
13604 const gdb_byte *info_ptr,
13605 const gdb_byte **new_info_ptr,
13606 struct die_info *parent)
13608 struct die_info *die;
13609 const gdb_byte *cur_ptr;
13612 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13615 *new_info_ptr = cur_ptr;
13618 store_in_ref_table (die, reader->cu);
13621 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13625 *new_info_ptr = cur_ptr;
13628 die->sibling = NULL;
13629 die->parent = parent;
13633 /* Read a die, all of its descendents, and all of its siblings; set
13634 all of the fields of all of the dies correctly. Arguments are as
13635 in read_die_and_children. */
13637 static struct die_info *
13638 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13639 const gdb_byte *info_ptr,
13640 const gdb_byte **new_info_ptr,
13641 struct die_info *parent)
13643 struct die_info *first_die, *last_sibling;
13644 const gdb_byte *cur_ptr;
13646 cur_ptr = info_ptr;
13647 first_die = last_sibling = NULL;
13651 struct die_info *die
13652 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13656 *new_info_ptr = cur_ptr;
13663 last_sibling->sibling = die;
13665 last_sibling = die;
13669 /* Read a die, all of its descendents, and all of its siblings; set
13670 all of the fields of all of the dies correctly. Arguments are as
13671 in read_die_and_children.
13672 This the main entry point for reading a DIE and all its children. */
13674 static struct die_info *
13675 read_die_and_siblings (const struct die_reader_specs *reader,
13676 const gdb_byte *info_ptr,
13677 const gdb_byte **new_info_ptr,
13678 struct die_info *parent)
13680 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13681 new_info_ptr, parent);
13683 if (dwarf2_die_debug)
13685 fprintf_unfiltered (gdb_stdlog,
13686 "Read die from %s@0x%x of %s:\n",
13687 bfd_section_name (reader->abfd,
13688 reader->die_section->asection),
13689 (unsigned) (info_ptr - reader->die_section->buffer),
13690 bfd_get_filename (reader->abfd));
13691 dump_die (die, dwarf2_die_debug);
13697 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13699 The caller is responsible for filling in the extra attributes
13700 and updating (*DIEP)->num_attrs.
13701 Set DIEP to point to a newly allocated die with its information,
13702 except for its child, sibling, and parent fields.
13703 Set HAS_CHILDREN to tell whether the die has children or not. */
13705 static const gdb_byte *
13706 read_full_die_1 (const struct die_reader_specs *reader,
13707 struct die_info **diep, const gdb_byte *info_ptr,
13708 int *has_children, int num_extra_attrs)
13710 unsigned int abbrev_number, bytes_read, i;
13711 sect_offset offset;
13712 struct abbrev_info *abbrev;
13713 struct die_info *die;
13714 struct dwarf2_cu *cu = reader->cu;
13715 bfd *abfd = reader->abfd;
13717 offset.sect_off = info_ptr - reader->buffer;
13718 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13719 info_ptr += bytes_read;
13720 if (!abbrev_number)
13727 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13729 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13731 bfd_get_filename (abfd));
13733 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13734 die->offset = offset;
13735 die->tag = abbrev->tag;
13736 die->abbrev = abbrev_number;
13738 /* Make the result usable.
13739 The caller needs to update num_attrs after adding the extra
13741 die->num_attrs = abbrev->num_attrs;
13743 for (i = 0; i < abbrev->num_attrs; ++i)
13744 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13748 *has_children = abbrev->has_children;
13752 /* Read a die and all its attributes.
13753 Set DIEP to point to a newly allocated die with its information,
13754 except for its child, sibling, and parent fields.
13755 Set HAS_CHILDREN to tell whether the die has children or not. */
13757 static const gdb_byte *
13758 read_full_die (const struct die_reader_specs *reader,
13759 struct die_info **diep, const gdb_byte *info_ptr,
13762 const gdb_byte *result;
13764 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13766 if (dwarf2_die_debug)
13768 fprintf_unfiltered (gdb_stdlog,
13769 "Read die from %s@0x%x of %s:\n",
13770 bfd_section_name (reader->abfd,
13771 reader->die_section->asection),
13772 (unsigned) (info_ptr - reader->die_section->buffer),
13773 bfd_get_filename (reader->abfd));
13774 dump_die (*diep, dwarf2_die_debug);
13780 /* Abbreviation tables.
13782 In DWARF version 2, the description of the debugging information is
13783 stored in a separate .debug_abbrev section. Before we read any
13784 dies from a section we read in all abbreviations and install them
13785 in a hash table. */
13787 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13789 static struct abbrev_info *
13790 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13792 struct abbrev_info *abbrev;
13794 abbrev = (struct abbrev_info *)
13795 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13796 memset (abbrev, 0, sizeof (struct abbrev_info));
13800 /* Add an abbreviation to the table. */
13803 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13804 unsigned int abbrev_number,
13805 struct abbrev_info *abbrev)
13807 unsigned int hash_number;
13809 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13810 abbrev->next = abbrev_table->abbrevs[hash_number];
13811 abbrev_table->abbrevs[hash_number] = abbrev;
13814 /* Look up an abbrev in the table.
13815 Returns NULL if the abbrev is not found. */
13817 static struct abbrev_info *
13818 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13819 unsigned int abbrev_number)
13821 unsigned int hash_number;
13822 struct abbrev_info *abbrev;
13824 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13825 abbrev = abbrev_table->abbrevs[hash_number];
13829 if (abbrev->number == abbrev_number)
13831 abbrev = abbrev->next;
13836 /* Read in an abbrev table. */
13838 static struct abbrev_table *
13839 abbrev_table_read_table (struct dwarf2_section_info *section,
13840 sect_offset offset)
13842 struct objfile *objfile = dwarf2_per_objfile->objfile;
13843 bfd *abfd = section->asection->owner;
13844 struct abbrev_table *abbrev_table;
13845 const gdb_byte *abbrev_ptr;
13846 struct abbrev_info *cur_abbrev;
13847 unsigned int abbrev_number, bytes_read, abbrev_name;
13848 unsigned int abbrev_form;
13849 struct attr_abbrev *cur_attrs;
13850 unsigned int allocated_attrs;
13852 abbrev_table = XMALLOC (struct abbrev_table);
13853 abbrev_table->offset = offset;
13854 obstack_init (&abbrev_table->abbrev_obstack);
13855 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13857 * sizeof (struct abbrev_info *)));
13858 memset (abbrev_table->abbrevs, 0,
13859 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13861 dwarf2_read_section (objfile, section);
13862 abbrev_ptr = section->buffer + offset.sect_off;
13863 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13864 abbrev_ptr += bytes_read;
13866 allocated_attrs = ATTR_ALLOC_CHUNK;
13867 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13869 /* Loop until we reach an abbrev number of 0. */
13870 while (abbrev_number)
13872 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13874 /* read in abbrev header */
13875 cur_abbrev->number = abbrev_number;
13876 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13877 abbrev_ptr += bytes_read;
13878 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13881 /* now read in declarations */
13882 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13883 abbrev_ptr += bytes_read;
13884 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13885 abbrev_ptr += bytes_read;
13886 while (abbrev_name)
13888 if (cur_abbrev->num_attrs == allocated_attrs)
13890 allocated_attrs += ATTR_ALLOC_CHUNK;
13892 = xrealloc (cur_attrs, (allocated_attrs
13893 * sizeof (struct attr_abbrev)));
13896 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13897 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13898 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13899 abbrev_ptr += bytes_read;
13900 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13901 abbrev_ptr += bytes_read;
13904 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13905 (cur_abbrev->num_attrs
13906 * sizeof (struct attr_abbrev)));
13907 memcpy (cur_abbrev->attrs, cur_attrs,
13908 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13910 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13912 /* Get next abbreviation.
13913 Under Irix6 the abbreviations for a compilation unit are not
13914 always properly terminated with an abbrev number of 0.
13915 Exit loop if we encounter an abbreviation which we have
13916 already read (which means we are about to read the abbreviations
13917 for the next compile unit) or if the end of the abbreviation
13918 table is reached. */
13919 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13921 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13922 abbrev_ptr += bytes_read;
13923 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13928 return abbrev_table;
13931 /* Free the resources held by ABBREV_TABLE. */
13934 abbrev_table_free (struct abbrev_table *abbrev_table)
13936 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13937 xfree (abbrev_table);
13940 /* Same as abbrev_table_free but as a cleanup.
13941 We pass in a pointer to the pointer to the table so that we can
13942 set the pointer to NULL when we're done. It also simplifies
13943 build_type_unit_groups. */
13946 abbrev_table_free_cleanup (void *table_ptr)
13948 struct abbrev_table **abbrev_table_ptr = table_ptr;
13950 if (*abbrev_table_ptr != NULL)
13951 abbrev_table_free (*abbrev_table_ptr);
13952 *abbrev_table_ptr = NULL;
13955 /* Read the abbrev table for CU from ABBREV_SECTION. */
13958 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13959 struct dwarf2_section_info *abbrev_section)
13962 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13965 /* Release the memory used by the abbrev table for a compilation unit. */
13968 dwarf2_free_abbrev_table (void *ptr_to_cu)
13970 struct dwarf2_cu *cu = ptr_to_cu;
13972 if (cu->abbrev_table != NULL)
13973 abbrev_table_free (cu->abbrev_table);
13974 /* Set this to NULL so that we SEGV if we try to read it later,
13975 and also because free_comp_unit verifies this is NULL. */
13976 cu->abbrev_table = NULL;
13979 /* Returns nonzero if TAG represents a type that we might generate a partial
13983 is_type_tag_for_partial (int tag)
13988 /* Some types that would be reasonable to generate partial symbols for,
13989 that we don't at present. */
13990 case DW_TAG_array_type:
13991 case DW_TAG_file_type:
13992 case DW_TAG_ptr_to_member_type:
13993 case DW_TAG_set_type:
13994 case DW_TAG_string_type:
13995 case DW_TAG_subroutine_type:
13997 case DW_TAG_base_type:
13998 case DW_TAG_class_type:
13999 case DW_TAG_interface_type:
14000 case DW_TAG_enumeration_type:
14001 case DW_TAG_structure_type:
14002 case DW_TAG_subrange_type:
14003 case DW_TAG_typedef:
14004 case DW_TAG_union_type:
14011 /* Load all DIEs that are interesting for partial symbols into memory. */
14013 static struct partial_die_info *
14014 load_partial_dies (const struct die_reader_specs *reader,
14015 const gdb_byte *info_ptr, int building_psymtab)
14017 struct dwarf2_cu *cu = reader->cu;
14018 struct objfile *objfile = cu->objfile;
14019 struct partial_die_info *part_die;
14020 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14021 struct abbrev_info *abbrev;
14022 unsigned int bytes_read;
14023 unsigned int load_all = 0;
14024 int nesting_level = 1;
14029 gdb_assert (cu->per_cu != NULL);
14030 if (cu->per_cu->load_all_dies)
14034 = htab_create_alloc_ex (cu->header.length / 12,
14038 &cu->comp_unit_obstack,
14039 hashtab_obstack_allocate,
14040 dummy_obstack_deallocate);
14042 part_die = obstack_alloc (&cu->comp_unit_obstack,
14043 sizeof (struct partial_die_info));
14047 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14049 /* A NULL abbrev means the end of a series of children. */
14050 if (abbrev == NULL)
14052 if (--nesting_level == 0)
14054 /* PART_DIE was probably the last thing allocated on the
14055 comp_unit_obstack, so we could call obstack_free
14056 here. We don't do that because the waste is small,
14057 and will be cleaned up when we're done with this
14058 compilation unit. This way, we're also more robust
14059 against other users of the comp_unit_obstack. */
14062 info_ptr += bytes_read;
14063 last_die = parent_die;
14064 parent_die = parent_die->die_parent;
14068 /* Check for template arguments. We never save these; if
14069 they're seen, we just mark the parent, and go on our way. */
14070 if (parent_die != NULL
14071 && cu->language == language_cplus
14072 && (abbrev->tag == DW_TAG_template_type_param
14073 || abbrev->tag == DW_TAG_template_value_param))
14075 parent_die->has_template_arguments = 1;
14079 /* We don't need a partial DIE for the template argument. */
14080 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14085 /* We only recurse into c++ subprograms looking for template arguments.
14086 Skip their other children. */
14088 && cu->language == language_cplus
14089 && parent_die != NULL
14090 && parent_die->tag == DW_TAG_subprogram)
14092 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14096 /* Check whether this DIE is interesting enough to save. Normally
14097 we would not be interested in members here, but there may be
14098 later variables referencing them via DW_AT_specification (for
14099 static members). */
14101 && !is_type_tag_for_partial (abbrev->tag)
14102 && abbrev->tag != DW_TAG_constant
14103 && abbrev->tag != DW_TAG_enumerator
14104 && abbrev->tag != DW_TAG_subprogram
14105 && abbrev->tag != DW_TAG_lexical_block
14106 && abbrev->tag != DW_TAG_variable
14107 && abbrev->tag != DW_TAG_namespace
14108 && abbrev->tag != DW_TAG_module
14109 && abbrev->tag != DW_TAG_member
14110 && abbrev->tag != DW_TAG_imported_unit)
14112 /* Otherwise we skip to the next sibling, if any. */
14113 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14117 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14120 /* This two-pass algorithm for processing partial symbols has a
14121 high cost in cache pressure. Thus, handle some simple cases
14122 here which cover the majority of C partial symbols. DIEs
14123 which neither have specification tags in them, nor could have
14124 specification tags elsewhere pointing at them, can simply be
14125 processed and discarded.
14127 This segment is also optional; scan_partial_symbols and
14128 add_partial_symbol will handle these DIEs if we chain
14129 them in normally. When compilers which do not emit large
14130 quantities of duplicate debug information are more common,
14131 this code can probably be removed. */
14133 /* Any complete simple types at the top level (pretty much all
14134 of them, for a language without namespaces), can be processed
14136 if (parent_die == NULL
14137 && part_die->has_specification == 0
14138 && part_die->is_declaration == 0
14139 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
14140 || part_die->tag == DW_TAG_base_type
14141 || part_die->tag == DW_TAG_subrange_type))
14143 if (building_psymtab && part_die->name != NULL)
14144 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14145 VAR_DOMAIN, LOC_TYPEDEF,
14146 &objfile->static_psymbols,
14147 0, (CORE_ADDR) 0, cu->language, objfile);
14148 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14152 /* The exception for DW_TAG_typedef with has_children above is
14153 a workaround of GCC PR debug/47510. In the case of this complaint
14154 type_name_no_tag_or_error will error on such types later.
14156 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14157 it could not find the child DIEs referenced later, this is checked
14158 above. In correct DWARF DW_TAG_typedef should have no children. */
14160 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14161 complaint (&symfile_complaints,
14162 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14163 "- DIE at 0x%x [in module %s]"),
14164 part_die->offset.sect_off, objfile_name (objfile));
14166 /* If we're at the second level, and we're an enumerator, and
14167 our parent has no specification (meaning possibly lives in a
14168 namespace elsewhere), then we can add the partial symbol now
14169 instead of queueing it. */
14170 if (part_die->tag == DW_TAG_enumerator
14171 && parent_die != NULL
14172 && parent_die->die_parent == NULL
14173 && parent_die->tag == DW_TAG_enumeration_type
14174 && parent_die->has_specification == 0)
14176 if (part_die->name == NULL)
14177 complaint (&symfile_complaints,
14178 _("malformed enumerator DIE ignored"));
14179 else if (building_psymtab)
14180 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14181 VAR_DOMAIN, LOC_CONST,
14182 (cu->language == language_cplus
14183 || cu->language == language_java)
14184 ? &objfile->global_psymbols
14185 : &objfile->static_psymbols,
14186 0, (CORE_ADDR) 0, cu->language, objfile);
14188 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14192 /* We'll save this DIE so link it in. */
14193 part_die->die_parent = parent_die;
14194 part_die->die_sibling = NULL;
14195 part_die->die_child = NULL;
14197 if (last_die && last_die == parent_die)
14198 last_die->die_child = part_die;
14200 last_die->die_sibling = part_die;
14202 last_die = part_die;
14204 if (first_die == NULL)
14205 first_die = part_die;
14207 /* Maybe add the DIE to the hash table. Not all DIEs that we
14208 find interesting need to be in the hash table, because we
14209 also have the parent/sibling/child chains; only those that we
14210 might refer to by offset later during partial symbol reading.
14212 For now this means things that might have be the target of a
14213 DW_AT_specification, DW_AT_abstract_origin, or
14214 DW_AT_extension. DW_AT_extension will refer only to
14215 namespaces; DW_AT_abstract_origin refers to functions (and
14216 many things under the function DIE, but we do not recurse
14217 into function DIEs during partial symbol reading) and
14218 possibly variables as well; DW_AT_specification refers to
14219 declarations. Declarations ought to have the DW_AT_declaration
14220 flag. It happens that GCC forgets to put it in sometimes, but
14221 only for functions, not for types.
14223 Adding more things than necessary to the hash table is harmless
14224 except for the performance cost. Adding too few will result in
14225 wasted time in find_partial_die, when we reread the compilation
14226 unit with load_all_dies set. */
14229 || abbrev->tag == DW_TAG_constant
14230 || abbrev->tag == DW_TAG_subprogram
14231 || abbrev->tag == DW_TAG_variable
14232 || abbrev->tag == DW_TAG_namespace
14233 || part_die->is_declaration)
14237 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14238 part_die->offset.sect_off, INSERT);
14242 part_die = obstack_alloc (&cu->comp_unit_obstack,
14243 sizeof (struct partial_die_info));
14245 /* For some DIEs we want to follow their children (if any). For C
14246 we have no reason to follow the children of structures; for other
14247 languages we have to, so that we can get at method physnames
14248 to infer fully qualified class names, for DW_AT_specification,
14249 and for C++ template arguments. For C++, we also look one level
14250 inside functions to find template arguments (if the name of the
14251 function does not already contain the template arguments).
14253 For Ada, we need to scan the children of subprograms and lexical
14254 blocks as well because Ada allows the definition of nested
14255 entities that could be interesting for the debugger, such as
14256 nested subprograms for instance. */
14257 if (last_die->has_children
14259 || last_die->tag == DW_TAG_namespace
14260 || last_die->tag == DW_TAG_module
14261 || last_die->tag == DW_TAG_enumeration_type
14262 || (cu->language == language_cplus
14263 && last_die->tag == DW_TAG_subprogram
14264 && (last_die->name == NULL
14265 || strchr (last_die->name, '<') == NULL))
14266 || (cu->language != language_c
14267 && (last_die->tag == DW_TAG_class_type
14268 || last_die->tag == DW_TAG_interface_type
14269 || last_die->tag == DW_TAG_structure_type
14270 || last_die->tag == DW_TAG_union_type))
14271 || (cu->language == language_ada
14272 && (last_die->tag == DW_TAG_subprogram
14273 || last_die->tag == DW_TAG_lexical_block))))
14276 parent_die = last_die;
14280 /* Otherwise we skip to the next sibling, if any. */
14281 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
14283 /* Back to the top, do it again. */
14287 /* Read a minimal amount of information into the minimal die structure. */
14289 static const gdb_byte *
14290 read_partial_die (const struct die_reader_specs *reader,
14291 struct partial_die_info *part_die,
14292 struct abbrev_info *abbrev, unsigned int abbrev_len,
14293 const gdb_byte *info_ptr)
14295 struct dwarf2_cu *cu = reader->cu;
14296 struct objfile *objfile = cu->objfile;
14297 const gdb_byte *buffer = reader->buffer;
14299 struct attribute attr;
14300 int has_low_pc_attr = 0;
14301 int has_high_pc_attr = 0;
14302 int high_pc_relative = 0;
14304 memset (part_die, 0, sizeof (struct partial_die_info));
14306 part_die->offset.sect_off = info_ptr - buffer;
14308 info_ptr += abbrev_len;
14310 if (abbrev == NULL)
14313 part_die->tag = abbrev->tag;
14314 part_die->has_children = abbrev->has_children;
14316 for (i = 0; i < abbrev->num_attrs; ++i)
14318 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
14320 /* Store the data if it is of an attribute we want to keep in a
14321 partial symbol table. */
14325 switch (part_die->tag)
14327 case DW_TAG_compile_unit:
14328 case DW_TAG_partial_unit:
14329 case DW_TAG_type_unit:
14330 /* Compilation units have a DW_AT_name that is a filename, not
14331 a source language identifier. */
14332 case DW_TAG_enumeration_type:
14333 case DW_TAG_enumerator:
14334 /* These tags always have simple identifiers already; no need
14335 to canonicalize them. */
14336 part_die->name = DW_STRING (&attr);
14340 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
14341 &objfile->objfile_obstack);
14345 case DW_AT_linkage_name:
14346 case DW_AT_MIPS_linkage_name:
14347 /* Note that both forms of linkage name might appear. We
14348 assume they will be the same, and we only store the last
14350 if (cu->language == language_ada)
14351 part_die->name = DW_STRING (&attr);
14352 part_die->linkage_name = DW_STRING (&attr);
14355 has_low_pc_attr = 1;
14356 part_die->lowpc = DW_ADDR (&attr);
14358 case DW_AT_high_pc:
14359 has_high_pc_attr = 1;
14360 if (attr.form == DW_FORM_addr
14361 || attr.form == DW_FORM_GNU_addr_index)
14362 part_die->highpc = DW_ADDR (&attr);
14365 high_pc_relative = 1;
14366 part_die->highpc = DW_UNSND (&attr);
14369 case DW_AT_location:
14370 /* Support the .debug_loc offsets. */
14371 if (attr_form_is_block (&attr))
14373 part_die->d.locdesc = DW_BLOCK (&attr);
14375 else if (attr_form_is_section_offset (&attr))
14377 dwarf2_complex_location_expr_complaint ();
14381 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14382 "partial symbol information");
14385 case DW_AT_external:
14386 part_die->is_external = DW_UNSND (&attr);
14388 case DW_AT_declaration:
14389 part_die->is_declaration = DW_UNSND (&attr);
14392 part_die->has_type = 1;
14394 case DW_AT_abstract_origin:
14395 case DW_AT_specification:
14396 case DW_AT_extension:
14397 part_die->has_specification = 1;
14398 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
14399 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14400 || cu->per_cu->is_dwz);
14402 case DW_AT_sibling:
14403 /* Ignore absolute siblings, they might point outside of
14404 the current compile unit. */
14405 if (attr.form == DW_FORM_ref_addr)
14406 complaint (&symfile_complaints,
14407 _("ignoring absolute DW_AT_sibling"));
14409 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
14411 case DW_AT_byte_size:
14412 part_die->has_byte_size = 1;
14414 case DW_AT_calling_convention:
14415 /* DWARF doesn't provide a way to identify a program's source-level
14416 entry point. DW_AT_calling_convention attributes are only meant
14417 to describe functions' calling conventions.
14419 However, because it's a necessary piece of information in
14420 Fortran, and because DW_CC_program is the only piece of debugging
14421 information whose definition refers to a 'main program' at all,
14422 several compilers have begun marking Fortran main programs with
14423 DW_CC_program --- even when those functions use the standard
14424 calling conventions.
14426 So until DWARF specifies a way to provide this information and
14427 compilers pick up the new representation, we'll support this
14429 if (DW_UNSND (&attr) == DW_CC_program
14430 && cu->language == language_fortran)
14432 set_main_name (part_die->name);
14434 /* As this DIE has a static linkage the name would be difficult
14435 to look up later. */
14436 language_of_main = language_fortran;
14440 if (DW_UNSND (&attr) == DW_INL_inlined
14441 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14442 part_die->may_be_inlined = 1;
14446 if (part_die->tag == DW_TAG_imported_unit)
14448 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14449 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14450 || cu->per_cu->is_dwz);
14459 if (high_pc_relative)
14460 part_die->highpc += part_die->lowpc;
14462 if (has_low_pc_attr && has_high_pc_attr)
14464 /* When using the GNU linker, .gnu.linkonce. sections are used to
14465 eliminate duplicate copies of functions and vtables and such.
14466 The linker will arbitrarily choose one and discard the others.
14467 The AT_*_pc values for such functions refer to local labels in
14468 these sections. If the section from that file was discarded, the
14469 labels are not in the output, so the relocs get a value of 0.
14470 If this is a discarded function, mark the pc bounds as invalid,
14471 so that GDB will ignore it. */
14472 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14476 complaint (&symfile_complaints,
14477 _("DW_AT_low_pc %s is zero "
14478 "for DIE at 0x%x [in module %s]"),
14479 paddress (gdbarch, part_die->lowpc),
14480 part_die->offset.sect_off, objfile_name (objfile));
14482 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14483 else if (part_die->lowpc >= part_die->highpc)
14485 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14487 complaint (&symfile_complaints,
14488 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14489 "for DIE at 0x%x [in module %s]"),
14490 paddress (gdbarch, part_die->lowpc),
14491 paddress (gdbarch, part_die->highpc),
14492 part_die->offset.sect_off, objfile_name (objfile));
14495 part_die->has_pc_info = 1;
14501 /* Find a cached partial DIE at OFFSET in CU. */
14503 static struct partial_die_info *
14504 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
14506 struct partial_die_info *lookup_die = NULL;
14507 struct partial_die_info part_die;
14509 part_die.offset = offset;
14510 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14516 /* Find a partial DIE at OFFSET, which may or may not be in CU,
14517 except in the case of .debug_types DIEs which do not reference
14518 outside their CU (they do however referencing other types via
14519 DW_FORM_ref_sig8). */
14521 static struct partial_die_info *
14522 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
14524 struct objfile *objfile = cu->objfile;
14525 struct dwarf2_per_cu_data *per_cu = NULL;
14526 struct partial_die_info *pd = NULL;
14528 if (offset_in_dwz == cu->per_cu->is_dwz
14529 && offset_in_cu_p (&cu->header, offset))
14531 pd = find_partial_die_in_comp_unit (offset, cu);
14534 /* We missed recording what we needed.
14535 Load all dies and try again. */
14536 per_cu = cu->per_cu;
14540 /* TUs don't reference other CUs/TUs (except via type signatures). */
14541 if (cu->per_cu->is_debug_types)
14543 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14544 " external reference to offset 0x%lx [in module %s].\n"),
14545 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14546 bfd_get_filename (objfile->obfd));
14548 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14551 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14552 load_partial_comp_unit (per_cu);
14554 per_cu->cu->last_used = 0;
14555 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14558 /* If we didn't find it, and not all dies have been loaded,
14559 load them all and try again. */
14561 if (pd == NULL && per_cu->load_all_dies == 0)
14563 per_cu->load_all_dies = 1;
14565 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14566 THIS_CU->cu may already be in use. So we can't just free it and
14567 replace its DIEs with the ones we read in. Instead, we leave those
14568 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14569 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14571 load_partial_comp_unit (per_cu);
14573 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14577 internal_error (__FILE__, __LINE__,
14578 _("could not find partial DIE 0x%x "
14579 "in cache [from module %s]\n"),
14580 offset.sect_off, bfd_get_filename (objfile->obfd));
14584 /* See if we can figure out if the class lives in a namespace. We do
14585 this by looking for a member function; its demangled name will
14586 contain namespace info, if there is any. */
14589 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14590 struct dwarf2_cu *cu)
14592 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14593 what template types look like, because the demangler
14594 frequently doesn't give the same name as the debug info. We
14595 could fix this by only using the demangled name to get the
14596 prefix (but see comment in read_structure_type). */
14598 struct partial_die_info *real_pdi;
14599 struct partial_die_info *child_pdi;
14601 /* If this DIE (this DIE's specification, if any) has a parent, then
14602 we should not do this. We'll prepend the parent's fully qualified
14603 name when we create the partial symbol. */
14605 real_pdi = struct_pdi;
14606 while (real_pdi->has_specification)
14607 real_pdi = find_partial_die (real_pdi->spec_offset,
14608 real_pdi->spec_is_dwz, cu);
14610 if (real_pdi->die_parent != NULL)
14613 for (child_pdi = struct_pdi->die_child;
14615 child_pdi = child_pdi->die_sibling)
14617 if (child_pdi->tag == DW_TAG_subprogram
14618 && child_pdi->linkage_name != NULL)
14620 char *actual_class_name
14621 = language_class_name_from_physname (cu->language_defn,
14622 child_pdi->linkage_name);
14623 if (actual_class_name != NULL)
14626 = obstack_copy0 (&cu->objfile->objfile_obstack,
14628 strlen (actual_class_name));
14629 xfree (actual_class_name);
14636 /* Adjust PART_DIE before generating a symbol for it. This function
14637 may set the is_external flag or change the DIE's name. */
14640 fixup_partial_die (struct partial_die_info *part_die,
14641 struct dwarf2_cu *cu)
14643 /* Once we've fixed up a die, there's no point in doing so again.
14644 This also avoids a memory leak if we were to call
14645 guess_partial_die_structure_name multiple times. */
14646 if (part_die->fixup_called)
14649 /* If we found a reference attribute and the DIE has no name, try
14650 to find a name in the referred to DIE. */
14652 if (part_die->name == NULL && part_die->has_specification)
14654 struct partial_die_info *spec_die;
14656 spec_die = find_partial_die (part_die->spec_offset,
14657 part_die->spec_is_dwz, cu);
14659 fixup_partial_die (spec_die, cu);
14661 if (spec_die->name)
14663 part_die->name = spec_die->name;
14665 /* Copy DW_AT_external attribute if it is set. */
14666 if (spec_die->is_external)
14667 part_die->is_external = spec_die->is_external;
14671 /* Set default names for some unnamed DIEs. */
14673 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14674 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14676 /* If there is no parent die to provide a namespace, and there are
14677 children, see if we can determine the namespace from their linkage
14679 if (cu->language == language_cplus
14680 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14681 && part_die->die_parent == NULL
14682 && part_die->has_children
14683 && (part_die->tag == DW_TAG_class_type
14684 || part_die->tag == DW_TAG_structure_type
14685 || part_die->tag == DW_TAG_union_type))
14686 guess_partial_die_structure_name (part_die, cu);
14688 /* GCC might emit a nameless struct or union that has a linkage
14689 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14690 if (part_die->name == NULL
14691 && (part_die->tag == DW_TAG_class_type
14692 || part_die->tag == DW_TAG_interface_type
14693 || part_die->tag == DW_TAG_structure_type
14694 || part_die->tag == DW_TAG_union_type)
14695 && part_die->linkage_name != NULL)
14699 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
14704 /* Strip any leading namespaces/classes, keep only the base name.
14705 DW_AT_name for named DIEs does not contain the prefixes. */
14706 base = strrchr (demangled, ':');
14707 if (base && base > demangled && base[-1] == ':')
14712 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14713 base, strlen (base));
14718 part_die->fixup_called = 1;
14721 /* Read an attribute value described by an attribute form. */
14723 static const gdb_byte *
14724 read_attribute_value (const struct die_reader_specs *reader,
14725 struct attribute *attr, unsigned form,
14726 const gdb_byte *info_ptr)
14728 struct dwarf2_cu *cu = reader->cu;
14729 bfd *abfd = reader->abfd;
14730 struct comp_unit_head *cu_header = &cu->header;
14731 unsigned int bytes_read;
14732 struct dwarf_block *blk;
14737 case DW_FORM_ref_addr:
14738 if (cu->header.version == 2)
14739 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14741 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14742 &cu->header, &bytes_read);
14743 info_ptr += bytes_read;
14745 case DW_FORM_GNU_ref_alt:
14746 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14747 info_ptr += bytes_read;
14750 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14751 info_ptr += bytes_read;
14753 case DW_FORM_block2:
14754 blk = dwarf_alloc_block (cu);
14755 blk->size = read_2_bytes (abfd, info_ptr);
14757 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14758 info_ptr += blk->size;
14759 DW_BLOCK (attr) = blk;
14761 case DW_FORM_block4:
14762 blk = dwarf_alloc_block (cu);
14763 blk->size = read_4_bytes (abfd, info_ptr);
14765 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14766 info_ptr += blk->size;
14767 DW_BLOCK (attr) = blk;
14769 case DW_FORM_data2:
14770 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14773 case DW_FORM_data4:
14774 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14777 case DW_FORM_data8:
14778 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14781 case DW_FORM_sec_offset:
14782 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14783 info_ptr += bytes_read;
14785 case DW_FORM_string:
14786 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14787 DW_STRING_IS_CANONICAL (attr) = 0;
14788 info_ptr += bytes_read;
14791 if (!cu->per_cu->is_dwz)
14793 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14795 DW_STRING_IS_CANONICAL (attr) = 0;
14796 info_ptr += bytes_read;
14800 case DW_FORM_GNU_strp_alt:
14802 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14803 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14806 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14807 DW_STRING_IS_CANONICAL (attr) = 0;
14808 info_ptr += bytes_read;
14811 case DW_FORM_exprloc:
14812 case DW_FORM_block:
14813 blk = dwarf_alloc_block (cu);
14814 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14815 info_ptr += bytes_read;
14816 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14817 info_ptr += blk->size;
14818 DW_BLOCK (attr) = blk;
14820 case DW_FORM_block1:
14821 blk = dwarf_alloc_block (cu);
14822 blk->size = read_1_byte (abfd, info_ptr);
14824 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14825 info_ptr += blk->size;
14826 DW_BLOCK (attr) = blk;
14828 case DW_FORM_data1:
14829 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14833 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14836 case DW_FORM_flag_present:
14837 DW_UNSND (attr) = 1;
14839 case DW_FORM_sdata:
14840 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14841 info_ptr += bytes_read;
14843 case DW_FORM_udata:
14844 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14845 info_ptr += bytes_read;
14848 DW_UNSND (attr) = (cu->header.offset.sect_off
14849 + read_1_byte (abfd, info_ptr));
14853 DW_UNSND (attr) = (cu->header.offset.sect_off
14854 + read_2_bytes (abfd, info_ptr));
14858 DW_UNSND (attr) = (cu->header.offset.sect_off
14859 + read_4_bytes (abfd, info_ptr));
14863 DW_UNSND (attr) = (cu->header.offset.sect_off
14864 + read_8_bytes (abfd, info_ptr));
14867 case DW_FORM_ref_sig8:
14868 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
14871 case DW_FORM_ref_udata:
14872 DW_UNSND (attr) = (cu->header.offset.sect_off
14873 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14874 info_ptr += bytes_read;
14876 case DW_FORM_indirect:
14877 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14878 info_ptr += bytes_read;
14879 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14881 case DW_FORM_GNU_addr_index:
14882 if (reader->dwo_file == NULL)
14884 /* For now flag a hard error.
14885 Later we can turn this into a complaint. */
14886 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14887 dwarf_form_name (form),
14888 bfd_get_filename (abfd));
14890 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14891 info_ptr += bytes_read;
14893 case DW_FORM_GNU_str_index:
14894 if (reader->dwo_file == NULL)
14896 /* For now flag a hard error.
14897 Later we can turn this into a complaint if warranted. */
14898 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14899 dwarf_form_name (form),
14900 bfd_get_filename (abfd));
14903 ULONGEST str_index =
14904 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14906 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14907 DW_STRING_IS_CANONICAL (attr) = 0;
14908 info_ptr += bytes_read;
14912 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14913 dwarf_form_name (form),
14914 bfd_get_filename (abfd));
14918 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
14919 attr->form = DW_FORM_GNU_ref_alt;
14921 /* We have seen instances where the compiler tried to emit a byte
14922 size attribute of -1 which ended up being encoded as an unsigned
14923 0xffffffff. Although 0xffffffff is technically a valid size value,
14924 an object of this size seems pretty unlikely so we can relatively
14925 safely treat these cases as if the size attribute was invalid and
14926 treat them as zero by default. */
14927 if (attr->name == DW_AT_byte_size
14928 && form == DW_FORM_data4
14929 && DW_UNSND (attr) >= 0xffffffff)
14932 (&symfile_complaints,
14933 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14934 hex_string (DW_UNSND (attr)));
14935 DW_UNSND (attr) = 0;
14941 /* Read an attribute described by an abbreviated attribute. */
14943 static const gdb_byte *
14944 read_attribute (const struct die_reader_specs *reader,
14945 struct attribute *attr, struct attr_abbrev *abbrev,
14946 const gdb_byte *info_ptr)
14948 attr->name = abbrev->name;
14949 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14952 /* Read dwarf information from a buffer. */
14954 static unsigned int
14955 read_1_byte (bfd *abfd, const gdb_byte *buf)
14957 return bfd_get_8 (abfd, buf);
14961 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14963 return bfd_get_signed_8 (abfd, buf);
14966 static unsigned int
14967 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14969 return bfd_get_16 (abfd, buf);
14973 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14975 return bfd_get_signed_16 (abfd, buf);
14978 static unsigned int
14979 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14981 return bfd_get_32 (abfd, buf);
14985 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14987 return bfd_get_signed_32 (abfd, buf);
14991 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14993 return bfd_get_64 (abfd, buf);
14997 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
14998 unsigned int *bytes_read)
15000 struct comp_unit_head *cu_header = &cu->header;
15001 CORE_ADDR retval = 0;
15003 if (cu_header->signed_addr_p)
15005 switch (cu_header->addr_size)
15008 retval = bfd_get_signed_16 (abfd, buf);
15011 retval = bfd_get_signed_32 (abfd, buf);
15014 retval = bfd_get_signed_64 (abfd, buf);
15017 internal_error (__FILE__, __LINE__,
15018 _("read_address: bad switch, signed [in module %s]"),
15019 bfd_get_filename (abfd));
15024 switch (cu_header->addr_size)
15027 retval = bfd_get_16 (abfd, buf);
15030 retval = bfd_get_32 (abfd, buf);
15033 retval = bfd_get_64 (abfd, buf);
15036 internal_error (__FILE__, __LINE__,
15037 _("read_address: bad switch, "
15038 "unsigned [in module %s]"),
15039 bfd_get_filename (abfd));
15043 *bytes_read = cu_header->addr_size;
15047 /* Read the initial length from a section. The (draft) DWARF 3
15048 specification allows the initial length to take up either 4 bytes
15049 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15050 bytes describe the length and all offsets will be 8 bytes in length
15053 An older, non-standard 64-bit format is also handled by this
15054 function. The older format in question stores the initial length
15055 as an 8-byte quantity without an escape value. Lengths greater
15056 than 2^32 aren't very common which means that the initial 4 bytes
15057 is almost always zero. Since a length value of zero doesn't make
15058 sense for the 32-bit format, this initial zero can be considered to
15059 be an escape value which indicates the presence of the older 64-bit
15060 format. As written, the code can't detect (old format) lengths
15061 greater than 4GB. If it becomes necessary to handle lengths
15062 somewhat larger than 4GB, we could allow other small values (such
15063 as the non-sensical values of 1, 2, and 3) to also be used as
15064 escape values indicating the presence of the old format.
15066 The value returned via bytes_read should be used to increment the
15067 relevant pointer after calling read_initial_length().
15069 [ Note: read_initial_length() and read_offset() are based on the
15070 document entitled "DWARF Debugging Information Format", revision
15071 3, draft 8, dated November 19, 2001. This document was obtained
15074 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
15076 This document is only a draft and is subject to change. (So beware.)
15078 Details regarding the older, non-standard 64-bit format were
15079 determined empirically by examining 64-bit ELF files produced by
15080 the SGI toolchain on an IRIX 6.5 machine.
15082 - Kevin, July 16, 2002
15086 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
15088 LONGEST length = bfd_get_32 (abfd, buf);
15090 if (length == 0xffffffff)
15092 length = bfd_get_64 (abfd, buf + 4);
15095 else if (length == 0)
15097 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
15098 length = bfd_get_64 (abfd, buf);
15109 /* Cover function for read_initial_length.
15110 Returns the length of the object at BUF, and stores the size of the
15111 initial length in *BYTES_READ and stores the size that offsets will be in
15113 If the initial length size is not equivalent to that specified in
15114 CU_HEADER then issue a complaint.
15115 This is useful when reading non-comp-unit headers. */
15118 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
15119 const struct comp_unit_head *cu_header,
15120 unsigned int *bytes_read,
15121 unsigned int *offset_size)
15123 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15125 gdb_assert (cu_header->initial_length_size == 4
15126 || cu_header->initial_length_size == 8
15127 || cu_header->initial_length_size == 12);
15129 if (cu_header->initial_length_size != *bytes_read)
15130 complaint (&symfile_complaints,
15131 _("intermixed 32-bit and 64-bit DWARF sections"));
15133 *offset_size = (*bytes_read == 4) ? 4 : 8;
15137 /* Read an offset from the data stream. The size of the offset is
15138 given by cu_header->offset_size. */
15141 read_offset (bfd *abfd, const gdb_byte *buf,
15142 const struct comp_unit_head *cu_header,
15143 unsigned int *bytes_read)
15145 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
15147 *bytes_read = cu_header->offset_size;
15151 /* Read an offset from the data stream. */
15154 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15156 LONGEST retval = 0;
15158 switch (offset_size)
15161 retval = bfd_get_32 (abfd, buf);
15164 retval = bfd_get_64 (abfd, buf);
15167 internal_error (__FILE__, __LINE__,
15168 _("read_offset_1: bad switch [in module %s]"),
15169 bfd_get_filename (abfd));
15175 static const gdb_byte *
15176 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15178 /* If the size of a host char is 8 bits, we can return a pointer
15179 to the buffer, otherwise we have to copy the data to a buffer
15180 allocated on the temporary obstack. */
15181 gdb_assert (HOST_CHAR_BIT == 8);
15185 static const char *
15186 read_direct_string (bfd *abfd, const gdb_byte *buf,
15187 unsigned int *bytes_read_ptr)
15189 /* If the size of a host char is 8 bits, we can return a pointer
15190 to the string, otherwise we have to copy the string to a buffer
15191 allocated on the temporary obstack. */
15192 gdb_assert (HOST_CHAR_BIT == 8);
15195 *bytes_read_ptr = 1;
15198 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15199 return (const char *) buf;
15202 static const char *
15203 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15205 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15206 if (dwarf2_per_objfile->str.buffer == NULL)
15207 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15208 bfd_get_filename (abfd));
15209 if (str_offset >= dwarf2_per_objfile->str.size)
15210 error (_("DW_FORM_strp pointing outside of "
15211 ".debug_str section [in module %s]"),
15212 bfd_get_filename (abfd));
15213 gdb_assert (HOST_CHAR_BIT == 8);
15214 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15216 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15219 /* Read a string at offset STR_OFFSET in the .debug_str section from
15220 the .dwz file DWZ. Throw an error if the offset is too large. If
15221 the string consists of a single NUL byte, return NULL; otherwise
15222 return a pointer to the string. */
15224 static const char *
15225 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15227 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15229 if (dwz->str.buffer == NULL)
15230 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15231 "section [in module %s]"),
15232 bfd_get_filename (dwz->dwz_bfd));
15233 if (str_offset >= dwz->str.size)
15234 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15235 ".debug_str section [in module %s]"),
15236 bfd_get_filename (dwz->dwz_bfd));
15237 gdb_assert (HOST_CHAR_BIT == 8);
15238 if (dwz->str.buffer[str_offset] == '\0')
15240 return (const char *) (dwz->str.buffer + str_offset);
15243 static const char *
15244 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15245 const struct comp_unit_head *cu_header,
15246 unsigned int *bytes_read_ptr)
15248 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15250 return read_indirect_string_at_offset (abfd, str_offset);
15254 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15255 unsigned int *bytes_read_ptr)
15258 unsigned int num_read;
15260 unsigned char byte;
15268 byte = bfd_get_8 (abfd, buf);
15271 result |= ((ULONGEST) (byte & 127) << shift);
15272 if ((byte & 128) == 0)
15278 *bytes_read_ptr = num_read;
15283 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15284 unsigned int *bytes_read_ptr)
15287 int i, shift, num_read;
15288 unsigned char byte;
15296 byte = bfd_get_8 (abfd, buf);
15299 result |= ((LONGEST) (byte & 127) << shift);
15301 if ((byte & 128) == 0)
15306 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
15307 result |= -(((LONGEST) 1) << shift);
15308 *bytes_read_ptr = num_read;
15312 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
15313 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15314 ADDR_SIZE is the size of addresses from the CU header. */
15317 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15319 struct objfile *objfile = dwarf2_per_objfile->objfile;
15320 bfd *abfd = objfile->obfd;
15321 const gdb_byte *info_ptr;
15323 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15324 if (dwarf2_per_objfile->addr.buffer == NULL)
15325 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15326 objfile_name (objfile));
15327 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15328 error (_("DW_FORM_addr_index pointing outside of "
15329 ".debug_addr section [in module %s]"),
15330 objfile_name (objfile));
15331 info_ptr = (dwarf2_per_objfile->addr.buffer
15332 + addr_base + addr_index * addr_size);
15333 if (addr_size == 4)
15334 return bfd_get_32 (abfd, info_ptr);
15336 return bfd_get_64 (abfd, info_ptr);
15339 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15342 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15344 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15347 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15350 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
15351 unsigned int *bytes_read)
15353 bfd *abfd = cu->objfile->obfd;
15354 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15356 return read_addr_index (cu, addr_index);
15359 /* Data structure to pass results from dwarf2_read_addr_index_reader
15360 back to dwarf2_read_addr_index. */
15362 struct dwarf2_read_addr_index_data
15364 ULONGEST addr_base;
15368 /* die_reader_func for dwarf2_read_addr_index. */
15371 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
15372 const gdb_byte *info_ptr,
15373 struct die_info *comp_unit_die,
15377 struct dwarf2_cu *cu = reader->cu;
15378 struct dwarf2_read_addr_index_data *aidata =
15379 (struct dwarf2_read_addr_index_data *) data;
15381 aidata->addr_base = cu->addr_base;
15382 aidata->addr_size = cu->header.addr_size;
15385 /* Given an index in .debug_addr, fetch the value.
15386 NOTE: This can be called during dwarf expression evaluation,
15387 long after the debug information has been read, and thus per_cu->cu
15388 may no longer exist. */
15391 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15392 unsigned int addr_index)
15394 struct objfile *objfile = per_cu->objfile;
15395 struct dwarf2_cu *cu = per_cu->cu;
15396 ULONGEST addr_base;
15399 /* This is intended to be called from outside this file. */
15400 dw2_setup (objfile);
15402 /* We need addr_base and addr_size.
15403 If we don't have PER_CU->cu, we have to get it.
15404 Nasty, but the alternative is storing the needed info in PER_CU,
15405 which at this point doesn't seem justified: it's not clear how frequently
15406 it would get used and it would increase the size of every PER_CU.
15407 Entry points like dwarf2_per_cu_addr_size do a similar thing
15408 so we're not in uncharted territory here.
15409 Alas we need to be a bit more complicated as addr_base is contained
15412 We don't need to read the entire CU(/TU).
15413 We just need the header and top level die.
15415 IWBN to use the aging mechanism to let us lazily later discard the CU.
15416 For now we skip this optimization. */
15420 addr_base = cu->addr_base;
15421 addr_size = cu->header.addr_size;
15425 struct dwarf2_read_addr_index_data aidata;
15427 /* Note: We can't use init_cutu_and_read_dies_simple here,
15428 we need addr_base. */
15429 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15430 dwarf2_read_addr_index_reader, &aidata);
15431 addr_base = aidata.addr_base;
15432 addr_size = aidata.addr_size;
15435 return read_addr_index_1 (addr_index, addr_base, addr_size);
15438 /* Given a DW_AT_str_index, fetch the string. */
15440 static const char *
15441 read_str_index (const struct die_reader_specs *reader,
15442 struct dwarf2_cu *cu, ULONGEST str_index)
15444 struct objfile *objfile = dwarf2_per_objfile->objfile;
15445 const char *dwo_name = objfile_name (objfile);
15446 bfd *abfd = objfile->obfd;
15447 struct dwo_sections *sections = &reader->dwo_file->sections;
15448 const gdb_byte *info_ptr;
15449 ULONGEST str_offset;
15451 dwarf2_read_section (objfile, §ions->str);
15452 dwarf2_read_section (objfile, §ions->str_offsets);
15453 if (sections->str.buffer == NULL)
15454 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15455 " in CU at offset 0x%lx [in module %s]"),
15456 (long) cu->header.offset.sect_off, dwo_name);
15457 if (sections->str_offsets.buffer == NULL)
15458 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15459 " in CU at offset 0x%lx [in module %s]"),
15460 (long) cu->header.offset.sect_off, dwo_name);
15461 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15462 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15463 " section in CU at offset 0x%lx [in module %s]"),
15464 (long) cu->header.offset.sect_off, dwo_name);
15465 info_ptr = (sections->str_offsets.buffer
15466 + str_index * cu->header.offset_size);
15467 if (cu->header.offset_size == 4)
15468 str_offset = bfd_get_32 (abfd, info_ptr);
15470 str_offset = bfd_get_64 (abfd, info_ptr);
15471 if (str_offset >= sections->str.size)
15472 error (_("Offset from DW_FORM_str_index pointing outside of"
15473 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15474 (long) cu->header.offset.sect_off, dwo_name);
15475 return (const char *) (sections->str.buffer + str_offset);
15478 /* Return the length of an LEB128 number in BUF. */
15481 leb128_size (const gdb_byte *buf)
15483 const gdb_byte *begin = buf;
15489 if ((byte & 128) == 0)
15490 return buf - begin;
15495 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
15503 cu->language = language_c;
15505 case DW_LANG_C_plus_plus:
15506 cu->language = language_cplus;
15509 cu->language = language_d;
15511 case DW_LANG_Fortran77:
15512 case DW_LANG_Fortran90:
15513 case DW_LANG_Fortran95:
15514 cu->language = language_fortran;
15517 cu->language = language_go;
15519 case DW_LANG_Mips_Assembler:
15520 cu->language = language_asm;
15523 cu->language = language_java;
15525 case DW_LANG_Ada83:
15526 case DW_LANG_Ada95:
15527 cu->language = language_ada;
15529 case DW_LANG_Modula2:
15530 cu->language = language_m2;
15532 case DW_LANG_Pascal83:
15533 cu->language = language_pascal;
15536 cu->language = language_objc;
15538 case DW_LANG_Cobol74:
15539 case DW_LANG_Cobol85:
15541 cu->language = language_minimal;
15544 cu->language_defn = language_def (cu->language);
15547 /* Return the named attribute or NULL if not there. */
15549 static struct attribute *
15550 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15555 struct attribute *spec = NULL;
15557 for (i = 0; i < die->num_attrs; ++i)
15559 if (die->attrs[i].name == name)
15560 return &die->attrs[i];
15561 if (die->attrs[i].name == DW_AT_specification
15562 || die->attrs[i].name == DW_AT_abstract_origin)
15563 spec = &die->attrs[i];
15569 die = follow_die_ref (die, spec, &cu);
15575 /* Return the named attribute or NULL if not there,
15576 but do not follow DW_AT_specification, etc.
15577 This is for use in contexts where we're reading .debug_types dies.
15578 Following DW_AT_specification, DW_AT_abstract_origin will take us
15579 back up the chain, and we want to go down. */
15581 static struct attribute *
15582 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15586 for (i = 0; i < die->num_attrs; ++i)
15587 if (die->attrs[i].name == name)
15588 return &die->attrs[i];
15593 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15594 and holds a non-zero value. This function should only be used for
15595 DW_FORM_flag or DW_FORM_flag_present attributes. */
15598 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15600 struct attribute *attr = dwarf2_attr (die, name, cu);
15602 return (attr && DW_UNSND (attr));
15606 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15608 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15609 which value is non-zero. However, we have to be careful with
15610 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15611 (via dwarf2_flag_true_p) follows this attribute. So we may
15612 end up accidently finding a declaration attribute that belongs
15613 to a different DIE referenced by the specification attribute,
15614 even though the given DIE does not have a declaration attribute. */
15615 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15616 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15619 /* Return the die giving the specification for DIE, if there is
15620 one. *SPEC_CU is the CU containing DIE on input, and the CU
15621 containing the return value on output. If there is no
15622 specification, but there is an abstract origin, that is
15625 static struct die_info *
15626 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15628 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15631 if (spec_attr == NULL)
15632 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15634 if (spec_attr == NULL)
15637 return follow_die_ref (die, spec_attr, spec_cu);
15640 /* Free the line_header structure *LH, and any arrays and strings it
15642 NOTE: This is also used as a "cleanup" function. */
15645 free_line_header (struct line_header *lh)
15647 if (lh->standard_opcode_lengths)
15648 xfree (lh->standard_opcode_lengths);
15650 /* Remember that all the lh->file_names[i].name pointers are
15651 pointers into debug_line_buffer, and don't need to be freed. */
15652 if (lh->file_names)
15653 xfree (lh->file_names);
15655 /* Similarly for the include directory names. */
15656 if (lh->include_dirs)
15657 xfree (lh->include_dirs);
15662 /* Add an entry to LH's include directory table. */
15665 add_include_dir (struct line_header *lh, const char *include_dir)
15667 /* Grow the array if necessary. */
15668 if (lh->include_dirs_size == 0)
15670 lh->include_dirs_size = 1; /* for testing */
15671 lh->include_dirs = xmalloc (lh->include_dirs_size
15672 * sizeof (*lh->include_dirs));
15674 else if (lh->num_include_dirs >= lh->include_dirs_size)
15676 lh->include_dirs_size *= 2;
15677 lh->include_dirs = xrealloc (lh->include_dirs,
15678 (lh->include_dirs_size
15679 * sizeof (*lh->include_dirs)));
15682 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15685 /* Add an entry to LH's file name table. */
15688 add_file_name (struct line_header *lh,
15690 unsigned int dir_index,
15691 unsigned int mod_time,
15692 unsigned int length)
15694 struct file_entry *fe;
15696 /* Grow the array if necessary. */
15697 if (lh->file_names_size == 0)
15699 lh->file_names_size = 1; /* for testing */
15700 lh->file_names = xmalloc (lh->file_names_size
15701 * sizeof (*lh->file_names));
15703 else if (lh->num_file_names >= lh->file_names_size)
15705 lh->file_names_size *= 2;
15706 lh->file_names = xrealloc (lh->file_names,
15707 (lh->file_names_size
15708 * sizeof (*lh->file_names)));
15711 fe = &lh->file_names[lh->num_file_names++];
15713 fe->dir_index = dir_index;
15714 fe->mod_time = mod_time;
15715 fe->length = length;
15716 fe->included_p = 0;
15720 /* A convenience function to find the proper .debug_line section for a
15723 static struct dwarf2_section_info *
15724 get_debug_line_section (struct dwarf2_cu *cu)
15726 struct dwarf2_section_info *section;
15728 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15730 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15731 section = &cu->dwo_unit->dwo_file->sections.line;
15732 else if (cu->per_cu->is_dwz)
15734 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15736 section = &dwz->line;
15739 section = &dwarf2_per_objfile->line;
15744 /* Read the statement program header starting at OFFSET in
15745 .debug_line, or .debug_line.dwo. Return a pointer
15746 to a struct line_header, allocated using xmalloc.
15748 NOTE: the strings in the include directory and file name tables of
15749 the returned object point into the dwarf line section buffer,
15750 and must not be freed. */
15752 static struct line_header *
15753 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15755 struct cleanup *back_to;
15756 struct line_header *lh;
15757 const gdb_byte *line_ptr;
15758 unsigned int bytes_read, offset_size;
15760 const char *cur_dir, *cur_file;
15761 struct dwarf2_section_info *section;
15764 section = get_debug_line_section (cu);
15765 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15766 if (section->buffer == NULL)
15768 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15769 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15771 complaint (&symfile_complaints, _("missing .debug_line section"));
15775 /* We can't do this until we know the section is non-empty.
15776 Only then do we know we have such a section. */
15777 abfd = section->asection->owner;
15779 /* Make sure that at least there's room for the total_length field.
15780 That could be 12 bytes long, but we're just going to fudge that. */
15781 if (offset + 4 >= section->size)
15783 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15787 lh = xmalloc (sizeof (*lh));
15788 memset (lh, 0, sizeof (*lh));
15789 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15792 line_ptr = section->buffer + offset;
15794 /* Read in the header. */
15796 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15797 &bytes_read, &offset_size);
15798 line_ptr += bytes_read;
15799 if (line_ptr + lh->total_length > (section->buffer + section->size))
15801 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15802 do_cleanups (back_to);
15805 lh->statement_program_end = line_ptr + lh->total_length;
15806 lh->version = read_2_bytes (abfd, line_ptr);
15808 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15809 line_ptr += offset_size;
15810 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15812 if (lh->version >= 4)
15814 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15818 lh->maximum_ops_per_instruction = 1;
15820 if (lh->maximum_ops_per_instruction == 0)
15822 lh->maximum_ops_per_instruction = 1;
15823 complaint (&symfile_complaints,
15824 _("invalid maximum_ops_per_instruction "
15825 "in `.debug_line' section"));
15828 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15830 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15832 lh->line_range = read_1_byte (abfd, line_ptr);
15834 lh->opcode_base = read_1_byte (abfd, line_ptr);
15836 lh->standard_opcode_lengths
15837 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15839 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15840 for (i = 1; i < lh->opcode_base; ++i)
15842 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15846 /* Read directory table. */
15847 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15849 line_ptr += bytes_read;
15850 add_include_dir (lh, cur_dir);
15852 line_ptr += bytes_read;
15854 /* Read file name table. */
15855 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15857 unsigned int dir_index, mod_time, length;
15859 line_ptr += bytes_read;
15860 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15861 line_ptr += bytes_read;
15862 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15863 line_ptr += bytes_read;
15864 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15865 line_ptr += bytes_read;
15867 add_file_name (lh, cur_file, dir_index, mod_time, length);
15869 line_ptr += bytes_read;
15870 lh->statement_program_start = line_ptr;
15872 if (line_ptr > (section->buffer + section->size))
15873 complaint (&symfile_complaints,
15874 _("line number info header doesn't "
15875 "fit in `.debug_line' section"));
15877 discard_cleanups (back_to);
15881 /* Subroutine of dwarf_decode_lines to simplify it.
15882 Return the file name of the psymtab for included file FILE_INDEX
15883 in line header LH of PST.
15884 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15885 If space for the result is malloc'd, it will be freed by a cleanup.
15886 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15888 The function creates dangling cleanup registration. */
15890 static const char *
15891 psymtab_include_file_name (const struct line_header *lh, int file_index,
15892 const struct partial_symtab *pst,
15893 const char *comp_dir)
15895 const struct file_entry fe = lh->file_names [file_index];
15896 const char *include_name = fe.name;
15897 const char *include_name_to_compare = include_name;
15898 const char *dir_name = NULL;
15899 const char *pst_filename;
15900 char *copied_name = NULL;
15904 dir_name = lh->include_dirs[fe.dir_index - 1];
15906 if (!IS_ABSOLUTE_PATH (include_name)
15907 && (dir_name != NULL || comp_dir != NULL))
15909 /* Avoid creating a duplicate psymtab for PST.
15910 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15911 Before we do the comparison, however, we need to account
15912 for DIR_NAME and COMP_DIR.
15913 First prepend dir_name (if non-NULL). If we still don't
15914 have an absolute path prepend comp_dir (if non-NULL).
15915 However, the directory we record in the include-file's
15916 psymtab does not contain COMP_DIR (to match the
15917 corresponding symtab(s)).
15922 bash$ gcc -g ./hello.c
15923 include_name = "hello.c"
15925 DW_AT_comp_dir = comp_dir = "/tmp"
15926 DW_AT_name = "./hello.c" */
15928 if (dir_name != NULL)
15930 char *tem = concat (dir_name, SLASH_STRING,
15931 include_name, (char *)NULL);
15933 make_cleanup (xfree, tem);
15934 include_name = tem;
15935 include_name_to_compare = include_name;
15937 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15939 char *tem = concat (comp_dir, SLASH_STRING,
15940 include_name, (char *)NULL);
15942 make_cleanup (xfree, tem);
15943 include_name_to_compare = tem;
15947 pst_filename = pst->filename;
15948 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15950 copied_name = concat (pst->dirname, SLASH_STRING,
15951 pst_filename, (char *)NULL);
15952 pst_filename = copied_name;
15955 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15957 if (copied_name != NULL)
15958 xfree (copied_name);
15962 return include_name;
15965 /* Ignore this record_line request. */
15968 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15973 /* Subroutine of dwarf_decode_lines to simplify it.
15974 Process the line number information in LH. */
15977 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15978 struct dwarf2_cu *cu, struct partial_symtab *pst)
15980 const gdb_byte *line_ptr, *extended_end;
15981 const gdb_byte *line_end;
15982 unsigned int bytes_read, extended_len;
15983 unsigned char op_code, extended_op, adj_opcode;
15984 CORE_ADDR baseaddr;
15985 struct objfile *objfile = cu->objfile;
15986 bfd *abfd = objfile->obfd;
15987 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15988 const int decode_for_pst_p = (pst != NULL);
15989 struct subfile *last_subfile = NULL;
15990 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15995 line_ptr = lh->statement_program_start;
15996 line_end = lh->statement_program_end;
15998 /* Read the statement sequences until there's nothing left. */
15999 while (line_ptr < line_end)
16001 /* state machine registers */
16002 CORE_ADDR address = 0;
16003 unsigned int file = 1;
16004 unsigned int line = 1;
16005 unsigned int column = 0;
16006 int is_stmt = lh->default_is_stmt;
16007 int basic_block = 0;
16008 int end_sequence = 0;
16010 unsigned char op_index = 0;
16012 if (!decode_for_pst_p && lh->num_file_names >= file)
16014 /* Start a subfile for the current file of the state machine. */
16015 /* lh->include_dirs and lh->file_names are 0-based, but the
16016 directory and file name numbers in the statement program
16018 struct file_entry *fe = &lh->file_names[file - 1];
16019 const char *dir = NULL;
16022 dir = lh->include_dirs[fe->dir_index - 1];
16024 dwarf2_start_subfile (fe->name, dir, comp_dir);
16027 /* Decode the table. */
16028 while (!end_sequence)
16030 op_code = read_1_byte (abfd, line_ptr);
16032 if (line_ptr > line_end)
16034 dwarf2_debug_line_missing_end_sequence_complaint ();
16038 if (op_code >= lh->opcode_base)
16040 /* Special operand. */
16041 adj_opcode = op_code - lh->opcode_base;
16042 address += (((op_index + (adj_opcode / lh->line_range))
16043 / lh->maximum_ops_per_instruction)
16044 * lh->minimum_instruction_length);
16045 op_index = ((op_index + (adj_opcode / lh->line_range))
16046 % lh->maximum_ops_per_instruction);
16047 line += lh->line_base + (adj_opcode % lh->line_range);
16048 if (lh->num_file_names < file || file == 0)
16049 dwarf2_debug_line_missing_file_complaint ();
16050 /* For now we ignore lines not starting on an
16051 instruction boundary. */
16052 else if (op_index == 0)
16054 lh->file_names[file - 1].included_p = 1;
16055 if (!decode_for_pst_p && is_stmt)
16057 if (last_subfile != current_subfile)
16059 addr = gdbarch_addr_bits_remove (gdbarch, address);
16061 (*p_record_line) (last_subfile, 0, addr);
16062 last_subfile = current_subfile;
16064 /* Append row to matrix using current values. */
16065 addr = gdbarch_addr_bits_remove (gdbarch, address);
16066 (*p_record_line) (current_subfile, line, addr);
16071 else switch (op_code)
16073 case DW_LNS_extended_op:
16074 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16076 line_ptr += bytes_read;
16077 extended_end = line_ptr + extended_len;
16078 extended_op = read_1_byte (abfd, line_ptr);
16080 switch (extended_op)
16082 case DW_LNE_end_sequence:
16083 p_record_line = record_line;
16086 case DW_LNE_set_address:
16087 address = read_address (abfd, line_ptr, cu, &bytes_read);
16089 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16091 /* This line table is for a function which has been
16092 GCd by the linker. Ignore it. PR gdb/12528 */
16095 = line_ptr - get_debug_line_section (cu)->buffer;
16097 complaint (&symfile_complaints,
16098 _(".debug_line address at offset 0x%lx is 0 "
16100 line_offset, objfile_name (objfile));
16101 p_record_line = noop_record_line;
16105 line_ptr += bytes_read;
16106 address += baseaddr;
16108 case DW_LNE_define_file:
16110 const char *cur_file;
16111 unsigned int dir_index, mod_time, length;
16113 cur_file = read_direct_string (abfd, line_ptr,
16115 line_ptr += bytes_read;
16117 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16118 line_ptr += bytes_read;
16120 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16121 line_ptr += bytes_read;
16123 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16124 line_ptr += bytes_read;
16125 add_file_name (lh, cur_file, dir_index, mod_time, length);
16128 case DW_LNE_set_discriminator:
16129 /* The discriminator is not interesting to the debugger;
16131 line_ptr = extended_end;
16134 complaint (&symfile_complaints,
16135 _("mangled .debug_line section"));
16138 /* Make sure that we parsed the extended op correctly. If e.g.
16139 we expected a different address size than the producer used,
16140 we may have read the wrong number of bytes. */
16141 if (line_ptr != extended_end)
16143 complaint (&symfile_complaints,
16144 _("mangled .debug_line section"));
16149 if (lh->num_file_names < file || file == 0)
16150 dwarf2_debug_line_missing_file_complaint ();
16153 lh->file_names[file - 1].included_p = 1;
16154 if (!decode_for_pst_p && is_stmt)
16156 if (last_subfile != current_subfile)
16158 addr = gdbarch_addr_bits_remove (gdbarch, address);
16160 (*p_record_line) (last_subfile, 0, addr);
16161 last_subfile = current_subfile;
16163 addr = gdbarch_addr_bits_remove (gdbarch, address);
16164 (*p_record_line) (current_subfile, line, addr);
16169 case DW_LNS_advance_pc:
16172 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16174 address += (((op_index + adjust)
16175 / lh->maximum_ops_per_instruction)
16176 * lh->minimum_instruction_length);
16177 op_index = ((op_index + adjust)
16178 % lh->maximum_ops_per_instruction);
16179 line_ptr += bytes_read;
16182 case DW_LNS_advance_line:
16183 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16184 line_ptr += bytes_read;
16186 case DW_LNS_set_file:
16188 /* The arrays lh->include_dirs and lh->file_names are
16189 0-based, but the directory and file name numbers in
16190 the statement program are 1-based. */
16191 struct file_entry *fe;
16192 const char *dir = NULL;
16194 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16195 line_ptr += bytes_read;
16196 if (lh->num_file_names < file || file == 0)
16197 dwarf2_debug_line_missing_file_complaint ();
16200 fe = &lh->file_names[file - 1];
16202 dir = lh->include_dirs[fe->dir_index - 1];
16203 if (!decode_for_pst_p)
16205 last_subfile = current_subfile;
16206 dwarf2_start_subfile (fe->name, dir, comp_dir);
16211 case DW_LNS_set_column:
16212 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16213 line_ptr += bytes_read;
16215 case DW_LNS_negate_stmt:
16216 is_stmt = (!is_stmt);
16218 case DW_LNS_set_basic_block:
16221 /* Add to the address register of the state machine the
16222 address increment value corresponding to special opcode
16223 255. I.e., this value is scaled by the minimum
16224 instruction length since special opcode 255 would have
16225 scaled the increment. */
16226 case DW_LNS_const_add_pc:
16228 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16230 address += (((op_index + adjust)
16231 / lh->maximum_ops_per_instruction)
16232 * lh->minimum_instruction_length);
16233 op_index = ((op_index + adjust)
16234 % lh->maximum_ops_per_instruction);
16237 case DW_LNS_fixed_advance_pc:
16238 address += read_2_bytes (abfd, line_ptr);
16244 /* Unknown standard opcode, ignore it. */
16247 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16249 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16250 line_ptr += bytes_read;
16255 if (lh->num_file_names < file || file == 0)
16256 dwarf2_debug_line_missing_file_complaint ();
16259 lh->file_names[file - 1].included_p = 1;
16260 if (!decode_for_pst_p)
16262 addr = gdbarch_addr_bits_remove (gdbarch, address);
16263 (*p_record_line) (current_subfile, 0, addr);
16269 /* Decode the Line Number Program (LNP) for the given line_header
16270 structure and CU. The actual information extracted and the type
16271 of structures created from the LNP depends on the value of PST.
16273 1. If PST is NULL, then this procedure uses the data from the program
16274 to create all necessary symbol tables, and their linetables.
16276 2. If PST is not NULL, this procedure reads the program to determine
16277 the list of files included by the unit represented by PST, and
16278 builds all the associated partial symbol tables.
16280 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16281 It is used for relative paths in the line table.
16282 NOTE: When processing partial symtabs (pst != NULL),
16283 comp_dir == pst->dirname.
16285 NOTE: It is important that psymtabs have the same file name (via strcmp)
16286 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16287 symtab we don't use it in the name of the psymtabs we create.
16288 E.g. expand_line_sal requires this when finding psymtabs to expand.
16289 A good testcase for this is mb-inline.exp. */
16292 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16293 struct dwarf2_cu *cu, struct partial_symtab *pst,
16294 int want_line_info)
16296 struct objfile *objfile = cu->objfile;
16297 const int decode_for_pst_p = (pst != NULL);
16298 struct subfile *first_subfile = current_subfile;
16300 if (want_line_info)
16301 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
16303 if (decode_for_pst_p)
16307 /* Now that we're done scanning the Line Header Program, we can
16308 create the psymtab of each included file. */
16309 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16310 if (lh->file_names[file_index].included_p == 1)
16312 const char *include_name =
16313 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16314 if (include_name != NULL)
16315 dwarf2_create_include_psymtab (include_name, pst, objfile);
16320 /* Make sure a symtab is created for every file, even files
16321 which contain only variables (i.e. no code with associated
16325 for (i = 0; i < lh->num_file_names; i++)
16327 const char *dir = NULL;
16328 struct file_entry *fe;
16330 fe = &lh->file_names[i];
16332 dir = lh->include_dirs[fe->dir_index - 1];
16333 dwarf2_start_subfile (fe->name, dir, comp_dir);
16335 /* Skip the main file; we don't need it, and it must be
16336 allocated last, so that it will show up before the
16337 non-primary symtabs in the objfile's symtab list. */
16338 if (current_subfile == first_subfile)
16341 if (current_subfile->symtab == NULL)
16342 current_subfile->symtab = allocate_symtab (current_subfile->name,
16344 fe->symtab = current_subfile->symtab;
16349 /* Start a subfile for DWARF. FILENAME is the name of the file and
16350 DIRNAME the name of the source directory which contains FILENAME
16351 or NULL if not known. COMP_DIR is the compilation directory for the
16352 linetable's compilation unit or NULL if not known.
16353 This routine tries to keep line numbers from identical absolute and
16354 relative file names in a common subfile.
16356 Using the `list' example from the GDB testsuite, which resides in
16357 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16358 of /srcdir/list0.c yields the following debugging information for list0.c:
16360 DW_AT_name: /srcdir/list0.c
16361 DW_AT_comp_dir: /compdir
16362 files.files[0].name: list0.h
16363 files.files[0].dir: /srcdir
16364 files.files[1].name: list0.c
16365 files.files[1].dir: /srcdir
16367 The line number information for list0.c has to end up in a single
16368 subfile, so that `break /srcdir/list0.c:1' works as expected.
16369 start_subfile will ensure that this happens provided that we pass the
16370 concatenation of files.files[1].dir and files.files[1].name as the
16374 dwarf2_start_subfile (const char *filename, const char *dirname,
16375 const char *comp_dir)
16379 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16380 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16381 second argument to start_subfile. To be consistent, we do the
16382 same here. In order not to lose the line information directory,
16383 we concatenate it to the filename when it makes sense.
16384 Note that the Dwarf3 standard says (speaking of filenames in line
16385 information): ``The directory index is ignored for file names
16386 that represent full path names''. Thus ignoring dirname in the
16387 `else' branch below isn't an issue. */
16389 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
16391 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16395 start_subfile (filename, comp_dir);
16401 /* Start a symtab for DWARF.
16402 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16405 dwarf2_start_symtab (struct dwarf2_cu *cu,
16406 const char *name, const char *comp_dir, CORE_ADDR low_pc)
16408 start_symtab (name, comp_dir, low_pc);
16409 record_debugformat ("DWARF 2");
16410 record_producer (cu->producer);
16412 /* We assume that we're processing GCC output. */
16413 processing_gcc_compilation = 2;
16415 cu->processing_has_namespace_info = 0;
16419 var_decode_location (struct attribute *attr, struct symbol *sym,
16420 struct dwarf2_cu *cu)
16422 struct objfile *objfile = cu->objfile;
16423 struct comp_unit_head *cu_header = &cu->header;
16425 /* NOTE drow/2003-01-30: There used to be a comment and some special
16426 code here to turn a symbol with DW_AT_external and a
16427 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16428 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16429 with some versions of binutils) where shared libraries could have
16430 relocations against symbols in their debug information - the
16431 minimal symbol would have the right address, but the debug info
16432 would not. It's no longer necessary, because we will explicitly
16433 apply relocations when we read in the debug information now. */
16435 /* A DW_AT_location attribute with no contents indicates that a
16436 variable has been optimized away. */
16437 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16439 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16443 /* Handle one degenerate form of location expression specially, to
16444 preserve GDB's previous behavior when section offsets are
16445 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16446 then mark this symbol as LOC_STATIC. */
16448 if (attr_form_is_block (attr)
16449 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16450 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16451 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16452 && (DW_BLOCK (attr)->size
16453 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
16455 unsigned int dummy;
16457 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16458 SYMBOL_VALUE_ADDRESS (sym) =
16459 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16461 SYMBOL_VALUE_ADDRESS (sym) =
16462 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
16463 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
16464 fixup_symbol_section (sym, objfile);
16465 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16466 SYMBOL_SECTION (sym));
16470 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16471 expression evaluator, and use LOC_COMPUTED only when necessary
16472 (i.e. when the value of a register or memory location is
16473 referenced, or a thread-local block, etc.). Then again, it might
16474 not be worthwhile. I'm assuming that it isn't unless performance
16475 or memory numbers show me otherwise. */
16477 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
16479 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
16480 cu->has_loclist = 1;
16483 /* Given a pointer to a DWARF information entry, figure out if we need
16484 to make a symbol table entry for it, and if so, create a new entry
16485 and return a pointer to it.
16486 If TYPE is NULL, determine symbol type from the die, otherwise
16487 used the passed type.
16488 If SPACE is not NULL, use it to hold the new symbol. If it is
16489 NULL, allocate a new symbol on the objfile's obstack. */
16491 static struct symbol *
16492 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16493 struct symbol *space)
16495 struct objfile *objfile = cu->objfile;
16496 struct symbol *sym = NULL;
16498 struct attribute *attr = NULL;
16499 struct attribute *attr2 = NULL;
16500 CORE_ADDR baseaddr;
16501 struct pending **list_to_add = NULL;
16503 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
16505 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16507 name = dwarf2_name (die, cu);
16510 const char *linkagename;
16511 int suppress_add = 0;
16516 sym = allocate_symbol (objfile);
16517 OBJSTAT (objfile, n_syms++);
16519 /* Cache this symbol's name and the name's demangled form (if any). */
16520 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
16521 linkagename = dwarf2_physname (name, die, cu);
16522 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
16524 /* Fortran does not have mangling standard and the mangling does differ
16525 between gfortran, iFort etc. */
16526 if (cu->language == language_fortran
16527 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
16528 symbol_set_demangled_name (&(sym->ginfo),
16529 dwarf2_full_name (name, die, cu),
16532 /* Default assumptions.
16533 Use the passed type or decode it from the die. */
16534 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16535 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16537 SYMBOL_TYPE (sym) = type;
16539 SYMBOL_TYPE (sym) = die_type (die, cu);
16540 attr = dwarf2_attr (die,
16541 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16545 SYMBOL_LINE (sym) = DW_UNSND (attr);
16548 attr = dwarf2_attr (die,
16549 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16553 int file_index = DW_UNSND (attr);
16555 if (cu->line_header == NULL
16556 || file_index > cu->line_header->num_file_names)
16557 complaint (&symfile_complaints,
16558 _("file index out of range"));
16559 else if (file_index > 0)
16561 struct file_entry *fe;
16563 fe = &cu->line_header->file_names[file_index - 1];
16564 SYMBOL_SYMTAB (sym) = fe->symtab;
16571 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16574 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16576 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16577 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16578 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16579 add_symbol_to_list (sym, cu->list_in_scope);
16581 case DW_TAG_subprogram:
16582 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16584 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16585 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16586 if ((attr2 && (DW_UNSND (attr2) != 0))
16587 || cu->language == language_ada)
16589 /* Subprograms marked external are stored as a global symbol.
16590 Ada subprograms, whether marked external or not, are always
16591 stored as a global symbol, because we want to be able to
16592 access them globally. For instance, we want to be able
16593 to break on a nested subprogram without having to
16594 specify the context. */
16595 list_to_add = &global_symbols;
16599 list_to_add = cu->list_in_scope;
16602 case DW_TAG_inlined_subroutine:
16603 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16605 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16606 SYMBOL_INLINED (sym) = 1;
16607 list_to_add = cu->list_in_scope;
16609 case DW_TAG_template_value_param:
16611 /* Fall through. */
16612 case DW_TAG_constant:
16613 case DW_TAG_variable:
16614 case DW_TAG_member:
16615 /* Compilation with minimal debug info may result in
16616 variables with missing type entries. Change the
16617 misleading `void' type to something sensible. */
16618 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16620 = objfile_type (objfile)->nodebug_data_symbol;
16622 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16623 /* In the case of DW_TAG_member, we should only be called for
16624 static const members. */
16625 if (die->tag == DW_TAG_member)
16627 /* dwarf2_add_field uses die_is_declaration,
16628 so we do the same. */
16629 gdb_assert (die_is_declaration (die, cu));
16634 dwarf2_const_value (attr, sym, cu);
16635 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16638 if (attr2 && (DW_UNSND (attr2) != 0))
16639 list_to_add = &global_symbols;
16641 list_to_add = cu->list_in_scope;
16645 attr = dwarf2_attr (die, DW_AT_location, cu);
16648 var_decode_location (attr, sym, cu);
16649 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16651 /* Fortran explicitly imports any global symbols to the local
16652 scope by DW_TAG_common_block. */
16653 if (cu->language == language_fortran && die->parent
16654 && die->parent->tag == DW_TAG_common_block)
16657 if (SYMBOL_CLASS (sym) == LOC_STATIC
16658 && SYMBOL_VALUE_ADDRESS (sym) == 0
16659 && !dwarf2_per_objfile->has_section_at_zero)
16661 /* When a static variable is eliminated by the linker,
16662 the corresponding debug information is not stripped
16663 out, but the variable address is set to null;
16664 do not add such variables into symbol table. */
16666 else if (attr2 && (DW_UNSND (attr2) != 0))
16668 /* Workaround gfortran PR debug/40040 - it uses
16669 DW_AT_location for variables in -fPIC libraries which may
16670 get overriden by other libraries/executable and get
16671 a different address. Resolve it by the minimal symbol
16672 which may come from inferior's executable using copy
16673 relocation. Make this workaround only for gfortran as for
16674 other compilers GDB cannot guess the minimal symbol
16675 Fortran mangling kind. */
16676 if (cu->language == language_fortran && die->parent
16677 && die->parent->tag == DW_TAG_module
16679 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16680 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16682 /* A variable with DW_AT_external is never static,
16683 but it may be block-scoped. */
16684 list_to_add = (cu->list_in_scope == &file_symbols
16685 ? &global_symbols : cu->list_in_scope);
16688 list_to_add = cu->list_in_scope;
16692 /* We do not know the address of this symbol.
16693 If it is an external symbol and we have type information
16694 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16695 The address of the variable will then be determined from
16696 the minimal symbol table whenever the variable is
16698 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16700 /* Fortran explicitly imports any global symbols to the local
16701 scope by DW_TAG_common_block. */
16702 if (cu->language == language_fortran && die->parent
16703 && die->parent->tag == DW_TAG_common_block)
16705 /* SYMBOL_CLASS doesn't matter here because
16706 read_common_block is going to reset it. */
16708 list_to_add = cu->list_in_scope;
16710 else if (attr2 && (DW_UNSND (attr2) != 0)
16711 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16713 /* A variable with DW_AT_external is never static, but it
16714 may be block-scoped. */
16715 list_to_add = (cu->list_in_scope == &file_symbols
16716 ? &global_symbols : cu->list_in_scope);
16718 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16720 else if (!die_is_declaration (die, cu))
16722 /* Use the default LOC_OPTIMIZED_OUT class. */
16723 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16725 list_to_add = cu->list_in_scope;
16729 case DW_TAG_formal_parameter:
16730 /* If we are inside a function, mark this as an argument. If
16731 not, we might be looking at an argument to an inlined function
16732 when we do not have enough information to show inlined frames;
16733 pretend it's a local variable in that case so that the user can
16735 if (context_stack_depth > 0
16736 && context_stack[context_stack_depth - 1].name != NULL)
16737 SYMBOL_IS_ARGUMENT (sym) = 1;
16738 attr = dwarf2_attr (die, DW_AT_location, cu);
16741 var_decode_location (attr, sym, cu);
16743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16746 dwarf2_const_value (attr, sym, cu);
16749 list_to_add = cu->list_in_scope;
16751 case DW_TAG_unspecified_parameters:
16752 /* From varargs functions; gdb doesn't seem to have any
16753 interest in this information, so just ignore it for now.
16756 case DW_TAG_template_type_param:
16758 /* Fall through. */
16759 case DW_TAG_class_type:
16760 case DW_TAG_interface_type:
16761 case DW_TAG_structure_type:
16762 case DW_TAG_union_type:
16763 case DW_TAG_set_type:
16764 case DW_TAG_enumeration_type:
16765 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16766 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16769 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16770 really ever be static objects: otherwise, if you try
16771 to, say, break of a class's method and you're in a file
16772 which doesn't mention that class, it won't work unless
16773 the check for all static symbols in lookup_symbol_aux
16774 saves you. See the OtherFileClass tests in
16775 gdb.c++/namespace.exp. */
16779 list_to_add = (cu->list_in_scope == &file_symbols
16780 && (cu->language == language_cplus
16781 || cu->language == language_java)
16782 ? &global_symbols : cu->list_in_scope);
16784 /* The semantics of C++ state that "struct foo {
16785 ... }" also defines a typedef for "foo". A Java
16786 class declaration also defines a typedef for the
16788 if (cu->language == language_cplus
16789 || cu->language == language_java
16790 || cu->language == language_ada)
16792 /* The symbol's name is already allocated along
16793 with this objfile, so we don't need to
16794 duplicate it for the type. */
16795 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16796 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16801 case DW_TAG_typedef:
16802 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16803 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16804 list_to_add = cu->list_in_scope;
16806 case DW_TAG_base_type:
16807 case DW_TAG_subrange_type:
16808 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16809 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16810 list_to_add = cu->list_in_scope;
16812 case DW_TAG_enumerator:
16813 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16816 dwarf2_const_value (attr, sym, cu);
16819 /* NOTE: carlton/2003-11-10: See comment above in the
16820 DW_TAG_class_type, etc. block. */
16822 list_to_add = (cu->list_in_scope == &file_symbols
16823 && (cu->language == language_cplus
16824 || cu->language == language_java)
16825 ? &global_symbols : cu->list_in_scope);
16828 case DW_TAG_namespace:
16829 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16830 list_to_add = &global_symbols;
16832 case DW_TAG_common_block:
16833 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16834 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16835 add_symbol_to_list (sym, cu->list_in_scope);
16838 /* Not a tag we recognize. Hopefully we aren't processing
16839 trash data, but since we must specifically ignore things
16840 we don't recognize, there is nothing else we should do at
16842 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16843 dwarf_tag_name (die->tag));
16849 sym->hash_next = objfile->template_symbols;
16850 objfile->template_symbols = sym;
16851 list_to_add = NULL;
16854 if (list_to_add != NULL)
16855 add_symbol_to_list (sym, list_to_add);
16857 /* For the benefit of old versions of GCC, check for anonymous
16858 namespaces based on the demangled name. */
16859 if (!cu->processing_has_namespace_info
16860 && cu->language == language_cplus)
16861 cp_scan_for_anonymous_namespaces (sym, objfile);
16866 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16868 static struct symbol *
16869 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16871 return new_symbol_full (die, type, cu, NULL);
16874 /* Given an attr with a DW_FORM_dataN value in host byte order,
16875 zero-extend it as appropriate for the symbol's type. The DWARF
16876 standard (v4) is not entirely clear about the meaning of using
16877 DW_FORM_dataN for a constant with a signed type, where the type is
16878 wider than the data. The conclusion of a discussion on the DWARF
16879 list was that this is unspecified. We choose to always zero-extend
16880 because that is the interpretation long in use by GCC. */
16883 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
16884 struct dwarf2_cu *cu, LONGEST *value, int bits)
16886 struct objfile *objfile = cu->objfile;
16887 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16888 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16889 LONGEST l = DW_UNSND (attr);
16891 if (bits < sizeof (*value) * 8)
16893 l &= ((LONGEST) 1 << bits) - 1;
16896 else if (bits == sizeof (*value) * 8)
16900 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16901 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16908 /* Read a constant value from an attribute. Either set *VALUE, or if
16909 the value does not fit in *VALUE, set *BYTES - either already
16910 allocated on the objfile obstack, or newly allocated on OBSTACK,
16911 or, set *BATON, if we translated the constant to a location
16915 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
16916 const char *name, struct obstack *obstack,
16917 struct dwarf2_cu *cu,
16918 LONGEST *value, const gdb_byte **bytes,
16919 struct dwarf2_locexpr_baton **baton)
16921 struct objfile *objfile = cu->objfile;
16922 struct comp_unit_head *cu_header = &cu->header;
16923 struct dwarf_block *blk;
16924 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16925 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16931 switch (attr->form)
16934 case DW_FORM_GNU_addr_index:
16938 if (TYPE_LENGTH (type) != cu_header->addr_size)
16939 dwarf2_const_value_length_mismatch_complaint (name,
16940 cu_header->addr_size,
16941 TYPE_LENGTH (type));
16942 /* Symbols of this form are reasonably rare, so we just
16943 piggyback on the existing location code rather than writing
16944 a new implementation of symbol_computed_ops. */
16945 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
16946 (*baton)->per_cu = cu->per_cu;
16947 gdb_assert ((*baton)->per_cu);
16949 (*baton)->size = 2 + cu_header->addr_size;
16950 data = obstack_alloc (obstack, (*baton)->size);
16951 (*baton)->data = data;
16953 data[0] = DW_OP_addr;
16954 store_unsigned_integer (&data[1], cu_header->addr_size,
16955 byte_order, DW_ADDR (attr));
16956 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16959 case DW_FORM_string:
16961 case DW_FORM_GNU_str_index:
16962 case DW_FORM_GNU_strp_alt:
16963 /* DW_STRING is already allocated on the objfile obstack, point
16965 *bytes = (const gdb_byte *) DW_STRING (attr);
16967 case DW_FORM_block1:
16968 case DW_FORM_block2:
16969 case DW_FORM_block4:
16970 case DW_FORM_block:
16971 case DW_FORM_exprloc:
16972 blk = DW_BLOCK (attr);
16973 if (TYPE_LENGTH (type) != blk->size)
16974 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16975 TYPE_LENGTH (type));
16976 *bytes = blk->data;
16979 /* The DW_AT_const_value attributes are supposed to carry the
16980 symbol's value "represented as it would be on the target
16981 architecture." By the time we get here, it's already been
16982 converted to host endianness, so we just need to sign- or
16983 zero-extend it as appropriate. */
16984 case DW_FORM_data1:
16985 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
16987 case DW_FORM_data2:
16988 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
16990 case DW_FORM_data4:
16991 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
16993 case DW_FORM_data8:
16994 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
16997 case DW_FORM_sdata:
16998 *value = DW_SND (attr);
17001 case DW_FORM_udata:
17002 *value = DW_UNSND (attr);
17006 complaint (&symfile_complaints,
17007 _("unsupported const value attribute form: '%s'"),
17008 dwarf_form_name (attr->form));
17015 /* Copy constant value from an attribute to a symbol. */
17018 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
17019 struct dwarf2_cu *cu)
17021 struct objfile *objfile = cu->objfile;
17022 struct comp_unit_head *cu_header = &cu->header;
17024 const gdb_byte *bytes;
17025 struct dwarf2_locexpr_baton *baton;
17027 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17028 SYMBOL_PRINT_NAME (sym),
17029 &objfile->objfile_obstack, cu,
17030 &value, &bytes, &baton);
17034 SYMBOL_LOCATION_BATON (sym) = baton;
17035 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17037 else if (bytes != NULL)
17039 SYMBOL_VALUE_BYTES (sym) = bytes;
17040 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
17044 SYMBOL_VALUE (sym) = value;
17045 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
17049 /* Return the type of the die in question using its DW_AT_type attribute. */
17051 static struct type *
17052 die_type (struct die_info *die, struct dwarf2_cu *cu)
17054 struct attribute *type_attr;
17056 type_attr = dwarf2_attr (die, DW_AT_type, cu);
17059 /* A missing DW_AT_type represents a void type. */
17060 return objfile_type (cu->objfile)->builtin_void;
17063 return lookup_die_type (die, type_attr, cu);
17066 /* True iff CU's producer generates GNAT Ada auxiliary information
17067 that allows to find parallel types through that information instead
17068 of having to do expensive parallel lookups by type name. */
17071 need_gnat_info (struct dwarf2_cu *cu)
17073 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17074 of GNAT produces this auxiliary information, without any indication
17075 that it is produced. Part of enhancing the FSF version of GNAT
17076 to produce that information will be to put in place an indicator
17077 that we can use in order to determine whether the descriptive type
17078 info is available or not. One suggestion that has been made is
17079 to use a new attribute, attached to the CU die. For now, assume
17080 that the descriptive type info is not available. */
17084 /* Return the auxiliary type of the die in question using its
17085 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17086 attribute is not present. */
17088 static struct type *
17089 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17091 struct attribute *type_attr;
17093 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17097 return lookup_die_type (die, type_attr, cu);
17100 /* If DIE has a descriptive_type attribute, then set the TYPE's
17101 descriptive type accordingly. */
17104 set_descriptive_type (struct type *type, struct die_info *die,
17105 struct dwarf2_cu *cu)
17107 struct type *descriptive_type = die_descriptive_type (die, cu);
17109 if (descriptive_type)
17111 ALLOCATE_GNAT_AUX_TYPE (type);
17112 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17116 /* Return the containing type of the die in question using its
17117 DW_AT_containing_type attribute. */
17119 static struct type *
17120 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
17122 struct attribute *type_attr;
17124 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
17126 error (_("Dwarf Error: Problem turning containing type into gdb type "
17127 "[in module %s]"), objfile_name (cu->objfile));
17129 return lookup_die_type (die, type_attr, cu);
17132 /* Return an error marker type to use for the ill formed type in DIE/CU. */
17134 static struct type *
17135 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17137 struct objfile *objfile = dwarf2_per_objfile->objfile;
17138 char *message, *saved;
17140 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17141 objfile_name (objfile),
17142 cu->header.offset.sect_off,
17143 die->offset.sect_off);
17144 saved = obstack_copy0 (&objfile->objfile_obstack,
17145 message, strlen (message));
17148 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17151 /* Look up the type of DIE in CU using its type attribute ATTR.
17152 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17153 DW_AT_containing_type.
17154 If there is no type substitute an error marker. */
17156 static struct type *
17157 lookup_die_type (struct die_info *die, const struct attribute *attr,
17158 struct dwarf2_cu *cu)
17160 struct objfile *objfile = cu->objfile;
17161 struct type *this_type;
17163 gdb_assert (attr->name == DW_AT_type
17164 || attr->name == DW_AT_GNAT_descriptive_type
17165 || attr->name == DW_AT_containing_type);
17167 /* First see if we have it cached. */
17169 if (attr->form == DW_FORM_GNU_ref_alt)
17171 struct dwarf2_per_cu_data *per_cu;
17172 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17174 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17175 this_type = get_die_type_at_offset (offset, per_cu);
17177 else if (attr_form_is_ref (attr))
17179 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17181 this_type = get_die_type_at_offset (offset, cu->per_cu);
17183 else if (attr->form == DW_FORM_ref_sig8)
17185 ULONGEST signature = DW_SIGNATURE (attr);
17187 return get_signatured_type (die, signature, cu);
17191 complaint (&symfile_complaints,
17192 _("Dwarf Error: Bad type attribute %s in DIE"
17193 " at 0x%x [in module %s]"),
17194 dwarf_attr_name (attr->name), die->offset.sect_off,
17195 objfile_name (objfile));
17196 return build_error_marker_type (cu, die);
17199 /* If not cached we need to read it in. */
17201 if (this_type == NULL)
17203 struct die_info *type_die = NULL;
17204 struct dwarf2_cu *type_cu = cu;
17206 if (attr_form_is_ref (attr))
17207 type_die = follow_die_ref (die, attr, &type_cu);
17208 if (type_die == NULL)
17209 return build_error_marker_type (cu, die);
17210 /* If we find the type now, it's probably because the type came
17211 from an inter-CU reference and the type's CU got expanded before
17213 this_type = read_type_die (type_die, type_cu);
17216 /* If we still don't have a type use an error marker. */
17218 if (this_type == NULL)
17219 return build_error_marker_type (cu, die);
17224 /* Return the type in DIE, CU.
17225 Returns NULL for invalid types.
17227 This first does a lookup in die_type_hash,
17228 and only reads the die in if necessary.
17230 NOTE: This can be called when reading in partial or full symbols. */
17232 static struct type *
17233 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17235 struct type *this_type;
17237 this_type = get_die_type (die, cu);
17241 return read_type_die_1 (die, cu);
17244 /* Read the type in DIE, CU.
17245 Returns NULL for invalid types. */
17247 static struct type *
17248 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17250 struct type *this_type = NULL;
17254 case DW_TAG_class_type:
17255 case DW_TAG_interface_type:
17256 case DW_TAG_structure_type:
17257 case DW_TAG_union_type:
17258 this_type = read_structure_type (die, cu);
17260 case DW_TAG_enumeration_type:
17261 this_type = read_enumeration_type (die, cu);
17263 case DW_TAG_subprogram:
17264 case DW_TAG_subroutine_type:
17265 case DW_TAG_inlined_subroutine:
17266 this_type = read_subroutine_type (die, cu);
17268 case DW_TAG_array_type:
17269 this_type = read_array_type (die, cu);
17271 case DW_TAG_set_type:
17272 this_type = read_set_type (die, cu);
17274 case DW_TAG_pointer_type:
17275 this_type = read_tag_pointer_type (die, cu);
17277 case DW_TAG_ptr_to_member_type:
17278 this_type = read_tag_ptr_to_member_type (die, cu);
17280 case DW_TAG_reference_type:
17281 this_type = read_tag_reference_type (die, cu);
17283 case DW_TAG_const_type:
17284 this_type = read_tag_const_type (die, cu);
17286 case DW_TAG_volatile_type:
17287 this_type = read_tag_volatile_type (die, cu);
17289 case DW_TAG_restrict_type:
17290 this_type = read_tag_restrict_type (die, cu);
17292 case DW_TAG_string_type:
17293 this_type = read_tag_string_type (die, cu);
17295 case DW_TAG_typedef:
17296 this_type = read_typedef (die, cu);
17298 case DW_TAG_subrange_type:
17299 this_type = read_subrange_type (die, cu);
17301 case DW_TAG_base_type:
17302 this_type = read_base_type (die, cu);
17304 case DW_TAG_unspecified_type:
17305 this_type = read_unspecified_type (die, cu);
17307 case DW_TAG_namespace:
17308 this_type = read_namespace_type (die, cu);
17310 case DW_TAG_module:
17311 this_type = read_module_type (die, cu);
17314 complaint (&symfile_complaints,
17315 _("unexpected tag in read_type_die: '%s'"),
17316 dwarf_tag_name (die->tag));
17323 /* See if we can figure out if the class lives in a namespace. We do
17324 this by looking for a member function; its demangled name will
17325 contain namespace info, if there is any.
17326 Return the computed name or NULL.
17327 Space for the result is allocated on the objfile's obstack.
17328 This is the full-die version of guess_partial_die_structure_name.
17329 In this case we know DIE has no useful parent. */
17332 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17334 struct die_info *spec_die;
17335 struct dwarf2_cu *spec_cu;
17336 struct die_info *child;
17339 spec_die = die_specification (die, &spec_cu);
17340 if (spec_die != NULL)
17346 for (child = die->child;
17348 child = child->sibling)
17350 if (child->tag == DW_TAG_subprogram)
17352 struct attribute *attr;
17354 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17356 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17360 = language_class_name_from_physname (cu->language_defn,
17364 if (actual_name != NULL)
17366 const char *die_name = dwarf2_name (die, cu);
17368 if (die_name != NULL
17369 && strcmp (die_name, actual_name) != 0)
17371 /* Strip off the class name from the full name.
17372 We want the prefix. */
17373 int die_name_len = strlen (die_name);
17374 int actual_name_len = strlen (actual_name);
17376 /* Test for '::' as a sanity check. */
17377 if (actual_name_len > die_name_len + 2
17378 && actual_name[actual_name_len
17379 - die_name_len - 1] == ':')
17381 obstack_copy0 (&cu->objfile->objfile_obstack,
17383 actual_name_len - die_name_len - 2);
17386 xfree (actual_name);
17395 /* GCC might emit a nameless typedef that has a linkage name. Determine the
17396 prefix part in such case. See
17397 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17400 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17402 struct attribute *attr;
17405 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17406 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17409 attr = dwarf2_attr (die, DW_AT_name, cu);
17410 if (attr != NULL && DW_STRING (attr) != NULL)
17413 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17415 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17416 if (attr == NULL || DW_STRING (attr) == NULL)
17419 /* dwarf2_name had to be already called. */
17420 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17422 /* Strip the base name, keep any leading namespaces/classes. */
17423 base = strrchr (DW_STRING (attr), ':');
17424 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17427 return obstack_copy0 (&cu->objfile->objfile_obstack,
17428 DW_STRING (attr), &base[-1] - DW_STRING (attr));
17431 /* Return the name of the namespace/class that DIE is defined within,
17432 or "" if we can't tell. The caller should not xfree the result.
17434 For example, if we're within the method foo() in the following
17444 then determine_prefix on foo's die will return "N::C". */
17446 static const char *
17447 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
17449 struct die_info *parent, *spec_die;
17450 struct dwarf2_cu *spec_cu;
17451 struct type *parent_type;
17454 if (cu->language != language_cplus && cu->language != language_java
17455 && cu->language != language_fortran)
17458 retval = anonymous_struct_prefix (die, cu);
17462 /* We have to be careful in the presence of DW_AT_specification.
17463 For example, with GCC 3.4, given the code
17467 // Definition of N::foo.
17471 then we'll have a tree of DIEs like this:
17473 1: DW_TAG_compile_unit
17474 2: DW_TAG_namespace // N
17475 3: DW_TAG_subprogram // declaration of N::foo
17476 4: DW_TAG_subprogram // definition of N::foo
17477 DW_AT_specification // refers to die #3
17479 Thus, when processing die #4, we have to pretend that we're in
17480 the context of its DW_AT_specification, namely the contex of die
17483 spec_die = die_specification (die, &spec_cu);
17484 if (spec_die == NULL)
17485 parent = die->parent;
17488 parent = spec_die->parent;
17492 if (parent == NULL)
17494 else if (parent->building_fullname)
17497 const char *parent_name;
17499 /* It has been seen on RealView 2.2 built binaries,
17500 DW_TAG_template_type_param types actually _defined_ as
17501 children of the parent class:
17504 template class <class Enum> Class{};
17505 Class<enum E> class_e;
17507 1: DW_TAG_class_type (Class)
17508 2: DW_TAG_enumeration_type (E)
17509 3: DW_TAG_enumerator (enum1:0)
17510 3: DW_TAG_enumerator (enum2:1)
17512 2: DW_TAG_template_type_param
17513 DW_AT_type DW_FORM_ref_udata (E)
17515 Besides being broken debug info, it can put GDB into an
17516 infinite loop. Consider:
17518 When we're building the full name for Class<E>, we'll start
17519 at Class, and go look over its template type parameters,
17520 finding E. We'll then try to build the full name of E, and
17521 reach here. We're now trying to build the full name of E,
17522 and look over the parent DIE for containing scope. In the
17523 broken case, if we followed the parent DIE of E, we'd again
17524 find Class, and once again go look at its template type
17525 arguments, etc., etc. Simply don't consider such parent die
17526 as source-level parent of this die (it can't be, the language
17527 doesn't allow it), and break the loop here. */
17528 name = dwarf2_name (die, cu);
17529 parent_name = dwarf2_name (parent, cu);
17530 complaint (&symfile_complaints,
17531 _("template param type '%s' defined within parent '%s'"),
17532 name ? name : "<unknown>",
17533 parent_name ? parent_name : "<unknown>");
17537 switch (parent->tag)
17539 case DW_TAG_namespace:
17540 parent_type = read_type_die (parent, cu);
17541 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17542 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17543 Work around this problem here. */
17544 if (cu->language == language_cplus
17545 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17547 /* We give a name to even anonymous namespaces. */
17548 return TYPE_TAG_NAME (parent_type);
17549 case DW_TAG_class_type:
17550 case DW_TAG_interface_type:
17551 case DW_TAG_structure_type:
17552 case DW_TAG_union_type:
17553 case DW_TAG_module:
17554 parent_type = read_type_die (parent, cu);
17555 if (TYPE_TAG_NAME (parent_type) != NULL)
17556 return TYPE_TAG_NAME (parent_type);
17558 /* An anonymous structure is only allowed non-static data
17559 members; no typedefs, no member functions, et cetera.
17560 So it does not need a prefix. */
17562 case DW_TAG_compile_unit:
17563 case DW_TAG_partial_unit:
17564 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17565 if (cu->language == language_cplus
17566 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17567 && die->child != NULL
17568 && (die->tag == DW_TAG_class_type
17569 || die->tag == DW_TAG_structure_type
17570 || die->tag == DW_TAG_union_type))
17572 char *name = guess_full_die_structure_name (die, cu);
17578 return determine_prefix (parent, cu);
17582 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17583 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17584 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17585 an obconcat, otherwise allocate storage for the result. The CU argument is
17586 used to determine the language and hence, the appropriate separator. */
17588 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17591 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17592 int physname, struct dwarf2_cu *cu)
17594 const char *lead = "";
17597 if (suffix == NULL || suffix[0] == '\0'
17598 || prefix == NULL || prefix[0] == '\0')
17600 else if (cu->language == language_java)
17602 else if (cu->language == language_fortran && physname)
17604 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17605 DW_AT_MIPS_linkage_name is preferred and used instead. */
17613 if (prefix == NULL)
17615 if (suffix == NULL)
17621 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17623 strcpy (retval, lead);
17624 strcat (retval, prefix);
17625 strcat (retval, sep);
17626 strcat (retval, suffix);
17631 /* We have an obstack. */
17632 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17636 /* Return sibling of die, NULL if no sibling. */
17638 static struct die_info *
17639 sibling_die (struct die_info *die)
17641 return die->sibling;
17644 /* Get name of a die, return NULL if not found. */
17646 static const char *
17647 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17648 struct obstack *obstack)
17650 if (name && cu->language == language_cplus)
17652 char *canon_name = cp_canonicalize_string (name);
17654 if (canon_name != NULL)
17656 if (strcmp (canon_name, name) != 0)
17657 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17658 xfree (canon_name);
17665 /* Get name of a die, return NULL if not found. */
17667 static const char *
17668 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17670 struct attribute *attr;
17672 attr = dwarf2_attr (die, DW_AT_name, cu);
17673 if ((!attr || !DW_STRING (attr))
17674 && die->tag != DW_TAG_class_type
17675 && die->tag != DW_TAG_interface_type
17676 && die->tag != DW_TAG_structure_type
17677 && die->tag != DW_TAG_union_type)
17682 case DW_TAG_compile_unit:
17683 case DW_TAG_partial_unit:
17684 /* Compilation units have a DW_AT_name that is a filename, not
17685 a source language identifier. */
17686 case DW_TAG_enumeration_type:
17687 case DW_TAG_enumerator:
17688 /* These tags always have simple identifiers already; no need
17689 to canonicalize them. */
17690 return DW_STRING (attr);
17692 case DW_TAG_subprogram:
17693 /* Java constructors will all be named "<init>", so return
17694 the class name when we see this special case. */
17695 if (cu->language == language_java
17696 && DW_STRING (attr) != NULL
17697 && strcmp (DW_STRING (attr), "<init>") == 0)
17699 struct dwarf2_cu *spec_cu = cu;
17700 struct die_info *spec_die;
17702 /* GCJ will output '<init>' for Java constructor names.
17703 For this special case, return the name of the parent class. */
17705 /* GCJ may output suprogram DIEs with AT_specification set.
17706 If so, use the name of the specified DIE. */
17707 spec_die = die_specification (die, &spec_cu);
17708 if (spec_die != NULL)
17709 return dwarf2_name (spec_die, spec_cu);
17714 if (die->tag == DW_TAG_class_type)
17715 return dwarf2_name (die, cu);
17717 while (die->tag != DW_TAG_compile_unit
17718 && die->tag != DW_TAG_partial_unit);
17722 case DW_TAG_class_type:
17723 case DW_TAG_interface_type:
17724 case DW_TAG_structure_type:
17725 case DW_TAG_union_type:
17726 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17727 structures or unions. These were of the form "._%d" in GCC 4.1,
17728 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17729 and GCC 4.4. We work around this problem by ignoring these. */
17730 if (attr && DW_STRING (attr)
17731 && (strncmp (DW_STRING (attr), "._", 2) == 0
17732 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17735 /* GCC might emit a nameless typedef that has a linkage name. See
17736 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17737 if (!attr || DW_STRING (attr) == NULL)
17739 char *demangled = NULL;
17741 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17743 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17745 if (attr == NULL || DW_STRING (attr) == NULL)
17748 /* Avoid demangling DW_STRING (attr) the second time on a second
17749 call for the same DIE. */
17750 if (!DW_STRING_IS_CANONICAL (attr))
17751 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
17757 /* FIXME: we already did this for the partial symbol... */
17758 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17759 demangled, strlen (demangled));
17760 DW_STRING_IS_CANONICAL (attr) = 1;
17763 /* Strip any leading namespaces/classes, keep only the base name.
17764 DW_AT_name for named DIEs does not contain the prefixes. */
17765 base = strrchr (DW_STRING (attr), ':');
17766 if (base && base > DW_STRING (attr) && base[-1] == ':')
17769 return DW_STRING (attr);
17778 if (!DW_STRING_IS_CANONICAL (attr))
17781 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17782 &cu->objfile->objfile_obstack);
17783 DW_STRING_IS_CANONICAL (attr) = 1;
17785 return DW_STRING (attr);
17788 /* Return the die that this die in an extension of, or NULL if there
17789 is none. *EXT_CU is the CU containing DIE on input, and the CU
17790 containing the return value on output. */
17792 static struct die_info *
17793 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17795 struct attribute *attr;
17797 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17801 return follow_die_ref (die, attr, ext_cu);
17804 /* Convert a DIE tag into its string name. */
17806 static const char *
17807 dwarf_tag_name (unsigned tag)
17809 const char *name = get_DW_TAG_name (tag);
17812 return "DW_TAG_<unknown>";
17817 /* Convert a DWARF attribute code into its string name. */
17819 static const char *
17820 dwarf_attr_name (unsigned attr)
17824 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17825 if (attr == DW_AT_MIPS_fde)
17826 return "DW_AT_MIPS_fde";
17828 if (attr == DW_AT_HP_block_index)
17829 return "DW_AT_HP_block_index";
17832 name = get_DW_AT_name (attr);
17835 return "DW_AT_<unknown>";
17840 /* Convert a DWARF value form code into its string name. */
17842 static const char *
17843 dwarf_form_name (unsigned form)
17845 const char *name = get_DW_FORM_name (form);
17848 return "DW_FORM_<unknown>";
17854 dwarf_bool_name (unsigned mybool)
17862 /* Convert a DWARF type code into its string name. */
17864 static const char *
17865 dwarf_type_encoding_name (unsigned enc)
17867 const char *name = get_DW_ATE_name (enc);
17870 return "DW_ATE_<unknown>";
17876 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17880 print_spaces (indent, f);
17881 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17882 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17884 if (die->parent != NULL)
17886 print_spaces (indent, f);
17887 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17888 die->parent->offset.sect_off);
17891 print_spaces (indent, f);
17892 fprintf_unfiltered (f, " has children: %s\n",
17893 dwarf_bool_name (die->child != NULL));
17895 print_spaces (indent, f);
17896 fprintf_unfiltered (f, " attributes:\n");
17898 for (i = 0; i < die->num_attrs; ++i)
17900 print_spaces (indent, f);
17901 fprintf_unfiltered (f, " %s (%s) ",
17902 dwarf_attr_name (die->attrs[i].name),
17903 dwarf_form_name (die->attrs[i].form));
17905 switch (die->attrs[i].form)
17908 case DW_FORM_GNU_addr_index:
17909 fprintf_unfiltered (f, "address: ");
17910 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17912 case DW_FORM_block2:
17913 case DW_FORM_block4:
17914 case DW_FORM_block:
17915 case DW_FORM_block1:
17916 fprintf_unfiltered (f, "block: size %s",
17917 pulongest (DW_BLOCK (&die->attrs[i])->size));
17919 case DW_FORM_exprloc:
17920 fprintf_unfiltered (f, "expression: size %s",
17921 pulongest (DW_BLOCK (&die->attrs[i])->size));
17923 case DW_FORM_ref_addr:
17924 fprintf_unfiltered (f, "ref address: ");
17925 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17927 case DW_FORM_GNU_ref_alt:
17928 fprintf_unfiltered (f, "alt ref address: ");
17929 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17935 case DW_FORM_ref_udata:
17936 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17937 (long) (DW_UNSND (&die->attrs[i])));
17939 case DW_FORM_data1:
17940 case DW_FORM_data2:
17941 case DW_FORM_data4:
17942 case DW_FORM_data8:
17943 case DW_FORM_udata:
17944 case DW_FORM_sdata:
17945 fprintf_unfiltered (f, "constant: %s",
17946 pulongest (DW_UNSND (&die->attrs[i])));
17948 case DW_FORM_sec_offset:
17949 fprintf_unfiltered (f, "section offset: %s",
17950 pulongest (DW_UNSND (&die->attrs[i])));
17952 case DW_FORM_ref_sig8:
17953 fprintf_unfiltered (f, "signature: %s",
17954 hex_string (DW_SIGNATURE (&die->attrs[i])));
17956 case DW_FORM_string:
17958 case DW_FORM_GNU_str_index:
17959 case DW_FORM_GNU_strp_alt:
17960 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17961 DW_STRING (&die->attrs[i])
17962 ? DW_STRING (&die->attrs[i]) : "",
17963 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17966 if (DW_UNSND (&die->attrs[i]))
17967 fprintf_unfiltered (f, "flag: TRUE");
17969 fprintf_unfiltered (f, "flag: FALSE");
17971 case DW_FORM_flag_present:
17972 fprintf_unfiltered (f, "flag: TRUE");
17974 case DW_FORM_indirect:
17975 /* The reader will have reduced the indirect form to
17976 the "base form" so this form should not occur. */
17977 fprintf_unfiltered (f,
17978 "unexpected attribute form: DW_FORM_indirect");
17981 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17982 die->attrs[i].form);
17985 fprintf_unfiltered (f, "\n");
17990 dump_die_for_error (struct die_info *die)
17992 dump_die_shallow (gdb_stderr, 0, die);
17996 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17998 int indent = level * 4;
18000 gdb_assert (die != NULL);
18002 if (level >= max_level)
18005 dump_die_shallow (f, indent, die);
18007 if (die->child != NULL)
18009 print_spaces (indent, f);
18010 fprintf_unfiltered (f, " Children:");
18011 if (level + 1 < max_level)
18013 fprintf_unfiltered (f, "\n");
18014 dump_die_1 (f, level + 1, max_level, die->child);
18018 fprintf_unfiltered (f,
18019 " [not printed, max nesting level reached]\n");
18023 if (die->sibling != NULL && level > 0)
18025 dump_die_1 (f, level, max_level, die->sibling);
18029 /* This is called from the pdie macro in gdbinit.in.
18030 It's not static so gcc will keep a copy callable from gdb. */
18033 dump_die (struct die_info *die, int max_level)
18035 dump_die_1 (gdb_stdlog, 0, max_level, die);
18039 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
18043 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18049 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18053 dwarf2_get_ref_die_offset (const struct attribute *attr)
18055 sect_offset retval = { DW_UNSND (attr) };
18057 if (attr_form_is_ref (attr))
18060 retval.sect_off = 0;
18061 complaint (&symfile_complaints,
18062 _("unsupported die ref attribute form: '%s'"),
18063 dwarf_form_name (attr->form));
18067 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18068 * the value held by the attribute is not constant. */
18071 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
18073 if (attr->form == DW_FORM_sdata)
18074 return DW_SND (attr);
18075 else if (attr->form == DW_FORM_udata
18076 || attr->form == DW_FORM_data1
18077 || attr->form == DW_FORM_data2
18078 || attr->form == DW_FORM_data4
18079 || attr->form == DW_FORM_data8)
18080 return DW_UNSND (attr);
18083 complaint (&symfile_complaints,
18084 _("Attribute value is not a constant (%s)"),
18085 dwarf_form_name (attr->form));
18086 return default_value;
18090 /* Follow reference or signature attribute ATTR of SRC_DIE.
18091 On entry *REF_CU is the CU of SRC_DIE.
18092 On exit *REF_CU is the CU of the result. */
18094 static struct die_info *
18095 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
18096 struct dwarf2_cu **ref_cu)
18098 struct die_info *die;
18100 if (attr_form_is_ref (attr))
18101 die = follow_die_ref (src_die, attr, ref_cu);
18102 else if (attr->form == DW_FORM_ref_sig8)
18103 die = follow_die_sig (src_die, attr, ref_cu);
18106 dump_die_for_error (src_die);
18107 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18108 objfile_name ((*ref_cu)->objfile));
18114 /* Follow reference OFFSET.
18115 On entry *REF_CU is the CU of the source die referencing OFFSET.
18116 On exit *REF_CU is the CU of the result.
18117 Returns NULL if OFFSET is invalid. */
18119 static struct die_info *
18120 follow_die_offset (sect_offset offset, int offset_in_dwz,
18121 struct dwarf2_cu **ref_cu)
18123 struct die_info temp_die;
18124 struct dwarf2_cu *target_cu, *cu = *ref_cu;
18126 gdb_assert (cu->per_cu != NULL);
18130 if (cu->per_cu->is_debug_types)
18132 /* .debug_types CUs cannot reference anything outside their CU.
18133 If they need to, they have to reference a signatured type via
18134 DW_FORM_ref_sig8. */
18135 if (! offset_in_cu_p (&cu->header, offset))
18138 else if (offset_in_dwz != cu->per_cu->is_dwz
18139 || ! offset_in_cu_p (&cu->header, offset))
18141 struct dwarf2_per_cu_data *per_cu;
18143 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18146 /* If necessary, add it to the queue and load its DIEs. */
18147 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18148 load_full_comp_unit (per_cu, cu->language);
18150 target_cu = per_cu->cu;
18152 else if (cu->dies == NULL)
18154 /* We're loading full DIEs during partial symbol reading. */
18155 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18156 load_full_comp_unit (cu->per_cu, language_minimal);
18159 *ref_cu = target_cu;
18160 temp_die.offset = offset;
18161 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18164 /* Follow reference attribute ATTR of SRC_DIE.
18165 On entry *REF_CU is the CU of SRC_DIE.
18166 On exit *REF_CU is the CU of the result. */
18168 static struct die_info *
18169 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
18170 struct dwarf2_cu **ref_cu)
18172 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18173 struct dwarf2_cu *cu = *ref_cu;
18174 struct die_info *die;
18176 die = follow_die_offset (offset,
18177 (attr->form == DW_FORM_GNU_ref_alt
18178 || cu->per_cu->is_dwz),
18181 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18182 "at 0x%x [in module %s]"),
18183 offset.sect_off, src_die->offset.sect_off,
18184 objfile_name (cu->objfile));
18189 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18190 Returned value is intended for DW_OP_call*. Returned
18191 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
18193 struct dwarf2_locexpr_baton
18194 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18195 struct dwarf2_per_cu_data *per_cu,
18196 CORE_ADDR (*get_frame_pc) (void *baton),
18199 struct dwarf2_cu *cu;
18200 struct die_info *die;
18201 struct attribute *attr;
18202 struct dwarf2_locexpr_baton retval;
18204 dw2_setup (per_cu->objfile);
18206 if (per_cu->cu == NULL)
18210 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18212 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18213 offset.sect_off, objfile_name (per_cu->objfile));
18215 attr = dwarf2_attr (die, DW_AT_location, cu);
18218 /* DWARF: "If there is no such attribute, then there is no effect.".
18219 DATA is ignored if SIZE is 0. */
18221 retval.data = NULL;
18224 else if (attr_form_is_section_offset (attr))
18226 struct dwarf2_loclist_baton loclist_baton;
18227 CORE_ADDR pc = (*get_frame_pc) (baton);
18230 fill_in_loclist_baton (cu, &loclist_baton, attr);
18232 retval.data = dwarf2_find_location_expression (&loclist_baton,
18234 retval.size = size;
18238 if (!attr_form_is_block (attr))
18239 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18240 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18241 offset.sect_off, objfile_name (per_cu->objfile));
18243 retval.data = DW_BLOCK (attr)->data;
18244 retval.size = DW_BLOCK (attr)->size;
18246 retval.per_cu = cu->per_cu;
18248 age_cached_comp_units ();
18253 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18256 struct dwarf2_locexpr_baton
18257 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18258 struct dwarf2_per_cu_data *per_cu,
18259 CORE_ADDR (*get_frame_pc) (void *baton),
18262 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18264 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18267 /* Write a constant of a given type as target-ordered bytes into
18270 static const gdb_byte *
18271 write_constant_as_bytes (struct obstack *obstack,
18272 enum bfd_endian byte_order,
18279 *len = TYPE_LENGTH (type);
18280 result = obstack_alloc (obstack, *len);
18281 store_unsigned_integer (result, *len, byte_order, value);
18286 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18287 pointer to the constant bytes and set LEN to the length of the
18288 data. If memory is needed, allocate it on OBSTACK. If the DIE
18289 does not have a DW_AT_const_value, return NULL. */
18292 dwarf2_fetch_constant_bytes (sect_offset offset,
18293 struct dwarf2_per_cu_data *per_cu,
18294 struct obstack *obstack,
18297 struct dwarf2_cu *cu;
18298 struct die_info *die;
18299 struct attribute *attr;
18300 const gdb_byte *result = NULL;
18303 enum bfd_endian byte_order;
18305 dw2_setup (per_cu->objfile);
18307 if (per_cu->cu == NULL)
18311 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18313 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18314 offset.sect_off, objfile_name (per_cu->objfile));
18317 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18321 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18322 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18324 switch (attr->form)
18327 case DW_FORM_GNU_addr_index:
18331 *len = cu->header.addr_size;
18332 tem = obstack_alloc (obstack, *len);
18333 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18337 case DW_FORM_string:
18339 case DW_FORM_GNU_str_index:
18340 case DW_FORM_GNU_strp_alt:
18341 /* DW_STRING is already allocated on the objfile obstack, point
18343 result = (const gdb_byte *) DW_STRING (attr);
18344 *len = strlen (DW_STRING (attr));
18346 case DW_FORM_block1:
18347 case DW_FORM_block2:
18348 case DW_FORM_block4:
18349 case DW_FORM_block:
18350 case DW_FORM_exprloc:
18351 result = DW_BLOCK (attr)->data;
18352 *len = DW_BLOCK (attr)->size;
18355 /* The DW_AT_const_value attributes are supposed to carry the
18356 symbol's value "represented as it would be on the target
18357 architecture." By the time we get here, it's already been
18358 converted to host endianness, so we just need to sign- or
18359 zero-extend it as appropriate. */
18360 case DW_FORM_data1:
18361 type = die_type (die, cu);
18362 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18363 if (result == NULL)
18364 result = write_constant_as_bytes (obstack, byte_order,
18367 case DW_FORM_data2:
18368 type = die_type (die, cu);
18369 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18370 if (result == NULL)
18371 result = write_constant_as_bytes (obstack, byte_order,
18374 case DW_FORM_data4:
18375 type = die_type (die, cu);
18376 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18377 if (result == NULL)
18378 result = write_constant_as_bytes (obstack, byte_order,
18381 case DW_FORM_data8:
18382 type = die_type (die, cu);
18383 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18384 if (result == NULL)
18385 result = write_constant_as_bytes (obstack, byte_order,
18389 case DW_FORM_sdata:
18390 type = die_type (die, cu);
18391 result = write_constant_as_bytes (obstack, byte_order,
18392 type, DW_SND (attr), len);
18395 case DW_FORM_udata:
18396 type = die_type (die, cu);
18397 result = write_constant_as_bytes (obstack, byte_order,
18398 type, DW_UNSND (attr), len);
18402 complaint (&symfile_complaints,
18403 _("unsupported const value attribute form: '%s'"),
18404 dwarf_form_name (attr->form));
18411 /* Return the type of the DIE at DIE_OFFSET in the CU named by
18415 dwarf2_get_die_type (cu_offset die_offset,
18416 struct dwarf2_per_cu_data *per_cu)
18418 sect_offset die_offset_sect;
18420 dw2_setup (per_cu->objfile);
18422 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18423 return get_die_type_at_offset (die_offset_sect, per_cu);
18426 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
18427 On entry *REF_CU is the CU of SRC_DIE.
18428 On exit *REF_CU is the CU of the result.
18429 Returns NULL if the referenced DIE isn't found. */
18431 static struct die_info *
18432 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18433 struct dwarf2_cu **ref_cu)
18435 struct objfile *objfile = (*ref_cu)->objfile;
18436 struct die_info temp_die;
18437 struct dwarf2_cu *sig_cu;
18438 struct die_info *die;
18440 /* While it might be nice to assert sig_type->type == NULL here,
18441 we can get here for DW_AT_imported_declaration where we need
18442 the DIE not the type. */
18444 /* If necessary, add it to the queue and load its DIEs. */
18446 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
18447 read_signatured_type (sig_type);
18449 sig_cu = sig_type->per_cu.cu;
18450 gdb_assert (sig_cu != NULL);
18451 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18452 temp_die.offset = sig_type->type_offset_in_section;
18453 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18454 temp_die.offset.sect_off);
18457 /* For .gdb_index version 7 keep track of included TUs.
18458 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18459 if (dwarf2_per_objfile->index_table != NULL
18460 && dwarf2_per_objfile->index_table->version <= 7)
18462 VEC_safe_push (dwarf2_per_cu_ptr,
18463 (*ref_cu)->per_cu->imported_symtabs,
18474 /* Follow signatured type referenced by ATTR in SRC_DIE.
18475 On entry *REF_CU is the CU of SRC_DIE.
18476 On exit *REF_CU is the CU of the result.
18477 The result is the DIE of the type.
18478 If the referenced type cannot be found an error is thrown. */
18480 static struct die_info *
18481 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
18482 struct dwarf2_cu **ref_cu)
18484 ULONGEST signature = DW_SIGNATURE (attr);
18485 struct signatured_type *sig_type;
18486 struct die_info *die;
18488 gdb_assert (attr->form == DW_FORM_ref_sig8);
18490 sig_type = lookup_signatured_type (*ref_cu, signature);
18491 /* sig_type will be NULL if the signatured type is missing from
18493 if (sig_type == NULL)
18495 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18496 " from DIE at 0x%x [in module %s]"),
18497 hex_string (signature), src_die->offset.sect_off,
18498 objfile_name ((*ref_cu)->objfile));
18501 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18504 dump_die_for_error (src_die);
18505 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18506 " from DIE at 0x%x [in module %s]"),
18507 hex_string (signature), src_die->offset.sect_off,
18508 objfile_name ((*ref_cu)->objfile));
18514 /* Get the type specified by SIGNATURE referenced in DIE/CU,
18515 reading in and processing the type unit if necessary. */
18517 static struct type *
18518 get_signatured_type (struct die_info *die, ULONGEST signature,
18519 struct dwarf2_cu *cu)
18521 struct signatured_type *sig_type;
18522 struct dwarf2_cu *type_cu;
18523 struct die_info *type_die;
18526 sig_type = lookup_signatured_type (cu, signature);
18527 /* sig_type will be NULL if the signatured type is missing from
18529 if (sig_type == NULL)
18531 complaint (&symfile_complaints,
18532 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18533 " from DIE at 0x%x [in module %s]"),
18534 hex_string (signature), die->offset.sect_off,
18535 objfile_name (dwarf2_per_objfile->objfile));
18536 return build_error_marker_type (cu, die);
18539 /* If we already know the type we're done. */
18540 if (sig_type->type != NULL)
18541 return sig_type->type;
18544 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18545 if (type_die != NULL)
18547 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18548 is created. This is important, for example, because for c++ classes
18549 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18550 type = read_type_die (type_die, type_cu);
18553 complaint (&symfile_complaints,
18554 _("Dwarf Error: Cannot build signatured type %s"
18555 " referenced from DIE at 0x%x [in module %s]"),
18556 hex_string (signature), die->offset.sect_off,
18557 objfile_name (dwarf2_per_objfile->objfile));
18558 type = build_error_marker_type (cu, die);
18563 complaint (&symfile_complaints,
18564 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18565 " from DIE at 0x%x [in module %s]"),
18566 hex_string (signature), die->offset.sect_off,
18567 objfile_name (dwarf2_per_objfile->objfile));
18568 type = build_error_marker_type (cu, die);
18570 sig_type->type = type;
18575 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18576 reading in and processing the type unit if necessary. */
18578 static struct type *
18579 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
18580 struct dwarf2_cu *cu) /* ARI: editCase function */
18582 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18583 if (attr_form_is_ref (attr))
18585 struct dwarf2_cu *type_cu = cu;
18586 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18588 return read_type_die (type_die, type_cu);
18590 else if (attr->form == DW_FORM_ref_sig8)
18592 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18596 complaint (&symfile_complaints,
18597 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18598 " at 0x%x [in module %s]"),
18599 dwarf_form_name (attr->form), die->offset.sect_off,
18600 objfile_name (dwarf2_per_objfile->objfile));
18601 return build_error_marker_type (cu, die);
18605 /* Load the DIEs associated with type unit PER_CU into memory. */
18608 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
18610 struct signatured_type *sig_type;
18612 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18613 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18615 /* We have the per_cu, but we need the signatured_type.
18616 Fortunately this is an easy translation. */
18617 gdb_assert (per_cu->is_debug_types);
18618 sig_type = (struct signatured_type *) per_cu;
18620 gdb_assert (per_cu->cu == NULL);
18622 read_signatured_type (sig_type);
18624 gdb_assert (per_cu->cu != NULL);
18627 /* die_reader_func for read_signatured_type.
18628 This is identical to load_full_comp_unit_reader,
18629 but is kept separate for now. */
18632 read_signatured_type_reader (const struct die_reader_specs *reader,
18633 const gdb_byte *info_ptr,
18634 struct die_info *comp_unit_die,
18638 struct dwarf2_cu *cu = reader->cu;
18640 gdb_assert (cu->die_hash == NULL);
18642 htab_create_alloc_ex (cu->header.length / 12,
18646 &cu->comp_unit_obstack,
18647 hashtab_obstack_allocate,
18648 dummy_obstack_deallocate);
18651 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18652 &info_ptr, comp_unit_die);
18653 cu->dies = comp_unit_die;
18654 /* comp_unit_die is not stored in die_hash, no need. */
18656 /* We try not to read any attributes in this function, because not
18657 all CUs needed for references have been loaded yet, and symbol
18658 table processing isn't initialized. But we have to set the CU language,
18659 or we won't be able to build types correctly.
18660 Similarly, if we do not read the producer, we can not apply
18661 producer-specific interpretation. */
18662 prepare_one_comp_unit (cu, cu->dies, language_minimal);
18665 /* Read in a signatured type and build its CU and DIEs.
18666 If the type is a stub for the real type in a DWO file,
18667 read in the real type from the DWO file as well. */
18670 read_signatured_type (struct signatured_type *sig_type)
18672 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
18674 gdb_assert (per_cu->is_debug_types);
18675 gdb_assert (per_cu->cu == NULL);
18677 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18678 read_signatured_type_reader, NULL);
18679 sig_type->per_cu.tu_read = 1;
18682 /* Decode simple location descriptions.
18683 Given a pointer to a dwarf block that defines a location, compute
18684 the location and return the value.
18686 NOTE drow/2003-11-18: This function is called in two situations
18687 now: for the address of static or global variables (partial symbols
18688 only) and for offsets into structures which are expected to be
18689 (more or less) constant. The partial symbol case should go away,
18690 and only the constant case should remain. That will let this
18691 function complain more accurately. A few special modes are allowed
18692 without complaint for global variables (for instance, global
18693 register values and thread-local values).
18695 A location description containing no operations indicates that the
18696 object is optimized out. The return value is 0 for that case.
18697 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18698 callers will only want a very basic result and this can become a
18701 Note that stack[0] is unused except as a default error return. */
18704 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
18706 struct objfile *objfile = cu->objfile;
18708 size_t size = blk->size;
18709 const gdb_byte *data = blk->data;
18710 CORE_ADDR stack[64];
18712 unsigned int bytes_read, unsnd;
18718 stack[++stacki] = 0;
18757 stack[++stacki] = op - DW_OP_lit0;
18792 stack[++stacki] = op - DW_OP_reg0;
18794 dwarf2_complex_location_expr_complaint ();
18798 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18800 stack[++stacki] = unsnd;
18802 dwarf2_complex_location_expr_complaint ();
18806 stack[++stacki] = read_address (objfile->obfd, &data[i],
18811 case DW_OP_const1u:
18812 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18816 case DW_OP_const1s:
18817 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18821 case DW_OP_const2u:
18822 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18826 case DW_OP_const2s:
18827 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18831 case DW_OP_const4u:
18832 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18836 case DW_OP_const4s:
18837 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18841 case DW_OP_const8u:
18842 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18847 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18853 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18858 stack[stacki + 1] = stack[stacki];
18863 stack[stacki - 1] += stack[stacki];
18867 case DW_OP_plus_uconst:
18868 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18874 stack[stacki - 1] -= stack[stacki];
18879 /* If we're not the last op, then we definitely can't encode
18880 this using GDB's address_class enum. This is valid for partial
18881 global symbols, although the variable's address will be bogus
18884 dwarf2_complex_location_expr_complaint ();
18887 case DW_OP_GNU_push_tls_address:
18888 /* The top of the stack has the offset from the beginning
18889 of the thread control block at which the variable is located. */
18890 /* Nothing should follow this operator, so the top of stack would
18892 /* This is valid for partial global symbols, but the variable's
18893 address will be bogus in the psymtab. Make it always at least
18894 non-zero to not look as a variable garbage collected by linker
18895 which have DW_OP_addr 0. */
18897 dwarf2_complex_location_expr_complaint ();
18901 case DW_OP_GNU_uninit:
18904 case DW_OP_GNU_addr_index:
18905 case DW_OP_GNU_const_index:
18906 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18913 const char *name = get_DW_OP_name (op);
18916 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18919 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18923 return (stack[stacki]);
18926 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18927 outside of the allocated space. Also enforce minimum>0. */
18928 if (stacki >= ARRAY_SIZE (stack) - 1)
18930 complaint (&symfile_complaints,
18931 _("location description stack overflow"));
18937 complaint (&symfile_complaints,
18938 _("location description stack underflow"));
18942 return (stack[stacki]);
18945 /* memory allocation interface */
18947 static struct dwarf_block *
18948 dwarf_alloc_block (struct dwarf2_cu *cu)
18950 struct dwarf_block *blk;
18952 blk = (struct dwarf_block *)
18953 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18957 static struct die_info *
18958 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18960 struct die_info *die;
18961 size_t size = sizeof (struct die_info);
18964 size += (num_attrs - 1) * sizeof (struct attribute);
18966 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18967 memset (die, 0, sizeof (struct die_info));
18972 /* Macro support. */
18974 /* Return file name relative to the compilation directory of file number I in
18975 *LH's file name table. The result is allocated using xmalloc; the caller is
18976 responsible for freeing it. */
18979 file_file_name (int file, struct line_header *lh)
18981 /* Is the file number a valid index into the line header's file name
18982 table? Remember that file numbers start with one, not zero. */
18983 if (1 <= file && file <= lh->num_file_names)
18985 struct file_entry *fe = &lh->file_names[file - 1];
18987 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18988 return xstrdup (fe->name);
18989 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18994 /* The compiler produced a bogus file number. We can at least
18995 record the macro definitions made in the file, even if we
18996 won't be able to find the file by name. */
18997 char fake_name[80];
18999 xsnprintf (fake_name, sizeof (fake_name),
19000 "<bad macro file number %d>", file);
19002 complaint (&symfile_complaints,
19003 _("bad file number in macro information (%d)"),
19006 return xstrdup (fake_name);
19010 /* Return the full name of file number I in *LH's file name table.
19011 Use COMP_DIR as the name of the current directory of the
19012 compilation. The result is allocated using xmalloc; the caller is
19013 responsible for freeing it. */
19015 file_full_name (int file, struct line_header *lh, const char *comp_dir)
19017 /* Is the file number a valid index into the line header's file name
19018 table? Remember that file numbers start with one, not zero. */
19019 if (1 <= file && file <= lh->num_file_names)
19021 char *relative = file_file_name (file, lh);
19023 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19025 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19028 return file_file_name (file, lh);
19032 static struct macro_source_file *
19033 macro_start_file (int file, int line,
19034 struct macro_source_file *current_file,
19035 const char *comp_dir,
19036 struct line_header *lh, struct objfile *objfile)
19038 /* File name relative to the compilation directory of this source file. */
19039 char *file_name = file_file_name (file, lh);
19041 if (! current_file)
19043 /* Note: We don't create a macro table for this compilation unit
19044 at all until we actually get a filename. */
19045 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19047 /* If we have no current file, then this must be the start_file
19048 directive for the compilation unit's main source file. */
19049 current_file = macro_set_main (macro_table, file_name);
19050 macro_define_special (macro_table);
19053 current_file = macro_include (current_file, line, file_name);
19057 return current_file;
19061 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19062 followed by a null byte. */
19064 copy_string (const char *buf, int len)
19066 char *s = xmalloc (len + 1);
19068 memcpy (s, buf, len);
19074 static const char *
19075 consume_improper_spaces (const char *p, const char *body)
19079 complaint (&symfile_complaints,
19080 _("macro definition contains spaces "
19081 "in formal argument list:\n`%s'"),
19093 parse_macro_definition (struct macro_source_file *file, int line,
19098 /* The body string takes one of two forms. For object-like macro
19099 definitions, it should be:
19101 <macro name> " " <definition>
19103 For function-like macro definitions, it should be:
19105 <macro name> "() " <definition>
19107 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19109 Spaces may appear only where explicitly indicated, and in the
19112 The Dwarf 2 spec says that an object-like macro's name is always
19113 followed by a space, but versions of GCC around March 2002 omit
19114 the space when the macro's definition is the empty string.
19116 The Dwarf 2 spec says that there should be no spaces between the
19117 formal arguments in a function-like macro's formal argument list,
19118 but versions of GCC around March 2002 include spaces after the
19122 /* Find the extent of the macro name. The macro name is terminated
19123 by either a space or null character (for an object-like macro) or
19124 an opening paren (for a function-like macro). */
19125 for (p = body; *p; p++)
19126 if (*p == ' ' || *p == '(')
19129 if (*p == ' ' || *p == '\0')
19131 /* It's an object-like macro. */
19132 int name_len = p - body;
19133 char *name = copy_string (body, name_len);
19134 const char *replacement;
19137 replacement = body + name_len + 1;
19140 dwarf2_macro_malformed_definition_complaint (body);
19141 replacement = body + name_len;
19144 macro_define_object (file, line, name, replacement);
19148 else if (*p == '(')
19150 /* It's a function-like macro. */
19151 char *name = copy_string (body, p - body);
19154 char **argv = xmalloc (argv_size * sizeof (*argv));
19158 p = consume_improper_spaces (p, body);
19160 /* Parse the formal argument list. */
19161 while (*p && *p != ')')
19163 /* Find the extent of the current argument name. */
19164 const char *arg_start = p;
19166 while (*p && *p != ',' && *p != ')' && *p != ' ')
19169 if (! *p || p == arg_start)
19170 dwarf2_macro_malformed_definition_complaint (body);
19173 /* Make sure argv has room for the new argument. */
19174 if (argc >= argv_size)
19177 argv = xrealloc (argv, argv_size * sizeof (*argv));
19180 argv[argc++] = copy_string (arg_start, p - arg_start);
19183 p = consume_improper_spaces (p, body);
19185 /* Consume the comma, if present. */
19190 p = consume_improper_spaces (p, body);
19199 /* Perfectly formed definition, no complaints. */
19200 macro_define_function (file, line, name,
19201 argc, (const char **) argv,
19203 else if (*p == '\0')
19205 /* Complain, but do define it. */
19206 dwarf2_macro_malformed_definition_complaint (body);
19207 macro_define_function (file, line, name,
19208 argc, (const char **) argv,
19212 /* Just complain. */
19213 dwarf2_macro_malformed_definition_complaint (body);
19216 /* Just complain. */
19217 dwarf2_macro_malformed_definition_complaint (body);
19223 for (i = 0; i < argc; i++)
19229 dwarf2_macro_malformed_definition_complaint (body);
19232 /* Skip some bytes from BYTES according to the form given in FORM.
19233 Returns the new pointer. */
19235 static const gdb_byte *
19236 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19237 enum dwarf_form form,
19238 unsigned int offset_size,
19239 struct dwarf2_section_info *section)
19241 unsigned int bytes_read;
19245 case DW_FORM_data1:
19250 case DW_FORM_data2:
19254 case DW_FORM_data4:
19258 case DW_FORM_data8:
19262 case DW_FORM_string:
19263 read_direct_string (abfd, bytes, &bytes_read);
19264 bytes += bytes_read;
19267 case DW_FORM_sec_offset:
19269 case DW_FORM_GNU_strp_alt:
19270 bytes += offset_size;
19273 case DW_FORM_block:
19274 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19275 bytes += bytes_read;
19278 case DW_FORM_block1:
19279 bytes += 1 + read_1_byte (abfd, bytes);
19281 case DW_FORM_block2:
19282 bytes += 2 + read_2_bytes (abfd, bytes);
19284 case DW_FORM_block4:
19285 bytes += 4 + read_4_bytes (abfd, bytes);
19288 case DW_FORM_sdata:
19289 case DW_FORM_udata:
19290 case DW_FORM_GNU_addr_index:
19291 case DW_FORM_GNU_str_index:
19292 bytes = gdb_skip_leb128 (bytes, buffer_end);
19295 dwarf2_section_buffer_overflow_complaint (section);
19303 complaint (&symfile_complaints,
19304 _("invalid form 0x%x in `%s'"),
19306 section->asection->name);
19314 /* A helper for dwarf_decode_macros that handles skipping an unknown
19315 opcode. Returns an updated pointer to the macro data buffer; or,
19316 on error, issues a complaint and returns NULL. */
19318 static const gdb_byte *
19319 skip_unknown_opcode (unsigned int opcode,
19320 const gdb_byte **opcode_definitions,
19321 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19323 unsigned int offset_size,
19324 struct dwarf2_section_info *section)
19326 unsigned int bytes_read, i;
19328 const gdb_byte *defn;
19330 if (opcode_definitions[opcode] == NULL)
19332 complaint (&symfile_complaints,
19333 _("unrecognized DW_MACFINO opcode 0x%x"),
19338 defn = opcode_definitions[opcode];
19339 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19340 defn += bytes_read;
19342 for (i = 0; i < arg; ++i)
19344 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19346 if (mac_ptr == NULL)
19348 /* skip_form_bytes already issued the complaint. */
19356 /* A helper function which parses the header of a macro section.
19357 If the macro section is the extended (for now called "GNU") type,
19358 then this updates *OFFSET_SIZE. Returns a pointer to just after
19359 the header, or issues a complaint and returns NULL on error. */
19361 static const gdb_byte *
19362 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
19364 const gdb_byte *mac_ptr,
19365 unsigned int *offset_size,
19366 int section_is_gnu)
19368 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
19370 if (section_is_gnu)
19372 unsigned int version, flags;
19374 version = read_2_bytes (abfd, mac_ptr);
19377 complaint (&symfile_complaints,
19378 _("unrecognized version `%d' in .debug_macro section"),
19384 flags = read_1_byte (abfd, mac_ptr);
19386 *offset_size = (flags & 1) ? 8 : 4;
19388 if ((flags & 2) != 0)
19389 /* We don't need the line table offset. */
19390 mac_ptr += *offset_size;
19392 /* Vendor opcode descriptions. */
19393 if ((flags & 4) != 0)
19395 unsigned int i, count;
19397 count = read_1_byte (abfd, mac_ptr);
19399 for (i = 0; i < count; ++i)
19401 unsigned int opcode, bytes_read;
19404 opcode = read_1_byte (abfd, mac_ptr);
19406 opcode_definitions[opcode] = mac_ptr;
19407 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19408 mac_ptr += bytes_read;
19417 /* A helper for dwarf_decode_macros that handles the GNU extensions,
19418 including DW_MACRO_GNU_transparent_include. */
19421 dwarf_decode_macro_bytes (bfd *abfd,
19422 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19423 struct macro_source_file *current_file,
19424 struct line_header *lh, const char *comp_dir,
19425 struct dwarf2_section_info *section,
19426 int section_is_gnu, int section_is_dwz,
19427 unsigned int offset_size,
19428 struct objfile *objfile,
19429 htab_t include_hash)
19431 enum dwarf_macro_record_type macinfo_type;
19432 int at_commandline;
19433 const gdb_byte *opcode_definitions[256];
19435 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19436 &offset_size, section_is_gnu);
19437 if (mac_ptr == NULL)
19439 /* We already issued a complaint. */
19443 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19444 GDB is still reading the definitions from command line. First
19445 DW_MACINFO_start_file will need to be ignored as it was already executed
19446 to create CURRENT_FILE for the main source holding also the command line
19447 definitions. On first met DW_MACINFO_start_file this flag is reset to
19448 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19450 at_commandline = 1;
19454 /* Do we at least have room for a macinfo type byte? */
19455 if (mac_ptr >= mac_end)
19457 dwarf2_section_buffer_overflow_complaint (section);
19461 macinfo_type = read_1_byte (abfd, mac_ptr);
19464 /* Note that we rely on the fact that the corresponding GNU and
19465 DWARF constants are the same. */
19466 switch (macinfo_type)
19468 /* A zero macinfo type indicates the end of the macro
19473 case DW_MACRO_GNU_define:
19474 case DW_MACRO_GNU_undef:
19475 case DW_MACRO_GNU_define_indirect:
19476 case DW_MACRO_GNU_undef_indirect:
19477 case DW_MACRO_GNU_define_indirect_alt:
19478 case DW_MACRO_GNU_undef_indirect_alt:
19480 unsigned int bytes_read;
19485 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19486 mac_ptr += bytes_read;
19488 if (macinfo_type == DW_MACRO_GNU_define
19489 || macinfo_type == DW_MACRO_GNU_undef)
19491 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19492 mac_ptr += bytes_read;
19496 LONGEST str_offset;
19498 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19499 mac_ptr += offset_size;
19501 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
19502 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19505 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19507 body = read_indirect_string_from_dwz (dwz, str_offset);
19510 body = read_indirect_string_at_offset (abfd, str_offset);
19513 is_define = (macinfo_type == DW_MACRO_GNU_define
19514 || macinfo_type == DW_MACRO_GNU_define_indirect
19515 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
19516 if (! current_file)
19518 /* DWARF violation as no main source is present. */
19519 complaint (&symfile_complaints,
19520 _("debug info with no main source gives macro %s "
19522 is_define ? _("definition") : _("undefinition"),
19526 if ((line == 0 && !at_commandline)
19527 || (line != 0 && at_commandline))
19528 complaint (&symfile_complaints,
19529 _("debug info gives %s macro %s with %s line %d: %s"),
19530 at_commandline ? _("command-line") : _("in-file"),
19531 is_define ? _("definition") : _("undefinition"),
19532 line == 0 ? _("zero") : _("non-zero"), line, body);
19535 parse_macro_definition (current_file, line, body);
19538 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
19539 || macinfo_type == DW_MACRO_GNU_undef_indirect
19540 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
19541 macro_undef (current_file, line, body);
19546 case DW_MACRO_GNU_start_file:
19548 unsigned int bytes_read;
19551 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19552 mac_ptr += bytes_read;
19553 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19554 mac_ptr += bytes_read;
19556 if ((line == 0 && !at_commandline)
19557 || (line != 0 && at_commandline))
19558 complaint (&symfile_complaints,
19559 _("debug info gives source %d included "
19560 "from %s at %s line %d"),
19561 file, at_commandline ? _("command-line") : _("file"),
19562 line == 0 ? _("zero") : _("non-zero"), line);
19564 if (at_commandline)
19566 /* This DW_MACRO_GNU_start_file was executed in the
19568 at_commandline = 0;
19571 current_file = macro_start_file (file, line,
19572 current_file, comp_dir,
19577 case DW_MACRO_GNU_end_file:
19578 if (! current_file)
19579 complaint (&symfile_complaints,
19580 _("macro debug info has an unmatched "
19581 "`close_file' directive"));
19584 current_file = current_file->included_by;
19585 if (! current_file)
19587 enum dwarf_macro_record_type next_type;
19589 /* GCC circa March 2002 doesn't produce the zero
19590 type byte marking the end of the compilation
19591 unit. Complain if it's not there, but exit no
19594 /* Do we at least have room for a macinfo type byte? */
19595 if (mac_ptr >= mac_end)
19597 dwarf2_section_buffer_overflow_complaint (section);
19601 /* We don't increment mac_ptr here, so this is just
19603 next_type = read_1_byte (abfd, mac_ptr);
19604 if (next_type != 0)
19605 complaint (&symfile_complaints,
19606 _("no terminating 0-type entry for "
19607 "macros in `.debug_macinfo' section"));
19614 case DW_MACRO_GNU_transparent_include:
19615 case DW_MACRO_GNU_transparent_include_alt:
19619 bfd *include_bfd = abfd;
19620 struct dwarf2_section_info *include_section = section;
19621 struct dwarf2_section_info alt_section;
19622 const gdb_byte *include_mac_end = mac_end;
19623 int is_dwz = section_is_dwz;
19624 const gdb_byte *new_mac_ptr;
19626 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19627 mac_ptr += offset_size;
19629 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19631 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19633 dwarf2_read_section (dwarf2_per_objfile->objfile,
19636 include_bfd = dwz->macro.asection->owner;
19637 include_section = &dwz->macro;
19638 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19642 new_mac_ptr = include_section->buffer + offset;
19643 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19647 /* This has actually happened; see
19648 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19649 complaint (&symfile_complaints,
19650 _("recursive DW_MACRO_GNU_transparent_include in "
19651 ".debug_macro section"));
19655 *slot = (void *) new_mac_ptr;
19657 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
19658 include_mac_end, current_file,
19660 section, section_is_gnu, is_dwz,
19661 offset_size, objfile, include_hash);
19663 htab_remove_elt (include_hash, (void *) new_mac_ptr);
19668 case DW_MACINFO_vendor_ext:
19669 if (!section_is_gnu)
19671 unsigned int bytes_read;
19674 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19675 mac_ptr += bytes_read;
19676 read_direct_string (abfd, mac_ptr, &bytes_read);
19677 mac_ptr += bytes_read;
19679 /* We don't recognize any vendor extensions. */
19685 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19686 mac_ptr, mac_end, abfd, offset_size,
19688 if (mac_ptr == NULL)
19692 } while (macinfo_type != 0);
19696 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
19697 const char *comp_dir, int section_is_gnu)
19699 struct objfile *objfile = dwarf2_per_objfile->objfile;
19700 struct line_header *lh = cu->line_header;
19702 const gdb_byte *mac_ptr, *mac_end;
19703 struct macro_source_file *current_file = 0;
19704 enum dwarf_macro_record_type macinfo_type;
19705 unsigned int offset_size = cu->header.offset_size;
19706 const gdb_byte *opcode_definitions[256];
19707 struct cleanup *cleanup;
19708 htab_t include_hash;
19710 struct dwarf2_section_info *section;
19711 const char *section_name;
19713 if (cu->dwo_unit != NULL)
19715 if (section_is_gnu)
19717 section = &cu->dwo_unit->dwo_file->sections.macro;
19718 section_name = ".debug_macro.dwo";
19722 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19723 section_name = ".debug_macinfo.dwo";
19728 if (section_is_gnu)
19730 section = &dwarf2_per_objfile->macro;
19731 section_name = ".debug_macro";
19735 section = &dwarf2_per_objfile->macinfo;
19736 section_name = ".debug_macinfo";
19740 dwarf2_read_section (objfile, section);
19741 if (section->buffer == NULL)
19743 complaint (&symfile_complaints, _("missing %s section"), section_name);
19746 abfd = section->asection->owner;
19748 /* First pass: Find the name of the base filename.
19749 This filename is needed in order to process all macros whose definition
19750 (or undefinition) comes from the command line. These macros are defined
19751 before the first DW_MACINFO_start_file entry, and yet still need to be
19752 associated to the base file.
19754 To determine the base file name, we scan the macro definitions until we
19755 reach the first DW_MACINFO_start_file entry. We then initialize
19756 CURRENT_FILE accordingly so that any macro definition found before the
19757 first DW_MACINFO_start_file can still be associated to the base file. */
19759 mac_ptr = section->buffer + offset;
19760 mac_end = section->buffer + section->size;
19762 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19763 &offset_size, section_is_gnu);
19764 if (mac_ptr == NULL)
19766 /* We already issued a complaint. */
19772 /* Do we at least have room for a macinfo type byte? */
19773 if (mac_ptr >= mac_end)
19775 /* Complaint is printed during the second pass as GDB will probably
19776 stop the first pass earlier upon finding
19777 DW_MACINFO_start_file. */
19781 macinfo_type = read_1_byte (abfd, mac_ptr);
19784 /* Note that we rely on the fact that the corresponding GNU and
19785 DWARF constants are the same. */
19786 switch (macinfo_type)
19788 /* A zero macinfo type indicates the end of the macro
19793 case DW_MACRO_GNU_define:
19794 case DW_MACRO_GNU_undef:
19795 /* Only skip the data by MAC_PTR. */
19797 unsigned int bytes_read;
19799 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19800 mac_ptr += bytes_read;
19801 read_direct_string (abfd, mac_ptr, &bytes_read);
19802 mac_ptr += bytes_read;
19806 case DW_MACRO_GNU_start_file:
19808 unsigned int bytes_read;
19811 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19812 mac_ptr += bytes_read;
19813 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19814 mac_ptr += bytes_read;
19816 current_file = macro_start_file (file, line, current_file,
19817 comp_dir, lh, objfile);
19821 case DW_MACRO_GNU_end_file:
19822 /* No data to skip by MAC_PTR. */
19825 case DW_MACRO_GNU_define_indirect:
19826 case DW_MACRO_GNU_undef_indirect:
19827 case DW_MACRO_GNU_define_indirect_alt:
19828 case DW_MACRO_GNU_undef_indirect_alt:
19830 unsigned int bytes_read;
19832 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19833 mac_ptr += bytes_read;
19834 mac_ptr += offset_size;
19838 case DW_MACRO_GNU_transparent_include:
19839 case DW_MACRO_GNU_transparent_include_alt:
19840 /* Note that, according to the spec, a transparent include
19841 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19842 skip this opcode. */
19843 mac_ptr += offset_size;
19846 case DW_MACINFO_vendor_ext:
19847 /* Only skip the data by MAC_PTR. */
19848 if (!section_is_gnu)
19850 unsigned int bytes_read;
19852 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19853 mac_ptr += bytes_read;
19854 read_direct_string (abfd, mac_ptr, &bytes_read);
19855 mac_ptr += bytes_read;
19860 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19861 mac_ptr, mac_end, abfd, offset_size,
19863 if (mac_ptr == NULL)
19867 } while (macinfo_type != 0 && current_file == NULL);
19869 /* Second pass: Process all entries.
19871 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19872 command-line macro definitions/undefinitions. This flag is unset when we
19873 reach the first DW_MACINFO_start_file entry. */
19875 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19876 NULL, xcalloc, xfree);
19877 cleanup = make_cleanup_htab_delete (include_hash);
19878 mac_ptr = section->buffer + offset;
19879 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19880 *slot = (void *) mac_ptr;
19881 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19882 current_file, lh, comp_dir, section,
19884 offset_size, objfile, include_hash);
19885 do_cleanups (cleanup);
19888 /* Check if the attribute's form is a DW_FORM_block*
19889 if so return true else false. */
19892 attr_form_is_block (const struct attribute *attr)
19894 return (attr == NULL ? 0 :
19895 attr->form == DW_FORM_block1
19896 || attr->form == DW_FORM_block2
19897 || attr->form == DW_FORM_block4
19898 || attr->form == DW_FORM_block
19899 || attr->form == DW_FORM_exprloc);
19902 /* Return non-zero if ATTR's value is a section offset --- classes
19903 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19904 You may use DW_UNSND (attr) to retrieve such offsets.
19906 Section 7.5.4, "Attribute Encodings", explains that no attribute
19907 may have a value that belongs to more than one of these classes; it
19908 would be ambiguous if we did, because we use the same forms for all
19912 attr_form_is_section_offset (const struct attribute *attr)
19914 return (attr->form == DW_FORM_data4
19915 || attr->form == DW_FORM_data8
19916 || attr->form == DW_FORM_sec_offset);
19919 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19920 zero otherwise. When this function returns true, you can apply
19921 dwarf2_get_attr_constant_value to it.
19923 However, note that for some attributes you must check
19924 attr_form_is_section_offset before using this test. DW_FORM_data4
19925 and DW_FORM_data8 are members of both the constant class, and of
19926 the classes that contain offsets into other debug sections
19927 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19928 that, if an attribute's can be either a constant or one of the
19929 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19930 taken as section offsets, not constants. */
19933 attr_form_is_constant (const struct attribute *attr)
19935 switch (attr->form)
19937 case DW_FORM_sdata:
19938 case DW_FORM_udata:
19939 case DW_FORM_data1:
19940 case DW_FORM_data2:
19941 case DW_FORM_data4:
19942 case DW_FORM_data8:
19950 /* DW_ADDR is always stored already as sect_offset; despite for the forms
19951 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19954 attr_form_is_ref (const struct attribute *attr)
19956 switch (attr->form)
19958 case DW_FORM_ref_addr:
19963 case DW_FORM_ref_udata:
19964 case DW_FORM_GNU_ref_alt:
19971 /* Return the .debug_loc section to use for CU.
19972 For DWO files use .debug_loc.dwo. */
19974 static struct dwarf2_section_info *
19975 cu_debug_loc_section (struct dwarf2_cu *cu)
19978 return &cu->dwo_unit->dwo_file->sections.loc;
19979 return &dwarf2_per_objfile->loc;
19982 /* A helper function that fills in a dwarf2_loclist_baton. */
19985 fill_in_loclist_baton (struct dwarf2_cu *cu,
19986 struct dwarf2_loclist_baton *baton,
19987 const struct attribute *attr)
19989 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19991 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19993 baton->per_cu = cu->per_cu;
19994 gdb_assert (baton->per_cu);
19995 /* We don't know how long the location list is, but make sure we
19996 don't run off the edge of the section. */
19997 baton->size = section->size - DW_UNSND (attr);
19998 baton->data = section->buffer + DW_UNSND (attr);
19999 baton->base_address = cu->base_address;
20000 baton->from_dwo = cu->dwo_unit != NULL;
20004 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
20005 struct dwarf2_cu *cu, int is_block)
20007 struct objfile *objfile = dwarf2_per_objfile->objfile;
20008 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20010 if (attr_form_is_section_offset (attr)
20011 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
20012 the section. If so, fall through to the complaint in the
20014 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
20016 struct dwarf2_loclist_baton *baton;
20018 baton = obstack_alloc (&objfile->objfile_obstack,
20019 sizeof (struct dwarf2_loclist_baton));
20021 fill_in_loclist_baton (cu, baton, attr);
20023 if (cu->base_known == 0)
20024 complaint (&symfile_complaints,
20025 _("Location list used without "
20026 "specifying the CU base address."));
20028 SYMBOL_ACLASS_INDEX (sym) = (is_block
20029 ? dwarf2_loclist_block_index
20030 : dwarf2_loclist_index);
20031 SYMBOL_LOCATION_BATON (sym) = baton;
20035 struct dwarf2_locexpr_baton *baton;
20037 baton = obstack_alloc (&objfile->objfile_obstack,
20038 sizeof (struct dwarf2_locexpr_baton));
20039 baton->per_cu = cu->per_cu;
20040 gdb_assert (baton->per_cu);
20042 if (attr_form_is_block (attr))
20044 /* Note that we're just copying the block's data pointer
20045 here, not the actual data. We're still pointing into the
20046 info_buffer for SYM's objfile; right now we never release
20047 that buffer, but when we do clean up properly this may
20049 baton->size = DW_BLOCK (attr)->size;
20050 baton->data = DW_BLOCK (attr)->data;
20054 dwarf2_invalid_attrib_class_complaint ("location description",
20055 SYMBOL_NATURAL_NAME (sym));
20059 SYMBOL_ACLASS_INDEX (sym) = (is_block
20060 ? dwarf2_locexpr_block_index
20061 : dwarf2_locexpr_index);
20062 SYMBOL_LOCATION_BATON (sym) = baton;
20066 /* Return the OBJFILE associated with the compilation unit CU. If CU
20067 came from a separate debuginfo file, then the master objfile is
20071 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20073 struct objfile *objfile = per_cu->objfile;
20075 /* Return the master objfile, so that we can report and look up the
20076 correct file containing this variable. */
20077 if (objfile->separate_debug_objfile_backlink)
20078 objfile = objfile->separate_debug_objfile_backlink;
20083 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20084 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20085 CU_HEADERP first. */
20087 static const struct comp_unit_head *
20088 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20089 struct dwarf2_per_cu_data *per_cu)
20091 const gdb_byte *info_ptr;
20094 return &per_cu->cu->header;
20096 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
20098 memset (cu_headerp, 0, sizeof (*cu_headerp));
20099 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
20104 /* Return the address size given in the compilation unit header for CU. */
20107 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20109 struct comp_unit_head cu_header_local;
20110 const struct comp_unit_head *cu_headerp;
20112 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20114 return cu_headerp->addr_size;
20117 /* Return the offset size given in the compilation unit header for CU. */
20120 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20122 struct comp_unit_head cu_header_local;
20123 const struct comp_unit_head *cu_headerp;
20125 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20127 return cu_headerp->offset_size;
20130 /* See its dwarf2loc.h declaration. */
20133 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20135 struct comp_unit_head cu_header_local;
20136 const struct comp_unit_head *cu_headerp;
20138 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20140 if (cu_headerp->version == 2)
20141 return cu_headerp->addr_size;
20143 return cu_headerp->offset_size;
20146 /* Return the text offset of the CU. The returned offset comes from
20147 this CU's objfile. If this objfile came from a separate debuginfo
20148 file, then the offset may be different from the corresponding
20149 offset in the parent objfile. */
20152 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20154 struct objfile *objfile = per_cu->objfile;
20156 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20159 /* Locate the .debug_info compilation unit from CU's objfile which contains
20160 the DIE at OFFSET. Raises an error on failure. */
20162 static struct dwarf2_per_cu_data *
20163 dwarf2_find_containing_comp_unit (sect_offset offset,
20164 unsigned int offset_in_dwz,
20165 struct objfile *objfile)
20167 struct dwarf2_per_cu_data *this_cu;
20169 const sect_offset *cu_off;
20172 high = dwarf2_per_objfile->n_comp_units - 1;
20175 struct dwarf2_per_cu_data *mid_cu;
20176 int mid = low + (high - low) / 2;
20178 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20179 cu_off = &mid_cu->offset;
20180 if (mid_cu->is_dwz > offset_in_dwz
20181 || (mid_cu->is_dwz == offset_in_dwz
20182 && cu_off->sect_off >= offset.sect_off))
20187 gdb_assert (low == high);
20188 this_cu = dwarf2_per_objfile->all_comp_units[low];
20189 cu_off = &this_cu->offset;
20190 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20192 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20193 error (_("Dwarf Error: could not find partial DIE containing "
20194 "offset 0x%lx [in module %s]"),
20195 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20197 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20198 <= offset.sect_off);
20199 return dwarf2_per_objfile->all_comp_units[low-1];
20203 this_cu = dwarf2_per_objfile->all_comp_units[low];
20204 if (low == dwarf2_per_objfile->n_comp_units - 1
20205 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20206 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20207 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20212 /* Initialize dwarf2_cu CU, owned by PER_CU. */
20215 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20217 memset (cu, 0, sizeof (*cu));
20219 cu->per_cu = per_cu;
20220 cu->objfile = per_cu->objfile;
20221 obstack_init (&cu->comp_unit_obstack);
20224 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20227 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20228 enum language pretend_language)
20230 struct attribute *attr;
20232 /* Set the language we're debugging. */
20233 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20235 set_cu_language (DW_UNSND (attr), cu);
20238 cu->language = pretend_language;
20239 cu->language_defn = language_def (cu->language);
20242 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20244 cu->producer = DW_STRING (attr);
20247 /* Release one cached compilation unit, CU. We unlink it from the tree
20248 of compilation units, but we don't remove it from the read_in_chain;
20249 the caller is responsible for that.
20250 NOTE: DATA is a void * because this function is also used as a
20251 cleanup routine. */
20254 free_heap_comp_unit (void *data)
20256 struct dwarf2_cu *cu = data;
20258 gdb_assert (cu->per_cu != NULL);
20259 cu->per_cu->cu = NULL;
20262 obstack_free (&cu->comp_unit_obstack, NULL);
20267 /* This cleanup function is passed the address of a dwarf2_cu on the stack
20268 when we're finished with it. We can't free the pointer itself, but be
20269 sure to unlink it from the cache. Also release any associated storage. */
20272 free_stack_comp_unit (void *data)
20274 struct dwarf2_cu *cu = data;
20276 gdb_assert (cu->per_cu != NULL);
20277 cu->per_cu->cu = NULL;
20280 obstack_free (&cu->comp_unit_obstack, NULL);
20281 cu->partial_dies = NULL;
20284 /* Free all cached compilation units. */
20287 free_cached_comp_units (void *data)
20289 struct dwarf2_per_cu_data *per_cu, **last_chain;
20291 per_cu = dwarf2_per_objfile->read_in_chain;
20292 last_chain = &dwarf2_per_objfile->read_in_chain;
20293 while (per_cu != NULL)
20295 struct dwarf2_per_cu_data *next_cu;
20297 next_cu = per_cu->cu->read_in_chain;
20299 free_heap_comp_unit (per_cu->cu);
20300 *last_chain = next_cu;
20306 /* Increase the age counter on each cached compilation unit, and free
20307 any that are too old. */
20310 age_cached_comp_units (void)
20312 struct dwarf2_per_cu_data *per_cu, **last_chain;
20314 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20315 per_cu = dwarf2_per_objfile->read_in_chain;
20316 while (per_cu != NULL)
20318 per_cu->cu->last_used ++;
20319 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20320 dwarf2_mark (per_cu->cu);
20321 per_cu = per_cu->cu->read_in_chain;
20324 per_cu = dwarf2_per_objfile->read_in_chain;
20325 last_chain = &dwarf2_per_objfile->read_in_chain;
20326 while (per_cu != NULL)
20328 struct dwarf2_per_cu_data *next_cu;
20330 next_cu = per_cu->cu->read_in_chain;
20332 if (!per_cu->cu->mark)
20334 free_heap_comp_unit (per_cu->cu);
20335 *last_chain = next_cu;
20338 last_chain = &per_cu->cu->read_in_chain;
20344 /* Remove a single compilation unit from the cache. */
20347 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
20349 struct dwarf2_per_cu_data *per_cu, **last_chain;
20351 per_cu = dwarf2_per_objfile->read_in_chain;
20352 last_chain = &dwarf2_per_objfile->read_in_chain;
20353 while (per_cu != NULL)
20355 struct dwarf2_per_cu_data *next_cu;
20357 next_cu = per_cu->cu->read_in_chain;
20359 if (per_cu == target_per_cu)
20361 free_heap_comp_unit (per_cu->cu);
20363 *last_chain = next_cu;
20367 last_chain = &per_cu->cu->read_in_chain;
20373 /* Release all extra memory associated with OBJFILE. */
20376 dwarf2_free_objfile (struct objfile *objfile)
20378 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20380 if (dwarf2_per_objfile == NULL)
20383 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20384 free_cached_comp_units (NULL);
20386 if (dwarf2_per_objfile->quick_file_names_table)
20387 htab_delete (dwarf2_per_objfile->quick_file_names_table);
20389 /* Everything else should be on the objfile obstack. */
20392 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20393 We store these in a hash table separate from the DIEs, and preserve them
20394 when the DIEs are flushed out of cache.
20396 The CU "per_cu" pointer is needed because offset alone is not enough to
20397 uniquely identify the type. A file may have multiple .debug_types sections,
20398 or the type may come from a DWO file. Furthermore, while it's more logical
20399 to use per_cu->section+offset, with Fission the section with the data is in
20400 the DWO file but we don't know that section at the point we need it.
20401 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20402 because we can enter the lookup routine, get_die_type_at_offset, from
20403 outside this file, and thus won't necessarily have PER_CU->cu.
20404 Fortunately, PER_CU is stable for the life of the objfile. */
20406 struct dwarf2_per_cu_offset_and_type
20408 const struct dwarf2_per_cu_data *per_cu;
20409 sect_offset offset;
20413 /* Hash function for a dwarf2_per_cu_offset_and_type. */
20416 per_cu_offset_and_type_hash (const void *item)
20418 const struct dwarf2_per_cu_offset_and_type *ofs = item;
20420 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
20423 /* Equality function for a dwarf2_per_cu_offset_and_type. */
20426 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
20428 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20429 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
20431 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20432 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
20435 /* Set the type associated with DIE to TYPE. Save it in CU's hash
20436 table if necessary. For convenience, return TYPE.
20438 The DIEs reading must have careful ordering to:
20439 * Not cause infite loops trying to read in DIEs as a prerequisite for
20440 reading current DIE.
20441 * Not trying to dereference contents of still incompletely read in types
20442 while reading in other DIEs.
20443 * Enable referencing still incompletely read in types just by a pointer to
20444 the type without accessing its fields.
20446 Therefore caller should follow these rules:
20447 * Try to fetch any prerequisite types we may need to build this DIE type
20448 before building the type and calling set_die_type.
20449 * After building type call set_die_type for current DIE as soon as
20450 possible before fetching more types to complete the current type.
20451 * Make the type as complete as possible before fetching more types. */
20453 static struct type *
20454 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20456 struct dwarf2_per_cu_offset_and_type **slot, ofs;
20457 struct objfile *objfile = cu->objfile;
20459 /* For Ada types, make sure that the gnat-specific data is always
20460 initialized (if not already set). There are a few types where
20461 we should not be doing so, because the type-specific area is
20462 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20463 where the type-specific area is used to store the floatformat).
20464 But this is not a problem, because the gnat-specific information
20465 is actually not needed for these types. */
20466 if (need_gnat_info (cu)
20467 && TYPE_CODE (type) != TYPE_CODE_FUNC
20468 && TYPE_CODE (type) != TYPE_CODE_FLT
20469 && !HAVE_GNAT_AUX_INFO (type))
20470 INIT_GNAT_SPECIFIC (type);
20472 if (dwarf2_per_objfile->die_type_hash == NULL)
20474 dwarf2_per_objfile->die_type_hash =
20475 htab_create_alloc_ex (127,
20476 per_cu_offset_and_type_hash,
20477 per_cu_offset_and_type_eq,
20479 &objfile->objfile_obstack,
20480 hashtab_obstack_allocate,
20481 dummy_obstack_deallocate);
20484 ofs.per_cu = cu->per_cu;
20485 ofs.offset = die->offset;
20487 slot = (struct dwarf2_per_cu_offset_and_type **)
20488 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
20490 complaint (&symfile_complaints,
20491 _("A problem internal to GDB: DIE 0x%x has type already set"),
20492 die->offset.sect_off);
20493 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
20498 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20499 or return NULL if the die does not have a saved type. */
20501 static struct type *
20502 get_die_type_at_offset (sect_offset offset,
20503 struct dwarf2_per_cu_data *per_cu)
20505 struct dwarf2_per_cu_offset_and_type *slot, ofs;
20507 if (dwarf2_per_objfile->die_type_hash == NULL)
20510 ofs.per_cu = per_cu;
20511 ofs.offset = offset;
20512 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
20519 /* Look up the type for DIE in CU in die_type_hash,
20520 or return NULL if DIE does not have a saved type. */
20522 static struct type *
20523 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20525 return get_die_type_at_offset (die->offset, cu->per_cu);
20528 /* Add a dependence relationship from CU to REF_PER_CU. */
20531 dwarf2_add_dependence (struct dwarf2_cu *cu,
20532 struct dwarf2_per_cu_data *ref_per_cu)
20536 if (cu->dependencies == NULL)
20538 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20539 NULL, &cu->comp_unit_obstack,
20540 hashtab_obstack_allocate,
20541 dummy_obstack_deallocate);
20543 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20545 *slot = ref_per_cu;
20548 /* Subroutine of dwarf2_mark to pass to htab_traverse.
20549 Set the mark field in every compilation unit in the
20550 cache that we must keep because we are keeping CU. */
20553 dwarf2_mark_helper (void **slot, void *data)
20555 struct dwarf2_per_cu_data *per_cu;
20557 per_cu = (struct dwarf2_per_cu_data *) *slot;
20559 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20560 reading of the chain. As such dependencies remain valid it is not much
20561 useful to track and undo them during QUIT cleanups. */
20562 if (per_cu->cu == NULL)
20565 if (per_cu->cu->mark)
20567 per_cu->cu->mark = 1;
20569 if (per_cu->cu->dependencies != NULL)
20570 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20575 /* Set the mark field in CU and in every other compilation unit in the
20576 cache that we must keep because we are keeping CU. */
20579 dwarf2_mark (struct dwarf2_cu *cu)
20584 if (cu->dependencies != NULL)
20585 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
20589 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20593 per_cu->cu->mark = 0;
20594 per_cu = per_cu->cu->read_in_chain;
20598 /* Trivial hash function for partial_die_info: the hash value of a DIE
20599 is its offset in .debug_info for this objfile. */
20602 partial_die_hash (const void *item)
20604 const struct partial_die_info *part_die = item;
20606 return part_die->offset.sect_off;
20609 /* Trivial comparison function for partial_die_info structures: two DIEs
20610 are equal if they have the same offset. */
20613 partial_die_eq (const void *item_lhs, const void *item_rhs)
20615 const struct partial_die_info *part_die_lhs = item_lhs;
20616 const struct partial_die_info *part_die_rhs = item_rhs;
20618 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
20621 static struct cmd_list_element *set_dwarf2_cmdlist;
20622 static struct cmd_list_element *show_dwarf2_cmdlist;
20625 set_dwarf2_cmd (char *args, int from_tty)
20627 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20631 show_dwarf2_cmd (char *args, int from_tty)
20633 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20636 /* Free data associated with OBJFILE, if necessary. */
20639 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
20641 struct dwarf2_per_objfile *data = d;
20644 /* Make sure we don't accidentally use dwarf2_per_objfile while
20646 dwarf2_per_objfile = NULL;
20648 for (ix = 0; ix < data->n_comp_units; ++ix)
20649 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
20651 for (ix = 0; ix < data->n_type_units; ++ix)
20652 VEC_free (dwarf2_per_cu_ptr,
20653 data->all_type_units[ix]->per_cu.imported_symtabs);
20654 xfree (data->all_type_units);
20656 VEC_free (dwarf2_section_info_def, data->types);
20658 if (data->dwo_files)
20659 free_dwo_files (data->dwo_files, objfile);
20660 if (data->dwp_file)
20661 gdb_bfd_unref (data->dwp_file->dbfd);
20663 if (data->dwz_file && data->dwz_file->dwz_bfd)
20664 gdb_bfd_unref (data->dwz_file->dwz_bfd);
20668 /* The "save gdb-index" command. */
20670 /* The contents of the hash table we create when building the string
20672 struct strtab_entry
20674 offset_type offset;
20678 /* Hash function for a strtab_entry.
20680 Function is used only during write_hash_table so no index format backward
20681 compatibility is needed. */
20684 hash_strtab_entry (const void *e)
20686 const struct strtab_entry *entry = e;
20687 return mapped_index_string_hash (INT_MAX, entry->str);
20690 /* Equality function for a strtab_entry. */
20693 eq_strtab_entry (const void *a, const void *b)
20695 const struct strtab_entry *ea = a;
20696 const struct strtab_entry *eb = b;
20697 return !strcmp (ea->str, eb->str);
20700 /* Create a strtab_entry hash table. */
20703 create_strtab (void)
20705 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20706 xfree, xcalloc, xfree);
20709 /* Add a string to the constant pool. Return the string's offset in
20713 add_string (htab_t table, struct obstack *cpool, const char *str)
20716 struct strtab_entry entry;
20717 struct strtab_entry *result;
20720 slot = htab_find_slot (table, &entry, INSERT);
20725 result = XNEW (struct strtab_entry);
20726 result->offset = obstack_object_size (cpool);
20728 obstack_grow_str0 (cpool, str);
20731 return result->offset;
20734 /* An entry in the symbol table. */
20735 struct symtab_index_entry
20737 /* The name of the symbol. */
20739 /* The offset of the name in the constant pool. */
20740 offset_type index_offset;
20741 /* A sorted vector of the indices of all the CUs that hold an object
20743 VEC (offset_type) *cu_indices;
20746 /* The symbol table. This is a power-of-2-sized hash table. */
20747 struct mapped_symtab
20749 offset_type n_elements;
20751 struct symtab_index_entry **data;
20754 /* Hash function for a symtab_index_entry. */
20757 hash_symtab_entry (const void *e)
20759 const struct symtab_index_entry *entry = e;
20760 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20761 sizeof (offset_type) * VEC_length (offset_type,
20762 entry->cu_indices),
20766 /* Equality function for a symtab_index_entry. */
20769 eq_symtab_entry (const void *a, const void *b)
20771 const struct symtab_index_entry *ea = a;
20772 const struct symtab_index_entry *eb = b;
20773 int len = VEC_length (offset_type, ea->cu_indices);
20774 if (len != VEC_length (offset_type, eb->cu_indices))
20776 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20777 VEC_address (offset_type, eb->cu_indices),
20778 sizeof (offset_type) * len);
20781 /* Destroy a symtab_index_entry. */
20784 delete_symtab_entry (void *p)
20786 struct symtab_index_entry *entry = p;
20787 VEC_free (offset_type, entry->cu_indices);
20791 /* Create a hash table holding symtab_index_entry objects. */
20794 create_symbol_hash_table (void)
20796 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20797 delete_symtab_entry, xcalloc, xfree);
20800 /* Create a new mapped symtab object. */
20802 static struct mapped_symtab *
20803 create_mapped_symtab (void)
20805 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20806 symtab->n_elements = 0;
20807 symtab->size = 1024;
20808 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20812 /* Destroy a mapped_symtab. */
20815 cleanup_mapped_symtab (void *p)
20817 struct mapped_symtab *symtab = p;
20818 /* The contents of the array are freed when the other hash table is
20820 xfree (symtab->data);
20824 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20827 Function is used only during write_hash_table so no index format backward
20828 compatibility is needed. */
20830 static struct symtab_index_entry **
20831 find_slot (struct mapped_symtab *symtab, const char *name)
20833 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20835 index = hash & (symtab->size - 1);
20836 step = ((hash * 17) & (symtab->size - 1)) | 1;
20840 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20841 return &symtab->data[index];
20842 index = (index + step) & (symtab->size - 1);
20846 /* Expand SYMTAB's hash table. */
20849 hash_expand (struct mapped_symtab *symtab)
20851 offset_type old_size = symtab->size;
20853 struct symtab_index_entry **old_entries = symtab->data;
20856 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20858 for (i = 0; i < old_size; ++i)
20860 if (old_entries[i])
20862 struct symtab_index_entry **slot = find_slot (symtab,
20863 old_entries[i]->name);
20864 *slot = old_entries[i];
20868 xfree (old_entries);
20871 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20872 CU_INDEX is the index of the CU in which the symbol appears.
20873 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20876 add_index_entry (struct mapped_symtab *symtab, const char *name,
20877 int is_static, gdb_index_symbol_kind kind,
20878 offset_type cu_index)
20880 struct symtab_index_entry **slot;
20881 offset_type cu_index_and_attrs;
20883 ++symtab->n_elements;
20884 if (4 * symtab->n_elements / 3 >= symtab->size)
20885 hash_expand (symtab);
20887 slot = find_slot (symtab, name);
20890 *slot = XNEW (struct symtab_index_entry);
20891 (*slot)->name = name;
20892 /* index_offset is set later. */
20893 (*slot)->cu_indices = NULL;
20896 cu_index_and_attrs = 0;
20897 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20898 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20899 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20901 /* We don't want to record an index value twice as we want to avoid the
20903 We process all global symbols and then all static symbols
20904 (which would allow us to avoid the duplication by only having to check
20905 the last entry pushed), but a symbol could have multiple kinds in one CU.
20906 To keep things simple we don't worry about the duplication here and
20907 sort and uniqufy the list after we've processed all symbols. */
20908 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20911 /* qsort helper routine for uniquify_cu_indices. */
20914 offset_type_compare (const void *ap, const void *bp)
20916 offset_type a = *(offset_type *) ap;
20917 offset_type b = *(offset_type *) bp;
20919 return (a > b) - (b > a);
20922 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20925 uniquify_cu_indices (struct mapped_symtab *symtab)
20929 for (i = 0; i < symtab->size; ++i)
20931 struct symtab_index_entry *entry = symtab->data[i];
20934 && entry->cu_indices != NULL)
20936 unsigned int next_to_insert, next_to_check;
20937 offset_type last_value;
20939 qsort (VEC_address (offset_type, entry->cu_indices),
20940 VEC_length (offset_type, entry->cu_indices),
20941 sizeof (offset_type), offset_type_compare);
20943 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20944 next_to_insert = 1;
20945 for (next_to_check = 1;
20946 next_to_check < VEC_length (offset_type, entry->cu_indices);
20949 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20952 last_value = VEC_index (offset_type, entry->cu_indices,
20954 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20959 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20964 /* Add a vector of indices to the constant pool. */
20967 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20968 struct symtab_index_entry *entry)
20972 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20975 offset_type len = VEC_length (offset_type, entry->cu_indices);
20976 offset_type val = MAYBE_SWAP (len);
20981 entry->index_offset = obstack_object_size (cpool);
20983 obstack_grow (cpool, &val, sizeof (val));
20985 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20988 val = MAYBE_SWAP (iter);
20989 obstack_grow (cpool, &val, sizeof (val));
20994 struct symtab_index_entry *old_entry = *slot;
20995 entry->index_offset = old_entry->index_offset;
20998 return entry->index_offset;
21001 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
21002 constant pool entries going into the obstack CPOOL. */
21005 write_hash_table (struct mapped_symtab *symtab,
21006 struct obstack *output, struct obstack *cpool)
21009 htab_t symbol_hash_table;
21012 symbol_hash_table = create_symbol_hash_table ();
21013 str_table = create_strtab ();
21015 /* We add all the index vectors to the constant pool first, to
21016 ensure alignment is ok. */
21017 for (i = 0; i < symtab->size; ++i)
21019 if (symtab->data[i])
21020 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
21023 /* Now write out the hash table. */
21024 for (i = 0; i < symtab->size; ++i)
21026 offset_type str_off, vec_off;
21028 if (symtab->data[i])
21030 str_off = add_string (str_table, cpool, symtab->data[i]->name);
21031 vec_off = symtab->data[i]->index_offset;
21035 /* While 0 is a valid constant pool index, it is not valid
21036 to have 0 for both offsets. */
21041 str_off = MAYBE_SWAP (str_off);
21042 vec_off = MAYBE_SWAP (vec_off);
21044 obstack_grow (output, &str_off, sizeof (str_off));
21045 obstack_grow (output, &vec_off, sizeof (vec_off));
21048 htab_delete (str_table);
21049 htab_delete (symbol_hash_table);
21052 /* Struct to map psymtab to CU index in the index file. */
21053 struct psymtab_cu_index_map
21055 struct partial_symtab *psymtab;
21056 unsigned int cu_index;
21060 hash_psymtab_cu_index (const void *item)
21062 const struct psymtab_cu_index_map *map = item;
21064 return htab_hash_pointer (map->psymtab);
21068 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21070 const struct psymtab_cu_index_map *lhs = item_lhs;
21071 const struct psymtab_cu_index_map *rhs = item_rhs;
21073 return lhs->psymtab == rhs->psymtab;
21076 /* Helper struct for building the address table. */
21077 struct addrmap_index_data
21079 struct objfile *objfile;
21080 struct obstack *addr_obstack;
21081 htab_t cu_index_htab;
21083 /* Non-zero if the previous_* fields are valid.
21084 We can't write an entry until we see the next entry (since it is only then
21085 that we know the end of the entry). */
21086 int previous_valid;
21087 /* Index of the CU in the table of all CUs in the index file. */
21088 unsigned int previous_cu_index;
21089 /* Start address of the CU. */
21090 CORE_ADDR previous_cu_start;
21093 /* Write an address entry to OBSTACK. */
21096 add_address_entry (struct objfile *objfile, struct obstack *obstack,
21097 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
21099 offset_type cu_index_to_write;
21101 CORE_ADDR baseaddr;
21103 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21105 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21106 obstack_grow (obstack, addr, 8);
21107 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21108 obstack_grow (obstack, addr, 8);
21109 cu_index_to_write = MAYBE_SWAP (cu_index);
21110 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21113 /* Worker function for traversing an addrmap to build the address table. */
21116 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21118 struct addrmap_index_data *data = datap;
21119 struct partial_symtab *pst = obj;
21121 if (data->previous_valid)
21122 add_address_entry (data->objfile, data->addr_obstack,
21123 data->previous_cu_start, start_addr,
21124 data->previous_cu_index);
21126 data->previous_cu_start = start_addr;
21129 struct psymtab_cu_index_map find_map, *map;
21130 find_map.psymtab = pst;
21131 map = htab_find (data->cu_index_htab, &find_map);
21132 gdb_assert (map != NULL);
21133 data->previous_cu_index = map->cu_index;
21134 data->previous_valid = 1;
21137 data->previous_valid = 0;
21142 /* Write OBJFILE's address map to OBSTACK.
21143 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21144 in the index file. */
21147 write_address_map (struct objfile *objfile, struct obstack *obstack,
21148 htab_t cu_index_htab)
21150 struct addrmap_index_data addrmap_index_data;
21152 /* When writing the address table, we have to cope with the fact that
21153 the addrmap iterator only provides the start of a region; we have to
21154 wait until the next invocation to get the start of the next region. */
21156 addrmap_index_data.objfile = objfile;
21157 addrmap_index_data.addr_obstack = obstack;
21158 addrmap_index_data.cu_index_htab = cu_index_htab;
21159 addrmap_index_data.previous_valid = 0;
21161 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21162 &addrmap_index_data);
21164 /* It's highly unlikely the last entry (end address = 0xff...ff)
21165 is valid, but we should still handle it.
21166 The end address is recorded as the start of the next region, but that
21167 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21169 if (addrmap_index_data.previous_valid)
21170 add_address_entry (objfile, obstack,
21171 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21172 addrmap_index_data.previous_cu_index);
21175 /* Return the symbol kind of PSYM. */
21177 static gdb_index_symbol_kind
21178 symbol_kind (struct partial_symbol *psym)
21180 domain_enum domain = PSYMBOL_DOMAIN (psym);
21181 enum address_class aclass = PSYMBOL_CLASS (psym);
21189 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21191 return GDB_INDEX_SYMBOL_KIND_TYPE;
21193 case LOC_CONST_BYTES:
21194 case LOC_OPTIMIZED_OUT:
21196 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21198 /* Note: It's currently impossible to recognize psyms as enum values
21199 short of reading the type info. For now punt. */
21200 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21202 /* There are other LOC_FOO values that one might want to classify
21203 as variables, but dwarf2read.c doesn't currently use them. */
21204 return GDB_INDEX_SYMBOL_KIND_OTHER;
21206 case STRUCT_DOMAIN:
21207 return GDB_INDEX_SYMBOL_KIND_TYPE;
21209 return GDB_INDEX_SYMBOL_KIND_OTHER;
21213 /* Add a list of partial symbols to SYMTAB. */
21216 write_psymbols (struct mapped_symtab *symtab,
21218 struct partial_symbol **psymp,
21220 offset_type cu_index,
21223 for (; count-- > 0; ++psymp)
21225 struct partial_symbol *psym = *psymp;
21228 if (SYMBOL_LANGUAGE (psym) == language_ada)
21229 error (_("Ada is not currently supported by the index"));
21231 /* Only add a given psymbol once. */
21232 slot = htab_find_slot (psyms_seen, psym, INSERT);
21235 gdb_index_symbol_kind kind = symbol_kind (psym);
21238 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21239 is_static, kind, cu_index);
21244 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
21245 exception if there is an error. */
21248 write_obstack (FILE *file, struct obstack *obstack)
21250 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21252 != obstack_object_size (obstack))
21253 error (_("couldn't data write to file"));
21256 /* Unlink a file if the argument is not NULL. */
21259 unlink_if_set (void *p)
21261 char **filename = p;
21263 unlink (*filename);
21266 /* A helper struct used when iterating over debug_types. */
21267 struct signatured_type_index_data
21269 struct objfile *objfile;
21270 struct mapped_symtab *symtab;
21271 struct obstack *types_list;
21276 /* A helper function that writes a single signatured_type to an
21280 write_one_signatured_type (void **slot, void *d)
21282 struct signatured_type_index_data *info = d;
21283 struct signatured_type *entry = (struct signatured_type *) *slot;
21284 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
21287 write_psymbols (info->symtab,
21289 info->objfile->global_psymbols.list
21290 + psymtab->globals_offset,
21291 psymtab->n_global_syms, info->cu_index,
21293 write_psymbols (info->symtab,
21295 info->objfile->static_psymbols.list
21296 + psymtab->statics_offset,
21297 psymtab->n_static_syms, info->cu_index,
21300 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21301 entry->per_cu.offset.sect_off);
21302 obstack_grow (info->types_list, val, 8);
21303 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21304 entry->type_offset_in_tu.cu_off);
21305 obstack_grow (info->types_list, val, 8);
21306 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21307 obstack_grow (info->types_list, val, 8);
21314 /* Recurse into all "included" dependencies and write their symbols as
21315 if they appeared in this psymtab. */
21318 recursively_write_psymbols (struct objfile *objfile,
21319 struct partial_symtab *psymtab,
21320 struct mapped_symtab *symtab,
21322 offset_type cu_index)
21326 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21327 if (psymtab->dependencies[i]->user != NULL)
21328 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21329 symtab, psyms_seen, cu_index);
21331 write_psymbols (symtab,
21333 objfile->global_psymbols.list + psymtab->globals_offset,
21334 psymtab->n_global_syms, cu_index,
21336 write_psymbols (symtab,
21338 objfile->static_psymbols.list + psymtab->statics_offset,
21339 psymtab->n_static_syms, cu_index,
21343 /* Create an index file for OBJFILE in the directory DIR. */
21346 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21348 struct cleanup *cleanup;
21349 char *filename, *cleanup_filename;
21350 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21351 struct obstack cu_list, types_cu_list;
21354 struct mapped_symtab *symtab;
21355 offset_type val, size_of_contents, total_len;
21358 htab_t cu_index_htab;
21359 struct psymtab_cu_index_map *psymtab_cu_index_map;
21361 if (dwarf2_per_objfile->using_index)
21362 error (_("Cannot use an index to create the index"));
21364 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21365 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21367 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21370 if (stat (objfile_name (objfile), &st) < 0)
21371 perror_with_name (objfile_name (objfile));
21373 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
21374 INDEX_SUFFIX, (char *) NULL);
21375 cleanup = make_cleanup (xfree, filename);
21377 out_file = gdb_fopen_cloexec (filename, "wb");
21379 error (_("Can't open `%s' for writing"), filename);
21381 cleanup_filename = filename;
21382 make_cleanup (unlink_if_set, &cleanup_filename);
21384 symtab = create_mapped_symtab ();
21385 make_cleanup (cleanup_mapped_symtab, symtab);
21387 obstack_init (&addr_obstack);
21388 make_cleanup_obstack_free (&addr_obstack);
21390 obstack_init (&cu_list);
21391 make_cleanup_obstack_free (&cu_list);
21393 obstack_init (&types_cu_list);
21394 make_cleanup_obstack_free (&types_cu_list);
21396 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21397 NULL, xcalloc, xfree);
21398 make_cleanup_htab_delete (psyms_seen);
21400 /* While we're scanning CU's create a table that maps a psymtab pointer
21401 (which is what addrmap records) to its index (which is what is recorded
21402 in the index file). This will later be needed to write the address
21404 cu_index_htab = htab_create_alloc (100,
21405 hash_psymtab_cu_index,
21406 eq_psymtab_cu_index,
21407 NULL, xcalloc, xfree);
21408 make_cleanup_htab_delete (cu_index_htab);
21409 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21410 xmalloc (sizeof (struct psymtab_cu_index_map)
21411 * dwarf2_per_objfile->n_comp_units);
21412 make_cleanup (xfree, psymtab_cu_index_map);
21414 /* The CU list is already sorted, so we don't need to do additional
21415 work here. Also, the debug_types entries do not appear in
21416 all_comp_units, but only in their own hash table. */
21417 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21419 struct dwarf2_per_cu_data *per_cu
21420 = dwarf2_per_objfile->all_comp_units[i];
21421 struct partial_symtab *psymtab = per_cu->v.psymtab;
21423 struct psymtab_cu_index_map *map;
21426 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21427 It may be referenced from a local scope but in such case it does not
21428 need to be present in .gdb_index. */
21429 if (psymtab == NULL)
21432 if (psymtab->user == NULL)
21433 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
21435 map = &psymtab_cu_index_map[i];
21436 map->psymtab = psymtab;
21438 slot = htab_find_slot (cu_index_htab, map, INSERT);
21439 gdb_assert (slot != NULL);
21440 gdb_assert (*slot == NULL);
21443 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21444 per_cu->offset.sect_off);
21445 obstack_grow (&cu_list, val, 8);
21446 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
21447 obstack_grow (&cu_list, val, 8);
21450 /* Dump the address map. */
21451 write_address_map (objfile, &addr_obstack, cu_index_htab);
21453 /* Write out the .debug_type entries, if any. */
21454 if (dwarf2_per_objfile->signatured_types)
21456 struct signatured_type_index_data sig_data;
21458 sig_data.objfile = objfile;
21459 sig_data.symtab = symtab;
21460 sig_data.types_list = &types_cu_list;
21461 sig_data.psyms_seen = psyms_seen;
21462 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21463 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21464 write_one_signatured_type, &sig_data);
21467 /* Now that we've processed all symbols we can shrink their cu_indices
21469 uniquify_cu_indices (symtab);
21471 obstack_init (&constant_pool);
21472 make_cleanup_obstack_free (&constant_pool);
21473 obstack_init (&symtab_obstack);
21474 make_cleanup_obstack_free (&symtab_obstack);
21475 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21477 obstack_init (&contents);
21478 make_cleanup_obstack_free (&contents);
21479 size_of_contents = 6 * sizeof (offset_type);
21480 total_len = size_of_contents;
21482 /* The version number. */
21483 val = MAYBE_SWAP (8);
21484 obstack_grow (&contents, &val, sizeof (val));
21486 /* The offset of the CU list from the start of the file. */
21487 val = MAYBE_SWAP (total_len);
21488 obstack_grow (&contents, &val, sizeof (val));
21489 total_len += obstack_object_size (&cu_list);
21491 /* The offset of the types CU list from the start of the file. */
21492 val = MAYBE_SWAP (total_len);
21493 obstack_grow (&contents, &val, sizeof (val));
21494 total_len += obstack_object_size (&types_cu_list);
21496 /* The offset of the address table from the start of the file. */
21497 val = MAYBE_SWAP (total_len);
21498 obstack_grow (&contents, &val, sizeof (val));
21499 total_len += obstack_object_size (&addr_obstack);
21501 /* The offset of the symbol table from the start of the file. */
21502 val = MAYBE_SWAP (total_len);
21503 obstack_grow (&contents, &val, sizeof (val));
21504 total_len += obstack_object_size (&symtab_obstack);
21506 /* The offset of the constant pool from the start of the file. */
21507 val = MAYBE_SWAP (total_len);
21508 obstack_grow (&contents, &val, sizeof (val));
21509 total_len += obstack_object_size (&constant_pool);
21511 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21513 write_obstack (out_file, &contents);
21514 write_obstack (out_file, &cu_list);
21515 write_obstack (out_file, &types_cu_list);
21516 write_obstack (out_file, &addr_obstack);
21517 write_obstack (out_file, &symtab_obstack);
21518 write_obstack (out_file, &constant_pool);
21522 /* We want to keep the file, so we set cleanup_filename to NULL
21523 here. See unlink_if_set. */
21524 cleanup_filename = NULL;
21526 do_cleanups (cleanup);
21529 /* Implementation of the `save gdb-index' command.
21531 Note that the file format used by this command is documented in the
21532 GDB manual. Any changes here must be documented there. */
21535 save_gdb_index_command (char *arg, int from_tty)
21537 struct objfile *objfile;
21540 error (_("usage: save gdb-index DIRECTORY"));
21542 ALL_OBJFILES (objfile)
21546 /* If the objfile does not correspond to an actual file, skip it. */
21547 if (stat (objfile_name (objfile), &st) < 0)
21550 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21551 if (dwarf2_per_objfile)
21553 volatile struct gdb_exception except;
21555 TRY_CATCH (except, RETURN_MASK_ERROR)
21557 write_psymtabs_to_index (objfile, arg);
21559 if (except.reason < 0)
21560 exception_fprintf (gdb_stderr, except,
21561 _("Error while writing index for `%s': "),
21562 objfile_name (objfile));
21569 int dwarf2_always_disassemble;
21572 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21573 struct cmd_list_element *c, const char *value)
21575 fprintf_filtered (file,
21576 _("Whether to always disassemble "
21577 "DWARF expressions is %s.\n"),
21582 show_check_physname (struct ui_file *file, int from_tty,
21583 struct cmd_list_element *c, const char *value)
21585 fprintf_filtered (file,
21586 _("Whether to check \"physname\" is %s.\n"),
21590 void _initialize_dwarf2_read (void);
21593 _initialize_dwarf2_read (void)
21595 struct cmd_list_element *c;
21597 dwarf2_objfile_data_key
21598 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
21600 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21601 Set DWARF 2 specific variables.\n\
21602 Configure DWARF 2 variables such as the cache size"),
21603 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21604 0/*allow-unknown*/, &maintenance_set_cmdlist);
21606 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21607 Show DWARF 2 specific variables\n\
21608 Show DWARF 2 variables such as the cache size"),
21609 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21610 0/*allow-unknown*/, &maintenance_show_cmdlist);
21612 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21613 &dwarf2_max_cache_age, _("\
21614 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21615 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21616 A higher limit means that cached compilation units will be stored\n\
21617 in memory longer, and more total memory will be used. Zero disables\n\
21618 caching, which can slow down startup."),
21620 show_dwarf2_max_cache_age,
21621 &set_dwarf2_cmdlist,
21622 &show_dwarf2_cmdlist);
21624 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21625 &dwarf2_always_disassemble, _("\
21626 Set whether `info address' always disassembles DWARF expressions."), _("\
21627 Show whether `info address' always disassembles DWARF expressions."), _("\
21628 When enabled, DWARF expressions are always printed in an assembly-like\n\
21629 syntax. When disabled, expressions will be printed in a more\n\
21630 conversational style, when possible."),
21632 show_dwarf2_always_disassemble,
21633 &set_dwarf2_cmdlist,
21634 &show_dwarf2_cmdlist);
21636 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21637 Set debugging of the dwarf2 reader."), _("\
21638 Show debugging of the dwarf2 reader."), _("\
21639 When enabled, debugging messages are printed during dwarf2 reading\n\
21640 and symtab expansion."),
21643 &setdebuglist, &showdebuglist);
21645 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
21646 Set debugging of the dwarf2 DIE reader."), _("\
21647 Show debugging of the dwarf2 DIE reader."), _("\
21648 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21649 The value is the maximum depth to print."),
21652 &setdebuglist, &showdebuglist);
21654 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21655 Set cross-checking of \"physname\" code against demangler."), _("\
21656 Show cross-checking of \"physname\" code against demangler."), _("\
21657 When enabled, GDB's internal \"physname\" code is checked against\n\
21659 NULL, show_check_physname,
21660 &setdebuglist, &showdebuglist);
21662 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21663 no_class, &use_deprecated_index_sections, _("\
21664 Set whether to use deprecated gdb_index sections."), _("\
21665 Show whether to use deprecated gdb_index sections."), _("\
21666 When enabled, deprecated .gdb_index sections are used anyway.\n\
21667 Normally they are ignored either because of a missing feature or\n\
21668 performance issue.\n\
21669 Warning: This option must be enabled before gdb reads the file."),
21672 &setlist, &showlist);
21674 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
21676 Save a gdb-index file.\n\
21677 Usage: save gdb-index DIRECTORY"),
21679 set_cmd_completer (c, filename_completer);
21681 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21682 &dwarf2_locexpr_funcs);
21683 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21684 &dwarf2_loclist_funcs);
21686 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21687 &dwarf2_block_frame_base_locexpr_funcs);
21688 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21689 &dwarf2_block_frame_base_loclist_funcs);