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 /* Backchain to our per_cu entry if the tree has been built. */
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 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
617 /* Entry in the signatured_types hash table. */
619 struct signatured_type
621 /* The "per_cu" object of this type.
622 This struct is used iff per_cu.is_debug_types.
623 N.B.: This is the first member so that it's easy to convert pointers
625 struct dwarf2_per_cu_data per_cu;
627 /* The type's signature. */
630 /* Offset in the TU of the type's DIE, as read from the TU header.
631 If this TU is a DWO stub and the definition lives in a DWO file
632 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
633 cu_offset type_offset_in_tu;
635 /* Offset in the section of the type's DIE.
636 If the definition lives in a DWO file, this is the offset in the
637 .debug_types.dwo section.
638 The value is zero until the actual value is known.
639 Zero is otherwise not a valid section offset. */
640 sect_offset type_offset_in_section;
642 /* Type units are grouped by their DW_AT_stmt_list entry so that they
643 can share them. This points to the containing symtab. */
644 struct type_unit_group *type_unit_group;
647 The first time we encounter this type we fully read it in and install it
648 in the symbol tables. Subsequent times we only need the type. */
651 /* Containing DWO unit.
652 This field is valid iff per_cu.reading_dwo_directly. */
653 struct dwo_unit *dwo_unit;
656 typedef struct signatured_type *sig_type_ptr;
657 DEF_VEC_P (sig_type_ptr);
659 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
660 This includes type_unit_group and quick_file_names. */
662 struct stmt_list_hash
664 /* The DWO unit this table is from or NULL if there is none. */
665 struct dwo_unit *dwo_unit;
667 /* Offset in .debug_line or .debug_line.dwo. */
668 sect_offset line_offset;
671 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
672 an object of this type. */
674 struct type_unit_group
676 /* dwarf2read.c's main "handle" on a TU symtab.
677 To simplify things we create an artificial CU that "includes" all the
678 type units using this stmt_list so that the rest of the code still has
679 a "per_cu" handle on the symtab.
680 This PER_CU is recognized by having no section. */
681 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
682 struct dwarf2_per_cu_data per_cu;
684 /* The TUs that share this DW_AT_stmt_list entry.
685 This is added to while parsing type units to build partial symtabs,
686 and is deleted afterwards and not used again. */
687 VEC (sig_type_ptr) *tus;
689 /* The primary symtab.
690 Type units in a group needn't all be defined in the same source file,
691 so we create an essentially anonymous symtab as the primary symtab. */
692 struct symtab *primary_symtab;
694 /* The data used to construct the hash key. */
695 struct stmt_list_hash hash;
697 /* The number of symtabs from the line header.
698 The value here must match line_header.num_file_names. */
699 unsigned int num_symtabs;
701 /* The symbol tables for this TU (obtained from the files listed in
703 WARNING: The order of entries here must match the order of entries
704 in the line header. After the first TU using this type_unit_group, the
705 line header for the subsequent TUs is recreated from this. This is done
706 because we need to use the same symtabs for each TU using the same
707 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
708 there's no guarantee the line header doesn't have duplicate entries. */
709 struct symtab **symtabs;
712 /* These sections are what may appear in a DWO file. */
716 struct dwarf2_section_info abbrev;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str;
722 struct dwarf2_section_info str_offsets;
723 /* In the case of a virtual DWO file, these two are unused. */
724 struct dwarf2_section_info info;
725 VEC (dwarf2_section_info_def) *types;
728 /* CUs/TUs in DWP/DWO files. */
732 /* Backlink to the containing struct dwo_file. */
733 struct dwo_file *dwo_file;
735 /* The "id" that distinguishes this CU/TU.
736 .debug_info calls this "dwo_id", .debug_types calls this "signature".
737 Since signatures came first, we stick with it for consistency. */
740 /* The section this CU/TU lives in, in the DWO file. */
741 struct dwarf2_section_info *section;
743 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
747 /* For types, offset in the type's DIE of the type defined by this TU. */
748 cu_offset type_offset_in_tu;
751 /* Data for one DWO file.
752 This includes virtual DWO files that have been packaged into a
757 /* The DW_AT_GNU_dwo_name attribute.
758 For virtual DWO files the name is constructed from the section offsets
759 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
760 from related CU+TUs. */
761 const char *dwo_name;
763 /* The DW_AT_comp_dir attribute. */
764 const char *comp_dir;
766 /* The bfd, when the file is open. Otherwise this is NULL.
767 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
770 /* Section info for this file. */
771 struct dwo_sections sections;
773 /* The CU in the file.
774 We only support one because having more than one requires hacking the
775 dwo_name of each to match, which is highly unlikely to happen.
776 Doing this means all TUs can share comp_dir: We also assume that
777 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
780 /* Table of TUs in the file.
781 Each element is a struct dwo_unit. */
785 /* These sections are what may appear in a DWP file. */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
793 by section number. We don't need to record them here. */
796 /* These sections are what may appear in a virtual DWO file. */
798 struct virtual_dwo_sections
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info line;
802 struct dwarf2_section_info loc;
803 struct dwarf2_section_info macinfo;
804 struct dwarf2_section_info macro;
805 struct dwarf2_section_info str_offsets;
806 /* Each DWP hash table entry records one CU or one TU.
807 That is recorded here, and copied to dwo_unit.section. */
808 struct dwarf2_section_info info_or_types;
811 /* Contents of DWP hash tables. */
813 struct dwp_hash_table
815 uint32_t nr_units, nr_slots;
816 const gdb_byte *hash_table, *unit_table, *section_pool;
819 /* Data for one DWP file. */
823 /* Name of the file. */
829 /* Section info for this file. */
830 struct dwp_sections sections;
832 /* Table of CUs in the file. */
833 const struct dwp_hash_table *cus;
835 /* Table of TUs in the file. */
836 const struct dwp_hash_table *tus;
838 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
841 /* Table to map ELF section numbers to their sections. */
842 unsigned int num_sections;
843 asection **elf_sections;
846 /* This represents a '.dwz' file. */
850 /* A dwz file can only contain a few sections. */
851 struct dwarf2_section_info abbrev;
852 struct dwarf2_section_info info;
853 struct dwarf2_section_info str;
854 struct dwarf2_section_info line;
855 struct dwarf2_section_info macro;
856 struct dwarf2_section_info gdb_index;
862 /* Struct used to pass misc. parameters to read_die_and_children, et
863 al. which are used for both .debug_info and .debug_types dies.
864 All parameters here are unchanging for the life of the call. This
865 struct exists to abstract away the constant parameters of die reading. */
867 struct die_reader_specs
869 /* die_section->asection->owner. */
872 /* The CU of the DIE we are parsing. */
873 struct dwarf2_cu *cu;
875 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
876 struct dwo_file *dwo_file;
878 /* The section the die comes from.
879 This is either .debug_info or .debug_types, or the .dwo variants. */
880 struct dwarf2_section_info *die_section;
882 /* die_section->buffer. */
883 const gdb_byte *buffer;
885 /* The end of the buffer. */
886 const gdb_byte *buffer_end;
888 /* The value of the DW_AT_comp_dir attribute. */
889 const char *comp_dir;
892 /* Type of function passed to init_cutu_and_read_dies, et.al. */
893 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
894 const gdb_byte *info_ptr,
895 struct die_info *comp_unit_die,
899 /* The line number information for a compilation unit (found in the
900 .debug_line section) begins with a "statement program header",
901 which contains the following information. */
904 unsigned int total_length;
905 unsigned short version;
906 unsigned int header_length;
907 unsigned char minimum_instruction_length;
908 unsigned char maximum_ops_per_instruction;
909 unsigned char default_is_stmt;
911 unsigned char line_range;
912 unsigned char opcode_base;
914 /* standard_opcode_lengths[i] is the number of operands for the
915 standard opcode whose value is i. This means that
916 standard_opcode_lengths[0] is unused, and the last meaningful
917 element is standard_opcode_lengths[opcode_base - 1]. */
918 unsigned char *standard_opcode_lengths;
920 /* The include_directories table. NOTE! These strings are not
921 allocated with xmalloc; instead, they are pointers into
922 debug_line_buffer. If you try to free them, `free' will get
924 unsigned int num_include_dirs, include_dirs_size;
925 const char **include_dirs;
927 /* The file_names table. NOTE! These strings are not allocated
928 with xmalloc; instead, they are pointers into debug_line_buffer.
929 Don't try to free them directly. */
930 unsigned int num_file_names, file_names_size;
934 unsigned int dir_index;
935 unsigned int mod_time;
937 int included_p; /* Non-zero if referenced by the Line Number Program. */
938 struct symtab *symtab; /* The associated symbol table, if any. */
941 /* The start and end of the statement program following this
942 header. These point into dwarf2_per_objfile->line_buffer. */
943 const gdb_byte *statement_program_start, *statement_program_end;
946 /* When we construct a partial symbol table entry we only
947 need this much information. */
948 struct partial_die_info
950 /* Offset of this DIE. */
953 /* DWARF-2 tag for this DIE. */
954 ENUM_BITFIELD(dwarf_tag) tag : 16;
956 /* Assorted flags describing the data found in this DIE. */
957 unsigned int has_children : 1;
958 unsigned int is_external : 1;
959 unsigned int is_declaration : 1;
960 unsigned int has_type : 1;
961 unsigned int has_specification : 1;
962 unsigned int has_pc_info : 1;
963 unsigned int may_be_inlined : 1;
965 /* Flag set if the SCOPE field of this structure has been
967 unsigned int scope_set : 1;
969 /* Flag set if the DIE has a byte_size attribute. */
970 unsigned int has_byte_size : 1;
972 /* Flag set if any of the DIE's children are template arguments. */
973 unsigned int has_template_arguments : 1;
975 /* Flag set if fixup_partial_die has been called on this die. */
976 unsigned int fixup_called : 1;
978 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
979 unsigned int is_dwz : 1;
981 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
982 unsigned int spec_is_dwz : 1;
984 /* The name of this DIE. Normally the value of DW_AT_name, but
985 sometimes a default name for unnamed DIEs. */
988 /* The linkage name, if present. */
989 const char *linkage_name;
991 /* The scope to prepend to our children. This is generally
992 allocated on the comp_unit_obstack, so will disappear
993 when this compilation unit leaves the cache. */
996 /* Some data associated with the partial DIE. The tag determines
997 which field is live. */
1000 /* The location description associated with this DIE, if any. */
1001 struct dwarf_block *locdesc;
1002 /* The offset of an import, for DW_TAG_imported_unit. */
1006 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1010 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1011 DW_AT_sibling, if any. */
1012 /* NOTE: This member isn't strictly necessary, read_partial_die could
1013 return DW_AT_sibling values to its caller load_partial_dies. */
1014 const gdb_byte *sibling;
1016 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1017 DW_AT_specification (or DW_AT_abstract_origin or
1018 DW_AT_extension). */
1019 sect_offset spec_offset;
1021 /* Pointers to this DIE's parent, first child, and next sibling,
1023 struct partial_die_info *die_parent, *die_child, *die_sibling;
1026 /* This data structure holds the information of an abbrev. */
1029 unsigned int number; /* number identifying abbrev */
1030 enum dwarf_tag tag; /* dwarf tag */
1031 unsigned short has_children; /* boolean */
1032 unsigned short num_attrs; /* number of attributes */
1033 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1034 struct abbrev_info *next; /* next in chain */
1039 ENUM_BITFIELD(dwarf_attribute) name : 16;
1040 ENUM_BITFIELD(dwarf_form) form : 16;
1043 /* Size of abbrev_table.abbrev_hash_table. */
1044 #define ABBREV_HASH_SIZE 121
1046 /* Top level data structure to contain an abbreviation table. */
1050 /* Where the abbrev table came from.
1051 This is used as a sanity check when the table is used. */
1054 /* Storage for the abbrev table. */
1055 struct obstack abbrev_obstack;
1057 /* Hash table of abbrevs.
1058 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1059 It could be statically allocated, but the previous code didn't so we
1061 struct abbrev_info **abbrevs;
1064 /* Attributes have a name and a value. */
1067 ENUM_BITFIELD(dwarf_attribute) name : 16;
1068 ENUM_BITFIELD(dwarf_form) form : 15;
1070 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1071 field should be in u.str (existing only for DW_STRING) but it is kept
1072 here for better struct attribute alignment. */
1073 unsigned int string_is_canonical : 1;
1078 struct dwarf_block *blk;
1087 /* This data structure holds a complete die structure. */
1090 /* DWARF-2 tag for this DIE. */
1091 ENUM_BITFIELD(dwarf_tag) tag : 16;
1093 /* Number of attributes */
1094 unsigned char num_attrs;
1096 /* True if we're presently building the full type name for the
1097 type derived from this DIE. */
1098 unsigned char building_fullname : 1;
1101 unsigned int abbrev;
1103 /* Offset in .debug_info or .debug_types section. */
1106 /* The dies in a compilation unit form an n-ary tree. PARENT
1107 points to this die's parent; CHILD points to the first child of
1108 this node; and all the children of a given node are chained
1109 together via their SIBLING fields. */
1110 struct die_info *child; /* Its first child, if any. */
1111 struct die_info *sibling; /* Its next sibling, if any. */
1112 struct die_info *parent; /* Its parent, if any. */
1114 /* An array of attributes, with NUM_ATTRS elements. There may be
1115 zero, but it's not common and zero-sized arrays are not
1116 sufficiently portable C. */
1117 struct attribute attrs[1];
1120 /* Get at parts of an attribute structure. */
1122 #define DW_STRING(attr) ((attr)->u.str)
1123 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1124 #define DW_UNSND(attr) ((attr)->u.unsnd)
1125 #define DW_BLOCK(attr) ((attr)->u.blk)
1126 #define DW_SND(attr) ((attr)->u.snd)
1127 #define DW_ADDR(attr) ((attr)->u.addr)
1128 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1130 /* Blocks are a bunch of untyped bytes. */
1135 /* Valid only if SIZE is not zero. */
1136 const gdb_byte *data;
1139 #ifndef ATTR_ALLOC_CHUNK
1140 #define ATTR_ALLOC_CHUNK 4
1143 /* Allocate fields for structs, unions and enums in this size. */
1144 #ifndef DW_FIELD_ALLOC_CHUNK
1145 #define DW_FIELD_ALLOC_CHUNK 4
1148 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1149 but this would require a corresponding change in unpack_field_as_long
1151 static int bits_per_byte = 8;
1153 /* The routines that read and process dies for a C struct or C++ class
1154 pass lists of data member fields and lists of member function fields
1155 in an instance of a field_info structure, as defined below. */
1158 /* List of data member and baseclasses fields. */
1161 struct nextfield *next;
1166 *fields, *baseclasses;
1168 /* Number of fields (including baseclasses). */
1171 /* Number of baseclasses. */
1174 /* Set if the accesibility of one of the fields is not public. */
1175 int non_public_fields;
1177 /* Member function fields array, entries are allocated in the order they
1178 are encountered in the object file. */
1181 struct nextfnfield *next;
1182 struct fn_field fnfield;
1186 /* Member function fieldlist array, contains name of possibly overloaded
1187 member function, number of overloaded member functions and a pointer
1188 to the head of the member function field chain. */
1193 struct nextfnfield *head;
1197 /* Number of entries in the fnfieldlists array. */
1200 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1201 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1202 struct typedef_field_list
1204 struct typedef_field field;
1205 struct typedef_field_list *next;
1207 *typedef_field_list;
1208 unsigned typedef_field_list_count;
1211 /* One item on the queue of compilation units to read in full symbols
1213 struct dwarf2_queue_item
1215 struct dwarf2_per_cu_data *per_cu;
1216 enum language pretend_language;
1217 struct dwarf2_queue_item *next;
1220 /* The current queue. */
1221 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1223 /* Loaded secondary compilation units are kept in memory until they
1224 have not been referenced for the processing of this many
1225 compilation units. Set this to zero to disable caching. Cache
1226 sizes of up to at least twenty will improve startup time for
1227 typical inter-CU-reference binaries, at an obvious memory cost. */
1228 static int dwarf2_max_cache_age = 5;
1230 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1231 struct cmd_list_element *c, const char *value)
1233 fprintf_filtered (file, _("The upper bound on the age of cached "
1234 "dwarf2 compilation units is %s.\n"),
1239 /* Various complaints about symbol reading that don't abort the process. */
1242 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1244 complaint (&symfile_complaints,
1245 _("statement list doesn't fit in .debug_line section"));
1249 dwarf2_debug_line_missing_file_complaint (void)
1251 complaint (&symfile_complaints,
1252 _(".debug_line section has line data without a file"));
1256 dwarf2_debug_line_missing_end_sequence_complaint (void)
1258 complaint (&symfile_complaints,
1259 _(".debug_line section has line "
1260 "program sequence without an end"));
1264 dwarf2_complex_location_expr_complaint (void)
1266 complaint (&symfile_complaints, _("location expression too complex"));
1270 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1273 complaint (&symfile_complaints,
1274 _("const value length mismatch for '%s', got %d, expected %d"),
1279 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1281 complaint (&symfile_complaints,
1282 _("debug info runs off end of %s section"
1284 section->asection->name,
1285 bfd_get_filename (section->asection->owner));
1289 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1291 complaint (&symfile_complaints,
1292 _("macro debug info contains a "
1293 "malformed macro definition:\n`%s'"),
1298 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1300 complaint (&symfile_complaints,
1301 _("invalid attribute class or form for '%s' in '%s'"),
1305 /* local function prototypes */
1307 static void dwarf2_locate_sections (bfd *, asection *, void *);
1309 static void dwarf2_find_base_address (struct die_info *die,
1310 struct dwarf2_cu *cu);
1312 static struct partial_symtab *create_partial_symtab
1313 (struct dwarf2_per_cu_data *per_cu, const char *name);
1315 static void dwarf2_build_psymtabs_hard (struct objfile *);
1317 static void scan_partial_symbols (struct partial_die_info *,
1318 CORE_ADDR *, CORE_ADDR *,
1319 int, struct dwarf2_cu *);
1321 static void add_partial_symbol (struct partial_die_info *,
1322 struct dwarf2_cu *);
1324 static void add_partial_namespace (struct partial_die_info *pdi,
1325 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1326 int need_pc, struct dwarf2_cu *cu);
1328 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1329 CORE_ADDR *highpc, int need_pc,
1330 struct dwarf2_cu *cu);
1332 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1333 struct dwarf2_cu *cu);
1335 static void add_partial_subprogram (struct partial_die_info *pdi,
1336 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1337 int need_pc, struct dwarf2_cu *cu);
1339 static void dwarf2_read_symtab (struct partial_symtab *,
1342 static void psymtab_to_symtab_1 (struct partial_symtab *);
1344 static struct abbrev_info *abbrev_table_lookup_abbrev
1345 (const struct abbrev_table *, unsigned int);
1347 static struct abbrev_table *abbrev_table_read_table
1348 (struct dwarf2_section_info *, sect_offset);
1350 static void abbrev_table_free (struct abbrev_table *);
1352 static void abbrev_table_free_cleanup (void *);
1354 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1355 struct dwarf2_section_info *);
1357 static void dwarf2_free_abbrev_table (void *);
1359 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1361 static struct partial_die_info *load_partial_dies
1362 (const struct die_reader_specs *, const gdb_byte *, int);
1364 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1365 struct partial_die_info *,
1366 struct abbrev_info *,
1370 static struct partial_die_info *find_partial_die (sect_offset, int,
1371 struct dwarf2_cu *);
1373 static void fixup_partial_die (struct partial_die_info *,
1374 struct dwarf2_cu *);
1376 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1377 struct attribute *, struct attr_abbrev *,
1380 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1382 static int read_1_signed_byte (bfd *, const gdb_byte *);
1384 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1386 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1388 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1390 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1393 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1395 static LONGEST read_checked_initial_length_and_offset
1396 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1397 unsigned int *, unsigned int *);
1399 static LONGEST read_offset (bfd *, const gdb_byte *,
1400 const struct comp_unit_head *,
1403 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1405 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1408 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1410 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1412 static const char *read_indirect_string (bfd *, const gdb_byte *,
1413 const struct comp_unit_head *,
1416 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1418 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1420 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1422 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1426 static const char *read_str_index (const struct die_reader_specs *reader,
1427 struct dwarf2_cu *cu, ULONGEST str_index);
1429 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1431 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1432 struct dwarf2_cu *);
1434 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1437 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1438 struct dwarf2_cu *cu);
1440 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1442 static struct die_info *die_specification (struct die_info *die,
1443 struct dwarf2_cu **);
1445 static void free_line_header (struct line_header *lh);
1447 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1448 struct dwarf2_cu *cu);
1450 static void dwarf_decode_lines (struct line_header *, const char *,
1451 struct dwarf2_cu *, struct partial_symtab *,
1454 static void dwarf2_start_subfile (const char *, const char *, const char *);
1456 static void dwarf2_start_symtab (struct dwarf2_cu *,
1457 const char *, const char *, CORE_ADDR);
1459 static struct symbol *new_symbol (struct die_info *, struct type *,
1460 struct dwarf2_cu *);
1462 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1463 struct dwarf2_cu *, struct symbol *);
1465 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1466 struct dwarf2_cu *);
1468 static void dwarf2_const_value_attr (const struct attribute *attr,
1471 struct obstack *obstack,
1472 struct dwarf2_cu *cu, LONGEST *value,
1473 const gdb_byte **bytes,
1474 struct dwarf2_locexpr_baton **baton);
1476 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1478 static int need_gnat_info (struct dwarf2_cu *);
1480 static struct type *die_descriptive_type (struct die_info *,
1481 struct dwarf2_cu *);
1483 static void set_descriptive_type (struct type *, struct die_info *,
1484 struct dwarf2_cu *);
1486 static struct type *die_containing_type (struct die_info *,
1487 struct dwarf2_cu *);
1489 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1490 struct dwarf2_cu *);
1492 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1494 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1496 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1498 static char *typename_concat (struct obstack *obs, const char *prefix,
1499 const char *suffix, int physname,
1500 struct dwarf2_cu *cu);
1502 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1504 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1506 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1508 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1510 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1512 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1513 struct dwarf2_cu *, struct partial_symtab *);
1515 static int dwarf2_get_pc_bounds (struct die_info *,
1516 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1517 struct partial_symtab *);
1519 static void get_scope_pc_bounds (struct die_info *,
1520 CORE_ADDR *, CORE_ADDR *,
1521 struct dwarf2_cu *);
1523 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1524 CORE_ADDR, struct dwarf2_cu *);
1526 static void dwarf2_add_field (struct field_info *, struct die_info *,
1527 struct dwarf2_cu *);
1529 static void dwarf2_attach_fields_to_type (struct field_info *,
1530 struct type *, struct dwarf2_cu *);
1532 static void dwarf2_add_member_fn (struct field_info *,
1533 struct die_info *, struct type *,
1534 struct dwarf2_cu *);
1536 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1538 struct dwarf2_cu *);
1540 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1542 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1544 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1546 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1548 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1550 static struct type *read_module_type (struct die_info *die,
1551 struct dwarf2_cu *cu);
1553 static const char *namespace_name (struct die_info *die,
1554 int *is_anonymous, struct dwarf2_cu *);
1556 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1558 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1560 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1561 struct dwarf2_cu *);
1563 static struct die_info *read_die_and_siblings_1
1564 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1567 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1568 const gdb_byte *info_ptr,
1569 const gdb_byte **new_info_ptr,
1570 struct die_info *parent);
1572 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1573 struct die_info **, const gdb_byte *,
1576 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1577 struct die_info **, const gdb_byte *,
1580 static void process_die (struct die_info *, struct dwarf2_cu *);
1582 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1585 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1587 static const char *dwarf2_full_name (const char *name,
1588 struct die_info *die,
1589 struct dwarf2_cu *cu);
1591 static const char *dwarf2_physname (const char *name, struct die_info *die,
1592 struct dwarf2_cu *cu);
1594 static struct die_info *dwarf2_extension (struct die_info *die,
1595 struct dwarf2_cu **);
1597 static const char *dwarf_tag_name (unsigned int);
1599 static const char *dwarf_attr_name (unsigned int);
1601 static const char *dwarf_form_name (unsigned int);
1603 static char *dwarf_bool_name (unsigned int);
1605 static const char *dwarf_type_encoding_name (unsigned int);
1607 static struct die_info *sibling_die (struct die_info *);
1609 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1611 static void dump_die_for_error (struct die_info *);
1613 static void dump_die_1 (struct ui_file *, int level, int max_level,
1616 /*static*/ void dump_die (struct die_info *, int max_level);
1618 static void store_in_ref_table (struct die_info *,
1619 struct dwarf2_cu *);
1621 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1623 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1625 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1626 const struct attribute *,
1627 struct dwarf2_cu **);
1629 static struct die_info *follow_die_ref (struct die_info *,
1630 const struct attribute *,
1631 struct dwarf2_cu **);
1633 static struct die_info *follow_die_sig (struct die_info *,
1634 const struct attribute *,
1635 struct dwarf2_cu **);
1637 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1638 struct dwarf2_cu *);
1640 static struct type *get_DW_AT_signature_type (struct die_info *,
1641 const struct attribute *,
1642 struct dwarf2_cu *);
1644 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1646 static void read_signatured_type (struct signatured_type *);
1648 static struct type_unit_group *get_type_unit_group
1649 (struct dwarf2_cu *, const struct attribute *);
1651 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1653 /* memory allocation interface */
1655 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1657 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1659 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1662 static int attr_form_is_block (const struct attribute *);
1664 static int attr_form_is_section_offset (const struct attribute *);
1666 static int attr_form_is_constant (const struct attribute *);
1668 static int attr_form_is_ref (const struct attribute *);
1670 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1671 struct dwarf2_loclist_baton *baton,
1672 const struct attribute *attr);
1674 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1676 struct dwarf2_cu *cu,
1679 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1680 const gdb_byte *info_ptr,
1681 struct abbrev_info *abbrev);
1683 static void free_stack_comp_unit (void *);
1685 static hashval_t partial_die_hash (const void *item);
1687 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1689 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1690 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1692 static void init_one_comp_unit (struct dwarf2_cu *cu,
1693 struct dwarf2_per_cu_data *per_cu);
1695 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1696 struct die_info *comp_unit_die,
1697 enum language pretend_language);
1699 static void free_heap_comp_unit (void *);
1701 static void free_cached_comp_units (void *);
1703 static void age_cached_comp_units (void);
1705 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1707 static struct type *set_die_type (struct die_info *, struct type *,
1708 struct dwarf2_cu *);
1710 static void create_all_comp_units (struct objfile *);
1712 static int create_all_type_units (struct objfile *);
1714 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1717 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1720 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1723 static void dwarf2_add_dependence (struct dwarf2_cu *,
1724 struct dwarf2_per_cu_data *);
1726 static void dwarf2_mark (struct dwarf2_cu *);
1728 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1730 static struct type *get_die_type_at_offset (sect_offset,
1731 struct dwarf2_per_cu_data *);
1733 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1735 static void dwarf2_release_queue (void *dummy);
1737 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1738 enum language pretend_language);
1740 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1741 struct dwarf2_per_cu_data *per_cu,
1742 enum language pretend_language);
1744 static void process_queue (void);
1746 static void find_file_and_directory (struct die_info *die,
1747 struct dwarf2_cu *cu,
1748 const char **name, const char **comp_dir);
1750 static char *file_full_name (int file, struct line_header *lh,
1751 const char *comp_dir);
1753 static const gdb_byte *read_and_check_comp_unit_head
1754 (struct comp_unit_head *header,
1755 struct dwarf2_section_info *section,
1756 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1757 int is_debug_types_section);
1759 static void init_cutu_and_read_dies
1760 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1761 int use_existing_cu, int keep,
1762 die_reader_func_ftype *die_reader_func, void *data);
1764 static void init_cutu_and_read_dies_simple
1765 (struct dwarf2_per_cu_data *this_cu,
1766 die_reader_func_ftype *die_reader_func, void *data);
1768 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1770 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1772 static struct dwo_unit *lookup_dwo_in_dwp
1773 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1774 const char *comp_dir, ULONGEST signature, int is_debug_types);
1776 static struct dwp_file *get_dwp_file (void);
1778 static struct dwo_unit *lookup_dwo_comp_unit
1779 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1781 static struct dwo_unit *lookup_dwo_type_unit
1782 (struct signatured_type *, const char *, const char *);
1784 static void free_dwo_file_cleanup (void *);
1786 static void process_cu_includes (void);
1788 static void check_producer (struct dwarf2_cu *cu);
1792 /* Convert VALUE between big- and little-endian. */
1794 byte_swap (offset_type value)
1798 result = (value & 0xff) << 24;
1799 result |= (value & 0xff00) << 8;
1800 result |= (value & 0xff0000) >> 8;
1801 result |= (value & 0xff000000) >> 24;
1805 #define MAYBE_SWAP(V) byte_swap (V)
1808 #define MAYBE_SWAP(V) (V)
1809 #endif /* WORDS_BIGENDIAN */
1811 /* The suffix for an index file. */
1812 #define INDEX_SUFFIX ".gdb-index"
1814 /* Try to locate the sections we need for DWARF 2 debugging
1815 information and return true if we have enough to do something.
1816 NAMES points to the dwarf2 section names, or is NULL if the standard
1817 ELF names are used. */
1820 dwarf2_has_info (struct objfile *objfile,
1821 const struct dwarf2_debug_sections *names)
1823 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1824 if (!dwarf2_per_objfile)
1826 /* Initialize per-objfile state. */
1827 struct dwarf2_per_objfile *data
1828 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1830 memset (data, 0, sizeof (*data));
1831 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1832 dwarf2_per_objfile = data;
1834 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1836 dwarf2_per_objfile->objfile = objfile;
1838 return (dwarf2_per_objfile->info.asection != NULL
1839 && dwarf2_per_objfile->abbrev.asection != NULL);
1842 /* When loading sections, we look either for uncompressed section or for
1843 compressed section names. */
1846 section_is_p (const char *section_name,
1847 const struct dwarf2_section_names *names)
1849 if (names->normal != NULL
1850 && strcmp (section_name, names->normal) == 0)
1852 if (names->compressed != NULL
1853 && strcmp (section_name, names->compressed) == 0)
1858 /* This function is mapped across the sections and remembers the
1859 offset and size of each of the debugging sections we are interested
1863 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1865 const struct dwarf2_debug_sections *names;
1866 flagword aflag = bfd_get_section_flags (abfd, sectp);
1869 names = &dwarf2_elf_names;
1871 names = (const struct dwarf2_debug_sections *) vnames;
1873 if ((aflag & SEC_HAS_CONTENTS) == 0)
1876 else if (section_is_p (sectp->name, &names->info))
1878 dwarf2_per_objfile->info.asection = sectp;
1879 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1881 else if (section_is_p (sectp->name, &names->abbrev))
1883 dwarf2_per_objfile->abbrev.asection = sectp;
1884 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1886 else if (section_is_p (sectp->name, &names->line))
1888 dwarf2_per_objfile->line.asection = sectp;
1889 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1891 else if (section_is_p (sectp->name, &names->loc))
1893 dwarf2_per_objfile->loc.asection = sectp;
1894 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1896 else if (section_is_p (sectp->name, &names->macinfo))
1898 dwarf2_per_objfile->macinfo.asection = sectp;
1899 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1901 else if (section_is_p (sectp->name, &names->macro))
1903 dwarf2_per_objfile->macro.asection = sectp;
1904 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1906 else if (section_is_p (sectp->name, &names->str))
1908 dwarf2_per_objfile->str.asection = sectp;
1909 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1911 else if (section_is_p (sectp->name, &names->addr))
1913 dwarf2_per_objfile->addr.asection = sectp;
1914 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1916 else if (section_is_p (sectp->name, &names->frame))
1918 dwarf2_per_objfile->frame.asection = sectp;
1919 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1921 else if (section_is_p (sectp->name, &names->eh_frame))
1923 dwarf2_per_objfile->eh_frame.asection = sectp;
1924 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1926 else if (section_is_p (sectp->name, &names->ranges))
1928 dwarf2_per_objfile->ranges.asection = sectp;
1929 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1931 else if (section_is_p (sectp->name, &names->types))
1933 struct dwarf2_section_info type_section;
1935 memset (&type_section, 0, sizeof (type_section));
1936 type_section.asection = sectp;
1937 type_section.size = bfd_get_section_size (sectp);
1939 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1942 else if (section_is_p (sectp->name, &names->gdb_index))
1944 dwarf2_per_objfile->gdb_index.asection = sectp;
1945 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1948 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1949 && bfd_section_vma (abfd, sectp) == 0)
1950 dwarf2_per_objfile->has_section_at_zero = 1;
1953 /* A helper function that decides whether a section is empty,
1957 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1959 return info->asection == NULL || info->size == 0;
1962 /* Read the contents of the section INFO.
1963 OBJFILE is the main object file, but not necessarily the file where
1964 the section comes from. E.g., for DWO files INFO->asection->owner
1965 is the bfd of the DWO file.
1966 If the section is compressed, uncompress it before returning. */
1969 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1971 asection *sectp = info->asection;
1973 gdb_byte *buf, *retbuf;
1974 unsigned char header[4];
1978 info->buffer = NULL;
1981 if (dwarf2_section_empty_p (info))
1984 abfd = sectp->owner;
1986 /* If the section has relocations, we must read it ourselves.
1987 Otherwise we attach it to the BFD. */
1988 if ((sectp->flags & SEC_RELOC) == 0)
1990 info->buffer = gdb_bfd_map_section (sectp, &info->size);
1994 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1997 /* When debugging .o files, we may need to apply relocations; see
1998 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1999 We never compress sections in .o files, so we only need to
2000 try this when the section is not compressed. */
2001 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2004 info->buffer = retbuf;
2008 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2009 || bfd_bread (buf, info->size, abfd) != info->size)
2010 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2011 bfd_get_filename (abfd));
2014 /* A helper function that returns the size of a section in a safe way.
2015 If you are positive that the section has been read before using the
2016 size, then it is safe to refer to the dwarf2_section_info object's
2017 "size" field directly. In other cases, you must call this
2018 function, because for compressed sections the size field is not set
2019 correctly until the section has been read. */
2021 static bfd_size_type
2022 dwarf2_section_size (struct objfile *objfile,
2023 struct dwarf2_section_info *info)
2026 dwarf2_read_section (objfile, info);
2030 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2034 dwarf2_get_section_info (struct objfile *objfile,
2035 enum dwarf2_section_enum sect,
2036 asection **sectp, const gdb_byte **bufp,
2037 bfd_size_type *sizep)
2039 struct dwarf2_per_objfile *data
2040 = objfile_data (objfile, dwarf2_objfile_data_key);
2041 struct dwarf2_section_info *info;
2043 /* We may see an objfile without any DWARF, in which case we just
2054 case DWARF2_DEBUG_FRAME:
2055 info = &data->frame;
2057 case DWARF2_EH_FRAME:
2058 info = &data->eh_frame;
2061 gdb_assert_not_reached ("unexpected section");
2064 dwarf2_read_section (objfile, info);
2066 *sectp = info->asection;
2067 *bufp = info->buffer;
2068 *sizep = info->size;
2071 /* A helper function to find the sections for a .dwz file. */
2074 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2076 struct dwz_file *dwz_file = arg;
2078 /* Note that we only support the standard ELF names, because .dwz
2079 is ELF-only (at the time of writing). */
2080 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2082 dwz_file->abbrev.asection = sectp;
2083 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2087 dwz_file->info.asection = sectp;
2088 dwz_file->info.size = bfd_get_section_size (sectp);
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2092 dwz_file->str.asection = sectp;
2093 dwz_file->str.size = bfd_get_section_size (sectp);
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2097 dwz_file->line.asection = sectp;
2098 dwz_file->line.size = bfd_get_section_size (sectp);
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2102 dwz_file->macro.asection = sectp;
2103 dwz_file->macro.size = bfd_get_section_size (sectp);
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2107 dwz_file->gdb_index.asection = sectp;
2108 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2112 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2113 there is no .gnu_debugaltlink section in the file. Error if there
2114 is such a section but the file cannot be found. */
2116 static struct dwz_file *
2117 dwarf2_get_dwz_file (void)
2121 struct cleanup *cleanup;
2122 const char *filename;
2123 struct dwz_file *result;
2124 unsigned long buildid;
2126 if (dwarf2_per_objfile->dwz_file != NULL)
2127 return dwarf2_per_objfile->dwz_file;
2129 bfd_set_error (bfd_error_no_error);
2130 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2134 if (bfd_get_error () == bfd_error_no_error)
2136 error (_("could not read '.gnu_debugaltlink' section: %s"),
2137 bfd_errmsg (bfd_get_error ()));
2139 cleanup = make_cleanup (xfree, data);
2141 filename = (const char *) data;
2142 if (!IS_ABSOLUTE_PATH (filename))
2144 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2147 make_cleanup (xfree, abs);
2148 abs = ldirname (abs);
2149 make_cleanup (xfree, abs);
2151 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2152 make_cleanup (xfree, rel);
2156 /* The format is just a NUL-terminated file name, followed by the
2157 build-id. For now, though, we ignore the build-id. */
2158 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2159 if (dwz_bfd == NULL)
2160 error (_("could not read '%s': %s"), filename,
2161 bfd_errmsg (bfd_get_error ()));
2163 if (!bfd_check_format (dwz_bfd, bfd_object))
2165 gdb_bfd_unref (dwz_bfd);
2166 error (_("file '%s' was not usable: %s"), filename,
2167 bfd_errmsg (bfd_get_error ()));
2170 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2172 result->dwz_bfd = dwz_bfd;
2174 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2176 do_cleanups (cleanup);
2178 dwarf2_per_objfile->dwz_file = result;
2182 /* DWARF quick_symbols_functions support. */
2184 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2185 unique line tables, so we maintain a separate table of all .debug_line
2186 derived entries to support the sharing.
2187 All the quick functions need is the list of file names. We discard the
2188 line_header when we're done and don't need to record it here. */
2189 struct quick_file_names
2191 /* The data used to construct the hash key. */
2192 struct stmt_list_hash hash;
2194 /* The number of entries in file_names, real_names. */
2195 unsigned int num_file_names;
2197 /* The file names from the line table, after being run through
2199 const char **file_names;
2201 /* The file names from the line table after being run through
2202 gdb_realpath. These are computed lazily. */
2203 const char **real_names;
2206 /* When using the index (and thus not using psymtabs), each CU has an
2207 object of this type. This is used to hold information needed by
2208 the various "quick" methods. */
2209 struct dwarf2_per_cu_quick_data
2211 /* The file table. This can be NULL if there was no file table
2212 or it's currently not read in.
2213 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2214 struct quick_file_names *file_names;
2216 /* The corresponding symbol table. This is NULL if symbols for this
2217 CU have not yet been read. */
2218 struct symtab *symtab;
2220 /* A temporary mark bit used when iterating over all CUs in
2221 expand_symtabs_matching. */
2222 unsigned int mark : 1;
2224 /* True if we've tried to read the file table and found there isn't one.
2225 There will be no point in trying to read it again next time. */
2226 unsigned int no_file_data : 1;
2229 /* Utility hash function for a stmt_list_hash. */
2232 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2236 if (stmt_list_hash->dwo_unit != NULL)
2237 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2238 v += stmt_list_hash->line_offset.sect_off;
2242 /* Utility equality function for a stmt_list_hash. */
2245 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2246 const struct stmt_list_hash *rhs)
2248 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2250 if (lhs->dwo_unit != NULL
2251 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2254 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2257 /* Hash function for a quick_file_names. */
2260 hash_file_name_entry (const void *e)
2262 const struct quick_file_names *file_data = e;
2264 return hash_stmt_list_entry (&file_data->hash);
2267 /* Equality function for a quick_file_names. */
2270 eq_file_name_entry (const void *a, const void *b)
2272 const struct quick_file_names *ea = a;
2273 const struct quick_file_names *eb = b;
2275 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2278 /* Delete function for a quick_file_names. */
2281 delete_file_name_entry (void *e)
2283 struct quick_file_names *file_data = e;
2286 for (i = 0; i < file_data->num_file_names; ++i)
2288 xfree ((void*) file_data->file_names[i]);
2289 if (file_data->real_names)
2290 xfree ((void*) file_data->real_names[i]);
2293 /* The space for the struct itself lives on objfile_obstack,
2294 so we don't free it here. */
2297 /* Create a quick_file_names hash table. */
2300 create_quick_file_names_table (unsigned int nr_initial_entries)
2302 return htab_create_alloc (nr_initial_entries,
2303 hash_file_name_entry, eq_file_name_entry,
2304 delete_file_name_entry, xcalloc, xfree);
2307 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2308 have to be created afterwards. You should call age_cached_comp_units after
2309 processing PER_CU->CU. dw2_setup must have been already called. */
2312 load_cu (struct dwarf2_per_cu_data *per_cu)
2314 if (per_cu->is_debug_types)
2315 load_full_type_unit (per_cu);
2317 load_full_comp_unit (per_cu, language_minimal);
2319 gdb_assert (per_cu->cu != NULL);
2321 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2324 /* Read in the symbols for PER_CU. */
2327 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2329 struct cleanup *back_to;
2331 /* Skip type_unit_groups, reading the type units they contain
2332 is handled elsewhere. */
2333 if (IS_TYPE_UNIT_GROUP (per_cu))
2336 back_to = make_cleanup (dwarf2_release_queue, NULL);
2338 if (dwarf2_per_objfile->using_index
2339 ? per_cu->v.quick->symtab == NULL
2340 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2342 queue_comp_unit (per_cu, language_minimal);
2348 /* Age the cache, releasing compilation units that have not
2349 been used recently. */
2350 age_cached_comp_units ();
2352 do_cleanups (back_to);
2355 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2359 static struct symtab *
2360 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2362 gdb_assert (dwarf2_per_objfile->using_index);
2363 if (!per_cu->v.quick->symtab)
2365 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2366 increment_reading_symtab ();
2367 dw2_do_instantiate_symtab (per_cu);
2368 process_cu_includes ();
2369 do_cleanups (back_to);
2371 return per_cu->v.quick->symtab;
2374 /* Return the CU given its index.
2376 This is intended for loops like:
2378 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2379 + dwarf2_per_objfile->n_type_units); ++i)
2381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2387 static struct dwarf2_per_cu_data *
2388 dw2_get_cu (int index)
2390 if (index >= dwarf2_per_objfile->n_comp_units)
2392 index -= dwarf2_per_objfile->n_comp_units;
2393 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2394 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2397 return dwarf2_per_objfile->all_comp_units[index];
2400 /* Return the primary CU given its index.
2401 The difference between this function and dw2_get_cu is in the handling
2402 of type units (TUs). Here we return the type_unit_group object.
2404 This is intended for loops like:
2406 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2407 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2409 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2415 static struct dwarf2_per_cu_data *
2416 dw2_get_primary_cu (int index)
2418 if (index >= dwarf2_per_objfile->n_comp_units)
2420 index -= dwarf2_per_objfile->n_comp_units;
2421 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2422 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2425 return dwarf2_per_objfile->all_comp_units[index];
2428 /* A helper for create_cus_from_index that handles a given list of
2432 create_cus_from_index_list (struct objfile *objfile,
2433 const gdb_byte *cu_list, offset_type n_elements,
2434 struct dwarf2_section_info *section,
2440 for (i = 0; i < n_elements; i += 2)
2442 struct dwarf2_per_cu_data *the_cu;
2443 ULONGEST offset, length;
2445 gdb_static_assert (sizeof (ULONGEST) >= 8);
2446 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2447 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2450 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2451 struct dwarf2_per_cu_data);
2452 the_cu->offset.sect_off = offset;
2453 the_cu->length = length;
2454 the_cu->objfile = objfile;
2455 the_cu->section = section;
2456 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2457 struct dwarf2_per_cu_quick_data);
2458 the_cu->is_dwz = is_dwz;
2459 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2463 /* Read the CU list from the mapped index, and use it to create all
2464 the CU objects for this objfile. */
2467 create_cus_from_index (struct objfile *objfile,
2468 const gdb_byte *cu_list, offset_type cu_list_elements,
2469 const gdb_byte *dwz_list, offset_type dwz_elements)
2471 struct dwz_file *dwz;
2473 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2474 dwarf2_per_objfile->all_comp_units
2475 = obstack_alloc (&objfile->objfile_obstack,
2476 dwarf2_per_objfile->n_comp_units
2477 * sizeof (struct dwarf2_per_cu_data *));
2479 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2480 &dwarf2_per_objfile->info, 0, 0);
2482 if (dwz_elements == 0)
2485 dwz = dwarf2_get_dwz_file ();
2486 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2487 cu_list_elements / 2);
2490 /* Create the signatured type hash table from the index. */
2493 create_signatured_type_table_from_index (struct objfile *objfile,
2494 struct dwarf2_section_info *section,
2495 const gdb_byte *bytes,
2496 offset_type elements)
2499 htab_t sig_types_hash;
2501 dwarf2_per_objfile->n_type_units = elements / 3;
2502 dwarf2_per_objfile->all_type_units
2503 = xmalloc (dwarf2_per_objfile->n_type_units
2504 * sizeof (struct signatured_type *));
2506 sig_types_hash = allocate_signatured_type_table (objfile);
2508 for (i = 0; i < elements; i += 3)
2510 struct signatured_type *sig_type;
2511 ULONGEST offset, type_offset_in_tu, signature;
2514 gdb_static_assert (sizeof (ULONGEST) >= 8);
2515 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2516 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2518 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2521 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2522 struct signatured_type);
2523 sig_type->signature = signature;
2524 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2525 sig_type->per_cu.is_debug_types = 1;
2526 sig_type->per_cu.section = section;
2527 sig_type->per_cu.offset.sect_off = offset;
2528 sig_type->per_cu.objfile = objfile;
2529 sig_type->per_cu.v.quick
2530 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2531 struct dwarf2_per_cu_quick_data);
2533 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2536 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2539 dwarf2_per_objfile->signatured_types = sig_types_hash;
2542 /* Read the address map data from the mapped index, and use it to
2543 populate the objfile's psymtabs_addrmap. */
2546 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2548 const gdb_byte *iter, *end;
2549 struct obstack temp_obstack;
2550 struct addrmap *mutable_map;
2551 struct cleanup *cleanup;
2554 obstack_init (&temp_obstack);
2555 cleanup = make_cleanup_obstack_free (&temp_obstack);
2556 mutable_map = addrmap_create_mutable (&temp_obstack);
2558 iter = index->address_table;
2559 end = iter + index->address_table_size;
2561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2565 ULONGEST hi, lo, cu_index;
2566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2575 complaint (&symfile_complaints,
2576 _(".gdb_index address table has invalid range (%s - %s)"),
2577 hex_string (lo), hex_string (hi));
2581 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2583 complaint (&symfile_complaints,
2584 _(".gdb_index address table has invalid CU number %u"),
2585 (unsigned) cu_index);
2589 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2590 dw2_get_cu (cu_index));
2593 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2594 &objfile->objfile_obstack);
2595 do_cleanups (cleanup);
2598 /* The hash function for strings in the mapped index. This is the same as
2599 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2600 implementation. This is necessary because the hash function is tied to the
2601 format of the mapped index file. The hash values do not have to match with
2604 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2607 mapped_index_string_hash (int index_version, const void *p)
2609 const unsigned char *str = (const unsigned char *) p;
2613 while ((c = *str++) != 0)
2615 if (index_version >= 5)
2617 r = r * 67 + c - 113;
2623 /* Find a slot in the mapped index INDEX for the object named NAME.
2624 If NAME is found, set *VEC_OUT to point to the CU vector in the
2625 constant pool and return 1. If NAME cannot be found, return 0. */
2628 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2629 offset_type **vec_out)
2631 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2633 offset_type slot, step;
2634 int (*cmp) (const char *, const char *);
2636 if (current_language->la_language == language_cplus
2637 || current_language->la_language == language_java
2638 || current_language->la_language == language_fortran)
2640 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2642 const char *paren = strchr (name, '(');
2648 dup = xmalloc (paren - name + 1);
2649 memcpy (dup, name, paren - name);
2650 dup[paren - name] = 0;
2652 make_cleanup (xfree, dup);
2657 /* Index version 4 did not support case insensitive searches. But the
2658 indices for case insensitive languages are built in lowercase, therefore
2659 simulate our NAME being searched is also lowercased. */
2660 hash = mapped_index_string_hash ((index->version == 4
2661 && case_sensitivity == case_sensitive_off
2662 ? 5 : index->version),
2665 slot = hash & (index->symbol_table_slots - 1);
2666 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2667 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2671 /* Convert a slot number to an offset into the table. */
2672 offset_type i = 2 * slot;
2674 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2676 do_cleanups (back_to);
2680 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2681 if (!cmp (name, str))
2683 *vec_out = (offset_type *) (index->constant_pool
2684 + MAYBE_SWAP (index->symbol_table[i + 1]));
2685 do_cleanups (back_to);
2689 slot = (slot + step) & (index->symbol_table_slots - 1);
2693 /* A helper function that reads the .gdb_index from SECTION and fills
2694 in MAP. FILENAME is the name of the file containing the section;
2695 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2696 ok to use deprecated sections.
2698 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2699 out parameters that are filled in with information about the CU and
2700 TU lists in the section.
2702 Returns 1 if all went well, 0 otherwise. */
2705 read_index_from_section (struct objfile *objfile,
2706 const char *filename,
2708 struct dwarf2_section_info *section,
2709 struct mapped_index *map,
2710 const gdb_byte **cu_list,
2711 offset_type *cu_list_elements,
2712 const gdb_byte **types_list,
2713 offset_type *types_list_elements)
2715 const gdb_byte *addr;
2716 offset_type version;
2717 offset_type *metadata;
2720 if (dwarf2_section_empty_p (section))
2723 /* Older elfutils strip versions could keep the section in the main
2724 executable while splitting it for the separate debug info file. */
2725 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2728 dwarf2_read_section (objfile, section);
2730 addr = section->buffer;
2731 /* Version check. */
2732 version = MAYBE_SWAP (*(offset_type *) addr);
2733 /* Versions earlier than 3 emitted every copy of a psymbol. This
2734 causes the index to behave very poorly for certain requests. Version 3
2735 contained incomplete addrmap. So, it seems better to just ignore such
2739 static int warning_printed = 0;
2740 if (!warning_printed)
2742 warning (_("Skipping obsolete .gdb_index section in %s."),
2744 warning_printed = 1;
2748 /* Index version 4 uses a different hash function than index version
2751 Versions earlier than 6 did not emit psymbols for inlined
2752 functions. Using these files will cause GDB not to be able to
2753 set breakpoints on inlined functions by name, so we ignore these
2754 indices unless the user has done
2755 "set use-deprecated-index-sections on". */
2756 if (version < 6 && !deprecated_ok)
2758 static int warning_printed = 0;
2759 if (!warning_printed)
2762 Skipping deprecated .gdb_index section in %s.\n\
2763 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2764 to use the section anyway."),
2766 warning_printed = 1;
2770 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2771 of the TU (for symbols coming from TUs). It's just a performance bug, and
2772 we can't distinguish gdb-generated indices from gold-generated ones, so
2773 nothing to do here. */
2775 /* Indexes with higher version than the one supported by GDB may be no
2776 longer backward compatible. */
2780 map->version = version;
2781 map->total_size = section->size;
2783 metadata = (offset_type *) (addr + sizeof (offset_type));
2786 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2787 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2791 *types_list = addr + MAYBE_SWAP (metadata[i]);
2792 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2793 - MAYBE_SWAP (metadata[i]))
2797 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2798 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2799 - MAYBE_SWAP (metadata[i]));
2802 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2803 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2804 - MAYBE_SWAP (metadata[i]))
2805 / (2 * sizeof (offset_type)));
2808 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2814 /* Read the index file. If everything went ok, initialize the "quick"
2815 elements of all the CUs and return 1. Otherwise, return 0. */
2818 dwarf2_read_index (struct objfile *objfile)
2820 struct mapped_index local_map, *map;
2821 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2822 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2823 struct dwz_file *dwz;
2825 if (!read_index_from_section (objfile, objfile->name,
2826 use_deprecated_index_sections,
2827 &dwarf2_per_objfile->gdb_index, &local_map,
2828 &cu_list, &cu_list_elements,
2829 &types_list, &types_list_elements))
2832 /* Don't use the index if it's empty. */
2833 if (local_map.symbol_table_slots == 0)
2836 /* If there is a .dwz file, read it so we can get its CU list as
2838 dwz = dwarf2_get_dwz_file ();
2841 struct mapped_index dwz_map;
2842 const gdb_byte *dwz_types_ignore;
2843 offset_type dwz_types_elements_ignore;
2845 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2847 &dwz->gdb_index, &dwz_map,
2848 &dwz_list, &dwz_list_elements,
2850 &dwz_types_elements_ignore))
2852 warning (_("could not read '.gdb_index' section from %s; skipping"),
2853 bfd_get_filename (dwz->dwz_bfd));
2858 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2861 if (types_list_elements)
2863 struct dwarf2_section_info *section;
2865 /* We can only handle a single .debug_types when we have an
2867 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2870 section = VEC_index (dwarf2_section_info_def,
2871 dwarf2_per_objfile->types, 0);
2873 create_signatured_type_table_from_index (objfile, section, types_list,
2874 types_list_elements);
2877 create_addrmap_from_index (objfile, &local_map);
2879 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2882 dwarf2_per_objfile->index_table = map;
2883 dwarf2_per_objfile->using_index = 1;
2884 dwarf2_per_objfile->quick_file_names_table =
2885 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2890 /* A helper for the "quick" functions which sets the global
2891 dwarf2_per_objfile according to OBJFILE. */
2894 dw2_setup (struct objfile *objfile)
2896 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2897 gdb_assert (dwarf2_per_objfile);
2900 /* die_reader_func for dw2_get_file_names. */
2903 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2904 const gdb_byte *info_ptr,
2905 struct die_info *comp_unit_die,
2909 struct dwarf2_cu *cu = reader->cu;
2910 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2911 struct objfile *objfile = dwarf2_per_objfile->objfile;
2912 struct dwarf2_per_cu_data *lh_cu;
2913 struct line_header *lh;
2914 struct attribute *attr;
2916 const char *name, *comp_dir;
2918 struct quick_file_names *qfn;
2919 unsigned int line_offset;
2921 gdb_assert (! this_cu->is_debug_types);
2923 /* Our callers never want to match partial units -- instead they
2924 will match the enclosing full CU. */
2925 if (comp_unit_die->tag == DW_TAG_partial_unit)
2927 this_cu->v.quick->no_file_data = 1;
2936 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2939 struct quick_file_names find_entry;
2941 line_offset = DW_UNSND (attr);
2943 /* We may have already read in this line header (TU line header sharing).
2944 If we have we're done. */
2945 find_entry.hash.dwo_unit = cu->dwo_unit;
2946 find_entry.hash.line_offset.sect_off = line_offset;
2947 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2948 &find_entry, INSERT);
2951 lh_cu->v.quick->file_names = *slot;
2955 lh = dwarf_decode_line_header (line_offset, cu);
2959 lh_cu->v.quick->no_file_data = 1;
2963 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2964 qfn->hash.dwo_unit = cu->dwo_unit;
2965 qfn->hash.line_offset.sect_off = line_offset;
2966 gdb_assert (slot != NULL);
2969 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2971 qfn->num_file_names = lh->num_file_names;
2972 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2973 lh->num_file_names * sizeof (char *));
2974 for (i = 0; i < lh->num_file_names; ++i)
2975 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2976 qfn->real_names = NULL;
2978 free_line_header (lh);
2980 lh_cu->v.quick->file_names = qfn;
2983 /* A helper for the "quick" functions which attempts to read the line
2984 table for THIS_CU. */
2986 static struct quick_file_names *
2987 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2989 /* This should never be called for TUs. */
2990 gdb_assert (! this_cu->is_debug_types);
2991 /* Nor type unit groups. */
2992 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
2994 if (this_cu->v.quick->file_names != NULL)
2995 return this_cu->v.quick->file_names;
2996 /* If we know there is no line data, no point in looking again. */
2997 if (this_cu->v.quick->no_file_data)
3000 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3002 if (this_cu->v.quick->no_file_data)
3004 return this_cu->v.quick->file_names;
3007 /* A helper for the "quick" functions which computes and caches the
3008 real path for a given file name from the line table. */
3011 dw2_get_real_path (struct objfile *objfile,
3012 struct quick_file_names *qfn, int index)
3014 if (qfn->real_names == NULL)
3015 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3016 qfn->num_file_names, sizeof (char *));
3018 if (qfn->real_names[index] == NULL)
3019 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3021 return qfn->real_names[index];
3024 static struct symtab *
3025 dw2_find_last_source_symtab (struct objfile *objfile)
3029 dw2_setup (objfile);
3030 index = dwarf2_per_objfile->n_comp_units - 1;
3031 return dw2_instantiate_symtab (dw2_get_cu (index));
3034 /* Traversal function for dw2_forget_cached_source_info. */
3037 dw2_free_cached_file_names (void **slot, void *info)
3039 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3041 if (file_data->real_names)
3045 for (i = 0; i < file_data->num_file_names; ++i)
3047 xfree ((void*) file_data->real_names[i]);
3048 file_data->real_names[i] = NULL;
3056 dw2_forget_cached_source_info (struct objfile *objfile)
3058 dw2_setup (objfile);
3060 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3061 dw2_free_cached_file_names, NULL);
3064 /* Helper function for dw2_map_symtabs_matching_filename that expands
3065 the symtabs and calls the iterator. */
3068 dw2_map_expand_apply (struct objfile *objfile,
3069 struct dwarf2_per_cu_data *per_cu,
3070 const char *name, const char *real_path,
3071 int (*callback) (struct symtab *, void *),
3074 struct symtab *last_made = objfile->symtabs;
3076 /* Don't visit already-expanded CUs. */
3077 if (per_cu->v.quick->symtab)
3080 /* This may expand more than one symtab, and we want to iterate over
3082 dw2_instantiate_symtab (per_cu);
3084 return iterate_over_some_symtabs (name, real_path, callback, data,
3085 objfile->symtabs, last_made);
3088 /* Implementation of the map_symtabs_matching_filename method. */
3091 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3092 const char *real_path,
3093 int (*callback) (struct symtab *, void *),
3097 const char *name_basename = lbasename (name);
3099 dw2_setup (objfile);
3101 /* The rule is CUs specify all the files, including those used by
3102 any TU, so there's no need to scan TUs here. */
3104 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3107 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3108 struct quick_file_names *file_data;
3110 /* We only need to look at symtabs not already expanded. */
3111 if (per_cu->v.quick->symtab)
3114 file_data = dw2_get_file_names (per_cu);
3115 if (file_data == NULL)
3118 for (j = 0; j < file_data->num_file_names; ++j)
3120 const char *this_name = file_data->file_names[j];
3121 const char *this_real_name;
3123 if (compare_filenames_for_search (this_name, name))
3125 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3131 /* Before we invoke realpath, which can get expensive when many
3132 files are involved, do a quick comparison of the basenames. */
3133 if (! basenames_may_differ
3134 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3137 this_real_name = dw2_get_real_path (objfile, file_data, j);
3138 if (compare_filenames_for_search (this_real_name, name))
3140 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3146 if (real_path != NULL)
3148 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3149 gdb_assert (IS_ABSOLUTE_PATH (name));
3150 if (this_real_name != NULL
3151 && FILENAME_CMP (real_path, this_real_name) == 0)
3153 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3165 /* Struct used to manage iterating over all CUs looking for a symbol. */
3167 struct dw2_symtab_iterator
3169 /* The internalized form of .gdb_index. */
3170 struct mapped_index *index;
3171 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3172 int want_specific_block;
3173 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3174 Unused if !WANT_SPECIFIC_BLOCK. */
3176 /* The kind of symbol we're looking for. */
3178 /* The list of CUs from the index entry of the symbol,
3179 or NULL if not found. */
3181 /* The next element in VEC to look at. */
3183 /* The number of elements in VEC, or zero if there is no match. */
3187 /* Initialize the index symtab iterator ITER.
3188 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3189 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3192 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3193 struct mapped_index *index,
3194 int want_specific_block,
3199 iter->index = index;
3200 iter->want_specific_block = want_specific_block;
3201 iter->block_index = block_index;
3202 iter->domain = domain;
3205 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3206 iter->length = MAYBE_SWAP (*iter->vec);
3214 /* Return the next matching CU or NULL if there are no more. */
3216 static struct dwarf2_per_cu_data *
3217 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3219 for ( ; iter->next < iter->length; ++iter->next)
3221 offset_type cu_index_and_attrs =
3222 MAYBE_SWAP (iter->vec[iter->next + 1]);
3223 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3224 struct dwarf2_per_cu_data *per_cu;
3225 int want_static = iter->block_index != GLOBAL_BLOCK;
3226 /* This value is only valid for index versions >= 7. */
3227 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3228 gdb_index_symbol_kind symbol_kind =
3229 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3230 /* Only check the symbol attributes if they're present.
3231 Indices prior to version 7 don't record them,
3232 and indices >= 7 may elide them for certain symbols
3233 (gold does this). */
3235 (iter->index->version >= 7
3236 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3238 /* Don't crash on bad data. */
3239 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3240 + dwarf2_per_objfile->n_type_units))
3242 complaint (&symfile_complaints,
3243 _(".gdb_index entry has bad CU index"
3244 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3248 per_cu = dw2_get_cu (cu_index);
3250 /* Skip if already read in. */
3251 if (per_cu->v.quick->symtab)
3255 && iter->want_specific_block
3256 && want_static != is_static)
3259 /* Only check the symbol's kind if it has one. */
3262 switch (iter->domain)
3265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3266 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3267 /* Some types are also in VAR_DOMAIN. */
3268 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3272 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3276 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3291 static struct symtab *
3292 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3293 const char *name, domain_enum domain)
3295 struct symtab *stab_best = NULL;
3296 struct mapped_index *index;
3298 dw2_setup (objfile);
3300 index = dwarf2_per_objfile->index_table;
3302 /* index is NULL if OBJF_READNOW. */
3305 struct dw2_symtab_iterator iter;
3306 struct dwarf2_per_cu_data *per_cu;
3308 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3310 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3312 struct symbol *sym = NULL;
3313 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3315 /* Some caution must be observed with overloaded functions
3316 and methods, since the index will not contain any overload
3317 information (but NAME might contain it). */
3320 struct blockvector *bv = BLOCKVECTOR (stab);
3321 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3323 sym = lookup_block_symbol (block, name, domain);
3326 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3328 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3334 /* Keep looking through other CUs. */
3342 dw2_print_stats (struct objfile *objfile)
3344 int i, total, count;
3346 dw2_setup (objfile);
3347 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3349 for (i = 0; i < total; ++i)
3351 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3353 if (!per_cu->v.quick->symtab)
3356 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3357 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3360 /* This dumps minimal information about the index.
3361 It is called via "mt print objfiles".
3362 One use is to verify .gdb_index has been loaded by the
3363 gdb.dwarf2/gdb-index.exp testcase. */
3366 dw2_dump (struct objfile *objfile)
3368 dw2_setup (objfile);
3369 gdb_assert (dwarf2_per_objfile->using_index);
3370 printf_filtered (".gdb_index:");
3371 if (dwarf2_per_objfile->index_table != NULL)
3373 printf_filtered (" version %d\n",
3374 dwarf2_per_objfile->index_table->version);
3377 printf_filtered (" faked for \"readnow\"\n");
3378 printf_filtered ("\n");
3382 dw2_relocate (struct objfile *objfile,
3383 const struct section_offsets *new_offsets,
3384 const struct section_offsets *delta)
3386 /* There's nothing to relocate here. */
3390 dw2_expand_symtabs_for_function (struct objfile *objfile,
3391 const char *func_name)
3393 struct mapped_index *index;
3395 dw2_setup (objfile);
3397 index = dwarf2_per_objfile->index_table;
3399 /* index is NULL if OBJF_READNOW. */
3402 struct dw2_symtab_iterator iter;
3403 struct dwarf2_per_cu_data *per_cu;
3405 /* Note: It doesn't matter what we pass for block_index here. */
3406 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3409 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3410 dw2_instantiate_symtab (per_cu);
3415 dw2_expand_all_symtabs (struct objfile *objfile)
3419 dw2_setup (objfile);
3421 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3422 + dwarf2_per_objfile->n_type_units); ++i)
3424 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3426 dw2_instantiate_symtab (per_cu);
3431 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3432 const char *fullname)
3436 dw2_setup (objfile);
3438 /* We don't need to consider type units here.
3439 This is only called for examining code, e.g. expand_line_sal.
3440 There can be an order of magnitude (or more) more type units
3441 than comp units, and we avoid them if we can. */
3443 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3446 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3447 struct quick_file_names *file_data;
3449 /* We only need to look at symtabs not already expanded. */
3450 if (per_cu->v.quick->symtab)
3453 file_data = dw2_get_file_names (per_cu);
3454 if (file_data == NULL)
3457 for (j = 0; j < file_data->num_file_names; ++j)
3459 const char *this_fullname = file_data->file_names[j];
3461 if (filename_cmp (this_fullname, fullname) == 0)
3463 dw2_instantiate_symtab (per_cu);
3471 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3472 struct objfile *objfile, int global,
3473 int (*callback) (struct block *,
3474 struct symbol *, void *),
3475 void *data, symbol_compare_ftype *match,
3476 symbol_compare_ftype *ordered_compare)
3478 /* Currently unimplemented; used for Ada. The function can be called if the
3479 current language is Ada for a non-Ada objfile using GNU index. As Ada
3480 does not look for non-Ada symbols this function should just return. */
3484 dw2_expand_symtabs_matching
3485 (struct objfile *objfile,
3486 int (*file_matcher) (const char *, void *, int basenames),
3487 int (*name_matcher) (const char *, void *),
3488 enum search_domain kind,
3493 struct mapped_index *index;
3495 dw2_setup (objfile);
3497 /* index_table is NULL if OBJF_READNOW. */
3498 if (!dwarf2_per_objfile->index_table)
3500 index = dwarf2_per_objfile->index_table;
3502 if (file_matcher != NULL)
3504 struct cleanup *cleanup;
3505 htab_t visited_found, visited_not_found;
3507 visited_found = htab_create_alloc (10,
3508 htab_hash_pointer, htab_eq_pointer,
3509 NULL, xcalloc, xfree);
3510 cleanup = make_cleanup_htab_delete (visited_found);
3511 visited_not_found = htab_create_alloc (10,
3512 htab_hash_pointer, htab_eq_pointer,
3513 NULL, xcalloc, xfree);
3514 make_cleanup_htab_delete (visited_not_found);
3516 /* The rule is CUs specify all the files, including those used by
3517 any TU, so there's no need to scan TUs here. */
3519 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3522 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3523 struct quick_file_names *file_data;
3526 per_cu->v.quick->mark = 0;
3528 /* We only need to look at symtabs not already expanded. */
3529 if (per_cu->v.quick->symtab)
3532 file_data = dw2_get_file_names (per_cu);
3533 if (file_data == NULL)
3536 if (htab_find (visited_not_found, file_data) != NULL)
3538 else if (htab_find (visited_found, file_data) != NULL)
3540 per_cu->v.quick->mark = 1;
3544 for (j = 0; j < file_data->num_file_names; ++j)
3546 const char *this_real_name;
3548 if (file_matcher (file_data->file_names[j], data, 0))
3550 per_cu->v.quick->mark = 1;
3554 /* Before we invoke realpath, which can get expensive when many
3555 files are involved, do a quick comparison of the basenames. */
3556 if (!basenames_may_differ
3557 && !file_matcher (lbasename (file_data->file_names[j]),
3561 this_real_name = dw2_get_real_path (objfile, file_data, j);
3562 if (file_matcher (this_real_name, data, 0))
3564 per_cu->v.quick->mark = 1;
3569 slot = htab_find_slot (per_cu->v.quick->mark
3571 : visited_not_found,
3576 do_cleanups (cleanup);
3579 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3581 offset_type idx = 2 * iter;
3583 offset_type *vec, vec_len, vec_idx;
3585 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3588 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3590 if (! (*name_matcher) (name, data))
3593 /* The name was matched, now expand corresponding CUs that were
3595 vec = (offset_type *) (index->constant_pool
3596 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3597 vec_len = MAYBE_SWAP (vec[0]);
3598 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3600 struct dwarf2_per_cu_data *per_cu;
3601 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3602 gdb_index_symbol_kind symbol_kind =
3603 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3604 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3605 /* Only check the symbol attributes if they're present.
3606 Indices prior to version 7 don't record them,
3607 and indices >= 7 may elide them for certain symbols
3608 (gold does this). */
3610 (index->version >= 7
3611 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3613 /* Only check the symbol's kind if it has one. */
3618 case VARIABLES_DOMAIN:
3619 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3622 case FUNCTIONS_DOMAIN:
3623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3635 /* Don't crash on bad data. */
3636 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3637 + dwarf2_per_objfile->n_type_units))
3639 complaint (&symfile_complaints,
3640 _(".gdb_index entry has bad CU index"
3641 " [in module %s]"), objfile->name);
3645 per_cu = dw2_get_cu (cu_index);
3646 if (file_matcher == NULL || per_cu->v.quick->mark)
3647 dw2_instantiate_symtab (per_cu);
3652 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3655 static struct symtab *
3656 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3660 if (BLOCKVECTOR (symtab) != NULL
3661 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3664 if (symtab->includes == NULL)
3667 for (i = 0; symtab->includes[i]; ++i)
3669 struct symtab *s = symtab->includes[i];
3671 s = recursively_find_pc_sect_symtab (s, pc);
3679 static struct symtab *
3680 dw2_find_pc_sect_symtab (struct objfile *objfile,
3681 struct minimal_symbol *msymbol,
3683 struct obj_section *section,
3686 struct dwarf2_per_cu_data *data;
3687 struct symtab *result;
3689 dw2_setup (objfile);
3691 if (!objfile->psymtabs_addrmap)
3694 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3698 if (warn_if_readin && data->v.quick->symtab)
3699 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3700 paddress (get_objfile_arch (objfile), pc));
3702 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3703 gdb_assert (result != NULL);
3708 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3709 void *data, int need_fullname)
3712 struct cleanup *cleanup;
3713 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3714 NULL, xcalloc, xfree);
3716 cleanup = make_cleanup_htab_delete (visited);
3717 dw2_setup (objfile);
3719 /* The rule is CUs specify all the files, including those used by
3720 any TU, so there's no need to scan TUs here.
3721 We can ignore file names coming from already-expanded CUs. */
3723 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3725 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3727 if (per_cu->v.quick->symtab)
3729 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3732 *slot = per_cu->v.quick->file_names;
3736 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3739 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3740 struct quick_file_names *file_data;
3743 /* We only need to look at symtabs not already expanded. */
3744 if (per_cu->v.quick->symtab)
3747 file_data = dw2_get_file_names (per_cu);
3748 if (file_data == NULL)
3751 slot = htab_find_slot (visited, file_data, INSERT);
3754 /* Already visited. */
3759 for (j = 0; j < file_data->num_file_names; ++j)
3761 const char *this_real_name;
3764 this_real_name = dw2_get_real_path (objfile, file_data, j);
3766 this_real_name = NULL;
3767 (*fun) (file_data->file_names[j], this_real_name, data);
3771 do_cleanups (cleanup);
3775 dw2_has_symbols (struct objfile *objfile)
3780 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3783 dw2_find_last_source_symtab,
3784 dw2_forget_cached_source_info,
3785 dw2_map_symtabs_matching_filename,
3790 dw2_expand_symtabs_for_function,
3791 dw2_expand_all_symtabs,
3792 dw2_expand_symtabs_with_fullname,
3793 dw2_map_matching_symbols,
3794 dw2_expand_symtabs_matching,
3795 dw2_find_pc_sect_symtab,
3796 dw2_map_symbol_filenames
3799 /* Initialize for reading DWARF for this objfile. Return 0 if this
3800 file will use psymtabs, or 1 if using the GNU index. */
3803 dwarf2_initialize_objfile (struct objfile *objfile)
3805 /* If we're about to read full symbols, don't bother with the
3806 indices. In this case we also don't care if some other debug
3807 format is making psymtabs, because they are all about to be
3809 if ((objfile->flags & OBJF_READNOW))
3813 dwarf2_per_objfile->using_index = 1;
3814 create_all_comp_units (objfile);
3815 create_all_type_units (objfile);
3816 dwarf2_per_objfile->quick_file_names_table =
3817 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3819 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3820 + dwarf2_per_objfile->n_type_units); ++i)
3822 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3824 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3825 struct dwarf2_per_cu_quick_data);
3828 /* Return 1 so that gdb sees the "quick" functions. However,
3829 these functions will be no-ops because we will have expanded
3834 if (dwarf2_read_index (objfile))
3842 /* Build a partial symbol table. */
3845 dwarf2_build_psymtabs (struct objfile *objfile)
3847 volatile struct gdb_exception except;
3849 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3851 init_psymbol_list (objfile, 1024);
3854 TRY_CATCH (except, RETURN_MASK_ERROR)
3856 /* This isn't really ideal: all the data we allocate on the
3857 objfile's obstack is still uselessly kept around. However,
3858 freeing it seems unsafe. */
3859 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3861 dwarf2_build_psymtabs_hard (objfile);
3862 discard_cleanups (cleanups);
3864 if (except.reason < 0)
3865 exception_print (gdb_stderr, except);
3868 /* Return the total length of the CU described by HEADER. */
3871 get_cu_length (const struct comp_unit_head *header)
3873 return header->initial_length_size + header->length;
3876 /* Return TRUE if OFFSET is within CU_HEADER. */
3879 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3881 sect_offset bottom = { cu_header->offset.sect_off };
3882 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3884 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3887 /* Find the base address of the compilation unit for range lists and
3888 location lists. It will normally be specified by DW_AT_low_pc.
3889 In DWARF-3 draft 4, the base address could be overridden by
3890 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3891 compilation units with discontinuous ranges. */
3894 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3896 struct attribute *attr;
3899 cu->base_address = 0;
3901 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3904 cu->base_address = DW_ADDR (attr);
3909 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3912 cu->base_address = DW_ADDR (attr);
3918 /* Read in the comp unit header information from the debug_info at info_ptr.
3919 NOTE: This leaves members offset, first_die_offset to be filled in
3922 static const gdb_byte *
3923 read_comp_unit_head (struct comp_unit_head *cu_header,
3924 const gdb_byte *info_ptr, bfd *abfd)
3927 unsigned int bytes_read;
3929 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3930 cu_header->initial_length_size = bytes_read;
3931 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3932 info_ptr += bytes_read;
3933 cu_header->version = read_2_bytes (abfd, info_ptr);
3935 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3937 info_ptr += bytes_read;
3938 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3940 signed_addr = bfd_get_sign_extend_vma (abfd);
3941 if (signed_addr < 0)
3942 internal_error (__FILE__, __LINE__,
3943 _("read_comp_unit_head: dwarf from non elf file"));
3944 cu_header->signed_addr_p = signed_addr;
3949 /* Helper function that returns the proper abbrev section for
3952 static struct dwarf2_section_info *
3953 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3955 struct dwarf2_section_info *abbrev;
3957 if (this_cu->is_dwz)
3958 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3960 abbrev = &dwarf2_per_objfile->abbrev;
3965 /* Subroutine of read_and_check_comp_unit_head and
3966 read_and_check_type_unit_head to simplify them.
3967 Perform various error checking on the header. */
3970 error_check_comp_unit_head (struct comp_unit_head *header,
3971 struct dwarf2_section_info *section,
3972 struct dwarf2_section_info *abbrev_section)
3974 bfd *abfd = section->asection->owner;
3975 const char *filename = bfd_get_filename (abfd);
3977 if (header->version != 2 && header->version != 3 && header->version != 4)
3978 error (_("Dwarf Error: wrong version in compilation unit header "
3979 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3982 if (header->abbrev_offset.sect_off
3983 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3984 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3985 "(offset 0x%lx + 6) [in module %s]"),
3986 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3989 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3990 avoid potential 32-bit overflow. */
3991 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3993 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3994 "(offset 0x%lx + 0) [in module %s]"),
3995 (long) header->length, (long) header->offset.sect_off,
3999 /* Read in a CU/TU header and perform some basic error checking.
4000 The contents of the header are stored in HEADER.
4001 The result is a pointer to the start of the first DIE. */
4003 static const gdb_byte *
4004 read_and_check_comp_unit_head (struct comp_unit_head *header,
4005 struct dwarf2_section_info *section,
4006 struct dwarf2_section_info *abbrev_section,
4007 const gdb_byte *info_ptr,
4008 int is_debug_types_section)
4010 const gdb_byte *beg_of_comp_unit = info_ptr;
4011 bfd *abfd = section->asection->owner;
4013 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4015 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4017 /* If we're reading a type unit, skip over the signature and
4018 type_offset fields. */
4019 if (is_debug_types_section)
4020 info_ptr += 8 /*signature*/ + header->offset_size;
4022 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4024 error_check_comp_unit_head (header, section, abbrev_section);
4029 /* Read in the types comp unit header information from .debug_types entry at
4030 types_ptr. The result is a pointer to one past the end of the header. */
4032 static const gdb_byte *
4033 read_and_check_type_unit_head (struct comp_unit_head *header,
4034 struct dwarf2_section_info *section,
4035 struct dwarf2_section_info *abbrev_section,
4036 const gdb_byte *info_ptr,
4037 ULONGEST *signature,
4038 cu_offset *type_offset_in_tu)
4040 const gdb_byte *beg_of_comp_unit = info_ptr;
4041 bfd *abfd = section->asection->owner;
4043 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4045 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4047 /* If we're reading a type unit, skip over the signature and
4048 type_offset fields. */
4049 if (signature != NULL)
4050 *signature = read_8_bytes (abfd, info_ptr);
4052 if (type_offset_in_tu != NULL)
4053 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4054 header->offset_size);
4055 info_ptr += header->offset_size;
4057 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4059 error_check_comp_unit_head (header, section, abbrev_section);
4064 /* Fetch the abbreviation table offset from a comp or type unit header. */
4067 read_abbrev_offset (struct dwarf2_section_info *section,
4070 bfd *abfd = section->asection->owner;
4071 const gdb_byte *info_ptr;
4072 unsigned int length, initial_length_size, offset_size;
4073 sect_offset abbrev_offset;
4075 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4076 info_ptr = section->buffer + offset.sect_off;
4077 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4078 offset_size = initial_length_size == 4 ? 4 : 8;
4079 info_ptr += initial_length_size + 2 /*version*/;
4080 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4081 return abbrev_offset;
4084 /* Allocate a new partial symtab for file named NAME and mark this new
4085 partial symtab as being an include of PST. */
4088 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4089 struct objfile *objfile)
4091 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4093 if (!IS_ABSOLUTE_PATH (subpst->filename))
4095 /* It shares objfile->objfile_obstack. */
4096 subpst->dirname = pst->dirname;
4099 subpst->section_offsets = pst->section_offsets;
4100 subpst->textlow = 0;
4101 subpst->texthigh = 0;
4103 subpst->dependencies = (struct partial_symtab **)
4104 obstack_alloc (&objfile->objfile_obstack,
4105 sizeof (struct partial_symtab *));
4106 subpst->dependencies[0] = pst;
4107 subpst->number_of_dependencies = 1;
4109 subpst->globals_offset = 0;
4110 subpst->n_global_syms = 0;
4111 subpst->statics_offset = 0;
4112 subpst->n_static_syms = 0;
4113 subpst->symtab = NULL;
4114 subpst->read_symtab = pst->read_symtab;
4117 /* No private part is necessary for include psymtabs. This property
4118 can be used to differentiate between such include psymtabs and
4119 the regular ones. */
4120 subpst->read_symtab_private = NULL;
4123 /* Read the Line Number Program data and extract the list of files
4124 included by the source file represented by PST. Build an include
4125 partial symtab for each of these included files. */
4128 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4129 struct die_info *die,
4130 struct partial_symtab *pst)
4132 struct line_header *lh = NULL;
4133 struct attribute *attr;
4135 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4137 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4139 return; /* No linetable, so no includes. */
4141 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4142 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4144 free_line_header (lh);
4148 hash_signatured_type (const void *item)
4150 const struct signatured_type *sig_type = item;
4152 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4153 return sig_type->signature;
4157 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4159 const struct signatured_type *lhs = item_lhs;
4160 const struct signatured_type *rhs = item_rhs;
4162 return lhs->signature == rhs->signature;
4165 /* Allocate a hash table for signatured types. */
4168 allocate_signatured_type_table (struct objfile *objfile)
4170 return htab_create_alloc_ex (41,
4171 hash_signatured_type,
4174 &objfile->objfile_obstack,
4175 hashtab_obstack_allocate,
4176 dummy_obstack_deallocate);
4179 /* A helper function to add a signatured type CU to a table. */
4182 add_signatured_type_cu_to_table (void **slot, void *datum)
4184 struct signatured_type *sigt = *slot;
4185 struct signatured_type ***datap = datum;
4193 /* Create the hash table of all entries in the .debug_types
4194 (or .debug_types.dwo) section(s).
4195 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4196 otherwise it is NULL.
4198 The result is a pointer to the hash table or NULL if there are no types.
4200 Note: This function processes DWO files only, not DWP files. */
4203 create_debug_types_hash_table (struct dwo_file *dwo_file,
4204 VEC (dwarf2_section_info_def) *types)
4206 struct objfile *objfile = dwarf2_per_objfile->objfile;
4207 htab_t types_htab = NULL;
4209 struct dwarf2_section_info *section;
4210 struct dwarf2_section_info *abbrev_section;
4212 if (VEC_empty (dwarf2_section_info_def, types))
4215 abbrev_section = (dwo_file != NULL
4216 ? &dwo_file->sections.abbrev
4217 : &dwarf2_per_objfile->abbrev);
4219 if (dwarf2_read_debug)
4220 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4221 dwo_file ? ".dwo" : "",
4222 bfd_get_filename (abbrev_section->asection->owner));
4225 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4229 const gdb_byte *info_ptr, *end_ptr;
4230 struct dwarf2_section_info *abbrev_section;
4232 dwarf2_read_section (objfile, section);
4233 info_ptr = section->buffer;
4235 if (info_ptr == NULL)
4238 /* We can't set abfd until now because the section may be empty or
4239 not present, in which case section->asection will be NULL. */
4240 abfd = section->asection->owner;
4243 abbrev_section = &dwo_file->sections.abbrev;
4245 abbrev_section = &dwarf2_per_objfile->abbrev;
4247 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4248 because we don't need to read any dies: the signature is in the
4251 end_ptr = info_ptr + section->size;
4252 while (info_ptr < end_ptr)
4255 cu_offset type_offset_in_tu;
4257 struct signatured_type *sig_type;
4258 struct dwo_unit *dwo_tu;
4260 const gdb_byte *ptr = info_ptr;
4261 struct comp_unit_head header;
4262 unsigned int length;
4264 offset.sect_off = ptr - section->buffer;
4266 /* We need to read the type's signature in order to build the hash
4267 table, but we don't need anything else just yet. */
4269 ptr = read_and_check_type_unit_head (&header, section,
4270 abbrev_section, ptr,
4271 &signature, &type_offset_in_tu);
4273 length = get_cu_length (&header);
4275 /* Skip dummy type units. */
4276 if (ptr >= info_ptr + length
4277 || peek_abbrev_code (abfd, ptr) == 0)
4283 if (types_htab == NULL)
4286 types_htab = allocate_dwo_unit_table (objfile);
4288 types_htab = allocate_signatured_type_table (objfile);
4294 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4296 dwo_tu->dwo_file = dwo_file;
4297 dwo_tu->signature = signature;
4298 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4299 dwo_tu->section = section;
4300 dwo_tu->offset = offset;
4301 dwo_tu->length = length;
4305 /* N.B.: type_offset is not usable if this type uses a DWO file.
4306 The real type_offset is in the DWO file. */
4308 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4309 struct signatured_type);
4310 sig_type->signature = signature;
4311 sig_type->type_offset_in_tu = type_offset_in_tu;
4312 sig_type->per_cu.objfile = objfile;
4313 sig_type->per_cu.is_debug_types = 1;
4314 sig_type->per_cu.section = section;
4315 sig_type->per_cu.offset = offset;
4316 sig_type->per_cu.length = length;
4319 slot = htab_find_slot (types_htab,
4320 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4322 gdb_assert (slot != NULL);
4325 sect_offset dup_offset;
4329 const struct dwo_unit *dup_tu = *slot;
4331 dup_offset = dup_tu->offset;
4335 const struct signatured_type *dup_tu = *slot;
4337 dup_offset = dup_tu->per_cu.offset;
4340 complaint (&symfile_complaints,
4341 _("debug type entry at offset 0x%x is duplicate to"
4342 " the entry at offset 0x%x, signature %s"),
4343 offset.sect_off, dup_offset.sect_off,
4344 hex_string (signature));
4346 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4348 if (dwarf2_read_debug)
4349 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4351 hex_string (signature));
4360 /* Create the hash table of all entries in the .debug_types section,
4361 and initialize all_type_units.
4362 The result is zero if there is an error (e.g. missing .debug_types section),
4363 otherwise non-zero. */
4366 create_all_type_units (struct objfile *objfile)
4369 struct signatured_type **iter;
4371 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4372 if (types_htab == NULL)
4374 dwarf2_per_objfile->signatured_types = NULL;
4378 dwarf2_per_objfile->signatured_types = types_htab;
4380 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4381 dwarf2_per_objfile->all_type_units
4382 = xmalloc (dwarf2_per_objfile->n_type_units
4383 * sizeof (struct signatured_type *));
4384 iter = &dwarf2_per_objfile->all_type_units[0];
4385 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4386 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4387 == dwarf2_per_objfile->n_type_units);
4392 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4393 Fill in SIG_ENTRY with DWO_ENTRY. */
4396 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4397 struct signatured_type *sig_entry,
4398 struct dwo_unit *dwo_entry)
4400 /* Make sure we're not clobbering something we don't expect to. */
4401 gdb_assert (! sig_entry->per_cu.queued);
4402 gdb_assert (sig_entry->per_cu.cu == NULL);
4403 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4404 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4405 gdb_assert (sig_entry->signature == dwo_entry->signature);
4406 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4407 gdb_assert (sig_entry->type_unit_group == NULL);
4408 gdb_assert (sig_entry->dwo_unit == NULL);
4410 sig_entry->per_cu.section = dwo_entry->section;
4411 sig_entry->per_cu.offset = dwo_entry->offset;
4412 sig_entry->per_cu.length = dwo_entry->length;
4413 sig_entry->per_cu.reading_dwo_directly = 1;
4414 sig_entry->per_cu.objfile = objfile;
4415 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4416 sig_entry->dwo_unit = dwo_entry;
4419 /* Subroutine of lookup_signatured_type.
4420 If we haven't read the TU yet, create the signatured_type data structure
4421 for a TU to be read in directly from a DWO file, bypassing the stub.
4422 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4423 using .gdb_index, then when reading a CU we want to stay in the DWO file
4424 containing that CU. Otherwise we could end up reading several other DWO
4425 files (due to comdat folding) to process the transitive closure of all the
4426 mentioned TUs, and that can be slow. The current DWO file will have every
4427 type signature that it needs.
4428 We only do this for .gdb_index because in the psymtab case we already have
4429 to read all the DWOs to build the type unit groups. */
4431 static struct signatured_type *
4432 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4434 struct objfile *objfile = dwarf2_per_objfile->objfile;
4435 struct dwo_file *dwo_file;
4436 struct dwo_unit find_dwo_entry, *dwo_entry;
4437 struct signatured_type find_sig_entry, *sig_entry;
4439 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4441 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4442 dwo_unit of the TU itself. */
4443 dwo_file = cu->dwo_unit->dwo_file;
4445 /* We only ever need to read in one copy of a signatured type.
4446 Just use the global signatured_types array. If this is the first time
4447 we're reading this type, replace the recorded data from .gdb_index with
4450 if (dwarf2_per_objfile->signatured_types == NULL)
4452 find_sig_entry.signature = sig;
4453 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4454 if (sig_entry == NULL)
4457 /* We can get here with the TU already read, *or* in the process of being
4458 read. Don't reassign it if that's the case. Also note that if the TU is
4459 already being read, it may not have come from a DWO, the program may be
4460 a mix of Fission-compiled code and non-Fission-compiled code. */
4461 /* Have we already tried to read this TU? */
4462 if (sig_entry->per_cu.tu_read)
4465 /* Ok, this is the first time we're reading this TU. */
4466 if (dwo_file->tus == NULL)
4468 find_dwo_entry.signature = sig;
4469 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4470 if (dwo_entry == NULL)
4473 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4477 /* Subroutine of lookup_dwp_signatured_type.
4478 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4480 static struct signatured_type *
4481 add_type_unit (ULONGEST sig)
4483 struct objfile *objfile = dwarf2_per_objfile->objfile;
4484 int n_type_units = dwarf2_per_objfile->n_type_units;
4485 struct signatured_type *sig_type;
4489 dwarf2_per_objfile->all_type_units =
4490 xrealloc (dwarf2_per_objfile->all_type_units,
4491 n_type_units * sizeof (struct signatured_type *));
4492 dwarf2_per_objfile->n_type_units = n_type_units;
4493 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4494 struct signatured_type);
4495 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4496 sig_type->signature = sig;
4497 sig_type->per_cu.is_debug_types = 1;
4498 sig_type->per_cu.v.quick =
4499 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4500 struct dwarf2_per_cu_quick_data);
4501 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4503 gdb_assert (*slot == NULL);
4505 /* The rest of sig_type must be filled in by the caller. */
4509 /* Subroutine of lookup_signatured_type.
4510 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4511 then try the DWP file.
4512 Normally this "can't happen", but if there's a bug in signature
4513 generation and/or the DWP file is built incorrectly, it can happen.
4514 Using the type directly from the DWP file means we don't have the stub
4515 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4516 not critical. [Eventually the stub may go away for type units anyway.] */
4518 static struct signatured_type *
4519 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4521 struct objfile *objfile = dwarf2_per_objfile->objfile;
4522 struct dwp_file *dwp_file = get_dwp_file ();
4523 struct dwo_unit *dwo_entry;
4524 struct signatured_type find_sig_entry, *sig_entry;
4526 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4527 gdb_assert (dwp_file != NULL);
4529 if (dwarf2_per_objfile->signatured_types != NULL)
4531 find_sig_entry.signature = sig;
4532 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4534 if (sig_entry != NULL)
4538 /* This is the "shouldn't happen" case.
4539 Try the DWP file and hope for the best. */
4540 if (dwp_file->tus == NULL)
4542 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4543 sig, 1 /* is_debug_types */);
4544 if (dwo_entry == NULL)
4547 sig_entry = add_type_unit (sig);
4548 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4550 /* The caller will signal a complaint if we return NULL.
4551 Here we don't return NULL but we still want to complain. */
4552 complaint (&symfile_complaints,
4553 _("Bad type signature %s referenced by %s at 0x%x,"
4554 " coping by using copy in DWP [in module %s]"),
4556 cu->per_cu->is_debug_types ? "TU" : "CU",
4557 cu->per_cu->offset.sect_off,
4563 /* Lookup a signature based type for DW_FORM_ref_sig8.
4564 Returns NULL if signature SIG is not present in the table.
4565 It is up to the caller to complain about this. */
4567 static struct signatured_type *
4568 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4571 && dwarf2_per_objfile->using_index)
4573 /* We're in a DWO/DWP file, and we're using .gdb_index.
4574 These cases require special processing. */
4575 if (get_dwp_file () == NULL)
4576 return lookup_dwo_signatured_type (cu, sig);
4578 return lookup_dwp_signatured_type (cu, sig);
4582 struct signatured_type find_entry, *entry;
4584 if (dwarf2_per_objfile->signatured_types == NULL)
4586 find_entry.signature = sig;
4587 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4592 /* Low level DIE reading support. */
4594 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4597 init_cu_die_reader (struct die_reader_specs *reader,
4598 struct dwarf2_cu *cu,
4599 struct dwarf2_section_info *section,
4600 struct dwo_file *dwo_file)
4602 gdb_assert (section->readin && section->buffer != NULL);
4603 reader->abfd = section->asection->owner;
4605 reader->dwo_file = dwo_file;
4606 reader->die_section = section;
4607 reader->buffer = section->buffer;
4608 reader->buffer_end = section->buffer + section->size;
4609 reader->comp_dir = NULL;
4612 /* Subroutine of init_cutu_and_read_dies to simplify it.
4613 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4614 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4617 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4618 from it to the DIE in the DWO. If NULL we are skipping the stub.
4619 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4620 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4621 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4622 COMP_DIR must be non-NULL.
4623 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4624 are filled in with the info of the DIE from the DWO file.
4625 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4626 provided an abbrev table to use.
4627 The result is non-zero if a valid (non-dummy) DIE was found. */
4630 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4631 struct dwo_unit *dwo_unit,
4632 int abbrev_table_provided,
4633 struct die_info *stub_comp_unit_die,
4634 const char *stub_comp_dir,
4635 struct die_reader_specs *result_reader,
4636 const gdb_byte **result_info_ptr,
4637 struct die_info **result_comp_unit_die,
4638 int *result_has_children)
4640 struct objfile *objfile = dwarf2_per_objfile->objfile;
4641 struct dwarf2_cu *cu = this_cu->cu;
4642 struct dwarf2_section_info *section;
4644 const gdb_byte *begin_info_ptr, *info_ptr;
4645 const char *comp_dir_string;
4646 ULONGEST signature; /* Or dwo_id. */
4647 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4648 int i,num_extra_attrs;
4649 struct dwarf2_section_info *dwo_abbrev_section;
4650 struct attribute *attr;
4651 struct attribute comp_dir_attr;
4652 struct die_info *comp_unit_die;
4654 /* Both can't be provided. */
4655 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4657 /* These attributes aren't processed until later:
4658 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4659 However, the attribute is found in the stub which we won't have later.
4660 In order to not impose this complication on the rest of the code,
4661 we read them here and copy them to the DWO CU/TU die. */
4669 if (stub_comp_unit_die != NULL)
4671 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4673 if (! this_cu->is_debug_types)
4674 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4675 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4676 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4677 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4678 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4680 /* There should be a DW_AT_addr_base attribute here (if needed).
4681 We need the value before we can process DW_FORM_GNU_addr_index. */
4683 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4685 cu->addr_base = DW_UNSND (attr);
4687 /* There should be a DW_AT_ranges_base attribute here (if needed).
4688 We need the value before we can process DW_AT_ranges. */
4689 cu->ranges_base = 0;
4690 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4692 cu->ranges_base = DW_UNSND (attr);
4694 else if (stub_comp_dir != NULL)
4696 /* Reconstruct the comp_dir attribute to simplify the code below. */
4697 comp_dir = (struct attribute *)
4698 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4699 comp_dir->name = DW_AT_comp_dir;
4700 comp_dir->form = DW_FORM_string;
4701 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4702 DW_STRING (comp_dir) = stub_comp_dir;
4705 /* Set up for reading the DWO CU/TU. */
4706 cu->dwo_unit = dwo_unit;
4707 section = dwo_unit->section;
4708 dwarf2_read_section (objfile, section);
4709 abfd = section->asection->owner;
4710 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4711 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4712 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4714 if (this_cu->is_debug_types)
4716 ULONGEST header_signature;
4717 cu_offset type_offset_in_tu;
4718 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4720 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4724 &type_offset_in_tu);
4725 /* This is not an assert because it can be caused by bad debug info. */
4726 if (sig_type->signature != header_signature)
4728 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4729 " TU at offset 0x%x [in module %s]"),
4730 hex_string (sig_type->signature),
4731 hex_string (header_signature),
4732 dwo_unit->offset.sect_off,
4733 bfd_get_filename (abfd));
4735 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4736 /* For DWOs coming from DWP files, we don't know the CU length
4737 nor the type's offset in the TU until now. */
4738 dwo_unit->length = get_cu_length (&cu->header);
4739 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4741 /* Establish the type offset that can be used to lookup the type.
4742 For DWO files, we don't know it until now. */
4743 sig_type->type_offset_in_section.sect_off =
4744 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4748 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4751 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4752 /* For DWOs coming from DWP files, we don't know the CU length
4754 dwo_unit->length = get_cu_length (&cu->header);
4757 /* Replace the CU's original abbrev table with the DWO's.
4758 Reminder: We can't read the abbrev table until we've read the header. */
4759 if (abbrev_table_provided)
4761 /* Don't free the provided abbrev table, the caller of
4762 init_cutu_and_read_dies owns it. */
4763 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4764 /* Ensure the DWO abbrev table gets freed. */
4765 make_cleanup (dwarf2_free_abbrev_table, cu);
4769 dwarf2_free_abbrev_table (cu);
4770 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4771 /* Leave any existing abbrev table cleanup as is. */
4774 /* Read in the die, but leave space to copy over the attributes
4775 from the stub. This has the benefit of simplifying the rest of
4776 the code - all the work to maintain the illusion of a single
4777 DW_TAG_{compile,type}_unit DIE is done here. */
4778 num_extra_attrs = ((stmt_list != NULL)
4782 + (comp_dir != NULL));
4783 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4784 result_has_children, num_extra_attrs);
4786 /* Copy over the attributes from the stub to the DIE we just read in. */
4787 comp_unit_die = *result_comp_unit_die;
4788 i = comp_unit_die->num_attrs;
4789 if (stmt_list != NULL)
4790 comp_unit_die->attrs[i++] = *stmt_list;
4792 comp_unit_die->attrs[i++] = *low_pc;
4793 if (high_pc != NULL)
4794 comp_unit_die->attrs[i++] = *high_pc;
4796 comp_unit_die->attrs[i++] = *ranges;
4797 if (comp_dir != NULL)
4798 comp_unit_die->attrs[i++] = *comp_dir;
4799 comp_unit_die->num_attrs += num_extra_attrs;
4801 if (dwarf2_die_debug)
4803 fprintf_unfiltered (gdb_stdlog,
4804 "Read die from %s@0x%x of %s:\n",
4805 bfd_section_name (abfd, section->asection),
4806 (unsigned) (begin_info_ptr - section->buffer),
4807 bfd_get_filename (abfd));
4808 dump_die (comp_unit_die, dwarf2_die_debug);
4811 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4812 TUs by skipping the stub and going directly to the entry in the DWO file.
4813 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4814 to get it via circuitous means. Blech. */
4815 if (comp_dir != NULL)
4816 result_reader->comp_dir = DW_STRING (comp_dir);
4818 /* Skip dummy compilation units. */
4819 if (info_ptr >= begin_info_ptr + dwo_unit->length
4820 || peek_abbrev_code (abfd, info_ptr) == 0)
4823 *result_info_ptr = info_ptr;
4827 /* Subroutine of init_cutu_and_read_dies to simplify it.
4828 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4829 Returns NULL if the specified DWO unit cannot be found. */
4831 static struct dwo_unit *
4832 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4833 struct die_info *comp_unit_die)
4835 struct dwarf2_cu *cu = this_cu->cu;
4836 struct attribute *attr;
4838 struct dwo_unit *dwo_unit;
4839 const char *comp_dir, *dwo_name;
4841 gdb_assert (cu != NULL);
4843 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4844 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4845 gdb_assert (attr != NULL);
4846 dwo_name = DW_STRING (attr);
4848 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4850 comp_dir = DW_STRING (attr);
4852 if (this_cu->is_debug_types)
4854 struct signatured_type *sig_type;
4856 /* Since this_cu is the first member of struct signatured_type,
4857 we can go from a pointer to one to a pointer to the other. */
4858 sig_type = (struct signatured_type *) this_cu;
4859 signature = sig_type->signature;
4860 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4864 struct attribute *attr;
4866 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4868 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4870 dwo_name, this_cu->objfile->name);
4871 signature = DW_UNSND (attr);
4872 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4879 /* Subroutine of init_cutu_and_read_dies to simplify it.
4880 Read a TU directly from a DWO file, bypassing the stub. */
4883 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4884 die_reader_func_ftype *die_reader_func,
4887 struct dwarf2_cu *cu;
4888 struct signatured_type *sig_type;
4889 struct cleanup *cleanups, *free_cu_cleanup;
4890 struct die_reader_specs reader;
4891 const gdb_byte *info_ptr;
4892 struct die_info *comp_unit_die;
4895 /* Verify we can do the following downcast, and that we have the
4897 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4898 sig_type = (struct signatured_type *) this_cu;
4899 gdb_assert (sig_type->dwo_unit != NULL);
4901 cleanups = make_cleanup (null_cleanup, NULL);
4903 gdb_assert (this_cu->cu == NULL);
4904 cu = xmalloc (sizeof (*cu));
4905 init_one_comp_unit (cu, this_cu);
4906 /* If an error occurs while loading, release our storage. */
4907 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4909 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4910 0 /* abbrev_table_provided */,
4911 NULL /* stub_comp_unit_die */,
4912 sig_type->dwo_unit->dwo_file->comp_dir,
4914 &comp_unit_die, &has_children) == 0)
4917 do_cleanups (cleanups);
4921 /* All the "real" work is done here. */
4922 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4924 /* This duplicates some code in init_cutu_and_read_dies,
4925 but the alternative is making the latter more complex.
4926 This function is only for the special case of using DWO files directly:
4927 no point in overly complicating the general case just to handle this. */
4930 /* We've successfully allocated this compilation unit. Let our
4931 caller clean it up when finished with it. */
4932 discard_cleanups (free_cu_cleanup);
4934 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4935 So we have to manually free the abbrev table. */
4936 dwarf2_free_abbrev_table (cu);
4938 /* Link this CU into read_in_chain. */
4939 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4940 dwarf2_per_objfile->read_in_chain = this_cu;
4943 do_cleanups (free_cu_cleanup);
4945 do_cleanups (cleanups);
4948 /* Initialize a CU (or TU) and read its DIEs.
4949 If the CU defers to a DWO file, read the DWO file as well.
4951 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4952 Otherwise the table specified in the comp unit header is read in and used.
4953 This is an optimization for when we already have the abbrev table.
4955 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4956 Otherwise, a new CU is allocated with xmalloc.
4958 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4959 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4961 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4962 linker) then DIE_READER_FUNC will not get called. */
4965 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4966 struct abbrev_table *abbrev_table,
4967 int use_existing_cu, int keep,
4968 die_reader_func_ftype *die_reader_func,
4971 struct objfile *objfile = dwarf2_per_objfile->objfile;
4972 struct dwarf2_section_info *section = this_cu->section;
4973 bfd *abfd = section->asection->owner;
4974 struct dwarf2_cu *cu;
4975 const gdb_byte *begin_info_ptr, *info_ptr;
4976 struct die_reader_specs reader;
4977 struct die_info *comp_unit_die;
4979 struct attribute *attr;
4980 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4981 struct signatured_type *sig_type = NULL;
4982 struct dwarf2_section_info *abbrev_section;
4983 /* Non-zero if CU currently points to a DWO file and we need to
4984 reread it. When this happens we need to reread the skeleton die
4985 before we can reread the DWO file (this only applies to CUs, not TUs). */
4986 int rereading_dwo_cu = 0;
4988 if (dwarf2_die_debug)
4989 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4990 this_cu->is_debug_types ? "type" : "comp",
4991 this_cu->offset.sect_off);
4993 if (use_existing_cu)
4996 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4997 file (instead of going through the stub), short-circuit all of this. */
4998 if (this_cu->reading_dwo_directly)
5000 /* Narrow down the scope of possibilities to have to understand. */
5001 gdb_assert (this_cu->is_debug_types);
5002 gdb_assert (abbrev_table == NULL);
5003 gdb_assert (!use_existing_cu);
5004 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5008 cleanups = make_cleanup (null_cleanup, NULL);
5010 /* This is cheap if the section is already read in. */
5011 dwarf2_read_section (objfile, section);
5013 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5015 abbrev_section = get_abbrev_section_for_cu (this_cu);
5017 if (use_existing_cu && this_cu->cu != NULL)
5021 /* If this CU is from a DWO file we need to start over, we need to
5022 refetch the attributes from the skeleton CU.
5023 This could be optimized by retrieving those attributes from when we
5024 were here the first time: the previous comp_unit_die was stored in
5025 comp_unit_obstack. But there's no data yet that we need this
5027 if (cu->dwo_unit != NULL)
5028 rereading_dwo_cu = 1;
5032 /* If !use_existing_cu, this_cu->cu must be NULL. */
5033 gdb_assert (this_cu->cu == NULL);
5035 cu = xmalloc (sizeof (*cu));
5036 init_one_comp_unit (cu, this_cu);
5038 /* If an error occurs while loading, release our storage. */
5039 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5042 /* Get the header. */
5043 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5045 /* We already have the header, there's no need to read it in again. */
5046 info_ptr += cu->header.first_die_offset.cu_off;
5050 if (this_cu->is_debug_types)
5053 cu_offset type_offset_in_tu;
5055 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5056 abbrev_section, info_ptr,
5058 &type_offset_in_tu);
5060 /* Since per_cu is the first member of struct signatured_type,
5061 we can go from a pointer to one to a pointer to the other. */
5062 sig_type = (struct signatured_type *) this_cu;
5063 gdb_assert (sig_type->signature == signature);
5064 gdb_assert (sig_type->type_offset_in_tu.cu_off
5065 == type_offset_in_tu.cu_off);
5066 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5068 /* LENGTH has not been set yet for type units if we're
5069 using .gdb_index. */
5070 this_cu->length = get_cu_length (&cu->header);
5072 /* Establish the type offset that can be used to lookup the type. */
5073 sig_type->type_offset_in_section.sect_off =
5074 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5078 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5082 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5083 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5087 /* Skip dummy compilation units. */
5088 if (info_ptr >= begin_info_ptr + this_cu->length
5089 || peek_abbrev_code (abfd, info_ptr) == 0)
5091 do_cleanups (cleanups);
5095 /* If we don't have them yet, read the abbrevs for this compilation unit.
5096 And if we need to read them now, make sure they're freed when we're
5097 done. Note that it's important that if the CU had an abbrev table
5098 on entry we don't free it when we're done: Somewhere up the call stack
5099 it may be in use. */
5100 if (abbrev_table != NULL)
5102 gdb_assert (cu->abbrev_table == NULL);
5103 gdb_assert (cu->header.abbrev_offset.sect_off
5104 == abbrev_table->offset.sect_off);
5105 cu->abbrev_table = abbrev_table;
5107 else if (cu->abbrev_table == NULL)
5109 dwarf2_read_abbrevs (cu, abbrev_section);
5110 make_cleanup (dwarf2_free_abbrev_table, cu);
5112 else if (rereading_dwo_cu)
5114 dwarf2_free_abbrev_table (cu);
5115 dwarf2_read_abbrevs (cu, abbrev_section);
5118 /* Read the top level CU/TU die. */
5119 init_cu_die_reader (&reader, cu, section, NULL);
5120 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5122 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5124 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5125 DWO CU, that this test will fail (the attribute will not be present). */
5126 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5129 struct dwo_unit *dwo_unit;
5130 struct die_info *dwo_comp_unit_die;
5134 complaint (&symfile_complaints,
5135 _("compilation unit with DW_AT_GNU_dwo_name"
5136 " has children (offset 0x%x) [in module %s]"),
5137 this_cu->offset.sect_off, bfd_get_filename (abfd));
5139 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5140 if (dwo_unit != NULL)
5142 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5143 abbrev_table != NULL,
5144 comp_unit_die, NULL,
5146 &dwo_comp_unit_die, &has_children) == 0)
5149 do_cleanups (cleanups);
5152 comp_unit_die = dwo_comp_unit_die;
5156 /* Yikes, we couldn't find the rest of the DIE, we only have
5157 the stub. A complaint has already been logged. There's
5158 not much more we can do except pass on the stub DIE to
5159 die_reader_func. We don't want to throw an error on bad
5164 /* All of the above is setup for this call. Yikes. */
5165 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5167 /* Done, clean up. */
5168 if (free_cu_cleanup != NULL)
5172 /* We've successfully allocated this compilation unit. Let our
5173 caller clean it up when finished with it. */
5174 discard_cleanups (free_cu_cleanup);
5176 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5177 So we have to manually free the abbrev table. */
5178 dwarf2_free_abbrev_table (cu);
5180 /* Link this CU into read_in_chain. */
5181 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5182 dwarf2_per_objfile->read_in_chain = this_cu;
5185 do_cleanups (free_cu_cleanup);
5188 do_cleanups (cleanups);
5191 /* Read CU/TU THIS_CU in section SECTION,
5192 but do not follow DW_AT_GNU_dwo_name if present.
5193 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5194 to have already done the lookup to find the DWO/DWP file).
5196 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5197 THIS_CU->is_debug_types, but nothing else.
5199 We fill in THIS_CU->length.
5201 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5202 linker) then DIE_READER_FUNC will not get called.
5204 THIS_CU->cu is always freed when done.
5205 This is done in order to not leave THIS_CU->cu in a state where we have
5206 to care whether it refers to the "main" CU or the DWO CU. */
5209 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5210 struct dwarf2_section_info *abbrev_section,
5211 struct dwo_file *dwo_file,
5212 die_reader_func_ftype *die_reader_func,
5215 struct objfile *objfile = dwarf2_per_objfile->objfile;
5216 struct dwarf2_section_info *section = this_cu->section;
5217 bfd *abfd = section->asection->owner;
5218 struct dwarf2_cu cu;
5219 const gdb_byte *begin_info_ptr, *info_ptr;
5220 struct die_reader_specs reader;
5221 struct cleanup *cleanups;
5222 struct die_info *comp_unit_die;
5225 if (dwarf2_die_debug)
5226 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5227 this_cu->is_debug_types ? "type" : "comp",
5228 this_cu->offset.sect_off);
5230 gdb_assert (this_cu->cu == NULL);
5232 /* This is cheap if the section is already read in. */
5233 dwarf2_read_section (objfile, section);
5235 init_one_comp_unit (&cu, this_cu);
5237 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5239 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5240 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5241 abbrev_section, info_ptr,
5242 this_cu->is_debug_types);
5244 this_cu->length = get_cu_length (&cu.header);
5246 /* Skip dummy compilation units. */
5247 if (info_ptr >= begin_info_ptr + this_cu->length
5248 || peek_abbrev_code (abfd, info_ptr) == 0)
5250 do_cleanups (cleanups);
5254 dwarf2_read_abbrevs (&cu, abbrev_section);
5255 make_cleanup (dwarf2_free_abbrev_table, &cu);
5257 init_cu_die_reader (&reader, &cu, section, dwo_file);
5258 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5260 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5262 do_cleanups (cleanups);
5265 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5266 does not lookup the specified DWO file.
5267 This cannot be used to read DWO files.
5269 THIS_CU->cu is always freed when done.
5270 This is done in order to not leave THIS_CU->cu in a state where we have
5271 to care whether it refers to the "main" CU or the DWO CU.
5272 We can revisit this if the data shows there's a performance issue. */
5275 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5276 die_reader_func_ftype *die_reader_func,
5279 init_cutu_and_read_dies_no_follow (this_cu,
5280 get_abbrev_section_for_cu (this_cu),
5282 die_reader_func, data);
5285 /* Type Unit Groups.
5287 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5288 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5289 so that all types coming from the same compilation (.o file) are grouped
5290 together. A future step could be to put the types in the same symtab as
5291 the CU the types ultimately came from. */
5294 hash_type_unit_group (const void *item)
5296 const struct type_unit_group *tu_group = item;
5298 return hash_stmt_list_entry (&tu_group->hash);
5302 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5304 const struct type_unit_group *lhs = item_lhs;
5305 const struct type_unit_group *rhs = item_rhs;
5307 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5310 /* Allocate a hash table for type unit groups. */
5313 allocate_type_unit_groups_table (void)
5315 return htab_create_alloc_ex (3,
5316 hash_type_unit_group,
5319 &dwarf2_per_objfile->objfile->objfile_obstack,
5320 hashtab_obstack_allocate,
5321 dummy_obstack_deallocate);
5324 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5325 partial symtabs. We combine several TUs per psymtab to not let the size
5326 of any one psymtab grow too big. */
5327 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5328 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5330 /* Helper routine for get_type_unit_group.
5331 Create the type_unit_group object used to hold one or more TUs. */
5333 static struct type_unit_group *
5334 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5336 struct objfile *objfile = dwarf2_per_objfile->objfile;
5337 struct dwarf2_per_cu_data *per_cu;
5338 struct type_unit_group *tu_group;
5340 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5341 struct type_unit_group);
5342 per_cu = &tu_group->per_cu;
5343 per_cu->objfile = objfile;
5345 if (dwarf2_per_objfile->using_index)
5347 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5348 struct dwarf2_per_cu_quick_data);
5352 unsigned int line_offset = line_offset_struct.sect_off;
5353 struct partial_symtab *pst;
5356 /* Give the symtab a useful name for debug purposes. */
5357 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5358 name = xstrprintf ("<type_units_%d>",
5359 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5361 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5363 pst = create_partial_symtab (per_cu, name);
5369 tu_group->hash.dwo_unit = cu->dwo_unit;
5370 tu_group->hash.line_offset = line_offset_struct;
5375 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5376 STMT_LIST is a DW_AT_stmt_list attribute. */
5378 static struct type_unit_group *
5379 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5381 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5382 struct type_unit_group *tu_group;
5384 unsigned int line_offset;
5385 struct type_unit_group type_unit_group_for_lookup;
5387 if (dwarf2_per_objfile->type_unit_groups == NULL)
5389 dwarf2_per_objfile->type_unit_groups =
5390 allocate_type_unit_groups_table ();
5393 /* Do we need to create a new group, or can we use an existing one? */
5397 line_offset = DW_UNSND (stmt_list);
5398 ++tu_stats->nr_symtab_sharers;
5402 /* Ugh, no stmt_list. Rare, but we have to handle it.
5403 We can do various things here like create one group per TU or
5404 spread them over multiple groups to split up the expansion work.
5405 To avoid worst case scenarios (too many groups or too large groups)
5406 we, umm, group them in bunches. */
5407 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5408 | (tu_stats->nr_stmt_less_type_units
5409 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5410 ++tu_stats->nr_stmt_less_type_units;
5413 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5414 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5415 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5416 &type_unit_group_for_lookup, INSERT);
5420 gdb_assert (tu_group != NULL);
5424 sect_offset line_offset_struct;
5426 line_offset_struct.sect_off = line_offset;
5427 tu_group = create_type_unit_group (cu, line_offset_struct);
5429 ++tu_stats->nr_symtabs;
5435 /* Struct used to sort TUs by their abbreviation table offset. */
5437 struct tu_abbrev_offset
5439 struct signatured_type *sig_type;
5440 sect_offset abbrev_offset;
5443 /* Helper routine for build_type_unit_groups, passed to qsort. */
5446 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5448 const struct tu_abbrev_offset * const *a = ap;
5449 const struct tu_abbrev_offset * const *b = bp;
5450 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5451 unsigned int boff = (*b)->abbrev_offset.sect_off;
5453 return (aoff > boff) - (aoff < boff);
5456 /* A helper function to add a type_unit_group to a table. */
5459 add_type_unit_group_to_table (void **slot, void *datum)
5461 struct type_unit_group *tu_group = *slot;
5462 struct type_unit_group ***datap = datum;
5470 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5471 each one passing FUNC,DATA.
5473 The efficiency is because we sort TUs by the abbrev table they use and
5474 only read each abbrev table once. In one program there are 200K TUs
5475 sharing 8K abbrev tables.
5477 The main purpose of this function is to support building the
5478 dwarf2_per_objfile->type_unit_groups table.
5479 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5480 can collapse the search space by grouping them by stmt_list.
5481 The savings can be significant, in the same program from above the 200K TUs
5482 share 8K stmt_list tables.
5484 FUNC is expected to call get_type_unit_group, which will create the
5485 struct type_unit_group if necessary and add it to
5486 dwarf2_per_objfile->type_unit_groups. */
5489 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5491 struct objfile *objfile = dwarf2_per_objfile->objfile;
5492 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5493 struct cleanup *cleanups;
5494 struct abbrev_table *abbrev_table;
5495 sect_offset abbrev_offset;
5496 struct tu_abbrev_offset *sorted_by_abbrev;
5497 struct type_unit_group **iter;
5500 /* It's up to the caller to not call us multiple times. */
5501 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5503 if (dwarf2_per_objfile->n_type_units == 0)
5506 /* TUs typically share abbrev tables, and there can be way more TUs than
5507 abbrev tables. Sort by abbrev table to reduce the number of times we
5508 read each abbrev table in.
5509 Alternatives are to punt or to maintain a cache of abbrev tables.
5510 This is simpler and efficient enough for now.
5512 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5513 symtab to use). Typically TUs with the same abbrev offset have the same
5514 stmt_list value too so in practice this should work well.
5516 The basic algorithm here is:
5518 sort TUs by abbrev table
5519 for each TU with same abbrev table:
5520 read abbrev table if first user
5521 read TU top level DIE
5522 [IWBN if DWO skeletons had DW_AT_stmt_list]
5525 if (dwarf2_read_debug)
5526 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5528 /* Sort in a separate table to maintain the order of all_type_units
5529 for .gdb_index: TU indices directly index all_type_units. */
5530 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5531 dwarf2_per_objfile->n_type_units);
5532 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5534 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5536 sorted_by_abbrev[i].sig_type = sig_type;
5537 sorted_by_abbrev[i].abbrev_offset =
5538 read_abbrev_offset (sig_type->per_cu.section,
5539 sig_type->per_cu.offset);
5541 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5542 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5543 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5545 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5546 called any number of times, so we don't reset tu_stats here. */
5548 abbrev_offset.sect_off = ~(unsigned) 0;
5549 abbrev_table = NULL;
5550 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5552 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5554 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5556 /* Switch to the next abbrev table if necessary. */
5557 if (abbrev_table == NULL
5558 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5560 if (abbrev_table != NULL)
5562 abbrev_table_free (abbrev_table);
5563 /* Reset to NULL in case abbrev_table_read_table throws
5564 an error: abbrev_table_free_cleanup will get called. */
5565 abbrev_table = NULL;
5567 abbrev_offset = tu->abbrev_offset;
5569 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5571 ++tu_stats->nr_uniq_abbrev_tables;
5574 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5578 /* type_unit_groups can be NULL if there is an error in the debug info.
5579 Just create an empty table so the rest of gdb doesn't have to watch
5580 for this error case. */
5581 if (dwarf2_per_objfile->type_unit_groups == NULL)
5583 dwarf2_per_objfile->type_unit_groups =
5584 allocate_type_unit_groups_table ();
5585 dwarf2_per_objfile->n_type_unit_groups = 0;
5588 /* Create a vector of pointers to primary type units to make it easy to
5589 iterate over them and CUs. See dw2_get_primary_cu. */
5590 dwarf2_per_objfile->n_type_unit_groups =
5591 htab_elements (dwarf2_per_objfile->type_unit_groups);
5592 dwarf2_per_objfile->all_type_unit_groups =
5593 obstack_alloc (&objfile->objfile_obstack,
5594 dwarf2_per_objfile->n_type_unit_groups
5595 * sizeof (struct type_unit_group *));
5596 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5597 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5598 add_type_unit_group_to_table, &iter);
5599 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5600 == dwarf2_per_objfile->n_type_unit_groups);
5602 do_cleanups (cleanups);
5604 if (dwarf2_read_debug)
5606 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5607 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5608 dwarf2_per_objfile->n_type_units);
5609 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5610 tu_stats->nr_uniq_abbrev_tables);
5611 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5612 tu_stats->nr_symtabs);
5613 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5614 tu_stats->nr_symtab_sharers);
5615 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5616 tu_stats->nr_stmt_less_type_units);
5620 /* Partial symbol tables. */
5622 /* Create a psymtab named NAME and assign it to PER_CU.
5624 The caller must fill in the following details:
5625 dirname, textlow, texthigh. */
5627 static struct partial_symtab *
5628 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5630 struct objfile *objfile = per_cu->objfile;
5631 struct partial_symtab *pst;
5633 pst = start_psymtab_common (objfile, objfile->section_offsets,
5635 objfile->global_psymbols.next,
5636 objfile->static_psymbols.next);
5638 pst->psymtabs_addrmap_supported = 1;
5640 /* This is the glue that links PST into GDB's symbol API. */
5641 pst->read_symtab_private = per_cu;
5642 pst->read_symtab = dwarf2_read_symtab;
5643 per_cu->v.psymtab = pst;
5648 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5651 struct process_psymtab_comp_unit_data
5653 /* True if we are reading a DW_TAG_partial_unit. */
5655 int want_partial_unit;
5657 /* The "pretend" language that is used if the CU doesn't declare a
5660 enum language pretend_language;
5663 /* die_reader_func for process_psymtab_comp_unit. */
5666 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5667 const gdb_byte *info_ptr,
5668 struct die_info *comp_unit_die,
5672 struct dwarf2_cu *cu = reader->cu;
5673 struct objfile *objfile = cu->objfile;
5674 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5675 struct attribute *attr;
5677 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5678 struct partial_symtab *pst;
5680 const char *filename;
5681 struct process_psymtab_comp_unit_data *info = data;
5683 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5686 gdb_assert (! per_cu->is_debug_types);
5688 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5690 cu->list_in_scope = &file_symbols;
5692 /* Allocate a new partial symbol table structure. */
5693 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5694 if (attr == NULL || !DW_STRING (attr))
5697 filename = DW_STRING (attr);
5699 pst = create_partial_symtab (per_cu, filename);
5701 /* This must be done before calling dwarf2_build_include_psymtabs. */
5702 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5704 pst->dirname = DW_STRING (attr);
5706 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5708 dwarf2_find_base_address (comp_unit_die, cu);
5710 /* Possibly set the default values of LOWPC and HIGHPC from
5712 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5713 &best_highpc, cu, pst);
5714 if (has_pc_info == 1 && best_lowpc < best_highpc)
5715 /* Store the contiguous range if it is not empty; it can be empty for
5716 CUs with no code. */
5717 addrmap_set_empty (objfile->psymtabs_addrmap,
5718 best_lowpc + baseaddr,
5719 best_highpc + baseaddr - 1, pst);
5721 /* Check if comp unit has_children.
5722 If so, read the rest of the partial symbols from this comp unit.
5723 If not, there's no more debug_info for this comp unit. */
5726 struct partial_die_info *first_die;
5727 CORE_ADDR lowpc, highpc;
5729 lowpc = ((CORE_ADDR) -1);
5730 highpc = ((CORE_ADDR) 0);
5732 first_die = load_partial_dies (reader, info_ptr, 1);
5734 scan_partial_symbols (first_die, &lowpc, &highpc,
5737 /* If we didn't find a lowpc, set it to highpc to avoid
5738 complaints from `maint check'. */
5739 if (lowpc == ((CORE_ADDR) -1))
5742 /* If the compilation unit didn't have an explicit address range,
5743 then use the information extracted from its child dies. */
5747 best_highpc = highpc;
5750 pst->textlow = best_lowpc + baseaddr;
5751 pst->texthigh = best_highpc + baseaddr;
5753 pst->n_global_syms = objfile->global_psymbols.next -
5754 (objfile->global_psymbols.list + pst->globals_offset);
5755 pst->n_static_syms = objfile->static_psymbols.next -
5756 (objfile->static_psymbols.list + pst->statics_offset);
5757 sort_pst_symbols (objfile, pst);
5759 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5762 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5763 struct dwarf2_per_cu_data *iter;
5765 /* Fill in 'dependencies' here; we fill in 'users' in a
5767 pst->number_of_dependencies = len;
5768 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5769 len * sizeof (struct symtab *));
5771 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5774 pst->dependencies[i] = iter->v.psymtab;
5776 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5779 /* Get the list of files included in the current compilation unit,
5780 and build a psymtab for each of them. */
5781 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5783 if (dwarf2_read_debug)
5785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5787 fprintf_unfiltered (gdb_stdlog,
5788 "Psymtab for %s unit @0x%x: %s - %s"
5789 ", %d global, %d static syms\n",
5790 per_cu->is_debug_types ? "type" : "comp",
5791 per_cu->offset.sect_off,
5792 paddress (gdbarch, pst->textlow),
5793 paddress (gdbarch, pst->texthigh),
5794 pst->n_global_syms, pst->n_static_syms);
5798 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5799 Process compilation unit THIS_CU for a psymtab. */
5802 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5803 int want_partial_unit,
5804 enum language pretend_language)
5806 struct process_psymtab_comp_unit_data info;
5808 /* If this compilation unit was already read in, free the
5809 cached copy in order to read it in again. This is
5810 necessary because we skipped some symbols when we first
5811 read in the compilation unit (see load_partial_dies).
5812 This problem could be avoided, but the benefit is unclear. */
5813 if (this_cu->cu != NULL)
5814 free_one_cached_comp_unit (this_cu);
5816 gdb_assert (! this_cu->is_debug_types);
5817 info.want_partial_unit = want_partial_unit;
5818 info.pretend_language = pretend_language;
5819 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5820 process_psymtab_comp_unit_reader,
5823 /* Age out any secondary CUs. */
5824 age_cached_comp_units ();
5827 /* Reader function for build_type_psymtabs. */
5830 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5831 const gdb_byte *info_ptr,
5832 struct die_info *type_unit_die,
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 struct dwarf2_cu *cu = reader->cu;
5838 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5839 struct signatured_type *sig_type;
5840 struct type_unit_group *tu_group;
5841 struct attribute *attr;
5842 struct partial_die_info *first_die;
5843 CORE_ADDR lowpc, highpc;
5844 struct partial_symtab *pst;
5846 gdb_assert (data == NULL);
5847 gdb_assert (per_cu->is_debug_types);
5848 sig_type = (struct signatured_type *) per_cu;
5853 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5854 tu_group = get_type_unit_group (cu, attr);
5856 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5858 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5859 cu->list_in_scope = &file_symbols;
5860 pst = create_partial_symtab (per_cu, "");
5863 first_die = load_partial_dies (reader, info_ptr, 1);
5865 lowpc = (CORE_ADDR) -1;
5866 highpc = (CORE_ADDR) 0;
5867 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5869 pst->n_global_syms = objfile->global_psymbols.next -
5870 (objfile->global_psymbols.list + pst->globals_offset);
5871 pst->n_static_syms = objfile->static_psymbols.next -
5872 (objfile->static_psymbols.list + pst->statics_offset);
5873 sort_pst_symbols (objfile, pst);
5876 /* Traversal function for build_type_psymtabs. */
5879 build_type_psymtab_dependencies (void **slot, void *info)
5881 struct objfile *objfile = dwarf2_per_objfile->objfile;
5882 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5883 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5884 struct partial_symtab *pst = per_cu->v.psymtab;
5885 int len = VEC_length (sig_type_ptr, tu_group->tus);
5886 struct signatured_type *iter;
5889 gdb_assert (len > 0);
5890 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5892 pst->number_of_dependencies = len;
5893 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5894 len * sizeof (struct psymtab *));
5896 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5899 gdb_assert (iter->per_cu.is_debug_types);
5900 pst->dependencies[i] = iter->per_cu.v.psymtab;
5901 iter->type_unit_group = tu_group;
5904 VEC_free (sig_type_ptr, tu_group->tus);
5909 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5910 Build partial symbol tables for the .debug_types comp-units. */
5913 build_type_psymtabs (struct objfile *objfile)
5915 if (! create_all_type_units (objfile))
5918 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5920 /* Now that all TUs have been processed we can fill in the dependencies. */
5921 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5922 build_type_psymtab_dependencies, NULL);
5925 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5928 psymtabs_addrmap_cleanup (void *o)
5930 struct objfile *objfile = o;
5932 objfile->psymtabs_addrmap = NULL;
5935 /* Compute the 'user' field for each psymtab in OBJFILE. */
5938 set_partial_user (struct objfile *objfile)
5942 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5944 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5945 struct partial_symtab *pst = per_cu->v.psymtab;
5951 for (j = 0; j < pst->number_of_dependencies; ++j)
5953 /* Set the 'user' field only if it is not already set. */
5954 if (pst->dependencies[j]->user == NULL)
5955 pst->dependencies[j]->user = pst;
5960 /* Build the partial symbol table by doing a quick pass through the
5961 .debug_info and .debug_abbrev sections. */
5964 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5966 struct cleanup *back_to, *addrmap_cleanup;
5967 struct obstack temp_obstack;
5970 if (dwarf2_read_debug)
5972 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5976 dwarf2_per_objfile->reading_partial_symbols = 1;
5978 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5980 /* Any cached compilation units will be linked by the per-objfile
5981 read_in_chain. Make sure to free them when we're done. */
5982 back_to = make_cleanup (free_cached_comp_units, NULL);
5984 build_type_psymtabs (objfile);
5986 create_all_comp_units (objfile);
5988 /* Create a temporary address map on a temporary obstack. We later
5989 copy this to the final obstack. */
5990 obstack_init (&temp_obstack);
5991 make_cleanup_obstack_free (&temp_obstack);
5992 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5993 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5995 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5997 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5999 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6002 set_partial_user (objfile);
6004 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6005 &objfile->objfile_obstack);
6006 discard_cleanups (addrmap_cleanup);
6008 do_cleanups (back_to);
6010 if (dwarf2_read_debug)
6011 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6015 /* die_reader_func for load_partial_comp_unit. */
6018 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6019 const gdb_byte *info_ptr,
6020 struct die_info *comp_unit_die,
6024 struct dwarf2_cu *cu = reader->cu;
6026 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6028 /* Check if comp unit has_children.
6029 If so, read the rest of the partial symbols from this comp unit.
6030 If not, there's no more debug_info for this comp unit. */
6032 load_partial_dies (reader, info_ptr, 0);
6035 /* Load the partial DIEs for a secondary CU into memory.
6036 This is also used when rereading a primary CU with load_all_dies. */
6039 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6041 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6042 load_partial_comp_unit_reader, NULL);
6046 read_comp_units_from_section (struct objfile *objfile,
6047 struct dwarf2_section_info *section,
6048 unsigned int is_dwz,
6051 struct dwarf2_per_cu_data ***all_comp_units)
6053 const gdb_byte *info_ptr;
6054 bfd *abfd = section->asection->owner;
6056 if (dwarf2_read_debug)
6057 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6058 section->asection->name, bfd_get_filename (abfd));
6060 dwarf2_read_section (objfile, section);
6062 info_ptr = section->buffer;
6064 while (info_ptr < section->buffer + section->size)
6066 unsigned int length, initial_length_size;
6067 struct dwarf2_per_cu_data *this_cu;
6070 offset.sect_off = info_ptr - section->buffer;
6072 /* Read just enough information to find out where the next
6073 compilation unit is. */
6074 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6076 /* Save the compilation unit for later lookup. */
6077 this_cu = obstack_alloc (&objfile->objfile_obstack,
6078 sizeof (struct dwarf2_per_cu_data));
6079 memset (this_cu, 0, sizeof (*this_cu));
6080 this_cu->offset = offset;
6081 this_cu->length = length + initial_length_size;
6082 this_cu->is_dwz = is_dwz;
6083 this_cu->objfile = objfile;
6084 this_cu->section = section;
6086 if (*n_comp_units == *n_allocated)
6089 *all_comp_units = xrealloc (*all_comp_units,
6091 * sizeof (struct dwarf2_per_cu_data *));
6093 (*all_comp_units)[*n_comp_units] = this_cu;
6096 info_ptr = info_ptr + this_cu->length;
6100 /* Create a list of all compilation units in OBJFILE.
6101 This is only done for -readnow and building partial symtabs. */
6104 create_all_comp_units (struct objfile *objfile)
6108 struct dwarf2_per_cu_data **all_comp_units;
6109 struct dwz_file *dwz;
6113 all_comp_units = xmalloc (n_allocated
6114 * sizeof (struct dwarf2_per_cu_data *));
6116 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6117 &n_allocated, &n_comp_units, &all_comp_units);
6119 dwz = dwarf2_get_dwz_file ();
6121 read_comp_units_from_section (objfile, &dwz->info, 1,
6122 &n_allocated, &n_comp_units,
6125 dwarf2_per_objfile->all_comp_units
6126 = obstack_alloc (&objfile->objfile_obstack,
6127 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6128 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6129 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6130 xfree (all_comp_units);
6131 dwarf2_per_objfile->n_comp_units = n_comp_units;
6134 /* Process all loaded DIEs for compilation unit CU, starting at
6135 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6136 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6137 DW_AT_ranges). If NEED_PC is set, then this function will set
6138 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6139 and record the covered ranges in the addrmap. */
6142 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6143 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6145 struct partial_die_info *pdi;
6147 /* Now, march along the PDI's, descending into ones which have
6148 interesting children but skipping the children of the other ones,
6149 until we reach the end of the compilation unit. */
6155 fixup_partial_die (pdi, cu);
6157 /* Anonymous namespaces or modules have no name but have interesting
6158 children, so we need to look at them. Ditto for anonymous
6161 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6162 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6163 || pdi->tag == DW_TAG_imported_unit)
6167 case DW_TAG_subprogram:
6168 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6170 case DW_TAG_constant:
6171 case DW_TAG_variable:
6172 case DW_TAG_typedef:
6173 case DW_TAG_union_type:
6174 if (!pdi->is_declaration)
6176 add_partial_symbol (pdi, cu);
6179 case DW_TAG_class_type:
6180 case DW_TAG_interface_type:
6181 case DW_TAG_structure_type:
6182 if (!pdi->is_declaration)
6184 add_partial_symbol (pdi, cu);
6187 case DW_TAG_enumeration_type:
6188 if (!pdi->is_declaration)
6189 add_partial_enumeration (pdi, cu);
6191 case DW_TAG_base_type:
6192 case DW_TAG_subrange_type:
6193 /* File scope base type definitions are added to the partial
6195 add_partial_symbol (pdi, cu);
6197 case DW_TAG_namespace:
6198 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
6201 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6203 case DW_TAG_imported_unit:
6205 struct dwarf2_per_cu_data *per_cu;
6207 /* For now we don't handle imported units in type units. */
6208 if (cu->per_cu->is_debug_types)
6210 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6211 " supported in type units [in module %s]"),
6215 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6219 /* Go read the partial unit, if needed. */
6220 if (per_cu->v.psymtab == NULL)
6221 process_psymtab_comp_unit (per_cu, 1, cu->language);
6223 VEC_safe_push (dwarf2_per_cu_ptr,
6224 cu->per_cu->imported_symtabs, per_cu);
6232 /* If the die has a sibling, skip to the sibling. */
6234 pdi = pdi->die_sibling;
6238 /* Functions used to compute the fully scoped name of a partial DIE.
6240 Normally, this is simple. For C++, the parent DIE's fully scoped
6241 name is concatenated with "::" and the partial DIE's name. For
6242 Java, the same thing occurs except that "." is used instead of "::".
6243 Enumerators are an exception; they use the scope of their parent
6244 enumeration type, i.e. the name of the enumeration type is not
6245 prepended to the enumerator.
6247 There are two complexities. One is DW_AT_specification; in this
6248 case "parent" means the parent of the target of the specification,
6249 instead of the direct parent of the DIE. The other is compilers
6250 which do not emit DW_TAG_namespace; in this case we try to guess
6251 the fully qualified name of structure types from their members'
6252 linkage names. This must be done using the DIE's children rather
6253 than the children of any DW_AT_specification target. We only need
6254 to do this for structures at the top level, i.e. if the target of
6255 any DW_AT_specification (if any; otherwise the DIE itself) does not
6258 /* Compute the scope prefix associated with PDI's parent, in
6259 compilation unit CU. The result will be allocated on CU's
6260 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6261 field. NULL is returned if no prefix is necessary. */
6263 partial_die_parent_scope (struct partial_die_info *pdi,
6264 struct dwarf2_cu *cu)
6266 const char *grandparent_scope;
6267 struct partial_die_info *parent, *real_pdi;
6269 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6270 then this means the parent of the specification DIE. */
6273 while (real_pdi->has_specification)
6274 real_pdi = find_partial_die (real_pdi->spec_offset,
6275 real_pdi->spec_is_dwz, cu);
6277 parent = real_pdi->die_parent;
6281 if (parent->scope_set)
6282 return parent->scope;
6284 fixup_partial_die (parent, cu);
6286 grandparent_scope = partial_die_parent_scope (parent, cu);
6288 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6289 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6290 Work around this problem here. */
6291 if (cu->language == language_cplus
6292 && parent->tag == DW_TAG_namespace
6293 && strcmp (parent->name, "::") == 0
6294 && grandparent_scope == NULL)
6296 parent->scope = NULL;
6297 parent->scope_set = 1;
6301 if (pdi->tag == DW_TAG_enumerator)
6302 /* Enumerators should not get the name of the enumeration as a prefix. */
6303 parent->scope = grandparent_scope;
6304 else if (parent->tag == DW_TAG_namespace
6305 || parent->tag == DW_TAG_module
6306 || parent->tag == DW_TAG_structure_type
6307 || parent->tag == DW_TAG_class_type
6308 || parent->tag == DW_TAG_interface_type
6309 || parent->tag == DW_TAG_union_type
6310 || parent->tag == DW_TAG_enumeration_type)
6312 if (grandparent_scope == NULL)
6313 parent->scope = parent->name;
6315 parent->scope = typename_concat (&cu->comp_unit_obstack,
6317 parent->name, 0, cu);
6321 /* FIXME drow/2004-04-01: What should we be doing with
6322 function-local names? For partial symbols, we should probably be
6324 complaint (&symfile_complaints,
6325 _("unhandled containing DIE tag %d for DIE at %d"),
6326 parent->tag, pdi->offset.sect_off);
6327 parent->scope = grandparent_scope;
6330 parent->scope_set = 1;
6331 return parent->scope;
6334 /* Return the fully scoped name associated with PDI, from compilation unit
6335 CU. The result will be allocated with malloc. */
6338 partial_die_full_name (struct partial_die_info *pdi,
6339 struct dwarf2_cu *cu)
6341 const char *parent_scope;
6343 /* If this is a template instantiation, we can not work out the
6344 template arguments from partial DIEs. So, unfortunately, we have
6345 to go through the full DIEs. At least any work we do building
6346 types here will be reused if full symbols are loaded later. */
6347 if (pdi->has_template_arguments)
6349 fixup_partial_die (pdi, cu);
6351 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6353 struct die_info *die;
6354 struct attribute attr;
6355 struct dwarf2_cu *ref_cu = cu;
6357 /* DW_FORM_ref_addr is using section offset. */
6359 attr.form = DW_FORM_ref_addr;
6360 attr.u.unsnd = pdi->offset.sect_off;
6361 die = follow_die_ref (NULL, &attr, &ref_cu);
6363 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6367 parent_scope = partial_die_parent_scope (pdi, cu);
6368 if (parent_scope == NULL)
6371 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6375 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6377 struct objfile *objfile = cu->objfile;
6379 const char *actual_name = NULL;
6381 char *built_actual_name;
6383 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6385 built_actual_name = partial_die_full_name (pdi, cu);
6386 if (built_actual_name != NULL)
6387 actual_name = built_actual_name;
6389 if (actual_name == NULL)
6390 actual_name = pdi->name;
6394 case DW_TAG_subprogram:
6395 if (pdi->is_external || cu->language == language_ada)
6397 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6398 of the global scope. But in Ada, we want to be able to access
6399 nested procedures globally. So all Ada subprograms are stored
6400 in the global scope. */
6401 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6402 mst_text, objfile); */
6403 add_psymbol_to_list (actual_name, strlen (actual_name),
6404 built_actual_name != NULL,
6405 VAR_DOMAIN, LOC_BLOCK,
6406 &objfile->global_psymbols,
6407 0, pdi->lowpc + baseaddr,
6408 cu->language, objfile);
6412 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6413 mst_file_text, objfile); */
6414 add_psymbol_to_list (actual_name, strlen (actual_name),
6415 built_actual_name != NULL,
6416 VAR_DOMAIN, LOC_BLOCK,
6417 &objfile->static_psymbols,
6418 0, pdi->lowpc + baseaddr,
6419 cu->language, objfile);
6422 case DW_TAG_constant:
6424 struct psymbol_allocation_list *list;
6426 if (pdi->is_external)
6427 list = &objfile->global_psymbols;
6429 list = &objfile->static_psymbols;
6430 add_psymbol_to_list (actual_name, strlen (actual_name),
6431 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6432 list, 0, 0, cu->language, objfile);
6435 case DW_TAG_variable:
6437 addr = decode_locdesc (pdi->d.locdesc, cu);
6441 && !dwarf2_per_objfile->has_section_at_zero)
6443 /* A global or static variable may also have been stripped
6444 out by the linker if unused, in which case its address
6445 will be nullified; do not add such variables into partial
6446 symbol table then. */
6448 else if (pdi->is_external)
6451 Don't enter into the minimal symbol tables as there is
6452 a minimal symbol table entry from the ELF symbols already.
6453 Enter into partial symbol table if it has a location
6454 descriptor or a type.
6455 If the location descriptor is missing, new_symbol will create
6456 a LOC_UNRESOLVED symbol, the address of the variable will then
6457 be determined from the minimal symbol table whenever the variable
6459 The address for the partial symbol table entry is not
6460 used by GDB, but it comes in handy for debugging partial symbol
6463 if (pdi->d.locdesc || pdi->has_type)
6464 add_psymbol_to_list (actual_name, strlen (actual_name),
6465 built_actual_name != NULL,
6466 VAR_DOMAIN, LOC_STATIC,
6467 &objfile->global_psymbols,
6469 cu->language, objfile);
6473 /* Static Variable. Skip symbols without location descriptors. */
6474 if (pdi->d.locdesc == NULL)
6476 xfree (built_actual_name);
6479 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6480 mst_file_data, objfile); */
6481 add_psymbol_to_list (actual_name, strlen (actual_name),
6482 built_actual_name != NULL,
6483 VAR_DOMAIN, LOC_STATIC,
6484 &objfile->static_psymbols,
6486 cu->language, objfile);
6489 case DW_TAG_typedef:
6490 case DW_TAG_base_type:
6491 case DW_TAG_subrange_type:
6492 add_psymbol_to_list (actual_name, strlen (actual_name),
6493 built_actual_name != NULL,
6494 VAR_DOMAIN, LOC_TYPEDEF,
6495 &objfile->static_psymbols,
6496 0, (CORE_ADDR) 0, cu->language, objfile);
6498 case DW_TAG_namespace:
6499 add_psymbol_to_list (actual_name, strlen (actual_name),
6500 built_actual_name != NULL,
6501 VAR_DOMAIN, LOC_TYPEDEF,
6502 &objfile->global_psymbols,
6503 0, (CORE_ADDR) 0, cu->language, objfile);
6505 case DW_TAG_class_type:
6506 case DW_TAG_interface_type:
6507 case DW_TAG_structure_type:
6508 case DW_TAG_union_type:
6509 case DW_TAG_enumeration_type:
6510 /* Skip external references. The DWARF standard says in the section
6511 about "Structure, Union, and Class Type Entries": "An incomplete
6512 structure, union or class type is represented by a structure,
6513 union or class entry that does not have a byte size attribute
6514 and that has a DW_AT_declaration attribute." */
6515 if (!pdi->has_byte_size && pdi->is_declaration)
6517 xfree (built_actual_name);
6521 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6522 static vs. global. */
6523 add_psymbol_to_list (actual_name, strlen (actual_name),
6524 built_actual_name != NULL,
6525 STRUCT_DOMAIN, LOC_TYPEDEF,
6526 (cu->language == language_cplus
6527 || cu->language == language_java)
6528 ? &objfile->global_psymbols
6529 : &objfile->static_psymbols,
6530 0, (CORE_ADDR) 0, cu->language, objfile);
6533 case DW_TAG_enumerator:
6534 add_psymbol_to_list (actual_name, strlen (actual_name),
6535 built_actual_name != NULL,
6536 VAR_DOMAIN, LOC_CONST,
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);
6547 xfree (built_actual_name);
6550 /* Read a partial die corresponding to a namespace; also, add a symbol
6551 corresponding to that namespace to the symbol table. NAMESPACE is
6552 the name of the enclosing namespace. */
6555 add_partial_namespace (struct partial_die_info *pdi,
6556 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6557 int need_pc, struct dwarf2_cu *cu)
6559 /* Add a symbol for the namespace. */
6561 add_partial_symbol (pdi, cu);
6563 /* Now scan partial symbols in that namespace. */
6565 if (pdi->has_children)
6566 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6569 /* Read a partial die corresponding to a Fortran module. */
6572 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6573 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6575 /* Now scan partial symbols in that module. */
6577 if (pdi->has_children)
6578 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6581 /* Read a partial die corresponding to a subprogram and create a partial
6582 symbol for that subprogram. When the CU language allows it, this
6583 routine also defines a partial symbol for each nested subprogram
6584 that this subprogram contains.
6586 DIE my also be a lexical block, in which case we simply search
6587 recursively for suprograms defined inside that lexical block.
6588 Again, this is only performed when the CU language allows this
6589 type of definitions. */
6592 add_partial_subprogram (struct partial_die_info *pdi,
6593 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6594 int need_pc, struct dwarf2_cu *cu)
6596 if (pdi->tag == DW_TAG_subprogram)
6598 if (pdi->has_pc_info)
6600 if (pdi->lowpc < *lowpc)
6601 *lowpc = pdi->lowpc;
6602 if (pdi->highpc > *highpc)
6603 *highpc = pdi->highpc;
6607 struct objfile *objfile = cu->objfile;
6609 baseaddr = ANOFFSET (objfile->section_offsets,
6610 SECT_OFF_TEXT (objfile));
6611 addrmap_set_empty (objfile->psymtabs_addrmap,
6612 pdi->lowpc + baseaddr,
6613 pdi->highpc - 1 + baseaddr,
6614 cu->per_cu->v.psymtab);
6618 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6620 if (!pdi->is_declaration)
6621 /* Ignore subprogram DIEs that do not have a name, they are
6622 illegal. Do not emit a complaint at this point, we will
6623 do so when we convert this psymtab into a symtab. */
6625 add_partial_symbol (pdi, cu);
6629 if (! pdi->has_children)
6632 if (cu->language == language_ada)
6634 pdi = pdi->die_child;
6637 fixup_partial_die (pdi, cu);
6638 if (pdi->tag == DW_TAG_subprogram
6639 || pdi->tag == DW_TAG_lexical_block)
6640 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6641 pdi = pdi->die_sibling;
6646 /* Read a partial die corresponding to an enumeration type. */
6649 add_partial_enumeration (struct partial_die_info *enum_pdi,
6650 struct dwarf2_cu *cu)
6652 struct partial_die_info *pdi;
6654 if (enum_pdi->name != NULL)
6655 add_partial_symbol (enum_pdi, cu);
6657 pdi = enum_pdi->die_child;
6660 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6661 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6663 add_partial_symbol (pdi, cu);
6664 pdi = pdi->die_sibling;
6668 /* Return the initial uleb128 in the die at INFO_PTR. */
6671 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6673 unsigned int bytes_read;
6675 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6678 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6679 Return the corresponding abbrev, or NULL if the number is zero (indicating
6680 an empty DIE). In either case *BYTES_READ will be set to the length of
6681 the initial number. */
6683 static struct abbrev_info *
6684 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
6685 struct dwarf2_cu *cu)
6687 bfd *abfd = cu->objfile->obfd;
6688 unsigned int abbrev_number;
6689 struct abbrev_info *abbrev;
6691 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6693 if (abbrev_number == 0)
6696 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6699 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6700 abbrev_number, bfd_get_filename (abfd));
6706 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6707 Returns a pointer to the end of a series of DIEs, terminated by an empty
6708 DIE. Any children of the skipped DIEs will also be skipped. */
6710 static const gdb_byte *
6711 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
6713 struct dwarf2_cu *cu = reader->cu;
6714 struct abbrev_info *abbrev;
6715 unsigned int bytes_read;
6719 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6721 return info_ptr + bytes_read;
6723 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6727 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6728 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6729 abbrev corresponding to that skipped uleb128 should be passed in
6730 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6733 static const gdb_byte *
6734 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
6735 struct abbrev_info *abbrev)
6737 unsigned int bytes_read;
6738 struct attribute attr;
6739 bfd *abfd = reader->abfd;
6740 struct dwarf2_cu *cu = reader->cu;
6741 const gdb_byte *buffer = reader->buffer;
6742 const gdb_byte *buffer_end = reader->buffer_end;
6743 const gdb_byte *start_info_ptr = info_ptr;
6744 unsigned int form, i;
6746 for (i = 0; i < abbrev->num_attrs; i++)
6748 /* The only abbrev we care about is DW_AT_sibling. */
6749 if (abbrev->attrs[i].name == DW_AT_sibling)
6751 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6752 if (attr.form == DW_FORM_ref_addr)
6753 complaint (&symfile_complaints,
6754 _("ignoring absolute DW_AT_sibling"));
6756 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6759 /* If it isn't DW_AT_sibling, skip this attribute. */
6760 form = abbrev->attrs[i].form;
6764 case DW_FORM_ref_addr:
6765 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6766 and later it is offset sized. */
6767 if (cu->header.version == 2)
6768 info_ptr += cu->header.addr_size;
6770 info_ptr += cu->header.offset_size;
6772 case DW_FORM_GNU_ref_alt:
6773 info_ptr += cu->header.offset_size;
6776 info_ptr += cu->header.addr_size;
6783 case DW_FORM_flag_present:
6795 case DW_FORM_ref_sig8:
6798 case DW_FORM_string:
6799 read_direct_string (abfd, info_ptr, &bytes_read);
6800 info_ptr += bytes_read;
6802 case DW_FORM_sec_offset:
6804 case DW_FORM_GNU_strp_alt:
6805 info_ptr += cu->header.offset_size;
6807 case DW_FORM_exprloc:
6809 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6810 info_ptr += bytes_read;
6812 case DW_FORM_block1:
6813 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6815 case DW_FORM_block2:
6816 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6818 case DW_FORM_block4:
6819 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6823 case DW_FORM_ref_udata:
6824 case DW_FORM_GNU_addr_index:
6825 case DW_FORM_GNU_str_index:
6826 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
6828 case DW_FORM_indirect:
6829 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6830 info_ptr += bytes_read;
6831 /* We need to continue parsing from here, so just go back to
6833 goto skip_attribute;
6836 error (_("Dwarf Error: Cannot handle %s "
6837 "in DWARF reader [in module %s]"),
6838 dwarf_form_name (form),
6839 bfd_get_filename (abfd));
6843 if (abbrev->has_children)
6844 return skip_children (reader, info_ptr);
6849 /* Locate ORIG_PDI's sibling.
6850 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6852 static const gdb_byte *
6853 locate_pdi_sibling (const struct die_reader_specs *reader,
6854 struct partial_die_info *orig_pdi,
6855 const gdb_byte *info_ptr)
6857 /* Do we know the sibling already? */
6859 if (orig_pdi->sibling)
6860 return orig_pdi->sibling;
6862 /* Are there any children to deal with? */
6864 if (!orig_pdi->has_children)
6867 /* Skip the children the long way. */
6869 return skip_children (reader, info_ptr);
6872 /* Expand this partial symbol table into a full symbol table. SELF is
6876 dwarf2_read_symtab (struct partial_symtab *self,
6877 struct objfile *objfile)
6881 warning (_("bug: psymtab for %s is already read in."),
6888 printf_filtered (_("Reading in symbols for %s..."),
6890 gdb_flush (gdb_stdout);
6893 /* Restore our global data. */
6894 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6896 /* If this psymtab is constructed from a debug-only objfile, the
6897 has_section_at_zero flag will not necessarily be correct. We
6898 can get the correct value for this flag by looking at the data
6899 associated with the (presumably stripped) associated objfile. */
6900 if (objfile->separate_debug_objfile_backlink)
6902 struct dwarf2_per_objfile *dpo_backlink
6903 = objfile_data (objfile->separate_debug_objfile_backlink,
6904 dwarf2_objfile_data_key);
6906 dwarf2_per_objfile->has_section_at_zero
6907 = dpo_backlink->has_section_at_zero;
6910 dwarf2_per_objfile->reading_partial_symbols = 0;
6912 psymtab_to_symtab_1 (self);
6914 /* Finish up the debug error message. */
6916 printf_filtered (_("done.\n"));
6919 process_cu_includes ();
6922 /* Reading in full CUs. */
6924 /* Add PER_CU to the queue. */
6927 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6928 enum language pretend_language)
6930 struct dwarf2_queue_item *item;
6933 item = xmalloc (sizeof (*item));
6934 item->per_cu = per_cu;
6935 item->pretend_language = pretend_language;
6938 if (dwarf2_queue == NULL)
6939 dwarf2_queue = item;
6941 dwarf2_queue_tail->next = item;
6943 dwarf2_queue_tail = item;
6946 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6947 unit and add it to our queue.
6948 The result is non-zero if PER_CU was queued, otherwise the result is zero
6949 meaning either PER_CU is already queued or it is already loaded. */
6952 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6953 struct dwarf2_per_cu_data *per_cu,
6954 enum language pretend_language)
6956 /* We may arrive here during partial symbol reading, if we need full
6957 DIEs to process an unusual case (e.g. template arguments). Do
6958 not queue PER_CU, just tell our caller to load its DIEs. */
6959 if (dwarf2_per_objfile->reading_partial_symbols)
6961 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6966 /* Mark the dependence relation so that we don't flush PER_CU
6968 dwarf2_add_dependence (this_cu, per_cu);
6970 /* If it's already on the queue, we have nothing to do. */
6974 /* If the compilation unit is already loaded, just mark it as
6976 if (per_cu->cu != NULL)
6978 per_cu->cu->last_used = 0;
6982 /* Add it to the queue. */
6983 queue_comp_unit (per_cu, pretend_language);
6988 /* Process the queue. */
6991 process_queue (void)
6993 struct dwarf2_queue_item *item, *next_item;
6995 if (dwarf2_read_debug)
6997 fprintf_unfiltered (gdb_stdlog,
6998 "Expanding one or more symtabs of objfile %s ...\n",
6999 dwarf2_per_objfile->objfile->name);
7002 /* The queue starts out with one item, but following a DIE reference
7003 may load a new CU, adding it to the end of the queue. */
7004 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7006 if (dwarf2_per_objfile->using_index
7007 ? !item->per_cu->v.quick->symtab
7008 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7010 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7013 if (per_cu->is_debug_types)
7015 struct signatured_type *sig_type =
7016 (struct signatured_type *) per_cu;
7018 sprintf (buf, "TU %s at offset 0x%x",
7019 hex_string (sig_type->signature), per_cu->offset.sect_off);
7022 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7024 if (dwarf2_read_debug)
7025 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7027 if (per_cu->is_debug_types)
7028 process_full_type_unit (per_cu, item->pretend_language);
7030 process_full_comp_unit (per_cu, item->pretend_language);
7032 if (dwarf2_read_debug)
7033 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7036 item->per_cu->queued = 0;
7037 next_item = item->next;
7041 dwarf2_queue_tail = NULL;
7043 if (dwarf2_read_debug)
7045 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7046 dwarf2_per_objfile->objfile->name);
7050 /* Free all allocated queue entries. This function only releases anything if
7051 an error was thrown; if the queue was processed then it would have been
7052 freed as we went along. */
7055 dwarf2_release_queue (void *dummy)
7057 struct dwarf2_queue_item *item, *last;
7059 item = dwarf2_queue;
7062 /* Anything still marked queued is likely to be in an
7063 inconsistent state, so discard it. */
7064 if (item->per_cu->queued)
7066 if (item->per_cu->cu != NULL)
7067 free_one_cached_comp_unit (item->per_cu);
7068 item->per_cu->queued = 0;
7076 dwarf2_queue = dwarf2_queue_tail = NULL;
7079 /* Read in full symbols for PST, and anything it depends on. */
7082 psymtab_to_symtab_1 (struct partial_symtab *pst)
7084 struct dwarf2_per_cu_data *per_cu;
7090 for (i = 0; i < pst->number_of_dependencies; i++)
7091 if (!pst->dependencies[i]->readin
7092 && pst->dependencies[i]->user == NULL)
7094 /* Inform about additional files that need to be read in. */
7097 /* FIXME: i18n: Need to make this a single string. */
7098 fputs_filtered (" ", gdb_stdout);
7100 fputs_filtered ("and ", gdb_stdout);
7102 printf_filtered ("%s...", pst->dependencies[i]->filename);
7103 wrap_here (""); /* Flush output. */
7104 gdb_flush (gdb_stdout);
7106 psymtab_to_symtab_1 (pst->dependencies[i]);
7109 per_cu = pst->read_symtab_private;
7113 /* It's an include file, no symbols to read for it.
7114 Everything is in the parent symtab. */
7119 dw2_do_instantiate_symtab (per_cu);
7122 /* Trivial hash function for die_info: the hash value of a DIE
7123 is its offset in .debug_info for this objfile. */
7126 die_hash (const void *item)
7128 const struct die_info *die = item;
7130 return die->offset.sect_off;
7133 /* Trivial comparison function for die_info structures: two DIEs
7134 are equal if they have the same offset. */
7137 die_eq (const void *item_lhs, const void *item_rhs)
7139 const struct die_info *die_lhs = item_lhs;
7140 const struct die_info *die_rhs = item_rhs;
7142 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7145 /* die_reader_func for load_full_comp_unit.
7146 This is identical to read_signatured_type_reader,
7147 but is kept separate for now. */
7150 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7151 const gdb_byte *info_ptr,
7152 struct die_info *comp_unit_die,
7156 struct dwarf2_cu *cu = reader->cu;
7157 enum language *language_ptr = data;
7159 gdb_assert (cu->die_hash == NULL);
7161 htab_create_alloc_ex (cu->header.length / 12,
7165 &cu->comp_unit_obstack,
7166 hashtab_obstack_allocate,
7167 dummy_obstack_deallocate);
7170 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7171 &info_ptr, comp_unit_die);
7172 cu->dies = comp_unit_die;
7173 /* comp_unit_die is not stored in die_hash, no need. */
7175 /* We try not to read any attributes in this function, because not
7176 all CUs needed for references have been loaded yet, and symbol
7177 table processing isn't initialized. But we have to set the CU language,
7178 or we won't be able to build types correctly.
7179 Similarly, if we do not read the producer, we can not apply
7180 producer-specific interpretation. */
7181 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7184 /* Load the DIEs associated with PER_CU into memory. */
7187 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7188 enum language pretend_language)
7190 gdb_assert (! this_cu->is_debug_types);
7192 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7193 load_full_comp_unit_reader, &pretend_language);
7196 /* Add a DIE to the delayed physname list. */
7199 add_to_method_list (struct type *type, int fnfield_index, int index,
7200 const char *name, struct die_info *die,
7201 struct dwarf2_cu *cu)
7203 struct delayed_method_info mi;
7205 mi.fnfield_index = fnfield_index;
7209 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7212 /* A cleanup for freeing the delayed method list. */
7215 free_delayed_list (void *ptr)
7217 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7218 if (cu->method_list != NULL)
7220 VEC_free (delayed_method_info, cu->method_list);
7221 cu->method_list = NULL;
7225 /* Compute the physnames of any methods on the CU's method list.
7227 The computation of method physnames is delayed in order to avoid the
7228 (bad) condition that one of the method's formal parameters is of an as yet
7232 compute_delayed_physnames (struct dwarf2_cu *cu)
7235 struct delayed_method_info *mi;
7236 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7238 const char *physname;
7239 struct fn_fieldlist *fn_flp
7240 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7241 physname = dwarf2_physname (mi->name, mi->die, cu);
7242 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7246 /* Go objects should be embedded in a DW_TAG_module DIE,
7247 and it's not clear if/how imported objects will appear.
7248 To keep Go support simple until that's worked out,
7249 go back through what we've read and create something usable.
7250 We could do this while processing each DIE, and feels kinda cleaner,
7251 but that way is more invasive.
7252 This is to, for example, allow the user to type "p var" or "b main"
7253 without having to specify the package name, and allow lookups
7254 of module.object to work in contexts that use the expression
7258 fixup_go_packaging (struct dwarf2_cu *cu)
7260 char *package_name = NULL;
7261 struct pending *list;
7264 for (list = global_symbols; list != NULL; list = list->next)
7266 for (i = 0; i < list->nsyms; ++i)
7268 struct symbol *sym = list->symbol[i];
7270 if (SYMBOL_LANGUAGE (sym) == language_go
7271 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7273 char *this_package_name = go_symbol_package_name (sym);
7275 if (this_package_name == NULL)
7277 if (package_name == NULL)
7278 package_name = this_package_name;
7281 if (strcmp (package_name, this_package_name) != 0)
7282 complaint (&symfile_complaints,
7283 _("Symtab %s has objects from two different Go packages: %s and %s"),
7284 (SYMBOL_SYMTAB (sym)
7285 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
7286 : cu->objfile->name),
7287 this_package_name, package_name);
7288 xfree (this_package_name);
7294 if (package_name != NULL)
7296 struct objfile *objfile = cu->objfile;
7297 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7299 strlen (package_name));
7300 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7301 saved_package_name, objfile);
7304 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7306 sym = allocate_symbol (objfile);
7307 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7308 SYMBOL_SET_NAMES (sym, saved_package_name,
7309 strlen (saved_package_name), 0, objfile);
7310 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7311 e.g., "main" finds the "main" module and not C's main(). */
7312 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7313 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7314 SYMBOL_TYPE (sym) = type;
7316 add_symbol_to_list (sym, &global_symbols);
7318 xfree (package_name);
7322 /* Return the symtab for PER_CU. This works properly regardless of
7323 whether we're using the index or psymtabs. */
7325 static struct symtab *
7326 get_symtab (struct dwarf2_per_cu_data *per_cu)
7328 return (dwarf2_per_objfile->using_index
7329 ? per_cu->v.quick->symtab
7330 : per_cu->v.psymtab->symtab);
7333 /* A helper function for computing the list of all symbol tables
7334 included by PER_CU. */
7337 recursively_compute_inclusions (VEC (symtab_ptr) **result,
7338 htab_t all_children, htab_t all_type_symtabs,
7339 struct dwarf2_per_cu_data *per_cu,
7340 struct symtab *immediate_parent)
7344 struct symtab *symtab;
7345 struct dwarf2_per_cu_data *iter;
7347 slot = htab_find_slot (all_children, per_cu, INSERT);
7350 /* This inclusion and its children have been processed. */
7355 /* Only add a CU if it has a symbol table. */
7356 symtab = get_symtab (per_cu);
7359 /* If this is a type unit only add its symbol table if we haven't
7360 seen it yet (type unit per_cu's can share symtabs). */
7361 if (per_cu->is_debug_types)
7363 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7367 VEC_safe_push (symtab_ptr, *result, symtab);
7368 if (symtab->user == NULL)
7369 symtab->user = immediate_parent;
7374 VEC_safe_push (symtab_ptr, *result, symtab);
7375 if (symtab->user == NULL)
7376 symtab->user = immediate_parent;
7381 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7384 recursively_compute_inclusions (result, all_children,
7385 all_type_symtabs, iter, symtab);
7389 /* Compute the symtab 'includes' fields for the symtab related to
7393 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7395 gdb_assert (! per_cu->is_debug_types);
7397 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7400 struct dwarf2_per_cu_data *per_cu_iter;
7401 struct symtab *symtab_iter;
7402 VEC (symtab_ptr) *result_symtabs = NULL;
7403 htab_t all_children, all_type_symtabs;
7404 struct symtab *symtab = get_symtab (per_cu);
7406 /* If we don't have a symtab, we can just skip this case. */
7410 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7411 NULL, xcalloc, xfree);
7412 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7413 NULL, xcalloc, xfree);
7416 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7420 recursively_compute_inclusions (&result_symtabs, all_children,
7421 all_type_symtabs, per_cu_iter,
7425 /* Now we have a transitive closure of all the included symtabs. */
7426 len = VEC_length (symtab_ptr, result_symtabs);
7428 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7429 (len + 1) * sizeof (struct symtab *));
7431 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
7433 symtab->includes[ix] = symtab_iter;
7434 symtab->includes[len] = NULL;
7436 VEC_free (symtab_ptr, result_symtabs);
7437 htab_delete (all_children);
7438 htab_delete (all_type_symtabs);
7442 /* Compute the 'includes' field for the symtabs of all the CUs we just
7446 process_cu_includes (void)
7449 struct dwarf2_per_cu_data *iter;
7452 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7456 if (! iter->is_debug_types)
7457 compute_symtab_includes (iter);
7460 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7463 /* Generate full symbol information for PER_CU, whose DIEs have
7464 already been loaded into memory. */
7467 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7468 enum language pretend_language)
7470 struct dwarf2_cu *cu = per_cu->cu;
7471 struct objfile *objfile = per_cu->objfile;
7472 CORE_ADDR lowpc, highpc;
7473 struct symtab *symtab;
7474 struct cleanup *back_to, *delayed_list_cleanup;
7476 struct block *static_block;
7478 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7481 back_to = make_cleanup (really_free_pendings, NULL);
7482 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7484 cu->list_in_scope = &file_symbols;
7486 cu->language = pretend_language;
7487 cu->language_defn = language_def (cu->language);
7489 /* Do line number decoding in read_file_scope () */
7490 process_die (cu->dies, cu);
7492 /* For now fudge the Go package. */
7493 if (cu->language == language_go)
7494 fixup_go_packaging (cu);
7496 /* Now that we have processed all the DIEs in the CU, all the types
7497 should be complete, and it should now be safe to compute all of the
7499 compute_delayed_physnames (cu);
7500 do_cleanups (delayed_list_cleanup);
7502 /* Some compilers don't define a DW_AT_high_pc attribute for the
7503 compilation unit. If the DW_AT_high_pc is missing, synthesize
7504 it, by scanning the DIE's below the compilation unit. */
7505 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7508 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
7510 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7511 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7512 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7513 addrmap to help ensure it has an accurate map of pc values belonging to
7515 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7517 symtab = end_symtab_from_static_block (static_block, objfile,
7518 SECT_OFF_TEXT (objfile), 0);
7522 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7524 /* Set symtab language to language from DW_AT_language. If the
7525 compilation is from a C file generated by language preprocessors, do
7526 not set the language if it was already deduced by start_subfile. */
7527 if (!(cu->language == language_c && symtab->language != language_c))
7528 symtab->language = cu->language;
7530 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7531 produce DW_AT_location with location lists but it can be possibly
7532 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7533 there were bugs in prologue debug info, fixed later in GCC-4.5
7534 by "unwind info for epilogues" patch (which is not directly related).
7536 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7537 needed, it would be wrong due to missing DW_AT_producer there.
7539 Still one can confuse GDB by using non-standard GCC compilation
7540 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7542 if (cu->has_loclist && gcc_4_minor >= 5)
7543 symtab->locations_valid = 1;
7545 if (gcc_4_minor >= 5)
7546 symtab->epilogue_unwind_valid = 1;
7548 symtab->call_site_htab = cu->call_site_htab;
7551 if (dwarf2_per_objfile->using_index)
7552 per_cu->v.quick->symtab = symtab;
7555 struct partial_symtab *pst = per_cu->v.psymtab;
7556 pst->symtab = symtab;
7560 /* Push it for inclusion processing later. */
7561 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7563 do_cleanups (back_to);
7566 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7567 already been loaded into memory. */
7570 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7571 enum language pretend_language)
7573 struct dwarf2_cu *cu = per_cu->cu;
7574 struct objfile *objfile = per_cu->objfile;
7575 struct symtab *symtab;
7576 struct cleanup *back_to, *delayed_list_cleanup;
7577 struct signatured_type *sig_type;
7579 gdb_assert (per_cu->is_debug_types);
7580 sig_type = (struct signatured_type *) per_cu;
7583 back_to = make_cleanup (really_free_pendings, NULL);
7584 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7586 cu->list_in_scope = &file_symbols;
7588 cu->language = pretend_language;
7589 cu->language_defn = language_def (cu->language);
7591 /* The symbol tables are set up in read_type_unit_scope. */
7592 process_die (cu->dies, cu);
7594 /* For now fudge the Go package. */
7595 if (cu->language == language_go)
7596 fixup_go_packaging (cu);
7598 /* Now that we have processed all the DIEs in the CU, all the types
7599 should be complete, and it should now be safe to compute all of the
7601 compute_delayed_physnames (cu);
7602 do_cleanups (delayed_list_cleanup);
7604 /* TUs share symbol tables.
7605 If this is the first TU to use this symtab, complete the construction
7606 of it with end_expandable_symtab. Otherwise, complete the addition of
7607 this TU's symbols to the existing symtab. */
7608 if (sig_type->type_unit_group->primary_symtab == NULL)
7610 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7611 sig_type->type_unit_group->primary_symtab = symtab;
7615 /* Set symtab language to language from DW_AT_language. If the
7616 compilation is from a C file generated by language preprocessors,
7617 do not set the language if it was already deduced by
7619 if (!(cu->language == language_c && symtab->language != language_c))
7620 symtab->language = cu->language;
7625 augment_type_symtab (objfile,
7626 sig_type->type_unit_group->primary_symtab);
7627 symtab = sig_type->type_unit_group->primary_symtab;
7630 if (dwarf2_per_objfile->using_index)
7631 per_cu->v.quick->symtab = symtab;
7634 struct partial_symtab *pst = per_cu->v.psymtab;
7635 pst->symtab = symtab;
7639 do_cleanups (back_to);
7642 /* Process an imported unit DIE. */
7645 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7647 struct attribute *attr;
7649 /* For now we don't handle imported units in type units. */
7650 if (cu->per_cu->is_debug_types)
7652 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7653 " supported in type units [in module %s]"),
7657 attr = dwarf2_attr (die, DW_AT_import, cu);
7660 struct dwarf2_per_cu_data *per_cu;
7661 struct symtab *imported_symtab;
7665 offset = dwarf2_get_ref_die_offset (attr);
7666 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7667 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7669 /* Queue the unit, if needed. */
7670 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7671 load_full_comp_unit (per_cu, cu->language);
7673 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7678 /* Process a die and its children. */
7681 process_die (struct die_info *die, struct dwarf2_cu *cu)
7685 case DW_TAG_padding:
7687 case DW_TAG_compile_unit:
7688 case DW_TAG_partial_unit:
7689 read_file_scope (die, cu);
7691 case DW_TAG_type_unit:
7692 read_type_unit_scope (die, cu);
7694 case DW_TAG_subprogram:
7695 case DW_TAG_inlined_subroutine:
7696 read_func_scope (die, cu);
7698 case DW_TAG_lexical_block:
7699 case DW_TAG_try_block:
7700 case DW_TAG_catch_block:
7701 read_lexical_block_scope (die, cu);
7703 case DW_TAG_GNU_call_site:
7704 read_call_site_scope (die, cu);
7706 case DW_TAG_class_type:
7707 case DW_TAG_interface_type:
7708 case DW_TAG_structure_type:
7709 case DW_TAG_union_type:
7710 process_structure_scope (die, cu);
7712 case DW_TAG_enumeration_type:
7713 process_enumeration_scope (die, cu);
7716 /* These dies have a type, but processing them does not create
7717 a symbol or recurse to process the children. Therefore we can
7718 read them on-demand through read_type_die. */
7719 case DW_TAG_subroutine_type:
7720 case DW_TAG_set_type:
7721 case DW_TAG_array_type:
7722 case DW_TAG_pointer_type:
7723 case DW_TAG_ptr_to_member_type:
7724 case DW_TAG_reference_type:
7725 case DW_TAG_string_type:
7728 case DW_TAG_base_type:
7729 case DW_TAG_subrange_type:
7730 case DW_TAG_typedef:
7731 /* Add a typedef symbol for the type definition, if it has a
7733 new_symbol (die, read_type_die (die, cu), cu);
7735 case DW_TAG_common_block:
7736 read_common_block (die, cu);
7738 case DW_TAG_common_inclusion:
7740 case DW_TAG_namespace:
7741 cu->processing_has_namespace_info = 1;
7742 read_namespace (die, cu);
7745 cu->processing_has_namespace_info = 1;
7746 read_module (die, cu);
7748 case DW_TAG_imported_declaration:
7749 case DW_TAG_imported_module:
7750 cu->processing_has_namespace_info = 1;
7751 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7752 || cu->language != language_fortran))
7753 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7754 dwarf_tag_name (die->tag));
7755 read_import_statement (die, cu);
7758 case DW_TAG_imported_unit:
7759 process_imported_unit_die (die, cu);
7763 new_symbol (die, NULL, cu);
7768 /* DWARF name computation. */
7770 /* A helper function for dwarf2_compute_name which determines whether DIE
7771 needs to have the name of the scope prepended to the name listed in the
7775 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7777 struct attribute *attr;
7781 case DW_TAG_namespace:
7782 case DW_TAG_typedef:
7783 case DW_TAG_class_type:
7784 case DW_TAG_interface_type:
7785 case DW_TAG_structure_type:
7786 case DW_TAG_union_type:
7787 case DW_TAG_enumeration_type:
7788 case DW_TAG_enumerator:
7789 case DW_TAG_subprogram:
7793 case DW_TAG_variable:
7794 case DW_TAG_constant:
7795 /* We only need to prefix "globally" visible variables. These include
7796 any variable marked with DW_AT_external or any variable that
7797 lives in a namespace. [Variables in anonymous namespaces
7798 require prefixing, but they are not DW_AT_external.] */
7800 if (dwarf2_attr (die, DW_AT_specification, cu))
7802 struct dwarf2_cu *spec_cu = cu;
7804 return die_needs_namespace (die_specification (die, &spec_cu),
7808 attr = dwarf2_attr (die, DW_AT_external, cu);
7809 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7810 && die->parent->tag != DW_TAG_module)
7812 /* A variable in a lexical block of some kind does not need a
7813 namespace, even though in C++ such variables may be external
7814 and have a mangled name. */
7815 if (die->parent->tag == DW_TAG_lexical_block
7816 || die->parent->tag == DW_TAG_try_block
7817 || die->parent->tag == DW_TAG_catch_block
7818 || die->parent->tag == DW_TAG_subprogram)
7827 /* Retrieve the last character from a mem_file. */
7830 do_ui_file_peek_last (void *object, const char *buffer, long length)
7832 char *last_char_p = (char *) object;
7835 *last_char_p = buffer[length - 1];
7838 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7839 compute the physname for the object, which include a method's:
7840 - formal parameters (C++/Java),
7841 - receiver type (Go),
7842 - return type (Java).
7844 The term "physname" is a bit confusing.
7845 For C++, for example, it is the demangled name.
7846 For Go, for example, it's the mangled name.
7848 For Ada, return the DIE's linkage name rather than the fully qualified
7849 name. PHYSNAME is ignored..
7851 The result is allocated on the objfile_obstack and canonicalized. */
7854 dwarf2_compute_name (const char *name,
7855 struct die_info *die, struct dwarf2_cu *cu,
7858 struct objfile *objfile = cu->objfile;
7861 name = dwarf2_name (die, cu);
7863 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7864 compute it by typename_concat inside GDB. */
7865 if (cu->language == language_ada
7866 || (cu->language == language_fortran && physname))
7868 /* For Ada unit, we prefer the linkage name over the name, as
7869 the former contains the exported name, which the user expects
7870 to be able to reference. Ideally, we want the user to be able
7871 to reference this entity using either natural or linkage name,
7872 but we haven't started looking at this enhancement yet. */
7873 struct attribute *attr;
7875 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7877 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7878 if (attr && DW_STRING (attr))
7879 return DW_STRING (attr);
7882 /* These are the only languages we know how to qualify names in. */
7884 && (cu->language == language_cplus || cu->language == language_java
7885 || cu->language == language_fortran))
7887 if (die_needs_namespace (die, cu))
7891 struct ui_file *buf;
7893 prefix = determine_prefix (die, cu);
7894 buf = mem_fileopen ();
7895 if (*prefix != '\0')
7897 char *prefixed_name = typename_concat (NULL, prefix, name,
7900 fputs_unfiltered (prefixed_name, buf);
7901 xfree (prefixed_name);
7904 fputs_unfiltered (name, buf);
7906 /* Template parameters may be specified in the DIE's DW_AT_name, or
7907 as children with DW_TAG_template_type_param or
7908 DW_TAG_value_type_param. If the latter, add them to the name
7909 here. If the name already has template parameters, then
7910 skip this step; some versions of GCC emit both, and
7911 it is more efficient to use the pre-computed name.
7913 Something to keep in mind about this process: it is very
7914 unlikely, or in some cases downright impossible, to produce
7915 something that will match the mangled name of a function.
7916 If the definition of the function has the same debug info,
7917 we should be able to match up with it anyway. But fallbacks
7918 using the minimal symbol, for instance to find a method
7919 implemented in a stripped copy of libstdc++, will not work.
7920 If we do not have debug info for the definition, we will have to
7921 match them up some other way.
7923 When we do name matching there is a related problem with function
7924 templates; two instantiated function templates are allowed to
7925 differ only by their return types, which we do not add here. */
7927 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7929 struct attribute *attr;
7930 struct die_info *child;
7933 die->building_fullname = 1;
7935 for (child = die->child; child != NULL; child = child->sibling)
7939 const gdb_byte *bytes;
7940 struct dwarf2_locexpr_baton *baton;
7943 if (child->tag != DW_TAG_template_type_param
7944 && child->tag != DW_TAG_template_value_param)
7949 fputs_unfiltered ("<", buf);
7953 fputs_unfiltered (", ", buf);
7955 attr = dwarf2_attr (child, DW_AT_type, cu);
7958 complaint (&symfile_complaints,
7959 _("template parameter missing DW_AT_type"));
7960 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7963 type = die_type (child, cu);
7965 if (child->tag == DW_TAG_template_type_param)
7967 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7971 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7974 complaint (&symfile_complaints,
7975 _("template parameter missing "
7976 "DW_AT_const_value"));
7977 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7981 dwarf2_const_value_attr (attr, type, name,
7982 &cu->comp_unit_obstack, cu,
7983 &value, &bytes, &baton);
7985 if (TYPE_NOSIGN (type))
7986 /* GDB prints characters as NUMBER 'CHAR'. If that's
7987 changed, this can use value_print instead. */
7988 c_printchar (value, type, buf);
7991 struct value_print_options opts;
7994 v = dwarf2_evaluate_loc_desc (type, NULL,
7998 else if (bytes != NULL)
8000 v = allocate_value (type);
8001 memcpy (value_contents_writeable (v), bytes,
8002 TYPE_LENGTH (type));
8005 v = value_from_longest (type, value);
8007 /* Specify decimal so that we do not depend on
8009 get_formatted_print_options (&opts, 'd');
8011 value_print (v, buf, &opts);
8017 die->building_fullname = 0;
8021 /* Close the argument list, with a space if necessary
8022 (nested templates). */
8023 char last_char = '\0';
8024 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8025 if (last_char == '>')
8026 fputs_unfiltered (" >", buf);
8028 fputs_unfiltered (">", buf);
8032 /* For Java and C++ methods, append formal parameter type
8033 information, if PHYSNAME. */
8035 if (physname && die->tag == DW_TAG_subprogram
8036 && (cu->language == language_cplus
8037 || cu->language == language_java))
8039 struct type *type = read_type_die (die, cu);
8041 c_type_print_args (type, buf, 1, cu->language,
8042 &type_print_raw_options);
8044 if (cu->language == language_java)
8046 /* For java, we must append the return type to method
8048 if (die->tag == DW_TAG_subprogram)
8049 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8050 0, 0, &type_print_raw_options);
8052 else if (cu->language == language_cplus)
8054 /* Assume that an artificial first parameter is
8055 "this", but do not crash if it is not. RealView
8056 marks unnamed (and thus unused) parameters as
8057 artificial; there is no way to differentiate
8059 if (TYPE_NFIELDS (type) > 0
8060 && TYPE_FIELD_ARTIFICIAL (type, 0)
8061 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8062 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8064 fputs_unfiltered (" const", buf);
8068 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
8070 ui_file_delete (buf);
8072 if (cu->language == language_cplus)
8075 = dwarf2_canonicalize_name (name, cu,
8076 &objfile->objfile_obstack);
8087 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8088 If scope qualifiers are appropriate they will be added. The result
8089 will be allocated on the objfile_obstack, or NULL if the DIE does
8090 not have a name. NAME may either be from a previous call to
8091 dwarf2_name or NULL.
8093 The output string will be canonicalized (if C++/Java). */
8096 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8098 return dwarf2_compute_name (name, die, cu, 0);
8101 /* Construct a physname for the given DIE in CU. NAME may either be
8102 from a previous call to dwarf2_name or NULL. The result will be
8103 allocated on the objfile_objstack or NULL if the DIE does not have a
8106 The output string will be canonicalized (if C++/Java). */
8109 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8111 struct objfile *objfile = cu->objfile;
8112 struct attribute *attr;
8113 const char *retval, *mangled = NULL, *canon = NULL;
8114 struct cleanup *back_to;
8117 /* In this case dwarf2_compute_name is just a shortcut not building anything
8119 if (!die_needs_namespace (die, cu))
8120 return dwarf2_compute_name (name, die, cu, 1);
8122 back_to = make_cleanup (null_cleanup, NULL);
8124 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8126 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8128 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8130 if (attr && DW_STRING (attr))
8134 mangled = DW_STRING (attr);
8136 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8137 type. It is easier for GDB users to search for such functions as
8138 `name(params)' than `long name(params)'. In such case the minimal
8139 symbol names do not match the full symbol names but for template
8140 functions there is never a need to look up their definition from their
8141 declaration so the only disadvantage remains the minimal symbol
8142 variant `long name(params)' does not have the proper inferior type.
8145 if (cu->language == language_go)
8147 /* This is a lie, but we already lie to the caller new_symbol_full.
8148 new_symbol_full assumes we return the mangled name.
8149 This just undoes that lie until things are cleaned up. */
8154 demangled = gdb_demangle (mangled,
8155 (DMGL_PARAMS | DMGL_ANSI
8156 | (cu->language == language_java
8157 ? DMGL_JAVA | DMGL_RET_POSTFIX
8162 make_cleanup (xfree, demangled);
8172 if (canon == NULL || check_physname)
8174 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8176 if (canon != NULL && strcmp (physname, canon) != 0)
8178 /* It may not mean a bug in GDB. The compiler could also
8179 compute DW_AT_linkage_name incorrectly. But in such case
8180 GDB would need to be bug-to-bug compatible. */
8182 complaint (&symfile_complaints,
8183 _("Computed physname <%s> does not match demangled <%s> "
8184 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8185 physname, canon, mangled, die->offset.sect_off, objfile->name);
8187 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8188 is available here - over computed PHYSNAME. It is safer
8189 against both buggy GDB and buggy compilers. */
8203 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
8205 do_cleanups (back_to);
8209 /* Read the import statement specified by the given die and record it. */
8212 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8214 struct objfile *objfile = cu->objfile;
8215 struct attribute *import_attr;
8216 struct die_info *imported_die, *child_die;
8217 struct dwarf2_cu *imported_cu;
8218 const char *imported_name;
8219 const char *imported_name_prefix;
8220 const char *canonical_name;
8221 const char *import_alias;
8222 const char *imported_declaration = NULL;
8223 const char *import_prefix;
8224 VEC (const_char_ptr) *excludes = NULL;
8225 struct cleanup *cleanups;
8227 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8228 if (import_attr == NULL)
8230 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8231 dwarf_tag_name (die->tag));
8236 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8237 imported_name = dwarf2_name (imported_die, imported_cu);
8238 if (imported_name == NULL)
8240 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8242 The import in the following code:
8256 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8257 <52> DW_AT_decl_file : 1
8258 <53> DW_AT_decl_line : 6
8259 <54> DW_AT_import : <0x75>
8260 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8262 <5b> DW_AT_decl_file : 1
8263 <5c> DW_AT_decl_line : 2
8264 <5d> DW_AT_type : <0x6e>
8266 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8267 <76> DW_AT_byte_size : 4
8268 <77> DW_AT_encoding : 5 (signed)
8270 imports the wrong die ( 0x75 instead of 0x58 ).
8271 This case will be ignored until the gcc bug is fixed. */
8275 /* Figure out the local name after import. */
8276 import_alias = dwarf2_name (die, cu);
8278 /* Figure out where the statement is being imported to. */
8279 import_prefix = determine_prefix (die, cu);
8281 /* Figure out what the scope of the imported die is and prepend it
8282 to the name of the imported die. */
8283 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8285 if (imported_die->tag != DW_TAG_namespace
8286 && imported_die->tag != DW_TAG_module)
8288 imported_declaration = imported_name;
8289 canonical_name = imported_name_prefix;
8291 else if (strlen (imported_name_prefix) > 0)
8292 canonical_name = obconcat (&objfile->objfile_obstack,
8293 imported_name_prefix, "::", imported_name,
8296 canonical_name = imported_name;
8298 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8300 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8301 for (child_die = die->child; child_die && child_die->tag;
8302 child_die = sibling_die (child_die))
8304 /* DWARF-4: A Fortran use statement with a “rename list” may be
8305 represented by an imported module entry with an import attribute
8306 referring to the module and owned entries corresponding to those
8307 entities that are renamed as part of being imported. */
8309 if (child_die->tag != DW_TAG_imported_declaration)
8311 complaint (&symfile_complaints,
8312 _("child DW_TAG_imported_declaration expected "
8313 "- DIE at 0x%x [in module %s]"),
8314 child_die->offset.sect_off, objfile->name);
8318 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8319 if (import_attr == NULL)
8321 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8322 dwarf_tag_name (child_die->tag));
8327 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8329 imported_name = dwarf2_name (imported_die, imported_cu);
8330 if (imported_name == NULL)
8332 complaint (&symfile_complaints,
8333 _("child DW_TAG_imported_declaration has unknown "
8334 "imported name - DIE at 0x%x [in module %s]"),
8335 child_die->offset.sect_off, objfile->name);
8339 VEC_safe_push (const_char_ptr, excludes, imported_name);
8341 process_die (child_die, cu);
8344 cp_add_using_directive (import_prefix,
8347 imported_declaration,
8350 &objfile->objfile_obstack);
8352 do_cleanups (cleanups);
8355 /* Cleanup function for handle_DW_AT_stmt_list. */
8358 free_cu_line_header (void *arg)
8360 struct dwarf2_cu *cu = arg;
8362 free_line_header (cu->line_header);
8363 cu->line_header = NULL;
8366 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8367 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8368 this, it was first present in GCC release 4.3.0. */
8371 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8373 if (!cu->checked_producer)
8374 check_producer (cu);
8376 return cu->producer_is_gcc_lt_4_3;
8380 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
8381 const char **name, const char **comp_dir)
8383 struct attribute *attr;
8388 /* Find the filename. Do not use dwarf2_name here, since the filename
8389 is not a source language identifier. */
8390 attr = dwarf2_attr (die, DW_AT_name, cu);
8393 *name = DW_STRING (attr);
8396 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8398 *comp_dir = DW_STRING (attr);
8399 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8400 && IS_ABSOLUTE_PATH (*name))
8402 char *d = ldirname (*name);
8406 make_cleanup (xfree, d);
8408 if (*comp_dir != NULL)
8410 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8411 directory, get rid of it. */
8412 char *cp = strchr (*comp_dir, ':');
8414 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8419 *name = "<unknown>";
8422 /* Handle DW_AT_stmt_list for a compilation unit.
8423 DIE is the DW_TAG_compile_unit die for CU.
8424 COMP_DIR is the compilation directory.
8425 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8428 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8429 const char *comp_dir) /* ARI: editCase function */
8431 struct attribute *attr;
8433 gdb_assert (! cu->per_cu->is_debug_types);
8435 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8438 unsigned int line_offset = DW_UNSND (attr);
8439 struct line_header *line_header
8440 = dwarf_decode_line_header (line_offset, cu);
8444 cu->line_header = line_header;
8445 make_cleanup (free_cu_line_header, cu);
8446 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8451 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8454 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8456 struct objfile *objfile = dwarf2_per_objfile->objfile;
8457 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8458 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8459 CORE_ADDR highpc = ((CORE_ADDR) 0);
8460 struct attribute *attr;
8461 const char *name = NULL;
8462 const char *comp_dir = NULL;
8463 struct die_info *child_die;
8464 bfd *abfd = objfile->obfd;
8467 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8469 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8471 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8472 from finish_block. */
8473 if (lowpc == ((CORE_ADDR) -1))
8478 find_file_and_directory (die, cu, &name, &comp_dir);
8480 prepare_one_comp_unit (cu, die, cu->language);
8482 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8483 standardised yet. As a workaround for the language detection we fall
8484 back to the DW_AT_producer string. */
8485 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8486 cu->language = language_opencl;
8488 /* Similar hack for Go. */
8489 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8490 set_cu_language (DW_LANG_Go, cu);
8492 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8494 /* Decode line number information if present. We do this before
8495 processing child DIEs, so that the line header table is available
8496 for DW_AT_decl_file. */
8497 handle_DW_AT_stmt_list (die, cu, comp_dir);
8499 /* Process all dies in compilation unit. */
8500 if (die->child != NULL)
8502 child_die = die->child;
8503 while (child_die && child_die->tag)
8505 process_die (child_die, cu);
8506 child_die = sibling_die (child_die);
8510 /* Decode macro information, if present. Dwarf 2 macro information
8511 refers to information in the line number info statement program
8512 header, so we can only read it if we've read the header
8514 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8515 if (attr && cu->line_header)
8517 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8518 complaint (&symfile_complaints,
8519 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8521 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8525 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8526 if (attr && cu->line_header)
8528 unsigned int macro_offset = DW_UNSND (attr);
8530 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8534 do_cleanups (back_to);
8537 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8538 Create the set of symtabs used by this TU, or if this TU is sharing
8539 symtabs with another TU and the symtabs have already been created
8540 then restore those symtabs in the line header.
8541 We don't need the pc/line-number mapping for type units. */
8544 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8548 struct type_unit_group *tu_group;
8550 struct line_header *lh;
8551 struct attribute *attr;
8552 unsigned int i, line_offset;
8553 struct signatured_type *sig_type;
8555 gdb_assert (per_cu->is_debug_types);
8556 sig_type = (struct signatured_type *) per_cu;
8558 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8560 /* If we're using .gdb_index (includes -readnow) then
8561 per_cu->type_unit_group may not have been set up yet. */
8562 if (sig_type->type_unit_group == NULL)
8563 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8564 tu_group = sig_type->type_unit_group;
8566 /* If we've already processed this stmt_list there's no real need to
8567 do it again, we could fake it and just recreate the part we need
8568 (file name,index -> symtab mapping). If data shows this optimization
8569 is useful we can do it then. */
8570 first_time = tu_group->primary_symtab == NULL;
8572 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8577 line_offset = DW_UNSND (attr);
8578 lh = dwarf_decode_line_header (line_offset, cu);
8583 dwarf2_start_symtab (cu, "", NULL, 0);
8586 gdb_assert (tu_group->symtabs == NULL);
8589 /* Note: The primary symtab will get allocated at the end. */
8593 cu->line_header = lh;
8594 make_cleanup (free_cu_line_header, cu);
8598 dwarf2_start_symtab (cu, "", NULL, 0);
8600 tu_group->num_symtabs = lh->num_file_names;
8601 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8603 for (i = 0; i < lh->num_file_names; ++i)
8605 const char *dir = NULL;
8606 struct file_entry *fe = &lh->file_names[i];
8609 dir = lh->include_dirs[fe->dir_index - 1];
8610 dwarf2_start_subfile (fe->name, dir, NULL);
8612 /* Note: We don't have to watch for the main subfile here, type units
8613 don't have DW_AT_name. */
8615 if (current_subfile->symtab == NULL)
8617 /* NOTE: start_subfile will recognize when it's been passed
8618 a file it has already seen. So we can't assume there's a
8619 simple mapping from lh->file_names to subfiles,
8620 lh->file_names may contain dups. */
8621 current_subfile->symtab = allocate_symtab (current_subfile->name,
8625 fe->symtab = current_subfile->symtab;
8626 tu_group->symtabs[i] = fe->symtab;
8633 for (i = 0; i < lh->num_file_names; ++i)
8635 struct file_entry *fe = &lh->file_names[i];
8637 fe->symtab = tu_group->symtabs[i];
8641 /* The main symtab is allocated last. Type units don't have DW_AT_name
8642 so they don't have a "real" (so to speak) symtab anyway.
8643 There is later code that will assign the main symtab to all symbols
8644 that don't have one. We need to handle the case of a symbol with a
8645 missing symtab (DW_AT_decl_file) anyway. */
8648 /* Process DW_TAG_type_unit.
8649 For TUs we want to skip the first top level sibling if it's not the
8650 actual type being defined by this TU. In this case the first top
8651 level sibling is there to provide context only. */
8654 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8656 struct die_info *child_die;
8658 prepare_one_comp_unit (cu, die, language_minimal);
8660 /* Initialize (or reinitialize) the machinery for building symtabs.
8661 We do this before processing child DIEs, so that the line header table
8662 is available for DW_AT_decl_file. */
8663 setup_type_unit_groups (die, cu);
8665 if (die->child != NULL)
8667 child_die = die->child;
8668 while (child_die && child_die->tag)
8670 process_die (child_die, cu);
8671 child_die = sibling_die (child_die);
8678 http://gcc.gnu.org/wiki/DebugFission
8679 http://gcc.gnu.org/wiki/DebugFissionDWP
8681 To simplify handling of both DWO files ("object" files with the DWARF info)
8682 and DWP files (a file with the DWOs packaged up into one file), we treat
8683 DWP files as having a collection of virtual DWO files. */
8686 hash_dwo_file (const void *item)
8688 const struct dwo_file *dwo_file = item;
8691 hash = htab_hash_string (dwo_file->dwo_name);
8692 if (dwo_file->comp_dir != NULL)
8693 hash += htab_hash_string (dwo_file->comp_dir);
8698 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8700 const struct dwo_file *lhs = item_lhs;
8701 const struct dwo_file *rhs = item_rhs;
8703 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8705 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8706 return lhs->comp_dir == rhs->comp_dir;
8707 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
8710 /* Allocate a hash table for DWO files. */
8713 allocate_dwo_file_hash_table (void)
8715 struct objfile *objfile = dwarf2_per_objfile->objfile;
8717 return htab_create_alloc_ex (41,
8721 &objfile->objfile_obstack,
8722 hashtab_obstack_allocate,
8723 dummy_obstack_deallocate);
8726 /* Lookup DWO file DWO_NAME. */
8729 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8731 struct dwo_file find_entry;
8734 if (dwarf2_per_objfile->dwo_files == NULL)
8735 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8737 memset (&find_entry, 0, sizeof (find_entry));
8738 find_entry.dwo_name = dwo_name;
8739 find_entry.comp_dir = comp_dir;
8740 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8746 hash_dwo_unit (const void *item)
8748 const struct dwo_unit *dwo_unit = item;
8750 /* This drops the top 32 bits of the id, but is ok for a hash. */
8751 return dwo_unit->signature;
8755 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8757 const struct dwo_unit *lhs = item_lhs;
8758 const struct dwo_unit *rhs = item_rhs;
8760 /* The signature is assumed to be unique within the DWO file.
8761 So while object file CU dwo_id's always have the value zero,
8762 that's OK, assuming each object file DWO file has only one CU,
8763 and that's the rule for now. */
8764 return lhs->signature == rhs->signature;
8767 /* Allocate a hash table for DWO CUs,TUs.
8768 There is one of these tables for each of CUs,TUs for each DWO file. */
8771 allocate_dwo_unit_table (struct objfile *objfile)
8773 /* Start out with a pretty small number.
8774 Generally DWO files contain only one CU and maybe some TUs. */
8775 return htab_create_alloc_ex (3,
8779 &objfile->objfile_obstack,
8780 hashtab_obstack_allocate,
8781 dummy_obstack_deallocate);
8784 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8786 struct create_dwo_cu_data
8788 struct dwo_file *dwo_file;
8789 struct dwo_unit dwo_unit;
8792 /* die_reader_func for create_dwo_cu. */
8795 create_dwo_cu_reader (const struct die_reader_specs *reader,
8796 const gdb_byte *info_ptr,
8797 struct die_info *comp_unit_die,
8801 struct dwarf2_cu *cu = reader->cu;
8802 struct objfile *objfile = dwarf2_per_objfile->objfile;
8803 sect_offset offset = cu->per_cu->offset;
8804 struct dwarf2_section_info *section = cu->per_cu->section;
8805 struct create_dwo_cu_data *data = datap;
8806 struct dwo_file *dwo_file = data->dwo_file;
8807 struct dwo_unit *dwo_unit = &data->dwo_unit;
8808 struct attribute *attr;
8810 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8813 complaint (&symfile_complaints,
8814 _("Dwarf Error: debug entry at offset 0x%x is missing"
8815 " its dwo_id [in module %s]"),
8816 offset.sect_off, dwo_file->dwo_name);
8820 dwo_unit->dwo_file = dwo_file;
8821 dwo_unit->signature = DW_UNSND (attr);
8822 dwo_unit->section = section;
8823 dwo_unit->offset = offset;
8824 dwo_unit->length = cu->per_cu->length;
8826 if (dwarf2_read_debug)
8827 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8828 offset.sect_off, hex_string (dwo_unit->signature));
8831 /* Create the dwo_unit for the lone CU in DWO_FILE.
8832 Note: This function processes DWO files only, not DWP files. */
8834 static struct dwo_unit *
8835 create_dwo_cu (struct dwo_file *dwo_file)
8837 struct objfile *objfile = dwarf2_per_objfile->objfile;
8838 struct dwarf2_section_info *section = &dwo_file->sections.info;
8841 const gdb_byte *info_ptr, *end_ptr;
8842 struct create_dwo_cu_data create_dwo_cu_data;
8843 struct dwo_unit *dwo_unit;
8845 dwarf2_read_section (objfile, section);
8846 info_ptr = section->buffer;
8848 if (info_ptr == NULL)
8851 /* We can't set abfd until now because the section may be empty or
8852 not present, in which case section->asection will be NULL. */
8853 abfd = section->asection->owner;
8855 if (dwarf2_read_debug)
8857 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8858 bfd_section_name (abfd, section->asection),
8859 bfd_get_filename (abfd));
8862 create_dwo_cu_data.dwo_file = dwo_file;
8865 end_ptr = info_ptr + section->size;
8866 while (info_ptr < end_ptr)
8868 struct dwarf2_per_cu_data per_cu;
8870 memset (&create_dwo_cu_data.dwo_unit, 0,
8871 sizeof (create_dwo_cu_data.dwo_unit));
8872 memset (&per_cu, 0, sizeof (per_cu));
8873 per_cu.objfile = objfile;
8874 per_cu.is_debug_types = 0;
8875 per_cu.offset.sect_off = info_ptr - section->buffer;
8876 per_cu.section = section;
8878 init_cutu_and_read_dies_no_follow (&per_cu,
8879 &dwo_file->sections.abbrev,
8881 create_dwo_cu_reader,
8882 &create_dwo_cu_data);
8884 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8886 /* If we've already found one, complain. We only support one
8887 because having more than one requires hacking the dwo_name of
8888 each to match, which is highly unlikely to happen. */
8889 if (dwo_unit != NULL)
8891 complaint (&symfile_complaints,
8892 _("Multiple CUs in DWO file %s [in module %s]"),
8893 dwo_file->dwo_name, objfile->name);
8897 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8898 *dwo_unit = create_dwo_cu_data.dwo_unit;
8901 info_ptr += per_cu.length;
8907 /* DWP file .debug_{cu,tu}_index section format:
8908 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8912 Both index sections have the same format, and serve to map a 64-bit
8913 signature to a set of section numbers. Each section begins with a header,
8914 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8915 indexes, and a pool of 32-bit section numbers. The index sections will be
8916 aligned at 8-byte boundaries in the file.
8918 The index section header consists of:
8920 V, 32 bit version number
8922 N, 32 bit number of compilation units or type units in the index
8923 M, 32 bit number of slots in the hash table
8925 Numbers are recorded using the byte order of the application binary.
8927 We assume that N and M will not exceed 2^32 - 1.
8929 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8931 The hash table begins at offset 16 in the section, and consists of an array
8932 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8933 order of the application binary). Unused slots in the hash table are 0.
8934 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8936 The parallel table begins immediately after the hash table
8937 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8938 array of 32-bit indexes (using the byte order of the application binary),
8939 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8940 table contains a 32-bit index into the pool of section numbers. For unused
8941 hash table slots, the corresponding entry in the parallel table will be 0.
8943 Given a 64-bit compilation unit signature or a type signature S, an entry
8944 in the hash table is located as follows:
8946 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8947 the low-order k bits all set to 1.
8949 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8951 3) If the hash table entry at index H matches the signature, use that
8952 entry. If the hash table entry at index H is unused (all zeroes),
8953 terminate the search: the signature is not present in the table.
8955 4) Let H = (H + H') modulo M. Repeat at Step 3.
8957 Because M > N and H' and M are relatively prime, the search is guaranteed
8958 to stop at an unused slot or find the match.
8960 The pool of section numbers begins immediately following the hash table
8961 (at offset 16 + 12 * M from the beginning of the section). The pool of
8962 section numbers consists of an array of 32-bit words (using the byte order
8963 of the application binary). Each item in the array is indexed starting
8964 from 0. The hash table entry provides the index of the first section
8965 number in the set. Additional section numbers in the set follow, and the
8966 set is terminated by a 0 entry (section number 0 is not used in ELF).
8968 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8969 section must be the first entry in the set, and the .debug_abbrev.dwo must
8970 be the second entry. Other members of the set may follow in any order. */
8972 /* Create a hash table to map DWO IDs to their CU/TU entry in
8973 .debug_{info,types}.dwo in DWP_FILE.
8974 Returns NULL if there isn't one.
8975 Note: This function processes DWP files only, not DWO files. */
8977 static struct dwp_hash_table *
8978 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8980 struct objfile *objfile = dwarf2_per_objfile->objfile;
8981 bfd *dbfd = dwp_file->dbfd;
8982 const gdb_byte *index_ptr, *index_end;
8983 struct dwarf2_section_info *index;
8984 uint32_t version, nr_units, nr_slots;
8985 struct dwp_hash_table *htab;
8988 index = &dwp_file->sections.tu_index;
8990 index = &dwp_file->sections.cu_index;
8992 if (dwarf2_section_empty_p (index))
8994 dwarf2_read_section (objfile, index);
8996 index_ptr = index->buffer;
8997 index_end = index_ptr + index->size;
8999 version = read_4_bytes (dbfd, index_ptr);
9000 index_ptr += 8; /* Skip the unused word. */
9001 nr_units = read_4_bytes (dbfd, index_ptr);
9003 nr_slots = read_4_bytes (dbfd, index_ptr);
9008 error (_("Dwarf Error: unsupported DWP file version (%s)"
9010 pulongest (version), dwp_file->name);
9012 if (nr_slots != (nr_slots & -nr_slots))
9014 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9015 " is not power of 2 [in module %s]"),
9016 pulongest (nr_slots), dwp_file->name);
9019 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9020 htab->nr_units = nr_units;
9021 htab->nr_slots = nr_slots;
9022 htab->hash_table = index_ptr;
9023 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9024 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9029 /* Update SECTIONS with the data from SECTP.
9031 This function is like the other "locate" section routines that are
9032 passed to bfd_map_over_sections, but in this context the sections to
9033 read comes from the DWP hash table, not the full ELF section table.
9035 The result is non-zero for success, or zero if an error was found. */
9038 locate_virtual_dwo_sections (asection *sectp,
9039 struct virtual_dwo_sections *sections)
9041 const struct dwop_section_names *names = &dwop_section_names;
9043 if (section_is_p (sectp->name, &names->abbrev_dwo))
9045 /* There can be only one. */
9046 if (sections->abbrev.asection != NULL)
9048 sections->abbrev.asection = sectp;
9049 sections->abbrev.size = bfd_get_section_size (sectp);
9051 else if (section_is_p (sectp->name, &names->info_dwo)
9052 || section_is_p (sectp->name, &names->types_dwo))
9054 /* There can be only one. */
9055 if (sections->info_or_types.asection != NULL)
9057 sections->info_or_types.asection = sectp;
9058 sections->info_or_types.size = bfd_get_section_size (sectp);
9060 else if (section_is_p (sectp->name, &names->line_dwo))
9062 /* There can be only one. */
9063 if (sections->line.asection != NULL)
9065 sections->line.asection = sectp;
9066 sections->line.size = bfd_get_section_size (sectp);
9068 else if (section_is_p (sectp->name, &names->loc_dwo))
9070 /* There can be only one. */
9071 if (sections->loc.asection != NULL)
9073 sections->loc.asection = sectp;
9074 sections->loc.size = bfd_get_section_size (sectp);
9076 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9078 /* There can be only one. */
9079 if (sections->macinfo.asection != NULL)
9081 sections->macinfo.asection = sectp;
9082 sections->macinfo.size = bfd_get_section_size (sectp);
9084 else if (section_is_p (sectp->name, &names->macro_dwo))
9086 /* There can be only one. */
9087 if (sections->macro.asection != NULL)
9089 sections->macro.asection = sectp;
9090 sections->macro.size = bfd_get_section_size (sectp);
9092 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9094 /* There can be only one. */
9095 if (sections->str_offsets.asection != NULL)
9097 sections->str_offsets.asection = sectp;
9098 sections->str_offsets.size = bfd_get_section_size (sectp);
9102 /* No other kind of section is valid. */
9109 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
9110 HTAB is the hash table from the DWP file.
9111 SECTION_INDEX is the index of the DWO in HTAB.
9112 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
9114 static struct dwo_unit *
9115 create_dwo_in_dwp (struct dwp_file *dwp_file,
9116 const struct dwp_hash_table *htab,
9117 uint32_t section_index,
9118 const char *comp_dir,
9119 ULONGEST signature, int is_debug_types)
9121 struct objfile *objfile = dwarf2_per_objfile->objfile;
9122 bfd *dbfd = dwp_file->dbfd;
9123 const char *kind = is_debug_types ? "TU" : "CU";
9124 struct dwo_file *dwo_file;
9125 struct dwo_unit *dwo_unit;
9126 struct virtual_dwo_sections sections;
9127 void **dwo_file_slot;
9128 char *virtual_dwo_name;
9129 struct dwarf2_section_info *cutu;
9130 struct cleanup *cleanups;
9133 if (dwarf2_read_debug)
9135 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
9137 pulongest (section_index), hex_string (signature),
9141 /* Fetch the sections of this DWO.
9142 Put a limit on the number of sections we look for so that bad data
9143 doesn't cause us to loop forever. */
9145 #define MAX_NR_DWO_SECTIONS \
9146 (1 /* .debug_info or .debug_types */ \
9147 + 1 /* .debug_abbrev */ \
9148 + 1 /* .debug_line */ \
9149 + 1 /* .debug_loc */ \
9150 + 1 /* .debug_str_offsets */ \
9151 + 1 /* .debug_macro */ \
9152 + 1 /* .debug_macinfo */ \
9153 + 1 /* trailing zero */)
9155 memset (§ions, 0, sizeof (sections));
9156 cleanups = make_cleanup (null_cleanup, 0);
9158 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9161 uint32_t section_nr =
9164 + (section_index + i) * sizeof (uint32_t));
9166 if (section_nr == 0)
9168 if (section_nr >= dwp_file->num_sections)
9170 error (_("Dwarf Error: bad DWP hash table, section number too large"
9175 sectp = dwp_file->elf_sections[section_nr];
9176 if (! locate_virtual_dwo_sections (sectp, §ions))
9178 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9185 || sections.info_or_types.asection == NULL
9186 || sections.abbrev.asection == NULL)
9188 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9192 if (i == MAX_NR_DWO_SECTIONS)
9194 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9199 /* It's easier for the rest of the code if we fake a struct dwo_file and
9200 have dwo_unit "live" in that. At least for now.
9202 The DWP file can be made up of a random collection of CUs and TUs.
9203 However, for each CU + set of TUs that came from the same original DWO
9204 file, we want to combine them back into a virtual DWO file to save space
9205 (fewer struct dwo_file objects to allocated). Remember that for really
9206 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9209 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9210 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9211 sections.line.asection ? sections.line.asection->id : 0,
9212 sections.loc.asection ? sections.loc.asection->id : 0,
9213 (sections.str_offsets.asection
9214 ? sections.str_offsets.asection->id
9216 make_cleanup (xfree, virtual_dwo_name);
9217 /* Can we use an existing virtual DWO file? */
9218 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9219 /* Create one if necessary. */
9220 if (*dwo_file_slot == NULL)
9222 if (dwarf2_read_debug)
9224 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9227 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9228 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9230 strlen (virtual_dwo_name));
9231 dwo_file->comp_dir = comp_dir;
9232 dwo_file->sections.abbrev = sections.abbrev;
9233 dwo_file->sections.line = sections.line;
9234 dwo_file->sections.loc = sections.loc;
9235 dwo_file->sections.macinfo = sections.macinfo;
9236 dwo_file->sections.macro = sections.macro;
9237 dwo_file->sections.str_offsets = sections.str_offsets;
9238 /* The "str" section is global to the entire DWP file. */
9239 dwo_file->sections.str = dwp_file->sections.str;
9240 /* The info or types section is assigned later to dwo_unit,
9241 there's no need to record it in dwo_file.
9242 Also, we can't simply record type sections in dwo_file because
9243 we record a pointer into the vector in dwo_unit. As we collect more
9244 types we'll grow the vector and eventually have to reallocate space
9245 for it, invalidating all the pointers into the current copy. */
9246 *dwo_file_slot = dwo_file;
9250 if (dwarf2_read_debug)
9252 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9255 dwo_file = *dwo_file_slot;
9257 do_cleanups (cleanups);
9259 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9260 dwo_unit->dwo_file = dwo_file;
9261 dwo_unit->signature = signature;
9262 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9263 sizeof (struct dwarf2_section_info));
9264 *dwo_unit->section = sections.info_or_types;
9265 /* offset, length, type_offset_in_tu are set later. */
9270 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
9272 static struct dwo_unit *
9273 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9274 const struct dwp_hash_table *htab,
9275 const char *comp_dir,
9276 ULONGEST signature, int is_debug_types)
9278 bfd *dbfd = dwp_file->dbfd;
9279 uint32_t mask = htab->nr_slots - 1;
9280 uint32_t hash = signature & mask;
9281 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9284 struct dwo_unit find_dwo_cu, *dwo_cu;
9286 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9287 find_dwo_cu.signature = signature;
9288 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9293 /* Use a for loop so that we don't loop forever on bad debug info. */
9294 for (i = 0; i < htab->nr_slots; ++i)
9296 ULONGEST signature_in_table;
9298 signature_in_table =
9299 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9300 if (signature_in_table == signature)
9302 uint32_t section_index =
9303 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9305 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
9306 comp_dir, signature, is_debug_types);
9309 if (signature_in_table == 0)
9311 hash = (hash + hash2) & mask;
9314 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9319 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9320 Open the file specified by FILE_NAME and hand it off to BFD for
9321 preliminary analysis. Return a newly initialized bfd *, which
9322 includes a canonicalized copy of FILE_NAME.
9323 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9324 SEARCH_CWD is true if the current directory is to be searched.
9325 It will be searched before debug-file-directory.
9326 If unable to find/open the file, return NULL.
9327 NOTE: This function is derived from symfile_bfd_open. */
9330 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
9334 char *absolute_name;
9335 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9336 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9337 to debug_file_directory. */
9339 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9343 if (*debug_file_directory != '\0')
9344 search_path = concat (".", dirname_separator_string,
9345 debug_file_directory, NULL);
9347 search_path = xstrdup (".");
9350 search_path = xstrdup (debug_file_directory);
9354 flags |= OPF_SEARCH_IN_PATH;
9355 desc = openp (search_path, flags, file_name,
9356 O_RDONLY | O_BINARY, &absolute_name);
9357 xfree (search_path);
9361 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
9362 xfree (absolute_name);
9363 if (sym_bfd == NULL)
9365 bfd_set_cacheable (sym_bfd, 1);
9367 if (!bfd_check_format (sym_bfd, bfd_object))
9369 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
9376 /* Try to open DWO file FILE_NAME.
9377 COMP_DIR is the DW_AT_comp_dir attribute.
9378 The result is the bfd handle of the file.
9379 If there is a problem finding or opening the file, return NULL.
9380 Upon success, the canonicalized path of the file is stored in the bfd,
9381 same as symfile_bfd_open. */
9384 open_dwo_file (const char *file_name, const char *comp_dir)
9388 if (IS_ABSOLUTE_PATH (file_name))
9389 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
9391 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9393 if (comp_dir != NULL)
9395 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
9397 /* NOTE: If comp_dir is a relative path, this will also try the
9398 search path, which seems useful. */
9399 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
9400 xfree (path_to_try);
9405 /* That didn't work, try debug-file-directory, which, despite its name,
9406 is a list of paths. */
9408 if (*debug_file_directory == '\0')
9411 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
9414 /* This function is mapped across the sections and remembers the offset and
9415 size of each of the DWO debugging sections we are interested in. */
9418 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9420 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9421 const struct dwop_section_names *names = &dwop_section_names;
9423 if (section_is_p (sectp->name, &names->abbrev_dwo))
9425 dwo_sections->abbrev.asection = sectp;
9426 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9428 else if (section_is_p (sectp->name, &names->info_dwo))
9430 dwo_sections->info.asection = sectp;
9431 dwo_sections->info.size = bfd_get_section_size (sectp);
9433 else if (section_is_p (sectp->name, &names->line_dwo))
9435 dwo_sections->line.asection = sectp;
9436 dwo_sections->line.size = bfd_get_section_size (sectp);
9438 else if (section_is_p (sectp->name, &names->loc_dwo))
9440 dwo_sections->loc.asection = sectp;
9441 dwo_sections->loc.size = bfd_get_section_size (sectp);
9443 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9445 dwo_sections->macinfo.asection = sectp;
9446 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9448 else if (section_is_p (sectp->name, &names->macro_dwo))
9450 dwo_sections->macro.asection = sectp;
9451 dwo_sections->macro.size = bfd_get_section_size (sectp);
9453 else if (section_is_p (sectp->name, &names->str_dwo))
9455 dwo_sections->str.asection = sectp;
9456 dwo_sections->str.size = bfd_get_section_size (sectp);
9458 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9460 dwo_sections->str_offsets.asection = sectp;
9461 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9463 else if (section_is_p (sectp->name, &names->types_dwo))
9465 struct dwarf2_section_info type_section;
9467 memset (&type_section, 0, sizeof (type_section));
9468 type_section.asection = sectp;
9469 type_section.size = bfd_get_section_size (sectp);
9470 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9475 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9476 by PER_CU. This is for the non-DWP case.
9477 The result is NULL if DWO_NAME can't be found. */
9479 static struct dwo_file *
9480 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9481 const char *dwo_name, const char *comp_dir)
9483 struct objfile *objfile = dwarf2_per_objfile->objfile;
9484 struct dwo_file *dwo_file;
9486 struct cleanup *cleanups;
9488 dbfd = open_dwo_file (dwo_name, comp_dir);
9491 if (dwarf2_read_debug)
9492 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9495 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9496 dwo_file->dwo_name = dwo_name;
9497 dwo_file->comp_dir = comp_dir;
9498 dwo_file->dbfd = dbfd;
9500 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9502 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9504 dwo_file->cu = create_dwo_cu (dwo_file);
9506 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9507 dwo_file->sections.types);
9509 discard_cleanups (cleanups);
9511 if (dwarf2_read_debug)
9512 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9517 /* This function is mapped across the sections and remembers the offset and
9518 size of each of the DWP debugging sections we are interested in. */
9521 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9523 struct dwp_file *dwp_file = dwp_file_ptr;
9524 const struct dwop_section_names *names = &dwop_section_names;
9525 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9527 /* Record the ELF section number for later lookup: this is what the
9528 .debug_cu_index,.debug_tu_index tables use. */
9529 gdb_assert (elf_section_nr < dwp_file->num_sections);
9530 dwp_file->elf_sections[elf_section_nr] = sectp;
9532 /* Look for specific sections that we need. */
9533 if (section_is_p (sectp->name, &names->str_dwo))
9535 dwp_file->sections.str.asection = sectp;
9536 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9538 else if (section_is_p (sectp->name, &names->cu_index))
9540 dwp_file->sections.cu_index.asection = sectp;
9541 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9543 else if (section_is_p (sectp->name, &names->tu_index))
9545 dwp_file->sections.tu_index.asection = sectp;
9546 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9550 /* Hash function for dwp_file loaded CUs/TUs. */
9553 hash_dwp_loaded_cutus (const void *item)
9555 const struct dwo_unit *dwo_unit = item;
9557 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9558 return dwo_unit->signature;
9561 /* Equality function for dwp_file loaded CUs/TUs. */
9564 eq_dwp_loaded_cutus (const void *a, const void *b)
9566 const struct dwo_unit *dua = a;
9567 const struct dwo_unit *dub = b;
9569 return dua->signature == dub->signature;
9572 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9575 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9577 return htab_create_alloc_ex (3,
9578 hash_dwp_loaded_cutus,
9579 eq_dwp_loaded_cutus,
9581 &objfile->objfile_obstack,
9582 hashtab_obstack_allocate,
9583 dummy_obstack_deallocate);
9586 /* Try to open DWP file FILE_NAME.
9587 The result is the bfd handle of the file.
9588 If there is a problem finding or opening the file, return NULL.
9589 Upon success, the canonicalized path of the file is stored in the bfd,
9590 same as symfile_bfd_open. */
9593 open_dwp_file (const char *file_name)
9597 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9601 /* Work around upstream bug 15652.
9602 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9603 [Whether that's a "bug" is debatable, but it is getting in our way.]
9604 We have no real idea where the dwp file is, because gdb's realpath-ing
9605 of the executable's path may have discarded the needed info.
9606 [IWBN if the dwp file name was recorded in the executable, akin to
9607 .gnu_debuglink, but that doesn't exist yet.]
9608 Strip the directory from FILE_NAME and search again. */
9609 if (*debug_file_directory != '\0')
9611 /* Don't implicitly search the current directory here.
9612 If the user wants to search "." to handle this case,
9613 it must be added to debug-file-directory. */
9614 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9621 /* Initialize the use of the DWP file for the current objfile.
9622 By convention the name of the DWP file is ${objfile}.dwp.
9623 The result is NULL if it can't be found. */
9625 static struct dwp_file *
9626 open_and_init_dwp_file (void)
9628 struct objfile *objfile = dwarf2_per_objfile->objfile;
9629 struct dwp_file *dwp_file;
9632 struct cleanup *cleanups;
9634 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9635 cleanups = make_cleanup (xfree, dwp_name);
9637 dbfd = open_dwp_file (dwp_name);
9640 if (dwarf2_read_debug)
9641 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9642 do_cleanups (cleanups);
9645 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9646 dwp_file->name = bfd_get_filename (dbfd);
9647 dwp_file->dbfd = dbfd;
9648 do_cleanups (cleanups);
9650 /* +1: section 0 is unused */
9651 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9652 dwp_file->elf_sections =
9653 OBSTACK_CALLOC (&objfile->objfile_obstack,
9654 dwp_file->num_sections, asection *);
9656 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9658 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9660 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9662 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9664 if (dwarf2_read_debug)
9666 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9667 fprintf_unfiltered (gdb_stdlog,
9668 " %s CUs, %s TUs\n",
9669 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9670 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9676 /* Wrapper around open_and_init_dwp_file, only open it once. */
9678 static struct dwp_file *
9681 if (! dwarf2_per_objfile->dwp_checked)
9683 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9684 dwarf2_per_objfile->dwp_checked = 1;
9686 return dwarf2_per_objfile->dwp_file;
9689 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9690 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9691 or in the DWP file for the objfile, referenced by THIS_UNIT.
9692 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9693 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9695 This is called, for example, when wanting to read a variable with a
9696 complex location. Therefore we don't want to do file i/o for every call.
9697 Therefore we don't want to look for a DWO file on every call.
9698 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9699 then we check if we've already seen DWO_NAME, and only THEN do we check
9702 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9703 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9705 static struct dwo_unit *
9706 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9707 const char *dwo_name, const char *comp_dir,
9708 ULONGEST signature, int is_debug_types)
9710 struct objfile *objfile = dwarf2_per_objfile->objfile;
9711 const char *kind = is_debug_types ? "TU" : "CU";
9712 void **dwo_file_slot;
9713 struct dwo_file *dwo_file;
9714 struct dwp_file *dwp_file;
9716 /* First see if there's a DWP file.
9717 If we have a DWP file but didn't find the DWO inside it, don't
9718 look for the original DWO file. It makes gdb behave differently
9719 depending on whether one is debugging in the build tree. */
9721 dwp_file = get_dwp_file ();
9722 if (dwp_file != NULL)
9724 const struct dwp_hash_table *dwp_htab =
9725 is_debug_types ? dwp_file->tus : dwp_file->cus;
9727 if (dwp_htab != NULL)
9729 struct dwo_unit *dwo_cutu =
9730 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9731 signature, is_debug_types);
9733 if (dwo_cutu != NULL)
9735 if (dwarf2_read_debug)
9737 fprintf_unfiltered (gdb_stdlog,
9738 "Virtual DWO %s %s found: @%s\n",
9739 kind, hex_string (signature),
9740 host_address_to_string (dwo_cutu));
9748 /* No DWP file, look for the DWO file. */
9750 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9751 if (*dwo_file_slot == NULL)
9753 /* Read in the file and build a table of the CUs/TUs it contains. */
9754 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9756 /* NOTE: This will be NULL if unable to open the file. */
9757 dwo_file = *dwo_file_slot;
9759 if (dwo_file != NULL)
9761 struct dwo_unit *dwo_cutu = NULL;
9763 if (is_debug_types && dwo_file->tus)
9765 struct dwo_unit find_dwo_cutu;
9767 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9768 find_dwo_cutu.signature = signature;
9769 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9771 else if (!is_debug_types && dwo_file->cu)
9773 if (signature == dwo_file->cu->signature)
9774 dwo_cutu = dwo_file->cu;
9777 if (dwo_cutu != NULL)
9779 if (dwarf2_read_debug)
9781 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9782 kind, dwo_name, hex_string (signature),
9783 host_address_to_string (dwo_cutu));
9790 /* We didn't find it. This could mean a dwo_id mismatch, or
9791 someone deleted the DWO/DWP file, or the search path isn't set up
9792 correctly to find the file. */
9794 if (dwarf2_read_debug)
9796 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9797 kind, dwo_name, hex_string (signature));
9800 /* This is a warning and not a complaint because it can be caused by
9801 pilot error (e.g., user accidentally deleting the DWO). */
9802 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9804 kind, dwo_name, hex_string (signature),
9805 this_unit->is_debug_types ? "TU" : "CU",
9806 this_unit->offset.sect_off, objfile->name);
9810 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9811 See lookup_dwo_cutu_unit for details. */
9813 static struct dwo_unit *
9814 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9815 const char *dwo_name, const char *comp_dir,
9818 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9821 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9822 See lookup_dwo_cutu_unit for details. */
9824 static struct dwo_unit *
9825 lookup_dwo_type_unit (struct signatured_type *this_tu,
9826 const char *dwo_name, const char *comp_dir)
9828 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9831 /* Free all resources associated with DWO_FILE.
9832 Close the DWO file and munmap the sections.
9833 All memory should be on the objfile obstack. */
9836 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9839 struct dwarf2_section_info *section;
9841 /* Note: dbfd is NULL for virtual DWO files. */
9842 gdb_bfd_unref (dwo_file->dbfd);
9844 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9847 /* Wrapper for free_dwo_file for use in cleanups. */
9850 free_dwo_file_cleanup (void *arg)
9852 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9853 struct objfile *objfile = dwarf2_per_objfile->objfile;
9855 free_dwo_file (dwo_file, objfile);
9858 /* Traversal function for free_dwo_files. */
9861 free_dwo_file_from_slot (void **slot, void *info)
9863 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9864 struct objfile *objfile = (struct objfile *) info;
9866 free_dwo_file (dwo_file, objfile);
9871 /* Free all resources associated with DWO_FILES. */
9874 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9876 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9879 /* Read in various DIEs. */
9881 /* qsort helper for inherit_abstract_dies. */
9884 unsigned_int_compar (const void *ap, const void *bp)
9886 unsigned int a = *(unsigned int *) ap;
9887 unsigned int b = *(unsigned int *) bp;
9889 return (a > b) - (b > a);
9892 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9893 Inherit only the children of the DW_AT_abstract_origin DIE not being
9894 already referenced by DW_AT_abstract_origin from the children of the
9898 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9900 struct die_info *child_die;
9901 unsigned die_children_count;
9902 /* CU offsets which were referenced by children of the current DIE. */
9903 sect_offset *offsets;
9904 sect_offset *offsets_end, *offsetp;
9905 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9906 struct die_info *origin_die;
9907 /* Iterator of the ORIGIN_DIE children. */
9908 struct die_info *origin_child_die;
9909 struct cleanup *cleanups;
9910 struct attribute *attr;
9911 struct dwarf2_cu *origin_cu;
9912 struct pending **origin_previous_list_in_scope;
9914 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9918 /* Note that following die references may follow to a die in a
9922 origin_die = follow_die_ref (die, attr, &origin_cu);
9924 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9926 origin_previous_list_in_scope = origin_cu->list_in_scope;
9927 origin_cu->list_in_scope = cu->list_in_scope;
9929 if (die->tag != origin_die->tag
9930 && !(die->tag == DW_TAG_inlined_subroutine
9931 && origin_die->tag == DW_TAG_subprogram))
9932 complaint (&symfile_complaints,
9933 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9934 die->offset.sect_off, origin_die->offset.sect_off);
9936 child_die = die->child;
9937 die_children_count = 0;
9938 while (child_die && child_die->tag)
9940 child_die = sibling_die (child_die);
9941 die_children_count++;
9943 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9944 cleanups = make_cleanup (xfree, offsets);
9946 offsets_end = offsets;
9947 child_die = die->child;
9948 while (child_die && child_die->tag)
9950 /* For each CHILD_DIE, find the corresponding child of
9951 ORIGIN_DIE. If there is more than one layer of
9952 DW_AT_abstract_origin, follow them all; there shouldn't be,
9953 but GCC versions at least through 4.4 generate this (GCC PR
9955 struct die_info *child_origin_die = child_die;
9956 struct dwarf2_cu *child_origin_cu = cu;
9960 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9964 child_origin_die = follow_die_ref (child_origin_die, attr,
9968 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9969 counterpart may exist. */
9970 if (child_origin_die != child_die)
9972 if (child_die->tag != child_origin_die->tag
9973 && !(child_die->tag == DW_TAG_inlined_subroutine
9974 && child_origin_die->tag == DW_TAG_subprogram))
9975 complaint (&symfile_complaints,
9976 _("Child DIE 0x%x and its abstract origin 0x%x have "
9977 "different tags"), child_die->offset.sect_off,
9978 child_origin_die->offset.sect_off);
9979 if (child_origin_die->parent != origin_die)
9980 complaint (&symfile_complaints,
9981 _("Child DIE 0x%x and its abstract origin 0x%x have "
9982 "different parents"), child_die->offset.sect_off,
9983 child_origin_die->offset.sect_off);
9985 *offsets_end++ = child_origin_die->offset;
9987 child_die = sibling_die (child_die);
9989 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9990 unsigned_int_compar);
9991 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9992 if (offsetp[-1].sect_off == offsetp->sect_off)
9993 complaint (&symfile_complaints,
9994 _("Multiple children of DIE 0x%x refer "
9995 "to DIE 0x%x as their abstract origin"),
9996 die->offset.sect_off, offsetp->sect_off);
9999 origin_child_die = origin_die->child;
10000 while (origin_child_die && origin_child_die->tag)
10002 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
10003 while (offsetp < offsets_end
10004 && offsetp->sect_off < origin_child_die->offset.sect_off)
10006 if (offsetp >= offsets_end
10007 || offsetp->sect_off > origin_child_die->offset.sect_off)
10009 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
10010 process_die (origin_child_die, origin_cu);
10012 origin_child_die = sibling_die (origin_child_die);
10014 origin_cu->list_in_scope = origin_previous_list_in_scope;
10016 do_cleanups (cleanups);
10020 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10022 struct objfile *objfile = cu->objfile;
10023 struct context_stack *new;
10026 struct die_info *child_die;
10027 struct attribute *attr, *call_line, *call_file;
10029 CORE_ADDR baseaddr;
10030 struct block *block;
10031 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10032 VEC (symbolp) *template_args = NULL;
10033 struct template_symbol *templ_func = NULL;
10037 /* If we do not have call site information, we can't show the
10038 caller of this inlined function. That's too confusing, so
10039 only use the scope for local variables. */
10040 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10041 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10042 if (call_line == NULL || call_file == NULL)
10044 read_lexical_block_scope (die, cu);
10049 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10051 name = dwarf2_name (die, cu);
10053 /* Ignore functions with missing or empty names. These are actually
10054 illegal according to the DWARF standard. */
10057 complaint (&symfile_complaints,
10058 _("missing name for subprogram DIE at %d"),
10059 die->offset.sect_off);
10063 /* Ignore functions with missing or invalid low and high pc attributes. */
10064 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10066 attr = dwarf2_attr (die, DW_AT_external, cu);
10067 if (!attr || !DW_UNSND (attr))
10068 complaint (&symfile_complaints,
10069 _("cannot get low and high bounds "
10070 "for subprogram DIE at %d"),
10071 die->offset.sect_off);
10076 highpc += baseaddr;
10078 /* If we have any template arguments, then we must allocate a
10079 different sort of symbol. */
10080 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10082 if (child_die->tag == DW_TAG_template_type_param
10083 || child_die->tag == DW_TAG_template_value_param)
10085 templ_func = allocate_template_symbol (objfile);
10086 templ_func->base.is_cplus_template_function = 1;
10091 new = push_context (0, lowpc);
10092 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10093 (struct symbol *) templ_func);
10095 /* If there is a location expression for DW_AT_frame_base, record
10097 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10099 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10101 cu->list_in_scope = &local_symbols;
10103 if (die->child != NULL)
10105 child_die = die->child;
10106 while (child_die && child_die->tag)
10108 if (child_die->tag == DW_TAG_template_type_param
10109 || child_die->tag == DW_TAG_template_value_param)
10111 struct symbol *arg = new_symbol (child_die, NULL, cu);
10114 VEC_safe_push (symbolp, template_args, arg);
10117 process_die (child_die, cu);
10118 child_die = sibling_die (child_die);
10122 inherit_abstract_dies (die, cu);
10124 /* If we have a DW_AT_specification, we might need to import using
10125 directives from the context of the specification DIE. See the
10126 comment in determine_prefix. */
10127 if (cu->language == language_cplus
10128 && dwarf2_attr (die, DW_AT_specification, cu))
10130 struct dwarf2_cu *spec_cu = cu;
10131 struct die_info *spec_die = die_specification (die, &spec_cu);
10135 child_die = spec_die->child;
10136 while (child_die && child_die->tag)
10138 if (child_die->tag == DW_TAG_imported_module)
10139 process_die (child_die, spec_cu);
10140 child_die = sibling_die (child_die);
10143 /* In some cases, GCC generates specification DIEs that
10144 themselves contain DW_AT_specification attributes. */
10145 spec_die = die_specification (spec_die, &spec_cu);
10149 new = pop_context ();
10150 /* Make a block for the local symbols within. */
10151 block = finish_block (new->name, &local_symbols, new->old_blocks,
10152 lowpc, highpc, objfile);
10154 /* For C++, set the block's scope. */
10155 if ((cu->language == language_cplus || cu->language == language_fortran)
10156 && cu->processing_has_namespace_info)
10157 block_set_scope (block, determine_prefix (die, cu),
10158 &objfile->objfile_obstack);
10160 /* If we have address ranges, record them. */
10161 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10163 /* Attach template arguments to function. */
10164 if (! VEC_empty (symbolp, template_args))
10166 gdb_assert (templ_func != NULL);
10168 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10169 templ_func->template_arguments
10170 = obstack_alloc (&objfile->objfile_obstack,
10171 (templ_func->n_template_arguments
10172 * sizeof (struct symbol *)));
10173 memcpy (templ_func->template_arguments,
10174 VEC_address (symbolp, template_args),
10175 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10176 VEC_free (symbolp, template_args);
10179 /* In C++, we can have functions nested inside functions (e.g., when
10180 a function declares a class that has methods). This means that
10181 when we finish processing a function scope, we may need to go
10182 back to building a containing block's symbol lists. */
10183 local_symbols = new->locals;
10184 using_directives = new->using_directives;
10186 /* If we've finished processing a top-level function, subsequent
10187 symbols go in the file symbol list. */
10188 if (outermost_context_p ())
10189 cu->list_in_scope = &file_symbols;
10192 /* Process all the DIES contained within a lexical block scope. Start
10193 a new scope, process the dies, and then close the scope. */
10196 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10198 struct objfile *objfile = cu->objfile;
10199 struct context_stack *new;
10200 CORE_ADDR lowpc, highpc;
10201 struct die_info *child_die;
10202 CORE_ADDR baseaddr;
10204 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10206 /* Ignore blocks with missing or invalid low and high pc attributes. */
10207 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10208 as multiple lexical blocks? Handling children in a sane way would
10209 be nasty. Might be easier to properly extend generic blocks to
10210 describe ranges. */
10211 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10214 highpc += baseaddr;
10216 push_context (0, lowpc);
10217 if (die->child != NULL)
10219 child_die = die->child;
10220 while (child_die && child_die->tag)
10222 process_die (child_die, cu);
10223 child_die = sibling_die (child_die);
10226 new = pop_context ();
10228 if (local_symbols != NULL || using_directives != NULL)
10230 struct block *block
10231 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10234 /* Note that recording ranges after traversing children, as we
10235 do here, means that recording a parent's ranges entails
10236 walking across all its children's ranges as they appear in
10237 the address map, which is quadratic behavior.
10239 It would be nicer to record the parent's ranges before
10240 traversing its children, simply overriding whatever you find
10241 there. But since we don't even decide whether to create a
10242 block until after we've traversed its children, that's hard
10244 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10246 local_symbols = new->locals;
10247 using_directives = new->using_directives;
10250 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10253 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10255 struct objfile *objfile = cu->objfile;
10256 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10257 CORE_ADDR pc, baseaddr;
10258 struct attribute *attr;
10259 struct call_site *call_site, call_site_local;
10262 struct die_info *child_die;
10264 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10266 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10269 complaint (&symfile_complaints,
10270 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10271 "DIE 0x%x [in module %s]"),
10272 die->offset.sect_off, objfile->name);
10275 pc = DW_ADDR (attr) + baseaddr;
10277 if (cu->call_site_htab == NULL)
10278 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10279 NULL, &objfile->objfile_obstack,
10280 hashtab_obstack_allocate, NULL);
10281 call_site_local.pc = pc;
10282 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10285 complaint (&symfile_complaints,
10286 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10287 "DIE 0x%x [in module %s]"),
10288 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
10292 /* Count parameters at the caller. */
10295 for (child_die = die->child; child_die && child_die->tag;
10296 child_die = sibling_die (child_die))
10298 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10300 complaint (&symfile_complaints,
10301 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10302 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10303 child_die->tag, child_die->offset.sect_off, objfile->name);
10310 call_site = obstack_alloc (&objfile->objfile_obstack,
10311 (sizeof (*call_site)
10312 + (sizeof (*call_site->parameter)
10313 * (nparams - 1))));
10315 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10316 call_site->pc = pc;
10318 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10320 struct die_info *func_die;
10322 /* Skip also over DW_TAG_inlined_subroutine. */
10323 for (func_die = die->parent;
10324 func_die && func_die->tag != DW_TAG_subprogram
10325 && func_die->tag != DW_TAG_subroutine_type;
10326 func_die = func_die->parent);
10328 /* DW_AT_GNU_all_call_sites is a superset
10329 of DW_AT_GNU_all_tail_call_sites. */
10331 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10332 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10334 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10335 not complete. But keep CALL_SITE for look ups via call_site_htab,
10336 both the initial caller containing the real return address PC and
10337 the final callee containing the current PC of a chain of tail
10338 calls do not need to have the tail call list complete. But any
10339 function candidate for a virtual tail call frame searched via
10340 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10341 determined unambiguously. */
10345 struct type *func_type = NULL;
10348 func_type = get_die_type (func_die, cu);
10349 if (func_type != NULL)
10351 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10353 /* Enlist this call site to the function. */
10354 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10355 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10358 complaint (&symfile_complaints,
10359 _("Cannot find function owning DW_TAG_GNU_call_site "
10360 "DIE 0x%x [in module %s]"),
10361 die->offset.sect_off, objfile->name);
10365 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10367 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10368 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10369 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10370 /* Keep NULL DWARF_BLOCK. */;
10371 else if (attr_form_is_block (attr))
10373 struct dwarf2_locexpr_baton *dlbaton;
10375 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10376 dlbaton->data = DW_BLOCK (attr)->data;
10377 dlbaton->size = DW_BLOCK (attr)->size;
10378 dlbaton->per_cu = cu->per_cu;
10380 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10382 else if (attr_form_is_ref (attr))
10384 struct dwarf2_cu *target_cu = cu;
10385 struct die_info *target_die;
10387 target_die = follow_die_ref (die, attr, &target_cu);
10388 gdb_assert (target_cu->objfile == objfile);
10389 if (die_is_declaration (target_die, target_cu))
10391 const char *target_physname = NULL;
10392 struct attribute *target_attr;
10394 /* Prefer the mangled name; otherwise compute the demangled one. */
10395 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10396 if (target_attr == NULL)
10397 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10399 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10400 target_physname = DW_STRING (target_attr);
10402 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10403 if (target_physname == NULL)
10404 complaint (&symfile_complaints,
10405 _("DW_AT_GNU_call_site_target target DIE has invalid "
10406 "physname, for referencing DIE 0x%x [in module %s]"),
10407 die->offset.sect_off, objfile->name);
10409 SET_FIELD_PHYSNAME (call_site->target, target_physname);
10415 /* DW_AT_entry_pc should be preferred. */
10416 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10417 complaint (&symfile_complaints,
10418 _("DW_AT_GNU_call_site_target target DIE has invalid "
10419 "low pc, for referencing DIE 0x%x [in module %s]"),
10420 die->offset.sect_off, objfile->name);
10422 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10426 complaint (&symfile_complaints,
10427 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10428 "block nor reference, for DIE 0x%x [in module %s]"),
10429 die->offset.sect_off, objfile->name);
10431 call_site->per_cu = cu->per_cu;
10433 for (child_die = die->child;
10434 child_die && child_die->tag;
10435 child_die = sibling_die (child_die))
10437 struct call_site_parameter *parameter;
10438 struct attribute *loc, *origin;
10440 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10442 /* Already printed the complaint above. */
10446 gdb_assert (call_site->parameter_count < nparams);
10447 parameter = &call_site->parameter[call_site->parameter_count];
10449 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10450 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10451 register is contained in DW_AT_GNU_call_site_value. */
10453 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10454 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10455 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
10457 sect_offset offset;
10459 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10460 offset = dwarf2_get_ref_die_offset (origin);
10461 if (!offset_in_cu_p (&cu->header, offset))
10463 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10464 binding can be done only inside one CU. Such referenced DIE
10465 therefore cannot be even moved to DW_TAG_partial_unit. */
10466 complaint (&symfile_complaints,
10467 _("DW_AT_abstract_origin offset is not in CU for "
10468 "DW_TAG_GNU_call_site child DIE 0x%x "
10470 child_die->offset.sect_off, objfile->name);
10473 parameter->u.param_offset.cu_off = (offset.sect_off
10474 - cu->header.offset.sect_off);
10476 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10478 complaint (&symfile_complaints,
10479 _("No DW_FORM_block* DW_AT_location for "
10480 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10481 child_die->offset.sect_off, objfile->name);
10486 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10487 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10488 if (parameter->u.dwarf_reg != -1)
10489 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10490 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10491 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10492 ¶meter->u.fb_offset))
10493 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10496 complaint (&symfile_complaints,
10497 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10498 "for DW_FORM_block* DW_AT_location is supported for "
10499 "DW_TAG_GNU_call_site child DIE 0x%x "
10501 child_die->offset.sect_off, objfile->name);
10506 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10507 if (!attr_form_is_block (attr))
10509 complaint (&symfile_complaints,
10510 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10511 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10512 child_die->offset.sect_off, objfile->name);
10515 parameter->value = DW_BLOCK (attr)->data;
10516 parameter->value_size = DW_BLOCK (attr)->size;
10518 /* Parameters are not pre-cleared by memset above. */
10519 parameter->data_value = NULL;
10520 parameter->data_value_size = 0;
10521 call_site->parameter_count++;
10523 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10526 if (!attr_form_is_block (attr))
10527 complaint (&symfile_complaints,
10528 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10529 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10530 child_die->offset.sect_off, objfile->name);
10533 parameter->data_value = DW_BLOCK (attr)->data;
10534 parameter->data_value_size = DW_BLOCK (attr)->size;
10540 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10541 Return 1 if the attributes are present and valid, otherwise, return 0.
10542 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10545 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10546 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10547 struct partial_symtab *ranges_pst)
10549 struct objfile *objfile = cu->objfile;
10550 struct comp_unit_head *cu_header = &cu->header;
10551 bfd *obfd = objfile->obfd;
10552 unsigned int addr_size = cu_header->addr_size;
10553 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10554 /* Base address selection entry. */
10557 unsigned int dummy;
10558 const gdb_byte *buffer;
10562 CORE_ADDR high = 0;
10563 CORE_ADDR baseaddr;
10565 found_base = cu->base_known;
10566 base = cu->base_address;
10568 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10569 if (offset >= dwarf2_per_objfile->ranges.size)
10571 complaint (&symfile_complaints,
10572 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10576 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10578 /* Read in the largest possible address. */
10579 marker = read_address (obfd, buffer, cu, &dummy);
10580 if ((marker & mask) == mask)
10582 /* If we found the largest possible address, then
10583 read the base address. */
10584 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10585 buffer += 2 * addr_size;
10586 offset += 2 * addr_size;
10592 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10596 CORE_ADDR range_beginning, range_end;
10598 range_beginning = read_address (obfd, buffer, cu, &dummy);
10599 buffer += addr_size;
10600 range_end = read_address (obfd, buffer, cu, &dummy);
10601 buffer += addr_size;
10602 offset += 2 * addr_size;
10604 /* An end of list marker is a pair of zero addresses. */
10605 if (range_beginning == 0 && range_end == 0)
10606 /* Found the end of list entry. */
10609 /* Each base address selection entry is a pair of 2 values.
10610 The first is the largest possible address, the second is
10611 the base address. Check for a base address here. */
10612 if ((range_beginning & mask) == mask)
10614 /* If we found the largest possible address, then
10615 read the base address. */
10616 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10623 /* We have no valid base address for the ranges
10625 complaint (&symfile_complaints,
10626 _("Invalid .debug_ranges data (no base address)"));
10630 if (range_beginning > range_end)
10632 /* Inverted range entries are invalid. */
10633 complaint (&symfile_complaints,
10634 _("Invalid .debug_ranges data (inverted range)"));
10638 /* Empty range entries have no effect. */
10639 if (range_beginning == range_end)
10642 range_beginning += base;
10645 /* A not-uncommon case of bad debug info.
10646 Don't pollute the addrmap with bad data. */
10647 if (range_beginning + baseaddr == 0
10648 && !dwarf2_per_objfile->has_section_at_zero)
10650 complaint (&symfile_complaints,
10651 _(".debug_ranges entry has start address of zero"
10652 " [in module %s]"), objfile->name);
10656 if (ranges_pst != NULL)
10657 addrmap_set_empty (objfile->psymtabs_addrmap,
10658 range_beginning + baseaddr,
10659 range_end - 1 + baseaddr,
10662 /* FIXME: This is recording everything as a low-high
10663 segment of consecutive addresses. We should have a
10664 data structure for discontiguous block ranges
10668 low = range_beginning;
10674 if (range_beginning < low)
10675 low = range_beginning;
10676 if (range_end > high)
10682 /* If the first entry is an end-of-list marker, the range
10683 describes an empty scope, i.e. no instructions. */
10689 *high_return = high;
10693 /* Get low and high pc attributes from a die. Return 1 if the attributes
10694 are present and valid, otherwise, return 0. Return -1 if the range is
10695 discontinuous, i.e. derived from DW_AT_ranges information. */
10698 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10699 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10700 struct partial_symtab *pst)
10702 struct attribute *attr;
10703 struct attribute *attr_high;
10705 CORE_ADDR high = 0;
10708 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10711 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10714 low = DW_ADDR (attr);
10715 if (attr_high->form == DW_FORM_addr
10716 || attr_high->form == DW_FORM_GNU_addr_index)
10717 high = DW_ADDR (attr_high);
10719 high = low + DW_UNSND (attr_high);
10722 /* Found high w/o low attribute. */
10725 /* Found consecutive range of addresses. */
10730 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10733 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10734 We take advantage of the fact that DW_AT_ranges does not appear
10735 in DW_TAG_compile_unit of DWO files. */
10736 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10737 unsigned int ranges_offset = (DW_UNSND (attr)
10738 + (need_ranges_base
10742 /* Value of the DW_AT_ranges attribute is the offset in the
10743 .debug_ranges section. */
10744 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10746 /* Found discontinuous range of addresses. */
10751 /* read_partial_die has also the strict LOW < HIGH requirement. */
10755 /* When using the GNU linker, .gnu.linkonce. sections are used to
10756 eliminate duplicate copies of functions and vtables and such.
10757 The linker will arbitrarily choose one and discard the others.
10758 The AT_*_pc values for such functions refer to local labels in
10759 these sections. If the section from that file was discarded, the
10760 labels are not in the output, so the relocs get a value of 0.
10761 If this is a discarded function, mark the pc bounds as invalid,
10762 so that GDB will ignore it. */
10763 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10772 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10773 its low and high PC addresses. Do nothing if these addresses could not
10774 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10775 and HIGHPC to the high address if greater than HIGHPC. */
10778 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10779 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10780 struct dwarf2_cu *cu)
10782 CORE_ADDR low, high;
10783 struct die_info *child = die->child;
10785 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10787 *lowpc = min (*lowpc, low);
10788 *highpc = max (*highpc, high);
10791 /* If the language does not allow nested subprograms (either inside
10792 subprograms or lexical blocks), we're done. */
10793 if (cu->language != language_ada)
10796 /* Check all the children of the given DIE. If it contains nested
10797 subprograms, then check their pc bounds. Likewise, we need to
10798 check lexical blocks as well, as they may also contain subprogram
10800 while (child && child->tag)
10802 if (child->tag == DW_TAG_subprogram
10803 || child->tag == DW_TAG_lexical_block)
10804 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10805 child = sibling_die (child);
10809 /* Get the low and high pc's represented by the scope DIE, and store
10810 them in *LOWPC and *HIGHPC. If the correct values can't be
10811 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10814 get_scope_pc_bounds (struct die_info *die,
10815 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10816 struct dwarf2_cu *cu)
10818 CORE_ADDR best_low = (CORE_ADDR) -1;
10819 CORE_ADDR best_high = (CORE_ADDR) 0;
10820 CORE_ADDR current_low, current_high;
10822 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
10824 best_low = current_low;
10825 best_high = current_high;
10829 struct die_info *child = die->child;
10831 while (child && child->tag)
10833 switch (child->tag) {
10834 case DW_TAG_subprogram:
10835 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10837 case DW_TAG_namespace:
10838 case DW_TAG_module:
10839 /* FIXME: carlton/2004-01-16: Should we do this for
10840 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10841 that current GCC's always emit the DIEs corresponding
10842 to definitions of methods of classes as children of a
10843 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10844 the DIEs giving the declarations, which could be
10845 anywhere). But I don't see any reason why the
10846 standards says that they have to be there. */
10847 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
10849 if (current_low != ((CORE_ADDR) -1))
10851 best_low = min (best_low, current_low);
10852 best_high = max (best_high, current_high);
10860 child = sibling_die (child);
10865 *highpc = best_high;
10868 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10872 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10873 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10875 struct objfile *objfile = cu->objfile;
10876 struct attribute *attr;
10877 struct attribute *attr_high;
10879 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10882 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10885 CORE_ADDR low = DW_ADDR (attr);
10887 if (attr_high->form == DW_FORM_addr
10888 || attr_high->form == DW_FORM_GNU_addr_index)
10889 high = DW_ADDR (attr_high);
10891 high = low + DW_UNSND (attr_high);
10893 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10897 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10900 bfd *obfd = objfile->obfd;
10901 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10902 We take advantage of the fact that DW_AT_ranges does not appear
10903 in DW_TAG_compile_unit of DWO files. */
10904 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10906 /* The value of the DW_AT_ranges attribute is the offset of the
10907 address range list in the .debug_ranges section. */
10908 unsigned long offset = (DW_UNSND (attr)
10909 + (need_ranges_base ? cu->ranges_base : 0));
10910 const gdb_byte *buffer;
10912 /* For some target architectures, but not others, the
10913 read_address function sign-extends the addresses it returns.
10914 To recognize base address selection entries, we need a
10916 unsigned int addr_size = cu->header.addr_size;
10917 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10919 /* The base address, to which the next pair is relative. Note
10920 that this 'base' is a DWARF concept: most entries in a range
10921 list are relative, to reduce the number of relocs against the
10922 debugging information. This is separate from this function's
10923 'baseaddr' argument, which GDB uses to relocate debugging
10924 information from a shared library based on the address at
10925 which the library was loaded. */
10926 CORE_ADDR base = cu->base_address;
10927 int base_known = cu->base_known;
10929 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10930 if (offset >= dwarf2_per_objfile->ranges.size)
10932 complaint (&symfile_complaints,
10933 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10937 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10941 unsigned int bytes_read;
10942 CORE_ADDR start, end;
10944 start = read_address (obfd, buffer, cu, &bytes_read);
10945 buffer += bytes_read;
10946 end = read_address (obfd, buffer, cu, &bytes_read);
10947 buffer += bytes_read;
10949 /* Did we find the end of the range list? */
10950 if (start == 0 && end == 0)
10953 /* Did we find a base address selection entry? */
10954 else if ((start & base_select_mask) == base_select_mask)
10960 /* We found an ordinary address range. */
10965 complaint (&symfile_complaints,
10966 _("Invalid .debug_ranges data "
10967 "(no base address)"));
10973 /* Inverted range entries are invalid. */
10974 complaint (&symfile_complaints,
10975 _("Invalid .debug_ranges data "
10976 "(inverted range)"));
10980 /* Empty range entries have no effect. */
10984 start += base + baseaddr;
10985 end += base + baseaddr;
10987 /* A not-uncommon case of bad debug info.
10988 Don't pollute the addrmap with bad data. */
10989 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10991 complaint (&symfile_complaints,
10992 _(".debug_ranges entry has start address of zero"
10993 " [in module %s]"), objfile->name);
10997 record_block_range (block, start, end - 1);
11003 /* Check whether the producer field indicates either of GCC < 4.6, or the
11004 Intel C/C++ compiler, and cache the result in CU. */
11007 check_producer (struct dwarf2_cu *cu)
11010 int major, minor, release;
11012 if (cu->producer == NULL)
11014 /* For unknown compilers expect their behavior is DWARF version
11017 GCC started to support .debug_types sections by -gdwarf-4 since
11018 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11019 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11020 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11021 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11023 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
11025 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11027 cs = &cu->producer[strlen ("GNU ")];
11028 while (*cs && !isdigit (*cs))
11030 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11032 /* Not recognized as GCC. */
11036 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11037 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11040 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11041 cu->producer_is_icc = 1;
11044 /* For other non-GCC compilers, expect their behavior is DWARF version
11048 cu->checked_producer = 1;
11051 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11052 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11053 during 4.6.0 experimental. */
11056 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11058 if (!cu->checked_producer)
11059 check_producer (cu);
11061 return cu->producer_is_gxx_lt_4_6;
11064 /* Return the default accessibility type if it is not overriden by
11065 DW_AT_accessibility. */
11067 static enum dwarf_access_attribute
11068 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11070 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11072 /* The default DWARF 2 accessibility for members is public, the default
11073 accessibility for inheritance is private. */
11075 if (die->tag != DW_TAG_inheritance)
11076 return DW_ACCESS_public;
11078 return DW_ACCESS_private;
11082 /* DWARF 3+ defines the default accessibility a different way. The same
11083 rules apply now for DW_TAG_inheritance as for the members and it only
11084 depends on the container kind. */
11086 if (die->parent->tag == DW_TAG_class_type)
11087 return DW_ACCESS_private;
11089 return DW_ACCESS_public;
11093 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11094 offset. If the attribute was not found return 0, otherwise return
11095 1. If it was found but could not properly be handled, set *OFFSET
11099 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11102 struct attribute *attr;
11104 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11109 /* Note that we do not check for a section offset first here.
11110 This is because DW_AT_data_member_location is new in DWARF 4,
11111 so if we see it, we can assume that a constant form is really
11112 a constant and not a section offset. */
11113 if (attr_form_is_constant (attr))
11114 *offset = dwarf2_get_attr_constant_value (attr, 0);
11115 else if (attr_form_is_section_offset (attr))
11116 dwarf2_complex_location_expr_complaint ();
11117 else if (attr_form_is_block (attr))
11118 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11120 dwarf2_complex_location_expr_complaint ();
11128 /* Add an aggregate field to the field list. */
11131 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11132 struct dwarf2_cu *cu)
11134 struct objfile *objfile = cu->objfile;
11135 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11136 struct nextfield *new_field;
11137 struct attribute *attr;
11139 const char *fieldname = "";
11141 /* Allocate a new field list entry and link it in. */
11142 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11143 make_cleanup (xfree, new_field);
11144 memset (new_field, 0, sizeof (struct nextfield));
11146 if (die->tag == DW_TAG_inheritance)
11148 new_field->next = fip->baseclasses;
11149 fip->baseclasses = new_field;
11153 new_field->next = fip->fields;
11154 fip->fields = new_field;
11158 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11160 new_field->accessibility = DW_UNSND (attr);
11162 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11163 if (new_field->accessibility != DW_ACCESS_public)
11164 fip->non_public_fields = 1;
11166 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11168 new_field->virtuality = DW_UNSND (attr);
11170 new_field->virtuality = DW_VIRTUALITY_none;
11172 fp = &new_field->field;
11174 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11178 /* Data member other than a C++ static data member. */
11180 /* Get type of field. */
11181 fp->type = die_type (die, cu);
11183 SET_FIELD_BITPOS (*fp, 0);
11185 /* Get bit size of field (zero if none). */
11186 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11189 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11193 FIELD_BITSIZE (*fp) = 0;
11196 /* Get bit offset of field. */
11197 if (handle_data_member_location (die, cu, &offset))
11198 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11199 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11202 if (gdbarch_bits_big_endian (gdbarch))
11204 /* For big endian bits, the DW_AT_bit_offset gives the
11205 additional bit offset from the MSB of the containing
11206 anonymous object to the MSB of the field. We don't
11207 have to do anything special since we don't need to
11208 know the size of the anonymous object. */
11209 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
11213 /* For little endian bits, compute the bit offset to the
11214 MSB of the anonymous object, subtract off the number of
11215 bits from the MSB of the field to the MSB of the
11216 object, and then subtract off the number of bits of
11217 the field itself. The result is the bit offset of
11218 the LSB of the field. */
11219 int anonymous_size;
11220 int bit_offset = DW_UNSND (attr);
11222 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11225 /* The size of the anonymous object containing
11226 the bit field is explicit, so use the
11227 indicated size (in bytes). */
11228 anonymous_size = DW_UNSND (attr);
11232 /* The size of the anonymous object containing
11233 the bit field must be inferred from the type
11234 attribute of the data member containing the
11236 anonymous_size = TYPE_LENGTH (fp->type);
11238 SET_FIELD_BITPOS (*fp,
11239 (FIELD_BITPOS (*fp)
11240 + anonymous_size * bits_per_byte
11241 - bit_offset - FIELD_BITSIZE (*fp)));
11245 /* Get name of field. */
11246 fieldname = dwarf2_name (die, cu);
11247 if (fieldname == NULL)
11250 /* The name is already allocated along with this objfile, so we don't
11251 need to duplicate it for the type. */
11252 fp->name = fieldname;
11254 /* Change accessibility for artificial fields (e.g. virtual table
11255 pointer or virtual base class pointer) to private. */
11256 if (dwarf2_attr (die, DW_AT_artificial, cu))
11258 FIELD_ARTIFICIAL (*fp) = 1;
11259 new_field->accessibility = DW_ACCESS_private;
11260 fip->non_public_fields = 1;
11263 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11265 /* C++ static member. */
11267 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11268 is a declaration, but all versions of G++ as of this writing
11269 (so through at least 3.2.1) incorrectly generate
11270 DW_TAG_variable tags. */
11272 const char *physname;
11274 /* Get name of field. */
11275 fieldname = dwarf2_name (die, cu);
11276 if (fieldname == NULL)
11279 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11281 /* Only create a symbol if this is an external value.
11282 new_symbol checks this and puts the value in the global symbol
11283 table, which we want. If it is not external, new_symbol
11284 will try to put the value in cu->list_in_scope which is wrong. */
11285 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11287 /* A static const member, not much different than an enum as far as
11288 we're concerned, except that we can support more types. */
11289 new_symbol (die, NULL, cu);
11292 /* Get physical name. */
11293 physname = dwarf2_physname (fieldname, die, cu);
11295 /* The name is already allocated along with this objfile, so we don't
11296 need to duplicate it for the type. */
11297 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
11298 FIELD_TYPE (*fp) = die_type (die, cu);
11299 FIELD_NAME (*fp) = fieldname;
11301 else if (die->tag == DW_TAG_inheritance)
11305 /* C++ base class field. */
11306 if (handle_data_member_location (die, cu, &offset))
11307 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11308 FIELD_BITSIZE (*fp) = 0;
11309 FIELD_TYPE (*fp) = die_type (die, cu);
11310 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11311 fip->nbaseclasses++;
11315 /* Add a typedef defined in the scope of the FIP's class. */
11318 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11319 struct dwarf2_cu *cu)
11321 struct objfile *objfile = cu->objfile;
11322 struct typedef_field_list *new_field;
11323 struct attribute *attr;
11324 struct typedef_field *fp;
11325 char *fieldname = "";
11327 /* Allocate a new field list entry and link it in. */
11328 new_field = xzalloc (sizeof (*new_field));
11329 make_cleanup (xfree, new_field);
11331 gdb_assert (die->tag == DW_TAG_typedef);
11333 fp = &new_field->field;
11335 /* Get name of field. */
11336 fp->name = dwarf2_name (die, cu);
11337 if (fp->name == NULL)
11340 fp->type = read_type_die (die, cu);
11342 new_field->next = fip->typedef_field_list;
11343 fip->typedef_field_list = new_field;
11344 fip->typedef_field_list_count++;
11347 /* Create the vector of fields, and attach it to the type. */
11350 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11351 struct dwarf2_cu *cu)
11353 int nfields = fip->nfields;
11355 /* Record the field count, allocate space for the array of fields,
11356 and create blank accessibility bitfields if necessary. */
11357 TYPE_NFIELDS (type) = nfields;
11358 TYPE_FIELDS (type) = (struct field *)
11359 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11360 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11362 if (fip->non_public_fields && cu->language != language_ada)
11364 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11366 TYPE_FIELD_PRIVATE_BITS (type) =
11367 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11368 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11370 TYPE_FIELD_PROTECTED_BITS (type) =
11371 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11372 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11374 TYPE_FIELD_IGNORE_BITS (type) =
11375 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11376 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
11379 /* If the type has baseclasses, allocate and clear a bit vector for
11380 TYPE_FIELD_VIRTUAL_BITS. */
11381 if (fip->nbaseclasses && cu->language != language_ada)
11383 int num_bytes = B_BYTES (fip->nbaseclasses);
11384 unsigned char *pointer;
11386 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11387 pointer = TYPE_ALLOC (type, num_bytes);
11388 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
11389 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11390 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11393 /* Copy the saved-up fields into the field vector. Start from the head of
11394 the list, adding to the tail of the field array, so that they end up in
11395 the same order in the array in which they were added to the list. */
11396 while (nfields-- > 0)
11398 struct nextfield *fieldp;
11402 fieldp = fip->fields;
11403 fip->fields = fieldp->next;
11407 fieldp = fip->baseclasses;
11408 fip->baseclasses = fieldp->next;
11411 TYPE_FIELD (type, nfields) = fieldp->field;
11412 switch (fieldp->accessibility)
11414 case DW_ACCESS_private:
11415 if (cu->language != language_ada)
11416 SET_TYPE_FIELD_PRIVATE (type, nfields);
11419 case DW_ACCESS_protected:
11420 if (cu->language != language_ada)
11421 SET_TYPE_FIELD_PROTECTED (type, nfields);
11424 case DW_ACCESS_public:
11428 /* Unknown accessibility. Complain and treat it as public. */
11430 complaint (&symfile_complaints, _("unsupported accessibility %d"),
11431 fieldp->accessibility);
11435 if (nfields < fip->nbaseclasses)
11437 switch (fieldp->virtuality)
11439 case DW_VIRTUALITY_virtual:
11440 case DW_VIRTUALITY_pure_virtual:
11441 if (cu->language == language_ada)
11442 error (_("unexpected virtuality in component of Ada type"));
11443 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11450 /* Return true if this member function is a constructor, false
11454 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11456 const char *fieldname;
11457 const char *typename;
11460 if (die->parent == NULL)
11463 if (die->parent->tag != DW_TAG_structure_type
11464 && die->parent->tag != DW_TAG_union_type
11465 && die->parent->tag != DW_TAG_class_type)
11468 fieldname = dwarf2_name (die, cu);
11469 typename = dwarf2_name (die->parent, cu);
11470 if (fieldname == NULL || typename == NULL)
11473 len = strlen (fieldname);
11474 return (strncmp (fieldname, typename, len) == 0
11475 && (typename[len] == '\0' || typename[len] == '<'));
11478 /* Add a member function to the proper fieldlist. */
11481 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11482 struct type *type, struct dwarf2_cu *cu)
11484 struct objfile *objfile = cu->objfile;
11485 struct attribute *attr;
11486 struct fnfieldlist *flp;
11488 struct fn_field *fnp;
11489 const char *fieldname;
11490 struct nextfnfield *new_fnfield;
11491 struct type *this_type;
11492 enum dwarf_access_attribute accessibility;
11494 if (cu->language == language_ada)
11495 error (_("unexpected member function in Ada type"));
11497 /* Get name of member function. */
11498 fieldname = dwarf2_name (die, cu);
11499 if (fieldname == NULL)
11502 /* Look up member function name in fieldlist. */
11503 for (i = 0; i < fip->nfnfields; i++)
11505 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11509 /* Create new list element if necessary. */
11510 if (i < fip->nfnfields)
11511 flp = &fip->fnfieldlists[i];
11514 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11516 fip->fnfieldlists = (struct fnfieldlist *)
11517 xrealloc (fip->fnfieldlists,
11518 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11519 * sizeof (struct fnfieldlist));
11520 if (fip->nfnfields == 0)
11521 make_cleanup (free_current_contents, &fip->fnfieldlists);
11523 flp = &fip->fnfieldlists[fip->nfnfields];
11524 flp->name = fieldname;
11527 i = fip->nfnfields++;
11530 /* Create a new member function field and chain it to the field list
11532 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11533 make_cleanup (xfree, new_fnfield);
11534 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11535 new_fnfield->next = flp->head;
11536 flp->head = new_fnfield;
11539 /* Fill in the member function field info. */
11540 fnp = &new_fnfield->fnfield;
11542 /* Delay processing of the physname until later. */
11543 if (cu->language == language_cplus || cu->language == language_java)
11545 add_to_method_list (type, i, flp->length - 1, fieldname,
11550 const char *physname = dwarf2_physname (fieldname, die, cu);
11551 fnp->physname = physname ? physname : "";
11554 fnp->type = alloc_type (objfile);
11555 this_type = read_type_die (die, cu);
11556 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11558 int nparams = TYPE_NFIELDS (this_type);
11560 /* TYPE is the domain of this method, and THIS_TYPE is the type
11561 of the method itself (TYPE_CODE_METHOD). */
11562 smash_to_method_type (fnp->type, type,
11563 TYPE_TARGET_TYPE (this_type),
11564 TYPE_FIELDS (this_type),
11565 TYPE_NFIELDS (this_type),
11566 TYPE_VARARGS (this_type));
11568 /* Handle static member functions.
11569 Dwarf2 has no clean way to discern C++ static and non-static
11570 member functions. G++ helps GDB by marking the first
11571 parameter for non-static member functions (which is the this
11572 pointer) as artificial. We obtain this information from
11573 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11574 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11575 fnp->voffset = VOFFSET_STATIC;
11578 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11579 dwarf2_full_name (fieldname, die, cu));
11581 /* Get fcontext from DW_AT_containing_type if present. */
11582 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11583 fnp->fcontext = die_containing_type (die, cu);
11585 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11586 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11588 /* Get accessibility. */
11589 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11591 accessibility = DW_UNSND (attr);
11593 accessibility = dwarf2_default_access_attribute (die, cu);
11594 switch (accessibility)
11596 case DW_ACCESS_private:
11597 fnp->is_private = 1;
11599 case DW_ACCESS_protected:
11600 fnp->is_protected = 1;
11604 /* Check for artificial methods. */
11605 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11606 if (attr && DW_UNSND (attr) != 0)
11607 fnp->is_artificial = 1;
11609 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11611 /* Get index in virtual function table if it is a virtual member
11612 function. For older versions of GCC, this is an offset in the
11613 appropriate virtual table, as specified by DW_AT_containing_type.
11614 For everyone else, it is an expression to be evaluated relative
11615 to the object address. */
11617 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11620 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11622 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11624 /* Old-style GCC. */
11625 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11627 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11628 || (DW_BLOCK (attr)->size > 1
11629 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11630 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11632 struct dwarf_block blk;
11635 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11637 blk.size = DW_BLOCK (attr)->size - offset;
11638 blk.data = DW_BLOCK (attr)->data + offset;
11639 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11640 if ((fnp->voffset % cu->header.addr_size) != 0)
11641 dwarf2_complex_location_expr_complaint ();
11643 fnp->voffset /= cu->header.addr_size;
11647 dwarf2_complex_location_expr_complaint ();
11649 if (!fnp->fcontext)
11650 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11652 else if (attr_form_is_section_offset (attr))
11654 dwarf2_complex_location_expr_complaint ();
11658 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11665 if (attr && DW_UNSND (attr))
11667 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11668 complaint (&symfile_complaints,
11669 _("Member function \"%s\" (offset %d) is virtual "
11670 "but the vtable offset is not specified"),
11671 fieldname, die->offset.sect_off);
11672 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11673 TYPE_CPLUS_DYNAMIC (type) = 1;
11678 /* Create the vector of member function fields, and attach it to the type. */
11681 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11682 struct dwarf2_cu *cu)
11684 struct fnfieldlist *flp;
11687 if (cu->language == language_ada)
11688 error (_("unexpected member functions in Ada type"));
11690 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11691 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11692 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11694 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11696 struct nextfnfield *nfp = flp->head;
11697 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11700 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11701 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11702 fn_flp->fn_fields = (struct fn_field *)
11703 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11704 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11705 fn_flp->fn_fields[k] = nfp->fnfield;
11708 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11711 /* Returns non-zero if NAME is the name of a vtable member in CU's
11712 language, zero otherwise. */
11714 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11716 static const char vptr[] = "_vptr";
11717 static const char vtable[] = "vtable";
11719 /* Look for the C++ and Java forms of the vtable. */
11720 if ((cu->language == language_java
11721 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11722 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11723 && is_cplus_marker (name[sizeof (vptr) - 1])))
11729 /* GCC outputs unnamed structures that are really pointers to member
11730 functions, with the ABI-specified layout. If TYPE describes
11731 such a structure, smash it into a member function type.
11733 GCC shouldn't do this; it should just output pointer to member DIEs.
11734 This is GCC PR debug/28767. */
11737 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11739 struct type *pfn_type, *domain_type, *new_type;
11741 /* Check for a structure with no name and two children. */
11742 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11745 /* Check for __pfn and __delta members. */
11746 if (TYPE_FIELD_NAME (type, 0) == NULL
11747 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11748 || TYPE_FIELD_NAME (type, 1) == NULL
11749 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11752 /* Find the type of the method. */
11753 pfn_type = TYPE_FIELD_TYPE (type, 0);
11754 if (pfn_type == NULL
11755 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11756 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11759 /* Look for the "this" argument. */
11760 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11761 if (TYPE_NFIELDS (pfn_type) == 0
11762 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11763 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11766 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11767 new_type = alloc_type (objfile);
11768 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11769 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11770 TYPE_VARARGS (pfn_type));
11771 smash_to_methodptr_type (type, new_type);
11774 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11778 producer_is_icc (struct dwarf2_cu *cu)
11780 if (!cu->checked_producer)
11781 check_producer (cu);
11783 return cu->producer_is_icc;
11786 /* Called when we find the DIE that starts a structure or union scope
11787 (definition) to create a type for the structure or union. Fill in
11788 the type's name and general properties; the members will not be
11789 processed until process_structure_scope.
11791 NOTE: we need to call these functions regardless of whether or not the
11792 DIE has a DW_AT_name attribute, since it might be an anonymous
11793 structure or union. This gets the type entered into our set of
11794 user defined types.
11796 However, if the structure is incomplete (an opaque struct/union)
11797 then suppress creating a symbol table entry for it since gdb only
11798 wants to find the one with the complete definition. Note that if
11799 it is complete, we just call new_symbol, which does it's own
11800 checking about whether the struct/union is anonymous or not (and
11801 suppresses creating a symbol table entry itself). */
11803 static struct type *
11804 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11806 struct objfile *objfile = cu->objfile;
11808 struct attribute *attr;
11811 /* If the definition of this type lives in .debug_types, read that type.
11812 Don't follow DW_AT_specification though, that will take us back up
11813 the chain and we want to go down. */
11814 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11817 type = get_DW_AT_signature_type (die, attr, cu);
11819 /* The type's CU may not be the same as CU.
11820 Ensure TYPE is recorded with CU in die_type_hash. */
11821 return set_die_type (die, type, cu);
11824 type = alloc_type (objfile);
11825 INIT_CPLUS_SPECIFIC (type);
11827 name = dwarf2_name (die, cu);
11830 if (cu->language == language_cplus
11831 || cu->language == language_java)
11833 const char *full_name = dwarf2_full_name (name, die, cu);
11835 /* dwarf2_full_name might have already finished building the DIE's
11836 type. If so, there is no need to continue. */
11837 if (get_die_type (die, cu) != NULL)
11838 return get_die_type (die, cu);
11840 TYPE_TAG_NAME (type) = full_name;
11841 if (die->tag == DW_TAG_structure_type
11842 || die->tag == DW_TAG_class_type)
11843 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11847 /* The name is already allocated along with this objfile, so
11848 we don't need to duplicate it for the type. */
11849 TYPE_TAG_NAME (type) = name;
11850 if (die->tag == DW_TAG_class_type)
11851 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11855 if (die->tag == DW_TAG_structure_type)
11857 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11859 else if (die->tag == DW_TAG_union_type)
11861 TYPE_CODE (type) = TYPE_CODE_UNION;
11865 TYPE_CODE (type) = TYPE_CODE_CLASS;
11868 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11869 TYPE_DECLARED_CLASS (type) = 1;
11871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11874 TYPE_LENGTH (type) = DW_UNSND (attr);
11878 TYPE_LENGTH (type) = 0;
11881 if (producer_is_icc (cu))
11883 /* ICC does not output the required DW_AT_declaration
11884 on incomplete types, but gives them a size of zero. */
11887 TYPE_STUB_SUPPORTED (type) = 1;
11889 if (die_is_declaration (die, cu))
11890 TYPE_STUB (type) = 1;
11891 else if (attr == NULL && die->child == NULL
11892 && producer_is_realview (cu->producer))
11893 /* RealView does not output the required DW_AT_declaration
11894 on incomplete types. */
11895 TYPE_STUB (type) = 1;
11897 /* We need to add the type field to the die immediately so we don't
11898 infinitely recurse when dealing with pointers to the structure
11899 type within the structure itself. */
11900 set_die_type (die, type, cu);
11902 /* set_die_type should be already done. */
11903 set_descriptive_type (type, die, cu);
11908 /* Finish creating a structure or union type, including filling in
11909 its members and creating a symbol for it. */
11912 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11914 struct objfile *objfile = cu->objfile;
11915 struct die_info *child_die = die->child;
11918 type = get_die_type (die, cu);
11920 type = read_structure_type (die, cu);
11922 if (die->child != NULL && ! die_is_declaration (die, cu))
11924 struct field_info fi;
11925 struct die_info *child_die;
11926 VEC (symbolp) *template_args = NULL;
11927 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11929 memset (&fi, 0, sizeof (struct field_info));
11931 child_die = die->child;
11933 while (child_die && child_die->tag)
11935 if (child_die->tag == DW_TAG_member
11936 || child_die->tag == DW_TAG_variable)
11938 /* NOTE: carlton/2002-11-05: A C++ static data member
11939 should be a DW_TAG_member that is a declaration, but
11940 all versions of G++ as of this writing (so through at
11941 least 3.2.1) incorrectly generate DW_TAG_variable
11942 tags for them instead. */
11943 dwarf2_add_field (&fi, child_die, cu);
11945 else if (child_die->tag == DW_TAG_subprogram)
11947 /* C++ member function. */
11948 dwarf2_add_member_fn (&fi, child_die, type, cu);
11950 else if (child_die->tag == DW_TAG_inheritance)
11952 /* C++ base class field. */
11953 dwarf2_add_field (&fi, child_die, cu);
11955 else if (child_die->tag == DW_TAG_typedef)
11956 dwarf2_add_typedef (&fi, child_die, cu);
11957 else if (child_die->tag == DW_TAG_template_type_param
11958 || child_die->tag == DW_TAG_template_value_param)
11960 struct symbol *arg = new_symbol (child_die, NULL, cu);
11963 VEC_safe_push (symbolp, template_args, arg);
11966 child_die = sibling_die (child_die);
11969 /* Attach template arguments to type. */
11970 if (! VEC_empty (symbolp, template_args))
11972 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11973 TYPE_N_TEMPLATE_ARGUMENTS (type)
11974 = VEC_length (symbolp, template_args);
11975 TYPE_TEMPLATE_ARGUMENTS (type)
11976 = obstack_alloc (&objfile->objfile_obstack,
11977 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11978 * sizeof (struct symbol *)));
11979 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11980 VEC_address (symbolp, template_args),
11981 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11982 * sizeof (struct symbol *)));
11983 VEC_free (symbolp, template_args);
11986 /* Attach fields and member functions to the type. */
11988 dwarf2_attach_fields_to_type (&fi, type, cu);
11991 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11993 /* Get the type which refers to the base class (possibly this
11994 class itself) which contains the vtable pointer for the current
11995 class from the DW_AT_containing_type attribute. This use of
11996 DW_AT_containing_type is a GNU extension. */
11998 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12000 struct type *t = die_containing_type (die, cu);
12002 TYPE_VPTR_BASETYPE (type) = t;
12007 /* Our own class provides vtbl ptr. */
12008 for (i = TYPE_NFIELDS (t) - 1;
12009 i >= TYPE_N_BASECLASSES (t);
12012 const char *fieldname = TYPE_FIELD_NAME (t, i);
12014 if (is_vtable_name (fieldname, cu))
12016 TYPE_VPTR_FIELDNO (type) = i;
12021 /* Complain if virtual function table field not found. */
12022 if (i < TYPE_N_BASECLASSES (t))
12023 complaint (&symfile_complaints,
12024 _("virtual function table pointer "
12025 "not found when defining class '%s'"),
12026 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12031 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12034 else if (cu->producer
12035 && strncmp (cu->producer,
12036 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12038 /* The IBM XLC compiler does not provide direct indication
12039 of the containing type, but the vtable pointer is
12040 always named __vfp. */
12044 for (i = TYPE_NFIELDS (type) - 1;
12045 i >= TYPE_N_BASECLASSES (type);
12048 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12050 TYPE_VPTR_FIELDNO (type) = i;
12051 TYPE_VPTR_BASETYPE (type) = type;
12058 /* Copy fi.typedef_field_list linked list elements content into the
12059 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12060 if (fi.typedef_field_list)
12062 int i = fi.typedef_field_list_count;
12064 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12065 TYPE_TYPEDEF_FIELD_ARRAY (type)
12066 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12067 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12069 /* Reverse the list order to keep the debug info elements order. */
12072 struct typedef_field *dest, *src;
12074 dest = &TYPE_TYPEDEF_FIELD (type, i);
12075 src = &fi.typedef_field_list->field;
12076 fi.typedef_field_list = fi.typedef_field_list->next;
12081 do_cleanups (back_to);
12083 if (HAVE_CPLUS_STRUCT (type))
12084 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12087 quirk_gcc_member_function_pointer (type, objfile);
12089 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12090 snapshots) has been known to create a die giving a declaration
12091 for a class that has, as a child, a die giving a definition for a
12092 nested class. So we have to process our children even if the
12093 current die is a declaration. Normally, of course, a declaration
12094 won't have any children at all. */
12096 while (child_die != NULL && child_die->tag)
12098 if (child_die->tag == DW_TAG_member
12099 || child_die->tag == DW_TAG_variable
12100 || child_die->tag == DW_TAG_inheritance
12101 || child_die->tag == DW_TAG_template_value_param
12102 || child_die->tag == DW_TAG_template_type_param)
12107 process_die (child_die, cu);
12109 child_die = sibling_die (child_die);
12112 /* Do not consider external references. According to the DWARF standard,
12113 these DIEs are identified by the fact that they have no byte_size
12114 attribute, and a declaration attribute. */
12115 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12116 || !die_is_declaration (die, cu))
12117 new_symbol (die, type, cu);
12120 /* Given a DW_AT_enumeration_type die, set its type. We do not
12121 complete the type's fields yet, or create any symbols. */
12123 static struct type *
12124 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12126 struct objfile *objfile = cu->objfile;
12128 struct attribute *attr;
12131 /* If the definition of this type lives in .debug_types, read that type.
12132 Don't follow DW_AT_specification though, that will take us back up
12133 the chain and we want to go down. */
12134 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12137 type = get_DW_AT_signature_type (die, attr, cu);
12139 /* The type's CU may not be the same as CU.
12140 Ensure TYPE is recorded with CU in die_type_hash. */
12141 return set_die_type (die, type, cu);
12144 type = alloc_type (objfile);
12146 TYPE_CODE (type) = TYPE_CODE_ENUM;
12147 name = dwarf2_full_name (NULL, die, cu);
12149 TYPE_TAG_NAME (type) = name;
12151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12154 TYPE_LENGTH (type) = DW_UNSND (attr);
12158 TYPE_LENGTH (type) = 0;
12161 /* The enumeration DIE can be incomplete. In Ada, any type can be
12162 declared as private in the package spec, and then defined only
12163 inside the package body. Such types are known as Taft Amendment
12164 Types. When another package uses such a type, an incomplete DIE
12165 may be generated by the compiler. */
12166 if (die_is_declaration (die, cu))
12167 TYPE_STUB (type) = 1;
12169 return set_die_type (die, type, cu);
12172 /* Given a pointer to a die which begins an enumeration, process all
12173 the dies that define the members of the enumeration, and create the
12174 symbol for the enumeration type.
12176 NOTE: We reverse the order of the element list. */
12179 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12181 struct type *this_type;
12183 this_type = get_die_type (die, cu);
12184 if (this_type == NULL)
12185 this_type = read_enumeration_type (die, cu);
12187 if (die->child != NULL)
12189 struct die_info *child_die;
12190 struct symbol *sym;
12191 struct field *fields = NULL;
12192 int num_fields = 0;
12193 int unsigned_enum = 1;
12198 child_die = die->child;
12199 while (child_die && child_die->tag)
12201 if (child_die->tag != DW_TAG_enumerator)
12203 process_die (child_die, cu);
12207 name = dwarf2_name (child_die, cu);
12210 sym = new_symbol (child_die, this_type, cu);
12211 if (SYMBOL_VALUE (sym) < 0)
12216 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12219 mask |= SYMBOL_VALUE (sym);
12221 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12223 fields = (struct field *)
12225 (num_fields + DW_FIELD_ALLOC_CHUNK)
12226 * sizeof (struct field));
12229 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
12230 FIELD_TYPE (fields[num_fields]) = NULL;
12231 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
12232 FIELD_BITSIZE (fields[num_fields]) = 0;
12238 child_die = sibling_die (child_die);
12243 TYPE_NFIELDS (this_type) = num_fields;
12244 TYPE_FIELDS (this_type) = (struct field *)
12245 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12246 memcpy (TYPE_FIELDS (this_type), fields,
12247 sizeof (struct field) * num_fields);
12251 TYPE_UNSIGNED (this_type) = 1;
12253 TYPE_FLAG_ENUM (this_type) = 1;
12256 /* If we are reading an enum from a .debug_types unit, and the enum
12257 is a declaration, and the enum is not the signatured type in the
12258 unit, then we do not want to add a symbol for it. Adding a
12259 symbol would in some cases obscure the true definition of the
12260 enum, giving users an incomplete type when the definition is
12261 actually available. Note that we do not want to do this for all
12262 enums which are just declarations, because C++0x allows forward
12263 enum declarations. */
12264 if (cu->per_cu->is_debug_types
12265 && die_is_declaration (die, cu))
12267 struct signatured_type *sig_type;
12269 sig_type = (struct signatured_type *) cu->per_cu;
12270 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12271 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
12275 new_symbol (die, this_type, cu);
12278 /* Extract all information from a DW_TAG_array_type DIE and put it in
12279 the DIE's type field. For now, this only handles one dimensional
12282 static struct type *
12283 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
12285 struct objfile *objfile = cu->objfile;
12286 struct die_info *child_die;
12288 struct type *element_type, *range_type, *index_type;
12289 struct type **range_types = NULL;
12290 struct attribute *attr;
12292 struct cleanup *back_to;
12295 element_type = die_type (die, cu);
12297 /* The die_type call above may have already set the type for this DIE. */
12298 type = get_die_type (die, cu);
12302 /* Irix 6.2 native cc creates array types without children for
12303 arrays with unspecified length. */
12304 if (die->child == NULL)
12306 index_type = objfile_type (objfile)->builtin_int;
12307 range_type = create_range_type (NULL, index_type, 0, -1);
12308 type = create_array_type (NULL, element_type, range_type);
12309 return set_die_type (die, type, cu);
12312 back_to = make_cleanup (null_cleanup, NULL);
12313 child_die = die->child;
12314 while (child_die && child_die->tag)
12316 if (child_die->tag == DW_TAG_subrange_type)
12318 struct type *child_type = read_type_die (child_die, cu);
12320 if (child_type != NULL)
12322 /* The range type was succesfully read. Save it for the
12323 array type creation. */
12324 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12326 range_types = (struct type **)
12327 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12328 * sizeof (struct type *));
12330 make_cleanup (free_current_contents, &range_types);
12332 range_types[ndim++] = child_type;
12335 child_die = sibling_die (child_die);
12338 /* Dwarf2 dimensions are output from left to right, create the
12339 necessary array types in backwards order. */
12341 type = element_type;
12343 if (read_array_order (die, cu) == DW_ORD_col_major)
12348 type = create_array_type (NULL, type, range_types[i++]);
12353 type = create_array_type (NULL, type, range_types[ndim]);
12356 /* Understand Dwarf2 support for vector types (like they occur on
12357 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12358 array type. This is not part of the Dwarf2/3 standard yet, but a
12359 custom vendor extension. The main difference between a regular
12360 array and the vector variant is that vectors are passed by value
12362 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
12364 make_vector_type (type);
12366 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12367 implementation may choose to implement triple vectors using this
12369 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12372 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12373 TYPE_LENGTH (type) = DW_UNSND (attr);
12375 complaint (&symfile_complaints,
12376 _("DW_AT_byte_size for array type smaller "
12377 "than the total size of elements"));
12380 name = dwarf2_name (die, cu);
12382 TYPE_NAME (type) = name;
12384 /* Install the type in the die. */
12385 set_die_type (die, type, cu);
12387 /* set_die_type should be already done. */
12388 set_descriptive_type (type, die, cu);
12390 do_cleanups (back_to);
12395 static enum dwarf_array_dim_ordering
12396 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
12398 struct attribute *attr;
12400 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12402 if (attr) return DW_SND (attr);
12404 /* GNU F77 is a special case, as at 08/2004 array type info is the
12405 opposite order to the dwarf2 specification, but data is still
12406 laid out as per normal fortran.
12408 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12409 version checking. */
12411 if (cu->language == language_fortran
12412 && cu->producer && strstr (cu->producer, "GNU F77"))
12414 return DW_ORD_row_major;
12417 switch (cu->language_defn->la_array_ordering)
12419 case array_column_major:
12420 return DW_ORD_col_major;
12421 case array_row_major:
12423 return DW_ORD_row_major;
12427 /* Extract all information from a DW_TAG_set_type DIE and put it in
12428 the DIE's type field. */
12430 static struct type *
12431 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12433 struct type *domain_type, *set_type;
12434 struct attribute *attr;
12436 domain_type = die_type (die, cu);
12438 /* The die_type call above may have already set the type for this DIE. */
12439 set_type = get_die_type (die, cu);
12443 set_type = create_set_type (NULL, domain_type);
12445 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12447 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12449 return set_die_type (die, set_type, cu);
12452 /* A helper for read_common_block that creates a locexpr baton.
12453 SYM is the symbol which we are marking as computed.
12454 COMMON_DIE is the DIE for the common block.
12455 COMMON_LOC is the location expression attribute for the common
12457 MEMBER_LOC is the location expression attribute for the particular
12458 member of the common block that we are processing.
12459 CU is the CU from which the above come. */
12462 mark_common_block_symbol_computed (struct symbol *sym,
12463 struct die_info *common_die,
12464 struct attribute *common_loc,
12465 struct attribute *member_loc,
12466 struct dwarf2_cu *cu)
12468 struct objfile *objfile = dwarf2_per_objfile->objfile;
12469 struct dwarf2_locexpr_baton *baton;
12471 unsigned int cu_off;
12472 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12473 LONGEST offset = 0;
12475 gdb_assert (common_loc && member_loc);
12476 gdb_assert (attr_form_is_block (common_loc));
12477 gdb_assert (attr_form_is_block (member_loc)
12478 || attr_form_is_constant (member_loc));
12480 baton = obstack_alloc (&objfile->objfile_obstack,
12481 sizeof (struct dwarf2_locexpr_baton));
12482 baton->per_cu = cu->per_cu;
12483 gdb_assert (baton->per_cu);
12485 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12487 if (attr_form_is_constant (member_loc))
12489 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12490 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12493 baton->size += DW_BLOCK (member_loc)->size;
12495 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12498 *ptr++ = DW_OP_call4;
12499 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12500 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12503 if (attr_form_is_constant (member_loc))
12505 *ptr++ = DW_OP_addr;
12506 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12507 ptr += cu->header.addr_size;
12511 /* We have to copy the data here, because DW_OP_call4 will only
12512 use a DW_AT_location attribute. */
12513 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12514 ptr += DW_BLOCK (member_loc)->size;
12517 *ptr++ = DW_OP_plus;
12518 gdb_assert (ptr - baton->data == baton->size);
12520 SYMBOL_LOCATION_BATON (sym) = baton;
12521 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12524 /* Create appropriate locally-scoped variables for all the
12525 DW_TAG_common_block entries. Also create a struct common_block
12526 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12527 is used to sepate the common blocks name namespace from regular
12531 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12533 struct attribute *attr;
12535 attr = dwarf2_attr (die, DW_AT_location, cu);
12538 /* Support the .debug_loc offsets. */
12539 if (attr_form_is_block (attr))
12543 else if (attr_form_is_section_offset (attr))
12545 dwarf2_complex_location_expr_complaint ();
12550 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12551 "common block member");
12556 if (die->child != NULL)
12558 struct objfile *objfile = cu->objfile;
12559 struct die_info *child_die;
12560 size_t n_entries = 0, size;
12561 struct common_block *common_block;
12562 struct symbol *sym;
12564 for (child_die = die->child;
12565 child_die && child_die->tag;
12566 child_die = sibling_die (child_die))
12569 size = (sizeof (struct common_block)
12570 + (n_entries - 1) * sizeof (struct symbol *));
12571 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12572 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12573 common_block->n_entries = 0;
12575 for (child_die = die->child;
12576 child_die && child_die->tag;
12577 child_die = sibling_die (child_die))
12579 /* Create the symbol in the DW_TAG_common_block block in the current
12581 sym = new_symbol (child_die, NULL, cu);
12584 struct attribute *member_loc;
12586 common_block->contents[common_block->n_entries++] = sym;
12588 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12592 /* GDB has handled this for a long time, but it is
12593 not specified by DWARF. It seems to have been
12594 emitted by gfortran at least as recently as:
12595 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12596 complaint (&symfile_complaints,
12597 _("Variable in common block has "
12598 "DW_AT_data_member_location "
12599 "- DIE at 0x%x [in module %s]"),
12600 child_die->offset.sect_off, cu->objfile->name);
12602 if (attr_form_is_section_offset (member_loc))
12603 dwarf2_complex_location_expr_complaint ();
12604 else if (attr_form_is_constant (member_loc)
12605 || attr_form_is_block (member_loc))
12608 mark_common_block_symbol_computed (sym, die, attr,
12612 dwarf2_complex_location_expr_complaint ();
12617 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12618 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12622 /* Create a type for a C++ namespace. */
12624 static struct type *
12625 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12627 struct objfile *objfile = cu->objfile;
12628 const char *previous_prefix, *name;
12632 /* For extensions, reuse the type of the original namespace. */
12633 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12635 struct die_info *ext_die;
12636 struct dwarf2_cu *ext_cu = cu;
12638 ext_die = dwarf2_extension (die, &ext_cu);
12639 type = read_type_die (ext_die, ext_cu);
12641 /* EXT_CU may not be the same as CU.
12642 Ensure TYPE is recorded with CU in die_type_hash. */
12643 return set_die_type (die, type, cu);
12646 name = namespace_name (die, &is_anonymous, cu);
12648 /* Now build the name of the current namespace. */
12650 previous_prefix = determine_prefix (die, cu);
12651 if (previous_prefix[0] != '\0')
12652 name = typename_concat (&objfile->objfile_obstack,
12653 previous_prefix, name, 0, cu);
12655 /* Create the type. */
12656 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12658 TYPE_NAME (type) = name;
12659 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12661 return set_die_type (die, type, cu);
12664 /* Read a C++ namespace. */
12667 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12669 struct objfile *objfile = cu->objfile;
12672 /* Add a symbol associated to this if we haven't seen the namespace
12673 before. Also, add a using directive if it's an anonymous
12676 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12680 type = read_type_die (die, cu);
12681 new_symbol (die, type, cu);
12683 namespace_name (die, &is_anonymous, cu);
12686 const char *previous_prefix = determine_prefix (die, cu);
12688 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12689 NULL, NULL, 0, &objfile->objfile_obstack);
12693 if (die->child != NULL)
12695 struct die_info *child_die = die->child;
12697 while (child_die && child_die->tag)
12699 process_die (child_die, cu);
12700 child_die = sibling_die (child_die);
12705 /* Read a Fortran module as type. This DIE can be only a declaration used for
12706 imported module. Still we need that type as local Fortran "use ... only"
12707 declaration imports depend on the created type in determine_prefix. */
12709 static struct type *
12710 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12712 struct objfile *objfile = cu->objfile;
12713 const char *module_name;
12716 module_name = dwarf2_name (die, cu);
12718 complaint (&symfile_complaints,
12719 _("DW_TAG_module has no name, offset 0x%x"),
12720 die->offset.sect_off);
12721 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12723 /* determine_prefix uses TYPE_TAG_NAME. */
12724 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12726 return set_die_type (die, type, cu);
12729 /* Read a Fortran module. */
12732 read_module (struct die_info *die, struct dwarf2_cu *cu)
12734 struct die_info *child_die = die->child;
12736 while (child_die && child_die->tag)
12738 process_die (child_die, cu);
12739 child_die = sibling_die (child_die);
12743 /* Return the name of the namespace represented by DIE. Set
12744 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12747 static const char *
12748 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12750 struct die_info *current_die;
12751 const char *name = NULL;
12753 /* Loop through the extensions until we find a name. */
12755 for (current_die = die;
12756 current_die != NULL;
12757 current_die = dwarf2_extension (die, &cu))
12759 name = dwarf2_name (current_die, cu);
12764 /* Is it an anonymous namespace? */
12766 *is_anonymous = (name == NULL);
12768 name = CP_ANONYMOUS_NAMESPACE_STR;
12773 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12774 the user defined type vector. */
12776 static struct type *
12777 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12779 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12780 struct comp_unit_head *cu_header = &cu->header;
12782 struct attribute *attr_byte_size;
12783 struct attribute *attr_address_class;
12784 int byte_size, addr_class;
12785 struct type *target_type;
12787 target_type = die_type (die, cu);
12789 /* The die_type call above may have already set the type for this DIE. */
12790 type = get_die_type (die, cu);
12794 type = lookup_pointer_type (target_type);
12796 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12797 if (attr_byte_size)
12798 byte_size = DW_UNSND (attr_byte_size);
12800 byte_size = cu_header->addr_size;
12802 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12803 if (attr_address_class)
12804 addr_class = DW_UNSND (attr_address_class);
12806 addr_class = DW_ADDR_none;
12808 /* If the pointer size or address class is different than the
12809 default, create a type variant marked as such and set the
12810 length accordingly. */
12811 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12813 if (gdbarch_address_class_type_flags_p (gdbarch))
12817 type_flags = gdbarch_address_class_type_flags
12818 (gdbarch, byte_size, addr_class);
12819 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12821 type = make_type_with_address_space (type, type_flags);
12823 else if (TYPE_LENGTH (type) != byte_size)
12825 complaint (&symfile_complaints,
12826 _("invalid pointer size %d"), byte_size);
12830 /* Should we also complain about unhandled address classes? */
12834 TYPE_LENGTH (type) = byte_size;
12835 return set_die_type (die, type, cu);
12838 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12839 the user defined type vector. */
12841 static struct type *
12842 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12845 struct type *to_type;
12846 struct type *domain;
12848 to_type = die_type (die, cu);
12849 domain = die_containing_type (die, cu);
12851 /* The calls above may have already set the type for this DIE. */
12852 type = get_die_type (die, cu);
12856 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12857 type = lookup_methodptr_type (to_type);
12858 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12860 struct type *new_type = alloc_type (cu->objfile);
12862 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12863 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12864 TYPE_VARARGS (to_type));
12865 type = lookup_methodptr_type (new_type);
12868 type = lookup_memberptr_type (to_type, domain);
12870 return set_die_type (die, type, cu);
12873 /* Extract all information from a DW_TAG_reference_type DIE and add to
12874 the user defined type vector. */
12876 static struct type *
12877 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12879 struct comp_unit_head *cu_header = &cu->header;
12880 struct type *type, *target_type;
12881 struct attribute *attr;
12883 target_type = die_type (die, cu);
12885 /* The die_type call above may have already set the type for this DIE. */
12886 type = get_die_type (die, cu);
12890 type = lookup_reference_type (target_type);
12891 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12894 TYPE_LENGTH (type) = DW_UNSND (attr);
12898 TYPE_LENGTH (type) = cu_header->addr_size;
12900 return set_die_type (die, type, cu);
12903 static struct type *
12904 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12906 struct type *base_type, *cv_type;
12908 base_type = die_type (die, cu);
12910 /* The die_type call above may have already set the type for this DIE. */
12911 cv_type = get_die_type (die, cu);
12915 /* In case the const qualifier is applied to an array type, the element type
12916 is so qualified, not the array type (section 6.7.3 of C99). */
12917 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12919 struct type *el_type, *inner_array;
12921 base_type = copy_type (base_type);
12922 inner_array = base_type;
12924 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12926 TYPE_TARGET_TYPE (inner_array) =
12927 copy_type (TYPE_TARGET_TYPE (inner_array));
12928 inner_array = TYPE_TARGET_TYPE (inner_array);
12931 el_type = TYPE_TARGET_TYPE (inner_array);
12932 TYPE_TARGET_TYPE (inner_array) =
12933 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12935 return set_die_type (die, base_type, cu);
12938 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12939 return set_die_type (die, cv_type, cu);
12942 static struct type *
12943 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12945 struct type *base_type, *cv_type;
12947 base_type = die_type (die, cu);
12949 /* The die_type call above may have already set the type for this DIE. */
12950 cv_type = get_die_type (die, cu);
12954 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12955 return set_die_type (die, cv_type, cu);
12958 /* Handle DW_TAG_restrict_type. */
12960 static struct type *
12961 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12963 struct type *base_type, *cv_type;
12965 base_type = die_type (die, cu);
12967 /* The die_type call above may have already set the type for this DIE. */
12968 cv_type = get_die_type (die, cu);
12972 cv_type = make_restrict_type (base_type);
12973 return set_die_type (die, cv_type, cu);
12976 /* Extract all information from a DW_TAG_string_type DIE and add to
12977 the user defined type vector. It isn't really a user defined type,
12978 but it behaves like one, with other DIE's using an AT_user_def_type
12979 attribute to reference it. */
12981 static struct type *
12982 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12984 struct objfile *objfile = cu->objfile;
12985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12986 struct type *type, *range_type, *index_type, *char_type;
12987 struct attribute *attr;
12988 unsigned int length;
12990 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12993 length = DW_UNSND (attr);
12997 /* Check for the DW_AT_byte_size attribute. */
12998 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13001 length = DW_UNSND (attr);
13009 index_type = objfile_type (objfile)->builtin_int;
13010 range_type = create_range_type (NULL, index_type, 1, length);
13011 char_type = language_string_char_type (cu->language_defn, gdbarch);
13012 type = create_string_type (NULL, char_type, range_type);
13014 return set_die_type (die, type, cu);
13017 /* Assuming that DIE corresponds to a function, returns nonzero
13018 if the function is prototyped. */
13021 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13023 struct attribute *attr;
13025 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13026 if (attr && (DW_UNSND (attr) != 0))
13029 /* The DWARF standard implies that the DW_AT_prototyped attribute
13030 is only meaninful for C, but the concept also extends to other
13031 languages that allow unprototyped functions (Eg: Objective C).
13032 For all other languages, assume that functions are always
13034 if (cu->language != language_c
13035 && cu->language != language_objc
13036 && cu->language != language_opencl)
13039 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13040 prototyped and unprototyped functions; default to prototyped,
13041 since that is more common in modern code (and RealView warns
13042 about unprototyped functions). */
13043 if (producer_is_realview (cu->producer))
13049 /* Handle DIES due to C code like:
13053 int (*funcp)(int a, long l);
13057 ('funcp' generates a DW_TAG_subroutine_type DIE). */
13059 static struct type *
13060 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
13062 struct objfile *objfile = cu->objfile;
13063 struct type *type; /* Type that this function returns. */
13064 struct type *ftype; /* Function that returns above type. */
13065 struct attribute *attr;
13067 type = die_type (die, cu);
13069 /* The die_type call above may have already set the type for this DIE. */
13070 ftype = get_die_type (die, cu);
13074 ftype = lookup_function_type (type);
13076 if (prototyped_function_p (die, cu))
13077 TYPE_PROTOTYPED (ftype) = 1;
13079 /* Store the calling convention in the type if it's available in
13080 the subroutine die. Otherwise set the calling convention to
13081 the default value DW_CC_normal. */
13082 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13084 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13085 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13086 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13088 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13090 /* We need to add the subroutine type to the die immediately so
13091 we don't infinitely recurse when dealing with parameters
13092 declared as the same subroutine type. */
13093 set_die_type (die, ftype, cu);
13095 if (die->child != NULL)
13097 struct type *void_type = objfile_type (objfile)->builtin_void;
13098 struct die_info *child_die;
13099 int nparams, iparams;
13101 /* Count the number of parameters.
13102 FIXME: GDB currently ignores vararg functions, but knows about
13103 vararg member functions. */
13105 child_die = die->child;
13106 while (child_die && child_die->tag)
13108 if (child_die->tag == DW_TAG_formal_parameter)
13110 else if (child_die->tag == DW_TAG_unspecified_parameters)
13111 TYPE_VARARGS (ftype) = 1;
13112 child_die = sibling_die (child_die);
13115 /* Allocate storage for parameters and fill them in. */
13116 TYPE_NFIELDS (ftype) = nparams;
13117 TYPE_FIELDS (ftype) = (struct field *)
13118 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13120 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13121 even if we error out during the parameters reading below. */
13122 for (iparams = 0; iparams < nparams; iparams++)
13123 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13126 child_die = die->child;
13127 while (child_die && child_die->tag)
13129 if (child_die->tag == DW_TAG_formal_parameter)
13131 struct type *arg_type;
13133 /* DWARF version 2 has no clean way to discern C++
13134 static and non-static member functions. G++ helps
13135 GDB by marking the first parameter for non-static
13136 member functions (which is the this pointer) as
13137 artificial. We pass this information to
13138 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13140 DWARF version 3 added DW_AT_object_pointer, which GCC
13141 4.5 does not yet generate. */
13142 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13144 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13147 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13149 /* GCC/43521: In java, the formal parameter
13150 "this" is sometimes not marked with DW_AT_artificial. */
13151 if (cu->language == language_java)
13153 const char *name = dwarf2_name (child_die, cu);
13155 if (name && !strcmp (name, "this"))
13156 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13159 arg_type = die_type (child_die, cu);
13161 /* RealView does not mark THIS as const, which the testsuite
13162 expects. GCC marks THIS as const in method definitions,
13163 but not in the class specifications (GCC PR 43053). */
13164 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13165 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13168 struct dwarf2_cu *arg_cu = cu;
13169 const char *name = dwarf2_name (child_die, cu);
13171 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13174 /* If the compiler emits this, use it. */
13175 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13178 else if (name && strcmp (name, "this") == 0)
13179 /* Function definitions will have the argument names. */
13181 else if (name == NULL && iparams == 0)
13182 /* Declarations may not have the names, so like
13183 elsewhere in GDB, assume an artificial first
13184 argument is "this". */
13188 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13192 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
13195 child_die = sibling_die (child_die);
13202 static struct type *
13203 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
13205 struct objfile *objfile = cu->objfile;
13206 const char *name = NULL;
13207 struct type *this_type, *target_type;
13209 name = dwarf2_full_name (NULL, die, cu);
13210 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
13211 TYPE_FLAG_TARGET_STUB, NULL, objfile);
13212 TYPE_NAME (this_type) = name;
13213 set_die_type (die, this_type, cu);
13214 target_type = die_type (die, cu);
13215 if (target_type != this_type)
13216 TYPE_TARGET_TYPE (this_type) = target_type;
13219 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13220 spec and cause infinite loops in GDB. */
13221 complaint (&symfile_complaints,
13222 _("Self-referential DW_TAG_typedef "
13223 "- DIE at 0x%x [in module %s]"),
13224 die->offset.sect_off, objfile->name);
13225 TYPE_TARGET_TYPE (this_type) = NULL;
13230 /* Find a representation of a given base type and install
13231 it in the TYPE field of the die. */
13233 static struct type *
13234 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
13236 struct objfile *objfile = cu->objfile;
13238 struct attribute *attr;
13239 int encoding = 0, size = 0;
13241 enum type_code code = TYPE_CODE_INT;
13242 int type_flags = 0;
13243 struct type *target_type = NULL;
13245 attr = dwarf2_attr (die, DW_AT_encoding, cu);
13248 encoding = DW_UNSND (attr);
13250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13253 size = DW_UNSND (attr);
13255 name = dwarf2_name (die, cu);
13258 complaint (&symfile_complaints,
13259 _("DW_AT_name missing from DW_TAG_base_type"));
13264 case DW_ATE_address:
13265 /* Turn DW_ATE_address into a void * pointer. */
13266 code = TYPE_CODE_PTR;
13267 type_flags |= TYPE_FLAG_UNSIGNED;
13268 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13270 case DW_ATE_boolean:
13271 code = TYPE_CODE_BOOL;
13272 type_flags |= TYPE_FLAG_UNSIGNED;
13274 case DW_ATE_complex_float:
13275 code = TYPE_CODE_COMPLEX;
13276 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13278 case DW_ATE_decimal_float:
13279 code = TYPE_CODE_DECFLOAT;
13282 code = TYPE_CODE_FLT;
13284 case DW_ATE_signed:
13286 case DW_ATE_unsigned:
13287 type_flags |= TYPE_FLAG_UNSIGNED;
13288 if (cu->language == language_fortran
13290 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13291 code = TYPE_CODE_CHAR;
13293 case DW_ATE_signed_char:
13294 if (cu->language == language_ada || cu->language == language_m2
13295 || cu->language == language_pascal
13296 || cu->language == language_fortran)
13297 code = TYPE_CODE_CHAR;
13299 case DW_ATE_unsigned_char:
13300 if (cu->language == language_ada || cu->language == language_m2
13301 || cu->language == language_pascal
13302 || cu->language == language_fortran)
13303 code = TYPE_CODE_CHAR;
13304 type_flags |= TYPE_FLAG_UNSIGNED;
13307 /* We just treat this as an integer and then recognize the
13308 type by name elsewhere. */
13312 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13313 dwarf_type_encoding_name (encoding));
13317 type = init_type (code, size, type_flags, NULL, objfile);
13318 TYPE_NAME (type) = name;
13319 TYPE_TARGET_TYPE (type) = target_type;
13321 if (name && strcmp (name, "char") == 0)
13322 TYPE_NOSIGN (type) = 1;
13324 return set_die_type (die, type, cu);
13327 /* Read the given DW_AT_subrange DIE. */
13329 static struct type *
13330 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13332 struct type *base_type, *orig_base_type;
13333 struct type *range_type;
13334 struct attribute *attr;
13336 int low_default_is_valid;
13338 LONGEST negative_mask;
13340 orig_base_type = die_type (die, cu);
13341 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13342 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13343 creating the range type, but we use the result of check_typedef
13344 when examining properties of the type. */
13345 base_type = check_typedef (orig_base_type);
13347 /* The die_type call above may have already set the type for this DIE. */
13348 range_type = get_die_type (die, cu);
13352 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13353 omitting DW_AT_lower_bound. */
13354 switch (cu->language)
13357 case language_cplus:
13359 low_default_is_valid = 1;
13361 case language_fortran:
13363 low_default_is_valid = 1;
13366 case language_java:
13367 case language_objc:
13369 low_default_is_valid = (cu->header.version >= 4);
13373 case language_pascal:
13375 low_default_is_valid = (cu->header.version >= 4);
13379 low_default_is_valid = 0;
13383 /* FIXME: For variable sized arrays either of these could be
13384 a variable rather than a constant value. We'll allow it,
13385 but we don't know how to handle it. */
13386 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
13388 low = dwarf2_get_attr_constant_value (attr, low);
13389 else if (!low_default_is_valid)
13390 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13391 "- DIE at 0x%x [in module %s]"),
13392 die->offset.sect_off, cu->objfile->name);
13394 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
13397 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
13399 /* GCC encodes arrays with unspecified or dynamic length
13400 with a DW_FORM_block1 attribute or a reference attribute.
13401 FIXME: GDB does not yet know how to handle dynamic
13402 arrays properly, treat them as arrays with unspecified
13405 FIXME: jimb/2003-09-22: GDB does not really know
13406 how to handle arrays of unspecified length
13407 either; we just represent them as zero-length
13408 arrays. Choose an appropriate upper bound given
13409 the lower bound we've computed above. */
13413 high = dwarf2_get_attr_constant_value (attr, 1);
13417 attr = dwarf2_attr (die, DW_AT_count, cu);
13420 int count = dwarf2_get_attr_constant_value (attr, 1);
13421 high = low + count - 1;
13425 /* Unspecified array length. */
13430 /* Dwarf-2 specifications explicitly allows to create subrange types
13431 without specifying a base type.
13432 In that case, the base type must be set to the type of
13433 the lower bound, upper bound or count, in that order, if any of these
13434 three attributes references an object that has a type.
13435 If no base type is found, the Dwarf-2 specifications say that
13436 a signed integer type of size equal to the size of an address should
13438 For the following C code: `extern char gdb_int [];'
13439 GCC produces an empty range DIE.
13440 FIXME: muller/2010-05-28: Possible references to object for low bound,
13441 high bound or count are not yet handled by this code. */
13442 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13444 struct objfile *objfile = cu->objfile;
13445 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13446 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13447 struct type *int_type = objfile_type (objfile)->builtin_int;
13449 /* Test "int", "long int", and "long long int" objfile types,
13450 and select the first one having a size above or equal to the
13451 architecture address size. */
13452 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13453 base_type = int_type;
13456 int_type = objfile_type (objfile)->builtin_long;
13457 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13458 base_type = int_type;
13461 int_type = objfile_type (objfile)->builtin_long_long;
13462 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13463 base_type = int_type;
13469 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13470 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13471 low |= negative_mask;
13472 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13473 high |= negative_mask;
13475 range_type = create_range_type (NULL, orig_base_type, low, high);
13477 /* Mark arrays with dynamic length at least as an array of unspecified
13478 length. GDB could check the boundary but before it gets implemented at
13479 least allow accessing the array elements. */
13480 if (attr && attr_form_is_block (attr))
13481 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13483 /* Ada expects an empty array on no boundary attributes. */
13484 if (attr == NULL && cu->language != language_ada)
13485 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13487 name = dwarf2_name (die, cu);
13489 TYPE_NAME (range_type) = name;
13491 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13493 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13495 set_die_type (die, range_type, cu);
13497 /* set_die_type should be already done. */
13498 set_descriptive_type (range_type, die, cu);
13503 static struct type *
13504 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13508 /* For now, we only support the C meaning of an unspecified type: void. */
13510 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13511 TYPE_NAME (type) = dwarf2_name (die, cu);
13513 return set_die_type (die, type, cu);
13516 /* Read a single die and all its descendents. Set the die's sibling
13517 field to NULL; set other fields in the die correctly, and set all
13518 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13519 location of the info_ptr after reading all of those dies. PARENT
13520 is the parent of the die in question. */
13522 static struct die_info *
13523 read_die_and_children (const struct die_reader_specs *reader,
13524 const gdb_byte *info_ptr,
13525 const gdb_byte **new_info_ptr,
13526 struct die_info *parent)
13528 struct die_info *die;
13529 const gdb_byte *cur_ptr;
13532 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13535 *new_info_ptr = cur_ptr;
13538 store_in_ref_table (die, reader->cu);
13541 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13545 *new_info_ptr = cur_ptr;
13548 die->sibling = NULL;
13549 die->parent = parent;
13553 /* Read a die, all of its descendents, and all of its siblings; set
13554 all of the fields of all of the dies correctly. Arguments are as
13555 in read_die_and_children. */
13557 static struct die_info *
13558 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13559 const gdb_byte *info_ptr,
13560 const gdb_byte **new_info_ptr,
13561 struct die_info *parent)
13563 struct die_info *first_die, *last_sibling;
13564 const gdb_byte *cur_ptr;
13566 cur_ptr = info_ptr;
13567 first_die = last_sibling = NULL;
13571 struct die_info *die
13572 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13576 *new_info_ptr = cur_ptr;
13583 last_sibling->sibling = die;
13585 last_sibling = die;
13589 /* Read a die, all of its descendents, and all of its siblings; set
13590 all of the fields of all of the dies correctly. Arguments are as
13591 in read_die_and_children.
13592 This the main entry point for reading a DIE and all its children. */
13594 static struct die_info *
13595 read_die_and_siblings (const struct die_reader_specs *reader,
13596 const gdb_byte *info_ptr,
13597 const gdb_byte **new_info_ptr,
13598 struct die_info *parent)
13600 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13601 new_info_ptr, parent);
13603 if (dwarf2_die_debug)
13605 fprintf_unfiltered (gdb_stdlog,
13606 "Read die from %s@0x%x of %s:\n",
13607 bfd_section_name (reader->abfd,
13608 reader->die_section->asection),
13609 (unsigned) (info_ptr - reader->die_section->buffer),
13610 bfd_get_filename (reader->abfd));
13611 dump_die (die, dwarf2_die_debug);
13617 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13619 The caller is responsible for filling in the extra attributes
13620 and updating (*DIEP)->num_attrs.
13621 Set DIEP to point to a newly allocated die with its information,
13622 except for its child, sibling, and parent fields.
13623 Set HAS_CHILDREN to tell whether the die has children or not. */
13625 static const gdb_byte *
13626 read_full_die_1 (const struct die_reader_specs *reader,
13627 struct die_info **diep, const gdb_byte *info_ptr,
13628 int *has_children, int num_extra_attrs)
13630 unsigned int abbrev_number, bytes_read, i;
13631 sect_offset offset;
13632 struct abbrev_info *abbrev;
13633 struct die_info *die;
13634 struct dwarf2_cu *cu = reader->cu;
13635 bfd *abfd = reader->abfd;
13637 offset.sect_off = info_ptr - reader->buffer;
13638 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13639 info_ptr += bytes_read;
13640 if (!abbrev_number)
13647 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13649 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13651 bfd_get_filename (abfd));
13653 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13654 die->offset = offset;
13655 die->tag = abbrev->tag;
13656 die->abbrev = abbrev_number;
13658 /* Make the result usable.
13659 The caller needs to update num_attrs after adding the extra
13661 die->num_attrs = abbrev->num_attrs;
13663 for (i = 0; i < abbrev->num_attrs; ++i)
13664 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13668 *has_children = abbrev->has_children;
13672 /* Read a die and all its attributes.
13673 Set DIEP to point to a newly allocated die with its information,
13674 except for its child, sibling, and parent fields.
13675 Set HAS_CHILDREN to tell whether the die has children or not. */
13677 static const gdb_byte *
13678 read_full_die (const struct die_reader_specs *reader,
13679 struct die_info **diep, const gdb_byte *info_ptr,
13682 const gdb_byte *result;
13684 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13686 if (dwarf2_die_debug)
13688 fprintf_unfiltered (gdb_stdlog,
13689 "Read die from %s@0x%x of %s:\n",
13690 bfd_section_name (reader->abfd,
13691 reader->die_section->asection),
13692 (unsigned) (info_ptr - reader->die_section->buffer),
13693 bfd_get_filename (reader->abfd));
13694 dump_die (*diep, dwarf2_die_debug);
13700 /* Abbreviation tables.
13702 In DWARF version 2, the description of the debugging information is
13703 stored in a separate .debug_abbrev section. Before we read any
13704 dies from a section we read in all abbreviations and install them
13705 in a hash table. */
13707 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13709 static struct abbrev_info *
13710 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13712 struct abbrev_info *abbrev;
13714 abbrev = (struct abbrev_info *)
13715 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13716 memset (abbrev, 0, sizeof (struct abbrev_info));
13720 /* Add an abbreviation to the table. */
13723 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13724 unsigned int abbrev_number,
13725 struct abbrev_info *abbrev)
13727 unsigned int hash_number;
13729 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13730 abbrev->next = abbrev_table->abbrevs[hash_number];
13731 abbrev_table->abbrevs[hash_number] = abbrev;
13734 /* Look up an abbrev in the table.
13735 Returns NULL if the abbrev is not found. */
13737 static struct abbrev_info *
13738 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13739 unsigned int abbrev_number)
13741 unsigned int hash_number;
13742 struct abbrev_info *abbrev;
13744 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13745 abbrev = abbrev_table->abbrevs[hash_number];
13749 if (abbrev->number == abbrev_number)
13751 abbrev = abbrev->next;
13756 /* Read in an abbrev table. */
13758 static struct abbrev_table *
13759 abbrev_table_read_table (struct dwarf2_section_info *section,
13760 sect_offset offset)
13762 struct objfile *objfile = dwarf2_per_objfile->objfile;
13763 bfd *abfd = section->asection->owner;
13764 struct abbrev_table *abbrev_table;
13765 const gdb_byte *abbrev_ptr;
13766 struct abbrev_info *cur_abbrev;
13767 unsigned int abbrev_number, bytes_read, abbrev_name;
13768 unsigned int abbrev_form;
13769 struct attr_abbrev *cur_attrs;
13770 unsigned int allocated_attrs;
13772 abbrev_table = XMALLOC (struct abbrev_table);
13773 abbrev_table->offset = offset;
13774 obstack_init (&abbrev_table->abbrev_obstack);
13775 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13777 * sizeof (struct abbrev_info *)));
13778 memset (abbrev_table->abbrevs, 0,
13779 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13781 dwarf2_read_section (objfile, section);
13782 abbrev_ptr = section->buffer + offset.sect_off;
13783 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13784 abbrev_ptr += bytes_read;
13786 allocated_attrs = ATTR_ALLOC_CHUNK;
13787 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13789 /* Loop until we reach an abbrev number of 0. */
13790 while (abbrev_number)
13792 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13794 /* read in abbrev header */
13795 cur_abbrev->number = abbrev_number;
13796 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13797 abbrev_ptr += bytes_read;
13798 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13801 /* now read in declarations */
13802 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13803 abbrev_ptr += bytes_read;
13804 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13805 abbrev_ptr += bytes_read;
13806 while (abbrev_name)
13808 if (cur_abbrev->num_attrs == allocated_attrs)
13810 allocated_attrs += ATTR_ALLOC_CHUNK;
13812 = xrealloc (cur_attrs, (allocated_attrs
13813 * sizeof (struct attr_abbrev)));
13816 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13817 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13818 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13819 abbrev_ptr += bytes_read;
13820 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13821 abbrev_ptr += bytes_read;
13824 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13825 (cur_abbrev->num_attrs
13826 * sizeof (struct attr_abbrev)));
13827 memcpy (cur_abbrev->attrs, cur_attrs,
13828 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13830 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13832 /* Get next abbreviation.
13833 Under Irix6 the abbreviations for a compilation unit are not
13834 always properly terminated with an abbrev number of 0.
13835 Exit loop if we encounter an abbreviation which we have
13836 already read (which means we are about to read the abbreviations
13837 for the next compile unit) or if the end of the abbreviation
13838 table is reached. */
13839 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13841 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13842 abbrev_ptr += bytes_read;
13843 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13848 return abbrev_table;
13851 /* Free the resources held by ABBREV_TABLE. */
13854 abbrev_table_free (struct abbrev_table *abbrev_table)
13856 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13857 xfree (abbrev_table);
13860 /* Same as abbrev_table_free but as a cleanup.
13861 We pass in a pointer to the pointer to the table so that we can
13862 set the pointer to NULL when we're done. It also simplifies
13863 build_type_unit_groups. */
13866 abbrev_table_free_cleanup (void *table_ptr)
13868 struct abbrev_table **abbrev_table_ptr = table_ptr;
13870 if (*abbrev_table_ptr != NULL)
13871 abbrev_table_free (*abbrev_table_ptr);
13872 *abbrev_table_ptr = NULL;
13875 /* Read the abbrev table for CU from ABBREV_SECTION. */
13878 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13879 struct dwarf2_section_info *abbrev_section)
13882 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13885 /* Release the memory used by the abbrev table for a compilation unit. */
13888 dwarf2_free_abbrev_table (void *ptr_to_cu)
13890 struct dwarf2_cu *cu = ptr_to_cu;
13892 if (cu->abbrev_table != NULL)
13893 abbrev_table_free (cu->abbrev_table);
13894 /* Set this to NULL so that we SEGV if we try to read it later,
13895 and also because free_comp_unit verifies this is NULL. */
13896 cu->abbrev_table = NULL;
13899 /* Returns nonzero if TAG represents a type that we might generate a partial
13903 is_type_tag_for_partial (int tag)
13908 /* Some types that would be reasonable to generate partial symbols for,
13909 that we don't at present. */
13910 case DW_TAG_array_type:
13911 case DW_TAG_file_type:
13912 case DW_TAG_ptr_to_member_type:
13913 case DW_TAG_set_type:
13914 case DW_TAG_string_type:
13915 case DW_TAG_subroutine_type:
13917 case DW_TAG_base_type:
13918 case DW_TAG_class_type:
13919 case DW_TAG_interface_type:
13920 case DW_TAG_enumeration_type:
13921 case DW_TAG_structure_type:
13922 case DW_TAG_subrange_type:
13923 case DW_TAG_typedef:
13924 case DW_TAG_union_type:
13931 /* Load all DIEs that are interesting for partial symbols into memory. */
13933 static struct partial_die_info *
13934 load_partial_dies (const struct die_reader_specs *reader,
13935 const gdb_byte *info_ptr, int building_psymtab)
13937 struct dwarf2_cu *cu = reader->cu;
13938 struct objfile *objfile = cu->objfile;
13939 struct partial_die_info *part_die;
13940 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13941 struct abbrev_info *abbrev;
13942 unsigned int bytes_read;
13943 unsigned int load_all = 0;
13944 int nesting_level = 1;
13949 gdb_assert (cu->per_cu != NULL);
13950 if (cu->per_cu->load_all_dies)
13954 = htab_create_alloc_ex (cu->header.length / 12,
13958 &cu->comp_unit_obstack,
13959 hashtab_obstack_allocate,
13960 dummy_obstack_deallocate);
13962 part_die = obstack_alloc (&cu->comp_unit_obstack,
13963 sizeof (struct partial_die_info));
13967 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13969 /* A NULL abbrev means the end of a series of children. */
13970 if (abbrev == NULL)
13972 if (--nesting_level == 0)
13974 /* PART_DIE was probably the last thing allocated on the
13975 comp_unit_obstack, so we could call obstack_free
13976 here. We don't do that because the waste is small,
13977 and will be cleaned up when we're done with this
13978 compilation unit. This way, we're also more robust
13979 against other users of the comp_unit_obstack. */
13982 info_ptr += bytes_read;
13983 last_die = parent_die;
13984 parent_die = parent_die->die_parent;
13988 /* Check for template arguments. We never save these; if
13989 they're seen, we just mark the parent, and go on our way. */
13990 if (parent_die != NULL
13991 && cu->language == language_cplus
13992 && (abbrev->tag == DW_TAG_template_type_param
13993 || abbrev->tag == DW_TAG_template_value_param))
13995 parent_die->has_template_arguments = 1;
13999 /* We don't need a partial DIE for the template argument. */
14000 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14005 /* We only recurse into c++ subprograms looking for template arguments.
14006 Skip their other children. */
14008 && cu->language == language_cplus
14009 && parent_die != NULL
14010 && parent_die->tag == DW_TAG_subprogram)
14012 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14016 /* Check whether this DIE is interesting enough to save. Normally
14017 we would not be interested in members here, but there may be
14018 later variables referencing them via DW_AT_specification (for
14019 static members). */
14021 && !is_type_tag_for_partial (abbrev->tag)
14022 && abbrev->tag != DW_TAG_constant
14023 && abbrev->tag != DW_TAG_enumerator
14024 && abbrev->tag != DW_TAG_subprogram
14025 && abbrev->tag != DW_TAG_lexical_block
14026 && abbrev->tag != DW_TAG_variable
14027 && abbrev->tag != DW_TAG_namespace
14028 && abbrev->tag != DW_TAG_module
14029 && abbrev->tag != DW_TAG_member
14030 && abbrev->tag != DW_TAG_imported_unit)
14032 /* Otherwise we skip to the next sibling, if any. */
14033 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14037 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14040 /* This two-pass algorithm for processing partial symbols has a
14041 high cost in cache pressure. Thus, handle some simple cases
14042 here which cover the majority of C partial symbols. DIEs
14043 which neither have specification tags in them, nor could have
14044 specification tags elsewhere pointing at them, can simply be
14045 processed and discarded.
14047 This segment is also optional; scan_partial_symbols and
14048 add_partial_symbol will handle these DIEs if we chain
14049 them in normally. When compilers which do not emit large
14050 quantities of duplicate debug information are more common,
14051 this code can probably be removed. */
14053 /* Any complete simple types at the top level (pretty much all
14054 of them, for a language without namespaces), can be processed
14056 if (parent_die == NULL
14057 && part_die->has_specification == 0
14058 && part_die->is_declaration == 0
14059 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
14060 || part_die->tag == DW_TAG_base_type
14061 || part_die->tag == DW_TAG_subrange_type))
14063 if (building_psymtab && part_die->name != NULL)
14064 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14065 VAR_DOMAIN, LOC_TYPEDEF,
14066 &objfile->static_psymbols,
14067 0, (CORE_ADDR) 0, cu->language, objfile);
14068 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14072 /* The exception for DW_TAG_typedef with has_children above is
14073 a workaround of GCC PR debug/47510. In the case of this complaint
14074 type_name_no_tag_or_error will error on such types later.
14076 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14077 it could not find the child DIEs referenced later, this is checked
14078 above. In correct DWARF DW_TAG_typedef should have no children. */
14080 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14081 complaint (&symfile_complaints,
14082 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14083 "- DIE at 0x%x [in module %s]"),
14084 part_die->offset.sect_off, objfile->name);
14086 /* If we're at the second level, and we're an enumerator, and
14087 our parent has no specification (meaning possibly lives in a
14088 namespace elsewhere), then we can add the partial symbol now
14089 instead of queueing it. */
14090 if (part_die->tag == DW_TAG_enumerator
14091 && parent_die != NULL
14092 && parent_die->die_parent == NULL
14093 && parent_die->tag == DW_TAG_enumeration_type
14094 && parent_die->has_specification == 0)
14096 if (part_die->name == NULL)
14097 complaint (&symfile_complaints,
14098 _("malformed enumerator DIE ignored"));
14099 else if (building_psymtab)
14100 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14101 VAR_DOMAIN, LOC_CONST,
14102 (cu->language == language_cplus
14103 || cu->language == language_java)
14104 ? &objfile->global_psymbols
14105 : &objfile->static_psymbols,
14106 0, (CORE_ADDR) 0, cu->language, objfile);
14108 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14112 /* We'll save this DIE so link it in. */
14113 part_die->die_parent = parent_die;
14114 part_die->die_sibling = NULL;
14115 part_die->die_child = NULL;
14117 if (last_die && last_die == parent_die)
14118 last_die->die_child = part_die;
14120 last_die->die_sibling = part_die;
14122 last_die = part_die;
14124 if (first_die == NULL)
14125 first_die = part_die;
14127 /* Maybe add the DIE to the hash table. Not all DIEs that we
14128 find interesting need to be in the hash table, because we
14129 also have the parent/sibling/child chains; only those that we
14130 might refer to by offset later during partial symbol reading.
14132 For now this means things that might have be the target of a
14133 DW_AT_specification, DW_AT_abstract_origin, or
14134 DW_AT_extension. DW_AT_extension will refer only to
14135 namespaces; DW_AT_abstract_origin refers to functions (and
14136 many things under the function DIE, but we do not recurse
14137 into function DIEs during partial symbol reading) and
14138 possibly variables as well; DW_AT_specification refers to
14139 declarations. Declarations ought to have the DW_AT_declaration
14140 flag. It happens that GCC forgets to put it in sometimes, but
14141 only for functions, not for types.
14143 Adding more things than necessary to the hash table is harmless
14144 except for the performance cost. Adding too few will result in
14145 wasted time in find_partial_die, when we reread the compilation
14146 unit with load_all_dies set. */
14149 || abbrev->tag == DW_TAG_constant
14150 || abbrev->tag == DW_TAG_subprogram
14151 || abbrev->tag == DW_TAG_variable
14152 || abbrev->tag == DW_TAG_namespace
14153 || part_die->is_declaration)
14157 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14158 part_die->offset.sect_off, INSERT);
14162 part_die = obstack_alloc (&cu->comp_unit_obstack,
14163 sizeof (struct partial_die_info));
14165 /* For some DIEs we want to follow their children (if any). For C
14166 we have no reason to follow the children of structures; for other
14167 languages we have to, so that we can get at method physnames
14168 to infer fully qualified class names, for DW_AT_specification,
14169 and for C++ template arguments. For C++, we also look one level
14170 inside functions to find template arguments (if the name of the
14171 function does not already contain the template arguments).
14173 For Ada, we need to scan the children of subprograms and lexical
14174 blocks as well because Ada allows the definition of nested
14175 entities that could be interesting for the debugger, such as
14176 nested subprograms for instance. */
14177 if (last_die->has_children
14179 || last_die->tag == DW_TAG_namespace
14180 || last_die->tag == DW_TAG_module
14181 || last_die->tag == DW_TAG_enumeration_type
14182 || (cu->language == language_cplus
14183 && last_die->tag == DW_TAG_subprogram
14184 && (last_die->name == NULL
14185 || strchr (last_die->name, '<') == NULL))
14186 || (cu->language != language_c
14187 && (last_die->tag == DW_TAG_class_type
14188 || last_die->tag == DW_TAG_interface_type
14189 || last_die->tag == DW_TAG_structure_type
14190 || last_die->tag == DW_TAG_union_type))
14191 || (cu->language == language_ada
14192 && (last_die->tag == DW_TAG_subprogram
14193 || last_die->tag == DW_TAG_lexical_block))))
14196 parent_die = last_die;
14200 /* Otherwise we skip to the next sibling, if any. */
14201 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
14203 /* Back to the top, do it again. */
14207 /* Read a minimal amount of information into the minimal die structure. */
14209 static const gdb_byte *
14210 read_partial_die (const struct die_reader_specs *reader,
14211 struct partial_die_info *part_die,
14212 struct abbrev_info *abbrev, unsigned int abbrev_len,
14213 const gdb_byte *info_ptr)
14215 struct dwarf2_cu *cu = reader->cu;
14216 struct objfile *objfile = cu->objfile;
14217 const gdb_byte *buffer = reader->buffer;
14219 struct attribute attr;
14220 int has_low_pc_attr = 0;
14221 int has_high_pc_attr = 0;
14222 int high_pc_relative = 0;
14224 memset (part_die, 0, sizeof (struct partial_die_info));
14226 part_die->offset.sect_off = info_ptr - buffer;
14228 info_ptr += abbrev_len;
14230 if (abbrev == NULL)
14233 part_die->tag = abbrev->tag;
14234 part_die->has_children = abbrev->has_children;
14236 for (i = 0; i < abbrev->num_attrs; ++i)
14238 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
14240 /* Store the data if it is of an attribute we want to keep in a
14241 partial symbol table. */
14245 switch (part_die->tag)
14247 case DW_TAG_compile_unit:
14248 case DW_TAG_partial_unit:
14249 case DW_TAG_type_unit:
14250 /* Compilation units have a DW_AT_name that is a filename, not
14251 a source language identifier. */
14252 case DW_TAG_enumeration_type:
14253 case DW_TAG_enumerator:
14254 /* These tags always have simple identifiers already; no need
14255 to canonicalize them. */
14256 part_die->name = DW_STRING (&attr);
14260 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
14261 &objfile->objfile_obstack);
14265 case DW_AT_linkage_name:
14266 case DW_AT_MIPS_linkage_name:
14267 /* Note that both forms of linkage name might appear. We
14268 assume they will be the same, and we only store the last
14270 if (cu->language == language_ada)
14271 part_die->name = DW_STRING (&attr);
14272 part_die->linkage_name = DW_STRING (&attr);
14275 has_low_pc_attr = 1;
14276 part_die->lowpc = DW_ADDR (&attr);
14278 case DW_AT_high_pc:
14279 has_high_pc_attr = 1;
14280 if (attr.form == DW_FORM_addr
14281 || attr.form == DW_FORM_GNU_addr_index)
14282 part_die->highpc = DW_ADDR (&attr);
14285 high_pc_relative = 1;
14286 part_die->highpc = DW_UNSND (&attr);
14289 case DW_AT_location:
14290 /* Support the .debug_loc offsets. */
14291 if (attr_form_is_block (&attr))
14293 part_die->d.locdesc = DW_BLOCK (&attr);
14295 else if (attr_form_is_section_offset (&attr))
14297 dwarf2_complex_location_expr_complaint ();
14301 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14302 "partial symbol information");
14305 case DW_AT_external:
14306 part_die->is_external = DW_UNSND (&attr);
14308 case DW_AT_declaration:
14309 part_die->is_declaration = DW_UNSND (&attr);
14312 part_die->has_type = 1;
14314 case DW_AT_abstract_origin:
14315 case DW_AT_specification:
14316 case DW_AT_extension:
14317 part_die->has_specification = 1;
14318 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
14319 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14320 || cu->per_cu->is_dwz);
14322 case DW_AT_sibling:
14323 /* Ignore absolute siblings, they might point outside of
14324 the current compile unit. */
14325 if (attr.form == DW_FORM_ref_addr)
14326 complaint (&symfile_complaints,
14327 _("ignoring absolute DW_AT_sibling"));
14329 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
14331 case DW_AT_byte_size:
14332 part_die->has_byte_size = 1;
14334 case DW_AT_calling_convention:
14335 /* DWARF doesn't provide a way to identify a program's source-level
14336 entry point. DW_AT_calling_convention attributes are only meant
14337 to describe functions' calling conventions.
14339 However, because it's a necessary piece of information in
14340 Fortran, and because DW_CC_program is the only piece of debugging
14341 information whose definition refers to a 'main program' at all,
14342 several compilers have begun marking Fortran main programs with
14343 DW_CC_program --- even when those functions use the standard
14344 calling conventions.
14346 So until DWARF specifies a way to provide this information and
14347 compilers pick up the new representation, we'll support this
14349 if (DW_UNSND (&attr) == DW_CC_program
14350 && cu->language == language_fortran)
14352 set_main_name (part_die->name);
14354 /* As this DIE has a static linkage the name would be difficult
14355 to look up later. */
14356 language_of_main = language_fortran;
14360 if (DW_UNSND (&attr) == DW_INL_inlined
14361 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14362 part_die->may_be_inlined = 1;
14366 if (part_die->tag == DW_TAG_imported_unit)
14368 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14369 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14370 || cu->per_cu->is_dwz);
14379 if (high_pc_relative)
14380 part_die->highpc += part_die->lowpc;
14382 if (has_low_pc_attr && has_high_pc_attr)
14384 /* When using the GNU linker, .gnu.linkonce. sections are used to
14385 eliminate duplicate copies of functions and vtables and such.
14386 The linker will arbitrarily choose one and discard the others.
14387 The AT_*_pc values for such functions refer to local labels in
14388 these sections. If the section from that file was discarded, the
14389 labels are not in the output, so the relocs get a value of 0.
14390 If this is a discarded function, mark the pc bounds as invalid,
14391 so that GDB will ignore it. */
14392 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14394 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14396 complaint (&symfile_complaints,
14397 _("DW_AT_low_pc %s is zero "
14398 "for DIE at 0x%x [in module %s]"),
14399 paddress (gdbarch, part_die->lowpc),
14400 part_die->offset.sect_off, objfile->name);
14402 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14403 else if (part_die->lowpc >= part_die->highpc)
14405 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14407 complaint (&symfile_complaints,
14408 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14409 "for DIE at 0x%x [in module %s]"),
14410 paddress (gdbarch, part_die->lowpc),
14411 paddress (gdbarch, part_die->highpc),
14412 part_die->offset.sect_off, objfile->name);
14415 part_die->has_pc_info = 1;
14421 /* Find a cached partial DIE at OFFSET in CU. */
14423 static struct partial_die_info *
14424 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
14426 struct partial_die_info *lookup_die = NULL;
14427 struct partial_die_info part_die;
14429 part_die.offset = offset;
14430 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14436 /* Find a partial DIE at OFFSET, which may or may not be in CU,
14437 except in the case of .debug_types DIEs which do not reference
14438 outside their CU (they do however referencing other types via
14439 DW_FORM_ref_sig8). */
14441 static struct partial_die_info *
14442 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
14444 struct objfile *objfile = cu->objfile;
14445 struct dwarf2_per_cu_data *per_cu = NULL;
14446 struct partial_die_info *pd = NULL;
14448 if (offset_in_dwz == cu->per_cu->is_dwz
14449 && offset_in_cu_p (&cu->header, offset))
14451 pd = find_partial_die_in_comp_unit (offset, cu);
14454 /* We missed recording what we needed.
14455 Load all dies and try again. */
14456 per_cu = cu->per_cu;
14460 /* TUs don't reference other CUs/TUs (except via type signatures). */
14461 if (cu->per_cu->is_debug_types)
14463 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14464 " external reference to offset 0x%lx [in module %s].\n"),
14465 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14466 bfd_get_filename (objfile->obfd));
14468 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14471 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14472 load_partial_comp_unit (per_cu);
14474 per_cu->cu->last_used = 0;
14475 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14478 /* If we didn't find it, and not all dies have been loaded,
14479 load them all and try again. */
14481 if (pd == NULL && per_cu->load_all_dies == 0)
14483 per_cu->load_all_dies = 1;
14485 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14486 THIS_CU->cu may already be in use. So we can't just free it and
14487 replace its DIEs with the ones we read in. Instead, we leave those
14488 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14489 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14491 load_partial_comp_unit (per_cu);
14493 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14497 internal_error (__FILE__, __LINE__,
14498 _("could not find partial DIE 0x%x "
14499 "in cache [from module %s]\n"),
14500 offset.sect_off, bfd_get_filename (objfile->obfd));
14504 /* See if we can figure out if the class lives in a namespace. We do
14505 this by looking for a member function; its demangled name will
14506 contain namespace info, if there is any. */
14509 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14510 struct dwarf2_cu *cu)
14512 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14513 what template types look like, because the demangler
14514 frequently doesn't give the same name as the debug info. We
14515 could fix this by only using the demangled name to get the
14516 prefix (but see comment in read_structure_type). */
14518 struct partial_die_info *real_pdi;
14519 struct partial_die_info *child_pdi;
14521 /* If this DIE (this DIE's specification, if any) has a parent, then
14522 we should not do this. We'll prepend the parent's fully qualified
14523 name when we create the partial symbol. */
14525 real_pdi = struct_pdi;
14526 while (real_pdi->has_specification)
14527 real_pdi = find_partial_die (real_pdi->spec_offset,
14528 real_pdi->spec_is_dwz, cu);
14530 if (real_pdi->die_parent != NULL)
14533 for (child_pdi = struct_pdi->die_child;
14535 child_pdi = child_pdi->die_sibling)
14537 if (child_pdi->tag == DW_TAG_subprogram
14538 && child_pdi->linkage_name != NULL)
14540 char *actual_class_name
14541 = language_class_name_from_physname (cu->language_defn,
14542 child_pdi->linkage_name);
14543 if (actual_class_name != NULL)
14546 = obstack_copy0 (&cu->objfile->objfile_obstack,
14548 strlen (actual_class_name));
14549 xfree (actual_class_name);
14556 /* Adjust PART_DIE before generating a symbol for it. This function
14557 may set the is_external flag or change the DIE's name. */
14560 fixup_partial_die (struct partial_die_info *part_die,
14561 struct dwarf2_cu *cu)
14563 /* Once we've fixed up a die, there's no point in doing so again.
14564 This also avoids a memory leak if we were to call
14565 guess_partial_die_structure_name multiple times. */
14566 if (part_die->fixup_called)
14569 /* If we found a reference attribute and the DIE has no name, try
14570 to find a name in the referred to DIE. */
14572 if (part_die->name == NULL && part_die->has_specification)
14574 struct partial_die_info *spec_die;
14576 spec_die = find_partial_die (part_die->spec_offset,
14577 part_die->spec_is_dwz, cu);
14579 fixup_partial_die (spec_die, cu);
14581 if (spec_die->name)
14583 part_die->name = spec_die->name;
14585 /* Copy DW_AT_external attribute if it is set. */
14586 if (spec_die->is_external)
14587 part_die->is_external = spec_die->is_external;
14591 /* Set default names for some unnamed DIEs. */
14593 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14594 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14596 /* If there is no parent die to provide a namespace, and there are
14597 children, see if we can determine the namespace from their linkage
14599 if (cu->language == language_cplus
14600 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14601 && part_die->die_parent == NULL
14602 && part_die->has_children
14603 && (part_die->tag == DW_TAG_class_type
14604 || part_die->tag == DW_TAG_structure_type
14605 || part_die->tag == DW_TAG_union_type))
14606 guess_partial_die_structure_name (part_die, cu);
14608 /* GCC might emit a nameless struct or union that has a linkage
14609 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14610 if (part_die->name == NULL
14611 && (part_die->tag == DW_TAG_class_type
14612 || part_die->tag == DW_TAG_interface_type
14613 || part_die->tag == DW_TAG_structure_type
14614 || part_die->tag == DW_TAG_union_type)
14615 && part_die->linkage_name != NULL)
14619 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
14624 /* Strip any leading namespaces/classes, keep only the base name.
14625 DW_AT_name for named DIEs does not contain the prefixes. */
14626 base = strrchr (demangled, ':');
14627 if (base && base > demangled && base[-1] == ':')
14632 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14633 base, strlen (base));
14638 part_die->fixup_called = 1;
14641 /* Read an attribute value described by an attribute form. */
14643 static const gdb_byte *
14644 read_attribute_value (const struct die_reader_specs *reader,
14645 struct attribute *attr, unsigned form,
14646 const gdb_byte *info_ptr)
14648 struct dwarf2_cu *cu = reader->cu;
14649 bfd *abfd = reader->abfd;
14650 struct comp_unit_head *cu_header = &cu->header;
14651 unsigned int bytes_read;
14652 struct dwarf_block *blk;
14657 case DW_FORM_ref_addr:
14658 if (cu->header.version == 2)
14659 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14661 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14662 &cu->header, &bytes_read);
14663 info_ptr += bytes_read;
14665 case DW_FORM_GNU_ref_alt:
14666 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14667 info_ptr += bytes_read;
14670 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14671 info_ptr += bytes_read;
14673 case DW_FORM_block2:
14674 blk = dwarf_alloc_block (cu);
14675 blk->size = read_2_bytes (abfd, info_ptr);
14677 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14678 info_ptr += blk->size;
14679 DW_BLOCK (attr) = blk;
14681 case DW_FORM_block4:
14682 blk = dwarf_alloc_block (cu);
14683 blk->size = read_4_bytes (abfd, info_ptr);
14685 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14686 info_ptr += blk->size;
14687 DW_BLOCK (attr) = blk;
14689 case DW_FORM_data2:
14690 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14693 case DW_FORM_data4:
14694 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14697 case DW_FORM_data8:
14698 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14701 case DW_FORM_sec_offset:
14702 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14703 info_ptr += bytes_read;
14705 case DW_FORM_string:
14706 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14707 DW_STRING_IS_CANONICAL (attr) = 0;
14708 info_ptr += bytes_read;
14711 if (!cu->per_cu->is_dwz)
14713 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14715 DW_STRING_IS_CANONICAL (attr) = 0;
14716 info_ptr += bytes_read;
14720 case DW_FORM_GNU_strp_alt:
14722 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14723 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14726 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14727 DW_STRING_IS_CANONICAL (attr) = 0;
14728 info_ptr += bytes_read;
14731 case DW_FORM_exprloc:
14732 case DW_FORM_block:
14733 blk = dwarf_alloc_block (cu);
14734 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14735 info_ptr += bytes_read;
14736 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14737 info_ptr += blk->size;
14738 DW_BLOCK (attr) = blk;
14740 case DW_FORM_block1:
14741 blk = dwarf_alloc_block (cu);
14742 blk->size = read_1_byte (abfd, info_ptr);
14744 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14745 info_ptr += blk->size;
14746 DW_BLOCK (attr) = blk;
14748 case DW_FORM_data1:
14749 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14753 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14756 case DW_FORM_flag_present:
14757 DW_UNSND (attr) = 1;
14759 case DW_FORM_sdata:
14760 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14761 info_ptr += bytes_read;
14763 case DW_FORM_udata:
14764 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14765 info_ptr += bytes_read;
14768 DW_UNSND (attr) = (cu->header.offset.sect_off
14769 + read_1_byte (abfd, info_ptr));
14773 DW_UNSND (attr) = (cu->header.offset.sect_off
14774 + read_2_bytes (abfd, info_ptr));
14778 DW_UNSND (attr) = (cu->header.offset.sect_off
14779 + read_4_bytes (abfd, info_ptr));
14783 DW_UNSND (attr) = (cu->header.offset.sect_off
14784 + read_8_bytes (abfd, info_ptr));
14787 case DW_FORM_ref_sig8:
14788 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
14791 case DW_FORM_ref_udata:
14792 DW_UNSND (attr) = (cu->header.offset.sect_off
14793 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14794 info_ptr += bytes_read;
14796 case DW_FORM_indirect:
14797 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14798 info_ptr += bytes_read;
14799 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14801 case DW_FORM_GNU_addr_index:
14802 if (reader->dwo_file == NULL)
14804 /* For now flag a hard error.
14805 Later we can turn this into a complaint. */
14806 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14807 dwarf_form_name (form),
14808 bfd_get_filename (abfd));
14810 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14811 info_ptr += bytes_read;
14813 case DW_FORM_GNU_str_index:
14814 if (reader->dwo_file == NULL)
14816 /* For now flag a hard error.
14817 Later we can turn this into a complaint if warranted. */
14818 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14819 dwarf_form_name (form),
14820 bfd_get_filename (abfd));
14823 ULONGEST str_index =
14824 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14826 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14827 DW_STRING_IS_CANONICAL (attr) = 0;
14828 info_ptr += bytes_read;
14832 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14833 dwarf_form_name (form),
14834 bfd_get_filename (abfd));
14838 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
14839 attr->form = DW_FORM_GNU_ref_alt;
14841 /* We have seen instances where the compiler tried to emit a byte
14842 size attribute of -1 which ended up being encoded as an unsigned
14843 0xffffffff. Although 0xffffffff is technically a valid size value,
14844 an object of this size seems pretty unlikely so we can relatively
14845 safely treat these cases as if the size attribute was invalid and
14846 treat them as zero by default. */
14847 if (attr->name == DW_AT_byte_size
14848 && form == DW_FORM_data4
14849 && DW_UNSND (attr) >= 0xffffffff)
14852 (&symfile_complaints,
14853 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14854 hex_string (DW_UNSND (attr)));
14855 DW_UNSND (attr) = 0;
14861 /* Read an attribute described by an abbreviated attribute. */
14863 static const gdb_byte *
14864 read_attribute (const struct die_reader_specs *reader,
14865 struct attribute *attr, struct attr_abbrev *abbrev,
14866 const gdb_byte *info_ptr)
14868 attr->name = abbrev->name;
14869 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14872 /* Read dwarf information from a buffer. */
14874 static unsigned int
14875 read_1_byte (bfd *abfd, const gdb_byte *buf)
14877 return bfd_get_8 (abfd, buf);
14881 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14883 return bfd_get_signed_8 (abfd, buf);
14886 static unsigned int
14887 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14889 return bfd_get_16 (abfd, buf);
14893 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14895 return bfd_get_signed_16 (abfd, buf);
14898 static unsigned int
14899 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14901 return bfd_get_32 (abfd, buf);
14905 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14907 return bfd_get_signed_32 (abfd, buf);
14911 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14913 return bfd_get_64 (abfd, buf);
14917 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
14918 unsigned int *bytes_read)
14920 struct comp_unit_head *cu_header = &cu->header;
14921 CORE_ADDR retval = 0;
14923 if (cu_header->signed_addr_p)
14925 switch (cu_header->addr_size)
14928 retval = bfd_get_signed_16 (abfd, buf);
14931 retval = bfd_get_signed_32 (abfd, buf);
14934 retval = bfd_get_signed_64 (abfd, buf);
14937 internal_error (__FILE__, __LINE__,
14938 _("read_address: bad switch, signed [in module %s]"),
14939 bfd_get_filename (abfd));
14944 switch (cu_header->addr_size)
14947 retval = bfd_get_16 (abfd, buf);
14950 retval = bfd_get_32 (abfd, buf);
14953 retval = bfd_get_64 (abfd, buf);
14956 internal_error (__FILE__, __LINE__,
14957 _("read_address: bad switch, "
14958 "unsigned [in module %s]"),
14959 bfd_get_filename (abfd));
14963 *bytes_read = cu_header->addr_size;
14967 /* Read the initial length from a section. The (draft) DWARF 3
14968 specification allows the initial length to take up either 4 bytes
14969 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14970 bytes describe the length and all offsets will be 8 bytes in length
14973 An older, non-standard 64-bit format is also handled by this
14974 function. The older format in question stores the initial length
14975 as an 8-byte quantity without an escape value. Lengths greater
14976 than 2^32 aren't very common which means that the initial 4 bytes
14977 is almost always zero. Since a length value of zero doesn't make
14978 sense for the 32-bit format, this initial zero can be considered to
14979 be an escape value which indicates the presence of the older 64-bit
14980 format. As written, the code can't detect (old format) lengths
14981 greater than 4GB. If it becomes necessary to handle lengths
14982 somewhat larger than 4GB, we could allow other small values (such
14983 as the non-sensical values of 1, 2, and 3) to also be used as
14984 escape values indicating the presence of the old format.
14986 The value returned via bytes_read should be used to increment the
14987 relevant pointer after calling read_initial_length().
14989 [ Note: read_initial_length() and read_offset() are based on the
14990 document entitled "DWARF Debugging Information Format", revision
14991 3, draft 8, dated November 19, 2001. This document was obtained
14994 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14996 This document is only a draft and is subject to change. (So beware.)
14998 Details regarding the older, non-standard 64-bit format were
14999 determined empirically by examining 64-bit ELF files produced by
15000 the SGI toolchain on an IRIX 6.5 machine.
15002 - Kevin, July 16, 2002
15006 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
15008 LONGEST length = bfd_get_32 (abfd, buf);
15010 if (length == 0xffffffff)
15012 length = bfd_get_64 (abfd, buf + 4);
15015 else if (length == 0)
15017 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
15018 length = bfd_get_64 (abfd, buf);
15029 /* Cover function for read_initial_length.
15030 Returns the length of the object at BUF, and stores the size of the
15031 initial length in *BYTES_READ and stores the size that offsets will be in
15033 If the initial length size is not equivalent to that specified in
15034 CU_HEADER then issue a complaint.
15035 This is useful when reading non-comp-unit headers. */
15038 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
15039 const struct comp_unit_head *cu_header,
15040 unsigned int *bytes_read,
15041 unsigned int *offset_size)
15043 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15045 gdb_assert (cu_header->initial_length_size == 4
15046 || cu_header->initial_length_size == 8
15047 || cu_header->initial_length_size == 12);
15049 if (cu_header->initial_length_size != *bytes_read)
15050 complaint (&symfile_complaints,
15051 _("intermixed 32-bit and 64-bit DWARF sections"));
15053 *offset_size = (*bytes_read == 4) ? 4 : 8;
15057 /* Read an offset from the data stream. The size of the offset is
15058 given by cu_header->offset_size. */
15061 read_offset (bfd *abfd, const gdb_byte *buf,
15062 const struct comp_unit_head *cu_header,
15063 unsigned int *bytes_read)
15065 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
15067 *bytes_read = cu_header->offset_size;
15071 /* Read an offset from the data stream. */
15074 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15076 LONGEST retval = 0;
15078 switch (offset_size)
15081 retval = bfd_get_32 (abfd, buf);
15084 retval = bfd_get_64 (abfd, buf);
15087 internal_error (__FILE__, __LINE__,
15088 _("read_offset_1: bad switch [in module %s]"),
15089 bfd_get_filename (abfd));
15095 static const gdb_byte *
15096 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15098 /* If the size of a host char is 8 bits, we can return a pointer
15099 to the buffer, otherwise we have to copy the data to a buffer
15100 allocated on the temporary obstack. */
15101 gdb_assert (HOST_CHAR_BIT == 8);
15105 static const char *
15106 read_direct_string (bfd *abfd, const gdb_byte *buf,
15107 unsigned int *bytes_read_ptr)
15109 /* If the size of a host char is 8 bits, we can return a pointer
15110 to the string, otherwise we have to copy the string to a buffer
15111 allocated on the temporary obstack. */
15112 gdb_assert (HOST_CHAR_BIT == 8);
15115 *bytes_read_ptr = 1;
15118 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15119 return (const char *) buf;
15122 static const char *
15123 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15125 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15126 if (dwarf2_per_objfile->str.buffer == NULL)
15127 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15128 bfd_get_filename (abfd));
15129 if (str_offset >= dwarf2_per_objfile->str.size)
15130 error (_("DW_FORM_strp pointing outside of "
15131 ".debug_str section [in module %s]"),
15132 bfd_get_filename (abfd));
15133 gdb_assert (HOST_CHAR_BIT == 8);
15134 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15136 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15139 /* Read a string at offset STR_OFFSET in the .debug_str section from
15140 the .dwz file DWZ. Throw an error if the offset is too large. If
15141 the string consists of a single NUL byte, return NULL; otherwise
15142 return a pointer to the string. */
15144 static const char *
15145 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15147 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15149 if (dwz->str.buffer == NULL)
15150 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15151 "section [in module %s]"),
15152 bfd_get_filename (dwz->dwz_bfd));
15153 if (str_offset >= dwz->str.size)
15154 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15155 ".debug_str section [in module %s]"),
15156 bfd_get_filename (dwz->dwz_bfd));
15157 gdb_assert (HOST_CHAR_BIT == 8);
15158 if (dwz->str.buffer[str_offset] == '\0')
15160 return (const char *) (dwz->str.buffer + str_offset);
15163 static const char *
15164 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15165 const struct comp_unit_head *cu_header,
15166 unsigned int *bytes_read_ptr)
15168 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15170 return read_indirect_string_at_offset (abfd, str_offset);
15174 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15175 unsigned int *bytes_read_ptr)
15178 unsigned int num_read;
15180 unsigned char byte;
15188 byte = bfd_get_8 (abfd, buf);
15191 result |= ((ULONGEST) (byte & 127) << shift);
15192 if ((byte & 128) == 0)
15198 *bytes_read_ptr = num_read;
15203 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15204 unsigned int *bytes_read_ptr)
15207 int i, shift, num_read;
15208 unsigned char byte;
15216 byte = bfd_get_8 (abfd, buf);
15219 result |= ((LONGEST) (byte & 127) << shift);
15221 if ((byte & 128) == 0)
15226 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
15227 result |= -(((LONGEST) 1) << shift);
15228 *bytes_read_ptr = num_read;
15232 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
15233 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15234 ADDR_SIZE is the size of addresses from the CU header. */
15237 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15239 struct objfile *objfile = dwarf2_per_objfile->objfile;
15240 bfd *abfd = objfile->obfd;
15241 const gdb_byte *info_ptr;
15243 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15244 if (dwarf2_per_objfile->addr.buffer == NULL)
15245 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15247 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15248 error (_("DW_FORM_addr_index pointing outside of "
15249 ".debug_addr section [in module %s]"),
15251 info_ptr = (dwarf2_per_objfile->addr.buffer
15252 + addr_base + addr_index * addr_size);
15253 if (addr_size == 4)
15254 return bfd_get_32 (abfd, info_ptr);
15256 return bfd_get_64 (abfd, info_ptr);
15259 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15262 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15264 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15267 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15270 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
15271 unsigned int *bytes_read)
15273 bfd *abfd = cu->objfile->obfd;
15274 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15276 return read_addr_index (cu, addr_index);
15279 /* Data structure to pass results from dwarf2_read_addr_index_reader
15280 back to dwarf2_read_addr_index. */
15282 struct dwarf2_read_addr_index_data
15284 ULONGEST addr_base;
15288 /* die_reader_func for dwarf2_read_addr_index. */
15291 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
15292 const gdb_byte *info_ptr,
15293 struct die_info *comp_unit_die,
15297 struct dwarf2_cu *cu = reader->cu;
15298 struct dwarf2_read_addr_index_data *aidata =
15299 (struct dwarf2_read_addr_index_data *) data;
15301 aidata->addr_base = cu->addr_base;
15302 aidata->addr_size = cu->header.addr_size;
15305 /* Given an index in .debug_addr, fetch the value.
15306 NOTE: This can be called during dwarf expression evaluation,
15307 long after the debug information has been read, and thus per_cu->cu
15308 may no longer exist. */
15311 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15312 unsigned int addr_index)
15314 struct objfile *objfile = per_cu->objfile;
15315 struct dwarf2_cu *cu = per_cu->cu;
15316 ULONGEST addr_base;
15319 /* This is intended to be called from outside this file. */
15320 dw2_setup (objfile);
15322 /* We need addr_base and addr_size.
15323 If we don't have PER_CU->cu, we have to get it.
15324 Nasty, but the alternative is storing the needed info in PER_CU,
15325 which at this point doesn't seem justified: it's not clear how frequently
15326 it would get used and it would increase the size of every PER_CU.
15327 Entry points like dwarf2_per_cu_addr_size do a similar thing
15328 so we're not in uncharted territory here.
15329 Alas we need to be a bit more complicated as addr_base is contained
15332 We don't need to read the entire CU(/TU).
15333 We just need the header and top level die.
15335 IWBN to use the aging mechanism to let us lazily later discard the CU.
15336 For now we skip this optimization. */
15340 addr_base = cu->addr_base;
15341 addr_size = cu->header.addr_size;
15345 struct dwarf2_read_addr_index_data aidata;
15347 /* Note: We can't use init_cutu_and_read_dies_simple here,
15348 we need addr_base. */
15349 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15350 dwarf2_read_addr_index_reader, &aidata);
15351 addr_base = aidata.addr_base;
15352 addr_size = aidata.addr_size;
15355 return read_addr_index_1 (addr_index, addr_base, addr_size);
15358 /* Given a DW_AT_str_index, fetch the string. */
15360 static const char *
15361 read_str_index (const struct die_reader_specs *reader,
15362 struct dwarf2_cu *cu, ULONGEST str_index)
15364 struct objfile *objfile = dwarf2_per_objfile->objfile;
15365 const char *dwo_name = objfile->name;
15366 bfd *abfd = objfile->obfd;
15367 struct dwo_sections *sections = &reader->dwo_file->sections;
15368 const gdb_byte *info_ptr;
15369 ULONGEST str_offset;
15371 dwarf2_read_section (objfile, §ions->str);
15372 dwarf2_read_section (objfile, §ions->str_offsets);
15373 if (sections->str.buffer == NULL)
15374 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15375 " in CU at offset 0x%lx [in module %s]"),
15376 (long) cu->header.offset.sect_off, dwo_name);
15377 if (sections->str_offsets.buffer == NULL)
15378 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15379 " in CU at offset 0x%lx [in module %s]"),
15380 (long) cu->header.offset.sect_off, dwo_name);
15381 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15382 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15383 " section in CU at offset 0x%lx [in module %s]"),
15384 (long) cu->header.offset.sect_off, dwo_name);
15385 info_ptr = (sections->str_offsets.buffer
15386 + str_index * cu->header.offset_size);
15387 if (cu->header.offset_size == 4)
15388 str_offset = bfd_get_32 (abfd, info_ptr);
15390 str_offset = bfd_get_64 (abfd, info_ptr);
15391 if (str_offset >= sections->str.size)
15392 error (_("Offset from DW_FORM_str_index pointing outside of"
15393 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15394 (long) cu->header.offset.sect_off, dwo_name);
15395 return (const char *) (sections->str.buffer + str_offset);
15398 /* Return the length of an LEB128 number in BUF. */
15401 leb128_size (const gdb_byte *buf)
15403 const gdb_byte *begin = buf;
15409 if ((byte & 128) == 0)
15410 return buf - begin;
15415 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
15423 cu->language = language_c;
15425 case DW_LANG_C_plus_plus:
15426 cu->language = language_cplus;
15429 cu->language = language_d;
15431 case DW_LANG_Fortran77:
15432 case DW_LANG_Fortran90:
15433 case DW_LANG_Fortran95:
15434 cu->language = language_fortran;
15437 cu->language = language_go;
15439 case DW_LANG_Mips_Assembler:
15440 cu->language = language_asm;
15443 cu->language = language_java;
15445 case DW_LANG_Ada83:
15446 case DW_LANG_Ada95:
15447 cu->language = language_ada;
15449 case DW_LANG_Modula2:
15450 cu->language = language_m2;
15452 case DW_LANG_Pascal83:
15453 cu->language = language_pascal;
15456 cu->language = language_objc;
15458 case DW_LANG_Cobol74:
15459 case DW_LANG_Cobol85:
15461 cu->language = language_minimal;
15464 cu->language_defn = language_def (cu->language);
15467 /* Return the named attribute or NULL if not there. */
15469 static struct attribute *
15470 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15475 struct attribute *spec = NULL;
15477 for (i = 0; i < die->num_attrs; ++i)
15479 if (die->attrs[i].name == name)
15480 return &die->attrs[i];
15481 if (die->attrs[i].name == DW_AT_specification
15482 || die->attrs[i].name == DW_AT_abstract_origin)
15483 spec = &die->attrs[i];
15489 die = follow_die_ref (die, spec, &cu);
15495 /* Return the named attribute or NULL if not there,
15496 but do not follow DW_AT_specification, etc.
15497 This is for use in contexts where we're reading .debug_types dies.
15498 Following DW_AT_specification, DW_AT_abstract_origin will take us
15499 back up the chain, and we want to go down. */
15501 static struct attribute *
15502 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15506 for (i = 0; i < die->num_attrs; ++i)
15507 if (die->attrs[i].name == name)
15508 return &die->attrs[i];
15513 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15514 and holds a non-zero value. This function should only be used for
15515 DW_FORM_flag or DW_FORM_flag_present attributes. */
15518 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15520 struct attribute *attr = dwarf2_attr (die, name, cu);
15522 return (attr && DW_UNSND (attr));
15526 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15528 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15529 which value is non-zero. However, we have to be careful with
15530 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15531 (via dwarf2_flag_true_p) follows this attribute. So we may
15532 end up accidently finding a declaration attribute that belongs
15533 to a different DIE referenced by the specification attribute,
15534 even though the given DIE does not have a declaration attribute. */
15535 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15536 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15539 /* Return the die giving the specification for DIE, if there is
15540 one. *SPEC_CU is the CU containing DIE on input, and the CU
15541 containing the return value on output. If there is no
15542 specification, but there is an abstract origin, that is
15545 static struct die_info *
15546 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15548 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15551 if (spec_attr == NULL)
15552 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15554 if (spec_attr == NULL)
15557 return follow_die_ref (die, spec_attr, spec_cu);
15560 /* Free the line_header structure *LH, and any arrays and strings it
15562 NOTE: This is also used as a "cleanup" function. */
15565 free_line_header (struct line_header *lh)
15567 if (lh->standard_opcode_lengths)
15568 xfree (lh->standard_opcode_lengths);
15570 /* Remember that all the lh->file_names[i].name pointers are
15571 pointers into debug_line_buffer, and don't need to be freed. */
15572 if (lh->file_names)
15573 xfree (lh->file_names);
15575 /* Similarly for the include directory names. */
15576 if (lh->include_dirs)
15577 xfree (lh->include_dirs);
15582 /* Add an entry to LH's include directory table. */
15585 add_include_dir (struct line_header *lh, const char *include_dir)
15587 /* Grow the array if necessary. */
15588 if (lh->include_dirs_size == 0)
15590 lh->include_dirs_size = 1; /* for testing */
15591 lh->include_dirs = xmalloc (lh->include_dirs_size
15592 * sizeof (*lh->include_dirs));
15594 else if (lh->num_include_dirs >= lh->include_dirs_size)
15596 lh->include_dirs_size *= 2;
15597 lh->include_dirs = xrealloc (lh->include_dirs,
15598 (lh->include_dirs_size
15599 * sizeof (*lh->include_dirs)));
15602 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15605 /* Add an entry to LH's file name table. */
15608 add_file_name (struct line_header *lh,
15610 unsigned int dir_index,
15611 unsigned int mod_time,
15612 unsigned int length)
15614 struct file_entry *fe;
15616 /* Grow the array if necessary. */
15617 if (lh->file_names_size == 0)
15619 lh->file_names_size = 1; /* for testing */
15620 lh->file_names = xmalloc (lh->file_names_size
15621 * sizeof (*lh->file_names));
15623 else if (lh->num_file_names >= lh->file_names_size)
15625 lh->file_names_size *= 2;
15626 lh->file_names = xrealloc (lh->file_names,
15627 (lh->file_names_size
15628 * sizeof (*lh->file_names)));
15631 fe = &lh->file_names[lh->num_file_names++];
15633 fe->dir_index = dir_index;
15634 fe->mod_time = mod_time;
15635 fe->length = length;
15636 fe->included_p = 0;
15640 /* A convenience function to find the proper .debug_line section for a
15643 static struct dwarf2_section_info *
15644 get_debug_line_section (struct dwarf2_cu *cu)
15646 struct dwarf2_section_info *section;
15648 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15650 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15651 section = &cu->dwo_unit->dwo_file->sections.line;
15652 else if (cu->per_cu->is_dwz)
15654 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15656 section = &dwz->line;
15659 section = &dwarf2_per_objfile->line;
15664 /* Read the statement program header starting at OFFSET in
15665 .debug_line, or .debug_line.dwo. Return a pointer
15666 to a struct line_header, allocated using xmalloc.
15668 NOTE: the strings in the include directory and file name tables of
15669 the returned object point into the dwarf line section buffer,
15670 and must not be freed. */
15672 static struct line_header *
15673 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15675 struct cleanup *back_to;
15676 struct line_header *lh;
15677 const gdb_byte *line_ptr;
15678 unsigned int bytes_read, offset_size;
15680 const char *cur_dir, *cur_file;
15681 struct dwarf2_section_info *section;
15684 section = get_debug_line_section (cu);
15685 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15686 if (section->buffer == NULL)
15688 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15689 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15691 complaint (&symfile_complaints, _("missing .debug_line section"));
15695 /* We can't do this until we know the section is non-empty.
15696 Only then do we know we have such a section. */
15697 abfd = section->asection->owner;
15699 /* Make sure that at least there's room for the total_length field.
15700 That could be 12 bytes long, but we're just going to fudge that. */
15701 if (offset + 4 >= section->size)
15703 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15707 lh = xmalloc (sizeof (*lh));
15708 memset (lh, 0, sizeof (*lh));
15709 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15712 line_ptr = section->buffer + offset;
15714 /* Read in the header. */
15716 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15717 &bytes_read, &offset_size);
15718 line_ptr += bytes_read;
15719 if (line_ptr + lh->total_length > (section->buffer + section->size))
15721 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15722 do_cleanups (back_to);
15725 lh->statement_program_end = line_ptr + lh->total_length;
15726 lh->version = read_2_bytes (abfd, line_ptr);
15728 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15729 line_ptr += offset_size;
15730 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15732 if (lh->version >= 4)
15734 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15738 lh->maximum_ops_per_instruction = 1;
15740 if (lh->maximum_ops_per_instruction == 0)
15742 lh->maximum_ops_per_instruction = 1;
15743 complaint (&symfile_complaints,
15744 _("invalid maximum_ops_per_instruction "
15745 "in `.debug_line' section"));
15748 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15750 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15752 lh->line_range = read_1_byte (abfd, line_ptr);
15754 lh->opcode_base = read_1_byte (abfd, line_ptr);
15756 lh->standard_opcode_lengths
15757 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15759 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15760 for (i = 1; i < lh->opcode_base; ++i)
15762 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15766 /* Read directory table. */
15767 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15769 line_ptr += bytes_read;
15770 add_include_dir (lh, cur_dir);
15772 line_ptr += bytes_read;
15774 /* Read file name table. */
15775 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15777 unsigned int dir_index, mod_time, length;
15779 line_ptr += bytes_read;
15780 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15781 line_ptr += bytes_read;
15782 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15783 line_ptr += bytes_read;
15784 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15785 line_ptr += bytes_read;
15787 add_file_name (lh, cur_file, dir_index, mod_time, length);
15789 line_ptr += bytes_read;
15790 lh->statement_program_start = line_ptr;
15792 if (line_ptr > (section->buffer + section->size))
15793 complaint (&symfile_complaints,
15794 _("line number info header doesn't "
15795 "fit in `.debug_line' section"));
15797 discard_cleanups (back_to);
15801 /* Subroutine of dwarf_decode_lines to simplify it.
15802 Return the file name of the psymtab for included file FILE_INDEX
15803 in line header LH of PST.
15804 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15805 If space for the result is malloc'd, it will be freed by a cleanup.
15806 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15808 The function creates dangling cleanup registration. */
15810 static const char *
15811 psymtab_include_file_name (const struct line_header *lh, int file_index,
15812 const struct partial_symtab *pst,
15813 const char *comp_dir)
15815 const struct file_entry fe = lh->file_names [file_index];
15816 const char *include_name = fe.name;
15817 const char *include_name_to_compare = include_name;
15818 const char *dir_name = NULL;
15819 const char *pst_filename;
15820 char *copied_name = NULL;
15824 dir_name = lh->include_dirs[fe.dir_index - 1];
15826 if (!IS_ABSOLUTE_PATH (include_name)
15827 && (dir_name != NULL || comp_dir != NULL))
15829 /* Avoid creating a duplicate psymtab for PST.
15830 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15831 Before we do the comparison, however, we need to account
15832 for DIR_NAME and COMP_DIR.
15833 First prepend dir_name (if non-NULL). If we still don't
15834 have an absolute path prepend comp_dir (if non-NULL).
15835 However, the directory we record in the include-file's
15836 psymtab does not contain COMP_DIR (to match the
15837 corresponding symtab(s)).
15842 bash$ gcc -g ./hello.c
15843 include_name = "hello.c"
15845 DW_AT_comp_dir = comp_dir = "/tmp"
15846 DW_AT_name = "./hello.c" */
15848 if (dir_name != NULL)
15850 char *tem = concat (dir_name, SLASH_STRING,
15851 include_name, (char *)NULL);
15853 make_cleanup (xfree, tem);
15854 include_name = tem;
15855 include_name_to_compare = include_name;
15857 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15859 char *tem = concat (comp_dir, SLASH_STRING,
15860 include_name, (char *)NULL);
15862 make_cleanup (xfree, tem);
15863 include_name_to_compare = tem;
15867 pst_filename = pst->filename;
15868 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15870 copied_name = concat (pst->dirname, SLASH_STRING,
15871 pst_filename, (char *)NULL);
15872 pst_filename = copied_name;
15875 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15877 if (copied_name != NULL)
15878 xfree (copied_name);
15882 return include_name;
15885 /* Ignore this record_line request. */
15888 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15893 /* Subroutine of dwarf_decode_lines to simplify it.
15894 Process the line number information in LH. */
15897 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15898 struct dwarf2_cu *cu, struct partial_symtab *pst)
15900 const gdb_byte *line_ptr, *extended_end;
15901 const gdb_byte *line_end;
15902 unsigned int bytes_read, extended_len;
15903 unsigned char op_code, extended_op, adj_opcode;
15904 CORE_ADDR baseaddr;
15905 struct objfile *objfile = cu->objfile;
15906 bfd *abfd = objfile->obfd;
15907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15908 const int decode_for_pst_p = (pst != NULL);
15909 struct subfile *last_subfile = NULL;
15910 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15913 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15915 line_ptr = lh->statement_program_start;
15916 line_end = lh->statement_program_end;
15918 /* Read the statement sequences until there's nothing left. */
15919 while (line_ptr < line_end)
15921 /* state machine registers */
15922 CORE_ADDR address = 0;
15923 unsigned int file = 1;
15924 unsigned int line = 1;
15925 unsigned int column = 0;
15926 int is_stmt = lh->default_is_stmt;
15927 int basic_block = 0;
15928 int end_sequence = 0;
15930 unsigned char op_index = 0;
15932 if (!decode_for_pst_p && lh->num_file_names >= file)
15934 /* Start a subfile for the current file of the state machine. */
15935 /* lh->include_dirs and lh->file_names are 0-based, but the
15936 directory and file name numbers in the statement program
15938 struct file_entry *fe = &lh->file_names[file - 1];
15939 const char *dir = NULL;
15942 dir = lh->include_dirs[fe->dir_index - 1];
15944 dwarf2_start_subfile (fe->name, dir, comp_dir);
15947 /* Decode the table. */
15948 while (!end_sequence)
15950 op_code = read_1_byte (abfd, line_ptr);
15952 if (line_ptr > line_end)
15954 dwarf2_debug_line_missing_end_sequence_complaint ();
15958 if (op_code >= lh->opcode_base)
15960 /* Special operand. */
15961 adj_opcode = op_code - lh->opcode_base;
15962 address += (((op_index + (adj_opcode / lh->line_range))
15963 / lh->maximum_ops_per_instruction)
15964 * lh->minimum_instruction_length);
15965 op_index = ((op_index + (adj_opcode / lh->line_range))
15966 % lh->maximum_ops_per_instruction);
15967 line += lh->line_base + (adj_opcode % lh->line_range);
15968 if (lh->num_file_names < file || file == 0)
15969 dwarf2_debug_line_missing_file_complaint ();
15970 /* For now we ignore lines not starting on an
15971 instruction boundary. */
15972 else if (op_index == 0)
15974 lh->file_names[file - 1].included_p = 1;
15975 if (!decode_for_pst_p && is_stmt)
15977 if (last_subfile != current_subfile)
15979 addr = gdbarch_addr_bits_remove (gdbarch, address);
15981 (*p_record_line) (last_subfile, 0, addr);
15982 last_subfile = current_subfile;
15984 /* Append row to matrix using current values. */
15985 addr = gdbarch_addr_bits_remove (gdbarch, address);
15986 (*p_record_line) (current_subfile, line, addr);
15991 else switch (op_code)
15993 case DW_LNS_extended_op:
15994 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15996 line_ptr += bytes_read;
15997 extended_end = line_ptr + extended_len;
15998 extended_op = read_1_byte (abfd, line_ptr);
16000 switch (extended_op)
16002 case DW_LNE_end_sequence:
16003 p_record_line = record_line;
16006 case DW_LNE_set_address:
16007 address = read_address (abfd, line_ptr, cu, &bytes_read);
16009 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16011 /* This line table is for a function which has been
16012 GCd by the linker. Ignore it. PR gdb/12528 */
16015 = line_ptr - get_debug_line_section (cu)->buffer;
16017 complaint (&symfile_complaints,
16018 _(".debug_line address at offset 0x%lx is 0 "
16020 line_offset, objfile->name);
16021 p_record_line = noop_record_line;
16025 line_ptr += bytes_read;
16026 address += baseaddr;
16028 case DW_LNE_define_file:
16030 const char *cur_file;
16031 unsigned int dir_index, mod_time, length;
16033 cur_file = read_direct_string (abfd, line_ptr,
16035 line_ptr += bytes_read;
16037 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16038 line_ptr += bytes_read;
16040 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16041 line_ptr += bytes_read;
16043 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16044 line_ptr += bytes_read;
16045 add_file_name (lh, cur_file, dir_index, mod_time, length);
16048 case DW_LNE_set_discriminator:
16049 /* The discriminator is not interesting to the debugger;
16051 line_ptr = extended_end;
16054 complaint (&symfile_complaints,
16055 _("mangled .debug_line section"));
16058 /* Make sure that we parsed the extended op correctly. If e.g.
16059 we expected a different address size than the producer used,
16060 we may have read the wrong number of bytes. */
16061 if (line_ptr != extended_end)
16063 complaint (&symfile_complaints,
16064 _("mangled .debug_line section"));
16069 if (lh->num_file_names < file || file == 0)
16070 dwarf2_debug_line_missing_file_complaint ();
16073 lh->file_names[file - 1].included_p = 1;
16074 if (!decode_for_pst_p && is_stmt)
16076 if (last_subfile != current_subfile)
16078 addr = gdbarch_addr_bits_remove (gdbarch, address);
16080 (*p_record_line) (last_subfile, 0, addr);
16081 last_subfile = current_subfile;
16083 addr = gdbarch_addr_bits_remove (gdbarch, address);
16084 (*p_record_line) (current_subfile, line, addr);
16089 case DW_LNS_advance_pc:
16092 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16094 address += (((op_index + adjust)
16095 / lh->maximum_ops_per_instruction)
16096 * lh->minimum_instruction_length);
16097 op_index = ((op_index + adjust)
16098 % lh->maximum_ops_per_instruction);
16099 line_ptr += bytes_read;
16102 case DW_LNS_advance_line:
16103 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16104 line_ptr += bytes_read;
16106 case DW_LNS_set_file:
16108 /* The arrays lh->include_dirs and lh->file_names are
16109 0-based, but the directory and file name numbers in
16110 the statement program are 1-based. */
16111 struct file_entry *fe;
16112 const char *dir = NULL;
16114 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16115 line_ptr += bytes_read;
16116 if (lh->num_file_names < file || file == 0)
16117 dwarf2_debug_line_missing_file_complaint ();
16120 fe = &lh->file_names[file - 1];
16122 dir = lh->include_dirs[fe->dir_index - 1];
16123 if (!decode_for_pst_p)
16125 last_subfile = current_subfile;
16126 dwarf2_start_subfile (fe->name, dir, comp_dir);
16131 case DW_LNS_set_column:
16132 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16133 line_ptr += bytes_read;
16135 case DW_LNS_negate_stmt:
16136 is_stmt = (!is_stmt);
16138 case DW_LNS_set_basic_block:
16141 /* Add to the address register of the state machine the
16142 address increment value corresponding to special opcode
16143 255. I.e., this value is scaled by the minimum
16144 instruction length since special opcode 255 would have
16145 scaled the increment. */
16146 case DW_LNS_const_add_pc:
16148 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16150 address += (((op_index + adjust)
16151 / lh->maximum_ops_per_instruction)
16152 * lh->minimum_instruction_length);
16153 op_index = ((op_index + adjust)
16154 % lh->maximum_ops_per_instruction);
16157 case DW_LNS_fixed_advance_pc:
16158 address += read_2_bytes (abfd, line_ptr);
16164 /* Unknown standard opcode, ignore it. */
16167 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16169 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16170 line_ptr += bytes_read;
16175 if (lh->num_file_names < file || file == 0)
16176 dwarf2_debug_line_missing_file_complaint ();
16179 lh->file_names[file - 1].included_p = 1;
16180 if (!decode_for_pst_p)
16182 addr = gdbarch_addr_bits_remove (gdbarch, address);
16183 (*p_record_line) (current_subfile, 0, addr);
16189 /* Decode the Line Number Program (LNP) for the given line_header
16190 structure and CU. The actual information extracted and the type
16191 of structures created from the LNP depends on the value of PST.
16193 1. If PST is NULL, then this procedure uses the data from the program
16194 to create all necessary symbol tables, and their linetables.
16196 2. If PST is not NULL, this procedure reads the program to determine
16197 the list of files included by the unit represented by PST, and
16198 builds all the associated partial symbol tables.
16200 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16201 It is used for relative paths in the line table.
16202 NOTE: When processing partial symtabs (pst != NULL),
16203 comp_dir == pst->dirname.
16205 NOTE: It is important that psymtabs have the same file name (via strcmp)
16206 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16207 symtab we don't use it in the name of the psymtabs we create.
16208 E.g. expand_line_sal requires this when finding psymtabs to expand.
16209 A good testcase for this is mb-inline.exp. */
16212 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16213 struct dwarf2_cu *cu, struct partial_symtab *pst,
16214 int want_line_info)
16216 struct objfile *objfile = cu->objfile;
16217 const int decode_for_pst_p = (pst != NULL);
16218 struct subfile *first_subfile = current_subfile;
16220 if (want_line_info)
16221 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
16223 if (decode_for_pst_p)
16227 /* Now that we're done scanning the Line Header Program, we can
16228 create the psymtab of each included file. */
16229 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16230 if (lh->file_names[file_index].included_p == 1)
16232 const char *include_name =
16233 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16234 if (include_name != NULL)
16235 dwarf2_create_include_psymtab (include_name, pst, objfile);
16240 /* Make sure a symtab is created for every file, even files
16241 which contain only variables (i.e. no code with associated
16245 for (i = 0; i < lh->num_file_names; i++)
16247 const char *dir = NULL;
16248 struct file_entry *fe;
16250 fe = &lh->file_names[i];
16252 dir = lh->include_dirs[fe->dir_index - 1];
16253 dwarf2_start_subfile (fe->name, dir, comp_dir);
16255 /* Skip the main file; we don't need it, and it must be
16256 allocated last, so that it will show up before the
16257 non-primary symtabs in the objfile's symtab list. */
16258 if (current_subfile == first_subfile)
16261 if (current_subfile->symtab == NULL)
16262 current_subfile->symtab = allocate_symtab (current_subfile->name,
16264 fe->symtab = current_subfile->symtab;
16269 /* Start a subfile for DWARF. FILENAME is the name of the file and
16270 DIRNAME the name of the source directory which contains FILENAME
16271 or NULL if not known. COMP_DIR is the compilation directory for the
16272 linetable's compilation unit or NULL if not known.
16273 This routine tries to keep line numbers from identical absolute and
16274 relative file names in a common subfile.
16276 Using the `list' example from the GDB testsuite, which resides in
16277 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16278 of /srcdir/list0.c yields the following debugging information for list0.c:
16280 DW_AT_name: /srcdir/list0.c
16281 DW_AT_comp_dir: /compdir
16282 files.files[0].name: list0.h
16283 files.files[0].dir: /srcdir
16284 files.files[1].name: list0.c
16285 files.files[1].dir: /srcdir
16287 The line number information for list0.c has to end up in a single
16288 subfile, so that `break /srcdir/list0.c:1' works as expected.
16289 start_subfile will ensure that this happens provided that we pass the
16290 concatenation of files.files[1].dir and files.files[1].name as the
16294 dwarf2_start_subfile (const char *filename, const char *dirname,
16295 const char *comp_dir)
16299 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16300 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16301 second argument to start_subfile. To be consistent, we do the
16302 same here. In order not to lose the line information directory,
16303 we concatenate it to the filename when it makes sense.
16304 Note that the Dwarf3 standard says (speaking of filenames in line
16305 information): ``The directory index is ignored for file names
16306 that represent full path names''. Thus ignoring dirname in the
16307 `else' branch below isn't an issue. */
16309 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
16311 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16315 start_subfile (filename, comp_dir);
16321 /* Start a symtab for DWARF.
16322 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16325 dwarf2_start_symtab (struct dwarf2_cu *cu,
16326 const char *name, const char *comp_dir, CORE_ADDR low_pc)
16328 start_symtab (name, comp_dir, low_pc);
16329 record_debugformat ("DWARF 2");
16330 record_producer (cu->producer);
16332 /* We assume that we're processing GCC output. */
16333 processing_gcc_compilation = 2;
16335 cu->processing_has_namespace_info = 0;
16339 var_decode_location (struct attribute *attr, struct symbol *sym,
16340 struct dwarf2_cu *cu)
16342 struct objfile *objfile = cu->objfile;
16343 struct comp_unit_head *cu_header = &cu->header;
16345 /* NOTE drow/2003-01-30: There used to be a comment and some special
16346 code here to turn a symbol with DW_AT_external and a
16347 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16348 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16349 with some versions of binutils) where shared libraries could have
16350 relocations against symbols in their debug information - the
16351 minimal symbol would have the right address, but the debug info
16352 would not. It's no longer necessary, because we will explicitly
16353 apply relocations when we read in the debug information now. */
16355 /* A DW_AT_location attribute with no contents indicates that a
16356 variable has been optimized away. */
16357 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16359 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16363 /* Handle one degenerate form of location expression specially, to
16364 preserve GDB's previous behavior when section offsets are
16365 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16366 then mark this symbol as LOC_STATIC. */
16368 if (attr_form_is_block (attr)
16369 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16370 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16371 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16372 && (DW_BLOCK (attr)->size
16373 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
16375 unsigned int dummy;
16377 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16378 SYMBOL_VALUE_ADDRESS (sym) =
16379 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16381 SYMBOL_VALUE_ADDRESS (sym) =
16382 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
16383 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
16384 fixup_symbol_section (sym, objfile);
16385 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16386 SYMBOL_SECTION (sym));
16390 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16391 expression evaluator, and use LOC_COMPUTED only when necessary
16392 (i.e. when the value of a register or memory location is
16393 referenced, or a thread-local block, etc.). Then again, it might
16394 not be worthwhile. I'm assuming that it isn't unless performance
16395 or memory numbers show me otherwise. */
16397 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
16399 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
16400 cu->has_loclist = 1;
16403 /* Given a pointer to a DWARF information entry, figure out if we need
16404 to make a symbol table entry for it, and if so, create a new entry
16405 and return a pointer to it.
16406 If TYPE is NULL, determine symbol type from the die, otherwise
16407 used the passed type.
16408 If SPACE is not NULL, use it to hold the new symbol. If it is
16409 NULL, allocate a new symbol on the objfile's obstack. */
16411 static struct symbol *
16412 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16413 struct symbol *space)
16415 struct objfile *objfile = cu->objfile;
16416 struct symbol *sym = NULL;
16418 struct attribute *attr = NULL;
16419 struct attribute *attr2 = NULL;
16420 CORE_ADDR baseaddr;
16421 struct pending **list_to_add = NULL;
16423 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
16425 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16427 name = dwarf2_name (die, cu);
16430 const char *linkagename;
16431 int suppress_add = 0;
16436 sym = allocate_symbol (objfile);
16437 OBJSTAT (objfile, n_syms++);
16439 /* Cache this symbol's name and the name's demangled form (if any). */
16440 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
16441 linkagename = dwarf2_physname (name, die, cu);
16442 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
16444 /* Fortran does not have mangling standard and the mangling does differ
16445 between gfortran, iFort etc. */
16446 if (cu->language == language_fortran
16447 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
16448 symbol_set_demangled_name (&(sym->ginfo),
16449 dwarf2_full_name (name, die, cu),
16452 /* Default assumptions.
16453 Use the passed type or decode it from the die. */
16454 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16455 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16457 SYMBOL_TYPE (sym) = type;
16459 SYMBOL_TYPE (sym) = die_type (die, cu);
16460 attr = dwarf2_attr (die,
16461 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16465 SYMBOL_LINE (sym) = DW_UNSND (attr);
16468 attr = dwarf2_attr (die,
16469 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16473 int file_index = DW_UNSND (attr);
16475 if (cu->line_header == NULL
16476 || file_index > cu->line_header->num_file_names)
16477 complaint (&symfile_complaints,
16478 _("file index out of range"));
16479 else if (file_index > 0)
16481 struct file_entry *fe;
16483 fe = &cu->line_header->file_names[file_index - 1];
16484 SYMBOL_SYMTAB (sym) = fe->symtab;
16491 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16494 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16496 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16497 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16498 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16499 add_symbol_to_list (sym, cu->list_in_scope);
16501 case DW_TAG_subprogram:
16502 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16504 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16505 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16506 if ((attr2 && (DW_UNSND (attr2) != 0))
16507 || cu->language == language_ada)
16509 /* Subprograms marked external are stored as a global symbol.
16510 Ada subprograms, whether marked external or not, are always
16511 stored as a global symbol, because we want to be able to
16512 access them globally. For instance, we want to be able
16513 to break on a nested subprogram without having to
16514 specify the context. */
16515 list_to_add = &global_symbols;
16519 list_to_add = cu->list_in_scope;
16522 case DW_TAG_inlined_subroutine:
16523 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16525 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16526 SYMBOL_INLINED (sym) = 1;
16527 list_to_add = cu->list_in_scope;
16529 case DW_TAG_template_value_param:
16531 /* Fall through. */
16532 case DW_TAG_constant:
16533 case DW_TAG_variable:
16534 case DW_TAG_member:
16535 /* Compilation with minimal debug info may result in
16536 variables with missing type entries. Change the
16537 misleading `void' type to something sensible. */
16538 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16540 = objfile_type (objfile)->nodebug_data_symbol;
16542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16543 /* In the case of DW_TAG_member, we should only be called for
16544 static const members. */
16545 if (die->tag == DW_TAG_member)
16547 /* dwarf2_add_field uses die_is_declaration,
16548 so we do the same. */
16549 gdb_assert (die_is_declaration (die, cu));
16554 dwarf2_const_value (attr, sym, cu);
16555 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16558 if (attr2 && (DW_UNSND (attr2) != 0))
16559 list_to_add = &global_symbols;
16561 list_to_add = cu->list_in_scope;
16565 attr = dwarf2_attr (die, DW_AT_location, cu);
16568 var_decode_location (attr, sym, cu);
16569 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16571 /* Fortran explicitly imports any global symbols to the local
16572 scope by DW_TAG_common_block. */
16573 if (cu->language == language_fortran && die->parent
16574 && die->parent->tag == DW_TAG_common_block)
16577 if (SYMBOL_CLASS (sym) == LOC_STATIC
16578 && SYMBOL_VALUE_ADDRESS (sym) == 0
16579 && !dwarf2_per_objfile->has_section_at_zero)
16581 /* When a static variable is eliminated by the linker,
16582 the corresponding debug information is not stripped
16583 out, but the variable address is set to null;
16584 do not add such variables into symbol table. */
16586 else if (attr2 && (DW_UNSND (attr2) != 0))
16588 /* Workaround gfortran PR debug/40040 - it uses
16589 DW_AT_location for variables in -fPIC libraries which may
16590 get overriden by other libraries/executable and get
16591 a different address. Resolve it by the minimal symbol
16592 which may come from inferior's executable using copy
16593 relocation. Make this workaround only for gfortran as for
16594 other compilers GDB cannot guess the minimal symbol
16595 Fortran mangling kind. */
16596 if (cu->language == language_fortran && die->parent
16597 && die->parent->tag == DW_TAG_module
16599 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16600 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16602 /* A variable with DW_AT_external is never static,
16603 but it may be block-scoped. */
16604 list_to_add = (cu->list_in_scope == &file_symbols
16605 ? &global_symbols : cu->list_in_scope);
16608 list_to_add = cu->list_in_scope;
16612 /* We do not know the address of this symbol.
16613 If it is an external symbol and we have type information
16614 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16615 The address of the variable will then be determined from
16616 the minimal symbol table whenever the variable is
16618 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16620 /* Fortran explicitly imports any global symbols to the local
16621 scope by DW_TAG_common_block. */
16622 if (cu->language == language_fortran && die->parent
16623 && die->parent->tag == DW_TAG_common_block)
16625 /* SYMBOL_CLASS doesn't matter here because
16626 read_common_block is going to reset it. */
16628 list_to_add = cu->list_in_scope;
16630 else if (attr2 && (DW_UNSND (attr2) != 0)
16631 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16633 /* A variable with DW_AT_external is never static, but it
16634 may be block-scoped. */
16635 list_to_add = (cu->list_in_scope == &file_symbols
16636 ? &global_symbols : cu->list_in_scope);
16638 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16640 else if (!die_is_declaration (die, cu))
16642 /* Use the default LOC_OPTIMIZED_OUT class. */
16643 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16645 list_to_add = cu->list_in_scope;
16649 case DW_TAG_formal_parameter:
16650 /* If we are inside a function, mark this as an argument. If
16651 not, we might be looking at an argument to an inlined function
16652 when we do not have enough information to show inlined frames;
16653 pretend it's a local variable in that case so that the user can
16655 if (context_stack_depth > 0
16656 && context_stack[context_stack_depth - 1].name != NULL)
16657 SYMBOL_IS_ARGUMENT (sym) = 1;
16658 attr = dwarf2_attr (die, DW_AT_location, cu);
16661 var_decode_location (attr, sym, cu);
16663 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16666 dwarf2_const_value (attr, sym, cu);
16669 list_to_add = cu->list_in_scope;
16671 case DW_TAG_unspecified_parameters:
16672 /* From varargs functions; gdb doesn't seem to have any
16673 interest in this information, so just ignore it for now.
16676 case DW_TAG_template_type_param:
16678 /* Fall through. */
16679 case DW_TAG_class_type:
16680 case DW_TAG_interface_type:
16681 case DW_TAG_structure_type:
16682 case DW_TAG_union_type:
16683 case DW_TAG_set_type:
16684 case DW_TAG_enumeration_type:
16685 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16686 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16689 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16690 really ever be static objects: otherwise, if you try
16691 to, say, break of a class's method and you're in a file
16692 which doesn't mention that class, it won't work unless
16693 the check for all static symbols in lookup_symbol_aux
16694 saves you. See the OtherFileClass tests in
16695 gdb.c++/namespace.exp. */
16699 list_to_add = (cu->list_in_scope == &file_symbols
16700 && (cu->language == language_cplus
16701 || cu->language == language_java)
16702 ? &global_symbols : cu->list_in_scope);
16704 /* The semantics of C++ state that "struct foo {
16705 ... }" also defines a typedef for "foo". A Java
16706 class declaration also defines a typedef for the
16708 if (cu->language == language_cplus
16709 || cu->language == language_java
16710 || cu->language == language_ada)
16712 /* The symbol's name is already allocated along
16713 with this objfile, so we don't need to
16714 duplicate it for the type. */
16715 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16716 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16721 case DW_TAG_typedef:
16722 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16723 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16724 list_to_add = cu->list_in_scope;
16726 case DW_TAG_base_type:
16727 case DW_TAG_subrange_type:
16728 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16729 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16730 list_to_add = cu->list_in_scope;
16732 case DW_TAG_enumerator:
16733 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16736 dwarf2_const_value (attr, sym, cu);
16739 /* NOTE: carlton/2003-11-10: See comment above in the
16740 DW_TAG_class_type, etc. block. */
16742 list_to_add = (cu->list_in_scope == &file_symbols
16743 && (cu->language == language_cplus
16744 || cu->language == language_java)
16745 ? &global_symbols : cu->list_in_scope);
16748 case DW_TAG_namespace:
16749 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16750 list_to_add = &global_symbols;
16752 case DW_TAG_common_block:
16753 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16754 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16755 add_symbol_to_list (sym, cu->list_in_scope);
16758 /* Not a tag we recognize. Hopefully we aren't processing
16759 trash data, but since we must specifically ignore things
16760 we don't recognize, there is nothing else we should do at
16762 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16763 dwarf_tag_name (die->tag));
16769 sym->hash_next = objfile->template_symbols;
16770 objfile->template_symbols = sym;
16771 list_to_add = NULL;
16774 if (list_to_add != NULL)
16775 add_symbol_to_list (sym, list_to_add);
16777 /* For the benefit of old versions of GCC, check for anonymous
16778 namespaces based on the demangled name. */
16779 if (!cu->processing_has_namespace_info
16780 && cu->language == language_cplus)
16781 cp_scan_for_anonymous_namespaces (sym, objfile);
16786 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16788 static struct symbol *
16789 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16791 return new_symbol_full (die, type, cu, NULL);
16794 /* Given an attr with a DW_FORM_dataN value in host byte order,
16795 zero-extend it as appropriate for the symbol's type. The DWARF
16796 standard (v4) is not entirely clear about the meaning of using
16797 DW_FORM_dataN for a constant with a signed type, where the type is
16798 wider than the data. The conclusion of a discussion on the DWARF
16799 list was that this is unspecified. We choose to always zero-extend
16800 because that is the interpretation long in use by GCC. */
16803 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
16804 struct dwarf2_cu *cu, LONGEST *value, int bits)
16806 struct objfile *objfile = cu->objfile;
16807 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16808 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16809 LONGEST l = DW_UNSND (attr);
16811 if (bits < sizeof (*value) * 8)
16813 l &= ((LONGEST) 1 << bits) - 1;
16816 else if (bits == sizeof (*value) * 8)
16820 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16821 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16828 /* Read a constant value from an attribute. Either set *VALUE, or if
16829 the value does not fit in *VALUE, set *BYTES - either already
16830 allocated on the objfile obstack, or newly allocated on OBSTACK,
16831 or, set *BATON, if we translated the constant to a location
16835 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
16836 const char *name, struct obstack *obstack,
16837 struct dwarf2_cu *cu,
16838 LONGEST *value, const gdb_byte **bytes,
16839 struct dwarf2_locexpr_baton **baton)
16841 struct objfile *objfile = cu->objfile;
16842 struct comp_unit_head *cu_header = &cu->header;
16843 struct dwarf_block *blk;
16844 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16845 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16851 switch (attr->form)
16854 case DW_FORM_GNU_addr_index:
16858 if (TYPE_LENGTH (type) != cu_header->addr_size)
16859 dwarf2_const_value_length_mismatch_complaint (name,
16860 cu_header->addr_size,
16861 TYPE_LENGTH (type));
16862 /* Symbols of this form are reasonably rare, so we just
16863 piggyback on the existing location code rather than writing
16864 a new implementation of symbol_computed_ops. */
16865 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
16866 (*baton)->per_cu = cu->per_cu;
16867 gdb_assert ((*baton)->per_cu);
16869 (*baton)->size = 2 + cu_header->addr_size;
16870 data = obstack_alloc (obstack, (*baton)->size);
16871 (*baton)->data = data;
16873 data[0] = DW_OP_addr;
16874 store_unsigned_integer (&data[1], cu_header->addr_size,
16875 byte_order, DW_ADDR (attr));
16876 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16879 case DW_FORM_string:
16881 case DW_FORM_GNU_str_index:
16882 case DW_FORM_GNU_strp_alt:
16883 /* DW_STRING is already allocated on the objfile obstack, point
16885 *bytes = (const gdb_byte *) DW_STRING (attr);
16887 case DW_FORM_block1:
16888 case DW_FORM_block2:
16889 case DW_FORM_block4:
16890 case DW_FORM_block:
16891 case DW_FORM_exprloc:
16892 blk = DW_BLOCK (attr);
16893 if (TYPE_LENGTH (type) != blk->size)
16894 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16895 TYPE_LENGTH (type));
16896 *bytes = blk->data;
16899 /* The DW_AT_const_value attributes are supposed to carry the
16900 symbol's value "represented as it would be on the target
16901 architecture." By the time we get here, it's already been
16902 converted to host endianness, so we just need to sign- or
16903 zero-extend it as appropriate. */
16904 case DW_FORM_data1:
16905 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
16907 case DW_FORM_data2:
16908 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
16910 case DW_FORM_data4:
16911 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
16913 case DW_FORM_data8:
16914 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
16917 case DW_FORM_sdata:
16918 *value = DW_SND (attr);
16921 case DW_FORM_udata:
16922 *value = DW_UNSND (attr);
16926 complaint (&symfile_complaints,
16927 _("unsupported const value attribute form: '%s'"),
16928 dwarf_form_name (attr->form));
16935 /* Copy constant value from an attribute to a symbol. */
16938 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
16939 struct dwarf2_cu *cu)
16941 struct objfile *objfile = cu->objfile;
16942 struct comp_unit_head *cu_header = &cu->header;
16944 const gdb_byte *bytes;
16945 struct dwarf2_locexpr_baton *baton;
16947 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16948 SYMBOL_PRINT_NAME (sym),
16949 &objfile->objfile_obstack, cu,
16950 &value, &bytes, &baton);
16954 SYMBOL_LOCATION_BATON (sym) = baton;
16955 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16957 else if (bytes != NULL)
16959 SYMBOL_VALUE_BYTES (sym) = bytes;
16960 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16964 SYMBOL_VALUE (sym) = value;
16965 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16969 /* Return the type of the die in question using its DW_AT_type attribute. */
16971 static struct type *
16972 die_type (struct die_info *die, struct dwarf2_cu *cu)
16974 struct attribute *type_attr;
16976 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16979 /* A missing DW_AT_type represents a void type. */
16980 return objfile_type (cu->objfile)->builtin_void;
16983 return lookup_die_type (die, type_attr, cu);
16986 /* True iff CU's producer generates GNAT Ada auxiliary information
16987 that allows to find parallel types through that information instead
16988 of having to do expensive parallel lookups by type name. */
16991 need_gnat_info (struct dwarf2_cu *cu)
16993 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16994 of GNAT produces this auxiliary information, without any indication
16995 that it is produced. Part of enhancing the FSF version of GNAT
16996 to produce that information will be to put in place an indicator
16997 that we can use in order to determine whether the descriptive type
16998 info is available or not. One suggestion that has been made is
16999 to use a new attribute, attached to the CU die. For now, assume
17000 that the descriptive type info is not available. */
17004 /* Return the auxiliary type of the die in question using its
17005 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17006 attribute is not present. */
17008 static struct type *
17009 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17011 struct attribute *type_attr;
17013 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17017 return lookup_die_type (die, type_attr, cu);
17020 /* If DIE has a descriptive_type attribute, then set the TYPE's
17021 descriptive type accordingly. */
17024 set_descriptive_type (struct type *type, struct die_info *die,
17025 struct dwarf2_cu *cu)
17027 struct type *descriptive_type = die_descriptive_type (die, cu);
17029 if (descriptive_type)
17031 ALLOCATE_GNAT_AUX_TYPE (type);
17032 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17036 /* Return the containing type of the die in question using its
17037 DW_AT_containing_type attribute. */
17039 static struct type *
17040 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
17042 struct attribute *type_attr;
17044 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
17046 error (_("Dwarf Error: Problem turning containing type into gdb type "
17047 "[in module %s]"), cu->objfile->name);
17049 return lookup_die_type (die, type_attr, cu);
17052 /* Return an error marker type to use for the ill formed type in DIE/CU. */
17054 static struct type *
17055 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17057 struct objfile *objfile = dwarf2_per_objfile->objfile;
17058 char *message, *saved;
17060 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17062 cu->header.offset.sect_off,
17063 die->offset.sect_off);
17064 saved = obstack_copy0 (&objfile->objfile_obstack,
17065 message, strlen (message));
17068 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17071 /* Look up the type of DIE in CU using its type attribute ATTR.
17072 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17073 DW_AT_containing_type.
17074 If there is no type substitute an error marker. */
17076 static struct type *
17077 lookup_die_type (struct die_info *die, const struct attribute *attr,
17078 struct dwarf2_cu *cu)
17080 struct objfile *objfile = cu->objfile;
17081 struct type *this_type;
17083 gdb_assert (attr->name == DW_AT_type
17084 || attr->name == DW_AT_GNAT_descriptive_type
17085 || attr->name == DW_AT_containing_type);
17087 /* First see if we have it cached. */
17089 if (attr->form == DW_FORM_GNU_ref_alt)
17091 struct dwarf2_per_cu_data *per_cu;
17092 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17094 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17095 this_type = get_die_type_at_offset (offset, per_cu);
17097 else if (attr_form_is_ref (attr))
17099 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17101 this_type = get_die_type_at_offset (offset, cu->per_cu);
17103 else if (attr->form == DW_FORM_ref_sig8)
17105 ULONGEST signature = DW_SIGNATURE (attr);
17107 return get_signatured_type (die, signature, cu);
17111 complaint (&symfile_complaints,
17112 _("Dwarf Error: Bad type attribute %s in DIE"
17113 " at 0x%x [in module %s]"),
17114 dwarf_attr_name (attr->name), die->offset.sect_off,
17116 return build_error_marker_type (cu, die);
17119 /* If not cached we need to read it in. */
17121 if (this_type == NULL)
17123 struct die_info *type_die = NULL;
17124 struct dwarf2_cu *type_cu = cu;
17126 if (attr_form_is_ref (attr))
17127 type_die = follow_die_ref (die, attr, &type_cu);
17128 if (type_die == NULL)
17129 return build_error_marker_type (cu, die);
17130 /* If we find the type now, it's probably because the type came
17131 from an inter-CU reference and the type's CU got expanded before
17133 this_type = read_type_die (type_die, type_cu);
17136 /* If we still don't have a type use an error marker. */
17138 if (this_type == NULL)
17139 return build_error_marker_type (cu, die);
17144 /* Return the type in DIE, CU.
17145 Returns NULL for invalid types.
17147 This first does a lookup in die_type_hash,
17148 and only reads the die in if necessary.
17150 NOTE: This can be called when reading in partial or full symbols. */
17152 static struct type *
17153 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17155 struct type *this_type;
17157 this_type = get_die_type (die, cu);
17161 return read_type_die_1 (die, cu);
17164 /* Read the type in DIE, CU.
17165 Returns NULL for invalid types. */
17167 static struct type *
17168 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17170 struct type *this_type = NULL;
17174 case DW_TAG_class_type:
17175 case DW_TAG_interface_type:
17176 case DW_TAG_structure_type:
17177 case DW_TAG_union_type:
17178 this_type = read_structure_type (die, cu);
17180 case DW_TAG_enumeration_type:
17181 this_type = read_enumeration_type (die, cu);
17183 case DW_TAG_subprogram:
17184 case DW_TAG_subroutine_type:
17185 case DW_TAG_inlined_subroutine:
17186 this_type = read_subroutine_type (die, cu);
17188 case DW_TAG_array_type:
17189 this_type = read_array_type (die, cu);
17191 case DW_TAG_set_type:
17192 this_type = read_set_type (die, cu);
17194 case DW_TAG_pointer_type:
17195 this_type = read_tag_pointer_type (die, cu);
17197 case DW_TAG_ptr_to_member_type:
17198 this_type = read_tag_ptr_to_member_type (die, cu);
17200 case DW_TAG_reference_type:
17201 this_type = read_tag_reference_type (die, cu);
17203 case DW_TAG_const_type:
17204 this_type = read_tag_const_type (die, cu);
17206 case DW_TAG_volatile_type:
17207 this_type = read_tag_volatile_type (die, cu);
17209 case DW_TAG_restrict_type:
17210 this_type = read_tag_restrict_type (die, cu);
17212 case DW_TAG_string_type:
17213 this_type = read_tag_string_type (die, cu);
17215 case DW_TAG_typedef:
17216 this_type = read_typedef (die, cu);
17218 case DW_TAG_subrange_type:
17219 this_type = read_subrange_type (die, cu);
17221 case DW_TAG_base_type:
17222 this_type = read_base_type (die, cu);
17224 case DW_TAG_unspecified_type:
17225 this_type = read_unspecified_type (die, cu);
17227 case DW_TAG_namespace:
17228 this_type = read_namespace_type (die, cu);
17230 case DW_TAG_module:
17231 this_type = read_module_type (die, cu);
17234 complaint (&symfile_complaints,
17235 _("unexpected tag in read_type_die: '%s'"),
17236 dwarf_tag_name (die->tag));
17243 /* See if we can figure out if the class lives in a namespace. We do
17244 this by looking for a member function; its demangled name will
17245 contain namespace info, if there is any.
17246 Return the computed name or NULL.
17247 Space for the result is allocated on the objfile's obstack.
17248 This is the full-die version of guess_partial_die_structure_name.
17249 In this case we know DIE has no useful parent. */
17252 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17254 struct die_info *spec_die;
17255 struct dwarf2_cu *spec_cu;
17256 struct die_info *child;
17259 spec_die = die_specification (die, &spec_cu);
17260 if (spec_die != NULL)
17266 for (child = die->child;
17268 child = child->sibling)
17270 if (child->tag == DW_TAG_subprogram)
17272 struct attribute *attr;
17274 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17276 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17280 = language_class_name_from_physname (cu->language_defn,
17284 if (actual_name != NULL)
17286 const char *die_name = dwarf2_name (die, cu);
17288 if (die_name != NULL
17289 && strcmp (die_name, actual_name) != 0)
17291 /* Strip off the class name from the full name.
17292 We want the prefix. */
17293 int die_name_len = strlen (die_name);
17294 int actual_name_len = strlen (actual_name);
17296 /* Test for '::' as a sanity check. */
17297 if (actual_name_len > die_name_len + 2
17298 && actual_name[actual_name_len
17299 - die_name_len - 1] == ':')
17301 obstack_copy0 (&cu->objfile->objfile_obstack,
17303 actual_name_len - die_name_len - 2);
17306 xfree (actual_name);
17315 /* GCC might emit a nameless typedef that has a linkage name. Determine the
17316 prefix part in such case. See
17317 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17320 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17322 struct attribute *attr;
17325 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17326 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17329 attr = dwarf2_attr (die, DW_AT_name, cu);
17330 if (attr != NULL && DW_STRING (attr) != NULL)
17333 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17335 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17336 if (attr == NULL || DW_STRING (attr) == NULL)
17339 /* dwarf2_name had to be already called. */
17340 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17342 /* Strip the base name, keep any leading namespaces/classes. */
17343 base = strrchr (DW_STRING (attr), ':');
17344 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17347 return obstack_copy0 (&cu->objfile->objfile_obstack,
17348 DW_STRING (attr), &base[-1] - DW_STRING (attr));
17351 /* Return the name of the namespace/class that DIE is defined within,
17352 or "" if we can't tell. The caller should not xfree the result.
17354 For example, if we're within the method foo() in the following
17364 then determine_prefix on foo's die will return "N::C". */
17366 static const char *
17367 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
17369 struct die_info *parent, *spec_die;
17370 struct dwarf2_cu *spec_cu;
17371 struct type *parent_type;
17374 if (cu->language != language_cplus && cu->language != language_java
17375 && cu->language != language_fortran)
17378 retval = anonymous_struct_prefix (die, cu);
17382 /* We have to be careful in the presence of DW_AT_specification.
17383 For example, with GCC 3.4, given the code
17387 // Definition of N::foo.
17391 then we'll have a tree of DIEs like this:
17393 1: DW_TAG_compile_unit
17394 2: DW_TAG_namespace // N
17395 3: DW_TAG_subprogram // declaration of N::foo
17396 4: DW_TAG_subprogram // definition of N::foo
17397 DW_AT_specification // refers to die #3
17399 Thus, when processing die #4, we have to pretend that we're in
17400 the context of its DW_AT_specification, namely the contex of die
17403 spec_die = die_specification (die, &spec_cu);
17404 if (spec_die == NULL)
17405 parent = die->parent;
17408 parent = spec_die->parent;
17412 if (parent == NULL)
17414 else if (parent->building_fullname)
17417 const char *parent_name;
17419 /* It has been seen on RealView 2.2 built binaries,
17420 DW_TAG_template_type_param types actually _defined_ as
17421 children of the parent class:
17424 template class <class Enum> Class{};
17425 Class<enum E> class_e;
17427 1: DW_TAG_class_type (Class)
17428 2: DW_TAG_enumeration_type (E)
17429 3: DW_TAG_enumerator (enum1:0)
17430 3: DW_TAG_enumerator (enum2:1)
17432 2: DW_TAG_template_type_param
17433 DW_AT_type DW_FORM_ref_udata (E)
17435 Besides being broken debug info, it can put GDB into an
17436 infinite loop. Consider:
17438 When we're building the full name for Class<E>, we'll start
17439 at Class, and go look over its template type parameters,
17440 finding E. We'll then try to build the full name of E, and
17441 reach here. We're now trying to build the full name of E,
17442 and look over the parent DIE for containing scope. In the
17443 broken case, if we followed the parent DIE of E, we'd again
17444 find Class, and once again go look at its template type
17445 arguments, etc., etc. Simply don't consider such parent die
17446 as source-level parent of this die (it can't be, the language
17447 doesn't allow it), and break the loop here. */
17448 name = dwarf2_name (die, cu);
17449 parent_name = dwarf2_name (parent, cu);
17450 complaint (&symfile_complaints,
17451 _("template param type '%s' defined within parent '%s'"),
17452 name ? name : "<unknown>",
17453 parent_name ? parent_name : "<unknown>");
17457 switch (parent->tag)
17459 case DW_TAG_namespace:
17460 parent_type = read_type_die (parent, cu);
17461 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17462 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17463 Work around this problem here. */
17464 if (cu->language == language_cplus
17465 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17467 /* We give a name to even anonymous namespaces. */
17468 return TYPE_TAG_NAME (parent_type);
17469 case DW_TAG_class_type:
17470 case DW_TAG_interface_type:
17471 case DW_TAG_structure_type:
17472 case DW_TAG_union_type:
17473 case DW_TAG_module:
17474 parent_type = read_type_die (parent, cu);
17475 if (TYPE_TAG_NAME (parent_type) != NULL)
17476 return TYPE_TAG_NAME (parent_type);
17478 /* An anonymous structure is only allowed non-static data
17479 members; no typedefs, no member functions, et cetera.
17480 So it does not need a prefix. */
17482 case DW_TAG_compile_unit:
17483 case DW_TAG_partial_unit:
17484 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17485 if (cu->language == language_cplus
17486 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17487 && die->child != NULL
17488 && (die->tag == DW_TAG_class_type
17489 || die->tag == DW_TAG_structure_type
17490 || die->tag == DW_TAG_union_type))
17492 char *name = guess_full_die_structure_name (die, cu);
17498 return determine_prefix (parent, cu);
17502 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17503 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17504 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17505 an obconcat, otherwise allocate storage for the result. The CU argument is
17506 used to determine the language and hence, the appropriate separator. */
17508 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17511 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17512 int physname, struct dwarf2_cu *cu)
17514 const char *lead = "";
17517 if (suffix == NULL || suffix[0] == '\0'
17518 || prefix == NULL || prefix[0] == '\0')
17520 else if (cu->language == language_java)
17522 else if (cu->language == language_fortran && physname)
17524 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17525 DW_AT_MIPS_linkage_name is preferred and used instead. */
17533 if (prefix == NULL)
17535 if (suffix == NULL)
17541 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17543 strcpy (retval, lead);
17544 strcat (retval, prefix);
17545 strcat (retval, sep);
17546 strcat (retval, suffix);
17551 /* We have an obstack. */
17552 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17556 /* Return sibling of die, NULL if no sibling. */
17558 static struct die_info *
17559 sibling_die (struct die_info *die)
17561 return die->sibling;
17564 /* Get name of a die, return NULL if not found. */
17566 static const char *
17567 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17568 struct obstack *obstack)
17570 if (name && cu->language == language_cplus)
17572 char *canon_name = cp_canonicalize_string (name);
17574 if (canon_name != NULL)
17576 if (strcmp (canon_name, name) != 0)
17577 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17578 xfree (canon_name);
17585 /* Get name of a die, return NULL if not found. */
17587 static const char *
17588 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17590 struct attribute *attr;
17592 attr = dwarf2_attr (die, DW_AT_name, cu);
17593 if ((!attr || !DW_STRING (attr))
17594 && die->tag != DW_TAG_class_type
17595 && die->tag != DW_TAG_interface_type
17596 && die->tag != DW_TAG_structure_type
17597 && die->tag != DW_TAG_union_type)
17602 case DW_TAG_compile_unit:
17603 case DW_TAG_partial_unit:
17604 /* Compilation units have a DW_AT_name that is a filename, not
17605 a source language identifier. */
17606 case DW_TAG_enumeration_type:
17607 case DW_TAG_enumerator:
17608 /* These tags always have simple identifiers already; no need
17609 to canonicalize them. */
17610 return DW_STRING (attr);
17612 case DW_TAG_subprogram:
17613 /* Java constructors will all be named "<init>", so return
17614 the class name when we see this special case. */
17615 if (cu->language == language_java
17616 && DW_STRING (attr) != NULL
17617 && strcmp (DW_STRING (attr), "<init>") == 0)
17619 struct dwarf2_cu *spec_cu = cu;
17620 struct die_info *spec_die;
17622 /* GCJ will output '<init>' for Java constructor names.
17623 For this special case, return the name of the parent class. */
17625 /* GCJ may output suprogram DIEs with AT_specification set.
17626 If so, use the name of the specified DIE. */
17627 spec_die = die_specification (die, &spec_cu);
17628 if (spec_die != NULL)
17629 return dwarf2_name (spec_die, spec_cu);
17634 if (die->tag == DW_TAG_class_type)
17635 return dwarf2_name (die, cu);
17637 while (die->tag != DW_TAG_compile_unit
17638 && die->tag != DW_TAG_partial_unit);
17642 case DW_TAG_class_type:
17643 case DW_TAG_interface_type:
17644 case DW_TAG_structure_type:
17645 case DW_TAG_union_type:
17646 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17647 structures or unions. These were of the form "._%d" in GCC 4.1,
17648 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17649 and GCC 4.4. We work around this problem by ignoring these. */
17650 if (attr && DW_STRING (attr)
17651 && (strncmp (DW_STRING (attr), "._", 2) == 0
17652 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17655 /* GCC might emit a nameless typedef that has a linkage name. See
17656 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17657 if (!attr || DW_STRING (attr) == NULL)
17659 char *demangled = NULL;
17661 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17663 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17665 if (attr == NULL || DW_STRING (attr) == NULL)
17668 /* Avoid demangling DW_STRING (attr) the second time on a second
17669 call for the same DIE. */
17670 if (!DW_STRING_IS_CANONICAL (attr))
17671 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
17677 /* FIXME: we already did this for the partial symbol... */
17678 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17679 demangled, strlen (demangled));
17680 DW_STRING_IS_CANONICAL (attr) = 1;
17683 /* Strip any leading namespaces/classes, keep only the base name.
17684 DW_AT_name for named DIEs does not contain the prefixes. */
17685 base = strrchr (DW_STRING (attr), ':');
17686 if (base && base > DW_STRING (attr) && base[-1] == ':')
17689 return DW_STRING (attr);
17698 if (!DW_STRING_IS_CANONICAL (attr))
17701 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17702 &cu->objfile->objfile_obstack);
17703 DW_STRING_IS_CANONICAL (attr) = 1;
17705 return DW_STRING (attr);
17708 /* Return the die that this die in an extension of, or NULL if there
17709 is none. *EXT_CU is the CU containing DIE on input, and the CU
17710 containing the return value on output. */
17712 static struct die_info *
17713 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17715 struct attribute *attr;
17717 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17721 return follow_die_ref (die, attr, ext_cu);
17724 /* Convert a DIE tag into its string name. */
17726 static const char *
17727 dwarf_tag_name (unsigned tag)
17729 const char *name = get_DW_TAG_name (tag);
17732 return "DW_TAG_<unknown>";
17737 /* Convert a DWARF attribute code into its string name. */
17739 static const char *
17740 dwarf_attr_name (unsigned attr)
17744 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17745 if (attr == DW_AT_MIPS_fde)
17746 return "DW_AT_MIPS_fde";
17748 if (attr == DW_AT_HP_block_index)
17749 return "DW_AT_HP_block_index";
17752 name = get_DW_AT_name (attr);
17755 return "DW_AT_<unknown>";
17760 /* Convert a DWARF value form code into its string name. */
17762 static const char *
17763 dwarf_form_name (unsigned form)
17765 const char *name = get_DW_FORM_name (form);
17768 return "DW_FORM_<unknown>";
17774 dwarf_bool_name (unsigned mybool)
17782 /* Convert a DWARF type code into its string name. */
17784 static const char *
17785 dwarf_type_encoding_name (unsigned enc)
17787 const char *name = get_DW_ATE_name (enc);
17790 return "DW_ATE_<unknown>";
17796 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17800 print_spaces (indent, f);
17801 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17802 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17804 if (die->parent != NULL)
17806 print_spaces (indent, f);
17807 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17808 die->parent->offset.sect_off);
17811 print_spaces (indent, f);
17812 fprintf_unfiltered (f, " has children: %s\n",
17813 dwarf_bool_name (die->child != NULL));
17815 print_spaces (indent, f);
17816 fprintf_unfiltered (f, " attributes:\n");
17818 for (i = 0; i < die->num_attrs; ++i)
17820 print_spaces (indent, f);
17821 fprintf_unfiltered (f, " %s (%s) ",
17822 dwarf_attr_name (die->attrs[i].name),
17823 dwarf_form_name (die->attrs[i].form));
17825 switch (die->attrs[i].form)
17828 case DW_FORM_GNU_addr_index:
17829 fprintf_unfiltered (f, "address: ");
17830 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17832 case DW_FORM_block2:
17833 case DW_FORM_block4:
17834 case DW_FORM_block:
17835 case DW_FORM_block1:
17836 fprintf_unfiltered (f, "block: size %s",
17837 pulongest (DW_BLOCK (&die->attrs[i])->size));
17839 case DW_FORM_exprloc:
17840 fprintf_unfiltered (f, "expression: size %s",
17841 pulongest (DW_BLOCK (&die->attrs[i])->size));
17843 case DW_FORM_ref_addr:
17844 fprintf_unfiltered (f, "ref address: ");
17845 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17847 case DW_FORM_GNU_ref_alt:
17848 fprintf_unfiltered (f, "alt ref address: ");
17849 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17855 case DW_FORM_ref_udata:
17856 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17857 (long) (DW_UNSND (&die->attrs[i])));
17859 case DW_FORM_data1:
17860 case DW_FORM_data2:
17861 case DW_FORM_data4:
17862 case DW_FORM_data8:
17863 case DW_FORM_udata:
17864 case DW_FORM_sdata:
17865 fprintf_unfiltered (f, "constant: %s",
17866 pulongest (DW_UNSND (&die->attrs[i])));
17868 case DW_FORM_sec_offset:
17869 fprintf_unfiltered (f, "section offset: %s",
17870 pulongest (DW_UNSND (&die->attrs[i])));
17872 case DW_FORM_ref_sig8:
17873 fprintf_unfiltered (f, "signature: %s",
17874 hex_string (DW_SIGNATURE (&die->attrs[i])));
17876 case DW_FORM_string:
17878 case DW_FORM_GNU_str_index:
17879 case DW_FORM_GNU_strp_alt:
17880 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17881 DW_STRING (&die->attrs[i])
17882 ? DW_STRING (&die->attrs[i]) : "",
17883 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17886 if (DW_UNSND (&die->attrs[i]))
17887 fprintf_unfiltered (f, "flag: TRUE");
17889 fprintf_unfiltered (f, "flag: FALSE");
17891 case DW_FORM_flag_present:
17892 fprintf_unfiltered (f, "flag: TRUE");
17894 case DW_FORM_indirect:
17895 /* The reader will have reduced the indirect form to
17896 the "base form" so this form should not occur. */
17897 fprintf_unfiltered (f,
17898 "unexpected attribute form: DW_FORM_indirect");
17901 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17902 die->attrs[i].form);
17905 fprintf_unfiltered (f, "\n");
17910 dump_die_for_error (struct die_info *die)
17912 dump_die_shallow (gdb_stderr, 0, die);
17916 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17918 int indent = level * 4;
17920 gdb_assert (die != NULL);
17922 if (level >= max_level)
17925 dump_die_shallow (f, indent, die);
17927 if (die->child != NULL)
17929 print_spaces (indent, f);
17930 fprintf_unfiltered (f, " Children:");
17931 if (level + 1 < max_level)
17933 fprintf_unfiltered (f, "\n");
17934 dump_die_1 (f, level + 1, max_level, die->child);
17938 fprintf_unfiltered (f,
17939 " [not printed, max nesting level reached]\n");
17943 if (die->sibling != NULL && level > 0)
17945 dump_die_1 (f, level, max_level, die->sibling);
17949 /* This is called from the pdie macro in gdbinit.in.
17950 It's not static so gcc will keep a copy callable from gdb. */
17953 dump_die (struct die_info *die, int max_level)
17955 dump_die_1 (gdb_stdlog, 0, max_level, die);
17959 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17963 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17969 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17973 dwarf2_get_ref_die_offset (const struct attribute *attr)
17975 sect_offset retval = { DW_UNSND (attr) };
17977 if (attr_form_is_ref (attr))
17980 retval.sect_off = 0;
17981 complaint (&symfile_complaints,
17982 _("unsupported die ref attribute form: '%s'"),
17983 dwarf_form_name (attr->form));
17987 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17988 * the value held by the attribute is not constant. */
17991 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
17993 if (attr->form == DW_FORM_sdata)
17994 return DW_SND (attr);
17995 else if (attr->form == DW_FORM_udata
17996 || attr->form == DW_FORM_data1
17997 || attr->form == DW_FORM_data2
17998 || attr->form == DW_FORM_data4
17999 || attr->form == DW_FORM_data8)
18000 return DW_UNSND (attr);
18003 complaint (&symfile_complaints,
18004 _("Attribute value is not a constant (%s)"),
18005 dwarf_form_name (attr->form));
18006 return default_value;
18010 /* Follow reference or signature attribute ATTR of SRC_DIE.
18011 On entry *REF_CU is the CU of SRC_DIE.
18012 On exit *REF_CU is the CU of the result. */
18014 static struct die_info *
18015 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
18016 struct dwarf2_cu **ref_cu)
18018 struct die_info *die;
18020 if (attr_form_is_ref (attr))
18021 die = follow_die_ref (src_die, attr, ref_cu);
18022 else if (attr->form == DW_FORM_ref_sig8)
18023 die = follow_die_sig (src_die, attr, ref_cu);
18026 dump_die_for_error (src_die);
18027 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18028 (*ref_cu)->objfile->name);
18034 /* Follow reference OFFSET.
18035 On entry *REF_CU is the CU of the source die referencing OFFSET.
18036 On exit *REF_CU is the CU of the result.
18037 Returns NULL if OFFSET is invalid. */
18039 static struct die_info *
18040 follow_die_offset (sect_offset offset, int offset_in_dwz,
18041 struct dwarf2_cu **ref_cu)
18043 struct die_info temp_die;
18044 struct dwarf2_cu *target_cu, *cu = *ref_cu;
18046 gdb_assert (cu->per_cu != NULL);
18050 if (cu->per_cu->is_debug_types)
18052 /* .debug_types CUs cannot reference anything outside their CU.
18053 If they need to, they have to reference a signatured type via
18054 DW_FORM_ref_sig8. */
18055 if (! offset_in_cu_p (&cu->header, offset))
18058 else if (offset_in_dwz != cu->per_cu->is_dwz
18059 || ! offset_in_cu_p (&cu->header, offset))
18061 struct dwarf2_per_cu_data *per_cu;
18063 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18066 /* If necessary, add it to the queue and load its DIEs. */
18067 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18068 load_full_comp_unit (per_cu, cu->language);
18070 target_cu = per_cu->cu;
18072 else if (cu->dies == NULL)
18074 /* We're loading full DIEs during partial symbol reading. */
18075 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18076 load_full_comp_unit (cu->per_cu, language_minimal);
18079 *ref_cu = target_cu;
18080 temp_die.offset = offset;
18081 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18084 /* Follow reference attribute ATTR of SRC_DIE.
18085 On entry *REF_CU is the CU of SRC_DIE.
18086 On exit *REF_CU is the CU of the result. */
18088 static struct die_info *
18089 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
18090 struct dwarf2_cu **ref_cu)
18092 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18093 struct dwarf2_cu *cu = *ref_cu;
18094 struct die_info *die;
18096 die = follow_die_offset (offset,
18097 (attr->form == DW_FORM_GNU_ref_alt
18098 || cu->per_cu->is_dwz),
18101 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18102 "at 0x%x [in module %s]"),
18103 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
18108 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18109 Returned value is intended for DW_OP_call*. Returned
18110 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
18112 struct dwarf2_locexpr_baton
18113 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18114 struct dwarf2_per_cu_data *per_cu,
18115 CORE_ADDR (*get_frame_pc) (void *baton),
18118 struct dwarf2_cu *cu;
18119 struct die_info *die;
18120 struct attribute *attr;
18121 struct dwarf2_locexpr_baton retval;
18123 dw2_setup (per_cu->objfile);
18125 if (per_cu->cu == NULL)
18129 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18131 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18132 offset.sect_off, per_cu->objfile->name);
18134 attr = dwarf2_attr (die, DW_AT_location, cu);
18137 /* DWARF: "If there is no such attribute, then there is no effect.".
18138 DATA is ignored if SIZE is 0. */
18140 retval.data = NULL;
18143 else if (attr_form_is_section_offset (attr))
18145 struct dwarf2_loclist_baton loclist_baton;
18146 CORE_ADDR pc = (*get_frame_pc) (baton);
18149 fill_in_loclist_baton (cu, &loclist_baton, attr);
18151 retval.data = dwarf2_find_location_expression (&loclist_baton,
18153 retval.size = size;
18157 if (!attr_form_is_block (attr))
18158 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18159 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18160 offset.sect_off, per_cu->objfile->name);
18162 retval.data = DW_BLOCK (attr)->data;
18163 retval.size = DW_BLOCK (attr)->size;
18165 retval.per_cu = cu->per_cu;
18167 age_cached_comp_units ();
18172 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18175 struct dwarf2_locexpr_baton
18176 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18177 struct dwarf2_per_cu_data *per_cu,
18178 CORE_ADDR (*get_frame_pc) (void *baton),
18181 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18183 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18186 /* Write a constant of a given type as target-ordered bytes into
18189 static const gdb_byte *
18190 write_constant_as_bytes (struct obstack *obstack,
18191 enum bfd_endian byte_order,
18198 *len = TYPE_LENGTH (type);
18199 result = obstack_alloc (obstack, *len);
18200 store_unsigned_integer (result, *len, byte_order, value);
18205 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18206 pointer to the constant bytes and set LEN to the length of the
18207 data. If memory is needed, allocate it on OBSTACK. If the DIE
18208 does not have a DW_AT_const_value, return NULL. */
18211 dwarf2_fetch_constant_bytes (sect_offset offset,
18212 struct dwarf2_per_cu_data *per_cu,
18213 struct obstack *obstack,
18216 struct dwarf2_cu *cu;
18217 struct die_info *die;
18218 struct attribute *attr;
18219 const gdb_byte *result = NULL;
18222 enum bfd_endian byte_order;
18224 dw2_setup (per_cu->objfile);
18226 if (per_cu->cu == NULL)
18230 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18232 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18233 offset.sect_off, per_cu->objfile->name);
18236 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18240 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18241 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18243 switch (attr->form)
18246 case DW_FORM_GNU_addr_index:
18250 *len = cu->header.addr_size;
18251 tem = obstack_alloc (obstack, *len);
18252 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18256 case DW_FORM_string:
18258 case DW_FORM_GNU_str_index:
18259 case DW_FORM_GNU_strp_alt:
18260 /* DW_STRING is already allocated on the objfile obstack, point
18262 result = (const gdb_byte *) DW_STRING (attr);
18263 *len = strlen (DW_STRING (attr));
18265 case DW_FORM_block1:
18266 case DW_FORM_block2:
18267 case DW_FORM_block4:
18268 case DW_FORM_block:
18269 case DW_FORM_exprloc:
18270 result = DW_BLOCK (attr)->data;
18271 *len = DW_BLOCK (attr)->size;
18274 /* The DW_AT_const_value attributes are supposed to carry the
18275 symbol's value "represented as it would be on the target
18276 architecture." By the time we get here, it's already been
18277 converted to host endianness, so we just need to sign- or
18278 zero-extend it as appropriate. */
18279 case DW_FORM_data1:
18280 type = die_type (die, cu);
18281 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18282 if (result == NULL)
18283 result = write_constant_as_bytes (obstack, byte_order,
18286 case DW_FORM_data2:
18287 type = die_type (die, cu);
18288 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18289 if (result == NULL)
18290 result = write_constant_as_bytes (obstack, byte_order,
18293 case DW_FORM_data4:
18294 type = die_type (die, cu);
18295 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18296 if (result == NULL)
18297 result = write_constant_as_bytes (obstack, byte_order,
18300 case DW_FORM_data8:
18301 type = die_type (die, cu);
18302 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18303 if (result == NULL)
18304 result = write_constant_as_bytes (obstack, byte_order,
18308 case DW_FORM_sdata:
18309 type = die_type (die, cu);
18310 result = write_constant_as_bytes (obstack, byte_order,
18311 type, DW_SND (attr), len);
18314 case DW_FORM_udata:
18315 type = die_type (die, cu);
18316 result = write_constant_as_bytes (obstack, byte_order,
18317 type, DW_UNSND (attr), len);
18321 complaint (&symfile_complaints,
18322 _("unsupported const value attribute form: '%s'"),
18323 dwarf_form_name (attr->form));
18330 /* Return the type of the DIE at DIE_OFFSET in the CU named by
18334 dwarf2_get_die_type (cu_offset die_offset,
18335 struct dwarf2_per_cu_data *per_cu)
18337 sect_offset die_offset_sect;
18339 dw2_setup (per_cu->objfile);
18341 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18342 return get_die_type_at_offset (die_offset_sect, per_cu);
18345 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
18346 On entry *REF_CU is the CU of SRC_DIE.
18347 On exit *REF_CU is the CU of the result.
18348 Returns NULL if the referenced DIE isn't found. */
18350 static struct die_info *
18351 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18352 struct dwarf2_cu **ref_cu)
18354 struct objfile *objfile = (*ref_cu)->objfile;
18355 struct die_info temp_die;
18356 struct dwarf2_cu *sig_cu;
18357 struct die_info *die;
18359 /* While it might be nice to assert sig_type->type == NULL here,
18360 we can get here for DW_AT_imported_declaration where we need
18361 the DIE not the type. */
18363 /* If necessary, add it to the queue and load its DIEs. */
18365 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
18366 read_signatured_type (sig_type);
18368 gdb_assert (sig_type->per_cu.cu != NULL);
18370 sig_cu = sig_type->per_cu.cu;
18371 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18372 temp_die.offset = sig_type->type_offset_in_section;
18373 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18374 temp_die.offset.sect_off);
18377 /* For .gdb_index version 7 keep track of included TUs.
18378 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18379 if (dwarf2_per_objfile->index_table != NULL
18380 && dwarf2_per_objfile->index_table->version <= 7)
18382 VEC_safe_push (dwarf2_per_cu_ptr,
18383 (*ref_cu)->per_cu->imported_symtabs,
18394 /* Follow signatured type referenced by ATTR in SRC_DIE.
18395 On entry *REF_CU is the CU of SRC_DIE.
18396 On exit *REF_CU is the CU of the result.
18397 The result is the DIE of the type.
18398 If the referenced type cannot be found an error is thrown. */
18400 static struct die_info *
18401 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
18402 struct dwarf2_cu **ref_cu)
18404 ULONGEST signature = DW_SIGNATURE (attr);
18405 struct signatured_type *sig_type;
18406 struct die_info *die;
18408 gdb_assert (attr->form == DW_FORM_ref_sig8);
18410 sig_type = lookup_signatured_type (*ref_cu, signature);
18411 /* sig_type will be NULL if the signatured type is missing from
18413 if (sig_type == NULL)
18415 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18416 " from DIE at 0x%x [in module %s]"),
18417 hex_string (signature), src_die->offset.sect_off,
18418 (*ref_cu)->objfile->name);
18421 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18424 dump_die_for_error (src_die);
18425 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18426 " from DIE at 0x%x [in module %s]"),
18427 hex_string (signature), src_die->offset.sect_off,
18428 (*ref_cu)->objfile->name);
18434 /* Get the type specified by SIGNATURE referenced in DIE/CU,
18435 reading in and processing the type unit if necessary. */
18437 static struct type *
18438 get_signatured_type (struct die_info *die, ULONGEST signature,
18439 struct dwarf2_cu *cu)
18441 struct signatured_type *sig_type;
18442 struct dwarf2_cu *type_cu;
18443 struct die_info *type_die;
18446 sig_type = lookup_signatured_type (cu, signature);
18447 /* sig_type will be NULL if the signatured type is missing from
18449 if (sig_type == NULL)
18451 complaint (&symfile_complaints,
18452 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18453 " from DIE at 0x%x [in module %s]"),
18454 hex_string (signature), die->offset.sect_off,
18455 dwarf2_per_objfile->objfile->name);
18456 return build_error_marker_type (cu, die);
18459 /* If we already know the type we're done. */
18460 if (sig_type->type != NULL)
18461 return sig_type->type;
18464 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18465 if (type_die != NULL)
18467 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18468 is created. This is important, for example, because for c++ classes
18469 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18470 type = read_type_die (type_die, type_cu);
18473 complaint (&symfile_complaints,
18474 _("Dwarf Error: Cannot build signatured type %s"
18475 " referenced from DIE at 0x%x [in module %s]"),
18476 hex_string (signature), die->offset.sect_off,
18477 dwarf2_per_objfile->objfile->name);
18478 type = build_error_marker_type (cu, die);
18483 complaint (&symfile_complaints,
18484 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18485 " from DIE at 0x%x [in module %s]"),
18486 hex_string (signature), die->offset.sect_off,
18487 dwarf2_per_objfile->objfile->name);
18488 type = build_error_marker_type (cu, die);
18490 sig_type->type = type;
18495 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18496 reading in and processing the type unit if necessary. */
18498 static struct type *
18499 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
18500 struct dwarf2_cu *cu) /* ARI: editCase function */
18502 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18503 if (attr_form_is_ref (attr))
18505 struct dwarf2_cu *type_cu = cu;
18506 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18508 return read_type_die (type_die, type_cu);
18510 else if (attr->form == DW_FORM_ref_sig8)
18512 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18516 complaint (&symfile_complaints,
18517 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18518 " at 0x%x [in module %s]"),
18519 dwarf_form_name (attr->form), die->offset.sect_off,
18520 dwarf2_per_objfile->objfile->name);
18521 return build_error_marker_type (cu, die);
18525 /* Load the DIEs associated with type unit PER_CU into memory. */
18528 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
18530 struct signatured_type *sig_type;
18532 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18533 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18535 /* We have the per_cu, but we need the signatured_type.
18536 Fortunately this is an easy translation. */
18537 gdb_assert (per_cu->is_debug_types);
18538 sig_type = (struct signatured_type *) per_cu;
18540 gdb_assert (per_cu->cu == NULL);
18542 read_signatured_type (sig_type);
18544 gdb_assert (per_cu->cu != NULL);
18547 /* die_reader_func for read_signatured_type.
18548 This is identical to load_full_comp_unit_reader,
18549 but is kept separate for now. */
18552 read_signatured_type_reader (const struct die_reader_specs *reader,
18553 const gdb_byte *info_ptr,
18554 struct die_info *comp_unit_die,
18558 struct dwarf2_cu *cu = reader->cu;
18560 gdb_assert (cu->die_hash == NULL);
18562 htab_create_alloc_ex (cu->header.length / 12,
18566 &cu->comp_unit_obstack,
18567 hashtab_obstack_allocate,
18568 dummy_obstack_deallocate);
18571 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18572 &info_ptr, comp_unit_die);
18573 cu->dies = comp_unit_die;
18574 /* comp_unit_die is not stored in die_hash, no need. */
18576 /* We try not to read any attributes in this function, because not
18577 all CUs needed for references have been loaded yet, and symbol
18578 table processing isn't initialized. But we have to set the CU language,
18579 or we won't be able to build types correctly.
18580 Similarly, if we do not read the producer, we can not apply
18581 producer-specific interpretation. */
18582 prepare_one_comp_unit (cu, cu->dies, language_minimal);
18585 /* Read in a signatured type and build its CU and DIEs.
18586 If the type is a stub for the real type in a DWO file,
18587 read in the real type from the DWO file as well. */
18590 read_signatured_type (struct signatured_type *sig_type)
18592 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
18594 gdb_assert (per_cu->is_debug_types);
18595 gdb_assert (per_cu->cu == NULL);
18597 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18598 read_signatured_type_reader, NULL);
18599 sig_type->per_cu.tu_read = 1;
18602 /* Decode simple location descriptions.
18603 Given a pointer to a dwarf block that defines a location, compute
18604 the location and return the value.
18606 NOTE drow/2003-11-18: This function is called in two situations
18607 now: for the address of static or global variables (partial symbols
18608 only) and for offsets into structures which are expected to be
18609 (more or less) constant. The partial symbol case should go away,
18610 and only the constant case should remain. That will let this
18611 function complain more accurately. A few special modes are allowed
18612 without complaint for global variables (for instance, global
18613 register values and thread-local values).
18615 A location description containing no operations indicates that the
18616 object is optimized out. The return value is 0 for that case.
18617 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18618 callers will only want a very basic result and this can become a
18621 Note that stack[0] is unused except as a default error return. */
18624 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
18626 struct objfile *objfile = cu->objfile;
18628 size_t size = blk->size;
18629 const gdb_byte *data = blk->data;
18630 CORE_ADDR stack[64];
18632 unsigned int bytes_read, unsnd;
18638 stack[++stacki] = 0;
18677 stack[++stacki] = op - DW_OP_lit0;
18712 stack[++stacki] = op - DW_OP_reg0;
18714 dwarf2_complex_location_expr_complaint ();
18718 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18720 stack[++stacki] = unsnd;
18722 dwarf2_complex_location_expr_complaint ();
18726 stack[++stacki] = read_address (objfile->obfd, &data[i],
18731 case DW_OP_const1u:
18732 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18736 case DW_OP_const1s:
18737 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18741 case DW_OP_const2u:
18742 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18746 case DW_OP_const2s:
18747 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18751 case DW_OP_const4u:
18752 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18756 case DW_OP_const4s:
18757 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18761 case DW_OP_const8u:
18762 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18767 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18773 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18778 stack[stacki + 1] = stack[stacki];
18783 stack[stacki - 1] += stack[stacki];
18787 case DW_OP_plus_uconst:
18788 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18794 stack[stacki - 1] -= stack[stacki];
18799 /* If we're not the last op, then we definitely can't encode
18800 this using GDB's address_class enum. This is valid for partial
18801 global symbols, although the variable's address will be bogus
18804 dwarf2_complex_location_expr_complaint ();
18807 case DW_OP_GNU_push_tls_address:
18808 /* The top of the stack has the offset from the beginning
18809 of the thread control block at which the variable is located. */
18810 /* Nothing should follow this operator, so the top of stack would
18812 /* This is valid for partial global symbols, but the variable's
18813 address will be bogus in the psymtab. Make it always at least
18814 non-zero to not look as a variable garbage collected by linker
18815 which have DW_OP_addr 0. */
18817 dwarf2_complex_location_expr_complaint ();
18821 case DW_OP_GNU_uninit:
18824 case DW_OP_GNU_addr_index:
18825 case DW_OP_GNU_const_index:
18826 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18833 const char *name = get_DW_OP_name (op);
18836 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18839 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18843 return (stack[stacki]);
18846 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18847 outside of the allocated space. Also enforce minimum>0. */
18848 if (stacki >= ARRAY_SIZE (stack) - 1)
18850 complaint (&symfile_complaints,
18851 _("location description stack overflow"));
18857 complaint (&symfile_complaints,
18858 _("location description stack underflow"));
18862 return (stack[stacki]);
18865 /* memory allocation interface */
18867 static struct dwarf_block *
18868 dwarf_alloc_block (struct dwarf2_cu *cu)
18870 struct dwarf_block *blk;
18872 blk = (struct dwarf_block *)
18873 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18877 static struct die_info *
18878 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18880 struct die_info *die;
18881 size_t size = sizeof (struct die_info);
18884 size += (num_attrs - 1) * sizeof (struct attribute);
18886 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18887 memset (die, 0, sizeof (struct die_info));
18892 /* Macro support. */
18894 /* Return file name relative to the compilation directory of file number I in
18895 *LH's file name table. The result is allocated using xmalloc; the caller is
18896 responsible for freeing it. */
18899 file_file_name (int file, struct line_header *lh)
18901 /* Is the file number a valid index into the line header's file name
18902 table? Remember that file numbers start with one, not zero. */
18903 if (1 <= file && file <= lh->num_file_names)
18905 struct file_entry *fe = &lh->file_names[file - 1];
18907 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18908 return xstrdup (fe->name);
18909 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18914 /* The compiler produced a bogus file number. We can at least
18915 record the macro definitions made in the file, even if we
18916 won't be able to find the file by name. */
18917 char fake_name[80];
18919 xsnprintf (fake_name, sizeof (fake_name),
18920 "<bad macro file number %d>", file);
18922 complaint (&symfile_complaints,
18923 _("bad file number in macro information (%d)"),
18926 return xstrdup (fake_name);
18930 /* Return the full name of file number I in *LH's file name table.
18931 Use COMP_DIR as the name of the current directory of the
18932 compilation. The result is allocated using xmalloc; the caller is
18933 responsible for freeing it. */
18935 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18937 /* Is the file number a valid index into the line header's file name
18938 table? Remember that file numbers start with one, not zero. */
18939 if (1 <= file && file <= lh->num_file_names)
18941 char *relative = file_file_name (file, lh);
18943 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18945 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18948 return file_file_name (file, lh);
18952 static struct macro_source_file *
18953 macro_start_file (int file, int line,
18954 struct macro_source_file *current_file,
18955 const char *comp_dir,
18956 struct line_header *lh, struct objfile *objfile)
18958 /* File name relative to the compilation directory of this source file. */
18959 char *file_name = file_file_name (file, lh);
18961 if (! current_file)
18963 /* Note: We don't create a macro table for this compilation unit
18964 at all until we actually get a filename. */
18965 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
18967 /* If we have no current file, then this must be the start_file
18968 directive for the compilation unit's main source file. */
18969 current_file = macro_set_main (macro_table, file_name);
18970 macro_define_special (macro_table);
18973 current_file = macro_include (current_file, line, file_name);
18977 return current_file;
18981 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18982 followed by a null byte. */
18984 copy_string (const char *buf, int len)
18986 char *s = xmalloc (len + 1);
18988 memcpy (s, buf, len);
18994 static const char *
18995 consume_improper_spaces (const char *p, const char *body)
18999 complaint (&symfile_complaints,
19000 _("macro definition contains spaces "
19001 "in formal argument list:\n`%s'"),
19013 parse_macro_definition (struct macro_source_file *file, int line,
19018 /* The body string takes one of two forms. For object-like macro
19019 definitions, it should be:
19021 <macro name> " " <definition>
19023 For function-like macro definitions, it should be:
19025 <macro name> "() " <definition>
19027 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19029 Spaces may appear only where explicitly indicated, and in the
19032 The Dwarf 2 spec says that an object-like macro's name is always
19033 followed by a space, but versions of GCC around March 2002 omit
19034 the space when the macro's definition is the empty string.
19036 The Dwarf 2 spec says that there should be no spaces between the
19037 formal arguments in a function-like macro's formal argument list,
19038 but versions of GCC around March 2002 include spaces after the
19042 /* Find the extent of the macro name. The macro name is terminated
19043 by either a space or null character (for an object-like macro) or
19044 an opening paren (for a function-like macro). */
19045 for (p = body; *p; p++)
19046 if (*p == ' ' || *p == '(')
19049 if (*p == ' ' || *p == '\0')
19051 /* It's an object-like macro. */
19052 int name_len = p - body;
19053 char *name = copy_string (body, name_len);
19054 const char *replacement;
19057 replacement = body + name_len + 1;
19060 dwarf2_macro_malformed_definition_complaint (body);
19061 replacement = body + name_len;
19064 macro_define_object (file, line, name, replacement);
19068 else if (*p == '(')
19070 /* It's a function-like macro. */
19071 char *name = copy_string (body, p - body);
19074 char **argv = xmalloc (argv_size * sizeof (*argv));
19078 p = consume_improper_spaces (p, body);
19080 /* Parse the formal argument list. */
19081 while (*p && *p != ')')
19083 /* Find the extent of the current argument name. */
19084 const char *arg_start = p;
19086 while (*p && *p != ',' && *p != ')' && *p != ' ')
19089 if (! *p || p == arg_start)
19090 dwarf2_macro_malformed_definition_complaint (body);
19093 /* Make sure argv has room for the new argument. */
19094 if (argc >= argv_size)
19097 argv = xrealloc (argv, argv_size * sizeof (*argv));
19100 argv[argc++] = copy_string (arg_start, p - arg_start);
19103 p = consume_improper_spaces (p, body);
19105 /* Consume the comma, if present. */
19110 p = consume_improper_spaces (p, body);
19119 /* Perfectly formed definition, no complaints. */
19120 macro_define_function (file, line, name,
19121 argc, (const char **) argv,
19123 else if (*p == '\0')
19125 /* Complain, but do define it. */
19126 dwarf2_macro_malformed_definition_complaint (body);
19127 macro_define_function (file, line, name,
19128 argc, (const char **) argv,
19132 /* Just complain. */
19133 dwarf2_macro_malformed_definition_complaint (body);
19136 /* Just complain. */
19137 dwarf2_macro_malformed_definition_complaint (body);
19143 for (i = 0; i < argc; i++)
19149 dwarf2_macro_malformed_definition_complaint (body);
19152 /* Skip some bytes from BYTES according to the form given in FORM.
19153 Returns the new pointer. */
19155 static const gdb_byte *
19156 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19157 enum dwarf_form form,
19158 unsigned int offset_size,
19159 struct dwarf2_section_info *section)
19161 unsigned int bytes_read;
19165 case DW_FORM_data1:
19170 case DW_FORM_data2:
19174 case DW_FORM_data4:
19178 case DW_FORM_data8:
19182 case DW_FORM_string:
19183 read_direct_string (abfd, bytes, &bytes_read);
19184 bytes += bytes_read;
19187 case DW_FORM_sec_offset:
19189 case DW_FORM_GNU_strp_alt:
19190 bytes += offset_size;
19193 case DW_FORM_block:
19194 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19195 bytes += bytes_read;
19198 case DW_FORM_block1:
19199 bytes += 1 + read_1_byte (abfd, bytes);
19201 case DW_FORM_block2:
19202 bytes += 2 + read_2_bytes (abfd, bytes);
19204 case DW_FORM_block4:
19205 bytes += 4 + read_4_bytes (abfd, bytes);
19208 case DW_FORM_sdata:
19209 case DW_FORM_udata:
19210 case DW_FORM_GNU_addr_index:
19211 case DW_FORM_GNU_str_index:
19212 bytes = gdb_skip_leb128 (bytes, buffer_end);
19215 dwarf2_section_buffer_overflow_complaint (section);
19223 complaint (&symfile_complaints,
19224 _("invalid form 0x%x in `%s'"),
19226 section->asection->name);
19234 /* A helper for dwarf_decode_macros that handles skipping an unknown
19235 opcode. Returns an updated pointer to the macro data buffer; or,
19236 on error, issues a complaint and returns NULL. */
19238 static const gdb_byte *
19239 skip_unknown_opcode (unsigned int opcode,
19240 const gdb_byte **opcode_definitions,
19241 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19243 unsigned int offset_size,
19244 struct dwarf2_section_info *section)
19246 unsigned int bytes_read, i;
19248 const gdb_byte *defn;
19250 if (opcode_definitions[opcode] == NULL)
19252 complaint (&symfile_complaints,
19253 _("unrecognized DW_MACFINO opcode 0x%x"),
19258 defn = opcode_definitions[opcode];
19259 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19260 defn += bytes_read;
19262 for (i = 0; i < arg; ++i)
19264 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19266 if (mac_ptr == NULL)
19268 /* skip_form_bytes already issued the complaint. */
19276 /* A helper function which parses the header of a macro section.
19277 If the macro section is the extended (for now called "GNU") type,
19278 then this updates *OFFSET_SIZE. Returns a pointer to just after
19279 the header, or issues a complaint and returns NULL on error. */
19281 static const gdb_byte *
19282 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
19284 const gdb_byte *mac_ptr,
19285 unsigned int *offset_size,
19286 int section_is_gnu)
19288 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
19290 if (section_is_gnu)
19292 unsigned int version, flags;
19294 version = read_2_bytes (abfd, mac_ptr);
19297 complaint (&symfile_complaints,
19298 _("unrecognized version `%d' in .debug_macro section"),
19304 flags = read_1_byte (abfd, mac_ptr);
19306 *offset_size = (flags & 1) ? 8 : 4;
19308 if ((flags & 2) != 0)
19309 /* We don't need the line table offset. */
19310 mac_ptr += *offset_size;
19312 /* Vendor opcode descriptions. */
19313 if ((flags & 4) != 0)
19315 unsigned int i, count;
19317 count = read_1_byte (abfd, mac_ptr);
19319 for (i = 0; i < count; ++i)
19321 unsigned int opcode, bytes_read;
19324 opcode = read_1_byte (abfd, mac_ptr);
19326 opcode_definitions[opcode] = mac_ptr;
19327 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19328 mac_ptr += bytes_read;
19337 /* A helper for dwarf_decode_macros that handles the GNU extensions,
19338 including DW_MACRO_GNU_transparent_include. */
19341 dwarf_decode_macro_bytes (bfd *abfd,
19342 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19343 struct macro_source_file *current_file,
19344 struct line_header *lh, const char *comp_dir,
19345 struct dwarf2_section_info *section,
19346 int section_is_gnu, int section_is_dwz,
19347 unsigned int offset_size,
19348 struct objfile *objfile,
19349 htab_t include_hash)
19351 enum dwarf_macro_record_type macinfo_type;
19352 int at_commandline;
19353 const gdb_byte *opcode_definitions[256];
19355 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19356 &offset_size, section_is_gnu);
19357 if (mac_ptr == NULL)
19359 /* We already issued a complaint. */
19363 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19364 GDB is still reading the definitions from command line. First
19365 DW_MACINFO_start_file will need to be ignored as it was already executed
19366 to create CURRENT_FILE for the main source holding also the command line
19367 definitions. On first met DW_MACINFO_start_file this flag is reset to
19368 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19370 at_commandline = 1;
19374 /* Do we at least have room for a macinfo type byte? */
19375 if (mac_ptr >= mac_end)
19377 dwarf2_section_buffer_overflow_complaint (section);
19381 macinfo_type = read_1_byte (abfd, mac_ptr);
19384 /* Note that we rely on the fact that the corresponding GNU and
19385 DWARF constants are the same. */
19386 switch (macinfo_type)
19388 /* A zero macinfo type indicates the end of the macro
19393 case DW_MACRO_GNU_define:
19394 case DW_MACRO_GNU_undef:
19395 case DW_MACRO_GNU_define_indirect:
19396 case DW_MACRO_GNU_undef_indirect:
19397 case DW_MACRO_GNU_define_indirect_alt:
19398 case DW_MACRO_GNU_undef_indirect_alt:
19400 unsigned int bytes_read;
19405 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19406 mac_ptr += bytes_read;
19408 if (macinfo_type == DW_MACRO_GNU_define
19409 || macinfo_type == DW_MACRO_GNU_undef)
19411 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19412 mac_ptr += bytes_read;
19416 LONGEST str_offset;
19418 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19419 mac_ptr += offset_size;
19421 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
19422 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19425 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19427 body = read_indirect_string_from_dwz (dwz, str_offset);
19430 body = read_indirect_string_at_offset (abfd, str_offset);
19433 is_define = (macinfo_type == DW_MACRO_GNU_define
19434 || macinfo_type == DW_MACRO_GNU_define_indirect
19435 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
19436 if (! current_file)
19438 /* DWARF violation as no main source is present. */
19439 complaint (&symfile_complaints,
19440 _("debug info with no main source gives macro %s "
19442 is_define ? _("definition") : _("undefinition"),
19446 if ((line == 0 && !at_commandline)
19447 || (line != 0 && at_commandline))
19448 complaint (&symfile_complaints,
19449 _("debug info gives %s macro %s with %s line %d: %s"),
19450 at_commandline ? _("command-line") : _("in-file"),
19451 is_define ? _("definition") : _("undefinition"),
19452 line == 0 ? _("zero") : _("non-zero"), line, body);
19455 parse_macro_definition (current_file, line, body);
19458 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
19459 || macinfo_type == DW_MACRO_GNU_undef_indirect
19460 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
19461 macro_undef (current_file, line, body);
19466 case DW_MACRO_GNU_start_file:
19468 unsigned int bytes_read;
19471 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19472 mac_ptr += bytes_read;
19473 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19474 mac_ptr += bytes_read;
19476 if ((line == 0 && !at_commandline)
19477 || (line != 0 && at_commandline))
19478 complaint (&symfile_complaints,
19479 _("debug info gives source %d included "
19480 "from %s at %s line %d"),
19481 file, at_commandline ? _("command-line") : _("file"),
19482 line == 0 ? _("zero") : _("non-zero"), line);
19484 if (at_commandline)
19486 /* This DW_MACRO_GNU_start_file was executed in the
19488 at_commandline = 0;
19491 current_file = macro_start_file (file, line,
19492 current_file, comp_dir,
19497 case DW_MACRO_GNU_end_file:
19498 if (! current_file)
19499 complaint (&symfile_complaints,
19500 _("macro debug info has an unmatched "
19501 "`close_file' directive"));
19504 current_file = current_file->included_by;
19505 if (! current_file)
19507 enum dwarf_macro_record_type next_type;
19509 /* GCC circa March 2002 doesn't produce the zero
19510 type byte marking the end of the compilation
19511 unit. Complain if it's not there, but exit no
19514 /* Do we at least have room for a macinfo type byte? */
19515 if (mac_ptr >= mac_end)
19517 dwarf2_section_buffer_overflow_complaint (section);
19521 /* We don't increment mac_ptr here, so this is just
19523 next_type = read_1_byte (abfd, mac_ptr);
19524 if (next_type != 0)
19525 complaint (&symfile_complaints,
19526 _("no terminating 0-type entry for "
19527 "macros in `.debug_macinfo' section"));
19534 case DW_MACRO_GNU_transparent_include:
19535 case DW_MACRO_GNU_transparent_include_alt:
19539 bfd *include_bfd = abfd;
19540 struct dwarf2_section_info *include_section = section;
19541 struct dwarf2_section_info alt_section;
19542 const gdb_byte *include_mac_end = mac_end;
19543 int is_dwz = section_is_dwz;
19544 const gdb_byte *new_mac_ptr;
19546 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19547 mac_ptr += offset_size;
19549 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19551 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19553 dwarf2_read_section (dwarf2_per_objfile->objfile,
19556 include_bfd = dwz->macro.asection->owner;
19557 include_section = &dwz->macro;
19558 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19562 new_mac_ptr = include_section->buffer + offset;
19563 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19567 /* This has actually happened; see
19568 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19569 complaint (&symfile_complaints,
19570 _("recursive DW_MACRO_GNU_transparent_include in "
19571 ".debug_macro section"));
19575 *slot = (void *) new_mac_ptr;
19577 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
19578 include_mac_end, current_file,
19580 section, section_is_gnu, is_dwz,
19581 offset_size, objfile, include_hash);
19583 htab_remove_elt (include_hash, (void *) new_mac_ptr);
19588 case DW_MACINFO_vendor_ext:
19589 if (!section_is_gnu)
19591 unsigned int bytes_read;
19594 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19595 mac_ptr += bytes_read;
19596 read_direct_string (abfd, mac_ptr, &bytes_read);
19597 mac_ptr += bytes_read;
19599 /* We don't recognize any vendor extensions. */
19605 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19606 mac_ptr, mac_end, abfd, offset_size,
19608 if (mac_ptr == NULL)
19612 } while (macinfo_type != 0);
19616 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
19617 const char *comp_dir, int section_is_gnu)
19619 struct objfile *objfile = dwarf2_per_objfile->objfile;
19620 struct line_header *lh = cu->line_header;
19622 const gdb_byte *mac_ptr, *mac_end;
19623 struct macro_source_file *current_file = 0;
19624 enum dwarf_macro_record_type macinfo_type;
19625 unsigned int offset_size = cu->header.offset_size;
19626 const gdb_byte *opcode_definitions[256];
19627 struct cleanup *cleanup;
19628 htab_t include_hash;
19630 struct dwarf2_section_info *section;
19631 const char *section_name;
19633 if (cu->dwo_unit != NULL)
19635 if (section_is_gnu)
19637 section = &cu->dwo_unit->dwo_file->sections.macro;
19638 section_name = ".debug_macro.dwo";
19642 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19643 section_name = ".debug_macinfo.dwo";
19648 if (section_is_gnu)
19650 section = &dwarf2_per_objfile->macro;
19651 section_name = ".debug_macro";
19655 section = &dwarf2_per_objfile->macinfo;
19656 section_name = ".debug_macinfo";
19660 dwarf2_read_section (objfile, section);
19661 if (section->buffer == NULL)
19663 complaint (&symfile_complaints, _("missing %s section"), section_name);
19666 abfd = section->asection->owner;
19668 /* First pass: Find the name of the base filename.
19669 This filename is needed in order to process all macros whose definition
19670 (or undefinition) comes from the command line. These macros are defined
19671 before the first DW_MACINFO_start_file entry, and yet still need to be
19672 associated to the base file.
19674 To determine the base file name, we scan the macro definitions until we
19675 reach the first DW_MACINFO_start_file entry. We then initialize
19676 CURRENT_FILE accordingly so that any macro definition found before the
19677 first DW_MACINFO_start_file can still be associated to the base file. */
19679 mac_ptr = section->buffer + offset;
19680 mac_end = section->buffer + section->size;
19682 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19683 &offset_size, section_is_gnu);
19684 if (mac_ptr == NULL)
19686 /* We already issued a complaint. */
19692 /* Do we at least have room for a macinfo type byte? */
19693 if (mac_ptr >= mac_end)
19695 /* Complaint is printed during the second pass as GDB will probably
19696 stop the first pass earlier upon finding
19697 DW_MACINFO_start_file. */
19701 macinfo_type = read_1_byte (abfd, mac_ptr);
19704 /* Note that we rely on the fact that the corresponding GNU and
19705 DWARF constants are the same. */
19706 switch (macinfo_type)
19708 /* A zero macinfo type indicates the end of the macro
19713 case DW_MACRO_GNU_define:
19714 case DW_MACRO_GNU_undef:
19715 /* Only skip the data by MAC_PTR. */
19717 unsigned int bytes_read;
19719 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19720 mac_ptr += bytes_read;
19721 read_direct_string (abfd, mac_ptr, &bytes_read);
19722 mac_ptr += bytes_read;
19726 case DW_MACRO_GNU_start_file:
19728 unsigned int bytes_read;
19731 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19732 mac_ptr += bytes_read;
19733 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19734 mac_ptr += bytes_read;
19736 current_file = macro_start_file (file, line, current_file,
19737 comp_dir, lh, objfile);
19741 case DW_MACRO_GNU_end_file:
19742 /* No data to skip by MAC_PTR. */
19745 case DW_MACRO_GNU_define_indirect:
19746 case DW_MACRO_GNU_undef_indirect:
19747 case DW_MACRO_GNU_define_indirect_alt:
19748 case DW_MACRO_GNU_undef_indirect_alt:
19750 unsigned int bytes_read;
19752 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19753 mac_ptr += bytes_read;
19754 mac_ptr += offset_size;
19758 case DW_MACRO_GNU_transparent_include:
19759 case DW_MACRO_GNU_transparent_include_alt:
19760 /* Note that, according to the spec, a transparent include
19761 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19762 skip this opcode. */
19763 mac_ptr += offset_size;
19766 case DW_MACINFO_vendor_ext:
19767 /* Only skip the data by MAC_PTR. */
19768 if (!section_is_gnu)
19770 unsigned int bytes_read;
19772 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19773 mac_ptr += bytes_read;
19774 read_direct_string (abfd, mac_ptr, &bytes_read);
19775 mac_ptr += bytes_read;
19780 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19781 mac_ptr, mac_end, abfd, offset_size,
19783 if (mac_ptr == NULL)
19787 } while (macinfo_type != 0 && current_file == NULL);
19789 /* Second pass: Process all entries.
19791 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19792 command-line macro definitions/undefinitions. This flag is unset when we
19793 reach the first DW_MACINFO_start_file entry. */
19795 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19796 NULL, xcalloc, xfree);
19797 cleanup = make_cleanup_htab_delete (include_hash);
19798 mac_ptr = section->buffer + offset;
19799 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19800 *slot = (void *) mac_ptr;
19801 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19802 current_file, lh, comp_dir, section,
19804 offset_size, objfile, include_hash);
19805 do_cleanups (cleanup);
19808 /* Check if the attribute's form is a DW_FORM_block*
19809 if so return true else false. */
19812 attr_form_is_block (const struct attribute *attr)
19814 return (attr == NULL ? 0 :
19815 attr->form == DW_FORM_block1
19816 || attr->form == DW_FORM_block2
19817 || attr->form == DW_FORM_block4
19818 || attr->form == DW_FORM_block
19819 || attr->form == DW_FORM_exprloc);
19822 /* Return non-zero if ATTR's value is a section offset --- classes
19823 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19824 You may use DW_UNSND (attr) to retrieve such offsets.
19826 Section 7.5.4, "Attribute Encodings", explains that no attribute
19827 may have a value that belongs to more than one of these classes; it
19828 would be ambiguous if we did, because we use the same forms for all
19832 attr_form_is_section_offset (const struct attribute *attr)
19834 return (attr->form == DW_FORM_data4
19835 || attr->form == DW_FORM_data8
19836 || attr->form == DW_FORM_sec_offset);
19839 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19840 zero otherwise. When this function returns true, you can apply
19841 dwarf2_get_attr_constant_value to it.
19843 However, note that for some attributes you must check
19844 attr_form_is_section_offset before using this test. DW_FORM_data4
19845 and DW_FORM_data8 are members of both the constant class, and of
19846 the classes that contain offsets into other debug sections
19847 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19848 that, if an attribute's can be either a constant or one of the
19849 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19850 taken as section offsets, not constants. */
19853 attr_form_is_constant (const struct attribute *attr)
19855 switch (attr->form)
19857 case DW_FORM_sdata:
19858 case DW_FORM_udata:
19859 case DW_FORM_data1:
19860 case DW_FORM_data2:
19861 case DW_FORM_data4:
19862 case DW_FORM_data8:
19870 /* DW_ADDR is always stored already as sect_offset; despite for the forms
19871 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19874 attr_form_is_ref (const struct attribute *attr)
19876 switch (attr->form)
19878 case DW_FORM_ref_addr:
19883 case DW_FORM_ref_udata:
19884 case DW_FORM_GNU_ref_alt:
19891 /* Return the .debug_loc section to use for CU.
19892 For DWO files use .debug_loc.dwo. */
19894 static struct dwarf2_section_info *
19895 cu_debug_loc_section (struct dwarf2_cu *cu)
19898 return &cu->dwo_unit->dwo_file->sections.loc;
19899 return &dwarf2_per_objfile->loc;
19902 /* A helper function that fills in a dwarf2_loclist_baton. */
19905 fill_in_loclist_baton (struct dwarf2_cu *cu,
19906 struct dwarf2_loclist_baton *baton,
19907 const struct attribute *attr)
19909 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19911 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19913 baton->per_cu = cu->per_cu;
19914 gdb_assert (baton->per_cu);
19915 /* We don't know how long the location list is, but make sure we
19916 don't run off the edge of the section. */
19917 baton->size = section->size - DW_UNSND (attr);
19918 baton->data = section->buffer + DW_UNSND (attr);
19919 baton->base_address = cu->base_address;
19920 baton->from_dwo = cu->dwo_unit != NULL;
19924 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
19925 struct dwarf2_cu *cu, int is_block)
19927 struct objfile *objfile = dwarf2_per_objfile->objfile;
19928 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19930 if (attr_form_is_section_offset (attr)
19931 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19932 the section. If so, fall through to the complaint in the
19934 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19936 struct dwarf2_loclist_baton *baton;
19938 baton = obstack_alloc (&objfile->objfile_obstack,
19939 sizeof (struct dwarf2_loclist_baton));
19941 fill_in_loclist_baton (cu, baton, attr);
19943 if (cu->base_known == 0)
19944 complaint (&symfile_complaints,
19945 _("Location list used without "
19946 "specifying the CU base address."));
19948 SYMBOL_ACLASS_INDEX (sym) = (is_block
19949 ? dwarf2_loclist_block_index
19950 : dwarf2_loclist_index);
19951 SYMBOL_LOCATION_BATON (sym) = baton;
19955 struct dwarf2_locexpr_baton *baton;
19957 baton = obstack_alloc (&objfile->objfile_obstack,
19958 sizeof (struct dwarf2_locexpr_baton));
19959 baton->per_cu = cu->per_cu;
19960 gdb_assert (baton->per_cu);
19962 if (attr_form_is_block (attr))
19964 /* Note that we're just copying the block's data pointer
19965 here, not the actual data. We're still pointing into the
19966 info_buffer for SYM's objfile; right now we never release
19967 that buffer, but when we do clean up properly this may
19969 baton->size = DW_BLOCK (attr)->size;
19970 baton->data = DW_BLOCK (attr)->data;
19974 dwarf2_invalid_attrib_class_complaint ("location description",
19975 SYMBOL_NATURAL_NAME (sym));
19979 SYMBOL_ACLASS_INDEX (sym) = (is_block
19980 ? dwarf2_locexpr_block_index
19981 : dwarf2_locexpr_index);
19982 SYMBOL_LOCATION_BATON (sym) = baton;
19986 /* Return the OBJFILE associated with the compilation unit CU. If CU
19987 came from a separate debuginfo file, then the master objfile is
19991 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19993 struct objfile *objfile = per_cu->objfile;
19995 /* Return the master objfile, so that we can report and look up the
19996 correct file containing this variable. */
19997 if (objfile->separate_debug_objfile_backlink)
19998 objfile = objfile->separate_debug_objfile_backlink;
20003 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20004 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20005 CU_HEADERP first. */
20007 static const struct comp_unit_head *
20008 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20009 struct dwarf2_per_cu_data *per_cu)
20011 const gdb_byte *info_ptr;
20014 return &per_cu->cu->header;
20016 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
20018 memset (cu_headerp, 0, sizeof (*cu_headerp));
20019 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
20024 /* Return the address size given in the compilation unit header for CU. */
20027 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20029 struct comp_unit_head cu_header_local;
20030 const struct comp_unit_head *cu_headerp;
20032 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20034 return cu_headerp->addr_size;
20037 /* Return the offset size given in the compilation unit header for CU. */
20040 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20042 struct comp_unit_head cu_header_local;
20043 const struct comp_unit_head *cu_headerp;
20045 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20047 return cu_headerp->offset_size;
20050 /* See its dwarf2loc.h declaration. */
20053 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20055 struct comp_unit_head cu_header_local;
20056 const struct comp_unit_head *cu_headerp;
20058 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20060 if (cu_headerp->version == 2)
20061 return cu_headerp->addr_size;
20063 return cu_headerp->offset_size;
20066 /* Return the text offset of the CU. The returned offset comes from
20067 this CU's objfile. If this objfile came from a separate debuginfo
20068 file, then the offset may be different from the corresponding
20069 offset in the parent objfile. */
20072 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20074 struct objfile *objfile = per_cu->objfile;
20076 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20079 /* Locate the .debug_info compilation unit from CU's objfile which contains
20080 the DIE at OFFSET. Raises an error on failure. */
20082 static struct dwarf2_per_cu_data *
20083 dwarf2_find_containing_comp_unit (sect_offset offset,
20084 unsigned int offset_in_dwz,
20085 struct objfile *objfile)
20087 struct dwarf2_per_cu_data *this_cu;
20089 const sect_offset *cu_off;
20092 high = dwarf2_per_objfile->n_comp_units - 1;
20095 struct dwarf2_per_cu_data *mid_cu;
20096 int mid = low + (high - low) / 2;
20098 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20099 cu_off = &mid_cu->offset;
20100 if (mid_cu->is_dwz > offset_in_dwz
20101 || (mid_cu->is_dwz == offset_in_dwz
20102 && cu_off->sect_off >= offset.sect_off))
20107 gdb_assert (low == high);
20108 this_cu = dwarf2_per_objfile->all_comp_units[low];
20109 cu_off = &this_cu->offset;
20110 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20112 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20113 error (_("Dwarf Error: could not find partial DIE containing "
20114 "offset 0x%lx [in module %s]"),
20115 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20117 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20118 <= offset.sect_off);
20119 return dwarf2_per_objfile->all_comp_units[low-1];
20123 this_cu = dwarf2_per_objfile->all_comp_units[low];
20124 if (low == dwarf2_per_objfile->n_comp_units - 1
20125 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20126 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20127 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20132 /* Initialize dwarf2_cu CU, owned by PER_CU. */
20135 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20137 memset (cu, 0, sizeof (*cu));
20139 cu->per_cu = per_cu;
20140 cu->objfile = per_cu->objfile;
20141 obstack_init (&cu->comp_unit_obstack);
20144 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20147 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20148 enum language pretend_language)
20150 struct attribute *attr;
20152 /* Set the language we're debugging. */
20153 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20155 set_cu_language (DW_UNSND (attr), cu);
20158 cu->language = pretend_language;
20159 cu->language_defn = language_def (cu->language);
20162 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20164 cu->producer = DW_STRING (attr);
20167 /* Release one cached compilation unit, CU. We unlink it from the tree
20168 of compilation units, but we don't remove it from the read_in_chain;
20169 the caller is responsible for that.
20170 NOTE: DATA is a void * because this function is also used as a
20171 cleanup routine. */
20174 free_heap_comp_unit (void *data)
20176 struct dwarf2_cu *cu = data;
20178 gdb_assert (cu->per_cu != NULL);
20179 cu->per_cu->cu = NULL;
20182 obstack_free (&cu->comp_unit_obstack, NULL);
20187 /* This cleanup function is passed the address of a dwarf2_cu on the stack
20188 when we're finished with it. We can't free the pointer itself, but be
20189 sure to unlink it from the cache. Also release any associated storage. */
20192 free_stack_comp_unit (void *data)
20194 struct dwarf2_cu *cu = data;
20196 gdb_assert (cu->per_cu != NULL);
20197 cu->per_cu->cu = NULL;
20200 obstack_free (&cu->comp_unit_obstack, NULL);
20201 cu->partial_dies = NULL;
20204 /* Free all cached compilation units. */
20207 free_cached_comp_units (void *data)
20209 struct dwarf2_per_cu_data *per_cu, **last_chain;
20211 per_cu = dwarf2_per_objfile->read_in_chain;
20212 last_chain = &dwarf2_per_objfile->read_in_chain;
20213 while (per_cu != NULL)
20215 struct dwarf2_per_cu_data *next_cu;
20217 next_cu = per_cu->cu->read_in_chain;
20219 free_heap_comp_unit (per_cu->cu);
20220 *last_chain = next_cu;
20226 /* Increase the age counter on each cached compilation unit, and free
20227 any that are too old. */
20230 age_cached_comp_units (void)
20232 struct dwarf2_per_cu_data *per_cu, **last_chain;
20234 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20235 per_cu = dwarf2_per_objfile->read_in_chain;
20236 while (per_cu != NULL)
20238 per_cu->cu->last_used ++;
20239 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20240 dwarf2_mark (per_cu->cu);
20241 per_cu = per_cu->cu->read_in_chain;
20244 per_cu = dwarf2_per_objfile->read_in_chain;
20245 last_chain = &dwarf2_per_objfile->read_in_chain;
20246 while (per_cu != NULL)
20248 struct dwarf2_per_cu_data *next_cu;
20250 next_cu = per_cu->cu->read_in_chain;
20252 if (!per_cu->cu->mark)
20254 free_heap_comp_unit (per_cu->cu);
20255 *last_chain = next_cu;
20258 last_chain = &per_cu->cu->read_in_chain;
20264 /* Remove a single compilation unit from the cache. */
20267 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
20269 struct dwarf2_per_cu_data *per_cu, **last_chain;
20271 per_cu = dwarf2_per_objfile->read_in_chain;
20272 last_chain = &dwarf2_per_objfile->read_in_chain;
20273 while (per_cu != NULL)
20275 struct dwarf2_per_cu_data *next_cu;
20277 next_cu = per_cu->cu->read_in_chain;
20279 if (per_cu == target_per_cu)
20281 free_heap_comp_unit (per_cu->cu);
20283 *last_chain = next_cu;
20287 last_chain = &per_cu->cu->read_in_chain;
20293 /* Release all extra memory associated with OBJFILE. */
20296 dwarf2_free_objfile (struct objfile *objfile)
20298 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20300 if (dwarf2_per_objfile == NULL)
20303 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20304 free_cached_comp_units (NULL);
20306 if (dwarf2_per_objfile->quick_file_names_table)
20307 htab_delete (dwarf2_per_objfile->quick_file_names_table);
20309 /* Everything else should be on the objfile obstack. */
20312 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20313 We store these in a hash table separate from the DIEs, and preserve them
20314 when the DIEs are flushed out of cache.
20316 The CU "per_cu" pointer is needed because offset alone is not enough to
20317 uniquely identify the type. A file may have multiple .debug_types sections,
20318 or the type may come from a DWO file. Furthermore, while it's more logical
20319 to use per_cu->section+offset, with Fission the section with the data is in
20320 the DWO file but we don't know that section at the point we need it.
20321 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20322 because we can enter the lookup routine, get_die_type_at_offset, from
20323 outside this file, and thus won't necessarily have PER_CU->cu.
20324 Fortunately, PER_CU is stable for the life of the objfile. */
20326 struct dwarf2_per_cu_offset_and_type
20328 const struct dwarf2_per_cu_data *per_cu;
20329 sect_offset offset;
20333 /* Hash function for a dwarf2_per_cu_offset_and_type. */
20336 per_cu_offset_and_type_hash (const void *item)
20338 const struct dwarf2_per_cu_offset_and_type *ofs = item;
20340 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
20343 /* Equality function for a dwarf2_per_cu_offset_and_type. */
20346 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
20348 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20349 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
20351 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20352 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
20355 /* Set the type associated with DIE to TYPE. Save it in CU's hash
20356 table if necessary. For convenience, return TYPE.
20358 The DIEs reading must have careful ordering to:
20359 * Not cause infite loops trying to read in DIEs as a prerequisite for
20360 reading current DIE.
20361 * Not trying to dereference contents of still incompletely read in types
20362 while reading in other DIEs.
20363 * Enable referencing still incompletely read in types just by a pointer to
20364 the type without accessing its fields.
20366 Therefore caller should follow these rules:
20367 * Try to fetch any prerequisite types we may need to build this DIE type
20368 before building the type and calling set_die_type.
20369 * After building type call set_die_type for current DIE as soon as
20370 possible before fetching more types to complete the current type.
20371 * Make the type as complete as possible before fetching more types. */
20373 static struct type *
20374 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20376 struct dwarf2_per_cu_offset_and_type **slot, ofs;
20377 struct objfile *objfile = cu->objfile;
20379 /* For Ada types, make sure that the gnat-specific data is always
20380 initialized (if not already set). There are a few types where
20381 we should not be doing so, because the type-specific area is
20382 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20383 where the type-specific area is used to store the floatformat).
20384 But this is not a problem, because the gnat-specific information
20385 is actually not needed for these types. */
20386 if (need_gnat_info (cu)
20387 && TYPE_CODE (type) != TYPE_CODE_FUNC
20388 && TYPE_CODE (type) != TYPE_CODE_FLT
20389 && !HAVE_GNAT_AUX_INFO (type))
20390 INIT_GNAT_SPECIFIC (type);
20392 if (dwarf2_per_objfile->die_type_hash == NULL)
20394 dwarf2_per_objfile->die_type_hash =
20395 htab_create_alloc_ex (127,
20396 per_cu_offset_and_type_hash,
20397 per_cu_offset_and_type_eq,
20399 &objfile->objfile_obstack,
20400 hashtab_obstack_allocate,
20401 dummy_obstack_deallocate);
20404 ofs.per_cu = cu->per_cu;
20405 ofs.offset = die->offset;
20407 slot = (struct dwarf2_per_cu_offset_and_type **)
20408 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
20410 complaint (&symfile_complaints,
20411 _("A problem internal to GDB: DIE 0x%x has type already set"),
20412 die->offset.sect_off);
20413 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
20418 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20419 or return NULL if the die does not have a saved type. */
20421 static struct type *
20422 get_die_type_at_offset (sect_offset offset,
20423 struct dwarf2_per_cu_data *per_cu)
20425 struct dwarf2_per_cu_offset_and_type *slot, ofs;
20427 if (dwarf2_per_objfile->die_type_hash == NULL)
20430 ofs.per_cu = per_cu;
20431 ofs.offset = offset;
20432 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
20439 /* Look up the type for DIE in CU in die_type_hash,
20440 or return NULL if DIE does not have a saved type. */
20442 static struct type *
20443 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20445 return get_die_type_at_offset (die->offset, cu->per_cu);
20448 /* Add a dependence relationship from CU to REF_PER_CU. */
20451 dwarf2_add_dependence (struct dwarf2_cu *cu,
20452 struct dwarf2_per_cu_data *ref_per_cu)
20456 if (cu->dependencies == NULL)
20458 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20459 NULL, &cu->comp_unit_obstack,
20460 hashtab_obstack_allocate,
20461 dummy_obstack_deallocate);
20463 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20465 *slot = ref_per_cu;
20468 /* Subroutine of dwarf2_mark to pass to htab_traverse.
20469 Set the mark field in every compilation unit in the
20470 cache that we must keep because we are keeping CU. */
20473 dwarf2_mark_helper (void **slot, void *data)
20475 struct dwarf2_per_cu_data *per_cu;
20477 per_cu = (struct dwarf2_per_cu_data *) *slot;
20479 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20480 reading of the chain. As such dependencies remain valid it is not much
20481 useful to track and undo them during QUIT cleanups. */
20482 if (per_cu->cu == NULL)
20485 if (per_cu->cu->mark)
20487 per_cu->cu->mark = 1;
20489 if (per_cu->cu->dependencies != NULL)
20490 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20495 /* Set the mark field in CU and in every other compilation unit in the
20496 cache that we must keep because we are keeping CU. */
20499 dwarf2_mark (struct dwarf2_cu *cu)
20504 if (cu->dependencies != NULL)
20505 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
20509 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20513 per_cu->cu->mark = 0;
20514 per_cu = per_cu->cu->read_in_chain;
20518 /* Trivial hash function for partial_die_info: the hash value of a DIE
20519 is its offset in .debug_info for this objfile. */
20522 partial_die_hash (const void *item)
20524 const struct partial_die_info *part_die = item;
20526 return part_die->offset.sect_off;
20529 /* Trivial comparison function for partial_die_info structures: two DIEs
20530 are equal if they have the same offset. */
20533 partial_die_eq (const void *item_lhs, const void *item_rhs)
20535 const struct partial_die_info *part_die_lhs = item_lhs;
20536 const struct partial_die_info *part_die_rhs = item_rhs;
20538 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
20541 static struct cmd_list_element *set_dwarf2_cmdlist;
20542 static struct cmd_list_element *show_dwarf2_cmdlist;
20545 set_dwarf2_cmd (char *args, int from_tty)
20547 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20551 show_dwarf2_cmd (char *args, int from_tty)
20553 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20556 /* Free data associated with OBJFILE, if necessary. */
20559 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
20561 struct dwarf2_per_objfile *data = d;
20564 /* Make sure we don't accidentally use dwarf2_per_objfile while
20566 dwarf2_per_objfile = NULL;
20568 for (ix = 0; ix < data->n_comp_units; ++ix)
20569 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
20571 for (ix = 0; ix < data->n_type_units; ++ix)
20572 VEC_free (dwarf2_per_cu_ptr,
20573 data->all_type_units[ix]->per_cu.imported_symtabs);
20574 xfree (data->all_type_units);
20576 VEC_free (dwarf2_section_info_def, data->types);
20578 if (data->dwo_files)
20579 free_dwo_files (data->dwo_files, objfile);
20580 if (data->dwp_file)
20581 gdb_bfd_unref (data->dwp_file->dbfd);
20583 if (data->dwz_file && data->dwz_file->dwz_bfd)
20584 gdb_bfd_unref (data->dwz_file->dwz_bfd);
20588 /* The "save gdb-index" command. */
20590 /* The contents of the hash table we create when building the string
20592 struct strtab_entry
20594 offset_type offset;
20598 /* Hash function for a strtab_entry.
20600 Function is used only during write_hash_table so no index format backward
20601 compatibility is needed. */
20604 hash_strtab_entry (const void *e)
20606 const struct strtab_entry *entry = e;
20607 return mapped_index_string_hash (INT_MAX, entry->str);
20610 /* Equality function for a strtab_entry. */
20613 eq_strtab_entry (const void *a, const void *b)
20615 const struct strtab_entry *ea = a;
20616 const struct strtab_entry *eb = b;
20617 return !strcmp (ea->str, eb->str);
20620 /* Create a strtab_entry hash table. */
20623 create_strtab (void)
20625 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20626 xfree, xcalloc, xfree);
20629 /* Add a string to the constant pool. Return the string's offset in
20633 add_string (htab_t table, struct obstack *cpool, const char *str)
20636 struct strtab_entry entry;
20637 struct strtab_entry *result;
20640 slot = htab_find_slot (table, &entry, INSERT);
20645 result = XNEW (struct strtab_entry);
20646 result->offset = obstack_object_size (cpool);
20648 obstack_grow_str0 (cpool, str);
20651 return result->offset;
20654 /* An entry in the symbol table. */
20655 struct symtab_index_entry
20657 /* The name of the symbol. */
20659 /* The offset of the name in the constant pool. */
20660 offset_type index_offset;
20661 /* A sorted vector of the indices of all the CUs that hold an object
20663 VEC (offset_type) *cu_indices;
20666 /* The symbol table. This is a power-of-2-sized hash table. */
20667 struct mapped_symtab
20669 offset_type n_elements;
20671 struct symtab_index_entry **data;
20674 /* Hash function for a symtab_index_entry. */
20677 hash_symtab_entry (const void *e)
20679 const struct symtab_index_entry *entry = e;
20680 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20681 sizeof (offset_type) * VEC_length (offset_type,
20682 entry->cu_indices),
20686 /* Equality function for a symtab_index_entry. */
20689 eq_symtab_entry (const void *a, const void *b)
20691 const struct symtab_index_entry *ea = a;
20692 const struct symtab_index_entry *eb = b;
20693 int len = VEC_length (offset_type, ea->cu_indices);
20694 if (len != VEC_length (offset_type, eb->cu_indices))
20696 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20697 VEC_address (offset_type, eb->cu_indices),
20698 sizeof (offset_type) * len);
20701 /* Destroy a symtab_index_entry. */
20704 delete_symtab_entry (void *p)
20706 struct symtab_index_entry *entry = p;
20707 VEC_free (offset_type, entry->cu_indices);
20711 /* Create a hash table holding symtab_index_entry objects. */
20714 create_symbol_hash_table (void)
20716 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20717 delete_symtab_entry, xcalloc, xfree);
20720 /* Create a new mapped symtab object. */
20722 static struct mapped_symtab *
20723 create_mapped_symtab (void)
20725 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20726 symtab->n_elements = 0;
20727 symtab->size = 1024;
20728 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20732 /* Destroy a mapped_symtab. */
20735 cleanup_mapped_symtab (void *p)
20737 struct mapped_symtab *symtab = p;
20738 /* The contents of the array are freed when the other hash table is
20740 xfree (symtab->data);
20744 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20747 Function is used only during write_hash_table so no index format backward
20748 compatibility is needed. */
20750 static struct symtab_index_entry **
20751 find_slot (struct mapped_symtab *symtab, const char *name)
20753 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20755 index = hash & (symtab->size - 1);
20756 step = ((hash * 17) & (symtab->size - 1)) | 1;
20760 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20761 return &symtab->data[index];
20762 index = (index + step) & (symtab->size - 1);
20766 /* Expand SYMTAB's hash table. */
20769 hash_expand (struct mapped_symtab *symtab)
20771 offset_type old_size = symtab->size;
20773 struct symtab_index_entry **old_entries = symtab->data;
20776 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20778 for (i = 0; i < old_size; ++i)
20780 if (old_entries[i])
20782 struct symtab_index_entry **slot = find_slot (symtab,
20783 old_entries[i]->name);
20784 *slot = old_entries[i];
20788 xfree (old_entries);
20791 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20792 CU_INDEX is the index of the CU in which the symbol appears.
20793 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20796 add_index_entry (struct mapped_symtab *symtab, const char *name,
20797 int is_static, gdb_index_symbol_kind kind,
20798 offset_type cu_index)
20800 struct symtab_index_entry **slot;
20801 offset_type cu_index_and_attrs;
20803 ++symtab->n_elements;
20804 if (4 * symtab->n_elements / 3 >= symtab->size)
20805 hash_expand (symtab);
20807 slot = find_slot (symtab, name);
20810 *slot = XNEW (struct symtab_index_entry);
20811 (*slot)->name = name;
20812 /* index_offset is set later. */
20813 (*slot)->cu_indices = NULL;
20816 cu_index_and_attrs = 0;
20817 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20818 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20819 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20821 /* We don't want to record an index value twice as we want to avoid the
20823 We process all global symbols and then all static symbols
20824 (which would allow us to avoid the duplication by only having to check
20825 the last entry pushed), but a symbol could have multiple kinds in one CU.
20826 To keep things simple we don't worry about the duplication here and
20827 sort and uniqufy the list after we've processed all symbols. */
20828 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20831 /* qsort helper routine for uniquify_cu_indices. */
20834 offset_type_compare (const void *ap, const void *bp)
20836 offset_type a = *(offset_type *) ap;
20837 offset_type b = *(offset_type *) bp;
20839 return (a > b) - (b > a);
20842 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20845 uniquify_cu_indices (struct mapped_symtab *symtab)
20849 for (i = 0; i < symtab->size; ++i)
20851 struct symtab_index_entry *entry = symtab->data[i];
20854 && entry->cu_indices != NULL)
20856 unsigned int next_to_insert, next_to_check;
20857 offset_type last_value;
20859 qsort (VEC_address (offset_type, entry->cu_indices),
20860 VEC_length (offset_type, entry->cu_indices),
20861 sizeof (offset_type), offset_type_compare);
20863 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20864 next_to_insert = 1;
20865 for (next_to_check = 1;
20866 next_to_check < VEC_length (offset_type, entry->cu_indices);
20869 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20872 last_value = VEC_index (offset_type, entry->cu_indices,
20874 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20879 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20884 /* Add a vector of indices to the constant pool. */
20887 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20888 struct symtab_index_entry *entry)
20892 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20895 offset_type len = VEC_length (offset_type, entry->cu_indices);
20896 offset_type val = MAYBE_SWAP (len);
20901 entry->index_offset = obstack_object_size (cpool);
20903 obstack_grow (cpool, &val, sizeof (val));
20905 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20908 val = MAYBE_SWAP (iter);
20909 obstack_grow (cpool, &val, sizeof (val));
20914 struct symtab_index_entry *old_entry = *slot;
20915 entry->index_offset = old_entry->index_offset;
20918 return entry->index_offset;
20921 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20922 constant pool entries going into the obstack CPOOL. */
20925 write_hash_table (struct mapped_symtab *symtab,
20926 struct obstack *output, struct obstack *cpool)
20929 htab_t symbol_hash_table;
20932 symbol_hash_table = create_symbol_hash_table ();
20933 str_table = create_strtab ();
20935 /* We add all the index vectors to the constant pool first, to
20936 ensure alignment is ok. */
20937 for (i = 0; i < symtab->size; ++i)
20939 if (symtab->data[i])
20940 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20943 /* Now write out the hash table. */
20944 for (i = 0; i < symtab->size; ++i)
20946 offset_type str_off, vec_off;
20948 if (symtab->data[i])
20950 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20951 vec_off = symtab->data[i]->index_offset;
20955 /* While 0 is a valid constant pool index, it is not valid
20956 to have 0 for both offsets. */
20961 str_off = MAYBE_SWAP (str_off);
20962 vec_off = MAYBE_SWAP (vec_off);
20964 obstack_grow (output, &str_off, sizeof (str_off));
20965 obstack_grow (output, &vec_off, sizeof (vec_off));
20968 htab_delete (str_table);
20969 htab_delete (symbol_hash_table);
20972 /* Struct to map psymtab to CU index in the index file. */
20973 struct psymtab_cu_index_map
20975 struct partial_symtab *psymtab;
20976 unsigned int cu_index;
20980 hash_psymtab_cu_index (const void *item)
20982 const struct psymtab_cu_index_map *map = item;
20984 return htab_hash_pointer (map->psymtab);
20988 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20990 const struct psymtab_cu_index_map *lhs = item_lhs;
20991 const struct psymtab_cu_index_map *rhs = item_rhs;
20993 return lhs->psymtab == rhs->psymtab;
20996 /* Helper struct for building the address table. */
20997 struct addrmap_index_data
20999 struct objfile *objfile;
21000 struct obstack *addr_obstack;
21001 htab_t cu_index_htab;
21003 /* Non-zero if the previous_* fields are valid.
21004 We can't write an entry until we see the next entry (since it is only then
21005 that we know the end of the entry). */
21006 int previous_valid;
21007 /* Index of the CU in the table of all CUs in the index file. */
21008 unsigned int previous_cu_index;
21009 /* Start address of the CU. */
21010 CORE_ADDR previous_cu_start;
21013 /* Write an address entry to OBSTACK. */
21016 add_address_entry (struct objfile *objfile, struct obstack *obstack,
21017 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
21019 offset_type cu_index_to_write;
21021 CORE_ADDR baseaddr;
21023 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21025 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21026 obstack_grow (obstack, addr, 8);
21027 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21028 obstack_grow (obstack, addr, 8);
21029 cu_index_to_write = MAYBE_SWAP (cu_index);
21030 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21033 /* Worker function for traversing an addrmap to build the address table. */
21036 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21038 struct addrmap_index_data *data = datap;
21039 struct partial_symtab *pst = obj;
21041 if (data->previous_valid)
21042 add_address_entry (data->objfile, data->addr_obstack,
21043 data->previous_cu_start, start_addr,
21044 data->previous_cu_index);
21046 data->previous_cu_start = start_addr;
21049 struct psymtab_cu_index_map find_map, *map;
21050 find_map.psymtab = pst;
21051 map = htab_find (data->cu_index_htab, &find_map);
21052 gdb_assert (map != NULL);
21053 data->previous_cu_index = map->cu_index;
21054 data->previous_valid = 1;
21057 data->previous_valid = 0;
21062 /* Write OBJFILE's address map to OBSTACK.
21063 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21064 in the index file. */
21067 write_address_map (struct objfile *objfile, struct obstack *obstack,
21068 htab_t cu_index_htab)
21070 struct addrmap_index_data addrmap_index_data;
21072 /* When writing the address table, we have to cope with the fact that
21073 the addrmap iterator only provides the start of a region; we have to
21074 wait until the next invocation to get the start of the next region. */
21076 addrmap_index_data.objfile = objfile;
21077 addrmap_index_data.addr_obstack = obstack;
21078 addrmap_index_data.cu_index_htab = cu_index_htab;
21079 addrmap_index_data.previous_valid = 0;
21081 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21082 &addrmap_index_data);
21084 /* It's highly unlikely the last entry (end address = 0xff...ff)
21085 is valid, but we should still handle it.
21086 The end address is recorded as the start of the next region, but that
21087 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21089 if (addrmap_index_data.previous_valid)
21090 add_address_entry (objfile, obstack,
21091 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21092 addrmap_index_data.previous_cu_index);
21095 /* Return the symbol kind of PSYM. */
21097 static gdb_index_symbol_kind
21098 symbol_kind (struct partial_symbol *psym)
21100 domain_enum domain = PSYMBOL_DOMAIN (psym);
21101 enum address_class aclass = PSYMBOL_CLASS (psym);
21109 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21111 return GDB_INDEX_SYMBOL_KIND_TYPE;
21113 case LOC_CONST_BYTES:
21114 case LOC_OPTIMIZED_OUT:
21116 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21118 /* Note: It's currently impossible to recognize psyms as enum values
21119 short of reading the type info. For now punt. */
21120 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21122 /* There are other LOC_FOO values that one might want to classify
21123 as variables, but dwarf2read.c doesn't currently use them. */
21124 return GDB_INDEX_SYMBOL_KIND_OTHER;
21126 case STRUCT_DOMAIN:
21127 return GDB_INDEX_SYMBOL_KIND_TYPE;
21129 return GDB_INDEX_SYMBOL_KIND_OTHER;
21133 /* Add a list of partial symbols to SYMTAB. */
21136 write_psymbols (struct mapped_symtab *symtab,
21138 struct partial_symbol **psymp,
21140 offset_type cu_index,
21143 for (; count-- > 0; ++psymp)
21145 struct partial_symbol *psym = *psymp;
21148 if (SYMBOL_LANGUAGE (psym) == language_ada)
21149 error (_("Ada is not currently supported by the index"));
21151 /* Only add a given psymbol once. */
21152 slot = htab_find_slot (psyms_seen, psym, INSERT);
21155 gdb_index_symbol_kind kind = symbol_kind (psym);
21158 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21159 is_static, kind, cu_index);
21164 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
21165 exception if there is an error. */
21168 write_obstack (FILE *file, struct obstack *obstack)
21170 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21172 != obstack_object_size (obstack))
21173 error (_("couldn't data write to file"));
21176 /* Unlink a file if the argument is not NULL. */
21179 unlink_if_set (void *p)
21181 char **filename = p;
21183 unlink (*filename);
21186 /* A helper struct used when iterating over debug_types. */
21187 struct signatured_type_index_data
21189 struct objfile *objfile;
21190 struct mapped_symtab *symtab;
21191 struct obstack *types_list;
21196 /* A helper function that writes a single signatured_type to an
21200 write_one_signatured_type (void **slot, void *d)
21202 struct signatured_type_index_data *info = d;
21203 struct signatured_type *entry = (struct signatured_type *) *slot;
21204 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
21207 write_psymbols (info->symtab,
21209 info->objfile->global_psymbols.list
21210 + psymtab->globals_offset,
21211 psymtab->n_global_syms, info->cu_index,
21213 write_psymbols (info->symtab,
21215 info->objfile->static_psymbols.list
21216 + psymtab->statics_offset,
21217 psymtab->n_static_syms, info->cu_index,
21220 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21221 entry->per_cu.offset.sect_off);
21222 obstack_grow (info->types_list, val, 8);
21223 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21224 entry->type_offset_in_tu.cu_off);
21225 obstack_grow (info->types_list, val, 8);
21226 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21227 obstack_grow (info->types_list, val, 8);
21234 /* Recurse into all "included" dependencies and write their symbols as
21235 if they appeared in this psymtab. */
21238 recursively_write_psymbols (struct objfile *objfile,
21239 struct partial_symtab *psymtab,
21240 struct mapped_symtab *symtab,
21242 offset_type cu_index)
21246 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21247 if (psymtab->dependencies[i]->user != NULL)
21248 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21249 symtab, psyms_seen, cu_index);
21251 write_psymbols (symtab,
21253 objfile->global_psymbols.list + psymtab->globals_offset,
21254 psymtab->n_global_syms, cu_index,
21256 write_psymbols (symtab,
21258 objfile->static_psymbols.list + psymtab->statics_offset,
21259 psymtab->n_static_syms, cu_index,
21263 /* Create an index file for OBJFILE in the directory DIR. */
21266 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21268 struct cleanup *cleanup;
21269 char *filename, *cleanup_filename;
21270 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21271 struct obstack cu_list, types_cu_list;
21274 struct mapped_symtab *symtab;
21275 offset_type val, size_of_contents, total_len;
21278 htab_t cu_index_htab;
21279 struct psymtab_cu_index_map *psymtab_cu_index_map;
21281 if (dwarf2_per_objfile->using_index)
21282 error (_("Cannot use an index to create the index"));
21284 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21285 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21287 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21290 if (stat (objfile->name, &st) < 0)
21291 perror_with_name (objfile->name);
21293 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21294 INDEX_SUFFIX, (char *) NULL);
21295 cleanup = make_cleanup (xfree, filename);
21297 out_file = gdb_fopen_cloexec (filename, "wb");
21299 error (_("Can't open `%s' for writing"), filename);
21301 cleanup_filename = filename;
21302 make_cleanup (unlink_if_set, &cleanup_filename);
21304 symtab = create_mapped_symtab ();
21305 make_cleanup (cleanup_mapped_symtab, symtab);
21307 obstack_init (&addr_obstack);
21308 make_cleanup_obstack_free (&addr_obstack);
21310 obstack_init (&cu_list);
21311 make_cleanup_obstack_free (&cu_list);
21313 obstack_init (&types_cu_list);
21314 make_cleanup_obstack_free (&types_cu_list);
21316 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21317 NULL, xcalloc, xfree);
21318 make_cleanup_htab_delete (psyms_seen);
21320 /* While we're scanning CU's create a table that maps a psymtab pointer
21321 (which is what addrmap records) to its index (which is what is recorded
21322 in the index file). This will later be needed to write the address
21324 cu_index_htab = htab_create_alloc (100,
21325 hash_psymtab_cu_index,
21326 eq_psymtab_cu_index,
21327 NULL, xcalloc, xfree);
21328 make_cleanup_htab_delete (cu_index_htab);
21329 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21330 xmalloc (sizeof (struct psymtab_cu_index_map)
21331 * dwarf2_per_objfile->n_comp_units);
21332 make_cleanup (xfree, psymtab_cu_index_map);
21334 /* The CU list is already sorted, so we don't need to do additional
21335 work here. Also, the debug_types entries do not appear in
21336 all_comp_units, but only in their own hash table. */
21337 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21339 struct dwarf2_per_cu_data *per_cu
21340 = dwarf2_per_objfile->all_comp_units[i];
21341 struct partial_symtab *psymtab = per_cu->v.psymtab;
21343 struct psymtab_cu_index_map *map;
21346 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21347 It may be referenced from a local scope but in such case it does not
21348 need to be present in .gdb_index. */
21349 if (psymtab == NULL)
21352 if (psymtab->user == NULL)
21353 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
21355 map = &psymtab_cu_index_map[i];
21356 map->psymtab = psymtab;
21358 slot = htab_find_slot (cu_index_htab, map, INSERT);
21359 gdb_assert (slot != NULL);
21360 gdb_assert (*slot == NULL);
21363 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21364 per_cu->offset.sect_off);
21365 obstack_grow (&cu_list, val, 8);
21366 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
21367 obstack_grow (&cu_list, val, 8);
21370 /* Dump the address map. */
21371 write_address_map (objfile, &addr_obstack, cu_index_htab);
21373 /* Write out the .debug_type entries, if any. */
21374 if (dwarf2_per_objfile->signatured_types)
21376 struct signatured_type_index_data sig_data;
21378 sig_data.objfile = objfile;
21379 sig_data.symtab = symtab;
21380 sig_data.types_list = &types_cu_list;
21381 sig_data.psyms_seen = psyms_seen;
21382 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21383 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21384 write_one_signatured_type, &sig_data);
21387 /* Now that we've processed all symbols we can shrink their cu_indices
21389 uniquify_cu_indices (symtab);
21391 obstack_init (&constant_pool);
21392 make_cleanup_obstack_free (&constant_pool);
21393 obstack_init (&symtab_obstack);
21394 make_cleanup_obstack_free (&symtab_obstack);
21395 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21397 obstack_init (&contents);
21398 make_cleanup_obstack_free (&contents);
21399 size_of_contents = 6 * sizeof (offset_type);
21400 total_len = size_of_contents;
21402 /* The version number. */
21403 val = MAYBE_SWAP (8);
21404 obstack_grow (&contents, &val, sizeof (val));
21406 /* The offset of the CU list from the start of the file. */
21407 val = MAYBE_SWAP (total_len);
21408 obstack_grow (&contents, &val, sizeof (val));
21409 total_len += obstack_object_size (&cu_list);
21411 /* The offset of the types CU list from the start of the file. */
21412 val = MAYBE_SWAP (total_len);
21413 obstack_grow (&contents, &val, sizeof (val));
21414 total_len += obstack_object_size (&types_cu_list);
21416 /* The offset of the address table from the start of the file. */
21417 val = MAYBE_SWAP (total_len);
21418 obstack_grow (&contents, &val, sizeof (val));
21419 total_len += obstack_object_size (&addr_obstack);
21421 /* The offset of the symbol table from the start of the file. */
21422 val = MAYBE_SWAP (total_len);
21423 obstack_grow (&contents, &val, sizeof (val));
21424 total_len += obstack_object_size (&symtab_obstack);
21426 /* The offset of the constant pool from the start of the file. */
21427 val = MAYBE_SWAP (total_len);
21428 obstack_grow (&contents, &val, sizeof (val));
21429 total_len += obstack_object_size (&constant_pool);
21431 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21433 write_obstack (out_file, &contents);
21434 write_obstack (out_file, &cu_list);
21435 write_obstack (out_file, &types_cu_list);
21436 write_obstack (out_file, &addr_obstack);
21437 write_obstack (out_file, &symtab_obstack);
21438 write_obstack (out_file, &constant_pool);
21442 /* We want to keep the file, so we set cleanup_filename to NULL
21443 here. See unlink_if_set. */
21444 cleanup_filename = NULL;
21446 do_cleanups (cleanup);
21449 /* Implementation of the `save gdb-index' command.
21451 Note that the file format used by this command is documented in the
21452 GDB manual. Any changes here must be documented there. */
21455 save_gdb_index_command (char *arg, int from_tty)
21457 struct objfile *objfile;
21460 error (_("usage: save gdb-index DIRECTORY"));
21462 ALL_OBJFILES (objfile)
21466 /* If the objfile does not correspond to an actual file, skip it. */
21467 if (stat (objfile->name, &st) < 0)
21470 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21471 if (dwarf2_per_objfile)
21473 volatile struct gdb_exception except;
21475 TRY_CATCH (except, RETURN_MASK_ERROR)
21477 write_psymtabs_to_index (objfile, arg);
21479 if (except.reason < 0)
21480 exception_fprintf (gdb_stderr, except,
21481 _("Error while writing index for `%s': "),
21489 int dwarf2_always_disassemble;
21492 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21493 struct cmd_list_element *c, const char *value)
21495 fprintf_filtered (file,
21496 _("Whether to always disassemble "
21497 "DWARF expressions is %s.\n"),
21502 show_check_physname (struct ui_file *file, int from_tty,
21503 struct cmd_list_element *c, const char *value)
21505 fprintf_filtered (file,
21506 _("Whether to check \"physname\" is %s.\n"),
21510 void _initialize_dwarf2_read (void);
21513 _initialize_dwarf2_read (void)
21515 struct cmd_list_element *c;
21517 dwarf2_objfile_data_key
21518 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
21520 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21521 Set DWARF 2 specific variables.\n\
21522 Configure DWARF 2 variables such as the cache size"),
21523 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21524 0/*allow-unknown*/, &maintenance_set_cmdlist);
21526 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21527 Show DWARF 2 specific variables\n\
21528 Show DWARF 2 variables such as the cache size"),
21529 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21530 0/*allow-unknown*/, &maintenance_show_cmdlist);
21532 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21533 &dwarf2_max_cache_age, _("\
21534 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21535 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21536 A higher limit means that cached compilation units will be stored\n\
21537 in memory longer, and more total memory will be used. Zero disables\n\
21538 caching, which can slow down startup."),
21540 show_dwarf2_max_cache_age,
21541 &set_dwarf2_cmdlist,
21542 &show_dwarf2_cmdlist);
21544 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21545 &dwarf2_always_disassemble, _("\
21546 Set whether `info address' always disassembles DWARF expressions."), _("\
21547 Show whether `info address' always disassembles DWARF expressions."), _("\
21548 When enabled, DWARF expressions are always printed in an assembly-like\n\
21549 syntax. When disabled, expressions will be printed in a more\n\
21550 conversational style, when possible."),
21552 show_dwarf2_always_disassemble,
21553 &set_dwarf2_cmdlist,
21554 &show_dwarf2_cmdlist);
21556 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21557 Set debugging of the dwarf2 reader."), _("\
21558 Show debugging of the dwarf2 reader."), _("\
21559 When enabled, debugging messages are printed during dwarf2 reading\n\
21560 and symtab expansion."),
21563 &setdebuglist, &showdebuglist);
21565 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
21566 Set debugging of the dwarf2 DIE reader."), _("\
21567 Show debugging of the dwarf2 DIE reader."), _("\
21568 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21569 The value is the maximum depth to print."),
21572 &setdebuglist, &showdebuglist);
21574 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21575 Set cross-checking of \"physname\" code against demangler."), _("\
21576 Show cross-checking of \"physname\" code against demangler."), _("\
21577 When enabled, GDB's internal \"physname\" code is checked against\n\
21579 NULL, show_check_physname,
21580 &setdebuglist, &showdebuglist);
21582 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21583 no_class, &use_deprecated_index_sections, _("\
21584 Set whether to use deprecated gdb_index sections."), _("\
21585 Show whether to use deprecated gdb_index sections."), _("\
21586 When enabled, deprecated .gdb_index sections are used anyway.\n\
21587 Normally they are ignored either because of a missing feature or\n\
21588 performance issue.\n\
21589 Warning: This option must be enabled before gdb reads the file."),
21592 &setlist, &showlist);
21594 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
21596 Save a gdb-index file.\n\
21597 Usage: save gdb-index DIRECTORY"),
21599 set_cmd_completer (c, filename_completer);
21601 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21602 &dwarf2_locexpr_funcs);
21603 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21604 &dwarf2_loclist_funcs);
21606 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21607 &dwarf2_block_frame_base_locexpr_funcs);
21608 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21609 &dwarf2_block_frame_base_loclist_funcs);