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 /* The section this CU/TU lives in.
562 If the DIE refers to a DWO file, this is always the original die,
564 struct dwarf2_section_info *section;
566 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
567 of the CU cache it gets reset to NULL again. */
568 struct dwarf2_cu *cu;
570 /* The corresponding objfile.
571 Normally we can get the objfile from dwarf2_per_objfile.
572 However we can enter this file with just a "per_cu" handle. */
573 struct objfile *objfile;
575 /* When using partial symbol tables, the 'psymtab' field is active.
576 Otherwise the 'quick' field is active. */
579 /* The partial symbol table associated with this compilation unit,
580 or NULL for unread partial units. */
581 struct partial_symtab *psymtab;
583 /* Data needed by the "quick" functions. */
584 struct dwarf2_per_cu_quick_data *quick;
587 /* The CUs we import using DW_TAG_imported_unit. This is filled in
588 while reading psymtabs, used to compute the psymtab dependencies,
589 and then cleared. Then it is filled in again while reading full
590 symbols, and only deleted when the objfile is destroyed.
592 This is also used to work around a difference between the way gold
593 generates .gdb_index version <=7 and the way gdb does. Arguably this
594 is a gold bug. For symbols coming from TUs, gold records in the index
595 the CU that includes the TU instead of the TU itself. This breaks
596 dw2_lookup_symbol: It assumes that if the index says symbol X lives
597 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
598 will find X. Alas TUs live in their own symtab, so after expanding CU Y
599 we need to look in TU Z to find X. Fortunately, this is akin to
600 DW_TAG_imported_unit, so we just use the same mechanism: For
601 .gdb_index version <=7 this also records the TUs that the CU referred
602 to. Concurrently with this change gdb was modified to emit version 8
603 indices so we only pay a price for gold generated indices. */
604 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
607 /* Entry in the signatured_types hash table. */
609 struct signatured_type
611 /* The "per_cu" object of this type.
612 This struct is used iff per_cu.is_debug_types.
613 N.B.: This is the first member so that it's easy to convert pointers
615 struct dwarf2_per_cu_data per_cu;
617 /* The type's signature. */
620 /* Offset in the TU of the type's DIE, as read from the TU header.
621 If this TU is a DWO stub and the definition lives in a DWO file
622 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
623 cu_offset type_offset_in_tu;
625 /* Offset in the section of the type's DIE.
626 If the definition lives in a DWO file, this is the offset in the
627 .debug_types.dwo section.
628 The value is zero until the actual value is known.
629 Zero is otherwise not a valid section offset. */
630 sect_offset type_offset_in_section;
632 /* Type units are grouped by their DW_AT_stmt_list entry so that they
633 can share them. This points to the containing symtab. */
634 struct type_unit_group *type_unit_group;
637 The first time we encounter this type we fully read it in and install it
638 in the symbol tables. Subsequent times we only need the type. */
641 /* Containing DWO unit.
642 This field is valid iff per_cu.reading_dwo_directly. */
643 struct dwo_unit *dwo_unit;
646 typedef struct signatured_type *sig_type_ptr;
647 DEF_VEC_P (sig_type_ptr);
649 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
650 This includes type_unit_group and quick_file_names. */
652 struct stmt_list_hash
654 /* The DWO unit this table is from or NULL if there is none. */
655 struct dwo_unit *dwo_unit;
657 /* Offset in .debug_line or .debug_line.dwo. */
658 sect_offset line_offset;
661 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
662 an object of this type. */
664 struct type_unit_group
666 /* dwarf2read.c's main "handle" on a TU symtab.
667 To simplify things we create an artificial CU that "includes" all the
668 type units using this stmt_list so that the rest of the code still has
669 a "per_cu" handle on the symtab.
670 This PER_CU is recognized by having no section. */
671 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
672 struct dwarf2_per_cu_data per_cu;
674 /* The TUs that share this DW_AT_stmt_list entry.
675 This is added to while parsing type units to build partial symtabs,
676 and is deleted afterwards and not used again. */
677 VEC (sig_type_ptr) *tus;
679 /* The primary symtab.
680 Type units in a group needn't all be defined in the same source file,
681 so we create an essentially anonymous symtab as the primary symtab. */
682 struct symtab *primary_symtab;
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
687 /* The number of symtabs from the line header.
688 The value here must match line_header.num_file_names. */
689 unsigned int num_symtabs;
691 /* The symbol tables for this TU (obtained from the files listed in
693 WARNING: The order of entries here must match the order of entries
694 in the line header. After the first TU using this type_unit_group, the
695 line header for the subsequent TUs is recreated from this. This is done
696 because we need to use the same symtabs for each TU using the same
697 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
698 there's no guarantee the line header doesn't have duplicate entries. */
699 struct symtab **symtabs;
702 /* These sections are what may appear in a DWO file. */
706 struct dwarf2_section_info abbrev;
707 struct dwarf2_section_info line;
708 struct dwarf2_section_info loc;
709 struct dwarf2_section_info macinfo;
710 struct dwarf2_section_info macro;
711 struct dwarf2_section_info str;
712 struct dwarf2_section_info str_offsets;
713 /* In the case of a virtual DWO file, these two are unused. */
714 struct dwarf2_section_info info;
715 VEC (dwarf2_section_info_def) *types;
718 /* CUs/TUs in DWP/DWO files. */
722 /* Backlink to the containing struct dwo_file. */
723 struct dwo_file *dwo_file;
725 /* The "id" that distinguishes this CU/TU.
726 .debug_info calls this "dwo_id", .debug_types calls this "signature".
727 Since signatures came first, we stick with it for consistency. */
730 /* The section this CU/TU lives in, in the DWO file. */
731 struct dwarf2_section_info *section;
733 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
737 /* For types, offset in the type's DIE of the type defined by this TU. */
738 cu_offset type_offset_in_tu;
741 /* Data for one DWO file.
742 This includes virtual DWO files that have been packaged into a
747 /* The DW_AT_GNU_dwo_name attribute.
748 For virtual DWO files the name is constructed from the section offsets
749 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
750 from related CU+TUs. */
751 const char *dwo_name;
753 /* The DW_AT_comp_dir attribute. */
754 const char *comp_dir;
756 /* The bfd, when the file is open. Otherwise this is NULL.
757 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
760 /* Section info for this file. */
761 struct dwo_sections sections;
763 /* The CU in the file.
764 We only support one because having more than one requires hacking the
765 dwo_name of each to match, which is highly unlikely to happen.
766 Doing this means all TUs can share comp_dir: We also assume that
767 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
770 /* Table of TUs in the file.
771 Each element is a struct dwo_unit. */
775 /* These sections are what may appear in a DWP file. */
779 struct dwarf2_section_info str;
780 struct dwarf2_section_info cu_index;
781 struct dwarf2_section_info tu_index;
782 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
783 by section number. We don't need to record them here. */
786 /* These sections are what may appear in a virtual DWO file. */
788 struct virtual_dwo_sections
790 struct dwarf2_section_info abbrev;
791 struct dwarf2_section_info line;
792 struct dwarf2_section_info loc;
793 struct dwarf2_section_info macinfo;
794 struct dwarf2_section_info macro;
795 struct dwarf2_section_info str_offsets;
796 /* Each DWP hash table entry records one CU or one TU.
797 That is recorded here, and copied to dwo_unit.section. */
798 struct dwarf2_section_info info_or_types;
801 /* Contents of DWP hash tables. */
803 struct dwp_hash_table
805 uint32_t nr_units, nr_slots;
806 const gdb_byte *hash_table, *unit_table, *section_pool;
809 /* Data for one DWP file. */
813 /* Name of the file. */
819 /* Section info for this file. */
820 struct dwp_sections sections;
822 /* Table of CUs in the file. */
823 const struct dwp_hash_table *cus;
825 /* Table of TUs in the file. */
826 const struct dwp_hash_table *tus;
828 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
831 /* Table to map ELF section numbers to their sections. */
832 unsigned int num_sections;
833 asection **elf_sections;
836 /* This represents a '.dwz' file. */
840 /* A dwz file can only contain a few sections. */
841 struct dwarf2_section_info abbrev;
842 struct dwarf2_section_info info;
843 struct dwarf2_section_info str;
844 struct dwarf2_section_info line;
845 struct dwarf2_section_info macro;
846 struct dwarf2_section_info gdb_index;
852 /* Struct used to pass misc. parameters to read_die_and_children, et
853 al. which are used for both .debug_info and .debug_types dies.
854 All parameters here are unchanging for the life of the call. This
855 struct exists to abstract away the constant parameters of die reading. */
857 struct die_reader_specs
859 /* die_section->asection->owner. */
862 /* The CU of the DIE we are parsing. */
863 struct dwarf2_cu *cu;
865 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
866 struct dwo_file *dwo_file;
868 /* The section the die comes from.
869 This is either .debug_info or .debug_types, or the .dwo variants. */
870 struct dwarf2_section_info *die_section;
872 /* die_section->buffer. */
873 const gdb_byte *buffer;
875 /* The end of the buffer. */
876 const gdb_byte *buffer_end;
878 /* The value of the DW_AT_comp_dir attribute. */
879 const char *comp_dir;
882 /* Type of function passed to init_cutu_and_read_dies, et.al. */
883 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
884 const gdb_byte *info_ptr,
885 struct die_info *comp_unit_die,
889 /* The line number information for a compilation unit (found in the
890 .debug_line section) begins with a "statement program header",
891 which contains the following information. */
894 unsigned int total_length;
895 unsigned short version;
896 unsigned int header_length;
897 unsigned char minimum_instruction_length;
898 unsigned char maximum_ops_per_instruction;
899 unsigned char default_is_stmt;
901 unsigned char line_range;
902 unsigned char opcode_base;
904 /* standard_opcode_lengths[i] is the number of operands for the
905 standard opcode whose value is i. This means that
906 standard_opcode_lengths[0] is unused, and the last meaningful
907 element is standard_opcode_lengths[opcode_base - 1]. */
908 unsigned char *standard_opcode_lengths;
910 /* The include_directories table. NOTE! These strings are not
911 allocated with xmalloc; instead, they are pointers into
912 debug_line_buffer. If you try to free them, `free' will get
914 unsigned int num_include_dirs, include_dirs_size;
915 const char **include_dirs;
917 /* The file_names table. NOTE! These strings are not allocated
918 with xmalloc; instead, they are pointers into debug_line_buffer.
919 Don't try to free them directly. */
920 unsigned int num_file_names, file_names_size;
924 unsigned int dir_index;
925 unsigned int mod_time;
927 int included_p; /* Non-zero if referenced by the Line Number Program. */
928 struct symtab *symtab; /* The associated symbol table, if any. */
931 /* The start and end of the statement program following this
932 header. These point into dwarf2_per_objfile->line_buffer. */
933 const gdb_byte *statement_program_start, *statement_program_end;
936 /* When we construct a partial symbol table entry we only
937 need this much information. */
938 struct partial_die_info
940 /* Offset of this DIE. */
943 /* DWARF-2 tag for this DIE. */
944 ENUM_BITFIELD(dwarf_tag) tag : 16;
946 /* Assorted flags describing the data found in this DIE. */
947 unsigned int has_children : 1;
948 unsigned int is_external : 1;
949 unsigned int is_declaration : 1;
950 unsigned int has_type : 1;
951 unsigned int has_specification : 1;
952 unsigned int has_pc_info : 1;
953 unsigned int may_be_inlined : 1;
955 /* Flag set if the SCOPE field of this structure has been
957 unsigned int scope_set : 1;
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
965 /* Flag set if fixup_partial_die has been called on this die. */
966 unsigned int fixup_called : 1;
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
974 /* The name of this DIE. Normally the value of DW_AT_name, but
975 sometimes a default name for unnamed DIEs. */
978 /* The linkage name, if present. */
979 const char *linkage_name;
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1001 DW_AT_sibling, if any. */
1002 /* NOTE: This member isn't strictly necessary, read_partial_die could
1003 return DW_AT_sibling values to its caller load_partial_dies. */
1004 const gdb_byte *sibling;
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
1009 sect_offset spec_offset;
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1013 struct partial_die_info *die_parent, *die_child, *die_sibling;
1016 /* This data structure holds the information of an abbrev. */
1019 unsigned int number; /* number identifying abbrev */
1020 enum dwarf_tag tag; /* dwarf tag */
1021 unsigned short has_children; /* boolean */
1022 unsigned short num_attrs; /* number of attributes */
1023 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1024 struct abbrev_info *next; /* next in chain */
1029 ENUM_BITFIELD(dwarf_attribute) name : 16;
1030 ENUM_BITFIELD(dwarf_form) form : 16;
1033 /* Size of abbrev_table.abbrev_hash_table. */
1034 #define ABBREV_HASH_SIZE 121
1036 /* Top level data structure to contain an abbreviation table. */
1040 /* Where the abbrev table came from.
1041 This is used as a sanity check when the table is used. */
1044 /* Storage for the abbrev table. */
1045 struct obstack abbrev_obstack;
1047 /* Hash table of abbrevs.
1048 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1049 It could be statically allocated, but the previous code didn't so we
1051 struct abbrev_info **abbrevs;
1054 /* Attributes have a name and a value. */
1057 ENUM_BITFIELD(dwarf_attribute) name : 16;
1058 ENUM_BITFIELD(dwarf_form) form : 15;
1060 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1061 field should be in u.str (existing only for DW_STRING) but it is kept
1062 here for better struct attribute alignment. */
1063 unsigned int string_is_canonical : 1;
1068 struct dwarf_block *blk;
1077 /* This data structure holds a complete die structure. */
1080 /* DWARF-2 tag for this DIE. */
1081 ENUM_BITFIELD(dwarf_tag) tag : 16;
1083 /* Number of attributes */
1084 unsigned char num_attrs;
1086 /* True if we're presently building the full type name for the
1087 type derived from this DIE. */
1088 unsigned char building_fullname : 1;
1091 unsigned int abbrev;
1093 /* Offset in .debug_info or .debug_types section. */
1096 /* The dies in a compilation unit form an n-ary tree. PARENT
1097 points to this die's parent; CHILD points to the first child of
1098 this node; and all the children of a given node are chained
1099 together via their SIBLING fields. */
1100 struct die_info *child; /* Its first child, if any. */
1101 struct die_info *sibling; /* Its next sibling, if any. */
1102 struct die_info *parent; /* Its parent, if any. */
1104 /* An array of attributes, with NUM_ATTRS elements. There may be
1105 zero, but it's not common and zero-sized arrays are not
1106 sufficiently portable C. */
1107 struct attribute attrs[1];
1110 /* Get at parts of an attribute structure. */
1112 #define DW_STRING(attr) ((attr)->u.str)
1113 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1114 #define DW_UNSND(attr) ((attr)->u.unsnd)
1115 #define DW_BLOCK(attr) ((attr)->u.blk)
1116 #define DW_SND(attr) ((attr)->u.snd)
1117 #define DW_ADDR(attr) ((attr)->u.addr)
1118 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1120 /* Blocks are a bunch of untyped bytes. */
1125 /* Valid only if SIZE is not zero. */
1126 const gdb_byte *data;
1129 #ifndef ATTR_ALLOC_CHUNK
1130 #define ATTR_ALLOC_CHUNK 4
1133 /* Allocate fields for structs, unions and enums in this size. */
1134 #ifndef DW_FIELD_ALLOC_CHUNK
1135 #define DW_FIELD_ALLOC_CHUNK 4
1138 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1139 but this would require a corresponding change in unpack_field_as_long
1141 static int bits_per_byte = 8;
1143 /* The routines that read and process dies for a C struct or C++ class
1144 pass lists of data member fields and lists of member function fields
1145 in an instance of a field_info structure, as defined below. */
1148 /* List of data member and baseclasses fields. */
1151 struct nextfield *next;
1156 *fields, *baseclasses;
1158 /* Number of fields (including baseclasses). */
1161 /* Number of baseclasses. */
1164 /* Set if the accesibility of one of the fields is not public. */
1165 int non_public_fields;
1167 /* Member function fields array, entries are allocated in the order they
1168 are encountered in the object file. */
1171 struct nextfnfield *next;
1172 struct fn_field fnfield;
1176 /* Member function fieldlist array, contains name of possibly overloaded
1177 member function, number of overloaded member functions and a pointer
1178 to the head of the member function field chain. */
1183 struct nextfnfield *head;
1187 /* Number of entries in the fnfieldlists array. */
1190 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1191 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1192 struct typedef_field_list
1194 struct typedef_field field;
1195 struct typedef_field_list *next;
1197 *typedef_field_list;
1198 unsigned typedef_field_list_count;
1201 /* One item on the queue of compilation units to read in full symbols
1203 struct dwarf2_queue_item
1205 struct dwarf2_per_cu_data *per_cu;
1206 enum language pretend_language;
1207 struct dwarf2_queue_item *next;
1210 /* The current queue. */
1211 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1213 /* Loaded secondary compilation units are kept in memory until they
1214 have not been referenced for the processing of this many
1215 compilation units. Set this to zero to disable caching. Cache
1216 sizes of up to at least twenty will improve startup time for
1217 typical inter-CU-reference binaries, at an obvious memory cost. */
1218 static int dwarf2_max_cache_age = 5;
1220 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1221 struct cmd_list_element *c, const char *value)
1223 fprintf_filtered (file, _("The upper bound on the age of cached "
1224 "dwarf2 compilation units is %s.\n"),
1229 /* Various complaints about symbol reading that don't abort the process. */
1232 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1234 complaint (&symfile_complaints,
1235 _("statement list doesn't fit in .debug_line section"));
1239 dwarf2_debug_line_missing_file_complaint (void)
1241 complaint (&symfile_complaints,
1242 _(".debug_line section has line data without a file"));
1246 dwarf2_debug_line_missing_end_sequence_complaint (void)
1248 complaint (&symfile_complaints,
1249 _(".debug_line section has line "
1250 "program sequence without an end"));
1254 dwarf2_complex_location_expr_complaint (void)
1256 complaint (&symfile_complaints, _("location expression too complex"));
1260 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1263 complaint (&symfile_complaints,
1264 _("const value length mismatch for '%s', got %d, expected %d"),
1269 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1271 complaint (&symfile_complaints,
1272 _("debug info runs off end of %s section"
1274 section->asection->name,
1275 bfd_get_filename (section->asection->owner));
1279 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1281 complaint (&symfile_complaints,
1282 _("macro debug info contains a "
1283 "malformed macro definition:\n`%s'"),
1288 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1290 complaint (&symfile_complaints,
1291 _("invalid attribute class or form for '%s' in '%s'"),
1295 /* local function prototypes */
1297 static void dwarf2_locate_sections (bfd *, asection *, void *);
1299 static void dwarf2_find_base_address (struct die_info *die,
1300 struct dwarf2_cu *cu);
1302 static struct partial_symtab *create_partial_symtab
1303 (struct dwarf2_per_cu_data *per_cu, const char *name);
1305 static void dwarf2_build_psymtabs_hard (struct objfile *);
1307 static void scan_partial_symbols (struct partial_die_info *,
1308 CORE_ADDR *, CORE_ADDR *,
1309 int, struct dwarf2_cu *);
1311 static void add_partial_symbol (struct partial_die_info *,
1312 struct dwarf2_cu *);
1314 static void add_partial_namespace (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1318 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1319 CORE_ADDR *highpc, int need_pc,
1320 struct dwarf2_cu *cu);
1322 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1323 struct dwarf2_cu *cu);
1325 static void add_partial_subprogram (struct partial_die_info *pdi,
1326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1327 int need_pc, struct dwarf2_cu *cu);
1329 static void dwarf2_read_symtab (struct partial_symtab *,
1332 static void psymtab_to_symtab_1 (struct partial_symtab *);
1334 static struct abbrev_info *abbrev_table_lookup_abbrev
1335 (const struct abbrev_table *, unsigned int);
1337 static struct abbrev_table *abbrev_table_read_table
1338 (struct dwarf2_section_info *, sect_offset);
1340 static void abbrev_table_free (struct abbrev_table *);
1342 static void abbrev_table_free_cleanup (void *);
1344 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1345 struct dwarf2_section_info *);
1347 static void dwarf2_free_abbrev_table (void *);
1349 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1351 static struct partial_die_info *load_partial_dies
1352 (const struct die_reader_specs *, const gdb_byte *, int);
1354 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1355 struct partial_die_info *,
1356 struct abbrev_info *,
1360 static struct partial_die_info *find_partial_die (sect_offset, int,
1361 struct dwarf2_cu *);
1363 static void fixup_partial_die (struct partial_die_info *,
1364 struct dwarf2_cu *);
1366 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1367 struct attribute *, struct attr_abbrev *,
1370 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1372 static int read_1_signed_byte (bfd *, const gdb_byte *);
1374 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1376 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1378 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1380 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1383 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1385 static LONGEST read_checked_initial_length_and_offset
1386 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1387 unsigned int *, unsigned int *);
1389 static LONGEST read_offset (bfd *, const gdb_byte *,
1390 const struct comp_unit_head *,
1393 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1395 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1398 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1400 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1402 static const char *read_indirect_string (bfd *, const gdb_byte *,
1403 const struct comp_unit_head *,
1406 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1408 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1410 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1412 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1416 static const char *read_str_index (const struct die_reader_specs *reader,
1417 struct dwarf2_cu *cu, ULONGEST str_index);
1419 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1421 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1422 struct dwarf2_cu *);
1424 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1427 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1428 struct dwarf2_cu *cu);
1430 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1432 static struct die_info *die_specification (struct die_info *die,
1433 struct dwarf2_cu **);
1435 static void free_line_header (struct line_header *lh);
1437 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1438 struct dwarf2_cu *cu);
1440 static void dwarf_decode_lines (struct line_header *, const char *,
1441 struct dwarf2_cu *, struct partial_symtab *,
1444 static void dwarf2_start_subfile (const char *, const char *, const char *);
1446 static void dwarf2_start_symtab (struct dwarf2_cu *,
1447 const char *, const char *, CORE_ADDR);
1449 static struct symbol *new_symbol (struct die_info *, struct type *,
1450 struct dwarf2_cu *);
1452 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1453 struct dwarf2_cu *, struct symbol *);
1455 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1456 struct dwarf2_cu *);
1458 static void dwarf2_const_value_attr (const struct attribute *attr,
1461 struct obstack *obstack,
1462 struct dwarf2_cu *cu, LONGEST *value,
1463 const gdb_byte **bytes,
1464 struct dwarf2_locexpr_baton **baton);
1466 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1468 static int need_gnat_info (struct dwarf2_cu *);
1470 static struct type *die_descriptive_type (struct die_info *,
1471 struct dwarf2_cu *);
1473 static void set_descriptive_type (struct type *, struct die_info *,
1474 struct dwarf2_cu *);
1476 static struct type *die_containing_type (struct die_info *,
1477 struct dwarf2_cu *);
1479 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1480 struct dwarf2_cu *);
1482 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1484 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1486 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1488 static char *typename_concat (struct obstack *obs, const char *prefix,
1489 const char *suffix, int physname,
1490 struct dwarf2_cu *cu);
1492 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1494 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1496 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1498 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1500 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1502 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1503 struct dwarf2_cu *, struct partial_symtab *);
1505 static int dwarf2_get_pc_bounds (struct die_info *,
1506 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1507 struct partial_symtab *);
1509 static void get_scope_pc_bounds (struct die_info *,
1510 CORE_ADDR *, CORE_ADDR *,
1511 struct dwarf2_cu *);
1513 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1514 CORE_ADDR, struct dwarf2_cu *);
1516 static void dwarf2_add_field (struct field_info *, struct die_info *,
1517 struct dwarf2_cu *);
1519 static void dwarf2_attach_fields_to_type (struct field_info *,
1520 struct type *, struct dwarf2_cu *);
1522 static void dwarf2_add_member_fn (struct field_info *,
1523 struct die_info *, struct type *,
1524 struct dwarf2_cu *);
1526 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1528 struct dwarf2_cu *);
1530 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1532 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1534 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1536 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1538 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1540 static struct type *read_module_type (struct die_info *die,
1541 struct dwarf2_cu *cu);
1543 static const char *namespace_name (struct die_info *die,
1544 int *is_anonymous, struct dwarf2_cu *);
1546 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1548 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1550 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1551 struct dwarf2_cu *);
1553 static struct die_info *read_die_and_siblings_1
1554 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1557 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1558 const gdb_byte *info_ptr,
1559 const gdb_byte **new_info_ptr,
1560 struct die_info *parent);
1562 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1563 struct die_info **, const gdb_byte *,
1566 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1567 struct die_info **, const gdb_byte *,
1570 static void process_die (struct die_info *, struct dwarf2_cu *);
1572 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1575 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1577 static const char *dwarf2_full_name (const char *name,
1578 struct die_info *die,
1579 struct dwarf2_cu *cu);
1581 static const char *dwarf2_physname (const char *name, struct die_info *die,
1582 struct dwarf2_cu *cu);
1584 static struct die_info *dwarf2_extension (struct die_info *die,
1585 struct dwarf2_cu **);
1587 static const char *dwarf_tag_name (unsigned int);
1589 static const char *dwarf_attr_name (unsigned int);
1591 static const char *dwarf_form_name (unsigned int);
1593 static char *dwarf_bool_name (unsigned int);
1595 static const char *dwarf_type_encoding_name (unsigned int);
1597 static struct die_info *sibling_die (struct die_info *);
1599 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1601 static void dump_die_for_error (struct die_info *);
1603 static void dump_die_1 (struct ui_file *, int level, int max_level,
1606 /*static*/ void dump_die (struct die_info *, int max_level);
1608 static void store_in_ref_table (struct die_info *,
1609 struct dwarf2_cu *);
1611 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1613 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1615 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1616 const struct attribute *,
1617 struct dwarf2_cu **);
1619 static struct die_info *follow_die_ref (struct die_info *,
1620 const struct attribute *,
1621 struct dwarf2_cu **);
1623 static struct die_info *follow_die_sig (struct die_info *,
1624 const struct attribute *,
1625 struct dwarf2_cu **);
1627 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1628 struct dwarf2_cu *);
1630 static struct type *get_DW_AT_signature_type (struct die_info *,
1631 const struct attribute *,
1632 struct dwarf2_cu *);
1634 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1636 static void read_signatured_type (struct signatured_type *);
1638 static struct type_unit_group *get_type_unit_group
1639 (struct dwarf2_cu *, const struct attribute *);
1641 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1643 /* memory allocation interface */
1645 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1647 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1649 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1652 static int attr_form_is_block (const struct attribute *);
1654 static int attr_form_is_section_offset (const struct attribute *);
1656 static int attr_form_is_constant (const struct attribute *);
1658 static int attr_form_is_ref (const struct attribute *);
1660 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1661 struct dwarf2_loclist_baton *baton,
1662 const struct attribute *attr);
1664 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1666 struct dwarf2_cu *cu,
1669 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1670 const gdb_byte *info_ptr,
1671 struct abbrev_info *abbrev);
1673 static void free_stack_comp_unit (void *);
1675 static hashval_t partial_die_hash (const void *item);
1677 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1679 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1680 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1682 static void init_one_comp_unit (struct dwarf2_cu *cu,
1683 struct dwarf2_per_cu_data *per_cu);
1685 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1686 struct die_info *comp_unit_die,
1687 enum language pretend_language);
1689 static void free_heap_comp_unit (void *);
1691 static void free_cached_comp_units (void *);
1693 static void age_cached_comp_units (void);
1695 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1697 static struct type *set_die_type (struct die_info *, struct type *,
1698 struct dwarf2_cu *);
1700 static void create_all_comp_units (struct objfile *);
1702 static int create_all_type_units (struct objfile *);
1704 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1707 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1710 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1713 static void dwarf2_add_dependence (struct dwarf2_cu *,
1714 struct dwarf2_per_cu_data *);
1716 static void dwarf2_mark (struct dwarf2_cu *);
1718 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1720 static struct type *get_die_type_at_offset (sect_offset,
1721 struct dwarf2_per_cu_data *);
1723 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1725 static void dwarf2_release_queue (void *dummy);
1727 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1728 enum language pretend_language);
1730 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1731 struct dwarf2_per_cu_data *per_cu,
1732 enum language pretend_language);
1734 static void process_queue (void);
1736 static void find_file_and_directory (struct die_info *die,
1737 struct dwarf2_cu *cu,
1738 const char **name, const char **comp_dir);
1740 static char *file_full_name (int file, struct line_header *lh,
1741 const char *comp_dir);
1743 static const gdb_byte *read_and_check_comp_unit_head
1744 (struct comp_unit_head *header,
1745 struct dwarf2_section_info *section,
1746 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1747 int is_debug_types_section);
1749 static void init_cutu_and_read_dies
1750 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1751 int use_existing_cu, int keep,
1752 die_reader_func_ftype *die_reader_func, void *data);
1754 static void init_cutu_and_read_dies_simple
1755 (struct dwarf2_per_cu_data *this_cu,
1756 die_reader_func_ftype *die_reader_func, void *data);
1758 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1760 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1762 static struct dwo_unit *lookup_dwo_in_dwp
1763 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1764 const char *comp_dir, ULONGEST signature, int is_debug_types);
1766 static struct dwp_file *get_dwp_file (void);
1768 static struct dwo_unit *lookup_dwo_comp_unit
1769 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1771 static struct dwo_unit *lookup_dwo_type_unit
1772 (struct signatured_type *, const char *, const char *);
1774 static void free_dwo_file_cleanup (void *);
1776 static void process_cu_includes (void);
1778 static void check_producer (struct dwarf2_cu *cu);
1782 /* Convert VALUE between big- and little-endian. */
1784 byte_swap (offset_type value)
1788 result = (value & 0xff) << 24;
1789 result |= (value & 0xff00) << 8;
1790 result |= (value & 0xff0000) >> 8;
1791 result |= (value & 0xff000000) >> 24;
1795 #define MAYBE_SWAP(V) byte_swap (V)
1798 #define MAYBE_SWAP(V) (V)
1799 #endif /* WORDS_BIGENDIAN */
1801 /* The suffix for an index file. */
1802 #define INDEX_SUFFIX ".gdb-index"
1804 /* Try to locate the sections we need for DWARF 2 debugging
1805 information and return true if we have enough to do something.
1806 NAMES points to the dwarf2 section names, or is NULL if the standard
1807 ELF names are used. */
1810 dwarf2_has_info (struct objfile *objfile,
1811 const struct dwarf2_debug_sections *names)
1813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1814 if (!dwarf2_per_objfile)
1816 /* Initialize per-objfile state. */
1817 struct dwarf2_per_objfile *data
1818 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1820 memset (data, 0, sizeof (*data));
1821 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1822 dwarf2_per_objfile = data;
1824 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1826 dwarf2_per_objfile->objfile = objfile;
1828 return (dwarf2_per_objfile->info.asection != NULL
1829 && dwarf2_per_objfile->abbrev.asection != NULL);
1832 /* When loading sections, we look either for uncompressed section or for
1833 compressed section names. */
1836 section_is_p (const char *section_name,
1837 const struct dwarf2_section_names *names)
1839 if (names->normal != NULL
1840 && strcmp (section_name, names->normal) == 0)
1842 if (names->compressed != NULL
1843 && strcmp (section_name, names->compressed) == 0)
1848 /* This function is mapped across the sections and remembers the
1849 offset and size of each of the debugging sections we are interested
1853 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1855 const struct dwarf2_debug_sections *names;
1856 flagword aflag = bfd_get_section_flags (abfd, sectp);
1859 names = &dwarf2_elf_names;
1861 names = (const struct dwarf2_debug_sections *) vnames;
1863 if ((aflag & SEC_HAS_CONTENTS) == 0)
1866 else if (section_is_p (sectp->name, &names->info))
1868 dwarf2_per_objfile->info.asection = sectp;
1869 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1871 else if (section_is_p (sectp->name, &names->abbrev))
1873 dwarf2_per_objfile->abbrev.asection = sectp;
1874 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1876 else if (section_is_p (sectp->name, &names->line))
1878 dwarf2_per_objfile->line.asection = sectp;
1879 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1881 else if (section_is_p (sectp->name, &names->loc))
1883 dwarf2_per_objfile->loc.asection = sectp;
1884 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1886 else if (section_is_p (sectp->name, &names->macinfo))
1888 dwarf2_per_objfile->macinfo.asection = sectp;
1889 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1891 else if (section_is_p (sectp->name, &names->macro))
1893 dwarf2_per_objfile->macro.asection = sectp;
1894 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1896 else if (section_is_p (sectp->name, &names->str))
1898 dwarf2_per_objfile->str.asection = sectp;
1899 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1901 else if (section_is_p (sectp->name, &names->addr))
1903 dwarf2_per_objfile->addr.asection = sectp;
1904 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1906 else if (section_is_p (sectp->name, &names->frame))
1908 dwarf2_per_objfile->frame.asection = sectp;
1909 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1911 else if (section_is_p (sectp->name, &names->eh_frame))
1913 dwarf2_per_objfile->eh_frame.asection = sectp;
1914 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1916 else if (section_is_p (sectp->name, &names->ranges))
1918 dwarf2_per_objfile->ranges.asection = sectp;
1919 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1921 else if (section_is_p (sectp->name, &names->types))
1923 struct dwarf2_section_info type_section;
1925 memset (&type_section, 0, sizeof (type_section));
1926 type_section.asection = sectp;
1927 type_section.size = bfd_get_section_size (sectp);
1929 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1932 else if (section_is_p (sectp->name, &names->gdb_index))
1934 dwarf2_per_objfile->gdb_index.asection = sectp;
1935 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1938 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1939 && bfd_section_vma (abfd, sectp) == 0)
1940 dwarf2_per_objfile->has_section_at_zero = 1;
1943 /* A helper function that decides whether a section is empty,
1947 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1949 return info->asection == NULL || info->size == 0;
1952 /* Read the contents of the section INFO.
1953 OBJFILE is the main object file, but not necessarily the file where
1954 the section comes from. E.g., for DWO files INFO->asection->owner
1955 is the bfd of the DWO file.
1956 If the section is compressed, uncompress it before returning. */
1959 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1961 asection *sectp = info->asection;
1963 gdb_byte *buf, *retbuf;
1964 unsigned char header[4];
1968 info->buffer = NULL;
1971 if (dwarf2_section_empty_p (info))
1974 abfd = sectp->owner;
1976 /* If the section has relocations, we must read it ourselves.
1977 Otherwise we attach it to the BFD. */
1978 if ((sectp->flags & SEC_RELOC) == 0)
1980 info->buffer = gdb_bfd_map_section (sectp, &info->size);
1984 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1987 /* When debugging .o files, we may need to apply relocations; see
1988 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1989 We never compress sections in .o files, so we only need to
1990 try this when the section is not compressed. */
1991 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1994 info->buffer = retbuf;
1998 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1999 || bfd_bread (buf, info->size, abfd) != info->size)
2000 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2001 bfd_get_filename (abfd));
2004 /* A helper function that returns the size of a section in a safe way.
2005 If you are positive that the section has been read before using the
2006 size, then it is safe to refer to the dwarf2_section_info object's
2007 "size" field directly. In other cases, you must call this
2008 function, because for compressed sections the size field is not set
2009 correctly until the section has been read. */
2011 static bfd_size_type
2012 dwarf2_section_size (struct objfile *objfile,
2013 struct dwarf2_section_info *info)
2016 dwarf2_read_section (objfile, info);
2020 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2024 dwarf2_get_section_info (struct objfile *objfile,
2025 enum dwarf2_section_enum sect,
2026 asection **sectp, const gdb_byte **bufp,
2027 bfd_size_type *sizep)
2029 struct dwarf2_per_objfile *data
2030 = objfile_data (objfile, dwarf2_objfile_data_key);
2031 struct dwarf2_section_info *info;
2033 /* We may see an objfile without any DWARF, in which case we just
2044 case DWARF2_DEBUG_FRAME:
2045 info = &data->frame;
2047 case DWARF2_EH_FRAME:
2048 info = &data->eh_frame;
2051 gdb_assert_not_reached ("unexpected section");
2054 dwarf2_read_section (objfile, info);
2056 *sectp = info->asection;
2057 *bufp = info->buffer;
2058 *sizep = info->size;
2061 /* A helper function to find the sections for a .dwz file. */
2064 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2066 struct dwz_file *dwz_file = arg;
2068 /* Note that we only support the standard ELF names, because .dwz
2069 is ELF-only (at the time of writing). */
2070 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2072 dwz_file->abbrev.asection = sectp;
2073 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2077 dwz_file->info.asection = sectp;
2078 dwz_file->info.size = bfd_get_section_size (sectp);
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2082 dwz_file->str.asection = sectp;
2083 dwz_file->str.size = bfd_get_section_size (sectp);
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2087 dwz_file->line.asection = sectp;
2088 dwz_file->line.size = bfd_get_section_size (sectp);
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2092 dwz_file->macro.asection = sectp;
2093 dwz_file->macro.size = bfd_get_section_size (sectp);
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2097 dwz_file->gdb_index.asection = sectp;
2098 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2102 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2103 there is no .gnu_debugaltlink section in the file. Error if there
2104 is such a section but the file cannot be found. */
2106 static struct dwz_file *
2107 dwarf2_get_dwz_file (void)
2111 struct cleanup *cleanup;
2112 const char *filename;
2113 struct dwz_file *result;
2114 unsigned long buildid;
2116 if (dwarf2_per_objfile->dwz_file != NULL)
2117 return dwarf2_per_objfile->dwz_file;
2119 bfd_set_error (bfd_error_no_error);
2120 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2124 if (bfd_get_error () == bfd_error_no_error)
2126 error (_("could not read '.gnu_debugaltlink' section: %s"),
2127 bfd_errmsg (bfd_get_error ()));
2129 cleanup = make_cleanup (xfree, data);
2131 filename = (const char *) data;
2132 if (!IS_ABSOLUTE_PATH (filename))
2134 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2137 make_cleanup (xfree, abs);
2138 abs = ldirname (abs);
2139 make_cleanup (xfree, abs);
2141 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2142 make_cleanup (xfree, rel);
2146 /* The format is just a NUL-terminated file name, followed by the
2147 build-id. For now, though, we ignore the build-id. */
2148 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2149 if (dwz_bfd == NULL)
2150 error (_("could not read '%s': %s"), filename,
2151 bfd_errmsg (bfd_get_error ()));
2153 if (!bfd_check_format (dwz_bfd, bfd_object))
2155 gdb_bfd_unref (dwz_bfd);
2156 error (_("file '%s' was not usable: %s"), filename,
2157 bfd_errmsg (bfd_get_error ()));
2160 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2162 result->dwz_bfd = dwz_bfd;
2164 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2166 do_cleanups (cleanup);
2168 dwarf2_per_objfile->dwz_file = result;
2172 /* DWARF quick_symbols_functions support. */
2174 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2175 unique line tables, so we maintain a separate table of all .debug_line
2176 derived entries to support the sharing.
2177 All the quick functions need is the list of file names. We discard the
2178 line_header when we're done and don't need to record it here. */
2179 struct quick_file_names
2181 /* The data used to construct the hash key. */
2182 struct stmt_list_hash hash;
2184 /* The number of entries in file_names, real_names. */
2185 unsigned int num_file_names;
2187 /* The file names from the line table, after being run through
2189 const char **file_names;
2191 /* The file names from the line table after being run through
2192 gdb_realpath. These are computed lazily. */
2193 const char **real_names;
2196 /* When using the index (and thus not using psymtabs), each CU has an
2197 object of this type. This is used to hold information needed by
2198 the various "quick" methods. */
2199 struct dwarf2_per_cu_quick_data
2201 /* The file table. This can be NULL if there was no file table
2202 or it's currently not read in.
2203 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2204 struct quick_file_names *file_names;
2206 /* The corresponding symbol table. This is NULL if symbols for this
2207 CU have not yet been read. */
2208 struct symtab *symtab;
2210 /* A temporary mark bit used when iterating over all CUs in
2211 expand_symtabs_matching. */
2212 unsigned int mark : 1;
2214 /* True if we've tried to read the file table and found there isn't one.
2215 There will be no point in trying to read it again next time. */
2216 unsigned int no_file_data : 1;
2219 /* Utility hash function for a stmt_list_hash. */
2222 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2226 if (stmt_list_hash->dwo_unit != NULL)
2227 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2228 v += stmt_list_hash->line_offset.sect_off;
2232 /* Utility equality function for a stmt_list_hash. */
2235 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2236 const struct stmt_list_hash *rhs)
2238 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2240 if (lhs->dwo_unit != NULL
2241 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2244 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2247 /* Hash function for a quick_file_names. */
2250 hash_file_name_entry (const void *e)
2252 const struct quick_file_names *file_data = e;
2254 return hash_stmt_list_entry (&file_data->hash);
2257 /* Equality function for a quick_file_names. */
2260 eq_file_name_entry (const void *a, const void *b)
2262 const struct quick_file_names *ea = a;
2263 const struct quick_file_names *eb = b;
2265 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2268 /* Delete function for a quick_file_names. */
2271 delete_file_name_entry (void *e)
2273 struct quick_file_names *file_data = e;
2276 for (i = 0; i < file_data->num_file_names; ++i)
2278 xfree ((void*) file_data->file_names[i]);
2279 if (file_data->real_names)
2280 xfree ((void*) file_data->real_names[i]);
2283 /* The space for the struct itself lives on objfile_obstack,
2284 so we don't free it here. */
2287 /* Create a quick_file_names hash table. */
2290 create_quick_file_names_table (unsigned int nr_initial_entries)
2292 return htab_create_alloc (nr_initial_entries,
2293 hash_file_name_entry, eq_file_name_entry,
2294 delete_file_name_entry, xcalloc, xfree);
2297 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2298 have to be created afterwards. You should call age_cached_comp_units after
2299 processing PER_CU->CU. dw2_setup must have been already called. */
2302 load_cu (struct dwarf2_per_cu_data *per_cu)
2304 if (per_cu->is_debug_types)
2305 load_full_type_unit (per_cu);
2307 load_full_comp_unit (per_cu, language_minimal);
2309 gdb_assert (per_cu->cu != NULL);
2311 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2314 /* Read in the symbols for PER_CU. */
2317 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2319 struct cleanup *back_to;
2321 /* Skip type_unit_groups, reading the type units they contain
2322 is handled elsewhere. */
2323 if (IS_TYPE_UNIT_GROUP (per_cu))
2326 back_to = make_cleanup (dwarf2_release_queue, NULL);
2328 if (dwarf2_per_objfile->using_index
2329 ? per_cu->v.quick->symtab == NULL
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2332 queue_comp_unit (per_cu, language_minimal);
2338 /* Age the cache, releasing compilation units that have not
2339 been used recently. */
2340 age_cached_comp_units ();
2342 do_cleanups (back_to);
2345 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2346 the objfile from which this CU came. Returns the resulting symbol
2349 static struct symtab *
2350 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2352 gdb_assert (dwarf2_per_objfile->using_index);
2353 if (!per_cu->v.quick->symtab)
2355 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2356 increment_reading_symtab ();
2357 dw2_do_instantiate_symtab (per_cu);
2358 process_cu_includes ();
2359 do_cleanups (back_to);
2361 return per_cu->v.quick->symtab;
2364 /* Return the CU given its index.
2366 This is intended for loops like:
2368 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2369 + dwarf2_per_objfile->n_type_units); ++i)
2371 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2377 static struct dwarf2_per_cu_data *
2378 dw2_get_cu (int index)
2380 if (index >= dwarf2_per_objfile->n_comp_units)
2382 index -= dwarf2_per_objfile->n_comp_units;
2383 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2384 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2387 return dwarf2_per_objfile->all_comp_units[index];
2390 /* Return the primary CU given its index.
2391 The difference between this function and dw2_get_cu is in the handling
2392 of type units (TUs). Here we return the type_unit_group object.
2394 This is intended for loops like:
2396 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2397 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2399 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2405 static struct dwarf2_per_cu_data *
2406 dw2_get_primary_cu (int index)
2408 if (index >= dwarf2_per_objfile->n_comp_units)
2410 index -= dwarf2_per_objfile->n_comp_units;
2411 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2412 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2415 return dwarf2_per_objfile->all_comp_units[index];
2418 /* A helper for create_cus_from_index that handles a given list of
2422 create_cus_from_index_list (struct objfile *objfile,
2423 const gdb_byte *cu_list, offset_type n_elements,
2424 struct dwarf2_section_info *section,
2430 for (i = 0; i < n_elements; i += 2)
2432 struct dwarf2_per_cu_data *the_cu;
2433 ULONGEST offset, length;
2435 gdb_static_assert (sizeof (ULONGEST) >= 8);
2436 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2437 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2440 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2441 struct dwarf2_per_cu_data);
2442 the_cu->offset.sect_off = offset;
2443 the_cu->length = length;
2444 the_cu->objfile = objfile;
2445 the_cu->section = section;
2446 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2447 struct dwarf2_per_cu_quick_data);
2448 the_cu->is_dwz = is_dwz;
2449 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2453 /* Read the CU list from the mapped index, and use it to create all
2454 the CU objects for this objfile. */
2457 create_cus_from_index (struct objfile *objfile,
2458 const gdb_byte *cu_list, offset_type cu_list_elements,
2459 const gdb_byte *dwz_list, offset_type dwz_elements)
2461 struct dwz_file *dwz;
2463 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2464 dwarf2_per_objfile->all_comp_units
2465 = obstack_alloc (&objfile->objfile_obstack,
2466 dwarf2_per_objfile->n_comp_units
2467 * sizeof (struct dwarf2_per_cu_data *));
2469 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2470 &dwarf2_per_objfile->info, 0, 0);
2472 if (dwz_elements == 0)
2475 dwz = dwarf2_get_dwz_file ();
2476 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2477 cu_list_elements / 2);
2480 /* Create the signatured type hash table from the index. */
2483 create_signatured_type_table_from_index (struct objfile *objfile,
2484 struct dwarf2_section_info *section,
2485 const gdb_byte *bytes,
2486 offset_type elements)
2489 htab_t sig_types_hash;
2491 dwarf2_per_objfile->n_type_units = elements / 3;
2492 dwarf2_per_objfile->all_type_units
2493 = xmalloc (dwarf2_per_objfile->n_type_units
2494 * sizeof (struct signatured_type *));
2496 sig_types_hash = allocate_signatured_type_table (objfile);
2498 for (i = 0; i < elements; i += 3)
2500 struct signatured_type *sig_type;
2501 ULONGEST offset, type_offset_in_tu, signature;
2504 gdb_static_assert (sizeof (ULONGEST) >= 8);
2505 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2506 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2508 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2511 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2512 struct signatured_type);
2513 sig_type->signature = signature;
2514 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2515 sig_type->per_cu.is_debug_types = 1;
2516 sig_type->per_cu.section = section;
2517 sig_type->per_cu.offset.sect_off = offset;
2518 sig_type->per_cu.objfile = objfile;
2519 sig_type->per_cu.v.quick
2520 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2521 struct dwarf2_per_cu_quick_data);
2523 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2526 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2529 dwarf2_per_objfile->signatured_types = sig_types_hash;
2532 /* Read the address map data from the mapped index, and use it to
2533 populate the objfile's psymtabs_addrmap. */
2536 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2538 const gdb_byte *iter, *end;
2539 struct obstack temp_obstack;
2540 struct addrmap *mutable_map;
2541 struct cleanup *cleanup;
2544 obstack_init (&temp_obstack);
2545 cleanup = make_cleanup_obstack_free (&temp_obstack);
2546 mutable_map = addrmap_create_mutable (&temp_obstack);
2548 iter = index->address_table;
2549 end = iter + index->address_table_size;
2551 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2555 ULONGEST hi, lo, cu_index;
2556 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2558 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2560 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2563 if (cu_index < dwarf2_per_objfile->n_comp_units)
2565 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2566 dw2_get_cu (cu_index));
2570 complaint (&symfile_complaints,
2571 _(".gdb_index address table has invalid CU number %u"),
2572 (unsigned) cu_index);
2576 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2577 &objfile->objfile_obstack);
2578 do_cleanups (cleanup);
2581 /* The hash function for strings in the mapped index. This is the same as
2582 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2583 implementation. This is necessary because the hash function is tied to the
2584 format of the mapped index file. The hash values do not have to match with
2587 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2590 mapped_index_string_hash (int index_version, const void *p)
2592 const unsigned char *str = (const unsigned char *) p;
2596 while ((c = *str++) != 0)
2598 if (index_version >= 5)
2600 r = r * 67 + c - 113;
2606 /* Find a slot in the mapped index INDEX for the object named NAME.
2607 If NAME is found, set *VEC_OUT to point to the CU vector in the
2608 constant pool and return 1. If NAME cannot be found, return 0. */
2611 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2612 offset_type **vec_out)
2614 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2616 offset_type slot, step;
2617 int (*cmp) (const char *, const char *);
2619 if (current_language->la_language == language_cplus
2620 || current_language->la_language == language_java
2621 || current_language->la_language == language_fortran)
2623 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2625 const char *paren = strchr (name, '(');
2631 dup = xmalloc (paren - name + 1);
2632 memcpy (dup, name, paren - name);
2633 dup[paren - name] = 0;
2635 make_cleanup (xfree, dup);
2640 /* Index version 4 did not support case insensitive searches. But the
2641 indices for case insensitive languages are built in lowercase, therefore
2642 simulate our NAME being searched is also lowercased. */
2643 hash = mapped_index_string_hash ((index->version == 4
2644 && case_sensitivity == case_sensitive_off
2645 ? 5 : index->version),
2648 slot = hash & (index->symbol_table_slots - 1);
2649 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2650 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2654 /* Convert a slot number to an offset into the table. */
2655 offset_type i = 2 * slot;
2657 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2659 do_cleanups (back_to);
2663 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2664 if (!cmp (name, str))
2666 *vec_out = (offset_type *) (index->constant_pool
2667 + MAYBE_SWAP (index->symbol_table[i + 1]));
2668 do_cleanups (back_to);
2672 slot = (slot + step) & (index->symbol_table_slots - 1);
2676 /* A helper function that reads the .gdb_index from SECTION and fills
2677 in MAP. FILENAME is the name of the file containing the section;
2678 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2679 ok to use deprecated sections.
2681 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2682 out parameters that are filled in with information about the CU and
2683 TU lists in the section.
2685 Returns 1 if all went well, 0 otherwise. */
2688 read_index_from_section (struct objfile *objfile,
2689 const char *filename,
2691 struct dwarf2_section_info *section,
2692 struct mapped_index *map,
2693 const gdb_byte **cu_list,
2694 offset_type *cu_list_elements,
2695 const gdb_byte **types_list,
2696 offset_type *types_list_elements)
2698 const gdb_byte *addr;
2699 offset_type version;
2700 offset_type *metadata;
2703 if (dwarf2_section_empty_p (section))
2706 /* Older elfutils strip versions could keep the section in the main
2707 executable while splitting it for the separate debug info file. */
2708 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2711 dwarf2_read_section (objfile, section);
2713 addr = section->buffer;
2714 /* Version check. */
2715 version = MAYBE_SWAP (*(offset_type *) addr);
2716 /* Versions earlier than 3 emitted every copy of a psymbol. This
2717 causes the index to behave very poorly for certain requests. Version 3
2718 contained incomplete addrmap. So, it seems better to just ignore such
2722 static int warning_printed = 0;
2723 if (!warning_printed)
2725 warning (_("Skipping obsolete .gdb_index section in %s."),
2727 warning_printed = 1;
2731 /* Index version 4 uses a different hash function than index version
2734 Versions earlier than 6 did not emit psymbols for inlined
2735 functions. Using these files will cause GDB not to be able to
2736 set breakpoints on inlined functions by name, so we ignore these
2737 indices unless the user has done
2738 "set use-deprecated-index-sections on". */
2739 if (version < 6 && !deprecated_ok)
2741 static int warning_printed = 0;
2742 if (!warning_printed)
2745 Skipping deprecated .gdb_index section in %s.\n\
2746 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2747 to use the section anyway."),
2749 warning_printed = 1;
2753 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2754 of the TU (for symbols coming from TUs). It's just a performance bug, and
2755 we can't distinguish gdb-generated indices from gold-generated ones, so
2756 nothing to do here. */
2758 /* Indexes with higher version than the one supported by GDB may be no
2759 longer backward compatible. */
2763 map->version = version;
2764 map->total_size = section->size;
2766 metadata = (offset_type *) (addr + sizeof (offset_type));
2769 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2770 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2774 *types_list = addr + MAYBE_SWAP (metadata[i]);
2775 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2776 - MAYBE_SWAP (metadata[i]))
2780 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2781 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2782 - MAYBE_SWAP (metadata[i]));
2785 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2786 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2787 - MAYBE_SWAP (metadata[i]))
2788 / (2 * sizeof (offset_type)));
2791 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2797 /* Read the index file. If everything went ok, initialize the "quick"
2798 elements of all the CUs and return 1. Otherwise, return 0. */
2801 dwarf2_read_index (struct objfile *objfile)
2803 struct mapped_index local_map, *map;
2804 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2805 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2806 struct dwz_file *dwz;
2808 if (!read_index_from_section (objfile, objfile->name,
2809 use_deprecated_index_sections,
2810 &dwarf2_per_objfile->gdb_index, &local_map,
2811 &cu_list, &cu_list_elements,
2812 &types_list, &types_list_elements))
2815 /* Don't use the index if it's empty. */
2816 if (local_map.symbol_table_slots == 0)
2819 /* If there is a .dwz file, read it so we can get its CU list as
2821 dwz = dwarf2_get_dwz_file ();
2824 struct mapped_index dwz_map;
2825 const gdb_byte *dwz_types_ignore;
2826 offset_type dwz_types_elements_ignore;
2828 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2830 &dwz->gdb_index, &dwz_map,
2831 &dwz_list, &dwz_list_elements,
2833 &dwz_types_elements_ignore))
2835 warning (_("could not read '.gdb_index' section from %s; skipping"),
2836 bfd_get_filename (dwz->dwz_bfd));
2841 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2844 if (types_list_elements)
2846 struct dwarf2_section_info *section;
2848 /* We can only handle a single .debug_types when we have an
2850 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2853 section = VEC_index (dwarf2_section_info_def,
2854 dwarf2_per_objfile->types, 0);
2856 create_signatured_type_table_from_index (objfile, section, types_list,
2857 types_list_elements);
2860 create_addrmap_from_index (objfile, &local_map);
2862 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2865 dwarf2_per_objfile->index_table = map;
2866 dwarf2_per_objfile->using_index = 1;
2867 dwarf2_per_objfile->quick_file_names_table =
2868 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2873 /* A helper for the "quick" functions which sets the global
2874 dwarf2_per_objfile according to OBJFILE. */
2877 dw2_setup (struct objfile *objfile)
2879 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2880 gdb_assert (dwarf2_per_objfile);
2883 /* die_reader_func for dw2_get_file_names. */
2886 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2887 const gdb_byte *info_ptr,
2888 struct die_info *comp_unit_die,
2892 struct dwarf2_cu *cu = reader->cu;
2893 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2894 struct objfile *objfile = dwarf2_per_objfile->objfile;
2895 struct dwarf2_per_cu_data *lh_cu;
2896 struct line_header *lh;
2897 struct attribute *attr;
2899 const char *name, *comp_dir;
2901 struct quick_file_names *qfn;
2902 unsigned int line_offset;
2904 gdb_assert (! this_cu->is_debug_types);
2906 /* Our callers never want to match partial units -- instead they
2907 will match the enclosing full CU. */
2908 if (comp_unit_die->tag == DW_TAG_partial_unit)
2910 this_cu->v.quick->no_file_data = 1;
2919 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2922 struct quick_file_names find_entry;
2924 line_offset = DW_UNSND (attr);
2926 /* We may have already read in this line header (TU line header sharing).
2927 If we have we're done. */
2928 find_entry.hash.dwo_unit = cu->dwo_unit;
2929 find_entry.hash.line_offset.sect_off = line_offset;
2930 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2931 &find_entry, INSERT);
2934 lh_cu->v.quick->file_names = *slot;
2938 lh = dwarf_decode_line_header (line_offset, cu);
2942 lh_cu->v.quick->no_file_data = 1;
2946 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2947 qfn->hash.dwo_unit = cu->dwo_unit;
2948 qfn->hash.line_offset.sect_off = line_offset;
2949 gdb_assert (slot != NULL);
2952 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2954 qfn->num_file_names = lh->num_file_names;
2955 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2956 lh->num_file_names * sizeof (char *));
2957 for (i = 0; i < lh->num_file_names; ++i)
2958 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2959 qfn->real_names = NULL;
2961 free_line_header (lh);
2963 lh_cu->v.quick->file_names = qfn;
2966 /* A helper for the "quick" functions which attempts to read the line
2967 table for THIS_CU. */
2969 static struct quick_file_names *
2970 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2972 /* This should never be called for TUs. */
2973 gdb_assert (! this_cu->is_debug_types);
2974 /* Nor type unit groups. */
2975 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
2977 if (this_cu->v.quick->file_names != NULL)
2978 return this_cu->v.quick->file_names;
2979 /* If we know there is no line data, no point in looking again. */
2980 if (this_cu->v.quick->no_file_data)
2983 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2985 if (this_cu->v.quick->no_file_data)
2987 return this_cu->v.quick->file_names;
2990 /* A helper for the "quick" functions which computes and caches the
2991 real path for a given file name from the line table. */
2994 dw2_get_real_path (struct objfile *objfile,
2995 struct quick_file_names *qfn, int index)
2997 if (qfn->real_names == NULL)
2998 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2999 qfn->num_file_names, sizeof (char *));
3001 if (qfn->real_names[index] == NULL)
3002 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3004 return qfn->real_names[index];
3007 static struct symtab *
3008 dw2_find_last_source_symtab (struct objfile *objfile)
3012 dw2_setup (objfile);
3013 index = dwarf2_per_objfile->n_comp_units - 1;
3014 return dw2_instantiate_symtab (dw2_get_cu (index));
3017 /* Traversal function for dw2_forget_cached_source_info. */
3020 dw2_free_cached_file_names (void **slot, void *info)
3022 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3024 if (file_data->real_names)
3028 for (i = 0; i < file_data->num_file_names; ++i)
3030 xfree ((void*) file_data->real_names[i]);
3031 file_data->real_names[i] = NULL;
3039 dw2_forget_cached_source_info (struct objfile *objfile)
3041 dw2_setup (objfile);
3043 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3044 dw2_free_cached_file_names, NULL);
3047 /* Helper function for dw2_map_symtabs_matching_filename that expands
3048 the symtabs and calls the iterator. */
3051 dw2_map_expand_apply (struct objfile *objfile,
3052 struct dwarf2_per_cu_data *per_cu,
3053 const char *name, const char *real_path,
3054 int (*callback) (struct symtab *, void *),
3057 struct symtab *last_made = objfile->symtabs;
3059 /* Don't visit already-expanded CUs. */
3060 if (per_cu->v.quick->symtab)
3063 /* This may expand more than one symtab, and we want to iterate over
3065 dw2_instantiate_symtab (per_cu);
3067 return iterate_over_some_symtabs (name, real_path, callback, data,
3068 objfile->symtabs, last_made);
3071 /* Implementation of the map_symtabs_matching_filename method. */
3074 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3075 const char *real_path,
3076 int (*callback) (struct symtab *, void *),
3080 const char *name_basename = lbasename (name);
3082 dw2_setup (objfile);
3084 /* The rule is CUs specify all the files, including those used by
3085 any TU, so there's no need to scan TUs here. */
3087 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3090 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3091 struct quick_file_names *file_data;
3093 /* We only need to look at symtabs not already expanded. */
3094 if (per_cu->v.quick->symtab)
3097 file_data = dw2_get_file_names (per_cu);
3098 if (file_data == NULL)
3101 for (j = 0; j < file_data->num_file_names; ++j)
3103 const char *this_name = file_data->file_names[j];
3104 const char *this_real_name;
3106 if (compare_filenames_for_search (this_name, name))
3108 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3114 /* Before we invoke realpath, which can get expensive when many
3115 files are involved, do a quick comparison of the basenames. */
3116 if (! basenames_may_differ
3117 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3120 this_real_name = dw2_get_real_path (objfile, file_data, j);
3121 if (compare_filenames_for_search (this_real_name, name))
3123 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3129 if (real_path != NULL)
3131 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3132 gdb_assert (IS_ABSOLUTE_PATH (name));
3133 if (this_real_name != NULL
3134 && FILENAME_CMP (real_path, this_real_name) == 0)
3136 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3148 /* Struct used to manage iterating over all CUs looking for a symbol. */
3150 struct dw2_symtab_iterator
3152 /* The internalized form of .gdb_index. */
3153 struct mapped_index *index;
3154 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3155 int want_specific_block;
3156 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3157 Unused if !WANT_SPECIFIC_BLOCK. */
3159 /* The kind of symbol we're looking for. */
3161 /* The list of CUs from the index entry of the symbol,
3162 or NULL if not found. */
3164 /* The next element in VEC to look at. */
3166 /* The number of elements in VEC, or zero if there is no match. */
3170 /* Initialize the index symtab iterator ITER.
3171 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3172 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3175 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3176 struct mapped_index *index,
3177 int want_specific_block,
3182 iter->index = index;
3183 iter->want_specific_block = want_specific_block;
3184 iter->block_index = block_index;
3185 iter->domain = domain;
3188 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3189 iter->length = MAYBE_SWAP (*iter->vec);
3197 /* Return the next matching CU or NULL if there are no more. */
3199 static struct dwarf2_per_cu_data *
3200 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3202 for ( ; iter->next < iter->length; ++iter->next)
3204 offset_type cu_index_and_attrs =
3205 MAYBE_SWAP (iter->vec[iter->next + 1]);
3206 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3207 struct dwarf2_per_cu_data *per_cu;
3208 int want_static = iter->block_index != GLOBAL_BLOCK;
3209 /* This value is only valid for index versions >= 7. */
3210 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3211 gdb_index_symbol_kind symbol_kind =
3212 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3213 /* Only check the symbol attributes if they're present.
3214 Indices prior to version 7 don't record them,
3215 and indices >= 7 may elide them for certain symbols
3216 (gold does this). */
3218 (iter->index->version >= 7
3219 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3221 /* Don't crash on bad data. */
3222 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3223 + dwarf2_per_objfile->n_type_units))
3225 complaint (&symfile_complaints,
3226 _(".gdb_index entry has bad CU index"
3227 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3231 per_cu = dw2_get_cu (cu_index);
3233 /* Skip if already read in. */
3234 if (per_cu->v.quick->symtab)
3238 && iter->want_specific_block
3239 && want_static != is_static)
3242 /* Only check the symbol's kind if it has one. */
3245 switch (iter->domain)
3248 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3249 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3250 /* Some types are also in VAR_DOMAIN. */
3251 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3255 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3259 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3274 static struct symtab *
3275 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3276 const char *name, domain_enum domain)
3278 struct symtab *stab_best = NULL;
3279 struct mapped_index *index;
3281 dw2_setup (objfile);
3283 index = dwarf2_per_objfile->index_table;
3285 /* index is NULL if OBJF_READNOW. */
3288 struct dw2_symtab_iterator iter;
3289 struct dwarf2_per_cu_data *per_cu;
3291 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3293 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3295 struct symbol *sym = NULL;
3296 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3298 /* Some caution must be observed with overloaded functions
3299 and methods, since the index will not contain any overload
3300 information (but NAME might contain it). */
3303 struct blockvector *bv = BLOCKVECTOR (stab);
3304 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3306 sym = lookup_block_symbol (block, name, domain);
3309 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3311 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3317 /* Keep looking through other CUs. */
3325 dw2_print_stats (struct objfile *objfile)
3327 int i, total, count;
3329 dw2_setup (objfile);
3330 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3332 for (i = 0; i < total; ++i)
3334 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3336 if (!per_cu->v.quick->symtab)
3339 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3340 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3344 dw2_dump (struct objfile *objfile)
3346 /* Nothing worth printing. */
3350 dw2_relocate (struct objfile *objfile,
3351 const struct section_offsets *new_offsets,
3352 const struct section_offsets *delta)
3354 /* There's nothing to relocate here. */
3358 dw2_expand_symtabs_for_function (struct objfile *objfile,
3359 const char *func_name)
3361 struct mapped_index *index;
3363 dw2_setup (objfile);
3365 index = dwarf2_per_objfile->index_table;
3367 /* index is NULL if OBJF_READNOW. */
3370 struct dw2_symtab_iterator iter;
3371 struct dwarf2_per_cu_data *per_cu;
3373 /* Note: It doesn't matter what we pass for block_index here. */
3374 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3377 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3378 dw2_instantiate_symtab (per_cu);
3383 dw2_expand_all_symtabs (struct objfile *objfile)
3387 dw2_setup (objfile);
3389 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3390 + dwarf2_per_objfile->n_type_units); ++i)
3392 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3394 dw2_instantiate_symtab (per_cu);
3399 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3400 const char *fullname)
3404 dw2_setup (objfile);
3406 /* We don't need to consider type units here.
3407 This is only called for examining code, e.g. expand_line_sal.
3408 There can be an order of magnitude (or more) more type units
3409 than comp units, and we avoid them if we can. */
3411 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3414 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3415 struct quick_file_names *file_data;
3417 /* We only need to look at symtabs not already expanded. */
3418 if (per_cu->v.quick->symtab)
3421 file_data = dw2_get_file_names (per_cu);
3422 if (file_data == NULL)
3425 for (j = 0; j < file_data->num_file_names; ++j)
3427 const char *this_fullname = file_data->file_names[j];
3429 if (filename_cmp (this_fullname, fullname) == 0)
3431 dw2_instantiate_symtab (per_cu);
3438 /* A helper function for dw2_find_symbol_file that finds the primary
3439 file name for a given CU. This is a die_reader_func. */
3442 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3443 const gdb_byte *info_ptr,
3444 struct die_info *comp_unit_die,
3448 const char **result_ptr = data;
3449 struct dwarf2_cu *cu = reader->cu;
3450 struct attribute *attr;
3452 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3456 *result_ptr = DW_STRING (attr);
3460 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3462 struct dwarf2_per_cu_data *per_cu;
3464 const char *filename;
3466 dw2_setup (objfile);
3468 /* index_table is NULL if OBJF_READNOW. */
3469 if (!dwarf2_per_objfile->index_table)
3473 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3475 struct blockvector *bv = BLOCKVECTOR (s);
3476 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3477 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3481 /* Only file extension of returned filename is recognized. */
3482 return SYMBOL_SYMTAB (sym)->filename;
3488 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3492 /* Note that this just looks at the very first one named NAME -- but
3493 actually we are looking for a function. find_main_filename
3494 should be rewritten so that it doesn't require a custom hook. It
3495 could just use the ordinary symbol tables. */
3496 /* vec[0] is the length, which must always be >0. */
3497 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3499 if (per_cu->v.quick->symtab != NULL)
3501 /* Only file extension of returned filename is recognized. */
3502 return per_cu->v.quick->symtab->filename;
3505 /* Initialize filename in case there's a problem reading the DWARF,
3506 dw2_get_primary_filename_reader may not get called. */
3508 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3509 dw2_get_primary_filename_reader, &filename);
3511 /* Only file extension of returned filename is recognized. */
3516 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3517 struct objfile *objfile, int global,
3518 int (*callback) (struct block *,
3519 struct symbol *, void *),
3520 void *data, symbol_compare_ftype *match,
3521 symbol_compare_ftype *ordered_compare)
3523 /* Currently unimplemented; used for Ada. The function can be called if the
3524 current language is Ada for a non-Ada objfile using GNU index. As Ada
3525 does not look for non-Ada symbols this function should just return. */
3529 dw2_expand_symtabs_matching
3530 (struct objfile *objfile,
3531 int (*file_matcher) (const char *, void *, int basenames),
3532 int (*name_matcher) (const char *, void *),
3533 enum search_domain kind,
3538 struct mapped_index *index;
3540 dw2_setup (objfile);
3542 /* index_table is NULL if OBJF_READNOW. */
3543 if (!dwarf2_per_objfile->index_table)
3545 index = dwarf2_per_objfile->index_table;
3547 if (file_matcher != NULL)
3549 struct cleanup *cleanup;
3550 htab_t visited_found, visited_not_found;
3552 visited_found = htab_create_alloc (10,
3553 htab_hash_pointer, htab_eq_pointer,
3554 NULL, xcalloc, xfree);
3555 cleanup = make_cleanup_htab_delete (visited_found);
3556 visited_not_found = htab_create_alloc (10,
3557 htab_hash_pointer, htab_eq_pointer,
3558 NULL, xcalloc, xfree);
3559 make_cleanup_htab_delete (visited_not_found);
3561 /* The rule is CUs specify all the files, including those used by
3562 any TU, so there's no need to scan TUs here. */
3564 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3567 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3568 struct quick_file_names *file_data;
3571 per_cu->v.quick->mark = 0;
3573 /* We only need to look at symtabs not already expanded. */
3574 if (per_cu->v.quick->symtab)
3577 file_data = dw2_get_file_names (per_cu);
3578 if (file_data == NULL)
3581 if (htab_find (visited_not_found, file_data) != NULL)
3583 else if (htab_find (visited_found, file_data) != NULL)
3585 per_cu->v.quick->mark = 1;
3589 for (j = 0; j < file_data->num_file_names; ++j)
3591 const char *this_real_name;
3593 if (file_matcher (file_data->file_names[j], data, 0))
3595 per_cu->v.quick->mark = 1;
3599 /* Before we invoke realpath, which can get expensive when many
3600 files are involved, do a quick comparison of the basenames. */
3601 if (!basenames_may_differ
3602 && !file_matcher (lbasename (file_data->file_names[j]),
3606 this_real_name = dw2_get_real_path (objfile, file_data, j);
3607 if (file_matcher (this_real_name, data, 0))
3609 per_cu->v.quick->mark = 1;
3614 slot = htab_find_slot (per_cu->v.quick->mark
3616 : visited_not_found,
3621 do_cleanups (cleanup);
3624 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3626 offset_type idx = 2 * iter;
3628 offset_type *vec, vec_len, vec_idx;
3630 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3633 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3635 if (! (*name_matcher) (name, data))
3638 /* The name was matched, now expand corresponding CUs that were
3640 vec = (offset_type *) (index->constant_pool
3641 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3642 vec_len = MAYBE_SWAP (vec[0]);
3643 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3645 struct dwarf2_per_cu_data *per_cu;
3646 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3647 gdb_index_symbol_kind symbol_kind =
3648 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3649 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3650 /* Only check the symbol attributes if they're present.
3651 Indices prior to version 7 don't record them,
3652 and indices >= 7 may elide them for certain symbols
3653 (gold does this). */
3655 (index->version >= 7
3656 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3658 /* Only check the symbol's kind if it has one. */
3663 case VARIABLES_DOMAIN:
3664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3667 case FUNCTIONS_DOMAIN:
3668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3680 /* Don't crash on bad data. */
3681 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3682 + dwarf2_per_objfile->n_type_units))
3684 complaint (&symfile_complaints,
3685 _(".gdb_index entry has bad CU index"
3686 " [in module %s]"), objfile->name);
3690 per_cu = dw2_get_cu (cu_index);
3691 if (file_matcher == NULL || per_cu->v.quick->mark)
3692 dw2_instantiate_symtab (per_cu);
3697 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3700 static struct symtab *
3701 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3705 if (BLOCKVECTOR (symtab) != NULL
3706 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3709 if (symtab->includes == NULL)
3712 for (i = 0; symtab->includes[i]; ++i)
3714 struct symtab *s = symtab->includes[i];
3716 s = recursively_find_pc_sect_symtab (s, pc);
3724 static struct symtab *
3725 dw2_find_pc_sect_symtab (struct objfile *objfile,
3726 struct minimal_symbol *msymbol,
3728 struct obj_section *section,
3731 struct dwarf2_per_cu_data *data;
3732 struct symtab *result;
3734 dw2_setup (objfile);
3736 if (!objfile->psymtabs_addrmap)
3739 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3743 if (warn_if_readin && data->v.quick->symtab)
3744 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3745 paddress (get_objfile_arch (objfile), pc));
3747 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3748 gdb_assert (result != NULL);
3753 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3754 void *data, int need_fullname)
3757 struct cleanup *cleanup;
3758 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3759 NULL, xcalloc, xfree);
3761 cleanup = make_cleanup_htab_delete (visited);
3762 dw2_setup (objfile);
3764 /* The rule is CUs specify all the files, including those used by
3765 any TU, so there's no need to scan TUs here.
3766 We can ignore file names coming from already-expanded CUs. */
3768 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3770 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3772 if (per_cu->v.quick->symtab)
3774 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3777 *slot = per_cu->v.quick->file_names;
3781 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3784 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3785 struct quick_file_names *file_data;
3788 /* We only need to look at symtabs not already expanded. */
3789 if (per_cu->v.quick->symtab)
3792 file_data = dw2_get_file_names (per_cu);
3793 if (file_data == NULL)
3796 slot = htab_find_slot (visited, file_data, INSERT);
3799 /* Already visited. */
3804 for (j = 0; j < file_data->num_file_names; ++j)
3806 const char *this_real_name;
3809 this_real_name = dw2_get_real_path (objfile, file_data, j);
3811 this_real_name = NULL;
3812 (*fun) (file_data->file_names[j], this_real_name, data);
3816 do_cleanups (cleanup);
3820 dw2_has_symbols (struct objfile *objfile)
3825 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3828 dw2_find_last_source_symtab,
3829 dw2_forget_cached_source_info,
3830 dw2_map_symtabs_matching_filename,
3835 dw2_expand_symtabs_for_function,
3836 dw2_expand_all_symtabs,
3837 dw2_expand_symtabs_with_fullname,
3838 dw2_find_symbol_file,
3839 dw2_map_matching_symbols,
3840 dw2_expand_symtabs_matching,
3841 dw2_find_pc_sect_symtab,
3842 dw2_map_symbol_filenames
3845 /* Initialize for reading DWARF for this objfile. Return 0 if this
3846 file will use psymtabs, or 1 if using the GNU index. */
3849 dwarf2_initialize_objfile (struct objfile *objfile)
3851 /* If we're about to read full symbols, don't bother with the
3852 indices. In this case we also don't care if some other debug
3853 format is making psymtabs, because they are all about to be
3855 if ((objfile->flags & OBJF_READNOW))
3859 dwarf2_per_objfile->using_index = 1;
3860 create_all_comp_units (objfile);
3861 create_all_type_units (objfile);
3862 dwarf2_per_objfile->quick_file_names_table =
3863 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3865 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3866 + dwarf2_per_objfile->n_type_units); ++i)
3868 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3870 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3871 struct dwarf2_per_cu_quick_data);
3874 /* Return 1 so that gdb sees the "quick" functions. However,
3875 these functions will be no-ops because we will have expanded
3880 if (dwarf2_read_index (objfile))
3888 /* Build a partial symbol table. */
3891 dwarf2_build_psymtabs (struct objfile *objfile)
3893 volatile struct gdb_exception except;
3895 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3897 init_psymbol_list (objfile, 1024);
3900 TRY_CATCH (except, RETURN_MASK_ERROR)
3902 /* This isn't really ideal: all the data we allocate on the
3903 objfile's obstack is still uselessly kept around. However,
3904 freeing it seems unsafe. */
3905 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3907 dwarf2_build_psymtabs_hard (objfile);
3908 discard_cleanups (cleanups);
3910 if (except.reason < 0)
3911 exception_print (gdb_stderr, except);
3914 /* Return the total length of the CU described by HEADER. */
3917 get_cu_length (const struct comp_unit_head *header)
3919 return header->initial_length_size + header->length;
3922 /* Return TRUE if OFFSET is within CU_HEADER. */
3925 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3927 sect_offset bottom = { cu_header->offset.sect_off };
3928 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3930 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3933 /* Find the base address of the compilation unit for range lists and
3934 location lists. It will normally be specified by DW_AT_low_pc.
3935 In DWARF-3 draft 4, the base address could be overridden by
3936 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3937 compilation units with discontinuous ranges. */
3940 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3942 struct attribute *attr;
3945 cu->base_address = 0;
3947 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3950 cu->base_address = DW_ADDR (attr);
3955 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3958 cu->base_address = DW_ADDR (attr);
3964 /* Read in the comp unit header information from the debug_info at info_ptr.
3965 NOTE: This leaves members offset, first_die_offset to be filled in
3968 static const gdb_byte *
3969 read_comp_unit_head (struct comp_unit_head *cu_header,
3970 const gdb_byte *info_ptr, bfd *abfd)
3973 unsigned int bytes_read;
3975 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3976 cu_header->initial_length_size = bytes_read;
3977 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3978 info_ptr += bytes_read;
3979 cu_header->version = read_2_bytes (abfd, info_ptr);
3981 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3983 info_ptr += bytes_read;
3984 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3986 signed_addr = bfd_get_sign_extend_vma (abfd);
3987 if (signed_addr < 0)
3988 internal_error (__FILE__, __LINE__,
3989 _("read_comp_unit_head: dwarf from non elf file"));
3990 cu_header->signed_addr_p = signed_addr;
3995 /* Helper function that returns the proper abbrev section for
3998 static struct dwarf2_section_info *
3999 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4001 struct dwarf2_section_info *abbrev;
4003 if (this_cu->is_dwz)
4004 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4006 abbrev = &dwarf2_per_objfile->abbrev;
4011 /* Subroutine of read_and_check_comp_unit_head and
4012 read_and_check_type_unit_head to simplify them.
4013 Perform various error checking on the header. */
4016 error_check_comp_unit_head (struct comp_unit_head *header,
4017 struct dwarf2_section_info *section,
4018 struct dwarf2_section_info *abbrev_section)
4020 bfd *abfd = section->asection->owner;
4021 const char *filename = bfd_get_filename (abfd);
4023 if (header->version != 2 && header->version != 3 && header->version != 4)
4024 error (_("Dwarf Error: wrong version in compilation unit header "
4025 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4028 if (header->abbrev_offset.sect_off
4029 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4030 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4031 "(offset 0x%lx + 6) [in module %s]"),
4032 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4035 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4036 avoid potential 32-bit overflow. */
4037 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4039 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4040 "(offset 0x%lx + 0) [in module %s]"),
4041 (long) header->length, (long) header->offset.sect_off,
4045 /* Read in a CU/TU header and perform some basic error checking.
4046 The contents of the header are stored in HEADER.
4047 The result is a pointer to the start of the first DIE. */
4049 static const gdb_byte *
4050 read_and_check_comp_unit_head (struct comp_unit_head *header,
4051 struct dwarf2_section_info *section,
4052 struct dwarf2_section_info *abbrev_section,
4053 const gdb_byte *info_ptr,
4054 int is_debug_types_section)
4056 const gdb_byte *beg_of_comp_unit = info_ptr;
4057 bfd *abfd = section->asection->owner;
4059 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4061 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4063 /* If we're reading a type unit, skip over the signature and
4064 type_offset fields. */
4065 if (is_debug_types_section)
4066 info_ptr += 8 /*signature*/ + header->offset_size;
4068 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4070 error_check_comp_unit_head (header, section, abbrev_section);
4075 /* Read in the types comp unit header information from .debug_types entry at
4076 types_ptr. The result is a pointer to one past the end of the header. */
4078 static const gdb_byte *
4079 read_and_check_type_unit_head (struct comp_unit_head *header,
4080 struct dwarf2_section_info *section,
4081 struct dwarf2_section_info *abbrev_section,
4082 const gdb_byte *info_ptr,
4083 ULONGEST *signature,
4084 cu_offset *type_offset_in_tu)
4086 const gdb_byte *beg_of_comp_unit = info_ptr;
4087 bfd *abfd = section->asection->owner;
4089 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4091 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4093 /* If we're reading a type unit, skip over the signature and
4094 type_offset fields. */
4095 if (signature != NULL)
4096 *signature = read_8_bytes (abfd, info_ptr);
4098 if (type_offset_in_tu != NULL)
4099 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4100 header->offset_size);
4101 info_ptr += header->offset_size;
4103 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4105 error_check_comp_unit_head (header, section, abbrev_section);
4110 /* Fetch the abbreviation table offset from a comp or type unit header. */
4113 read_abbrev_offset (struct dwarf2_section_info *section,
4116 bfd *abfd = section->asection->owner;
4117 const gdb_byte *info_ptr;
4118 unsigned int length, initial_length_size, offset_size;
4119 sect_offset abbrev_offset;
4121 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4122 info_ptr = section->buffer + offset.sect_off;
4123 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4124 offset_size = initial_length_size == 4 ? 4 : 8;
4125 info_ptr += initial_length_size + 2 /*version*/;
4126 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4127 return abbrev_offset;
4130 /* Allocate a new partial symtab for file named NAME and mark this new
4131 partial symtab as being an include of PST. */
4134 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4135 struct objfile *objfile)
4137 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4139 if (!IS_ABSOLUTE_PATH (subpst->filename))
4141 /* It shares objfile->objfile_obstack. */
4142 subpst->dirname = pst->dirname;
4145 subpst->section_offsets = pst->section_offsets;
4146 subpst->textlow = 0;
4147 subpst->texthigh = 0;
4149 subpst->dependencies = (struct partial_symtab **)
4150 obstack_alloc (&objfile->objfile_obstack,
4151 sizeof (struct partial_symtab *));
4152 subpst->dependencies[0] = pst;
4153 subpst->number_of_dependencies = 1;
4155 subpst->globals_offset = 0;
4156 subpst->n_global_syms = 0;
4157 subpst->statics_offset = 0;
4158 subpst->n_static_syms = 0;
4159 subpst->symtab = NULL;
4160 subpst->read_symtab = pst->read_symtab;
4163 /* No private part is necessary for include psymtabs. This property
4164 can be used to differentiate between such include psymtabs and
4165 the regular ones. */
4166 subpst->read_symtab_private = NULL;
4169 /* Read the Line Number Program data and extract the list of files
4170 included by the source file represented by PST. Build an include
4171 partial symtab for each of these included files. */
4174 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4175 struct die_info *die,
4176 struct partial_symtab *pst)
4178 struct line_header *lh = NULL;
4179 struct attribute *attr;
4181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4183 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4185 return; /* No linetable, so no includes. */
4187 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4188 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4190 free_line_header (lh);
4194 hash_signatured_type (const void *item)
4196 const struct signatured_type *sig_type = item;
4198 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4199 return sig_type->signature;
4203 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4205 const struct signatured_type *lhs = item_lhs;
4206 const struct signatured_type *rhs = item_rhs;
4208 return lhs->signature == rhs->signature;
4211 /* Allocate a hash table for signatured types. */
4214 allocate_signatured_type_table (struct objfile *objfile)
4216 return htab_create_alloc_ex (41,
4217 hash_signatured_type,
4220 &objfile->objfile_obstack,
4221 hashtab_obstack_allocate,
4222 dummy_obstack_deallocate);
4225 /* A helper function to add a signatured type CU to a table. */
4228 add_signatured_type_cu_to_table (void **slot, void *datum)
4230 struct signatured_type *sigt = *slot;
4231 struct signatured_type ***datap = datum;
4239 /* Create the hash table of all entries in the .debug_types
4240 (or .debug_types.dwo) section(s).
4241 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4242 otherwise it is NULL.
4244 The result is a pointer to the hash table or NULL if there are no types.
4246 Note: This function processes DWO files only, not DWP files. */
4249 create_debug_types_hash_table (struct dwo_file *dwo_file,
4250 VEC (dwarf2_section_info_def) *types)
4252 struct objfile *objfile = dwarf2_per_objfile->objfile;
4253 htab_t types_htab = NULL;
4255 struct dwarf2_section_info *section;
4256 struct dwarf2_section_info *abbrev_section;
4258 if (VEC_empty (dwarf2_section_info_def, types))
4261 abbrev_section = (dwo_file != NULL
4262 ? &dwo_file->sections.abbrev
4263 : &dwarf2_per_objfile->abbrev);
4265 if (dwarf2_read_debug)
4266 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4267 dwo_file ? ".dwo" : "",
4268 bfd_get_filename (abbrev_section->asection->owner));
4271 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4275 const gdb_byte *info_ptr, *end_ptr;
4276 struct dwarf2_section_info *abbrev_section;
4278 dwarf2_read_section (objfile, section);
4279 info_ptr = section->buffer;
4281 if (info_ptr == NULL)
4284 /* We can't set abfd until now because the section may be empty or
4285 not present, in which case section->asection will be NULL. */
4286 abfd = section->asection->owner;
4289 abbrev_section = &dwo_file->sections.abbrev;
4291 abbrev_section = &dwarf2_per_objfile->abbrev;
4293 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4294 because we don't need to read any dies: the signature is in the
4297 end_ptr = info_ptr + section->size;
4298 while (info_ptr < end_ptr)
4301 cu_offset type_offset_in_tu;
4303 struct signatured_type *sig_type;
4304 struct dwo_unit *dwo_tu;
4306 const gdb_byte *ptr = info_ptr;
4307 struct comp_unit_head header;
4308 unsigned int length;
4310 offset.sect_off = ptr - section->buffer;
4312 /* We need to read the type's signature in order to build the hash
4313 table, but we don't need anything else just yet. */
4315 ptr = read_and_check_type_unit_head (&header, section,
4316 abbrev_section, ptr,
4317 &signature, &type_offset_in_tu);
4319 length = get_cu_length (&header);
4321 /* Skip dummy type units. */
4322 if (ptr >= info_ptr + length
4323 || peek_abbrev_code (abfd, ptr) == 0)
4329 if (types_htab == NULL)
4332 types_htab = allocate_dwo_unit_table (objfile);
4334 types_htab = allocate_signatured_type_table (objfile);
4340 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4342 dwo_tu->dwo_file = dwo_file;
4343 dwo_tu->signature = signature;
4344 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4345 dwo_tu->section = section;
4346 dwo_tu->offset = offset;
4347 dwo_tu->length = length;
4351 /* N.B.: type_offset is not usable if this type uses a DWO file.
4352 The real type_offset is in the DWO file. */
4354 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4355 struct signatured_type);
4356 sig_type->signature = signature;
4357 sig_type->type_offset_in_tu = type_offset_in_tu;
4358 sig_type->per_cu.objfile = objfile;
4359 sig_type->per_cu.is_debug_types = 1;
4360 sig_type->per_cu.section = section;
4361 sig_type->per_cu.offset = offset;
4362 sig_type->per_cu.length = length;
4365 slot = htab_find_slot (types_htab,
4366 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4368 gdb_assert (slot != NULL);
4371 sect_offset dup_offset;
4375 const struct dwo_unit *dup_tu = *slot;
4377 dup_offset = dup_tu->offset;
4381 const struct signatured_type *dup_tu = *slot;
4383 dup_offset = dup_tu->per_cu.offset;
4386 complaint (&symfile_complaints,
4387 _("debug type entry at offset 0x%x is duplicate to"
4388 " the entry at offset 0x%x, signature %s"),
4389 offset.sect_off, dup_offset.sect_off,
4390 hex_string (signature));
4392 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4394 if (dwarf2_read_debug)
4395 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4397 hex_string (signature));
4406 /* Create the hash table of all entries in the .debug_types section,
4407 and initialize all_type_units.
4408 The result is zero if there is an error (e.g. missing .debug_types section),
4409 otherwise non-zero. */
4412 create_all_type_units (struct objfile *objfile)
4415 struct signatured_type **iter;
4417 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4418 if (types_htab == NULL)
4420 dwarf2_per_objfile->signatured_types = NULL;
4424 dwarf2_per_objfile->signatured_types = types_htab;
4426 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4427 dwarf2_per_objfile->all_type_units
4428 = xmalloc (dwarf2_per_objfile->n_type_units
4429 * sizeof (struct signatured_type *));
4430 iter = &dwarf2_per_objfile->all_type_units[0];
4431 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4432 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4433 == dwarf2_per_objfile->n_type_units);
4438 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4439 Fill in SIG_ENTRY with DWO_ENTRY. */
4442 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4443 struct signatured_type *sig_entry,
4444 struct dwo_unit *dwo_entry)
4446 sig_entry->per_cu.section = dwo_entry->section;
4447 sig_entry->per_cu.offset = dwo_entry->offset;
4448 sig_entry->per_cu.length = dwo_entry->length;
4449 sig_entry->per_cu.reading_dwo_directly = 1;
4450 sig_entry->per_cu.objfile = objfile;
4451 gdb_assert (! sig_entry->per_cu.queued);
4452 gdb_assert (sig_entry->per_cu.cu == NULL);
4453 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4454 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4455 gdb_assert (sig_entry->signature == dwo_entry->signature);
4456 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4457 gdb_assert (sig_entry->type_unit_group == NULL);
4458 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4459 sig_entry->dwo_unit = dwo_entry;
4462 /* Subroutine of lookup_signatured_type.
4463 Create the signatured_type data structure for a TU to be read in
4464 directly from a DWO file, bypassing the stub.
4465 We do this for the case where there is no DWP file and we're using
4466 .gdb_index: When reading a CU we want to stay in the DWO file containing
4467 that CU. Otherwise we could end up reading several other DWO files (due
4468 to comdat folding) to process the transitive closure of all the mentioned
4469 TUs, and that can be slow. The current DWO file will have every type
4470 signature that it needs.
4471 We only do this for .gdb_index because in the psymtab case we already have
4472 to read all the DWOs to build the type unit groups. */
4474 static struct signatured_type *
4475 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4477 struct objfile *objfile = dwarf2_per_objfile->objfile;
4478 struct dwo_file *dwo_file;
4479 struct dwo_unit find_dwo_entry, *dwo_entry;
4480 struct signatured_type find_sig_entry, *sig_entry;
4482 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4484 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4485 dwo_unit of the TU itself. */
4486 dwo_file = cu->dwo_unit->dwo_file;
4488 /* We only ever need to read in one copy of a signatured type.
4489 Just use the global signatured_types array. If this is the first time
4490 we're reading this type, replace the recorded data from .gdb_index with
4493 if (dwarf2_per_objfile->signatured_types == NULL)
4495 find_sig_entry.signature = sig;
4496 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4497 if (sig_entry == NULL)
4499 /* Have we already tried to read this TU? */
4500 if (sig_entry->dwo_unit != NULL)
4503 /* Ok, this is the first time we're reading this TU. */
4504 if (dwo_file->tus == NULL)
4506 find_dwo_entry.signature = sig;
4507 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4508 if (dwo_entry == NULL)
4511 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4515 /* Subroutine of lookup_dwp_signatured_type.
4516 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4518 static struct signatured_type *
4519 add_type_unit (ULONGEST sig)
4521 struct objfile *objfile = dwarf2_per_objfile->objfile;
4522 int n_type_units = dwarf2_per_objfile->n_type_units;
4523 struct signatured_type *sig_type;
4527 dwarf2_per_objfile->all_type_units =
4528 xrealloc (dwarf2_per_objfile->all_type_units,
4529 n_type_units * sizeof (struct signatured_type *));
4530 dwarf2_per_objfile->n_type_units = n_type_units;
4531 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4532 struct signatured_type);
4533 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4534 sig_type->signature = sig;
4535 sig_type->per_cu.is_debug_types = 1;
4536 sig_type->per_cu.v.quick =
4537 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4538 struct dwarf2_per_cu_quick_data);
4539 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4541 gdb_assert (*slot == NULL);
4543 /* The rest of sig_type must be filled in by the caller. */
4547 /* Subroutine of lookup_signatured_type.
4548 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4549 then try the DWP file.
4550 Normally this "can't happen", but if there's a bug in signature
4551 generation and/or the DWP file is built incorrectly, it can happen.
4552 Using the type directly from the DWP file means we don't have the stub
4553 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4554 not critical. [Eventually the stub may go away for type units anyway.] */
4556 static struct signatured_type *
4557 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4559 struct objfile *objfile = dwarf2_per_objfile->objfile;
4560 struct dwp_file *dwp_file = get_dwp_file ();
4561 struct dwo_unit *dwo_entry;
4562 struct signatured_type find_sig_entry, *sig_entry;
4564 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4565 gdb_assert (dwp_file != NULL);
4567 if (dwarf2_per_objfile->signatured_types != NULL)
4569 find_sig_entry.signature = sig;
4570 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4572 if (sig_entry != NULL)
4576 /* This is the "shouldn't happen" case.
4577 Try the DWP file and hope for the best. */
4578 if (dwp_file->tus == NULL)
4580 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4581 sig, 1 /* is_debug_types */);
4582 if (dwo_entry == NULL)
4585 sig_entry = add_type_unit (sig);
4586 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4588 /* The caller will signal a complaint if we return NULL.
4589 Here we don't return NULL but we still want to complain. */
4590 complaint (&symfile_complaints,
4591 _("Bad type signature %s referenced by %s at 0x%x,"
4592 " coping by using copy in DWP [in module %s]"),
4594 cu->per_cu->is_debug_types ? "TU" : "CU",
4595 cu->per_cu->offset.sect_off,
4601 /* Lookup a signature based type for DW_FORM_ref_sig8.
4602 Returns NULL if signature SIG is not present in the table.
4603 It is up to the caller to complain about this. */
4605 static struct signatured_type *
4606 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4609 && dwarf2_per_objfile->using_index)
4611 /* We're in a DWO/DWP file, and we're using .gdb_index.
4612 These cases require special processing. */
4613 if (get_dwp_file () == NULL)
4614 return lookup_dwo_signatured_type (cu, sig);
4616 return lookup_dwp_signatured_type (cu, sig);
4620 struct signatured_type find_entry, *entry;
4622 if (dwarf2_per_objfile->signatured_types == NULL)
4624 find_entry.signature = sig;
4625 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4630 /* Low level DIE reading support. */
4632 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4635 init_cu_die_reader (struct die_reader_specs *reader,
4636 struct dwarf2_cu *cu,
4637 struct dwarf2_section_info *section,
4638 struct dwo_file *dwo_file)
4640 gdb_assert (section->readin && section->buffer != NULL);
4641 reader->abfd = section->asection->owner;
4643 reader->dwo_file = dwo_file;
4644 reader->die_section = section;
4645 reader->buffer = section->buffer;
4646 reader->buffer_end = section->buffer + section->size;
4647 reader->comp_dir = NULL;
4650 /* Subroutine of init_cutu_and_read_dies to simplify it.
4651 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4652 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4655 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4656 from it to the DIE in the DWO. If NULL we are skipping the stub.
4657 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4658 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4659 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4660 COMP_DIR must be non-NULL.
4661 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4662 are filled in with the info of the DIE from the DWO file.
4663 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4664 provided an abbrev table to use.
4665 The result is non-zero if a valid (non-dummy) DIE was found. */
4668 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4669 struct dwo_unit *dwo_unit,
4670 int abbrev_table_provided,
4671 struct die_info *stub_comp_unit_die,
4672 const char *stub_comp_dir,
4673 struct die_reader_specs *result_reader,
4674 const gdb_byte **result_info_ptr,
4675 struct die_info **result_comp_unit_die,
4676 int *result_has_children)
4678 struct objfile *objfile = dwarf2_per_objfile->objfile;
4679 struct dwarf2_cu *cu = this_cu->cu;
4680 struct dwarf2_section_info *section;
4682 const gdb_byte *begin_info_ptr, *info_ptr;
4683 const char *comp_dir_string;
4684 ULONGEST signature; /* Or dwo_id. */
4685 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4686 int i,num_extra_attrs;
4687 struct dwarf2_section_info *dwo_abbrev_section;
4688 struct attribute *attr;
4689 struct attribute comp_dir_attr;
4690 struct die_info *comp_unit_die;
4692 /* Both can't be provided. */
4693 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4695 /* These attributes aren't processed until later:
4696 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4697 However, the attribute is found in the stub which we won't have later.
4698 In order to not impose this complication on the rest of the code,
4699 we read them here and copy them to the DWO CU/TU die. */
4707 if (stub_comp_unit_die != NULL)
4709 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4711 if (! this_cu->is_debug_types)
4712 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4713 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4714 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4715 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4716 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4718 /* There should be a DW_AT_addr_base attribute here (if needed).
4719 We need the value before we can process DW_FORM_GNU_addr_index. */
4721 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4723 cu->addr_base = DW_UNSND (attr);
4725 /* There should be a DW_AT_ranges_base attribute here (if needed).
4726 We need the value before we can process DW_AT_ranges. */
4727 cu->ranges_base = 0;
4728 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4730 cu->ranges_base = DW_UNSND (attr);
4732 else if (stub_comp_dir != NULL)
4734 /* Reconstruct the comp_dir attribute to simplify the code below. */
4735 comp_dir = (struct attribute *)
4736 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4737 comp_dir->name = DW_AT_comp_dir;
4738 comp_dir->form = DW_FORM_string;
4739 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4740 DW_STRING (comp_dir) = stub_comp_dir;
4743 /* Set up for reading the DWO CU/TU. */
4744 cu->dwo_unit = dwo_unit;
4745 section = dwo_unit->section;
4746 dwarf2_read_section (objfile, section);
4747 abfd = section->asection->owner;
4748 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4749 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4750 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4752 if (this_cu->is_debug_types)
4754 ULONGEST header_signature;
4755 cu_offset type_offset_in_tu;
4756 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4758 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4762 &type_offset_in_tu);
4763 /* This is not an assert because it can be caused by bad debug info. */
4764 if (sig_type->signature != header_signature)
4766 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4767 " TU at offset 0x%x [in module %s]"),
4768 hex_string (sig_type->signature),
4769 hex_string (header_signature),
4770 dwo_unit->offset.sect_off,
4771 bfd_get_filename (abfd));
4773 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4774 /* For DWOs coming from DWP files, we don't know the CU length
4775 nor the type's offset in the TU until now. */
4776 dwo_unit->length = get_cu_length (&cu->header);
4777 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4779 /* Establish the type offset that can be used to lookup the type.
4780 For DWO files, we don't know it until now. */
4781 sig_type->type_offset_in_section.sect_off =
4782 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4786 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4789 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4790 /* For DWOs coming from DWP files, we don't know the CU length
4792 dwo_unit->length = get_cu_length (&cu->header);
4795 /* Replace the CU's original abbrev table with the DWO's.
4796 Reminder: We can't read the abbrev table until we've read the header. */
4797 if (abbrev_table_provided)
4799 /* Don't free the provided abbrev table, the caller of
4800 init_cutu_and_read_dies owns it. */
4801 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4802 /* Ensure the DWO abbrev table gets freed. */
4803 make_cleanup (dwarf2_free_abbrev_table, cu);
4807 dwarf2_free_abbrev_table (cu);
4808 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4809 /* Leave any existing abbrev table cleanup as is. */
4812 /* Read in the die, but leave space to copy over the attributes
4813 from the stub. This has the benefit of simplifying the rest of
4814 the code - all the work to maintain the illusion of a single
4815 DW_TAG_{compile,type}_unit DIE is done here. */
4816 num_extra_attrs = ((stmt_list != NULL)
4820 + (comp_dir != NULL));
4821 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4822 result_has_children, num_extra_attrs);
4824 /* Copy over the attributes from the stub to the DIE we just read in. */
4825 comp_unit_die = *result_comp_unit_die;
4826 i = comp_unit_die->num_attrs;
4827 if (stmt_list != NULL)
4828 comp_unit_die->attrs[i++] = *stmt_list;
4830 comp_unit_die->attrs[i++] = *low_pc;
4831 if (high_pc != NULL)
4832 comp_unit_die->attrs[i++] = *high_pc;
4834 comp_unit_die->attrs[i++] = *ranges;
4835 if (comp_dir != NULL)
4836 comp_unit_die->attrs[i++] = *comp_dir;
4837 comp_unit_die->num_attrs += num_extra_attrs;
4839 if (dwarf2_die_debug)
4841 fprintf_unfiltered (gdb_stdlog,
4842 "Read die from %s@0x%x of %s:\n",
4843 bfd_section_name (abfd, section->asection),
4844 (unsigned) (begin_info_ptr - section->buffer),
4845 bfd_get_filename (abfd));
4846 dump_die (comp_unit_die, dwarf2_die_debug);
4849 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4850 TUs by skipping the stub and going directly to the entry in the DWO file.
4851 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4852 to get it via circuitous means. Blech. */
4853 if (comp_dir != NULL)
4854 result_reader->comp_dir = DW_STRING (comp_dir);
4856 /* Skip dummy compilation units. */
4857 if (info_ptr >= begin_info_ptr + dwo_unit->length
4858 || peek_abbrev_code (abfd, info_ptr) == 0)
4861 *result_info_ptr = info_ptr;
4865 /* Subroutine of init_cutu_and_read_dies to simplify it.
4866 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4867 Returns NULL if the specified DWO unit cannot be found. */
4869 static struct dwo_unit *
4870 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4871 struct die_info *comp_unit_die)
4873 struct dwarf2_cu *cu = this_cu->cu;
4874 struct attribute *attr;
4876 struct dwo_unit *dwo_unit;
4877 const char *comp_dir, *dwo_name;
4879 gdb_assert (cu != NULL);
4881 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4882 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4883 gdb_assert (attr != NULL);
4884 dwo_name = DW_STRING (attr);
4886 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4888 comp_dir = DW_STRING (attr);
4890 if (this_cu->is_debug_types)
4892 struct signatured_type *sig_type;
4894 /* Since this_cu is the first member of struct signatured_type,
4895 we can go from a pointer to one to a pointer to the other. */
4896 sig_type = (struct signatured_type *) this_cu;
4897 signature = sig_type->signature;
4898 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4902 struct attribute *attr;
4904 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4906 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4908 dwo_name, this_cu->objfile->name);
4909 signature = DW_UNSND (attr);
4910 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4917 /* Subroutine of init_cutu_and_read_dies to simplify it.
4918 Read a TU directly from a DWO file, bypassing the stub. */
4921 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4922 die_reader_func_ftype *die_reader_func,
4925 struct dwarf2_cu *cu;
4926 struct signatured_type *sig_type;
4927 struct cleanup *cleanups, *free_cu_cleanup;
4928 struct die_reader_specs reader;
4929 const gdb_byte *info_ptr;
4930 struct die_info *comp_unit_die;
4933 /* Verify we can do the following downcast, and that we have the
4935 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4936 sig_type = (struct signatured_type *) this_cu;
4937 gdb_assert (sig_type->dwo_unit != NULL);
4939 cleanups = make_cleanup (null_cleanup, NULL);
4941 gdb_assert (this_cu->cu == NULL);
4942 cu = xmalloc (sizeof (*cu));
4943 init_one_comp_unit (cu, this_cu);
4944 /* If an error occurs while loading, release our storage. */
4945 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4947 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4948 0 /* abbrev_table_provided */,
4949 NULL /* stub_comp_unit_die */,
4950 sig_type->dwo_unit->dwo_file->comp_dir,
4952 &comp_unit_die, &has_children) == 0)
4955 do_cleanups (cleanups);
4959 /* All the "real" work is done here. */
4960 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4962 /* This duplicates some code in init_cutu_and_read_dies,
4963 but the alternative is making the latter more complex.
4964 This function is only for the special case of using DWO files directly:
4965 no point in overly complicating the general case just to handle this. */
4968 /* We've successfully allocated this compilation unit. Let our
4969 caller clean it up when finished with it. */
4970 discard_cleanups (free_cu_cleanup);
4972 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4973 So we have to manually free the abbrev table. */
4974 dwarf2_free_abbrev_table (cu);
4976 /* Link this CU into read_in_chain. */
4977 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4978 dwarf2_per_objfile->read_in_chain = this_cu;
4981 do_cleanups (free_cu_cleanup);
4983 do_cleanups (cleanups);
4986 /* Initialize a CU (or TU) and read its DIEs.
4987 If the CU defers to a DWO file, read the DWO file as well.
4989 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4990 Otherwise the table specified in the comp unit header is read in and used.
4991 This is an optimization for when we already have the abbrev table.
4993 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4994 Otherwise, a new CU is allocated with xmalloc.
4996 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4997 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4999 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5000 linker) then DIE_READER_FUNC will not get called. */
5003 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5004 struct abbrev_table *abbrev_table,
5005 int use_existing_cu, int keep,
5006 die_reader_func_ftype *die_reader_func,
5009 struct objfile *objfile = dwarf2_per_objfile->objfile;
5010 struct dwarf2_section_info *section = this_cu->section;
5011 bfd *abfd = section->asection->owner;
5012 struct dwarf2_cu *cu;
5013 const gdb_byte *begin_info_ptr, *info_ptr;
5014 struct die_reader_specs reader;
5015 struct die_info *comp_unit_die;
5017 struct attribute *attr;
5018 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5019 struct signatured_type *sig_type = NULL;
5020 struct dwarf2_section_info *abbrev_section;
5021 /* Non-zero if CU currently points to a DWO file and we need to
5022 reread it. When this happens we need to reread the skeleton die
5023 before we can reread the DWO file (this only applies to CUs, not TUs). */
5024 int rereading_dwo_cu = 0;
5026 if (dwarf2_die_debug)
5027 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5028 this_cu->is_debug_types ? "type" : "comp",
5029 this_cu->offset.sect_off);
5031 if (use_existing_cu)
5034 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5035 file (instead of going through the stub), short-circuit all of this. */
5036 if (this_cu->reading_dwo_directly)
5038 /* Narrow down the scope of possibilities to have to understand. */
5039 gdb_assert (this_cu->is_debug_types);
5040 gdb_assert (abbrev_table == NULL);
5041 gdb_assert (!use_existing_cu);
5042 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5046 cleanups = make_cleanup (null_cleanup, NULL);
5048 /* This is cheap if the section is already read in. */
5049 dwarf2_read_section (objfile, section);
5051 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5053 abbrev_section = get_abbrev_section_for_cu (this_cu);
5055 if (use_existing_cu && this_cu->cu != NULL)
5059 /* If this CU is from a DWO file we need to start over, we need to
5060 refetch the attributes from the skeleton CU.
5061 This could be optimized by retrieving those attributes from when we
5062 were here the first time: the previous comp_unit_die was stored in
5063 comp_unit_obstack. But there's no data yet that we need this
5065 if (cu->dwo_unit != NULL)
5066 rereading_dwo_cu = 1;
5070 /* If !use_existing_cu, this_cu->cu must be NULL. */
5071 gdb_assert (this_cu->cu == NULL);
5073 cu = xmalloc (sizeof (*cu));
5074 init_one_comp_unit (cu, this_cu);
5076 /* If an error occurs while loading, release our storage. */
5077 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5080 /* Get the header. */
5081 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5083 /* We already have the header, there's no need to read it in again. */
5084 info_ptr += cu->header.first_die_offset.cu_off;
5088 if (this_cu->is_debug_types)
5091 cu_offset type_offset_in_tu;
5093 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5094 abbrev_section, info_ptr,
5096 &type_offset_in_tu);
5098 /* Since per_cu is the first member of struct signatured_type,
5099 we can go from a pointer to one to a pointer to the other. */
5100 sig_type = (struct signatured_type *) this_cu;
5101 gdb_assert (sig_type->signature == signature);
5102 gdb_assert (sig_type->type_offset_in_tu.cu_off
5103 == type_offset_in_tu.cu_off);
5104 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5106 /* LENGTH has not been set yet for type units if we're
5107 using .gdb_index. */
5108 this_cu->length = get_cu_length (&cu->header);
5110 /* Establish the type offset that can be used to lookup the type. */
5111 sig_type->type_offset_in_section.sect_off =
5112 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5116 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5120 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5121 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5125 /* Skip dummy compilation units. */
5126 if (info_ptr >= begin_info_ptr + this_cu->length
5127 || peek_abbrev_code (abfd, info_ptr) == 0)
5129 do_cleanups (cleanups);
5133 /* If we don't have them yet, read the abbrevs for this compilation unit.
5134 And if we need to read them now, make sure they're freed when we're
5135 done. Note that it's important that if the CU had an abbrev table
5136 on entry we don't free it when we're done: Somewhere up the call stack
5137 it may be in use. */
5138 if (abbrev_table != NULL)
5140 gdb_assert (cu->abbrev_table == NULL);
5141 gdb_assert (cu->header.abbrev_offset.sect_off
5142 == abbrev_table->offset.sect_off);
5143 cu->abbrev_table = abbrev_table;
5145 else if (cu->abbrev_table == NULL)
5147 dwarf2_read_abbrevs (cu, abbrev_section);
5148 make_cleanup (dwarf2_free_abbrev_table, cu);
5150 else if (rereading_dwo_cu)
5152 dwarf2_free_abbrev_table (cu);
5153 dwarf2_read_abbrevs (cu, abbrev_section);
5156 /* Read the top level CU/TU die. */
5157 init_cu_die_reader (&reader, cu, section, NULL);
5158 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5160 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5162 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5163 DWO CU, that this test will fail (the attribute will not be present). */
5164 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5167 struct dwo_unit *dwo_unit;
5168 struct die_info *dwo_comp_unit_die;
5172 complaint (&symfile_complaints,
5173 _("compilation unit with DW_AT_GNU_dwo_name"
5174 " has children (offset 0x%x) [in module %s]"),
5175 this_cu->offset.sect_off, bfd_get_filename (abfd));
5177 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5178 if (dwo_unit != NULL)
5180 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5181 abbrev_table != NULL,
5182 comp_unit_die, NULL,
5184 &dwo_comp_unit_die, &has_children) == 0)
5187 do_cleanups (cleanups);
5190 comp_unit_die = dwo_comp_unit_die;
5194 /* Yikes, we couldn't find the rest of the DIE, we only have
5195 the stub. A complaint has already been logged. There's
5196 not much more we can do except pass on the stub DIE to
5197 die_reader_func. We don't want to throw an error on bad
5202 /* All of the above is setup for this call. Yikes. */
5203 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5205 /* Done, clean up. */
5206 if (free_cu_cleanup != NULL)
5210 /* We've successfully allocated this compilation unit. Let our
5211 caller clean it up when finished with it. */
5212 discard_cleanups (free_cu_cleanup);
5214 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5215 So we have to manually free the abbrev table. */
5216 dwarf2_free_abbrev_table (cu);
5218 /* Link this CU into read_in_chain. */
5219 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5220 dwarf2_per_objfile->read_in_chain = this_cu;
5223 do_cleanups (free_cu_cleanup);
5226 do_cleanups (cleanups);
5229 /* Read CU/TU THIS_CU in section SECTION,
5230 but do not follow DW_AT_GNU_dwo_name if present.
5231 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5232 to have already done the lookup to find the DWO/DWP file).
5234 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5235 THIS_CU->is_debug_types, but nothing else.
5237 We fill in THIS_CU->length.
5239 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5240 linker) then DIE_READER_FUNC will not get called.
5242 THIS_CU->cu is always freed when done.
5243 This is done in order to not leave THIS_CU->cu in a state where we have
5244 to care whether it refers to the "main" CU or the DWO CU. */
5247 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5248 struct dwarf2_section_info *abbrev_section,
5249 struct dwo_file *dwo_file,
5250 die_reader_func_ftype *die_reader_func,
5253 struct objfile *objfile = dwarf2_per_objfile->objfile;
5254 struct dwarf2_section_info *section = this_cu->section;
5255 bfd *abfd = section->asection->owner;
5256 struct dwarf2_cu cu;
5257 const gdb_byte *begin_info_ptr, *info_ptr;
5258 struct die_reader_specs reader;
5259 struct cleanup *cleanups;
5260 struct die_info *comp_unit_die;
5263 if (dwarf2_die_debug)
5264 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5265 this_cu->is_debug_types ? "type" : "comp",
5266 this_cu->offset.sect_off);
5268 gdb_assert (this_cu->cu == NULL);
5270 /* This is cheap if the section is already read in. */
5271 dwarf2_read_section (objfile, section);
5273 init_one_comp_unit (&cu, this_cu);
5275 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5277 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5278 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5279 abbrev_section, info_ptr,
5280 this_cu->is_debug_types);
5282 this_cu->length = get_cu_length (&cu.header);
5284 /* Skip dummy compilation units. */
5285 if (info_ptr >= begin_info_ptr + this_cu->length
5286 || peek_abbrev_code (abfd, info_ptr) == 0)
5288 do_cleanups (cleanups);
5292 dwarf2_read_abbrevs (&cu, abbrev_section);
5293 make_cleanup (dwarf2_free_abbrev_table, &cu);
5295 init_cu_die_reader (&reader, &cu, section, dwo_file);
5296 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5298 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5300 do_cleanups (cleanups);
5303 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5304 does not lookup the specified DWO file.
5305 This cannot be used to read DWO files.
5307 THIS_CU->cu is always freed when done.
5308 This is done in order to not leave THIS_CU->cu in a state where we have
5309 to care whether it refers to the "main" CU or the DWO CU.
5310 We can revisit this if the data shows there's a performance issue. */
5313 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5314 die_reader_func_ftype *die_reader_func,
5317 init_cutu_and_read_dies_no_follow (this_cu,
5318 get_abbrev_section_for_cu (this_cu),
5320 die_reader_func, data);
5323 /* Type Unit Groups.
5325 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5326 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5327 so that all types coming from the same compilation (.o file) are grouped
5328 together. A future step could be to put the types in the same symtab as
5329 the CU the types ultimately came from. */
5332 hash_type_unit_group (const void *item)
5334 const struct type_unit_group *tu_group = item;
5336 return hash_stmt_list_entry (&tu_group->hash);
5340 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5342 const struct type_unit_group *lhs = item_lhs;
5343 const struct type_unit_group *rhs = item_rhs;
5345 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5348 /* Allocate a hash table for type unit groups. */
5351 allocate_type_unit_groups_table (void)
5353 return htab_create_alloc_ex (3,
5354 hash_type_unit_group,
5357 &dwarf2_per_objfile->objfile->objfile_obstack,
5358 hashtab_obstack_allocate,
5359 dummy_obstack_deallocate);
5362 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5363 partial symtabs. We combine several TUs per psymtab to not let the size
5364 of any one psymtab grow too big. */
5365 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5366 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5368 /* Helper routine for get_type_unit_group.
5369 Create the type_unit_group object used to hold one or more TUs. */
5371 static struct type_unit_group *
5372 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5374 struct objfile *objfile = dwarf2_per_objfile->objfile;
5375 struct dwarf2_per_cu_data *per_cu;
5376 struct type_unit_group *tu_group;
5378 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5379 struct type_unit_group);
5380 per_cu = &tu_group->per_cu;
5381 per_cu->objfile = objfile;
5383 if (dwarf2_per_objfile->using_index)
5385 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5386 struct dwarf2_per_cu_quick_data);
5390 unsigned int line_offset = line_offset_struct.sect_off;
5391 struct partial_symtab *pst;
5394 /* Give the symtab a useful name for debug purposes. */
5395 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5396 name = xstrprintf ("<type_units_%d>",
5397 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5399 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5401 pst = create_partial_symtab (per_cu, name);
5407 tu_group->hash.dwo_unit = cu->dwo_unit;
5408 tu_group->hash.line_offset = line_offset_struct;
5413 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5414 STMT_LIST is a DW_AT_stmt_list attribute. */
5416 static struct type_unit_group *
5417 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5419 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5420 struct type_unit_group *tu_group;
5422 unsigned int line_offset;
5423 struct type_unit_group type_unit_group_for_lookup;
5425 if (dwarf2_per_objfile->type_unit_groups == NULL)
5427 dwarf2_per_objfile->type_unit_groups =
5428 allocate_type_unit_groups_table ();
5431 /* Do we need to create a new group, or can we use an existing one? */
5435 line_offset = DW_UNSND (stmt_list);
5436 ++tu_stats->nr_symtab_sharers;
5440 /* Ugh, no stmt_list. Rare, but we have to handle it.
5441 We can do various things here like create one group per TU or
5442 spread them over multiple groups to split up the expansion work.
5443 To avoid worst case scenarios (too many groups or too large groups)
5444 we, umm, group them in bunches. */
5445 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5446 | (tu_stats->nr_stmt_less_type_units
5447 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5448 ++tu_stats->nr_stmt_less_type_units;
5451 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5452 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5453 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5454 &type_unit_group_for_lookup, INSERT);
5458 gdb_assert (tu_group != NULL);
5462 sect_offset line_offset_struct;
5464 line_offset_struct.sect_off = line_offset;
5465 tu_group = create_type_unit_group (cu, line_offset_struct);
5467 ++tu_stats->nr_symtabs;
5473 /* Struct used to sort TUs by their abbreviation table offset. */
5475 struct tu_abbrev_offset
5477 struct signatured_type *sig_type;
5478 sect_offset abbrev_offset;
5481 /* Helper routine for build_type_unit_groups, passed to qsort. */
5484 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5486 const struct tu_abbrev_offset * const *a = ap;
5487 const struct tu_abbrev_offset * const *b = bp;
5488 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5489 unsigned int boff = (*b)->abbrev_offset.sect_off;
5491 return (aoff > boff) - (aoff < boff);
5494 /* A helper function to add a type_unit_group to a table. */
5497 add_type_unit_group_to_table (void **slot, void *datum)
5499 struct type_unit_group *tu_group = *slot;
5500 struct type_unit_group ***datap = datum;
5508 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5509 each one passing FUNC,DATA.
5511 The efficiency is because we sort TUs by the abbrev table they use and
5512 only read each abbrev table once. In one program there are 200K TUs
5513 sharing 8K abbrev tables.
5515 The main purpose of this function is to support building the
5516 dwarf2_per_objfile->type_unit_groups table.
5517 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5518 can collapse the search space by grouping them by stmt_list.
5519 The savings can be significant, in the same program from above the 200K TUs
5520 share 8K stmt_list tables.
5522 FUNC is expected to call get_type_unit_group, which will create the
5523 struct type_unit_group if necessary and add it to
5524 dwarf2_per_objfile->type_unit_groups. */
5527 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5529 struct objfile *objfile = dwarf2_per_objfile->objfile;
5530 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5531 struct cleanup *cleanups;
5532 struct abbrev_table *abbrev_table;
5533 sect_offset abbrev_offset;
5534 struct tu_abbrev_offset *sorted_by_abbrev;
5535 struct type_unit_group **iter;
5538 /* It's up to the caller to not call us multiple times. */
5539 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5541 if (dwarf2_per_objfile->n_type_units == 0)
5544 /* TUs typically share abbrev tables, and there can be way more TUs than
5545 abbrev tables. Sort by abbrev table to reduce the number of times we
5546 read each abbrev table in.
5547 Alternatives are to punt or to maintain a cache of abbrev tables.
5548 This is simpler and efficient enough for now.
5550 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5551 symtab to use). Typically TUs with the same abbrev offset have the same
5552 stmt_list value too so in practice this should work well.
5554 The basic algorithm here is:
5556 sort TUs by abbrev table
5557 for each TU with same abbrev table:
5558 read abbrev table if first user
5559 read TU top level DIE
5560 [IWBN if DWO skeletons had DW_AT_stmt_list]
5563 if (dwarf2_read_debug)
5564 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5566 /* Sort in a separate table to maintain the order of all_type_units
5567 for .gdb_index: TU indices directly index all_type_units. */
5568 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5569 dwarf2_per_objfile->n_type_units);
5570 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5572 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5574 sorted_by_abbrev[i].sig_type = sig_type;
5575 sorted_by_abbrev[i].abbrev_offset =
5576 read_abbrev_offset (sig_type->per_cu.section,
5577 sig_type->per_cu.offset);
5579 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5580 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5581 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5583 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5584 called any number of times, so we don't reset tu_stats here. */
5586 abbrev_offset.sect_off = ~(unsigned) 0;
5587 abbrev_table = NULL;
5588 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5590 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5592 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5594 /* Switch to the next abbrev table if necessary. */
5595 if (abbrev_table == NULL
5596 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5598 if (abbrev_table != NULL)
5600 abbrev_table_free (abbrev_table);
5601 /* Reset to NULL in case abbrev_table_read_table throws
5602 an error: abbrev_table_free_cleanup will get called. */
5603 abbrev_table = NULL;
5605 abbrev_offset = tu->abbrev_offset;
5607 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5609 ++tu_stats->nr_uniq_abbrev_tables;
5612 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5616 /* type_unit_groups can be NULL if there is an error in the debug info.
5617 Just create an empty table so the rest of gdb doesn't have to watch
5618 for this error case. */
5619 if (dwarf2_per_objfile->type_unit_groups == NULL)
5621 dwarf2_per_objfile->type_unit_groups =
5622 allocate_type_unit_groups_table ();
5623 dwarf2_per_objfile->n_type_unit_groups = 0;
5626 /* Create a vector of pointers to primary type units to make it easy to
5627 iterate over them and CUs. See dw2_get_primary_cu. */
5628 dwarf2_per_objfile->n_type_unit_groups =
5629 htab_elements (dwarf2_per_objfile->type_unit_groups);
5630 dwarf2_per_objfile->all_type_unit_groups =
5631 obstack_alloc (&objfile->objfile_obstack,
5632 dwarf2_per_objfile->n_type_unit_groups
5633 * sizeof (struct type_unit_group *));
5634 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5635 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5636 add_type_unit_group_to_table, &iter);
5637 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5638 == dwarf2_per_objfile->n_type_unit_groups);
5640 do_cleanups (cleanups);
5642 if (dwarf2_read_debug)
5644 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5645 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5646 dwarf2_per_objfile->n_type_units);
5647 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5648 tu_stats->nr_uniq_abbrev_tables);
5649 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5650 tu_stats->nr_symtabs);
5651 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5652 tu_stats->nr_symtab_sharers);
5653 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5654 tu_stats->nr_stmt_less_type_units);
5658 /* Partial symbol tables. */
5660 /* Create a psymtab named NAME and assign it to PER_CU.
5662 The caller must fill in the following details:
5663 dirname, textlow, texthigh. */
5665 static struct partial_symtab *
5666 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5668 struct objfile *objfile = per_cu->objfile;
5669 struct partial_symtab *pst;
5671 pst = start_psymtab_common (objfile, objfile->section_offsets,
5673 objfile->global_psymbols.next,
5674 objfile->static_psymbols.next);
5676 pst->psymtabs_addrmap_supported = 1;
5678 /* This is the glue that links PST into GDB's symbol API. */
5679 pst->read_symtab_private = per_cu;
5680 pst->read_symtab = dwarf2_read_symtab;
5681 per_cu->v.psymtab = pst;
5686 /* die_reader_func for process_psymtab_comp_unit. */
5689 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5690 const gdb_byte *info_ptr,
5691 struct die_info *comp_unit_die,
5695 struct dwarf2_cu *cu = reader->cu;
5696 struct objfile *objfile = cu->objfile;
5697 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5698 struct attribute *attr;
5700 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5701 struct partial_symtab *pst;
5703 const char *filename;
5704 int *want_partial_unit_ptr = data;
5706 if (comp_unit_die->tag == DW_TAG_partial_unit
5707 && (want_partial_unit_ptr == NULL
5708 || !*want_partial_unit_ptr))
5711 gdb_assert (! per_cu->is_debug_types);
5713 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5715 cu->list_in_scope = &file_symbols;
5717 /* Allocate a new partial symbol table structure. */
5718 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5719 if (attr == NULL || !DW_STRING (attr))
5722 filename = DW_STRING (attr);
5724 pst = create_partial_symtab (per_cu, filename);
5726 /* This must be done before calling dwarf2_build_include_psymtabs. */
5727 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5729 pst->dirname = DW_STRING (attr);
5731 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5733 dwarf2_find_base_address (comp_unit_die, cu);
5735 /* Possibly set the default values of LOWPC and HIGHPC from
5737 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5738 &best_highpc, cu, pst);
5739 if (has_pc_info == 1 && best_lowpc < best_highpc)
5740 /* Store the contiguous range if it is not empty; it can be empty for
5741 CUs with no code. */
5742 addrmap_set_empty (objfile->psymtabs_addrmap,
5743 best_lowpc + baseaddr,
5744 best_highpc + baseaddr - 1, pst);
5746 /* Check if comp unit has_children.
5747 If so, read the rest of the partial symbols from this comp unit.
5748 If not, there's no more debug_info for this comp unit. */
5751 struct partial_die_info *first_die;
5752 CORE_ADDR lowpc, highpc;
5754 lowpc = ((CORE_ADDR) -1);
5755 highpc = ((CORE_ADDR) 0);
5757 first_die = load_partial_dies (reader, info_ptr, 1);
5759 scan_partial_symbols (first_die, &lowpc, &highpc,
5762 /* If we didn't find a lowpc, set it to highpc to avoid
5763 complaints from `maint check'. */
5764 if (lowpc == ((CORE_ADDR) -1))
5767 /* If the compilation unit didn't have an explicit address range,
5768 then use the information extracted from its child dies. */
5772 best_highpc = highpc;
5775 pst->textlow = best_lowpc + baseaddr;
5776 pst->texthigh = best_highpc + baseaddr;
5778 pst->n_global_syms = objfile->global_psymbols.next -
5779 (objfile->global_psymbols.list + pst->globals_offset);
5780 pst->n_static_syms = objfile->static_psymbols.next -
5781 (objfile->static_psymbols.list + pst->statics_offset);
5782 sort_pst_symbols (objfile, pst);
5784 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5787 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5788 struct dwarf2_per_cu_data *iter;
5790 /* Fill in 'dependencies' here; we fill in 'users' in a
5792 pst->number_of_dependencies = len;
5793 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5794 len * sizeof (struct symtab *));
5796 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5799 pst->dependencies[i] = iter->v.psymtab;
5801 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5804 /* Get the list of files included in the current compilation unit,
5805 and build a psymtab for each of them. */
5806 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5808 if (dwarf2_read_debug)
5810 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5812 fprintf_unfiltered (gdb_stdlog,
5813 "Psymtab for %s unit @0x%x: %s - %s"
5814 ", %d global, %d static syms\n",
5815 per_cu->is_debug_types ? "type" : "comp",
5816 per_cu->offset.sect_off,
5817 paddress (gdbarch, pst->textlow),
5818 paddress (gdbarch, pst->texthigh),
5819 pst->n_global_syms, pst->n_static_syms);
5823 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5824 Process compilation unit THIS_CU for a psymtab. */
5827 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5828 int want_partial_unit)
5830 /* If this compilation unit was already read in, free the
5831 cached copy in order to read it in again. This is
5832 necessary because we skipped some symbols when we first
5833 read in the compilation unit (see load_partial_dies).
5834 This problem could be avoided, but the benefit is unclear. */
5835 if (this_cu->cu != NULL)
5836 free_one_cached_comp_unit (this_cu);
5838 gdb_assert (! this_cu->is_debug_types);
5839 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5840 process_psymtab_comp_unit_reader,
5841 &want_partial_unit);
5843 /* Age out any secondary CUs. */
5844 age_cached_comp_units ();
5847 /* Reader function for build_type_psymtabs. */
5850 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5851 const gdb_byte *info_ptr,
5852 struct die_info *type_unit_die,
5856 struct objfile *objfile = dwarf2_per_objfile->objfile;
5857 struct dwarf2_cu *cu = reader->cu;
5858 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5859 struct signatured_type *sig_type;
5860 struct type_unit_group *tu_group;
5861 struct attribute *attr;
5862 struct partial_die_info *first_die;
5863 CORE_ADDR lowpc, highpc;
5864 struct partial_symtab *pst;
5866 gdb_assert (data == NULL);
5867 gdb_assert (per_cu->is_debug_types);
5868 sig_type = (struct signatured_type *) per_cu;
5873 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5874 tu_group = get_type_unit_group (cu, attr);
5876 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5878 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5879 cu->list_in_scope = &file_symbols;
5880 pst = create_partial_symtab (per_cu, "");
5883 first_die = load_partial_dies (reader, info_ptr, 1);
5885 lowpc = (CORE_ADDR) -1;
5886 highpc = (CORE_ADDR) 0;
5887 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5889 pst->n_global_syms = objfile->global_psymbols.next -
5890 (objfile->global_psymbols.list + pst->globals_offset);
5891 pst->n_static_syms = objfile->static_psymbols.next -
5892 (objfile->static_psymbols.list + pst->statics_offset);
5893 sort_pst_symbols (objfile, pst);
5896 /* Traversal function for build_type_psymtabs. */
5899 build_type_psymtab_dependencies (void **slot, void *info)
5901 struct objfile *objfile = dwarf2_per_objfile->objfile;
5902 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5903 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5904 struct partial_symtab *pst = per_cu->v.psymtab;
5905 int len = VEC_length (sig_type_ptr, tu_group->tus);
5906 struct signatured_type *iter;
5909 gdb_assert (len > 0);
5910 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5912 pst->number_of_dependencies = len;
5913 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5914 len * sizeof (struct psymtab *));
5916 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5919 gdb_assert (iter->per_cu.is_debug_types);
5920 pst->dependencies[i] = iter->per_cu.v.psymtab;
5921 iter->type_unit_group = tu_group;
5924 VEC_free (sig_type_ptr, tu_group->tus);
5929 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5930 Build partial symbol tables for the .debug_types comp-units. */
5933 build_type_psymtabs (struct objfile *objfile)
5935 if (! create_all_type_units (objfile))
5938 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5940 /* Now that all TUs have been processed we can fill in the dependencies. */
5941 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5942 build_type_psymtab_dependencies, NULL);
5945 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5948 psymtabs_addrmap_cleanup (void *o)
5950 struct objfile *objfile = o;
5952 objfile->psymtabs_addrmap = NULL;
5955 /* Compute the 'user' field for each psymtab in OBJFILE. */
5958 set_partial_user (struct objfile *objfile)
5962 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5964 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5965 struct partial_symtab *pst = per_cu->v.psymtab;
5971 for (j = 0; j < pst->number_of_dependencies; ++j)
5973 /* Set the 'user' field only if it is not already set. */
5974 if (pst->dependencies[j]->user == NULL)
5975 pst->dependencies[j]->user = pst;
5980 /* Build the partial symbol table by doing a quick pass through the
5981 .debug_info and .debug_abbrev sections. */
5984 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5986 struct cleanup *back_to, *addrmap_cleanup;
5987 struct obstack temp_obstack;
5990 if (dwarf2_read_debug)
5992 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5996 dwarf2_per_objfile->reading_partial_symbols = 1;
5998 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6000 /* Any cached compilation units will be linked by the per-objfile
6001 read_in_chain. Make sure to free them when we're done. */
6002 back_to = make_cleanup (free_cached_comp_units, NULL);
6004 build_type_psymtabs (objfile);
6006 create_all_comp_units (objfile);
6008 /* Create a temporary address map on a temporary obstack. We later
6009 copy this to the final obstack. */
6010 obstack_init (&temp_obstack);
6011 make_cleanup_obstack_free (&temp_obstack);
6012 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6013 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6015 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6017 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
6019 process_psymtab_comp_unit (per_cu, 0);
6022 set_partial_user (objfile);
6024 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6025 &objfile->objfile_obstack);
6026 discard_cleanups (addrmap_cleanup);
6028 do_cleanups (back_to);
6030 if (dwarf2_read_debug)
6031 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6035 /* die_reader_func for load_partial_comp_unit. */
6038 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6039 const gdb_byte *info_ptr,
6040 struct die_info *comp_unit_die,
6044 struct dwarf2_cu *cu = reader->cu;
6046 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6048 /* Check if comp unit has_children.
6049 If so, read the rest of the partial symbols from this comp unit.
6050 If not, there's no more debug_info for this comp unit. */
6052 load_partial_dies (reader, info_ptr, 0);
6055 /* Load the partial DIEs for a secondary CU into memory.
6056 This is also used when rereading a primary CU with load_all_dies. */
6059 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6061 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6062 load_partial_comp_unit_reader, NULL);
6066 read_comp_units_from_section (struct objfile *objfile,
6067 struct dwarf2_section_info *section,
6068 unsigned int is_dwz,
6071 struct dwarf2_per_cu_data ***all_comp_units)
6073 const gdb_byte *info_ptr;
6074 bfd *abfd = section->asection->owner;
6076 if (dwarf2_read_debug)
6077 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6078 section->asection->name, bfd_get_filename (abfd));
6080 dwarf2_read_section (objfile, section);
6082 info_ptr = section->buffer;
6084 while (info_ptr < section->buffer + section->size)
6086 unsigned int length, initial_length_size;
6087 struct dwarf2_per_cu_data *this_cu;
6090 offset.sect_off = info_ptr - section->buffer;
6092 /* Read just enough information to find out where the next
6093 compilation unit is. */
6094 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6096 /* Save the compilation unit for later lookup. */
6097 this_cu = obstack_alloc (&objfile->objfile_obstack,
6098 sizeof (struct dwarf2_per_cu_data));
6099 memset (this_cu, 0, sizeof (*this_cu));
6100 this_cu->offset = offset;
6101 this_cu->length = length + initial_length_size;
6102 this_cu->is_dwz = is_dwz;
6103 this_cu->objfile = objfile;
6104 this_cu->section = section;
6106 if (*n_comp_units == *n_allocated)
6109 *all_comp_units = xrealloc (*all_comp_units,
6111 * sizeof (struct dwarf2_per_cu_data *));
6113 (*all_comp_units)[*n_comp_units] = this_cu;
6116 info_ptr = info_ptr + this_cu->length;
6120 /* Create a list of all compilation units in OBJFILE.
6121 This is only done for -readnow and building partial symtabs. */
6124 create_all_comp_units (struct objfile *objfile)
6128 struct dwarf2_per_cu_data **all_comp_units;
6129 struct dwz_file *dwz;
6133 all_comp_units = xmalloc (n_allocated
6134 * sizeof (struct dwarf2_per_cu_data *));
6136 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6137 &n_allocated, &n_comp_units, &all_comp_units);
6139 dwz = dwarf2_get_dwz_file ();
6141 read_comp_units_from_section (objfile, &dwz->info, 1,
6142 &n_allocated, &n_comp_units,
6145 dwarf2_per_objfile->all_comp_units
6146 = obstack_alloc (&objfile->objfile_obstack,
6147 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6148 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6149 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6150 xfree (all_comp_units);
6151 dwarf2_per_objfile->n_comp_units = n_comp_units;
6154 /* Process all loaded DIEs for compilation unit CU, starting at
6155 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6156 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6157 DW_AT_ranges). If NEED_PC is set, then this function will set
6158 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6159 and record the covered ranges in the addrmap. */
6162 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6163 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6165 struct partial_die_info *pdi;
6167 /* Now, march along the PDI's, descending into ones which have
6168 interesting children but skipping the children of the other ones,
6169 until we reach the end of the compilation unit. */
6175 fixup_partial_die (pdi, cu);
6177 /* Anonymous namespaces or modules have no name but have interesting
6178 children, so we need to look at them. Ditto for anonymous
6181 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6182 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6183 || pdi->tag == DW_TAG_imported_unit)
6187 case DW_TAG_subprogram:
6188 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6190 case DW_TAG_constant:
6191 case DW_TAG_variable:
6192 case DW_TAG_typedef:
6193 case DW_TAG_union_type:
6194 if (!pdi->is_declaration)
6196 add_partial_symbol (pdi, cu);
6199 case DW_TAG_class_type:
6200 case DW_TAG_interface_type:
6201 case DW_TAG_structure_type:
6202 if (!pdi->is_declaration)
6204 add_partial_symbol (pdi, cu);
6207 case DW_TAG_enumeration_type:
6208 if (!pdi->is_declaration)
6209 add_partial_enumeration (pdi, cu);
6211 case DW_TAG_base_type:
6212 case DW_TAG_subrange_type:
6213 /* File scope base type definitions are added to the partial
6215 add_partial_symbol (pdi, cu);
6217 case DW_TAG_namespace:
6218 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
6221 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6223 case DW_TAG_imported_unit:
6225 struct dwarf2_per_cu_data *per_cu;
6227 /* For now we don't handle imported units in type units. */
6228 if (cu->per_cu->is_debug_types)
6230 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6231 " supported in type units [in module %s]"),
6235 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6239 /* Go read the partial unit, if needed. */
6240 if (per_cu->v.psymtab == NULL)
6241 process_psymtab_comp_unit (per_cu, 1);
6243 VEC_safe_push (dwarf2_per_cu_ptr,
6244 cu->per_cu->imported_symtabs, per_cu);
6252 /* If the die has a sibling, skip to the sibling. */
6254 pdi = pdi->die_sibling;
6258 /* Functions used to compute the fully scoped name of a partial DIE.
6260 Normally, this is simple. For C++, the parent DIE's fully scoped
6261 name is concatenated with "::" and the partial DIE's name. For
6262 Java, the same thing occurs except that "." is used instead of "::".
6263 Enumerators are an exception; they use the scope of their parent
6264 enumeration type, i.e. the name of the enumeration type is not
6265 prepended to the enumerator.
6267 There are two complexities. One is DW_AT_specification; in this
6268 case "parent" means the parent of the target of the specification,
6269 instead of the direct parent of the DIE. The other is compilers
6270 which do not emit DW_TAG_namespace; in this case we try to guess
6271 the fully qualified name of structure types from their members'
6272 linkage names. This must be done using the DIE's children rather
6273 than the children of any DW_AT_specification target. We only need
6274 to do this for structures at the top level, i.e. if the target of
6275 any DW_AT_specification (if any; otherwise the DIE itself) does not
6278 /* Compute the scope prefix associated with PDI's parent, in
6279 compilation unit CU. The result will be allocated on CU's
6280 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6281 field. NULL is returned if no prefix is necessary. */
6283 partial_die_parent_scope (struct partial_die_info *pdi,
6284 struct dwarf2_cu *cu)
6286 const char *grandparent_scope;
6287 struct partial_die_info *parent, *real_pdi;
6289 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6290 then this means the parent of the specification DIE. */
6293 while (real_pdi->has_specification)
6294 real_pdi = find_partial_die (real_pdi->spec_offset,
6295 real_pdi->spec_is_dwz, cu);
6297 parent = real_pdi->die_parent;
6301 if (parent->scope_set)
6302 return parent->scope;
6304 fixup_partial_die (parent, cu);
6306 grandparent_scope = partial_die_parent_scope (parent, cu);
6308 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6309 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6310 Work around this problem here. */
6311 if (cu->language == language_cplus
6312 && parent->tag == DW_TAG_namespace
6313 && strcmp (parent->name, "::") == 0
6314 && grandparent_scope == NULL)
6316 parent->scope = NULL;
6317 parent->scope_set = 1;
6321 if (pdi->tag == DW_TAG_enumerator)
6322 /* Enumerators should not get the name of the enumeration as a prefix. */
6323 parent->scope = grandparent_scope;
6324 else if (parent->tag == DW_TAG_namespace
6325 || parent->tag == DW_TAG_module
6326 || parent->tag == DW_TAG_structure_type
6327 || parent->tag == DW_TAG_class_type
6328 || parent->tag == DW_TAG_interface_type
6329 || parent->tag == DW_TAG_union_type
6330 || parent->tag == DW_TAG_enumeration_type)
6332 if (grandparent_scope == NULL)
6333 parent->scope = parent->name;
6335 parent->scope = typename_concat (&cu->comp_unit_obstack,
6337 parent->name, 0, cu);
6341 /* FIXME drow/2004-04-01: What should we be doing with
6342 function-local names? For partial symbols, we should probably be
6344 complaint (&symfile_complaints,
6345 _("unhandled containing DIE tag %d for DIE at %d"),
6346 parent->tag, pdi->offset.sect_off);
6347 parent->scope = grandparent_scope;
6350 parent->scope_set = 1;
6351 return parent->scope;
6354 /* Return the fully scoped name associated with PDI, from compilation unit
6355 CU. The result will be allocated with malloc. */
6358 partial_die_full_name (struct partial_die_info *pdi,
6359 struct dwarf2_cu *cu)
6361 const char *parent_scope;
6363 /* If this is a template instantiation, we can not work out the
6364 template arguments from partial DIEs. So, unfortunately, we have
6365 to go through the full DIEs. At least any work we do building
6366 types here will be reused if full symbols are loaded later. */
6367 if (pdi->has_template_arguments)
6369 fixup_partial_die (pdi, cu);
6371 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6373 struct die_info *die;
6374 struct attribute attr;
6375 struct dwarf2_cu *ref_cu = cu;
6377 /* DW_FORM_ref_addr is using section offset. */
6379 attr.form = DW_FORM_ref_addr;
6380 attr.u.unsnd = pdi->offset.sect_off;
6381 die = follow_die_ref (NULL, &attr, &ref_cu);
6383 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6387 parent_scope = partial_die_parent_scope (pdi, cu);
6388 if (parent_scope == NULL)
6391 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6395 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6397 struct objfile *objfile = cu->objfile;
6399 const char *actual_name = NULL;
6401 char *built_actual_name;
6403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6405 built_actual_name = partial_die_full_name (pdi, cu);
6406 if (built_actual_name != NULL)
6407 actual_name = built_actual_name;
6409 if (actual_name == NULL)
6410 actual_name = pdi->name;
6414 case DW_TAG_subprogram:
6415 if (pdi->is_external || cu->language == language_ada)
6417 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6418 of the global scope. But in Ada, we want to be able to access
6419 nested procedures globally. So all Ada subprograms are stored
6420 in the global scope. */
6421 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6422 mst_text, objfile); */
6423 add_psymbol_to_list (actual_name, strlen (actual_name),
6424 built_actual_name != NULL,
6425 VAR_DOMAIN, LOC_BLOCK,
6426 &objfile->global_psymbols,
6427 0, pdi->lowpc + baseaddr,
6428 cu->language, objfile);
6432 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6433 mst_file_text, objfile); */
6434 add_psymbol_to_list (actual_name, strlen (actual_name),
6435 built_actual_name != NULL,
6436 VAR_DOMAIN, LOC_BLOCK,
6437 &objfile->static_psymbols,
6438 0, pdi->lowpc + baseaddr,
6439 cu->language, objfile);
6442 case DW_TAG_constant:
6444 struct psymbol_allocation_list *list;
6446 if (pdi->is_external)
6447 list = &objfile->global_psymbols;
6449 list = &objfile->static_psymbols;
6450 add_psymbol_to_list (actual_name, strlen (actual_name),
6451 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6452 list, 0, 0, cu->language, objfile);
6455 case DW_TAG_variable:
6457 addr = decode_locdesc (pdi->d.locdesc, cu);
6461 && !dwarf2_per_objfile->has_section_at_zero)
6463 /* A global or static variable may also have been stripped
6464 out by the linker if unused, in which case its address
6465 will be nullified; do not add such variables into partial
6466 symbol table then. */
6468 else if (pdi->is_external)
6471 Don't enter into the minimal symbol tables as there is
6472 a minimal symbol table entry from the ELF symbols already.
6473 Enter into partial symbol table if it has a location
6474 descriptor or a type.
6475 If the location descriptor is missing, new_symbol will create
6476 a LOC_UNRESOLVED symbol, the address of the variable will then
6477 be determined from the minimal symbol table whenever the variable
6479 The address for the partial symbol table entry is not
6480 used by GDB, but it comes in handy for debugging partial symbol
6483 if (pdi->d.locdesc || pdi->has_type)
6484 add_psymbol_to_list (actual_name, strlen (actual_name),
6485 built_actual_name != NULL,
6486 VAR_DOMAIN, LOC_STATIC,
6487 &objfile->global_psymbols,
6489 cu->language, objfile);
6493 /* Static Variable. Skip symbols without location descriptors. */
6494 if (pdi->d.locdesc == NULL)
6496 xfree (built_actual_name);
6499 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6500 mst_file_data, objfile); */
6501 add_psymbol_to_list (actual_name, strlen (actual_name),
6502 built_actual_name != NULL,
6503 VAR_DOMAIN, LOC_STATIC,
6504 &objfile->static_psymbols,
6506 cu->language, objfile);
6509 case DW_TAG_typedef:
6510 case DW_TAG_base_type:
6511 case DW_TAG_subrange_type:
6512 add_psymbol_to_list (actual_name, strlen (actual_name),
6513 built_actual_name != NULL,
6514 VAR_DOMAIN, LOC_TYPEDEF,
6515 &objfile->static_psymbols,
6516 0, (CORE_ADDR) 0, cu->language, objfile);
6518 case DW_TAG_namespace:
6519 add_psymbol_to_list (actual_name, strlen (actual_name),
6520 built_actual_name != NULL,
6521 VAR_DOMAIN, LOC_TYPEDEF,
6522 &objfile->global_psymbols,
6523 0, (CORE_ADDR) 0, cu->language, objfile);
6525 case DW_TAG_class_type:
6526 case DW_TAG_interface_type:
6527 case DW_TAG_structure_type:
6528 case DW_TAG_union_type:
6529 case DW_TAG_enumeration_type:
6530 /* Skip external references. The DWARF standard says in the section
6531 about "Structure, Union, and Class Type Entries": "An incomplete
6532 structure, union or class type is represented by a structure,
6533 union or class entry that does not have a byte size attribute
6534 and that has a DW_AT_declaration attribute." */
6535 if (!pdi->has_byte_size && pdi->is_declaration)
6537 xfree (built_actual_name);
6541 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6542 static vs. global. */
6543 add_psymbol_to_list (actual_name, strlen (actual_name),
6544 built_actual_name != NULL,
6545 STRUCT_DOMAIN, LOC_TYPEDEF,
6546 (cu->language == language_cplus
6547 || cu->language == language_java)
6548 ? &objfile->global_psymbols
6549 : &objfile->static_psymbols,
6550 0, (CORE_ADDR) 0, cu->language, objfile);
6553 case DW_TAG_enumerator:
6554 add_psymbol_to_list (actual_name, strlen (actual_name),
6555 built_actual_name != NULL,
6556 VAR_DOMAIN, LOC_CONST,
6557 (cu->language == language_cplus
6558 || cu->language == language_java)
6559 ? &objfile->global_psymbols
6560 : &objfile->static_psymbols,
6561 0, (CORE_ADDR) 0, cu->language, objfile);
6567 xfree (built_actual_name);
6570 /* Read a partial die corresponding to a namespace; also, add a symbol
6571 corresponding to that namespace to the symbol table. NAMESPACE is
6572 the name of the enclosing namespace. */
6575 add_partial_namespace (struct partial_die_info *pdi,
6576 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6577 int need_pc, struct dwarf2_cu *cu)
6579 /* Add a symbol for the namespace. */
6581 add_partial_symbol (pdi, cu);
6583 /* Now scan partial symbols in that namespace. */
6585 if (pdi->has_children)
6586 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6589 /* Read a partial die corresponding to a Fortran module. */
6592 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6593 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6595 /* Now scan partial symbols in that module. */
6597 if (pdi->has_children)
6598 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6601 /* Read a partial die corresponding to a subprogram and create a partial
6602 symbol for that subprogram. When the CU language allows it, this
6603 routine also defines a partial symbol for each nested subprogram
6604 that this subprogram contains.
6606 DIE my also be a lexical block, in which case we simply search
6607 recursively for suprograms defined inside that lexical block.
6608 Again, this is only performed when the CU language allows this
6609 type of definitions. */
6612 add_partial_subprogram (struct partial_die_info *pdi,
6613 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6614 int need_pc, struct dwarf2_cu *cu)
6616 if (pdi->tag == DW_TAG_subprogram)
6618 if (pdi->has_pc_info)
6620 if (pdi->lowpc < *lowpc)
6621 *lowpc = pdi->lowpc;
6622 if (pdi->highpc > *highpc)
6623 *highpc = pdi->highpc;
6627 struct objfile *objfile = cu->objfile;
6629 baseaddr = ANOFFSET (objfile->section_offsets,
6630 SECT_OFF_TEXT (objfile));
6631 addrmap_set_empty (objfile->psymtabs_addrmap,
6632 pdi->lowpc + baseaddr,
6633 pdi->highpc - 1 + baseaddr,
6634 cu->per_cu->v.psymtab);
6638 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6640 if (!pdi->is_declaration)
6641 /* Ignore subprogram DIEs that do not have a name, they are
6642 illegal. Do not emit a complaint at this point, we will
6643 do so when we convert this psymtab into a symtab. */
6645 add_partial_symbol (pdi, cu);
6649 if (! pdi->has_children)
6652 if (cu->language == language_ada)
6654 pdi = pdi->die_child;
6657 fixup_partial_die (pdi, cu);
6658 if (pdi->tag == DW_TAG_subprogram
6659 || pdi->tag == DW_TAG_lexical_block)
6660 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6661 pdi = pdi->die_sibling;
6666 /* Read a partial die corresponding to an enumeration type. */
6669 add_partial_enumeration (struct partial_die_info *enum_pdi,
6670 struct dwarf2_cu *cu)
6672 struct partial_die_info *pdi;
6674 if (enum_pdi->name != NULL)
6675 add_partial_symbol (enum_pdi, cu);
6677 pdi = enum_pdi->die_child;
6680 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6681 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6683 add_partial_symbol (pdi, cu);
6684 pdi = pdi->die_sibling;
6688 /* Return the initial uleb128 in the die at INFO_PTR. */
6691 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6693 unsigned int bytes_read;
6695 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6698 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6699 Return the corresponding abbrev, or NULL if the number is zero (indicating
6700 an empty DIE). In either case *BYTES_READ will be set to the length of
6701 the initial number. */
6703 static struct abbrev_info *
6704 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
6705 struct dwarf2_cu *cu)
6707 bfd *abfd = cu->objfile->obfd;
6708 unsigned int abbrev_number;
6709 struct abbrev_info *abbrev;
6711 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6713 if (abbrev_number == 0)
6716 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6719 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6720 abbrev_number, bfd_get_filename (abfd));
6726 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6727 Returns a pointer to the end of a series of DIEs, terminated by an empty
6728 DIE. Any children of the skipped DIEs will also be skipped. */
6730 static const gdb_byte *
6731 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
6733 struct dwarf2_cu *cu = reader->cu;
6734 struct abbrev_info *abbrev;
6735 unsigned int bytes_read;
6739 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6741 return info_ptr + bytes_read;
6743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6747 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6748 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6749 abbrev corresponding to that skipped uleb128 should be passed in
6750 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6753 static const gdb_byte *
6754 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
6755 struct abbrev_info *abbrev)
6757 unsigned int bytes_read;
6758 struct attribute attr;
6759 bfd *abfd = reader->abfd;
6760 struct dwarf2_cu *cu = reader->cu;
6761 const gdb_byte *buffer = reader->buffer;
6762 const gdb_byte *buffer_end = reader->buffer_end;
6763 const gdb_byte *start_info_ptr = info_ptr;
6764 unsigned int form, i;
6766 for (i = 0; i < abbrev->num_attrs; i++)
6768 /* The only abbrev we care about is DW_AT_sibling. */
6769 if (abbrev->attrs[i].name == DW_AT_sibling)
6771 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6772 if (attr.form == DW_FORM_ref_addr)
6773 complaint (&symfile_complaints,
6774 _("ignoring absolute DW_AT_sibling"));
6776 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6779 /* If it isn't DW_AT_sibling, skip this attribute. */
6780 form = abbrev->attrs[i].form;
6784 case DW_FORM_ref_addr:
6785 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6786 and later it is offset sized. */
6787 if (cu->header.version == 2)
6788 info_ptr += cu->header.addr_size;
6790 info_ptr += cu->header.offset_size;
6792 case DW_FORM_GNU_ref_alt:
6793 info_ptr += cu->header.offset_size;
6796 info_ptr += cu->header.addr_size;
6803 case DW_FORM_flag_present:
6815 case DW_FORM_ref_sig8:
6818 case DW_FORM_string:
6819 read_direct_string (abfd, info_ptr, &bytes_read);
6820 info_ptr += bytes_read;
6822 case DW_FORM_sec_offset:
6824 case DW_FORM_GNU_strp_alt:
6825 info_ptr += cu->header.offset_size;
6827 case DW_FORM_exprloc:
6829 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6830 info_ptr += bytes_read;
6832 case DW_FORM_block1:
6833 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6835 case DW_FORM_block2:
6836 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6838 case DW_FORM_block4:
6839 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6843 case DW_FORM_ref_udata:
6844 case DW_FORM_GNU_addr_index:
6845 case DW_FORM_GNU_str_index:
6846 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
6848 case DW_FORM_indirect:
6849 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6850 info_ptr += bytes_read;
6851 /* We need to continue parsing from here, so just go back to
6853 goto skip_attribute;
6856 error (_("Dwarf Error: Cannot handle %s "
6857 "in DWARF reader [in module %s]"),
6858 dwarf_form_name (form),
6859 bfd_get_filename (abfd));
6863 if (abbrev->has_children)
6864 return skip_children (reader, info_ptr);
6869 /* Locate ORIG_PDI's sibling.
6870 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6872 static const gdb_byte *
6873 locate_pdi_sibling (const struct die_reader_specs *reader,
6874 struct partial_die_info *orig_pdi,
6875 const gdb_byte *info_ptr)
6877 /* Do we know the sibling already? */
6879 if (orig_pdi->sibling)
6880 return orig_pdi->sibling;
6882 /* Are there any children to deal with? */
6884 if (!orig_pdi->has_children)
6887 /* Skip the children the long way. */
6889 return skip_children (reader, info_ptr);
6892 /* Expand this partial symbol table into a full symbol table. SELF is
6896 dwarf2_read_symtab (struct partial_symtab *self,
6897 struct objfile *objfile)
6901 warning (_("bug: psymtab for %s is already read in."),
6908 printf_filtered (_("Reading in symbols for %s..."),
6910 gdb_flush (gdb_stdout);
6913 /* Restore our global data. */
6914 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6916 /* If this psymtab is constructed from a debug-only objfile, the
6917 has_section_at_zero flag will not necessarily be correct. We
6918 can get the correct value for this flag by looking at the data
6919 associated with the (presumably stripped) associated objfile. */
6920 if (objfile->separate_debug_objfile_backlink)
6922 struct dwarf2_per_objfile *dpo_backlink
6923 = objfile_data (objfile->separate_debug_objfile_backlink,
6924 dwarf2_objfile_data_key);
6926 dwarf2_per_objfile->has_section_at_zero
6927 = dpo_backlink->has_section_at_zero;
6930 dwarf2_per_objfile->reading_partial_symbols = 0;
6932 psymtab_to_symtab_1 (self);
6934 /* Finish up the debug error message. */
6936 printf_filtered (_("done.\n"));
6939 process_cu_includes ();
6942 /* Reading in full CUs. */
6944 /* Add PER_CU to the queue. */
6947 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6948 enum language pretend_language)
6950 struct dwarf2_queue_item *item;
6953 item = xmalloc (sizeof (*item));
6954 item->per_cu = per_cu;
6955 item->pretend_language = pretend_language;
6958 if (dwarf2_queue == NULL)
6959 dwarf2_queue = item;
6961 dwarf2_queue_tail->next = item;
6963 dwarf2_queue_tail = item;
6966 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6967 unit and add it to our queue.
6968 The result is non-zero if PER_CU was queued, otherwise the result is zero
6969 meaning either PER_CU is already queued or it is already loaded. */
6972 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6973 struct dwarf2_per_cu_data *per_cu,
6974 enum language pretend_language)
6976 /* We may arrive here during partial symbol reading, if we need full
6977 DIEs to process an unusual case (e.g. template arguments). Do
6978 not queue PER_CU, just tell our caller to load its DIEs. */
6979 if (dwarf2_per_objfile->reading_partial_symbols)
6981 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6986 /* Mark the dependence relation so that we don't flush PER_CU
6988 dwarf2_add_dependence (this_cu, per_cu);
6990 /* If it's already on the queue, we have nothing to do. */
6994 /* If the compilation unit is already loaded, just mark it as
6996 if (per_cu->cu != NULL)
6998 per_cu->cu->last_used = 0;
7002 /* Add it to the queue. */
7003 queue_comp_unit (per_cu, pretend_language);
7008 /* Process the queue. */
7011 process_queue (void)
7013 struct dwarf2_queue_item *item, *next_item;
7015 if (dwarf2_read_debug)
7017 fprintf_unfiltered (gdb_stdlog,
7018 "Expanding one or more symtabs of objfile %s ...\n",
7019 dwarf2_per_objfile->objfile->name);
7022 /* The queue starts out with one item, but following a DIE reference
7023 may load a new CU, adding it to the end of the queue. */
7024 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7026 if (dwarf2_per_objfile->using_index
7027 ? !item->per_cu->v.quick->symtab
7028 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7030 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7033 if (per_cu->is_debug_types)
7035 struct signatured_type *sig_type =
7036 (struct signatured_type *) per_cu;
7038 sprintf (buf, "TU %s at offset 0x%x",
7039 hex_string (sig_type->signature), per_cu->offset.sect_off);
7042 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7044 if (dwarf2_read_debug)
7045 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7047 if (per_cu->is_debug_types)
7048 process_full_type_unit (per_cu, item->pretend_language);
7050 process_full_comp_unit (per_cu, item->pretend_language);
7052 if (dwarf2_read_debug)
7053 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7056 item->per_cu->queued = 0;
7057 next_item = item->next;
7061 dwarf2_queue_tail = NULL;
7063 if (dwarf2_read_debug)
7065 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7066 dwarf2_per_objfile->objfile->name);
7070 /* Free all allocated queue entries. This function only releases anything if
7071 an error was thrown; if the queue was processed then it would have been
7072 freed as we went along. */
7075 dwarf2_release_queue (void *dummy)
7077 struct dwarf2_queue_item *item, *last;
7079 item = dwarf2_queue;
7082 /* Anything still marked queued is likely to be in an
7083 inconsistent state, so discard it. */
7084 if (item->per_cu->queued)
7086 if (item->per_cu->cu != NULL)
7087 free_one_cached_comp_unit (item->per_cu);
7088 item->per_cu->queued = 0;
7096 dwarf2_queue = dwarf2_queue_tail = NULL;
7099 /* Read in full symbols for PST, and anything it depends on. */
7102 psymtab_to_symtab_1 (struct partial_symtab *pst)
7104 struct dwarf2_per_cu_data *per_cu;
7110 for (i = 0; i < pst->number_of_dependencies; i++)
7111 if (!pst->dependencies[i]->readin
7112 && pst->dependencies[i]->user == NULL)
7114 /* Inform about additional files that need to be read in. */
7117 /* FIXME: i18n: Need to make this a single string. */
7118 fputs_filtered (" ", gdb_stdout);
7120 fputs_filtered ("and ", gdb_stdout);
7122 printf_filtered ("%s...", pst->dependencies[i]->filename);
7123 wrap_here (""); /* Flush output. */
7124 gdb_flush (gdb_stdout);
7126 psymtab_to_symtab_1 (pst->dependencies[i]);
7129 per_cu = pst->read_symtab_private;
7133 /* It's an include file, no symbols to read for it.
7134 Everything is in the parent symtab. */
7139 dw2_do_instantiate_symtab (per_cu);
7142 /* Trivial hash function for die_info: the hash value of a DIE
7143 is its offset in .debug_info for this objfile. */
7146 die_hash (const void *item)
7148 const struct die_info *die = item;
7150 return die->offset.sect_off;
7153 /* Trivial comparison function for die_info structures: two DIEs
7154 are equal if they have the same offset. */
7157 die_eq (const void *item_lhs, const void *item_rhs)
7159 const struct die_info *die_lhs = item_lhs;
7160 const struct die_info *die_rhs = item_rhs;
7162 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7165 /* die_reader_func for load_full_comp_unit.
7166 This is identical to read_signatured_type_reader,
7167 but is kept separate for now. */
7170 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7171 const gdb_byte *info_ptr,
7172 struct die_info *comp_unit_die,
7176 struct dwarf2_cu *cu = reader->cu;
7177 enum language *language_ptr = data;
7179 gdb_assert (cu->die_hash == NULL);
7181 htab_create_alloc_ex (cu->header.length / 12,
7185 &cu->comp_unit_obstack,
7186 hashtab_obstack_allocate,
7187 dummy_obstack_deallocate);
7190 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7191 &info_ptr, comp_unit_die);
7192 cu->dies = comp_unit_die;
7193 /* comp_unit_die is not stored in die_hash, no need. */
7195 /* We try not to read any attributes in this function, because not
7196 all CUs needed for references have been loaded yet, and symbol
7197 table processing isn't initialized. But we have to set the CU language,
7198 or we won't be able to build types correctly.
7199 Similarly, if we do not read the producer, we can not apply
7200 producer-specific interpretation. */
7201 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7204 /* Load the DIEs associated with PER_CU into memory. */
7207 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7208 enum language pretend_language)
7210 gdb_assert (! this_cu->is_debug_types);
7212 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7213 load_full_comp_unit_reader, &pretend_language);
7216 /* Add a DIE to the delayed physname list. */
7219 add_to_method_list (struct type *type, int fnfield_index, int index,
7220 const char *name, struct die_info *die,
7221 struct dwarf2_cu *cu)
7223 struct delayed_method_info mi;
7225 mi.fnfield_index = fnfield_index;
7229 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7232 /* A cleanup for freeing the delayed method list. */
7235 free_delayed_list (void *ptr)
7237 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7238 if (cu->method_list != NULL)
7240 VEC_free (delayed_method_info, cu->method_list);
7241 cu->method_list = NULL;
7245 /* Compute the physnames of any methods on the CU's method list.
7247 The computation of method physnames is delayed in order to avoid the
7248 (bad) condition that one of the method's formal parameters is of an as yet
7252 compute_delayed_physnames (struct dwarf2_cu *cu)
7255 struct delayed_method_info *mi;
7256 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7258 const char *physname;
7259 struct fn_fieldlist *fn_flp
7260 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7261 physname = dwarf2_physname (mi->name, mi->die, cu);
7262 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7266 /* Go objects should be embedded in a DW_TAG_module DIE,
7267 and it's not clear if/how imported objects will appear.
7268 To keep Go support simple until that's worked out,
7269 go back through what we've read and create something usable.
7270 We could do this while processing each DIE, and feels kinda cleaner,
7271 but that way is more invasive.
7272 This is to, for example, allow the user to type "p var" or "b main"
7273 without having to specify the package name, and allow lookups
7274 of module.object to work in contexts that use the expression
7278 fixup_go_packaging (struct dwarf2_cu *cu)
7280 char *package_name = NULL;
7281 struct pending *list;
7284 for (list = global_symbols; list != NULL; list = list->next)
7286 for (i = 0; i < list->nsyms; ++i)
7288 struct symbol *sym = list->symbol[i];
7290 if (SYMBOL_LANGUAGE (sym) == language_go
7291 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7293 char *this_package_name = go_symbol_package_name (sym);
7295 if (this_package_name == NULL)
7297 if (package_name == NULL)
7298 package_name = this_package_name;
7301 if (strcmp (package_name, this_package_name) != 0)
7302 complaint (&symfile_complaints,
7303 _("Symtab %s has objects from two different Go packages: %s and %s"),
7304 (SYMBOL_SYMTAB (sym)
7305 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
7306 : cu->objfile->name),
7307 this_package_name, package_name);
7308 xfree (this_package_name);
7314 if (package_name != NULL)
7316 struct objfile *objfile = cu->objfile;
7317 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7319 strlen (package_name));
7320 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7321 saved_package_name, objfile);
7324 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7326 sym = allocate_symbol (objfile);
7327 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7328 SYMBOL_SET_NAMES (sym, saved_package_name,
7329 strlen (saved_package_name), 0, objfile);
7330 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7331 e.g., "main" finds the "main" module and not C's main(). */
7332 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7333 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7334 SYMBOL_TYPE (sym) = type;
7336 add_symbol_to_list (sym, &global_symbols);
7338 xfree (package_name);
7342 /* Return the symtab for PER_CU. This works properly regardless of
7343 whether we're using the index or psymtabs. */
7345 static struct symtab *
7346 get_symtab (struct dwarf2_per_cu_data *per_cu)
7348 return (dwarf2_per_objfile->using_index
7349 ? per_cu->v.quick->symtab
7350 : per_cu->v.psymtab->symtab);
7353 /* A helper function for computing the list of all symbol tables
7354 included by PER_CU. */
7357 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
7358 htab_t all_children,
7359 struct dwarf2_per_cu_data *per_cu)
7363 struct dwarf2_per_cu_data *iter;
7365 slot = htab_find_slot (all_children, per_cu, INSERT);
7368 /* This inclusion and its children have been processed. */
7373 /* Only add a CU if it has a symbol table. */
7374 if (get_symtab (per_cu) != NULL)
7375 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7378 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7380 recursively_compute_inclusions (result, all_children, iter);
7383 /* Compute the symtab 'includes' fields for the symtab related to
7387 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7389 gdb_assert (! per_cu->is_debug_types);
7391 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7394 struct dwarf2_per_cu_data *iter;
7395 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7396 htab_t all_children;
7397 struct symtab *symtab = get_symtab (per_cu);
7399 /* If we don't have a symtab, we can just skip this case. */
7403 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7404 NULL, xcalloc, xfree);
7407 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7410 recursively_compute_inclusions (&result_children, all_children, iter);
7412 /* Now we have a transitive closure of all the included CUs, and
7413 for .gdb_index version 7 the included TUs, so we can convert it
7414 to a list of symtabs. */
7415 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7417 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7418 (len + 1) * sizeof (struct symtab *));
7420 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7422 symtab->includes[ix] = get_symtab (iter);
7423 symtab->includes[len] = NULL;
7425 VEC_free (dwarf2_per_cu_ptr, result_children);
7426 htab_delete (all_children);
7430 /* Compute the 'includes' field for the symtabs of all the CUs we just
7434 process_cu_includes (void)
7437 struct dwarf2_per_cu_data *iter;
7440 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7444 if (! iter->is_debug_types)
7445 compute_symtab_includes (iter);
7448 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7451 /* Generate full symbol information for PER_CU, whose DIEs have
7452 already been loaded into memory. */
7455 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7456 enum language pretend_language)
7458 struct dwarf2_cu *cu = per_cu->cu;
7459 struct objfile *objfile = per_cu->objfile;
7460 CORE_ADDR lowpc, highpc;
7461 struct symtab *symtab;
7462 struct cleanup *back_to, *delayed_list_cleanup;
7464 struct block *static_block;
7466 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7469 back_to = make_cleanup (really_free_pendings, NULL);
7470 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7472 cu->list_in_scope = &file_symbols;
7474 cu->language = pretend_language;
7475 cu->language_defn = language_def (cu->language);
7477 /* Do line number decoding in read_file_scope () */
7478 process_die (cu->dies, cu);
7480 /* For now fudge the Go package. */
7481 if (cu->language == language_go)
7482 fixup_go_packaging (cu);
7484 /* Now that we have processed all the DIEs in the CU, all the types
7485 should be complete, and it should now be safe to compute all of the
7487 compute_delayed_physnames (cu);
7488 do_cleanups (delayed_list_cleanup);
7490 /* Some compilers don't define a DW_AT_high_pc attribute for the
7491 compilation unit. If the DW_AT_high_pc is missing, synthesize
7492 it, by scanning the DIE's below the compilation unit. */
7493 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7496 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
7498 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7499 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7500 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7501 addrmap to help ensure it has an accurate map of pc values belonging to
7503 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7505 symtab = end_symtab_from_static_block (static_block, objfile,
7506 SECT_OFF_TEXT (objfile), 0);
7510 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7512 /* Set symtab language to language from DW_AT_language. If the
7513 compilation is from a C file generated by language preprocessors, do
7514 not set the language if it was already deduced by start_subfile. */
7515 if (!(cu->language == language_c && symtab->language != language_c))
7516 symtab->language = cu->language;
7518 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7519 produce DW_AT_location with location lists but it can be possibly
7520 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7521 there were bugs in prologue debug info, fixed later in GCC-4.5
7522 by "unwind info for epilogues" patch (which is not directly related).
7524 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7525 needed, it would be wrong due to missing DW_AT_producer there.
7527 Still one can confuse GDB by using non-standard GCC compilation
7528 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7530 if (cu->has_loclist && gcc_4_minor >= 5)
7531 symtab->locations_valid = 1;
7533 if (gcc_4_minor >= 5)
7534 symtab->epilogue_unwind_valid = 1;
7536 symtab->call_site_htab = cu->call_site_htab;
7539 if (dwarf2_per_objfile->using_index)
7540 per_cu->v.quick->symtab = symtab;
7543 struct partial_symtab *pst = per_cu->v.psymtab;
7544 pst->symtab = symtab;
7548 /* Push it for inclusion processing later. */
7549 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7551 do_cleanups (back_to);
7554 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7555 already been loaded into memory. */
7558 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7559 enum language pretend_language)
7561 struct dwarf2_cu *cu = per_cu->cu;
7562 struct objfile *objfile = per_cu->objfile;
7563 struct symtab *symtab;
7564 struct cleanup *back_to, *delayed_list_cleanup;
7565 struct signatured_type *sig_type;
7567 gdb_assert (per_cu->is_debug_types);
7568 sig_type = (struct signatured_type *) per_cu;
7571 back_to = make_cleanup (really_free_pendings, NULL);
7572 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7574 cu->list_in_scope = &file_symbols;
7576 cu->language = pretend_language;
7577 cu->language_defn = language_def (cu->language);
7579 /* The symbol tables are set up in read_type_unit_scope. */
7580 process_die (cu->dies, cu);
7582 /* For now fudge the Go package. */
7583 if (cu->language == language_go)
7584 fixup_go_packaging (cu);
7586 /* Now that we have processed all the DIEs in the CU, all the types
7587 should be complete, and it should now be safe to compute all of the
7589 compute_delayed_physnames (cu);
7590 do_cleanups (delayed_list_cleanup);
7592 /* TUs share symbol tables.
7593 If this is the first TU to use this symtab, complete the construction
7594 of it with end_expandable_symtab. Otherwise, complete the addition of
7595 this TU's symbols to the existing symtab. */
7596 if (sig_type->type_unit_group->primary_symtab == NULL)
7598 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7599 sig_type->type_unit_group->primary_symtab = symtab;
7603 /* Set symtab language to language from DW_AT_language. If the
7604 compilation is from a C file generated by language preprocessors,
7605 do not set the language if it was already deduced by
7607 if (!(cu->language == language_c && symtab->language != language_c))
7608 symtab->language = cu->language;
7613 augment_type_symtab (objfile,
7614 sig_type->type_unit_group->primary_symtab);
7615 symtab = sig_type->type_unit_group->primary_symtab;
7618 if (dwarf2_per_objfile->using_index)
7619 per_cu->v.quick->symtab = symtab;
7622 struct partial_symtab *pst = per_cu->v.psymtab;
7623 pst->symtab = symtab;
7627 do_cleanups (back_to);
7630 /* Process an imported unit DIE. */
7633 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7635 struct attribute *attr;
7637 /* For now we don't handle imported units in type units. */
7638 if (cu->per_cu->is_debug_types)
7640 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7641 " supported in type units [in module %s]"),
7645 attr = dwarf2_attr (die, DW_AT_import, cu);
7648 struct dwarf2_per_cu_data *per_cu;
7649 struct symtab *imported_symtab;
7653 offset = dwarf2_get_ref_die_offset (attr);
7654 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7655 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7657 /* Queue the unit, if needed. */
7658 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7659 load_full_comp_unit (per_cu, cu->language);
7661 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7666 /* Process a die and its children. */
7669 process_die (struct die_info *die, struct dwarf2_cu *cu)
7673 case DW_TAG_padding:
7675 case DW_TAG_compile_unit:
7676 case DW_TAG_partial_unit:
7677 read_file_scope (die, cu);
7679 case DW_TAG_type_unit:
7680 read_type_unit_scope (die, cu);
7682 case DW_TAG_subprogram:
7683 case DW_TAG_inlined_subroutine:
7684 read_func_scope (die, cu);
7686 case DW_TAG_lexical_block:
7687 case DW_TAG_try_block:
7688 case DW_TAG_catch_block:
7689 read_lexical_block_scope (die, cu);
7691 case DW_TAG_GNU_call_site:
7692 read_call_site_scope (die, cu);
7694 case DW_TAG_class_type:
7695 case DW_TAG_interface_type:
7696 case DW_TAG_structure_type:
7697 case DW_TAG_union_type:
7698 process_structure_scope (die, cu);
7700 case DW_TAG_enumeration_type:
7701 process_enumeration_scope (die, cu);
7704 /* These dies have a type, but processing them does not create
7705 a symbol or recurse to process the children. Therefore we can
7706 read them on-demand through read_type_die. */
7707 case DW_TAG_subroutine_type:
7708 case DW_TAG_set_type:
7709 case DW_TAG_array_type:
7710 case DW_TAG_pointer_type:
7711 case DW_TAG_ptr_to_member_type:
7712 case DW_TAG_reference_type:
7713 case DW_TAG_string_type:
7716 case DW_TAG_base_type:
7717 case DW_TAG_subrange_type:
7718 case DW_TAG_typedef:
7719 /* Add a typedef symbol for the type definition, if it has a
7721 new_symbol (die, read_type_die (die, cu), cu);
7723 case DW_TAG_common_block:
7724 read_common_block (die, cu);
7726 case DW_TAG_common_inclusion:
7728 case DW_TAG_namespace:
7729 cu->processing_has_namespace_info = 1;
7730 read_namespace (die, cu);
7733 cu->processing_has_namespace_info = 1;
7734 read_module (die, cu);
7736 case DW_TAG_imported_declaration:
7737 case DW_TAG_imported_module:
7738 cu->processing_has_namespace_info = 1;
7739 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7740 || cu->language != language_fortran))
7741 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7742 dwarf_tag_name (die->tag));
7743 read_import_statement (die, cu);
7746 case DW_TAG_imported_unit:
7747 process_imported_unit_die (die, cu);
7751 new_symbol (die, NULL, cu);
7756 /* DWARF name computation. */
7758 /* A helper function for dwarf2_compute_name which determines whether DIE
7759 needs to have the name of the scope prepended to the name listed in the
7763 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7765 struct attribute *attr;
7769 case DW_TAG_namespace:
7770 case DW_TAG_typedef:
7771 case DW_TAG_class_type:
7772 case DW_TAG_interface_type:
7773 case DW_TAG_structure_type:
7774 case DW_TAG_union_type:
7775 case DW_TAG_enumeration_type:
7776 case DW_TAG_enumerator:
7777 case DW_TAG_subprogram:
7781 case DW_TAG_variable:
7782 case DW_TAG_constant:
7783 /* We only need to prefix "globally" visible variables. These include
7784 any variable marked with DW_AT_external or any variable that
7785 lives in a namespace. [Variables in anonymous namespaces
7786 require prefixing, but they are not DW_AT_external.] */
7788 if (dwarf2_attr (die, DW_AT_specification, cu))
7790 struct dwarf2_cu *spec_cu = cu;
7792 return die_needs_namespace (die_specification (die, &spec_cu),
7796 attr = dwarf2_attr (die, DW_AT_external, cu);
7797 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7798 && die->parent->tag != DW_TAG_module)
7800 /* A variable in a lexical block of some kind does not need a
7801 namespace, even though in C++ such variables may be external
7802 and have a mangled name. */
7803 if (die->parent->tag == DW_TAG_lexical_block
7804 || die->parent->tag == DW_TAG_try_block
7805 || die->parent->tag == DW_TAG_catch_block
7806 || die->parent->tag == DW_TAG_subprogram)
7815 /* Retrieve the last character from a mem_file. */
7818 do_ui_file_peek_last (void *object, const char *buffer, long length)
7820 char *last_char_p = (char *) object;
7823 *last_char_p = buffer[length - 1];
7826 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7827 compute the physname for the object, which include a method's:
7828 - formal parameters (C++/Java),
7829 - receiver type (Go),
7830 - return type (Java).
7832 The term "physname" is a bit confusing.
7833 For C++, for example, it is the demangled name.
7834 For Go, for example, it's the mangled name.
7836 For Ada, return the DIE's linkage name rather than the fully qualified
7837 name. PHYSNAME is ignored..
7839 The result is allocated on the objfile_obstack and canonicalized. */
7842 dwarf2_compute_name (const char *name,
7843 struct die_info *die, struct dwarf2_cu *cu,
7846 struct objfile *objfile = cu->objfile;
7849 name = dwarf2_name (die, cu);
7851 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7852 compute it by typename_concat inside GDB. */
7853 if (cu->language == language_ada
7854 || (cu->language == language_fortran && physname))
7856 /* For Ada unit, we prefer the linkage name over the name, as
7857 the former contains the exported name, which the user expects
7858 to be able to reference. Ideally, we want the user to be able
7859 to reference this entity using either natural or linkage name,
7860 but we haven't started looking at this enhancement yet. */
7861 struct attribute *attr;
7863 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7865 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7866 if (attr && DW_STRING (attr))
7867 return DW_STRING (attr);
7870 /* These are the only languages we know how to qualify names in. */
7872 && (cu->language == language_cplus || cu->language == language_java
7873 || cu->language == language_fortran))
7875 if (die_needs_namespace (die, cu))
7879 struct ui_file *buf;
7881 prefix = determine_prefix (die, cu);
7882 buf = mem_fileopen ();
7883 if (*prefix != '\0')
7885 char *prefixed_name = typename_concat (NULL, prefix, name,
7888 fputs_unfiltered (prefixed_name, buf);
7889 xfree (prefixed_name);
7892 fputs_unfiltered (name, buf);
7894 /* Template parameters may be specified in the DIE's DW_AT_name, or
7895 as children with DW_TAG_template_type_param or
7896 DW_TAG_value_type_param. If the latter, add them to the name
7897 here. If the name already has template parameters, then
7898 skip this step; some versions of GCC emit both, and
7899 it is more efficient to use the pre-computed name.
7901 Something to keep in mind about this process: it is very
7902 unlikely, or in some cases downright impossible, to produce
7903 something that will match the mangled name of a function.
7904 If the definition of the function has the same debug info,
7905 we should be able to match up with it anyway. But fallbacks
7906 using the minimal symbol, for instance to find a method
7907 implemented in a stripped copy of libstdc++, will not work.
7908 If we do not have debug info for the definition, we will have to
7909 match them up some other way.
7911 When we do name matching there is a related problem with function
7912 templates; two instantiated function templates are allowed to
7913 differ only by their return types, which we do not add here. */
7915 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7917 struct attribute *attr;
7918 struct die_info *child;
7921 die->building_fullname = 1;
7923 for (child = die->child; child != NULL; child = child->sibling)
7927 const gdb_byte *bytes;
7928 struct dwarf2_locexpr_baton *baton;
7931 if (child->tag != DW_TAG_template_type_param
7932 && child->tag != DW_TAG_template_value_param)
7937 fputs_unfiltered ("<", buf);
7941 fputs_unfiltered (", ", buf);
7943 attr = dwarf2_attr (child, DW_AT_type, cu);
7946 complaint (&symfile_complaints,
7947 _("template parameter missing DW_AT_type"));
7948 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7951 type = die_type (child, cu);
7953 if (child->tag == DW_TAG_template_type_param)
7955 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7959 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7962 complaint (&symfile_complaints,
7963 _("template parameter missing "
7964 "DW_AT_const_value"));
7965 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7969 dwarf2_const_value_attr (attr, type, name,
7970 &cu->comp_unit_obstack, cu,
7971 &value, &bytes, &baton);
7973 if (TYPE_NOSIGN (type))
7974 /* GDB prints characters as NUMBER 'CHAR'. If that's
7975 changed, this can use value_print instead. */
7976 c_printchar (value, type, buf);
7979 struct value_print_options opts;
7982 v = dwarf2_evaluate_loc_desc (type, NULL,
7986 else if (bytes != NULL)
7988 v = allocate_value (type);
7989 memcpy (value_contents_writeable (v), bytes,
7990 TYPE_LENGTH (type));
7993 v = value_from_longest (type, value);
7995 /* Specify decimal so that we do not depend on
7997 get_formatted_print_options (&opts, 'd');
7999 value_print (v, buf, &opts);
8005 die->building_fullname = 0;
8009 /* Close the argument list, with a space if necessary
8010 (nested templates). */
8011 char last_char = '\0';
8012 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8013 if (last_char == '>')
8014 fputs_unfiltered (" >", buf);
8016 fputs_unfiltered (">", buf);
8020 /* For Java and C++ methods, append formal parameter type
8021 information, if PHYSNAME. */
8023 if (physname && die->tag == DW_TAG_subprogram
8024 && (cu->language == language_cplus
8025 || cu->language == language_java))
8027 struct type *type = read_type_die (die, cu);
8029 c_type_print_args (type, buf, 1, cu->language,
8030 &type_print_raw_options);
8032 if (cu->language == language_java)
8034 /* For java, we must append the return type to method
8036 if (die->tag == DW_TAG_subprogram)
8037 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8038 0, 0, &type_print_raw_options);
8040 else if (cu->language == language_cplus)
8042 /* Assume that an artificial first parameter is
8043 "this", but do not crash if it is not. RealView
8044 marks unnamed (and thus unused) parameters as
8045 artificial; there is no way to differentiate
8047 if (TYPE_NFIELDS (type) > 0
8048 && TYPE_FIELD_ARTIFICIAL (type, 0)
8049 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8050 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8052 fputs_unfiltered (" const", buf);
8056 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
8058 ui_file_delete (buf);
8060 if (cu->language == language_cplus)
8063 = dwarf2_canonicalize_name (name, cu,
8064 &objfile->objfile_obstack);
8075 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8076 If scope qualifiers are appropriate they will be added. The result
8077 will be allocated on the objfile_obstack, or NULL if the DIE does
8078 not have a name. NAME may either be from a previous call to
8079 dwarf2_name or NULL.
8081 The output string will be canonicalized (if C++/Java). */
8084 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8086 return dwarf2_compute_name (name, die, cu, 0);
8089 /* Construct a physname for the given DIE in CU. NAME may either be
8090 from a previous call to dwarf2_name or NULL. The result will be
8091 allocated on the objfile_objstack or NULL if the DIE does not have a
8094 The output string will be canonicalized (if C++/Java). */
8097 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8099 struct objfile *objfile = cu->objfile;
8100 struct attribute *attr;
8101 const char *retval, *mangled = NULL, *canon = NULL;
8102 struct cleanup *back_to;
8105 /* In this case dwarf2_compute_name is just a shortcut not building anything
8107 if (!die_needs_namespace (die, cu))
8108 return dwarf2_compute_name (name, die, cu, 1);
8110 back_to = make_cleanup (null_cleanup, NULL);
8112 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8114 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8116 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8118 if (attr && DW_STRING (attr))
8122 mangled = DW_STRING (attr);
8124 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8125 type. It is easier for GDB users to search for such functions as
8126 `name(params)' than `long name(params)'. In such case the minimal
8127 symbol names do not match the full symbol names but for template
8128 functions there is never a need to look up their definition from their
8129 declaration so the only disadvantage remains the minimal symbol
8130 variant `long name(params)' does not have the proper inferior type.
8133 if (cu->language == language_go)
8135 /* This is a lie, but we already lie to the caller new_symbol_full.
8136 new_symbol_full assumes we return the mangled name.
8137 This just undoes that lie until things are cleaned up. */
8142 demangled = gdb_demangle (mangled,
8143 (DMGL_PARAMS | DMGL_ANSI
8144 | (cu->language == language_java
8145 ? DMGL_JAVA | DMGL_RET_POSTFIX
8150 make_cleanup (xfree, demangled);
8160 if (canon == NULL || check_physname)
8162 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8164 if (canon != NULL && strcmp (physname, canon) != 0)
8166 /* It may not mean a bug in GDB. The compiler could also
8167 compute DW_AT_linkage_name incorrectly. But in such case
8168 GDB would need to be bug-to-bug compatible. */
8170 complaint (&symfile_complaints,
8171 _("Computed physname <%s> does not match demangled <%s> "
8172 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8173 physname, canon, mangled, die->offset.sect_off, objfile->name);
8175 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8176 is available here - over computed PHYSNAME. It is safer
8177 against both buggy GDB and buggy compilers. */
8191 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
8193 do_cleanups (back_to);
8197 /* Read the import statement specified by the given die and record it. */
8200 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8202 struct objfile *objfile = cu->objfile;
8203 struct attribute *import_attr;
8204 struct die_info *imported_die, *child_die;
8205 struct dwarf2_cu *imported_cu;
8206 const char *imported_name;
8207 const char *imported_name_prefix;
8208 const char *canonical_name;
8209 const char *import_alias;
8210 const char *imported_declaration = NULL;
8211 const char *import_prefix;
8212 VEC (const_char_ptr) *excludes = NULL;
8213 struct cleanup *cleanups;
8215 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8216 if (import_attr == NULL)
8218 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8219 dwarf_tag_name (die->tag));
8224 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8225 imported_name = dwarf2_name (imported_die, imported_cu);
8226 if (imported_name == NULL)
8228 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8230 The import in the following code:
8244 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8245 <52> DW_AT_decl_file : 1
8246 <53> DW_AT_decl_line : 6
8247 <54> DW_AT_import : <0x75>
8248 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8250 <5b> DW_AT_decl_file : 1
8251 <5c> DW_AT_decl_line : 2
8252 <5d> DW_AT_type : <0x6e>
8254 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8255 <76> DW_AT_byte_size : 4
8256 <77> DW_AT_encoding : 5 (signed)
8258 imports the wrong die ( 0x75 instead of 0x58 ).
8259 This case will be ignored until the gcc bug is fixed. */
8263 /* Figure out the local name after import. */
8264 import_alias = dwarf2_name (die, cu);
8266 /* Figure out where the statement is being imported to. */
8267 import_prefix = determine_prefix (die, cu);
8269 /* Figure out what the scope of the imported die is and prepend it
8270 to the name of the imported die. */
8271 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8273 if (imported_die->tag != DW_TAG_namespace
8274 && imported_die->tag != DW_TAG_module)
8276 imported_declaration = imported_name;
8277 canonical_name = imported_name_prefix;
8279 else if (strlen (imported_name_prefix) > 0)
8280 canonical_name = obconcat (&objfile->objfile_obstack,
8281 imported_name_prefix, "::", imported_name,
8284 canonical_name = imported_name;
8286 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8288 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8289 for (child_die = die->child; child_die && child_die->tag;
8290 child_die = sibling_die (child_die))
8292 /* DWARF-4: A Fortran use statement with a “rename list” may be
8293 represented by an imported module entry with an import attribute
8294 referring to the module and owned entries corresponding to those
8295 entities that are renamed as part of being imported. */
8297 if (child_die->tag != DW_TAG_imported_declaration)
8299 complaint (&symfile_complaints,
8300 _("child DW_TAG_imported_declaration expected "
8301 "- DIE at 0x%x [in module %s]"),
8302 child_die->offset.sect_off, objfile->name);
8306 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8307 if (import_attr == NULL)
8309 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8310 dwarf_tag_name (child_die->tag));
8315 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8317 imported_name = dwarf2_name (imported_die, imported_cu);
8318 if (imported_name == NULL)
8320 complaint (&symfile_complaints,
8321 _("child DW_TAG_imported_declaration has unknown "
8322 "imported name - DIE at 0x%x [in module %s]"),
8323 child_die->offset.sect_off, objfile->name);
8327 VEC_safe_push (const_char_ptr, excludes, imported_name);
8329 process_die (child_die, cu);
8332 cp_add_using_directive (import_prefix,
8335 imported_declaration,
8338 &objfile->objfile_obstack);
8340 do_cleanups (cleanups);
8343 /* Cleanup function for handle_DW_AT_stmt_list. */
8346 free_cu_line_header (void *arg)
8348 struct dwarf2_cu *cu = arg;
8350 free_line_header (cu->line_header);
8351 cu->line_header = NULL;
8354 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8355 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8356 this, it was first present in GCC release 4.3.0. */
8359 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8361 if (!cu->checked_producer)
8362 check_producer (cu);
8364 return cu->producer_is_gcc_lt_4_3;
8368 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
8369 const char **name, const char **comp_dir)
8371 struct attribute *attr;
8376 /* Find the filename. Do not use dwarf2_name here, since the filename
8377 is not a source language identifier. */
8378 attr = dwarf2_attr (die, DW_AT_name, cu);
8381 *name = DW_STRING (attr);
8384 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8386 *comp_dir = DW_STRING (attr);
8387 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8388 && IS_ABSOLUTE_PATH (*name))
8390 char *d = ldirname (*name);
8394 make_cleanup (xfree, d);
8396 if (*comp_dir != NULL)
8398 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8399 directory, get rid of it. */
8400 char *cp = strchr (*comp_dir, ':');
8402 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8407 *name = "<unknown>";
8410 /* Handle DW_AT_stmt_list for a compilation unit.
8411 DIE is the DW_TAG_compile_unit die for CU.
8412 COMP_DIR is the compilation directory.
8413 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8416 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8417 const char *comp_dir) /* ARI: editCase function */
8419 struct attribute *attr;
8421 gdb_assert (! cu->per_cu->is_debug_types);
8423 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8426 unsigned int line_offset = DW_UNSND (attr);
8427 struct line_header *line_header
8428 = dwarf_decode_line_header (line_offset, cu);
8432 cu->line_header = line_header;
8433 make_cleanup (free_cu_line_header, cu);
8434 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8439 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8442 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8444 struct objfile *objfile = dwarf2_per_objfile->objfile;
8445 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8446 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8447 CORE_ADDR highpc = ((CORE_ADDR) 0);
8448 struct attribute *attr;
8449 const char *name = NULL;
8450 const char *comp_dir = NULL;
8451 struct die_info *child_die;
8452 bfd *abfd = objfile->obfd;
8455 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8457 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8459 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8460 from finish_block. */
8461 if (lowpc == ((CORE_ADDR) -1))
8466 find_file_and_directory (die, cu, &name, &comp_dir);
8468 prepare_one_comp_unit (cu, die, cu->language);
8470 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8471 standardised yet. As a workaround for the language detection we fall
8472 back to the DW_AT_producer string. */
8473 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8474 cu->language = language_opencl;
8476 /* Similar hack for Go. */
8477 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8478 set_cu_language (DW_LANG_Go, cu);
8480 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8482 /* Decode line number information if present. We do this before
8483 processing child DIEs, so that the line header table is available
8484 for DW_AT_decl_file. */
8485 handle_DW_AT_stmt_list (die, cu, comp_dir);
8487 /* Process all dies in compilation unit. */
8488 if (die->child != NULL)
8490 child_die = die->child;
8491 while (child_die && child_die->tag)
8493 process_die (child_die, cu);
8494 child_die = sibling_die (child_die);
8498 /* Decode macro information, if present. Dwarf 2 macro information
8499 refers to information in the line number info statement program
8500 header, so we can only read it if we've read the header
8502 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8503 if (attr && cu->line_header)
8505 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8506 complaint (&symfile_complaints,
8507 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8509 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8513 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8514 if (attr && cu->line_header)
8516 unsigned int macro_offset = DW_UNSND (attr);
8518 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8522 do_cleanups (back_to);
8525 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8526 Create the set of symtabs used by this TU, or if this TU is sharing
8527 symtabs with another TU and the symtabs have already been created
8528 then restore those symtabs in the line header.
8529 We don't need the pc/line-number mapping for type units. */
8532 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8534 struct objfile *objfile = dwarf2_per_objfile->objfile;
8535 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8536 struct type_unit_group *tu_group;
8538 struct line_header *lh;
8539 struct attribute *attr;
8540 unsigned int i, line_offset;
8541 struct signatured_type *sig_type;
8543 gdb_assert (per_cu->is_debug_types);
8544 sig_type = (struct signatured_type *) per_cu;
8546 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8548 /* If we're using .gdb_index (includes -readnow) then
8549 per_cu->type_unit_group may not have been set up yet. */
8550 if (sig_type->type_unit_group == NULL)
8551 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8552 tu_group = sig_type->type_unit_group;
8554 /* If we've already processed this stmt_list there's no real need to
8555 do it again, we could fake it and just recreate the part we need
8556 (file name,index -> symtab mapping). If data shows this optimization
8557 is useful we can do it then. */
8558 first_time = tu_group->primary_symtab == NULL;
8560 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8565 line_offset = DW_UNSND (attr);
8566 lh = dwarf_decode_line_header (line_offset, cu);
8571 dwarf2_start_symtab (cu, "", NULL, 0);
8574 gdb_assert (tu_group->symtabs == NULL);
8577 /* Note: The primary symtab will get allocated at the end. */
8581 cu->line_header = lh;
8582 make_cleanup (free_cu_line_header, cu);
8586 dwarf2_start_symtab (cu, "", NULL, 0);
8588 tu_group->num_symtabs = lh->num_file_names;
8589 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8591 for (i = 0; i < lh->num_file_names; ++i)
8593 const char *dir = NULL;
8594 struct file_entry *fe = &lh->file_names[i];
8597 dir = lh->include_dirs[fe->dir_index - 1];
8598 dwarf2_start_subfile (fe->name, dir, NULL);
8600 /* Note: We don't have to watch for the main subfile here, type units
8601 don't have DW_AT_name. */
8603 if (current_subfile->symtab == NULL)
8605 /* NOTE: start_subfile will recognize when it's been passed
8606 a file it has already seen. So we can't assume there's a
8607 simple mapping from lh->file_names to subfiles,
8608 lh->file_names may contain dups. */
8609 current_subfile->symtab = allocate_symtab (current_subfile->name,
8613 fe->symtab = current_subfile->symtab;
8614 tu_group->symtabs[i] = fe->symtab;
8621 for (i = 0; i < lh->num_file_names; ++i)
8623 struct file_entry *fe = &lh->file_names[i];
8625 fe->symtab = tu_group->symtabs[i];
8629 /* The main symtab is allocated last. Type units don't have DW_AT_name
8630 so they don't have a "real" (so to speak) symtab anyway.
8631 There is later code that will assign the main symtab to all symbols
8632 that don't have one. We need to handle the case of a symbol with a
8633 missing symtab (DW_AT_decl_file) anyway. */
8636 /* Process DW_TAG_type_unit.
8637 For TUs we want to skip the first top level sibling if it's not the
8638 actual type being defined by this TU. In this case the first top
8639 level sibling is there to provide context only. */
8642 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8644 struct die_info *child_die;
8646 prepare_one_comp_unit (cu, die, language_minimal);
8648 /* Initialize (or reinitialize) the machinery for building symtabs.
8649 We do this before processing child DIEs, so that the line header table
8650 is available for DW_AT_decl_file. */
8651 setup_type_unit_groups (die, cu);
8653 if (die->child != NULL)
8655 child_die = die->child;
8656 while (child_die && child_die->tag)
8658 process_die (child_die, cu);
8659 child_die = sibling_die (child_die);
8666 http://gcc.gnu.org/wiki/DebugFission
8667 http://gcc.gnu.org/wiki/DebugFissionDWP
8669 To simplify handling of both DWO files ("object" files with the DWARF info)
8670 and DWP files (a file with the DWOs packaged up into one file), we treat
8671 DWP files as having a collection of virtual DWO files. */
8674 hash_dwo_file (const void *item)
8676 const struct dwo_file *dwo_file = item;
8679 hash = htab_hash_string (dwo_file->dwo_name);
8680 if (dwo_file->comp_dir != NULL)
8681 hash += htab_hash_string (dwo_file->comp_dir);
8686 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8688 const struct dwo_file *lhs = item_lhs;
8689 const struct dwo_file *rhs = item_rhs;
8691 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8693 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8694 return lhs->comp_dir == rhs->comp_dir;
8695 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
8698 /* Allocate a hash table for DWO files. */
8701 allocate_dwo_file_hash_table (void)
8703 struct objfile *objfile = dwarf2_per_objfile->objfile;
8705 return htab_create_alloc_ex (41,
8709 &objfile->objfile_obstack,
8710 hashtab_obstack_allocate,
8711 dummy_obstack_deallocate);
8714 /* Lookup DWO file DWO_NAME. */
8717 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8719 struct dwo_file find_entry;
8722 if (dwarf2_per_objfile->dwo_files == NULL)
8723 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8725 memset (&find_entry, 0, sizeof (find_entry));
8726 find_entry.dwo_name = dwo_name;
8727 find_entry.comp_dir = comp_dir;
8728 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8734 hash_dwo_unit (const void *item)
8736 const struct dwo_unit *dwo_unit = item;
8738 /* This drops the top 32 bits of the id, but is ok for a hash. */
8739 return dwo_unit->signature;
8743 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8745 const struct dwo_unit *lhs = item_lhs;
8746 const struct dwo_unit *rhs = item_rhs;
8748 /* The signature is assumed to be unique within the DWO file.
8749 So while object file CU dwo_id's always have the value zero,
8750 that's OK, assuming each object file DWO file has only one CU,
8751 and that's the rule for now. */
8752 return lhs->signature == rhs->signature;
8755 /* Allocate a hash table for DWO CUs,TUs.
8756 There is one of these tables for each of CUs,TUs for each DWO file. */
8759 allocate_dwo_unit_table (struct objfile *objfile)
8761 /* Start out with a pretty small number.
8762 Generally DWO files contain only one CU and maybe some TUs. */
8763 return htab_create_alloc_ex (3,
8767 &objfile->objfile_obstack,
8768 hashtab_obstack_allocate,
8769 dummy_obstack_deallocate);
8772 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8774 struct create_dwo_cu_data
8776 struct dwo_file *dwo_file;
8777 struct dwo_unit dwo_unit;
8780 /* die_reader_func for create_dwo_cu. */
8783 create_dwo_cu_reader (const struct die_reader_specs *reader,
8784 const gdb_byte *info_ptr,
8785 struct die_info *comp_unit_die,
8789 struct dwarf2_cu *cu = reader->cu;
8790 struct objfile *objfile = dwarf2_per_objfile->objfile;
8791 sect_offset offset = cu->per_cu->offset;
8792 struct dwarf2_section_info *section = cu->per_cu->section;
8793 struct create_dwo_cu_data *data = datap;
8794 struct dwo_file *dwo_file = data->dwo_file;
8795 struct dwo_unit *dwo_unit = &data->dwo_unit;
8796 struct attribute *attr;
8798 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8801 complaint (&symfile_complaints,
8802 _("Dwarf Error: debug entry at offset 0x%x is missing"
8803 " its dwo_id [in module %s]"),
8804 offset.sect_off, dwo_file->dwo_name);
8808 dwo_unit->dwo_file = dwo_file;
8809 dwo_unit->signature = DW_UNSND (attr);
8810 dwo_unit->section = section;
8811 dwo_unit->offset = offset;
8812 dwo_unit->length = cu->per_cu->length;
8814 if (dwarf2_read_debug)
8815 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8816 offset.sect_off, hex_string (dwo_unit->signature));
8819 /* Create the dwo_unit for the lone CU in DWO_FILE.
8820 Note: This function processes DWO files only, not DWP files. */
8822 static struct dwo_unit *
8823 create_dwo_cu (struct dwo_file *dwo_file)
8825 struct objfile *objfile = dwarf2_per_objfile->objfile;
8826 struct dwarf2_section_info *section = &dwo_file->sections.info;
8829 const gdb_byte *info_ptr, *end_ptr;
8830 struct create_dwo_cu_data create_dwo_cu_data;
8831 struct dwo_unit *dwo_unit;
8833 dwarf2_read_section (objfile, section);
8834 info_ptr = section->buffer;
8836 if (info_ptr == NULL)
8839 /* We can't set abfd until now because the section may be empty or
8840 not present, in which case section->asection will be NULL. */
8841 abfd = section->asection->owner;
8843 if (dwarf2_read_debug)
8845 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8846 bfd_section_name (abfd, section->asection),
8847 bfd_get_filename (abfd));
8850 create_dwo_cu_data.dwo_file = dwo_file;
8853 end_ptr = info_ptr + section->size;
8854 while (info_ptr < end_ptr)
8856 struct dwarf2_per_cu_data per_cu;
8858 memset (&create_dwo_cu_data.dwo_unit, 0,
8859 sizeof (create_dwo_cu_data.dwo_unit));
8860 memset (&per_cu, 0, sizeof (per_cu));
8861 per_cu.objfile = objfile;
8862 per_cu.is_debug_types = 0;
8863 per_cu.offset.sect_off = info_ptr - section->buffer;
8864 per_cu.section = section;
8866 init_cutu_and_read_dies_no_follow (&per_cu,
8867 &dwo_file->sections.abbrev,
8869 create_dwo_cu_reader,
8870 &create_dwo_cu_data);
8872 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8874 /* If we've already found one, complain. We only support one
8875 because having more than one requires hacking the dwo_name of
8876 each to match, which is highly unlikely to happen. */
8877 if (dwo_unit != NULL)
8879 complaint (&symfile_complaints,
8880 _("Multiple CUs in DWO file %s [in module %s]"),
8881 dwo_file->dwo_name, objfile->name);
8885 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8886 *dwo_unit = create_dwo_cu_data.dwo_unit;
8889 info_ptr += per_cu.length;
8895 /* DWP file .debug_{cu,tu}_index section format:
8896 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8900 Both index sections have the same format, and serve to map a 64-bit
8901 signature to a set of section numbers. Each section begins with a header,
8902 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8903 indexes, and a pool of 32-bit section numbers. The index sections will be
8904 aligned at 8-byte boundaries in the file.
8906 The index section header consists of:
8908 V, 32 bit version number
8910 N, 32 bit number of compilation units or type units in the index
8911 M, 32 bit number of slots in the hash table
8913 Numbers are recorded using the byte order of the application binary.
8915 We assume that N and M will not exceed 2^32 - 1.
8917 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8919 The hash table begins at offset 16 in the section, and consists of an array
8920 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8921 order of the application binary). Unused slots in the hash table are 0.
8922 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8924 The parallel table begins immediately after the hash table
8925 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8926 array of 32-bit indexes (using the byte order of the application binary),
8927 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8928 table contains a 32-bit index into the pool of section numbers. For unused
8929 hash table slots, the corresponding entry in the parallel table will be 0.
8931 Given a 64-bit compilation unit signature or a type signature S, an entry
8932 in the hash table is located as follows:
8934 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8935 the low-order k bits all set to 1.
8937 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8939 3) If the hash table entry at index H matches the signature, use that
8940 entry. If the hash table entry at index H is unused (all zeroes),
8941 terminate the search: the signature is not present in the table.
8943 4) Let H = (H + H') modulo M. Repeat at Step 3.
8945 Because M > N and H' and M are relatively prime, the search is guaranteed
8946 to stop at an unused slot or find the match.
8948 The pool of section numbers begins immediately following the hash table
8949 (at offset 16 + 12 * M from the beginning of the section). The pool of
8950 section numbers consists of an array of 32-bit words (using the byte order
8951 of the application binary). Each item in the array is indexed starting
8952 from 0. The hash table entry provides the index of the first section
8953 number in the set. Additional section numbers in the set follow, and the
8954 set is terminated by a 0 entry (section number 0 is not used in ELF).
8956 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8957 section must be the first entry in the set, and the .debug_abbrev.dwo must
8958 be the second entry. Other members of the set may follow in any order. */
8960 /* Create a hash table to map DWO IDs to their CU/TU entry in
8961 .debug_{info,types}.dwo in DWP_FILE.
8962 Returns NULL if there isn't one.
8963 Note: This function processes DWP files only, not DWO files. */
8965 static struct dwp_hash_table *
8966 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8968 struct objfile *objfile = dwarf2_per_objfile->objfile;
8969 bfd *dbfd = dwp_file->dbfd;
8970 const gdb_byte *index_ptr, *index_end;
8971 struct dwarf2_section_info *index;
8972 uint32_t version, nr_units, nr_slots;
8973 struct dwp_hash_table *htab;
8976 index = &dwp_file->sections.tu_index;
8978 index = &dwp_file->sections.cu_index;
8980 if (dwarf2_section_empty_p (index))
8982 dwarf2_read_section (objfile, index);
8984 index_ptr = index->buffer;
8985 index_end = index_ptr + index->size;
8987 version = read_4_bytes (dbfd, index_ptr);
8988 index_ptr += 8; /* Skip the unused word. */
8989 nr_units = read_4_bytes (dbfd, index_ptr);
8991 nr_slots = read_4_bytes (dbfd, index_ptr);
8996 error (_("Dwarf Error: unsupported DWP file version (%s)"
8998 pulongest (version), dwp_file->name);
9000 if (nr_slots != (nr_slots & -nr_slots))
9002 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9003 " is not power of 2 [in module %s]"),
9004 pulongest (nr_slots), dwp_file->name);
9007 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9008 htab->nr_units = nr_units;
9009 htab->nr_slots = nr_slots;
9010 htab->hash_table = index_ptr;
9011 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9012 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9017 /* Update SECTIONS with the data from SECTP.
9019 This function is like the other "locate" section routines that are
9020 passed to bfd_map_over_sections, but in this context the sections to
9021 read comes from the DWP hash table, not the full ELF section table.
9023 The result is non-zero for success, or zero if an error was found. */
9026 locate_virtual_dwo_sections (asection *sectp,
9027 struct virtual_dwo_sections *sections)
9029 const struct dwop_section_names *names = &dwop_section_names;
9031 if (section_is_p (sectp->name, &names->abbrev_dwo))
9033 /* There can be only one. */
9034 if (sections->abbrev.asection != NULL)
9036 sections->abbrev.asection = sectp;
9037 sections->abbrev.size = bfd_get_section_size (sectp);
9039 else if (section_is_p (sectp->name, &names->info_dwo)
9040 || section_is_p (sectp->name, &names->types_dwo))
9042 /* There can be only one. */
9043 if (sections->info_or_types.asection != NULL)
9045 sections->info_or_types.asection = sectp;
9046 sections->info_or_types.size = bfd_get_section_size (sectp);
9048 else if (section_is_p (sectp->name, &names->line_dwo))
9050 /* There can be only one. */
9051 if (sections->line.asection != NULL)
9053 sections->line.asection = sectp;
9054 sections->line.size = bfd_get_section_size (sectp);
9056 else if (section_is_p (sectp->name, &names->loc_dwo))
9058 /* There can be only one. */
9059 if (sections->loc.asection != NULL)
9061 sections->loc.asection = sectp;
9062 sections->loc.size = bfd_get_section_size (sectp);
9064 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9066 /* There can be only one. */
9067 if (sections->macinfo.asection != NULL)
9069 sections->macinfo.asection = sectp;
9070 sections->macinfo.size = bfd_get_section_size (sectp);
9072 else if (section_is_p (sectp->name, &names->macro_dwo))
9074 /* There can be only one. */
9075 if (sections->macro.asection != NULL)
9077 sections->macro.asection = sectp;
9078 sections->macro.size = bfd_get_section_size (sectp);
9080 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9082 /* There can be only one. */
9083 if (sections->str_offsets.asection != NULL)
9085 sections->str_offsets.asection = sectp;
9086 sections->str_offsets.size = bfd_get_section_size (sectp);
9090 /* No other kind of section is valid. */
9097 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
9098 HTAB is the hash table from the DWP file.
9099 SECTION_INDEX is the index of the DWO in HTAB.
9100 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
9102 static struct dwo_unit *
9103 create_dwo_in_dwp (struct dwp_file *dwp_file,
9104 const struct dwp_hash_table *htab,
9105 uint32_t section_index,
9106 const char *comp_dir,
9107 ULONGEST signature, int is_debug_types)
9109 struct objfile *objfile = dwarf2_per_objfile->objfile;
9110 bfd *dbfd = dwp_file->dbfd;
9111 const char *kind = is_debug_types ? "TU" : "CU";
9112 struct dwo_file *dwo_file;
9113 struct dwo_unit *dwo_unit;
9114 struct virtual_dwo_sections sections;
9115 void **dwo_file_slot;
9116 char *virtual_dwo_name;
9117 struct dwarf2_section_info *cutu;
9118 struct cleanup *cleanups;
9121 if (dwarf2_read_debug)
9123 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
9125 pulongest (section_index), hex_string (signature),
9129 /* Fetch the sections of this DWO.
9130 Put a limit on the number of sections we look for so that bad data
9131 doesn't cause us to loop forever. */
9133 #define MAX_NR_DWO_SECTIONS \
9134 (1 /* .debug_info or .debug_types */ \
9135 + 1 /* .debug_abbrev */ \
9136 + 1 /* .debug_line */ \
9137 + 1 /* .debug_loc */ \
9138 + 1 /* .debug_str_offsets */ \
9139 + 1 /* .debug_macro */ \
9140 + 1 /* .debug_macinfo */ \
9141 + 1 /* trailing zero */)
9143 memset (§ions, 0, sizeof (sections));
9144 cleanups = make_cleanup (null_cleanup, 0);
9146 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9149 uint32_t section_nr =
9152 + (section_index + i) * sizeof (uint32_t));
9154 if (section_nr == 0)
9156 if (section_nr >= dwp_file->num_sections)
9158 error (_("Dwarf Error: bad DWP hash table, section number too large"
9163 sectp = dwp_file->elf_sections[section_nr];
9164 if (! locate_virtual_dwo_sections (sectp, §ions))
9166 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9173 || sections.info_or_types.asection == NULL
9174 || sections.abbrev.asection == NULL)
9176 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9180 if (i == MAX_NR_DWO_SECTIONS)
9182 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9187 /* It's easier for the rest of the code if we fake a struct dwo_file and
9188 have dwo_unit "live" in that. At least for now.
9190 The DWP file can be made up of a random collection of CUs and TUs.
9191 However, for each CU + set of TUs that came from the same original DWO
9192 file, we want to combine them back into a virtual DWO file to save space
9193 (fewer struct dwo_file objects to allocated). Remember that for really
9194 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9197 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9198 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9199 sections.line.asection ? sections.line.asection->id : 0,
9200 sections.loc.asection ? sections.loc.asection->id : 0,
9201 (sections.str_offsets.asection
9202 ? sections.str_offsets.asection->id
9204 make_cleanup (xfree, virtual_dwo_name);
9205 /* Can we use an existing virtual DWO file? */
9206 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9207 /* Create one if necessary. */
9208 if (*dwo_file_slot == NULL)
9210 if (dwarf2_read_debug)
9212 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9215 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9216 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9218 strlen (virtual_dwo_name));
9219 dwo_file->comp_dir = comp_dir;
9220 dwo_file->sections.abbrev = sections.abbrev;
9221 dwo_file->sections.line = sections.line;
9222 dwo_file->sections.loc = sections.loc;
9223 dwo_file->sections.macinfo = sections.macinfo;
9224 dwo_file->sections.macro = sections.macro;
9225 dwo_file->sections.str_offsets = sections.str_offsets;
9226 /* The "str" section is global to the entire DWP file. */
9227 dwo_file->sections.str = dwp_file->sections.str;
9228 /* The info or types section is assigned later to dwo_unit,
9229 there's no need to record it in dwo_file.
9230 Also, we can't simply record type sections in dwo_file because
9231 we record a pointer into the vector in dwo_unit. As we collect more
9232 types we'll grow the vector and eventually have to reallocate space
9233 for it, invalidating all the pointers into the current copy. */
9234 *dwo_file_slot = dwo_file;
9238 if (dwarf2_read_debug)
9240 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9243 dwo_file = *dwo_file_slot;
9245 do_cleanups (cleanups);
9247 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9248 dwo_unit->dwo_file = dwo_file;
9249 dwo_unit->signature = signature;
9250 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9251 sizeof (struct dwarf2_section_info));
9252 *dwo_unit->section = sections.info_or_types;
9253 /* offset, length, type_offset_in_tu are set later. */
9258 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
9260 static struct dwo_unit *
9261 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9262 const struct dwp_hash_table *htab,
9263 const char *comp_dir,
9264 ULONGEST signature, int is_debug_types)
9266 bfd *dbfd = dwp_file->dbfd;
9267 uint32_t mask = htab->nr_slots - 1;
9268 uint32_t hash = signature & mask;
9269 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9272 struct dwo_unit find_dwo_cu, *dwo_cu;
9274 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9275 find_dwo_cu.signature = signature;
9276 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9281 /* Use a for loop so that we don't loop forever on bad debug info. */
9282 for (i = 0; i < htab->nr_slots; ++i)
9284 ULONGEST signature_in_table;
9286 signature_in_table =
9287 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9288 if (signature_in_table == signature)
9290 uint32_t section_index =
9291 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9293 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
9294 comp_dir, signature, is_debug_types);
9297 if (signature_in_table == 0)
9299 hash = (hash + hash2) & mask;
9302 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9307 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9308 Open the file specified by FILE_NAME and hand it off to BFD for
9309 preliminary analysis. Return a newly initialized bfd *, which
9310 includes a canonicalized copy of FILE_NAME.
9311 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9312 SEARCH_CWD is true if the current directory is to be searched.
9313 It will be searched before debug-file-directory.
9314 If unable to find/open the file, return NULL.
9315 NOTE: This function is derived from symfile_bfd_open. */
9318 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
9322 char *absolute_name;
9323 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9324 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9325 to debug_file_directory. */
9327 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9331 if (*debug_file_directory != '\0')
9332 search_path = concat (".", dirname_separator_string,
9333 debug_file_directory, NULL);
9335 search_path = xstrdup (".");
9338 search_path = xstrdup (debug_file_directory);
9342 flags |= OPF_SEARCH_IN_PATH;
9343 desc = openp (search_path, flags, file_name,
9344 O_RDONLY | O_BINARY, &absolute_name);
9345 xfree (search_path);
9349 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
9350 xfree (absolute_name);
9351 if (sym_bfd == NULL)
9353 bfd_set_cacheable (sym_bfd, 1);
9355 if (!bfd_check_format (sym_bfd, bfd_object))
9357 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
9364 /* Try to open DWO file FILE_NAME.
9365 COMP_DIR is the DW_AT_comp_dir attribute.
9366 The result is the bfd handle of the file.
9367 If there is a problem finding or opening the file, return NULL.
9368 Upon success, the canonicalized path of the file is stored in the bfd,
9369 same as symfile_bfd_open. */
9372 open_dwo_file (const char *file_name, const char *comp_dir)
9376 if (IS_ABSOLUTE_PATH (file_name))
9377 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
9379 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9381 if (comp_dir != NULL)
9383 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
9385 /* NOTE: If comp_dir is a relative path, this will also try the
9386 search path, which seems useful. */
9387 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
9388 xfree (path_to_try);
9393 /* That didn't work, try debug-file-directory, which, despite its name,
9394 is a list of paths. */
9396 if (*debug_file_directory == '\0')
9399 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
9402 /* This function is mapped across the sections and remembers the offset and
9403 size of each of the DWO debugging sections we are interested in. */
9406 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9408 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9409 const struct dwop_section_names *names = &dwop_section_names;
9411 if (section_is_p (sectp->name, &names->abbrev_dwo))
9413 dwo_sections->abbrev.asection = sectp;
9414 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9416 else if (section_is_p (sectp->name, &names->info_dwo))
9418 dwo_sections->info.asection = sectp;
9419 dwo_sections->info.size = bfd_get_section_size (sectp);
9421 else if (section_is_p (sectp->name, &names->line_dwo))
9423 dwo_sections->line.asection = sectp;
9424 dwo_sections->line.size = bfd_get_section_size (sectp);
9426 else if (section_is_p (sectp->name, &names->loc_dwo))
9428 dwo_sections->loc.asection = sectp;
9429 dwo_sections->loc.size = bfd_get_section_size (sectp);
9431 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9433 dwo_sections->macinfo.asection = sectp;
9434 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9436 else if (section_is_p (sectp->name, &names->macro_dwo))
9438 dwo_sections->macro.asection = sectp;
9439 dwo_sections->macro.size = bfd_get_section_size (sectp);
9441 else if (section_is_p (sectp->name, &names->str_dwo))
9443 dwo_sections->str.asection = sectp;
9444 dwo_sections->str.size = bfd_get_section_size (sectp);
9446 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9448 dwo_sections->str_offsets.asection = sectp;
9449 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9451 else if (section_is_p (sectp->name, &names->types_dwo))
9453 struct dwarf2_section_info type_section;
9455 memset (&type_section, 0, sizeof (type_section));
9456 type_section.asection = sectp;
9457 type_section.size = bfd_get_section_size (sectp);
9458 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9463 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9464 by PER_CU. This is for the non-DWP case.
9465 The result is NULL if DWO_NAME can't be found. */
9467 static struct dwo_file *
9468 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9469 const char *dwo_name, const char *comp_dir)
9471 struct objfile *objfile = dwarf2_per_objfile->objfile;
9472 struct dwo_file *dwo_file;
9474 struct cleanup *cleanups;
9476 dbfd = open_dwo_file (dwo_name, comp_dir);
9479 if (dwarf2_read_debug)
9480 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9483 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9484 dwo_file->dwo_name = dwo_name;
9485 dwo_file->comp_dir = comp_dir;
9486 dwo_file->dbfd = dbfd;
9488 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9490 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9492 dwo_file->cu = create_dwo_cu (dwo_file);
9494 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9495 dwo_file->sections.types);
9497 discard_cleanups (cleanups);
9499 if (dwarf2_read_debug)
9500 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9505 /* This function is mapped across the sections and remembers the offset and
9506 size of each of the DWP debugging sections we are interested in. */
9509 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9511 struct dwp_file *dwp_file = dwp_file_ptr;
9512 const struct dwop_section_names *names = &dwop_section_names;
9513 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9515 /* Record the ELF section number for later lookup: this is what the
9516 .debug_cu_index,.debug_tu_index tables use. */
9517 gdb_assert (elf_section_nr < dwp_file->num_sections);
9518 dwp_file->elf_sections[elf_section_nr] = sectp;
9520 /* Look for specific sections that we need. */
9521 if (section_is_p (sectp->name, &names->str_dwo))
9523 dwp_file->sections.str.asection = sectp;
9524 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9526 else if (section_is_p (sectp->name, &names->cu_index))
9528 dwp_file->sections.cu_index.asection = sectp;
9529 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9531 else if (section_is_p (sectp->name, &names->tu_index))
9533 dwp_file->sections.tu_index.asection = sectp;
9534 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9538 /* Hash function for dwp_file loaded CUs/TUs. */
9541 hash_dwp_loaded_cutus (const void *item)
9543 const struct dwo_unit *dwo_unit = item;
9545 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9546 return dwo_unit->signature;
9549 /* Equality function for dwp_file loaded CUs/TUs. */
9552 eq_dwp_loaded_cutus (const void *a, const void *b)
9554 const struct dwo_unit *dua = a;
9555 const struct dwo_unit *dub = b;
9557 return dua->signature == dub->signature;
9560 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9563 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9565 return htab_create_alloc_ex (3,
9566 hash_dwp_loaded_cutus,
9567 eq_dwp_loaded_cutus,
9569 &objfile->objfile_obstack,
9570 hashtab_obstack_allocate,
9571 dummy_obstack_deallocate);
9574 /* Try to open DWP file FILE_NAME.
9575 The result is the bfd handle of the file.
9576 If there is a problem finding or opening the file, return NULL.
9577 Upon success, the canonicalized path of the file is stored in the bfd,
9578 same as symfile_bfd_open. */
9581 open_dwp_file (const char *file_name)
9585 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9589 /* Work around upstream bug 15652.
9590 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9591 [Whether that's a "bug" is debatable, but it is getting in our way.]
9592 We have no real idea where the dwp file is, because gdb's realpath-ing
9593 of the executable's path may have discarded the needed info.
9594 [IWBN if the dwp file name was recorded in the executable, akin to
9595 .gnu_debuglink, but that doesn't exist yet.]
9596 Strip the directory from FILE_NAME and search again. */
9597 if (*debug_file_directory != '\0')
9599 /* Don't implicitly search the current directory here.
9600 If the user wants to search "." to handle this case,
9601 it must be added to debug-file-directory. */
9602 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9609 /* Initialize the use of the DWP file for the current objfile.
9610 By convention the name of the DWP file is ${objfile}.dwp.
9611 The result is NULL if it can't be found. */
9613 static struct dwp_file *
9614 open_and_init_dwp_file (void)
9616 struct objfile *objfile = dwarf2_per_objfile->objfile;
9617 struct dwp_file *dwp_file;
9620 struct cleanup *cleanups;
9622 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9623 cleanups = make_cleanup (xfree, dwp_name);
9625 dbfd = open_dwp_file (dwp_name);
9628 if (dwarf2_read_debug)
9629 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9630 do_cleanups (cleanups);
9633 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9634 dwp_file->name = bfd_get_filename (dbfd);
9635 dwp_file->dbfd = dbfd;
9636 do_cleanups (cleanups);
9638 /* +1: section 0 is unused */
9639 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9640 dwp_file->elf_sections =
9641 OBSTACK_CALLOC (&objfile->objfile_obstack,
9642 dwp_file->num_sections, asection *);
9644 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9646 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9648 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9650 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9652 if (dwarf2_read_debug)
9654 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9655 fprintf_unfiltered (gdb_stdlog,
9656 " %s CUs, %s TUs\n",
9657 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9658 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9664 /* Wrapper around open_and_init_dwp_file, only open it once. */
9666 static struct dwp_file *
9669 if (! dwarf2_per_objfile->dwp_checked)
9671 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9672 dwarf2_per_objfile->dwp_checked = 1;
9674 return dwarf2_per_objfile->dwp_file;
9677 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9678 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9679 or in the DWP file for the objfile, referenced by THIS_UNIT.
9680 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9681 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9683 This is called, for example, when wanting to read a variable with a
9684 complex location. Therefore we don't want to do file i/o for every call.
9685 Therefore we don't want to look for a DWO file on every call.
9686 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9687 then we check if we've already seen DWO_NAME, and only THEN do we check
9690 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9691 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9693 static struct dwo_unit *
9694 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9695 const char *dwo_name, const char *comp_dir,
9696 ULONGEST signature, int is_debug_types)
9698 struct objfile *objfile = dwarf2_per_objfile->objfile;
9699 const char *kind = is_debug_types ? "TU" : "CU";
9700 void **dwo_file_slot;
9701 struct dwo_file *dwo_file;
9702 struct dwp_file *dwp_file;
9704 /* First see if there's a DWP file.
9705 If we have a DWP file but didn't find the DWO inside it, don't
9706 look for the original DWO file. It makes gdb behave differently
9707 depending on whether one is debugging in the build tree. */
9709 dwp_file = get_dwp_file ();
9710 if (dwp_file != NULL)
9712 const struct dwp_hash_table *dwp_htab =
9713 is_debug_types ? dwp_file->tus : dwp_file->cus;
9715 if (dwp_htab != NULL)
9717 struct dwo_unit *dwo_cutu =
9718 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9719 signature, is_debug_types);
9721 if (dwo_cutu != NULL)
9723 if (dwarf2_read_debug)
9725 fprintf_unfiltered (gdb_stdlog,
9726 "Virtual DWO %s %s found: @%s\n",
9727 kind, hex_string (signature),
9728 host_address_to_string (dwo_cutu));
9736 /* No DWP file, look for the DWO file. */
9738 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9739 if (*dwo_file_slot == NULL)
9741 /* Read in the file and build a table of the CUs/TUs it contains. */
9742 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9744 /* NOTE: This will be NULL if unable to open the file. */
9745 dwo_file = *dwo_file_slot;
9747 if (dwo_file != NULL)
9749 struct dwo_unit *dwo_cutu = NULL;
9751 if (is_debug_types && dwo_file->tus)
9753 struct dwo_unit find_dwo_cutu;
9755 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9756 find_dwo_cutu.signature = signature;
9757 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9759 else if (!is_debug_types && dwo_file->cu)
9761 if (signature == dwo_file->cu->signature)
9762 dwo_cutu = dwo_file->cu;
9765 if (dwo_cutu != NULL)
9767 if (dwarf2_read_debug)
9769 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9770 kind, dwo_name, hex_string (signature),
9771 host_address_to_string (dwo_cutu));
9778 /* We didn't find it. This could mean a dwo_id mismatch, or
9779 someone deleted the DWO/DWP file, or the search path isn't set up
9780 correctly to find the file. */
9782 if (dwarf2_read_debug)
9784 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9785 kind, dwo_name, hex_string (signature));
9788 /* This is a warning and not a complaint because it can be caused by
9789 pilot error (e.g., user accidentally deleting the DWO). */
9790 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9792 kind, dwo_name, hex_string (signature),
9793 this_unit->is_debug_types ? "TU" : "CU",
9794 this_unit->offset.sect_off, objfile->name);
9798 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9799 See lookup_dwo_cutu_unit for details. */
9801 static struct dwo_unit *
9802 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9803 const char *dwo_name, const char *comp_dir,
9806 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9809 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9810 See lookup_dwo_cutu_unit for details. */
9812 static struct dwo_unit *
9813 lookup_dwo_type_unit (struct signatured_type *this_tu,
9814 const char *dwo_name, const char *comp_dir)
9816 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9819 /* Free all resources associated with DWO_FILE.
9820 Close the DWO file and munmap the sections.
9821 All memory should be on the objfile obstack. */
9824 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9827 struct dwarf2_section_info *section;
9829 /* Note: dbfd is NULL for virtual DWO files. */
9830 gdb_bfd_unref (dwo_file->dbfd);
9832 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9835 /* Wrapper for free_dwo_file for use in cleanups. */
9838 free_dwo_file_cleanup (void *arg)
9840 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9841 struct objfile *objfile = dwarf2_per_objfile->objfile;
9843 free_dwo_file (dwo_file, objfile);
9846 /* Traversal function for free_dwo_files. */
9849 free_dwo_file_from_slot (void **slot, void *info)
9851 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9852 struct objfile *objfile = (struct objfile *) info;
9854 free_dwo_file (dwo_file, objfile);
9859 /* Free all resources associated with DWO_FILES. */
9862 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9864 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9867 /* Read in various DIEs. */
9869 /* qsort helper for inherit_abstract_dies. */
9872 unsigned_int_compar (const void *ap, const void *bp)
9874 unsigned int a = *(unsigned int *) ap;
9875 unsigned int b = *(unsigned int *) bp;
9877 return (a > b) - (b > a);
9880 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9881 Inherit only the children of the DW_AT_abstract_origin DIE not being
9882 already referenced by DW_AT_abstract_origin from the children of the
9886 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9888 struct die_info *child_die;
9889 unsigned die_children_count;
9890 /* CU offsets which were referenced by children of the current DIE. */
9891 sect_offset *offsets;
9892 sect_offset *offsets_end, *offsetp;
9893 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9894 struct die_info *origin_die;
9895 /* Iterator of the ORIGIN_DIE children. */
9896 struct die_info *origin_child_die;
9897 struct cleanup *cleanups;
9898 struct attribute *attr;
9899 struct dwarf2_cu *origin_cu;
9900 struct pending **origin_previous_list_in_scope;
9902 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9906 /* Note that following die references may follow to a die in a
9910 origin_die = follow_die_ref (die, attr, &origin_cu);
9912 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9914 origin_previous_list_in_scope = origin_cu->list_in_scope;
9915 origin_cu->list_in_scope = cu->list_in_scope;
9917 if (die->tag != origin_die->tag
9918 && !(die->tag == DW_TAG_inlined_subroutine
9919 && origin_die->tag == DW_TAG_subprogram))
9920 complaint (&symfile_complaints,
9921 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9922 die->offset.sect_off, origin_die->offset.sect_off);
9924 child_die = die->child;
9925 die_children_count = 0;
9926 while (child_die && child_die->tag)
9928 child_die = sibling_die (child_die);
9929 die_children_count++;
9931 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9932 cleanups = make_cleanup (xfree, offsets);
9934 offsets_end = offsets;
9935 child_die = die->child;
9936 while (child_die && child_die->tag)
9938 /* For each CHILD_DIE, find the corresponding child of
9939 ORIGIN_DIE. If there is more than one layer of
9940 DW_AT_abstract_origin, follow them all; there shouldn't be,
9941 but GCC versions at least through 4.4 generate this (GCC PR
9943 struct die_info *child_origin_die = child_die;
9944 struct dwarf2_cu *child_origin_cu = cu;
9948 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9952 child_origin_die = follow_die_ref (child_origin_die, attr,
9956 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9957 counterpart may exist. */
9958 if (child_origin_die != child_die)
9960 if (child_die->tag != child_origin_die->tag
9961 && !(child_die->tag == DW_TAG_inlined_subroutine
9962 && child_origin_die->tag == DW_TAG_subprogram))
9963 complaint (&symfile_complaints,
9964 _("Child DIE 0x%x and its abstract origin 0x%x have "
9965 "different tags"), child_die->offset.sect_off,
9966 child_origin_die->offset.sect_off);
9967 if (child_origin_die->parent != origin_die)
9968 complaint (&symfile_complaints,
9969 _("Child DIE 0x%x and its abstract origin 0x%x have "
9970 "different parents"), child_die->offset.sect_off,
9971 child_origin_die->offset.sect_off);
9973 *offsets_end++ = child_origin_die->offset;
9975 child_die = sibling_die (child_die);
9977 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9978 unsigned_int_compar);
9979 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9980 if (offsetp[-1].sect_off == offsetp->sect_off)
9981 complaint (&symfile_complaints,
9982 _("Multiple children of DIE 0x%x refer "
9983 "to DIE 0x%x as their abstract origin"),
9984 die->offset.sect_off, offsetp->sect_off);
9987 origin_child_die = origin_die->child;
9988 while (origin_child_die && origin_child_die->tag)
9990 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9991 while (offsetp < offsets_end
9992 && offsetp->sect_off < origin_child_die->offset.sect_off)
9994 if (offsetp >= offsets_end
9995 || offsetp->sect_off > origin_child_die->offset.sect_off)
9997 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9998 process_die (origin_child_die, origin_cu);
10000 origin_child_die = sibling_die (origin_child_die);
10002 origin_cu->list_in_scope = origin_previous_list_in_scope;
10004 do_cleanups (cleanups);
10008 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10010 struct objfile *objfile = cu->objfile;
10011 struct context_stack *new;
10014 struct die_info *child_die;
10015 struct attribute *attr, *call_line, *call_file;
10017 CORE_ADDR baseaddr;
10018 struct block *block;
10019 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10020 VEC (symbolp) *template_args = NULL;
10021 struct template_symbol *templ_func = NULL;
10025 /* If we do not have call site information, we can't show the
10026 caller of this inlined function. That's too confusing, so
10027 only use the scope for local variables. */
10028 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10029 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10030 if (call_line == NULL || call_file == NULL)
10032 read_lexical_block_scope (die, cu);
10037 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10039 name = dwarf2_name (die, cu);
10041 /* Ignore functions with missing or empty names. These are actually
10042 illegal according to the DWARF standard. */
10045 complaint (&symfile_complaints,
10046 _("missing name for subprogram DIE at %d"),
10047 die->offset.sect_off);
10051 /* Ignore functions with missing or invalid low and high pc attributes. */
10052 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10054 attr = dwarf2_attr (die, DW_AT_external, cu);
10055 if (!attr || !DW_UNSND (attr))
10056 complaint (&symfile_complaints,
10057 _("cannot get low and high bounds "
10058 "for subprogram DIE at %d"),
10059 die->offset.sect_off);
10064 highpc += baseaddr;
10066 /* If we have any template arguments, then we must allocate a
10067 different sort of symbol. */
10068 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10070 if (child_die->tag == DW_TAG_template_type_param
10071 || child_die->tag == DW_TAG_template_value_param)
10073 templ_func = allocate_template_symbol (objfile);
10074 templ_func->base.is_cplus_template_function = 1;
10079 new = push_context (0, lowpc);
10080 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10081 (struct symbol *) templ_func);
10083 /* If there is a location expression for DW_AT_frame_base, record
10085 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10087 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10089 cu->list_in_scope = &local_symbols;
10091 if (die->child != NULL)
10093 child_die = die->child;
10094 while (child_die && child_die->tag)
10096 if (child_die->tag == DW_TAG_template_type_param
10097 || child_die->tag == DW_TAG_template_value_param)
10099 struct symbol *arg = new_symbol (child_die, NULL, cu);
10102 VEC_safe_push (symbolp, template_args, arg);
10105 process_die (child_die, cu);
10106 child_die = sibling_die (child_die);
10110 inherit_abstract_dies (die, cu);
10112 /* If we have a DW_AT_specification, we might need to import using
10113 directives from the context of the specification DIE. See the
10114 comment in determine_prefix. */
10115 if (cu->language == language_cplus
10116 && dwarf2_attr (die, DW_AT_specification, cu))
10118 struct dwarf2_cu *spec_cu = cu;
10119 struct die_info *spec_die = die_specification (die, &spec_cu);
10123 child_die = spec_die->child;
10124 while (child_die && child_die->tag)
10126 if (child_die->tag == DW_TAG_imported_module)
10127 process_die (child_die, spec_cu);
10128 child_die = sibling_die (child_die);
10131 /* In some cases, GCC generates specification DIEs that
10132 themselves contain DW_AT_specification attributes. */
10133 spec_die = die_specification (spec_die, &spec_cu);
10137 new = pop_context ();
10138 /* Make a block for the local symbols within. */
10139 block = finish_block (new->name, &local_symbols, new->old_blocks,
10140 lowpc, highpc, objfile);
10142 /* For C++, set the block's scope. */
10143 if ((cu->language == language_cplus || cu->language == language_fortran)
10144 && cu->processing_has_namespace_info)
10145 block_set_scope (block, determine_prefix (die, cu),
10146 &objfile->objfile_obstack);
10148 /* If we have address ranges, record them. */
10149 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10151 /* Attach template arguments to function. */
10152 if (! VEC_empty (symbolp, template_args))
10154 gdb_assert (templ_func != NULL);
10156 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10157 templ_func->template_arguments
10158 = obstack_alloc (&objfile->objfile_obstack,
10159 (templ_func->n_template_arguments
10160 * sizeof (struct symbol *)));
10161 memcpy (templ_func->template_arguments,
10162 VEC_address (symbolp, template_args),
10163 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10164 VEC_free (symbolp, template_args);
10167 /* In C++, we can have functions nested inside functions (e.g., when
10168 a function declares a class that has methods). This means that
10169 when we finish processing a function scope, we may need to go
10170 back to building a containing block's symbol lists. */
10171 local_symbols = new->locals;
10172 using_directives = new->using_directives;
10174 /* If we've finished processing a top-level function, subsequent
10175 symbols go in the file symbol list. */
10176 if (outermost_context_p ())
10177 cu->list_in_scope = &file_symbols;
10180 /* Process all the DIES contained within a lexical block scope. Start
10181 a new scope, process the dies, and then close the scope. */
10184 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10186 struct objfile *objfile = cu->objfile;
10187 struct context_stack *new;
10188 CORE_ADDR lowpc, highpc;
10189 struct die_info *child_die;
10190 CORE_ADDR baseaddr;
10192 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10194 /* Ignore blocks with missing or invalid low and high pc attributes. */
10195 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10196 as multiple lexical blocks? Handling children in a sane way would
10197 be nasty. Might be easier to properly extend generic blocks to
10198 describe ranges. */
10199 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10202 highpc += baseaddr;
10204 push_context (0, lowpc);
10205 if (die->child != NULL)
10207 child_die = die->child;
10208 while (child_die && child_die->tag)
10210 process_die (child_die, cu);
10211 child_die = sibling_die (child_die);
10214 new = pop_context ();
10216 if (local_symbols != NULL || using_directives != NULL)
10218 struct block *block
10219 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10222 /* Note that recording ranges after traversing children, as we
10223 do here, means that recording a parent's ranges entails
10224 walking across all its children's ranges as they appear in
10225 the address map, which is quadratic behavior.
10227 It would be nicer to record the parent's ranges before
10228 traversing its children, simply overriding whatever you find
10229 there. But since we don't even decide whether to create a
10230 block until after we've traversed its children, that's hard
10232 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10234 local_symbols = new->locals;
10235 using_directives = new->using_directives;
10238 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10241 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10243 struct objfile *objfile = cu->objfile;
10244 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10245 CORE_ADDR pc, baseaddr;
10246 struct attribute *attr;
10247 struct call_site *call_site, call_site_local;
10250 struct die_info *child_die;
10252 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10254 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10257 complaint (&symfile_complaints,
10258 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10259 "DIE 0x%x [in module %s]"),
10260 die->offset.sect_off, objfile->name);
10263 pc = DW_ADDR (attr) + baseaddr;
10265 if (cu->call_site_htab == NULL)
10266 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10267 NULL, &objfile->objfile_obstack,
10268 hashtab_obstack_allocate, NULL);
10269 call_site_local.pc = pc;
10270 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10273 complaint (&symfile_complaints,
10274 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10275 "DIE 0x%x [in module %s]"),
10276 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
10280 /* Count parameters at the caller. */
10283 for (child_die = die->child; child_die && child_die->tag;
10284 child_die = sibling_die (child_die))
10286 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10288 complaint (&symfile_complaints,
10289 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10290 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10291 child_die->tag, child_die->offset.sect_off, objfile->name);
10298 call_site = obstack_alloc (&objfile->objfile_obstack,
10299 (sizeof (*call_site)
10300 + (sizeof (*call_site->parameter)
10301 * (nparams - 1))));
10303 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10304 call_site->pc = pc;
10306 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10308 struct die_info *func_die;
10310 /* Skip also over DW_TAG_inlined_subroutine. */
10311 for (func_die = die->parent;
10312 func_die && func_die->tag != DW_TAG_subprogram
10313 && func_die->tag != DW_TAG_subroutine_type;
10314 func_die = func_die->parent);
10316 /* DW_AT_GNU_all_call_sites is a superset
10317 of DW_AT_GNU_all_tail_call_sites. */
10319 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10320 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10322 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10323 not complete. But keep CALL_SITE for look ups via call_site_htab,
10324 both the initial caller containing the real return address PC and
10325 the final callee containing the current PC of a chain of tail
10326 calls do not need to have the tail call list complete. But any
10327 function candidate for a virtual tail call frame searched via
10328 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10329 determined unambiguously. */
10333 struct type *func_type = NULL;
10336 func_type = get_die_type (func_die, cu);
10337 if (func_type != NULL)
10339 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10341 /* Enlist this call site to the function. */
10342 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10343 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10346 complaint (&symfile_complaints,
10347 _("Cannot find function owning DW_TAG_GNU_call_site "
10348 "DIE 0x%x [in module %s]"),
10349 die->offset.sect_off, objfile->name);
10353 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10355 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10356 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10357 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10358 /* Keep NULL DWARF_BLOCK. */;
10359 else if (attr_form_is_block (attr))
10361 struct dwarf2_locexpr_baton *dlbaton;
10363 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10364 dlbaton->data = DW_BLOCK (attr)->data;
10365 dlbaton->size = DW_BLOCK (attr)->size;
10366 dlbaton->per_cu = cu->per_cu;
10368 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10370 else if (attr_form_is_ref (attr))
10372 struct dwarf2_cu *target_cu = cu;
10373 struct die_info *target_die;
10375 target_die = follow_die_ref (die, attr, &target_cu);
10376 gdb_assert (target_cu->objfile == objfile);
10377 if (die_is_declaration (target_die, target_cu))
10379 const char *target_physname = NULL;
10380 struct attribute *target_attr;
10382 /* Prefer the mangled name; otherwise compute the demangled one. */
10383 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10384 if (target_attr == NULL)
10385 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10387 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10388 target_physname = DW_STRING (target_attr);
10390 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10391 if (target_physname == NULL)
10392 complaint (&symfile_complaints,
10393 _("DW_AT_GNU_call_site_target target DIE has invalid "
10394 "physname, for referencing DIE 0x%x [in module %s]"),
10395 die->offset.sect_off, objfile->name);
10397 SET_FIELD_PHYSNAME (call_site->target, target_physname);
10403 /* DW_AT_entry_pc should be preferred. */
10404 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10405 complaint (&symfile_complaints,
10406 _("DW_AT_GNU_call_site_target target DIE has invalid "
10407 "low pc, for referencing DIE 0x%x [in module %s]"),
10408 die->offset.sect_off, objfile->name);
10410 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10414 complaint (&symfile_complaints,
10415 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10416 "block nor reference, for DIE 0x%x [in module %s]"),
10417 die->offset.sect_off, objfile->name);
10419 call_site->per_cu = cu->per_cu;
10421 for (child_die = die->child;
10422 child_die && child_die->tag;
10423 child_die = sibling_die (child_die))
10425 struct call_site_parameter *parameter;
10426 struct attribute *loc, *origin;
10428 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10430 /* Already printed the complaint above. */
10434 gdb_assert (call_site->parameter_count < nparams);
10435 parameter = &call_site->parameter[call_site->parameter_count];
10437 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10438 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10439 register is contained in DW_AT_GNU_call_site_value. */
10441 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10442 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10443 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
10445 sect_offset offset;
10447 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10448 offset = dwarf2_get_ref_die_offset (origin);
10449 if (!offset_in_cu_p (&cu->header, offset))
10451 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10452 binding can be done only inside one CU. Such referenced DIE
10453 therefore cannot be even moved to DW_TAG_partial_unit. */
10454 complaint (&symfile_complaints,
10455 _("DW_AT_abstract_origin offset is not in CU for "
10456 "DW_TAG_GNU_call_site child DIE 0x%x "
10458 child_die->offset.sect_off, objfile->name);
10461 parameter->u.param_offset.cu_off = (offset.sect_off
10462 - cu->header.offset.sect_off);
10464 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10466 complaint (&symfile_complaints,
10467 _("No DW_FORM_block* DW_AT_location for "
10468 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10469 child_die->offset.sect_off, objfile->name);
10474 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10475 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10476 if (parameter->u.dwarf_reg != -1)
10477 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10478 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10479 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10480 ¶meter->u.fb_offset))
10481 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10484 complaint (&symfile_complaints,
10485 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10486 "for DW_FORM_block* DW_AT_location is supported for "
10487 "DW_TAG_GNU_call_site child DIE 0x%x "
10489 child_die->offset.sect_off, objfile->name);
10494 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10495 if (!attr_form_is_block (attr))
10497 complaint (&symfile_complaints,
10498 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10499 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10500 child_die->offset.sect_off, objfile->name);
10503 parameter->value = DW_BLOCK (attr)->data;
10504 parameter->value_size = DW_BLOCK (attr)->size;
10506 /* Parameters are not pre-cleared by memset above. */
10507 parameter->data_value = NULL;
10508 parameter->data_value_size = 0;
10509 call_site->parameter_count++;
10511 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10514 if (!attr_form_is_block (attr))
10515 complaint (&symfile_complaints,
10516 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10517 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10518 child_die->offset.sect_off, objfile->name);
10521 parameter->data_value = DW_BLOCK (attr)->data;
10522 parameter->data_value_size = DW_BLOCK (attr)->size;
10528 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10529 Return 1 if the attributes are present and valid, otherwise, return 0.
10530 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10533 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10534 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10535 struct partial_symtab *ranges_pst)
10537 struct objfile *objfile = cu->objfile;
10538 struct comp_unit_head *cu_header = &cu->header;
10539 bfd *obfd = objfile->obfd;
10540 unsigned int addr_size = cu_header->addr_size;
10541 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10542 /* Base address selection entry. */
10545 unsigned int dummy;
10546 const gdb_byte *buffer;
10550 CORE_ADDR high = 0;
10551 CORE_ADDR baseaddr;
10553 found_base = cu->base_known;
10554 base = cu->base_address;
10556 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10557 if (offset >= dwarf2_per_objfile->ranges.size)
10559 complaint (&symfile_complaints,
10560 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10564 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10566 /* Read in the largest possible address. */
10567 marker = read_address (obfd, buffer, cu, &dummy);
10568 if ((marker & mask) == mask)
10570 /* If we found the largest possible address, then
10571 read the base address. */
10572 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10573 buffer += 2 * addr_size;
10574 offset += 2 * addr_size;
10580 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10584 CORE_ADDR range_beginning, range_end;
10586 range_beginning = read_address (obfd, buffer, cu, &dummy);
10587 buffer += addr_size;
10588 range_end = read_address (obfd, buffer, cu, &dummy);
10589 buffer += addr_size;
10590 offset += 2 * addr_size;
10592 /* An end of list marker is a pair of zero addresses. */
10593 if (range_beginning == 0 && range_end == 0)
10594 /* Found the end of list entry. */
10597 /* Each base address selection entry is a pair of 2 values.
10598 The first is the largest possible address, the second is
10599 the base address. Check for a base address here. */
10600 if ((range_beginning & mask) == mask)
10602 /* If we found the largest possible address, then
10603 read the base address. */
10604 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10611 /* We have no valid base address for the ranges
10613 complaint (&symfile_complaints,
10614 _("Invalid .debug_ranges data (no base address)"));
10618 if (range_beginning > range_end)
10620 /* Inverted range entries are invalid. */
10621 complaint (&symfile_complaints,
10622 _("Invalid .debug_ranges data (inverted range)"));
10626 /* Empty range entries have no effect. */
10627 if (range_beginning == range_end)
10630 range_beginning += base;
10633 /* A not-uncommon case of bad debug info.
10634 Don't pollute the addrmap with bad data. */
10635 if (range_beginning + baseaddr == 0
10636 && !dwarf2_per_objfile->has_section_at_zero)
10638 complaint (&symfile_complaints,
10639 _(".debug_ranges entry has start address of zero"
10640 " [in module %s]"), objfile->name);
10644 if (ranges_pst != NULL)
10645 addrmap_set_empty (objfile->psymtabs_addrmap,
10646 range_beginning + baseaddr,
10647 range_end - 1 + baseaddr,
10650 /* FIXME: This is recording everything as a low-high
10651 segment of consecutive addresses. We should have a
10652 data structure for discontiguous block ranges
10656 low = range_beginning;
10662 if (range_beginning < low)
10663 low = range_beginning;
10664 if (range_end > high)
10670 /* If the first entry is an end-of-list marker, the range
10671 describes an empty scope, i.e. no instructions. */
10677 *high_return = high;
10681 /* Get low and high pc attributes from a die. Return 1 if the attributes
10682 are present and valid, otherwise, return 0. Return -1 if the range is
10683 discontinuous, i.e. derived from DW_AT_ranges information. */
10686 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10687 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10688 struct partial_symtab *pst)
10690 struct attribute *attr;
10691 struct attribute *attr_high;
10693 CORE_ADDR high = 0;
10696 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10699 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10702 low = DW_ADDR (attr);
10703 if (attr_high->form == DW_FORM_addr
10704 || attr_high->form == DW_FORM_GNU_addr_index)
10705 high = DW_ADDR (attr_high);
10707 high = low + DW_UNSND (attr_high);
10710 /* Found high w/o low attribute. */
10713 /* Found consecutive range of addresses. */
10718 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10721 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10722 We take advantage of the fact that DW_AT_ranges does not appear
10723 in DW_TAG_compile_unit of DWO files. */
10724 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10725 unsigned int ranges_offset = (DW_UNSND (attr)
10726 + (need_ranges_base
10730 /* Value of the DW_AT_ranges attribute is the offset in the
10731 .debug_ranges section. */
10732 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10734 /* Found discontinuous range of addresses. */
10739 /* read_partial_die has also the strict LOW < HIGH requirement. */
10743 /* When using the GNU linker, .gnu.linkonce. sections are used to
10744 eliminate duplicate copies of functions and vtables and such.
10745 The linker will arbitrarily choose one and discard the others.
10746 The AT_*_pc values for such functions refer to local labels in
10747 these sections. If the section from that file was discarded, the
10748 labels are not in the output, so the relocs get a value of 0.
10749 If this is a discarded function, mark the pc bounds as invalid,
10750 so that GDB will ignore it. */
10751 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10760 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10761 its low and high PC addresses. Do nothing if these addresses could not
10762 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10763 and HIGHPC to the high address if greater than HIGHPC. */
10766 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10767 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10768 struct dwarf2_cu *cu)
10770 CORE_ADDR low, high;
10771 struct die_info *child = die->child;
10773 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10775 *lowpc = min (*lowpc, low);
10776 *highpc = max (*highpc, high);
10779 /* If the language does not allow nested subprograms (either inside
10780 subprograms or lexical blocks), we're done. */
10781 if (cu->language != language_ada)
10784 /* Check all the children of the given DIE. If it contains nested
10785 subprograms, then check their pc bounds. Likewise, we need to
10786 check lexical blocks as well, as they may also contain subprogram
10788 while (child && child->tag)
10790 if (child->tag == DW_TAG_subprogram
10791 || child->tag == DW_TAG_lexical_block)
10792 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10793 child = sibling_die (child);
10797 /* Get the low and high pc's represented by the scope DIE, and store
10798 them in *LOWPC and *HIGHPC. If the correct values can't be
10799 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10802 get_scope_pc_bounds (struct die_info *die,
10803 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10804 struct dwarf2_cu *cu)
10806 CORE_ADDR best_low = (CORE_ADDR) -1;
10807 CORE_ADDR best_high = (CORE_ADDR) 0;
10808 CORE_ADDR current_low, current_high;
10810 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
10812 best_low = current_low;
10813 best_high = current_high;
10817 struct die_info *child = die->child;
10819 while (child && child->tag)
10821 switch (child->tag) {
10822 case DW_TAG_subprogram:
10823 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10825 case DW_TAG_namespace:
10826 case DW_TAG_module:
10827 /* FIXME: carlton/2004-01-16: Should we do this for
10828 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10829 that current GCC's always emit the DIEs corresponding
10830 to definitions of methods of classes as children of a
10831 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10832 the DIEs giving the declarations, which could be
10833 anywhere). But I don't see any reason why the
10834 standards says that they have to be there. */
10835 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
10837 if (current_low != ((CORE_ADDR) -1))
10839 best_low = min (best_low, current_low);
10840 best_high = max (best_high, current_high);
10848 child = sibling_die (child);
10853 *highpc = best_high;
10856 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10860 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10861 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10863 struct objfile *objfile = cu->objfile;
10864 struct attribute *attr;
10865 struct attribute *attr_high;
10867 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10870 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10873 CORE_ADDR low = DW_ADDR (attr);
10875 if (attr_high->form == DW_FORM_addr
10876 || attr_high->form == DW_FORM_GNU_addr_index)
10877 high = DW_ADDR (attr_high);
10879 high = low + DW_UNSND (attr_high);
10881 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10885 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10888 bfd *obfd = objfile->obfd;
10889 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10890 We take advantage of the fact that DW_AT_ranges does not appear
10891 in DW_TAG_compile_unit of DWO files. */
10892 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10894 /* The value of the DW_AT_ranges attribute is the offset of the
10895 address range list in the .debug_ranges section. */
10896 unsigned long offset = (DW_UNSND (attr)
10897 + (need_ranges_base ? cu->ranges_base : 0));
10898 const gdb_byte *buffer;
10900 /* For some target architectures, but not others, the
10901 read_address function sign-extends the addresses it returns.
10902 To recognize base address selection entries, we need a
10904 unsigned int addr_size = cu->header.addr_size;
10905 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10907 /* The base address, to which the next pair is relative. Note
10908 that this 'base' is a DWARF concept: most entries in a range
10909 list are relative, to reduce the number of relocs against the
10910 debugging information. This is separate from this function's
10911 'baseaddr' argument, which GDB uses to relocate debugging
10912 information from a shared library based on the address at
10913 which the library was loaded. */
10914 CORE_ADDR base = cu->base_address;
10915 int base_known = cu->base_known;
10917 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10918 if (offset >= dwarf2_per_objfile->ranges.size)
10920 complaint (&symfile_complaints,
10921 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10925 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10929 unsigned int bytes_read;
10930 CORE_ADDR start, end;
10932 start = read_address (obfd, buffer, cu, &bytes_read);
10933 buffer += bytes_read;
10934 end = read_address (obfd, buffer, cu, &bytes_read);
10935 buffer += bytes_read;
10937 /* Did we find the end of the range list? */
10938 if (start == 0 && end == 0)
10941 /* Did we find a base address selection entry? */
10942 else if ((start & base_select_mask) == base_select_mask)
10948 /* We found an ordinary address range. */
10953 complaint (&symfile_complaints,
10954 _("Invalid .debug_ranges data "
10955 "(no base address)"));
10961 /* Inverted range entries are invalid. */
10962 complaint (&symfile_complaints,
10963 _("Invalid .debug_ranges data "
10964 "(inverted range)"));
10968 /* Empty range entries have no effect. */
10972 start += base + baseaddr;
10973 end += base + baseaddr;
10975 /* A not-uncommon case of bad debug info.
10976 Don't pollute the addrmap with bad data. */
10977 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10979 complaint (&symfile_complaints,
10980 _(".debug_ranges entry has start address of zero"
10981 " [in module %s]"), objfile->name);
10985 record_block_range (block, start, end - 1);
10991 /* Check whether the producer field indicates either of GCC < 4.6, or the
10992 Intel C/C++ compiler, and cache the result in CU. */
10995 check_producer (struct dwarf2_cu *cu)
10998 int major, minor, release;
11000 if (cu->producer == NULL)
11002 /* For unknown compilers expect their behavior is DWARF version
11005 GCC started to support .debug_types sections by -gdwarf-4 since
11006 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11007 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11008 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11009 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11011 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
11013 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11015 cs = &cu->producer[strlen ("GNU ")];
11016 while (*cs && !isdigit (*cs))
11018 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11020 /* Not recognized as GCC. */
11024 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11025 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11028 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11029 cu->producer_is_icc = 1;
11032 /* For other non-GCC compilers, expect their behavior is DWARF version
11036 cu->checked_producer = 1;
11039 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11040 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11041 during 4.6.0 experimental. */
11044 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11046 if (!cu->checked_producer)
11047 check_producer (cu);
11049 return cu->producer_is_gxx_lt_4_6;
11052 /* Return the default accessibility type if it is not overriden by
11053 DW_AT_accessibility. */
11055 static enum dwarf_access_attribute
11056 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11058 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11060 /* The default DWARF 2 accessibility for members is public, the default
11061 accessibility for inheritance is private. */
11063 if (die->tag != DW_TAG_inheritance)
11064 return DW_ACCESS_public;
11066 return DW_ACCESS_private;
11070 /* DWARF 3+ defines the default accessibility a different way. The same
11071 rules apply now for DW_TAG_inheritance as for the members and it only
11072 depends on the container kind. */
11074 if (die->parent->tag == DW_TAG_class_type)
11075 return DW_ACCESS_private;
11077 return DW_ACCESS_public;
11081 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11082 offset. If the attribute was not found return 0, otherwise return
11083 1. If it was found but could not properly be handled, set *OFFSET
11087 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11090 struct attribute *attr;
11092 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11097 /* Note that we do not check for a section offset first here.
11098 This is because DW_AT_data_member_location is new in DWARF 4,
11099 so if we see it, we can assume that a constant form is really
11100 a constant and not a section offset. */
11101 if (attr_form_is_constant (attr))
11102 *offset = dwarf2_get_attr_constant_value (attr, 0);
11103 else if (attr_form_is_section_offset (attr))
11104 dwarf2_complex_location_expr_complaint ();
11105 else if (attr_form_is_block (attr))
11106 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11108 dwarf2_complex_location_expr_complaint ();
11116 /* Add an aggregate field to the field list. */
11119 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11120 struct dwarf2_cu *cu)
11122 struct objfile *objfile = cu->objfile;
11123 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11124 struct nextfield *new_field;
11125 struct attribute *attr;
11127 const char *fieldname = "";
11129 /* Allocate a new field list entry and link it in. */
11130 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11131 make_cleanup (xfree, new_field);
11132 memset (new_field, 0, sizeof (struct nextfield));
11134 if (die->tag == DW_TAG_inheritance)
11136 new_field->next = fip->baseclasses;
11137 fip->baseclasses = new_field;
11141 new_field->next = fip->fields;
11142 fip->fields = new_field;
11146 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11148 new_field->accessibility = DW_UNSND (attr);
11150 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11151 if (new_field->accessibility != DW_ACCESS_public)
11152 fip->non_public_fields = 1;
11154 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11156 new_field->virtuality = DW_UNSND (attr);
11158 new_field->virtuality = DW_VIRTUALITY_none;
11160 fp = &new_field->field;
11162 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11166 /* Data member other than a C++ static data member. */
11168 /* Get type of field. */
11169 fp->type = die_type (die, cu);
11171 SET_FIELD_BITPOS (*fp, 0);
11173 /* Get bit size of field (zero if none). */
11174 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11177 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11181 FIELD_BITSIZE (*fp) = 0;
11184 /* Get bit offset of field. */
11185 if (handle_data_member_location (die, cu, &offset))
11186 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11187 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11190 if (gdbarch_bits_big_endian (gdbarch))
11192 /* For big endian bits, the DW_AT_bit_offset gives the
11193 additional bit offset from the MSB of the containing
11194 anonymous object to the MSB of the field. We don't
11195 have to do anything special since we don't need to
11196 know the size of the anonymous object. */
11197 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
11201 /* For little endian bits, compute the bit offset to the
11202 MSB of the anonymous object, subtract off the number of
11203 bits from the MSB of the field to the MSB of the
11204 object, and then subtract off the number of bits of
11205 the field itself. The result is the bit offset of
11206 the LSB of the field. */
11207 int anonymous_size;
11208 int bit_offset = DW_UNSND (attr);
11210 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11213 /* The size of the anonymous object containing
11214 the bit field is explicit, so use the
11215 indicated size (in bytes). */
11216 anonymous_size = DW_UNSND (attr);
11220 /* The size of the anonymous object containing
11221 the bit field must be inferred from the type
11222 attribute of the data member containing the
11224 anonymous_size = TYPE_LENGTH (fp->type);
11226 SET_FIELD_BITPOS (*fp,
11227 (FIELD_BITPOS (*fp)
11228 + anonymous_size * bits_per_byte
11229 - bit_offset - FIELD_BITSIZE (*fp)));
11233 /* Get name of field. */
11234 fieldname = dwarf2_name (die, cu);
11235 if (fieldname == NULL)
11238 /* The name is already allocated along with this objfile, so we don't
11239 need to duplicate it for the type. */
11240 fp->name = fieldname;
11242 /* Change accessibility for artificial fields (e.g. virtual table
11243 pointer or virtual base class pointer) to private. */
11244 if (dwarf2_attr (die, DW_AT_artificial, cu))
11246 FIELD_ARTIFICIAL (*fp) = 1;
11247 new_field->accessibility = DW_ACCESS_private;
11248 fip->non_public_fields = 1;
11251 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11253 /* C++ static member. */
11255 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11256 is a declaration, but all versions of G++ as of this writing
11257 (so through at least 3.2.1) incorrectly generate
11258 DW_TAG_variable tags. */
11260 const char *physname;
11262 /* Get name of field. */
11263 fieldname = dwarf2_name (die, cu);
11264 if (fieldname == NULL)
11267 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11269 /* Only create a symbol if this is an external value.
11270 new_symbol checks this and puts the value in the global symbol
11271 table, which we want. If it is not external, new_symbol
11272 will try to put the value in cu->list_in_scope which is wrong. */
11273 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11275 /* A static const member, not much different than an enum as far as
11276 we're concerned, except that we can support more types. */
11277 new_symbol (die, NULL, cu);
11280 /* Get physical name. */
11281 physname = dwarf2_physname (fieldname, die, cu);
11283 /* The name is already allocated along with this objfile, so we don't
11284 need to duplicate it for the type. */
11285 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
11286 FIELD_TYPE (*fp) = die_type (die, cu);
11287 FIELD_NAME (*fp) = fieldname;
11289 else if (die->tag == DW_TAG_inheritance)
11293 /* C++ base class field. */
11294 if (handle_data_member_location (die, cu, &offset))
11295 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11296 FIELD_BITSIZE (*fp) = 0;
11297 FIELD_TYPE (*fp) = die_type (die, cu);
11298 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11299 fip->nbaseclasses++;
11303 /* Add a typedef defined in the scope of the FIP's class. */
11306 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11307 struct dwarf2_cu *cu)
11309 struct objfile *objfile = cu->objfile;
11310 struct typedef_field_list *new_field;
11311 struct attribute *attr;
11312 struct typedef_field *fp;
11313 char *fieldname = "";
11315 /* Allocate a new field list entry and link it in. */
11316 new_field = xzalloc (sizeof (*new_field));
11317 make_cleanup (xfree, new_field);
11319 gdb_assert (die->tag == DW_TAG_typedef);
11321 fp = &new_field->field;
11323 /* Get name of field. */
11324 fp->name = dwarf2_name (die, cu);
11325 if (fp->name == NULL)
11328 fp->type = read_type_die (die, cu);
11330 new_field->next = fip->typedef_field_list;
11331 fip->typedef_field_list = new_field;
11332 fip->typedef_field_list_count++;
11335 /* Create the vector of fields, and attach it to the type. */
11338 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11339 struct dwarf2_cu *cu)
11341 int nfields = fip->nfields;
11343 /* Record the field count, allocate space for the array of fields,
11344 and create blank accessibility bitfields if necessary. */
11345 TYPE_NFIELDS (type) = nfields;
11346 TYPE_FIELDS (type) = (struct field *)
11347 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11348 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11350 if (fip->non_public_fields && cu->language != language_ada)
11352 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11354 TYPE_FIELD_PRIVATE_BITS (type) =
11355 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11356 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11358 TYPE_FIELD_PROTECTED_BITS (type) =
11359 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11360 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11362 TYPE_FIELD_IGNORE_BITS (type) =
11363 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11364 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
11367 /* If the type has baseclasses, allocate and clear a bit vector for
11368 TYPE_FIELD_VIRTUAL_BITS. */
11369 if (fip->nbaseclasses && cu->language != language_ada)
11371 int num_bytes = B_BYTES (fip->nbaseclasses);
11372 unsigned char *pointer;
11374 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11375 pointer = TYPE_ALLOC (type, num_bytes);
11376 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
11377 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11378 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11381 /* Copy the saved-up fields into the field vector. Start from the head of
11382 the list, adding to the tail of the field array, so that they end up in
11383 the same order in the array in which they were added to the list. */
11384 while (nfields-- > 0)
11386 struct nextfield *fieldp;
11390 fieldp = fip->fields;
11391 fip->fields = fieldp->next;
11395 fieldp = fip->baseclasses;
11396 fip->baseclasses = fieldp->next;
11399 TYPE_FIELD (type, nfields) = fieldp->field;
11400 switch (fieldp->accessibility)
11402 case DW_ACCESS_private:
11403 if (cu->language != language_ada)
11404 SET_TYPE_FIELD_PRIVATE (type, nfields);
11407 case DW_ACCESS_protected:
11408 if (cu->language != language_ada)
11409 SET_TYPE_FIELD_PROTECTED (type, nfields);
11412 case DW_ACCESS_public:
11416 /* Unknown accessibility. Complain and treat it as public. */
11418 complaint (&symfile_complaints, _("unsupported accessibility %d"),
11419 fieldp->accessibility);
11423 if (nfields < fip->nbaseclasses)
11425 switch (fieldp->virtuality)
11427 case DW_VIRTUALITY_virtual:
11428 case DW_VIRTUALITY_pure_virtual:
11429 if (cu->language == language_ada)
11430 error (_("unexpected virtuality in component of Ada type"));
11431 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11438 /* Return true if this member function is a constructor, false
11442 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11444 const char *fieldname;
11445 const char *typename;
11448 if (die->parent == NULL)
11451 if (die->parent->tag != DW_TAG_structure_type
11452 && die->parent->tag != DW_TAG_union_type
11453 && die->parent->tag != DW_TAG_class_type)
11456 fieldname = dwarf2_name (die, cu);
11457 typename = dwarf2_name (die->parent, cu);
11458 if (fieldname == NULL || typename == NULL)
11461 len = strlen (fieldname);
11462 return (strncmp (fieldname, typename, len) == 0
11463 && (typename[len] == '\0' || typename[len] == '<'));
11466 /* Add a member function to the proper fieldlist. */
11469 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11470 struct type *type, struct dwarf2_cu *cu)
11472 struct objfile *objfile = cu->objfile;
11473 struct attribute *attr;
11474 struct fnfieldlist *flp;
11476 struct fn_field *fnp;
11477 const char *fieldname;
11478 struct nextfnfield *new_fnfield;
11479 struct type *this_type;
11480 enum dwarf_access_attribute accessibility;
11482 if (cu->language == language_ada)
11483 error (_("unexpected member function in Ada type"));
11485 /* Get name of member function. */
11486 fieldname = dwarf2_name (die, cu);
11487 if (fieldname == NULL)
11490 /* Look up member function name in fieldlist. */
11491 for (i = 0; i < fip->nfnfields; i++)
11493 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11497 /* Create new list element if necessary. */
11498 if (i < fip->nfnfields)
11499 flp = &fip->fnfieldlists[i];
11502 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11504 fip->fnfieldlists = (struct fnfieldlist *)
11505 xrealloc (fip->fnfieldlists,
11506 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11507 * sizeof (struct fnfieldlist));
11508 if (fip->nfnfields == 0)
11509 make_cleanup (free_current_contents, &fip->fnfieldlists);
11511 flp = &fip->fnfieldlists[fip->nfnfields];
11512 flp->name = fieldname;
11515 i = fip->nfnfields++;
11518 /* Create a new member function field and chain it to the field list
11520 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11521 make_cleanup (xfree, new_fnfield);
11522 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11523 new_fnfield->next = flp->head;
11524 flp->head = new_fnfield;
11527 /* Fill in the member function field info. */
11528 fnp = &new_fnfield->fnfield;
11530 /* Delay processing of the physname until later. */
11531 if (cu->language == language_cplus || cu->language == language_java)
11533 add_to_method_list (type, i, flp->length - 1, fieldname,
11538 const char *physname = dwarf2_physname (fieldname, die, cu);
11539 fnp->physname = physname ? physname : "";
11542 fnp->type = alloc_type (objfile);
11543 this_type = read_type_die (die, cu);
11544 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11546 int nparams = TYPE_NFIELDS (this_type);
11548 /* TYPE is the domain of this method, and THIS_TYPE is the type
11549 of the method itself (TYPE_CODE_METHOD). */
11550 smash_to_method_type (fnp->type, type,
11551 TYPE_TARGET_TYPE (this_type),
11552 TYPE_FIELDS (this_type),
11553 TYPE_NFIELDS (this_type),
11554 TYPE_VARARGS (this_type));
11556 /* Handle static member functions.
11557 Dwarf2 has no clean way to discern C++ static and non-static
11558 member functions. G++ helps GDB by marking the first
11559 parameter for non-static member functions (which is the this
11560 pointer) as artificial. We obtain this information from
11561 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11562 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11563 fnp->voffset = VOFFSET_STATIC;
11566 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11567 dwarf2_full_name (fieldname, die, cu));
11569 /* Get fcontext from DW_AT_containing_type if present. */
11570 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11571 fnp->fcontext = die_containing_type (die, cu);
11573 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11574 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11576 /* Get accessibility. */
11577 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11579 accessibility = DW_UNSND (attr);
11581 accessibility = dwarf2_default_access_attribute (die, cu);
11582 switch (accessibility)
11584 case DW_ACCESS_private:
11585 fnp->is_private = 1;
11587 case DW_ACCESS_protected:
11588 fnp->is_protected = 1;
11592 /* Check for artificial methods. */
11593 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11594 if (attr && DW_UNSND (attr) != 0)
11595 fnp->is_artificial = 1;
11597 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11599 /* Get index in virtual function table if it is a virtual member
11600 function. For older versions of GCC, this is an offset in the
11601 appropriate virtual table, as specified by DW_AT_containing_type.
11602 For everyone else, it is an expression to be evaluated relative
11603 to the object address. */
11605 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11608 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11610 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11612 /* Old-style GCC. */
11613 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11615 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11616 || (DW_BLOCK (attr)->size > 1
11617 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11618 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11620 struct dwarf_block blk;
11623 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11625 blk.size = DW_BLOCK (attr)->size - offset;
11626 blk.data = DW_BLOCK (attr)->data + offset;
11627 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11628 if ((fnp->voffset % cu->header.addr_size) != 0)
11629 dwarf2_complex_location_expr_complaint ();
11631 fnp->voffset /= cu->header.addr_size;
11635 dwarf2_complex_location_expr_complaint ();
11637 if (!fnp->fcontext)
11638 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11640 else if (attr_form_is_section_offset (attr))
11642 dwarf2_complex_location_expr_complaint ();
11646 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11652 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11653 if (attr && DW_UNSND (attr))
11655 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11656 complaint (&symfile_complaints,
11657 _("Member function \"%s\" (offset %d) is virtual "
11658 "but the vtable offset is not specified"),
11659 fieldname, die->offset.sect_off);
11660 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11661 TYPE_CPLUS_DYNAMIC (type) = 1;
11666 /* Create the vector of member function fields, and attach it to the type. */
11669 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11670 struct dwarf2_cu *cu)
11672 struct fnfieldlist *flp;
11675 if (cu->language == language_ada)
11676 error (_("unexpected member functions in Ada type"));
11678 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11679 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11680 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11682 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11684 struct nextfnfield *nfp = flp->head;
11685 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11688 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11689 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11690 fn_flp->fn_fields = (struct fn_field *)
11691 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11692 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11693 fn_flp->fn_fields[k] = nfp->fnfield;
11696 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11699 /* Returns non-zero if NAME is the name of a vtable member in CU's
11700 language, zero otherwise. */
11702 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11704 static const char vptr[] = "_vptr";
11705 static const char vtable[] = "vtable";
11707 /* Look for the C++ and Java forms of the vtable. */
11708 if ((cu->language == language_java
11709 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11710 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11711 && is_cplus_marker (name[sizeof (vptr) - 1])))
11717 /* GCC outputs unnamed structures that are really pointers to member
11718 functions, with the ABI-specified layout. If TYPE describes
11719 such a structure, smash it into a member function type.
11721 GCC shouldn't do this; it should just output pointer to member DIEs.
11722 This is GCC PR debug/28767. */
11725 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11727 struct type *pfn_type, *domain_type, *new_type;
11729 /* Check for a structure with no name and two children. */
11730 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11733 /* Check for __pfn and __delta members. */
11734 if (TYPE_FIELD_NAME (type, 0) == NULL
11735 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11736 || TYPE_FIELD_NAME (type, 1) == NULL
11737 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11740 /* Find the type of the method. */
11741 pfn_type = TYPE_FIELD_TYPE (type, 0);
11742 if (pfn_type == NULL
11743 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11744 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11747 /* Look for the "this" argument. */
11748 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11749 if (TYPE_NFIELDS (pfn_type) == 0
11750 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11751 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11754 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11755 new_type = alloc_type (objfile);
11756 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11757 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11758 TYPE_VARARGS (pfn_type));
11759 smash_to_methodptr_type (type, new_type);
11762 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11766 producer_is_icc (struct dwarf2_cu *cu)
11768 if (!cu->checked_producer)
11769 check_producer (cu);
11771 return cu->producer_is_icc;
11774 /* Called when we find the DIE that starts a structure or union scope
11775 (definition) to create a type for the structure or union. Fill in
11776 the type's name and general properties; the members will not be
11777 processed until process_structure_scope.
11779 NOTE: we need to call these functions regardless of whether or not the
11780 DIE has a DW_AT_name attribute, since it might be an anonymous
11781 structure or union. This gets the type entered into our set of
11782 user defined types.
11784 However, if the structure is incomplete (an opaque struct/union)
11785 then suppress creating a symbol table entry for it since gdb only
11786 wants to find the one with the complete definition. Note that if
11787 it is complete, we just call new_symbol, which does it's own
11788 checking about whether the struct/union is anonymous or not (and
11789 suppresses creating a symbol table entry itself). */
11791 static struct type *
11792 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11794 struct objfile *objfile = cu->objfile;
11796 struct attribute *attr;
11799 /* If the definition of this type lives in .debug_types, read that type.
11800 Don't follow DW_AT_specification though, that will take us back up
11801 the chain and we want to go down. */
11802 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11805 type = get_DW_AT_signature_type (die, attr, cu);
11807 /* The type's CU may not be the same as CU.
11808 Ensure TYPE is recorded with CU in die_type_hash. */
11809 return set_die_type (die, type, cu);
11812 type = alloc_type (objfile);
11813 INIT_CPLUS_SPECIFIC (type);
11815 name = dwarf2_name (die, cu);
11818 if (cu->language == language_cplus
11819 || cu->language == language_java)
11821 const char *full_name = dwarf2_full_name (name, die, cu);
11823 /* dwarf2_full_name might have already finished building the DIE's
11824 type. If so, there is no need to continue. */
11825 if (get_die_type (die, cu) != NULL)
11826 return get_die_type (die, cu);
11828 TYPE_TAG_NAME (type) = full_name;
11829 if (die->tag == DW_TAG_structure_type
11830 || die->tag == DW_TAG_class_type)
11831 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11835 /* The name is already allocated along with this objfile, so
11836 we don't need to duplicate it for the type. */
11837 TYPE_TAG_NAME (type) = name;
11838 if (die->tag == DW_TAG_class_type)
11839 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11843 if (die->tag == DW_TAG_structure_type)
11845 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11847 else if (die->tag == DW_TAG_union_type)
11849 TYPE_CODE (type) = TYPE_CODE_UNION;
11853 TYPE_CODE (type) = TYPE_CODE_CLASS;
11856 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11857 TYPE_DECLARED_CLASS (type) = 1;
11859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11862 TYPE_LENGTH (type) = DW_UNSND (attr);
11866 TYPE_LENGTH (type) = 0;
11869 if (producer_is_icc (cu))
11871 /* ICC does not output the required DW_AT_declaration
11872 on incomplete types, but gives them a size of zero. */
11875 TYPE_STUB_SUPPORTED (type) = 1;
11877 if (die_is_declaration (die, cu))
11878 TYPE_STUB (type) = 1;
11879 else if (attr == NULL && die->child == NULL
11880 && producer_is_realview (cu->producer))
11881 /* RealView does not output the required DW_AT_declaration
11882 on incomplete types. */
11883 TYPE_STUB (type) = 1;
11885 /* We need to add the type field to the die immediately so we don't
11886 infinitely recurse when dealing with pointers to the structure
11887 type within the structure itself. */
11888 set_die_type (die, type, cu);
11890 /* set_die_type should be already done. */
11891 set_descriptive_type (type, die, cu);
11896 /* Finish creating a structure or union type, including filling in
11897 its members and creating a symbol for it. */
11900 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11902 struct objfile *objfile = cu->objfile;
11903 struct die_info *child_die = die->child;
11906 type = get_die_type (die, cu);
11908 type = read_structure_type (die, cu);
11910 if (die->child != NULL && ! die_is_declaration (die, cu))
11912 struct field_info fi;
11913 struct die_info *child_die;
11914 VEC (symbolp) *template_args = NULL;
11915 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11917 memset (&fi, 0, sizeof (struct field_info));
11919 child_die = die->child;
11921 while (child_die && child_die->tag)
11923 if (child_die->tag == DW_TAG_member
11924 || child_die->tag == DW_TAG_variable)
11926 /* NOTE: carlton/2002-11-05: A C++ static data member
11927 should be a DW_TAG_member that is a declaration, but
11928 all versions of G++ as of this writing (so through at
11929 least 3.2.1) incorrectly generate DW_TAG_variable
11930 tags for them instead. */
11931 dwarf2_add_field (&fi, child_die, cu);
11933 else if (child_die->tag == DW_TAG_subprogram)
11935 /* C++ member function. */
11936 dwarf2_add_member_fn (&fi, child_die, type, cu);
11938 else if (child_die->tag == DW_TAG_inheritance)
11940 /* C++ base class field. */
11941 dwarf2_add_field (&fi, child_die, cu);
11943 else if (child_die->tag == DW_TAG_typedef)
11944 dwarf2_add_typedef (&fi, child_die, cu);
11945 else if (child_die->tag == DW_TAG_template_type_param
11946 || child_die->tag == DW_TAG_template_value_param)
11948 struct symbol *arg = new_symbol (child_die, NULL, cu);
11951 VEC_safe_push (symbolp, template_args, arg);
11954 child_die = sibling_die (child_die);
11957 /* Attach template arguments to type. */
11958 if (! VEC_empty (symbolp, template_args))
11960 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11961 TYPE_N_TEMPLATE_ARGUMENTS (type)
11962 = VEC_length (symbolp, template_args);
11963 TYPE_TEMPLATE_ARGUMENTS (type)
11964 = obstack_alloc (&objfile->objfile_obstack,
11965 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11966 * sizeof (struct symbol *)));
11967 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11968 VEC_address (symbolp, template_args),
11969 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11970 * sizeof (struct symbol *)));
11971 VEC_free (symbolp, template_args);
11974 /* Attach fields and member functions to the type. */
11976 dwarf2_attach_fields_to_type (&fi, type, cu);
11979 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11981 /* Get the type which refers to the base class (possibly this
11982 class itself) which contains the vtable pointer for the current
11983 class from the DW_AT_containing_type attribute. This use of
11984 DW_AT_containing_type is a GNU extension. */
11986 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11988 struct type *t = die_containing_type (die, cu);
11990 TYPE_VPTR_BASETYPE (type) = t;
11995 /* Our own class provides vtbl ptr. */
11996 for (i = TYPE_NFIELDS (t) - 1;
11997 i >= TYPE_N_BASECLASSES (t);
12000 const char *fieldname = TYPE_FIELD_NAME (t, i);
12002 if (is_vtable_name (fieldname, cu))
12004 TYPE_VPTR_FIELDNO (type) = i;
12009 /* Complain if virtual function table field not found. */
12010 if (i < TYPE_N_BASECLASSES (t))
12011 complaint (&symfile_complaints,
12012 _("virtual function table pointer "
12013 "not found when defining class '%s'"),
12014 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12019 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12022 else if (cu->producer
12023 && strncmp (cu->producer,
12024 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12026 /* The IBM XLC compiler does not provide direct indication
12027 of the containing type, but the vtable pointer is
12028 always named __vfp. */
12032 for (i = TYPE_NFIELDS (type) - 1;
12033 i >= TYPE_N_BASECLASSES (type);
12036 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12038 TYPE_VPTR_FIELDNO (type) = i;
12039 TYPE_VPTR_BASETYPE (type) = type;
12046 /* Copy fi.typedef_field_list linked list elements content into the
12047 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12048 if (fi.typedef_field_list)
12050 int i = fi.typedef_field_list_count;
12052 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12053 TYPE_TYPEDEF_FIELD_ARRAY (type)
12054 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12055 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12057 /* Reverse the list order to keep the debug info elements order. */
12060 struct typedef_field *dest, *src;
12062 dest = &TYPE_TYPEDEF_FIELD (type, i);
12063 src = &fi.typedef_field_list->field;
12064 fi.typedef_field_list = fi.typedef_field_list->next;
12069 do_cleanups (back_to);
12071 if (HAVE_CPLUS_STRUCT (type))
12072 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12075 quirk_gcc_member_function_pointer (type, objfile);
12077 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12078 snapshots) has been known to create a die giving a declaration
12079 for a class that has, as a child, a die giving a definition for a
12080 nested class. So we have to process our children even if the
12081 current die is a declaration. Normally, of course, a declaration
12082 won't have any children at all. */
12084 while (child_die != NULL && child_die->tag)
12086 if (child_die->tag == DW_TAG_member
12087 || child_die->tag == DW_TAG_variable
12088 || child_die->tag == DW_TAG_inheritance
12089 || child_die->tag == DW_TAG_template_value_param
12090 || child_die->tag == DW_TAG_template_type_param)
12095 process_die (child_die, cu);
12097 child_die = sibling_die (child_die);
12100 /* Do not consider external references. According to the DWARF standard,
12101 these DIEs are identified by the fact that they have no byte_size
12102 attribute, and a declaration attribute. */
12103 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12104 || !die_is_declaration (die, cu))
12105 new_symbol (die, type, cu);
12108 /* Given a DW_AT_enumeration_type die, set its type. We do not
12109 complete the type's fields yet, or create any symbols. */
12111 static struct type *
12112 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12114 struct objfile *objfile = cu->objfile;
12116 struct attribute *attr;
12119 /* If the definition of this type lives in .debug_types, read that type.
12120 Don't follow DW_AT_specification though, that will take us back up
12121 the chain and we want to go down. */
12122 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12125 type = get_DW_AT_signature_type (die, attr, cu);
12127 /* The type's CU may not be the same as CU.
12128 Ensure TYPE is recorded with CU in die_type_hash. */
12129 return set_die_type (die, type, cu);
12132 type = alloc_type (objfile);
12134 TYPE_CODE (type) = TYPE_CODE_ENUM;
12135 name = dwarf2_full_name (NULL, die, cu);
12137 TYPE_TAG_NAME (type) = name;
12139 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12142 TYPE_LENGTH (type) = DW_UNSND (attr);
12146 TYPE_LENGTH (type) = 0;
12149 /* The enumeration DIE can be incomplete. In Ada, any type can be
12150 declared as private in the package spec, and then defined only
12151 inside the package body. Such types are known as Taft Amendment
12152 Types. When another package uses such a type, an incomplete DIE
12153 may be generated by the compiler. */
12154 if (die_is_declaration (die, cu))
12155 TYPE_STUB (type) = 1;
12157 return set_die_type (die, type, cu);
12160 /* Given a pointer to a die which begins an enumeration, process all
12161 the dies that define the members of the enumeration, and create the
12162 symbol for the enumeration type.
12164 NOTE: We reverse the order of the element list. */
12167 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12169 struct type *this_type;
12171 this_type = get_die_type (die, cu);
12172 if (this_type == NULL)
12173 this_type = read_enumeration_type (die, cu);
12175 if (die->child != NULL)
12177 struct die_info *child_die;
12178 struct symbol *sym;
12179 struct field *fields = NULL;
12180 int num_fields = 0;
12181 int unsigned_enum = 1;
12186 child_die = die->child;
12187 while (child_die && child_die->tag)
12189 if (child_die->tag != DW_TAG_enumerator)
12191 process_die (child_die, cu);
12195 name = dwarf2_name (child_die, cu);
12198 sym = new_symbol (child_die, this_type, cu);
12199 if (SYMBOL_VALUE (sym) < 0)
12204 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12207 mask |= SYMBOL_VALUE (sym);
12209 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12211 fields = (struct field *)
12213 (num_fields + DW_FIELD_ALLOC_CHUNK)
12214 * sizeof (struct field));
12217 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
12218 FIELD_TYPE (fields[num_fields]) = NULL;
12219 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
12220 FIELD_BITSIZE (fields[num_fields]) = 0;
12226 child_die = sibling_die (child_die);
12231 TYPE_NFIELDS (this_type) = num_fields;
12232 TYPE_FIELDS (this_type) = (struct field *)
12233 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12234 memcpy (TYPE_FIELDS (this_type), fields,
12235 sizeof (struct field) * num_fields);
12239 TYPE_UNSIGNED (this_type) = 1;
12241 TYPE_FLAG_ENUM (this_type) = 1;
12244 /* If we are reading an enum from a .debug_types unit, and the enum
12245 is a declaration, and the enum is not the signatured type in the
12246 unit, then we do not want to add a symbol for it. Adding a
12247 symbol would in some cases obscure the true definition of the
12248 enum, giving users an incomplete type when the definition is
12249 actually available. Note that we do not want to do this for all
12250 enums which are just declarations, because C++0x allows forward
12251 enum declarations. */
12252 if (cu->per_cu->is_debug_types
12253 && die_is_declaration (die, cu))
12255 struct signatured_type *sig_type;
12257 sig_type = (struct signatured_type *) cu->per_cu;
12258 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12259 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
12263 new_symbol (die, this_type, cu);
12266 /* Extract all information from a DW_TAG_array_type DIE and put it in
12267 the DIE's type field. For now, this only handles one dimensional
12270 static struct type *
12271 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
12273 struct objfile *objfile = cu->objfile;
12274 struct die_info *child_die;
12276 struct type *element_type, *range_type, *index_type;
12277 struct type **range_types = NULL;
12278 struct attribute *attr;
12280 struct cleanup *back_to;
12283 element_type = die_type (die, cu);
12285 /* The die_type call above may have already set the type for this DIE. */
12286 type = get_die_type (die, cu);
12290 /* Irix 6.2 native cc creates array types without children for
12291 arrays with unspecified length. */
12292 if (die->child == NULL)
12294 index_type = objfile_type (objfile)->builtin_int;
12295 range_type = create_range_type (NULL, index_type, 0, -1);
12296 type = create_array_type (NULL, element_type, range_type);
12297 return set_die_type (die, type, cu);
12300 back_to = make_cleanup (null_cleanup, NULL);
12301 child_die = die->child;
12302 while (child_die && child_die->tag)
12304 if (child_die->tag == DW_TAG_subrange_type)
12306 struct type *child_type = read_type_die (child_die, cu);
12308 if (child_type != NULL)
12310 /* The range type was succesfully read. Save it for the
12311 array type creation. */
12312 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12314 range_types = (struct type **)
12315 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12316 * sizeof (struct type *));
12318 make_cleanup (free_current_contents, &range_types);
12320 range_types[ndim++] = child_type;
12323 child_die = sibling_die (child_die);
12326 /* Dwarf2 dimensions are output from left to right, create the
12327 necessary array types in backwards order. */
12329 type = element_type;
12331 if (read_array_order (die, cu) == DW_ORD_col_major)
12336 type = create_array_type (NULL, type, range_types[i++]);
12341 type = create_array_type (NULL, type, range_types[ndim]);
12344 /* Understand Dwarf2 support for vector types (like they occur on
12345 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12346 array type. This is not part of the Dwarf2/3 standard yet, but a
12347 custom vendor extension. The main difference between a regular
12348 array and the vector variant is that vectors are passed by value
12350 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
12352 make_vector_type (type);
12354 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12355 implementation may choose to implement triple vectors using this
12357 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12360 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12361 TYPE_LENGTH (type) = DW_UNSND (attr);
12363 complaint (&symfile_complaints,
12364 _("DW_AT_byte_size for array type smaller "
12365 "than the total size of elements"));
12368 name = dwarf2_name (die, cu);
12370 TYPE_NAME (type) = name;
12372 /* Install the type in the die. */
12373 set_die_type (die, type, cu);
12375 /* set_die_type should be already done. */
12376 set_descriptive_type (type, die, cu);
12378 do_cleanups (back_to);
12383 static enum dwarf_array_dim_ordering
12384 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
12386 struct attribute *attr;
12388 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12390 if (attr) return DW_SND (attr);
12392 /* GNU F77 is a special case, as at 08/2004 array type info is the
12393 opposite order to the dwarf2 specification, but data is still
12394 laid out as per normal fortran.
12396 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12397 version checking. */
12399 if (cu->language == language_fortran
12400 && cu->producer && strstr (cu->producer, "GNU F77"))
12402 return DW_ORD_row_major;
12405 switch (cu->language_defn->la_array_ordering)
12407 case array_column_major:
12408 return DW_ORD_col_major;
12409 case array_row_major:
12411 return DW_ORD_row_major;
12415 /* Extract all information from a DW_TAG_set_type DIE and put it in
12416 the DIE's type field. */
12418 static struct type *
12419 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12421 struct type *domain_type, *set_type;
12422 struct attribute *attr;
12424 domain_type = die_type (die, cu);
12426 /* The die_type call above may have already set the type for this DIE. */
12427 set_type = get_die_type (die, cu);
12431 set_type = create_set_type (NULL, domain_type);
12433 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12435 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12437 return set_die_type (die, set_type, cu);
12440 /* A helper for read_common_block that creates a locexpr baton.
12441 SYM is the symbol which we are marking as computed.
12442 COMMON_DIE is the DIE for the common block.
12443 COMMON_LOC is the location expression attribute for the common
12445 MEMBER_LOC is the location expression attribute for the particular
12446 member of the common block that we are processing.
12447 CU is the CU from which the above come. */
12450 mark_common_block_symbol_computed (struct symbol *sym,
12451 struct die_info *common_die,
12452 struct attribute *common_loc,
12453 struct attribute *member_loc,
12454 struct dwarf2_cu *cu)
12456 struct objfile *objfile = dwarf2_per_objfile->objfile;
12457 struct dwarf2_locexpr_baton *baton;
12459 unsigned int cu_off;
12460 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12461 LONGEST offset = 0;
12463 gdb_assert (common_loc && member_loc);
12464 gdb_assert (attr_form_is_block (common_loc));
12465 gdb_assert (attr_form_is_block (member_loc)
12466 || attr_form_is_constant (member_loc));
12468 baton = obstack_alloc (&objfile->objfile_obstack,
12469 sizeof (struct dwarf2_locexpr_baton));
12470 baton->per_cu = cu->per_cu;
12471 gdb_assert (baton->per_cu);
12473 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12475 if (attr_form_is_constant (member_loc))
12477 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12478 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12481 baton->size += DW_BLOCK (member_loc)->size;
12483 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12486 *ptr++ = DW_OP_call4;
12487 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12488 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12491 if (attr_form_is_constant (member_loc))
12493 *ptr++ = DW_OP_addr;
12494 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12495 ptr += cu->header.addr_size;
12499 /* We have to copy the data here, because DW_OP_call4 will only
12500 use a DW_AT_location attribute. */
12501 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12502 ptr += DW_BLOCK (member_loc)->size;
12505 *ptr++ = DW_OP_plus;
12506 gdb_assert (ptr - baton->data == baton->size);
12508 SYMBOL_LOCATION_BATON (sym) = baton;
12509 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12512 /* Create appropriate locally-scoped variables for all the
12513 DW_TAG_common_block entries. Also create a struct common_block
12514 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12515 is used to sepate the common blocks name namespace from regular
12519 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12521 struct attribute *attr;
12523 attr = dwarf2_attr (die, DW_AT_location, cu);
12526 /* Support the .debug_loc offsets. */
12527 if (attr_form_is_block (attr))
12531 else if (attr_form_is_section_offset (attr))
12533 dwarf2_complex_location_expr_complaint ();
12538 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12539 "common block member");
12544 if (die->child != NULL)
12546 struct objfile *objfile = cu->objfile;
12547 struct die_info *child_die;
12548 size_t n_entries = 0, size;
12549 struct common_block *common_block;
12550 struct symbol *sym;
12552 for (child_die = die->child;
12553 child_die && child_die->tag;
12554 child_die = sibling_die (child_die))
12557 size = (sizeof (struct common_block)
12558 + (n_entries - 1) * sizeof (struct symbol *));
12559 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12560 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12561 common_block->n_entries = 0;
12563 for (child_die = die->child;
12564 child_die && child_die->tag;
12565 child_die = sibling_die (child_die))
12567 /* Create the symbol in the DW_TAG_common_block block in the current
12569 sym = new_symbol (child_die, NULL, cu);
12572 struct attribute *member_loc;
12574 common_block->contents[common_block->n_entries++] = sym;
12576 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12580 /* GDB has handled this for a long time, but it is
12581 not specified by DWARF. It seems to have been
12582 emitted by gfortran at least as recently as:
12583 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12584 complaint (&symfile_complaints,
12585 _("Variable in common block has "
12586 "DW_AT_data_member_location "
12587 "- DIE at 0x%x [in module %s]"),
12588 child_die->offset.sect_off, cu->objfile->name);
12590 if (attr_form_is_section_offset (member_loc))
12591 dwarf2_complex_location_expr_complaint ();
12592 else if (attr_form_is_constant (member_loc)
12593 || attr_form_is_block (member_loc))
12596 mark_common_block_symbol_computed (sym, die, attr,
12600 dwarf2_complex_location_expr_complaint ();
12605 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12606 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12610 /* Create a type for a C++ namespace. */
12612 static struct type *
12613 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12615 struct objfile *objfile = cu->objfile;
12616 const char *previous_prefix, *name;
12620 /* For extensions, reuse the type of the original namespace. */
12621 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12623 struct die_info *ext_die;
12624 struct dwarf2_cu *ext_cu = cu;
12626 ext_die = dwarf2_extension (die, &ext_cu);
12627 type = read_type_die (ext_die, ext_cu);
12629 /* EXT_CU may not be the same as CU.
12630 Ensure TYPE is recorded with CU in die_type_hash. */
12631 return set_die_type (die, type, cu);
12634 name = namespace_name (die, &is_anonymous, cu);
12636 /* Now build the name of the current namespace. */
12638 previous_prefix = determine_prefix (die, cu);
12639 if (previous_prefix[0] != '\0')
12640 name = typename_concat (&objfile->objfile_obstack,
12641 previous_prefix, name, 0, cu);
12643 /* Create the type. */
12644 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12646 TYPE_NAME (type) = name;
12647 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12649 return set_die_type (die, type, cu);
12652 /* Read a C++ namespace. */
12655 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12657 struct objfile *objfile = cu->objfile;
12660 /* Add a symbol associated to this if we haven't seen the namespace
12661 before. Also, add a using directive if it's an anonymous
12664 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12668 type = read_type_die (die, cu);
12669 new_symbol (die, type, cu);
12671 namespace_name (die, &is_anonymous, cu);
12674 const char *previous_prefix = determine_prefix (die, cu);
12676 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12677 NULL, NULL, 0, &objfile->objfile_obstack);
12681 if (die->child != NULL)
12683 struct die_info *child_die = die->child;
12685 while (child_die && child_die->tag)
12687 process_die (child_die, cu);
12688 child_die = sibling_die (child_die);
12693 /* Read a Fortran module as type. This DIE can be only a declaration used for
12694 imported module. Still we need that type as local Fortran "use ... only"
12695 declaration imports depend on the created type in determine_prefix. */
12697 static struct type *
12698 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12700 struct objfile *objfile = cu->objfile;
12701 const char *module_name;
12704 module_name = dwarf2_name (die, cu);
12706 complaint (&symfile_complaints,
12707 _("DW_TAG_module has no name, offset 0x%x"),
12708 die->offset.sect_off);
12709 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12711 /* determine_prefix uses TYPE_TAG_NAME. */
12712 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12714 return set_die_type (die, type, cu);
12717 /* Read a Fortran module. */
12720 read_module (struct die_info *die, struct dwarf2_cu *cu)
12722 struct die_info *child_die = die->child;
12724 while (child_die && child_die->tag)
12726 process_die (child_die, cu);
12727 child_die = sibling_die (child_die);
12731 /* Return the name of the namespace represented by DIE. Set
12732 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12735 static const char *
12736 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12738 struct die_info *current_die;
12739 const char *name = NULL;
12741 /* Loop through the extensions until we find a name. */
12743 for (current_die = die;
12744 current_die != NULL;
12745 current_die = dwarf2_extension (die, &cu))
12747 name = dwarf2_name (current_die, cu);
12752 /* Is it an anonymous namespace? */
12754 *is_anonymous = (name == NULL);
12756 name = CP_ANONYMOUS_NAMESPACE_STR;
12761 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12762 the user defined type vector. */
12764 static struct type *
12765 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12767 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12768 struct comp_unit_head *cu_header = &cu->header;
12770 struct attribute *attr_byte_size;
12771 struct attribute *attr_address_class;
12772 int byte_size, addr_class;
12773 struct type *target_type;
12775 target_type = die_type (die, cu);
12777 /* The die_type call above may have already set the type for this DIE. */
12778 type = get_die_type (die, cu);
12782 type = lookup_pointer_type (target_type);
12784 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12785 if (attr_byte_size)
12786 byte_size = DW_UNSND (attr_byte_size);
12788 byte_size = cu_header->addr_size;
12790 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12791 if (attr_address_class)
12792 addr_class = DW_UNSND (attr_address_class);
12794 addr_class = DW_ADDR_none;
12796 /* If the pointer size or address class is different than the
12797 default, create a type variant marked as such and set the
12798 length accordingly. */
12799 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12801 if (gdbarch_address_class_type_flags_p (gdbarch))
12805 type_flags = gdbarch_address_class_type_flags
12806 (gdbarch, byte_size, addr_class);
12807 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12809 type = make_type_with_address_space (type, type_flags);
12811 else if (TYPE_LENGTH (type) != byte_size)
12813 complaint (&symfile_complaints,
12814 _("invalid pointer size %d"), byte_size);
12818 /* Should we also complain about unhandled address classes? */
12822 TYPE_LENGTH (type) = byte_size;
12823 return set_die_type (die, type, cu);
12826 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12827 the user defined type vector. */
12829 static struct type *
12830 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12833 struct type *to_type;
12834 struct type *domain;
12836 to_type = die_type (die, cu);
12837 domain = die_containing_type (die, cu);
12839 /* The calls above may have already set the type for this DIE. */
12840 type = get_die_type (die, cu);
12844 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12845 type = lookup_methodptr_type (to_type);
12846 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12848 struct type *new_type = alloc_type (cu->objfile);
12850 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12851 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12852 TYPE_VARARGS (to_type));
12853 type = lookup_methodptr_type (new_type);
12856 type = lookup_memberptr_type (to_type, domain);
12858 return set_die_type (die, type, cu);
12861 /* Extract all information from a DW_TAG_reference_type DIE and add to
12862 the user defined type vector. */
12864 static struct type *
12865 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12867 struct comp_unit_head *cu_header = &cu->header;
12868 struct type *type, *target_type;
12869 struct attribute *attr;
12871 target_type = die_type (die, cu);
12873 /* The die_type call above may have already set the type for this DIE. */
12874 type = get_die_type (die, cu);
12878 type = lookup_reference_type (target_type);
12879 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12882 TYPE_LENGTH (type) = DW_UNSND (attr);
12886 TYPE_LENGTH (type) = cu_header->addr_size;
12888 return set_die_type (die, type, cu);
12891 static struct type *
12892 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12894 struct type *base_type, *cv_type;
12896 base_type = die_type (die, cu);
12898 /* The die_type call above may have already set the type for this DIE. */
12899 cv_type = get_die_type (die, cu);
12903 /* In case the const qualifier is applied to an array type, the element type
12904 is so qualified, not the array type (section 6.7.3 of C99). */
12905 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12907 struct type *el_type, *inner_array;
12909 base_type = copy_type (base_type);
12910 inner_array = base_type;
12912 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12914 TYPE_TARGET_TYPE (inner_array) =
12915 copy_type (TYPE_TARGET_TYPE (inner_array));
12916 inner_array = TYPE_TARGET_TYPE (inner_array);
12919 el_type = TYPE_TARGET_TYPE (inner_array);
12920 TYPE_TARGET_TYPE (inner_array) =
12921 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12923 return set_die_type (die, base_type, cu);
12926 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12927 return set_die_type (die, cv_type, cu);
12930 static struct type *
12931 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12933 struct type *base_type, *cv_type;
12935 base_type = die_type (die, cu);
12937 /* The die_type call above may have already set the type for this DIE. */
12938 cv_type = get_die_type (die, cu);
12942 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12943 return set_die_type (die, cv_type, cu);
12946 /* Handle DW_TAG_restrict_type. */
12948 static struct type *
12949 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12951 struct type *base_type, *cv_type;
12953 base_type = die_type (die, cu);
12955 /* The die_type call above may have already set the type for this DIE. */
12956 cv_type = get_die_type (die, cu);
12960 cv_type = make_restrict_type (base_type);
12961 return set_die_type (die, cv_type, cu);
12964 /* Extract all information from a DW_TAG_string_type DIE and add to
12965 the user defined type vector. It isn't really a user defined type,
12966 but it behaves like one, with other DIE's using an AT_user_def_type
12967 attribute to reference it. */
12969 static struct type *
12970 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12972 struct objfile *objfile = cu->objfile;
12973 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12974 struct type *type, *range_type, *index_type, *char_type;
12975 struct attribute *attr;
12976 unsigned int length;
12978 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12981 length = DW_UNSND (attr);
12985 /* Check for the DW_AT_byte_size attribute. */
12986 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12989 length = DW_UNSND (attr);
12997 index_type = objfile_type (objfile)->builtin_int;
12998 range_type = create_range_type (NULL, index_type, 1, length);
12999 char_type = language_string_char_type (cu->language_defn, gdbarch);
13000 type = create_string_type (NULL, char_type, range_type);
13002 return set_die_type (die, type, cu);
13005 /* Assuming that DIE corresponds to a function, returns nonzero
13006 if the function is prototyped. */
13009 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13011 struct attribute *attr;
13013 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13014 if (attr && (DW_UNSND (attr) != 0))
13017 /* The DWARF standard implies that the DW_AT_prototyped attribute
13018 is only meaninful for C, but the concept also extends to other
13019 languages that allow unprototyped functions (Eg: Objective C).
13020 For all other languages, assume that functions are always
13022 if (cu->language != language_c
13023 && cu->language != language_objc
13024 && cu->language != language_opencl)
13027 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13028 prototyped and unprototyped functions; default to prototyped,
13029 since that is more common in modern code (and RealView warns
13030 about unprototyped functions). */
13031 if (producer_is_realview (cu->producer))
13037 /* Handle DIES due to C code like:
13041 int (*funcp)(int a, long l);
13045 ('funcp' generates a DW_TAG_subroutine_type DIE). */
13047 static struct type *
13048 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
13050 struct objfile *objfile = cu->objfile;
13051 struct type *type; /* Type that this function returns. */
13052 struct type *ftype; /* Function that returns above type. */
13053 struct attribute *attr;
13055 type = die_type (die, cu);
13057 /* The die_type call above may have already set the type for this DIE. */
13058 ftype = get_die_type (die, cu);
13062 ftype = lookup_function_type (type);
13064 if (prototyped_function_p (die, cu))
13065 TYPE_PROTOTYPED (ftype) = 1;
13067 /* Store the calling convention in the type if it's available in
13068 the subroutine die. Otherwise set the calling convention to
13069 the default value DW_CC_normal. */
13070 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13072 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13073 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13074 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13076 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13078 /* We need to add the subroutine type to the die immediately so
13079 we don't infinitely recurse when dealing with parameters
13080 declared as the same subroutine type. */
13081 set_die_type (die, ftype, cu);
13083 if (die->child != NULL)
13085 struct type *void_type = objfile_type (objfile)->builtin_void;
13086 struct die_info *child_die;
13087 int nparams, iparams;
13089 /* Count the number of parameters.
13090 FIXME: GDB currently ignores vararg functions, but knows about
13091 vararg member functions. */
13093 child_die = die->child;
13094 while (child_die && child_die->tag)
13096 if (child_die->tag == DW_TAG_formal_parameter)
13098 else if (child_die->tag == DW_TAG_unspecified_parameters)
13099 TYPE_VARARGS (ftype) = 1;
13100 child_die = sibling_die (child_die);
13103 /* Allocate storage for parameters and fill them in. */
13104 TYPE_NFIELDS (ftype) = nparams;
13105 TYPE_FIELDS (ftype) = (struct field *)
13106 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13108 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13109 even if we error out during the parameters reading below. */
13110 for (iparams = 0; iparams < nparams; iparams++)
13111 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13114 child_die = die->child;
13115 while (child_die && child_die->tag)
13117 if (child_die->tag == DW_TAG_formal_parameter)
13119 struct type *arg_type;
13121 /* DWARF version 2 has no clean way to discern C++
13122 static and non-static member functions. G++ helps
13123 GDB by marking the first parameter for non-static
13124 member functions (which is the this pointer) as
13125 artificial. We pass this information to
13126 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13128 DWARF version 3 added DW_AT_object_pointer, which GCC
13129 4.5 does not yet generate. */
13130 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13132 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13135 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13137 /* GCC/43521: In java, the formal parameter
13138 "this" is sometimes not marked with DW_AT_artificial. */
13139 if (cu->language == language_java)
13141 const char *name = dwarf2_name (child_die, cu);
13143 if (name && !strcmp (name, "this"))
13144 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13147 arg_type = die_type (child_die, cu);
13149 /* RealView does not mark THIS as const, which the testsuite
13150 expects. GCC marks THIS as const in method definitions,
13151 but not in the class specifications (GCC PR 43053). */
13152 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13153 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13156 struct dwarf2_cu *arg_cu = cu;
13157 const char *name = dwarf2_name (child_die, cu);
13159 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13162 /* If the compiler emits this, use it. */
13163 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13166 else if (name && strcmp (name, "this") == 0)
13167 /* Function definitions will have the argument names. */
13169 else if (name == NULL && iparams == 0)
13170 /* Declarations may not have the names, so like
13171 elsewhere in GDB, assume an artificial first
13172 argument is "this". */
13176 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13180 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
13183 child_die = sibling_die (child_die);
13190 static struct type *
13191 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
13193 struct objfile *objfile = cu->objfile;
13194 const char *name = NULL;
13195 struct type *this_type, *target_type;
13197 name = dwarf2_full_name (NULL, die, cu);
13198 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
13199 TYPE_FLAG_TARGET_STUB, NULL, objfile);
13200 TYPE_NAME (this_type) = name;
13201 set_die_type (die, this_type, cu);
13202 target_type = die_type (die, cu);
13203 if (target_type != this_type)
13204 TYPE_TARGET_TYPE (this_type) = target_type;
13207 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13208 spec and cause infinite loops in GDB. */
13209 complaint (&symfile_complaints,
13210 _("Self-referential DW_TAG_typedef "
13211 "- DIE at 0x%x [in module %s]"),
13212 die->offset.sect_off, objfile->name);
13213 TYPE_TARGET_TYPE (this_type) = NULL;
13218 /* Find a representation of a given base type and install
13219 it in the TYPE field of the die. */
13221 static struct type *
13222 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
13224 struct objfile *objfile = cu->objfile;
13226 struct attribute *attr;
13227 int encoding = 0, size = 0;
13229 enum type_code code = TYPE_CODE_INT;
13230 int type_flags = 0;
13231 struct type *target_type = NULL;
13233 attr = dwarf2_attr (die, DW_AT_encoding, cu);
13236 encoding = DW_UNSND (attr);
13238 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13241 size = DW_UNSND (attr);
13243 name = dwarf2_name (die, cu);
13246 complaint (&symfile_complaints,
13247 _("DW_AT_name missing from DW_TAG_base_type"));
13252 case DW_ATE_address:
13253 /* Turn DW_ATE_address into a void * pointer. */
13254 code = TYPE_CODE_PTR;
13255 type_flags |= TYPE_FLAG_UNSIGNED;
13256 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13258 case DW_ATE_boolean:
13259 code = TYPE_CODE_BOOL;
13260 type_flags |= TYPE_FLAG_UNSIGNED;
13262 case DW_ATE_complex_float:
13263 code = TYPE_CODE_COMPLEX;
13264 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13266 case DW_ATE_decimal_float:
13267 code = TYPE_CODE_DECFLOAT;
13270 code = TYPE_CODE_FLT;
13272 case DW_ATE_signed:
13274 case DW_ATE_unsigned:
13275 type_flags |= TYPE_FLAG_UNSIGNED;
13276 if (cu->language == language_fortran
13278 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13279 code = TYPE_CODE_CHAR;
13281 case DW_ATE_signed_char:
13282 if (cu->language == language_ada || cu->language == language_m2
13283 || cu->language == language_pascal
13284 || cu->language == language_fortran)
13285 code = TYPE_CODE_CHAR;
13287 case DW_ATE_unsigned_char:
13288 if (cu->language == language_ada || cu->language == language_m2
13289 || cu->language == language_pascal
13290 || cu->language == language_fortran)
13291 code = TYPE_CODE_CHAR;
13292 type_flags |= TYPE_FLAG_UNSIGNED;
13295 /* We just treat this as an integer and then recognize the
13296 type by name elsewhere. */
13300 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13301 dwarf_type_encoding_name (encoding));
13305 type = init_type (code, size, type_flags, NULL, objfile);
13306 TYPE_NAME (type) = name;
13307 TYPE_TARGET_TYPE (type) = target_type;
13309 if (name && strcmp (name, "char") == 0)
13310 TYPE_NOSIGN (type) = 1;
13312 return set_die_type (die, type, cu);
13315 /* Read the given DW_AT_subrange DIE. */
13317 static struct type *
13318 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13320 struct type *base_type, *orig_base_type;
13321 struct type *range_type;
13322 struct attribute *attr;
13324 int low_default_is_valid;
13326 LONGEST negative_mask;
13328 orig_base_type = die_type (die, cu);
13329 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13330 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13331 creating the range type, but we use the result of check_typedef
13332 when examining properties of the type. */
13333 base_type = check_typedef (orig_base_type);
13335 /* The die_type call above may have already set the type for this DIE. */
13336 range_type = get_die_type (die, cu);
13340 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13341 omitting DW_AT_lower_bound. */
13342 switch (cu->language)
13345 case language_cplus:
13347 low_default_is_valid = 1;
13349 case language_fortran:
13351 low_default_is_valid = 1;
13354 case language_java:
13355 case language_objc:
13357 low_default_is_valid = (cu->header.version >= 4);
13361 case language_pascal:
13363 low_default_is_valid = (cu->header.version >= 4);
13367 low_default_is_valid = 0;
13371 /* FIXME: For variable sized arrays either of these could be
13372 a variable rather than a constant value. We'll allow it,
13373 but we don't know how to handle it. */
13374 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
13376 low = dwarf2_get_attr_constant_value (attr, low);
13377 else if (!low_default_is_valid)
13378 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13379 "- DIE at 0x%x [in module %s]"),
13380 die->offset.sect_off, cu->objfile->name);
13382 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
13385 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
13387 /* GCC encodes arrays with unspecified or dynamic length
13388 with a DW_FORM_block1 attribute or a reference attribute.
13389 FIXME: GDB does not yet know how to handle dynamic
13390 arrays properly, treat them as arrays with unspecified
13393 FIXME: jimb/2003-09-22: GDB does not really know
13394 how to handle arrays of unspecified length
13395 either; we just represent them as zero-length
13396 arrays. Choose an appropriate upper bound given
13397 the lower bound we've computed above. */
13401 high = dwarf2_get_attr_constant_value (attr, 1);
13405 attr = dwarf2_attr (die, DW_AT_count, cu);
13408 int count = dwarf2_get_attr_constant_value (attr, 1);
13409 high = low + count - 1;
13413 /* Unspecified array length. */
13418 /* Dwarf-2 specifications explicitly allows to create subrange types
13419 without specifying a base type.
13420 In that case, the base type must be set to the type of
13421 the lower bound, upper bound or count, in that order, if any of these
13422 three attributes references an object that has a type.
13423 If no base type is found, the Dwarf-2 specifications say that
13424 a signed integer type of size equal to the size of an address should
13426 For the following C code: `extern char gdb_int [];'
13427 GCC produces an empty range DIE.
13428 FIXME: muller/2010-05-28: Possible references to object for low bound,
13429 high bound or count are not yet handled by this code. */
13430 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13432 struct objfile *objfile = cu->objfile;
13433 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13434 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13435 struct type *int_type = objfile_type (objfile)->builtin_int;
13437 /* Test "int", "long int", and "long long int" objfile types,
13438 and select the first one having a size above or equal to the
13439 architecture address size. */
13440 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13441 base_type = int_type;
13444 int_type = objfile_type (objfile)->builtin_long;
13445 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13446 base_type = int_type;
13449 int_type = objfile_type (objfile)->builtin_long_long;
13450 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13451 base_type = int_type;
13457 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13458 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13459 low |= negative_mask;
13460 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13461 high |= negative_mask;
13463 range_type = create_range_type (NULL, orig_base_type, low, high);
13465 /* Mark arrays with dynamic length at least as an array of unspecified
13466 length. GDB could check the boundary but before it gets implemented at
13467 least allow accessing the array elements. */
13468 if (attr && attr_form_is_block (attr))
13469 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13471 /* Ada expects an empty array on no boundary attributes. */
13472 if (attr == NULL && cu->language != language_ada)
13473 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13475 name = dwarf2_name (die, cu);
13477 TYPE_NAME (range_type) = name;
13479 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13481 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13483 set_die_type (die, range_type, cu);
13485 /* set_die_type should be already done. */
13486 set_descriptive_type (range_type, die, cu);
13491 static struct type *
13492 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13496 /* For now, we only support the C meaning of an unspecified type: void. */
13498 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13499 TYPE_NAME (type) = dwarf2_name (die, cu);
13501 return set_die_type (die, type, cu);
13504 /* Read a single die and all its descendents. Set the die's sibling
13505 field to NULL; set other fields in the die correctly, and set all
13506 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13507 location of the info_ptr after reading all of those dies. PARENT
13508 is the parent of the die in question. */
13510 static struct die_info *
13511 read_die_and_children (const struct die_reader_specs *reader,
13512 const gdb_byte *info_ptr,
13513 const gdb_byte **new_info_ptr,
13514 struct die_info *parent)
13516 struct die_info *die;
13517 const gdb_byte *cur_ptr;
13520 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13523 *new_info_ptr = cur_ptr;
13526 store_in_ref_table (die, reader->cu);
13529 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13533 *new_info_ptr = cur_ptr;
13536 die->sibling = NULL;
13537 die->parent = parent;
13541 /* Read a die, all of its descendents, and all of its siblings; set
13542 all of the fields of all of the dies correctly. Arguments are as
13543 in read_die_and_children. */
13545 static struct die_info *
13546 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13547 const gdb_byte *info_ptr,
13548 const gdb_byte **new_info_ptr,
13549 struct die_info *parent)
13551 struct die_info *first_die, *last_sibling;
13552 const gdb_byte *cur_ptr;
13554 cur_ptr = info_ptr;
13555 first_die = last_sibling = NULL;
13559 struct die_info *die
13560 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13564 *new_info_ptr = cur_ptr;
13571 last_sibling->sibling = die;
13573 last_sibling = die;
13577 /* Read a die, all of its descendents, and all of its siblings; set
13578 all of the fields of all of the dies correctly. Arguments are as
13579 in read_die_and_children.
13580 This the main entry point for reading a DIE and all its children. */
13582 static struct die_info *
13583 read_die_and_siblings (const struct die_reader_specs *reader,
13584 const gdb_byte *info_ptr,
13585 const gdb_byte **new_info_ptr,
13586 struct die_info *parent)
13588 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13589 new_info_ptr, parent);
13591 if (dwarf2_die_debug)
13593 fprintf_unfiltered (gdb_stdlog,
13594 "Read die from %s@0x%x of %s:\n",
13595 bfd_section_name (reader->abfd,
13596 reader->die_section->asection),
13597 (unsigned) (info_ptr - reader->die_section->buffer),
13598 bfd_get_filename (reader->abfd));
13599 dump_die (die, dwarf2_die_debug);
13605 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13607 The caller is responsible for filling in the extra attributes
13608 and updating (*DIEP)->num_attrs.
13609 Set DIEP to point to a newly allocated die with its information,
13610 except for its child, sibling, and parent fields.
13611 Set HAS_CHILDREN to tell whether the die has children or not. */
13613 static const gdb_byte *
13614 read_full_die_1 (const struct die_reader_specs *reader,
13615 struct die_info **diep, const gdb_byte *info_ptr,
13616 int *has_children, int num_extra_attrs)
13618 unsigned int abbrev_number, bytes_read, i;
13619 sect_offset offset;
13620 struct abbrev_info *abbrev;
13621 struct die_info *die;
13622 struct dwarf2_cu *cu = reader->cu;
13623 bfd *abfd = reader->abfd;
13625 offset.sect_off = info_ptr - reader->buffer;
13626 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13627 info_ptr += bytes_read;
13628 if (!abbrev_number)
13635 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13637 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13639 bfd_get_filename (abfd));
13641 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13642 die->offset = offset;
13643 die->tag = abbrev->tag;
13644 die->abbrev = abbrev_number;
13646 /* Make the result usable.
13647 The caller needs to update num_attrs after adding the extra
13649 die->num_attrs = abbrev->num_attrs;
13651 for (i = 0; i < abbrev->num_attrs; ++i)
13652 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13656 *has_children = abbrev->has_children;
13660 /* Read a die and all its attributes.
13661 Set DIEP to point to a newly allocated die with its information,
13662 except for its child, sibling, and parent fields.
13663 Set HAS_CHILDREN to tell whether the die has children or not. */
13665 static const gdb_byte *
13666 read_full_die (const struct die_reader_specs *reader,
13667 struct die_info **diep, const gdb_byte *info_ptr,
13670 const gdb_byte *result;
13672 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13674 if (dwarf2_die_debug)
13676 fprintf_unfiltered (gdb_stdlog,
13677 "Read die from %s@0x%x of %s:\n",
13678 bfd_section_name (reader->abfd,
13679 reader->die_section->asection),
13680 (unsigned) (info_ptr - reader->die_section->buffer),
13681 bfd_get_filename (reader->abfd));
13682 dump_die (*diep, dwarf2_die_debug);
13688 /* Abbreviation tables.
13690 In DWARF version 2, the description of the debugging information is
13691 stored in a separate .debug_abbrev section. Before we read any
13692 dies from a section we read in all abbreviations and install them
13693 in a hash table. */
13695 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13697 static struct abbrev_info *
13698 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13700 struct abbrev_info *abbrev;
13702 abbrev = (struct abbrev_info *)
13703 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13704 memset (abbrev, 0, sizeof (struct abbrev_info));
13708 /* Add an abbreviation to the table. */
13711 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13712 unsigned int abbrev_number,
13713 struct abbrev_info *abbrev)
13715 unsigned int hash_number;
13717 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13718 abbrev->next = abbrev_table->abbrevs[hash_number];
13719 abbrev_table->abbrevs[hash_number] = abbrev;
13722 /* Look up an abbrev in the table.
13723 Returns NULL if the abbrev is not found. */
13725 static struct abbrev_info *
13726 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13727 unsigned int abbrev_number)
13729 unsigned int hash_number;
13730 struct abbrev_info *abbrev;
13732 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13733 abbrev = abbrev_table->abbrevs[hash_number];
13737 if (abbrev->number == abbrev_number)
13739 abbrev = abbrev->next;
13744 /* Read in an abbrev table. */
13746 static struct abbrev_table *
13747 abbrev_table_read_table (struct dwarf2_section_info *section,
13748 sect_offset offset)
13750 struct objfile *objfile = dwarf2_per_objfile->objfile;
13751 bfd *abfd = section->asection->owner;
13752 struct abbrev_table *abbrev_table;
13753 const gdb_byte *abbrev_ptr;
13754 struct abbrev_info *cur_abbrev;
13755 unsigned int abbrev_number, bytes_read, abbrev_name;
13756 unsigned int abbrev_form;
13757 struct attr_abbrev *cur_attrs;
13758 unsigned int allocated_attrs;
13760 abbrev_table = XMALLOC (struct abbrev_table);
13761 abbrev_table->offset = offset;
13762 obstack_init (&abbrev_table->abbrev_obstack);
13763 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13765 * sizeof (struct abbrev_info *)));
13766 memset (abbrev_table->abbrevs, 0,
13767 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13769 dwarf2_read_section (objfile, section);
13770 abbrev_ptr = section->buffer + offset.sect_off;
13771 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13772 abbrev_ptr += bytes_read;
13774 allocated_attrs = ATTR_ALLOC_CHUNK;
13775 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13777 /* Loop until we reach an abbrev number of 0. */
13778 while (abbrev_number)
13780 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13782 /* read in abbrev header */
13783 cur_abbrev->number = abbrev_number;
13784 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13785 abbrev_ptr += bytes_read;
13786 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13789 /* now read in declarations */
13790 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13791 abbrev_ptr += bytes_read;
13792 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13793 abbrev_ptr += bytes_read;
13794 while (abbrev_name)
13796 if (cur_abbrev->num_attrs == allocated_attrs)
13798 allocated_attrs += ATTR_ALLOC_CHUNK;
13800 = xrealloc (cur_attrs, (allocated_attrs
13801 * sizeof (struct attr_abbrev)));
13804 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13805 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13806 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13807 abbrev_ptr += bytes_read;
13808 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13809 abbrev_ptr += bytes_read;
13812 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13813 (cur_abbrev->num_attrs
13814 * sizeof (struct attr_abbrev)));
13815 memcpy (cur_abbrev->attrs, cur_attrs,
13816 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13818 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13820 /* Get next abbreviation.
13821 Under Irix6 the abbreviations for a compilation unit are not
13822 always properly terminated with an abbrev number of 0.
13823 Exit loop if we encounter an abbreviation which we have
13824 already read (which means we are about to read the abbreviations
13825 for the next compile unit) or if the end of the abbreviation
13826 table is reached. */
13827 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13829 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13830 abbrev_ptr += bytes_read;
13831 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13836 return abbrev_table;
13839 /* Free the resources held by ABBREV_TABLE. */
13842 abbrev_table_free (struct abbrev_table *abbrev_table)
13844 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13845 xfree (abbrev_table);
13848 /* Same as abbrev_table_free but as a cleanup.
13849 We pass in a pointer to the pointer to the table so that we can
13850 set the pointer to NULL when we're done. It also simplifies
13851 build_type_unit_groups. */
13854 abbrev_table_free_cleanup (void *table_ptr)
13856 struct abbrev_table **abbrev_table_ptr = table_ptr;
13858 if (*abbrev_table_ptr != NULL)
13859 abbrev_table_free (*abbrev_table_ptr);
13860 *abbrev_table_ptr = NULL;
13863 /* Read the abbrev table for CU from ABBREV_SECTION. */
13866 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13867 struct dwarf2_section_info *abbrev_section)
13870 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13873 /* Release the memory used by the abbrev table for a compilation unit. */
13876 dwarf2_free_abbrev_table (void *ptr_to_cu)
13878 struct dwarf2_cu *cu = ptr_to_cu;
13880 if (cu->abbrev_table != NULL)
13881 abbrev_table_free (cu->abbrev_table);
13882 /* Set this to NULL so that we SEGV if we try to read it later,
13883 and also because free_comp_unit verifies this is NULL. */
13884 cu->abbrev_table = NULL;
13887 /* Returns nonzero if TAG represents a type that we might generate a partial
13891 is_type_tag_for_partial (int tag)
13896 /* Some types that would be reasonable to generate partial symbols for,
13897 that we don't at present. */
13898 case DW_TAG_array_type:
13899 case DW_TAG_file_type:
13900 case DW_TAG_ptr_to_member_type:
13901 case DW_TAG_set_type:
13902 case DW_TAG_string_type:
13903 case DW_TAG_subroutine_type:
13905 case DW_TAG_base_type:
13906 case DW_TAG_class_type:
13907 case DW_TAG_interface_type:
13908 case DW_TAG_enumeration_type:
13909 case DW_TAG_structure_type:
13910 case DW_TAG_subrange_type:
13911 case DW_TAG_typedef:
13912 case DW_TAG_union_type:
13919 /* Load all DIEs that are interesting for partial symbols into memory. */
13921 static struct partial_die_info *
13922 load_partial_dies (const struct die_reader_specs *reader,
13923 const gdb_byte *info_ptr, int building_psymtab)
13925 struct dwarf2_cu *cu = reader->cu;
13926 struct objfile *objfile = cu->objfile;
13927 struct partial_die_info *part_die;
13928 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13929 struct abbrev_info *abbrev;
13930 unsigned int bytes_read;
13931 unsigned int load_all = 0;
13932 int nesting_level = 1;
13937 gdb_assert (cu->per_cu != NULL);
13938 if (cu->per_cu->load_all_dies)
13942 = htab_create_alloc_ex (cu->header.length / 12,
13946 &cu->comp_unit_obstack,
13947 hashtab_obstack_allocate,
13948 dummy_obstack_deallocate);
13950 part_die = obstack_alloc (&cu->comp_unit_obstack,
13951 sizeof (struct partial_die_info));
13955 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13957 /* A NULL abbrev means the end of a series of children. */
13958 if (abbrev == NULL)
13960 if (--nesting_level == 0)
13962 /* PART_DIE was probably the last thing allocated on the
13963 comp_unit_obstack, so we could call obstack_free
13964 here. We don't do that because the waste is small,
13965 and will be cleaned up when we're done with this
13966 compilation unit. This way, we're also more robust
13967 against other users of the comp_unit_obstack. */
13970 info_ptr += bytes_read;
13971 last_die = parent_die;
13972 parent_die = parent_die->die_parent;
13976 /* Check for template arguments. We never save these; if
13977 they're seen, we just mark the parent, and go on our way. */
13978 if (parent_die != NULL
13979 && cu->language == language_cplus
13980 && (abbrev->tag == DW_TAG_template_type_param
13981 || abbrev->tag == DW_TAG_template_value_param))
13983 parent_die->has_template_arguments = 1;
13987 /* We don't need a partial DIE for the template argument. */
13988 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13993 /* We only recurse into c++ subprograms looking for template arguments.
13994 Skip their other children. */
13996 && cu->language == language_cplus
13997 && parent_die != NULL
13998 && parent_die->tag == DW_TAG_subprogram)
14000 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14004 /* Check whether this DIE is interesting enough to save. Normally
14005 we would not be interested in members here, but there may be
14006 later variables referencing them via DW_AT_specification (for
14007 static members). */
14009 && !is_type_tag_for_partial (abbrev->tag)
14010 && abbrev->tag != DW_TAG_constant
14011 && abbrev->tag != DW_TAG_enumerator
14012 && abbrev->tag != DW_TAG_subprogram
14013 && abbrev->tag != DW_TAG_lexical_block
14014 && abbrev->tag != DW_TAG_variable
14015 && abbrev->tag != DW_TAG_namespace
14016 && abbrev->tag != DW_TAG_module
14017 && abbrev->tag != DW_TAG_member
14018 && abbrev->tag != DW_TAG_imported_unit)
14020 /* Otherwise we skip to the next sibling, if any. */
14021 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14025 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14028 /* This two-pass algorithm for processing partial symbols has a
14029 high cost in cache pressure. Thus, handle some simple cases
14030 here which cover the majority of C partial symbols. DIEs
14031 which neither have specification tags in them, nor could have
14032 specification tags elsewhere pointing at them, can simply be
14033 processed and discarded.
14035 This segment is also optional; scan_partial_symbols and
14036 add_partial_symbol will handle these DIEs if we chain
14037 them in normally. When compilers which do not emit large
14038 quantities of duplicate debug information are more common,
14039 this code can probably be removed. */
14041 /* Any complete simple types at the top level (pretty much all
14042 of them, for a language without namespaces), can be processed
14044 if (parent_die == NULL
14045 && part_die->has_specification == 0
14046 && part_die->is_declaration == 0
14047 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
14048 || part_die->tag == DW_TAG_base_type
14049 || part_die->tag == DW_TAG_subrange_type))
14051 if (building_psymtab && part_die->name != NULL)
14052 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14053 VAR_DOMAIN, LOC_TYPEDEF,
14054 &objfile->static_psymbols,
14055 0, (CORE_ADDR) 0, cu->language, objfile);
14056 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14060 /* The exception for DW_TAG_typedef with has_children above is
14061 a workaround of GCC PR debug/47510. In the case of this complaint
14062 type_name_no_tag_or_error will error on such types later.
14064 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14065 it could not find the child DIEs referenced later, this is checked
14066 above. In correct DWARF DW_TAG_typedef should have no children. */
14068 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14069 complaint (&symfile_complaints,
14070 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14071 "- DIE at 0x%x [in module %s]"),
14072 part_die->offset.sect_off, objfile->name);
14074 /* If we're at the second level, and we're an enumerator, and
14075 our parent has no specification (meaning possibly lives in a
14076 namespace elsewhere), then we can add the partial symbol now
14077 instead of queueing it. */
14078 if (part_die->tag == DW_TAG_enumerator
14079 && parent_die != NULL
14080 && parent_die->die_parent == NULL
14081 && parent_die->tag == DW_TAG_enumeration_type
14082 && parent_die->has_specification == 0)
14084 if (part_die->name == NULL)
14085 complaint (&symfile_complaints,
14086 _("malformed enumerator DIE ignored"));
14087 else if (building_psymtab)
14088 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14089 VAR_DOMAIN, LOC_CONST,
14090 (cu->language == language_cplus
14091 || cu->language == language_java)
14092 ? &objfile->global_psymbols
14093 : &objfile->static_psymbols,
14094 0, (CORE_ADDR) 0, cu->language, objfile);
14096 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14100 /* We'll save this DIE so link it in. */
14101 part_die->die_parent = parent_die;
14102 part_die->die_sibling = NULL;
14103 part_die->die_child = NULL;
14105 if (last_die && last_die == parent_die)
14106 last_die->die_child = part_die;
14108 last_die->die_sibling = part_die;
14110 last_die = part_die;
14112 if (first_die == NULL)
14113 first_die = part_die;
14115 /* Maybe add the DIE to the hash table. Not all DIEs that we
14116 find interesting need to be in the hash table, because we
14117 also have the parent/sibling/child chains; only those that we
14118 might refer to by offset later during partial symbol reading.
14120 For now this means things that might have be the target of a
14121 DW_AT_specification, DW_AT_abstract_origin, or
14122 DW_AT_extension. DW_AT_extension will refer only to
14123 namespaces; DW_AT_abstract_origin refers to functions (and
14124 many things under the function DIE, but we do not recurse
14125 into function DIEs during partial symbol reading) and
14126 possibly variables as well; DW_AT_specification refers to
14127 declarations. Declarations ought to have the DW_AT_declaration
14128 flag. It happens that GCC forgets to put it in sometimes, but
14129 only for functions, not for types.
14131 Adding more things than necessary to the hash table is harmless
14132 except for the performance cost. Adding too few will result in
14133 wasted time in find_partial_die, when we reread the compilation
14134 unit with load_all_dies set. */
14137 || abbrev->tag == DW_TAG_constant
14138 || abbrev->tag == DW_TAG_subprogram
14139 || abbrev->tag == DW_TAG_variable
14140 || abbrev->tag == DW_TAG_namespace
14141 || part_die->is_declaration)
14145 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14146 part_die->offset.sect_off, INSERT);
14150 part_die = obstack_alloc (&cu->comp_unit_obstack,
14151 sizeof (struct partial_die_info));
14153 /* For some DIEs we want to follow their children (if any). For C
14154 we have no reason to follow the children of structures; for other
14155 languages we have to, so that we can get at method physnames
14156 to infer fully qualified class names, for DW_AT_specification,
14157 and for C++ template arguments. For C++, we also look one level
14158 inside functions to find template arguments (if the name of the
14159 function does not already contain the template arguments).
14161 For Ada, we need to scan the children of subprograms and lexical
14162 blocks as well because Ada allows the definition of nested
14163 entities that could be interesting for the debugger, such as
14164 nested subprograms for instance. */
14165 if (last_die->has_children
14167 || last_die->tag == DW_TAG_namespace
14168 || last_die->tag == DW_TAG_module
14169 || last_die->tag == DW_TAG_enumeration_type
14170 || (cu->language == language_cplus
14171 && last_die->tag == DW_TAG_subprogram
14172 && (last_die->name == NULL
14173 || strchr (last_die->name, '<') == NULL))
14174 || (cu->language != language_c
14175 && (last_die->tag == DW_TAG_class_type
14176 || last_die->tag == DW_TAG_interface_type
14177 || last_die->tag == DW_TAG_structure_type
14178 || last_die->tag == DW_TAG_union_type))
14179 || (cu->language == language_ada
14180 && (last_die->tag == DW_TAG_subprogram
14181 || last_die->tag == DW_TAG_lexical_block))))
14184 parent_die = last_die;
14188 /* Otherwise we skip to the next sibling, if any. */
14189 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
14191 /* Back to the top, do it again. */
14195 /* Read a minimal amount of information into the minimal die structure. */
14197 static const gdb_byte *
14198 read_partial_die (const struct die_reader_specs *reader,
14199 struct partial_die_info *part_die,
14200 struct abbrev_info *abbrev, unsigned int abbrev_len,
14201 const gdb_byte *info_ptr)
14203 struct dwarf2_cu *cu = reader->cu;
14204 struct objfile *objfile = cu->objfile;
14205 const gdb_byte *buffer = reader->buffer;
14207 struct attribute attr;
14208 int has_low_pc_attr = 0;
14209 int has_high_pc_attr = 0;
14210 int high_pc_relative = 0;
14212 memset (part_die, 0, sizeof (struct partial_die_info));
14214 part_die->offset.sect_off = info_ptr - buffer;
14216 info_ptr += abbrev_len;
14218 if (abbrev == NULL)
14221 part_die->tag = abbrev->tag;
14222 part_die->has_children = abbrev->has_children;
14224 for (i = 0; i < abbrev->num_attrs; ++i)
14226 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
14228 /* Store the data if it is of an attribute we want to keep in a
14229 partial symbol table. */
14233 switch (part_die->tag)
14235 case DW_TAG_compile_unit:
14236 case DW_TAG_partial_unit:
14237 case DW_TAG_type_unit:
14238 /* Compilation units have a DW_AT_name that is a filename, not
14239 a source language identifier. */
14240 case DW_TAG_enumeration_type:
14241 case DW_TAG_enumerator:
14242 /* These tags always have simple identifiers already; no need
14243 to canonicalize them. */
14244 part_die->name = DW_STRING (&attr);
14248 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
14249 &objfile->objfile_obstack);
14253 case DW_AT_linkage_name:
14254 case DW_AT_MIPS_linkage_name:
14255 /* Note that both forms of linkage name might appear. We
14256 assume they will be the same, and we only store the last
14258 if (cu->language == language_ada)
14259 part_die->name = DW_STRING (&attr);
14260 part_die->linkage_name = DW_STRING (&attr);
14263 has_low_pc_attr = 1;
14264 part_die->lowpc = DW_ADDR (&attr);
14266 case DW_AT_high_pc:
14267 has_high_pc_attr = 1;
14268 if (attr.form == DW_FORM_addr
14269 || attr.form == DW_FORM_GNU_addr_index)
14270 part_die->highpc = DW_ADDR (&attr);
14273 high_pc_relative = 1;
14274 part_die->highpc = DW_UNSND (&attr);
14277 case DW_AT_location:
14278 /* Support the .debug_loc offsets. */
14279 if (attr_form_is_block (&attr))
14281 part_die->d.locdesc = DW_BLOCK (&attr);
14283 else if (attr_form_is_section_offset (&attr))
14285 dwarf2_complex_location_expr_complaint ();
14289 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14290 "partial symbol information");
14293 case DW_AT_external:
14294 part_die->is_external = DW_UNSND (&attr);
14296 case DW_AT_declaration:
14297 part_die->is_declaration = DW_UNSND (&attr);
14300 part_die->has_type = 1;
14302 case DW_AT_abstract_origin:
14303 case DW_AT_specification:
14304 case DW_AT_extension:
14305 part_die->has_specification = 1;
14306 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
14307 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14308 || cu->per_cu->is_dwz);
14310 case DW_AT_sibling:
14311 /* Ignore absolute siblings, they might point outside of
14312 the current compile unit. */
14313 if (attr.form == DW_FORM_ref_addr)
14314 complaint (&symfile_complaints,
14315 _("ignoring absolute DW_AT_sibling"));
14317 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
14319 case DW_AT_byte_size:
14320 part_die->has_byte_size = 1;
14322 case DW_AT_calling_convention:
14323 /* DWARF doesn't provide a way to identify a program's source-level
14324 entry point. DW_AT_calling_convention attributes are only meant
14325 to describe functions' calling conventions.
14327 However, because it's a necessary piece of information in
14328 Fortran, and because DW_CC_program is the only piece of debugging
14329 information whose definition refers to a 'main program' at all,
14330 several compilers have begun marking Fortran main programs with
14331 DW_CC_program --- even when those functions use the standard
14332 calling conventions.
14334 So until DWARF specifies a way to provide this information and
14335 compilers pick up the new representation, we'll support this
14337 if (DW_UNSND (&attr) == DW_CC_program
14338 && cu->language == language_fortran)
14340 set_main_name (part_die->name);
14342 /* As this DIE has a static linkage the name would be difficult
14343 to look up later. */
14344 language_of_main = language_fortran;
14348 if (DW_UNSND (&attr) == DW_INL_inlined
14349 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14350 part_die->may_be_inlined = 1;
14354 if (part_die->tag == DW_TAG_imported_unit)
14356 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14357 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14358 || cu->per_cu->is_dwz);
14367 if (high_pc_relative)
14368 part_die->highpc += part_die->lowpc;
14370 if (has_low_pc_attr && has_high_pc_attr)
14372 /* When using the GNU linker, .gnu.linkonce. sections are used to
14373 eliminate duplicate copies of functions and vtables and such.
14374 The linker will arbitrarily choose one and discard the others.
14375 The AT_*_pc values for such functions refer to local labels in
14376 these sections. If the section from that file was discarded, the
14377 labels are not in the output, so the relocs get a value of 0.
14378 If this is a discarded function, mark the pc bounds as invalid,
14379 so that GDB will ignore it. */
14380 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14382 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14384 complaint (&symfile_complaints,
14385 _("DW_AT_low_pc %s is zero "
14386 "for DIE at 0x%x [in module %s]"),
14387 paddress (gdbarch, part_die->lowpc),
14388 part_die->offset.sect_off, objfile->name);
14390 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14391 else if (part_die->lowpc >= part_die->highpc)
14393 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14395 complaint (&symfile_complaints,
14396 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14397 "for DIE at 0x%x [in module %s]"),
14398 paddress (gdbarch, part_die->lowpc),
14399 paddress (gdbarch, part_die->highpc),
14400 part_die->offset.sect_off, objfile->name);
14403 part_die->has_pc_info = 1;
14409 /* Find a cached partial DIE at OFFSET in CU. */
14411 static struct partial_die_info *
14412 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
14414 struct partial_die_info *lookup_die = NULL;
14415 struct partial_die_info part_die;
14417 part_die.offset = offset;
14418 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14424 /* Find a partial DIE at OFFSET, which may or may not be in CU,
14425 except in the case of .debug_types DIEs which do not reference
14426 outside their CU (they do however referencing other types via
14427 DW_FORM_ref_sig8). */
14429 static struct partial_die_info *
14430 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
14432 struct objfile *objfile = cu->objfile;
14433 struct dwarf2_per_cu_data *per_cu = NULL;
14434 struct partial_die_info *pd = NULL;
14436 if (offset_in_dwz == cu->per_cu->is_dwz
14437 && offset_in_cu_p (&cu->header, offset))
14439 pd = find_partial_die_in_comp_unit (offset, cu);
14442 /* We missed recording what we needed.
14443 Load all dies and try again. */
14444 per_cu = cu->per_cu;
14448 /* TUs don't reference other CUs/TUs (except via type signatures). */
14449 if (cu->per_cu->is_debug_types)
14451 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14452 " external reference to offset 0x%lx [in module %s].\n"),
14453 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14454 bfd_get_filename (objfile->obfd));
14456 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14459 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14460 load_partial_comp_unit (per_cu);
14462 per_cu->cu->last_used = 0;
14463 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14466 /* If we didn't find it, and not all dies have been loaded,
14467 load them all and try again. */
14469 if (pd == NULL && per_cu->load_all_dies == 0)
14471 per_cu->load_all_dies = 1;
14473 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14474 THIS_CU->cu may already be in use. So we can't just free it and
14475 replace its DIEs with the ones we read in. Instead, we leave those
14476 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14477 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14479 load_partial_comp_unit (per_cu);
14481 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14485 internal_error (__FILE__, __LINE__,
14486 _("could not find partial DIE 0x%x "
14487 "in cache [from module %s]\n"),
14488 offset.sect_off, bfd_get_filename (objfile->obfd));
14492 /* See if we can figure out if the class lives in a namespace. We do
14493 this by looking for a member function; its demangled name will
14494 contain namespace info, if there is any. */
14497 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14498 struct dwarf2_cu *cu)
14500 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14501 what template types look like, because the demangler
14502 frequently doesn't give the same name as the debug info. We
14503 could fix this by only using the demangled name to get the
14504 prefix (but see comment in read_structure_type). */
14506 struct partial_die_info *real_pdi;
14507 struct partial_die_info *child_pdi;
14509 /* If this DIE (this DIE's specification, if any) has a parent, then
14510 we should not do this. We'll prepend the parent's fully qualified
14511 name when we create the partial symbol. */
14513 real_pdi = struct_pdi;
14514 while (real_pdi->has_specification)
14515 real_pdi = find_partial_die (real_pdi->spec_offset,
14516 real_pdi->spec_is_dwz, cu);
14518 if (real_pdi->die_parent != NULL)
14521 for (child_pdi = struct_pdi->die_child;
14523 child_pdi = child_pdi->die_sibling)
14525 if (child_pdi->tag == DW_TAG_subprogram
14526 && child_pdi->linkage_name != NULL)
14528 char *actual_class_name
14529 = language_class_name_from_physname (cu->language_defn,
14530 child_pdi->linkage_name);
14531 if (actual_class_name != NULL)
14534 = obstack_copy0 (&cu->objfile->objfile_obstack,
14536 strlen (actual_class_name));
14537 xfree (actual_class_name);
14544 /* Adjust PART_DIE before generating a symbol for it. This function
14545 may set the is_external flag or change the DIE's name. */
14548 fixup_partial_die (struct partial_die_info *part_die,
14549 struct dwarf2_cu *cu)
14551 /* Once we've fixed up a die, there's no point in doing so again.
14552 This also avoids a memory leak if we were to call
14553 guess_partial_die_structure_name multiple times. */
14554 if (part_die->fixup_called)
14557 /* If we found a reference attribute and the DIE has no name, try
14558 to find a name in the referred to DIE. */
14560 if (part_die->name == NULL && part_die->has_specification)
14562 struct partial_die_info *spec_die;
14564 spec_die = find_partial_die (part_die->spec_offset,
14565 part_die->spec_is_dwz, cu);
14567 fixup_partial_die (spec_die, cu);
14569 if (spec_die->name)
14571 part_die->name = spec_die->name;
14573 /* Copy DW_AT_external attribute if it is set. */
14574 if (spec_die->is_external)
14575 part_die->is_external = spec_die->is_external;
14579 /* Set default names for some unnamed DIEs. */
14581 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14582 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14584 /* If there is no parent die to provide a namespace, and there are
14585 children, see if we can determine the namespace from their linkage
14587 if (cu->language == language_cplus
14588 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14589 && part_die->die_parent == NULL
14590 && part_die->has_children
14591 && (part_die->tag == DW_TAG_class_type
14592 || part_die->tag == DW_TAG_structure_type
14593 || part_die->tag == DW_TAG_union_type))
14594 guess_partial_die_structure_name (part_die, cu);
14596 /* GCC might emit a nameless struct or union that has a linkage
14597 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14598 if (part_die->name == NULL
14599 && (part_die->tag == DW_TAG_class_type
14600 || part_die->tag == DW_TAG_interface_type
14601 || part_die->tag == DW_TAG_structure_type
14602 || part_die->tag == DW_TAG_union_type)
14603 && part_die->linkage_name != NULL)
14607 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
14612 /* Strip any leading namespaces/classes, keep only the base name.
14613 DW_AT_name for named DIEs does not contain the prefixes. */
14614 base = strrchr (demangled, ':');
14615 if (base && base > demangled && base[-1] == ':')
14620 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14621 base, strlen (base));
14626 part_die->fixup_called = 1;
14629 /* Read an attribute value described by an attribute form. */
14631 static const gdb_byte *
14632 read_attribute_value (const struct die_reader_specs *reader,
14633 struct attribute *attr, unsigned form,
14634 const gdb_byte *info_ptr)
14636 struct dwarf2_cu *cu = reader->cu;
14637 bfd *abfd = reader->abfd;
14638 struct comp_unit_head *cu_header = &cu->header;
14639 unsigned int bytes_read;
14640 struct dwarf_block *blk;
14645 case DW_FORM_ref_addr:
14646 if (cu->header.version == 2)
14647 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14649 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14650 &cu->header, &bytes_read);
14651 info_ptr += bytes_read;
14653 case DW_FORM_GNU_ref_alt:
14654 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14655 info_ptr += bytes_read;
14658 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14659 info_ptr += bytes_read;
14661 case DW_FORM_block2:
14662 blk = dwarf_alloc_block (cu);
14663 blk->size = read_2_bytes (abfd, info_ptr);
14665 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14666 info_ptr += blk->size;
14667 DW_BLOCK (attr) = blk;
14669 case DW_FORM_block4:
14670 blk = dwarf_alloc_block (cu);
14671 blk->size = read_4_bytes (abfd, info_ptr);
14673 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14674 info_ptr += blk->size;
14675 DW_BLOCK (attr) = blk;
14677 case DW_FORM_data2:
14678 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14681 case DW_FORM_data4:
14682 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14685 case DW_FORM_data8:
14686 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14689 case DW_FORM_sec_offset:
14690 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14691 info_ptr += bytes_read;
14693 case DW_FORM_string:
14694 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14695 DW_STRING_IS_CANONICAL (attr) = 0;
14696 info_ptr += bytes_read;
14699 if (!cu->per_cu->is_dwz)
14701 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14703 DW_STRING_IS_CANONICAL (attr) = 0;
14704 info_ptr += bytes_read;
14708 case DW_FORM_GNU_strp_alt:
14710 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14711 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14714 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14715 DW_STRING_IS_CANONICAL (attr) = 0;
14716 info_ptr += bytes_read;
14719 case DW_FORM_exprloc:
14720 case DW_FORM_block:
14721 blk = dwarf_alloc_block (cu);
14722 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14723 info_ptr += bytes_read;
14724 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14725 info_ptr += blk->size;
14726 DW_BLOCK (attr) = blk;
14728 case DW_FORM_block1:
14729 blk = dwarf_alloc_block (cu);
14730 blk->size = read_1_byte (abfd, info_ptr);
14732 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14733 info_ptr += blk->size;
14734 DW_BLOCK (attr) = blk;
14736 case DW_FORM_data1:
14737 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14741 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14744 case DW_FORM_flag_present:
14745 DW_UNSND (attr) = 1;
14747 case DW_FORM_sdata:
14748 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14749 info_ptr += bytes_read;
14751 case DW_FORM_udata:
14752 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14753 info_ptr += bytes_read;
14756 DW_UNSND (attr) = (cu->header.offset.sect_off
14757 + read_1_byte (abfd, info_ptr));
14761 DW_UNSND (attr) = (cu->header.offset.sect_off
14762 + read_2_bytes (abfd, info_ptr));
14766 DW_UNSND (attr) = (cu->header.offset.sect_off
14767 + read_4_bytes (abfd, info_ptr));
14771 DW_UNSND (attr) = (cu->header.offset.sect_off
14772 + read_8_bytes (abfd, info_ptr));
14775 case DW_FORM_ref_sig8:
14776 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
14779 case DW_FORM_ref_udata:
14780 DW_UNSND (attr) = (cu->header.offset.sect_off
14781 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14782 info_ptr += bytes_read;
14784 case DW_FORM_indirect:
14785 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14786 info_ptr += bytes_read;
14787 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14789 case DW_FORM_GNU_addr_index:
14790 if (reader->dwo_file == NULL)
14792 /* For now flag a hard error.
14793 Later we can turn this into a complaint. */
14794 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14795 dwarf_form_name (form),
14796 bfd_get_filename (abfd));
14798 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14799 info_ptr += bytes_read;
14801 case DW_FORM_GNU_str_index:
14802 if (reader->dwo_file == NULL)
14804 /* For now flag a hard error.
14805 Later we can turn this into a complaint if warranted. */
14806 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14807 dwarf_form_name (form),
14808 bfd_get_filename (abfd));
14811 ULONGEST str_index =
14812 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14814 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14815 DW_STRING_IS_CANONICAL (attr) = 0;
14816 info_ptr += bytes_read;
14820 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14821 dwarf_form_name (form),
14822 bfd_get_filename (abfd));
14826 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
14827 attr->form = DW_FORM_GNU_ref_alt;
14829 /* We have seen instances where the compiler tried to emit a byte
14830 size attribute of -1 which ended up being encoded as an unsigned
14831 0xffffffff. Although 0xffffffff is technically a valid size value,
14832 an object of this size seems pretty unlikely so we can relatively
14833 safely treat these cases as if the size attribute was invalid and
14834 treat them as zero by default. */
14835 if (attr->name == DW_AT_byte_size
14836 && form == DW_FORM_data4
14837 && DW_UNSND (attr) >= 0xffffffff)
14840 (&symfile_complaints,
14841 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14842 hex_string (DW_UNSND (attr)));
14843 DW_UNSND (attr) = 0;
14849 /* Read an attribute described by an abbreviated attribute. */
14851 static const gdb_byte *
14852 read_attribute (const struct die_reader_specs *reader,
14853 struct attribute *attr, struct attr_abbrev *abbrev,
14854 const gdb_byte *info_ptr)
14856 attr->name = abbrev->name;
14857 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14860 /* Read dwarf information from a buffer. */
14862 static unsigned int
14863 read_1_byte (bfd *abfd, const gdb_byte *buf)
14865 return bfd_get_8 (abfd, buf);
14869 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14871 return bfd_get_signed_8 (abfd, buf);
14874 static unsigned int
14875 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14877 return bfd_get_16 (abfd, buf);
14881 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14883 return bfd_get_signed_16 (abfd, buf);
14886 static unsigned int
14887 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14889 return bfd_get_32 (abfd, buf);
14893 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14895 return bfd_get_signed_32 (abfd, buf);
14899 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14901 return bfd_get_64 (abfd, buf);
14905 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
14906 unsigned int *bytes_read)
14908 struct comp_unit_head *cu_header = &cu->header;
14909 CORE_ADDR retval = 0;
14911 if (cu_header->signed_addr_p)
14913 switch (cu_header->addr_size)
14916 retval = bfd_get_signed_16 (abfd, buf);
14919 retval = bfd_get_signed_32 (abfd, buf);
14922 retval = bfd_get_signed_64 (abfd, buf);
14925 internal_error (__FILE__, __LINE__,
14926 _("read_address: bad switch, signed [in module %s]"),
14927 bfd_get_filename (abfd));
14932 switch (cu_header->addr_size)
14935 retval = bfd_get_16 (abfd, buf);
14938 retval = bfd_get_32 (abfd, buf);
14941 retval = bfd_get_64 (abfd, buf);
14944 internal_error (__FILE__, __LINE__,
14945 _("read_address: bad switch, "
14946 "unsigned [in module %s]"),
14947 bfd_get_filename (abfd));
14951 *bytes_read = cu_header->addr_size;
14955 /* Read the initial length from a section. The (draft) DWARF 3
14956 specification allows the initial length to take up either 4 bytes
14957 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14958 bytes describe the length and all offsets will be 8 bytes in length
14961 An older, non-standard 64-bit format is also handled by this
14962 function. The older format in question stores the initial length
14963 as an 8-byte quantity without an escape value. Lengths greater
14964 than 2^32 aren't very common which means that the initial 4 bytes
14965 is almost always zero. Since a length value of zero doesn't make
14966 sense for the 32-bit format, this initial zero can be considered to
14967 be an escape value which indicates the presence of the older 64-bit
14968 format. As written, the code can't detect (old format) lengths
14969 greater than 4GB. If it becomes necessary to handle lengths
14970 somewhat larger than 4GB, we could allow other small values (such
14971 as the non-sensical values of 1, 2, and 3) to also be used as
14972 escape values indicating the presence of the old format.
14974 The value returned via bytes_read should be used to increment the
14975 relevant pointer after calling read_initial_length().
14977 [ Note: read_initial_length() and read_offset() are based on the
14978 document entitled "DWARF Debugging Information Format", revision
14979 3, draft 8, dated November 19, 2001. This document was obtained
14982 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14984 This document is only a draft and is subject to change. (So beware.)
14986 Details regarding the older, non-standard 64-bit format were
14987 determined empirically by examining 64-bit ELF files produced by
14988 the SGI toolchain on an IRIX 6.5 machine.
14990 - Kevin, July 16, 2002
14994 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
14996 LONGEST length = bfd_get_32 (abfd, buf);
14998 if (length == 0xffffffff)
15000 length = bfd_get_64 (abfd, buf + 4);
15003 else if (length == 0)
15005 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
15006 length = bfd_get_64 (abfd, buf);
15017 /* Cover function for read_initial_length.
15018 Returns the length of the object at BUF, and stores the size of the
15019 initial length in *BYTES_READ and stores the size that offsets will be in
15021 If the initial length size is not equivalent to that specified in
15022 CU_HEADER then issue a complaint.
15023 This is useful when reading non-comp-unit headers. */
15026 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
15027 const struct comp_unit_head *cu_header,
15028 unsigned int *bytes_read,
15029 unsigned int *offset_size)
15031 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15033 gdb_assert (cu_header->initial_length_size == 4
15034 || cu_header->initial_length_size == 8
15035 || cu_header->initial_length_size == 12);
15037 if (cu_header->initial_length_size != *bytes_read)
15038 complaint (&symfile_complaints,
15039 _("intermixed 32-bit and 64-bit DWARF sections"));
15041 *offset_size = (*bytes_read == 4) ? 4 : 8;
15045 /* Read an offset from the data stream. The size of the offset is
15046 given by cu_header->offset_size. */
15049 read_offset (bfd *abfd, const gdb_byte *buf,
15050 const struct comp_unit_head *cu_header,
15051 unsigned int *bytes_read)
15053 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
15055 *bytes_read = cu_header->offset_size;
15059 /* Read an offset from the data stream. */
15062 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15064 LONGEST retval = 0;
15066 switch (offset_size)
15069 retval = bfd_get_32 (abfd, buf);
15072 retval = bfd_get_64 (abfd, buf);
15075 internal_error (__FILE__, __LINE__,
15076 _("read_offset_1: bad switch [in module %s]"),
15077 bfd_get_filename (abfd));
15083 static const gdb_byte *
15084 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15086 /* If the size of a host char is 8 bits, we can return a pointer
15087 to the buffer, otherwise we have to copy the data to a buffer
15088 allocated on the temporary obstack. */
15089 gdb_assert (HOST_CHAR_BIT == 8);
15093 static const char *
15094 read_direct_string (bfd *abfd, const gdb_byte *buf,
15095 unsigned int *bytes_read_ptr)
15097 /* If the size of a host char is 8 bits, we can return a pointer
15098 to the string, otherwise we have to copy the string to a buffer
15099 allocated on the temporary obstack. */
15100 gdb_assert (HOST_CHAR_BIT == 8);
15103 *bytes_read_ptr = 1;
15106 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15107 return (const char *) buf;
15110 static const char *
15111 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15113 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15114 if (dwarf2_per_objfile->str.buffer == NULL)
15115 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15116 bfd_get_filename (abfd));
15117 if (str_offset >= dwarf2_per_objfile->str.size)
15118 error (_("DW_FORM_strp pointing outside of "
15119 ".debug_str section [in module %s]"),
15120 bfd_get_filename (abfd));
15121 gdb_assert (HOST_CHAR_BIT == 8);
15122 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15124 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15127 /* Read a string at offset STR_OFFSET in the .debug_str section from
15128 the .dwz file DWZ. Throw an error if the offset is too large. If
15129 the string consists of a single NUL byte, return NULL; otherwise
15130 return a pointer to the string. */
15132 static const char *
15133 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15135 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15137 if (dwz->str.buffer == NULL)
15138 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15139 "section [in module %s]"),
15140 bfd_get_filename (dwz->dwz_bfd));
15141 if (str_offset >= dwz->str.size)
15142 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15143 ".debug_str section [in module %s]"),
15144 bfd_get_filename (dwz->dwz_bfd));
15145 gdb_assert (HOST_CHAR_BIT == 8);
15146 if (dwz->str.buffer[str_offset] == '\0')
15148 return (const char *) (dwz->str.buffer + str_offset);
15151 static const char *
15152 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15153 const struct comp_unit_head *cu_header,
15154 unsigned int *bytes_read_ptr)
15156 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15158 return read_indirect_string_at_offset (abfd, str_offset);
15162 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15163 unsigned int *bytes_read_ptr)
15166 unsigned int num_read;
15168 unsigned char byte;
15176 byte = bfd_get_8 (abfd, buf);
15179 result |= ((ULONGEST) (byte & 127) << shift);
15180 if ((byte & 128) == 0)
15186 *bytes_read_ptr = num_read;
15191 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15192 unsigned int *bytes_read_ptr)
15195 int i, shift, num_read;
15196 unsigned char byte;
15204 byte = bfd_get_8 (abfd, buf);
15207 result |= ((LONGEST) (byte & 127) << shift);
15209 if ((byte & 128) == 0)
15214 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
15215 result |= -(((LONGEST) 1) << shift);
15216 *bytes_read_ptr = num_read;
15220 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
15221 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15222 ADDR_SIZE is the size of addresses from the CU header. */
15225 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15227 struct objfile *objfile = dwarf2_per_objfile->objfile;
15228 bfd *abfd = objfile->obfd;
15229 const gdb_byte *info_ptr;
15231 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15232 if (dwarf2_per_objfile->addr.buffer == NULL)
15233 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15235 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15236 error (_("DW_FORM_addr_index pointing outside of "
15237 ".debug_addr section [in module %s]"),
15239 info_ptr = (dwarf2_per_objfile->addr.buffer
15240 + addr_base + addr_index * addr_size);
15241 if (addr_size == 4)
15242 return bfd_get_32 (abfd, info_ptr);
15244 return bfd_get_64 (abfd, info_ptr);
15247 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15250 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15252 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15255 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15258 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
15259 unsigned int *bytes_read)
15261 bfd *abfd = cu->objfile->obfd;
15262 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15264 return read_addr_index (cu, addr_index);
15267 /* Data structure to pass results from dwarf2_read_addr_index_reader
15268 back to dwarf2_read_addr_index. */
15270 struct dwarf2_read_addr_index_data
15272 ULONGEST addr_base;
15276 /* die_reader_func for dwarf2_read_addr_index. */
15279 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
15280 const gdb_byte *info_ptr,
15281 struct die_info *comp_unit_die,
15285 struct dwarf2_cu *cu = reader->cu;
15286 struct dwarf2_read_addr_index_data *aidata =
15287 (struct dwarf2_read_addr_index_data *) data;
15289 aidata->addr_base = cu->addr_base;
15290 aidata->addr_size = cu->header.addr_size;
15293 /* Given an index in .debug_addr, fetch the value.
15294 NOTE: This can be called during dwarf expression evaluation,
15295 long after the debug information has been read, and thus per_cu->cu
15296 may no longer exist. */
15299 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15300 unsigned int addr_index)
15302 struct objfile *objfile = per_cu->objfile;
15303 struct dwarf2_cu *cu = per_cu->cu;
15304 ULONGEST addr_base;
15307 /* This is intended to be called from outside this file. */
15308 dw2_setup (objfile);
15310 /* We need addr_base and addr_size.
15311 If we don't have PER_CU->cu, we have to get it.
15312 Nasty, but the alternative is storing the needed info in PER_CU,
15313 which at this point doesn't seem justified: it's not clear how frequently
15314 it would get used and it would increase the size of every PER_CU.
15315 Entry points like dwarf2_per_cu_addr_size do a similar thing
15316 so we're not in uncharted territory here.
15317 Alas we need to be a bit more complicated as addr_base is contained
15320 We don't need to read the entire CU(/TU).
15321 We just need the header and top level die.
15323 IWBN to use the aging mechanism to let us lazily later discard the CU.
15324 For now we skip this optimization. */
15328 addr_base = cu->addr_base;
15329 addr_size = cu->header.addr_size;
15333 struct dwarf2_read_addr_index_data aidata;
15335 /* Note: We can't use init_cutu_and_read_dies_simple here,
15336 we need addr_base. */
15337 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15338 dwarf2_read_addr_index_reader, &aidata);
15339 addr_base = aidata.addr_base;
15340 addr_size = aidata.addr_size;
15343 return read_addr_index_1 (addr_index, addr_base, addr_size);
15346 /* Given a DW_AT_str_index, fetch the string. */
15348 static const char *
15349 read_str_index (const struct die_reader_specs *reader,
15350 struct dwarf2_cu *cu, ULONGEST str_index)
15352 struct objfile *objfile = dwarf2_per_objfile->objfile;
15353 const char *dwo_name = objfile->name;
15354 bfd *abfd = objfile->obfd;
15355 struct dwo_sections *sections = &reader->dwo_file->sections;
15356 const gdb_byte *info_ptr;
15357 ULONGEST str_offset;
15359 dwarf2_read_section (objfile, §ions->str);
15360 dwarf2_read_section (objfile, §ions->str_offsets);
15361 if (sections->str.buffer == NULL)
15362 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15363 " in CU at offset 0x%lx [in module %s]"),
15364 (long) cu->header.offset.sect_off, dwo_name);
15365 if (sections->str_offsets.buffer == NULL)
15366 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15367 " in CU at offset 0x%lx [in module %s]"),
15368 (long) cu->header.offset.sect_off, dwo_name);
15369 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15370 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15371 " section in CU at offset 0x%lx [in module %s]"),
15372 (long) cu->header.offset.sect_off, dwo_name);
15373 info_ptr = (sections->str_offsets.buffer
15374 + str_index * cu->header.offset_size);
15375 if (cu->header.offset_size == 4)
15376 str_offset = bfd_get_32 (abfd, info_ptr);
15378 str_offset = bfd_get_64 (abfd, info_ptr);
15379 if (str_offset >= sections->str.size)
15380 error (_("Offset from DW_FORM_str_index pointing outside of"
15381 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15382 (long) cu->header.offset.sect_off, dwo_name);
15383 return (const char *) (sections->str.buffer + str_offset);
15386 /* Return the length of an LEB128 number in BUF. */
15389 leb128_size (const gdb_byte *buf)
15391 const gdb_byte *begin = buf;
15397 if ((byte & 128) == 0)
15398 return buf - begin;
15403 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
15411 cu->language = language_c;
15413 case DW_LANG_C_plus_plus:
15414 cu->language = language_cplus;
15417 cu->language = language_d;
15419 case DW_LANG_Fortran77:
15420 case DW_LANG_Fortran90:
15421 case DW_LANG_Fortran95:
15422 cu->language = language_fortran;
15425 cu->language = language_go;
15427 case DW_LANG_Mips_Assembler:
15428 cu->language = language_asm;
15431 cu->language = language_java;
15433 case DW_LANG_Ada83:
15434 case DW_LANG_Ada95:
15435 cu->language = language_ada;
15437 case DW_LANG_Modula2:
15438 cu->language = language_m2;
15440 case DW_LANG_Pascal83:
15441 cu->language = language_pascal;
15444 cu->language = language_objc;
15446 case DW_LANG_Cobol74:
15447 case DW_LANG_Cobol85:
15449 cu->language = language_minimal;
15452 cu->language_defn = language_def (cu->language);
15455 /* Return the named attribute or NULL if not there. */
15457 static struct attribute *
15458 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15463 struct attribute *spec = NULL;
15465 for (i = 0; i < die->num_attrs; ++i)
15467 if (die->attrs[i].name == name)
15468 return &die->attrs[i];
15469 if (die->attrs[i].name == DW_AT_specification
15470 || die->attrs[i].name == DW_AT_abstract_origin)
15471 spec = &die->attrs[i];
15477 die = follow_die_ref (die, spec, &cu);
15483 /* Return the named attribute or NULL if not there,
15484 but do not follow DW_AT_specification, etc.
15485 This is for use in contexts where we're reading .debug_types dies.
15486 Following DW_AT_specification, DW_AT_abstract_origin will take us
15487 back up the chain, and we want to go down. */
15489 static struct attribute *
15490 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15494 for (i = 0; i < die->num_attrs; ++i)
15495 if (die->attrs[i].name == name)
15496 return &die->attrs[i];
15501 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15502 and holds a non-zero value. This function should only be used for
15503 DW_FORM_flag or DW_FORM_flag_present attributes. */
15506 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15508 struct attribute *attr = dwarf2_attr (die, name, cu);
15510 return (attr && DW_UNSND (attr));
15514 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15516 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15517 which value is non-zero. However, we have to be careful with
15518 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15519 (via dwarf2_flag_true_p) follows this attribute. So we may
15520 end up accidently finding a declaration attribute that belongs
15521 to a different DIE referenced by the specification attribute,
15522 even though the given DIE does not have a declaration attribute. */
15523 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15524 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15527 /* Return the die giving the specification for DIE, if there is
15528 one. *SPEC_CU is the CU containing DIE on input, and the CU
15529 containing the return value on output. If there is no
15530 specification, but there is an abstract origin, that is
15533 static struct die_info *
15534 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15536 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15539 if (spec_attr == NULL)
15540 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15542 if (spec_attr == NULL)
15545 return follow_die_ref (die, spec_attr, spec_cu);
15548 /* Free the line_header structure *LH, and any arrays and strings it
15550 NOTE: This is also used as a "cleanup" function. */
15553 free_line_header (struct line_header *lh)
15555 if (lh->standard_opcode_lengths)
15556 xfree (lh->standard_opcode_lengths);
15558 /* Remember that all the lh->file_names[i].name pointers are
15559 pointers into debug_line_buffer, and don't need to be freed. */
15560 if (lh->file_names)
15561 xfree (lh->file_names);
15563 /* Similarly for the include directory names. */
15564 if (lh->include_dirs)
15565 xfree (lh->include_dirs);
15570 /* Add an entry to LH's include directory table. */
15573 add_include_dir (struct line_header *lh, const char *include_dir)
15575 /* Grow the array if necessary. */
15576 if (lh->include_dirs_size == 0)
15578 lh->include_dirs_size = 1; /* for testing */
15579 lh->include_dirs = xmalloc (lh->include_dirs_size
15580 * sizeof (*lh->include_dirs));
15582 else if (lh->num_include_dirs >= lh->include_dirs_size)
15584 lh->include_dirs_size *= 2;
15585 lh->include_dirs = xrealloc (lh->include_dirs,
15586 (lh->include_dirs_size
15587 * sizeof (*lh->include_dirs)));
15590 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15593 /* Add an entry to LH's file name table. */
15596 add_file_name (struct line_header *lh,
15598 unsigned int dir_index,
15599 unsigned int mod_time,
15600 unsigned int length)
15602 struct file_entry *fe;
15604 /* Grow the array if necessary. */
15605 if (lh->file_names_size == 0)
15607 lh->file_names_size = 1; /* for testing */
15608 lh->file_names = xmalloc (lh->file_names_size
15609 * sizeof (*lh->file_names));
15611 else if (lh->num_file_names >= lh->file_names_size)
15613 lh->file_names_size *= 2;
15614 lh->file_names = xrealloc (lh->file_names,
15615 (lh->file_names_size
15616 * sizeof (*lh->file_names)));
15619 fe = &lh->file_names[lh->num_file_names++];
15621 fe->dir_index = dir_index;
15622 fe->mod_time = mod_time;
15623 fe->length = length;
15624 fe->included_p = 0;
15628 /* A convenience function to find the proper .debug_line section for a
15631 static struct dwarf2_section_info *
15632 get_debug_line_section (struct dwarf2_cu *cu)
15634 struct dwarf2_section_info *section;
15636 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15638 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15639 section = &cu->dwo_unit->dwo_file->sections.line;
15640 else if (cu->per_cu->is_dwz)
15642 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15644 section = &dwz->line;
15647 section = &dwarf2_per_objfile->line;
15652 /* Read the statement program header starting at OFFSET in
15653 .debug_line, or .debug_line.dwo. Return a pointer
15654 to a struct line_header, allocated using xmalloc.
15656 NOTE: the strings in the include directory and file name tables of
15657 the returned object point into the dwarf line section buffer,
15658 and must not be freed. */
15660 static struct line_header *
15661 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15663 struct cleanup *back_to;
15664 struct line_header *lh;
15665 const gdb_byte *line_ptr;
15666 unsigned int bytes_read, offset_size;
15668 const char *cur_dir, *cur_file;
15669 struct dwarf2_section_info *section;
15672 section = get_debug_line_section (cu);
15673 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15674 if (section->buffer == NULL)
15676 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15677 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15679 complaint (&symfile_complaints, _("missing .debug_line section"));
15683 /* We can't do this until we know the section is non-empty.
15684 Only then do we know we have such a section. */
15685 abfd = section->asection->owner;
15687 /* Make sure that at least there's room for the total_length field.
15688 That could be 12 bytes long, but we're just going to fudge that. */
15689 if (offset + 4 >= section->size)
15691 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15695 lh = xmalloc (sizeof (*lh));
15696 memset (lh, 0, sizeof (*lh));
15697 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15700 line_ptr = section->buffer + offset;
15702 /* Read in the header. */
15704 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15705 &bytes_read, &offset_size);
15706 line_ptr += bytes_read;
15707 if (line_ptr + lh->total_length > (section->buffer + section->size))
15709 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15710 do_cleanups (back_to);
15713 lh->statement_program_end = line_ptr + lh->total_length;
15714 lh->version = read_2_bytes (abfd, line_ptr);
15716 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15717 line_ptr += offset_size;
15718 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15720 if (lh->version >= 4)
15722 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15726 lh->maximum_ops_per_instruction = 1;
15728 if (lh->maximum_ops_per_instruction == 0)
15730 lh->maximum_ops_per_instruction = 1;
15731 complaint (&symfile_complaints,
15732 _("invalid maximum_ops_per_instruction "
15733 "in `.debug_line' section"));
15736 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15738 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15740 lh->line_range = read_1_byte (abfd, line_ptr);
15742 lh->opcode_base = read_1_byte (abfd, line_ptr);
15744 lh->standard_opcode_lengths
15745 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15747 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15748 for (i = 1; i < lh->opcode_base; ++i)
15750 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15754 /* Read directory table. */
15755 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15757 line_ptr += bytes_read;
15758 add_include_dir (lh, cur_dir);
15760 line_ptr += bytes_read;
15762 /* Read file name table. */
15763 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15765 unsigned int dir_index, mod_time, length;
15767 line_ptr += bytes_read;
15768 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15769 line_ptr += bytes_read;
15770 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15771 line_ptr += bytes_read;
15772 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15773 line_ptr += bytes_read;
15775 add_file_name (lh, cur_file, dir_index, mod_time, length);
15777 line_ptr += bytes_read;
15778 lh->statement_program_start = line_ptr;
15780 if (line_ptr > (section->buffer + section->size))
15781 complaint (&symfile_complaints,
15782 _("line number info header doesn't "
15783 "fit in `.debug_line' section"));
15785 discard_cleanups (back_to);
15789 /* Subroutine of dwarf_decode_lines to simplify it.
15790 Return the file name of the psymtab for included file FILE_INDEX
15791 in line header LH of PST.
15792 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15793 If space for the result is malloc'd, it will be freed by a cleanup.
15794 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15796 The function creates dangling cleanup registration. */
15798 static const char *
15799 psymtab_include_file_name (const struct line_header *lh, int file_index,
15800 const struct partial_symtab *pst,
15801 const char *comp_dir)
15803 const struct file_entry fe = lh->file_names [file_index];
15804 const char *include_name = fe.name;
15805 const char *include_name_to_compare = include_name;
15806 const char *dir_name = NULL;
15807 const char *pst_filename;
15808 char *copied_name = NULL;
15812 dir_name = lh->include_dirs[fe.dir_index - 1];
15814 if (!IS_ABSOLUTE_PATH (include_name)
15815 && (dir_name != NULL || comp_dir != NULL))
15817 /* Avoid creating a duplicate psymtab for PST.
15818 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15819 Before we do the comparison, however, we need to account
15820 for DIR_NAME and COMP_DIR.
15821 First prepend dir_name (if non-NULL). If we still don't
15822 have an absolute path prepend comp_dir (if non-NULL).
15823 However, the directory we record in the include-file's
15824 psymtab does not contain COMP_DIR (to match the
15825 corresponding symtab(s)).
15830 bash$ gcc -g ./hello.c
15831 include_name = "hello.c"
15833 DW_AT_comp_dir = comp_dir = "/tmp"
15834 DW_AT_name = "./hello.c" */
15836 if (dir_name != NULL)
15838 char *tem = concat (dir_name, SLASH_STRING,
15839 include_name, (char *)NULL);
15841 make_cleanup (xfree, tem);
15842 include_name = tem;
15843 include_name_to_compare = include_name;
15845 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15847 char *tem = concat (comp_dir, SLASH_STRING,
15848 include_name, (char *)NULL);
15850 make_cleanup (xfree, tem);
15851 include_name_to_compare = tem;
15855 pst_filename = pst->filename;
15856 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15858 copied_name = concat (pst->dirname, SLASH_STRING,
15859 pst_filename, (char *)NULL);
15860 pst_filename = copied_name;
15863 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15865 if (copied_name != NULL)
15866 xfree (copied_name);
15870 return include_name;
15873 /* Ignore this record_line request. */
15876 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15881 /* Subroutine of dwarf_decode_lines to simplify it.
15882 Process the line number information in LH. */
15885 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15886 struct dwarf2_cu *cu, struct partial_symtab *pst)
15888 const gdb_byte *line_ptr, *extended_end;
15889 const gdb_byte *line_end;
15890 unsigned int bytes_read, extended_len;
15891 unsigned char op_code, extended_op, adj_opcode;
15892 CORE_ADDR baseaddr;
15893 struct objfile *objfile = cu->objfile;
15894 bfd *abfd = objfile->obfd;
15895 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15896 const int decode_for_pst_p = (pst != NULL);
15897 struct subfile *last_subfile = NULL;
15898 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15901 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15903 line_ptr = lh->statement_program_start;
15904 line_end = lh->statement_program_end;
15906 /* Read the statement sequences until there's nothing left. */
15907 while (line_ptr < line_end)
15909 /* state machine registers */
15910 CORE_ADDR address = 0;
15911 unsigned int file = 1;
15912 unsigned int line = 1;
15913 unsigned int column = 0;
15914 int is_stmt = lh->default_is_stmt;
15915 int basic_block = 0;
15916 int end_sequence = 0;
15918 unsigned char op_index = 0;
15920 if (!decode_for_pst_p && lh->num_file_names >= file)
15922 /* Start a subfile for the current file of the state machine. */
15923 /* lh->include_dirs and lh->file_names are 0-based, but the
15924 directory and file name numbers in the statement program
15926 struct file_entry *fe = &lh->file_names[file - 1];
15927 const char *dir = NULL;
15930 dir = lh->include_dirs[fe->dir_index - 1];
15932 dwarf2_start_subfile (fe->name, dir, comp_dir);
15935 /* Decode the table. */
15936 while (!end_sequence)
15938 op_code = read_1_byte (abfd, line_ptr);
15940 if (line_ptr > line_end)
15942 dwarf2_debug_line_missing_end_sequence_complaint ();
15946 if (op_code >= lh->opcode_base)
15948 /* Special operand. */
15949 adj_opcode = op_code - lh->opcode_base;
15950 address += (((op_index + (adj_opcode / lh->line_range))
15951 / lh->maximum_ops_per_instruction)
15952 * lh->minimum_instruction_length);
15953 op_index = ((op_index + (adj_opcode / lh->line_range))
15954 % lh->maximum_ops_per_instruction);
15955 line += lh->line_base + (adj_opcode % lh->line_range);
15956 if (lh->num_file_names < file || file == 0)
15957 dwarf2_debug_line_missing_file_complaint ();
15958 /* For now we ignore lines not starting on an
15959 instruction boundary. */
15960 else if (op_index == 0)
15962 lh->file_names[file - 1].included_p = 1;
15963 if (!decode_for_pst_p && is_stmt)
15965 if (last_subfile != current_subfile)
15967 addr = gdbarch_addr_bits_remove (gdbarch, address);
15969 (*p_record_line) (last_subfile, 0, addr);
15970 last_subfile = current_subfile;
15972 /* Append row to matrix using current values. */
15973 addr = gdbarch_addr_bits_remove (gdbarch, address);
15974 (*p_record_line) (current_subfile, line, addr);
15979 else switch (op_code)
15981 case DW_LNS_extended_op:
15982 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15984 line_ptr += bytes_read;
15985 extended_end = line_ptr + extended_len;
15986 extended_op = read_1_byte (abfd, line_ptr);
15988 switch (extended_op)
15990 case DW_LNE_end_sequence:
15991 p_record_line = record_line;
15994 case DW_LNE_set_address:
15995 address = read_address (abfd, line_ptr, cu, &bytes_read);
15997 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15999 /* This line table is for a function which has been
16000 GCd by the linker. Ignore it. PR gdb/12528 */
16003 = line_ptr - get_debug_line_section (cu)->buffer;
16005 complaint (&symfile_complaints,
16006 _(".debug_line address at offset 0x%lx is 0 "
16008 line_offset, objfile->name);
16009 p_record_line = noop_record_line;
16013 line_ptr += bytes_read;
16014 address += baseaddr;
16016 case DW_LNE_define_file:
16018 const char *cur_file;
16019 unsigned int dir_index, mod_time, length;
16021 cur_file = read_direct_string (abfd, line_ptr,
16023 line_ptr += bytes_read;
16025 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16026 line_ptr += bytes_read;
16028 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16029 line_ptr += bytes_read;
16031 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16032 line_ptr += bytes_read;
16033 add_file_name (lh, cur_file, dir_index, mod_time, length);
16036 case DW_LNE_set_discriminator:
16037 /* The discriminator is not interesting to the debugger;
16039 line_ptr = extended_end;
16042 complaint (&symfile_complaints,
16043 _("mangled .debug_line section"));
16046 /* Make sure that we parsed the extended op correctly. If e.g.
16047 we expected a different address size than the producer used,
16048 we may have read the wrong number of bytes. */
16049 if (line_ptr != extended_end)
16051 complaint (&symfile_complaints,
16052 _("mangled .debug_line section"));
16057 if (lh->num_file_names < file || file == 0)
16058 dwarf2_debug_line_missing_file_complaint ();
16061 lh->file_names[file - 1].included_p = 1;
16062 if (!decode_for_pst_p && is_stmt)
16064 if (last_subfile != current_subfile)
16066 addr = gdbarch_addr_bits_remove (gdbarch, address);
16068 (*p_record_line) (last_subfile, 0, addr);
16069 last_subfile = current_subfile;
16071 addr = gdbarch_addr_bits_remove (gdbarch, address);
16072 (*p_record_line) (current_subfile, line, addr);
16077 case DW_LNS_advance_pc:
16080 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16082 address += (((op_index + adjust)
16083 / lh->maximum_ops_per_instruction)
16084 * lh->minimum_instruction_length);
16085 op_index = ((op_index + adjust)
16086 % lh->maximum_ops_per_instruction);
16087 line_ptr += bytes_read;
16090 case DW_LNS_advance_line:
16091 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16092 line_ptr += bytes_read;
16094 case DW_LNS_set_file:
16096 /* The arrays lh->include_dirs and lh->file_names are
16097 0-based, but the directory and file name numbers in
16098 the statement program are 1-based. */
16099 struct file_entry *fe;
16100 const char *dir = NULL;
16102 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16103 line_ptr += bytes_read;
16104 if (lh->num_file_names < file || file == 0)
16105 dwarf2_debug_line_missing_file_complaint ();
16108 fe = &lh->file_names[file - 1];
16110 dir = lh->include_dirs[fe->dir_index - 1];
16111 if (!decode_for_pst_p)
16113 last_subfile = current_subfile;
16114 dwarf2_start_subfile (fe->name, dir, comp_dir);
16119 case DW_LNS_set_column:
16120 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16121 line_ptr += bytes_read;
16123 case DW_LNS_negate_stmt:
16124 is_stmt = (!is_stmt);
16126 case DW_LNS_set_basic_block:
16129 /* Add to the address register of the state machine the
16130 address increment value corresponding to special opcode
16131 255. I.e., this value is scaled by the minimum
16132 instruction length since special opcode 255 would have
16133 scaled the increment. */
16134 case DW_LNS_const_add_pc:
16136 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16138 address += (((op_index + adjust)
16139 / lh->maximum_ops_per_instruction)
16140 * lh->minimum_instruction_length);
16141 op_index = ((op_index + adjust)
16142 % lh->maximum_ops_per_instruction);
16145 case DW_LNS_fixed_advance_pc:
16146 address += read_2_bytes (abfd, line_ptr);
16152 /* Unknown standard opcode, ignore it. */
16155 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16157 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16158 line_ptr += bytes_read;
16163 if (lh->num_file_names < file || file == 0)
16164 dwarf2_debug_line_missing_file_complaint ();
16167 lh->file_names[file - 1].included_p = 1;
16168 if (!decode_for_pst_p)
16170 addr = gdbarch_addr_bits_remove (gdbarch, address);
16171 (*p_record_line) (current_subfile, 0, addr);
16177 /* Decode the Line Number Program (LNP) for the given line_header
16178 structure and CU. The actual information extracted and the type
16179 of structures created from the LNP depends on the value of PST.
16181 1. If PST is NULL, then this procedure uses the data from the program
16182 to create all necessary symbol tables, and their linetables.
16184 2. If PST is not NULL, this procedure reads the program to determine
16185 the list of files included by the unit represented by PST, and
16186 builds all the associated partial symbol tables.
16188 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16189 It is used for relative paths in the line table.
16190 NOTE: When processing partial symtabs (pst != NULL),
16191 comp_dir == pst->dirname.
16193 NOTE: It is important that psymtabs have the same file name (via strcmp)
16194 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16195 symtab we don't use it in the name of the psymtabs we create.
16196 E.g. expand_line_sal requires this when finding psymtabs to expand.
16197 A good testcase for this is mb-inline.exp. */
16200 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16201 struct dwarf2_cu *cu, struct partial_symtab *pst,
16202 int want_line_info)
16204 struct objfile *objfile = cu->objfile;
16205 const int decode_for_pst_p = (pst != NULL);
16206 struct subfile *first_subfile = current_subfile;
16208 if (want_line_info)
16209 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
16211 if (decode_for_pst_p)
16215 /* Now that we're done scanning the Line Header Program, we can
16216 create the psymtab of each included file. */
16217 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16218 if (lh->file_names[file_index].included_p == 1)
16220 const char *include_name =
16221 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16222 if (include_name != NULL)
16223 dwarf2_create_include_psymtab (include_name, pst, objfile);
16228 /* Make sure a symtab is created for every file, even files
16229 which contain only variables (i.e. no code with associated
16233 for (i = 0; i < lh->num_file_names; i++)
16235 const char *dir = NULL;
16236 struct file_entry *fe;
16238 fe = &lh->file_names[i];
16240 dir = lh->include_dirs[fe->dir_index - 1];
16241 dwarf2_start_subfile (fe->name, dir, comp_dir);
16243 /* Skip the main file; we don't need it, and it must be
16244 allocated last, so that it will show up before the
16245 non-primary symtabs in the objfile's symtab list. */
16246 if (current_subfile == first_subfile)
16249 if (current_subfile->symtab == NULL)
16250 current_subfile->symtab = allocate_symtab (current_subfile->name,
16252 fe->symtab = current_subfile->symtab;
16257 /* Start a subfile for DWARF. FILENAME is the name of the file and
16258 DIRNAME the name of the source directory which contains FILENAME
16259 or NULL if not known. COMP_DIR is the compilation directory for the
16260 linetable's compilation unit or NULL if not known.
16261 This routine tries to keep line numbers from identical absolute and
16262 relative file names in a common subfile.
16264 Using the `list' example from the GDB testsuite, which resides in
16265 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16266 of /srcdir/list0.c yields the following debugging information for list0.c:
16268 DW_AT_name: /srcdir/list0.c
16269 DW_AT_comp_dir: /compdir
16270 files.files[0].name: list0.h
16271 files.files[0].dir: /srcdir
16272 files.files[1].name: list0.c
16273 files.files[1].dir: /srcdir
16275 The line number information for list0.c has to end up in a single
16276 subfile, so that `break /srcdir/list0.c:1' works as expected.
16277 start_subfile will ensure that this happens provided that we pass the
16278 concatenation of files.files[1].dir and files.files[1].name as the
16282 dwarf2_start_subfile (const char *filename, const char *dirname,
16283 const char *comp_dir)
16287 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16288 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16289 second argument to start_subfile. To be consistent, we do the
16290 same here. In order not to lose the line information directory,
16291 we concatenate it to the filename when it makes sense.
16292 Note that the Dwarf3 standard says (speaking of filenames in line
16293 information): ``The directory index is ignored for file names
16294 that represent full path names''. Thus ignoring dirname in the
16295 `else' branch below isn't an issue. */
16297 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
16299 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16303 start_subfile (filename, comp_dir);
16309 /* Start a symtab for DWARF.
16310 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16313 dwarf2_start_symtab (struct dwarf2_cu *cu,
16314 const char *name, const char *comp_dir, CORE_ADDR low_pc)
16316 start_symtab (name, comp_dir, low_pc);
16317 record_debugformat ("DWARF 2");
16318 record_producer (cu->producer);
16320 /* We assume that we're processing GCC output. */
16321 processing_gcc_compilation = 2;
16323 cu->processing_has_namespace_info = 0;
16327 var_decode_location (struct attribute *attr, struct symbol *sym,
16328 struct dwarf2_cu *cu)
16330 struct objfile *objfile = cu->objfile;
16331 struct comp_unit_head *cu_header = &cu->header;
16333 /* NOTE drow/2003-01-30: There used to be a comment and some special
16334 code here to turn a symbol with DW_AT_external and a
16335 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16336 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16337 with some versions of binutils) where shared libraries could have
16338 relocations against symbols in their debug information - the
16339 minimal symbol would have the right address, but the debug info
16340 would not. It's no longer necessary, because we will explicitly
16341 apply relocations when we read in the debug information now. */
16343 /* A DW_AT_location attribute with no contents indicates that a
16344 variable has been optimized away. */
16345 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16347 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16351 /* Handle one degenerate form of location expression specially, to
16352 preserve GDB's previous behavior when section offsets are
16353 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16354 then mark this symbol as LOC_STATIC. */
16356 if (attr_form_is_block (attr)
16357 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16358 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16359 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16360 && (DW_BLOCK (attr)->size
16361 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
16363 unsigned int dummy;
16365 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16366 SYMBOL_VALUE_ADDRESS (sym) =
16367 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16369 SYMBOL_VALUE_ADDRESS (sym) =
16370 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
16371 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
16372 fixup_symbol_section (sym, objfile);
16373 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16374 SYMBOL_SECTION (sym));
16378 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16379 expression evaluator, and use LOC_COMPUTED only when necessary
16380 (i.e. when the value of a register or memory location is
16381 referenced, or a thread-local block, etc.). Then again, it might
16382 not be worthwhile. I'm assuming that it isn't unless performance
16383 or memory numbers show me otherwise. */
16385 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
16387 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
16388 cu->has_loclist = 1;
16391 /* Given a pointer to a DWARF information entry, figure out if we need
16392 to make a symbol table entry for it, and if so, create a new entry
16393 and return a pointer to it.
16394 If TYPE is NULL, determine symbol type from the die, otherwise
16395 used the passed type.
16396 If SPACE is not NULL, use it to hold the new symbol. If it is
16397 NULL, allocate a new symbol on the objfile's obstack. */
16399 static struct symbol *
16400 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16401 struct symbol *space)
16403 struct objfile *objfile = cu->objfile;
16404 struct symbol *sym = NULL;
16406 struct attribute *attr = NULL;
16407 struct attribute *attr2 = NULL;
16408 CORE_ADDR baseaddr;
16409 struct pending **list_to_add = NULL;
16411 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
16413 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16415 name = dwarf2_name (die, cu);
16418 const char *linkagename;
16419 int suppress_add = 0;
16424 sym = allocate_symbol (objfile);
16425 OBJSTAT (objfile, n_syms++);
16427 /* Cache this symbol's name and the name's demangled form (if any). */
16428 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
16429 linkagename = dwarf2_physname (name, die, cu);
16430 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
16432 /* Fortran does not have mangling standard and the mangling does differ
16433 between gfortran, iFort etc. */
16434 if (cu->language == language_fortran
16435 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
16436 symbol_set_demangled_name (&(sym->ginfo),
16437 dwarf2_full_name (name, die, cu),
16440 /* Default assumptions.
16441 Use the passed type or decode it from the die. */
16442 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16443 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16445 SYMBOL_TYPE (sym) = type;
16447 SYMBOL_TYPE (sym) = die_type (die, cu);
16448 attr = dwarf2_attr (die,
16449 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16453 SYMBOL_LINE (sym) = DW_UNSND (attr);
16456 attr = dwarf2_attr (die,
16457 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16461 int file_index = DW_UNSND (attr);
16463 if (cu->line_header == NULL
16464 || file_index > cu->line_header->num_file_names)
16465 complaint (&symfile_complaints,
16466 _("file index out of range"));
16467 else if (file_index > 0)
16469 struct file_entry *fe;
16471 fe = &cu->line_header->file_names[file_index - 1];
16472 SYMBOL_SYMTAB (sym) = fe->symtab;
16479 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16482 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16484 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16485 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16486 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16487 add_symbol_to_list (sym, cu->list_in_scope);
16489 case DW_TAG_subprogram:
16490 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16492 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16493 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16494 if ((attr2 && (DW_UNSND (attr2) != 0))
16495 || cu->language == language_ada)
16497 /* Subprograms marked external are stored as a global symbol.
16498 Ada subprograms, whether marked external or not, are always
16499 stored as a global symbol, because we want to be able to
16500 access them globally. For instance, we want to be able
16501 to break on a nested subprogram without having to
16502 specify the context. */
16503 list_to_add = &global_symbols;
16507 list_to_add = cu->list_in_scope;
16510 case DW_TAG_inlined_subroutine:
16511 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16513 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16514 SYMBOL_INLINED (sym) = 1;
16515 list_to_add = cu->list_in_scope;
16517 case DW_TAG_template_value_param:
16519 /* Fall through. */
16520 case DW_TAG_constant:
16521 case DW_TAG_variable:
16522 case DW_TAG_member:
16523 /* Compilation with minimal debug info may result in
16524 variables with missing type entries. Change the
16525 misleading `void' type to something sensible. */
16526 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16528 = objfile_type (objfile)->nodebug_data_symbol;
16530 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16531 /* In the case of DW_TAG_member, we should only be called for
16532 static const members. */
16533 if (die->tag == DW_TAG_member)
16535 /* dwarf2_add_field uses die_is_declaration,
16536 so we do the same. */
16537 gdb_assert (die_is_declaration (die, cu));
16542 dwarf2_const_value (attr, sym, cu);
16543 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16546 if (attr2 && (DW_UNSND (attr2) != 0))
16547 list_to_add = &global_symbols;
16549 list_to_add = cu->list_in_scope;
16553 attr = dwarf2_attr (die, DW_AT_location, cu);
16556 var_decode_location (attr, sym, cu);
16557 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16559 /* Fortran explicitly imports any global symbols to the local
16560 scope by DW_TAG_common_block. */
16561 if (cu->language == language_fortran && die->parent
16562 && die->parent->tag == DW_TAG_common_block)
16565 if (SYMBOL_CLASS (sym) == LOC_STATIC
16566 && SYMBOL_VALUE_ADDRESS (sym) == 0
16567 && !dwarf2_per_objfile->has_section_at_zero)
16569 /* When a static variable is eliminated by the linker,
16570 the corresponding debug information is not stripped
16571 out, but the variable address is set to null;
16572 do not add such variables into symbol table. */
16574 else if (attr2 && (DW_UNSND (attr2) != 0))
16576 /* Workaround gfortran PR debug/40040 - it uses
16577 DW_AT_location for variables in -fPIC libraries which may
16578 get overriden by other libraries/executable and get
16579 a different address. Resolve it by the minimal symbol
16580 which may come from inferior's executable using copy
16581 relocation. Make this workaround only for gfortran as for
16582 other compilers GDB cannot guess the minimal symbol
16583 Fortran mangling kind. */
16584 if (cu->language == language_fortran && die->parent
16585 && die->parent->tag == DW_TAG_module
16587 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16588 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16590 /* A variable with DW_AT_external is never static,
16591 but it may be block-scoped. */
16592 list_to_add = (cu->list_in_scope == &file_symbols
16593 ? &global_symbols : cu->list_in_scope);
16596 list_to_add = cu->list_in_scope;
16600 /* We do not know the address of this symbol.
16601 If it is an external symbol and we have type information
16602 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16603 The address of the variable will then be determined from
16604 the minimal symbol table whenever the variable is
16606 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16608 /* Fortran explicitly imports any global symbols to the local
16609 scope by DW_TAG_common_block. */
16610 if (cu->language == language_fortran && die->parent
16611 && die->parent->tag == DW_TAG_common_block)
16613 /* SYMBOL_CLASS doesn't matter here because
16614 read_common_block is going to reset it. */
16616 list_to_add = cu->list_in_scope;
16618 else if (attr2 && (DW_UNSND (attr2) != 0)
16619 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16621 /* A variable with DW_AT_external is never static, but it
16622 may be block-scoped. */
16623 list_to_add = (cu->list_in_scope == &file_symbols
16624 ? &global_symbols : cu->list_in_scope);
16626 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16628 else if (!die_is_declaration (die, cu))
16630 /* Use the default LOC_OPTIMIZED_OUT class. */
16631 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16633 list_to_add = cu->list_in_scope;
16637 case DW_TAG_formal_parameter:
16638 /* If we are inside a function, mark this as an argument. If
16639 not, we might be looking at an argument to an inlined function
16640 when we do not have enough information to show inlined frames;
16641 pretend it's a local variable in that case so that the user can
16643 if (context_stack_depth > 0
16644 && context_stack[context_stack_depth - 1].name != NULL)
16645 SYMBOL_IS_ARGUMENT (sym) = 1;
16646 attr = dwarf2_attr (die, DW_AT_location, cu);
16649 var_decode_location (attr, sym, cu);
16651 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16654 dwarf2_const_value (attr, sym, cu);
16657 list_to_add = cu->list_in_scope;
16659 case DW_TAG_unspecified_parameters:
16660 /* From varargs functions; gdb doesn't seem to have any
16661 interest in this information, so just ignore it for now.
16664 case DW_TAG_template_type_param:
16666 /* Fall through. */
16667 case DW_TAG_class_type:
16668 case DW_TAG_interface_type:
16669 case DW_TAG_structure_type:
16670 case DW_TAG_union_type:
16671 case DW_TAG_set_type:
16672 case DW_TAG_enumeration_type:
16673 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16674 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16677 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16678 really ever be static objects: otherwise, if you try
16679 to, say, break of a class's method and you're in a file
16680 which doesn't mention that class, it won't work unless
16681 the check for all static symbols in lookup_symbol_aux
16682 saves you. See the OtherFileClass tests in
16683 gdb.c++/namespace.exp. */
16687 list_to_add = (cu->list_in_scope == &file_symbols
16688 && (cu->language == language_cplus
16689 || cu->language == language_java)
16690 ? &global_symbols : cu->list_in_scope);
16692 /* The semantics of C++ state that "struct foo {
16693 ... }" also defines a typedef for "foo". A Java
16694 class declaration also defines a typedef for the
16696 if (cu->language == language_cplus
16697 || cu->language == language_java
16698 || cu->language == language_ada)
16700 /* The symbol's name is already allocated along
16701 with this objfile, so we don't need to
16702 duplicate it for the type. */
16703 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16704 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16709 case DW_TAG_typedef:
16710 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16711 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16712 list_to_add = cu->list_in_scope;
16714 case DW_TAG_base_type:
16715 case DW_TAG_subrange_type:
16716 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16717 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16718 list_to_add = cu->list_in_scope;
16720 case DW_TAG_enumerator:
16721 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16724 dwarf2_const_value (attr, sym, cu);
16727 /* NOTE: carlton/2003-11-10: See comment above in the
16728 DW_TAG_class_type, etc. block. */
16730 list_to_add = (cu->list_in_scope == &file_symbols
16731 && (cu->language == language_cplus
16732 || cu->language == language_java)
16733 ? &global_symbols : cu->list_in_scope);
16736 case DW_TAG_namespace:
16737 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16738 list_to_add = &global_symbols;
16740 case DW_TAG_common_block:
16741 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16742 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16743 add_symbol_to_list (sym, cu->list_in_scope);
16746 /* Not a tag we recognize. Hopefully we aren't processing
16747 trash data, but since we must specifically ignore things
16748 we don't recognize, there is nothing else we should do at
16750 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16751 dwarf_tag_name (die->tag));
16757 sym->hash_next = objfile->template_symbols;
16758 objfile->template_symbols = sym;
16759 list_to_add = NULL;
16762 if (list_to_add != NULL)
16763 add_symbol_to_list (sym, list_to_add);
16765 /* For the benefit of old versions of GCC, check for anonymous
16766 namespaces based on the demangled name. */
16767 if (!cu->processing_has_namespace_info
16768 && cu->language == language_cplus)
16769 cp_scan_for_anonymous_namespaces (sym, objfile);
16774 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16776 static struct symbol *
16777 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16779 return new_symbol_full (die, type, cu, NULL);
16782 /* Given an attr with a DW_FORM_dataN value in host byte order,
16783 zero-extend it as appropriate for the symbol's type. The DWARF
16784 standard (v4) is not entirely clear about the meaning of using
16785 DW_FORM_dataN for a constant with a signed type, where the type is
16786 wider than the data. The conclusion of a discussion on the DWARF
16787 list was that this is unspecified. We choose to always zero-extend
16788 because that is the interpretation long in use by GCC. */
16791 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
16792 struct dwarf2_cu *cu, LONGEST *value, int bits)
16794 struct objfile *objfile = cu->objfile;
16795 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16796 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16797 LONGEST l = DW_UNSND (attr);
16799 if (bits < sizeof (*value) * 8)
16801 l &= ((LONGEST) 1 << bits) - 1;
16804 else if (bits == sizeof (*value) * 8)
16808 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16809 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16816 /* Read a constant value from an attribute. Either set *VALUE, or if
16817 the value does not fit in *VALUE, set *BYTES - either already
16818 allocated on the objfile obstack, or newly allocated on OBSTACK,
16819 or, set *BATON, if we translated the constant to a location
16823 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
16824 const char *name, struct obstack *obstack,
16825 struct dwarf2_cu *cu,
16826 LONGEST *value, const gdb_byte **bytes,
16827 struct dwarf2_locexpr_baton **baton)
16829 struct objfile *objfile = cu->objfile;
16830 struct comp_unit_head *cu_header = &cu->header;
16831 struct dwarf_block *blk;
16832 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16833 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16839 switch (attr->form)
16842 case DW_FORM_GNU_addr_index:
16846 if (TYPE_LENGTH (type) != cu_header->addr_size)
16847 dwarf2_const_value_length_mismatch_complaint (name,
16848 cu_header->addr_size,
16849 TYPE_LENGTH (type));
16850 /* Symbols of this form are reasonably rare, so we just
16851 piggyback on the existing location code rather than writing
16852 a new implementation of symbol_computed_ops. */
16853 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
16854 (*baton)->per_cu = cu->per_cu;
16855 gdb_assert ((*baton)->per_cu);
16857 (*baton)->size = 2 + cu_header->addr_size;
16858 data = obstack_alloc (obstack, (*baton)->size);
16859 (*baton)->data = data;
16861 data[0] = DW_OP_addr;
16862 store_unsigned_integer (&data[1], cu_header->addr_size,
16863 byte_order, DW_ADDR (attr));
16864 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16867 case DW_FORM_string:
16869 case DW_FORM_GNU_str_index:
16870 case DW_FORM_GNU_strp_alt:
16871 /* DW_STRING is already allocated on the objfile obstack, point
16873 *bytes = (const gdb_byte *) DW_STRING (attr);
16875 case DW_FORM_block1:
16876 case DW_FORM_block2:
16877 case DW_FORM_block4:
16878 case DW_FORM_block:
16879 case DW_FORM_exprloc:
16880 blk = DW_BLOCK (attr);
16881 if (TYPE_LENGTH (type) != blk->size)
16882 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16883 TYPE_LENGTH (type));
16884 *bytes = blk->data;
16887 /* The DW_AT_const_value attributes are supposed to carry the
16888 symbol's value "represented as it would be on the target
16889 architecture." By the time we get here, it's already been
16890 converted to host endianness, so we just need to sign- or
16891 zero-extend it as appropriate. */
16892 case DW_FORM_data1:
16893 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
16895 case DW_FORM_data2:
16896 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
16898 case DW_FORM_data4:
16899 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
16901 case DW_FORM_data8:
16902 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
16905 case DW_FORM_sdata:
16906 *value = DW_SND (attr);
16909 case DW_FORM_udata:
16910 *value = DW_UNSND (attr);
16914 complaint (&symfile_complaints,
16915 _("unsupported const value attribute form: '%s'"),
16916 dwarf_form_name (attr->form));
16923 /* Copy constant value from an attribute to a symbol. */
16926 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
16927 struct dwarf2_cu *cu)
16929 struct objfile *objfile = cu->objfile;
16930 struct comp_unit_head *cu_header = &cu->header;
16932 const gdb_byte *bytes;
16933 struct dwarf2_locexpr_baton *baton;
16935 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16936 SYMBOL_PRINT_NAME (sym),
16937 &objfile->objfile_obstack, cu,
16938 &value, &bytes, &baton);
16942 SYMBOL_LOCATION_BATON (sym) = baton;
16943 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16945 else if (bytes != NULL)
16947 SYMBOL_VALUE_BYTES (sym) = bytes;
16948 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16952 SYMBOL_VALUE (sym) = value;
16953 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16957 /* Return the type of the die in question using its DW_AT_type attribute. */
16959 static struct type *
16960 die_type (struct die_info *die, struct dwarf2_cu *cu)
16962 struct attribute *type_attr;
16964 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16967 /* A missing DW_AT_type represents a void type. */
16968 return objfile_type (cu->objfile)->builtin_void;
16971 return lookup_die_type (die, type_attr, cu);
16974 /* True iff CU's producer generates GNAT Ada auxiliary information
16975 that allows to find parallel types through that information instead
16976 of having to do expensive parallel lookups by type name. */
16979 need_gnat_info (struct dwarf2_cu *cu)
16981 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16982 of GNAT produces this auxiliary information, without any indication
16983 that it is produced. Part of enhancing the FSF version of GNAT
16984 to produce that information will be to put in place an indicator
16985 that we can use in order to determine whether the descriptive type
16986 info is available or not. One suggestion that has been made is
16987 to use a new attribute, attached to the CU die. For now, assume
16988 that the descriptive type info is not available. */
16992 /* Return the auxiliary type of the die in question using its
16993 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16994 attribute is not present. */
16996 static struct type *
16997 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16999 struct attribute *type_attr;
17001 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17005 return lookup_die_type (die, type_attr, cu);
17008 /* If DIE has a descriptive_type attribute, then set the TYPE's
17009 descriptive type accordingly. */
17012 set_descriptive_type (struct type *type, struct die_info *die,
17013 struct dwarf2_cu *cu)
17015 struct type *descriptive_type = die_descriptive_type (die, cu);
17017 if (descriptive_type)
17019 ALLOCATE_GNAT_AUX_TYPE (type);
17020 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17024 /* Return the containing type of the die in question using its
17025 DW_AT_containing_type attribute. */
17027 static struct type *
17028 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
17030 struct attribute *type_attr;
17032 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
17034 error (_("Dwarf Error: Problem turning containing type into gdb type "
17035 "[in module %s]"), cu->objfile->name);
17037 return lookup_die_type (die, type_attr, cu);
17040 /* Return an error marker type to use for the ill formed type in DIE/CU. */
17042 static struct type *
17043 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17045 struct objfile *objfile = dwarf2_per_objfile->objfile;
17046 char *message, *saved;
17048 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17050 cu->header.offset.sect_off,
17051 die->offset.sect_off);
17052 saved = obstack_copy0 (&objfile->objfile_obstack,
17053 message, strlen (message));
17056 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17059 /* Look up the type of DIE in CU using its type attribute ATTR.
17060 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17061 DW_AT_containing_type.
17062 If there is no type substitute an error marker. */
17064 static struct type *
17065 lookup_die_type (struct die_info *die, const struct attribute *attr,
17066 struct dwarf2_cu *cu)
17068 struct objfile *objfile = cu->objfile;
17069 struct type *this_type;
17071 gdb_assert (attr->name == DW_AT_type
17072 || attr->name == DW_AT_GNAT_descriptive_type
17073 || attr->name == DW_AT_containing_type);
17075 /* First see if we have it cached. */
17077 if (attr->form == DW_FORM_GNU_ref_alt)
17079 struct dwarf2_per_cu_data *per_cu;
17080 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17082 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17083 this_type = get_die_type_at_offset (offset, per_cu);
17085 else if (attr_form_is_ref (attr))
17087 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17089 this_type = get_die_type_at_offset (offset, cu->per_cu);
17091 else if (attr->form == DW_FORM_ref_sig8)
17093 ULONGEST signature = DW_SIGNATURE (attr);
17095 return get_signatured_type (die, signature, cu);
17099 complaint (&symfile_complaints,
17100 _("Dwarf Error: Bad type attribute %s in DIE"
17101 " at 0x%x [in module %s]"),
17102 dwarf_attr_name (attr->name), die->offset.sect_off,
17104 return build_error_marker_type (cu, die);
17107 /* If not cached we need to read it in. */
17109 if (this_type == NULL)
17111 struct die_info *type_die = NULL;
17112 struct dwarf2_cu *type_cu = cu;
17114 if (attr_form_is_ref (attr))
17115 type_die = follow_die_ref (die, attr, &type_cu);
17116 if (type_die == NULL)
17117 return build_error_marker_type (cu, die);
17118 /* If we find the type now, it's probably because the type came
17119 from an inter-CU reference and the type's CU got expanded before
17121 this_type = read_type_die (type_die, type_cu);
17124 /* If we still don't have a type use an error marker. */
17126 if (this_type == NULL)
17127 return build_error_marker_type (cu, die);
17132 /* Return the type in DIE, CU.
17133 Returns NULL for invalid types.
17135 This first does a lookup in die_type_hash,
17136 and only reads the die in if necessary.
17138 NOTE: This can be called when reading in partial or full symbols. */
17140 static struct type *
17141 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17143 struct type *this_type;
17145 this_type = get_die_type (die, cu);
17149 return read_type_die_1 (die, cu);
17152 /* Read the type in DIE, CU.
17153 Returns NULL for invalid types. */
17155 static struct type *
17156 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17158 struct type *this_type = NULL;
17162 case DW_TAG_class_type:
17163 case DW_TAG_interface_type:
17164 case DW_TAG_structure_type:
17165 case DW_TAG_union_type:
17166 this_type = read_structure_type (die, cu);
17168 case DW_TAG_enumeration_type:
17169 this_type = read_enumeration_type (die, cu);
17171 case DW_TAG_subprogram:
17172 case DW_TAG_subroutine_type:
17173 case DW_TAG_inlined_subroutine:
17174 this_type = read_subroutine_type (die, cu);
17176 case DW_TAG_array_type:
17177 this_type = read_array_type (die, cu);
17179 case DW_TAG_set_type:
17180 this_type = read_set_type (die, cu);
17182 case DW_TAG_pointer_type:
17183 this_type = read_tag_pointer_type (die, cu);
17185 case DW_TAG_ptr_to_member_type:
17186 this_type = read_tag_ptr_to_member_type (die, cu);
17188 case DW_TAG_reference_type:
17189 this_type = read_tag_reference_type (die, cu);
17191 case DW_TAG_const_type:
17192 this_type = read_tag_const_type (die, cu);
17194 case DW_TAG_volatile_type:
17195 this_type = read_tag_volatile_type (die, cu);
17197 case DW_TAG_restrict_type:
17198 this_type = read_tag_restrict_type (die, cu);
17200 case DW_TAG_string_type:
17201 this_type = read_tag_string_type (die, cu);
17203 case DW_TAG_typedef:
17204 this_type = read_typedef (die, cu);
17206 case DW_TAG_subrange_type:
17207 this_type = read_subrange_type (die, cu);
17209 case DW_TAG_base_type:
17210 this_type = read_base_type (die, cu);
17212 case DW_TAG_unspecified_type:
17213 this_type = read_unspecified_type (die, cu);
17215 case DW_TAG_namespace:
17216 this_type = read_namespace_type (die, cu);
17218 case DW_TAG_module:
17219 this_type = read_module_type (die, cu);
17222 complaint (&symfile_complaints,
17223 _("unexpected tag in read_type_die: '%s'"),
17224 dwarf_tag_name (die->tag));
17231 /* See if we can figure out if the class lives in a namespace. We do
17232 this by looking for a member function; its demangled name will
17233 contain namespace info, if there is any.
17234 Return the computed name or NULL.
17235 Space for the result is allocated on the objfile's obstack.
17236 This is the full-die version of guess_partial_die_structure_name.
17237 In this case we know DIE has no useful parent. */
17240 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17242 struct die_info *spec_die;
17243 struct dwarf2_cu *spec_cu;
17244 struct die_info *child;
17247 spec_die = die_specification (die, &spec_cu);
17248 if (spec_die != NULL)
17254 for (child = die->child;
17256 child = child->sibling)
17258 if (child->tag == DW_TAG_subprogram)
17260 struct attribute *attr;
17262 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17264 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17268 = language_class_name_from_physname (cu->language_defn,
17272 if (actual_name != NULL)
17274 const char *die_name = dwarf2_name (die, cu);
17276 if (die_name != NULL
17277 && strcmp (die_name, actual_name) != 0)
17279 /* Strip off the class name from the full name.
17280 We want the prefix. */
17281 int die_name_len = strlen (die_name);
17282 int actual_name_len = strlen (actual_name);
17284 /* Test for '::' as a sanity check. */
17285 if (actual_name_len > die_name_len + 2
17286 && actual_name[actual_name_len
17287 - die_name_len - 1] == ':')
17289 obstack_copy0 (&cu->objfile->objfile_obstack,
17291 actual_name_len - die_name_len - 2);
17294 xfree (actual_name);
17303 /* GCC might emit a nameless typedef that has a linkage name. Determine the
17304 prefix part in such case. See
17305 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17308 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17310 struct attribute *attr;
17313 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17314 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17317 attr = dwarf2_attr (die, DW_AT_name, cu);
17318 if (attr != NULL && DW_STRING (attr) != NULL)
17321 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17323 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17324 if (attr == NULL || DW_STRING (attr) == NULL)
17327 /* dwarf2_name had to be already called. */
17328 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17330 /* Strip the base name, keep any leading namespaces/classes. */
17331 base = strrchr (DW_STRING (attr), ':');
17332 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17335 return obstack_copy0 (&cu->objfile->objfile_obstack,
17336 DW_STRING (attr), &base[-1] - DW_STRING (attr));
17339 /* Return the name of the namespace/class that DIE is defined within,
17340 or "" if we can't tell. The caller should not xfree the result.
17342 For example, if we're within the method foo() in the following
17352 then determine_prefix on foo's die will return "N::C". */
17354 static const char *
17355 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
17357 struct die_info *parent, *spec_die;
17358 struct dwarf2_cu *spec_cu;
17359 struct type *parent_type;
17362 if (cu->language != language_cplus && cu->language != language_java
17363 && cu->language != language_fortran)
17366 retval = anonymous_struct_prefix (die, cu);
17370 /* We have to be careful in the presence of DW_AT_specification.
17371 For example, with GCC 3.4, given the code
17375 // Definition of N::foo.
17379 then we'll have a tree of DIEs like this:
17381 1: DW_TAG_compile_unit
17382 2: DW_TAG_namespace // N
17383 3: DW_TAG_subprogram // declaration of N::foo
17384 4: DW_TAG_subprogram // definition of N::foo
17385 DW_AT_specification // refers to die #3
17387 Thus, when processing die #4, we have to pretend that we're in
17388 the context of its DW_AT_specification, namely the contex of die
17391 spec_die = die_specification (die, &spec_cu);
17392 if (spec_die == NULL)
17393 parent = die->parent;
17396 parent = spec_die->parent;
17400 if (parent == NULL)
17402 else if (parent->building_fullname)
17405 const char *parent_name;
17407 /* It has been seen on RealView 2.2 built binaries,
17408 DW_TAG_template_type_param types actually _defined_ as
17409 children of the parent class:
17412 template class <class Enum> Class{};
17413 Class<enum E> class_e;
17415 1: DW_TAG_class_type (Class)
17416 2: DW_TAG_enumeration_type (E)
17417 3: DW_TAG_enumerator (enum1:0)
17418 3: DW_TAG_enumerator (enum2:1)
17420 2: DW_TAG_template_type_param
17421 DW_AT_type DW_FORM_ref_udata (E)
17423 Besides being broken debug info, it can put GDB into an
17424 infinite loop. Consider:
17426 When we're building the full name for Class<E>, we'll start
17427 at Class, and go look over its template type parameters,
17428 finding E. We'll then try to build the full name of E, and
17429 reach here. We're now trying to build the full name of E,
17430 and look over the parent DIE for containing scope. In the
17431 broken case, if we followed the parent DIE of E, we'd again
17432 find Class, and once again go look at its template type
17433 arguments, etc., etc. Simply don't consider such parent die
17434 as source-level parent of this die (it can't be, the language
17435 doesn't allow it), and break the loop here. */
17436 name = dwarf2_name (die, cu);
17437 parent_name = dwarf2_name (parent, cu);
17438 complaint (&symfile_complaints,
17439 _("template param type '%s' defined within parent '%s'"),
17440 name ? name : "<unknown>",
17441 parent_name ? parent_name : "<unknown>");
17445 switch (parent->tag)
17447 case DW_TAG_namespace:
17448 parent_type = read_type_die (parent, cu);
17449 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17450 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17451 Work around this problem here. */
17452 if (cu->language == language_cplus
17453 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17455 /* We give a name to even anonymous namespaces. */
17456 return TYPE_TAG_NAME (parent_type);
17457 case DW_TAG_class_type:
17458 case DW_TAG_interface_type:
17459 case DW_TAG_structure_type:
17460 case DW_TAG_union_type:
17461 case DW_TAG_module:
17462 parent_type = read_type_die (parent, cu);
17463 if (TYPE_TAG_NAME (parent_type) != NULL)
17464 return TYPE_TAG_NAME (parent_type);
17466 /* An anonymous structure is only allowed non-static data
17467 members; no typedefs, no member functions, et cetera.
17468 So it does not need a prefix. */
17470 case DW_TAG_compile_unit:
17471 case DW_TAG_partial_unit:
17472 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17473 if (cu->language == language_cplus
17474 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17475 && die->child != NULL
17476 && (die->tag == DW_TAG_class_type
17477 || die->tag == DW_TAG_structure_type
17478 || die->tag == DW_TAG_union_type))
17480 char *name = guess_full_die_structure_name (die, cu);
17486 return determine_prefix (parent, cu);
17490 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17491 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17492 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17493 an obconcat, otherwise allocate storage for the result. The CU argument is
17494 used to determine the language and hence, the appropriate separator. */
17496 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17499 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17500 int physname, struct dwarf2_cu *cu)
17502 const char *lead = "";
17505 if (suffix == NULL || suffix[0] == '\0'
17506 || prefix == NULL || prefix[0] == '\0')
17508 else if (cu->language == language_java)
17510 else if (cu->language == language_fortran && physname)
17512 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17513 DW_AT_MIPS_linkage_name is preferred and used instead. */
17521 if (prefix == NULL)
17523 if (suffix == NULL)
17529 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17531 strcpy (retval, lead);
17532 strcat (retval, prefix);
17533 strcat (retval, sep);
17534 strcat (retval, suffix);
17539 /* We have an obstack. */
17540 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17544 /* Return sibling of die, NULL if no sibling. */
17546 static struct die_info *
17547 sibling_die (struct die_info *die)
17549 return die->sibling;
17552 /* Get name of a die, return NULL if not found. */
17554 static const char *
17555 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17556 struct obstack *obstack)
17558 if (name && cu->language == language_cplus)
17560 char *canon_name = cp_canonicalize_string (name);
17562 if (canon_name != NULL)
17564 if (strcmp (canon_name, name) != 0)
17565 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17566 xfree (canon_name);
17573 /* Get name of a die, return NULL if not found. */
17575 static const char *
17576 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17578 struct attribute *attr;
17580 attr = dwarf2_attr (die, DW_AT_name, cu);
17581 if ((!attr || !DW_STRING (attr))
17582 && die->tag != DW_TAG_class_type
17583 && die->tag != DW_TAG_interface_type
17584 && die->tag != DW_TAG_structure_type
17585 && die->tag != DW_TAG_union_type)
17590 case DW_TAG_compile_unit:
17591 case DW_TAG_partial_unit:
17592 /* Compilation units have a DW_AT_name that is a filename, not
17593 a source language identifier. */
17594 case DW_TAG_enumeration_type:
17595 case DW_TAG_enumerator:
17596 /* These tags always have simple identifiers already; no need
17597 to canonicalize them. */
17598 return DW_STRING (attr);
17600 case DW_TAG_subprogram:
17601 /* Java constructors will all be named "<init>", so return
17602 the class name when we see this special case. */
17603 if (cu->language == language_java
17604 && DW_STRING (attr) != NULL
17605 && strcmp (DW_STRING (attr), "<init>") == 0)
17607 struct dwarf2_cu *spec_cu = cu;
17608 struct die_info *spec_die;
17610 /* GCJ will output '<init>' for Java constructor names.
17611 For this special case, return the name of the parent class. */
17613 /* GCJ may output suprogram DIEs with AT_specification set.
17614 If so, use the name of the specified DIE. */
17615 spec_die = die_specification (die, &spec_cu);
17616 if (spec_die != NULL)
17617 return dwarf2_name (spec_die, spec_cu);
17622 if (die->tag == DW_TAG_class_type)
17623 return dwarf2_name (die, cu);
17625 while (die->tag != DW_TAG_compile_unit
17626 && die->tag != DW_TAG_partial_unit);
17630 case DW_TAG_class_type:
17631 case DW_TAG_interface_type:
17632 case DW_TAG_structure_type:
17633 case DW_TAG_union_type:
17634 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17635 structures or unions. These were of the form "._%d" in GCC 4.1,
17636 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17637 and GCC 4.4. We work around this problem by ignoring these. */
17638 if (attr && DW_STRING (attr)
17639 && (strncmp (DW_STRING (attr), "._", 2) == 0
17640 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17643 /* GCC might emit a nameless typedef that has a linkage name. See
17644 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17645 if (!attr || DW_STRING (attr) == NULL)
17647 char *demangled = NULL;
17649 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17651 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17653 if (attr == NULL || DW_STRING (attr) == NULL)
17656 /* Avoid demangling DW_STRING (attr) the second time on a second
17657 call for the same DIE. */
17658 if (!DW_STRING_IS_CANONICAL (attr))
17659 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
17665 /* FIXME: we already did this for the partial symbol... */
17666 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17667 demangled, strlen (demangled));
17668 DW_STRING_IS_CANONICAL (attr) = 1;
17671 /* Strip any leading namespaces/classes, keep only the base name.
17672 DW_AT_name for named DIEs does not contain the prefixes. */
17673 base = strrchr (DW_STRING (attr), ':');
17674 if (base && base > DW_STRING (attr) && base[-1] == ':')
17677 return DW_STRING (attr);
17686 if (!DW_STRING_IS_CANONICAL (attr))
17689 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17690 &cu->objfile->objfile_obstack);
17691 DW_STRING_IS_CANONICAL (attr) = 1;
17693 return DW_STRING (attr);
17696 /* Return the die that this die in an extension of, or NULL if there
17697 is none. *EXT_CU is the CU containing DIE on input, and the CU
17698 containing the return value on output. */
17700 static struct die_info *
17701 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17703 struct attribute *attr;
17705 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17709 return follow_die_ref (die, attr, ext_cu);
17712 /* Convert a DIE tag into its string name. */
17714 static const char *
17715 dwarf_tag_name (unsigned tag)
17717 const char *name = get_DW_TAG_name (tag);
17720 return "DW_TAG_<unknown>";
17725 /* Convert a DWARF attribute code into its string name. */
17727 static const char *
17728 dwarf_attr_name (unsigned attr)
17732 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17733 if (attr == DW_AT_MIPS_fde)
17734 return "DW_AT_MIPS_fde";
17736 if (attr == DW_AT_HP_block_index)
17737 return "DW_AT_HP_block_index";
17740 name = get_DW_AT_name (attr);
17743 return "DW_AT_<unknown>";
17748 /* Convert a DWARF value form code into its string name. */
17750 static const char *
17751 dwarf_form_name (unsigned form)
17753 const char *name = get_DW_FORM_name (form);
17756 return "DW_FORM_<unknown>";
17762 dwarf_bool_name (unsigned mybool)
17770 /* Convert a DWARF type code into its string name. */
17772 static const char *
17773 dwarf_type_encoding_name (unsigned enc)
17775 const char *name = get_DW_ATE_name (enc);
17778 return "DW_ATE_<unknown>";
17784 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17788 print_spaces (indent, f);
17789 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17790 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17792 if (die->parent != NULL)
17794 print_spaces (indent, f);
17795 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17796 die->parent->offset.sect_off);
17799 print_spaces (indent, f);
17800 fprintf_unfiltered (f, " has children: %s\n",
17801 dwarf_bool_name (die->child != NULL));
17803 print_spaces (indent, f);
17804 fprintf_unfiltered (f, " attributes:\n");
17806 for (i = 0; i < die->num_attrs; ++i)
17808 print_spaces (indent, f);
17809 fprintf_unfiltered (f, " %s (%s) ",
17810 dwarf_attr_name (die->attrs[i].name),
17811 dwarf_form_name (die->attrs[i].form));
17813 switch (die->attrs[i].form)
17816 case DW_FORM_GNU_addr_index:
17817 fprintf_unfiltered (f, "address: ");
17818 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17820 case DW_FORM_block2:
17821 case DW_FORM_block4:
17822 case DW_FORM_block:
17823 case DW_FORM_block1:
17824 fprintf_unfiltered (f, "block: size %s",
17825 pulongest (DW_BLOCK (&die->attrs[i])->size));
17827 case DW_FORM_exprloc:
17828 fprintf_unfiltered (f, "expression: size %s",
17829 pulongest (DW_BLOCK (&die->attrs[i])->size));
17831 case DW_FORM_ref_addr:
17832 fprintf_unfiltered (f, "ref address: ");
17833 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17835 case DW_FORM_GNU_ref_alt:
17836 fprintf_unfiltered (f, "alt ref address: ");
17837 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17843 case DW_FORM_ref_udata:
17844 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17845 (long) (DW_UNSND (&die->attrs[i])));
17847 case DW_FORM_data1:
17848 case DW_FORM_data2:
17849 case DW_FORM_data4:
17850 case DW_FORM_data8:
17851 case DW_FORM_udata:
17852 case DW_FORM_sdata:
17853 fprintf_unfiltered (f, "constant: %s",
17854 pulongest (DW_UNSND (&die->attrs[i])));
17856 case DW_FORM_sec_offset:
17857 fprintf_unfiltered (f, "section offset: %s",
17858 pulongest (DW_UNSND (&die->attrs[i])));
17860 case DW_FORM_ref_sig8:
17861 fprintf_unfiltered (f, "signature: %s",
17862 hex_string (DW_SIGNATURE (&die->attrs[i])));
17864 case DW_FORM_string:
17866 case DW_FORM_GNU_str_index:
17867 case DW_FORM_GNU_strp_alt:
17868 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17869 DW_STRING (&die->attrs[i])
17870 ? DW_STRING (&die->attrs[i]) : "",
17871 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17874 if (DW_UNSND (&die->attrs[i]))
17875 fprintf_unfiltered (f, "flag: TRUE");
17877 fprintf_unfiltered (f, "flag: FALSE");
17879 case DW_FORM_flag_present:
17880 fprintf_unfiltered (f, "flag: TRUE");
17882 case DW_FORM_indirect:
17883 /* The reader will have reduced the indirect form to
17884 the "base form" so this form should not occur. */
17885 fprintf_unfiltered (f,
17886 "unexpected attribute form: DW_FORM_indirect");
17889 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17890 die->attrs[i].form);
17893 fprintf_unfiltered (f, "\n");
17898 dump_die_for_error (struct die_info *die)
17900 dump_die_shallow (gdb_stderr, 0, die);
17904 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17906 int indent = level * 4;
17908 gdb_assert (die != NULL);
17910 if (level >= max_level)
17913 dump_die_shallow (f, indent, die);
17915 if (die->child != NULL)
17917 print_spaces (indent, f);
17918 fprintf_unfiltered (f, " Children:");
17919 if (level + 1 < max_level)
17921 fprintf_unfiltered (f, "\n");
17922 dump_die_1 (f, level + 1, max_level, die->child);
17926 fprintf_unfiltered (f,
17927 " [not printed, max nesting level reached]\n");
17931 if (die->sibling != NULL && level > 0)
17933 dump_die_1 (f, level, max_level, die->sibling);
17937 /* This is called from the pdie macro in gdbinit.in.
17938 It's not static so gcc will keep a copy callable from gdb. */
17941 dump_die (struct die_info *die, int max_level)
17943 dump_die_1 (gdb_stdlog, 0, max_level, die);
17947 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17951 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17957 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17961 dwarf2_get_ref_die_offset (const struct attribute *attr)
17963 sect_offset retval = { DW_UNSND (attr) };
17965 if (attr_form_is_ref (attr))
17968 retval.sect_off = 0;
17969 complaint (&symfile_complaints,
17970 _("unsupported die ref attribute form: '%s'"),
17971 dwarf_form_name (attr->form));
17975 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17976 * the value held by the attribute is not constant. */
17979 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
17981 if (attr->form == DW_FORM_sdata)
17982 return DW_SND (attr);
17983 else if (attr->form == DW_FORM_udata
17984 || attr->form == DW_FORM_data1
17985 || attr->form == DW_FORM_data2
17986 || attr->form == DW_FORM_data4
17987 || attr->form == DW_FORM_data8)
17988 return DW_UNSND (attr);
17991 complaint (&symfile_complaints,
17992 _("Attribute value is not a constant (%s)"),
17993 dwarf_form_name (attr->form));
17994 return default_value;
17998 /* Follow reference or signature attribute ATTR of SRC_DIE.
17999 On entry *REF_CU is the CU of SRC_DIE.
18000 On exit *REF_CU is the CU of the result. */
18002 static struct die_info *
18003 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
18004 struct dwarf2_cu **ref_cu)
18006 struct die_info *die;
18008 if (attr_form_is_ref (attr))
18009 die = follow_die_ref (src_die, attr, ref_cu);
18010 else if (attr->form == DW_FORM_ref_sig8)
18011 die = follow_die_sig (src_die, attr, ref_cu);
18014 dump_die_for_error (src_die);
18015 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18016 (*ref_cu)->objfile->name);
18022 /* Follow reference OFFSET.
18023 On entry *REF_CU is the CU of the source die referencing OFFSET.
18024 On exit *REF_CU is the CU of the result.
18025 Returns NULL if OFFSET is invalid. */
18027 static struct die_info *
18028 follow_die_offset (sect_offset offset, int offset_in_dwz,
18029 struct dwarf2_cu **ref_cu)
18031 struct die_info temp_die;
18032 struct dwarf2_cu *target_cu, *cu = *ref_cu;
18034 gdb_assert (cu->per_cu != NULL);
18038 if (cu->per_cu->is_debug_types)
18040 /* .debug_types CUs cannot reference anything outside their CU.
18041 If they need to, they have to reference a signatured type via
18042 DW_FORM_ref_sig8. */
18043 if (! offset_in_cu_p (&cu->header, offset))
18046 else if (offset_in_dwz != cu->per_cu->is_dwz
18047 || ! offset_in_cu_p (&cu->header, offset))
18049 struct dwarf2_per_cu_data *per_cu;
18051 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18054 /* If necessary, add it to the queue and load its DIEs. */
18055 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18056 load_full_comp_unit (per_cu, cu->language);
18058 target_cu = per_cu->cu;
18060 else if (cu->dies == NULL)
18062 /* We're loading full DIEs during partial symbol reading. */
18063 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18064 load_full_comp_unit (cu->per_cu, language_minimal);
18067 *ref_cu = target_cu;
18068 temp_die.offset = offset;
18069 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18072 /* Follow reference attribute ATTR of SRC_DIE.
18073 On entry *REF_CU is the CU of SRC_DIE.
18074 On exit *REF_CU is the CU of the result. */
18076 static struct die_info *
18077 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
18078 struct dwarf2_cu **ref_cu)
18080 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18081 struct dwarf2_cu *cu = *ref_cu;
18082 struct die_info *die;
18084 die = follow_die_offset (offset,
18085 (attr->form == DW_FORM_GNU_ref_alt
18086 || cu->per_cu->is_dwz),
18089 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18090 "at 0x%x [in module %s]"),
18091 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
18096 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18097 Returned value is intended for DW_OP_call*. Returned
18098 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
18100 struct dwarf2_locexpr_baton
18101 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18102 struct dwarf2_per_cu_data *per_cu,
18103 CORE_ADDR (*get_frame_pc) (void *baton),
18106 struct dwarf2_cu *cu;
18107 struct die_info *die;
18108 struct attribute *attr;
18109 struct dwarf2_locexpr_baton retval;
18111 dw2_setup (per_cu->objfile);
18113 if (per_cu->cu == NULL)
18117 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18119 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18120 offset.sect_off, per_cu->objfile->name);
18122 attr = dwarf2_attr (die, DW_AT_location, cu);
18125 /* DWARF: "If there is no such attribute, then there is no effect.".
18126 DATA is ignored if SIZE is 0. */
18128 retval.data = NULL;
18131 else if (attr_form_is_section_offset (attr))
18133 struct dwarf2_loclist_baton loclist_baton;
18134 CORE_ADDR pc = (*get_frame_pc) (baton);
18137 fill_in_loclist_baton (cu, &loclist_baton, attr);
18139 retval.data = dwarf2_find_location_expression (&loclist_baton,
18141 retval.size = size;
18145 if (!attr_form_is_block (attr))
18146 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18147 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18148 offset.sect_off, per_cu->objfile->name);
18150 retval.data = DW_BLOCK (attr)->data;
18151 retval.size = DW_BLOCK (attr)->size;
18153 retval.per_cu = cu->per_cu;
18155 age_cached_comp_units ();
18160 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18163 struct dwarf2_locexpr_baton
18164 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18165 struct dwarf2_per_cu_data *per_cu,
18166 CORE_ADDR (*get_frame_pc) (void *baton),
18169 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18171 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18174 /* Write a constant of a given type as target-ordered bytes into
18177 static const gdb_byte *
18178 write_constant_as_bytes (struct obstack *obstack,
18179 enum bfd_endian byte_order,
18186 *len = TYPE_LENGTH (type);
18187 result = obstack_alloc (obstack, *len);
18188 store_unsigned_integer (result, *len, byte_order, value);
18193 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18194 pointer to the constant bytes and set LEN to the length of the
18195 data. If memory is needed, allocate it on OBSTACK. If the DIE
18196 does not have a DW_AT_const_value, return NULL. */
18199 dwarf2_fetch_constant_bytes (sect_offset offset,
18200 struct dwarf2_per_cu_data *per_cu,
18201 struct obstack *obstack,
18204 struct dwarf2_cu *cu;
18205 struct die_info *die;
18206 struct attribute *attr;
18207 const gdb_byte *result = NULL;
18210 enum bfd_endian byte_order;
18212 dw2_setup (per_cu->objfile);
18214 if (per_cu->cu == NULL)
18218 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18220 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18221 offset.sect_off, per_cu->objfile->name);
18224 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18228 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18229 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18231 switch (attr->form)
18234 case DW_FORM_GNU_addr_index:
18238 *len = cu->header.addr_size;
18239 tem = obstack_alloc (obstack, *len);
18240 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18244 case DW_FORM_string:
18246 case DW_FORM_GNU_str_index:
18247 case DW_FORM_GNU_strp_alt:
18248 /* DW_STRING is already allocated on the objfile obstack, point
18250 result = (const gdb_byte *) DW_STRING (attr);
18251 *len = strlen (DW_STRING (attr));
18253 case DW_FORM_block1:
18254 case DW_FORM_block2:
18255 case DW_FORM_block4:
18256 case DW_FORM_block:
18257 case DW_FORM_exprloc:
18258 result = DW_BLOCK (attr)->data;
18259 *len = DW_BLOCK (attr)->size;
18262 /* The DW_AT_const_value attributes are supposed to carry the
18263 symbol's value "represented as it would be on the target
18264 architecture." By the time we get here, it's already been
18265 converted to host endianness, so we just need to sign- or
18266 zero-extend it as appropriate. */
18267 case DW_FORM_data1:
18268 type = die_type (die, cu);
18269 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18270 if (result == NULL)
18271 result = write_constant_as_bytes (obstack, byte_order,
18274 case DW_FORM_data2:
18275 type = die_type (die, cu);
18276 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18277 if (result == NULL)
18278 result = write_constant_as_bytes (obstack, byte_order,
18281 case DW_FORM_data4:
18282 type = die_type (die, cu);
18283 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18284 if (result == NULL)
18285 result = write_constant_as_bytes (obstack, byte_order,
18288 case DW_FORM_data8:
18289 type = die_type (die, cu);
18290 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18291 if (result == NULL)
18292 result = write_constant_as_bytes (obstack, byte_order,
18296 case DW_FORM_sdata:
18297 type = die_type (die, cu);
18298 result = write_constant_as_bytes (obstack, byte_order,
18299 type, DW_SND (attr), len);
18302 case DW_FORM_udata:
18303 type = die_type (die, cu);
18304 result = write_constant_as_bytes (obstack, byte_order,
18305 type, DW_UNSND (attr), len);
18309 complaint (&symfile_complaints,
18310 _("unsupported const value attribute form: '%s'"),
18311 dwarf_form_name (attr->form));
18318 /* Return the type of the DIE at DIE_OFFSET in the CU named by
18322 dwarf2_get_die_type (cu_offset die_offset,
18323 struct dwarf2_per_cu_data *per_cu)
18325 sect_offset die_offset_sect;
18327 dw2_setup (per_cu->objfile);
18329 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18330 return get_die_type_at_offset (die_offset_sect, per_cu);
18333 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
18334 On entry *REF_CU is the CU of SRC_DIE.
18335 On exit *REF_CU is the CU of the result.
18336 Returns NULL if the referenced DIE isn't found. */
18338 static struct die_info *
18339 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18340 struct dwarf2_cu **ref_cu)
18342 struct objfile *objfile = (*ref_cu)->objfile;
18343 struct die_info temp_die;
18344 struct dwarf2_cu *sig_cu;
18345 struct die_info *die;
18347 /* While it might be nice to assert sig_type->type == NULL here,
18348 we can get here for DW_AT_imported_declaration where we need
18349 the DIE not the type. */
18351 /* If necessary, add it to the queue and load its DIEs. */
18353 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
18354 read_signatured_type (sig_type);
18356 gdb_assert (sig_type->per_cu.cu != NULL);
18358 sig_cu = sig_type->per_cu.cu;
18359 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18360 temp_die.offset = sig_type->type_offset_in_section;
18361 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18362 temp_die.offset.sect_off);
18365 /* For .gdb_index version 7 keep track of included TUs.
18366 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18367 if (dwarf2_per_objfile->index_table != NULL
18368 && dwarf2_per_objfile->index_table->version <= 7)
18370 VEC_safe_push (dwarf2_per_cu_ptr,
18371 (*ref_cu)->per_cu->imported_symtabs,
18382 /* Follow signatured type referenced by ATTR in SRC_DIE.
18383 On entry *REF_CU is the CU of SRC_DIE.
18384 On exit *REF_CU is the CU of the result.
18385 The result is the DIE of the type.
18386 If the referenced type cannot be found an error is thrown. */
18388 static struct die_info *
18389 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
18390 struct dwarf2_cu **ref_cu)
18392 ULONGEST signature = DW_SIGNATURE (attr);
18393 struct signatured_type *sig_type;
18394 struct die_info *die;
18396 gdb_assert (attr->form == DW_FORM_ref_sig8);
18398 sig_type = lookup_signatured_type (*ref_cu, signature);
18399 /* sig_type will be NULL if the signatured type is missing from
18401 if (sig_type == NULL)
18403 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18404 " from DIE at 0x%x [in module %s]"),
18405 hex_string (signature), src_die->offset.sect_off,
18406 (*ref_cu)->objfile->name);
18409 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18412 dump_die_for_error (src_die);
18413 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18414 " from DIE at 0x%x [in module %s]"),
18415 hex_string (signature), src_die->offset.sect_off,
18416 (*ref_cu)->objfile->name);
18422 /* Get the type specified by SIGNATURE referenced in DIE/CU,
18423 reading in and processing the type unit if necessary. */
18425 static struct type *
18426 get_signatured_type (struct die_info *die, ULONGEST signature,
18427 struct dwarf2_cu *cu)
18429 struct signatured_type *sig_type;
18430 struct dwarf2_cu *type_cu;
18431 struct die_info *type_die;
18434 sig_type = lookup_signatured_type (cu, signature);
18435 /* sig_type will be NULL if the signatured type is missing from
18437 if (sig_type == NULL)
18439 complaint (&symfile_complaints,
18440 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18441 " from DIE at 0x%x [in module %s]"),
18442 hex_string (signature), die->offset.sect_off,
18443 dwarf2_per_objfile->objfile->name);
18444 return build_error_marker_type (cu, die);
18447 /* If we already know the type we're done. */
18448 if (sig_type->type != NULL)
18449 return sig_type->type;
18452 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18453 if (type_die != NULL)
18455 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18456 is created. This is important, for example, because for c++ classes
18457 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18458 type = read_type_die (type_die, type_cu);
18461 complaint (&symfile_complaints,
18462 _("Dwarf Error: Cannot build signatured type %s"
18463 " referenced from DIE at 0x%x [in module %s]"),
18464 hex_string (signature), die->offset.sect_off,
18465 dwarf2_per_objfile->objfile->name);
18466 type = build_error_marker_type (cu, die);
18471 complaint (&symfile_complaints,
18472 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18473 " from DIE at 0x%x [in module %s]"),
18474 hex_string (signature), die->offset.sect_off,
18475 dwarf2_per_objfile->objfile->name);
18476 type = build_error_marker_type (cu, die);
18478 sig_type->type = type;
18483 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18484 reading in and processing the type unit if necessary. */
18486 static struct type *
18487 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
18488 struct dwarf2_cu *cu) /* ARI: editCase function */
18490 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18491 if (attr_form_is_ref (attr))
18493 struct dwarf2_cu *type_cu = cu;
18494 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18496 return read_type_die (type_die, type_cu);
18498 else if (attr->form == DW_FORM_ref_sig8)
18500 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18504 complaint (&symfile_complaints,
18505 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18506 " at 0x%x [in module %s]"),
18507 dwarf_form_name (attr->form), die->offset.sect_off,
18508 dwarf2_per_objfile->objfile->name);
18509 return build_error_marker_type (cu, die);
18513 /* Load the DIEs associated with type unit PER_CU into memory. */
18516 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
18518 struct signatured_type *sig_type;
18520 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18521 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18523 /* We have the per_cu, but we need the signatured_type.
18524 Fortunately this is an easy translation. */
18525 gdb_assert (per_cu->is_debug_types);
18526 sig_type = (struct signatured_type *) per_cu;
18528 gdb_assert (per_cu->cu == NULL);
18530 read_signatured_type (sig_type);
18532 gdb_assert (per_cu->cu != NULL);
18535 /* die_reader_func for read_signatured_type.
18536 This is identical to load_full_comp_unit_reader,
18537 but is kept separate for now. */
18540 read_signatured_type_reader (const struct die_reader_specs *reader,
18541 const gdb_byte *info_ptr,
18542 struct die_info *comp_unit_die,
18546 struct dwarf2_cu *cu = reader->cu;
18548 gdb_assert (cu->die_hash == NULL);
18550 htab_create_alloc_ex (cu->header.length / 12,
18554 &cu->comp_unit_obstack,
18555 hashtab_obstack_allocate,
18556 dummy_obstack_deallocate);
18559 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18560 &info_ptr, comp_unit_die);
18561 cu->dies = comp_unit_die;
18562 /* comp_unit_die is not stored in die_hash, no need. */
18564 /* We try not to read any attributes in this function, because not
18565 all CUs needed for references have been loaded yet, and symbol
18566 table processing isn't initialized. But we have to set the CU language,
18567 or we won't be able to build types correctly.
18568 Similarly, if we do not read the producer, we can not apply
18569 producer-specific interpretation. */
18570 prepare_one_comp_unit (cu, cu->dies, language_minimal);
18573 /* Read in a signatured type and build its CU and DIEs.
18574 If the type is a stub for the real type in a DWO file,
18575 read in the real type from the DWO file as well. */
18578 read_signatured_type (struct signatured_type *sig_type)
18580 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
18582 gdb_assert (per_cu->is_debug_types);
18583 gdb_assert (per_cu->cu == NULL);
18585 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18586 read_signatured_type_reader, NULL);
18589 /* Decode simple location descriptions.
18590 Given a pointer to a dwarf block that defines a location, compute
18591 the location and return the value.
18593 NOTE drow/2003-11-18: This function is called in two situations
18594 now: for the address of static or global variables (partial symbols
18595 only) and for offsets into structures which are expected to be
18596 (more or less) constant. The partial symbol case should go away,
18597 and only the constant case should remain. That will let this
18598 function complain more accurately. A few special modes are allowed
18599 without complaint for global variables (for instance, global
18600 register values and thread-local values).
18602 A location description containing no operations indicates that the
18603 object is optimized out. The return value is 0 for that case.
18604 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18605 callers will only want a very basic result and this can become a
18608 Note that stack[0] is unused except as a default error return. */
18611 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
18613 struct objfile *objfile = cu->objfile;
18615 size_t size = blk->size;
18616 const gdb_byte *data = blk->data;
18617 CORE_ADDR stack[64];
18619 unsigned int bytes_read, unsnd;
18625 stack[++stacki] = 0;
18664 stack[++stacki] = op - DW_OP_lit0;
18699 stack[++stacki] = op - DW_OP_reg0;
18701 dwarf2_complex_location_expr_complaint ();
18705 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18707 stack[++stacki] = unsnd;
18709 dwarf2_complex_location_expr_complaint ();
18713 stack[++stacki] = read_address (objfile->obfd, &data[i],
18718 case DW_OP_const1u:
18719 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18723 case DW_OP_const1s:
18724 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18728 case DW_OP_const2u:
18729 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18733 case DW_OP_const2s:
18734 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18738 case DW_OP_const4u:
18739 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18743 case DW_OP_const4s:
18744 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18748 case DW_OP_const8u:
18749 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18754 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18760 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18765 stack[stacki + 1] = stack[stacki];
18770 stack[stacki - 1] += stack[stacki];
18774 case DW_OP_plus_uconst:
18775 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18781 stack[stacki - 1] -= stack[stacki];
18786 /* If we're not the last op, then we definitely can't encode
18787 this using GDB's address_class enum. This is valid for partial
18788 global symbols, although the variable's address will be bogus
18791 dwarf2_complex_location_expr_complaint ();
18794 case DW_OP_GNU_push_tls_address:
18795 /* The top of the stack has the offset from the beginning
18796 of the thread control block at which the variable is located. */
18797 /* Nothing should follow this operator, so the top of stack would
18799 /* This is valid for partial global symbols, but the variable's
18800 address will be bogus in the psymtab. Make it always at least
18801 non-zero to not look as a variable garbage collected by linker
18802 which have DW_OP_addr 0. */
18804 dwarf2_complex_location_expr_complaint ();
18808 case DW_OP_GNU_uninit:
18811 case DW_OP_GNU_addr_index:
18812 case DW_OP_GNU_const_index:
18813 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18820 const char *name = get_DW_OP_name (op);
18823 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18826 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18830 return (stack[stacki]);
18833 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18834 outside of the allocated space. Also enforce minimum>0. */
18835 if (stacki >= ARRAY_SIZE (stack) - 1)
18837 complaint (&symfile_complaints,
18838 _("location description stack overflow"));
18844 complaint (&symfile_complaints,
18845 _("location description stack underflow"));
18849 return (stack[stacki]);
18852 /* memory allocation interface */
18854 static struct dwarf_block *
18855 dwarf_alloc_block (struct dwarf2_cu *cu)
18857 struct dwarf_block *blk;
18859 blk = (struct dwarf_block *)
18860 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18864 static struct die_info *
18865 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18867 struct die_info *die;
18868 size_t size = sizeof (struct die_info);
18871 size += (num_attrs - 1) * sizeof (struct attribute);
18873 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18874 memset (die, 0, sizeof (struct die_info));
18879 /* Macro support. */
18881 /* Return file name relative to the compilation directory of file number I in
18882 *LH's file name table. The result is allocated using xmalloc; the caller is
18883 responsible for freeing it. */
18886 file_file_name (int file, struct line_header *lh)
18888 /* Is the file number a valid index into the line header's file name
18889 table? Remember that file numbers start with one, not zero. */
18890 if (1 <= file && file <= lh->num_file_names)
18892 struct file_entry *fe = &lh->file_names[file - 1];
18894 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18895 return xstrdup (fe->name);
18896 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18901 /* The compiler produced a bogus file number. We can at least
18902 record the macro definitions made in the file, even if we
18903 won't be able to find the file by name. */
18904 char fake_name[80];
18906 xsnprintf (fake_name, sizeof (fake_name),
18907 "<bad macro file number %d>", file);
18909 complaint (&symfile_complaints,
18910 _("bad file number in macro information (%d)"),
18913 return xstrdup (fake_name);
18917 /* Return the full name of file number I in *LH's file name table.
18918 Use COMP_DIR as the name of the current directory of the
18919 compilation. The result is allocated using xmalloc; the caller is
18920 responsible for freeing it. */
18922 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18924 /* Is the file number a valid index into the line header's file name
18925 table? Remember that file numbers start with one, not zero. */
18926 if (1 <= file && file <= lh->num_file_names)
18928 char *relative = file_file_name (file, lh);
18930 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18932 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18935 return file_file_name (file, lh);
18939 static struct macro_source_file *
18940 macro_start_file (int file, int line,
18941 struct macro_source_file *current_file,
18942 const char *comp_dir,
18943 struct line_header *lh, struct objfile *objfile)
18945 /* File name relative to the compilation directory of this source file. */
18946 char *file_name = file_file_name (file, lh);
18948 /* We don't create a macro table for this compilation unit
18949 at all until we actually get a filename. */
18950 if (! pending_macros)
18951 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18952 objfile->per_bfd->macro_cache,
18955 if (! current_file)
18957 /* If we have no current file, then this must be the start_file
18958 directive for the compilation unit's main source file. */
18959 current_file = macro_set_main (pending_macros, file_name);
18960 macro_define_special (pending_macros);
18963 current_file = macro_include (current_file, line, file_name);
18967 return current_file;
18971 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18972 followed by a null byte. */
18974 copy_string (const char *buf, int len)
18976 char *s = xmalloc (len + 1);
18978 memcpy (s, buf, len);
18984 static const char *
18985 consume_improper_spaces (const char *p, const char *body)
18989 complaint (&symfile_complaints,
18990 _("macro definition contains spaces "
18991 "in formal argument list:\n`%s'"),
19003 parse_macro_definition (struct macro_source_file *file, int line,
19008 /* The body string takes one of two forms. For object-like macro
19009 definitions, it should be:
19011 <macro name> " " <definition>
19013 For function-like macro definitions, it should be:
19015 <macro name> "() " <definition>
19017 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19019 Spaces may appear only where explicitly indicated, and in the
19022 The Dwarf 2 spec says that an object-like macro's name is always
19023 followed by a space, but versions of GCC around March 2002 omit
19024 the space when the macro's definition is the empty string.
19026 The Dwarf 2 spec says that there should be no spaces between the
19027 formal arguments in a function-like macro's formal argument list,
19028 but versions of GCC around March 2002 include spaces after the
19032 /* Find the extent of the macro name. The macro name is terminated
19033 by either a space or null character (for an object-like macro) or
19034 an opening paren (for a function-like macro). */
19035 for (p = body; *p; p++)
19036 if (*p == ' ' || *p == '(')
19039 if (*p == ' ' || *p == '\0')
19041 /* It's an object-like macro. */
19042 int name_len = p - body;
19043 char *name = copy_string (body, name_len);
19044 const char *replacement;
19047 replacement = body + name_len + 1;
19050 dwarf2_macro_malformed_definition_complaint (body);
19051 replacement = body + name_len;
19054 macro_define_object (file, line, name, replacement);
19058 else if (*p == '(')
19060 /* It's a function-like macro. */
19061 char *name = copy_string (body, p - body);
19064 char **argv = xmalloc (argv_size * sizeof (*argv));
19068 p = consume_improper_spaces (p, body);
19070 /* Parse the formal argument list. */
19071 while (*p && *p != ')')
19073 /* Find the extent of the current argument name. */
19074 const char *arg_start = p;
19076 while (*p && *p != ',' && *p != ')' && *p != ' ')
19079 if (! *p || p == arg_start)
19080 dwarf2_macro_malformed_definition_complaint (body);
19083 /* Make sure argv has room for the new argument. */
19084 if (argc >= argv_size)
19087 argv = xrealloc (argv, argv_size * sizeof (*argv));
19090 argv[argc++] = copy_string (arg_start, p - arg_start);
19093 p = consume_improper_spaces (p, body);
19095 /* Consume the comma, if present. */
19100 p = consume_improper_spaces (p, body);
19109 /* Perfectly formed definition, no complaints. */
19110 macro_define_function (file, line, name,
19111 argc, (const char **) argv,
19113 else if (*p == '\0')
19115 /* Complain, but do define it. */
19116 dwarf2_macro_malformed_definition_complaint (body);
19117 macro_define_function (file, line, name,
19118 argc, (const char **) argv,
19122 /* Just complain. */
19123 dwarf2_macro_malformed_definition_complaint (body);
19126 /* Just complain. */
19127 dwarf2_macro_malformed_definition_complaint (body);
19133 for (i = 0; i < argc; i++)
19139 dwarf2_macro_malformed_definition_complaint (body);
19142 /* Skip some bytes from BYTES according to the form given in FORM.
19143 Returns the new pointer. */
19145 static const gdb_byte *
19146 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19147 enum dwarf_form form,
19148 unsigned int offset_size,
19149 struct dwarf2_section_info *section)
19151 unsigned int bytes_read;
19155 case DW_FORM_data1:
19160 case DW_FORM_data2:
19164 case DW_FORM_data4:
19168 case DW_FORM_data8:
19172 case DW_FORM_string:
19173 read_direct_string (abfd, bytes, &bytes_read);
19174 bytes += bytes_read;
19177 case DW_FORM_sec_offset:
19179 case DW_FORM_GNU_strp_alt:
19180 bytes += offset_size;
19183 case DW_FORM_block:
19184 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19185 bytes += bytes_read;
19188 case DW_FORM_block1:
19189 bytes += 1 + read_1_byte (abfd, bytes);
19191 case DW_FORM_block2:
19192 bytes += 2 + read_2_bytes (abfd, bytes);
19194 case DW_FORM_block4:
19195 bytes += 4 + read_4_bytes (abfd, bytes);
19198 case DW_FORM_sdata:
19199 case DW_FORM_udata:
19200 case DW_FORM_GNU_addr_index:
19201 case DW_FORM_GNU_str_index:
19202 bytes = gdb_skip_leb128 (bytes, buffer_end);
19205 dwarf2_section_buffer_overflow_complaint (section);
19213 complaint (&symfile_complaints,
19214 _("invalid form 0x%x in `%s'"),
19216 section->asection->name);
19224 /* A helper for dwarf_decode_macros that handles skipping an unknown
19225 opcode. Returns an updated pointer to the macro data buffer; or,
19226 on error, issues a complaint and returns NULL. */
19228 static const gdb_byte *
19229 skip_unknown_opcode (unsigned int opcode,
19230 const gdb_byte **opcode_definitions,
19231 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19233 unsigned int offset_size,
19234 struct dwarf2_section_info *section)
19236 unsigned int bytes_read, i;
19238 const gdb_byte *defn;
19240 if (opcode_definitions[opcode] == NULL)
19242 complaint (&symfile_complaints,
19243 _("unrecognized DW_MACFINO opcode 0x%x"),
19248 defn = opcode_definitions[opcode];
19249 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19250 defn += bytes_read;
19252 for (i = 0; i < arg; ++i)
19254 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19256 if (mac_ptr == NULL)
19258 /* skip_form_bytes already issued the complaint. */
19266 /* A helper function which parses the header of a macro section.
19267 If the macro section is the extended (for now called "GNU") type,
19268 then this updates *OFFSET_SIZE. Returns a pointer to just after
19269 the header, or issues a complaint and returns NULL on error. */
19271 static const gdb_byte *
19272 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
19274 const gdb_byte *mac_ptr,
19275 unsigned int *offset_size,
19276 int section_is_gnu)
19278 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
19280 if (section_is_gnu)
19282 unsigned int version, flags;
19284 version = read_2_bytes (abfd, mac_ptr);
19287 complaint (&symfile_complaints,
19288 _("unrecognized version `%d' in .debug_macro section"),
19294 flags = read_1_byte (abfd, mac_ptr);
19296 *offset_size = (flags & 1) ? 8 : 4;
19298 if ((flags & 2) != 0)
19299 /* We don't need the line table offset. */
19300 mac_ptr += *offset_size;
19302 /* Vendor opcode descriptions. */
19303 if ((flags & 4) != 0)
19305 unsigned int i, count;
19307 count = read_1_byte (abfd, mac_ptr);
19309 for (i = 0; i < count; ++i)
19311 unsigned int opcode, bytes_read;
19314 opcode = read_1_byte (abfd, mac_ptr);
19316 opcode_definitions[opcode] = mac_ptr;
19317 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19318 mac_ptr += bytes_read;
19327 /* A helper for dwarf_decode_macros that handles the GNU extensions,
19328 including DW_MACRO_GNU_transparent_include. */
19331 dwarf_decode_macro_bytes (bfd *abfd,
19332 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19333 struct macro_source_file *current_file,
19334 struct line_header *lh, const char *comp_dir,
19335 struct dwarf2_section_info *section,
19336 int section_is_gnu, int section_is_dwz,
19337 unsigned int offset_size,
19338 struct objfile *objfile,
19339 htab_t include_hash)
19341 enum dwarf_macro_record_type macinfo_type;
19342 int at_commandline;
19343 const gdb_byte *opcode_definitions[256];
19345 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19346 &offset_size, section_is_gnu);
19347 if (mac_ptr == NULL)
19349 /* We already issued a complaint. */
19353 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19354 GDB is still reading the definitions from command line. First
19355 DW_MACINFO_start_file will need to be ignored as it was already executed
19356 to create CURRENT_FILE for the main source holding also the command line
19357 definitions. On first met DW_MACINFO_start_file this flag is reset to
19358 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19360 at_commandline = 1;
19364 /* Do we at least have room for a macinfo type byte? */
19365 if (mac_ptr >= mac_end)
19367 dwarf2_section_buffer_overflow_complaint (section);
19371 macinfo_type = read_1_byte (abfd, mac_ptr);
19374 /* Note that we rely on the fact that the corresponding GNU and
19375 DWARF constants are the same. */
19376 switch (macinfo_type)
19378 /* A zero macinfo type indicates the end of the macro
19383 case DW_MACRO_GNU_define:
19384 case DW_MACRO_GNU_undef:
19385 case DW_MACRO_GNU_define_indirect:
19386 case DW_MACRO_GNU_undef_indirect:
19387 case DW_MACRO_GNU_define_indirect_alt:
19388 case DW_MACRO_GNU_undef_indirect_alt:
19390 unsigned int bytes_read;
19395 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19396 mac_ptr += bytes_read;
19398 if (macinfo_type == DW_MACRO_GNU_define
19399 || macinfo_type == DW_MACRO_GNU_undef)
19401 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19402 mac_ptr += bytes_read;
19406 LONGEST str_offset;
19408 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19409 mac_ptr += offset_size;
19411 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
19412 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19415 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19417 body = read_indirect_string_from_dwz (dwz, str_offset);
19420 body = read_indirect_string_at_offset (abfd, str_offset);
19423 is_define = (macinfo_type == DW_MACRO_GNU_define
19424 || macinfo_type == DW_MACRO_GNU_define_indirect
19425 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
19426 if (! current_file)
19428 /* DWARF violation as no main source is present. */
19429 complaint (&symfile_complaints,
19430 _("debug info with no main source gives macro %s "
19432 is_define ? _("definition") : _("undefinition"),
19436 if ((line == 0 && !at_commandline)
19437 || (line != 0 && at_commandline))
19438 complaint (&symfile_complaints,
19439 _("debug info gives %s macro %s with %s line %d: %s"),
19440 at_commandline ? _("command-line") : _("in-file"),
19441 is_define ? _("definition") : _("undefinition"),
19442 line == 0 ? _("zero") : _("non-zero"), line, body);
19445 parse_macro_definition (current_file, line, body);
19448 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
19449 || macinfo_type == DW_MACRO_GNU_undef_indirect
19450 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
19451 macro_undef (current_file, line, body);
19456 case DW_MACRO_GNU_start_file:
19458 unsigned int bytes_read;
19461 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19462 mac_ptr += bytes_read;
19463 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19464 mac_ptr += bytes_read;
19466 if ((line == 0 && !at_commandline)
19467 || (line != 0 && at_commandline))
19468 complaint (&symfile_complaints,
19469 _("debug info gives source %d included "
19470 "from %s at %s line %d"),
19471 file, at_commandline ? _("command-line") : _("file"),
19472 line == 0 ? _("zero") : _("non-zero"), line);
19474 if (at_commandline)
19476 /* This DW_MACRO_GNU_start_file was executed in the
19478 at_commandline = 0;
19481 current_file = macro_start_file (file, line,
19482 current_file, comp_dir,
19487 case DW_MACRO_GNU_end_file:
19488 if (! current_file)
19489 complaint (&symfile_complaints,
19490 _("macro debug info has an unmatched "
19491 "`close_file' directive"));
19494 current_file = current_file->included_by;
19495 if (! current_file)
19497 enum dwarf_macro_record_type next_type;
19499 /* GCC circa March 2002 doesn't produce the zero
19500 type byte marking the end of the compilation
19501 unit. Complain if it's not there, but exit no
19504 /* Do we at least have room for a macinfo type byte? */
19505 if (mac_ptr >= mac_end)
19507 dwarf2_section_buffer_overflow_complaint (section);
19511 /* We don't increment mac_ptr here, so this is just
19513 next_type = read_1_byte (abfd, mac_ptr);
19514 if (next_type != 0)
19515 complaint (&symfile_complaints,
19516 _("no terminating 0-type entry for "
19517 "macros in `.debug_macinfo' section"));
19524 case DW_MACRO_GNU_transparent_include:
19525 case DW_MACRO_GNU_transparent_include_alt:
19529 bfd *include_bfd = abfd;
19530 struct dwarf2_section_info *include_section = section;
19531 struct dwarf2_section_info alt_section;
19532 const gdb_byte *include_mac_end = mac_end;
19533 int is_dwz = section_is_dwz;
19534 const gdb_byte *new_mac_ptr;
19536 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19537 mac_ptr += offset_size;
19539 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19541 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19543 dwarf2_read_section (dwarf2_per_objfile->objfile,
19546 include_bfd = dwz->macro.asection->owner;
19547 include_section = &dwz->macro;
19548 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19552 new_mac_ptr = include_section->buffer + offset;
19553 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19557 /* This has actually happened; see
19558 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19559 complaint (&symfile_complaints,
19560 _("recursive DW_MACRO_GNU_transparent_include in "
19561 ".debug_macro section"));
19565 *slot = (void *) new_mac_ptr;
19567 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
19568 include_mac_end, current_file,
19570 section, section_is_gnu, is_dwz,
19571 offset_size, objfile, include_hash);
19573 htab_remove_elt (include_hash, (void *) new_mac_ptr);
19578 case DW_MACINFO_vendor_ext:
19579 if (!section_is_gnu)
19581 unsigned int bytes_read;
19584 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19585 mac_ptr += bytes_read;
19586 read_direct_string (abfd, mac_ptr, &bytes_read);
19587 mac_ptr += bytes_read;
19589 /* We don't recognize any vendor extensions. */
19595 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19596 mac_ptr, mac_end, abfd, offset_size,
19598 if (mac_ptr == NULL)
19602 } while (macinfo_type != 0);
19606 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
19607 const char *comp_dir, int section_is_gnu)
19609 struct objfile *objfile = dwarf2_per_objfile->objfile;
19610 struct line_header *lh = cu->line_header;
19612 const gdb_byte *mac_ptr, *mac_end;
19613 struct macro_source_file *current_file = 0;
19614 enum dwarf_macro_record_type macinfo_type;
19615 unsigned int offset_size = cu->header.offset_size;
19616 const gdb_byte *opcode_definitions[256];
19617 struct cleanup *cleanup;
19618 htab_t include_hash;
19620 struct dwarf2_section_info *section;
19621 const char *section_name;
19623 if (cu->dwo_unit != NULL)
19625 if (section_is_gnu)
19627 section = &cu->dwo_unit->dwo_file->sections.macro;
19628 section_name = ".debug_macro.dwo";
19632 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19633 section_name = ".debug_macinfo.dwo";
19638 if (section_is_gnu)
19640 section = &dwarf2_per_objfile->macro;
19641 section_name = ".debug_macro";
19645 section = &dwarf2_per_objfile->macinfo;
19646 section_name = ".debug_macinfo";
19650 dwarf2_read_section (objfile, section);
19651 if (section->buffer == NULL)
19653 complaint (&symfile_complaints, _("missing %s section"), section_name);
19656 abfd = section->asection->owner;
19658 /* First pass: Find the name of the base filename.
19659 This filename is needed in order to process all macros whose definition
19660 (or undefinition) comes from the command line. These macros are defined
19661 before the first DW_MACINFO_start_file entry, and yet still need to be
19662 associated to the base file.
19664 To determine the base file name, we scan the macro definitions until we
19665 reach the first DW_MACINFO_start_file entry. We then initialize
19666 CURRENT_FILE accordingly so that any macro definition found before the
19667 first DW_MACINFO_start_file can still be associated to the base file. */
19669 mac_ptr = section->buffer + offset;
19670 mac_end = section->buffer + section->size;
19672 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19673 &offset_size, section_is_gnu);
19674 if (mac_ptr == NULL)
19676 /* We already issued a complaint. */
19682 /* Do we at least have room for a macinfo type byte? */
19683 if (mac_ptr >= mac_end)
19685 /* Complaint is printed during the second pass as GDB will probably
19686 stop the first pass earlier upon finding
19687 DW_MACINFO_start_file. */
19691 macinfo_type = read_1_byte (abfd, mac_ptr);
19694 /* Note that we rely on the fact that the corresponding GNU and
19695 DWARF constants are the same. */
19696 switch (macinfo_type)
19698 /* A zero macinfo type indicates the end of the macro
19703 case DW_MACRO_GNU_define:
19704 case DW_MACRO_GNU_undef:
19705 /* Only skip the data by MAC_PTR. */
19707 unsigned int bytes_read;
19709 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19710 mac_ptr += bytes_read;
19711 read_direct_string (abfd, mac_ptr, &bytes_read);
19712 mac_ptr += bytes_read;
19716 case DW_MACRO_GNU_start_file:
19718 unsigned int bytes_read;
19721 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19722 mac_ptr += bytes_read;
19723 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19724 mac_ptr += bytes_read;
19726 current_file = macro_start_file (file, line, current_file,
19727 comp_dir, lh, objfile);
19731 case DW_MACRO_GNU_end_file:
19732 /* No data to skip by MAC_PTR. */
19735 case DW_MACRO_GNU_define_indirect:
19736 case DW_MACRO_GNU_undef_indirect:
19737 case DW_MACRO_GNU_define_indirect_alt:
19738 case DW_MACRO_GNU_undef_indirect_alt:
19740 unsigned int bytes_read;
19742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19743 mac_ptr += bytes_read;
19744 mac_ptr += offset_size;
19748 case DW_MACRO_GNU_transparent_include:
19749 case DW_MACRO_GNU_transparent_include_alt:
19750 /* Note that, according to the spec, a transparent include
19751 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19752 skip this opcode. */
19753 mac_ptr += offset_size;
19756 case DW_MACINFO_vendor_ext:
19757 /* Only skip the data by MAC_PTR. */
19758 if (!section_is_gnu)
19760 unsigned int bytes_read;
19762 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19763 mac_ptr += bytes_read;
19764 read_direct_string (abfd, mac_ptr, &bytes_read);
19765 mac_ptr += bytes_read;
19770 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19771 mac_ptr, mac_end, abfd, offset_size,
19773 if (mac_ptr == NULL)
19777 } while (macinfo_type != 0 && current_file == NULL);
19779 /* Second pass: Process all entries.
19781 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19782 command-line macro definitions/undefinitions. This flag is unset when we
19783 reach the first DW_MACINFO_start_file entry. */
19785 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19786 NULL, xcalloc, xfree);
19787 cleanup = make_cleanup_htab_delete (include_hash);
19788 mac_ptr = section->buffer + offset;
19789 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19790 *slot = (void *) mac_ptr;
19791 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19792 current_file, lh, comp_dir, section,
19794 offset_size, objfile, include_hash);
19795 do_cleanups (cleanup);
19798 /* Check if the attribute's form is a DW_FORM_block*
19799 if so return true else false. */
19802 attr_form_is_block (const struct attribute *attr)
19804 return (attr == NULL ? 0 :
19805 attr->form == DW_FORM_block1
19806 || attr->form == DW_FORM_block2
19807 || attr->form == DW_FORM_block4
19808 || attr->form == DW_FORM_block
19809 || attr->form == DW_FORM_exprloc);
19812 /* Return non-zero if ATTR's value is a section offset --- classes
19813 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19814 You may use DW_UNSND (attr) to retrieve such offsets.
19816 Section 7.5.4, "Attribute Encodings", explains that no attribute
19817 may have a value that belongs to more than one of these classes; it
19818 would be ambiguous if we did, because we use the same forms for all
19822 attr_form_is_section_offset (const struct attribute *attr)
19824 return (attr->form == DW_FORM_data4
19825 || attr->form == DW_FORM_data8
19826 || attr->form == DW_FORM_sec_offset);
19829 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19830 zero otherwise. When this function returns true, you can apply
19831 dwarf2_get_attr_constant_value to it.
19833 However, note that for some attributes you must check
19834 attr_form_is_section_offset before using this test. DW_FORM_data4
19835 and DW_FORM_data8 are members of both the constant class, and of
19836 the classes that contain offsets into other debug sections
19837 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19838 that, if an attribute's can be either a constant or one of the
19839 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19840 taken as section offsets, not constants. */
19843 attr_form_is_constant (const struct attribute *attr)
19845 switch (attr->form)
19847 case DW_FORM_sdata:
19848 case DW_FORM_udata:
19849 case DW_FORM_data1:
19850 case DW_FORM_data2:
19851 case DW_FORM_data4:
19852 case DW_FORM_data8:
19860 /* DW_ADDR is always stored already as sect_offset; despite for the forms
19861 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19864 attr_form_is_ref (const struct attribute *attr)
19866 switch (attr->form)
19868 case DW_FORM_ref_addr:
19873 case DW_FORM_ref_udata:
19874 case DW_FORM_GNU_ref_alt:
19881 /* Return the .debug_loc section to use for CU.
19882 For DWO files use .debug_loc.dwo. */
19884 static struct dwarf2_section_info *
19885 cu_debug_loc_section (struct dwarf2_cu *cu)
19888 return &cu->dwo_unit->dwo_file->sections.loc;
19889 return &dwarf2_per_objfile->loc;
19892 /* A helper function that fills in a dwarf2_loclist_baton. */
19895 fill_in_loclist_baton (struct dwarf2_cu *cu,
19896 struct dwarf2_loclist_baton *baton,
19897 const struct attribute *attr)
19899 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19901 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19903 baton->per_cu = cu->per_cu;
19904 gdb_assert (baton->per_cu);
19905 /* We don't know how long the location list is, but make sure we
19906 don't run off the edge of the section. */
19907 baton->size = section->size - DW_UNSND (attr);
19908 baton->data = section->buffer + DW_UNSND (attr);
19909 baton->base_address = cu->base_address;
19910 baton->from_dwo = cu->dwo_unit != NULL;
19914 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
19915 struct dwarf2_cu *cu, int is_block)
19917 struct objfile *objfile = dwarf2_per_objfile->objfile;
19918 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19920 if (attr_form_is_section_offset (attr)
19921 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19922 the section. If so, fall through to the complaint in the
19924 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19926 struct dwarf2_loclist_baton *baton;
19928 baton = obstack_alloc (&objfile->objfile_obstack,
19929 sizeof (struct dwarf2_loclist_baton));
19931 fill_in_loclist_baton (cu, baton, attr);
19933 if (cu->base_known == 0)
19934 complaint (&symfile_complaints,
19935 _("Location list used without "
19936 "specifying the CU base address."));
19938 SYMBOL_ACLASS_INDEX (sym) = (is_block
19939 ? dwarf2_loclist_block_index
19940 : dwarf2_loclist_index);
19941 SYMBOL_LOCATION_BATON (sym) = baton;
19945 struct dwarf2_locexpr_baton *baton;
19947 baton = obstack_alloc (&objfile->objfile_obstack,
19948 sizeof (struct dwarf2_locexpr_baton));
19949 baton->per_cu = cu->per_cu;
19950 gdb_assert (baton->per_cu);
19952 if (attr_form_is_block (attr))
19954 /* Note that we're just copying the block's data pointer
19955 here, not the actual data. We're still pointing into the
19956 info_buffer for SYM's objfile; right now we never release
19957 that buffer, but when we do clean up properly this may
19959 baton->size = DW_BLOCK (attr)->size;
19960 baton->data = DW_BLOCK (attr)->data;
19964 dwarf2_invalid_attrib_class_complaint ("location description",
19965 SYMBOL_NATURAL_NAME (sym));
19969 SYMBOL_ACLASS_INDEX (sym) = (is_block
19970 ? dwarf2_locexpr_block_index
19971 : dwarf2_locexpr_index);
19972 SYMBOL_LOCATION_BATON (sym) = baton;
19976 /* Return the OBJFILE associated with the compilation unit CU. If CU
19977 came from a separate debuginfo file, then the master objfile is
19981 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19983 struct objfile *objfile = per_cu->objfile;
19985 /* Return the master objfile, so that we can report and look up the
19986 correct file containing this variable. */
19987 if (objfile->separate_debug_objfile_backlink)
19988 objfile = objfile->separate_debug_objfile_backlink;
19993 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19994 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19995 CU_HEADERP first. */
19997 static const struct comp_unit_head *
19998 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19999 struct dwarf2_per_cu_data *per_cu)
20001 const gdb_byte *info_ptr;
20004 return &per_cu->cu->header;
20006 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
20008 memset (cu_headerp, 0, sizeof (*cu_headerp));
20009 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
20014 /* Return the address size given in the compilation unit header for CU. */
20017 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20019 struct comp_unit_head cu_header_local;
20020 const struct comp_unit_head *cu_headerp;
20022 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20024 return cu_headerp->addr_size;
20027 /* Return the offset size given in the compilation unit header for CU. */
20030 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20032 struct comp_unit_head cu_header_local;
20033 const struct comp_unit_head *cu_headerp;
20035 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20037 return cu_headerp->offset_size;
20040 /* See its dwarf2loc.h declaration. */
20043 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20045 struct comp_unit_head cu_header_local;
20046 const struct comp_unit_head *cu_headerp;
20048 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20050 if (cu_headerp->version == 2)
20051 return cu_headerp->addr_size;
20053 return cu_headerp->offset_size;
20056 /* Return the text offset of the CU. The returned offset comes from
20057 this CU's objfile. If this objfile came from a separate debuginfo
20058 file, then the offset may be different from the corresponding
20059 offset in the parent objfile. */
20062 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20064 struct objfile *objfile = per_cu->objfile;
20066 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20069 /* Locate the .debug_info compilation unit from CU's objfile which contains
20070 the DIE at OFFSET. Raises an error on failure. */
20072 static struct dwarf2_per_cu_data *
20073 dwarf2_find_containing_comp_unit (sect_offset offset,
20074 unsigned int offset_in_dwz,
20075 struct objfile *objfile)
20077 struct dwarf2_per_cu_data *this_cu;
20079 const sect_offset *cu_off;
20082 high = dwarf2_per_objfile->n_comp_units - 1;
20085 struct dwarf2_per_cu_data *mid_cu;
20086 int mid = low + (high - low) / 2;
20088 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20089 cu_off = &mid_cu->offset;
20090 if (mid_cu->is_dwz > offset_in_dwz
20091 || (mid_cu->is_dwz == offset_in_dwz
20092 && cu_off->sect_off >= offset.sect_off))
20097 gdb_assert (low == high);
20098 this_cu = dwarf2_per_objfile->all_comp_units[low];
20099 cu_off = &this_cu->offset;
20100 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20102 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20103 error (_("Dwarf Error: could not find partial DIE containing "
20104 "offset 0x%lx [in module %s]"),
20105 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20107 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20108 <= offset.sect_off);
20109 return dwarf2_per_objfile->all_comp_units[low-1];
20113 this_cu = dwarf2_per_objfile->all_comp_units[low];
20114 if (low == dwarf2_per_objfile->n_comp_units - 1
20115 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20116 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20117 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20122 /* Initialize dwarf2_cu CU, owned by PER_CU. */
20125 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20127 memset (cu, 0, sizeof (*cu));
20129 cu->per_cu = per_cu;
20130 cu->objfile = per_cu->objfile;
20131 obstack_init (&cu->comp_unit_obstack);
20134 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20137 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20138 enum language pretend_language)
20140 struct attribute *attr;
20142 /* Set the language we're debugging. */
20143 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20145 set_cu_language (DW_UNSND (attr), cu);
20148 cu->language = pretend_language;
20149 cu->language_defn = language_def (cu->language);
20152 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20154 cu->producer = DW_STRING (attr);
20157 /* Release one cached compilation unit, CU. We unlink it from the tree
20158 of compilation units, but we don't remove it from the read_in_chain;
20159 the caller is responsible for that.
20160 NOTE: DATA is a void * because this function is also used as a
20161 cleanup routine. */
20164 free_heap_comp_unit (void *data)
20166 struct dwarf2_cu *cu = data;
20168 gdb_assert (cu->per_cu != NULL);
20169 cu->per_cu->cu = NULL;
20172 obstack_free (&cu->comp_unit_obstack, NULL);
20177 /* This cleanup function is passed the address of a dwarf2_cu on the stack
20178 when we're finished with it. We can't free the pointer itself, but be
20179 sure to unlink it from the cache. Also release any associated storage. */
20182 free_stack_comp_unit (void *data)
20184 struct dwarf2_cu *cu = data;
20186 gdb_assert (cu->per_cu != NULL);
20187 cu->per_cu->cu = NULL;
20190 obstack_free (&cu->comp_unit_obstack, NULL);
20191 cu->partial_dies = NULL;
20194 /* Free all cached compilation units. */
20197 free_cached_comp_units (void *data)
20199 struct dwarf2_per_cu_data *per_cu, **last_chain;
20201 per_cu = dwarf2_per_objfile->read_in_chain;
20202 last_chain = &dwarf2_per_objfile->read_in_chain;
20203 while (per_cu != NULL)
20205 struct dwarf2_per_cu_data *next_cu;
20207 next_cu = per_cu->cu->read_in_chain;
20209 free_heap_comp_unit (per_cu->cu);
20210 *last_chain = next_cu;
20216 /* Increase the age counter on each cached compilation unit, and free
20217 any that are too old. */
20220 age_cached_comp_units (void)
20222 struct dwarf2_per_cu_data *per_cu, **last_chain;
20224 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20225 per_cu = dwarf2_per_objfile->read_in_chain;
20226 while (per_cu != NULL)
20228 per_cu->cu->last_used ++;
20229 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20230 dwarf2_mark (per_cu->cu);
20231 per_cu = per_cu->cu->read_in_chain;
20234 per_cu = dwarf2_per_objfile->read_in_chain;
20235 last_chain = &dwarf2_per_objfile->read_in_chain;
20236 while (per_cu != NULL)
20238 struct dwarf2_per_cu_data *next_cu;
20240 next_cu = per_cu->cu->read_in_chain;
20242 if (!per_cu->cu->mark)
20244 free_heap_comp_unit (per_cu->cu);
20245 *last_chain = next_cu;
20248 last_chain = &per_cu->cu->read_in_chain;
20254 /* Remove a single compilation unit from the cache. */
20257 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
20259 struct dwarf2_per_cu_data *per_cu, **last_chain;
20261 per_cu = dwarf2_per_objfile->read_in_chain;
20262 last_chain = &dwarf2_per_objfile->read_in_chain;
20263 while (per_cu != NULL)
20265 struct dwarf2_per_cu_data *next_cu;
20267 next_cu = per_cu->cu->read_in_chain;
20269 if (per_cu == target_per_cu)
20271 free_heap_comp_unit (per_cu->cu);
20273 *last_chain = next_cu;
20277 last_chain = &per_cu->cu->read_in_chain;
20283 /* Release all extra memory associated with OBJFILE. */
20286 dwarf2_free_objfile (struct objfile *objfile)
20288 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20290 if (dwarf2_per_objfile == NULL)
20293 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20294 free_cached_comp_units (NULL);
20296 if (dwarf2_per_objfile->quick_file_names_table)
20297 htab_delete (dwarf2_per_objfile->quick_file_names_table);
20299 /* Everything else should be on the objfile obstack. */
20302 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20303 We store these in a hash table separate from the DIEs, and preserve them
20304 when the DIEs are flushed out of cache.
20306 The CU "per_cu" pointer is needed because offset alone is not enough to
20307 uniquely identify the type. A file may have multiple .debug_types sections,
20308 or the type may come from a DWO file. Furthermore, while it's more logical
20309 to use per_cu->section+offset, with Fission the section with the data is in
20310 the DWO file but we don't know that section at the point we need it.
20311 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20312 because we can enter the lookup routine, get_die_type_at_offset, from
20313 outside this file, and thus won't necessarily have PER_CU->cu.
20314 Fortunately, PER_CU is stable for the life of the objfile. */
20316 struct dwarf2_per_cu_offset_and_type
20318 const struct dwarf2_per_cu_data *per_cu;
20319 sect_offset offset;
20323 /* Hash function for a dwarf2_per_cu_offset_and_type. */
20326 per_cu_offset_and_type_hash (const void *item)
20328 const struct dwarf2_per_cu_offset_and_type *ofs = item;
20330 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
20333 /* Equality function for a dwarf2_per_cu_offset_and_type. */
20336 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
20338 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20339 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
20341 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20342 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
20345 /* Set the type associated with DIE to TYPE. Save it in CU's hash
20346 table if necessary. For convenience, return TYPE.
20348 The DIEs reading must have careful ordering to:
20349 * Not cause infite loops trying to read in DIEs as a prerequisite for
20350 reading current DIE.
20351 * Not trying to dereference contents of still incompletely read in types
20352 while reading in other DIEs.
20353 * Enable referencing still incompletely read in types just by a pointer to
20354 the type without accessing its fields.
20356 Therefore caller should follow these rules:
20357 * Try to fetch any prerequisite types we may need to build this DIE type
20358 before building the type and calling set_die_type.
20359 * After building type call set_die_type for current DIE as soon as
20360 possible before fetching more types to complete the current type.
20361 * Make the type as complete as possible before fetching more types. */
20363 static struct type *
20364 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20366 struct dwarf2_per_cu_offset_and_type **slot, ofs;
20367 struct objfile *objfile = cu->objfile;
20369 /* For Ada types, make sure that the gnat-specific data is always
20370 initialized (if not already set). There are a few types where
20371 we should not be doing so, because the type-specific area is
20372 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20373 where the type-specific area is used to store the floatformat).
20374 But this is not a problem, because the gnat-specific information
20375 is actually not needed for these types. */
20376 if (need_gnat_info (cu)
20377 && TYPE_CODE (type) != TYPE_CODE_FUNC
20378 && TYPE_CODE (type) != TYPE_CODE_FLT
20379 && !HAVE_GNAT_AUX_INFO (type))
20380 INIT_GNAT_SPECIFIC (type);
20382 if (dwarf2_per_objfile->die_type_hash == NULL)
20384 dwarf2_per_objfile->die_type_hash =
20385 htab_create_alloc_ex (127,
20386 per_cu_offset_and_type_hash,
20387 per_cu_offset_and_type_eq,
20389 &objfile->objfile_obstack,
20390 hashtab_obstack_allocate,
20391 dummy_obstack_deallocate);
20394 ofs.per_cu = cu->per_cu;
20395 ofs.offset = die->offset;
20397 slot = (struct dwarf2_per_cu_offset_and_type **)
20398 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
20400 complaint (&symfile_complaints,
20401 _("A problem internal to GDB: DIE 0x%x has type already set"),
20402 die->offset.sect_off);
20403 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
20408 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20409 or return NULL if the die does not have a saved type. */
20411 static struct type *
20412 get_die_type_at_offset (sect_offset offset,
20413 struct dwarf2_per_cu_data *per_cu)
20415 struct dwarf2_per_cu_offset_and_type *slot, ofs;
20417 if (dwarf2_per_objfile->die_type_hash == NULL)
20420 ofs.per_cu = per_cu;
20421 ofs.offset = offset;
20422 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
20429 /* Look up the type for DIE in CU in die_type_hash,
20430 or return NULL if DIE does not have a saved type. */
20432 static struct type *
20433 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20435 return get_die_type_at_offset (die->offset, cu->per_cu);
20438 /* Add a dependence relationship from CU to REF_PER_CU. */
20441 dwarf2_add_dependence (struct dwarf2_cu *cu,
20442 struct dwarf2_per_cu_data *ref_per_cu)
20446 if (cu->dependencies == NULL)
20448 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20449 NULL, &cu->comp_unit_obstack,
20450 hashtab_obstack_allocate,
20451 dummy_obstack_deallocate);
20453 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20455 *slot = ref_per_cu;
20458 /* Subroutine of dwarf2_mark to pass to htab_traverse.
20459 Set the mark field in every compilation unit in the
20460 cache that we must keep because we are keeping CU. */
20463 dwarf2_mark_helper (void **slot, void *data)
20465 struct dwarf2_per_cu_data *per_cu;
20467 per_cu = (struct dwarf2_per_cu_data *) *slot;
20469 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20470 reading of the chain. As such dependencies remain valid it is not much
20471 useful to track and undo them during QUIT cleanups. */
20472 if (per_cu->cu == NULL)
20475 if (per_cu->cu->mark)
20477 per_cu->cu->mark = 1;
20479 if (per_cu->cu->dependencies != NULL)
20480 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20485 /* Set the mark field in CU and in every other compilation unit in the
20486 cache that we must keep because we are keeping CU. */
20489 dwarf2_mark (struct dwarf2_cu *cu)
20494 if (cu->dependencies != NULL)
20495 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
20499 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20503 per_cu->cu->mark = 0;
20504 per_cu = per_cu->cu->read_in_chain;
20508 /* Trivial hash function for partial_die_info: the hash value of a DIE
20509 is its offset in .debug_info for this objfile. */
20512 partial_die_hash (const void *item)
20514 const struct partial_die_info *part_die = item;
20516 return part_die->offset.sect_off;
20519 /* Trivial comparison function for partial_die_info structures: two DIEs
20520 are equal if they have the same offset. */
20523 partial_die_eq (const void *item_lhs, const void *item_rhs)
20525 const struct partial_die_info *part_die_lhs = item_lhs;
20526 const struct partial_die_info *part_die_rhs = item_rhs;
20528 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
20531 static struct cmd_list_element *set_dwarf2_cmdlist;
20532 static struct cmd_list_element *show_dwarf2_cmdlist;
20535 set_dwarf2_cmd (char *args, int from_tty)
20537 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20541 show_dwarf2_cmd (char *args, int from_tty)
20543 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20546 /* Free data associated with OBJFILE, if necessary. */
20549 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
20551 struct dwarf2_per_objfile *data = d;
20554 /* Make sure we don't accidentally use dwarf2_per_objfile while
20556 dwarf2_per_objfile = NULL;
20558 for (ix = 0; ix < data->n_comp_units; ++ix)
20559 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
20561 for (ix = 0; ix < data->n_type_units; ++ix)
20562 VEC_free (dwarf2_per_cu_ptr,
20563 data->all_type_units[ix]->per_cu.imported_symtabs);
20564 xfree (data->all_type_units);
20566 VEC_free (dwarf2_section_info_def, data->types);
20568 if (data->dwo_files)
20569 free_dwo_files (data->dwo_files, objfile);
20570 if (data->dwp_file)
20571 gdb_bfd_unref (data->dwp_file->dbfd);
20573 if (data->dwz_file && data->dwz_file->dwz_bfd)
20574 gdb_bfd_unref (data->dwz_file->dwz_bfd);
20578 /* The "save gdb-index" command. */
20580 /* The contents of the hash table we create when building the string
20582 struct strtab_entry
20584 offset_type offset;
20588 /* Hash function for a strtab_entry.
20590 Function is used only during write_hash_table so no index format backward
20591 compatibility is needed. */
20594 hash_strtab_entry (const void *e)
20596 const struct strtab_entry *entry = e;
20597 return mapped_index_string_hash (INT_MAX, entry->str);
20600 /* Equality function for a strtab_entry. */
20603 eq_strtab_entry (const void *a, const void *b)
20605 const struct strtab_entry *ea = a;
20606 const struct strtab_entry *eb = b;
20607 return !strcmp (ea->str, eb->str);
20610 /* Create a strtab_entry hash table. */
20613 create_strtab (void)
20615 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20616 xfree, xcalloc, xfree);
20619 /* Add a string to the constant pool. Return the string's offset in
20623 add_string (htab_t table, struct obstack *cpool, const char *str)
20626 struct strtab_entry entry;
20627 struct strtab_entry *result;
20630 slot = htab_find_slot (table, &entry, INSERT);
20635 result = XNEW (struct strtab_entry);
20636 result->offset = obstack_object_size (cpool);
20638 obstack_grow_str0 (cpool, str);
20641 return result->offset;
20644 /* An entry in the symbol table. */
20645 struct symtab_index_entry
20647 /* The name of the symbol. */
20649 /* The offset of the name in the constant pool. */
20650 offset_type index_offset;
20651 /* A sorted vector of the indices of all the CUs that hold an object
20653 VEC (offset_type) *cu_indices;
20656 /* The symbol table. This is a power-of-2-sized hash table. */
20657 struct mapped_symtab
20659 offset_type n_elements;
20661 struct symtab_index_entry **data;
20664 /* Hash function for a symtab_index_entry. */
20667 hash_symtab_entry (const void *e)
20669 const struct symtab_index_entry *entry = e;
20670 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20671 sizeof (offset_type) * VEC_length (offset_type,
20672 entry->cu_indices),
20676 /* Equality function for a symtab_index_entry. */
20679 eq_symtab_entry (const void *a, const void *b)
20681 const struct symtab_index_entry *ea = a;
20682 const struct symtab_index_entry *eb = b;
20683 int len = VEC_length (offset_type, ea->cu_indices);
20684 if (len != VEC_length (offset_type, eb->cu_indices))
20686 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20687 VEC_address (offset_type, eb->cu_indices),
20688 sizeof (offset_type) * len);
20691 /* Destroy a symtab_index_entry. */
20694 delete_symtab_entry (void *p)
20696 struct symtab_index_entry *entry = p;
20697 VEC_free (offset_type, entry->cu_indices);
20701 /* Create a hash table holding symtab_index_entry objects. */
20704 create_symbol_hash_table (void)
20706 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20707 delete_symtab_entry, xcalloc, xfree);
20710 /* Create a new mapped symtab object. */
20712 static struct mapped_symtab *
20713 create_mapped_symtab (void)
20715 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20716 symtab->n_elements = 0;
20717 symtab->size = 1024;
20718 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20722 /* Destroy a mapped_symtab. */
20725 cleanup_mapped_symtab (void *p)
20727 struct mapped_symtab *symtab = p;
20728 /* The contents of the array are freed when the other hash table is
20730 xfree (symtab->data);
20734 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20737 Function is used only during write_hash_table so no index format backward
20738 compatibility is needed. */
20740 static struct symtab_index_entry **
20741 find_slot (struct mapped_symtab *symtab, const char *name)
20743 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20745 index = hash & (symtab->size - 1);
20746 step = ((hash * 17) & (symtab->size - 1)) | 1;
20750 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20751 return &symtab->data[index];
20752 index = (index + step) & (symtab->size - 1);
20756 /* Expand SYMTAB's hash table. */
20759 hash_expand (struct mapped_symtab *symtab)
20761 offset_type old_size = symtab->size;
20763 struct symtab_index_entry **old_entries = symtab->data;
20766 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20768 for (i = 0; i < old_size; ++i)
20770 if (old_entries[i])
20772 struct symtab_index_entry **slot = find_slot (symtab,
20773 old_entries[i]->name);
20774 *slot = old_entries[i];
20778 xfree (old_entries);
20781 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20782 CU_INDEX is the index of the CU in which the symbol appears.
20783 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20786 add_index_entry (struct mapped_symtab *symtab, const char *name,
20787 int is_static, gdb_index_symbol_kind kind,
20788 offset_type cu_index)
20790 struct symtab_index_entry **slot;
20791 offset_type cu_index_and_attrs;
20793 ++symtab->n_elements;
20794 if (4 * symtab->n_elements / 3 >= symtab->size)
20795 hash_expand (symtab);
20797 slot = find_slot (symtab, name);
20800 *slot = XNEW (struct symtab_index_entry);
20801 (*slot)->name = name;
20802 /* index_offset is set later. */
20803 (*slot)->cu_indices = NULL;
20806 cu_index_and_attrs = 0;
20807 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20808 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20809 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20811 /* We don't want to record an index value twice as we want to avoid the
20813 We process all global symbols and then all static symbols
20814 (which would allow us to avoid the duplication by only having to check
20815 the last entry pushed), but a symbol could have multiple kinds in one CU.
20816 To keep things simple we don't worry about the duplication here and
20817 sort and uniqufy the list after we've processed all symbols. */
20818 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20821 /* qsort helper routine for uniquify_cu_indices. */
20824 offset_type_compare (const void *ap, const void *bp)
20826 offset_type a = *(offset_type *) ap;
20827 offset_type b = *(offset_type *) bp;
20829 return (a > b) - (b > a);
20832 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20835 uniquify_cu_indices (struct mapped_symtab *symtab)
20839 for (i = 0; i < symtab->size; ++i)
20841 struct symtab_index_entry *entry = symtab->data[i];
20844 && entry->cu_indices != NULL)
20846 unsigned int next_to_insert, next_to_check;
20847 offset_type last_value;
20849 qsort (VEC_address (offset_type, entry->cu_indices),
20850 VEC_length (offset_type, entry->cu_indices),
20851 sizeof (offset_type), offset_type_compare);
20853 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20854 next_to_insert = 1;
20855 for (next_to_check = 1;
20856 next_to_check < VEC_length (offset_type, entry->cu_indices);
20859 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20862 last_value = VEC_index (offset_type, entry->cu_indices,
20864 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20869 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20874 /* Add a vector of indices to the constant pool. */
20877 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20878 struct symtab_index_entry *entry)
20882 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20885 offset_type len = VEC_length (offset_type, entry->cu_indices);
20886 offset_type val = MAYBE_SWAP (len);
20891 entry->index_offset = obstack_object_size (cpool);
20893 obstack_grow (cpool, &val, sizeof (val));
20895 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20898 val = MAYBE_SWAP (iter);
20899 obstack_grow (cpool, &val, sizeof (val));
20904 struct symtab_index_entry *old_entry = *slot;
20905 entry->index_offset = old_entry->index_offset;
20908 return entry->index_offset;
20911 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20912 constant pool entries going into the obstack CPOOL. */
20915 write_hash_table (struct mapped_symtab *symtab,
20916 struct obstack *output, struct obstack *cpool)
20919 htab_t symbol_hash_table;
20922 symbol_hash_table = create_symbol_hash_table ();
20923 str_table = create_strtab ();
20925 /* We add all the index vectors to the constant pool first, to
20926 ensure alignment is ok. */
20927 for (i = 0; i < symtab->size; ++i)
20929 if (symtab->data[i])
20930 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20933 /* Now write out the hash table. */
20934 for (i = 0; i < symtab->size; ++i)
20936 offset_type str_off, vec_off;
20938 if (symtab->data[i])
20940 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20941 vec_off = symtab->data[i]->index_offset;
20945 /* While 0 is a valid constant pool index, it is not valid
20946 to have 0 for both offsets. */
20951 str_off = MAYBE_SWAP (str_off);
20952 vec_off = MAYBE_SWAP (vec_off);
20954 obstack_grow (output, &str_off, sizeof (str_off));
20955 obstack_grow (output, &vec_off, sizeof (vec_off));
20958 htab_delete (str_table);
20959 htab_delete (symbol_hash_table);
20962 /* Struct to map psymtab to CU index in the index file. */
20963 struct psymtab_cu_index_map
20965 struct partial_symtab *psymtab;
20966 unsigned int cu_index;
20970 hash_psymtab_cu_index (const void *item)
20972 const struct psymtab_cu_index_map *map = item;
20974 return htab_hash_pointer (map->psymtab);
20978 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20980 const struct psymtab_cu_index_map *lhs = item_lhs;
20981 const struct psymtab_cu_index_map *rhs = item_rhs;
20983 return lhs->psymtab == rhs->psymtab;
20986 /* Helper struct for building the address table. */
20987 struct addrmap_index_data
20989 struct objfile *objfile;
20990 struct obstack *addr_obstack;
20991 htab_t cu_index_htab;
20993 /* Non-zero if the previous_* fields are valid.
20994 We can't write an entry until we see the next entry (since it is only then
20995 that we know the end of the entry). */
20996 int previous_valid;
20997 /* Index of the CU in the table of all CUs in the index file. */
20998 unsigned int previous_cu_index;
20999 /* Start address of the CU. */
21000 CORE_ADDR previous_cu_start;
21003 /* Write an address entry to OBSTACK. */
21006 add_address_entry (struct objfile *objfile, struct obstack *obstack,
21007 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
21009 offset_type cu_index_to_write;
21011 CORE_ADDR baseaddr;
21013 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21015 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21016 obstack_grow (obstack, addr, 8);
21017 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21018 obstack_grow (obstack, addr, 8);
21019 cu_index_to_write = MAYBE_SWAP (cu_index);
21020 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21023 /* Worker function for traversing an addrmap to build the address table. */
21026 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21028 struct addrmap_index_data *data = datap;
21029 struct partial_symtab *pst = obj;
21031 if (data->previous_valid)
21032 add_address_entry (data->objfile, data->addr_obstack,
21033 data->previous_cu_start, start_addr,
21034 data->previous_cu_index);
21036 data->previous_cu_start = start_addr;
21039 struct psymtab_cu_index_map find_map, *map;
21040 find_map.psymtab = pst;
21041 map = htab_find (data->cu_index_htab, &find_map);
21042 gdb_assert (map != NULL);
21043 data->previous_cu_index = map->cu_index;
21044 data->previous_valid = 1;
21047 data->previous_valid = 0;
21052 /* Write OBJFILE's address map to OBSTACK.
21053 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21054 in the index file. */
21057 write_address_map (struct objfile *objfile, struct obstack *obstack,
21058 htab_t cu_index_htab)
21060 struct addrmap_index_data addrmap_index_data;
21062 /* When writing the address table, we have to cope with the fact that
21063 the addrmap iterator only provides the start of a region; we have to
21064 wait until the next invocation to get the start of the next region. */
21066 addrmap_index_data.objfile = objfile;
21067 addrmap_index_data.addr_obstack = obstack;
21068 addrmap_index_data.cu_index_htab = cu_index_htab;
21069 addrmap_index_data.previous_valid = 0;
21071 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21072 &addrmap_index_data);
21074 /* It's highly unlikely the last entry (end address = 0xff...ff)
21075 is valid, but we should still handle it.
21076 The end address is recorded as the start of the next region, but that
21077 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21079 if (addrmap_index_data.previous_valid)
21080 add_address_entry (objfile, obstack,
21081 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21082 addrmap_index_data.previous_cu_index);
21085 /* Return the symbol kind of PSYM. */
21087 static gdb_index_symbol_kind
21088 symbol_kind (struct partial_symbol *psym)
21090 domain_enum domain = PSYMBOL_DOMAIN (psym);
21091 enum address_class aclass = PSYMBOL_CLASS (psym);
21099 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21101 return GDB_INDEX_SYMBOL_KIND_TYPE;
21103 case LOC_CONST_BYTES:
21104 case LOC_OPTIMIZED_OUT:
21106 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21108 /* Note: It's currently impossible to recognize psyms as enum values
21109 short of reading the type info. For now punt. */
21110 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21112 /* There are other LOC_FOO values that one might want to classify
21113 as variables, but dwarf2read.c doesn't currently use them. */
21114 return GDB_INDEX_SYMBOL_KIND_OTHER;
21116 case STRUCT_DOMAIN:
21117 return GDB_INDEX_SYMBOL_KIND_TYPE;
21119 return GDB_INDEX_SYMBOL_KIND_OTHER;
21123 /* Add a list of partial symbols to SYMTAB. */
21126 write_psymbols (struct mapped_symtab *symtab,
21128 struct partial_symbol **psymp,
21130 offset_type cu_index,
21133 for (; count-- > 0; ++psymp)
21135 struct partial_symbol *psym = *psymp;
21138 if (SYMBOL_LANGUAGE (psym) == language_ada)
21139 error (_("Ada is not currently supported by the index"));
21141 /* Only add a given psymbol once. */
21142 slot = htab_find_slot (psyms_seen, psym, INSERT);
21145 gdb_index_symbol_kind kind = symbol_kind (psym);
21148 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21149 is_static, kind, cu_index);
21154 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
21155 exception if there is an error. */
21158 write_obstack (FILE *file, struct obstack *obstack)
21160 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21162 != obstack_object_size (obstack))
21163 error (_("couldn't data write to file"));
21166 /* Unlink a file if the argument is not NULL. */
21169 unlink_if_set (void *p)
21171 char **filename = p;
21173 unlink (*filename);
21176 /* A helper struct used when iterating over debug_types. */
21177 struct signatured_type_index_data
21179 struct objfile *objfile;
21180 struct mapped_symtab *symtab;
21181 struct obstack *types_list;
21186 /* A helper function that writes a single signatured_type to an
21190 write_one_signatured_type (void **slot, void *d)
21192 struct signatured_type_index_data *info = d;
21193 struct signatured_type *entry = (struct signatured_type *) *slot;
21194 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
21197 write_psymbols (info->symtab,
21199 info->objfile->global_psymbols.list
21200 + psymtab->globals_offset,
21201 psymtab->n_global_syms, info->cu_index,
21203 write_psymbols (info->symtab,
21205 info->objfile->static_psymbols.list
21206 + psymtab->statics_offset,
21207 psymtab->n_static_syms, info->cu_index,
21210 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21211 entry->per_cu.offset.sect_off);
21212 obstack_grow (info->types_list, val, 8);
21213 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21214 entry->type_offset_in_tu.cu_off);
21215 obstack_grow (info->types_list, val, 8);
21216 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21217 obstack_grow (info->types_list, val, 8);
21224 /* Recurse into all "included" dependencies and write their symbols as
21225 if they appeared in this psymtab. */
21228 recursively_write_psymbols (struct objfile *objfile,
21229 struct partial_symtab *psymtab,
21230 struct mapped_symtab *symtab,
21232 offset_type cu_index)
21236 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21237 if (psymtab->dependencies[i]->user != NULL)
21238 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21239 symtab, psyms_seen, cu_index);
21241 write_psymbols (symtab,
21243 objfile->global_psymbols.list + psymtab->globals_offset,
21244 psymtab->n_global_syms, cu_index,
21246 write_psymbols (symtab,
21248 objfile->static_psymbols.list + psymtab->statics_offset,
21249 psymtab->n_static_syms, cu_index,
21253 /* Create an index file for OBJFILE in the directory DIR. */
21256 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21258 struct cleanup *cleanup;
21259 char *filename, *cleanup_filename;
21260 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21261 struct obstack cu_list, types_cu_list;
21264 struct mapped_symtab *symtab;
21265 offset_type val, size_of_contents, total_len;
21268 htab_t cu_index_htab;
21269 struct psymtab_cu_index_map *psymtab_cu_index_map;
21271 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21274 if (dwarf2_per_objfile->using_index)
21275 error (_("Cannot use an index to create the index"));
21277 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21278 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21280 if (stat (objfile->name, &st) < 0)
21281 perror_with_name (objfile->name);
21283 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21284 INDEX_SUFFIX, (char *) NULL);
21285 cleanup = make_cleanup (xfree, filename);
21287 out_file = gdb_fopen_cloexec (filename, "wb");
21289 error (_("Can't open `%s' for writing"), filename);
21291 cleanup_filename = filename;
21292 make_cleanup (unlink_if_set, &cleanup_filename);
21294 symtab = create_mapped_symtab ();
21295 make_cleanup (cleanup_mapped_symtab, symtab);
21297 obstack_init (&addr_obstack);
21298 make_cleanup_obstack_free (&addr_obstack);
21300 obstack_init (&cu_list);
21301 make_cleanup_obstack_free (&cu_list);
21303 obstack_init (&types_cu_list);
21304 make_cleanup_obstack_free (&types_cu_list);
21306 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21307 NULL, xcalloc, xfree);
21308 make_cleanup_htab_delete (psyms_seen);
21310 /* While we're scanning CU's create a table that maps a psymtab pointer
21311 (which is what addrmap records) to its index (which is what is recorded
21312 in the index file). This will later be needed to write the address
21314 cu_index_htab = htab_create_alloc (100,
21315 hash_psymtab_cu_index,
21316 eq_psymtab_cu_index,
21317 NULL, xcalloc, xfree);
21318 make_cleanup_htab_delete (cu_index_htab);
21319 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21320 xmalloc (sizeof (struct psymtab_cu_index_map)
21321 * dwarf2_per_objfile->n_comp_units);
21322 make_cleanup (xfree, psymtab_cu_index_map);
21324 /* The CU list is already sorted, so we don't need to do additional
21325 work here. Also, the debug_types entries do not appear in
21326 all_comp_units, but only in their own hash table. */
21327 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21329 struct dwarf2_per_cu_data *per_cu
21330 = dwarf2_per_objfile->all_comp_units[i];
21331 struct partial_symtab *psymtab = per_cu->v.psymtab;
21333 struct psymtab_cu_index_map *map;
21336 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21337 It may be referenced from a local scope but in such case it does not
21338 need to be present in .gdb_index. */
21339 if (psymtab == NULL)
21342 if (psymtab->user == NULL)
21343 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
21345 map = &psymtab_cu_index_map[i];
21346 map->psymtab = psymtab;
21348 slot = htab_find_slot (cu_index_htab, map, INSERT);
21349 gdb_assert (slot != NULL);
21350 gdb_assert (*slot == NULL);
21353 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21354 per_cu->offset.sect_off);
21355 obstack_grow (&cu_list, val, 8);
21356 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
21357 obstack_grow (&cu_list, val, 8);
21360 /* Dump the address map. */
21361 write_address_map (objfile, &addr_obstack, cu_index_htab);
21363 /* Write out the .debug_type entries, if any. */
21364 if (dwarf2_per_objfile->signatured_types)
21366 struct signatured_type_index_data sig_data;
21368 sig_data.objfile = objfile;
21369 sig_data.symtab = symtab;
21370 sig_data.types_list = &types_cu_list;
21371 sig_data.psyms_seen = psyms_seen;
21372 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21373 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21374 write_one_signatured_type, &sig_data);
21377 /* Now that we've processed all symbols we can shrink their cu_indices
21379 uniquify_cu_indices (symtab);
21381 obstack_init (&constant_pool);
21382 make_cleanup_obstack_free (&constant_pool);
21383 obstack_init (&symtab_obstack);
21384 make_cleanup_obstack_free (&symtab_obstack);
21385 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21387 obstack_init (&contents);
21388 make_cleanup_obstack_free (&contents);
21389 size_of_contents = 6 * sizeof (offset_type);
21390 total_len = size_of_contents;
21392 /* The version number. */
21393 val = MAYBE_SWAP (8);
21394 obstack_grow (&contents, &val, sizeof (val));
21396 /* The offset of the CU list from the start of the file. */
21397 val = MAYBE_SWAP (total_len);
21398 obstack_grow (&contents, &val, sizeof (val));
21399 total_len += obstack_object_size (&cu_list);
21401 /* The offset of the types CU list from the start of the file. */
21402 val = MAYBE_SWAP (total_len);
21403 obstack_grow (&contents, &val, sizeof (val));
21404 total_len += obstack_object_size (&types_cu_list);
21406 /* The offset of the address table 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 (&addr_obstack);
21411 /* The offset of the symbol table 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 (&symtab_obstack);
21416 /* The offset of the constant pool 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 (&constant_pool);
21421 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21423 write_obstack (out_file, &contents);
21424 write_obstack (out_file, &cu_list);
21425 write_obstack (out_file, &types_cu_list);
21426 write_obstack (out_file, &addr_obstack);
21427 write_obstack (out_file, &symtab_obstack);
21428 write_obstack (out_file, &constant_pool);
21432 /* We want to keep the file, so we set cleanup_filename to NULL
21433 here. See unlink_if_set. */
21434 cleanup_filename = NULL;
21436 do_cleanups (cleanup);
21439 /* Implementation of the `save gdb-index' command.
21441 Note that the file format used by this command is documented in the
21442 GDB manual. Any changes here must be documented there. */
21445 save_gdb_index_command (char *arg, int from_tty)
21447 struct objfile *objfile;
21450 error (_("usage: save gdb-index DIRECTORY"));
21452 ALL_OBJFILES (objfile)
21456 /* If the objfile does not correspond to an actual file, skip it. */
21457 if (stat (objfile->name, &st) < 0)
21460 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21461 if (dwarf2_per_objfile)
21463 volatile struct gdb_exception except;
21465 TRY_CATCH (except, RETURN_MASK_ERROR)
21467 write_psymtabs_to_index (objfile, arg);
21469 if (except.reason < 0)
21470 exception_fprintf (gdb_stderr, except,
21471 _("Error while writing index for `%s': "),
21479 int dwarf2_always_disassemble;
21482 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21483 struct cmd_list_element *c, const char *value)
21485 fprintf_filtered (file,
21486 _("Whether to always disassemble "
21487 "DWARF expressions is %s.\n"),
21492 show_check_physname (struct ui_file *file, int from_tty,
21493 struct cmd_list_element *c, const char *value)
21495 fprintf_filtered (file,
21496 _("Whether to check \"physname\" is %s.\n"),
21500 void _initialize_dwarf2_read (void);
21503 _initialize_dwarf2_read (void)
21505 struct cmd_list_element *c;
21507 dwarf2_objfile_data_key
21508 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
21510 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21511 Set DWARF 2 specific variables.\n\
21512 Configure DWARF 2 variables such as the cache size"),
21513 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21514 0/*allow-unknown*/, &maintenance_set_cmdlist);
21516 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21517 Show DWARF 2 specific variables\n\
21518 Show DWARF 2 variables such as the cache size"),
21519 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21520 0/*allow-unknown*/, &maintenance_show_cmdlist);
21522 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21523 &dwarf2_max_cache_age, _("\
21524 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21525 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21526 A higher limit means that cached compilation units will be stored\n\
21527 in memory longer, and more total memory will be used. Zero disables\n\
21528 caching, which can slow down startup."),
21530 show_dwarf2_max_cache_age,
21531 &set_dwarf2_cmdlist,
21532 &show_dwarf2_cmdlist);
21534 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21535 &dwarf2_always_disassemble, _("\
21536 Set whether `info address' always disassembles DWARF expressions."), _("\
21537 Show whether `info address' always disassembles DWARF expressions."), _("\
21538 When enabled, DWARF expressions are always printed in an assembly-like\n\
21539 syntax. When disabled, expressions will be printed in a more\n\
21540 conversational style, when possible."),
21542 show_dwarf2_always_disassemble,
21543 &set_dwarf2_cmdlist,
21544 &show_dwarf2_cmdlist);
21546 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21547 Set debugging of the dwarf2 reader."), _("\
21548 Show debugging of the dwarf2 reader."), _("\
21549 When enabled, debugging messages are printed during dwarf2 reading\n\
21550 and symtab expansion."),
21553 &setdebuglist, &showdebuglist);
21555 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
21556 Set debugging of the dwarf2 DIE reader."), _("\
21557 Show debugging of the dwarf2 DIE reader."), _("\
21558 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21559 The value is the maximum depth to print."),
21562 &setdebuglist, &showdebuglist);
21564 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21565 Set cross-checking of \"physname\" code against demangler."), _("\
21566 Show cross-checking of \"physname\" code against demangler."), _("\
21567 When enabled, GDB's internal \"physname\" code is checked against\n\
21569 NULL, show_check_physname,
21570 &setdebuglist, &showdebuglist);
21572 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21573 no_class, &use_deprecated_index_sections, _("\
21574 Set whether to use deprecated gdb_index sections."), _("\
21575 Show whether to use deprecated gdb_index sections."), _("\
21576 When enabled, deprecated .gdb_index sections are used anyway.\n\
21577 Normally they are ignored either because of a missing feature or\n\
21578 performance issue.\n\
21579 Warning: This option must be enabled before gdb reads the file."),
21582 &setlist, &showlist);
21584 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
21586 Save a gdb-index file.\n\
21587 Usage: save gdb-index DIRECTORY"),
21589 set_cmd_completer (c, filename_completer);
21591 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21592 &dwarf2_locexpr_funcs);
21593 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21594 &dwarf2_loclist_funcs);
21596 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21597 &dwarf2_block_frame_base_locexpr_funcs);
21598 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21599 &dwarf2_block_frame_base_loclist_funcs);